* opts.c (finish_options): Remove duplicate sorry.
[official-gcc.git] / gcc / ada / sem_ch12.adb
blob3557ed813c37071e22cf385c08c12028704fce75
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-2011, 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 Hostparm;
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_Res; use Sem_Res;
63 with Sem_Type; use Sem_Type;
64 with Sem_Util; use Sem_Util;
65 with Sem_Warn; use Sem_Warn;
66 with Stand; use Stand;
67 with Sinfo; use Sinfo;
68 with Sinfo.CN; use Sinfo.CN;
69 with Sinput; use Sinput;
70 with Sinput.L; use Sinput.L;
71 with Snames; use Snames;
72 with Stringt; use Stringt;
73 with Uname; use Uname;
74 with Table;
75 with Tbuild; use Tbuild;
76 with Uintp; use Uintp;
77 with Urealp; use Urealp;
79 with GNAT.HTable;
81 package body Sem_Ch12 is
83 ----------------------------------------------------------
84 -- Implementation of Generic Analysis and Instantiation --
85 ----------------------------------------------------------
87 -- GNAT implements generics by macro expansion. No attempt is made to share
88 -- generic instantiations (for now). Analysis of a generic definition does
89 -- not perform any expansion action, but the expander must be called on the
90 -- tree for each instantiation, because the expansion may of course depend
91 -- on the generic actuals. All of this is best achieved as follows:
93 -- a) Semantic analysis of a generic unit is performed on a copy of the
94 -- tree for the generic unit. All tree modifications that follow analysis
95 -- do not affect the original tree. Links are kept between the original
96 -- tree and the copy, in order to recognize non-local references within
97 -- the generic, and propagate them to each instance (recall that name
98 -- resolution is done on the generic declaration: generics are not really
99 -- macros!). This is summarized in the following diagram:
101 -- .-----------. .----------.
102 -- | semantic |<--------------| generic |
103 -- | copy | | unit |
104 -- | |==============>| |
105 -- |___________| global |__________|
106 -- references | | |
107 -- | | |
108 -- .-----|--|.
109 -- | .-----|---.
110 -- | | .----------.
111 -- | | | generic |
112 -- |__| | |
113 -- |__| instance |
114 -- |__________|
116 -- b) Each instantiation copies the original tree, and inserts into it a
117 -- series of declarations that describe the mapping between generic formals
118 -- and actuals. For example, a generic In OUT parameter is an object
119 -- renaming of the corresponding actual, etc. Generic IN parameters are
120 -- constant declarations.
122 -- c) In order to give the right visibility for these renamings, we use
123 -- a different scheme for package and subprogram instantiations. For
124 -- packages, the list of renamings is inserted into the package
125 -- specification, before the visible declarations of the package. The
126 -- renamings are analyzed before any of the text of the instance, and are
127 -- thus visible at the right place. Furthermore, outside of the instance,
128 -- the generic parameters are visible and denote their corresponding
129 -- actuals.
131 -- For subprograms, we create a container package to hold the renamings
132 -- and the subprogram instance itself. Analysis of the package makes the
133 -- renaming declarations visible to the subprogram. After analyzing the
134 -- package, the defining entity for the subprogram is touched-up so that
135 -- it appears declared in the current scope, and not inside the container
136 -- package.
138 -- If the instantiation is a compilation unit, the container package is
139 -- given the same name as the subprogram instance. This ensures that
140 -- the elaboration procedure called by the binder, using the compilation
141 -- unit name, calls in fact the elaboration procedure for the package.
143 -- Not surprisingly, private types complicate this approach. By saving in
144 -- the original generic object the non-local references, we guarantee that
145 -- the proper entities are referenced at the point of instantiation.
146 -- However, for private types, this by itself does not insure that the
147 -- proper VIEW of the entity is used (the full type may be visible at the
148 -- point of generic definition, but not at instantiation, or vice-versa).
149 -- In order to reference the proper view, we special-case any reference
150 -- to private types in the generic object, by saving both views, one in
151 -- the generic and one in the semantic copy. At time of instantiation, we
152 -- check whether the two views are consistent, and exchange declarations if
153 -- necessary, in order to restore the correct visibility. Similarly, if
154 -- the instance view is private when the generic view was not, we perform
155 -- the exchange. After completing the instantiation, we restore the
156 -- current visibility. The flag Has_Private_View marks identifiers in the
157 -- the generic unit that require checking.
159 -- Visibility within nested generic units requires special handling.
160 -- Consider the following scheme:
162 -- type Global is ... -- outside of generic unit.
163 -- generic ...
164 -- package Outer is
165 -- ...
166 -- type Semi_Global is ... -- global to inner.
168 -- generic ... -- 1
169 -- procedure inner (X1 : Global; X2 : Semi_Global);
171 -- procedure in2 is new inner (...); -- 4
172 -- end Outer;
174 -- package New_Outer is new Outer (...); -- 2
175 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
177 -- The semantic analysis of Outer captures all occurrences of Global.
178 -- The semantic analysis of Inner (at 1) captures both occurrences of
179 -- Global and Semi_Global.
181 -- At point 2 (instantiation of Outer), we also produce a generic copy
182 -- of Inner, even though Inner is, at that point, not being instantiated.
183 -- (This is just part of the semantic analysis of New_Outer).
185 -- Critically, references to Global within Inner must be preserved, while
186 -- references to Semi_Global should not preserved, because they must now
187 -- resolve to an entity within New_Outer. To distinguish between these, we
188 -- use a global variable, Current_Instantiated_Parent, which is set when
189 -- performing a generic copy during instantiation (at 2). This variable is
190 -- used when performing a generic copy that is not an instantiation, but
191 -- that is nested within one, as the occurrence of 1 within 2. The analysis
192 -- of a nested generic only preserves references that are global to the
193 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
194 -- determine whether a reference is external to the given parent.
196 -- The instantiation at point 3 requires no special treatment. The method
197 -- works as well for further nestings of generic units, but of course the
198 -- variable Current_Instantiated_Parent must be stacked because nested
199 -- instantiations can occur, e.g. the occurrence of 4 within 2.
201 -- The instantiation of package and subprogram bodies is handled in a
202 -- similar manner, except that it is delayed until after semantic
203 -- analysis is complete. In this fashion complex cross-dependencies
204 -- between several package declarations and bodies containing generics
205 -- can be compiled which otherwise would diagnose spurious circularities.
207 -- For example, it is possible to compile two packages A and B that
208 -- have the following structure:
210 -- package A is package B is
211 -- generic ... generic ...
212 -- package G_A is package G_B is
214 -- with B; with A;
215 -- package body A is package body B is
216 -- package N_B is new G_B (..) package N_A is new G_A (..)
218 -- The table Pending_Instantiations in package Inline is used to keep
219 -- track of body instantiations that are delayed in this manner. Inline
220 -- handles the actual calls to do the body instantiations. This activity
221 -- is part of Inline, since the processing occurs at the same point, and
222 -- for essentially the same reason, as the handling of inlined routines.
224 ----------------------------------------------
225 -- Detection of Instantiation Circularities --
226 ----------------------------------------------
228 -- If we have a chain of instantiations that is circular, this is static
229 -- error which must be detected at compile time. The detection of these
230 -- circularities is carried out at the point that we insert a generic
231 -- instance spec or body. If there is a circularity, then the analysis of
232 -- the offending spec or body will eventually result in trying to load the
233 -- same unit again, and we detect this problem as we analyze the package
234 -- instantiation for the second time.
236 -- At least in some cases after we have detected the circularity, we get
237 -- into trouble if we try to keep going. The following flag is set if a
238 -- circularity is detected, and used to abandon compilation after the
239 -- messages have been posted.
241 Circularity_Detected : Boolean := False;
242 -- This should really be reset on encountering a new main unit, but in
243 -- practice we are not using multiple main units so it is not critical.
245 -------------------------------------------------
246 -- Formal packages and partial parametrization --
247 -------------------------------------------------
249 -- When compiling a generic, a formal package is a local instantiation. If
250 -- declared with a box, its generic formals are visible in the enclosing
251 -- generic. If declared with a partial list of actuals, those actuals that
252 -- are defaulted (covered by an Others clause, or given an explicit box
253 -- initialization) are also visible in the enclosing generic, while those
254 -- that have a corresponding actual are not.
256 -- In our source model of instantiation, the same visibility must be
257 -- present in the spec and body of an instance: the names of the formals
258 -- that are defaulted must be made visible within the instance, and made
259 -- invisible (hidden) after the instantiation is complete, so that they
260 -- are not accessible outside of the instance.
262 -- In a generic, a formal package is treated like a special instantiation.
263 -- Our Ada 95 compiler handled formals with and without box in different
264 -- ways. With partial parametrization, we use a single model for both.
265 -- We create a package declaration that consists of the specification of
266 -- the generic package, and a set of declarations that map the actuals
267 -- into local renamings, just as we do for bona fide instantiations. For
268 -- defaulted parameters and formals with a box, we copy directly the
269 -- declarations of the formal into this local package. The result is a
270 -- a package whose visible declarations may include generic formals. This
271 -- package is only used for type checking and visibility analysis, and
272 -- never reaches the back-end, so it can freely violate the placement
273 -- rules for generic formal declarations.
275 -- The list of declarations (renamings and copies of formals) is built
276 -- by Analyze_Associations, just as for regular instantiations.
278 -- At the point of instantiation, conformance checking must be applied only
279 -- to those parameters that were specified in the formal. We perform this
280 -- checking by creating another internal instantiation, this one including
281 -- only the renamings and the formals (the rest of the package spec is not
282 -- relevant to conformance checking). We can then traverse two lists: the
283 -- list of actuals in the instance that corresponds to the formal package,
284 -- and the list of actuals produced for this bogus instantiation. We apply
285 -- the conformance rules to those actuals that are not defaulted (i.e.
286 -- which still appear as generic formals.
288 -- When we compile an instance body we must make the right parameters
289 -- visible again. The predicate Is_Generic_Formal indicates which of the
290 -- formals should have its Is_Hidden flag reset.
292 -----------------------
293 -- Local subprograms --
294 -----------------------
296 procedure Abandon_Instantiation (N : Node_Id);
297 pragma No_Return (Abandon_Instantiation);
298 -- Posts an error message "instantiation abandoned" at the indicated node
299 -- and then raises the exception Instantiation_Error to do it.
301 procedure Analyze_Formal_Array_Type
302 (T : in out Entity_Id;
303 Def : Node_Id);
304 -- A formal array type is treated like an array type declaration, and
305 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
306 -- in-out, because in the case of an anonymous type the entity is
307 -- actually created in the procedure.
309 -- The following procedures treat other kinds of formal parameters
311 procedure Analyze_Formal_Derived_Interface_Type
312 (N : Node_Id;
313 T : Entity_Id;
314 Def : Node_Id);
316 procedure Analyze_Formal_Derived_Type
317 (N : Node_Id;
318 T : Entity_Id;
319 Def : Node_Id);
321 procedure Analyze_Formal_Interface_Type
322 (N : Node_Id;
323 T : Entity_Id;
324 Def : Node_Id);
326 -- The following subprograms create abbreviated declarations for formal
327 -- scalar types. We introduce an anonymous base of the proper class for
328 -- each of them, and define the formals as constrained first subtypes of
329 -- their bases. The bounds are expressions that are non-static in the
330 -- generic.
332 procedure Analyze_Formal_Decimal_Fixed_Point_Type
333 (T : Entity_Id; Def : Node_Id);
334 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
335 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
336 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
337 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
338 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
339 (T : Entity_Id; Def : Node_Id);
341 procedure Analyze_Formal_Private_Type
342 (N : Node_Id;
343 T : Entity_Id;
344 Def : Node_Id);
345 -- Creates a new private type, which does not require completion
347 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
348 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
350 procedure Analyze_Generic_Formal_Part (N : Node_Id);
351 -- Analyze generic formal part
353 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
354 -- Create a new access type with the given designated type
356 function Analyze_Associations
357 (I_Node : Node_Id;
358 Formals : List_Id;
359 F_Copy : List_Id) return List_Id;
360 -- At instantiation time, build the list of associations between formals
361 -- and actuals. Each association becomes a renaming declaration for the
362 -- formal entity. F_Copy is the analyzed list of formals in the generic
363 -- copy. It is used to apply legality checks to the actuals. I_Node is the
364 -- instantiation node itself.
366 procedure Analyze_Subprogram_Instantiation
367 (N : Node_Id;
368 K : Entity_Kind);
370 procedure Build_Instance_Compilation_Unit_Nodes
371 (N : Node_Id;
372 Act_Body : Node_Id;
373 Act_Decl : Node_Id);
374 -- This procedure is used in the case where the generic instance of a
375 -- subprogram body or package body is a library unit. In this case, the
376 -- original library unit node for the generic instantiation must be
377 -- replaced by the resulting generic body, and a link made to a new
378 -- compilation unit node for the generic declaration. The argument N is
379 -- the original generic instantiation. Act_Body and Act_Decl are the body
380 -- and declaration of the instance (either package body and declaration
381 -- nodes or subprogram body and declaration nodes depending on the case).
382 -- On return, the node N has been rewritten with the actual body.
384 procedure Check_Access_Definition (N : Node_Id);
385 -- Subsidiary routine to null exclusion processing. Perform an assertion
386 -- check on Ada version and the presence of an access definition in N.
388 procedure Check_Formal_Packages (P_Id : Entity_Id);
389 -- Apply the following to all formal packages in generic associations
391 procedure Check_Formal_Package_Instance
392 (Formal_Pack : Entity_Id;
393 Actual_Pack : Entity_Id);
394 -- Verify that the actuals of the actual instance match the actuals of
395 -- the template for a formal package that is not declared with a box.
397 procedure Check_Forward_Instantiation (Decl : Node_Id);
398 -- If the generic is a local entity and the corresponding body has not
399 -- been seen yet, flag enclosing packages to indicate that it will be
400 -- elaborated after the generic body. Subprograms declared in the same
401 -- package cannot be inlined by the front-end because front-end inlining
402 -- requires a strict linear order of elaboration.
404 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
405 -- Check if some association between formals and actuals requires to make
406 -- visible primitives of a tagged type, and make those primitives visible.
407 -- Return the list of primitives whose visibility is modified (to restore
408 -- their visibility later through Restore_Hidden_Primitives). If no
409 -- candidate is found then return No_Elist.
411 procedure Check_Hidden_Child_Unit
412 (N : Node_Id;
413 Gen_Unit : Entity_Id;
414 Act_Decl_Id : Entity_Id);
415 -- If the generic unit is an implicit child instance within a parent
416 -- instance, we need to make an explicit test that it is not hidden by
417 -- a child instance of the same name and parent.
419 procedure Check_Generic_Actuals
420 (Instance : Entity_Id;
421 Is_Formal_Box : Boolean);
422 -- Similar to previous one. Check the actuals in the instantiation,
423 -- whose views can change between the point of instantiation and the point
424 -- of instantiation of the body. In addition, mark the generic renamings
425 -- as generic actuals, so that they are not compatible with other actuals.
426 -- Recurse on an actual that is a formal package whose declaration has
427 -- a box.
429 function Contains_Instance_Of
430 (Inner : Entity_Id;
431 Outer : Entity_Id;
432 N : Node_Id) return Boolean;
433 -- Inner is instantiated within the generic Outer. Check whether Inner
434 -- directly or indirectly contains an instance of Outer or of one of its
435 -- parents, in the case of a subunit. Each generic unit holds a list of
436 -- the entities instantiated within (at any depth). This procedure
437 -- determines whether the set of such lists contains a cycle, i.e. an
438 -- illegal circular instantiation.
440 function Denotes_Formal_Package
441 (Pack : Entity_Id;
442 On_Exit : Boolean := False;
443 Instance : Entity_Id := Empty) return Boolean;
444 -- Returns True if E is a formal package of an enclosing generic, or
445 -- the actual for such a formal in an enclosing instantiation. If such
446 -- a package is used as a formal in an nested generic, or as an actual
447 -- in a nested instantiation, the visibility of ITS formals should not
448 -- be modified. When called from within Restore_Private_Views, the flag
449 -- On_Exit is true, to indicate that the search for a possible enclosing
450 -- instance should ignore the current one. In that case Instance denotes
451 -- the declaration for which this is an actual. This declaration may be
452 -- an instantiation in the source, or the internal instantiation that
453 -- corresponds to the actual for a formal package.
455 function Earlier (N1, N2 : Node_Id) return Boolean;
456 -- Yields True if N1 and N2 appear in the same compilation unit,
457 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
458 -- traversal of the tree for the unit. Used to determine the placement
459 -- of freeze nodes for instance bodies that may depend on other instances.
461 function Find_Actual_Type
462 (Typ : Entity_Id;
463 Gen_Type : Entity_Id) return Entity_Id;
464 -- When validating the actual types of a child instance, check whether
465 -- the formal is a formal type of the parent unit, and retrieve the current
466 -- actual for it. Typ is the entity in the analyzed formal type declaration
467 -- (component or index type of an array type, or designated type of an
468 -- access formal) and Gen_Type is the enclosing analyzed formal array
469 -- or access type. The desired actual may be a formal of a parent, or may
470 -- be declared in a formal package of a parent. In both cases it is a
471 -- generic actual type because it appears within a visible instance.
472 -- Finally, it may be declared in a parent unit without being a formal
473 -- of that unit, in which case it must be retrieved by visibility.
474 -- Ambiguities may still arise if two homonyms are declared in two formal
475 -- packages, and the prefix of the formal type may be needed to resolve
476 -- the ambiguity in the instance ???
478 function In_Same_Declarative_Part
479 (F_Node : Node_Id;
480 Inst : Node_Id) return Boolean;
481 -- True if the instantiation Inst and the given freeze_node F_Node appear
482 -- within the same declarative part, ignoring subunits, but with no inter-
483 -- vening subprograms or concurrent units. Used to find the proper plave
484 -- for the freeze node of an instance, when the generic is declared in a
485 -- previous instance. If predicate is true, the freeze node of the instance
486 -- can be placed after the freeze node of the previous instance, Otherwise
487 -- it has to be placed at the end of the current declarative part.
489 function In_Main_Context (E : Entity_Id) return Boolean;
490 -- Check whether an instantiation is in the context of the main unit.
491 -- Used to determine whether its body should be elaborated to allow
492 -- front-end inlining.
494 procedure Set_Instance_Env
495 (Gen_Unit : Entity_Id;
496 Act_Unit : Entity_Id);
497 -- Save current instance on saved environment, to be used to determine
498 -- the global status of entities in nested instances. Part of Save_Env.
499 -- called after verifying that the generic unit is legal for the instance,
500 -- The procedure also examines whether the generic unit is a predefined
501 -- unit, in order to set configuration switches accordingly. As a result
502 -- the procedure must be called after analyzing and freezing the actuals.
504 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
505 -- Associate analyzed generic parameter with corresponding
506 -- instance. Used for semantic checks at instantiation time.
508 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
509 -- Traverse the Exchanged_Views list to see if a type was private
510 -- and has already been flipped during this phase of instantiation.
512 procedure Hide_Current_Scope;
513 -- When instantiating a generic child unit, the parent context must be
514 -- present, but the instance and all entities that may be generated
515 -- must be inserted in the current scope. We leave the current scope
516 -- on the stack, but make its entities invisible to avoid visibility
517 -- problems. This is reversed at the end of the instantiation. This is
518 -- not done for the instantiation of the bodies, which only require the
519 -- instances of the generic parents to be in scope.
521 procedure Install_Body
522 (Act_Body : Node_Id;
523 N : Node_Id;
524 Gen_Body : Node_Id;
525 Gen_Decl : Node_Id);
526 -- If the instantiation happens textually before the body of the generic,
527 -- the instantiation of the body must be analyzed after the generic body,
528 -- and not at the point of instantiation. Such early instantiations can
529 -- happen if the generic and the instance appear in a package declaration
530 -- because the generic body can only appear in the corresponding package
531 -- body. Early instantiations can also appear if generic, instance and
532 -- body are all in the declarative part of a subprogram or entry. Entities
533 -- of packages that are early instantiations are delayed, and their freeze
534 -- node appears after the generic body.
536 procedure Insert_Freeze_Node_For_Instance
537 (N : Node_Id;
538 F_Node : Node_Id);
539 -- N denotes a package or a subprogram instantiation and F_Node is the
540 -- associated freeze node. Insert the freeze node before the first source
541 -- body which follows immediately after N. If no such body is found, the
542 -- freeze node is inserted at the end of the declarative region which
543 -- contains N.
545 procedure Freeze_Subprogram_Body
546 (Inst_Node : Node_Id;
547 Gen_Body : Node_Id;
548 Pack_Id : Entity_Id);
549 -- The generic body may appear textually after the instance, including
550 -- in the proper body of a stub, or within a different package instance.
551 -- Given that the instance can only be elaborated after the generic, we
552 -- place freeze_nodes for the instance and/or for packages that may enclose
553 -- the instance and the generic, so that the back-end can establish the
554 -- proper order of elaboration.
556 procedure Init_Env;
557 -- Establish environment for subsequent instantiation. Separated from
558 -- Save_Env because data-structures for visibility handling must be
559 -- initialized before call to Check_Generic_Child_Unit.
561 procedure Install_Formal_Packages (Par : Entity_Id);
562 -- Install the visible part of any formal of the parent that is a formal
563 -- package. Note that for the case of a formal package with a box, this
564 -- includes the formal part of the formal package (12.7(10/2)).
566 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
567 -- When compiling an instance of a child unit the parent (which is
568 -- itself an instance) is an enclosing scope that must be made
569 -- immediately visible. This procedure is also used to install the non-
570 -- generic parent of a generic child unit when compiling its body, so
571 -- that full views of types in the parent are made visible.
573 procedure Remove_Parent (In_Body : Boolean := False);
574 -- Reverse effect after instantiation of child is complete
576 procedure Install_Hidden_Primitives
577 (Prims_List : in out Elist_Id;
578 Gen_T : Entity_Id;
579 Act_T : Entity_Id);
580 -- Remove suffix 'P' from hidden primitives of Act_T to match the
581 -- visibility of primitives of Gen_T. The list of primitives to which
582 -- the suffix is removed is added to Prims_List to restore them later.
584 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
585 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
586 -- set to No_Elist.
588 procedure Inline_Instance_Body
589 (N : Node_Id;
590 Gen_Unit : Entity_Id;
591 Act_Decl : Node_Id);
592 -- If front-end inlining is requested, instantiate the package body,
593 -- and preserve the visibility of its compilation unit, to insure
594 -- that successive instantiations succeed.
596 -- The functions Instantiate_XXX perform various legality checks and build
597 -- the declarations for instantiated generic parameters. In all of these
598 -- Formal is the entity in the generic unit, Actual is the entity of
599 -- expression in the generic associations, and Analyzed_Formal is the
600 -- formal in the generic copy, which contains the semantic information to
601 -- be used to validate the actual.
603 function Instantiate_Object
604 (Formal : Node_Id;
605 Actual : Node_Id;
606 Analyzed_Formal : Node_Id) return List_Id;
608 function Instantiate_Type
609 (Formal : Node_Id;
610 Actual : Node_Id;
611 Analyzed_Formal : Node_Id;
612 Actual_Decls : List_Id) return List_Id;
614 function Instantiate_Formal_Subprogram
615 (Formal : Node_Id;
616 Actual : Node_Id;
617 Analyzed_Formal : Node_Id) return Node_Id;
619 function Instantiate_Formal_Package
620 (Formal : Node_Id;
621 Actual : Node_Id;
622 Analyzed_Formal : Node_Id) return List_Id;
623 -- If the formal package is declared with a box, special visibility rules
624 -- apply to its formals: they are in the visible part of the package. This
625 -- is true in the declarative region of the formal package, that is to say
626 -- in the enclosing generic or instantiation. For an instantiation, the
627 -- parameters of the formal package are made visible in an explicit step.
628 -- Furthermore, if the actual has a visible USE clause, these formals must
629 -- be made potentially use-visible as well. On exit from the enclosing
630 -- instantiation, the reverse must be done.
632 -- For a formal package declared without a box, there are conformance rules
633 -- that apply to the actuals in the generic declaration and the actuals of
634 -- the actual package in the enclosing instantiation. The simplest way to
635 -- apply these rules is to repeat the instantiation of the formal package
636 -- in the context of the enclosing instance, and compare the generic
637 -- associations of this instantiation with those of the actual package.
638 -- This internal instantiation only needs to contain the renamings of the
639 -- formals: the visible and private declarations themselves need not be
640 -- created.
642 -- In Ada 2005, the formal package may be only partially parameterized.
643 -- In that case the visibility step must make visible those actuals whose
644 -- corresponding formals were given with a box. A final complication
645 -- involves inherited operations from formal derived types, which must
646 -- be visible if the type is.
648 function Is_In_Main_Unit (N : Node_Id) return Boolean;
649 -- Test if given node is in the main unit
651 procedure Load_Parent_Of_Generic
652 (N : Node_Id;
653 Spec : Node_Id;
654 Body_Optional : Boolean := False);
655 -- If the generic appears in a separate non-generic library unit, load the
656 -- corresponding body to retrieve the body of the generic. N is the node
657 -- for the generic instantiation, Spec is the generic package declaration.
659 -- Body_Optional is a flag that indicates that the body is being loaded to
660 -- ensure that temporaries are generated consistently when there are other
661 -- instances in the current declarative part that precede the one being
662 -- loaded. In that case a missing body is acceptable.
664 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
665 -- Add the context clause of the unit containing a generic unit to a
666 -- compilation unit that is, or contains, an instantiation.
668 function Get_Associated_Node (N : Node_Id) return Node_Id;
669 -- In order to propagate semantic information back from the analyzed copy
670 -- to the original generic, we maintain links between selected nodes in the
671 -- generic and their corresponding copies. At the end of generic analysis,
672 -- the routine Save_Global_References traverses the generic tree, examines
673 -- the semantic information, and preserves the links to those nodes that
674 -- contain global information. At instantiation, the information from the
675 -- associated node is placed on the new copy, so that name resolution is
676 -- not repeated.
678 -- Three kinds of source nodes have associated nodes:
680 -- a) those that can reference (denote) entities, that is identifiers,
681 -- character literals, expanded_names, operator symbols, operators,
682 -- and attribute reference nodes. These nodes have an Entity field
683 -- and are the set of nodes that are in N_Has_Entity.
685 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
687 -- c) selected components (N_Selected_Component)
689 -- For the first class, the associated node preserves the entity if it is
690 -- global. If the generic contains nested instantiations, the associated
691 -- node itself has been recopied, and a chain of them must be followed.
693 -- For aggregates, the associated node allows retrieval of the type, which
694 -- may otherwise not appear in the generic. The view of this type may be
695 -- different between generic and instantiation, and the full view can be
696 -- installed before the instantiation is analyzed. For aggregates of type
697 -- extensions, the same view exchange may have to be performed for some of
698 -- the ancestor types, if their view is private at the point of
699 -- instantiation.
701 -- Nodes that are selected components in the parse tree may be rewritten
702 -- as expanded names after resolution, and must be treated as potential
703 -- entity holders, which is why they also have an Associated_Node.
705 -- Nodes that do not come from source, such as freeze nodes, do not appear
706 -- in the generic tree, and need not have an associated node.
708 -- The associated node is stored in the Associated_Node field. Note that
709 -- this field overlaps Entity, which is fine, because the whole point is
710 -- that we don't need or want the normal Entity field in this situation.
712 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
713 -- Within the generic part, entities in the formal package are
714 -- visible. To validate subsequent type declarations, indicate
715 -- the correspondence between the entities in the analyzed formal,
716 -- and the entities in the actual package. There are three packages
717 -- involved in the instantiation of a formal package: the parent
718 -- generic P1 which appears in the generic declaration, the fake
719 -- instantiation P2 which appears in the analyzed generic, and whose
720 -- visible entities may be used in subsequent formals, and the actual
721 -- P3 in the instance. To validate subsequent formals, me indicate
722 -- that the entities in P2 are mapped into those of P3. The mapping of
723 -- entities has to be done recursively for nested packages.
725 procedure Move_Freeze_Nodes
726 (Out_Of : Entity_Id;
727 After : Node_Id;
728 L : List_Id);
729 -- Freeze nodes can be generated in the analysis of a generic unit, but
730 -- will not be seen by the back-end. It is necessary to move those nodes
731 -- to the enclosing scope if they freeze an outer entity. We place them
732 -- at the end of the enclosing generic package, which is semantically
733 -- neutral.
735 procedure Preanalyze_Actuals (N : Node_Id);
736 -- Analyze actuals to perform name resolution. Full resolution is done
737 -- later, when the expected types are known, but names have to be captured
738 -- before installing parents of generics, that are not visible for the
739 -- actuals themselves.
741 function True_Parent (N : Node_Id) return Node_Id;
742 -- For a subunit, return parent of corresponding stub, else return
743 -- parent of node.
745 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
746 -- Verify that an attribute that appears as the default for a formal
747 -- subprogram is a function or procedure with the correct profile.
749 -------------------------------------------
750 -- Data Structures for Generic Renamings --
751 -------------------------------------------
753 -- The map Generic_Renamings associates generic entities with their
754 -- corresponding actuals. Currently used to validate type instances. It
755 -- will eventually be used for all generic parameters to eliminate the
756 -- need for overload resolution in the instance.
758 type Assoc_Ptr is new Int;
760 Assoc_Null : constant Assoc_Ptr := -1;
762 type Assoc is record
763 Gen_Id : Entity_Id;
764 Act_Id : Entity_Id;
765 Next_In_HTable : Assoc_Ptr;
766 end record;
768 package Generic_Renamings is new Table.Table
769 (Table_Component_Type => Assoc,
770 Table_Index_Type => Assoc_Ptr,
771 Table_Low_Bound => 0,
772 Table_Initial => 10,
773 Table_Increment => 100,
774 Table_Name => "Generic_Renamings");
776 -- Variable to hold enclosing instantiation. When the environment is
777 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
779 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
781 -- Hash table for associations
783 HTable_Size : constant := 37;
784 type HTable_Range is range 0 .. HTable_Size - 1;
786 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
787 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
788 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
789 function Hash (F : Entity_Id) return HTable_Range;
791 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
792 Header_Num => HTable_Range,
793 Element => Assoc,
794 Elmt_Ptr => Assoc_Ptr,
795 Null_Ptr => Assoc_Null,
796 Set_Next => Set_Next_Assoc,
797 Next => Next_Assoc,
798 Key => Entity_Id,
799 Get_Key => Get_Gen_Id,
800 Hash => Hash,
801 Equal => "=");
803 Exchanged_Views : Elist_Id;
804 -- This list holds the private views that have been exchanged during
805 -- instantiation to restore the visibility of the generic declaration.
806 -- (see comments above). After instantiation, the current visibility is
807 -- reestablished by means of a traversal of this list.
809 Hidden_Entities : Elist_Id;
810 -- This list holds the entities of the current scope that are removed
811 -- from immediate visibility when instantiating a child unit. Their
812 -- visibility is restored in Remove_Parent.
814 -- Because instantiations can be recursive, the following must be saved
815 -- on entry and restored on exit from an instantiation (spec or body).
816 -- This is done by the two procedures Save_Env and Restore_Env. For
817 -- package and subprogram instantiations (but not for the body instances)
818 -- the action of Save_Env is done in two steps: Init_Env is called before
819 -- Check_Generic_Child_Unit, because setting the parent instances requires
820 -- that the visibility data structures be properly initialized. Once the
821 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
823 Parent_Unit_Visible : Boolean := False;
824 -- Parent_Unit_Visible is used when the generic is a child unit, and
825 -- indicates whether the ultimate parent of the generic is visible in the
826 -- instantiation environment. It is used to reset the visibility of the
827 -- parent at the end of the instantiation (see Remove_Parent).
829 Instance_Parent_Unit : Entity_Id := Empty;
830 -- This records the ultimate parent unit of an instance of a generic
831 -- child unit and is used in conjunction with Parent_Unit_Visible to
832 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
834 type Instance_Env is record
835 Instantiated_Parent : Assoc;
836 Exchanged_Views : Elist_Id;
837 Hidden_Entities : Elist_Id;
838 Current_Sem_Unit : Unit_Number_Type;
839 Parent_Unit_Visible : Boolean := False;
840 Instance_Parent_Unit : Entity_Id := Empty;
841 Switches : Config_Switches_Type;
842 end record;
844 package Instance_Envs is new Table.Table (
845 Table_Component_Type => Instance_Env,
846 Table_Index_Type => Int,
847 Table_Low_Bound => 0,
848 Table_Initial => 32,
849 Table_Increment => 100,
850 Table_Name => "Instance_Envs");
852 procedure Restore_Private_Views
853 (Pack_Id : Entity_Id;
854 Is_Package : Boolean := True);
855 -- Restore the private views of external types, and unmark the generic
856 -- renamings of actuals, so that they become compatible subtypes again.
857 -- For subprograms, Pack_Id is the package constructed to hold the
858 -- renamings.
860 procedure Switch_View (T : Entity_Id);
861 -- Switch the partial and full views of a type and its private
862 -- dependents (i.e. its subtypes and derived types).
864 ------------------------------------
865 -- Structures for Error Reporting --
866 ------------------------------------
868 Instantiation_Node : Node_Id;
869 -- Used by subprograms that validate instantiation of formal parameters
870 -- where there might be no actual on which to place the error message.
871 -- Also used to locate the instantiation node for generic subunits.
873 Instantiation_Error : exception;
874 -- When there is a semantic error in the generic parameter matching,
875 -- there is no point in continuing the instantiation, because the
876 -- number of cascaded errors is unpredictable. This exception aborts
877 -- the instantiation process altogether.
879 S_Adjustment : Sloc_Adjustment;
880 -- Offset created for each node in an instantiation, in order to keep
881 -- track of the source position of the instantiation in each of its nodes.
882 -- A subsequent semantic error or warning on a construct of the instance
883 -- points to both places: the original generic node, and the point of
884 -- instantiation. See Sinput and Sinput.L for additional details.
886 ------------------------------------------------------------
887 -- Data structure for keeping track when inside a Generic --
888 ------------------------------------------------------------
890 -- The following table is used to save values of the Inside_A_Generic
891 -- flag (see spec of Sem) when they are saved by Start_Generic.
893 package Generic_Flags is new Table.Table (
894 Table_Component_Type => Boolean,
895 Table_Index_Type => Int,
896 Table_Low_Bound => 0,
897 Table_Initial => 32,
898 Table_Increment => 200,
899 Table_Name => "Generic_Flags");
901 ---------------------------
902 -- Abandon_Instantiation --
903 ---------------------------
905 procedure Abandon_Instantiation (N : Node_Id) is
906 begin
907 Error_Msg_N ("\instantiation abandoned!", N);
908 raise Instantiation_Error;
909 end Abandon_Instantiation;
911 --------------------------
912 -- Analyze_Associations --
913 --------------------------
915 function Analyze_Associations
916 (I_Node : Node_Id;
917 Formals : List_Id;
918 F_Copy : List_Id) return List_Id
920 Actual_Types : constant Elist_Id := New_Elmt_List;
921 Assoc : constant List_Id := New_List;
922 Default_Actuals : constant Elist_Id := New_Elmt_List;
923 Gen_Unit : constant Entity_Id :=
924 Defining_Entity (Parent (F_Copy));
926 Actuals : List_Id;
927 Actual : Node_Id;
928 Formal : Node_Id;
929 Next_Formal : Node_Id;
930 Analyzed_Formal : Node_Id;
931 Match : Node_Id;
932 Named : Node_Id;
933 First_Named : Node_Id := Empty;
935 Default_Formals : constant List_Id := New_List;
936 -- If an Others_Choice is present, some of the formals may be defaulted.
937 -- To simplify the treatment of visibility in an instance, we introduce
938 -- individual defaults for each such formal. These defaults are
939 -- appended to the list of associations and replace the Others_Choice.
941 Found_Assoc : Node_Id;
942 -- Association for the current formal being match. Empty if there are
943 -- no remaining actuals, or if there is no named association with the
944 -- name of the formal.
946 Is_Named_Assoc : Boolean;
947 Num_Matched : Int := 0;
948 Num_Actuals : Int := 0;
950 Others_Present : Boolean := False;
951 Others_Choice : Node_Id := Empty;
952 -- In Ada 2005, indicates partial parametrization of a formal
953 -- package. As usual an other association must be last in the list.
955 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
956 -- Apply RM 12.3 (9): if a formal subprogram is overloaded, the instance
957 -- cannot have a named association for it. AI05-0025 extends this rule
958 -- to formals of formal packages by AI05-0025, and it also applies to
959 -- box-initialized formals.
961 function Matching_Actual
962 (F : Entity_Id;
963 A_F : Entity_Id) return Node_Id;
964 -- Find actual that corresponds to a given a formal parameter. If the
965 -- actuals are positional, return the next one, if any. If the actuals
966 -- are named, scan the parameter associations to find the right one.
967 -- A_F is the corresponding entity in the analyzed generic,which is
968 -- placed on the selector name for ASIS use.
970 -- In Ada 2005, a named association may be given with a box, in which
971 -- case Matching_Actual sets Found_Assoc to the generic association,
972 -- but return Empty for the actual itself. In this case the code below
973 -- creates a corresponding declaration for the formal.
975 function Partial_Parametrization return Boolean;
976 -- Ada 2005: if no match is found for a given formal, check if the
977 -- association for it includes a box, or whether the associations
978 -- include an Others clause.
980 procedure Process_Default (F : Entity_Id);
981 -- Add a copy of the declaration of generic formal F to the list of
982 -- associations, and add an explicit box association for F if there
983 -- is none yet, and the default comes from an Others_Choice.
985 procedure Set_Analyzed_Formal;
986 -- Find the node in the generic copy that corresponds to a given formal.
987 -- The semantic information on this node is used to perform legality
988 -- checks on the actuals. Because semantic analysis can introduce some
989 -- anonymous entities or modify the declaration node itself, the
990 -- correspondence between the two lists is not one-one. In addition to
991 -- anonymous types, the presence a formal equality will introduce an
992 -- implicit declaration for the corresponding inequality.
994 ----------------------------------------
995 -- Check_Overloaded_Formal_Subprogram --
996 ----------------------------------------
998 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
999 Temp_Formal : Entity_Id;
1001 begin
1002 Temp_Formal := First (Formals);
1003 while Present (Temp_Formal) loop
1004 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1005 and then Temp_Formal /= Formal
1006 and then
1007 Chars (Defining_Unit_Name (Specification (Formal))) =
1008 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1009 then
1010 if Present (Found_Assoc) then
1011 Error_Msg_N
1012 ("named association not allowed for overloaded formal",
1013 Found_Assoc);
1015 else
1016 Error_Msg_N
1017 ("named association not allowed for overloaded formal",
1018 Others_Choice);
1019 end if;
1021 Abandon_Instantiation (Instantiation_Node);
1022 end if;
1024 Next (Temp_Formal);
1025 end loop;
1026 end Check_Overloaded_Formal_Subprogram;
1028 ---------------------
1029 -- Matching_Actual --
1030 ---------------------
1032 function Matching_Actual
1033 (F : Entity_Id;
1034 A_F : Entity_Id) return Node_Id
1036 Prev : Node_Id;
1037 Act : Node_Id;
1039 begin
1040 Is_Named_Assoc := False;
1042 -- End of list of purely positional parameters
1044 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1045 Found_Assoc := Empty;
1046 Act := Empty;
1048 -- Case of positional parameter corresponding to current formal
1050 elsif No (Selector_Name (Actual)) then
1051 Found_Assoc := Actual;
1052 Act := Explicit_Generic_Actual_Parameter (Actual);
1053 Num_Matched := Num_Matched + 1;
1054 Next (Actual);
1056 -- Otherwise scan list of named actuals to find the one with the
1057 -- desired name. All remaining actuals have explicit names.
1059 else
1060 Is_Named_Assoc := True;
1061 Found_Assoc := Empty;
1062 Act := Empty;
1063 Prev := Empty;
1065 while Present (Actual) loop
1066 if Chars (Selector_Name (Actual)) = Chars (F) then
1067 Set_Entity (Selector_Name (Actual), A_F);
1068 Set_Etype (Selector_Name (Actual), Etype (A_F));
1069 Generate_Reference (A_F, Selector_Name (Actual));
1070 Found_Assoc := Actual;
1071 Act := Explicit_Generic_Actual_Parameter (Actual);
1072 Num_Matched := Num_Matched + 1;
1073 exit;
1074 end if;
1076 Prev := Actual;
1077 Next (Actual);
1078 end loop;
1080 -- Reset for subsequent searches. In most cases the named
1081 -- associations are in order. If they are not, we reorder them
1082 -- to avoid scanning twice the same actual. This is not just a
1083 -- question of efficiency: there may be multiple defaults with
1084 -- boxes that have the same name. In a nested instantiation we
1085 -- insert actuals for those defaults, and cannot rely on their
1086 -- names to disambiguate them.
1088 if Actual = First_Named then
1089 Next (First_Named);
1091 elsif Present (Actual) then
1092 Insert_Before (First_Named, Remove_Next (Prev));
1093 end if;
1095 Actual := First_Named;
1096 end if;
1098 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1099 Set_Used_As_Generic_Actual (Entity (Act));
1100 end if;
1102 return Act;
1103 end Matching_Actual;
1105 -----------------------------
1106 -- Partial_Parametrization --
1107 -----------------------------
1109 function Partial_Parametrization return Boolean is
1110 begin
1111 return Others_Present
1112 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1113 end Partial_Parametrization;
1115 ---------------------
1116 -- Process_Default --
1117 ---------------------
1119 procedure Process_Default (F : Entity_Id) is
1120 Loc : constant Source_Ptr := Sloc (I_Node);
1121 F_Id : constant Entity_Id := Defining_Entity (F);
1122 Decl : Node_Id;
1123 Default : Node_Id;
1124 Id : Entity_Id;
1126 begin
1127 -- Append copy of formal declaration to associations, and create new
1128 -- defining identifier for it.
1130 Decl := New_Copy_Tree (F);
1131 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1133 if Nkind (F) in N_Formal_Subprogram_Declaration then
1134 Set_Defining_Unit_Name (Specification (Decl), Id);
1136 else
1137 Set_Defining_Identifier (Decl, Id);
1138 end if;
1140 Append (Decl, Assoc);
1142 if No (Found_Assoc) then
1143 Default :=
1144 Make_Generic_Association (Loc,
1145 Selector_Name => New_Occurrence_Of (Id, Loc),
1146 Explicit_Generic_Actual_Parameter => Empty);
1147 Set_Box_Present (Default);
1148 Append (Default, Default_Formals);
1149 end if;
1150 end Process_Default;
1152 -------------------------
1153 -- Set_Analyzed_Formal --
1154 -------------------------
1156 procedure Set_Analyzed_Formal is
1157 Kind : Node_Kind;
1159 begin
1160 while Present (Analyzed_Formal) loop
1161 Kind := Nkind (Analyzed_Formal);
1163 case Nkind (Formal) is
1165 when N_Formal_Subprogram_Declaration =>
1166 exit when Kind in N_Formal_Subprogram_Declaration
1167 and then
1168 Chars
1169 (Defining_Unit_Name (Specification (Formal))) =
1170 Chars
1171 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1173 when N_Formal_Package_Declaration =>
1174 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1175 N_Generic_Package_Declaration,
1176 N_Package_Declaration);
1178 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1180 when others =>
1182 -- Skip freeze nodes, and nodes inserted to replace
1183 -- unrecognized pragmas.
1185 exit when
1186 Kind not in N_Formal_Subprogram_Declaration
1187 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1188 N_Freeze_Entity,
1189 N_Null_Statement,
1190 N_Itype_Reference)
1191 and then Chars (Defining_Identifier (Formal)) =
1192 Chars (Defining_Identifier (Analyzed_Formal));
1193 end case;
1195 Next (Analyzed_Formal);
1196 end loop;
1197 end Set_Analyzed_Formal;
1199 -- Start of processing for Analyze_Associations
1201 begin
1202 Actuals := Generic_Associations (I_Node);
1204 if Present (Actuals) then
1206 -- Check for an Others choice, indicating a partial parametrization
1207 -- for a formal package.
1209 Actual := First (Actuals);
1210 while Present (Actual) loop
1211 if Nkind (Actual) = N_Others_Choice then
1212 Others_Present := True;
1213 Others_Choice := Actual;
1215 if Present (Next (Actual)) then
1216 Error_Msg_N ("others must be last association", Actual);
1217 end if;
1219 -- This subprogram is used both for formal packages and for
1220 -- instantiations. For the latter, associations must all be
1221 -- explicit.
1223 if Nkind (I_Node) /= N_Formal_Package_Declaration
1224 and then Comes_From_Source (I_Node)
1225 then
1226 Error_Msg_N
1227 ("others association not allowed in an instance",
1228 Actual);
1229 end if;
1231 -- In any case, nothing to do after the others association
1233 exit;
1235 elsif Box_Present (Actual)
1236 and then Comes_From_Source (I_Node)
1237 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1238 then
1239 Error_Msg_N
1240 ("box association not allowed in an instance", Actual);
1241 end if;
1243 Next (Actual);
1244 end loop;
1246 -- If named associations are present, save first named association
1247 -- (it may of course be Empty) to facilitate subsequent name search.
1249 First_Named := First (Actuals);
1250 while Present (First_Named)
1251 and then Nkind (First_Named) /= N_Others_Choice
1252 and then No (Selector_Name (First_Named))
1253 loop
1254 Num_Actuals := Num_Actuals + 1;
1255 Next (First_Named);
1256 end loop;
1257 end if;
1259 Named := First_Named;
1260 while Present (Named) loop
1261 if Nkind (Named) /= N_Others_Choice
1262 and then No (Selector_Name (Named))
1263 then
1264 Error_Msg_N ("invalid positional actual after named one", Named);
1265 Abandon_Instantiation (Named);
1266 end if;
1268 -- A named association may lack an actual parameter, if it was
1269 -- introduced for a default subprogram that turns out to be local
1270 -- to the outer instantiation.
1272 if Nkind (Named) /= N_Others_Choice
1273 and then Present (Explicit_Generic_Actual_Parameter (Named))
1274 then
1275 Num_Actuals := Num_Actuals + 1;
1276 end if;
1278 Next (Named);
1279 end loop;
1281 if Present (Formals) then
1282 Formal := First_Non_Pragma (Formals);
1283 Analyzed_Formal := First_Non_Pragma (F_Copy);
1285 if Present (Actuals) then
1286 Actual := First (Actuals);
1288 -- All formals should have default values
1290 else
1291 Actual := Empty;
1292 end if;
1294 while Present (Formal) loop
1295 Set_Analyzed_Formal;
1296 Next_Formal := Next_Non_Pragma (Formal);
1298 case Nkind (Formal) is
1299 when N_Formal_Object_Declaration =>
1300 Match :=
1301 Matching_Actual (
1302 Defining_Identifier (Formal),
1303 Defining_Identifier (Analyzed_Formal));
1305 if No (Match) and then Partial_Parametrization then
1306 Process_Default (Formal);
1307 else
1308 Append_List
1309 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1310 Assoc);
1311 end if;
1313 when N_Formal_Type_Declaration =>
1314 Match :=
1315 Matching_Actual (
1316 Defining_Identifier (Formal),
1317 Defining_Identifier (Analyzed_Formal));
1319 if No (Match) then
1320 if Partial_Parametrization then
1321 Process_Default (Formal);
1323 else
1324 Error_Msg_Sloc := Sloc (Gen_Unit);
1325 Error_Msg_NE
1326 ("missing actual&",
1327 Instantiation_Node,
1328 Defining_Identifier (Formal));
1329 Error_Msg_NE ("\in instantiation of & declared#",
1330 Instantiation_Node, Gen_Unit);
1331 Abandon_Instantiation (Instantiation_Node);
1332 end if;
1334 else
1335 Analyze (Match);
1336 Append_List
1337 (Instantiate_Type
1338 (Formal, Match, Analyzed_Formal, Assoc),
1339 Assoc);
1341 -- An instantiation is a freeze point for the actuals,
1342 -- unless this is a rewritten formal package, or the
1343 -- formal is an Ada 2012 formal incomplete type.
1345 if Nkind (I_Node) /= N_Formal_Package_Declaration
1346 and then
1347 Ekind (Defining_Identifier (Analyzed_Formal)) /=
1348 E_Incomplete_Type
1349 then
1350 Append_Elmt (Entity (Match), Actual_Types);
1351 end if;
1352 end if;
1354 -- A remote access-to-class-wide type is not a legal actual
1355 -- for a generic formal of an access type (E.2.2(17)).
1357 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1358 and then
1359 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1360 N_Access_To_Object_Definition
1361 then
1362 Validate_Remote_Access_To_Class_Wide_Type (Match);
1363 end if;
1365 when N_Formal_Subprogram_Declaration =>
1366 Match :=
1367 Matching_Actual (
1368 Defining_Unit_Name (Specification (Formal)),
1369 Defining_Unit_Name (Specification (Analyzed_Formal)));
1371 -- If the formal subprogram has the same name as another
1372 -- formal subprogram of the generic, then a named
1373 -- association is illegal (12.3(9)). Exclude named
1374 -- associations that are generated for a nested instance.
1376 if Present (Match)
1377 and then Is_Named_Assoc
1378 and then Comes_From_Source (Found_Assoc)
1379 then
1380 Check_Overloaded_Formal_Subprogram (Formal);
1381 end if;
1383 -- If there is no corresponding actual, this may be case of
1384 -- partial parametrization, or else the formal has a default
1385 -- or a box.
1387 if No (Match)
1388 and then Partial_Parametrization
1389 then
1390 Process_Default (Formal);
1391 if Nkind (I_Node) = N_Formal_Package_Declaration then
1392 Check_Overloaded_Formal_Subprogram (Formal);
1393 end if;
1395 else
1396 Append_To (Assoc,
1397 Instantiate_Formal_Subprogram
1398 (Formal, Match, Analyzed_Formal));
1399 end if;
1401 -- If this is a nested generic, preserve default for later
1402 -- instantiations.
1404 if No (Match)
1405 and then Box_Present (Formal)
1406 then
1407 Append_Elmt
1408 (Defining_Unit_Name (Specification (Last (Assoc))),
1409 Default_Actuals);
1410 end if;
1412 when N_Formal_Package_Declaration =>
1413 Match :=
1414 Matching_Actual (
1415 Defining_Identifier (Formal),
1416 Defining_Identifier (Original_Node (Analyzed_Formal)));
1418 if No (Match) then
1419 if Partial_Parametrization then
1420 Process_Default (Formal);
1422 else
1423 Error_Msg_Sloc := Sloc (Gen_Unit);
1424 Error_Msg_NE
1425 ("missing actual&",
1426 Instantiation_Node, Defining_Identifier (Formal));
1427 Error_Msg_NE ("\in instantiation of & declared#",
1428 Instantiation_Node, Gen_Unit);
1430 Abandon_Instantiation (Instantiation_Node);
1431 end if;
1433 else
1434 Analyze (Match);
1435 Append_List
1436 (Instantiate_Formal_Package
1437 (Formal, Match, Analyzed_Formal),
1438 Assoc);
1439 end if;
1441 -- For use type and use package appearing in the generic part,
1442 -- we have already copied them, so we can just move them where
1443 -- they belong (we mustn't recopy them since this would mess up
1444 -- the Sloc values).
1446 when N_Use_Package_Clause |
1447 N_Use_Type_Clause =>
1448 if Nkind (Original_Node (I_Node)) =
1449 N_Formal_Package_Declaration
1450 then
1451 Append (New_Copy_Tree (Formal), Assoc);
1452 else
1453 Remove (Formal);
1454 Append (Formal, Assoc);
1455 end if;
1457 when others =>
1458 raise Program_Error;
1460 end case;
1462 Formal := Next_Formal;
1463 Next_Non_Pragma (Analyzed_Formal);
1464 end loop;
1466 if Num_Actuals > Num_Matched then
1467 Error_Msg_Sloc := Sloc (Gen_Unit);
1469 if Present (Selector_Name (Actual)) then
1470 Error_Msg_NE
1471 ("unmatched actual&",
1472 Actual, Selector_Name (Actual));
1473 Error_Msg_NE ("\in instantiation of& declared#",
1474 Actual, Gen_Unit);
1475 else
1476 Error_Msg_NE
1477 ("unmatched actual in instantiation of& declared#",
1478 Actual, Gen_Unit);
1479 end if;
1480 end if;
1482 elsif Present (Actuals) then
1483 Error_Msg_N
1484 ("too many actuals in generic instantiation", Instantiation_Node);
1485 end if;
1487 declare
1488 Elmt : Elmt_Id := First_Elmt (Actual_Types);
1489 begin
1490 while Present (Elmt) loop
1491 Freeze_Before (I_Node, Node (Elmt));
1492 Next_Elmt (Elmt);
1493 end loop;
1494 end;
1496 -- If there are default subprograms, normalize the tree by adding
1497 -- explicit associations for them. This is required if the instance
1498 -- appears within a generic.
1500 declare
1501 Elmt : Elmt_Id;
1502 Subp : Entity_Id;
1503 New_D : Node_Id;
1505 begin
1506 Elmt := First_Elmt (Default_Actuals);
1507 while Present (Elmt) loop
1508 if No (Actuals) then
1509 Actuals := New_List;
1510 Set_Generic_Associations (I_Node, Actuals);
1511 end if;
1513 Subp := Node (Elmt);
1514 New_D :=
1515 Make_Generic_Association (Sloc (Subp),
1516 Selector_Name => New_Occurrence_Of (Subp, Sloc (Subp)),
1517 Explicit_Generic_Actual_Parameter =>
1518 New_Occurrence_Of (Subp, Sloc (Subp)));
1519 Mark_Rewrite_Insertion (New_D);
1520 Append_To (Actuals, New_D);
1521 Next_Elmt (Elmt);
1522 end loop;
1523 end;
1525 -- If this is a formal package, normalize the parameter list by adding
1526 -- explicit box associations for the formals that are covered by an
1527 -- Others_Choice.
1529 if not Is_Empty_List (Default_Formals) then
1530 Append_List (Default_Formals, Formals);
1531 end if;
1533 return Assoc;
1534 end Analyze_Associations;
1536 -------------------------------
1537 -- Analyze_Formal_Array_Type --
1538 -------------------------------
1540 procedure Analyze_Formal_Array_Type
1541 (T : in out Entity_Id;
1542 Def : Node_Id)
1544 DSS : Node_Id;
1546 begin
1547 -- Treated like a non-generic array declaration, with additional
1548 -- semantic checks.
1550 Enter_Name (T);
1552 if Nkind (Def) = N_Constrained_Array_Definition then
1553 DSS := First (Discrete_Subtype_Definitions (Def));
1554 while Present (DSS) loop
1555 if Nkind_In (DSS, N_Subtype_Indication,
1556 N_Range,
1557 N_Attribute_Reference)
1558 then
1559 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1560 end if;
1562 Next (DSS);
1563 end loop;
1564 end if;
1566 Array_Type_Declaration (T, Def);
1567 Set_Is_Generic_Type (Base_Type (T));
1569 if Ekind (Component_Type (T)) = E_Incomplete_Type
1570 and then No (Full_View (Component_Type (T)))
1571 then
1572 Error_Msg_N ("premature usage of incomplete type", Def);
1574 -- Check that range constraint is not allowed on the component type
1575 -- of a generic formal array type (AARM 12.5.3(3))
1577 elsif Is_Internal (Component_Type (T))
1578 and then Present (Subtype_Indication (Component_Definition (Def)))
1579 and then Nkind (Original_Node
1580 (Subtype_Indication (Component_Definition (Def)))) =
1581 N_Subtype_Indication
1582 then
1583 Error_Msg_N
1584 ("in a formal, a subtype indication can only be "
1585 & "a subtype mark (RM 12.5.3(3))",
1586 Subtype_Indication (Component_Definition (Def)));
1587 end if;
1589 end Analyze_Formal_Array_Type;
1591 ---------------------------------------------
1592 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1593 ---------------------------------------------
1595 -- As for other generic types, we create a valid type representation with
1596 -- legal but arbitrary attributes, whose values are never considered
1597 -- static. For all scalar types we introduce an anonymous base type, with
1598 -- the same attributes. We choose the corresponding integer type to be
1599 -- Standard_Integer.
1600 -- Here and in other similar routines, the Sloc of the generated internal
1601 -- type must be the same as the sloc of the defining identifier of the
1602 -- formal type declaration, to provide proper source navigation.
1604 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1605 (T : Entity_Id;
1606 Def : Node_Id)
1608 Loc : constant Source_Ptr := Sloc (Def);
1610 Base : constant Entity_Id :=
1611 New_Internal_Entity
1612 (E_Decimal_Fixed_Point_Type,
1613 Current_Scope,
1614 Sloc (Defining_Identifier (Parent (Def))), 'G');
1616 Int_Base : constant Entity_Id := Standard_Integer;
1617 Delta_Val : constant Ureal := Ureal_1;
1618 Digs_Val : constant Uint := Uint_6;
1620 begin
1621 Enter_Name (T);
1623 Set_Etype (Base, Base);
1624 Set_Size_Info (Base, Int_Base);
1625 Set_RM_Size (Base, RM_Size (Int_Base));
1626 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1627 Set_Digits_Value (Base, Digs_Val);
1628 Set_Delta_Value (Base, Delta_Val);
1629 Set_Small_Value (Base, Delta_Val);
1630 Set_Scalar_Range (Base,
1631 Make_Range (Loc,
1632 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1633 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1635 Set_Is_Generic_Type (Base);
1636 Set_Parent (Base, Parent (Def));
1638 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1639 Set_Etype (T, Base);
1640 Set_Size_Info (T, Int_Base);
1641 Set_RM_Size (T, RM_Size (Int_Base));
1642 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1643 Set_Digits_Value (T, Digs_Val);
1644 Set_Delta_Value (T, Delta_Val);
1645 Set_Small_Value (T, Delta_Val);
1646 Set_Scalar_Range (T, Scalar_Range (Base));
1647 Set_Is_Constrained (T);
1649 Check_Restriction (No_Fixed_Point, Def);
1650 end Analyze_Formal_Decimal_Fixed_Point_Type;
1652 -------------------------------------------
1653 -- Analyze_Formal_Derived_Interface_Type --
1654 -------------------------------------------
1656 procedure Analyze_Formal_Derived_Interface_Type
1657 (N : Node_Id;
1658 T : Entity_Id;
1659 Def : Node_Id)
1661 Loc : constant Source_Ptr := Sloc (Def);
1663 begin
1664 -- Rewrite as a type declaration of a derived type. This ensures that
1665 -- the interface list and primitive operations are properly captured.
1667 Rewrite (N,
1668 Make_Full_Type_Declaration (Loc,
1669 Defining_Identifier => T,
1670 Type_Definition => Def));
1671 Analyze (N);
1672 Set_Is_Generic_Type (T);
1673 end Analyze_Formal_Derived_Interface_Type;
1675 ---------------------------------
1676 -- Analyze_Formal_Derived_Type --
1677 ---------------------------------
1679 procedure Analyze_Formal_Derived_Type
1680 (N : Node_Id;
1681 T : Entity_Id;
1682 Def : Node_Id)
1684 Loc : constant Source_Ptr := Sloc (Def);
1685 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
1686 New_N : Node_Id;
1688 begin
1689 Set_Is_Generic_Type (T);
1691 if Private_Present (Def) then
1692 New_N :=
1693 Make_Private_Extension_Declaration (Loc,
1694 Defining_Identifier => T,
1695 Discriminant_Specifications => Discriminant_Specifications (N),
1696 Unknown_Discriminants_Present => Unk_Disc,
1697 Subtype_Indication => Subtype_Mark (Def),
1698 Interface_List => Interface_List (Def));
1700 Set_Abstract_Present (New_N, Abstract_Present (Def));
1701 Set_Limited_Present (New_N, Limited_Present (Def));
1702 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
1704 else
1705 New_N :=
1706 Make_Full_Type_Declaration (Loc,
1707 Defining_Identifier => T,
1708 Discriminant_Specifications =>
1709 Discriminant_Specifications (Parent (T)),
1710 Type_Definition =>
1711 Make_Derived_Type_Definition (Loc,
1712 Subtype_Indication => Subtype_Mark (Def)));
1714 Set_Abstract_Present
1715 (Type_Definition (New_N), Abstract_Present (Def));
1716 Set_Limited_Present
1717 (Type_Definition (New_N), Limited_Present (Def));
1718 end if;
1720 Rewrite (N, New_N);
1721 Analyze (N);
1723 if Unk_Disc then
1724 if not Is_Composite_Type (T) then
1725 Error_Msg_N
1726 ("unknown discriminants not allowed for elementary types", N);
1727 else
1728 Set_Has_Unknown_Discriminants (T);
1729 Set_Is_Constrained (T, False);
1730 end if;
1731 end if;
1733 -- If the parent type has a known size, so does the formal, which makes
1734 -- legal representation clauses that involve the formal.
1736 Set_Size_Known_At_Compile_Time
1737 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
1738 end Analyze_Formal_Derived_Type;
1740 ----------------------------------
1741 -- Analyze_Formal_Discrete_Type --
1742 ----------------------------------
1744 -- The operations defined for a discrete types are those of an enumeration
1745 -- type. The size is set to an arbitrary value, for use in analyzing the
1746 -- generic unit.
1748 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
1749 Loc : constant Source_Ptr := Sloc (Def);
1750 Lo : Node_Id;
1751 Hi : Node_Id;
1753 Base : constant Entity_Id :=
1754 New_Internal_Entity
1755 (E_Floating_Point_Type, Current_Scope,
1756 Sloc (Defining_Identifier (Parent (Def))), 'G');
1758 begin
1759 Enter_Name (T);
1760 Set_Ekind (T, E_Enumeration_Subtype);
1761 Set_Etype (T, Base);
1762 Init_Size (T, 8);
1763 Init_Alignment (T);
1764 Set_Is_Generic_Type (T);
1765 Set_Is_Constrained (T);
1767 -- For semantic analysis, the bounds of the type must be set to some
1768 -- non-static value. The simplest is to create attribute nodes for those
1769 -- bounds, that refer to the type itself. These bounds are never
1770 -- analyzed but serve as place-holders.
1772 Lo :=
1773 Make_Attribute_Reference (Loc,
1774 Attribute_Name => Name_First,
1775 Prefix => New_Reference_To (T, Loc));
1776 Set_Etype (Lo, T);
1778 Hi :=
1779 Make_Attribute_Reference (Loc,
1780 Attribute_Name => Name_Last,
1781 Prefix => New_Reference_To (T, Loc));
1782 Set_Etype (Hi, T);
1784 Set_Scalar_Range (T,
1785 Make_Range (Loc,
1786 Low_Bound => Lo,
1787 High_Bound => Hi));
1789 Set_Ekind (Base, E_Enumeration_Type);
1790 Set_Etype (Base, Base);
1791 Init_Size (Base, 8);
1792 Init_Alignment (Base);
1793 Set_Is_Generic_Type (Base);
1794 Set_Scalar_Range (Base, Scalar_Range (T));
1795 Set_Parent (Base, Parent (Def));
1796 end Analyze_Formal_Discrete_Type;
1798 ----------------------------------
1799 -- Analyze_Formal_Floating_Type --
1800 ---------------------------------
1802 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
1803 Base : constant Entity_Id :=
1804 New_Internal_Entity
1805 (E_Floating_Point_Type, Current_Scope,
1806 Sloc (Defining_Identifier (Parent (Def))), 'G');
1808 begin
1809 -- The various semantic attributes are taken from the predefined type
1810 -- Float, just so that all of them are initialized. Their values are
1811 -- never used because no constant folding or expansion takes place in
1812 -- the generic itself.
1814 Enter_Name (T);
1815 Set_Ekind (T, E_Floating_Point_Subtype);
1816 Set_Etype (T, Base);
1817 Set_Size_Info (T, (Standard_Float));
1818 Set_RM_Size (T, RM_Size (Standard_Float));
1819 Set_Digits_Value (T, Digits_Value (Standard_Float));
1820 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
1821 Set_Is_Constrained (T);
1823 Set_Is_Generic_Type (Base);
1824 Set_Etype (Base, Base);
1825 Set_Size_Info (Base, (Standard_Float));
1826 Set_RM_Size (Base, RM_Size (Standard_Float));
1827 Set_Digits_Value (Base, Digits_Value (Standard_Float));
1828 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
1829 Set_Parent (Base, Parent (Def));
1831 Check_Restriction (No_Floating_Point, Def);
1832 end Analyze_Formal_Floating_Type;
1834 -----------------------------------
1835 -- Analyze_Formal_Interface_Type;--
1836 -----------------------------------
1838 procedure Analyze_Formal_Interface_Type
1839 (N : Node_Id;
1840 T : Entity_Id;
1841 Def : Node_Id)
1843 Loc : constant Source_Ptr := Sloc (N);
1844 New_N : Node_Id;
1846 begin
1847 New_N :=
1848 Make_Full_Type_Declaration (Loc,
1849 Defining_Identifier => T,
1850 Type_Definition => Def);
1852 Rewrite (N, New_N);
1853 Analyze (N);
1854 Set_Is_Generic_Type (T);
1855 end Analyze_Formal_Interface_Type;
1857 ---------------------------------
1858 -- Analyze_Formal_Modular_Type --
1859 ---------------------------------
1861 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
1862 begin
1863 -- Apart from their entity kind, generic modular types are treated like
1864 -- signed integer types, and have the same attributes.
1866 Analyze_Formal_Signed_Integer_Type (T, Def);
1867 Set_Ekind (T, E_Modular_Integer_Subtype);
1868 Set_Ekind (Etype (T), E_Modular_Integer_Type);
1870 end Analyze_Formal_Modular_Type;
1872 ---------------------------------------
1873 -- Analyze_Formal_Object_Declaration --
1874 ---------------------------------------
1876 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
1877 E : constant Node_Id := Default_Expression (N);
1878 Id : constant Node_Id := Defining_Identifier (N);
1879 K : Entity_Kind;
1880 T : Node_Id;
1882 begin
1883 Enter_Name (Id);
1885 -- Determine the mode of the formal object
1887 if Out_Present (N) then
1888 K := E_Generic_In_Out_Parameter;
1890 if not In_Present (N) then
1891 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
1892 end if;
1894 else
1895 K := E_Generic_In_Parameter;
1896 end if;
1898 if Present (Subtype_Mark (N)) then
1899 Find_Type (Subtype_Mark (N));
1900 T := Entity (Subtype_Mark (N));
1902 -- Verify that there is no redundant null exclusion
1904 if Null_Exclusion_Present (N) then
1905 if not Is_Access_Type (T) then
1906 Error_Msg_N
1907 ("null exclusion can only apply to an access type", N);
1909 elsif Can_Never_Be_Null (T) then
1910 Error_Msg_NE
1911 ("`NOT NULL` not allowed (& already excludes null)",
1912 N, T);
1913 end if;
1914 end if;
1916 -- Ada 2005 (AI-423): Formal object with an access definition
1918 else
1919 Check_Access_Definition (N);
1920 T := Access_Definition
1921 (Related_Nod => N,
1922 N => Access_Definition (N));
1923 end if;
1925 if Ekind (T) = E_Incomplete_Type then
1926 declare
1927 Error_Node : Node_Id;
1929 begin
1930 if Present (Subtype_Mark (N)) then
1931 Error_Node := Subtype_Mark (N);
1932 else
1933 Check_Access_Definition (N);
1934 Error_Node := Access_Definition (N);
1935 end if;
1937 Error_Msg_N ("premature usage of incomplete type", Error_Node);
1938 end;
1939 end if;
1941 if K = E_Generic_In_Parameter then
1943 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
1945 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
1946 Error_Msg_N
1947 ("generic formal of mode IN must not be of limited type", N);
1948 Explain_Limited_Type (T, N);
1949 end if;
1951 if Is_Abstract_Type (T) then
1952 Error_Msg_N
1953 ("generic formal of mode IN must not be of abstract type", N);
1954 end if;
1956 if Present (E) then
1957 Preanalyze_Spec_Expression (E, T);
1959 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
1960 Error_Msg_N
1961 ("initialization not allowed for limited types", E);
1962 Explain_Limited_Type (T, E);
1963 end if;
1964 end if;
1966 Set_Ekind (Id, K);
1967 Set_Etype (Id, T);
1969 -- Case of generic IN OUT parameter
1971 else
1972 -- If the formal has an unconstrained type, construct its actual
1973 -- subtype, as is done for subprogram formals. In this fashion, all
1974 -- its uses can refer to specific bounds.
1976 Set_Ekind (Id, K);
1977 Set_Etype (Id, T);
1979 if (Is_Array_Type (T)
1980 and then not Is_Constrained (T))
1981 or else
1982 (Ekind (T) = E_Record_Type
1983 and then Has_Discriminants (T))
1984 then
1985 declare
1986 Non_Freezing_Ref : constant Node_Id :=
1987 New_Reference_To (Id, Sloc (Id));
1988 Decl : Node_Id;
1990 begin
1991 -- Make sure the actual subtype doesn't generate bogus freezing
1993 Set_Must_Not_Freeze (Non_Freezing_Ref);
1994 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
1995 Insert_Before_And_Analyze (N, Decl);
1996 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
1997 end;
1998 else
1999 Set_Actual_Subtype (Id, T);
2000 end if;
2002 if Present (E) then
2003 Error_Msg_N
2004 ("initialization not allowed for `IN OUT` formals", N);
2005 end if;
2006 end if;
2008 if Has_Aspects (N) then
2009 Analyze_Aspect_Specifications (N, Id);
2010 end if;
2011 end Analyze_Formal_Object_Declaration;
2013 ----------------------------------------------
2014 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2015 ----------------------------------------------
2017 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2018 (T : Entity_Id;
2019 Def : Node_Id)
2021 Loc : constant Source_Ptr := Sloc (Def);
2022 Base : constant Entity_Id :=
2023 New_Internal_Entity
2024 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2025 Sloc (Defining_Identifier (Parent (Def))), 'G');
2027 begin
2028 -- The semantic attributes are set for completeness only, their values
2029 -- will never be used, since all properties of the type are non-static.
2031 Enter_Name (T);
2032 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2033 Set_Etype (T, Base);
2034 Set_Size_Info (T, Standard_Integer);
2035 Set_RM_Size (T, RM_Size (Standard_Integer));
2036 Set_Small_Value (T, Ureal_1);
2037 Set_Delta_Value (T, Ureal_1);
2038 Set_Scalar_Range (T,
2039 Make_Range (Loc,
2040 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2041 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2042 Set_Is_Constrained (T);
2044 Set_Is_Generic_Type (Base);
2045 Set_Etype (Base, Base);
2046 Set_Size_Info (Base, Standard_Integer);
2047 Set_RM_Size (Base, RM_Size (Standard_Integer));
2048 Set_Small_Value (Base, Ureal_1);
2049 Set_Delta_Value (Base, Ureal_1);
2050 Set_Scalar_Range (Base, Scalar_Range (T));
2051 Set_Parent (Base, Parent (Def));
2053 Check_Restriction (No_Fixed_Point, Def);
2054 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2056 ----------------------------------------
2057 -- Analyze_Formal_Package_Declaration --
2058 ----------------------------------------
2060 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2061 Loc : constant Source_Ptr := Sloc (N);
2062 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2063 Formal : Entity_Id;
2064 Gen_Id : constant Node_Id := Name (N);
2065 Gen_Decl : Node_Id;
2066 Gen_Unit : Entity_Id;
2067 New_N : Node_Id;
2068 Parent_Installed : Boolean := False;
2069 Renaming : Node_Id;
2070 Parent_Instance : Entity_Id;
2071 Renaming_In_Par : Entity_Id;
2072 Associations : Boolean := True;
2074 Vis_Prims_List : Elist_Id := No_Elist;
2075 -- List of primitives made temporarily visible in the instantiation
2076 -- to match the visibility of the formal type
2078 function Build_Local_Package return Node_Id;
2079 -- The formal package is rewritten so that its parameters are replaced
2080 -- with corresponding declarations. For parameters with bona fide
2081 -- associations these declarations are created by Analyze_Associations
2082 -- as for a regular instantiation. For boxed parameters, we preserve
2083 -- the formal declarations and analyze them, in order to introduce
2084 -- entities of the right kind in the environment of the formal.
2086 -------------------------
2087 -- Build_Local_Package --
2088 -------------------------
2090 function Build_Local_Package return Node_Id is
2091 Decls : List_Id;
2092 Pack_Decl : Node_Id;
2094 begin
2095 -- Within the formal, the name of the generic package is a renaming
2096 -- of the formal (as for a regular instantiation).
2098 Pack_Decl :=
2099 Make_Package_Declaration (Loc,
2100 Specification =>
2101 Copy_Generic_Node
2102 (Specification (Original_Node (Gen_Decl)),
2103 Empty, Instantiating => True));
2105 Renaming := Make_Package_Renaming_Declaration (Loc,
2106 Defining_Unit_Name =>
2107 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2108 Name => New_Occurrence_Of (Formal, Loc));
2110 if Nkind (Gen_Id) = N_Identifier
2111 and then Chars (Gen_Id) = Chars (Pack_Id)
2112 then
2113 Error_Msg_NE
2114 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2115 end if;
2117 -- If the formal is declared with a box, or with an others choice,
2118 -- create corresponding declarations for all entities in the formal
2119 -- part, so that names with the proper types are available in the
2120 -- specification of the formal package.
2122 -- On the other hand, if there are no associations, then all the
2123 -- formals must have defaults, and this will be checked by the
2124 -- call to Analyze_Associations.
2126 if Box_Present (N)
2127 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2128 then
2129 declare
2130 Formal_Decl : Node_Id;
2132 begin
2133 -- TBA : for a formal package, need to recurse ???
2135 Decls := New_List;
2136 Formal_Decl :=
2137 First
2138 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2139 while Present (Formal_Decl) loop
2140 Append_To
2141 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2142 Next (Formal_Decl);
2143 end loop;
2144 end;
2146 -- If generic associations are present, use Analyze_Associations to
2147 -- create the proper renaming declarations.
2149 else
2150 declare
2151 Act_Tree : constant Node_Id :=
2152 Copy_Generic_Node
2153 (Original_Node (Gen_Decl), Empty,
2154 Instantiating => True);
2156 begin
2157 Generic_Renamings.Set_Last (0);
2158 Generic_Renamings_HTable.Reset;
2159 Instantiation_Node := N;
2161 Decls :=
2162 Analyze_Associations
2163 (I_Node => Original_Node (N),
2164 Formals => Generic_Formal_Declarations (Act_Tree),
2165 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2167 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2168 end;
2169 end if;
2171 Append (Renaming, To => Decls);
2173 -- Add generated declarations ahead of local declarations in
2174 -- the package.
2176 if No (Visible_Declarations (Specification (Pack_Decl))) then
2177 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2178 else
2179 Insert_List_Before
2180 (First (Visible_Declarations (Specification (Pack_Decl))),
2181 Decls);
2182 end if;
2184 return Pack_Decl;
2185 end Build_Local_Package;
2187 -- Start of processing for Analyze_Formal_Package_Declaration
2189 begin
2190 Text_IO_Kludge (Gen_Id);
2192 Init_Env;
2193 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2194 Gen_Unit := Entity (Gen_Id);
2196 -- Check for a formal package that is a package renaming
2198 if Present (Renamed_Object (Gen_Unit)) then
2200 -- Indicate that unit is used, before replacing it with renamed
2201 -- entity for use below.
2203 if In_Extended_Main_Source_Unit (N) then
2204 Set_Is_Instantiated (Gen_Unit);
2205 Generate_Reference (Gen_Unit, N);
2206 end if;
2208 Gen_Unit := Renamed_Object (Gen_Unit);
2209 end if;
2211 if Ekind (Gen_Unit) /= E_Generic_Package then
2212 Error_Msg_N ("expect generic package name", Gen_Id);
2213 Restore_Env;
2214 goto Leave;
2216 elsif Gen_Unit = Current_Scope then
2217 Error_Msg_N
2218 ("generic package cannot be used as a formal package of itself",
2219 Gen_Id);
2220 Restore_Env;
2221 goto Leave;
2223 elsif In_Open_Scopes (Gen_Unit) then
2224 if Is_Compilation_Unit (Gen_Unit)
2225 and then Is_Child_Unit (Current_Scope)
2226 then
2227 -- Special-case the error when the formal is a parent, and
2228 -- continue analysis to minimize cascaded errors.
2230 Error_Msg_N
2231 ("generic parent cannot be used as formal package "
2232 & "of a child unit",
2233 Gen_Id);
2235 else
2236 Error_Msg_N
2237 ("generic package cannot be used as a formal package "
2238 & "within itself",
2239 Gen_Id);
2240 Restore_Env;
2241 goto Leave;
2242 end if;
2243 end if;
2245 -- Check that name of formal package does not hide name of generic,
2246 -- or its leading prefix. This check must be done separately because
2247 -- the name of the generic has already been analyzed.
2249 declare
2250 Gen_Name : Entity_Id;
2252 begin
2253 Gen_Name := Gen_Id;
2254 while Nkind (Gen_Name) = N_Expanded_Name loop
2255 Gen_Name := Prefix (Gen_Name);
2256 end loop;
2258 if Chars (Gen_Name) = Chars (Pack_Id) then
2259 Error_Msg_NE
2260 ("& is hidden within declaration of formal package",
2261 Gen_Id, Gen_Name);
2262 end if;
2263 end;
2265 if Box_Present (N)
2266 or else No (Generic_Associations (N))
2267 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2268 then
2269 Associations := False;
2270 end if;
2272 -- If there are no generic associations, the generic parameters appear
2273 -- as local entities and are instantiated like them. We copy the generic
2274 -- package declaration as if it were an instantiation, and analyze it
2275 -- like a regular package, except that we treat the formals as
2276 -- additional visible components.
2278 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2280 if In_Extended_Main_Source_Unit (N) then
2281 Set_Is_Instantiated (Gen_Unit);
2282 Generate_Reference (Gen_Unit, N);
2283 end if;
2285 Formal := New_Copy (Pack_Id);
2286 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2288 begin
2289 -- Make local generic without formals. The formals will be replaced
2290 -- with internal declarations.
2292 New_N := Build_Local_Package;
2294 -- If there are errors in the parameter list, Analyze_Associations
2295 -- raises Instantiation_Error. Patch the declaration to prevent
2296 -- further exception propagation.
2298 exception
2299 when Instantiation_Error =>
2301 Enter_Name (Formal);
2302 Set_Ekind (Formal, E_Variable);
2303 Set_Etype (Formal, Any_Type);
2304 Restore_Hidden_Primitives (Vis_Prims_List);
2306 if Parent_Installed then
2307 Remove_Parent;
2308 end if;
2310 goto Leave;
2311 end;
2313 Rewrite (N, New_N);
2314 Set_Defining_Unit_Name (Specification (New_N), Formal);
2315 Set_Generic_Parent (Specification (N), Gen_Unit);
2316 Set_Instance_Env (Gen_Unit, Formal);
2317 Set_Is_Generic_Instance (Formal);
2319 Enter_Name (Formal);
2320 Set_Ekind (Formal, E_Package);
2321 Set_Etype (Formal, Standard_Void_Type);
2322 Set_Inner_Instances (Formal, New_Elmt_List);
2323 Push_Scope (Formal);
2325 if Is_Child_Unit (Gen_Unit)
2326 and then Parent_Installed
2327 then
2328 -- Similarly, we have to make the name of the formal visible in the
2329 -- parent instance, to resolve properly fully qualified names that
2330 -- may appear in the generic unit. The parent instance has been
2331 -- placed on the scope stack ahead of the current scope.
2333 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2335 Renaming_In_Par :=
2336 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2337 Set_Ekind (Renaming_In_Par, E_Package);
2338 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2339 Set_Scope (Renaming_In_Par, Parent_Instance);
2340 Set_Parent (Renaming_In_Par, Parent (Formal));
2341 Set_Renamed_Object (Renaming_In_Par, Formal);
2342 Append_Entity (Renaming_In_Par, Parent_Instance);
2343 end if;
2345 Analyze (Specification (N));
2347 -- The formals for which associations are provided are not visible
2348 -- outside of the formal package. The others are still declared by a
2349 -- formal parameter declaration.
2351 -- If there are no associations, the only local entity to hide is the
2352 -- generated package renaming itself.
2354 declare
2355 E : Entity_Id;
2357 begin
2358 E := First_Entity (Formal);
2359 while Present (E) loop
2360 if Associations
2361 and then not Is_Generic_Formal (E)
2362 then
2363 Set_Is_Hidden (E);
2364 end if;
2366 if Ekind (E) = E_Package
2367 and then Renamed_Entity (E) = Formal
2368 then
2369 Set_Is_Hidden (E);
2370 exit;
2371 end if;
2373 Next_Entity (E);
2374 end loop;
2375 end;
2377 End_Package_Scope (Formal);
2378 Restore_Hidden_Primitives (Vis_Prims_List);
2380 if Parent_Installed then
2381 Remove_Parent;
2382 end if;
2384 Restore_Env;
2386 -- Inside the generic unit, the formal package is a regular package, but
2387 -- no body is needed for it. Note that after instantiation, the defining
2388 -- unit name we need is in the new tree and not in the original (see
2389 -- Package_Instantiation). A generic formal package is an instance, and
2390 -- can be used as an actual for an inner instance.
2392 Set_Has_Completion (Formal, True);
2394 -- Add semantic information to the original defining identifier.
2395 -- for ASIS use.
2397 Set_Ekind (Pack_Id, E_Package);
2398 Set_Etype (Pack_Id, Standard_Void_Type);
2399 Set_Scope (Pack_Id, Scope (Formal));
2400 Set_Has_Completion (Pack_Id, True);
2402 <<Leave>>
2403 if Has_Aspects (N) then
2404 Analyze_Aspect_Specifications (N, Pack_Id);
2405 end if;
2406 end Analyze_Formal_Package_Declaration;
2408 ---------------------------------
2409 -- Analyze_Formal_Private_Type --
2410 ---------------------------------
2412 procedure Analyze_Formal_Private_Type
2413 (N : Node_Id;
2414 T : Entity_Id;
2415 Def : Node_Id)
2417 begin
2418 New_Private_Type (N, T, Def);
2420 -- Set the size to an arbitrary but legal value
2422 Set_Size_Info (T, Standard_Integer);
2423 Set_RM_Size (T, RM_Size (Standard_Integer));
2424 end Analyze_Formal_Private_Type;
2426 ------------------------------------
2427 -- Analyze_Formal_Incomplete_Type --
2428 ------------------------------------
2430 procedure Analyze_Formal_Incomplete_Type
2431 (T : Entity_Id;
2432 Def : Node_Id)
2434 begin
2435 Enter_Name (T);
2436 Set_Ekind (T, E_Incomplete_Type);
2437 Set_Etype (T, T);
2438 Set_Private_Dependents (T, New_Elmt_List);
2440 if Tagged_Present (Def) then
2441 Set_Is_Tagged_Type (T);
2442 Make_Class_Wide_Type (T);
2443 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2444 end if;
2445 end Analyze_Formal_Incomplete_Type;
2447 ----------------------------------------
2448 -- Analyze_Formal_Signed_Integer_Type --
2449 ----------------------------------------
2451 procedure Analyze_Formal_Signed_Integer_Type
2452 (T : Entity_Id;
2453 Def : Node_Id)
2455 Base : constant Entity_Id :=
2456 New_Internal_Entity
2457 (E_Signed_Integer_Type,
2458 Current_Scope,
2459 Sloc (Defining_Identifier (Parent (Def))), 'G');
2461 begin
2462 Enter_Name (T);
2464 Set_Ekind (T, E_Signed_Integer_Subtype);
2465 Set_Etype (T, Base);
2466 Set_Size_Info (T, Standard_Integer);
2467 Set_RM_Size (T, RM_Size (Standard_Integer));
2468 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2469 Set_Is_Constrained (T);
2471 Set_Is_Generic_Type (Base);
2472 Set_Size_Info (Base, Standard_Integer);
2473 Set_RM_Size (Base, RM_Size (Standard_Integer));
2474 Set_Etype (Base, Base);
2475 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2476 Set_Parent (Base, Parent (Def));
2477 end Analyze_Formal_Signed_Integer_Type;
2479 -------------------------------------------
2480 -- Analyze_Formal_Subprogram_Declaration --
2481 -------------------------------------------
2483 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2484 Spec : constant Node_Id := Specification (N);
2485 Def : constant Node_Id := Default_Name (N);
2486 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2487 Subp : Entity_Id;
2489 begin
2490 if Nam = Error then
2491 return;
2492 end if;
2494 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2495 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2496 goto Leave;
2497 end if;
2499 Analyze_Subprogram_Declaration (N);
2500 Set_Is_Formal_Subprogram (Nam);
2501 Set_Has_Completion (Nam);
2503 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2504 Set_Is_Abstract_Subprogram (Nam);
2505 Set_Is_Dispatching_Operation (Nam);
2507 declare
2508 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2509 begin
2510 if No (Ctrl_Type) then
2511 Error_Msg_N
2512 ("abstract formal subprogram must have a controlling type",
2514 else
2515 Check_Controlling_Formals (Ctrl_Type, Nam);
2516 end if;
2517 end;
2518 end if;
2520 -- Default name is resolved at the point of instantiation
2522 if Box_Present (N) then
2523 null;
2525 -- Else default is bound at the point of generic declaration
2527 elsif Present (Def) then
2528 if Nkind (Def) = N_Operator_Symbol then
2529 Find_Direct_Name (Def);
2531 elsif Nkind (Def) /= N_Attribute_Reference then
2532 Analyze (Def);
2534 else
2535 -- For an attribute reference, analyze the prefix and verify
2536 -- that it has the proper profile for the subprogram.
2538 Analyze (Prefix (Def));
2539 Valid_Default_Attribute (Nam, Def);
2540 goto Leave;
2541 end if;
2543 -- Default name may be overloaded, in which case the interpretation
2544 -- with the correct profile must be selected, as for a renaming.
2545 -- If the definition is an indexed component, it must denote a
2546 -- member of an entry family. If it is a selected component, it
2547 -- can be a protected operation.
2549 if Etype (Def) = Any_Type then
2550 goto Leave;
2552 elsif Nkind (Def) = N_Selected_Component then
2553 if not Is_Overloadable (Entity (Selector_Name (Def))) then
2554 Error_Msg_N ("expect valid subprogram name as default", Def);
2555 end if;
2557 elsif Nkind (Def) = N_Indexed_Component then
2558 if Is_Entity_Name (Prefix (Def)) then
2559 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
2560 Error_Msg_N ("expect valid subprogram name as default", Def);
2561 end if;
2563 elsif Nkind (Prefix (Def)) = N_Selected_Component then
2564 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
2565 E_Entry_Family
2566 then
2567 Error_Msg_N ("expect valid subprogram name as default", Def);
2568 end if;
2570 else
2571 Error_Msg_N ("expect valid subprogram name as default", Def);
2572 goto Leave;
2573 end if;
2575 elsif Nkind (Def) = N_Character_Literal then
2577 -- Needs some type checks: subprogram should be parameterless???
2579 Resolve (Def, (Etype (Nam)));
2581 elsif not Is_Entity_Name (Def)
2582 or else not Is_Overloadable (Entity (Def))
2583 then
2584 Error_Msg_N ("expect valid subprogram name as default", Def);
2585 goto Leave;
2587 elsif not Is_Overloaded (Def) then
2588 Subp := Entity (Def);
2590 if Subp = Nam then
2591 Error_Msg_N ("premature usage of formal subprogram", Def);
2593 elsif not Entity_Matches_Spec (Subp, Nam) then
2594 Error_Msg_N ("no visible entity matches specification", Def);
2595 end if;
2597 -- More than one interpretation, so disambiguate as for a renaming
2599 else
2600 declare
2601 I : Interp_Index;
2602 I1 : Interp_Index := 0;
2603 It : Interp;
2604 It1 : Interp;
2606 begin
2607 Subp := Any_Id;
2608 Get_First_Interp (Def, I, It);
2609 while Present (It.Nam) loop
2610 if Entity_Matches_Spec (It.Nam, Nam) then
2611 if Subp /= Any_Id then
2612 It1 := Disambiguate (Def, I1, I, Etype (Subp));
2614 if It1 = No_Interp then
2615 Error_Msg_N ("ambiguous default subprogram", Def);
2616 else
2617 Subp := It1.Nam;
2618 end if;
2620 exit;
2622 else
2623 I1 := I;
2624 Subp := It.Nam;
2625 end if;
2626 end if;
2628 Get_Next_Interp (I, It);
2629 end loop;
2630 end;
2632 if Subp /= Any_Id then
2634 -- Subprogram found, generate reference to it
2636 Set_Entity (Def, Subp);
2637 Generate_Reference (Subp, Def);
2639 if Subp = Nam then
2640 Error_Msg_N ("premature usage of formal subprogram", Def);
2642 elsif Ekind (Subp) /= E_Operator then
2643 Check_Mode_Conformant (Subp, Nam);
2644 end if;
2646 else
2647 Error_Msg_N ("no visible subprogram matches specification", N);
2648 end if;
2649 end if;
2650 end if;
2652 <<Leave>>
2653 if Has_Aspects (N) then
2654 Analyze_Aspect_Specifications (N, Nam);
2655 end if;
2657 end Analyze_Formal_Subprogram_Declaration;
2659 -------------------------------------
2660 -- Analyze_Formal_Type_Declaration --
2661 -------------------------------------
2663 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
2664 Def : constant Node_Id := Formal_Type_Definition (N);
2665 T : Entity_Id;
2667 begin
2668 T := Defining_Identifier (N);
2670 if Present (Discriminant_Specifications (N))
2671 and then Nkind (Def) /= N_Formal_Private_Type_Definition
2672 then
2673 Error_Msg_N
2674 ("discriminants not allowed for this formal type", T);
2675 end if;
2677 -- Enter the new name, and branch to specific routine
2679 case Nkind (Def) is
2680 when N_Formal_Private_Type_Definition =>
2681 Analyze_Formal_Private_Type (N, T, Def);
2683 when N_Formal_Derived_Type_Definition =>
2684 Analyze_Formal_Derived_Type (N, T, Def);
2686 when N_Formal_Incomplete_Type_Definition =>
2687 Analyze_Formal_Incomplete_Type (T, Def);
2689 when N_Formal_Discrete_Type_Definition =>
2690 Analyze_Formal_Discrete_Type (T, Def);
2692 when N_Formal_Signed_Integer_Type_Definition =>
2693 Analyze_Formal_Signed_Integer_Type (T, Def);
2695 when N_Formal_Modular_Type_Definition =>
2696 Analyze_Formal_Modular_Type (T, Def);
2698 when N_Formal_Floating_Point_Definition =>
2699 Analyze_Formal_Floating_Type (T, Def);
2701 when N_Formal_Ordinary_Fixed_Point_Definition =>
2702 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
2704 when N_Formal_Decimal_Fixed_Point_Definition =>
2705 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
2707 when N_Array_Type_Definition =>
2708 Analyze_Formal_Array_Type (T, Def);
2710 when N_Access_To_Object_Definition |
2711 N_Access_Function_Definition |
2712 N_Access_Procedure_Definition =>
2713 Analyze_Generic_Access_Type (T, Def);
2715 -- Ada 2005: a interface declaration is encoded as an abstract
2716 -- record declaration or a abstract type derivation.
2718 when N_Record_Definition =>
2719 Analyze_Formal_Interface_Type (N, T, Def);
2721 when N_Derived_Type_Definition =>
2722 Analyze_Formal_Derived_Interface_Type (N, T, Def);
2724 when N_Error =>
2725 null;
2727 when others =>
2728 raise Program_Error;
2730 end case;
2732 Set_Is_Generic_Type (T);
2734 if Has_Aspects (N) then
2735 Analyze_Aspect_Specifications (N, T);
2736 end if;
2737 end Analyze_Formal_Type_Declaration;
2739 ------------------------------------
2740 -- Analyze_Function_Instantiation --
2741 ------------------------------------
2743 procedure Analyze_Function_Instantiation (N : Node_Id) is
2744 begin
2745 Analyze_Subprogram_Instantiation (N, E_Function);
2746 end Analyze_Function_Instantiation;
2748 ---------------------------------
2749 -- Analyze_Generic_Access_Type --
2750 ---------------------------------
2752 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
2753 begin
2754 Enter_Name (T);
2756 if Nkind (Def) = N_Access_To_Object_Definition then
2757 Access_Type_Declaration (T, Def);
2759 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
2760 and then No (Full_View (Designated_Type (T)))
2761 and then not Is_Generic_Type (Designated_Type (T))
2762 then
2763 Error_Msg_N ("premature usage of incomplete type", Def);
2765 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
2766 Error_Msg_N
2767 ("only a subtype mark is allowed in a formal", Def);
2768 end if;
2770 else
2771 Access_Subprogram_Declaration (T, Def);
2772 end if;
2773 end Analyze_Generic_Access_Type;
2775 ---------------------------------
2776 -- Analyze_Generic_Formal_Part --
2777 ---------------------------------
2779 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
2780 Gen_Parm_Decl : Node_Id;
2782 begin
2783 -- The generic formals are processed in the scope of the generic unit,
2784 -- where they are immediately visible. The scope is installed by the
2785 -- caller.
2787 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
2789 while Present (Gen_Parm_Decl) loop
2790 Analyze (Gen_Parm_Decl);
2791 Next (Gen_Parm_Decl);
2792 end loop;
2794 Generate_Reference_To_Generic_Formals (Current_Scope);
2795 end Analyze_Generic_Formal_Part;
2797 ------------------------------------------
2798 -- Analyze_Generic_Package_Declaration --
2799 ------------------------------------------
2801 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
2802 Loc : constant Source_Ptr := Sloc (N);
2803 Id : Entity_Id;
2804 New_N : Node_Id;
2805 Save_Parent : Node_Id;
2806 Renaming : Node_Id;
2807 Decls : constant List_Id :=
2808 Visible_Declarations (Specification (N));
2809 Decl : Node_Id;
2811 begin
2812 Check_SPARK_Restriction ("generic is not allowed", N);
2814 -- We introduce a renaming of the enclosing package, to have a usable
2815 -- entity as the prefix of an expanded name for a local entity of the
2816 -- form Par.P.Q, where P is the generic package. This is because a local
2817 -- entity named P may hide it, so that the usual visibility rules in
2818 -- the instance will not resolve properly.
2820 Renaming :=
2821 Make_Package_Renaming_Declaration (Loc,
2822 Defining_Unit_Name =>
2823 Make_Defining_Identifier (Loc,
2824 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
2825 Name => Make_Identifier (Loc, Chars (Defining_Entity (N))));
2827 if Present (Decls) then
2828 Decl := First (Decls);
2829 while Present (Decl)
2830 and then Nkind (Decl) = N_Pragma
2831 loop
2832 Next (Decl);
2833 end loop;
2835 if Present (Decl) then
2836 Insert_Before (Decl, Renaming);
2837 else
2838 Append (Renaming, Visible_Declarations (Specification (N)));
2839 end if;
2841 else
2842 Set_Visible_Declarations (Specification (N), New_List (Renaming));
2843 end if;
2845 -- Create copy of generic unit, and save for instantiation. If the unit
2846 -- is a child unit, do not copy the specifications for the parent, which
2847 -- are not part of the generic tree.
2849 Save_Parent := Parent_Spec (N);
2850 Set_Parent_Spec (N, Empty);
2852 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2853 Set_Parent_Spec (New_N, Save_Parent);
2854 Rewrite (N, New_N);
2855 Id := Defining_Entity (N);
2856 Generate_Definition (Id);
2858 -- Expansion is not applied to generic units
2860 Start_Generic;
2862 Enter_Name (Id);
2863 Set_Ekind (Id, E_Generic_Package);
2864 Set_Etype (Id, Standard_Void_Type);
2865 Push_Scope (Id);
2866 Enter_Generic_Scope (Id);
2867 Set_Inner_Instances (Id, New_Elmt_List);
2869 Set_Categorization_From_Pragmas (N);
2870 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2872 -- Link the declaration of the generic homonym in the generic copy to
2873 -- the package it renames, so that it is always resolved properly.
2875 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
2876 Set_Entity (Associated_Node (Name (Renaming)), Id);
2878 -- For a library unit, we have reconstructed the entity for the unit,
2879 -- and must reset it in the library tables.
2881 if Nkind (Parent (N)) = N_Compilation_Unit then
2882 Set_Cunit_Entity (Current_Sem_Unit, Id);
2883 end if;
2885 Analyze_Generic_Formal_Part (N);
2887 -- After processing the generic formals, analysis proceeds as for a
2888 -- non-generic package.
2890 Analyze (Specification (N));
2892 Validate_Categorization_Dependency (N, Id);
2894 End_Generic;
2896 End_Package_Scope (Id);
2897 Exit_Generic_Scope (Id);
2899 if Nkind (Parent (N)) /= N_Compilation_Unit then
2900 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
2901 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
2902 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
2904 else
2905 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2906 Validate_RT_RAT_Component (N);
2908 -- If this is a spec without a body, check that generic parameters
2909 -- are referenced.
2911 if not Body_Required (Parent (N)) then
2912 Check_References (Id);
2913 end if;
2914 end if;
2916 if Has_Aspects (N) then
2917 Analyze_Aspect_Specifications (N, Id);
2918 end if;
2919 end Analyze_Generic_Package_Declaration;
2921 --------------------------------------------
2922 -- Analyze_Generic_Subprogram_Declaration --
2923 --------------------------------------------
2925 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
2926 Spec : Node_Id;
2927 Id : Entity_Id;
2928 Formals : List_Id;
2929 New_N : Node_Id;
2930 Result_Type : Entity_Id;
2931 Save_Parent : Node_Id;
2932 Typ : Entity_Id;
2934 begin
2935 Check_SPARK_Restriction ("generic is not allowed", N);
2937 -- Create copy of generic unit, and save for instantiation. If the unit
2938 -- is a child unit, do not copy the specifications for the parent, which
2939 -- are not part of the generic tree.
2941 Save_Parent := Parent_Spec (N);
2942 Set_Parent_Spec (N, Empty);
2944 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2945 Set_Parent_Spec (New_N, Save_Parent);
2946 Rewrite (N, New_N);
2948 -- The aspect specifications are not attached to the tree, and must
2949 -- be copied and attached to the generic copy explicitly.
2951 if Present (Aspect_Specifications (New_N)) then
2952 declare
2953 Aspects : constant List_Id := Aspect_Specifications (N);
2954 begin
2955 Set_Has_Aspects (N, False);
2956 Move_Aspects (New_N, N);
2957 Set_Has_Aspects (Original_Node (N), False);
2958 Set_Aspect_Specifications (Original_Node (N), Aspects);
2959 end;
2960 end if;
2962 Spec := Specification (N);
2963 Id := Defining_Entity (Spec);
2964 Generate_Definition (Id);
2965 Set_Contract (Id, Make_Contract (Sloc (Id)));
2967 if Nkind (Id) = N_Defining_Operator_Symbol then
2968 Error_Msg_N
2969 ("operator symbol not allowed for generic subprogram", Id);
2970 end if;
2972 Start_Generic;
2974 Enter_Name (Id);
2976 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
2977 Push_Scope (Id);
2978 Enter_Generic_Scope (Id);
2979 Set_Inner_Instances (Id, New_Elmt_List);
2980 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2982 Analyze_Generic_Formal_Part (N);
2984 Formals := Parameter_Specifications (Spec);
2986 if Present (Formals) then
2987 Process_Formals (Formals, Spec);
2988 end if;
2990 if Nkind (Spec) = N_Function_Specification then
2991 Set_Ekind (Id, E_Generic_Function);
2993 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
2994 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
2995 Set_Etype (Id, Result_Type);
2997 -- Check restriction imposed by AI05-073: a generic function
2998 -- cannot return an abstract type or an access to such.
3000 -- This is a binding interpretation should it apply to earlier
3001 -- versions of Ada as well as Ada 2012???
3003 if Is_Abstract_Type (Designated_Type (Result_Type))
3004 and then Ada_Version >= Ada_2012
3005 then
3006 Error_Msg_N ("generic function cannot have an access result"
3007 & " that designates an abstract type", Spec);
3008 end if;
3010 else
3011 Find_Type (Result_Definition (Spec));
3012 Typ := Entity (Result_Definition (Spec));
3014 if Is_Abstract_Type (Typ)
3015 and then Ada_Version >= Ada_2012
3016 then
3017 Error_Msg_N
3018 ("generic function cannot have abstract result type", Spec);
3019 end if;
3021 -- If a null exclusion is imposed on the result type, then create
3022 -- a null-excluding itype (an access subtype) and use it as the
3023 -- function's Etype.
3025 if Is_Access_Type (Typ)
3026 and then Null_Exclusion_Present (Spec)
3027 then
3028 Set_Etype (Id,
3029 Create_Null_Excluding_Itype
3030 (T => Typ,
3031 Related_Nod => Spec,
3032 Scope_Id => Defining_Unit_Name (Spec)));
3033 else
3034 Set_Etype (Id, Typ);
3035 end if;
3036 end if;
3038 else
3039 Set_Ekind (Id, E_Generic_Procedure);
3040 Set_Etype (Id, Standard_Void_Type);
3041 end if;
3043 -- For a library unit, we have reconstructed the entity for the unit,
3044 -- and must reset it in the library tables. We also make sure that
3045 -- Body_Required is set properly in the original compilation unit node.
3047 if Nkind (Parent (N)) = N_Compilation_Unit then
3048 Set_Cunit_Entity (Current_Sem_Unit, Id);
3049 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3050 end if;
3052 Set_Categorization_From_Pragmas (N);
3053 Validate_Categorization_Dependency (N, Id);
3055 Save_Global_References (Original_Node (N));
3057 -- To capture global references, analyze the expressions of aspects,
3058 -- and propagate information to original tree. Note that in this case
3059 -- analysis of attributes is not delayed until the freeze point.
3061 -- It seems very hard to recreate the proper visibility of the generic
3062 -- subprogram at a later point because the analysis of an aspect may
3063 -- create pragmas after the generic copies have been made ???
3065 if Has_Aspects (N) then
3066 declare
3067 Aspect : Node_Id;
3069 begin
3070 Aspect := First (Aspect_Specifications (N));
3071 while Present (Aspect) loop
3072 if Get_Aspect_Id (Chars (Identifier (Aspect)))
3073 /= Aspect_Warnings
3074 then
3075 Analyze (Expression (Aspect));
3076 end if;
3077 Next (Aspect);
3078 end loop;
3080 Aspect := First (Aspect_Specifications (Original_Node (N)));
3081 while Present (Aspect) loop
3082 Save_Global_References (Expression (Aspect));
3083 Next (Aspect);
3084 end loop;
3085 end;
3086 end if;
3088 End_Generic;
3089 End_Scope;
3090 Exit_Generic_Scope (Id);
3091 Generate_Reference_To_Formals (Id);
3093 List_Inherited_Pre_Post_Aspects (Id);
3094 end Analyze_Generic_Subprogram_Declaration;
3096 -----------------------------------
3097 -- Analyze_Package_Instantiation --
3098 -----------------------------------
3100 procedure Analyze_Package_Instantiation (N : Node_Id) is
3101 Loc : constant Source_Ptr := Sloc (N);
3102 Gen_Id : constant Node_Id := Name (N);
3104 Act_Decl : Node_Id;
3105 Act_Decl_Name : Node_Id;
3106 Act_Decl_Id : Entity_Id;
3107 Act_Spec : Node_Id;
3108 Act_Tree : Node_Id;
3110 Gen_Decl : Node_Id;
3111 Gen_Unit : Entity_Id;
3113 Is_Actual_Pack : constant Boolean :=
3114 Is_Internal (Defining_Entity (N));
3116 Env_Installed : Boolean := False;
3117 Parent_Installed : Boolean := False;
3118 Renaming_List : List_Id;
3119 Unit_Renaming : Node_Id;
3120 Needs_Body : Boolean;
3121 Inline_Now : Boolean := False;
3123 Save_Style_Check : constant Boolean := Style_Check;
3124 -- Save style check mode for restore on exit
3126 procedure Delay_Descriptors (E : Entity_Id);
3127 -- Delay generation of subprogram descriptors for given entity
3129 function Might_Inline_Subp return Boolean;
3130 -- If inlining is active and the generic contains inlined subprograms,
3131 -- we instantiate the body. This may cause superfluous instantiations,
3132 -- but it is simpler than detecting the need for the body at the point
3133 -- of inlining, when the context of the instance is not available.
3135 -----------------------
3136 -- Delay_Descriptors --
3137 -----------------------
3139 procedure Delay_Descriptors (E : Entity_Id) is
3140 begin
3141 if not Delay_Subprogram_Descriptors (E) then
3142 Set_Delay_Subprogram_Descriptors (E);
3143 Pending_Descriptor.Append (E);
3144 end if;
3145 end Delay_Descriptors;
3147 -----------------------
3148 -- Might_Inline_Subp --
3149 -----------------------
3151 function Might_Inline_Subp return Boolean is
3152 E : Entity_Id;
3154 begin
3155 if not Inline_Processing_Required then
3156 return False;
3158 else
3159 E := First_Entity (Gen_Unit);
3160 while Present (E) loop
3161 if Is_Subprogram (E)
3162 and then Is_Inlined (E)
3163 then
3164 return True;
3165 end if;
3167 Next_Entity (E);
3168 end loop;
3169 end if;
3171 return False;
3172 end Might_Inline_Subp;
3174 -- Local declarations
3176 Vis_Prims_List : Elist_Id := No_Elist;
3177 -- List of primitives made temporarily visible in the instantiation
3178 -- to match the visibility of the formal type
3180 -- Start of processing for Analyze_Package_Instantiation
3182 begin
3183 Check_SPARK_Restriction ("generic is not allowed", N);
3185 -- Very first thing: apply the special kludge for Text_IO processing
3186 -- in case we are instantiating one of the children of [Wide_]Text_IO.
3188 Text_IO_Kludge (Name (N));
3190 -- Make node global for error reporting
3192 Instantiation_Node := N;
3194 -- Turn off style checking in instances. If the check is enabled on the
3195 -- generic unit, a warning in an instance would just be noise. If not
3196 -- enabled on the generic, then a warning in an instance is just wrong.
3198 Style_Check := False;
3200 -- Case of instantiation of a generic package
3202 if Nkind (N) = N_Package_Instantiation then
3203 Act_Decl_Id := New_Copy (Defining_Entity (N));
3204 Set_Comes_From_Source (Act_Decl_Id, True);
3206 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3207 Act_Decl_Name :=
3208 Make_Defining_Program_Unit_Name (Loc,
3209 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
3210 Defining_Identifier => Act_Decl_Id);
3211 else
3212 Act_Decl_Name := Act_Decl_Id;
3213 end if;
3215 -- Case of instantiation of a formal package
3217 else
3218 Act_Decl_Id := Defining_Identifier (N);
3219 Act_Decl_Name := Act_Decl_Id;
3220 end if;
3222 Generate_Definition (Act_Decl_Id);
3223 Preanalyze_Actuals (N);
3225 Init_Env;
3226 Env_Installed := True;
3228 -- Reset renaming map for formal types. The mapping is established
3229 -- when analyzing the generic associations, but some mappings are
3230 -- inherited from formal packages of parent units, and these are
3231 -- constructed when the parents are installed.
3233 Generic_Renamings.Set_Last (0);
3234 Generic_Renamings_HTable.Reset;
3236 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3237 Gen_Unit := Entity (Gen_Id);
3239 -- Verify that it is the name of a generic package
3241 -- A visibility glitch: if the instance is a child unit and the generic
3242 -- is the generic unit of a parent instance (i.e. both the parent and
3243 -- the child units are instances of the same package) the name now
3244 -- denotes the renaming within the parent, not the intended generic
3245 -- unit. See if there is a homonym that is the desired generic. The
3246 -- renaming declaration must be visible inside the instance of the
3247 -- child, but not when analyzing the name in the instantiation itself.
3249 if Ekind (Gen_Unit) = E_Package
3250 and then Present (Renamed_Entity (Gen_Unit))
3251 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3252 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3253 and then Present (Homonym (Gen_Unit))
3254 then
3255 Gen_Unit := Homonym (Gen_Unit);
3256 end if;
3258 if Etype (Gen_Unit) = Any_Type then
3259 Restore_Env;
3260 goto Leave;
3262 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3264 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3266 if From_With_Type (Gen_Unit) then
3267 Error_Msg_N
3268 ("cannot instantiate a limited withed package", Gen_Id);
3269 else
3270 Error_Msg_N
3271 ("expect name of generic package in instantiation", Gen_Id);
3272 end if;
3274 Restore_Env;
3275 goto Leave;
3276 end if;
3278 if In_Extended_Main_Source_Unit (N) then
3279 Set_Is_Instantiated (Gen_Unit);
3280 Generate_Reference (Gen_Unit, N);
3282 if Present (Renamed_Object (Gen_Unit)) then
3283 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3284 Generate_Reference (Renamed_Object (Gen_Unit), N);
3285 end if;
3286 end if;
3288 if Nkind (Gen_Id) = N_Identifier
3289 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3290 then
3291 Error_Msg_NE
3292 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3294 elsif Nkind (Gen_Id) = N_Expanded_Name
3295 and then Is_Child_Unit (Gen_Unit)
3296 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3297 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3298 then
3299 Error_Msg_N
3300 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3301 end if;
3303 Set_Entity (Gen_Id, Gen_Unit);
3305 -- If generic is a renaming, get original generic unit
3307 if Present (Renamed_Object (Gen_Unit))
3308 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3309 then
3310 Gen_Unit := Renamed_Object (Gen_Unit);
3311 end if;
3313 -- Verify that there are no circular instantiations
3315 if In_Open_Scopes (Gen_Unit) then
3316 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3317 Restore_Env;
3318 goto Leave;
3320 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3321 Error_Msg_Node_2 := Current_Scope;
3322 Error_Msg_NE
3323 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3324 Circularity_Detected := True;
3325 Restore_Env;
3326 goto Leave;
3328 else
3329 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3331 -- Initialize renamings map, for error checking, and the list that
3332 -- holds private entities whose views have changed between generic
3333 -- definition and instantiation. If this is the instance created to
3334 -- validate an actual package, the instantiation environment is that
3335 -- of the enclosing instance.
3337 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3339 -- Copy original generic tree, to produce text for instantiation
3341 Act_Tree :=
3342 Copy_Generic_Node
3343 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3345 Act_Spec := Specification (Act_Tree);
3347 -- If this is the instance created to validate an actual package,
3348 -- only the formals matter, do not examine the package spec itself.
3350 if Is_Actual_Pack then
3351 Set_Visible_Declarations (Act_Spec, New_List);
3352 Set_Private_Declarations (Act_Spec, New_List);
3353 end if;
3355 Renaming_List :=
3356 Analyze_Associations
3357 (I_Node => N,
3358 Formals => Generic_Formal_Declarations (Act_Tree),
3359 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3361 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3363 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3364 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3365 Set_Is_Generic_Instance (Act_Decl_Id);
3367 Set_Generic_Parent (Act_Spec, Gen_Unit);
3369 -- References to the generic in its own declaration or its body are
3370 -- references to the instance. Add a renaming declaration for the
3371 -- generic unit itself. This declaration, as well as the renaming
3372 -- declarations for the generic formals, must remain private to the
3373 -- unit: the formals, because this is the language semantics, and
3374 -- the unit because its use is an artifact of the implementation.
3376 Unit_Renaming :=
3377 Make_Package_Renaming_Declaration (Loc,
3378 Defining_Unit_Name =>
3379 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3380 Name => New_Reference_To (Act_Decl_Id, Loc));
3382 Append (Unit_Renaming, Renaming_List);
3384 -- The renaming declarations are the first local declarations of
3385 -- the new unit.
3387 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3388 Insert_List_Before
3389 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3390 else
3391 Set_Visible_Declarations (Act_Spec, Renaming_List);
3392 end if;
3394 Act_Decl :=
3395 Make_Package_Declaration (Loc,
3396 Specification => Act_Spec);
3398 -- Save the instantiation node, for subsequent instantiation of the
3399 -- body, if there is one and we are generating code for the current
3400 -- unit. Mark the unit as having a body, to avoid a premature error
3401 -- message.
3403 -- We instantiate the body if we are generating code, if we are
3404 -- generating cross-reference information, or if we are building
3405 -- trees for ASIS use.
3407 declare
3408 Enclosing_Body_Present : Boolean := False;
3409 -- If the generic unit is not a compilation unit, then a body may
3410 -- be present in its parent even if none is required. We create a
3411 -- tentative pending instantiation for the body, which will be
3412 -- discarded if none is actually present.
3414 Scop : Entity_Id;
3416 begin
3417 if Scope (Gen_Unit) /= Standard_Standard
3418 and then not Is_Child_Unit (Gen_Unit)
3419 then
3420 Scop := Scope (Gen_Unit);
3422 while Present (Scop)
3423 and then Scop /= Standard_Standard
3424 loop
3425 if Unit_Requires_Body (Scop) then
3426 Enclosing_Body_Present := True;
3427 exit;
3429 elsif In_Open_Scopes (Scop)
3430 and then In_Package_Body (Scop)
3431 then
3432 Enclosing_Body_Present := True;
3433 exit;
3434 end if;
3436 exit when Is_Compilation_Unit (Scop);
3437 Scop := Scope (Scop);
3438 end loop;
3439 end if;
3441 -- If front-end inlining is enabled, and this is a unit for which
3442 -- code will be generated, we instantiate the body at once.
3444 -- This is done if the instance is not the main unit, and if the
3445 -- generic is not a child unit of another generic, to avoid scope
3446 -- problems and the reinstallation of parent instances.
3448 if Expander_Active
3449 and then (not Is_Child_Unit (Gen_Unit)
3450 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3451 and then Might_Inline_Subp
3452 and then not Is_Actual_Pack
3453 then
3454 if Front_End_Inlining
3455 and then (Is_In_Main_Unit (N)
3456 or else In_Main_Context (Current_Scope))
3457 and then Nkind (Parent (N)) /= N_Compilation_Unit
3458 then
3459 Inline_Now := True;
3461 -- In configurable_run_time mode we force the inlining of
3462 -- predefined subprograms marked Inline_Always, to minimize
3463 -- the use of the run-time library.
3465 elsif Is_Predefined_File_Name
3466 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3467 and then Configurable_Run_Time_Mode
3468 and then Nkind (Parent (N)) /= N_Compilation_Unit
3469 then
3470 Inline_Now := True;
3471 end if;
3473 -- If the current scope is itself an instance within a child
3474 -- unit, there will be duplications in the scope stack, and the
3475 -- unstacking mechanism in Inline_Instance_Body will fail.
3476 -- This loses some rare cases of optimization, and might be
3477 -- improved some day, if we can find a proper abstraction for
3478 -- "the complete compilation context" that can be saved and
3479 -- restored. ???
3481 if Is_Generic_Instance (Current_Scope) then
3482 declare
3483 Curr_Unit : constant Entity_Id :=
3484 Cunit_Entity (Current_Sem_Unit);
3485 begin
3486 if Curr_Unit /= Current_Scope
3487 and then Is_Child_Unit (Curr_Unit)
3488 then
3489 Inline_Now := False;
3490 end if;
3491 end;
3492 end if;
3493 end if;
3495 Needs_Body :=
3496 (Unit_Requires_Body (Gen_Unit)
3497 or else Enclosing_Body_Present
3498 or else Present (Corresponding_Body (Gen_Decl)))
3499 and then (Is_In_Main_Unit (N)
3500 or else Might_Inline_Subp)
3501 and then not Is_Actual_Pack
3502 and then not Inline_Now
3503 and then not Alfa_Mode
3504 and then (Operating_Mode = Generate_Code
3505 or else (Operating_Mode = Check_Semantics
3506 and then ASIS_Mode));
3508 -- If front_end_inlining is enabled, do not instantiate body if
3509 -- within a generic context.
3511 if (Front_End_Inlining
3512 and then not Expander_Active)
3513 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
3514 then
3515 Needs_Body := False;
3516 end if;
3518 -- If the current context is generic, and the package being
3519 -- instantiated is declared within a formal package, there is no
3520 -- body to instantiate until the enclosing generic is instantiated
3521 -- and there is an actual for the formal package. If the formal
3522 -- package has parameters, we build a regular package instance for
3523 -- it, that precedes the original formal package declaration.
3525 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
3526 declare
3527 Decl : constant Node_Id :=
3528 Original_Node
3529 (Unit_Declaration_Node (Scope (Gen_Unit)));
3530 begin
3531 if Nkind (Decl) = N_Formal_Package_Declaration
3532 or else (Nkind (Decl) = N_Package_Declaration
3533 and then Is_List_Member (Decl)
3534 and then Present (Next (Decl))
3535 and then
3536 Nkind (Next (Decl)) =
3537 N_Formal_Package_Declaration)
3538 then
3539 Needs_Body := False;
3540 end if;
3541 end;
3542 end if;
3543 end;
3545 -- Note that we generate the instance body even when generating
3546 -- calling stubs for an RCI unit: it may be required e.g. if it
3547 -- provides stream attributes for some type used in the profile of a
3548 -- remote subprogram. If the instantiation is within the visible part
3549 -- of the RCI, then calling stubs for any relevant subprogram will
3550 -- be inserted immediately after the subprogram declaration, and
3551 -- will take precedence over the subsequent (original) body. (The
3552 -- stub and original body will be complete homographs, but this is
3553 -- permitted in an instance).
3555 -- Could we do better and remove the original subprogram body in that
3556 -- case???
3558 if Needs_Body then
3560 -- Here is a defence against a ludicrous number of instantiations
3561 -- caused by a circular set of instantiation attempts.
3563 if Pending_Instantiations.Last >
3564 Hostparm.Max_Instantiations
3565 then
3566 Error_Msg_N ("too many instantiations", N);
3567 raise Unrecoverable_Error;
3568 end if;
3570 -- Indicate that the enclosing scopes contain an instantiation,
3571 -- and that cleanup actions should be delayed until after the
3572 -- instance body is expanded.
3574 Check_Forward_Instantiation (Gen_Decl);
3575 if Nkind (N) = N_Package_Instantiation then
3576 declare
3577 Enclosing_Master : Entity_Id;
3579 begin
3580 -- Loop to search enclosing masters
3582 Enclosing_Master := Current_Scope;
3583 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
3584 if Ekind (Enclosing_Master) = E_Package then
3585 if Is_Compilation_Unit (Enclosing_Master) then
3586 if In_Package_Body (Enclosing_Master) then
3587 Delay_Descriptors
3588 (Body_Entity (Enclosing_Master));
3589 else
3590 Delay_Descriptors
3591 (Enclosing_Master);
3592 end if;
3594 exit Scope_Loop;
3596 else
3597 Enclosing_Master := Scope (Enclosing_Master);
3598 end if;
3600 elsif Is_Generic_Unit (Enclosing_Master)
3601 or else Ekind (Enclosing_Master) = E_Void
3602 then
3603 -- Cleanup actions will eventually be performed on the
3604 -- enclosing subprogram or package instance, if any.
3605 -- Enclosing scope is void in the formal part of a
3606 -- generic subprogram.
3608 exit Scope_Loop;
3610 else
3611 if Ekind (Enclosing_Master) = E_Entry
3612 and then
3613 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
3614 then
3615 if not Expander_Active then
3616 exit Scope_Loop;
3617 else
3618 Enclosing_Master :=
3619 Protected_Body_Subprogram (Enclosing_Master);
3620 end if;
3621 end if;
3623 Set_Delay_Cleanups (Enclosing_Master);
3625 while Ekind (Enclosing_Master) = E_Block loop
3626 Enclosing_Master := Scope (Enclosing_Master);
3627 end loop;
3629 if Is_Subprogram (Enclosing_Master) then
3630 Delay_Descriptors (Enclosing_Master);
3632 elsif Is_Task_Type (Enclosing_Master) then
3633 declare
3634 TBP : constant Node_Id :=
3635 Get_Task_Body_Procedure
3636 (Enclosing_Master);
3637 begin
3638 if Present (TBP) then
3639 Delay_Descriptors (TBP);
3640 Set_Delay_Cleanups (TBP);
3641 end if;
3642 end;
3643 end if;
3645 exit Scope_Loop;
3646 end if;
3647 end loop Scope_Loop;
3648 end;
3650 -- Make entry in table
3652 Pending_Instantiations.Append
3653 ((Inst_Node => N,
3654 Act_Decl => Act_Decl,
3655 Expander_Status => Expander_Active,
3656 Current_Sem_Unit => Current_Sem_Unit,
3657 Scope_Suppress => Scope_Suppress,
3658 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
3659 Version => Ada_Version));
3660 end if;
3661 end if;
3663 Set_Categorization_From_Pragmas (Act_Decl);
3665 if Parent_Installed then
3666 Hide_Current_Scope;
3667 end if;
3669 Set_Instance_Spec (N, Act_Decl);
3671 -- If not a compilation unit, insert the package declaration before
3672 -- the original instantiation node.
3674 if Nkind (Parent (N)) /= N_Compilation_Unit then
3675 Mark_Rewrite_Insertion (Act_Decl);
3676 Insert_Before (N, Act_Decl);
3677 Analyze (Act_Decl);
3679 -- For an instantiation that is a compilation unit, place declaration
3680 -- on current node so context is complete for analysis (including
3681 -- nested instantiations). If this is the main unit, the declaration
3682 -- eventually replaces the instantiation node. If the instance body
3683 -- is created later, it replaces the instance node, and the
3684 -- declaration is attached to it (see
3685 -- Build_Instance_Compilation_Unit_Nodes).
3687 else
3688 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
3690 -- The entity for the current unit is the newly created one,
3691 -- and all semantic information is attached to it.
3693 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
3695 -- If this is the main unit, replace the main entity as well
3697 if Current_Sem_Unit = Main_Unit then
3698 Main_Unit_Entity := Act_Decl_Id;
3699 end if;
3700 end if;
3702 Set_Unit (Parent (N), Act_Decl);
3703 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
3704 Set_Package_Instantiation (Act_Decl_Id, N);
3705 Analyze (Act_Decl);
3706 Set_Unit (Parent (N), N);
3707 Set_Body_Required (Parent (N), False);
3709 -- We never need elaboration checks on instantiations, since by
3710 -- definition, the body instantiation is elaborated at the same
3711 -- time as the spec instantiation.
3713 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
3714 Set_Kill_Elaboration_Checks (Act_Decl_Id);
3715 end if;
3717 Check_Elab_Instantiation (N);
3719 if ABE_Is_Certain (N) and then Needs_Body then
3720 Pending_Instantiations.Decrement_Last;
3721 end if;
3723 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
3725 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
3726 First_Private_Entity (Act_Decl_Id));
3728 -- If the instantiation will receive a body, the unit will be
3729 -- transformed into a package body, and receive its own elaboration
3730 -- entity. Otherwise, the nature of the unit is now a package
3731 -- declaration.
3733 if Nkind (Parent (N)) = N_Compilation_Unit
3734 and then not Needs_Body
3735 then
3736 Rewrite (N, Act_Decl);
3737 end if;
3739 if Present (Corresponding_Body (Gen_Decl))
3740 or else Unit_Requires_Body (Gen_Unit)
3741 then
3742 Set_Has_Completion (Act_Decl_Id);
3743 end if;
3745 Check_Formal_Packages (Act_Decl_Id);
3747 Restore_Hidden_Primitives (Vis_Prims_List);
3748 Restore_Private_Views (Act_Decl_Id);
3750 Inherit_Context (Gen_Decl, N);
3752 if Parent_Installed then
3753 Remove_Parent;
3754 end if;
3756 Restore_Env;
3757 Env_Installed := False;
3758 end if;
3760 Validate_Categorization_Dependency (N, Act_Decl_Id);
3762 -- There used to be a check here to prevent instantiations in local
3763 -- contexts if the No_Local_Allocators restriction was active. This
3764 -- check was removed by a binding interpretation in AI-95-00130/07,
3765 -- but we retain the code for documentation purposes.
3767 -- if Ekind (Act_Decl_Id) /= E_Void
3768 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
3769 -- then
3770 -- Check_Restriction (No_Local_Allocators, N);
3771 -- end if;
3773 if Inline_Now then
3774 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
3775 end if;
3777 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
3778 -- be used as defining identifiers for a formal package and for the
3779 -- corresponding expanded package.
3781 if Nkind (N) = N_Formal_Package_Declaration then
3782 Act_Decl_Id := New_Copy (Defining_Entity (N));
3783 Set_Comes_From_Source (Act_Decl_Id, True);
3784 Set_Is_Generic_Instance (Act_Decl_Id, False);
3785 Set_Defining_Identifier (N, Act_Decl_Id);
3786 end if;
3788 Style_Check := Save_Style_Check;
3790 -- Check that if N is an instantiation of System.Dim_Float_IO or
3791 -- System.Dim_Integer_IO, the formal type has a dimension system.
3793 if Nkind (N) = N_Package_Instantiation
3794 and then Is_Dim_IO_Package_Instantiation (N)
3795 then
3796 declare
3797 Assoc : constant Node_Id := First (Generic_Associations (N));
3798 begin
3799 if not Has_Dimension_System
3800 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
3801 then
3802 Error_Msg_N ("type with a dimension system expected", Assoc);
3803 end if;
3804 end;
3805 end if;
3807 <<Leave>>
3808 if Has_Aspects (N) then
3809 Analyze_Aspect_Specifications (N, Act_Decl_Id);
3810 end if;
3812 exception
3813 when Instantiation_Error =>
3814 if Parent_Installed then
3815 Remove_Parent;
3816 end if;
3818 if Env_Installed then
3819 Restore_Env;
3820 end if;
3822 Style_Check := Save_Style_Check;
3823 end Analyze_Package_Instantiation;
3825 --------------------------
3826 -- Inline_Instance_Body --
3827 --------------------------
3829 procedure Inline_Instance_Body
3830 (N : Node_Id;
3831 Gen_Unit : Entity_Id;
3832 Act_Decl : Node_Id)
3834 Vis : Boolean;
3835 Gen_Comp : constant Entity_Id :=
3836 Cunit_Entity (Get_Source_Unit (Gen_Unit));
3837 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
3838 Curr_Scope : Entity_Id := Empty;
3839 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
3840 Removed : Boolean := False;
3841 Num_Scopes : Int := 0;
3843 Scope_Stack_Depth : constant Int :=
3844 Scope_Stack.Last - Scope_Stack.First + 1;
3846 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
3847 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
3848 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
3849 Num_Inner : Int := 0;
3850 N_Instances : Int := 0;
3851 S : Entity_Id;
3853 begin
3854 -- Case of generic unit defined in another unit. We must remove the
3855 -- complete context of the current unit to install that of the generic.
3857 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
3859 -- Add some comments for the following two loops ???
3861 S := Current_Scope;
3862 while Present (S) and then S /= Standard_Standard loop
3863 loop
3864 Num_Scopes := Num_Scopes + 1;
3866 Use_Clauses (Num_Scopes) :=
3867 (Scope_Stack.Table
3868 (Scope_Stack.Last - Num_Scopes + 1).
3869 First_Use_Clause);
3870 End_Use_Clauses (Use_Clauses (Num_Scopes));
3872 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
3873 or else Scope_Stack.Table
3874 (Scope_Stack.Last - Num_Scopes).Entity
3875 = Scope (S);
3876 end loop;
3878 exit when Is_Generic_Instance (S)
3879 and then (In_Package_Body (S)
3880 or else Ekind (S) = E_Procedure
3881 or else Ekind (S) = E_Function);
3882 S := Scope (S);
3883 end loop;
3885 Vis := Is_Immediately_Visible (Gen_Comp);
3887 -- Find and save all enclosing instances
3889 S := Current_Scope;
3891 while Present (S)
3892 and then S /= Standard_Standard
3893 loop
3894 if Is_Generic_Instance (S) then
3895 N_Instances := N_Instances + 1;
3896 Instances (N_Instances) := S;
3898 exit when In_Package_Body (S);
3899 end if;
3901 S := Scope (S);
3902 end loop;
3904 -- Remove context of current compilation unit, unless we are within a
3905 -- nested package instantiation, in which case the context has been
3906 -- removed previously.
3908 -- If current scope is the body of a child unit, remove context of
3909 -- spec as well. If an enclosing scope is an instance body, the
3910 -- context has already been removed, but the entities in the body
3911 -- must be made invisible as well.
3913 S := Current_Scope;
3915 while Present (S)
3916 and then S /= Standard_Standard
3917 loop
3918 if Is_Generic_Instance (S)
3919 and then (In_Package_Body (S)
3920 or else Ekind (S) = E_Procedure
3921 or else Ekind (S) = E_Function)
3922 then
3923 -- We still have to remove the entities of the enclosing
3924 -- instance from direct visibility.
3926 declare
3927 E : Entity_Id;
3928 begin
3929 E := First_Entity (S);
3930 while Present (E) loop
3931 Set_Is_Immediately_Visible (E, False);
3932 Next_Entity (E);
3933 end loop;
3934 end;
3936 exit;
3937 end if;
3939 if S = Curr_Unit
3940 or else (Ekind (Curr_Unit) = E_Package_Body
3941 and then S = Spec_Entity (Curr_Unit))
3942 or else (Ekind (Curr_Unit) = E_Subprogram_Body
3943 and then S =
3944 Corresponding_Spec
3945 (Unit_Declaration_Node (Curr_Unit)))
3946 then
3947 Removed := True;
3949 -- Remove entities in current scopes from visibility, so that
3950 -- instance body is compiled in a clean environment.
3952 Save_Scope_Stack (Handle_Use => False);
3954 if Is_Child_Unit (S) then
3956 -- Remove child unit from stack, as well as inner scopes.
3957 -- Removing the context of a child unit removes parent units
3958 -- as well.
3960 while Current_Scope /= S loop
3961 Num_Inner := Num_Inner + 1;
3962 Inner_Scopes (Num_Inner) := Current_Scope;
3963 Pop_Scope;
3964 end loop;
3966 Pop_Scope;
3967 Remove_Context (Curr_Comp);
3968 Curr_Scope := S;
3970 else
3971 Remove_Context (Curr_Comp);
3972 end if;
3974 if Ekind (Curr_Unit) = E_Package_Body then
3975 Remove_Context (Library_Unit (Curr_Comp));
3976 end if;
3977 end if;
3979 S := Scope (S);
3980 end loop;
3981 pragma Assert (Num_Inner < Num_Scopes);
3983 Push_Scope (Standard_Standard);
3984 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
3985 Instantiate_Package_Body
3986 (Body_Info =>
3987 ((Inst_Node => N,
3988 Act_Decl => Act_Decl,
3989 Expander_Status => Expander_Active,
3990 Current_Sem_Unit => Current_Sem_Unit,
3991 Scope_Suppress => Scope_Suppress,
3992 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
3993 Version => Ada_Version)),
3994 Inlined_Body => True);
3996 Pop_Scope;
3998 -- Restore context
4000 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4002 -- Reset Generic_Instance flag so that use clauses can be installed
4003 -- in the proper order. (See Use_One_Package for effect of enclosing
4004 -- instances on processing of use clauses).
4006 for J in 1 .. N_Instances loop
4007 Set_Is_Generic_Instance (Instances (J), False);
4008 end loop;
4010 if Removed then
4011 Install_Context (Curr_Comp);
4013 if Present (Curr_Scope)
4014 and then Is_Child_Unit (Curr_Scope)
4015 then
4016 Push_Scope (Curr_Scope);
4017 Set_Is_Immediately_Visible (Curr_Scope);
4019 -- Finally, restore inner scopes as well
4021 for J in reverse 1 .. Num_Inner loop
4022 Push_Scope (Inner_Scopes (J));
4023 end loop;
4024 end if;
4026 Restore_Scope_Stack (Handle_Use => False);
4028 if Present (Curr_Scope)
4029 and then
4030 (In_Private_Part (Curr_Scope)
4031 or else In_Package_Body (Curr_Scope))
4032 then
4033 -- Install private declaration of ancestor units, which are
4034 -- currently available. Restore_Scope_Stack and Install_Context
4035 -- only install the visible part of parents.
4037 declare
4038 Par : Entity_Id;
4039 begin
4040 Par := Scope (Curr_Scope);
4041 while (Present (Par))
4042 and then Par /= Standard_Standard
4043 loop
4044 Install_Private_Declarations (Par);
4045 Par := Scope (Par);
4046 end loop;
4047 end;
4048 end if;
4049 end if;
4051 -- Restore use clauses. For a child unit, use clauses in the parents
4052 -- are restored when installing the context, so only those in inner
4053 -- scopes (and those local to the child unit itself) need to be
4054 -- installed explicitly.
4056 if Is_Child_Unit (Curr_Unit)
4057 and then Removed
4058 then
4059 for J in reverse 1 .. Num_Inner + 1 loop
4060 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4061 Use_Clauses (J);
4062 Install_Use_Clauses (Use_Clauses (J));
4063 end loop;
4065 else
4066 for J in reverse 1 .. Num_Scopes loop
4067 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4068 Use_Clauses (J);
4069 Install_Use_Clauses (Use_Clauses (J));
4070 end loop;
4071 end if;
4073 -- Restore status of instances. If one of them is a body, make
4074 -- its local entities visible again.
4076 declare
4077 E : Entity_Id;
4078 Inst : Entity_Id;
4080 begin
4081 for J in 1 .. N_Instances loop
4082 Inst := Instances (J);
4083 Set_Is_Generic_Instance (Inst, True);
4085 if In_Package_Body (Inst)
4086 or else Ekind (S) = E_Procedure
4087 or else Ekind (S) = E_Function
4088 then
4089 E := First_Entity (Instances (J));
4090 while Present (E) loop
4091 Set_Is_Immediately_Visible (E);
4092 Next_Entity (E);
4093 end loop;
4094 end if;
4095 end loop;
4096 end;
4098 -- If generic unit is in current unit, current context is correct
4100 else
4101 Instantiate_Package_Body
4102 (Body_Info =>
4103 ((Inst_Node => N,
4104 Act_Decl => Act_Decl,
4105 Expander_Status => Expander_Active,
4106 Current_Sem_Unit => Current_Sem_Unit,
4107 Scope_Suppress => Scope_Suppress,
4108 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4109 Version => Ada_Version)),
4110 Inlined_Body => True);
4111 end if;
4112 end Inline_Instance_Body;
4114 -------------------------------------
4115 -- Analyze_Procedure_Instantiation --
4116 -------------------------------------
4118 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4119 begin
4120 Analyze_Subprogram_Instantiation (N, E_Procedure);
4121 end Analyze_Procedure_Instantiation;
4123 -----------------------------------
4124 -- Need_Subprogram_Instance_Body --
4125 -----------------------------------
4127 function Need_Subprogram_Instance_Body
4128 (N : Node_Id;
4129 Subp : Entity_Id) return Boolean
4131 begin
4132 if (Is_In_Main_Unit (N)
4133 or else Is_Inlined (Subp)
4134 or else Is_Inlined (Alias (Subp)))
4135 and then (Operating_Mode = Generate_Code
4136 or else (Operating_Mode = Check_Semantics
4137 and then ASIS_Mode))
4138 and then (Full_Expander_Active or else ASIS_Mode)
4139 and then not ABE_Is_Certain (N)
4140 and then not Is_Eliminated (Subp)
4141 then
4142 Pending_Instantiations.Append
4143 ((Inst_Node => N,
4144 Act_Decl => Unit_Declaration_Node (Subp),
4145 Expander_Status => Expander_Active,
4146 Current_Sem_Unit => Current_Sem_Unit,
4147 Scope_Suppress => Scope_Suppress,
4148 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4149 Version => Ada_Version));
4150 return True;
4152 else
4153 return False;
4154 end if;
4155 end Need_Subprogram_Instance_Body;
4157 --------------------------------------
4158 -- Analyze_Subprogram_Instantiation --
4159 --------------------------------------
4161 procedure Analyze_Subprogram_Instantiation
4162 (N : Node_Id;
4163 K : Entity_Kind)
4165 Loc : constant Source_Ptr := Sloc (N);
4166 Gen_Id : constant Node_Id := Name (N);
4168 Anon_Id : constant Entity_Id :=
4169 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4170 Chars => New_External_Name
4171 (Chars (Defining_Entity (N)), 'R'));
4173 Act_Decl_Id : Entity_Id;
4174 Act_Decl : Node_Id;
4175 Act_Spec : Node_Id;
4176 Act_Tree : Node_Id;
4178 Env_Installed : Boolean := False;
4179 Gen_Unit : Entity_Id;
4180 Gen_Decl : Node_Id;
4181 Pack_Id : Entity_Id;
4182 Parent_Installed : Boolean := False;
4183 Renaming_List : List_Id;
4185 Save_Style_Check : constant Boolean := Style_Check;
4186 -- Save style check mode for restore on exit
4188 procedure Analyze_Instance_And_Renamings;
4189 -- The instance must be analyzed in a context that includes the mappings
4190 -- of generic parameters into actuals. We create a package declaration
4191 -- for this purpose, and a subprogram with an internal name within the
4192 -- package. The subprogram instance is simply an alias for the internal
4193 -- subprogram, declared in the current scope.
4195 ------------------------------------
4196 -- Analyze_Instance_And_Renamings --
4197 ------------------------------------
4199 procedure Analyze_Instance_And_Renamings is
4200 Def_Ent : constant Entity_Id := Defining_Entity (N);
4201 Pack_Decl : Node_Id;
4203 begin
4204 if Nkind (Parent (N)) = N_Compilation_Unit then
4206 -- For the case of a compilation unit, the container package has
4207 -- the same name as the instantiation, to insure that the binder
4208 -- calls the elaboration procedure with the right name. Copy the
4209 -- entity of the instance, which may have compilation level flags
4210 -- (e.g. Is_Child_Unit) set.
4212 Pack_Id := New_Copy (Def_Ent);
4214 else
4215 -- Otherwise we use the name of the instantiation concatenated
4216 -- with its source position to ensure uniqueness if there are
4217 -- several instantiations with the same name.
4219 Pack_Id :=
4220 Make_Defining_Identifier (Loc,
4221 Chars => New_External_Name
4222 (Related_Id => Chars (Def_Ent),
4223 Suffix => "GP",
4224 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4225 end if;
4227 Pack_Decl := Make_Package_Declaration (Loc,
4228 Specification => Make_Package_Specification (Loc,
4229 Defining_Unit_Name => Pack_Id,
4230 Visible_Declarations => Renaming_List,
4231 End_Label => Empty));
4233 Set_Instance_Spec (N, Pack_Decl);
4234 Set_Is_Generic_Instance (Pack_Id);
4235 Set_Debug_Info_Needed (Pack_Id);
4237 -- Case of not a compilation unit
4239 if Nkind (Parent (N)) /= N_Compilation_Unit then
4240 Mark_Rewrite_Insertion (Pack_Decl);
4241 Insert_Before (N, Pack_Decl);
4242 Set_Has_Completion (Pack_Id);
4244 -- Case of an instantiation that is a compilation unit
4246 -- Place declaration on current node so context is complete for
4247 -- analysis (including nested instantiations), and for use in a
4248 -- context_clause (see Analyze_With_Clause).
4250 else
4251 Set_Unit (Parent (N), Pack_Decl);
4252 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4253 end if;
4255 Analyze (Pack_Decl);
4256 Check_Formal_Packages (Pack_Id);
4257 Set_Is_Generic_Instance (Pack_Id, False);
4259 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4260 -- above???
4262 -- Body of the enclosing package is supplied when instantiating the
4263 -- subprogram body, after semantic analysis is completed.
4265 if Nkind (Parent (N)) = N_Compilation_Unit then
4267 -- Remove package itself from visibility, so it does not
4268 -- conflict with subprogram.
4270 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
4272 -- Set name and scope of internal subprogram so that the proper
4273 -- external name will be generated. The proper scope is the scope
4274 -- of the wrapper package. We need to generate debugging info for
4275 -- the internal subprogram, so set flag accordingly.
4277 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
4278 Set_Scope (Anon_Id, Scope (Pack_Id));
4280 -- Mark wrapper package as referenced, to avoid spurious warnings
4281 -- if the instantiation appears in various with_ clauses of
4282 -- subunits of the main unit.
4284 Set_Referenced (Pack_Id);
4285 end if;
4287 Set_Is_Generic_Instance (Anon_Id);
4288 Set_Debug_Info_Needed (Anon_Id);
4289 Act_Decl_Id := New_Copy (Anon_Id);
4291 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4292 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
4293 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
4294 Set_Comes_From_Source (Act_Decl_Id, True);
4296 -- The signature may involve types that are not frozen yet, but the
4297 -- subprogram will be frozen at the point the wrapper package is
4298 -- frozen, so it does not need its own freeze node. In fact, if one
4299 -- is created, it might conflict with the freezing actions from the
4300 -- wrapper package.
4302 Set_Has_Delayed_Freeze (Anon_Id, False);
4304 -- If the instance is a child unit, mark the Id accordingly. Mark
4305 -- the anonymous entity as well, which is the real subprogram and
4306 -- which is used when the instance appears in a context clause.
4307 -- Similarly, propagate the Is_Eliminated flag to handle properly
4308 -- nested eliminated subprograms.
4310 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
4311 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
4312 New_Overloaded_Entity (Act_Decl_Id);
4313 Check_Eliminated (Act_Decl_Id);
4314 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
4316 -- In compilation unit case, kill elaboration checks on the
4317 -- instantiation, since they are never needed -- the body is
4318 -- instantiated at the same point as the spec.
4320 if Nkind (Parent (N)) = N_Compilation_Unit then
4321 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4322 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4323 Set_Is_Compilation_Unit (Anon_Id);
4325 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
4326 end if;
4328 -- The instance is not a freezing point for the new subprogram
4330 Set_Is_Frozen (Act_Decl_Id, False);
4332 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
4333 Valid_Operator_Definition (Act_Decl_Id);
4334 end if;
4336 Set_Alias (Act_Decl_Id, Anon_Id);
4337 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4338 Set_Has_Completion (Act_Decl_Id);
4339 Set_Related_Instance (Pack_Id, Act_Decl_Id);
4341 if Nkind (Parent (N)) = N_Compilation_Unit then
4342 Set_Body_Required (Parent (N), False);
4343 end if;
4344 end Analyze_Instance_And_Renamings;
4346 -- Local variables
4348 Vis_Prims_List : Elist_Id := No_Elist;
4349 -- List of primitives made temporarily visible in the instantiation
4350 -- to match the visibility of the formal type
4352 -- Start of processing for Analyze_Subprogram_Instantiation
4354 begin
4355 Check_SPARK_Restriction ("generic is not allowed", N);
4357 -- Very first thing: apply the special kludge for Text_IO processing
4358 -- in case we are instantiating one of the children of [Wide_]Text_IO.
4359 -- Of course such an instantiation is bogus (these are packages, not
4360 -- subprograms), but we get a better error message if we do this.
4362 Text_IO_Kludge (Gen_Id);
4364 -- Make node global for error reporting
4366 Instantiation_Node := N;
4368 -- Turn off style checking in instances. If the check is enabled on the
4369 -- generic unit, a warning in an instance would just be noise. If not
4370 -- enabled on the generic, then a warning in an instance is just wrong.
4372 Style_Check := False;
4374 Preanalyze_Actuals (N);
4376 Init_Env;
4377 Env_Installed := True;
4378 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4379 Gen_Unit := Entity (Gen_Id);
4381 Generate_Reference (Gen_Unit, Gen_Id);
4383 if Nkind (Gen_Id) = N_Identifier
4384 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4385 then
4386 Error_Msg_NE
4387 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4388 end if;
4390 if Etype (Gen_Unit) = Any_Type then
4391 Restore_Env;
4392 return;
4393 end if;
4395 -- Verify that it is a generic subprogram of the right kind, and that
4396 -- it does not lead to a circular instantiation.
4398 if not Ekind_In (Gen_Unit, E_Generic_Procedure, E_Generic_Function) then
4399 Error_Msg_N ("expect generic subprogram in instantiation", Gen_Id);
4401 elsif In_Open_Scopes (Gen_Unit) then
4402 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4404 elsif K = E_Procedure
4405 and then Ekind (Gen_Unit) /= E_Generic_Procedure
4406 then
4407 if Ekind (Gen_Unit) = E_Generic_Function then
4408 Error_Msg_N
4409 ("cannot instantiate generic function as procedure", Gen_Id);
4410 else
4411 Error_Msg_N
4412 ("expect name of generic procedure in instantiation", Gen_Id);
4413 end if;
4415 elsif K = E_Function
4416 and then Ekind (Gen_Unit) /= E_Generic_Function
4417 then
4418 if Ekind (Gen_Unit) = E_Generic_Procedure then
4419 Error_Msg_N
4420 ("cannot instantiate generic procedure as function", Gen_Id);
4421 else
4422 Error_Msg_N
4423 ("expect name of generic function in instantiation", Gen_Id);
4424 end if;
4426 else
4427 Set_Entity (Gen_Id, Gen_Unit);
4428 Set_Is_Instantiated (Gen_Unit);
4430 if In_Extended_Main_Source_Unit (N) then
4431 Generate_Reference (Gen_Unit, N);
4432 end if;
4434 -- If renaming, get original unit
4436 if Present (Renamed_Object (Gen_Unit))
4437 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
4438 or else
4439 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
4440 then
4441 Gen_Unit := Renamed_Object (Gen_Unit);
4442 Set_Is_Instantiated (Gen_Unit);
4443 Generate_Reference (Gen_Unit, N);
4444 end if;
4446 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4447 Error_Msg_Node_2 := Current_Scope;
4448 Error_Msg_NE
4449 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4450 Circularity_Detected := True;
4451 Restore_Hidden_Primitives (Vis_Prims_List);
4452 goto Leave;
4453 end if;
4455 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4457 -- Initialize renamings map, for error checking
4459 Generic_Renamings.Set_Last (0);
4460 Generic_Renamings_HTable.Reset;
4462 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
4464 -- Copy original generic tree, to produce text for instantiation
4466 Act_Tree :=
4467 Copy_Generic_Node
4468 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4470 -- Inherit overriding indicator from instance node
4472 Act_Spec := Specification (Act_Tree);
4473 Set_Must_Override (Act_Spec, Must_Override (N));
4474 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
4476 Renaming_List :=
4477 Analyze_Associations
4478 (I_Node => N,
4479 Formals => Generic_Formal_Declarations (Act_Tree),
4480 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4482 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4484 -- The subprogram itself cannot contain a nested instance, so the
4485 -- current parent is left empty.
4487 Set_Instance_Env (Gen_Unit, Empty);
4489 -- Build the subprogram declaration, which does not appear in the
4490 -- generic template, and give it a sloc consistent with that of the
4491 -- template.
4493 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
4494 Set_Generic_Parent (Act_Spec, Gen_Unit);
4495 Act_Decl :=
4496 Make_Subprogram_Declaration (Sloc (Act_Spec),
4497 Specification => Act_Spec);
4499 -- The aspects have been copied previously, but they have to be
4500 -- linked explicitly to the new subprogram declaration. Explicit
4501 -- pre/postconditions on the instance are analyzed below, in a
4502 -- separate step.
4504 Move_Aspects (Act_Tree, Act_Decl);
4505 Set_Categorization_From_Pragmas (Act_Decl);
4507 if Parent_Installed then
4508 Hide_Current_Scope;
4509 end if;
4511 Append (Act_Decl, Renaming_List);
4512 Analyze_Instance_And_Renamings;
4514 -- If the generic is marked Import (Intrinsic), then so is the
4515 -- instance. This indicates that there is no body to instantiate. If
4516 -- generic is marked inline, so it the instance, and the anonymous
4517 -- subprogram it renames. If inlined, or else if inlining is enabled
4518 -- for the compilation, we generate the instance body even if it is
4519 -- not within the main unit.
4521 if Is_Intrinsic_Subprogram (Gen_Unit) then
4522 Set_Is_Intrinsic_Subprogram (Anon_Id);
4523 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
4525 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
4526 Validate_Unchecked_Conversion (N, Act_Decl_Id);
4527 end if;
4528 end if;
4530 -- Inherit convention from generic unit. Intrinsic convention, as for
4531 -- an instance of unchecked conversion, is not inherited because an
4532 -- explicit Ada instance has been created.
4534 if Has_Convention_Pragma (Gen_Unit)
4535 and then Convention (Gen_Unit) /= Convention_Intrinsic
4536 then
4537 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
4538 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
4539 end if;
4541 Generate_Definition (Act_Decl_Id);
4542 Set_Contract (Anon_Id, Make_Contract (Sloc (Anon_Id))); -- ??? needed?
4543 Set_Contract (Act_Decl_Id, Make_Contract (Sloc (Act_Decl_Id)));
4545 -- Inherit all inlining-related flags which apply to the generic in
4546 -- the subprogram and its declaration.
4548 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
4549 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
4551 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
4552 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
4554 Set_Has_Pragma_Inline_Always
4555 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
4556 Set_Has_Pragma_Inline_Always
4557 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
4559 if not Is_Intrinsic_Subprogram (Gen_Unit) then
4560 Check_Elab_Instantiation (N);
4561 end if;
4563 if Is_Dispatching_Operation (Act_Decl_Id)
4564 and then Ada_Version >= Ada_2005
4565 then
4566 declare
4567 Formal : Entity_Id;
4569 begin
4570 Formal := First_Formal (Act_Decl_Id);
4571 while Present (Formal) loop
4572 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
4573 and then Is_Controlling_Formal (Formal)
4574 and then not Can_Never_Be_Null (Formal)
4575 then
4576 Error_Msg_NE ("access parameter& is controlling,",
4577 N, Formal);
4578 Error_Msg_NE
4579 ("\corresponding parameter of & must be"
4580 & " explicitly null-excluding", N, Gen_Id);
4581 end if;
4583 Next_Formal (Formal);
4584 end loop;
4585 end;
4586 end if;
4588 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4590 Validate_Categorization_Dependency (N, Act_Decl_Id);
4592 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
4593 Inherit_Context (Gen_Decl, N);
4595 Restore_Private_Views (Pack_Id, False);
4597 -- If the context requires a full instantiation, mark node for
4598 -- subsequent construction of the body.
4600 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
4602 Check_Forward_Instantiation (Gen_Decl);
4604 -- The wrapper package is always delayed, because it does not
4605 -- constitute a freeze point, but to insure that the freeze
4606 -- node is placed properly, it is created directly when
4607 -- instantiating the body (otherwise the freeze node might
4608 -- appear to early for nested instantiations).
4610 elsif Nkind (Parent (N)) = N_Compilation_Unit then
4612 -- For ASIS purposes, indicate that the wrapper package has
4613 -- replaced the instantiation node.
4615 Rewrite (N, Unit (Parent (N)));
4616 Set_Unit (Parent (N), N);
4617 end if;
4619 elsif Nkind (Parent (N)) = N_Compilation_Unit then
4621 -- Replace instance node for library-level instantiations of
4622 -- intrinsic subprograms, for ASIS use.
4624 Rewrite (N, Unit (Parent (N)));
4625 Set_Unit (Parent (N), N);
4626 end if;
4628 if Parent_Installed then
4629 Remove_Parent;
4630 end if;
4632 Restore_Hidden_Primitives (Vis_Prims_List);
4633 Restore_Env;
4634 Env_Installed := False;
4635 Generic_Renamings.Set_Last (0);
4636 Generic_Renamings_HTable.Reset;
4637 end if;
4639 Style_Check := Save_Style_Check;
4641 <<Leave>>
4642 if Has_Aspects (N) then
4643 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4644 end if;
4646 exception
4647 when Instantiation_Error =>
4648 if Parent_Installed then
4649 Remove_Parent;
4650 end if;
4652 if Env_Installed then
4653 Restore_Env;
4654 end if;
4656 Style_Check := Save_Style_Check;
4657 end Analyze_Subprogram_Instantiation;
4659 -------------------------
4660 -- Get_Associated_Node --
4661 -------------------------
4663 function Get_Associated_Node (N : Node_Id) return Node_Id is
4664 Assoc : Node_Id;
4666 begin
4667 Assoc := Associated_Node (N);
4669 if Nkind (Assoc) /= Nkind (N) then
4670 return Assoc;
4672 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
4673 return Assoc;
4675 else
4676 -- If the node is part of an inner generic, it may itself have been
4677 -- remapped into a further generic copy. Associated_Node is otherwise
4678 -- used for the entity of the node, and will be of a different node
4679 -- kind, or else N has been rewritten as a literal or function call.
4681 while Present (Associated_Node (Assoc))
4682 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
4683 loop
4684 Assoc := Associated_Node (Assoc);
4685 end loop;
4687 -- Follow and additional link in case the final node was rewritten.
4688 -- This can only happen with nested generic units.
4690 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
4691 and then Present (Associated_Node (Assoc))
4692 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
4693 N_Explicit_Dereference,
4694 N_Integer_Literal,
4695 N_Real_Literal,
4696 N_String_Literal))
4697 then
4698 Assoc := Associated_Node (Assoc);
4699 end if;
4701 return Assoc;
4702 end if;
4703 end Get_Associated_Node;
4705 -------------------------------------------
4706 -- Build_Instance_Compilation_Unit_Nodes --
4707 -------------------------------------------
4709 procedure Build_Instance_Compilation_Unit_Nodes
4710 (N : Node_Id;
4711 Act_Body : Node_Id;
4712 Act_Decl : Node_Id)
4714 Decl_Cunit : Node_Id;
4715 Body_Cunit : Node_Id;
4716 Citem : Node_Id;
4717 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
4718 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
4720 begin
4721 -- A new compilation unit node is built for the instance declaration
4723 Decl_Cunit :=
4724 Make_Compilation_Unit (Sloc (N),
4725 Context_Items => Empty_List,
4726 Unit => Act_Decl,
4727 Aux_Decls_Node =>
4728 Make_Compilation_Unit_Aux (Sloc (N)));
4730 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4732 -- The new compilation unit is linked to its body, but both share the
4733 -- same file, so we do not set Body_Required on the new unit so as not
4734 -- to create a spurious dependency on a non-existent body in the ali.
4735 -- This simplifies CodePeer unit traversal.
4737 -- We use the original instantiation compilation unit as the resulting
4738 -- compilation unit of the instance, since this is the main unit.
4740 Rewrite (N, Act_Body);
4741 Body_Cunit := Parent (N);
4743 -- The two compilation unit nodes are linked by the Library_Unit field
4745 Set_Library_Unit (Decl_Cunit, Body_Cunit);
4746 Set_Library_Unit (Body_Cunit, Decl_Cunit);
4748 -- Preserve the private nature of the package if needed
4750 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
4752 -- If the instance is not the main unit, its context, categorization
4753 -- and elaboration entity are not relevant to the compilation.
4755 if Body_Cunit /= Cunit (Main_Unit) then
4756 Make_Instance_Unit (Body_Cunit, In_Main => False);
4757 return;
4758 end if;
4760 -- The context clause items on the instantiation, which are now attached
4761 -- to the body compilation unit (since the body overwrote the original
4762 -- instantiation node), semantically belong on the spec, so copy them
4763 -- there. It's harmless to leave them on the body as well. In fact one
4764 -- could argue that they belong in both places.
4766 Citem := First (Context_Items (Body_Cunit));
4767 while Present (Citem) loop
4768 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
4769 Next (Citem);
4770 end loop;
4772 -- Propagate categorization flags on packages, so that they appear in
4773 -- the ali file for the spec of the unit.
4775 if Ekind (New_Main) = E_Package then
4776 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
4777 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
4778 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
4779 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
4780 Set_Is_Remote_Call_Interface
4781 (Old_Main, Is_Remote_Call_Interface (New_Main));
4782 end if;
4784 -- Make entry in Units table, so that binder can generate call to
4785 -- elaboration procedure for body, if any.
4787 Make_Instance_Unit (Body_Cunit, In_Main => True);
4788 Main_Unit_Entity := New_Main;
4789 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
4791 -- Build elaboration entity, since the instance may certainly generate
4792 -- elaboration code requiring a flag for protection.
4794 Build_Elaboration_Entity (Decl_Cunit, New_Main);
4795 end Build_Instance_Compilation_Unit_Nodes;
4797 -----------------------------
4798 -- Check_Access_Definition --
4799 -----------------------------
4801 procedure Check_Access_Definition (N : Node_Id) is
4802 begin
4803 pragma Assert
4804 (Ada_Version >= Ada_2005
4805 and then Present (Access_Definition (N)));
4806 null;
4807 end Check_Access_Definition;
4809 -----------------------------------
4810 -- Check_Formal_Package_Instance --
4811 -----------------------------------
4813 -- If the formal has specific parameters, they must match those of the
4814 -- actual. Both of them are instances, and the renaming declarations for
4815 -- their formal parameters appear in the same order in both. The analyzed
4816 -- formal has been analyzed in the context of the current instance.
4818 procedure Check_Formal_Package_Instance
4819 (Formal_Pack : Entity_Id;
4820 Actual_Pack : Entity_Id)
4822 E1 : Entity_Id := First_Entity (Actual_Pack);
4823 E2 : Entity_Id := First_Entity (Formal_Pack);
4825 Expr1 : Node_Id;
4826 Expr2 : Node_Id;
4828 procedure Check_Mismatch (B : Boolean);
4829 -- Common error routine for mismatch between the parameters of the
4830 -- actual instance and those of the formal package.
4832 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
4833 -- The formal may come from a nested formal package, and the actual may
4834 -- have been constant-folded. To determine whether the two denote the
4835 -- same entity we may have to traverse several definitions to recover
4836 -- the ultimate entity that they refer to.
4838 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
4839 -- Similarly, if the formal comes from a nested formal package, the
4840 -- actual may designate the formal through multiple renamings, which
4841 -- have to be followed to determine the original variable in question.
4843 --------------------
4844 -- Check_Mismatch --
4845 --------------------
4847 procedure Check_Mismatch (B : Boolean) is
4848 Kind : constant Node_Kind := Nkind (Parent (E2));
4850 begin
4851 if Kind = N_Formal_Type_Declaration then
4852 return;
4854 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
4855 N_Formal_Package_Declaration)
4856 or else Kind in N_Formal_Subprogram_Declaration
4857 then
4858 null;
4860 elsif B then
4861 Error_Msg_NE
4862 ("actual for & in actual instance does not match formal",
4863 Parent (Actual_Pack), E1);
4864 end if;
4865 end Check_Mismatch;
4867 --------------------------------
4868 -- Same_Instantiated_Constant --
4869 --------------------------------
4871 function Same_Instantiated_Constant
4872 (E1, E2 : Entity_Id) return Boolean
4874 Ent : Entity_Id;
4876 begin
4877 Ent := E2;
4878 while Present (Ent) loop
4879 if E1 = Ent then
4880 return True;
4882 elsif Ekind (Ent) /= E_Constant then
4883 return False;
4885 elsif Is_Entity_Name (Constant_Value (Ent)) then
4886 if Entity (Constant_Value (Ent)) = E1 then
4887 return True;
4888 else
4889 Ent := Entity (Constant_Value (Ent));
4890 end if;
4892 -- The actual may be a constant that has been folded. Recover
4893 -- original name.
4895 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
4896 Ent := Entity (Original_Node (Constant_Value (Ent)));
4897 else
4898 return False;
4899 end if;
4900 end loop;
4902 return False;
4903 end Same_Instantiated_Constant;
4905 --------------------------------
4906 -- Same_Instantiated_Variable --
4907 --------------------------------
4909 function Same_Instantiated_Variable
4910 (E1, E2 : Entity_Id) return Boolean
4912 function Original_Entity (E : Entity_Id) return Entity_Id;
4913 -- Follow chain of renamings to the ultimate ancestor
4915 ---------------------
4916 -- Original_Entity --
4917 ---------------------
4919 function Original_Entity (E : Entity_Id) return Entity_Id is
4920 Orig : Entity_Id;
4922 begin
4923 Orig := E;
4924 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
4925 and then Present (Renamed_Object (Orig))
4926 and then Is_Entity_Name (Renamed_Object (Orig))
4927 loop
4928 Orig := Entity (Renamed_Object (Orig));
4929 end loop;
4931 return Orig;
4932 end Original_Entity;
4934 -- Start of processing for Same_Instantiated_Variable
4936 begin
4937 return Ekind (E1) = Ekind (E2)
4938 and then Original_Entity (E1) = Original_Entity (E2);
4939 end Same_Instantiated_Variable;
4941 -- Start of processing for Check_Formal_Package_Instance
4943 begin
4944 while Present (E1)
4945 and then Present (E2)
4946 loop
4947 exit when Ekind (E1) = E_Package
4948 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
4950 -- If the formal is the renaming of the formal package, this
4951 -- is the end of its formal part, which may occur before the
4952 -- end of the formal part in the actual in the presence of
4953 -- defaulted parameters in the formal package.
4955 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
4956 and then Renamed_Entity (E2) = Scope (E2);
4958 -- The analysis of the actual may generate additional internal
4959 -- entities. If the formal is defaulted, there is no corresponding
4960 -- analysis and the internal entities must be skipped, until we
4961 -- find corresponding entities again.
4963 if Comes_From_Source (E2)
4964 and then not Comes_From_Source (E1)
4965 and then Chars (E1) /= Chars (E2)
4966 then
4967 while Present (E1)
4968 and then Chars (E1) /= Chars (E2)
4969 loop
4970 Next_Entity (E1);
4971 end loop;
4972 end if;
4974 if No (E1) then
4975 return;
4977 -- If the formal entity comes from a formal declaration, it was
4978 -- defaulted in the formal package, and no check is needed on it.
4980 elsif Nkind (Parent (E2)) = N_Formal_Object_Declaration then
4981 goto Next_E;
4983 elsif Is_Type (E1) then
4985 -- Subtypes must statically match. E1, E2 are the local entities
4986 -- that are subtypes of the actuals. Itypes generated for other
4987 -- parameters need not be checked, the check will be performed
4988 -- on the parameters themselves.
4990 -- If E2 is a formal type declaration, it is a defaulted parameter
4991 -- and needs no checking.
4993 if not Is_Itype (E1)
4994 and then not Is_Itype (E2)
4995 then
4996 Check_Mismatch
4997 (not Is_Type (E2)
4998 or else Etype (E1) /= Etype (E2)
4999 or else not Subtypes_Statically_Match (E1, E2));
5000 end if;
5002 elsif Ekind (E1) = E_Constant then
5004 -- IN parameters must denote the same static value, or the same
5005 -- constant, or the literal null.
5007 Expr1 := Expression (Parent (E1));
5009 if Ekind (E2) /= E_Constant then
5010 Check_Mismatch (True);
5011 goto Next_E;
5012 else
5013 Expr2 := Expression (Parent (E2));
5014 end if;
5016 if Is_Static_Expression (Expr1) then
5018 if not Is_Static_Expression (Expr2) then
5019 Check_Mismatch (True);
5021 elsif Is_Discrete_Type (Etype (E1)) then
5022 declare
5023 V1 : constant Uint := Expr_Value (Expr1);
5024 V2 : constant Uint := Expr_Value (Expr2);
5025 begin
5026 Check_Mismatch (V1 /= V2);
5027 end;
5029 elsif Is_Real_Type (Etype (E1)) then
5030 declare
5031 V1 : constant Ureal := Expr_Value_R (Expr1);
5032 V2 : constant Ureal := Expr_Value_R (Expr2);
5033 begin
5034 Check_Mismatch (V1 /= V2);
5035 end;
5037 elsif Is_String_Type (Etype (E1))
5038 and then Nkind (Expr1) = N_String_Literal
5039 then
5040 if Nkind (Expr2) /= N_String_Literal then
5041 Check_Mismatch (True);
5042 else
5043 Check_Mismatch
5044 (not String_Equal (Strval (Expr1), Strval (Expr2)));
5045 end if;
5046 end if;
5048 elsif Is_Entity_Name (Expr1) then
5049 if Is_Entity_Name (Expr2) then
5050 if Entity (Expr1) = Entity (Expr2) then
5051 null;
5052 else
5053 Check_Mismatch
5054 (not Same_Instantiated_Constant
5055 (Entity (Expr1), Entity (Expr2)));
5056 end if;
5057 else
5058 Check_Mismatch (True);
5059 end if;
5061 elsif Is_Entity_Name (Original_Node (Expr1))
5062 and then Is_Entity_Name (Expr2)
5063 and then
5064 Same_Instantiated_Constant
5065 (Entity (Original_Node (Expr1)), Entity (Expr2))
5066 then
5067 null;
5069 elsif Nkind (Expr1) = N_Null then
5070 Check_Mismatch (Nkind (Expr1) /= N_Null);
5072 else
5073 Check_Mismatch (True);
5074 end if;
5076 elsif Ekind (E1) = E_Variable then
5077 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
5079 elsif Ekind (E1) = E_Package then
5080 Check_Mismatch
5081 (Ekind (E1) /= Ekind (E2)
5082 or else Renamed_Object (E1) /= Renamed_Object (E2));
5084 elsif Is_Overloadable (E1) then
5086 -- Verify that the actual subprograms match. Note that actuals
5087 -- that are attributes are rewritten as subprograms. If the
5088 -- subprogram in the formal package is defaulted, no check is
5089 -- needed. Note that this can only happen in Ada 2005 when the
5090 -- formal package can be partially parameterized.
5092 if Nkind (Unit_Declaration_Node (E1)) =
5093 N_Subprogram_Renaming_Declaration
5094 and then From_Default (Unit_Declaration_Node (E1))
5095 then
5096 null;
5098 -- If the formal package has an "others" box association that
5099 -- covers this formal, there is no need for a check either.
5101 elsif Nkind (Unit_Declaration_Node (E2)) in
5102 N_Formal_Subprogram_Declaration
5103 and then Box_Present (Unit_Declaration_Node (E2))
5104 then
5105 null;
5107 -- Otherwise the actual in the formal and the actual in the
5108 -- instantiation of the formal must match, up to renamings.
5110 else
5111 Check_Mismatch
5112 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
5113 end if;
5115 else
5116 raise Program_Error;
5117 end if;
5119 <<Next_E>>
5120 Next_Entity (E1);
5121 Next_Entity (E2);
5122 end loop;
5123 end Check_Formal_Package_Instance;
5125 ---------------------------
5126 -- Check_Formal_Packages --
5127 ---------------------------
5129 procedure Check_Formal_Packages (P_Id : Entity_Id) is
5130 E : Entity_Id;
5131 Formal_P : Entity_Id;
5133 begin
5134 -- Iterate through the declarations in the instance, looking for package
5135 -- renaming declarations that denote instances of formal packages. Stop
5136 -- when we find the renaming of the current package itself. The
5137 -- declaration for a formal package without a box is followed by an
5138 -- internal entity that repeats the instantiation.
5140 E := First_Entity (P_Id);
5141 while Present (E) loop
5142 if Ekind (E) = E_Package then
5143 if Renamed_Object (E) = P_Id then
5144 exit;
5146 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
5147 null;
5149 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
5150 Formal_P := Next_Entity (E);
5151 Check_Formal_Package_Instance (Formal_P, E);
5153 -- After checking, remove the internal validating package. It
5154 -- is only needed for semantic checks, and as it may contain
5155 -- generic formal declarations it should not reach gigi.
5157 Remove (Unit_Declaration_Node (Formal_P));
5158 end if;
5159 end if;
5161 Next_Entity (E);
5162 end loop;
5163 end Check_Formal_Packages;
5165 ---------------------------------
5166 -- Check_Forward_Instantiation --
5167 ---------------------------------
5169 procedure Check_Forward_Instantiation (Decl : Node_Id) is
5170 S : Entity_Id;
5171 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
5173 begin
5174 -- The instantiation appears before the generic body if we are in the
5175 -- scope of the unit containing the generic, either in its spec or in
5176 -- the package body, and before the generic body.
5178 if Ekind (Gen_Comp) = E_Package_Body then
5179 Gen_Comp := Spec_Entity (Gen_Comp);
5180 end if;
5182 if In_Open_Scopes (Gen_Comp)
5183 and then No (Corresponding_Body (Decl))
5184 then
5185 S := Current_Scope;
5187 while Present (S)
5188 and then not Is_Compilation_Unit (S)
5189 and then not Is_Child_Unit (S)
5190 loop
5191 if Ekind (S) = E_Package then
5192 Set_Has_Forward_Instantiation (S);
5193 end if;
5195 S := Scope (S);
5196 end loop;
5197 end if;
5198 end Check_Forward_Instantiation;
5200 ---------------------------
5201 -- Check_Generic_Actuals --
5202 ---------------------------
5204 -- The visibility of the actuals may be different between the point of
5205 -- generic instantiation and the instantiation of the body.
5207 procedure Check_Generic_Actuals
5208 (Instance : Entity_Id;
5209 Is_Formal_Box : Boolean)
5211 E : Entity_Id;
5212 Astype : Entity_Id;
5214 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
5215 -- For a formal that is an array type, the component type is often a
5216 -- previous formal in the same unit. The privacy status of the component
5217 -- type will have been examined earlier in the traversal of the
5218 -- corresponding actuals, and this status should not be modified for the
5219 -- array type itself.
5221 -- To detect this case we have to rescan the list of formals, which
5222 -- is usually short enough to ignore the resulting inefficiency.
5224 -----------------------------
5225 -- Denotes_Previous_Actual --
5226 -----------------------------
5228 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
5229 Prev : Entity_Id;
5231 begin
5232 Prev := First_Entity (Instance);
5233 while Present (Prev) loop
5234 if Is_Type (Prev)
5235 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
5236 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
5237 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
5238 then
5239 return True;
5241 elsif Prev = E then
5242 return False;
5244 else
5245 Next_Entity (Prev);
5246 end if;
5247 end loop;
5249 return False;
5250 end Denotes_Previous_Actual;
5252 -- Start of processing for Check_Generic_Actuals
5254 begin
5255 E := First_Entity (Instance);
5256 while Present (E) loop
5257 if Is_Type (E)
5258 and then Nkind (Parent (E)) = N_Subtype_Declaration
5259 and then Scope (Etype (E)) /= Instance
5260 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
5261 then
5262 if Is_Array_Type (E)
5263 and then Denotes_Previous_Actual (Component_Type (E))
5264 then
5265 null;
5266 else
5267 Check_Private_View (Subtype_Indication (Parent (E)));
5268 end if;
5270 Set_Is_Generic_Actual_Type (E, True);
5271 Set_Is_Hidden (E, False);
5272 Set_Is_Potentially_Use_Visible (E,
5273 In_Use (Instance));
5275 -- We constructed the generic actual type as a subtype of the
5276 -- supplied type. This means that it normally would not inherit
5277 -- subtype specific attributes of the actual, which is wrong for
5278 -- the generic case.
5280 Astype := Ancestor_Subtype (E);
5282 if No (Astype) then
5284 -- This can happen when E is an itype that is the full view of
5285 -- a private type completed, e.g. with a constrained array. In
5286 -- that case, use the first subtype, which will carry size
5287 -- information. The base type itself is unconstrained and will
5288 -- not carry it.
5290 Astype := First_Subtype (E);
5291 end if;
5293 Set_Size_Info (E, (Astype));
5294 Set_RM_Size (E, RM_Size (Astype));
5295 Set_First_Rep_Item (E, First_Rep_Item (Astype));
5297 if Is_Discrete_Or_Fixed_Point_Type (E) then
5298 Set_RM_Size (E, RM_Size (Astype));
5300 -- In nested instances, the base type of an access actual
5301 -- may itself be private, and need to be exchanged.
5303 elsif Is_Access_Type (E)
5304 and then Is_Private_Type (Etype (E))
5305 then
5306 Check_Private_View
5307 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
5308 end if;
5310 elsif Ekind (E) = E_Package then
5312 -- If this is the renaming for the current instance, we're done.
5313 -- Otherwise it is a formal package. If the corresponding formal
5314 -- was declared with a box, the (instantiations of the) generic
5315 -- formal part are also visible. Otherwise, ignore the entity
5316 -- created to validate the actuals.
5318 if Renamed_Object (E) = Instance then
5319 exit;
5321 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
5322 null;
5324 -- The visibility of a formal of an enclosing generic is already
5325 -- correct.
5327 elsif Denotes_Formal_Package (E) then
5328 null;
5330 elsif Present (Associated_Formal_Package (E))
5331 and then not Is_Generic_Formal (E)
5332 then
5333 if Box_Present (Parent (Associated_Formal_Package (E))) then
5334 Check_Generic_Actuals (Renamed_Object (E), True);
5336 else
5337 Check_Generic_Actuals (Renamed_Object (E), False);
5338 end if;
5340 Set_Is_Hidden (E, False);
5341 end if;
5343 -- If this is a subprogram instance (in a wrapper package) the
5344 -- actual is fully visible.
5346 elsif Is_Wrapper_Package (Instance) then
5347 Set_Is_Hidden (E, False);
5349 -- If the formal package is declared with a box, or if the formal
5350 -- parameter is defaulted, it is visible in the body.
5352 elsif Is_Formal_Box
5353 or else Is_Visible_Formal (E)
5354 then
5355 Set_Is_Hidden (E, False);
5356 end if;
5358 if Ekind (E) = E_Constant then
5360 -- If the type of the actual is a private type declared in the
5361 -- enclosing scope of the generic unit, the body of the generic
5362 -- sees the full view of the type (because it has to appear in
5363 -- the corresponding package body). If the type is private now,
5364 -- exchange views to restore the proper visiblity in the instance.
5366 declare
5367 Typ : constant Entity_Id := Base_Type (Etype (E));
5368 -- The type of the actual
5370 Gen_Id : Entity_Id;
5371 -- The generic unit
5373 Parent_Scope : Entity_Id;
5374 -- The enclosing scope of the generic unit
5376 begin
5377 if Is_Wrapper_Package (Instance) then
5378 Gen_Id :=
5379 Generic_Parent
5380 (Specification
5381 (Unit_Declaration_Node
5382 (Related_Instance (Instance))));
5383 else
5384 Gen_Id :=
5385 Generic_Parent
5386 (Specification (Unit_Declaration_Node (Instance)));
5387 end if;
5389 Parent_Scope := Scope (Gen_Id);
5391 -- The exchange is only needed if the generic is defined
5392 -- within a package which is not a common ancestor of the
5393 -- scope of the instance, and is not already in scope.
5395 if Is_Private_Type (Typ)
5396 and then Scope (Typ) = Parent_Scope
5397 and then Scope (Instance) /= Parent_Scope
5398 and then Ekind (Parent_Scope) = E_Package
5399 and then not Is_Child_Unit (Gen_Id)
5400 then
5401 Switch_View (Typ);
5403 -- If the type of the entity is a subtype, it may also
5404 -- have to be made visible, together with the base type
5405 -- of its full view, after exchange.
5407 if Is_Private_Type (Etype (E)) then
5408 Switch_View (Etype (E));
5409 Switch_View (Base_Type (Etype (E)));
5410 end if;
5411 end if;
5412 end;
5413 end if;
5415 Next_Entity (E);
5416 end loop;
5417 end Check_Generic_Actuals;
5419 ------------------------------
5420 -- Check_Generic_Child_Unit --
5421 ------------------------------
5423 procedure Check_Generic_Child_Unit
5424 (Gen_Id : Node_Id;
5425 Parent_Installed : in out Boolean)
5427 Loc : constant Source_Ptr := Sloc (Gen_Id);
5428 Gen_Par : Entity_Id := Empty;
5429 E : Entity_Id;
5430 Inst_Par : Entity_Id;
5431 S : Node_Id;
5433 function Find_Generic_Child
5434 (Scop : Entity_Id;
5435 Id : Node_Id) return Entity_Id;
5436 -- Search generic parent for possible child unit with the given name
5438 function In_Enclosing_Instance return Boolean;
5439 -- Within an instance of the parent, the child unit may be denoted
5440 -- by a simple name, or an abbreviated expanded name. Examine enclosing
5441 -- scopes to locate a possible parent instantiation.
5443 ------------------------
5444 -- Find_Generic_Child --
5445 ------------------------
5447 function Find_Generic_Child
5448 (Scop : Entity_Id;
5449 Id : Node_Id) return Entity_Id
5451 E : Entity_Id;
5453 begin
5454 -- If entity of name is already set, instance has already been
5455 -- resolved, e.g. in an enclosing instantiation.
5457 if Present (Entity (Id)) then
5458 if Scope (Entity (Id)) = Scop then
5459 return Entity (Id);
5460 else
5461 return Empty;
5462 end if;
5464 else
5465 E := First_Entity (Scop);
5466 while Present (E) loop
5467 if Chars (E) = Chars (Id)
5468 and then Is_Child_Unit (E)
5469 then
5470 if Is_Child_Unit (E)
5471 and then not Is_Visible_Child_Unit (E)
5472 then
5473 Error_Msg_NE
5474 ("generic child unit& is not visible", Gen_Id, E);
5475 end if;
5477 Set_Entity (Id, E);
5478 return E;
5479 end if;
5481 Next_Entity (E);
5482 end loop;
5484 return Empty;
5485 end if;
5486 end Find_Generic_Child;
5488 ---------------------------
5489 -- In_Enclosing_Instance --
5490 ---------------------------
5492 function In_Enclosing_Instance return Boolean is
5493 Enclosing_Instance : Node_Id;
5494 Instance_Decl : Node_Id;
5496 begin
5497 -- We do not inline any call that contains instantiations, except
5498 -- for instantiations of Unchecked_Conversion, so if we are within
5499 -- an inlined body the current instance does not require parents.
5501 if In_Inlined_Body then
5502 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
5503 return False;
5504 end if;
5506 -- Loop to check enclosing scopes
5508 Enclosing_Instance := Current_Scope;
5509 while Present (Enclosing_Instance) loop
5510 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
5512 if Ekind (Enclosing_Instance) = E_Package
5513 and then Is_Generic_Instance (Enclosing_Instance)
5514 and then Present
5515 (Generic_Parent (Specification (Instance_Decl)))
5516 then
5517 -- Check whether the generic we are looking for is a child of
5518 -- this instance.
5520 E := Find_Generic_Child
5521 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
5522 exit when Present (E);
5524 else
5525 E := Empty;
5526 end if;
5528 Enclosing_Instance := Scope (Enclosing_Instance);
5529 end loop;
5531 if No (E) then
5533 -- Not a child unit
5535 Analyze (Gen_Id);
5536 return False;
5538 else
5539 Rewrite (Gen_Id,
5540 Make_Expanded_Name (Loc,
5541 Chars => Chars (E),
5542 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
5543 Selector_Name => New_Occurrence_Of (E, Loc)));
5545 Set_Entity (Gen_Id, E);
5546 Set_Etype (Gen_Id, Etype (E));
5547 Parent_Installed := False; -- Already in scope.
5548 return True;
5549 end if;
5550 end In_Enclosing_Instance;
5552 -- Start of processing for Check_Generic_Child_Unit
5554 begin
5555 -- If the name of the generic is given by a selected component, it may
5556 -- be the name of a generic child unit, and the prefix is the name of an
5557 -- instance of the parent, in which case the child unit must be visible.
5558 -- If this instance is not in scope, it must be placed there and removed
5559 -- after instantiation, because what is being instantiated is not the
5560 -- original child, but the corresponding child present in the instance
5561 -- of the parent.
5563 -- If the child is instantiated within the parent, it can be given by
5564 -- a simple name. In this case the instance is already in scope, but
5565 -- the child generic must be recovered from the generic parent as well.
5567 if Nkind (Gen_Id) = N_Selected_Component then
5568 S := Selector_Name (Gen_Id);
5569 Analyze (Prefix (Gen_Id));
5570 Inst_Par := Entity (Prefix (Gen_Id));
5572 if Ekind (Inst_Par) = E_Package
5573 and then Present (Renamed_Object (Inst_Par))
5574 then
5575 Inst_Par := Renamed_Object (Inst_Par);
5576 end if;
5578 if Ekind (Inst_Par) = E_Package then
5579 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
5580 Gen_Par := Generic_Parent (Parent (Inst_Par));
5582 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
5583 and then
5584 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
5585 then
5586 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
5587 end if;
5589 elsif Ekind (Inst_Par) = E_Generic_Package
5590 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
5591 then
5592 -- A formal package may be a real child package, and not the
5593 -- implicit instance within a parent. In this case the child is
5594 -- not visible and has to be retrieved explicitly as well.
5596 Gen_Par := Inst_Par;
5597 end if;
5599 if Present (Gen_Par) then
5601 -- The prefix denotes an instantiation. The entity itself may be a
5602 -- nested generic, or a child unit.
5604 E := Find_Generic_Child (Gen_Par, S);
5606 if Present (E) then
5607 Change_Selected_Component_To_Expanded_Name (Gen_Id);
5608 Set_Entity (Gen_Id, E);
5609 Set_Etype (Gen_Id, Etype (E));
5610 Set_Entity (S, E);
5611 Set_Etype (S, Etype (E));
5613 -- Indicate that this is a reference to the parent
5615 if In_Extended_Main_Source_Unit (Gen_Id) then
5616 Set_Is_Instantiated (Inst_Par);
5617 end if;
5619 -- A common mistake is to replicate the naming scheme of a
5620 -- hierarchy by instantiating a generic child directly, rather
5621 -- than the implicit child in a parent instance:
5623 -- generic .. package Gpar is ..
5624 -- generic .. package Gpar.Child is ..
5625 -- package Par is new Gpar ();
5627 -- with Gpar.Child;
5628 -- package Par.Child is new Gpar.Child ();
5629 -- rather than Par.Child
5631 -- In this case the instantiation is within Par, which is an
5632 -- instance, but Gpar does not denote Par because we are not IN
5633 -- the instance of Gpar, so this is illegal. The test below
5634 -- recognizes this particular case.
5636 if Is_Child_Unit (E)
5637 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
5638 and then (not In_Instance
5639 or else Nkind (Parent (Parent (Gen_Id))) =
5640 N_Compilation_Unit)
5641 then
5642 Error_Msg_N
5643 ("prefix of generic child unit must be instance of parent",
5644 Gen_Id);
5645 end if;
5647 if not In_Open_Scopes (Inst_Par)
5648 and then Nkind (Parent (Gen_Id)) not in
5649 N_Generic_Renaming_Declaration
5650 then
5651 Install_Parent (Inst_Par);
5652 Parent_Installed := True;
5654 elsif In_Open_Scopes (Inst_Par) then
5656 -- If the parent is already installed, install the actuals
5657 -- for its formal packages. This is necessary when the
5658 -- child instance is a child of the parent instance:
5659 -- in this case, the parent is placed on the scope stack
5660 -- but the formal packages are not made visible.
5662 Install_Formal_Packages (Inst_Par);
5663 end if;
5665 else
5666 -- If the generic parent does not contain an entity that
5667 -- corresponds to the selector, the instance doesn't either.
5668 -- Analyzing the node will yield the appropriate error message.
5669 -- If the entity is not a child unit, then it is an inner
5670 -- generic in the parent.
5672 Analyze (Gen_Id);
5673 end if;
5675 else
5676 Analyze (Gen_Id);
5678 if Is_Child_Unit (Entity (Gen_Id))
5679 and then
5680 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
5681 and then not In_Open_Scopes (Inst_Par)
5682 then
5683 Install_Parent (Inst_Par);
5684 Parent_Installed := True;
5686 -- The generic unit may be the renaming of the implicit child
5687 -- present in an instance. In that case the parent instance is
5688 -- obtained from the name of the renamed entity.
5690 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
5691 and then Present (Renamed_Entity (Entity (Gen_Id)))
5692 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
5693 then
5694 declare
5695 Renamed_Package : constant Node_Id :=
5696 Name (Parent (Entity (Gen_Id)));
5697 begin
5698 if Nkind (Renamed_Package) = N_Expanded_Name then
5699 Inst_Par := Entity (Prefix (Renamed_Package));
5700 Install_Parent (Inst_Par);
5701 Parent_Installed := True;
5702 end if;
5703 end;
5704 end if;
5705 end if;
5707 elsif Nkind (Gen_Id) = N_Expanded_Name then
5709 -- Entity already present, analyze prefix, whose meaning may be
5710 -- an instance in the current context. If it is an instance of
5711 -- a relative within another, the proper parent may still have
5712 -- to be installed, if they are not of the same generation.
5714 Analyze (Prefix (Gen_Id));
5716 -- In the unlikely case that a local declaration hides the name
5717 -- of the parent package, locate it on the homonym chain. If the
5718 -- context is an instance of the parent, the renaming entity is
5719 -- flagged as such.
5721 Inst_Par := Entity (Prefix (Gen_Id));
5722 while Present (Inst_Par)
5723 and then not Is_Package_Or_Generic_Package (Inst_Par)
5724 loop
5725 Inst_Par := Homonym (Inst_Par);
5726 end loop;
5728 pragma Assert (Present (Inst_Par));
5729 Set_Entity (Prefix (Gen_Id), Inst_Par);
5731 if In_Enclosing_Instance then
5732 null;
5734 elsif Present (Entity (Gen_Id))
5735 and then Is_Child_Unit (Entity (Gen_Id))
5736 and then not In_Open_Scopes (Inst_Par)
5737 then
5738 Install_Parent (Inst_Par);
5739 Parent_Installed := True;
5740 end if;
5742 elsif In_Enclosing_Instance then
5744 -- The child unit is found in some enclosing scope
5746 null;
5748 else
5749 Analyze (Gen_Id);
5751 -- If this is the renaming of the implicit child in a parent
5752 -- instance, recover the parent name and install it.
5754 if Is_Entity_Name (Gen_Id) then
5755 E := Entity (Gen_Id);
5757 if Is_Generic_Unit (E)
5758 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
5759 and then Is_Child_Unit (Renamed_Object (E))
5760 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
5761 and then Nkind (Name (Parent (E))) = N_Expanded_Name
5762 then
5763 Rewrite (Gen_Id,
5764 New_Copy_Tree (Name (Parent (E))));
5765 Inst_Par := Entity (Prefix (Gen_Id));
5767 if not In_Open_Scopes (Inst_Par) then
5768 Install_Parent (Inst_Par);
5769 Parent_Installed := True;
5770 end if;
5772 -- If it is a child unit of a non-generic parent, it may be
5773 -- use-visible and given by a direct name. Install parent as
5774 -- for other cases.
5776 elsif Is_Generic_Unit (E)
5777 and then Is_Child_Unit (E)
5778 and then
5779 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
5780 and then not Is_Generic_Unit (Scope (E))
5781 then
5782 if not In_Open_Scopes (Scope (E)) then
5783 Install_Parent (Scope (E));
5784 Parent_Installed := True;
5785 end if;
5786 end if;
5787 end if;
5788 end if;
5789 end Check_Generic_Child_Unit;
5791 -----------------------------
5792 -- Check_Hidden_Child_Unit --
5793 -----------------------------
5795 procedure Check_Hidden_Child_Unit
5796 (N : Node_Id;
5797 Gen_Unit : Entity_Id;
5798 Act_Decl_Id : Entity_Id)
5800 Gen_Id : constant Node_Id := Name (N);
5802 begin
5803 if Is_Child_Unit (Gen_Unit)
5804 and then Is_Child_Unit (Act_Decl_Id)
5805 and then Nkind (Gen_Id) = N_Expanded_Name
5806 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
5807 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
5808 then
5809 Error_Msg_Node_2 := Scope (Act_Decl_Id);
5810 Error_Msg_NE
5811 ("generic unit & is implicitly declared in &",
5812 Defining_Unit_Name (N), Gen_Unit);
5813 Error_Msg_N ("\instance must have different name",
5814 Defining_Unit_Name (N));
5815 end if;
5816 end Check_Hidden_Child_Unit;
5818 ------------------------
5819 -- Check_Private_View --
5820 ------------------------
5822 procedure Check_Private_View (N : Node_Id) is
5823 T : constant Entity_Id := Etype (N);
5824 BT : Entity_Id;
5826 begin
5827 -- Exchange views if the type was not private in the generic but is
5828 -- private at the point of instantiation. Do not exchange views if
5829 -- the scope of the type is in scope. This can happen if both generic
5830 -- and instance are sibling units, or if type is defined in a parent.
5831 -- In this case the visibility of the type will be correct for all
5832 -- semantic checks.
5834 if Present (T) then
5835 BT := Base_Type (T);
5837 if Is_Private_Type (T)
5838 and then not Has_Private_View (N)
5839 and then Present (Full_View (T))
5840 and then not In_Open_Scopes (Scope (T))
5841 then
5842 -- In the generic, the full type was visible. Save the private
5843 -- entity, for subsequent exchange.
5845 Switch_View (T);
5847 elsif Has_Private_View (N)
5848 and then not Is_Private_Type (T)
5849 and then not Has_Been_Exchanged (T)
5850 and then Etype (Get_Associated_Node (N)) /= T
5851 then
5852 -- Only the private declaration was visible in the generic. If
5853 -- the type appears in a subtype declaration, the subtype in the
5854 -- instance must have a view compatible with that of its parent,
5855 -- which must be exchanged (see corresponding code in Restore_
5856 -- Private_Views). Otherwise, if the type is defined in a parent
5857 -- unit, leave full visibility within instance, which is safe.
5859 if In_Open_Scopes (Scope (Base_Type (T)))
5860 and then not Is_Private_Type (Base_Type (T))
5861 and then Comes_From_Source (Base_Type (T))
5862 then
5863 null;
5865 elsif Nkind (Parent (N)) = N_Subtype_Declaration
5866 or else not In_Private_Part (Scope (Base_Type (T)))
5867 then
5868 Prepend_Elmt (T, Exchanged_Views);
5869 Exchange_Declarations (Etype (Get_Associated_Node (N)));
5870 end if;
5872 -- For composite types with inconsistent representation exchange
5873 -- component types accordingly.
5875 elsif Is_Access_Type (T)
5876 and then Is_Private_Type (Designated_Type (T))
5877 and then not Has_Private_View (N)
5878 and then Present (Full_View (Designated_Type (T)))
5879 then
5880 Switch_View (Designated_Type (T));
5882 elsif Is_Array_Type (T) then
5883 if Is_Private_Type (Component_Type (T))
5884 and then not Has_Private_View (N)
5885 and then Present (Full_View (Component_Type (T)))
5886 then
5887 Switch_View (Component_Type (T));
5888 end if;
5890 -- The normal exchange mechanism relies on the setting of a
5891 -- flag on the reference in the generic. However, an additional
5892 -- mechanism is needed for types that are not explicitly mentioned
5893 -- in the generic, but may be needed in expanded code in the
5894 -- instance. This includes component types of arrays and
5895 -- designated types of access types. This processing must also
5896 -- include the index types of arrays which we take care of here.
5898 declare
5899 Indx : Node_Id;
5900 Typ : Entity_Id;
5902 begin
5903 Indx := First_Index (T);
5904 Typ := Base_Type (Etype (Indx));
5905 while Present (Indx) loop
5906 if Is_Private_Type (Typ)
5907 and then Present (Full_View (Typ))
5908 then
5909 Switch_View (Typ);
5910 end if;
5912 Next_Index (Indx);
5913 end loop;
5914 end;
5916 elsif Is_Private_Type (T)
5917 and then Present (Full_View (T))
5918 and then Is_Array_Type (Full_View (T))
5919 and then Is_Private_Type (Component_Type (Full_View (T)))
5920 then
5921 Switch_View (T);
5923 -- Finally, a non-private subtype may have a private base type, which
5924 -- must be exchanged for consistency. This can happen when a package
5925 -- body is instantiated, when the scope stack is empty but in fact
5926 -- the subtype and the base type are declared in an enclosing scope.
5928 -- Note that in this case we introduce an inconsistency in the view
5929 -- set, because we switch the base type BT, but there could be some
5930 -- private dependent subtypes of BT which remain unswitched. Such
5931 -- subtypes might need to be switched at a later point (see specific
5932 -- provision for that case in Switch_View).
5934 elsif not Is_Private_Type (T)
5935 and then not Has_Private_View (N)
5936 and then Is_Private_Type (BT)
5937 and then Present (Full_View (BT))
5938 and then not Is_Generic_Type (BT)
5939 and then not In_Open_Scopes (BT)
5940 then
5941 Prepend_Elmt (Full_View (BT), Exchanged_Views);
5942 Exchange_Declarations (BT);
5943 end if;
5944 end if;
5945 end Check_Private_View;
5947 -----------------------------
5948 -- Check_Hidden_Primitives --
5949 -----------------------------
5951 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
5952 Actual : Node_Id;
5953 Gen_T : Entity_Id;
5954 Result : Elist_Id := No_Elist;
5956 begin
5957 if No (Assoc_List) then
5958 return No_Elist;
5959 end if;
5961 -- Traverse the list of associations between formals and actuals
5962 -- searching for renamings of tagged types
5964 Actual := First (Assoc_List);
5965 while Present (Actual) loop
5966 if Nkind (Actual) = N_Subtype_Declaration then
5967 Gen_T := Generic_Parent_Type (Actual);
5969 if Present (Gen_T)
5970 and then Is_Tagged_Type (Gen_T)
5971 then
5972 -- Traverse the list of primitives of the actual types
5973 -- searching for hidden primitives that are visible in the
5974 -- corresponding generic formal; leave them visible and
5975 -- append them to Result to restore their decoration later.
5977 Install_Hidden_Primitives
5978 (Prims_List => Result,
5979 Gen_T => Gen_T,
5980 Act_T => Entity (Subtype_Indication (Actual)));
5981 end if;
5982 end if;
5984 Next (Actual);
5985 end loop;
5987 return Result;
5988 end Check_Hidden_Primitives;
5990 --------------------------
5991 -- Contains_Instance_Of --
5992 --------------------------
5994 function Contains_Instance_Of
5995 (Inner : Entity_Id;
5996 Outer : Entity_Id;
5997 N : Node_Id) return Boolean
5999 Elmt : Elmt_Id;
6000 Scop : Entity_Id;
6002 begin
6003 Scop := Outer;
6005 -- Verify that there are no circular instantiations. We check whether
6006 -- the unit contains an instance of the current scope or some enclosing
6007 -- scope (in case one of the instances appears in a subunit). Longer
6008 -- circularities involving subunits might seem too pathological to
6009 -- consider, but they were not too pathological for the authors of
6010 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
6011 -- enclosing generic scopes as containing an instance.
6013 loop
6014 -- Within a generic subprogram body, the scope is not generic, to
6015 -- allow for recursive subprograms. Use the declaration to determine
6016 -- whether this is a generic unit.
6018 if Ekind (Scop) = E_Generic_Package
6019 or else (Is_Subprogram (Scop)
6020 and then Nkind (Unit_Declaration_Node (Scop)) =
6021 N_Generic_Subprogram_Declaration)
6022 then
6023 Elmt := First_Elmt (Inner_Instances (Inner));
6025 while Present (Elmt) loop
6026 if Node (Elmt) = Scop then
6027 Error_Msg_Node_2 := Inner;
6028 Error_Msg_NE
6029 ("circular Instantiation: & instantiated within &!",
6030 N, Scop);
6031 return True;
6033 elsif Node (Elmt) = Inner then
6034 return True;
6036 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
6037 Error_Msg_Node_2 := Inner;
6038 Error_Msg_NE
6039 ("circular Instantiation: & instantiated within &!",
6040 N, Node (Elmt));
6041 return True;
6042 end if;
6044 Next_Elmt (Elmt);
6045 end loop;
6047 -- Indicate that Inner is being instantiated within Scop
6049 Append_Elmt (Inner, Inner_Instances (Scop));
6050 end if;
6052 if Scop = Standard_Standard then
6053 exit;
6054 else
6055 Scop := Scope (Scop);
6056 end if;
6057 end loop;
6059 return False;
6060 end Contains_Instance_Of;
6062 -----------------------
6063 -- Copy_Generic_Node --
6064 -----------------------
6066 function Copy_Generic_Node
6067 (N : Node_Id;
6068 Parent_Id : Node_Id;
6069 Instantiating : Boolean) return Node_Id
6071 Ent : Entity_Id;
6072 New_N : Node_Id;
6074 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
6075 -- Check the given value of one of the Fields referenced by the
6076 -- current node to determine whether to copy it recursively. The
6077 -- field may hold a Node_Id, a List_Id, or an Elist_Id, or a plain
6078 -- value (Sloc, Uint, Char) in which case it need not be copied.
6080 procedure Copy_Descendants;
6081 -- Common utility for various nodes
6083 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
6084 -- Make copy of element list
6086 function Copy_Generic_List
6087 (L : List_Id;
6088 Parent_Id : Node_Id) return List_Id;
6089 -- Apply Copy_Node recursively to the members of a node list
6091 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
6092 -- True if an identifier is part of the defining program unit name
6093 -- of a child unit. The entity of such an identifier must be kept
6094 -- (for ASIS use) even though as the name of an enclosing generic
6095 -- it would otherwise not be preserved in the generic tree.
6097 ----------------------
6098 -- Copy_Descendants --
6099 ----------------------
6101 procedure Copy_Descendants is
6103 use Atree.Unchecked_Access;
6104 -- This code section is part of the implementation of an untyped
6105 -- tree traversal, so it needs direct access to node fields.
6107 begin
6108 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
6109 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
6110 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
6111 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
6112 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
6113 end Copy_Descendants;
6115 -----------------------------
6116 -- Copy_Generic_Descendant --
6117 -----------------------------
6119 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
6120 begin
6121 if D = Union_Id (Empty) then
6122 return D;
6124 elsif D in Node_Range then
6125 return Union_Id
6126 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
6128 elsif D in List_Range then
6129 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
6131 elsif D in Elist_Range then
6132 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
6134 -- Nothing else is copyable (e.g. Uint values), return as is
6136 else
6137 return D;
6138 end if;
6139 end Copy_Generic_Descendant;
6141 ------------------------
6142 -- Copy_Generic_Elist --
6143 ------------------------
6145 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
6146 M : Elmt_Id;
6147 L : Elist_Id;
6149 begin
6150 if Present (E) then
6151 L := New_Elmt_List;
6152 M := First_Elmt (E);
6153 while Present (M) loop
6154 Append_Elmt
6155 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
6156 Next_Elmt (M);
6157 end loop;
6159 return L;
6161 else
6162 return No_Elist;
6163 end if;
6164 end Copy_Generic_Elist;
6166 -----------------------
6167 -- Copy_Generic_List --
6168 -----------------------
6170 function Copy_Generic_List
6171 (L : List_Id;
6172 Parent_Id : Node_Id) return List_Id
6174 N : Node_Id;
6175 New_L : List_Id;
6177 begin
6178 if Present (L) then
6179 New_L := New_List;
6180 Set_Parent (New_L, Parent_Id);
6182 N := First (L);
6183 while Present (N) loop
6184 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
6185 Next (N);
6186 end loop;
6188 return New_L;
6190 else
6191 return No_List;
6192 end if;
6193 end Copy_Generic_List;
6195 ---------------------------
6196 -- In_Defining_Unit_Name --
6197 ---------------------------
6199 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
6200 begin
6201 return Present (Parent (Nam))
6202 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
6203 or else
6204 (Nkind (Parent (Nam)) = N_Expanded_Name
6205 and then In_Defining_Unit_Name (Parent (Nam))));
6206 end In_Defining_Unit_Name;
6208 -- Start of processing for Copy_Generic_Node
6210 begin
6211 if N = Empty then
6212 return N;
6213 end if;
6215 New_N := New_Copy (N);
6217 -- Copy aspects if present
6219 if Has_Aspects (N) then
6220 Set_Has_Aspects (New_N, False);
6221 Set_Aspect_Specifications
6222 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
6223 end if;
6225 if Instantiating then
6226 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
6227 end if;
6229 if not Is_List_Member (N) then
6230 Set_Parent (New_N, Parent_Id);
6231 end if;
6233 -- If defining identifier, then all fields have been copied already
6235 if Nkind (New_N) in N_Entity then
6236 null;
6238 -- Special casing for identifiers and other entity names and operators
6240 elsif Nkind_In (New_N, N_Identifier,
6241 N_Character_Literal,
6242 N_Expanded_Name,
6243 N_Operator_Symbol)
6244 or else Nkind (New_N) in N_Op
6245 then
6246 if not Instantiating then
6248 -- Link both nodes in order to assign subsequently the entity of
6249 -- the copy to the original node, in case this is a global
6250 -- reference.
6252 Set_Associated_Node (N, New_N);
6254 -- If we are within an instantiation, this is a nested generic
6255 -- that has already been analyzed at the point of definition. We
6256 -- must preserve references that were global to the enclosing
6257 -- parent at that point. Other occurrences, whether global or
6258 -- local to the current generic, must be resolved anew, so we
6259 -- reset the entity in the generic copy. A global reference has a
6260 -- smaller depth than the parent, or else the same depth in case
6261 -- both are distinct compilation units.
6262 -- A child unit is implicitly declared within the enclosing parent
6263 -- but is in fact global to it, and must be preserved.
6265 -- It is also possible for Current_Instantiated_Parent to be
6266 -- defined, and for this not to be a nested generic, namely if the
6267 -- unit is loaded through Rtsfind. In that case, the entity of
6268 -- New_N is only a link to the associated node, and not a defining
6269 -- occurrence.
6271 -- The entities for parent units in the defining_program_unit of a
6272 -- generic child unit are established when the context of the unit
6273 -- is first analyzed, before the generic copy is made. They are
6274 -- preserved in the copy for use in ASIS queries.
6276 Ent := Entity (New_N);
6278 if No (Current_Instantiated_Parent.Gen_Id) then
6279 if No (Ent)
6280 or else Nkind (Ent) /= N_Defining_Identifier
6281 or else not In_Defining_Unit_Name (N)
6282 then
6283 Set_Associated_Node (New_N, Empty);
6284 end if;
6286 elsif No (Ent)
6287 or else
6288 not Nkind_In (Ent, N_Defining_Identifier,
6289 N_Defining_Character_Literal,
6290 N_Defining_Operator_Symbol)
6291 or else No (Scope (Ent))
6292 or else
6293 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
6294 and then not Is_Child_Unit (Ent))
6295 or else
6296 (Scope_Depth (Scope (Ent)) >
6297 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
6298 and then
6299 Get_Source_Unit (Ent) =
6300 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
6301 then
6302 Set_Associated_Node (New_N, Empty);
6303 end if;
6305 -- Case of instantiating identifier or some other name or operator
6307 else
6308 -- If the associated node is still defined, the entity in it is
6309 -- global, and must be copied to the instance. If this copy is
6310 -- being made for a body to inline, it is applied to an
6311 -- instantiated tree, and the entity is already present and must
6312 -- be also preserved.
6314 declare
6315 Assoc : constant Node_Id := Get_Associated_Node (N);
6317 begin
6318 if Present (Assoc) then
6319 if Nkind (Assoc) = Nkind (N) then
6320 Set_Entity (New_N, Entity (Assoc));
6321 Check_Private_View (N);
6323 elsif Nkind (Assoc) = N_Function_Call then
6324 Set_Entity (New_N, Entity (Name (Assoc)));
6326 elsif Nkind_In (Assoc, N_Defining_Identifier,
6327 N_Defining_Character_Literal,
6328 N_Defining_Operator_Symbol)
6329 and then Expander_Active
6330 then
6331 -- Inlining case: we are copying a tree that contains
6332 -- global entities, which are preserved in the copy to be
6333 -- used for subsequent inlining.
6335 null;
6337 else
6338 Set_Entity (New_N, Empty);
6339 end if;
6340 end if;
6341 end;
6342 end if;
6344 -- For expanded name, we must copy the Prefix and Selector_Name
6346 if Nkind (N) = N_Expanded_Name then
6347 Set_Prefix
6348 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
6350 Set_Selector_Name (New_N,
6351 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
6353 -- For operators, we must copy the right operand
6355 elsif Nkind (N) in N_Op then
6356 Set_Right_Opnd (New_N,
6357 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
6359 -- And for binary operators, the left operand as well
6361 if Nkind (N) in N_Binary_Op then
6362 Set_Left_Opnd (New_N,
6363 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
6364 end if;
6365 end if;
6367 -- Special casing for stubs
6369 elsif Nkind (N) in N_Body_Stub then
6371 -- In any case, we must copy the specification or defining
6372 -- identifier as appropriate.
6374 if Nkind (N) = N_Subprogram_Body_Stub then
6375 Set_Specification (New_N,
6376 Copy_Generic_Node (Specification (N), New_N, Instantiating));
6378 else
6379 Set_Defining_Identifier (New_N,
6380 Copy_Generic_Node
6381 (Defining_Identifier (N), New_N, Instantiating));
6382 end if;
6384 -- If we are not instantiating, then this is where we load and
6385 -- analyze subunits, i.e. at the point where the stub occurs. A
6386 -- more permissive system might defer this analysis to the point
6387 -- of instantiation, but this seems to complicated for now.
6389 if not Instantiating then
6390 declare
6391 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
6392 Subunit : Node_Id;
6393 Unum : Unit_Number_Type;
6394 New_Body : Node_Id;
6396 begin
6397 -- Make sure that, if it is a subunit of the main unit that is
6398 -- preprocessed and if -gnateG is specified, the preprocessed
6399 -- file will be written.
6401 Lib.Analysing_Subunit_Of_Main :=
6402 Lib.In_Extended_Main_Source_Unit (N);
6403 Unum :=
6404 Load_Unit
6405 (Load_Name => Subunit_Name,
6406 Required => False,
6407 Subunit => True,
6408 Error_Node => N);
6409 Lib.Analysing_Subunit_Of_Main := False;
6411 -- If the proper body is not found, a warning message will be
6412 -- emitted when analyzing the stub, or later at the point
6413 -- of instantiation. Here we just leave the stub as is.
6415 if Unum = No_Unit then
6416 Subunits_Missing := True;
6417 goto Subunit_Not_Found;
6418 end if;
6420 Subunit := Cunit (Unum);
6422 if Nkind (Unit (Subunit)) /= N_Subunit then
6423 Error_Msg_N
6424 ("found child unit instead of expected SEPARATE subunit",
6425 Subunit);
6426 Error_Msg_Sloc := Sloc (N);
6427 Error_Msg_N ("\to complete stub #", Subunit);
6428 goto Subunit_Not_Found;
6429 end if;
6431 -- We must create a generic copy of the subunit, in order to
6432 -- perform semantic analysis on it, and we must replace the
6433 -- stub in the original generic unit with the subunit, in order
6434 -- to preserve non-local references within.
6436 -- Only the proper body needs to be copied. Library_Unit and
6437 -- context clause are simply inherited by the generic copy.
6438 -- Note that the copy (which may be recursive if there are
6439 -- nested subunits) must be done first, before attaching it to
6440 -- the enclosing generic.
6442 New_Body :=
6443 Copy_Generic_Node
6444 (Proper_Body (Unit (Subunit)),
6445 Empty, Instantiating => False);
6447 -- Now place the original proper body in the original generic
6448 -- unit. This is a body, not a compilation unit.
6450 Rewrite (N, Proper_Body (Unit (Subunit)));
6451 Set_Is_Compilation_Unit (Defining_Entity (N), False);
6452 Set_Was_Originally_Stub (N);
6454 -- Finally replace the body of the subunit with its copy, and
6455 -- make this new subunit into the library unit of the generic
6456 -- copy, which does not have stubs any longer.
6458 Set_Proper_Body (Unit (Subunit), New_Body);
6459 Set_Library_Unit (New_N, Subunit);
6460 Inherit_Context (Unit (Subunit), N);
6461 end;
6463 -- If we are instantiating, this must be an error case, since
6464 -- otherwise we would have replaced the stub node by the proper body
6465 -- that corresponds. So just ignore it in the copy (i.e. we have
6466 -- copied it, and that is good enough).
6468 else
6469 null;
6470 end if;
6472 <<Subunit_Not_Found>> null;
6474 -- If the node is a compilation unit, it is the subunit of a stub, which
6475 -- has been loaded already (see code below). In this case, the library
6476 -- unit field of N points to the parent unit (which is a compilation
6477 -- unit) and need not (and cannot!) be copied.
6479 -- When the proper body of the stub is analyzed, the library_unit link
6480 -- is used to establish the proper context (see sem_ch10).
6482 -- The other fields of a compilation unit are copied as usual
6484 elsif Nkind (N) = N_Compilation_Unit then
6486 -- This code can only be executed when not instantiating, because in
6487 -- the copy made for an instantiation, the compilation unit node has
6488 -- disappeared at the point that a stub is replaced by its proper
6489 -- body.
6491 pragma Assert (not Instantiating);
6493 Set_Context_Items (New_N,
6494 Copy_Generic_List (Context_Items (N), New_N));
6496 Set_Unit (New_N,
6497 Copy_Generic_Node (Unit (N), New_N, False));
6499 Set_First_Inlined_Subprogram (New_N,
6500 Copy_Generic_Node
6501 (First_Inlined_Subprogram (N), New_N, False));
6503 Set_Aux_Decls_Node (New_N,
6504 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
6506 -- For an assignment node, the assignment is known to be semantically
6507 -- legal if we are instantiating the template. This avoids incorrect
6508 -- diagnostics in generated code.
6510 elsif Nkind (N) = N_Assignment_Statement then
6512 -- Copy name and expression fields in usual manner
6514 Set_Name (New_N,
6515 Copy_Generic_Node (Name (N), New_N, Instantiating));
6517 Set_Expression (New_N,
6518 Copy_Generic_Node (Expression (N), New_N, Instantiating));
6520 if Instantiating then
6521 Set_Assignment_OK (Name (New_N), True);
6522 end if;
6524 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
6525 if not Instantiating then
6526 Set_Associated_Node (N, New_N);
6528 else
6529 if Present (Get_Associated_Node (N))
6530 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
6531 then
6532 -- In the generic the aggregate has some composite type. If at
6533 -- the point of instantiation the type has a private view,
6534 -- install the full view (and that of its ancestors, if any).
6536 declare
6537 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
6538 Rt : Entity_Id;
6540 begin
6541 if Present (T)
6542 and then Is_Private_Type (T)
6543 then
6544 Switch_View (T);
6545 end if;
6547 if Present (T)
6548 and then Is_Tagged_Type (T)
6549 and then Is_Derived_Type (T)
6550 then
6551 Rt := Root_Type (T);
6553 loop
6554 T := Etype (T);
6556 if Is_Private_Type (T) then
6557 Switch_View (T);
6558 end if;
6560 exit when T = Rt;
6561 end loop;
6562 end if;
6563 end;
6564 end if;
6565 end if;
6567 -- Do not copy the associated node, which points to the generic copy
6568 -- of the aggregate.
6570 declare
6571 use Atree.Unchecked_Access;
6572 -- This code section is part of the implementation of an untyped
6573 -- tree traversal, so it needs direct access to node fields.
6575 begin
6576 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
6577 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
6578 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
6579 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
6580 end;
6582 -- Allocators do not have an identifier denoting the access type, so we
6583 -- must locate it through the expression to check whether the views are
6584 -- consistent.
6586 elsif Nkind (N) = N_Allocator
6587 and then Nkind (Expression (N)) = N_Qualified_Expression
6588 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
6589 and then Instantiating
6590 then
6591 declare
6592 T : constant Node_Id :=
6593 Get_Associated_Node (Subtype_Mark (Expression (N)));
6594 Acc_T : Entity_Id;
6596 begin
6597 if Present (T) then
6599 -- Retrieve the allocator node in the generic copy
6601 Acc_T := Etype (Parent (Parent (T)));
6602 if Present (Acc_T)
6603 and then Is_Private_Type (Acc_T)
6604 then
6605 Switch_View (Acc_T);
6606 end if;
6607 end if;
6609 Copy_Descendants;
6610 end;
6612 -- For a proper body, we must catch the case of a proper body that
6613 -- replaces a stub. This represents the point at which a separate
6614 -- compilation unit, and hence template file, may be referenced, so we
6615 -- must make a new source instantiation entry for the template of the
6616 -- subunit, and ensure that all nodes in the subunit are adjusted using
6617 -- this new source instantiation entry.
6619 elsif Nkind (N) in N_Proper_Body then
6620 declare
6621 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
6623 begin
6624 if Instantiating and then Was_Originally_Stub (N) then
6625 Create_Instantiation_Source
6626 (Instantiation_Node,
6627 Defining_Entity (N),
6628 False,
6629 S_Adjustment);
6630 end if;
6632 -- Now copy the fields of the proper body, using the new
6633 -- adjustment factor if one was needed as per test above.
6635 Copy_Descendants;
6637 -- Restore the original adjustment factor in case changed
6639 S_Adjustment := Save_Adjustment;
6640 end;
6642 -- Don't copy Ident or Comment pragmas, since the comment belongs to the
6643 -- generic unit, not to the instantiating unit.
6645 elsif Nkind (N) = N_Pragma and then Instantiating then
6646 declare
6647 Prag_Id : constant Pragma_Id := Get_Pragma_Id (N);
6648 begin
6649 if Prag_Id = Pragma_Ident or else Prag_Id = Pragma_Comment then
6650 New_N := Make_Null_Statement (Sloc (N));
6652 else
6653 Copy_Descendants;
6654 end if;
6655 end;
6657 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
6659 -- No descendant fields need traversing
6661 null;
6663 elsif Nkind (N) = N_String_Literal
6664 and then Present (Etype (N))
6665 and then Instantiating
6666 then
6667 -- If the string is declared in an outer scope, the string_literal
6668 -- subtype created for it may have the wrong scope. We force the
6669 -- reanalysis of the constant to generate a new itype in the proper
6670 -- context.
6672 Set_Etype (New_N, Empty);
6673 Set_Analyzed (New_N, False);
6675 -- For the remaining nodes, copy their descendants recursively
6677 else
6678 Copy_Descendants;
6680 if Instantiating and then Nkind (N) = N_Subprogram_Body then
6681 Set_Generic_Parent (Specification (New_N), N);
6683 -- Should preserve Corresponding_Spec??? (12.3(14))
6684 end if;
6685 end if;
6687 return New_N;
6688 end Copy_Generic_Node;
6690 ----------------------------
6691 -- Denotes_Formal_Package --
6692 ----------------------------
6694 function Denotes_Formal_Package
6695 (Pack : Entity_Id;
6696 On_Exit : Boolean := False;
6697 Instance : Entity_Id := Empty) return Boolean
6699 Par : Entity_Id;
6700 Scop : constant Entity_Id := Scope (Pack);
6701 E : Entity_Id;
6703 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
6704 -- The package in question may be an actual for a previous formal
6705 -- package P of the current instance, so examine its actuals as well.
6706 -- This must be recursive over other formal packages.
6708 ----------------------------------
6709 -- Is_Actual_Of_Previous_Formal --
6710 ----------------------------------
6712 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
6713 E1 : Entity_Id;
6715 begin
6716 E1 := First_Entity (P);
6717 while Present (E1) and then E1 /= Instance loop
6718 if Ekind (E1) = E_Package
6719 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
6720 then
6721 if Renamed_Object (E1) = Pack then
6722 return True;
6724 elsif E1 = P or else Renamed_Object (E1) = P then
6725 return False;
6727 elsif Is_Actual_Of_Previous_Formal (E1) then
6728 return True;
6729 end if;
6730 end if;
6732 Next_Entity (E1);
6733 end loop;
6735 return False;
6736 end Is_Actual_Of_Previous_Formal;
6738 -- Start of processing for Denotes_Formal_Package
6740 begin
6741 if On_Exit then
6742 Par :=
6743 Instance_Envs.Table
6744 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
6745 else
6746 Par := Current_Instantiated_Parent.Act_Id;
6747 end if;
6749 if Ekind (Scop) = E_Generic_Package
6750 or else Nkind (Unit_Declaration_Node (Scop)) =
6751 N_Generic_Subprogram_Declaration
6752 then
6753 return True;
6755 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
6756 N_Formal_Package_Declaration
6757 then
6758 return True;
6760 elsif No (Par) then
6761 return False;
6763 else
6764 -- Check whether this package is associated with a formal package of
6765 -- the enclosing instantiation. Iterate over the list of renamings.
6767 E := First_Entity (Par);
6768 while Present (E) loop
6769 if Ekind (E) /= E_Package
6770 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
6771 then
6772 null;
6774 elsif Renamed_Object (E) = Par then
6775 return False;
6777 elsif Renamed_Object (E) = Pack then
6778 return True;
6780 elsif Is_Actual_Of_Previous_Formal (E) then
6781 return True;
6783 end if;
6785 Next_Entity (E);
6786 end loop;
6788 return False;
6789 end if;
6790 end Denotes_Formal_Package;
6792 -----------------
6793 -- End_Generic --
6794 -----------------
6796 procedure End_Generic is
6797 begin
6798 -- ??? More things could be factored out in this routine. Should
6799 -- probably be done at a later stage.
6801 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
6802 Generic_Flags.Decrement_Last;
6804 Expander_Mode_Restore;
6805 end End_Generic;
6807 -------------
6808 -- Earlier --
6809 -------------
6811 function Earlier (N1, N2 : Node_Id) return Boolean is
6812 D1 : Integer := 0;
6813 D2 : Integer := 0;
6814 P1 : Node_Id := N1;
6815 P2 : Node_Id := N2;
6817 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
6818 -- Find distance from given node to enclosing compilation unit
6820 ----------------
6821 -- Find_Depth --
6822 ----------------
6824 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
6825 begin
6826 while Present (P)
6827 and then Nkind (P) /= N_Compilation_Unit
6828 loop
6829 P := True_Parent (P);
6830 D := D + 1;
6831 end loop;
6832 end Find_Depth;
6834 -- Start of processing for Earlier
6836 begin
6837 Find_Depth (P1, D1);
6838 Find_Depth (P2, D2);
6840 if P1 /= P2 then
6841 return False;
6842 else
6843 P1 := N1;
6844 P2 := N2;
6845 end if;
6847 while D1 > D2 loop
6848 P1 := True_Parent (P1);
6849 D1 := D1 - 1;
6850 end loop;
6852 while D2 > D1 loop
6853 P2 := True_Parent (P2);
6854 D2 := D2 - 1;
6855 end loop;
6857 -- At this point P1 and P2 are at the same distance from the root.
6858 -- We examine their parents until we find a common declarative list,
6859 -- at which point we can establish their relative placement by
6860 -- comparing their ultimate slocs. If we reach the root, N1 and N2
6861 -- do not descend from the same declarative list (e.g. one is nested
6862 -- in the declarative part and the other is in a block in the
6863 -- statement part) and the earlier one is already frozen.
6865 while not Is_List_Member (P1)
6866 or else not Is_List_Member (P2)
6867 or else List_Containing (P1) /= List_Containing (P2)
6868 loop
6869 P1 := True_Parent (P1);
6870 P2 := True_Parent (P2);
6872 if Nkind (Parent (P1)) = N_Subunit then
6873 P1 := Corresponding_Stub (Parent (P1));
6874 end if;
6876 if Nkind (Parent (P2)) = N_Subunit then
6877 P2 := Corresponding_Stub (Parent (P2));
6878 end if;
6880 if P1 = P2 then
6881 return False;
6882 end if;
6883 end loop;
6885 -- If the sloc positions are different the result is unambiguous. If
6886 -- the slocs are identical, one of them must not come from source, which
6887 -- is the case for freeze nodes, whose sloc is unrelated to the point
6888 -- point at which they are inserted in the tree. The source node is the
6889 -- earlier one in the tree.
6891 if Top_Level_Location (Sloc (P1)) < Top_Level_Location (Sloc (P2)) then
6892 return True;
6894 elsif
6895 Top_Level_Location (Sloc (P1)) > Top_Level_Location (Sloc (P2))
6896 then
6897 return False;
6899 else
6900 return Comes_From_Source (P1);
6901 end if;
6902 end Earlier;
6904 ----------------------
6905 -- Find_Actual_Type --
6906 ----------------------
6908 function Find_Actual_Type
6909 (Typ : Entity_Id;
6910 Gen_Type : Entity_Id) return Entity_Id
6912 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
6913 T : Entity_Id;
6915 begin
6916 -- Special processing only applies to child units
6918 if not Is_Child_Unit (Gen_Scope) then
6919 return Get_Instance_Of (Typ);
6921 -- If designated or component type is itself a formal of the child unit,
6922 -- its instance is available.
6924 elsif Scope (Typ) = Gen_Scope then
6925 return Get_Instance_Of (Typ);
6927 -- If the array or access type is not declared in the parent unit,
6928 -- no special processing needed.
6930 elsif not Is_Generic_Type (Typ)
6931 and then Scope (Gen_Scope) /= Scope (Typ)
6932 then
6933 return Get_Instance_Of (Typ);
6935 -- Otherwise, retrieve designated or component type by visibility
6937 else
6938 T := Current_Entity (Typ);
6939 while Present (T) loop
6940 if In_Open_Scopes (Scope (T)) then
6941 return T;
6943 elsif Is_Generic_Actual_Type (T) then
6944 return T;
6945 end if;
6947 T := Homonym (T);
6948 end loop;
6950 return Typ;
6951 end if;
6952 end Find_Actual_Type;
6954 ----------------------------
6955 -- Freeze_Subprogram_Body --
6956 ----------------------------
6958 procedure Freeze_Subprogram_Body
6959 (Inst_Node : Node_Id;
6960 Gen_Body : Node_Id;
6961 Pack_Id : Entity_Id)
6963 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
6964 Par : constant Entity_Id := Scope (Gen_Unit);
6965 E_G_Id : Entity_Id;
6966 Enc_G : Entity_Id;
6967 Enc_I : Node_Id;
6968 F_Node : Node_Id;
6970 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
6971 -- Find innermost package body that encloses the given node, and which
6972 -- is not a compilation unit. Freeze nodes for the instance, or for its
6973 -- enclosing body, may be inserted after the enclosing_body of the
6974 -- generic unit. Used to determine proper placement of freeze node for
6975 -- both package and subprogram instances.
6977 function Package_Freeze_Node (B : Node_Id) return Node_Id;
6978 -- Find entity for given package body, and locate or create a freeze
6979 -- node for it.
6981 ----------------------------
6982 -- Enclosing_Package_Body --
6983 ----------------------------
6985 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
6986 P : Node_Id;
6988 begin
6989 P := Parent (N);
6990 while Present (P)
6991 and then Nkind (Parent (P)) /= N_Compilation_Unit
6992 loop
6993 if Nkind (P) = N_Package_Body then
6994 if Nkind (Parent (P)) = N_Subunit then
6995 return Corresponding_Stub (Parent (P));
6996 else
6997 return P;
6998 end if;
6999 end if;
7001 P := True_Parent (P);
7002 end loop;
7004 return Empty;
7005 end Enclosing_Package_Body;
7007 -------------------------
7008 -- Package_Freeze_Node --
7009 -------------------------
7011 function Package_Freeze_Node (B : Node_Id) return Node_Id is
7012 Id : Entity_Id;
7014 begin
7015 if Nkind (B) = N_Package_Body then
7016 Id := Corresponding_Spec (B);
7017 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
7018 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
7019 end if;
7021 Ensure_Freeze_Node (Id);
7022 return Freeze_Node (Id);
7023 end Package_Freeze_Node;
7025 -- Start of processing of Freeze_Subprogram_Body
7027 begin
7028 -- If the instance and the generic body appear within the same unit, and
7029 -- the instance precedes the generic, the freeze node for the instance
7030 -- must appear after that of the generic. If the generic is nested
7031 -- within another instance I2, then current instance must be frozen
7032 -- after I2. In both cases, the freeze nodes are those of enclosing
7033 -- packages. Otherwise, the freeze node is placed at the end of the
7034 -- current declarative part.
7036 Enc_G := Enclosing_Package_Body (Gen_Body);
7037 Enc_I := Enclosing_Package_Body (Inst_Node);
7038 Ensure_Freeze_Node (Pack_Id);
7039 F_Node := Freeze_Node (Pack_Id);
7041 if Is_Generic_Instance (Par)
7042 and then Present (Freeze_Node (Par))
7043 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
7044 then
7045 -- The parent was a premature instantiation. Insert freeze node at
7046 -- the end the current declarative part.
7048 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
7049 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7051 -- Handle the following case:
7053 -- package Parent_Inst is new ...
7054 -- Parent_Inst []
7056 -- procedure P ... -- this body freezes Parent_Inst
7058 -- package Inst is new ...
7060 -- In this particular scenario, the freeze node for Inst must be
7061 -- inserted in the same manner as that of Parent_Inst - before the
7062 -- next source body or at the end of the declarative list (body not
7063 -- available). If body P did not exist and Parent_Inst was frozen
7064 -- after Inst, either by a body following Inst or at the end of the
7065 -- declarative region, the freeze node for Inst must be inserted
7066 -- after that of Parent_Inst. This relation is established by
7067 -- comparing the Slocs of Parent_Inst freeze node and Inst.
7069 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
7070 List_Containing (Inst_Node)
7071 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
7072 then
7073 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7075 else
7076 Insert_After (Freeze_Node (Par), F_Node);
7077 end if;
7079 -- The body enclosing the instance should be frozen after the body that
7080 -- includes the generic, because the body of the instance may make
7081 -- references to entities therein. If the two are not in the same
7082 -- declarative part, or if the one enclosing the instance is frozen
7083 -- already, freeze the instance at the end of the current declarative
7084 -- part.
7086 elsif Is_Generic_Instance (Par)
7087 and then Present (Freeze_Node (Par))
7088 and then Present (Enc_I)
7089 then
7090 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
7091 or else
7092 (Nkind (Enc_I) = N_Package_Body
7093 and then
7094 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
7095 then
7096 -- The enclosing package may contain several instances. Rather
7097 -- than computing the earliest point at which to insert its freeze
7098 -- node, we place it at the end of the declarative part of the
7099 -- parent of the generic.
7101 Insert_Freeze_Node_For_Instance
7102 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
7103 end if;
7105 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7107 elsif Present (Enc_G)
7108 and then Present (Enc_I)
7109 and then Enc_G /= Enc_I
7110 and then Earlier (Inst_Node, Gen_Body)
7111 then
7112 if Nkind (Enc_G) = N_Package_Body then
7113 E_G_Id := Corresponding_Spec (Enc_G);
7114 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
7115 E_G_Id :=
7116 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
7117 end if;
7119 -- Freeze package that encloses instance, and place node after
7120 -- package that encloses generic. If enclosing package is already
7121 -- frozen we have to assume it is at the proper place. This may be a
7122 -- potential ABE that requires dynamic checking. Do not add a freeze
7123 -- node if the package that encloses the generic is inside the body
7124 -- that encloses the instance, because the freeze node would be in
7125 -- the wrong scope. Additional contortions needed if the bodies are
7126 -- within a subunit.
7128 declare
7129 Enclosing_Body : Node_Id;
7131 begin
7132 if Nkind (Enc_I) = N_Package_Body_Stub then
7133 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
7134 else
7135 Enclosing_Body := Enc_I;
7136 end if;
7138 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
7139 Insert_Freeze_Node_For_Instance
7140 (Enc_G, Package_Freeze_Node (Enc_I));
7141 end if;
7142 end;
7144 -- Freeze enclosing subunit before instance
7146 Ensure_Freeze_Node (E_G_Id);
7148 if not Is_List_Member (Freeze_Node (E_G_Id)) then
7149 Insert_After (Enc_G, Freeze_Node (E_G_Id));
7150 end if;
7152 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7154 else
7155 -- If none of the above, insert freeze node at the end of the current
7156 -- declarative part.
7158 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
7159 end if;
7160 end Freeze_Subprogram_Body;
7162 ----------------
7163 -- Get_Gen_Id --
7164 ----------------
7166 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
7167 begin
7168 return Generic_Renamings.Table (E).Gen_Id;
7169 end Get_Gen_Id;
7171 ---------------------
7172 -- Get_Instance_Of --
7173 ---------------------
7175 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
7176 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
7178 begin
7179 if Res /= Assoc_Null then
7180 return Generic_Renamings.Table (Res).Act_Id;
7181 else
7182 -- On exit, entity is not instantiated: not a generic parameter, or
7183 -- else parameter of an inner generic unit.
7185 return A;
7186 end if;
7187 end Get_Instance_Of;
7189 ------------------------------------
7190 -- Get_Package_Instantiation_Node --
7191 ------------------------------------
7193 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
7194 Decl : Node_Id := Unit_Declaration_Node (A);
7195 Inst : Node_Id;
7197 begin
7198 -- If the Package_Instantiation attribute has been set on the package
7199 -- entity, then use it directly when it (or its Original_Node) refers
7200 -- to an N_Package_Instantiation node. In principle it should be
7201 -- possible to have this field set in all cases, which should be
7202 -- investigated, and would allow this function to be significantly
7203 -- simplified. ???
7205 Inst := Package_Instantiation (A);
7207 if Present (Inst) then
7208 if Nkind (Inst) = N_Package_Instantiation then
7209 return Inst;
7211 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
7212 return Original_Node (Inst);
7213 end if;
7214 end if;
7216 -- If the instantiation is a compilation unit that does not need body
7217 -- then the instantiation node has been rewritten as a package
7218 -- declaration for the instance, and we return the original node.
7220 -- If it is a compilation unit and the instance node has not been
7221 -- rewritten, then it is still the unit of the compilation. Finally, if
7222 -- a body is present, this is a parent of the main unit whose body has
7223 -- been compiled for inlining purposes, and the instantiation node has
7224 -- been rewritten with the instance body.
7226 -- Otherwise the instantiation node appears after the declaration. If
7227 -- the entity is a formal package, the declaration may have been
7228 -- rewritten as a generic declaration (in the case of a formal with box)
7229 -- or left as a formal package declaration if it has actuals, and is
7230 -- found with a forward search.
7232 if Nkind (Parent (Decl)) = N_Compilation_Unit then
7233 if Nkind (Decl) = N_Package_Declaration
7234 and then Present (Corresponding_Body (Decl))
7235 then
7236 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
7237 end if;
7239 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
7240 return Original_Node (Decl);
7241 else
7242 return Unit (Parent (Decl));
7243 end if;
7245 elsif Nkind (Decl) = N_Package_Declaration
7246 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
7247 then
7248 return Original_Node (Decl);
7250 else
7251 Inst := Next (Decl);
7252 while not Nkind_In (Inst, N_Package_Instantiation,
7253 N_Formal_Package_Declaration)
7254 loop
7255 Next (Inst);
7256 end loop;
7258 return Inst;
7259 end if;
7260 end Get_Package_Instantiation_Node;
7262 ------------------------
7263 -- Has_Been_Exchanged --
7264 ------------------------
7266 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
7267 Next : Elmt_Id;
7269 begin
7270 Next := First_Elmt (Exchanged_Views);
7271 while Present (Next) loop
7272 if Full_View (Node (Next)) = E then
7273 return True;
7274 end if;
7276 Next_Elmt (Next);
7277 end loop;
7279 return False;
7280 end Has_Been_Exchanged;
7282 ----------
7283 -- Hash --
7284 ----------
7286 function Hash (F : Entity_Id) return HTable_Range is
7287 begin
7288 return HTable_Range (F mod HTable_Size);
7289 end Hash;
7291 ------------------------
7292 -- Hide_Current_Scope --
7293 ------------------------
7295 procedure Hide_Current_Scope is
7296 C : constant Entity_Id := Current_Scope;
7297 E : Entity_Id;
7299 begin
7300 Set_Is_Hidden_Open_Scope (C);
7302 E := First_Entity (C);
7303 while Present (E) loop
7304 if Is_Immediately_Visible (E) then
7305 Set_Is_Immediately_Visible (E, False);
7306 Append_Elmt (E, Hidden_Entities);
7307 end if;
7309 Next_Entity (E);
7310 end loop;
7312 -- Make the scope name invisible as well. This is necessary, but might
7313 -- conflict with calls to Rtsfind later on, in case the scope is a
7314 -- predefined one. There is no clean solution to this problem, so for
7315 -- now we depend on the user not redefining Standard itself in one of
7316 -- the parent units.
7318 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
7319 Set_Is_Immediately_Visible (C, False);
7320 Append_Elmt (C, Hidden_Entities);
7321 end if;
7323 end Hide_Current_Scope;
7325 --------------
7326 -- Init_Env --
7327 --------------
7329 procedure Init_Env is
7330 Saved : Instance_Env;
7332 begin
7333 Saved.Instantiated_Parent := Current_Instantiated_Parent;
7334 Saved.Exchanged_Views := Exchanged_Views;
7335 Saved.Hidden_Entities := Hidden_Entities;
7336 Saved.Current_Sem_Unit := Current_Sem_Unit;
7337 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
7338 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
7340 -- Save configuration switches. These may be reset if the unit is a
7341 -- predefined unit, and the current mode is not Ada 2005.
7343 Save_Opt_Config_Switches (Saved.Switches);
7345 Instance_Envs.Append (Saved);
7347 Exchanged_Views := New_Elmt_List;
7348 Hidden_Entities := New_Elmt_List;
7350 -- Make dummy entry for Instantiated parent. If generic unit is legal,
7351 -- this is set properly in Set_Instance_Env.
7353 Current_Instantiated_Parent :=
7354 (Current_Scope, Current_Scope, Assoc_Null);
7355 end Init_Env;
7357 ------------------------------
7358 -- In_Same_Declarative_Part --
7359 ------------------------------
7361 function In_Same_Declarative_Part
7362 (F_Node : Node_Id;
7363 Inst : Node_Id) return Boolean
7365 Decls : constant Node_Id := Parent (F_Node);
7366 Nod : Node_Id := Parent (Inst);
7368 begin
7369 while Present (Nod) loop
7370 if Nod = Decls then
7371 return True;
7373 elsif Nkind_In (Nod, N_Subprogram_Body,
7374 N_Package_Body,
7375 N_Package_Declaration,
7376 N_Task_Body,
7377 N_Protected_Body,
7378 N_Block_Statement)
7379 then
7380 return False;
7382 elsif Nkind (Nod) = N_Subunit then
7383 Nod := Corresponding_Stub (Nod);
7385 elsif Nkind (Nod) = N_Compilation_Unit then
7386 return False;
7388 else
7389 Nod := Parent (Nod);
7390 end if;
7391 end loop;
7393 return False;
7394 end In_Same_Declarative_Part;
7396 ---------------------
7397 -- In_Main_Context --
7398 ---------------------
7400 function In_Main_Context (E : Entity_Id) return Boolean is
7401 Context : List_Id;
7402 Clause : Node_Id;
7403 Nam : Node_Id;
7405 begin
7406 if not Is_Compilation_Unit (E)
7407 or else Ekind (E) /= E_Package
7408 or else In_Private_Part (E)
7409 then
7410 return False;
7411 end if;
7413 Context := Context_Items (Cunit (Main_Unit));
7415 Clause := First (Context);
7416 while Present (Clause) loop
7417 if Nkind (Clause) = N_With_Clause then
7418 Nam := Name (Clause);
7420 -- If the current scope is part of the context of the main unit,
7421 -- analysis of the corresponding with_clause is not complete, and
7422 -- the entity is not set. We use the Chars field directly, which
7423 -- might produce false positives in rare cases, but guarantees
7424 -- that we produce all the instance bodies we will need.
7426 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
7427 or else (Nkind (Nam) = N_Selected_Component
7428 and then Chars (Selector_Name (Nam)) = Chars (E))
7429 then
7430 return True;
7431 end if;
7432 end if;
7434 Next (Clause);
7435 end loop;
7437 return False;
7438 end In_Main_Context;
7440 ---------------------
7441 -- Inherit_Context --
7442 ---------------------
7444 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
7445 Current_Context : List_Id;
7446 Current_Unit : Node_Id;
7447 Item : Node_Id;
7448 New_I : Node_Id;
7450 begin
7451 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
7453 -- The inherited context is attached to the enclosing compilation
7454 -- unit. This is either the main unit, or the declaration for the
7455 -- main unit (in case the instantiation appears within the package
7456 -- declaration and the main unit is its body).
7458 Current_Unit := Parent (Inst);
7459 while Present (Current_Unit)
7460 and then Nkind (Current_Unit) /= N_Compilation_Unit
7461 loop
7462 Current_Unit := Parent (Current_Unit);
7463 end loop;
7465 Current_Context := Context_Items (Current_Unit);
7467 Item := First (Context_Items (Parent (Gen_Decl)));
7468 while Present (Item) loop
7469 if Nkind (Item) = N_With_Clause then
7471 -- Take care to prevent direct cyclic with's, which can happen
7472 -- if the generic body with's the current unit. Such a case
7473 -- would result in binder errors (or run-time errors if the
7474 -- -gnatE switch is in effect), but we want to prevent it here,
7475 -- because Sem.Walk_Library_Items doesn't like cycles. Note
7476 -- that we don't bother to detect indirect cycles.
7478 if Library_Unit (Item) /= Current_Unit then
7479 New_I := New_Copy (Item);
7480 Set_Implicit_With (New_I, True);
7481 Append (New_I, Current_Context);
7482 end if;
7483 end if;
7485 Next (Item);
7486 end loop;
7487 end if;
7488 end Inherit_Context;
7490 ----------------
7491 -- Initialize --
7492 ----------------
7494 procedure Initialize is
7495 begin
7496 Generic_Renamings.Init;
7497 Instance_Envs.Init;
7498 Generic_Flags.Init;
7499 Generic_Renamings_HTable.Reset;
7500 Circularity_Detected := False;
7501 Exchanged_Views := No_Elist;
7502 Hidden_Entities := No_Elist;
7503 end Initialize;
7505 -------------------------------------
7506 -- Insert_Freeze_Node_For_Instance --
7507 -------------------------------------
7509 procedure Insert_Freeze_Node_For_Instance
7510 (N : Node_Id;
7511 F_Node : Node_Id)
7513 Inst : constant Entity_Id := Entity (F_Node);
7514 Decl : Node_Id;
7515 Decls : List_Id;
7516 Par_N : Node_Id;
7518 function Enclosing_Body (N : Node_Id) return Node_Id;
7519 -- Find enclosing package or subprogram body, if any. Freeze node
7520 -- may be placed at end of current declarative list if previous
7521 -- instance and current one have different enclosing bodies.
7523 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
7524 -- Find the local instance, if any, that declares the generic that is
7525 -- being instantiated. If present, the freeze node for this instance
7526 -- must follow the freeze node for the previous instance.
7528 --------------------
7529 -- Enclosing_Body --
7530 --------------------
7532 function Enclosing_Body (N : Node_Id) return Node_Id is
7533 P : Node_Id;
7535 begin
7536 P := Parent (N);
7537 while Present (P)
7538 and then Nkind (Parent (P)) /= N_Compilation_Unit
7539 loop
7540 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
7541 if Nkind (Parent (P)) = N_Subunit then
7542 return Corresponding_Stub (Parent (P));
7543 else
7544 return P;
7545 end if;
7546 end if;
7548 P := True_Parent (P);
7549 end loop;
7551 return Empty;
7552 end Enclosing_Body;
7554 -----------------------
7555 -- Previous_Instance --
7556 -----------------------
7558 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
7559 S : Entity_Id;
7561 begin
7562 S := Scope (Gen);
7563 while Present (S)
7564 and then S /= Standard_Standard
7565 loop
7566 if Is_Generic_Instance (S)
7567 and then In_Same_Source_Unit (S, N)
7568 then
7569 return S;
7570 end if;
7572 S := Scope (S);
7573 end loop;
7575 return Empty;
7576 end Previous_Instance;
7578 -- Start of processing for Insert_Freeze_Node_For_Instance
7580 begin
7581 if not Is_List_Member (F_Node) then
7582 Decls := List_Containing (N);
7583 Par_N := Parent (Decls);
7584 Decl := N;
7586 -- If this is a package instance, check whether the generic is
7587 -- declared in a previous instance and the current instance is
7588 -- not within the previous one.
7590 if Present (Generic_Parent (Parent (Inst)))
7591 and then Is_In_Main_Unit (N)
7592 then
7593 declare
7594 Enclosing_N : constant Node_Id := Enclosing_Body (N);
7595 Par_I : constant Entity_Id :=
7596 Previous_Instance
7597 (Generic_Parent (Parent (Inst)));
7598 Scop : Entity_Id;
7600 begin
7601 if Present (Par_I)
7602 and then Earlier (N, Freeze_Node (Par_I))
7603 then
7604 Scop := Scope (Inst);
7606 -- If the current instance is within the one that contains
7607 -- the generic, the freeze node for the current one must
7608 -- appear in the current declarative part. Ditto, if the
7609 -- current instance is within another package instance or
7610 -- within a body that does not enclose the current instance.
7611 -- In these three cases the freeze node of the previous
7612 -- instance is not relevant.
7614 while Present (Scop)
7615 and then Scop /= Standard_Standard
7616 loop
7617 exit when Scop = Par_I
7618 or else
7619 (Is_Generic_Instance (Scop)
7620 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
7621 Scop := Scope (Scop);
7622 end loop;
7624 -- Previous instance encloses current instance
7626 if Scop = Par_I then
7627 null;
7629 -- If the next node is a source body we must freeze in
7630 -- the current scope as well.
7632 elsif Present (Next (N))
7633 and then Nkind_In (Next (N),
7634 N_Subprogram_Body, N_Package_Body)
7635 and then Comes_From_Source (Next (N))
7636 then
7637 null;
7639 -- Current instance is within an unrelated instance
7641 elsif Is_Generic_Instance (Scop) then
7642 null;
7644 -- Current instance is within an unrelated body
7646 elsif Present (Enclosing_N)
7647 and then Enclosing_N /= Enclosing_Body (Par_I)
7648 then
7649 null;
7651 else
7652 Insert_After (Freeze_Node (Par_I), F_Node);
7653 return;
7654 end if;
7655 end if;
7656 end;
7657 end if;
7659 -- When the instantiation occurs in a package declaration, append the
7660 -- freeze node to the private declarations (if any).
7662 if Nkind (Par_N) = N_Package_Specification
7663 and then Decls = Visible_Declarations (Par_N)
7664 and then Present (Private_Declarations (Par_N))
7665 and then not Is_Empty_List (Private_Declarations (Par_N))
7666 then
7667 Decls := Private_Declarations (Par_N);
7668 Decl := First (Decls);
7669 end if;
7671 -- Determine the proper freeze point of a package instantiation. We
7672 -- adhere to the general rule of a package or subprogram body causing
7673 -- freezing of anything before it in the same declarative region. In
7674 -- this case, the proper freeze point of a package instantiation is
7675 -- before the first source body which follows, or before a stub. This
7676 -- ensures that entities coming from the instance are already frozen
7677 -- and usable in source bodies.
7679 if Nkind (Par_N) /= N_Package_Declaration
7680 and then Ekind (Inst) = E_Package
7681 and then Is_Generic_Instance (Inst)
7682 and then
7683 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
7684 then
7685 while Present (Decl) loop
7686 if (Nkind (Decl) in N_Unit_Body
7687 or else
7688 Nkind (Decl) in N_Body_Stub)
7689 and then Comes_From_Source (Decl)
7690 then
7691 Insert_Before (Decl, F_Node);
7692 return;
7693 end if;
7695 Next (Decl);
7696 end loop;
7697 end if;
7699 -- In a package declaration, or if no previous body, insert at end
7700 -- of list.
7702 Set_Sloc (F_Node, Sloc (Last (Decls)));
7703 Insert_After (Last (Decls), F_Node);
7704 end if;
7705 end Insert_Freeze_Node_For_Instance;
7707 ------------------
7708 -- Install_Body --
7709 ------------------
7711 procedure Install_Body
7712 (Act_Body : Node_Id;
7713 N : Node_Id;
7714 Gen_Body : Node_Id;
7715 Gen_Decl : Node_Id)
7717 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
7718 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
7719 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
7720 Par : constant Entity_Id := Scope (Gen_Id);
7721 Gen_Unit : constant Node_Id :=
7722 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
7723 Orig_Body : Node_Id := Gen_Body;
7724 F_Node : Node_Id;
7725 Body_Unit : Node_Id;
7727 Must_Delay : Boolean;
7729 function Enclosing_Subp (Id : Entity_Id) return Entity_Id;
7730 -- Find subprogram (if any) that encloses instance and/or generic body
7732 function True_Sloc (N : Node_Id) return Source_Ptr;
7733 -- If the instance is nested inside a generic unit, the Sloc of the
7734 -- instance indicates the place of the original definition, not the
7735 -- point of the current enclosing instance. Pending a better usage of
7736 -- Slocs to indicate instantiation places, we determine the place of
7737 -- origin of a node by finding the maximum sloc of any ancestor node.
7738 -- Why is this not equivalent to Top_Level_Location ???
7740 --------------------
7741 -- Enclosing_Subp --
7742 --------------------
7744 function Enclosing_Subp (Id : Entity_Id) return Entity_Id is
7745 Scop : Entity_Id;
7747 begin
7748 Scop := Scope (Id);
7749 while Scop /= Standard_Standard
7750 and then not Is_Overloadable (Scop)
7751 loop
7752 Scop := Scope (Scop);
7753 end loop;
7755 return Scop;
7756 end Enclosing_Subp;
7758 ---------------
7759 -- True_Sloc --
7760 ---------------
7762 function True_Sloc (N : Node_Id) return Source_Ptr is
7763 Res : Source_Ptr;
7764 N1 : Node_Id;
7766 begin
7767 Res := Sloc (N);
7768 N1 := N;
7769 while Present (N1) and then N1 /= Act_Unit loop
7770 if Sloc (N1) > Res then
7771 Res := Sloc (N1);
7772 end if;
7774 N1 := Parent (N1);
7775 end loop;
7777 return Res;
7778 end True_Sloc;
7780 -- Start of processing for Install_Body
7782 begin
7783 -- If the body is a subunit, the freeze point is the corresponding stub
7784 -- in the current compilation, not the subunit itself.
7786 if Nkind (Parent (Gen_Body)) = N_Subunit then
7787 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
7788 else
7789 Orig_Body := Gen_Body;
7790 end if;
7792 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
7794 -- If the instantiation and the generic definition appear in the same
7795 -- package declaration, this is an early instantiation. If they appear
7796 -- in the same declarative part, it is an early instantiation only if
7797 -- the generic body appears textually later, and the generic body is
7798 -- also in the main unit.
7800 -- If instance is nested within a subprogram, and the generic body is
7801 -- not, the instance is delayed because the enclosing body is. If
7802 -- instance and body are within the same scope, or the same sub-
7803 -- program body, indicate explicitly that the instance is delayed.
7805 Must_Delay :=
7806 (Gen_Unit = Act_Unit
7807 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
7808 N_Generic_Package_Declaration)
7809 or else (Gen_Unit = Body_Unit
7810 and then True_Sloc (N) < Sloc (Orig_Body)))
7811 and then Is_In_Main_Unit (Gen_Unit)
7812 and then (Scope (Act_Id) = Scope (Gen_Id)
7813 or else
7814 Enclosing_Subp (Act_Id) = Enclosing_Subp (Gen_Id)));
7816 -- If this is an early instantiation, the freeze node is placed after
7817 -- the generic body. Otherwise, if the generic appears in an instance,
7818 -- we cannot freeze the current instance until the outer one is frozen.
7819 -- This is only relevant if the current instance is nested within some
7820 -- inner scope not itself within the outer instance. If this scope is
7821 -- a package body in the same declarative part as the outer instance,
7822 -- then that body needs to be frozen after the outer instance. Finally,
7823 -- if no delay is needed, we place the freeze node at the end of the
7824 -- current declarative part.
7826 if Expander_Active then
7827 Ensure_Freeze_Node (Act_Id);
7828 F_Node := Freeze_Node (Act_Id);
7830 if Must_Delay then
7831 Insert_After (Orig_Body, F_Node);
7833 elsif Is_Generic_Instance (Par)
7834 and then Present (Freeze_Node (Par))
7835 and then Scope (Act_Id) /= Par
7836 then
7837 -- Freeze instance of inner generic after instance of enclosing
7838 -- generic.
7840 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
7842 -- Handle the following case:
7844 -- package Parent_Inst is new ...
7845 -- Parent_Inst []
7847 -- procedure P ... -- this body freezes Parent_Inst
7849 -- package Inst is new ...
7851 -- In this particular scenario, the freeze node for Inst must
7852 -- be inserted in the same manner as that of Parent_Inst -
7853 -- before the next source body or at the end of the declarative
7854 -- list (body not available). If body P did not exist and
7855 -- Parent_Inst was frozen after Inst, either by a body
7856 -- following Inst or at the end of the declarative region, the
7857 -- freeze node for Inst must be inserted after that of
7858 -- Parent_Inst. This relation is established by comparing the
7859 -- Slocs of Parent_Inst freeze node and Inst.
7861 if List_Containing (Get_Package_Instantiation_Node (Par)) =
7862 List_Containing (N)
7863 and then Sloc (Freeze_Node (Par)) < Sloc (N)
7864 then
7865 Insert_Freeze_Node_For_Instance (N, F_Node);
7866 else
7867 Insert_After (Freeze_Node (Par), F_Node);
7868 end if;
7870 -- Freeze package enclosing instance of inner generic after
7871 -- instance of enclosing generic.
7873 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
7874 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
7875 then
7876 declare
7877 Enclosing : Entity_Id;
7879 begin
7880 Enclosing := Corresponding_Spec (Parent (N));
7882 if No (Enclosing) then
7883 Enclosing := Defining_Entity (Parent (N));
7884 end if;
7886 Insert_Freeze_Node_For_Instance (N, F_Node);
7887 Ensure_Freeze_Node (Enclosing);
7889 if not Is_List_Member (Freeze_Node (Enclosing)) then
7891 -- The enclosing context is a subunit, insert the freeze
7892 -- node after the stub.
7894 if Nkind (Parent (Parent (N))) = N_Subunit then
7895 Insert_Freeze_Node_For_Instance
7896 (Corresponding_Stub (Parent (Parent (N))),
7897 Freeze_Node (Enclosing));
7899 -- The parent instance has been frozen before the body of
7900 -- the enclosing package, insert the freeze node after
7901 -- the body.
7903 elsif List_Containing (Freeze_Node (Par)) =
7904 List_Containing (Parent (N))
7905 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
7906 then
7907 Insert_Freeze_Node_For_Instance
7908 (Parent (N), Freeze_Node (Enclosing));
7910 else
7911 Insert_After
7912 (Freeze_Node (Par), Freeze_Node (Enclosing));
7913 end if;
7914 end if;
7915 end;
7917 else
7918 Insert_Freeze_Node_For_Instance (N, F_Node);
7919 end if;
7921 else
7922 Insert_Freeze_Node_For_Instance (N, F_Node);
7923 end if;
7924 end if;
7926 Set_Is_Frozen (Act_Id);
7927 Insert_Before (N, Act_Body);
7928 Mark_Rewrite_Insertion (Act_Body);
7929 end Install_Body;
7931 -----------------------------
7932 -- Install_Formal_Packages --
7933 -----------------------------
7935 procedure Install_Formal_Packages (Par : Entity_Id) is
7936 E : Entity_Id;
7937 Gen : Entity_Id;
7938 Gen_E : Entity_Id := Empty;
7940 begin
7941 E := First_Entity (Par);
7943 -- If we are installing an instance parent, locate the formal packages
7944 -- of its generic parent.
7946 if Is_Generic_Instance (Par) then
7947 Gen := Generic_Parent (Specification (Unit_Declaration_Node (Par)));
7948 Gen_E := First_Entity (Gen);
7949 end if;
7951 while Present (E) loop
7952 if Ekind (E) = E_Package
7953 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
7954 then
7955 -- If this is the renaming for the parent instance, done
7957 if Renamed_Object (E) = Par then
7958 exit;
7960 -- The visibility of a formal of an enclosing generic is already
7961 -- correct.
7963 elsif Denotes_Formal_Package (E) then
7964 null;
7966 elsif Present (Associated_Formal_Package (E)) then
7967 Check_Generic_Actuals (Renamed_Object (E), True);
7968 Set_Is_Hidden (E, False);
7970 -- Find formal package in generic unit that corresponds to
7971 -- (instance of) formal package in instance.
7973 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
7974 Next_Entity (Gen_E);
7975 end loop;
7977 if Present (Gen_E) then
7978 Map_Formal_Package_Entities (Gen_E, E);
7979 end if;
7980 end if;
7981 end if;
7983 Next_Entity (E);
7984 if Present (Gen_E) then
7985 Next_Entity (Gen_E);
7986 end if;
7987 end loop;
7988 end Install_Formal_Packages;
7990 --------------------
7991 -- Install_Parent --
7992 --------------------
7994 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
7995 Ancestors : constant Elist_Id := New_Elmt_List;
7996 S : constant Entity_Id := Current_Scope;
7997 Inst_Par : Entity_Id;
7998 First_Par : Entity_Id;
7999 Inst_Node : Node_Id;
8000 Gen_Par : Entity_Id;
8001 First_Gen : Entity_Id;
8002 Elmt : Elmt_Id;
8004 procedure Install_Noninstance_Specs (Par : Entity_Id);
8005 -- Install the scopes of noninstance parent units ending with Par
8007 procedure Install_Spec (Par : Entity_Id);
8008 -- The child unit is within the declarative part of the parent, so
8009 -- the declarations within the parent are immediately visible.
8011 -------------------------------
8012 -- Install_Noninstance_Specs --
8013 -------------------------------
8015 procedure Install_Noninstance_Specs (Par : Entity_Id) is
8016 begin
8017 if Present (Par)
8018 and then Par /= Standard_Standard
8019 and then not In_Open_Scopes (Par)
8020 then
8021 Install_Noninstance_Specs (Scope (Par));
8022 Install_Spec (Par);
8023 end if;
8024 end Install_Noninstance_Specs;
8026 ------------------
8027 -- Install_Spec --
8028 ------------------
8030 procedure Install_Spec (Par : Entity_Id) is
8031 Spec : constant Node_Id :=
8032 Specification (Unit_Declaration_Node (Par));
8034 begin
8035 -- If this parent of the child instance is a top-level unit,
8036 -- then record the unit and its visibility for later resetting
8037 -- in Remove_Parent. We exclude units that are generic instances,
8038 -- as we only want to record this information for the ultimate
8039 -- top-level noninstance parent (is that always correct???).
8041 if Scope (Par) = Standard_Standard
8042 and then not Is_Generic_Instance (Par)
8043 then
8044 Parent_Unit_Visible := Is_Immediately_Visible (Par);
8045 Instance_Parent_Unit := Par;
8046 end if;
8048 -- Open the parent scope and make it and its declarations visible.
8049 -- If this point is not within a body, then only the visible
8050 -- declarations should be made visible, and installation of the
8051 -- private declarations is deferred until the appropriate point
8052 -- within analysis of the spec being instantiated (see the handling
8053 -- of parent visibility in Analyze_Package_Specification). This is
8054 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
8055 -- private view problems that occur when compiling instantiations of
8056 -- a generic child of that package (Generic_Dispatching_Constructor).
8057 -- If the instance freezes a tagged type, inlinings of operations
8058 -- from Ada.Tags may need the full view of type Tag. If inlining took
8059 -- proper account of establishing visibility of inlined subprograms'
8060 -- parents then it should be possible to remove this
8061 -- special check. ???
8063 Push_Scope (Par);
8064 Set_Is_Immediately_Visible (Par);
8065 Install_Visible_Declarations (Par);
8066 Set_Use (Visible_Declarations (Spec));
8068 if In_Body or else Is_RTU (Par, Ada_Tags) then
8069 Install_Private_Declarations (Par);
8070 Set_Use (Private_Declarations (Spec));
8071 end if;
8072 end Install_Spec;
8074 -- Start of processing for Install_Parent
8076 begin
8077 -- We need to install the parent instance to compile the instantiation
8078 -- of the child, but the child instance must appear in the current
8079 -- scope. Given that we cannot place the parent above the current scope
8080 -- in the scope stack, we duplicate the current scope and unstack both
8081 -- after the instantiation is complete.
8083 -- If the parent is itself the instantiation of a child unit, we must
8084 -- also stack the instantiation of its parent, and so on. Each such
8085 -- ancestor is the prefix of the name in a prior instantiation.
8087 -- If this is a nested instance, the parent unit itself resolves to
8088 -- a renaming of the parent instance, whose declaration we need.
8090 -- Finally, the parent may be a generic (not an instance) when the
8091 -- child unit appears as a formal package.
8093 Inst_Par := P;
8095 if Present (Renamed_Entity (Inst_Par)) then
8096 Inst_Par := Renamed_Entity (Inst_Par);
8097 end if;
8099 First_Par := Inst_Par;
8101 Gen_Par :=
8102 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
8104 First_Gen := Gen_Par;
8106 while Present (Gen_Par)
8107 and then Is_Child_Unit (Gen_Par)
8108 loop
8109 -- Load grandparent instance as well
8111 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
8113 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
8114 Inst_Par := Entity (Prefix (Name (Inst_Node)));
8116 if Present (Renamed_Entity (Inst_Par)) then
8117 Inst_Par := Renamed_Entity (Inst_Par);
8118 end if;
8120 Gen_Par :=
8121 Generic_Parent
8122 (Specification (Unit_Declaration_Node (Inst_Par)));
8124 if Present (Gen_Par) then
8125 Prepend_Elmt (Inst_Par, Ancestors);
8127 else
8128 -- Parent is not the name of an instantiation
8130 Install_Noninstance_Specs (Inst_Par);
8131 exit;
8132 end if;
8134 else
8135 -- Previous error
8137 exit;
8138 end if;
8139 end loop;
8141 if Present (First_Gen) then
8142 Append_Elmt (First_Par, Ancestors);
8143 else
8144 Install_Noninstance_Specs (First_Par);
8145 end if;
8147 if not Is_Empty_Elmt_List (Ancestors) then
8148 Elmt := First_Elmt (Ancestors);
8149 while Present (Elmt) loop
8150 Install_Spec (Node (Elmt));
8151 Install_Formal_Packages (Node (Elmt));
8152 Next_Elmt (Elmt);
8153 end loop;
8154 end if;
8156 if not In_Body then
8157 Push_Scope (S);
8158 end if;
8159 end Install_Parent;
8161 -------------------------------
8162 -- Install_Hidden_Primitives --
8163 -------------------------------
8165 procedure Install_Hidden_Primitives
8166 (Prims_List : in out Elist_Id;
8167 Gen_T : Entity_Id;
8168 Act_T : Entity_Id)
8170 Elmt : Elmt_Id;
8171 List : Elist_Id := No_Elist;
8172 Prim_G_Elmt : Elmt_Id;
8173 Prim_A_Elmt : Elmt_Id;
8174 Prim_G : Node_Id;
8175 Prim_A : Node_Id;
8177 begin
8178 -- No action needed in case of serious errors because we cannot trust
8179 -- in the order of primitives
8181 if Serious_Errors_Detected > 0 then
8182 return;
8184 -- No action possible if we don't have available the list of primitive
8185 -- operations
8187 elsif No (Gen_T)
8188 or else not Is_Record_Type (Gen_T)
8189 or else not Is_Tagged_Type (Gen_T)
8190 or else not Is_Record_Type (Act_T)
8191 or else not Is_Tagged_Type (Act_T)
8192 then
8193 return;
8195 -- There is no need to handle interface types since their primitives
8196 -- cannot be hidden
8198 elsif Is_Interface (Gen_T) then
8199 return;
8200 end if;
8202 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
8204 if not Is_Class_Wide_Type (Act_T) then
8205 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
8206 else
8207 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
8208 end if;
8210 loop
8211 -- Skip predefined primitives in the generic formal
8213 while Present (Prim_G_Elmt)
8214 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
8215 loop
8216 Next_Elmt (Prim_G_Elmt);
8217 end loop;
8219 -- Skip predefined primitives in the generic actual
8221 while Present (Prim_A_Elmt)
8222 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
8223 loop
8224 Next_Elmt (Prim_A_Elmt);
8225 end loop;
8227 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
8229 Prim_G := Node (Prim_G_Elmt);
8230 Prim_A := Node (Prim_A_Elmt);
8232 -- There is no need to handle interface primitives because their
8233 -- primitives are not hidden
8235 exit when Present (Interface_Alias (Prim_G));
8237 -- Here we install one hidden primitive
8239 if Chars (Prim_G) /= Chars (Prim_A)
8240 and then Has_Suffix (Prim_A, 'P')
8241 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
8242 then
8243 Set_Chars (Prim_A, Chars (Prim_G));
8245 if List = No_Elist then
8246 List := New_Elmt_List;
8247 end if;
8249 Append_Elmt (Prim_A, List);
8250 end if;
8252 Next_Elmt (Prim_A_Elmt);
8253 Next_Elmt (Prim_G_Elmt);
8254 end loop;
8256 -- Append the elements to the list of temporarily visible primitives
8257 -- avoiding duplicates.
8259 if Present (List) then
8260 if No (Prims_List) then
8261 Prims_List := New_Elmt_List;
8262 end if;
8264 Elmt := First_Elmt (List);
8265 while Present (Elmt) loop
8266 Append_Unique_Elmt (Node (Elmt), Prims_List);
8267 Next_Elmt (Elmt);
8268 end loop;
8269 end if;
8270 end Install_Hidden_Primitives;
8272 -------------------------------
8273 -- Restore_Hidden_Primitives --
8274 -------------------------------
8276 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
8277 Prim_Elmt : Elmt_Id;
8278 Prim : Node_Id;
8280 begin
8281 if Prims_List /= No_Elist then
8282 Prim_Elmt := First_Elmt (Prims_List);
8283 while Present (Prim_Elmt) loop
8284 Prim := Node (Prim_Elmt);
8285 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
8286 Next_Elmt (Prim_Elmt);
8287 end loop;
8289 Prims_List := No_Elist;
8290 end if;
8291 end Restore_Hidden_Primitives;
8293 --------------------------------
8294 -- Instantiate_Formal_Package --
8295 --------------------------------
8297 function Instantiate_Formal_Package
8298 (Formal : Node_Id;
8299 Actual : Node_Id;
8300 Analyzed_Formal : Node_Id) return List_Id
8302 Loc : constant Source_Ptr := Sloc (Actual);
8303 Actual_Pack : Entity_Id;
8304 Formal_Pack : Entity_Id;
8305 Gen_Parent : Entity_Id;
8306 Decls : List_Id;
8307 Nod : Node_Id;
8308 Parent_Spec : Node_Id;
8310 procedure Find_Matching_Actual
8311 (F : Node_Id;
8312 Act : in out Entity_Id);
8313 -- We need to associate each formal entity in the formal package
8314 -- with the corresponding entity in the actual package. The actual
8315 -- package has been analyzed and possibly expanded, and as a result
8316 -- there is no one-to-one correspondence between the two lists (for
8317 -- example, the actual may include subtypes, itypes, and inherited
8318 -- primitive operations, interspersed among the renaming declarations
8319 -- for the actuals) . We retrieve the corresponding actual by name
8320 -- because each actual has the same name as the formal, and they do
8321 -- appear in the same order.
8323 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
8324 -- Retrieve entity of defining entity of generic formal parameter.
8325 -- Only the declarations of formals need to be considered when
8326 -- linking them to actuals, but the declarative list may include
8327 -- internal entities generated during analysis, and those are ignored.
8329 procedure Match_Formal_Entity
8330 (Formal_Node : Node_Id;
8331 Formal_Ent : Entity_Id;
8332 Actual_Ent : Entity_Id);
8333 -- Associates the formal entity with the actual. In the case
8334 -- where Formal_Ent is a formal package, this procedure iterates
8335 -- through all of its formals and enters associations between the
8336 -- actuals occurring in the formal package's corresponding actual
8337 -- package (given by Actual_Ent) and the formal package's formal
8338 -- parameters. This procedure recurses if any of the parameters is
8339 -- itself a package.
8341 function Is_Instance_Of
8342 (Act_Spec : Entity_Id;
8343 Gen_Anc : Entity_Id) return Boolean;
8344 -- The actual can be an instantiation of a generic within another
8345 -- instance, in which case there is no direct link from it to the
8346 -- original generic ancestor. In that case, we recognize that the
8347 -- ultimate ancestor is the same by examining names and scopes.
8349 procedure Process_Nested_Formal (Formal : Entity_Id);
8350 -- If the current formal is declared with a box, its own formals are
8351 -- visible in the instance, as they were in the generic, and their
8352 -- Hidden flag must be reset. If some of these formals are themselves
8353 -- packages declared with a box, the processing must be recursive.
8355 --------------------------
8356 -- Find_Matching_Actual --
8357 --------------------------
8359 procedure Find_Matching_Actual
8360 (F : Node_Id;
8361 Act : in out Entity_Id)
8363 Formal_Ent : Entity_Id;
8365 begin
8366 case Nkind (Original_Node (F)) is
8367 when N_Formal_Object_Declaration |
8368 N_Formal_Type_Declaration =>
8369 Formal_Ent := Defining_Identifier (F);
8371 while Chars (Act) /= Chars (Formal_Ent) loop
8372 Next_Entity (Act);
8373 end loop;
8375 when N_Formal_Subprogram_Declaration |
8376 N_Formal_Package_Declaration |
8377 N_Package_Declaration |
8378 N_Generic_Package_Declaration =>
8379 Formal_Ent := Defining_Entity (F);
8381 while Chars (Act) /= Chars (Formal_Ent) loop
8382 Next_Entity (Act);
8383 end loop;
8385 when others =>
8386 raise Program_Error;
8387 end case;
8388 end Find_Matching_Actual;
8390 -------------------------
8391 -- Match_Formal_Entity --
8392 -------------------------
8394 procedure Match_Formal_Entity
8395 (Formal_Node : Node_Id;
8396 Formal_Ent : Entity_Id;
8397 Actual_Ent : Entity_Id)
8399 Act_Pkg : Entity_Id;
8401 begin
8402 Set_Instance_Of (Formal_Ent, Actual_Ent);
8404 if Ekind (Actual_Ent) = E_Package then
8406 -- Record associations for each parameter
8408 Act_Pkg := Actual_Ent;
8410 declare
8411 A_Ent : Entity_Id := First_Entity (Act_Pkg);
8412 F_Ent : Entity_Id;
8413 F_Node : Node_Id;
8415 Gen_Decl : Node_Id;
8416 Formals : List_Id;
8417 Actual : Entity_Id;
8419 begin
8420 -- Retrieve the actual given in the formal package declaration
8422 Actual := Entity (Name (Original_Node (Formal_Node)));
8424 -- The actual in the formal package declaration may be a
8425 -- renamed generic package, in which case we want to retrieve
8426 -- the original generic in order to traverse its formal part.
8428 if Present (Renamed_Entity (Actual)) then
8429 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
8430 else
8431 Gen_Decl := Unit_Declaration_Node (Actual);
8432 end if;
8434 Formals := Generic_Formal_Declarations (Gen_Decl);
8436 if Present (Formals) then
8437 F_Node := First_Non_Pragma (Formals);
8438 else
8439 F_Node := Empty;
8440 end if;
8442 while Present (A_Ent)
8443 and then Present (F_Node)
8444 and then A_Ent /= First_Private_Entity (Act_Pkg)
8445 loop
8446 F_Ent := Get_Formal_Entity (F_Node);
8448 if Present (F_Ent) then
8450 -- This is a formal of the original package. Record
8451 -- association and recurse.
8453 Find_Matching_Actual (F_Node, A_Ent);
8454 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
8455 Next_Entity (A_Ent);
8456 end if;
8458 Next_Non_Pragma (F_Node);
8459 end loop;
8460 end;
8461 end if;
8462 end Match_Formal_Entity;
8464 -----------------------
8465 -- Get_Formal_Entity --
8466 -----------------------
8468 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
8469 Kind : constant Node_Kind := Nkind (Original_Node (N));
8470 begin
8471 case Kind is
8472 when N_Formal_Object_Declaration =>
8473 return Defining_Identifier (N);
8475 when N_Formal_Type_Declaration =>
8476 return Defining_Identifier (N);
8478 when N_Formal_Subprogram_Declaration =>
8479 return Defining_Unit_Name (Specification (N));
8481 when N_Formal_Package_Declaration =>
8482 return Defining_Identifier (Original_Node (N));
8484 when N_Generic_Package_Declaration =>
8485 return Defining_Identifier (Original_Node (N));
8487 -- All other declarations are introduced by semantic analysis and
8488 -- have no match in the actual.
8490 when others =>
8491 return Empty;
8492 end case;
8493 end Get_Formal_Entity;
8495 --------------------
8496 -- Is_Instance_Of --
8497 --------------------
8499 function Is_Instance_Of
8500 (Act_Spec : Entity_Id;
8501 Gen_Anc : Entity_Id) return Boolean
8503 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
8505 begin
8506 if No (Gen_Par) then
8507 return False;
8509 -- Simplest case: the generic parent of the actual is the formal
8511 elsif Gen_Par = Gen_Anc then
8512 return True;
8514 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
8515 return False;
8517 -- The actual may be obtained through several instantiations. Its
8518 -- scope must itself be an instance of a generic declared in the
8519 -- same scope as the formal. Any other case is detected above.
8521 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
8522 return False;
8524 else
8525 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
8526 end if;
8527 end Is_Instance_Of;
8529 ---------------------------
8530 -- Process_Nested_Formal --
8531 ---------------------------
8533 procedure Process_Nested_Formal (Formal : Entity_Id) is
8534 Ent : Entity_Id;
8536 begin
8537 if Present (Associated_Formal_Package (Formal))
8538 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
8539 then
8540 Ent := First_Entity (Formal);
8541 while Present (Ent) loop
8542 Set_Is_Hidden (Ent, False);
8543 Set_Is_Visible_Formal (Ent);
8544 Set_Is_Potentially_Use_Visible
8545 (Ent, Is_Potentially_Use_Visible (Formal));
8547 if Ekind (Ent) = E_Package then
8548 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
8549 Process_Nested_Formal (Ent);
8550 end if;
8552 Next_Entity (Ent);
8553 end loop;
8554 end if;
8555 end Process_Nested_Formal;
8557 -- Start of processing for Instantiate_Formal_Package
8559 begin
8560 Analyze (Actual);
8562 if not Is_Entity_Name (Actual)
8563 or else Ekind (Entity (Actual)) /= E_Package
8564 then
8565 Error_Msg_N
8566 ("expect package instance to instantiate formal", Actual);
8567 Abandon_Instantiation (Actual);
8568 raise Program_Error;
8570 else
8571 Actual_Pack := Entity (Actual);
8572 Set_Is_Instantiated (Actual_Pack);
8574 -- The actual may be a renamed package, or an outer generic formal
8575 -- package whose instantiation is converted into a renaming.
8577 if Present (Renamed_Object (Actual_Pack)) then
8578 Actual_Pack := Renamed_Object (Actual_Pack);
8579 end if;
8581 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
8582 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
8583 Formal_Pack := Defining_Identifier (Analyzed_Formal);
8584 else
8585 Gen_Parent :=
8586 Generic_Parent (Specification (Analyzed_Formal));
8587 Formal_Pack :=
8588 Defining_Unit_Name (Specification (Analyzed_Formal));
8589 end if;
8591 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
8592 Parent_Spec := Specification (Unit_Declaration_Node (Actual_Pack));
8593 else
8594 Parent_Spec := Parent (Actual_Pack);
8595 end if;
8597 if Gen_Parent = Any_Id then
8598 Error_Msg_N
8599 ("previous error in declaration of formal package", Actual);
8600 Abandon_Instantiation (Actual);
8602 elsif
8603 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
8604 then
8605 null;
8607 else
8608 Error_Msg_NE
8609 ("actual parameter must be instance of&", Actual, Gen_Parent);
8610 Abandon_Instantiation (Actual);
8611 end if;
8613 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
8614 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
8616 Nod :=
8617 Make_Package_Renaming_Declaration (Loc,
8618 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
8619 Name => New_Reference_To (Actual_Pack, Loc));
8621 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
8622 Defining_Identifier (Formal));
8623 Decls := New_List (Nod);
8625 -- If the formal F has a box, then the generic declarations are
8626 -- visible in the generic G. In an instance of G, the corresponding
8627 -- entities in the actual for F (which are the actuals for the
8628 -- instantiation of the generic that F denotes) must also be made
8629 -- visible for analysis of the current instance. On exit from the
8630 -- current instance, those entities are made private again. If the
8631 -- actual is currently in use, these entities are also use-visible.
8633 -- The loop through the actual entities also steps through the formal
8634 -- entities and enters associations from formals to actuals into the
8635 -- renaming map. This is necessary to properly handle checking of
8636 -- actual parameter associations for later formals that depend on
8637 -- actuals declared in the formal package.
8639 -- In Ada 2005, partial parametrization requires that we make visible
8640 -- the actuals corresponding to formals that were defaulted in the
8641 -- formal package. There formals are identified because they remain
8642 -- formal generics within the formal package, rather than being
8643 -- renamings of the actuals supplied.
8645 declare
8646 Gen_Decl : constant Node_Id :=
8647 Unit_Declaration_Node (Gen_Parent);
8648 Formals : constant List_Id :=
8649 Generic_Formal_Declarations (Gen_Decl);
8651 Actual_Ent : Entity_Id;
8652 Actual_Of_Formal : Node_Id;
8653 Formal_Node : Node_Id;
8654 Formal_Ent : Entity_Id;
8656 begin
8657 if Present (Formals) then
8658 Formal_Node := First_Non_Pragma (Formals);
8659 else
8660 Formal_Node := Empty;
8661 end if;
8663 Actual_Ent := First_Entity (Actual_Pack);
8664 Actual_Of_Formal :=
8665 First (Visible_Declarations (Specification (Analyzed_Formal)));
8666 while Present (Actual_Ent)
8667 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
8668 loop
8669 if Present (Formal_Node) then
8670 Formal_Ent := Get_Formal_Entity (Formal_Node);
8672 if Present (Formal_Ent) then
8673 Find_Matching_Actual (Formal_Node, Actual_Ent);
8674 Match_Formal_Entity
8675 (Formal_Node, Formal_Ent, Actual_Ent);
8677 -- We iterate at the same time over the actuals of the
8678 -- local package created for the formal, to determine
8679 -- which one of the formals of the original generic were
8680 -- defaulted in the formal. The corresponding actual
8681 -- entities are visible in the enclosing instance.
8683 if Box_Present (Formal)
8684 or else
8685 (Present (Actual_Of_Formal)
8686 and then
8687 Is_Generic_Formal
8688 (Get_Formal_Entity (Actual_Of_Formal)))
8689 then
8690 Set_Is_Hidden (Actual_Ent, False);
8691 Set_Is_Visible_Formal (Actual_Ent);
8692 Set_Is_Potentially_Use_Visible
8693 (Actual_Ent, In_Use (Actual_Pack));
8695 if Ekind (Actual_Ent) = E_Package then
8696 Process_Nested_Formal (Actual_Ent);
8697 end if;
8699 else
8700 Set_Is_Hidden (Actual_Ent);
8701 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
8702 end if;
8703 end if;
8705 Next_Non_Pragma (Formal_Node);
8706 Next (Actual_Of_Formal);
8708 else
8709 -- No further formals to match, but the generic part may
8710 -- contain inherited operation that are not hidden in the
8711 -- enclosing instance.
8713 Next_Entity (Actual_Ent);
8714 end if;
8715 end loop;
8717 -- Inherited subprograms generated by formal derived types are
8718 -- also visible if the types are.
8720 Actual_Ent := First_Entity (Actual_Pack);
8721 while Present (Actual_Ent)
8722 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
8723 loop
8724 if Is_Overloadable (Actual_Ent)
8725 and then
8726 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
8727 and then
8728 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
8729 then
8730 Set_Is_Hidden (Actual_Ent, False);
8731 Set_Is_Potentially_Use_Visible
8732 (Actual_Ent, In_Use (Actual_Pack));
8733 end if;
8735 Next_Entity (Actual_Ent);
8736 end loop;
8737 end;
8739 -- If the formal is not declared with a box, reanalyze it as an
8740 -- abbreviated instantiation, to verify the matching rules of 12.7.
8741 -- The actual checks are performed after the generic associations
8742 -- have been analyzed, to guarantee the same visibility for this
8743 -- instantiation and for the actuals.
8745 -- In Ada 2005, the generic associations for the formal can include
8746 -- defaulted parameters. These are ignored during check. This
8747 -- internal instantiation is removed from the tree after conformance
8748 -- checking, because it contains formal declarations for those
8749 -- defaulted parameters, and those should not reach the back-end.
8751 if not Box_Present (Formal) then
8752 declare
8753 I_Pack : constant Entity_Id :=
8754 Make_Temporary (Sloc (Actual), 'P');
8756 begin
8757 Set_Is_Internal (I_Pack);
8759 Append_To (Decls,
8760 Make_Package_Instantiation (Sloc (Actual),
8761 Defining_Unit_Name => I_Pack,
8762 Name =>
8763 New_Occurrence_Of
8764 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
8765 Generic_Associations =>
8766 Generic_Associations (Formal)));
8767 end;
8768 end if;
8770 return Decls;
8771 end if;
8772 end Instantiate_Formal_Package;
8774 -----------------------------------
8775 -- Instantiate_Formal_Subprogram --
8776 -----------------------------------
8778 function Instantiate_Formal_Subprogram
8779 (Formal : Node_Id;
8780 Actual : Node_Id;
8781 Analyzed_Formal : Node_Id) return Node_Id
8783 Loc : Source_Ptr;
8784 Formal_Sub : constant Entity_Id :=
8785 Defining_Unit_Name (Specification (Formal));
8786 Analyzed_S : constant Entity_Id :=
8787 Defining_Unit_Name (Specification (Analyzed_Formal));
8788 Decl_Node : Node_Id;
8789 Nam : Node_Id;
8790 New_Spec : Node_Id;
8792 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
8793 -- If the generic is a child unit, the parent has been installed on the
8794 -- scope stack, but a default subprogram cannot resolve to something on
8795 -- the parent because that parent is not really part of the visible
8796 -- context (it is there to resolve explicit local entities). If the
8797 -- default has resolved in this way, we remove the entity from
8798 -- immediate visibility and analyze the node again to emit an error
8799 -- message or find another visible candidate.
8801 procedure Valid_Actual_Subprogram (Act : Node_Id);
8802 -- Perform legality check and raise exception on failure
8804 -----------------------
8805 -- From_Parent_Scope --
8806 -----------------------
8808 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
8809 Gen_Scope : Node_Id;
8811 begin
8812 Gen_Scope := Scope (Analyzed_S);
8813 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
8814 if Scope (Subp) = Scope (Gen_Scope) then
8815 return True;
8816 end if;
8818 Gen_Scope := Scope (Gen_Scope);
8819 end loop;
8821 return False;
8822 end From_Parent_Scope;
8824 -----------------------------
8825 -- Valid_Actual_Subprogram --
8826 -----------------------------
8828 procedure Valid_Actual_Subprogram (Act : Node_Id) is
8829 Act_E : Entity_Id;
8831 begin
8832 if Is_Entity_Name (Act) then
8833 Act_E := Entity (Act);
8835 elsif Nkind (Act) = N_Selected_Component
8836 and then Is_Entity_Name (Selector_Name (Act))
8837 then
8838 Act_E := Entity (Selector_Name (Act));
8840 else
8841 Act_E := Empty;
8842 end if;
8844 if (Present (Act_E) and then Is_Overloadable (Act_E))
8845 or else Nkind_In (Act, N_Attribute_Reference,
8846 N_Indexed_Component,
8847 N_Character_Literal,
8848 N_Explicit_Dereference)
8849 then
8850 return;
8851 end if;
8853 Error_Msg_NE
8854 ("expect subprogram or entry name in instantiation of&",
8855 Instantiation_Node, Formal_Sub);
8856 Abandon_Instantiation (Instantiation_Node);
8858 end Valid_Actual_Subprogram;
8860 -- Start of processing for Instantiate_Formal_Subprogram
8862 begin
8863 New_Spec := New_Copy_Tree (Specification (Formal));
8865 -- The tree copy has created the proper instantiation sloc for the
8866 -- new specification. Use this location for all other constructed
8867 -- declarations.
8869 Loc := Sloc (Defining_Unit_Name (New_Spec));
8871 -- Create new entity for the actual (New_Copy_Tree does not)
8873 Set_Defining_Unit_Name
8874 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
8876 -- Create new entities for the each of the formals in the
8877 -- specification of the renaming declaration built for the actual.
8879 if Present (Parameter_Specifications (New_Spec)) then
8880 declare
8881 F : Node_Id;
8882 begin
8883 F := First (Parameter_Specifications (New_Spec));
8884 while Present (F) loop
8885 Set_Defining_Identifier (F,
8886 Make_Defining_Identifier (Sloc (F),
8887 Chars => Chars (Defining_Identifier (F))));
8888 Next (F);
8889 end loop;
8890 end;
8891 end if;
8893 -- Find entity of actual. If the actual is an attribute reference, it
8894 -- cannot be resolved here (its formal is missing) but is handled
8895 -- instead in Attribute_Renaming. If the actual is overloaded, it is
8896 -- fully resolved subsequently, when the renaming declaration for the
8897 -- formal is analyzed. If it is an explicit dereference, resolve the
8898 -- prefix but not the actual itself, to prevent interpretation as call.
8900 if Present (Actual) then
8901 Loc := Sloc (Actual);
8902 Set_Sloc (New_Spec, Loc);
8904 if Nkind (Actual) = N_Operator_Symbol then
8905 Find_Direct_Name (Actual);
8907 elsif Nkind (Actual) = N_Explicit_Dereference then
8908 Analyze (Prefix (Actual));
8910 elsif Nkind (Actual) /= N_Attribute_Reference then
8911 Analyze (Actual);
8912 end if;
8914 Valid_Actual_Subprogram (Actual);
8915 Nam := Actual;
8917 elsif Present (Default_Name (Formal)) then
8918 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
8919 N_Selected_Component,
8920 N_Indexed_Component,
8921 N_Character_Literal)
8922 and then Present (Entity (Default_Name (Formal)))
8923 then
8924 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
8925 else
8926 Nam := New_Copy (Default_Name (Formal));
8927 Set_Sloc (Nam, Loc);
8928 end if;
8930 elsif Box_Present (Formal) then
8932 -- Actual is resolved at the point of instantiation. Create an
8933 -- identifier or operator with the same name as the formal.
8935 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
8936 Nam := Make_Operator_Symbol (Loc,
8937 Chars => Chars (Formal_Sub),
8938 Strval => No_String);
8939 else
8940 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
8941 end if;
8943 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
8944 and then Null_Present (Specification (Formal))
8945 then
8946 -- Generate null body for procedure, for use in the instance
8948 Decl_Node :=
8949 Make_Subprogram_Body (Loc,
8950 Specification => New_Spec,
8951 Declarations => New_List,
8952 Handled_Statement_Sequence =>
8953 Make_Handled_Sequence_Of_Statements (Loc,
8954 Statements => New_List (Make_Null_Statement (Loc))));
8956 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
8957 return Decl_Node;
8959 else
8960 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
8961 Error_Msg_NE
8962 ("missing actual&", Instantiation_Node, Formal_Sub);
8963 Error_Msg_NE
8964 ("\in instantiation of & declared#",
8965 Instantiation_Node, Scope (Analyzed_S));
8966 Abandon_Instantiation (Instantiation_Node);
8967 end if;
8969 Decl_Node :=
8970 Make_Subprogram_Renaming_Declaration (Loc,
8971 Specification => New_Spec,
8972 Name => Nam);
8974 -- If we do not have an actual and the formal specified <> then set to
8975 -- get proper default.
8977 if No (Actual) and then Box_Present (Formal) then
8978 Set_From_Default (Decl_Node);
8979 end if;
8981 -- Gather possible interpretations for the actual before analyzing the
8982 -- instance. If overloaded, it will be resolved when analyzing the
8983 -- renaming declaration.
8985 if Box_Present (Formal)
8986 and then No (Actual)
8987 then
8988 Analyze (Nam);
8990 if Is_Child_Unit (Scope (Analyzed_S))
8991 and then Present (Entity (Nam))
8992 then
8993 if not Is_Overloaded (Nam) then
8994 if From_Parent_Scope (Entity (Nam)) then
8995 Set_Is_Immediately_Visible (Entity (Nam), False);
8996 Set_Entity (Nam, Empty);
8997 Set_Etype (Nam, Empty);
8999 Analyze (Nam);
9000 Set_Is_Immediately_Visible (Entity (Nam));
9001 end if;
9003 else
9004 declare
9005 I : Interp_Index;
9006 It : Interp;
9008 begin
9009 Get_First_Interp (Nam, I, It);
9010 while Present (It.Nam) loop
9011 if From_Parent_Scope (It.Nam) then
9012 Remove_Interp (I);
9013 end if;
9015 Get_Next_Interp (I, It);
9016 end loop;
9017 end;
9018 end if;
9019 end if;
9020 end if;
9022 -- The generic instantiation freezes the actual. This can only be done
9023 -- once the actual is resolved, in the analysis of the renaming
9024 -- declaration. To make the formal subprogram entity available, we set
9025 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
9026 -- This is also needed in Analyze_Subprogram_Renaming for the processing
9027 -- of formal abstract subprograms.
9029 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
9031 -- We cannot analyze the renaming declaration, and thus find the actual,
9032 -- until all the actuals are assembled in the instance. For subsequent
9033 -- checks of other actuals, indicate the node that will hold the
9034 -- instance of this formal.
9036 Set_Instance_Of (Analyzed_S, Nam);
9038 if Nkind (Actual) = N_Selected_Component
9039 and then Is_Task_Type (Etype (Prefix (Actual)))
9040 and then not Is_Frozen (Etype (Prefix (Actual)))
9041 then
9042 -- The renaming declaration will create a body, which must appear
9043 -- outside of the instantiation, We move the renaming declaration
9044 -- out of the instance, and create an additional renaming inside,
9045 -- to prevent freezing anomalies.
9047 declare
9048 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
9050 begin
9051 Set_Defining_Unit_Name (New_Spec, Anon_Id);
9052 Insert_Before (Instantiation_Node, Decl_Node);
9053 Analyze (Decl_Node);
9055 -- Now create renaming within the instance
9057 Decl_Node :=
9058 Make_Subprogram_Renaming_Declaration (Loc,
9059 Specification => New_Copy_Tree (New_Spec),
9060 Name => New_Occurrence_Of (Anon_Id, Loc));
9062 Set_Defining_Unit_Name (Specification (Decl_Node),
9063 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
9064 end;
9065 end if;
9067 return Decl_Node;
9068 end Instantiate_Formal_Subprogram;
9070 ------------------------
9071 -- Instantiate_Object --
9072 ------------------------
9074 function Instantiate_Object
9075 (Formal : Node_Id;
9076 Actual : Node_Id;
9077 Analyzed_Formal : Node_Id) return List_Id
9079 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
9080 A_Gen_Obj : constant Entity_Id :=
9081 Defining_Identifier (Analyzed_Formal);
9082 Acc_Def : Node_Id := Empty;
9083 Act_Assoc : constant Node_Id := Parent (Actual);
9084 Actual_Decl : Node_Id := Empty;
9085 Decl_Node : Node_Id;
9086 Def : Node_Id;
9087 Ftyp : Entity_Id;
9088 List : constant List_Id := New_List;
9089 Loc : constant Source_Ptr := Sloc (Actual);
9090 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
9091 Subt_Decl : Node_Id := Empty;
9092 Subt_Mark : Node_Id := Empty;
9094 begin
9095 if Present (Subtype_Mark (Formal)) then
9096 Subt_Mark := Subtype_Mark (Formal);
9097 else
9098 Check_Access_Definition (Formal);
9099 Acc_Def := Access_Definition (Formal);
9100 end if;
9102 -- Sloc for error message on missing actual
9104 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
9106 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
9107 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
9108 end if;
9110 Set_Parent (List, Parent (Actual));
9112 -- OUT present
9114 if Out_Present (Formal) then
9116 -- An IN OUT generic actual must be a name. The instantiation is a
9117 -- renaming declaration. The actual is the name being renamed. We
9118 -- use the actual directly, rather than a copy, because it is not
9119 -- used further in the list of actuals, and because a copy or a use
9120 -- of relocate_node is incorrect if the instance is nested within a
9121 -- generic. In order to simplify ASIS searches, the Generic_Parent
9122 -- field links the declaration to the generic association.
9124 if No (Actual) then
9125 Error_Msg_NE
9126 ("missing actual&",
9127 Instantiation_Node, Gen_Obj);
9128 Error_Msg_NE
9129 ("\in instantiation of & declared#",
9130 Instantiation_Node, Scope (A_Gen_Obj));
9131 Abandon_Instantiation (Instantiation_Node);
9132 end if;
9134 if Present (Subt_Mark) then
9135 Decl_Node :=
9136 Make_Object_Renaming_Declaration (Loc,
9137 Defining_Identifier => New_Copy (Gen_Obj),
9138 Subtype_Mark => New_Copy_Tree (Subt_Mark),
9139 Name => Actual);
9141 else pragma Assert (Present (Acc_Def));
9142 Decl_Node :=
9143 Make_Object_Renaming_Declaration (Loc,
9144 Defining_Identifier => New_Copy (Gen_Obj),
9145 Access_Definition => New_Copy_Tree (Acc_Def),
9146 Name => Actual);
9147 end if;
9149 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
9151 -- The analysis of the actual may produce insert_action nodes, so
9152 -- the declaration must have a context in which to attach them.
9154 Append (Decl_Node, List);
9155 Analyze (Actual);
9157 -- Return if the analysis of the actual reported some error
9159 if Etype (Actual) = Any_Type then
9160 return List;
9161 end if;
9163 -- This check is performed here because Analyze_Object_Renaming will
9164 -- not check it when Comes_From_Source is False. Note though that the
9165 -- check for the actual being the name of an object will be performed
9166 -- in Analyze_Object_Renaming.
9168 if Is_Object_Reference (Actual)
9169 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
9170 then
9171 Error_Msg_N
9172 ("illegal discriminant-dependent component for in out parameter",
9173 Actual);
9174 end if;
9176 -- The actual has to be resolved in order to check that it is a
9177 -- variable (due to cases such as F (1), where F returns access to an
9178 -- array, and for overloaded prefixes).
9180 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
9182 -- If the type of the formal is not itself a formal, and the
9183 -- current unit is a child unit, the formal type must be declared
9184 -- in a parent, and must be retrieved by visibility.
9186 if Ftyp = Orig_Ftyp
9187 and then Is_Generic_Unit (Scope (Ftyp))
9188 and then Is_Child_Unit (Scope (A_Gen_Obj))
9189 then
9190 declare
9191 Temp : constant Node_Id :=
9192 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
9193 begin
9194 Set_Entity (Temp, Empty);
9195 Find_Type (Temp);
9196 Ftyp := Entity (Temp);
9197 end;
9198 end if;
9200 if Is_Private_Type (Ftyp)
9201 and then not Is_Private_Type (Etype (Actual))
9202 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
9203 or else Base_Type (Etype (Actual)) = Ftyp)
9204 then
9205 -- If the actual has the type of the full view of the formal, or
9206 -- else a non-private subtype of the formal, then the visibility
9207 -- of the formal type has changed. Add to the actuals a subtype
9208 -- declaration that will force the exchange of views in the body
9209 -- of the instance as well.
9211 Subt_Decl :=
9212 Make_Subtype_Declaration (Loc,
9213 Defining_Identifier => Make_Temporary (Loc, 'P'),
9214 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
9216 Prepend (Subt_Decl, List);
9218 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
9219 Exchange_Declarations (Ftyp);
9220 end if;
9222 Resolve (Actual, Ftyp);
9224 if not Denotes_Variable (Actual) then
9225 Error_Msg_NE
9226 ("actual for& must be a variable", Actual, Gen_Obj);
9228 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
9230 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
9231 -- the type of the actual shall resolve to a specific anonymous
9232 -- access type.
9234 if Ada_Version < Ada_2005
9235 or else
9236 Ekind (Base_Type (Ftyp)) /=
9237 E_Anonymous_Access_Type
9238 or else
9239 Ekind (Base_Type (Etype (Actual))) /=
9240 E_Anonymous_Access_Type
9241 then
9242 Error_Msg_NE ("type of actual does not match type of&",
9243 Actual, Gen_Obj);
9244 end if;
9245 end if;
9247 Note_Possible_Modification (Actual, Sure => True);
9249 -- Check for instantiation of atomic/volatile actual for
9250 -- non-atomic/volatile formal (RM C.6 (12)).
9252 if Is_Atomic_Object (Actual)
9253 and then not Is_Atomic (Orig_Ftyp)
9254 then
9255 Error_Msg_N
9256 ("cannot instantiate non-atomic formal object " &
9257 "with atomic actual", Actual);
9259 elsif Is_Volatile_Object (Actual)
9260 and then not Is_Volatile (Orig_Ftyp)
9261 then
9262 Error_Msg_N
9263 ("cannot instantiate non-volatile formal object " &
9264 "with volatile actual", Actual);
9265 end if;
9267 -- Formal in-parameter
9269 else
9270 -- The instantiation of a generic formal in-parameter is constant
9271 -- declaration. The actual is the expression for that declaration.
9273 if Present (Actual) then
9274 if Present (Subt_Mark) then
9275 Def := Subt_Mark;
9276 else pragma Assert (Present (Acc_Def));
9277 Def := Acc_Def;
9278 end if;
9280 Decl_Node :=
9281 Make_Object_Declaration (Loc,
9282 Defining_Identifier => New_Copy (Gen_Obj),
9283 Constant_Present => True,
9284 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
9285 Object_Definition => New_Copy_Tree (Def),
9286 Expression => Actual);
9288 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
9290 -- A generic formal object of a tagged type is defined to be
9291 -- aliased so the new constant must also be treated as aliased.
9293 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
9294 Set_Aliased_Present (Decl_Node);
9295 end if;
9297 Append (Decl_Node, List);
9299 -- No need to repeat (pre-)analysis of some expression nodes
9300 -- already handled in Preanalyze_Actuals.
9302 if Nkind (Actual) /= N_Allocator then
9303 Analyze (Actual);
9305 -- Return if the analysis of the actual reported some error
9307 if Etype (Actual) = Any_Type then
9308 return List;
9309 end if;
9310 end if;
9312 declare
9313 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
9314 Typ : Entity_Id;
9316 begin
9317 Typ := Get_Instance_Of (Formal_Type);
9319 Freeze_Before (Instantiation_Node, Typ);
9321 -- If the actual is an aggregate, perform name resolution on
9322 -- its components (the analysis of an aggregate does not do it)
9323 -- to capture local names that may be hidden if the generic is
9324 -- a child unit.
9326 if Nkind (Actual) = N_Aggregate then
9327 Preanalyze_And_Resolve (Actual, Typ);
9328 end if;
9330 if Is_Limited_Type (Typ)
9331 and then not OK_For_Limited_Init (Typ, Actual)
9332 then
9333 Error_Msg_N
9334 ("initialization not allowed for limited types", Actual);
9335 Explain_Limited_Type (Typ, Actual);
9336 end if;
9337 end;
9339 elsif Present (Default_Expression (Formal)) then
9341 -- Use default to construct declaration
9343 if Present (Subt_Mark) then
9344 Def := Subt_Mark;
9345 else pragma Assert (Present (Acc_Def));
9346 Def := Acc_Def;
9347 end if;
9349 Decl_Node :=
9350 Make_Object_Declaration (Sloc (Formal),
9351 Defining_Identifier => New_Copy (Gen_Obj),
9352 Constant_Present => True,
9353 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
9354 Object_Definition => New_Copy (Def),
9355 Expression => New_Copy_Tree
9356 (Default_Expression (Formal)));
9358 Append (Decl_Node, List);
9359 Set_Analyzed (Expression (Decl_Node), False);
9361 else
9362 Error_Msg_NE
9363 ("missing actual&",
9364 Instantiation_Node, Gen_Obj);
9365 Error_Msg_NE ("\in instantiation of & declared#",
9366 Instantiation_Node, Scope (A_Gen_Obj));
9368 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
9370 -- Create dummy constant declaration so that instance can be
9371 -- analyzed, to minimize cascaded visibility errors.
9373 if Present (Subt_Mark) then
9374 Def := Subt_Mark;
9375 else pragma Assert (Present (Acc_Def));
9376 Def := Acc_Def;
9377 end if;
9379 Decl_Node :=
9380 Make_Object_Declaration (Loc,
9381 Defining_Identifier => New_Copy (Gen_Obj),
9382 Constant_Present => True,
9383 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
9384 Object_Definition => New_Copy (Def),
9385 Expression =>
9386 Make_Attribute_Reference (Sloc (Gen_Obj),
9387 Attribute_Name => Name_First,
9388 Prefix => New_Copy (Def)));
9390 Append (Decl_Node, List);
9392 else
9393 Abandon_Instantiation (Instantiation_Node);
9394 end if;
9395 end if;
9396 end if;
9398 if Nkind (Actual) in N_Has_Entity then
9399 Actual_Decl := Parent (Entity (Actual));
9400 end if;
9402 -- Ada 2005 (AI-423): For a formal object declaration with a null
9403 -- exclusion or an access definition that has a null exclusion: If the
9404 -- actual matching the formal object declaration denotes a generic
9405 -- formal object of another generic unit G, and the instantiation
9406 -- containing the actual occurs within the body of G or within the body
9407 -- of a generic unit declared within the declarative region of G, then
9408 -- the declaration of the formal object of G must have a null exclusion.
9409 -- Otherwise, the subtype of the actual matching the formal object
9410 -- declaration shall exclude null.
9412 if Ada_Version >= Ada_2005
9413 and then Present (Actual_Decl)
9414 and then
9415 Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
9416 N_Object_Declaration)
9417 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
9418 and then not Has_Null_Exclusion (Actual_Decl)
9419 and then Has_Null_Exclusion (Analyzed_Formal)
9420 then
9421 Error_Msg_Sloc := Sloc (Analyzed_Formal);
9422 Error_Msg_N
9423 ("actual must exclude null to match generic formal#", Actual);
9424 end if;
9426 return List;
9427 end Instantiate_Object;
9429 ------------------------------
9430 -- Instantiate_Package_Body --
9431 ------------------------------
9433 procedure Instantiate_Package_Body
9434 (Body_Info : Pending_Body_Info;
9435 Inlined_Body : Boolean := False;
9436 Body_Optional : Boolean := False)
9438 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
9439 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
9440 Loc : constant Source_Ptr := Sloc (Inst_Node);
9442 Gen_Id : constant Node_Id := Name (Inst_Node);
9443 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
9444 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
9445 Act_Spec : constant Node_Id := Specification (Act_Decl);
9446 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
9448 Act_Body_Name : Node_Id;
9449 Gen_Body : Node_Id;
9450 Gen_Body_Id : Node_Id;
9451 Act_Body : Node_Id;
9452 Act_Body_Id : Entity_Id;
9454 Parent_Installed : Boolean := False;
9455 Save_Style_Check : constant Boolean := Style_Check;
9457 Par_Ent : Entity_Id := Empty;
9458 Par_Vis : Boolean := False;
9460 Vis_Prims_List : Elist_Id := No_Elist;
9461 -- List of primitives made temporarily visible in the instantiation
9462 -- to match the visibility of the formal type
9464 begin
9465 Gen_Body_Id := Corresponding_Body (Gen_Decl);
9467 -- The instance body may already have been processed, as the parent of
9468 -- another instance that is inlined (Load_Parent_Of_Generic).
9470 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
9471 return;
9472 end if;
9474 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
9476 -- Re-establish the state of information on which checks are suppressed.
9477 -- This information was set in Body_Info at the point of instantiation,
9478 -- and now we restore it so that the instance is compiled using the
9479 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
9481 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
9482 Scope_Suppress := Body_Info.Scope_Suppress;
9483 Opt.Ada_Version := Body_Info.Version;
9485 if No (Gen_Body_Id) then
9486 Load_Parent_Of_Generic
9487 (Inst_Node, Specification (Gen_Decl), Body_Optional);
9488 Gen_Body_Id := Corresponding_Body (Gen_Decl);
9489 end if;
9491 -- Establish global variable for sloc adjustment and for error recovery
9493 Instantiation_Node := Inst_Node;
9495 if Present (Gen_Body_Id) then
9496 Save_Env (Gen_Unit, Act_Decl_Id);
9497 Style_Check := False;
9498 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
9500 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
9502 Create_Instantiation_Source
9503 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
9505 Act_Body :=
9506 Copy_Generic_Node
9507 (Original_Node (Gen_Body), Empty, Instantiating => True);
9509 -- Build new name (possibly qualified) for body declaration
9511 Act_Body_Id := New_Copy (Act_Decl_Id);
9513 -- Some attributes of spec entity are not inherited by body entity
9515 Set_Handler_Records (Act_Body_Id, No_List);
9517 if Nkind (Defining_Unit_Name (Act_Spec)) =
9518 N_Defining_Program_Unit_Name
9519 then
9520 Act_Body_Name :=
9521 Make_Defining_Program_Unit_Name (Loc,
9522 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
9523 Defining_Identifier => Act_Body_Id);
9524 else
9525 Act_Body_Name := Act_Body_Id;
9526 end if;
9528 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
9530 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
9531 Check_Generic_Actuals (Act_Decl_Id, False);
9533 -- Install primitives hidden at the point of the instantiation but
9534 -- visible when processing the generic formals
9536 declare
9537 E : Entity_Id;
9539 begin
9540 E := First_Entity (Act_Decl_Id);
9541 while Present (E) loop
9542 if Is_Type (E)
9543 and then Is_Generic_Actual_Type (E)
9544 and then Is_Tagged_Type (E)
9545 then
9546 Install_Hidden_Primitives
9547 (Prims_List => Vis_Prims_List,
9548 Gen_T => Generic_Parent_Type (Parent (E)),
9549 Act_T => E);
9550 end if;
9552 Next_Entity (E);
9553 end loop;
9554 end;
9556 -- If it is a child unit, make the parent instance (which is an
9557 -- instance of the parent of the generic) visible. The parent
9558 -- instance is the prefix of the name of the generic unit.
9560 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
9561 and then Nkind (Gen_Id) = N_Expanded_Name
9562 then
9563 Par_Ent := Entity (Prefix (Gen_Id));
9564 Par_Vis := Is_Immediately_Visible (Par_Ent);
9565 Install_Parent (Par_Ent, In_Body => True);
9566 Parent_Installed := True;
9568 elsif Is_Child_Unit (Gen_Unit) then
9569 Par_Ent := Scope (Gen_Unit);
9570 Par_Vis := Is_Immediately_Visible (Par_Ent);
9571 Install_Parent (Par_Ent, In_Body => True);
9572 Parent_Installed := True;
9573 end if;
9575 -- If the instantiation is a library unit, and this is the main unit,
9576 -- then build the resulting compilation unit nodes for the instance.
9577 -- If this is a compilation unit but it is not the main unit, then it
9578 -- is the body of a unit in the context, that is being compiled
9579 -- because it is encloses some inlined unit or another generic unit
9580 -- being instantiated. In that case, this body is not part of the
9581 -- current compilation, and is not attached to the tree, but its
9582 -- parent must be set for analysis.
9584 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
9586 -- Replace instance node with body of instance, and create new
9587 -- node for corresponding instance declaration.
9589 Build_Instance_Compilation_Unit_Nodes
9590 (Inst_Node, Act_Body, Act_Decl);
9591 Analyze (Inst_Node);
9593 if Parent (Inst_Node) = Cunit (Main_Unit) then
9595 -- If the instance is a child unit itself, then set the scope
9596 -- of the expanded body to be the parent of the instantiation
9597 -- (ensuring that the fully qualified name will be generated
9598 -- for the elaboration subprogram).
9600 if Nkind (Defining_Unit_Name (Act_Spec)) =
9601 N_Defining_Program_Unit_Name
9602 then
9603 Set_Scope
9604 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
9605 end if;
9606 end if;
9608 -- Case where instantiation is not a library unit
9610 else
9611 -- If this is an early instantiation, i.e. appears textually
9612 -- before the corresponding body and must be elaborated first,
9613 -- indicate that the body instance is to be delayed.
9615 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
9617 -- Now analyze the body. We turn off all checks if this is an
9618 -- internal unit, since there is no reason to have checks on for
9619 -- any predefined run-time library code. All such code is designed
9620 -- to be compiled with checks off.
9622 -- Note that we do NOT apply this criterion to children of GNAT
9623 -- (or on VMS, children of DEC). The latter units must suppress
9624 -- checks explicitly if this is needed.
9626 if Is_Predefined_File_Name
9627 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
9628 then
9629 Analyze (Act_Body, Suppress => All_Checks);
9630 else
9631 Analyze (Act_Body);
9632 end if;
9633 end if;
9635 Inherit_Context (Gen_Body, Inst_Node);
9637 -- Remove the parent instances if they have been placed on the scope
9638 -- stack to compile the body.
9640 if Parent_Installed then
9641 Remove_Parent (In_Body => True);
9643 -- Restore the previous visibility of the parent
9645 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
9646 end if;
9648 Restore_Hidden_Primitives (Vis_Prims_List);
9649 Restore_Private_Views (Act_Decl_Id);
9651 -- Remove the current unit from visibility if this is an instance
9652 -- that is not elaborated on the fly for inlining purposes.
9654 if not Inlined_Body then
9655 Set_Is_Immediately_Visible (Act_Decl_Id, False);
9656 end if;
9658 Restore_Env;
9659 Style_Check := Save_Style_Check;
9661 -- If we have no body, and the unit requires a body, then complain. This
9662 -- complaint is suppressed if we have detected other errors (since a
9663 -- common reason for missing the body is that it had errors).
9664 -- In CodePeer mode, a warning has been emitted already, no need for
9665 -- further messages.
9667 elsif Unit_Requires_Body (Gen_Unit)
9668 and then not Body_Optional
9669 then
9670 if CodePeer_Mode then
9671 null;
9673 elsif Serious_Errors_Detected = 0 then
9674 Error_Msg_NE
9675 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
9677 -- Don't attempt to perform any cleanup actions if some other error
9678 -- was already detected, since this can cause blowups.
9680 else
9681 return;
9682 end if;
9684 -- Case of package that does not need a body
9686 else
9687 -- If the instantiation of the declaration is a library unit, rewrite
9688 -- the original package instantiation as a package declaration in the
9689 -- compilation unit node.
9691 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
9692 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
9693 Rewrite (Inst_Node, Act_Decl);
9695 -- Generate elaboration entity, in case spec has elaboration code.
9696 -- This cannot be done when the instance is analyzed, because it
9697 -- is not known yet whether the body exists.
9699 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
9700 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
9702 -- If the instantiation is not a library unit, then append the
9703 -- declaration to the list of implicitly generated entities, unless
9704 -- it is already a list member which means that it was already
9705 -- processed
9707 elsif not Is_List_Member (Act_Decl) then
9708 Mark_Rewrite_Insertion (Act_Decl);
9709 Insert_Before (Inst_Node, Act_Decl);
9710 end if;
9711 end if;
9713 Expander_Mode_Restore;
9714 end Instantiate_Package_Body;
9716 ---------------------------------
9717 -- Instantiate_Subprogram_Body --
9718 ---------------------------------
9720 procedure Instantiate_Subprogram_Body
9721 (Body_Info : Pending_Body_Info;
9722 Body_Optional : Boolean := False)
9724 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
9725 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
9726 Loc : constant Source_Ptr := Sloc (Inst_Node);
9727 Gen_Id : constant Node_Id := Name (Inst_Node);
9728 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
9729 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
9730 Anon_Id : constant Entity_Id :=
9731 Defining_Unit_Name (Specification (Act_Decl));
9732 Pack_Id : constant Entity_Id :=
9733 Defining_Unit_Name (Parent (Act_Decl));
9734 Decls : List_Id;
9735 Gen_Body : Node_Id;
9736 Gen_Body_Id : Node_Id;
9737 Act_Body : Node_Id;
9738 Pack_Body : Node_Id;
9739 Prev_Formal : Entity_Id;
9740 Ret_Expr : Node_Id;
9741 Unit_Renaming : Node_Id;
9743 Parent_Installed : Boolean := False;
9744 Save_Style_Check : constant Boolean := Style_Check;
9746 Par_Ent : Entity_Id := Empty;
9747 Par_Vis : Boolean := False;
9749 begin
9750 Gen_Body_Id := Corresponding_Body (Gen_Decl);
9752 -- Subprogram body may have been created already because of an inline
9753 -- pragma, or because of multiple elaborations of the enclosing package
9754 -- when several instances of the subprogram appear in the main unit.
9756 if Present (Corresponding_Body (Act_Decl)) then
9757 return;
9758 end if;
9760 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
9762 -- Re-establish the state of information on which checks are suppressed.
9763 -- This information was set in Body_Info at the point of instantiation,
9764 -- and now we restore it so that the instance is compiled using the
9765 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
9767 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
9768 Scope_Suppress := Body_Info.Scope_Suppress;
9769 Opt.Ada_Version := Body_Info.Version;
9771 if No (Gen_Body_Id) then
9773 -- For imported generic subprogram, no body to compile, complete
9774 -- the spec entity appropriately.
9776 if Is_Imported (Gen_Unit) then
9777 Set_Is_Imported (Anon_Id);
9778 Set_First_Rep_Item (Anon_Id, First_Rep_Item (Gen_Unit));
9779 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
9780 Set_Convention (Anon_Id, Convention (Gen_Unit));
9781 Set_Has_Completion (Anon_Id);
9782 return;
9784 -- For other cases, compile the body
9786 else
9787 Load_Parent_Of_Generic
9788 (Inst_Node, Specification (Gen_Decl), Body_Optional);
9789 Gen_Body_Id := Corresponding_Body (Gen_Decl);
9790 end if;
9791 end if;
9793 Instantiation_Node := Inst_Node;
9795 if Present (Gen_Body_Id) then
9796 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
9798 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
9800 -- Either body is not present, or context is non-expanding, as
9801 -- when compiling a subunit. Mark the instance as completed, and
9802 -- diagnose a missing body when needed.
9804 if Expander_Active
9805 and then Operating_Mode = Generate_Code
9806 then
9807 Error_Msg_N
9808 ("missing proper body for instantiation", Gen_Body);
9809 end if;
9811 Set_Has_Completion (Anon_Id);
9812 return;
9813 end if;
9815 Save_Env (Gen_Unit, Anon_Id);
9816 Style_Check := False;
9817 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
9818 Create_Instantiation_Source
9819 (Inst_Node,
9820 Gen_Body_Id,
9821 False,
9822 S_Adjustment);
9824 Act_Body :=
9825 Copy_Generic_Node
9826 (Original_Node (Gen_Body), Empty, Instantiating => True);
9828 -- Create proper defining name for the body, to correspond to
9829 -- the one in the spec.
9831 Set_Defining_Unit_Name (Specification (Act_Body),
9832 Make_Defining_Identifier
9833 (Sloc (Defining_Entity (Inst_Node)), Chars (Anon_Id)));
9834 Set_Corresponding_Spec (Act_Body, Anon_Id);
9835 Set_Has_Completion (Anon_Id);
9836 Check_Generic_Actuals (Pack_Id, False);
9838 -- Generate a reference to link the visible subprogram instance to
9839 -- the generic body, which for navigation purposes is the only
9840 -- available source for the instance.
9842 Generate_Reference
9843 (Related_Instance (Pack_Id),
9844 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
9846 -- If it is a child unit, make the parent instance (which is an
9847 -- instance of the parent of the generic) visible. The parent
9848 -- instance is the prefix of the name of the generic unit.
9850 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
9851 and then Nkind (Gen_Id) = N_Expanded_Name
9852 then
9853 Par_Ent := Entity (Prefix (Gen_Id));
9854 Par_Vis := Is_Immediately_Visible (Par_Ent);
9855 Install_Parent (Par_Ent, In_Body => True);
9856 Parent_Installed := True;
9858 elsif Is_Child_Unit (Gen_Unit) then
9859 Par_Ent := Scope (Gen_Unit);
9860 Par_Vis := Is_Immediately_Visible (Par_Ent);
9861 Install_Parent (Par_Ent, In_Body => True);
9862 Parent_Installed := True;
9863 end if;
9865 -- Inside its body, a reference to the generic unit is a reference
9866 -- to the instance. The corresponding renaming is the first
9867 -- declaration in the body.
9869 Unit_Renaming :=
9870 Make_Subprogram_Renaming_Declaration (Loc,
9871 Specification =>
9872 Copy_Generic_Node (
9873 Specification (Original_Node (Gen_Body)),
9874 Empty,
9875 Instantiating => True),
9876 Name => New_Occurrence_Of (Anon_Id, Loc));
9878 -- If there is a formal subprogram with the same name as the unit
9879 -- itself, do not add this renaming declaration. This is a temporary
9880 -- fix for one ACVC test. ???
9882 Prev_Formal := First_Entity (Pack_Id);
9883 while Present (Prev_Formal) loop
9884 if Chars (Prev_Formal) = Chars (Gen_Unit)
9885 and then Is_Overloadable (Prev_Formal)
9886 then
9887 exit;
9888 end if;
9890 Next_Entity (Prev_Formal);
9891 end loop;
9893 if Present (Prev_Formal) then
9894 Decls := New_List (Act_Body);
9895 else
9896 Decls := New_List (Unit_Renaming, Act_Body);
9897 end if;
9899 -- The subprogram body is placed in the body of a dummy package body,
9900 -- whose spec contains the subprogram declaration as well as the
9901 -- renaming declarations for the generic parameters.
9903 Pack_Body := Make_Package_Body (Loc,
9904 Defining_Unit_Name => New_Copy (Pack_Id),
9905 Declarations => Decls);
9907 Set_Corresponding_Spec (Pack_Body, Pack_Id);
9909 -- If the instantiation is a library unit, then build resulting
9910 -- compilation unit nodes for the instance. The declaration of
9911 -- the enclosing package is the grandparent of the subprogram
9912 -- declaration. First replace the instantiation node as the unit
9913 -- of the corresponding compilation.
9915 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
9916 if Parent (Inst_Node) = Cunit (Main_Unit) then
9917 Set_Unit (Parent (Inst_Node), Inst_Node);
9918 Build_Instance_Compilation_Unit_Nodes
9919 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
9920 Analyze (Inst_Node);
9921 else
9922 Set_Parent (Pack_Body, Parent (Inst_Node));
9923 Analyze (Pack_Body);
9924 end if;
9926 else
9927 Insert_Before (Inst_Node, Pack_Body);
9928 Mark_Rewrite_Insertion (Pack_Body);
9929 Analyze (Pack_Body);
9931 if Expander_Active then
9932 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
9933 end if;
9934 end if;
9936 Inherit_Context (Gen_Body, Inst_Node);
9938 Restore_Private_Views (Pack_Id, False);
9940 if Parent_Installed then
9941 Remove_Parent (In_Body => True);
9943 -- Restore the previous visibility of the parent
9945 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
9946 end if;
9948 Restore_Env;
9949 Style_Check := Save_Style_Check;
9951 -- Body not found. Error was emitted already. If there were no previous
9952 -- errors, this may be an instance whose scope is a premature instance.
9953 -- In that case we must insure that the (legal) program does raise
9954 -- program error if executed. We generate a subprogram body for this
9955 -- purpose. See DEC ac30vso.
9957 -- Should not reference proprietary DEC tests in comments ???
9959 elsif Serious_Errors_Detected = 0
9960 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
9961 then
9962 if Body_Optional then
9963 return;
9965 elsif Ekind (Anon_Id) = E_Procedure then
9966 Act_Body :=
9967 Make_Subprogram_Body (Loc,
9968 Specification =>
9969 Make_Procedure_Specification (Loc,
9970 Defining_Unit_Name =>
9971 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
9972 Parameter_Specifications =>
9973 New_Copy_List
9974 (Parameter_Specifications (Parent (Anon_Id)))),
9976 Declarations => Empty_List,
9977 Handled_Statement_Sequence =>
9978 Make_Handled_Sequence_Of_Statements (Loc,
9979 Statements =>
9980 New_List (
9981 Make_Raise_Program_Error (Loc,
9982 Reason =>
9983 PE_Access_Before_Elaboration))));
9985 else
9986 Ret_Expr :=
9987 Make_Raise_Program_Error (Loc,
9988 Reason => PE_Access_Before_Elaboration);
9990 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
9991 Set_Analyzed (Ret_Expr);
9993 Act_Body :=
9994 Make_Subprogram_Body (Loc,
9995 Specification =>
9996 Make_Function_Specification (Loc,
9997 Defining_Unit_Name =>
9998 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
9999 Parameter_Specifications =>
10000 New_Copy_List
10001 (Parameter_Specifications (Parent (Anon_Id))),
10002 Result_Definition =>
10003 New_Occurrence_Of (Etype (Anon_Id), Loc)),
10005 Declarations => Empty_List,
10006 Handled_Statement_Sequence =>
10007 Make_Handled_Sequence_Of_Statements (Loc,
10008 Statements =>
10009 New_List
10010 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
10011 end if;
10013 Pack_Body := Make_Package_Body (Loc,
10014 Defining_Unit_Name => New_Copy (Pack_Id),
10015 Declarations => New_List (Act_Body));
10017 Insert_After (Inst_Node, Pack_Body);
10018 Set_Corresponding_Spec (Pack_Body, Pack_Id);
10019 Analyze (Pack_Body);
10020 end if;
10022 Expander_Mode_Restore;
10023 end Instantiate_Subprogram_Body;
10025 ----------------------
10026 -- Instantiate_Type --
10027 ----------------------
10029 function Instantiate_Type
10030 (Formal : Node_Id;
10031 Actual : Node_Id;
10032 Analyzed_Formal : Node_Id;
10033 Actual_Decls : List_Id) return List_Id
10035 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
10036 A_Gen_T : constant Entity_Id :=
10037 Defining_Identifier (Analyzed_Formal);
10038 Ancestor : Entity_Id := Empty;
10039 Def : constant Node_Id := Formal_Type_Definition (Formal);
10040 Act_T : Entity_Id;
10041 Decl_Node : Node_Id;
10042 Decl_Nodes : List_Id;
10043 Loc : Source_Ptr;
10044 Subt : Entity_Id;
10046 procedure Validate_Array_Type_Instance;
10047 procedure Validate_Access_Subprogram_Instance;
10048 procedure Validate_Access_Type_Instance;
10049 procedure Validate_Derived_Type_Instance;
10050 procedure Validate_Derived_Interface_Type_Instance;
10051 procedure Validate_Discriminated_Formal_Type;
10052 procedure Validate_Interface_Type_Instance;
10053 procedure Validate_Private_Type_Instance;
10054 procedure Validate_Incomplete_Type_Instance;
10055 -- These procedures perform validation tests for the named case.
10056 -- Validate_Discriminated_Formal_Type is shared by formal private
10057 -- types and Ada 2012 formal incomplete types.
10059 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
10060 -- Check that base types are the same and that the subtypes match
10061 -- statically. Used in several of the above.
10063 --------------------
10064 -- Subtypes_Match --
10065 --------------------
10067 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
10068 T : constant Entity_Id := Get_Instance_Of (Gen_T);
10070 begin
10071 return (Base_Type (T) = Base_Type (Act_T)
10072 and then Subtypes_Statically_Match (T, Act_T))
10074 or else (Is_Class_Wide_Type (Gen_T)
10075 and then Is_Class_Wide_Type (Act_T)
10076 and then
10077 Subtypes_Match
10078 (Get_Instance_Of (Root_Type (Gen_T)),
10079 Root_Type (Act_T)))
10081 or else
10082 ((Ekind (Gen_T) = E_Anonymous_Access_Subprogram_Type
10083 or else Ekind (Gen_T) = E_Anonymous_Access_Type)
10084 and then Ekind (Act_T) = Ekind (Gen_T)
10085 and then
10086 Subtypes_Statically_Match
10087 (Designated_Type (Gen_T), Designated_Type (Act_T)));
10088 end Subtypes_Match;
10090 -----------------------------------------
10091 -- Validate_Access_Subprogram_Instance --
10092 -----------------------------------------
10094 procedure Validate_Access_Subprogram_Instance is
10095 begin
10096 if not Is_Access_Type (Act_T)
10097 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
10098 then
10099 Error_Msg_NE
10100 ("expect access type in instantiation of &", Actual, Gen_T);
10101 Abandon_Instantiation (Actual);
10102 end if;
10104 Check_Mode_Conformant
10105 (Designated_Type (Act_T),
10106 Designated_Type (A_Gen_T),
10107 Actual,
10108 Get_Inst => True);
10110 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
10111 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
10112 Error_Msg_NE
10113 ("protected access type not allowed for formal &",
10114 Actual, Gen_T);
10115 end if;
10117 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
10118 Error_Msg_NE
10119 ("expect protected access type for formal &",
10120 Actual, Gen_T);
10121 end if;
10122 end Validate_Access_Subprogram_Instance;
10124 -----------------------------------
10125 -- Validate_Access_Type_Instance --
10126 -----------------------------------
10128 procedure Validate_Access_Type_Instance is
10129 Desig_Type : constant Entity_Id :=
10130 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
10131 Desig_Act : Entity_Id;
10133 begin
10134 if not Is_Access_Type (Act_T) then
10135 Error_Msg_NE
10136 ("expect access type in instantiation of &", Actual, Gen_T);
10137 Abandon_Instantiation (Actual);
10138 end if;
10140 if Is_Access_Constant (A_Gen_T) then
10141 if not Is_Access_Constant (Act_T) then
10142 Error_Msg_N
10143 ("actual type must be access-to-constant type", Actual);
10144 Abandon_Instantiation (Actual);
10145 end if;
10146 else
10147 if Is_Access_Constant (Act_T) then
10148 Error_Msg_N
10149 ("actual type must be access-to-variable type", Actual);
10150 Abandon_Instantiation (Actual);
10152 elsif Ekind (A_Gen_T) = E_General_Access_Type
10153 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
10154 then
10155 Error_Msg_N -- CODEFIX
10156 ("actual must be general access type!", Actual);
10157 Error_Msg_NE -- CODEFIX
10158 ("add ALL to }!", Actual, Act_T);
10159 Abandon_Instantiation (Actual);
10160 end if;
10161 end if;
10163 -- The designated subtypes, that is to say the subtypes introduced
10164 -- by an access type declaration (and not by a subtype declaration)
10165 -- must match.
10167 Desig_Act := Designated_Type (Base_Type (Act_T));
10169 -- The designated type may have been introduced through a limited_
10170 -- with clause, in which case retrieve the non-limited view. This
10171 -- applies to incomplete types as well as to class-wide types.
10173 if From_With_Type (Desig_Act) then
10174 Desig_Act := Available_View (Desig_Act);
10175 end if;
10177 if not Subtypes_Match
10178 (Desig_Type, Desig_Act) then
10179 Error_Msg_NE
10180 ("designated type of actual does not match that of formal &",
10181 Actual, Gen_T);
10182 Abandon_Instantiation (Actual);
10184 elsif Is_Access_Type (Designated_Type (Act_T))
10185 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
10187 Is_Constrained (Designated_Type (Desig_Type))
10188 then
10189 Error_Msg_NE
10190 ("designated type of actual does not match that of formal &",
10191 Actual, Gen_T);
10192 Abandon_Instantiation (Actual);
10193 end if;
10195 -- Ada 2005: null-exclusion indicators of the two types must agree
10197 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
10198 Error_Msg_NE
10199 ("non null exclusion of actual and formal & do not match",
10200 Actual, Gen_T);
10201 end if;
10202 end Validate_Access_Type_Instance;
10204 ----------------------------------
10205 -- Validate_Array_Type_Instance --
10206 ----------------------------------
10208 procedure Validate_Array_Type_Instance is
10209 I1 : Node_Id;
10210 I2 : Node_Id;
10211 T2 : Entity_Id;
10213 function Formal_Dimensions return Int;
10214 -- Count number of dimensions in array type formal
10216 -----------------------
10217 -- Formal_Dimensions --
10218 -----------------------
10220 function Formal_Dimensions return Int is
10221 Num : Int := 0;
10222 Index : Node_Id;
10224 begin
10225 if Nkind (Def) = N_Constrained_Array_Definition then
10226 Index := First (Discrete_Subtype_Definitions (Def));
10227 else
10228 Index := First (Subtype_Marks (Def));
10229 end if;
10231 while Present (Index) loop
10232 Num := Num + 1;
10233 Next_Index (Index);
10234 end loop;
10236 return Num;
10237 end Formal_Dimensions;
10239 -- Start of processing for Validate_Array_Type_Instance
10241 begin
10242 if not Is_Array_Type (Act_T) then
10243 Error_Msg_NE
10244 ("expect array type in instantiation of &", Actual, Gen_T);
10245 Abandon_Instantiation (Actual);
10247 elsif Nkind (Def) = N_Constrained_Array_Definition then
10248 if not (Is_Constrained (Act_T)) then
10249 Error_Msg_NE
10250 ("expect constrained array in instantiation of &",
10251 Actual, Gen_T);
10252 Abandon_Instantiation (Actual);
10253 end if;
10255 else
10256 if Is_Constrained (Act_T) then
10257 Error_Msg_NE
10258 ("expect unconstrained array in instantiation of &",
10259 Actual, Gen_T);
10260 Abandon_Instantiation (Actual);
10261 end if;
10262 end if;
10264 if Formal_Dimensions /= Number_Dimensions (Act_T) then
10265 Error_Msg_NE
10266 ("dimensions of actual do not match formal &", Actual, Gen_T);
10267 Abandon_Instantiation (Actual);
10268 end if;
10270 I1 := First_Index (A_Gen_T);
10271 I2 := First_Index (Act_T);
10272 for J in 1 .. Formal_Dimensions loop
10274 -- If the indexes of the actual were given by a subtype_mark,
10275 -- the index was transformed into a range attribute. Retrieve
10276 -- the original type mark for checking.
10278 if Is_Entity_Name (Original_Node (I2)) then
10279 T2 := Entity (Original_Node (I2));
10280 else
10281 T2 := Etype (I2);
10282 end if;
10284 if not Subtypes_Match
10285 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
10286 then
10287 Error_Msg_NE
10288 ("index types of actual do not match those of formal &",
10289 Actual, Gen_T);
10290 Abandon_Instantiation (Actual);
10291 end if;
10293 Next_Index (I1);
10294 Next_Index (I2);
10295 end loop;
10297 -- Check matching subtypes. Note that there are complex visibility
10298 -- issues when the generic is a child unit and some aspect of the
10299 -- generic type is declared in a parent unit of the generic. We do
10300 -- the test to handle this special case only after a direct check
10301 -- for static matching has failed.
10303 if Subtypes_Match
10304 (Component_Type (A_Gen_T), Component_Type (Act_T))
10305 or else Subtypes_Match
10306 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
10307 Component_Type (Act_T))
10308 then
10309 null;
10310 else
10311 Error_Msg_NE
10312 ("component subtype of actual does not match that of formal &",
10313 Actual, Gen_T);
10314 Abandon_Instantiation (Actual);
10315 end if;
10317 if Has_Aliased_Components (A_Gen_T)
10318 and then not Has_Aliased_Components (Act_T)
10319 then
10320 Error_Msg_NE
10321 ("actual must have aliased components to match formal type &",
10322 Actual, Gen_T);
10323 end if;
10324 end Validate_Array_Type_Instance;
10326 -----------------------------------------------
10327 -- Validate_Derived_Interface_Type_Instance --
10328 -----------------------------------------------
10330 procedure Validate_Derived_Interface_Type_Instance is
10331 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
10332 Elmt : Elmt_Id;
10334 begin
10335 -- First apply interface instance checks
10337 Validate_Interface_Type_Instance;
10339 -- Verify that immediate parent interface is an ancestor of
10340 -- the actual.
10342 if Present (Par)
10343 and then not Interface_Present_In_Ancestor (Act_T, Par)
10344 then
10345 Error_Msg_NE
10346 ("interface actual must include progenitor&", Actual, Par);
10347 end if;
10349 -- Now verify that the actual includes all other ancestors of
10350 -- the formal.
10352 Elmt := First_Elmt (Interfaces (A_Gen_T));
10353 while Present (Elmt) loop
10354 if not Interface_Present_In_Ancestor
10355 (Act_T, Get_Instance_Of (Node (Elmt)))
10356 then
10357 Error_Msg_NE
10358 ("interface actual must include progenitor&",
10359 Actual, Node (Elmt));
10360 end if;
10362 Next_Elmt (Elmt);
10363 end loop;
10364 end Validate_Derived_Interface_Type_Instance;
10366 ------------------------------------
10367 -- Validate_Derived_Type_Instance --
10368 ------------------------------------
10370 procedure Validate_Derived_Type_Instance is
10371 Actual_Discr : Entity_Id;
10372 Ancestor_Discr : Entity_Id;
10374 begin
10375 -- If the parent type in the generic declaration is itself a previous
10376 -- formal type, then it is local to the generic and absent from the
10377 -- analyzed generic definition. In that case the ancestor is the
10378 -- instance of the formal (which must have been instantiated
10379 -- previously), unless the ancestor is itself a formal derived type.
10380 -- In this latter case (which is the subject of Corrigendum 8652/0038
10381 -- (AI-202) the ancestor of the formals is the ancestor of its
10382 -- parent. Otherwise, the analyzed generic carries the parent type.
10383 -- If the parent type is defined in a previous formal package, then
10384 -- the scope of that formal package is that of the generic type
10385 -- itself, and it has already been mapped into the corresponding type
10386 -- in the actual package.
10388 -- Common case: parent type defined outside of the generic
10390 if Is_Entity_Name (Subtype_Mark (Def))
10391 and then Present (Entity (Subtype_Mark (Def)))
10392 then
10393 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
10395 -- Check whether parent is defined in a previous formal package
10397 elsif
10398 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
10399 then
10400 Ancestor :=
10401 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
10403 -- The type may be a local derivation, or a type extension of a
10404 -- previous formal, or of a formal of a parent package.
10406 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
10407 or else
10408 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
10409 then
10410 -- Check whether the parent is another derived formal type in the
10411 -- same generic unit.
10413 if Etype (A_Gen_T) /= A_Gen_T
10414 and then Is_Generic_Type (Etype (A_Gen_T))
10415 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
10416 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
10417 then
10418 -- Locate ancestor of parent from the subtype declaration
10419 -- created for the actual.
10421 declare
10422 Decl : Node_Id;
10424 begin
10425 Decl := First (Actual_Decls);
10426 while Present (Decl) loop
10427 if Nkind (Decl) = N_Subtype_Declaration
10428 and then Chars (Defining_Identifier (Decl)) =
10429 Chars (Etype (A_Gen_T))
10430 then
10431 Ancestor := Generic_Parent_Type (Decl);
10432 exit;
10433 else
10434 Next (Decl);
10435 end if;
10436 end loop;
10437 end;
10439 pragma Assert (Present (Ancestor));
10441 else
10442 Ancestor :=
10443 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
10444 end if;
10446 else
10447 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
10448 end if;
10450 -- If the formal derived type has pragma Preelaborable_Initialization
10451 -- then the actual type must have preelaborable initialization.
10453 if Known_To_Have_Preelab_Init (A_Gen_T)
10454 and then not Has_Preelaborable_Initialization (Act_T)
10455 then
10456 Error_Msg_NE
10457 ("actual for & must have preelaborable initialization",
10458 Actual, Gen_T);
10459 end if;
10461 -- Ada 2005 (AI-251)
10463 if Ada_Version >= Ada_2005
10464 and then Is_Interface (Ancestor)
10465 then
10466 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
10467 Error_Msg_NE
10468 ("(Ada 2005) expected type implementing & in instantiation",
10469 Actual, Ancestor);
10470 end if;
10472 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
10473 Error_Msg_NE
10474 ("expect type derived from & in instantiation",
10475 Actual, First_Subtype (Ancestor));
10476 Abandon_Instantiation (Actual);
10477 end if;
10479 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
10480 -- that the formal type declaration has been rewritten as a private
10481 -- extension.
10483 if Ada_Version >= Ada_2005
10484 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
10485 and then Synchronized_Present (Parent (A_Gen_T))
10486 then
10487 -- The actual must be a synchronized tagged type
10489 if not Is_Tagged_Type (Act_T) then
10490 Error_Msg_N
10491 ("actual of synchronized type must be tagged", Actual);
10492 Abandon_Instantiation (Actual);
10494 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
10495 and then Nkind (Type_Definition (Parent (Act_T))) =
10496 N_Derived_Type_Definition
10497 and then not Synchronized_Present (Type_Definition
10498 (Parent (Act_T)))
10499 then
10500 Error_Msg_N
10501 ("actual of synchronized type must be synchronized", Actual);
10502 Abandon_Instantiation (Actual);
10503 end if;
10504 end if;
10506 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
10507 -- removes the second instance of the phrase "or allow pass by copy".
10509 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
10510 Error_Msg_N
10511 ("cannot have atomic actual type for non-atomic formal type",
10512 Actual);
10514 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
10515 Error_Msg_N
10516 ("cannot have volatile actual type for non-volatile formal type",
10517 Actual);
10518 end if;
10520 -- It should not be necessary to check for unknown discriminants on
10521 -- Formal, but for some reason Has_Unknown_Discriminants is false for
10522 -- A_Gen_T, so Is_Indefinite_Subtype incorrectly returns False. This
10523 -- needs fixing. ???
10525 if not Is_Indefinite_Subtype (A_Gen_T)
10526 and then not Unknown_Discriminants_Present (Formal)
10527 and then Is_Indefinite_Subtype (Act_T)
10528 then
10529 Error_Msg_N
10530 ("actual subtype must be constrained", Actual);
10531 Abandon_Instantiation (Actual);
10532 end if;
10534 if not Unknown_Discriminants_Present (Formal) then
10535 if Is_Constrained (Ancestor) then
10536 if not Is_Constrained (Act_T) then
10537 Error_Msg_N
10538 ("actual subtype must be constrained", Actual);
10539 Abandon_Instantiation (Actual);
10540 end if;
10542 -- Ancestor is unconstrained, Check if generic formal and actual
10543 -- agree on constrainedness. The check only applies to array types
10544 -- and discriminated types.
10546 elsif Is_Constrained (Act_T) then
10547 if Ekind (Ancestor) = E_Access_Type
10548 or else
10549 (not Is_Constrained (A_Gen_T)
10550 and then Is_Composite_Type (A_Gen_T))
10551 then
10552 Error_Msg_N
10553 ("actual subtype must be unconstrained", Actual);
10554 Abandon_Instantiation (Actual);
10555 end if;
10557 -- A class-wide type is only allowed if the formal has unknown
10558 -- discriminants.
10560 elsif Is_Class_Wide_Type (Act_T)
10561 and then not Has_Unknown_Discriminants (Ancestor)
10562 then
10563 Error_Msg_NE
10564 ("actual for & cannot be a class-wide type", Actual, Gen_T);
10565 Abandon_Instantiation (Actual);
10567 -- Otherwise, the formal and actual shall have the same number
10568 -- of discriminants and each discriminant of the actual must
10569 -- correspond to a discriminant of the formal.
10571 elsif Has_Discriminants (Act_T)
10572 and then not Has_Unknown_Discriminants (Act_T)
10573 and then Has_Discriminants (Ancestor)
10574 then
10575 Actual_Discr := First_Discriminant (Act_T);
10576 Ancestor_Discr := First_Discriminant (Ancestor);
10577 while Present (Actual_Discr)
10578 and then Present (Ancestor_Discr)
10579 loop
10580 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
10581 No (Corresponding_Discriminant (Actual_Discr))
10582 then
10583 Error_Msg_NE
10584 ("discriminant & does not correspond " &
10585 "to ancestor discriminant", Actual, Actual_Discr);
10586 Abandon_Instantiation (Actual);
10587 end if;
10589 Next_Discriminant (Actual_Discr);
10590 Next_Discriminant (Ancestor_Discr);
10591 end loop;
10593 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
10594 Error_Msg_NE
10595 ("actual for & must have same number of discriminants",
10596 Actual, Gen_T);
10597 Abandon_Instantiation (Actual);
10598 end if;
10600 -- This case should be caught by the earlier check for
10601 -- constrainedness, but the check here is added for completeness.
10603 elsif Has_Discriminants (Act_T)
10604 and then not Has_Unknown_Discriminants (Act_T)
10605 then
10606 Error_Msg_NE
10607 ("actual for & must not have discriminants", Actual, Gen_T);
10608 Abandon_Instantiation (Actual);
10610 elsif Has_Discriminants (Ancestor) then
10611 Error_Msg_NE
10612 ("actual for & must have known discriminants", Actual, Gen_T);
10613 Abandon_Instantiation (Actual);
10614 end if;
10616 if not Subtypes_Statically_Compatible (Act_T, Ancestor) then
10617 Error_Msg_N
10618 ("constraint on actual is incompatible with formal", Actual);
10619 Abandon_Instantiation (Actual);
10620 end if;
10621 end if;
10623 -- If the formal and actual types are abstract, check that there
10624 -- are no abstract primitives of the actual type that correspond to
10625 -- nonabstract primitives of the formal type (second sentence of
10626 -- RM95-3.9.3(9)).
10628 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
10629 Check_Abstract_Primitives : declare
10630 Gen_Prims : constant Elist_Id :=
10631 Primitive_Operations (A_Gen_T);
10632 Gen_Elmt : Elmt_Id;
10633 Gen_Subp : Entity_Id;
10634 Anc_Subp : Entity_Id;
10635 Anc_Formal : Entity_Id;
10636 Anc_F_Type : Entity_Id;
10638 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
10639 Act_Elmt : Elmt_Id;
10640 Act_Subp : Entity_Id;
10641 Act_Formal : Entity_Id;
10642 Act_F_Type : Entity_Id;
10644 Subprograms_Correspond : Boolean;
10646 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
10647 -- Returns true if T2 is derived directly or indirectly from
10648 -- T1, including derivations from interfaces. T1 and T2 are
10649 -- required to be specific tagged base types.
10651 ------------------------
10652 -- Is_Tagged_Ancestor --
10653 ------------------------
10655 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
10657 Intfc_Elmt : Elmt_Id;
10659 begin
10660 -- The predicate is satisfied if the types are the same
10662 if T1 = T2 then
10663 return True;
10665 -- If we've reached the top of the derivation chain then
10666 -- we know that T1 is not an ancestor of T2.
10668 elsif Etype (T2) = T2 then
10669 return False;
10671 -- Proceed to check T2's immediate parent
10673 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
10674 return True;
10676 -- Finally, check to see if T1 is an ancestor of any of T2's
10677 -- progenitors.
10679 else
10680 Intfc_Elmt := First_Elmt (Interfaces (T2));
10681 while Present (Intfc_Elmt) loop
10682 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
10683 return True;
10684 end if;
10686 Next_Elmt (Intfc_Elmt);
10687 end loop;
10688 end if;
10690 return False;
10691 end Is_Tagged_Ancestor;
10693 -- Start of processing for Check_Abstract_Primitives
10695 begin
10696 -- Loop over all of the formal derived type's primitives
10698 Gen_Elmt := First_Elmt (Gen_Prims);
10699 while Present (Gen_Elmt) loop
10700 Gen_Subp := Node (Gen_Elmt);
10702 -- If the primitive of the formal is not abstract, then
10703 -- determine whether there is a corresponding primitive of
10704 -- the actual type that's abstract.
10706 if not Is_Abstract_Subprogram (Gen_Subp) then
10707 Act_Elmt := First_Elmt (Act_Prims);
10708 while Present (Act_Elmt) loop
10709 Act_Subp := Node (Act_Elmt);
10711 -- If we find an abstract primitive of the actual,
10712 -- then we need to test whether it corresponds to the
10713 -- subprogram from which the generic formal primitive
10714 -- is inherited.
10716 if Is_Abstract_Subprogram (Act_Subp) then
10717 Anc_Subp := Alias (Gen_Subp);
10719 -- Test whether we have a corresponding primitive
10720 -- by comparing names, kinds, formal types, and
10721 -- result types.
10723 if Chars (Anc_Subp) = Chars (Act_Subp)
10724 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
10725 then
10726 Anc_Formal := First_Formal (Anc_Subp);
10727 Act_Formal := First_Formal (Act_Subp);
10728 while Present (Anc_Formal)
10729 and then Present (Act_Formal)
10730 loop
10731 Anc_F_Type := Etype (Anc_Formal);
10732 Act_F_Type := Etype (Act_Formal);
10734 if Ekind (Anc_F_Type)
10735 = E_Anonymous_Access_Type
10736 then
10737 Anc_F_Type := Designated_Type (Anc_F_Type);
10739 if Ekind (Act_F_Type)
10740 = E_Anonymous_Access_Type
10741 then
10742 Act_F_Type :=
10743 Designated_Type (Act_F_Type);
10744 else
10745 exit;
10746 end if;
10748 elsif
10749 Ekind (Act_F_Type) = E_Anonymous_Access_Type
10750 then
10751 exit;
10752 end if;
10754 Anc_F_Type := Base_Type (Anc_F_Type);
10755 Act_F_Type := Base_Type (Act_F_Type);
10757 -- If the formal is controlling, then the
10758 -- the type of the actual primitive's formal
10759 -- must be derived directly or indirectly
10760 -- from the type of the ancestor primitive's
10761 -- formal.
10763 if Is_Controlling_Formal (Anc_Formal) then
10764 if not Is_Tagged_Ancestor
10765 (Anc_F_Type, Act_F_Type)
10766 then
10767 exit;
10768 end if;
10770 -- Otherwise the types of the formals must
10771 -- be the same.
10773 elsif Anc_F_Type /= Act_F_Type then
10774 exit;
10775 end if;
10777 Next_Entity (Anc_Formal);
10778 Next_Entity (Act_Formal);
10779 end loop;
10781 -- If we traversed through all of the formals
10782 -- then so far the subprograms correspond, so
10783 -- now check that any result types correspond.
10785 if No (Anc_Formal) and then No (Act_Formal) then
10786 Subprograms_Correspond := True;
10788 if Ekind (Act_Subp) = E_Function then
10789 Anc_F_Type := Etype (Anc_Subp);
10790 Act_F_Type := Etype (Act_Subp);
10792 if Ekind (Anc_F_Type)
10793 = E_Anonymous_Access_Type
10794 then
10795 Anc_F_Type :=
10796 Designated_Type (Anc_F_Type);
10798 if Ekind (Act_F_Type)
10799 = E_Anonymous_Access_Type
10800 then
10801 Act_F_Type :=
10802 Designated_Type (Act_F_Type);
10803 else
10804 Subprograms_Correspond := False;
10805 end if;
10807 elsif
10808 Ekind (Act_F_Type)
10809 = E_Anonymous_Access_Type
10810 then
10811 Subprograms_Correspond := False;
10812 end if;
10814 Anc_F_Type := Base_Type (Anc_F_Type);
10815 Act_F_Type := Base_Type (Act_F_Type);
10817 -- Now either the result types must be
10818 -- the same or, if the result type is
10819 -- controlling, the result type of the
10820 -- actual primitive must descend from the
10821 -- result type of the ancestor primitive.
10823 if Subprograms_Correspond
10824 and then Anc_F_Type /= Act_F_Type
10825 and then
10826 Has_Controlling_Result (Anc_Subp)
10827 and then
10828 not Is_Tagged_Ancestor
10829 (Anc_F_Type, Act_F_Type)
10830 then
10831 Subprograms_Correspond := False;
10832 end if;
10833 end if;
10835 -- Found a matching subprogram belonging to
10836 -- formal ancestor type, so actual subprogram
10837 -- corresponds and this violates 3.9.3(9).
10839 if Subprograms_Correspond then
10840 Error_Msg_NE
10841 ("abstract subprogram & overrides " &
10842 "nonabstract subprogram of ancestor",
10843 Actual,
10844 Act_Subp);
10845 end if;
10846 end if;
10847 end if;
10848 end if;
10850 Next_Elmt (Act_Elmt);
10851 end loop;
10852 end if;
10854 Next_Elmt (Gen_Elmt);
10855 end loop;
10856 end Check_Abstract_Primitives;
10857 end if;
10859 -- Verify that limitedness matches. If parent is a limited
10860 -- interface then the generic formal is not unless declared
10861 -- explicitly so. If not declared limited, the actual cannot be
10862 -- limited (see AI05-0087).
10864 -- Even though this AI is a binding interpretation, we enable the
10865 -- check only in Ada 2012 mode, because this improper construct
10866 -- shows up in user code and in existing B-tests.
10868 if Is_Limited_Type (Act_T)
10869 and then not Is_Limited_Type (A_Gen_T)
10870 and then Ada_Version >= Ada_2012
10871 then
10872 if In_Instance then
10873 null;
10874 else
10875 Error_Msg_NE
10876 ("actual for non-limited & cannot be a limited type", Actual,
10877 Gen_T);
10878 Explain_Limited_Type (Act_T, Actual);
10879 Abandon_Instantiation (Actual);
10880 end if;
10881 end if;
10882 end Validate_Derived_Type_Instance;
10884 ----------------------------------------
10885 -- Validate_Discriminated_Formal_Type --
10886 ----------------------------------------
10888 procedure Validate_Discriminated_Formal_Type is
10889 Formal_Discr : Entity_Id;
10890 Actual_Discr : Entity_Id;
10891 Formal_Subt : Entity_Id;
10893 begin
10894 if Has_Discriminants (A_Gen_T) then
10895 if not Has_Discriminants (Act_T) then
10896 Error_Msg_NE
10897 ("actual for & must have discriminants", Actual, Gen_T);
10898 Abandon_Instantiation (Actual);
10900 elsif Is_Constrained (Act_T) then
10901 Error_Msg_NE
10902 ("actual for & must be unconstrained", Actual, Gen_T);
10903 Abandon_Instantiation (Actual);
10905 else
10906 Formal_Discr := First_Discriminant (A_Gen_T);
10907 Actual_Discr := First_Discriminant (Act_T);
10908 while Formal_Discr /= Empty loop
10909 if Actual_Discr = Empty then
10910 Error_Msg_NE
10911 ("discriminants on actual do not match formal",
10912 Actual, Gen_T);
10913 Abandon_Instantiation (Actual);
10914 end if;
10916 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
10918 -- Access discriminants match if designated types do
10920 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
10921 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
10922 E_Anonymous_Access_Type
10923 and then
10924 Get_Instance_Of
10925 (Designated_Type (Base_Type (Formal_Subt))) =
10926 Designated_Type (Base_Type (Etype (Actual_Discr)))
10927 then
10928 null;
10930 elsif Base_Type (Formal_Subt) /=
10931 Base_Type (Etype (Actual_Discr))
10932 then
10933 Error_Msg_NE
10934 ("types of actual discriminants must match formal",
10935 Actual, Gen_T);
10936 Abandon_Instantiation (Actual);
10938 elsif not Subtypes_Statically_Match
10939 (Formal_Subt, Etype (Actual_Discr))
10940 and then Ada_Version >= Ada_95
10941 then
10942 Error_Msg_NE
10943 ("subtypes of actual discriminants must match formal",
10944 Actual, Gen_T);
10945 Abandon_Instantiation (Actual);
10946 end if;
10948 Next_Discriminant (Formal_Discr);
10949 Next_Discriminant (Actual_Discr);
10950 end loop;
10952 if Actual_Discr /= Empty then
10953 Error_Msg_NE
10954 ("discriminants on actual do not match formal",
10955 Actual, Gen_T);
10956 Abandon_Instantiation (Actual);
10957 end if;
10958 end if;
10959 end if;
10960 end Validate_Discriminated_Formal_Type;
10962 ---------------------------------------
10963 -- Validate_Incomplete_Type_Instance --
10964 ---------------------------------------
10966 procedure Validate_Incomplete_Type_Instance is
10967 begin
10968 if not Is_Tagged_Type (Act_T)
10969 and then Is_Tagged_Type (A_Gen_T)
10970 then
10971 Error_Msg_NE
10972 ("actual for & must be a tagged type", Actual, Gen_T);
10973 end if;
10975 Validate_Discriminated_Formal_Type;
10976 end Validate_Incomplete_Type_Instance;
10978 --------------------------------------
10979 -- Validate_Interface_Type_Instance --
10980 --------------------------------------
10982 procedure Validate_Interface_Type_Instance is
10983 begin
10984 if not Is_Interface (Act_T) then
10985 Error_Msg_NE
10986 ("actual for formal interface type must be an interface",
10987 Actual, Gen_T);
10989 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
10990 or else
10991 Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
10992 or else
10993 Is_Protected_Interface (A_Gen_T) /=
10994 Is_Protected_Interface (Act_T)
10995 or else
10996 Is_Synchronized_Interface (A_Gen_T) /=
10997 Is_Synchronized_Interface (Act_T)
10998 then
10999 Error_Msg_NE
11000 ("actual for interface& does not match (RM 12.5.5(4))",
11001 Actual, Gen_T);
11002 end if;
11003 end Validate_Interface_Type_Instance;
11005 ------------------------------------
11006 -- Validate_Private_Type_Instance --
11007 ------------------------------------
11009 procedure Validate_Private_Type_Instance is
11010 begin
11011 if Is_Limited_Type (Act_T)
11012 and then not Is_Limited_Type (A_Gen_T)
11013 then
11014 if In_Instance then
11015 null;
11016 else
11017 Error_Msg_NE
11018 ("actual for non-limited & cannot be a limited type", Actual,
11019 Gen_T);
11020 Explain_Limited_Type (Act_T, Actual);
11021 Abandon_Instantiation (Actual);
11022 end if;
11024 elsif Known_To_Have_Preelab_Init (A_Gen_T)
11025 and then not Has_Preelaborable_Initialization (Act_T)
11026 then
11027 Error_Msg_NE
11028 ("actual for & must have preelaborable initialization", Actual,
11029 Gen_T);
11031 elsif Is_Indefinite_Subtype (Act_T)
11032 and then not Is_Indefinite_Subtype (A_Gen_T)
11033 and then Ada_Version >= Ada_95
11034 then
11035 Error_Msg_NE
11036 ("actual for & must be a definite subtype", Actual, Gen_T);
11038 elsif not Is_Tagged_Type (Act_T)
11039 and then Is_Tagged_Type (A_Gen_T)
11040 then
11041 Error_Msg_NE
11042 ("actual for & must be a tagged type", Actual, Gen_T);
11043 end if;
11045 Validate_Discriminated_Formal_Type;
11046 Ancestor := Gen_T;
11047 end Validate_Private_Type_Instance;
11049 -- Start of processing for Instantiate_Type
11051 begin
11052 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
11053 Error_Msg_N ("duplicate instantiation of generic type", Actual);
11054 return New_List (Error);
11056 elsif not Is_Entity_Name (Actual)
11057 or else not Is_Type (Entity (Actual))
11058 then
11059 Error_Msg_NE
11060 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
11061 Abandon_Instantiation (Actual);
11063 else
11064 Act_T := Entity (Actual);
11066 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
11067 -- as a generic actual parameter if the corresponding formal type
11068 -- does not have a known_discriminant_part, or is a formal derived
11069 -- type that is an Unchecked_Union type.
11071 if Is_Unchecked_Union (Base_Type (Act_T)) then
11072 if not Has_Discriminants (A_Gen_T)
11073 or else
11074 (Is_Derived_Type (A_Gen_T)
11075 and then
11076 Is_Unchecked_Union (A_Gen_T))
11077 then
11078 null;
11079 else
11080 Error_Msg_N ("Unchecked_Union cannot be the actual for a" &
11081 " discriminated formal type", Act_T);
11083 end if;
11084 end if;
11086 -- Deal with fixed/floating restrictions
11088 if Is_Floating_Point_Type (Act_T) then
11089 Check_Restriction (No_Floating_Point, Actual);
11090 elsif Is_Fixed_Point_Type (Act_T) then
11091 Check_Restriction (No_Fixed_Point, Actual);
11092 end if;
11094 -- Deal with error of using incomplete type as generic actual.
11095 -- This includes limited views of a type, even if the non-limited
11096 -- view may be available.
11098 if Ekind (Act_T) = E_Incomplete_Type
11099 or else (Is_Class_Wide_Type (Act_T)
11100 and then
11101 Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
11102 then
11103 -- If the formal is an incomplete type, the actual can be
11104 -- incomplete as well.
11106 if Ekind (A_Gen_T) = E_Incomplete_Type then
11107 null;
11109 elsif Is_Class_Wide_Type (Act_T)
11110 or else No (Full_View (Act_T))
11111 then
11112 Error_Msg_N ("premature use of incomplete type", Actual);
11113 Abandon_Instantiation (Actual);
11114 else
11115 Act_T := Full_View (Act_T);
11116 Set_Entity (Actual, Act_T);
11118 if Has_Private_Component (Act_T) then
11119 Error_Msg_N
11120 ("premature use of type with private component", Actual);
11121 end if;
11122 end if;
11124 -- Deal with error of premature use of private type as generic actual
11126 elsif Is_Private_Type (Act_T)
11127 and then Is_Private_Type (Base_Type (Act_T))
11128 and then not Is_Generic_Type (Act_T)
11129 and then not Is_Derived_Type (Act_T)
11130 and then No (Full_View (Root_Type (Act_T)))
11131 then
11132 -- If the formal is an incomplete type, the actual can be
11133 -- private or incomplete as well.
11135 if Ekind (A_Gen_T) = E_Incomplete_Type then
11136 null;
11137 else
11138 Error_Msg_N ("premature use of private type", Actual);
11139 end if;
11141 elsif Has_Private_Component (Act_T) then
11142 Error_Msg_N
11143 ("premature use of type with private component", Actual);
11144 end if;
11146 Set_Instance_Of (A_Gen_T, Act_T);
11148 -- If the type is generic, the class-wide type may also be used
11150 if Is_Tagged_Type (A_Gen_T)
11151 and then Is_Tagged_Type (Act_T)
11152 and then not Is_Class_Wide_Type (A_Gen_T)
11153 then
11154 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
11155 Class_Wide_Type (Act_T));
11156 end if;
11158 if not Is_Abstract_Type (A_Gen_T)
11159 and then Is_Abstract_Type (Act_T)
11160 then
11161 Error_Msg_N
11162 ("actual of non-abstract formal cannot be abstract", Actual);
11163 end if;
11165 -- A generic scalar type is a first subtype for which we generate
11166 -- an anonymous base type. Indicate that the instance of this base
11167 -- is the base type of the actual.
11169 if Is_Scalar_Type (A_Gen_T) then
11170 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
11171 end if;
11172 end if;
11174 if Error_Posted (Act_T) then
11175 null;
11176 else
11177 case Nkind (Def) is
11178 when N_Formal_Private_Type_Definition =>
11179 Validate_Private_Type_Instance;
11181 when N_Formal_Incomplete_Type_Definition =>
11182 Validate_Incomplete_Type_Instance;
11184 when N_Formal_Derived_Type_Definition =>
11185 Validate_Derived_Type_Instance;
11187 when N_Formal_Discrete_Type_Definition =>
11188 if not Is_Discrete_Type (Act_T) then
11189 Error_Msg_NE
11190 ("expect discrete type in instantiation of&",
11191 Actual, Gen_T);
11192 Abandon_Instantiation (Actual);
11193 end if;
11195 when N_Formal_Signed_Integer_Type_Definition =>
11196 if not Is_Signed_Integer_Type (Act_T) then
11197 Error_Msg_NE
11198 ("expect signed integer type in instantiation of&",
11199 Actual, Gen_T);
11200 Abandon_Instantiation (Actual);
11201 end if;
11203 when N_Formal_Modular_Type_Definition =>
11204 if not Is_Modular_Integer_Type (Act_T) then
11205 Error_Msg_NE
11206 ("expect modular type in instantiation of &",
11207 Actual, Gen_T);
11208 Abandon_Instantiation (Actual);
11209 end if;
11211 when N_Formal_Floating_Point_Definition =>
11212 if not Is_Floating_Point_Type (Act_T) then
11213 Error_Msg_NE
11214 ("expect float type in instantiation of &", Actual, Gen_T);
11215 Abandon_Instantiation (Actual);
11216 end if;
11218 when N_Formal_Ordinary_Fixed_Point_Definition =>
11219 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
11220 Error_Msg_NE
11221 ("expect ordinary fixed point type in instantiation of &",
11222 Actual, Gen_T);
11223 Abandon_Instantiation (Actual);
11224 end if;
11226 when N_Formal_Decimal_Fixed_Point_Definition =>
11227 if not Is_Decimal_Fixed_Point_Type (Act_T) then
11228 Error_Msg_NE
11229 ("expect decimal type in instantiation of &",
11230 Actual, Gen_T);
11231 Abandon_Instantiation (Actual);
11232 end if;
11234 when N_Array_Type_Definition =>
11235 Validate_Array_Type_Instance;
11237 when N_Access_To_Object_Definition =>
11238 Validate_Access_Type_Instance;
11240 when N_Access_Function_Definition |
11241 N_Access_Procedure_Definition =>
11242 Validate_Access_Subprogram_Instance;
11244 when N_Record_Definition =>
11245 Validate_Interface_Type_Instance;
11247 when N_Derived_Type_Definition =>
11248 Validate_Derived_Interface_Type_Instance;
11250 when others =>
11251 raise Program_Error;
11253 end case;
11254 end if;
11256 Subt := New_Copy (Gen_T);
11258 -- Use adjusted sloc of subtype name as the location for other nodes in
11259 -- the subtype declaration.
11261 Loc := Sloc (Subt);
11263 Decl_Node :=
11264 Make_Subtype_Declaration (Loc,
11265 Defining_Identifier => Subt,
11266 Subtype_Indication => New_Reference_To (Act_T, Loc));
11268 if Is_Private_Type (Act_T) then
11269 Set_Has_Private_View (Subtype_Indication (Decl_Node));
11271 elsif Is_Access_Type (Act_T)
11272 and then Is_Private_Type (Designated_Type (Act_T))
11273 then
11274 Set_Has_Private_View (Subtype_Indication (Decl_Node));
11275 end if;
11277 Decl_Nodes := New_List (Decl_Node);
11279 -- Flag actual derived types so their elaboration produces the
11280 -- appropriate renamings for the primitive operations of the ancestor.
11281 -- Flag actual for formal private types as well, to determine whether
11282 -- operations in the private part may override inherited operations.
11283 -- If the formal has an interface list, the ancestor is not the
11284 -- parent, but the analyzed formal that includes the interface
11285 -- operations of all its progenitors.
11287 -- Same treatment for formal private types, so we can check whether the
11288 -- type is tagged limited when validating derivations in the private
11289 -- part. (See AI05-096).
11291 if Nkind (Def) = N_Formal_Derived_Type_Definition then
11292 if Present (Interface_List (Def)) then
11293 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
11294 else
11295 Set_Generic_Parent_Type (Decl_Node, Ancestor);
11296 end if;
11298 elsif Nkind_In (Def,
11299 N_Formal_Private_Type_Definition,
11300 N_Formal_Incomplete_Type_Definition)
11301 then
11302 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
11303 end if;
11305 -- If the actual is a synchronized type that implements an interface,
11306 -- the primitive operations are attached to the corresponding record,
11307 -- and we have to treat it as an additional generic actual, so that its
11308 -- primitive operations become visible in the instance. The task or
11309 -- protected type itself does not carry primitive operations.
11311 if Is_Concurrent_Type (Act_T)
11312 and then Is_Tagged_Type (Act_T)
11313 and then Present (Corresponding_Record_Type (Act_T))
11314 and then Present (Ancestor)
11315 and then Is_Interface (Ancestor)
11316 then
11317 declare
11318 Corr_Rec : constant Entity_Id :=
11319 Corresponding_Record_Type (Act_T);
11320 New_Corr : Entity_Id;
11321 Corr_Decl : Node_Id;
11323 begin
11324 New_Corr := Make_Temporary (Loc, 'S');
11325 Corr_Decl :=
11326 Make_Subtype_Declaration (Loc,
11327 Defining_Identifier => New_Corr,
11328 Subtype_Indication =>
11329 New_Reference_To (Corr_Rec, Loc));
11330 Append_To (Decl_Nodes, Corr_Decl);
11332 if Ekind (Act_T) = E_Task_Type then
11333 Set_Ekind (Subt, E_Task_Subtype);
11334 else
11335 Set_Ekind (Subt, E_Protected_Subtype);
11336 end if;
11338 Set_Corresponding_Record_Type (Subt, Corr_Rec);
11339 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
11340 Set_Generic_Parent_Type (Decl_Node, Empty);
11341 end;
11342 end if;
11344 return Decl_Nodes;
11345 end Instantiate_Type;
11347 ---------------------
11348 -- Is_In_Main_Unit --
11349 ---------------------
11351 function Is_In_Main_Unit (N : Node_Id) return Boolean is
11352 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
11353 Current_Unit : Node_Id;
11355 begin
11356 if Unum = Main_Unit then
11357 return True;
11359 -- If the current unit is a subunit then it is either the main unit or
11360 -- is being compiled as part of the main unit.
11362 elsif Nkind (N) = N_Compilation_Unit then
11363 return Nkind (Unit (N)) = N_Subunit;
11364 end if;
11366 Current_Unit := Parent (N);
11367 while Present (Current_Unit)
11368 and then Nkind (Current_Unit) /= N_Compilation_Unit
11369 loop
11370 Current_Unit := Parent (Current_Unit);
11371 end loop;
11373 -- The instantiation node is in the main unit, or else the current node
11374 -- (perhaps as the result of nested instantiations) is in the main unit,
11375 -- or in the declaration of the main unit, which in this last case must
11376 -- be a body.
11378 return Unum = Main_Unit
11379 or else Current_Unit = Cunit (Main_Unit)
11380 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
11381 or else (Present (Library_Unit (Current_Unit))
11382 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
11383 end Is_In_Main_Unit;
11385 ----------------------------
11386 -- Load_Parent_Of_Generic --
11387 ----------------------------
11389 procedure Load_Parent_Of_Generic
11390 (N : Node_Id;
11391 Spec : Node_Id;
11392 Body_Optional : Boolean := False)
11394 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
11395 Save_Style_Check : constant Boolean := Style_Check;
11396 True_Parent : Node_Id;
11397 Inst_Node : Node_Id;
11398 OK : Boolean;
11399 Previous_Instances : constant Elist_Id := New_Elmt_List;
11401 procedure Collect_Previous_Instances (Decls : List_Id);
11402 -- Collect all instantiations in the given list of declarations, that
11403 -- precede the generic that we need to load. If the bodies of these
11404 -- instantiations are available, we must analyze them, to ensure that
11405 -- the public symbols generated are the same when the unit is compiled
11406 -- to generate code, and when it is compiled in the context of a unit
11407 -- that needs a particular nested instance. This process is applied to
11408 -- both package and subprogram instances.
11410 --------------------------------
11411 -- Collect_Previous_Instances --
11412 --------------------------------
11414 procedure Collect_Previous_Instances (Decls : List_Id) is
11415 Decl : Node_Id;
11417 begin
11418 Decl := First (Decls);
11419 while Present (Decl) loop
11420 if Sloc (Decl) >= Sloc (Inst_Node) then
11421 return;
11423 -- If Decl is an instantiation, then record it as requiring
11424 -- instantiation of the corresponding body, except if it is an
11425 -- abbreviated instantiation generated internally for conformance
11426 -- checking purposes only for the case of a formal package
11427 -- declared without a box (see Instantiate_Formal_Package). Such
11428 -- an instantiation does not generate any code (the actual code
11429 -- comes from actual) and thus does not need to be analyzed here.
11430 -- If the instantiation appears with a generic package body it is
11431 -- not analyzed here either.
11433 elsif Nkind (Decl) = N_Package_Instantiation
11434 and then not Is_Internal (Defining_Entity (Decl))
11435 then
11436 Append_Elmt (Decl, Previous_Instances);
11438 -- For a subprogram instantiation, omit instantiations intrinsic
11439 -- operations (Unchecked_Conversions, etc.) that have no bodies.
11441 elsif Nkind_In (Decl, N_Function_Instantiation,
11442 N_Procedure_Instantiation)
11443 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
11444 then
11445 Append_Elmt (Decl, Previous_Instances);
11447 elsif Nkind (Decl) = N_Package_Declaration then
11448 Collect_Previous_Instances
11449 (Visible_Declarations (Specification (Decl)));
11450 Collect_Previous_Instances
11451 (Private_Declarations (Specification (Decl)));
11453 -- Previous non-generic bodies may contain instances as well
11455 elsif Nkind (Decl) = N_Package_Body
11456 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
11457 then
11458 Collect_Previous_Instances (Declarations (Decl));
11460 elsif Nkind (Decl) = N_Subprogram_Body
11461 and then not Acts_As_Spec (Decl)
11462 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
11463 then
11464 Collect_Previous_Instances (Declarations (Decl));
11465 end if;
11467 Next (Decl);
11468 end loop;
11469 end Collect_Previous_Instances;
11471 -- Start of processing for Load_Parent_Of_Generic
11473 begin
11474 if not In_Same_Source_Unit (N, Spec)
11475 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
11476 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
11477 and then not Is_In_Main_Unit (Spec))
11478 then
11479 -- Find body of parent of spec, and analyze it. A special case arises
11480 -- when the parent is an instantiation, that is to say when we are
11481 -- currently instantiating a nested generic. In that case, there is
11482 -- no separate file for the body of the enclosing instance. Instead,
11483 -- the enclosing body must be instantiated as if it were a pending
11484 -- instantiation, in order to produce the body for the nested generic
11485 -- we require now. Note that in that case the generic may be defined
11486 -- in a package body, the instance defined in the same package body,
11487 -- and the original enclosing body may not be in the main unit.
11489 Inst_Node := Empty;
11491 True_Parent := Parent (Spec);
11492 while Present (True_Parent)
11493 and then Nkind (True_Parent) /= N_Compilation_Unit
11494 loop
11495 if Nkind (True_Parent) = N_Package_Declaration
11496 and then
11497 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
11498 then
11499 -- Parent is a compilation unit that is an instantiation.
11500 -- Instantiation node has been replaced with package decl.
11502 Inst_Node := Original_Node (True_Parent);
11503 exit;
11505 elsif Nkind (True_Parent) = N_Package_Declaration
11506 and then Present (Generic_Parent (Specification (True_Parent)))
11507 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
11508 then
11509 -- Parent is an instantiation within another specification.
11510 -- Declaration for instance has been inserted before original
11511 -- instantiation node. A direct link would be preferable?
11513 Inst_Node := Next (True_Parent);
11514 while Present (Inst_Node)
11515 and then Nkind (Inst_Node) /= N_Package_Instantiation
11516 loop
11517 Next (Inst_Node);
11518 end loop;
11520 -- If the instance appears within a generic, and the generic
11521 -- unit is defined within a formal package of the enclosing
11522 -- generic, there is no generic body available, and none
11523 -- needed. A more precise test should be used ???
11525 if No (Inst_Node) then
11526 return;
11527 end if;
11529 exit;
11531 else
11532 True_Parent := Parent (True_Parent);
11533 end if;
11534 end loop;
11536 -- Case where we are currently instantiating a nested generic
11538 if Present (Inst_Node) then
11539 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
11541 -- Instantiation node and declaration of instantiated package
11542 -- were exchanged when only the declaration was needed.
11543 -- Restore instantiation node before proceeding with body.
11545 Set_Unit (Parent (True_Parent), Inst_Node);
11546 end if;
11548 -- Now complete instantiation of enclosing body, if it appears in
11549 -- some other unit. If it appears in the current unit, the body
11550 -- will have been instantiated already.
11552 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
11554 -- We need to determine the expander mode to instantiate the
11555 -- enclosing body. Because the generic body we need may use
11556 -- global entities declared in the enclosing package (including
11557 -- aggregates) it is in general necessary to compile this body
11558 -- with expansion enabled, except if we are within a generic
11559 -- package, in which case the usual generic rule applies.
11561 declare
11562 Exp_Status : Boolean := True;
11563 Scop : Entity_Id;
11565 begin
11566 -- Loop through scopes looking for generic package
11568 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
11569 while Present (Scop)
11570 and then Scop /= Standard_Standard
11571 loop
11572 if Ekind (Scop) = E_Generic_Package then
11573 Exp_Status := False;
11574 exit;
11575 end if;
11577 Scop := Scope (Scop);
11578 end loop;
11580 -- Collect previous instantiations in the unit that contains
11581 -- the desired generic.
11583 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
11584 and then not Body_Optional
11585 then
11586 declare
11587 Decl : Elmt_Id;
11588 Info : Pending_Body_Info;
11589 Par : Node_Id;
11591 begin
11592 Par := Parent (Inst_Node);
11593 while Present (Par) loop
11594 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
11595 Par := Parent (Par);
11596 end loop;
11598 pragma Assert (Present (Par));
11600 if Nkind (Par) = N_Package_Body then
11601 Collect_Previous_Instances (Declarations (Par));
11603 elsif Nkind (Par) = N_Package_Declaration then
11604 Collect_Previous_Instances
11605 (Visible_Declarations (Specification (Par)));
11606 Collect_Previous_Instances
11607 (Private_Declarations (Specification (Par)));
11609 else
11610 -- Enclosing unit is a subprogram body. In this
11611 -- case all instance bodies are processed in order
11612 -- and there is no need to collect them separately.
11614 null;
11615 end if;
11617 Decl := First_Elmt (Previous_Instances);
11618 while Present (Decl) loop
11619 Info :=
11620 (Inst_Node => Node (Decl),
11621 Act_Decl =>
11622 Instance_Spec (Node (Decl)),
11623 Expander_Status => Exp_Status,
11624 Current_Sem_Unit =>
11625 Get_Code_Unit (Sloc (Node (Decl))),
11626 Scope_Suppress => Scope_Suppress,
11627 Local_Suppress_Stack_Top =>
11628 Local_Suppress_Stack_Top,
11629 Version => Ada_Version);
11631 -- Package instance
11634 Nkind (Node (Decl)) = N_Package_Instantiation
11635 then
11636 Instantiate_Package_Body
11637 (Info, Body_Optional => True);
11639 -- Subprogram instance
11641 else
11642 -- The instance_spec is the wrapper package,
11643 -- and the subprogram declaration is the last
11644 -- declaration in the wrapper.
11646 Info.Act_Decl :=
11647 Last
11648 (Visible_Declarations
11649 (Specification (Info.Act_Decl)));
11651 Instantiate_Subprogram_Body
11652 (Info, Body_Optional => True);
11653 end if;
11655 Next_Elmt (Decl);
11656 end loop;
11657 end;
11658 end if;
11660 Instantiate_Package_Body
11661 (Body_Info =>
11662 ((Inst_Node => Inst_Node,
11663 Act_Decl => True_Parent,
11664 Expander_Status => Exp_Status,
11665 Current_Sem_Unit =>
11666 Get_Code_Unit (Sloc (Inst_Node)),
11667 Scope_Suppress => Scope_Suppress,
11668 Local_Suppress_Stack_Top =>
11669 Local_Suppress_Stack_Top,
11670 Version => Ada_Version)),
11671 Body_Optional => Body_Optional);
11672 end;
11673 end if;
11675 -- Case where we are not instantiating a nested generic
11677 else
11678 Opt.Style_Check := False;
11679 Expander_Mode_Save_And_Set (True);
11680 Load_Needed_Body (Comp_Unit, OK);
11681 Opt.Style_Check := Save_Style_Check;
11682 Expander_Mode_Restore;
11684 if not OK
11685 and then Unit_Requires_Body (Defining_Entity (Spec))
11686 and then not Body_Optional
11687 then
11688 declare
11689 Bname : constant Unit_Name_Type :=
11690 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
11692 begin
11693 -- In CodePeer mode, the missing body may make the analysis
11694 -- incomplete, but we do not treat it as fatal.
11696 if CodePeer_Mode then
11697 return;
11699 else
11700 Error_Msg_Unit_1 := Bname;
11701 Error_Msg_N ("this instantiation requires$!", N);
11702 Error_Msg_File_1 :=
11703 Get_File_Name (Bname, Subunit => False);
11704 Error_Msg_N ("\but file{ was not found!", N);
11705 raise Unrecoverable_Error;
11706 end if;
11707 end;
11708 end if;
11709 end if;
11710 end if;
11712 -- If loading parent of the generic caused an instantiation circularity,
11713 -- we abandon compilation at this point, because otherwise in some cases
11714 -- we get into trouble with infinite recursions after this point.
11716 if Circularity_Detected then
11717 raise Unrecoverable_Error;
11718 end if;
11719 end Load_Parent_Of_Generic;
11721 ---------------------------------
11722 -- Map_Formal_Package_Entities --
11723 ---------------------------------
11725 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
11726 E1 : Entity_Id;
11727 E2 : Entity_Id;
11729 begin
11730 Set_Instance_Of (Form, Act);
11732 -- Traverse formal and actual package to map the corresponding entities.
11733 -- We skip over internal entities that may be generated during semantic
11734 -- analysis, and find the matching entities by name, given that they
11735 -- must appear in the same order.
11737 E1 := First_Entity (Form);
11738 E2 := First_Entity (Act);
11739 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
11740 -- Could this test be a single condition???
11741 -- Seems like it could, and isn't FPE (Form) a constant anyway???
11743 if not Is_Internal (E1)
11744 and then Present (Parent (E1))
11745 and then not Is_Class_Wide_Type (E1)
11746 and then not Is_Internal_Name (Chars (E1))
11747 then
11748 while Present (E2) and then Chars (E2) /= Chars (E1) loop
11749 Next_Entity (E2);
11750 end loop;
11752 if No (E2) then
11753 exit;
11754 else
11755 Set_Instance_Of (E1, E2);
11757 if Is_Type (E1) and then Is_Tagged_Type (E2) then
11758 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
11759 end if;
11761 if Is_Constrained (E1) then
11762 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
11763 end if;
11765 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
11766 Map_Formal_Package_Entities (E1, E2);
11767 end if;
11768 end if;
11769 end if;
11771 Next_Entity (E1);
11772 end loop;
11773 end Map_Formal_Package_Entities;
11775 -----------------------
11776 -- Move_Freeze_Nodes --
11777 -----------------------
11779 procedure Move_Freeze_Nodes
11780 (Out_Of : Entity_Id;
11781 After : Node_Id;
11782 L : List_Id)
11784 Decl : Node_Id;
11785 Next_Decl : Node_Id;
11786 Next_Node : Node_Id := After;
11787 Spec : Node_Id;
11789 function Is_Outer_Type (T : Entity_Id) return Boolean;
11790 -- Check whether entity is declared in a scope external to that of the
11791 -- generic unit.
11793 -------------------
11794 -- Is_Outer_Type --
11795 -------------------
11797 function Is_Outer_Type (T : Entity_Id) return Boolean is
11798 Scop : Entity_Id := Scope (T);
11800 begin
11801 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
11802 return True;
11804 else
11805 while Scop /= Standard_Standard loop
11806 if Scop = Out_Of then
11807 return False;
11808 else
11809 Scop := Scope (Scop);
11810 end if;
11811 end loop;
11813 return True;
11814 end if;
11815 end Is_Outer_Type;
11817 -- Start of processing for Move_Freeze_Nodes
11819 begin
11820 if No (L) then
11821 return;
11822 end if;
11824 -- First remove the freeze nodes that may appear before all other
11825 -- declarations.
11827 Decl := First (L);
11828 while Present (Decl)
11829 and then Nkind (Decl) = N_Freeze_Entity
11830 and then Is_Outer_Type (Entity (Decl))
11831 loop
11832 Decl := Remove_Head (L);
11833 Insert_After (Next_Node, Decl);
11834 Set_Analyzed (Decl, False);
11835 Next_Node := Decl;
11836 Decl := First (L);
11837 end loop;
11839 -- Next scan the list of declarations and remove each freeze node that
11840 -- appears ahead of the current node.
11842 while Present (Decl) loop
11843 while Present (Next (Decl))
11844 and then Nkind (Next (Decl)) = N_Freeze_Entity
11845 and then Is_Outer_Type (Entity (Next (Decl)))
11846 loop
11847 Next_Decl := Remove_Next (Decl);
11848 Insert_After (Next_Node, Next_Decl);
11849 Set_Analyzed (Next_Decl, False);
11850 Next_Node := Next_Decl;
11851 end loop;
11853 -- If the declaration is a nested package or concurrent type, then
11854 -- recurse. Nested generic packages will have been processed from the
11855 -- inside out.
11857 case Nkind (Decl) is
11858 when N_Package_Declaration =>
11859 Spec := Specification (Decl);
11861 when N_Task_Type_Declaration =>
11862 Spec := Task_Definition (Decl);
11864 when N_Protected_Type_Declaration =>
11865 Spec := Protected_Definition (Decl);
11867 when others =>
11868 Spec := Empty;
11869 end case;
11871 if Present (Spec) then
11872 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
11873 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
11874 end if;
11876 Next (Decl);
11877 end loop;
11878 end Move_Freeze_Nodes;
11880 ----------------
11881 -- Next_Assoc --
11882 ----------------
11884 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
11885 begin
11886 return Generic_Renamings.Table (E).Next_In_HTable;
11887 end Next_Assoc;
11889 ------------------------
11890 -- Preanalyze_Actuals --
11891 ------------------------
11893 procedure Preanalyze_Actuals (N : Node_Id) is
11894 Assoc : Node_Id;
11895 Act : Node_Id;
11896 Errs : constant Int := Serious_Errors_Detected;
11898 Cur : Entity_Id := Empty;
11899 -- Current homograph of the instance name
11901 Vis : Boolean;
11902 -- Saved visibility status of the current homograph
11904 begin
11905 Assoc := First (Generic_Associations (N));
11907 -- If the instance is a child unit, its name may hide an outer homonym,
11908 -- so make it invisible to perform name resolution on the actuals.
11910 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
11911 and then Present
11912 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
11913 then
11914 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
11916 if Is_Compilation_Unit (Cur) then
11917 Vis := Is_Immediately_Visible (Cur);
11918 Set_Is_Immediately_Visible (Cur, False);
11919 else
11920 Cur := Empty;
11921 end if;
11922 end if;
11924 while Present (Assoc) loop
11925 if Nkind (Assoc) /= N_Others_Choice then
11926 Act := Explicit_Generic_Actual_Parameter (Assoc);
11928 -- Within a nested instantiation, a defaulted actual is an empty
11929 -- association, so nothing to analyze. If the subprogram actual
11930 -- is an attribute, analyze prefix only, because actual is not a
11931 -- complete attribute reference.
11933 -- If actual is an allocator, analyze expression only. The full
11934 -- analysis can generate code, and if instance is a compilation
11935 -- unit we have to wait until the package instance is installed
11936 -- to have a proper place to insert this code.
11938 -- String literals may be operators, but at this point we do not
11939 -- know whether the actual is a formal subprogram or a string.
11941 if No (Act) then
11942 null;
11944 elsif Nkind (Act) = N_Attribute_Reference then
11945 Analyze (Prefix (Act));
11947 elsif Nkind (Act) = N_Explicit_Dereference then
11948 Analyze (Prefix (Act));
11950 elsif Nkind (Act) = N_Allocator then
11951 declare
11952 Expr : constant Node_Id := Expression (Act);
11954 begin
11955 if Nkind (Expr) = N_Subtype_Indication then
11956 Analyze (Subtype_Mark (Expr));
11958 -- Analyze separately each discriminant constraint, when
11959 -- given with a named association.
11961 declare
11962 Constr : Node_Id;
11964 begin
11965 Constr := First (Constraints (Constraint (Expr)));
11966 while Present (Constr) loop
11967 if Nkind (Constr) = N_Discriminant_Association then
11968 Analyze (Expression (Constr));
11969 else
11970 Analyze (Constr);
11971 end if;
11973 Next (Constr);
11974 end loop;
11975 end;
11977 else
11978 Analyze (Expr);
11979 end if;
11980 end;
11982 elsif Nkind (Act) /= N_Operator_Symbol then
11983 Analyze (Act);
11984 end if;
11986 if Errs /= Serious_Errors_Detected then
11988 -- Do a minimal analysis of the generic, to prevent spurious
11989 -- warnings complaining about the generic being unreferenced,
11990 -- before abandoning the instantiation.
11992 Analyze (Name (N));
11994 if Is_Entity_Name (Name (N))
11995 and then Etype (Name (N)) /= Any_Type
11996 then
11997 Generate_Reference (Entity (Name (N)), Name (N));
11998 Set_Is_Instantiated (Entity (Name (N)));
11999 end if;
12001 if Present (Cur) then
12003 -- For the case of a child instance hiding an outer homonym,
12004 -- provide additional warning which might explain the error.
12006 Set_Is_Immediately_Visible (Cur, Vis);
12007 Error_Msg_NE ("& hides outer unit with the same name?",
12008 N, Defining_Unit_Name (N));
12009 end if;
12011 Abandon_Instantiation (Act);
12012 end if;
12013 end if;
12015 Next (Assoc);
12016 end loop;
12018 if Present (Cur) then
12019 Set_Is_Immediately_Visible (Cur, Vis);
12020 end if;
12021 end Preanalyze_Actuals;
12023 -------------------
12024 -- Remove_Parent --
12025 -------------------
12027 procedure Remove_Parent (In_Body : Boolean := False) is
12028 S : Entity_Id := Current_Scope;
12029 -- S is the scope containing the instantiation just completed. The scope
12030 -- stack contains the parent instances of the instantiation, followed by
12031 -- the original S.
12033 Cur_P : Entity_Id;
12034 E : Entity_Id;
12035 P : Entity_Id;
12036 Hidden : Elmt_Id;
12038 begin
12039 -- After child instantiation is complete, remove from scope stack the
12040 -- extra copy of the current scope, and then remove parent instances.
12042 if not In_Body then
12043 Pop_Scope;
12045 while Current_Scope /= S loop
12046 P := Current_Scope;
12047 End_Package_Scope (Current_Scope);
12049 if In_Open_Scopes (P) then
12050 E := First_Entity (P);
12051 while Present (E) loop
12052 Set_Is_Immediately_Visible (E, True);
12053 Next_Entity (E);
12054 end loop;
12056 -- If instantiation is declared in a block, it is the enclosing
12057 -- scope that might be a parent instance. Note that only one
12058 -- block can be involved, because the parent instances have
12059 -- been installed within it.
12061 if Ekind (P) = E_Block then
12062 Cur_P := Scope (P);
12063 else
12064 Cur_P := P;
12065 end if;
12067 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
12068 -- We are within an instance of some sibling. Retain
12069 -- visibility of parent, for proper subsequent cleanup, and
12070 -- reinstall private declarations as well.
12072 Set_In_Private_Part (P);
12073 Install_Private_Declarations (P);
12074 end if;
12076 -- If the ultimate parent is a top-level unit recorded in
12077 -- Instance_Parent_Unit, then reset its visibility to what it was
12078 -- before instantiation. (It's not clear what the purpose is of
12079 -- testing whether Scope (P) is In_Open_Scopes, but that test was
12080 -- present before the ultimate parent test was added.???)
12082 elsif not In_Open_Scopes (Scope (P))
12083 or else (P = Instance_Parent_Unit
12084 and then not Parent_Unit_Visible)
12085 then
12086 Set_Is_Immediately_Visible (P, False);
12088 -- If the current scope is itself an instantiation of a generic
12089 -- nested within P, and we are in the private part of body of this
12090 -- instantiation, restore the full views of P, that were removed
12091 -- in End_Package_Scope above. This obscure case can occur when a
12092 -- subunit of a generic contains an instance of a child unit of
12093 -- its generic parent unit.
12095 elsif S = Current_Scope and then Is_Generic_Instance (S) then
12096 declare
12097 Par : constant Entity_Id :=
12098 Generic_Parent
12099 (Specification (Unit_Declaration_Node (S)));
12100 begin
12101 if Present (Par)
12102 and then P = Scope (Par)
12103 and then (In_Package_Body (S) or else In_Private_Part (S))
12104 then
12105 Set_In_Private_Part (P);
12106 Install_Private_Declarations (P);
12107 end if;
12108 end;
12109 end if;
12110 end loop;
12112 -- Reset visibility of entities in the enclosing scope
12114 Set_Is_Hidden_Open_Scope (Current_Scope, False);
12116 Hidden := First_Elmt (Hidden_Entities);
12117 while Present (Hidden) loop
12118 Set_Is_Immediately_Visible (Node (Hidden), True);
12119 Next_Elmt (Hidden);
12120 end loop;
12122 else
12123 -- Each body is analyzed separately, and there is no context that
12124 -- needs preserving from one body instance to the next, so remove all
12125 -- parent scopes that have been installed.
12127 while Present (S) loop
12128 End_Package_Scope (S);
12129 Set_Is_Immediately_Visible (S, False);
12130 S := Current_Scope;
12131 exit when S = Standard_Standard;
12132 end loop;
12133 end if;
12134 end Remove_Parent;
12136 -----------------
12137 -- Restore_Env --
12138 -----------------
12140 procedure Restore_Env is
12141 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
12143 begin
12144 if No (Current_Instantiated_Parent.Act_Id) then
12145 -- Restore environment after subprogram inlining
12147 Restore_Private_Views (Empty);
12148 end if;
12150 Current_Instantiated_Parent := Saved.Instantiated_Parent;
12151 Exchanged_Views := Saved.Exchanged_Views;
12152 Hidden_Entities := Saved.Hidden_Entities;
12153 Current_Sem_Unit := Saved.Current_Sem_Unit;
12154 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
12155 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
12157 Restore_Opt_Config_Switches (Saved.Switches);
12159 Instance_Envs.Decrement_Last;
12160 end Restore_Env;
12162 ---------------------------
12163 -- Restore_Private_Views --
12164 ---------------------------
12166 procedure Restore_Private_Views
12167 (Pack_Id : Entity_Id;
12168 Is_Package : Boolean := True)
12170 M : Elmt_Id;
12171 E : Entity_Id;
12172 Typ : Entity_Id;
12173 Dep_Elmt : Elmt_Id;
12174 Dep_Typ : Node_Id;
12176 procedure Restore_Nested_Formal (Formal : Entity_Id);
12177 -- Hide the generic formals of formal packages declared with box which
12178 -- were reachable in the current instantiation.
12180 ---------------------------
12181 -- Restore_Nested_Formal --
12182 ---------------------------
12184 procedure Restore_Nested_Formal (Formal : Entity_Id) is
12185 Ent : Entity_Id;
12187 begin
12188 if Present (Renamed_Object (Formal))
12189 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
12190 then
12191 return;
12193 elsif Present (Associated_Formal_Package (Formal)) then
12194 Ent := First_Entity (Formal);
12195 while Present (Ent) loop
12196 exit when Ekind (Ent) = E_Package
12197 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
12199 Set_Is_Hidden (Ent);
12200 Set_Is_Potentially_Use_Visible (Ent, False);
12202 -- If package, then recurse
12204 if Ekind (Ent) = E_Package then
12205 Restore_Nested_Formal (Ent);
12206 end if;
12208 Next_Entity (Ent);
12209 end loop;
12210 end if;
12211 end Restore_Nested_Formal;
12213 -- Start of processing for Restore_Private_Views
12215 begin
12216 M := First_Elmt (Exchanged_Views);
12217 while Present (M) loop
12218 Typ := Node (M);
12220 -- Subtypes of types whose views have been exchanged, and that are
12221 -- defined within the instance, were not on the Private_Dependents
12222 -- list on entry to the instance, so they have to be exchanged
12223 -- explicitly now, in order to remain consistent with the view of the
12224 -- parent type.
12226 if Ekind_In (Typ, E_Private_Type,
12227 E_Limited_Private_Type,
12228 E_Record_Type_With_Private)
12229 then
12230 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
12231 while Present (Dep_Elmt) loop
12232 Dep_Typ := Node (Dep_Elmt);
12234 if Scope (Dep_Typ) = Pack_Id
12235 and then Present (Full_View (Dep_Typ))
12236 then
12237 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
12238 Exchange_Declarations (Dep_Typ);
12239 end if;
12241 Next_Elmt (Dep_Elmt);
12242 end loop;
12243 end if;
12245 Exchange_Declarations (Node (M));
12246 Next_Elmt (M);
12247 end loop;
12249 if No (Pack_Id) then
12250 return;
12251 end if;
12253 -- Make the generic formal parameters private, and make the formal types
12254 -- into subtypes of the actuals again.
12256 E := First_Entity (Pack_Id);
12257 while Present (E) loop
12258 Set_Is_Hidden (E, True);
12260 if Is_Type (E)
12261 and then Nkind (Parent (E)) = N_Subtype_Declaration
12262 then
12263 Set_Is_Generic_Actual_Type (E, False);
12265 -- An unusual case of aliasing: the actual may also be directly
12266 -- visible in the generic, and be private there, while it is fully
12267 -- visible in the context of the instance. The internal subtype
12268 -- is private in the instance but has full visibility like its
12269 -- parent in the enclosing scope. This enforces the invariant that
12270 -- the privacy status of all private dependents of a type coincide
12271 -- with that of the parent type. This can only happen when a
12272 -- generic child unit is instantiated within a sibling.
12274 if Is_Private_Type (E)
12275 and then not Is_Private_Type (Etype (E))
12276 then
12277 Exchange_Declarations (E);
12278 end if;
12280 elsif Ekind (E) = E_Package then
12282 -- The end of the renaming list is the renaming of the generic
12283 -- package itself. If the instance is a subprogram, all entities
12284 -- in the corresponding package are renamings. If this entity is
12285 -- a formal package, make its own formals private as well. The
12286 -- actual in this case is itself the renaming of an instantiation.
12287 -- If the entity is not a package renaming, it is the entity
12288 -- created to validate formal package actuals: ignore it.
12290 -- If the actual is itself a formal package for the enclosing
12291 -- generic, or the actual for such a formal package, it remains
12292 -- visible on exit from the instance, and therefore nothing needs
12293 -- to be done either, except to keep it accessible.
12295 if Is_Package and then Renamed_Object (E) = Pack_Id then
12296 exit;
12298 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
12299 null;
12301 elsif
12302 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
12303 then
12304 Set_Is_Hidden (E, False);
12306 else
12307 declare
12308 Act_P : constant Entity_Id := Renamed_Object (E);
12309 Id : Entity_Id;
12311 begin
12312 Id := First_Entity (Act_P);
12313 while Present (Id)
12314 and then Id /= First_Private_Entity (Act_P)
12315 loop
12316 exit when Ekind (Id) = E_Package
12317 and then Renamed_Object (Id) = Act_P;
12319 Set_Is_Hidden (Id, True);
12320 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
12322 if Ekind (Id) = E_Package then
12323 Restore_Nested_Formal (Id);
12324 end if;
12326 Next_Entity (Id);
12327 end loop;
12328 end;
12329 end if;
12330 end if;
12332 Next_Entity (E);
12333 end loop;
12334 end Restore_Private_Views;
12336 --------------
12337 -- Save_Env --
12338 --------------
12340 procedure Save_Env
12341 (Gen_Unit : Entity_Id;
12342 Act_Unit : Entity_Id)
12344 begin
12345 Init_Env;
12346 Set_Instance_Env (Gen_Unit, Act_Unit);
12347 end Save_Env;
12349 ----------------------------
12350 -- Save_Global_References --
12351 ----------------------------
12353 procedure Save_Global_References (N : Node_Id) is
12354 Gen_Scope : Entity_Id;
12355 E : Entity_Id;
12356 N2 : Node_Id;
12358 function Is_Global (E : Entity_Id) return Boolean;
12359 -- Check whether entity is defined outside of generic unit. Examine the
12360 -- scope of an entity, and the scope of the scope, etc, until we find
12361 -- either Standard, in which case the entity is global, or the generic
12362 -- unit itself, which indicates that the entity is local. If the entity
12363 -- is the generic unit itself, as in the case of a recursive call, or
12364 -- the enclosing generic unit, if different from the current scope, then
12365 -- it is local as well, because it will be replaced at the point of
12366 -- instantiation. On the other hand, if it is a reference to a child
12367 -- unit of a common ancestor, which appears in an instantiation, it is
12368 -- global because it is used to denote a specific compilation unit at
12369 -- the time the instantiations will be analyzed.
12371 procedure Reset_Entity (N : Node_Id);
12372 -- Save semantic information on global entity so that it is not resolved
12373 -- again at instantiation time.
12375 procedure Save_Entity_Descendants (N : Node_Id);
12376 -- Apply Save_Global_References to the two syntactic descendants of
12377 -- non-terminal nodes that carry an Associated_Node and are processed
12378 -- through Reset_Entity. Once the global entity (if any) has been
12379 -- captured together with its type, only two syntactic descendants need
12380 -- to be traversed to complete the processing of the tree rooted at N.
12381 -- This applies to Selected_Components, Expanded_Names, and to Operator
12382 -- nodes. N can also be a character literal, identifier, or operator
12383 -- symbol node, but the call has no effect in these cases.
12385 procedure Save_Global_Defaults (N1, N2 : Node_Id);
12386 -- Default actuals in nested instances must be handled specially
12387 -- because there is no link to them from the original tree. When an
12388 -- actual subprogram is given by a default, we add an explicit generic
12389 -- association for it in the instantiation node. When we save the
12390 -- global references on the name of the instance, we recover the list
12391 -- of generic associations, and add an explicit one to the original
12392 -- generic tree, through which a global actual can be preserved.
12393 -- Similarly, if a child unit is instantiated within a sibling, in the
12394 -- context of the parent, we must preserve the identifier of the parent
12395 -- so that it can be properly resolved in a subsequent instantiation.
12397 procedure Save_Global_Descendant (D : Union_Id);
12398 -- Apply Save_Global_References recursively to the descendents of the
12399 -- current node.
12401 procedure Save_References (N : Node_Id);
12402 -- This is the recursive procedure that does the work, once the
12403 -- enclosing generic scope has been established.
12405 ---------------
12406 -- Is_Global --
12407 ---------------
12409 function Is_Global (E : Entity_Id) return Boolean is
12410 Se : Entity_Id;
12412 function Is_Instance_Node (Decl : Node_Id) return Boolean;
12413 -- Determine whether the parent node of a reference to a child unit
12414 -- denotes an instantiation or a formal package, in which case the
12415 -- reference to the child unit is global, even if it appears within
12416 -- the current scope (e.g. when the instance appears within the body
12417 -- of an ancestor).
12419 ----------------------
12420 -- Is_Instance_Node --
12421 ----------------------
12423 function Is_Instance_Node (Decl : Node_Id) return Boolean is
12424 begin
12425 return Nkind (Decl) in N_Generic_Instantiation
12426 or else
12427 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
12428 end Is_Instance_Node;
12430 -- Start of processing for Is_Global
12432 begin
12433 if E = Gen_Scope then
12434 return False;
12436 elsif E = Standard_Standard then
12437 return True;
12439 elsif Is_Child_Unit (E)
12440 and then (Is_Instance_Node (Parent (N2))
12441 or else (Nkind (Parent (N2)) = N_Expanded_Name
12442 and then N2 = Selector_Name (Parent (N2))
12443 and then
12444 Is_Instance_Node (Parent (Parent (N2)))))
12445 then
12446 return True;
12448 else
12449 Se := Scope (E);
12450 while Se /= Gen_Scope loop
12451 if Se = Standard_Standard then
12452 return True;
12453 else
12454 Se := Scope (Se);
12455 end if;
12456 end loop;
12458 return False;
12459 end if;
12460 end Is_Global;
12462 ------------------
12463 -- Reset_Entity --
12464 ------------------
12466 procedure Reset_Entity (N : Node_Id) is
12468 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
12469 -- If the type of N2 is global to the generic unit, save the type in
12470 -- the generic node. Just as we perform name capture for explicit
12471 -- references within the generic, we must capture the global types
12472 -- of local entities because they may participate in resolution in
12473 -- the instance.
12475 function Top_Ancestor (E : Entity_Id) return Entity_Id;
12476 -- Find the ultimate ancestor of the current unit. If it is not a
12477 -- generic unit, then the name of the current unit in the prefix of
12478 -- an expanded name must be replaced with its generic homonym to
12479 -- ensure that it will be properly resolved in an instance.
12481 ---------------------
12482 -- Set_Global_Type --
12483 ---------------------
12485 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
12486 Typ : constant Entity_Id := Etype (N2);
12488 begin
12489 Set_Etype (N, Typ);
12491 if Entity (N) /= N2
12492 and then Has_Private_View (Entity (N))
12493 then
12494 -- If the entity of N is not the associated node, this is a
12495 -- nested generic and it has an associated node as well, whose
12496 -- type is already the full view (see below). Indicate that the
12497 -- original node has a private view.
12499 Set_Has_Private_View (N);
12500 end if;
12502 -- If not a private type, nothing else to do
12504 if not Is_Private_Type (Typ) then
12505 if Is_Array_Type (Typ)
12506 and then Is_Private_Type (Component_Type (Typ))
12507 then
12508 Set_Has_Private_View (N);
12509 end if;
12511 -- If it is a derivation of a private type in a context where no
12512 -- full view is needed, nothing to do either.
12514 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
12515 null;
12517 -- Otherwise mark the type for flipping and use the full view when
12518 -- available.
12520 else
12521 Set_Has_Private_View (N);
12523 if Present (Full_View (Typ)) then
12524 Set_Etype (N2, Full_View (Typ));
12525 end if;
12526 end if;
12527 end Set_Global_Type;
12529 ------------------
12530 -- Top_Ancestor --
12531 ------------------
12533 function Top_Ancestor (E : Entity_Id) return Entity_Id is
12534 Par : Entity_Id;
12536 begin
12537 Par := E;
12538 while Is_Child_Unit (Par) loop
12539 Par := Scope (Par);
12540 end loop;
12542 return Par;
12543 end Top_Ancestor;
12545 -- Start of processing for Reset_Entity
12547 begin
12548 N2 := Get_Associated_Node (N);
12549 E := Entity (N2);
12551 if Present (E) then
12553 -- If the node is an entry call to an entry in an enclosing task,
12554 -- it is rewritten as a selected component. No global entity to
12555 -- preserve in this case, since the expansion will be redone in
12556 -- the instance.
12558 if not Nkind_In (E, N_Defining_Identifier,
12559 N_Defining_Character_Literal,
12560 N_Defining_Operator_Symbol)
12561 then
12562 Set_Associated_Node (N, Empty);
12563 Set_Etype (N, Empty);
12564 return;
12565 end if;
12567 -- If the entity is an itype created as a subtype of an access
12568 -- type with a null exclusion restore source entity for proper
12569 -- visibility. The itype will be created anew in the instance.
12571 if Is_Itype (E)
12572 and then Ekind (E) = E_Access_Subtype
12573 and then Is_Entity_Name (N)
12574 and then Chars (Etype (E)) = Chars (N)
12575 then
12576 E := Etype (E);
12577 Set_Entity (N2, E);
12578 Set_Etype (N2, E);
12579 end if;
12581 if Is_Global (E) then
12582 Set_Global_Type (N, N2);
12584 elsif Nkind (N) = N_Op_Concat
12585 and then Is_Generic_Type (Etype (N2))
12586 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
12587 or else
12588 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
12589 and then Is_Intrinsic_Subprogram (E)
12590 then
12591 null;
12593 else
12594 -- Entity is local. Mark generic node as unresolved.
12595 -- Note that now it does not have an entity.
12597 Set_Associated_Node (N, Empty);
12598 Set_Etype (N, Empty);
12599 end if;
12601 if Nkind (Parent (N)) in N_Generic_Instantiation
12602 and then N = Name (Parent (N))
12603 then
12604 Save_Global_Defaults (Parent (N), Parent (N2));
12605 end if;
12607 elsif Nkind (Parent (N)) = N_Selected_Component
12608 and then Nkind (Parent (N2)) = N_Expanded_Name
12609 then
12610 if Is_Global (Entity (Parent (N2))) then
12611 Change_Selected_Component_To_Expanded_Name (Parent (N));
12612 Set_Associated_Node (Parent (N), Parent (N2));
12613 Set_Global_Type (Parent (N), Parent (N2));
12614 Save_Entity_Descendants (N);
12616 -- If this is a reference to the current generic entity, replace
12617 -- by the name of the generic homonym of the current package. This
12618 -- is because in an instantiation Par.P.Q will not resolve to the
12619 -- name of the instance, whose enclosing scope is not necessarily
12620 -- Par. We use the generic homonym rather that the name of the
12621 -- generic itself because it may be hidden by a local declaration.
12623 elsif In_Open_Scopes (Entity (Parent (N2)))
12624 and then not
12625 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
12626 then
12627 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
12628 Rewrite (Parent (N),
12629 Make_Identifier (Sloc (N),
12630 Chars =>
12631 Chars (Generic_Homonym (Entity (Parent (N2))))));
12632 else
12633 Rewrite (Parent (N),
12634 Make_Identifier (Sloc (N),
12635 Chars => Chars (Selector_Name (Parent (N2)))));
12636 end if;
12637 end if;
12639 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
12640 and then Parent (N) = Name (Parent (Parent (N)))
12641 then
12642 Save_Global_Defaults
12643 (Parent (Parent (N)), Parent (Parent ((N2))));
12644 end if;
12646 -- A selected component may denote a static constant that has been
12647 -- folded. If the static constant is global to the generic, capture
12648 -- its value. Otherwise the folding will happen in any instantiation.
12650 elsif Nkind (Parent (N)) = N_Selected_Component
12651 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
12652 then
12653 if Present (Entity (Original_Node (Parent (N2))))
12654 and then Is_Global (Entity (Original_Node (Parent (N2))))
12655 then
12656 Rewrite (Parent (N), New_Copy (Parent (N2)));
12657 Set_Analyzed (Parent (N), False);
12659 else
12660 null;
12661 end if;
12663 -- A selected component may be transformed into a parameterless
12664 -- function call. If the called entity is global, rewrite the node
12665 -- appropriately, i.e. as an extended name for the global entity.
12667 elsif Nkind (Parent (N)) = N_Selected_Component
12668 and then Nkind (Parent (N2)) = N_Function_Call
12669 and then N = Selector_Name (Parent (N))
12670 then
12671 if No (Parameter_Associations (Parent (N2))) then
12672 if Is_Global (Entity (Name (Parent (N2)))) then
12673 Change_Selected_Component_To_Expanded_Name (Parent (N));
12674 Set_Associated_Node (Parent (N), Name (Parent (N2)));
12675 Set_Global_Type (Parent (N), Name (Parent (N2)));
12676 Save_Entity_Descendants (N);
12678 else
12679 Set_Associated_Node (N, Empty);
12680 Set_Etype (N, Empty);
12681 end if;
12683 -- In Ada 2005, X.F may be a call to a primitive operation,
12684 -- rewritten as F (X). This rewriting will be done again in an
12685 -- instance, so keep the original node. Global entities will be
12686 -- captured as for other constructs.
12688 else
12689 null;
12690 end if;
12692 -- Entity is local. Reset in generic unit, so that node is resolved
12693 -- anew at the point of instantiation.
12695 else
12696 Set_Associated_Node (N, Empty);
12697 Set_Etype (N, Empty);
12698 end if;
12699 end Reset_Entity;
12701 -----------------------------
12702 -- Save_Entity_Descendants --
12703 -----------------------------
12705 procedure Save_Entity_Descendants (N : Node_Id) is
12706 begin
12707 case Nkind (N) is
12708 when N_Binary_Op =>
12709 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
12710 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
12712 when N_Unary_Op =>
12713 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
12715 when N_Expanded_Name | N_Selected_Component =>
12716 Save_Global_Descendant (Union_Id (Prefix (N)));
12717 Save_Global_Descendant (Union_Id (Selector_Name (N)));
12719 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
12720 null;
12722 when others =>
12723 raise Program_Error;
12724 end case;
12725 end Save_Entity_Descendants;
12727 --------------------------
12728 -- Save_Global_Defaults --
12729 --------------------------
12731 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
12732 Loc : constant Source_Ptr := Sloc (N1);
12733 Assoc2 : constant List_Id := Generic_Associations (N2);
12734 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
12735 Assoc1 : List_Id;
12736 Act1 : Node_Id;
12737 Act2 : Node_Id;
12738 Def : Node_Id;
12739 Ndec : Node_Id;
12740 Subp : Entity_Id;
12741 Actual : Entity_Id;
12743 begin
12744 Assoc1 := Generic_Associations (N1);
12746 if Present (Assoc1) then
12747 Act1 := First (Assoc1);
12748 else
12749 Act1 := Empty;
12750 Set_Generic_Associations (N1, New_List);
12751 Assoc1 := Generic_Associations (N1);
12752 end if;
12754 if Present (Assoc2) then
12755 Act2 := First (Assoc2);
12756 else
12757 return;
12758 end if;
12760 while Present (Act1) and then Present (Act2) loop
12761 Next (Act1);
12762 Next (Act2);
12763 end loop;
12765 -- Find the associations added for default subprograms
12767 if Present (Act2) then
12768 while Nkind (Act2) /= N_Generic_Association
12769 or else No (Entity (Selector_Name (Act2)))
12770 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
12771 loop
12772 Next (Act2);
12773 end loop;
12775 -- Add a similar association if the default is global. The
12776 -- renaming declaration for the actual has been analyzed, and
12777 -- its alias is the program it renames. Link the actual in the
12778 -- original generic tree with the node in the analyzed tree.
12780 while Present (Act2) loop
12781 Subp := Entity (Selector_Name (Act2));
12782 Def := Explicit_Generic_Actual_Parameter (Act2);
12784 -- Following test is defence against rubbish errors
12786 if No (Alias (Subp)) then
12787 return;
12788 end if;
12790 -- Retrieve the resolved actual from the renaming declaration
12791 -- created for the instantiated formal.
12793 Actual := Entity (Name (Parent (Parent (Subp))));
12794 Set_Entity (Def, Actual);
12795 Set_Etype (Def, Etype (Actual));
12797 if Is_Global (Actual) then
12798 Ndec :=
12799 Make_Generic_Association (Loc,
12800 Selector_Name => New_Occurrence_Of (Subp, Loc),
12801 Explicit_Generic_Actual_Parameter =>
12802 New_Occurrence_Of (Actual, Loc));
12804 Set_Associated_Node
12805 (Explicit_Generic_Actual_Parameter (Ndec), Def);
12807 Append (Ndec, Assoc1);
12809 -- If there are other defaults, add a dummy association in case
12810 -- there are other defaulted formals with the same name.
12812 elsif Present (Next (Act2)) then
12813 Ndec :=
12814 Make_Generic_Association (Loc,
12815 Selector_Name => New_Occurrence_Of (Subp, Loc),
12816 Explicit_Generic_Actual_Parameter => Empty);
12818 Append (Ndec, Assoc1);
12819 end if;
12821 Next (Act2);
12822 end loop;
12823 end if;
12825 if Nkind (Name (N1)) = N_Identifier
12826 and then Is_Child_Unit (Gen_Id)
12827 and then Is_Global (Gen_Id)
12828 and then Is_Generic_Unit (Scope (Gen_Id))
12829 and then In_Open_Scopes (Scope (Gen_Id))
12830 then
12831 -- This is an instantiation of a child unit within a sibling, so
12832 -- that the generic parent is in scope. An eventual instance must
12833 -- occur within the scope of an instance of the parent. Make name
12834 -- in instance into an expanded name, to preserve the identifier
12835 -- of the parent, so it can be resolved subsequently.
12837 Rewrite (Name (N2),
12838 Make_Expanded_Name (Loc,
12839 Chars => Chars (Gen_Id),
12840 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
12841 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
12842 Set_Entity (Name (N2), Gen_Id);
12844 Rewrite (Name (N1),
12845 Make_Expanded_Name (Loc,
12846 Chars => Chars (Gen_Id),
12847 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
12848 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
12850 Set_Associated_Node (Name (N1), Name (N2));
12851 Set_Associated_Node (Prefix (Name (N1)), Empty);
12852 Set_Associated_Node
12853 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
12854 Set_Etype (Name (N1), Etype (Gen_Id));
12855 end if;
12857 end Save_Global_Defaults;
12859 ----------------------------
12860 -- Save_Global_Descendant --
12861 ----------------------------
12863 procedure Save_Global_Descendant (D : Union_Id) is
12864 N1 : Node_Id;
12866 begin
12867 if D in Node_Range then
12868 if D = Union_Id (Empty) then
12869 null;
12871 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
12872 Save_References (Node_Id (D));
12873 end if;
12875 elsif D in List_Range then
12876 if D = Union_Id (No_List)
12877 or else Is_Empty_List (List_Id (D))
12878 then
12879 null;
12881 else
12882 N1 := First (List_Id (D));
12883 while Present (N1) loop
12884 Save_References (N1);
12885 Next (N1);
12886 end loop;
12887 end if;
12889 -- Element list or other non-node field, nothing to do
12891 else
12892 null;
12893 end if;
12894 end Save_Global_Descendant;
12896 ---------------------
12897 -- Save_References --
12898 ---------------------
12900 -- This is the recursive procedure that does the work once the enclosing
12901 -- generic scope has been established. We have to treat specially a
12902 -- number of node rewritings that are required by semantic processing
12903 -- and which change the kind of nodes in the generic copy: typically
12904 -- constant-folding, replacing an operator node by a string literal, or
12905 -- a selected component by an expanded name. In each of those cases, the
12906 -- transformation is propagated to the generic unit.
12908 procedure Save_References (N : Node_Id) is
12909 Loc : constant Source_Ptr := Sloc (N);
12911 begin
12912 if N = Empty then
12913 null;
12915 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
12916 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
12917 Reset_Entity (N);
12919 elsif Nkind (N) = N_Operator_Symbol
12920 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
12921 then
12922 Change_Operator_Symbol_To_String_Literal (N);
12923 end if;
12925 elsif Nkind (N) in N_Op then
12926 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
12927 if Nkind (N) = N_Op_Concat then
12928 Set_Is_Component_Left_Opnd (N,
12929 Is_Component_Left_Opnd (Get_Associated_Node (N)));
12931 Set_Is_Component_Right_Opnd (N,
12932 Is_Component_Right_Opnd (Get_Associated_Node (N)));
12933 end if;
12935 Reset_Entity (N);
12937 else
12938 -- Node may be transformed into call to a user-defined operator
12940 N2 := Get_Associated_Node (N);
12942 if Nkind (N2) = N_Function_Call then
12943 E := Entity (Name (N2));
12945 if Present (E)
12946 and then Is_Global (E)
12947 then
12948 Set_Etype (N, Etype (N2));
12949 else
12950 Set_Associated_Node (N, Empty);
12951 Set_Etype (N, Empty);
12952 end if;
12954 elsif Nkind_In (N2, N_Integer_Literal,
12955 N_Real_Literal,
12956 N_String_Literal)
12957 then
12958 if Present (Original_Node (N2))
12959 and then Nkind (Original_Node (N2)) = Nkind (N)
12960 then
12962 -- Operation was constant-folded. Whenever possible,
12963 -- recover semantic information from unfolded node,
12964 -- for ASIS use.
12966 Set_Associated_Node (N, Original_Node (N2));
12968 if Nkind (N) = N_Op_Concat then
12969 Set_Is_Component_Left_Opnd (N,
12970 Is_Component_Left_Opnd (Get_Associated_Node (N)));
12971 Set_Is_Component_Right_Opnd (N,
12972 Is_Component_Right_Opnd (Get_Associated_Node (N)));
12973 end if;
12975 Reset_Entity (N);
12977 else
12978 -- If original node is already modified, propagate
12979 -- constant-folding to template.
12981 Rewrite (N, New_Copy (N2));
12982 Set_Analyzed (N, False);
12983 end if;
12985 elsif Nkind (N2) = N_Identifier
12986 and then Ekind (Entity (N2)) = E_Enumeration_Literal
12987 then
12988 -- Same if call was folded into a literal, but in this case
12989 -- retain the entity to avoid spurious ambiguities if it is
12990 -- overloaded at the point of instantiation or inlining.
12992 Rewrite (N, New_Copy (N2));
12993 Set_Analyzed (N, False);
12994 end if;
12995 end if;
12997 -- Complete operands check if node has not been constant-folded
12999 if Nkind (N) in N_Op then
13000 Save_Entity_Descendants (N);
13001 end if;
13003 elsif Nkind (N) = N_Identifier then
13004 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
13006 -- If this is a discriminant reference, always save it. It is
13007 -- used in the instance to find the corresponding discriminant
13008 -- positionally rather than by name.
13010 Set_Original_Discriminant
13011 (N, Original_Discriminant (Get_Associated_Node (N)));
13012 Reset_Entity (N);
13014 else
13015 N2 := Get_Associated_Node (N);
13017 if Nkind (N2) = N_Function_Call then
13018 E := Entity (Name (N2));
13020 -- Name resolves to a call to parameterless function. If
13021 -- original entity is global, mark node as resolved.
13023 if Present (E)
13024 and then Is_Global (E)
13025 then
13026 Set_Etype (N, Etype (N2));
13027 else
13028 Set_Associated_Node (N, Empty);
13029 Set_Etype (N, Empty);
13030 end if;
13032 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
13033 and then Is_Entity_Name (Original_Node (N2))
13034 then
13035 -- Name resolves to named number that is constant-folded,
13036 -- We must preserve the original name for ASIS use, and
13037 -- undo the constant-folding, which will be repeated in
13038 -- each instance.
13040 Set_Associated_Node (N, Original_Node (N2));
13041 Reset_Entity (N);
13043 elsif Nkind (N2) = N_String_Literal then
13045 -- Name resolves to string literal. Perform the same
13046 -- replacement in generic.
13048 Rewrite (N, New_Copy (N2));
13050 elsif Nkind (N2) = N_Explicit_Dereference then
13052 -- An identifier is rewritten as a dereference if it is the
13053 -- prefix in an implicit dereference (call or attribute).
13054 -- The analysis of an instantiation will expand the node
13055 -- again, so we preserve the original tree but link it to
13056 -- the resolved entity in case it is global.
13058 if Is_Entity_Name (Prefix (N2))
13059 and then Present (Entity (Prefix (N2)))
13060 and then Is_Global (Entity (Prefix (N2)))
13061 then
13062 Set_Associated_Node (N, Prefix (N2));
13064 elsif Nkind (Prefix (N2)) = N_Function_Call
13065 and then Is_Global (Entity (Name (Prefix (N2))))
13066 then
13067 Rewrite (N,
13068 Make_Explicit_Dereference (Loc,
13069 Prefix => Make_Function_Call (Loc,
13070 Name =>
13071 New_Occurrence_Of (Entity (Name (Prefix (N2))),
13072 Loc))));
13074 else
13075 Set_Associated_Node (N, Empty);
13076 Set_Etype (N, Empty);
13077 end if;
13079 -- The subtype mark of a nominally unconstrained object is
13080 -- rewritten as a subtype indication using the bounds of the
13081 -- expression. Recover the original subtype mark.
13083 elsif Nkind (N2) = N_Subtype_Indication
13084 and then Is_Entity_Name (Original_Node (N2))
13085 then
13086 Set_Associated_Node (N, Original_Node (N2));
13087 Reset_Entity (N);
13089 else
13090 null;
13091 end if;
13092 end if;
13094 elsif Nkind (N) in N_Entity then
13095 null;
13097 else
13098 declare
13099 Qual : Node_Id := Empty;
13100 Typ : Entity_Id := Empty;
13101 Nam : Node_Id;
13103 use Atree.Unchecked_Access;
13104 -- This code section is part of implementing an untyped tree
13105 -- traversal, so it needs direct access to node fields.
13107 begin
13108 if Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
13109 N2 := Get_Associated_Node (N);
13111 if No (N2) then
13112 Typ := Empty;
13113 else
13114 Typ := Etype (N2);
13116 -- In an instance within a generic, use the name of the
13117 -- actual and not the original generic parameter. If the
13118 -- actual is global in the current generic it must be
13119 -- preserved for its instantiation.
13121 if Nkind (Parent (Typ)) = N_Subtype_Declaration
13122 and then
13123 Present (Generic_Parent_Type (Parent (Typ)))
13124 then
13125 Typ := Base_Type (Typ);
13126 Set_Etype (N2, Typ);
13127 end if;
13128 end if;
13130 if No (N2)
13131 or else No (Typ)
13132 or else not Is_Global (Typ)
13133 then
13134 Set_Associated_Node (N, Empty);
13136 -- If the aggregate is an actual in a call, it has been
13137 -- resolved in the current context, to some local type.
13138 -- The enclosing call may have been disambiguated by the
13139 -- aggregate, and this disambiguation might fail at
13140 -- instantiation time because the type to which the
13141 -- aggregate did resolve is not preserved. In order to
13142 -- preserve some of this information, we wrap the
13143 -- aggregate in a qualified expression, using the id of
13144 -- its type. For further disambiguation we qualify the
13145 -- type name with its scope (if visible) because both
13146 -- id's will have corresponding entities in an instance.
13147 -- This resolves most of the problems with missing type
13148 -- information on aggregates in instances.
13150 if Nkind (N2) = Nkind (N)
13151 and then
13152 Nkind_In (Parent (N2), N_Procedure_Call_Statement,
13153 N_Function_Call)
13154 and then Comes_From_Source (Typ)
13155 then
13156 if Is_Immediately_Visible (Scope (Typ)) then
13157 Nam := Make_Selected_Component (Loc,
13158 Prefix =>
13159 Make_Identifier (Loc, Chars (Scope (Typ))),
13160 Selector_Name =>
13161 Make_Identifier (Loc, Chars (Typ)));
13162 else
13163 Nam := Make_Identifier (Loc, Chars (Typ));
13164 end if;
13166 Qual :=
13167 Make_Qualified_Expression (Loc,
13168 Subtype_Mark => Nam,
13169 Expression => Relocate_Node (N));
13170 end if;
13171 end if;
13173 Save_Global_Descendant (Field1 (N));
13174 Save_Global_Descendant (Field2 (N));
13175 Save_Global_Descendant (Field3 (N));
13176 Save_Global_Descendant (Field5 (N));
13178 if Present (Qual) then
13179 Rewrite (N, Qual);
13180 end if;
13182 -- All other cases than aggregates
13184 else
13185 Save_Global_Descendant (Field1 (N));
13186 Save_Global_Descendant (Field2 (N));
13187 Save_Global_Descendant (Field3 (N));
13188 Save_Global_Descendant (Field4 (N));
13189 Save_Global_Descendant (Field5 (N));
13190 end if;
13191 end;
13192 end if;
13194 -- If a node has aspects, references within their expressions must
13195 -- be saved separately, given that they are not directly in the
13196 -- tree.
13198 if Has_Aspects (N) then
13199 declare
13200 Aspect : Node_Id;
13201 begin
13202 Aspect := First (Aspect_Specifications (N));
13203 while Present (Aspect) loop
13204 Save_Global_References (Expression (Aspect));
13205 Next (Aspect);
13206 end loop;
13207 end;
13208 end if;
13209 end Save_References;
13211 -- Start of processing for Save_Global_References
13213 begin
13214 Gen_Scope := Current_Scope;
13216 -- If the generic unit is a child unit, references to entities in the
13217 -- parent are treated as local, because they will be resolved anew in
13218 -- the context of the instance of the parent.
13220 while Is_Child_Unit (Gen_Scope)
13221 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
13222 loop
13223 Gen_Scope := Scope (Gen_Scope);
13224 end loop;
13226 Save_References (N);
13227 end Save_Global_References;
13229 --------------------------------------
13230 -- Set_Copied_Sloc_For_Inlined_Body --
13231 --------------------------------------
13233 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
13234 begin
13235 Create_Instantiation_Source (N, E, True, S_Adjustment);
13236 end Set_Copied_Sloc_For_Inlined_Body;
13238 ---------------------
13239 -- Set_Instance_Of --
13240 ---------------------
13242 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
13243 begin
13244 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
13245 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
13246 Generic_Renamings.Increment_Last;
13247 end Set_Instance_Of;
13249 --------------------
13250 -- Set_Next_Assoc --
13251 --------------------
13253 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
13254 begin
13255 Generic_Renamings.Table (E).Next_In_HTable := Next;
13256 end Set_Next_Assoc;
13258 -------------------
13259 -- Start_Generic --
13260 -------------------
13262 procedure Start_Generic is
13263 begin
13264 -- ??? More things could be factored out in this routine.
13265 -- Should probably be done at a later stage.
13267 Generic_Flags.Append (Inside_A_Generic);
13268 Inside_A_Generic := True;
13270 Expander_Mode_Save_And_Set (False);
13271 end Start_Generic;
13273 ----------------------
13274 -- Set_Instance_Env --
13275 ----------------------
13277 procedure Set_Instance_Env
13278 (Gen_Unit : Entity_Id;
13279 Act_Unit : Entity_Id)
13281 begin
13282 -- Regardless of the current mode, predefined units are analyzed in the
13283 -- most current Ada mode, and earlier version Ada checks do not apply
13284 -- to predefined units. Nothing needs to be done for non-internal units.
13285 -- These are always analyzed in the current mode.
13287 if Is_Internal_File_Name
13288 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
13289 Renamings_Included => True)
13290 then
13291 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
13292 end if;
13294 Current_Instantiated_Parent :=
13295 (Gen_Id => Gen_Unit,
13296 Act_Id => Act_Unit,
13297 Next_In_HTable => Assoc_Null);
13298 end Set_Instance_Env;
13300 -----------------
13301 -- Switch_View --
13302 -----------------
13304 procedure Switch_View (T : Entity_Id) is
13305 BT : constant Entity_Id := Base_Type (T);
13306 Priv_Elmt : Elmt_Id := No_Elmt;
13307 Priv_Sub : Entity_Id;
13309 begin
13310 -- T may be private but its base type may have been exchanged through
13311 -- some other occurrence, in which case there is nothing to switch
13312 -- besides T itself. Note that a private dependent subtype of a private
13313 -- type might not have been switched even if the base type has been,
13314 -- because of the last branch of Check_Private_View (see comment there).
13316 if not Is_Private_Type (BT) then
13317 Prepend_Elmt (Full_View (T), Exchanged_Views);
13318 Exchange_Declarations (T);
13319 return;
13320 end if;
13322 Priv_Elmt := First_Elmt (Private_Dependents (BT));
13324 if Present (Full_View (BT)) then
13325 Prepend_Elmt (Full_View (BT), Exchanged_Views);
13326 Exchange_Declarations (BT);
13327 end if;
13329 while Present (Priv_Elmt) loop
13330 Priv_Sub := (Node (Priv_Elmt));
13332 -- We avoid flipping the subtype if the Etype of its full view is
13333 -- private because this would result in a malformed subtype. This
13334 -- occurs when the Etype of the subtype full view is the full view of
13335 -- the base type (and since the base types were just switched, the
13336 -- subtype is pointing to the wrong view). This is currently the case
13337 -- for tagged record types, access types (maybe more?) and needs to
13338 -- be resolved. ???
13340 if Present (Full_View (Priv_Sub))
13341 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
13342 then
13343 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
13344 Exchange_Declarations (Priv_Sub);
13345 end if;
13347 Next_Elmt (Priv_Elmt);
13348 end loop;
13349 end Switch_View;
13351 -----------------
13352 -- True_Parent --
13353 -----------------
13355 function True_Parent (N : Node_Id) return Node_Id is
13356 begin
13357 if Nkind (Parent (N)) = N_Subunit then
13358 return Parent (Corresponding_Stub (Parent (N)));
13359 else
13360 return Parent (N);
13361 end if;
13362 end True_Parent;
13364 -----------------------------
13365 -- Valid_Default_Attribute --
13366 -----------------------------
13368 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
13369 Attr_Id : constant Attribute_Id :=
13370 Get_Attribute_Id (Attribute_Name (Def));
13371 T : constant Entity_Id := Entity (Prefix (Def));
13372 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
13373 F : Entity_Id;
13374 Num_F : Int;
13375 OK : Boolean;
13377 begin
13378 if No (T)
13379 or else T = Any_Id
13380 then
13381 return;
13382 end if;
13384 Num_F := 0;
13385 F := First_Formal (Nam);
13386 while Present (F) loop
13387 Num_F := Num_F + 1;
13388 Next_Formal (F);
13389 end loop;
13391 case Attr_Id is
13392 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
13393 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
13394 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
13395 Attribute_Unbiased_Rounding =>
13396 OK := Is_Fun
13397 and then Num_F = 1
13398 and then Is_Floating_Point_Type (T);
13400 when Attribute_Image | Attribute_Pred | Attribute_Succ |
13401 Attribute_Value | Attribute_Wide_Image |
13402 Attribute_Wide_Value =>
13403 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
13405 when Attribute_Max | Attribute_Min =>
13406 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
13408 when Attribute_Input =>
13409 OK := (Is_Fun and then Num_F = 1);
13411 when Attribute_Output | Attribute_Read | Attribute_Write =>
13412 OK := (not Is_Fun and then Num_F = 2);
13414 when others =>
13415 OK := False;
13416 end case;
13418 if not OK then
13419 Error_Msg_N ("attribute reference has wrong profile for subprogram",
13420 Def);
13421 end if;
13422 end Valid_Default_Attribute;
13424 end Sem_Ch12;