PR testsuite/44195
[official-gcc.git] / gcc / ada / sem_ch12.adb
bloba2009c2b66e7cffc03167230de0f2e420fedf6f0
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-2010, 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 Atree; use Atree;
27 with Einfo; use Einfo;
28 with Elists; use Elists;
29 with Errout; use Errout;
30 with Expander; use Expander;
31 with Fname; use Fname;
32 with Fname.UF; use Fname.UF;
33 with Freeze; use Freeze;
34 with Hostparm;
35 with Itypes; use Itypes;
36 with Lib; use Lib;
37 with Lib.Load; use Lib.Load;
38 with Lib.Xref; use Lib.Xref;
39 with Nlists; use Nlists;
40 with Namet; use Namet;
41 with Nmake; use Nmake;
42 with Opt; use Opt;
43 with Rident; use Rident;
44 with Restrict; use Restrict;
45 with Rtsfind; use Rtsfind;
46 with Sem; use Sem;
47 with Sem_Aux; use Sem_Aux;
48 with Sem_Cat; use Sem_Cat;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Ch6; use Sem_Ch6;
51 with Sem_Ch7; use Sem_Ch7;
52 with Sem_Ch8; use Sem_Ch8;
53 with Sem_Ch10; use Sem_Ch10;
54 with Sem_Ch13; use Sem_Ch13;
55 with Sem_Disp; use Sem_Disp;
56 with Sem_Elab; use Sem_Elab;
57 with Sem_Elim; use Sem_Elim;
58 with Sem_Eval; use Sem_Eval;
59 with Sem_Res; use Sem_Res;
60 with Sem_Type; use Sem_Type;
61 with Sem_Util; use Sem_Util;
62 with Sem_Warn; use Sem_Warn;
63 with Stand; use Stand;
64 with Sinfo; use Sinfo;
65 with Sinfo.CN; use Sinfo.CN;
66 with Sinput; use Sinput;
67 with Sinput.L; use Sinput.L;
68 with Snames; use Snames;
69 with Stringt; use Stringt;
70 with Uname; use Uname;
71 with Table;
72 with Tbuild; use Tbuild;
73 with Uintp; use Uintp;
74 with Urealp; use Urealp;
76 with GNAT.HTable;
78 package body Sem_Ch12 is
80 ----------------------------------------------------------
81 -- Implementation of Generic Analysis and Instantiation --
82 ----------------------------------------------------------
84 -- GNAT implements generics by macro expansion. No attempt is made to share
85 -- generic instantiations (for now). Analysis of a generic definition does
86 -- not perform any expansion action, but the expander must be called on the
87 -- tree for each instantiation, because the expansion may of course depend
88 -- on the generic actuals. All of this is best achieved as follows:
90 -- a) Semantic analysis of a generic unit is performed on a copy of the
91 -- tree for the generic unit. All tree modifications that follow analysis
92 -- do not affect the original tree. Links are kept between the original
93 -- tree and the copy, in order to recognize non-local references within
94 -- the generic, and propagate them to each instance (recall that name
95 -- resolution is done on the generic declaration: generics are not really
96 -- macros!). This is summarized in the following diagram:
98 -- .-----------. .----------.
99 -- | semantic |<--------------| generic |
100 -- | copy | | unit |
101 -- | |==============>| |
102 -- |___________| global |__________|
103 -- references | | |
104 -- | | |
105 -- .-----|--|.
106 -- | .-----|---.
107 -- | | .----------.
108 -- | | | generic |
109 -- |__| | |
110 -- |__| instance |
111 -- |__________|
113 -- b) Each instantiation copies the original tree, and inserts into it a
114 -- series of declarations that describe the mapping between generic formals
115 -- and actuals. For example, a generic In OUT parameter is an object
116 -- renaming of the corresponding actual, etc. Generic IN parameters are
117 -- constant declarations.
119 -- c) In order to give the right visibility for these renamings, we use
120 -- a different scheme for package and subprogram instantiations. For
121 -- packages, the list of renamings is inserted into the package
122 -- specification, before the visible declarations of the package. The
123 -- renamings are analyzed before any of the text of the instance, and are
124 -- thus visible at the right place. Furthermore, outside of the instance,
125 -- the generic parameters are visible and denote their corresponding
126 -- actuals.
128 -- For subprograms, we create a container package to hold the renamings
129 -- and the subprogram instance itself. Analysis of the package makes the
130 -- renaming declarations visible to the subprogram. After analyzing the
131 -- package, the defining entity for the subprogram is touched-up so that
132 -- it appears declared in the current scope, and not inside the container
133 -- package.
135 -- If the instantiation is a compilation unit, the container package is
136 -- given the same name as the subprogram instance. This ensures that
137 -- the elaboration procedure called by the binder, using the compilation
138 -- unit name, calls in fact the elaboration procedure for the package.
140 -- Not surprisingly, private types complicate this approach. By saving in
141 -- the original generic object the non-local references, we guarantee that
142 -- the proper entities are referenced at the point of instantiation.
143 -- However, for private types, this by itself does not insure that the
144 -- proper VIEW of the entity is used (the full type may be visible at the
145 -- point of generic definition, but not at instantiation, or vice-versa).
146 -- In order to reference the proper view, we special-case any reference
147 -- to private types in the generic object, by saving both views, one in
148 -- the generic and one in the semantic copy. At time of instantiation, we
149 -- check whether the two views are consistent, and exchange declarations if
150 -- necessary, in order to restore the correct visibility. Similarly, if
151 -- the instance view is private when the generic view was not, we perform
152 -- the exchange. After completing the instantiation, we restore the
153 -- current visibility. The flag Has_Private_View marks identifiers in the
154 -- the generic unit that require checking.
156 -- Visibility within nested generic units requires special handling.
157 -- Consider the following scheme:
159 -- type Global is ... -- outside of generic unit.
160 -- generic ...
161 -- package Outer is
162 -- ...
163 -- type Semi_Global is ... -- global to inner.
165 -- generic ... -- 1
166 -- procedure inner (X1 : Global; X2 : Semi_Global);
168 -- procedure in2 is new inner (...); -- 4
169 -- end Outer;
171 -- package New_Outer is new Outer (...); -- 2
172 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
174 -- The semantic analysis of Outer captures all occurrences of Global.
175 -- The semantic analysis of Inner (at 1) captures both occurrences of
176 -- Global and Semi_Global.
178 -- At point 2 (instantiation of Outer), we also produce a generic copy
179 -- of Inner, even though Inner is, at that point, not being instantiated.
180 -- (This is just part of the semantic analysis of New_Outer).
182 -- Critically, references to Global within Inner must be preserved, while
183 -- references to Semi_Global should not preserved, because they must now
184 -- resolve to an entity within New_Outer. To distinguish between these, we
185 -- use a global variable, Current_Instantiated_Parent, which is set when
186 -- performing a generic copy during instantiation (at 2). This variable is
187 -- used when performing a generic copy that is not an instantiation, but
188 -- that is nested within one, as the occurrence of 1 within 2. The analysis
189 -- of a nested generic only preserves references that are global to the
190 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
191 -- determine whether a reference is external to the given parent.
193 -- The instantiation at point 3 requires no special treatment. The method
194 -- works as well for further nestings of generic units, but of course the
195 -- variable Current_Instantiated_Parent must be stacked because nested
196 -- instantiations can occur, e.g. the occurrence of 4 within 2.
198 -- The instantiation of package and subprogram bodies is handled in a
199 -- similar manner, except that it is delayed until after semantic
200 -- analysis is complete. In this fashion complex cross-dependencies
201 -- between several package declarations and bodies containing generics
202 -- can be compiled which otherwise would diagnose spurious circularities.
204 -- For example, it is possible to compile two packages A and B that
205 -- have the following structure:
207 -- package A is package B is
208 -- generic ... generic ...
209 -- package G_A is package G_B is
211 -- with B; with A;
212 -- package body A is package body B is
213 -- package N_B is new G_B (..) package N_A is new G_A (..)
215 -- The table Pending_Instantiations in package Inline is used to keep
216 -- track of body instantiations that are delayed in this manner. Inline
217 -- handles the actual calls to do the body instantiations. This activity
218 -- is part of Inline, since the processing occurs at the same point, and
219 -- for essentially the same reason, as the handling of inlined routines.
221 ----------------------------------------------
222 -- Detection of Instantiation Circularities --
223 ----------------------------------------------
225 -- If we have a chain of instantiations that is circular, this is static
226 -- error which must be detected at compile time. The detection of these
227 -- circularities is carried out at the point that we insert a generic
228 -- instance spec or body. If there is a circularity, then the analysis of
229 -- the offending spec or body will eventually result in trying to load the
230 -- same unit again, and we detect this problem as we analyze the package
231 -- instantiation for the second time.
233 -- At least in some cases after we have detected the circularity, we get
234 -- into trouble if we try to keep going. The following flag is set if a
235 -- circularity is detected, and used to abandon compilation after the
236 -- messages have been posted.
238 Circularity_Detected : Boolean := False;
239 -- This should really be reset on encountering a new main unit, but in
240 -- practice we are not using multiple main units so it is not critical.
242 -------------------------------------------------
243 -- Formal packages and partial parametrization --
244 -------------------------------------------------
246 -- When compiling a generic, a formal package is a local instantiation. If
247 -- declared with a box, its generic formals are visible in the enclosing
248 -- generic. If declared with a partial list of actuals, those actuals that
249 -- are defaulted (covered by an Others clause, or given an explicit box
250 -- initialization) are also visible in the enclosing generic, while those
251 -- that have a corresponding actual are not.
253 -- In our source model of instantiation, the same visibility must be
254 -- present in the spec and body of an instance: the names of the formals
255 -- that are defaulted must be made visible within the instance, and made
256 -- invisible (hidden) after the instantiation is complete, so that they
257 -- are not accessible outside of the instance.
259 -- In a generic, a formal package is treated like a special instantiation.
260 -- Our Ada95 compiler handled formals with and without box in different
261 -- ways. With partial parametrization, we use a single model for both.
262 -- We create a package declaration that consists of the specification of
263 -- the generic package, and a set of declarations that map the actuals
264 -- into local renamings, just as we do for bona fide instantiations. For
265 -- defaulted parameters and formals with a box, we copy directly the
266 -- declarations of the formal into this local package. The result is a
267 -- a package whose visible declarations may include generic formals. This
268 -- package is only used for type checking and visibility analysis, and
269 -- never reaches the back-end, so it can freely violate the placement
270 -- rules for generic formal declarations.
272 -- The list of declarations (renamings and copies of formals) is built
273 -- by Analyze_Associations, just as for regular instantiations.
275 -- At the point of instantiation, conformance checking must be applied only
276 -- to those parameters that were specified in the formal. We perform this
277 -- checking by creating another internal instantiation, this one including
278 -- only the renamings and the formals (the rest of the package spec is not
279 -- relevant to conformance checking). We can then traverse two lists: the
280 -- list of actuals in the instance that corresponds to the formal package,
281 -- and the list of actuals produced for this bogus instantiation. We apply
282 -- the conformance rules to those actuals that are not defaulted (i.e.
283 -- which still appear as generic formals.
285 -- When we compile an instance body we must make the right parameters
286 -- visible again. The predicate Is_Generic_Formal indicates which of the
287 -- formals should have its Is_Hidden flag reset.
289 -----------------------
290 -- Local subprograms --
291 -----------------------
293 procedure Abandon_Instantiation (N : Node_Id);
294 pragma No_Return (Abandon_Instantiation);
295 -- Posts an error message "instantiation abandoned" at the indicated node
296 -- and then raises the exception Instantiation_Error to do it.
298 procedure Analyze_Formal_Array_Type
299 (T : in out Entity_Id;
300 Def : Node_Id);
301 -- A formal array type is treated like an array type declaration, and
302 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
303 -- in-out, because in the case of an anonymous type the entity is
304 -- actually created in the procedure.
306 -- The following procedures treat other kinds of formal parameters
308 procedure Analyze_Formal_Derived_Interface_Type
309 (N : Node_Id;
310 T : Entity_Id;
311 Def : Node_Id);
313 procedure Analyze_Formal_Derived_Type
314 (N : Node_Id;
315 T : Entity_Id;
316 Def : Node_Id);
318 procedure Analyze_Formal_Interface_Type
319 (N : Node_Id;
320 T : Entity_Id;
321 Def : Node_Id);
323 -- The following subprograms create abbreviated declarations for formal
324 -- scalar types. We introduce an anonymous base of the proper class for
325 -- each of them, and define the formals as constrained first subtypes of
326 -- their bases. The bounds are expressions that are non-static in the
327 -- generic.
329 procedure Analyze_Formal_Decimal_Fixed_Point_Type
330 (T : Entity_Id; Def : Node_Id);
331 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
332 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
333 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
334 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
335 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
336 (T : Entity_Id; Def : Node_Id);
338 procedure Analyze_Formal_Private_Type
339 (N : Node_Id;
340 T : Entity_Id;
341 Def : Node_Id);
342 -- Creates a new private type, which does not require completion
344 procedure Analyze_Generic_Formal_Part (N : Node_Id);
346 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
347 -- Create a new access type with the given designated type
349 function Analyze_Associations
350 (I_Node : Node_Id;
351 Formals : List_Id;
352 F_Copy : List_Id) return List_Id;
353 -- At instantiation time, build the list of associations between formals
354 -- and actuals. Each association becomes a renaming declaration for the
355 -- formal entity. F_Copy is the analyzed list of formals in the generic
356 -- copy. It is used to apply legality checks to the actuals. I_Node is the
357 -- instantiation node itself.
359 procedure Analyze_Subprogram_Instantiation
360 (N : Node_Id;
361 K : Entity_Kind);
363 procedure Build_Instance_Compilation_Unit_Nodes
364 (N : Node_Id;
365 Act_Body : Node_Id;
366 Act_Decl : Node_Id);
367 -- This procedure is used in the case where the generic instance of a
368 -- subprogram body or package body is a library unit. In this case, the
369 -- original library unit node for the generic instantiation must be
370 -- replaced by the resulting generic body, and a link made to a new
371 -- compilation unit node for the generic declaration. The argument N is
372 -- the original generic instantiation. Act_Body and Act_Decl are the body
373 -- and declaration of the instance (either package body and declaration
374 -- nodes or subprogram body and declaration nodes depending on the case).
375 -- On return, the node N has been rewritten with the actual body.
377 procedure Check_Access_Definition (N : Node_Id);
378 -- Subsidiary routine to null exclusion processing. Perform an assertion
379 -- check on Ada version and the presence of an access definition in N.
381 procedure Check_Formal_Packages (P_Id : Entity_Id);
382 -- Apply the following to all formal packages in generic associations
384 procedure Check_Formal_Package_Instance
385 (Formal_Pack : Entity_Id;
386 Actual_Pack : Entity_Id);
387 -- Verify that the actuals of the actual instance match the actuals of
388 -- the template for a formal package that is not declared with a box.
390 procedure Check_Forward_Instantiation (Decl : Node_Id);
391 -- If the generic is a local entity and the corresponding body has not
392 -- been seen yet, flag enclosing packages to indicate that it will be
393 -- elaborated after the generic body. Subprograms declared in the same
394 -- package cannot be inlined by the front-end because front-end inlining
395 -- requires a strict linear order of elaboration.
397 procedure Check_Hidden_Child_Unit
398 (N : Node_Id;
399 Gen_Unit : Entity_Id;
400 Act_Decl_Id : Entity_Id);
401 -- If the generic unit is an implicit child instance within a parent
402 -- instance, we need to make an explicit test that it is not hidden by
403 -- a child instance of the same name and parent.
405 procedure Check_Generic_Actuals
406 (Instance : Entity_Id;
407 Is_Formal_Box : Boolean);
408 -- Similar to previous one. Check the actuals in the instantiation,
409 -- whose views can change between the point of instantiation and the point
410 -- of instantiation of the body. In addition, mark the generic renamings
411 -- as generic actuals, so that they are not compatible with other actuals.
412 -- Recurse on an actual that is a formal package whose declaration has
413 -- a box.
415 function Contains_Instance_Of
416 (Inner : Entity_Id;
417 Outer : Entity_Id;
418 N : Node_Id) return Boolean;
419 -- Inner is instantiated within the generic Outer. Check whether Inner
420 -- directly or indirectly contains an instance of Outer or of one of its
421 -- parents, in the case of a subunit. Each generic unit holds a list of
422 -- the entities instantiated within (at any depth). This procedure
423 -- determines whether the set of such lists contains a cycle, i.e. an
424 -- illegal circular instantiation.
426 function Denotes_Formal_Package
427 (Pack : Entity_Id;
428 On_Exit : Boolean := False;
429 Instance : Entity_Id := Empty) return Boolean;
430 -- Returns True if E is a formal package of an enclosing generic, or
431 -- the actual for such a formal in an enclosing instantiation. If such
432 -- a package is used as a formal in an nested generic, or as an actual
433 -- in a nested instantiation, the visibility of ITS formals should not
434 -- be modified. When called from within Restore_Private_Views, the flag
435 -- On_Exit is true, to indicate that the search for a possible enclosing
436 -- instance should ignore the current one. In that case Instance denotes
437 -- the declaration for which this is an actual. This declaration may be
438 -- an instantiation in the source, or the internal instantiation that
439 -- corresponds to the actual for a formal package.
441 function Find_Actual_Type
442 (Typ : Entity_Id;
443 Gen_Type : Entity_Id) return Entity_Id;
444 -- When validating the actual types of a child instance, check whether
445 -- the formal is a formal type of the parent unit, and retrieve the current
446 -- actual for it. Typ is the entity in the analyzed formal type declaration
447 -- (component or index type of an array type, or designated type of an
448 -- access formal) and Gen_Type is the enclosing analyzed formal array
449 -- or access type. The desired actual may be a formal of a parent, or may
450 -- be declared in a formal package of a parent. In both cases it is a
451 -- generic actual type because it appears within a visible instance.
452 -- Finally, it may be declared in a parent unit without being a formal
453 -- of that unit, in which case it must be retrieved by visibility.
454 -- Ambiguities may still arise if two homonyms are declared in two formal
455 -- packages, and the prefix of the formal type may be needed to resolve
456 -- the ambiguity in the instance ???
458 function In_Same_Declarative_Part
459 (F_Node : Node_Id;
460 Inst : Node_Id) return Boolean;
461 -- True if the instantiation Inst and the given freeze_node F_Node appear
462 -- within the same declarative part, ignoring subunits, but with no inter-
463 -- vening subprograms or concurrent units. If true, the freeze node
464 -- of the instance can be placed after the freeze node of the parent,
465 -- which it itself an instance.
467 function In_Main_Context (E : Entity_Id) return Boolean;
468 -- Check whether an instantiation is in the context of the main unit.
469 -- Used to determine whether its body should be elaborated to allow
470 -- front-end inlining.
472 function Is_Generic_Formal (E : Entity_Id) return Boolean;
473 -- Utility to determine whether a given entity is declared by means of
474 -- of a formal parameter declaration. Used to set properly the visibility
475 -- of generic formals of a generic package declared with a box or with
476 -- partial parametrization.
478 procedure Mark_Context (Inst_Decl : Node_Id; Gen_Decl : Node_Id);
479 -- If the generic unit comes from a different unit, indicate that the
480 -- unit that contains the instance depends on the body that contains
481 -- the generic body. Used to determine a more precise dependency graph
482 -- for use by CodePeer.
484 procedure Set_Instance_Env
485 (Gen_Unit : Entity_Id;
486 Act_Unit : Entity_Id);
487 -- Save current instance on saved environment, to be used to determine
488 -- the global status of entities in nested instances. Part of Save_Env.
489 -- called after verifying that the generic unit is legal for the instance,
490 -- The procedure also examines whether the generic unit is a predefined
491 -- unit, in order to set configuration switches accordingly. As a result
492 -- the procedure must be called after analyzing and freezing the actuals.
494 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
495 -- Associate analyzed generic parameter with corresponding
496 -- instance. Used for semantic checks at instantiation time.
498 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
499 -- Traverse the Exchanged_Views list to see if a type was private
500 -- and has already been flipped during this phase of instantiation.
502 procedure Hide_Current_Scope;
503 -- When instantiating a generic child unit, the parent context must be
504 -- present, but the instance and all entities that may be generated
505 -- must be inserted in the current scope. We leave the current scope
506 -- on the stack, but make its entities invisible to avoid visibility
507 -- problems. This is reversed at the end of the instantiation. This is
508 -- not done for the instantiation of the bodies, which only require the
509 -- instances of the generic parents to be in scope.
511 procedure Install_Body
512 (Act_Body : Node_Id;
513 N : Node_Id;
514 Gen_Body : Node_Id;
515 Gen_Decl : Node_Id);
516 -- If the instantiation happens textually before the body of the generic,
517 -- the instantiation of the body must be analyzed after the generic body,
518 -- and not at the point of instantiation. Such early instantiations can
519 -- happen if the generic and the instance appear in a package declaration
520 -- because the generic body can only appear in the corresponding package
521 -- body. Early instantiations can also appear if generic, instance and
522 -- body are all in the declarative part of a subprogram or entry. Entities
523 -- of packages that are early instantiations are delayed, and their freeze
524 -- node appears after the generic body.
526 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id);
527 -- Insert freeze node at the end of the declarative part that includes the
528 -- instance node N. If N is in the visible part of an enclosing package
529 -- declaration, the freeze node has to be inserted at the end of the
530 -- private declarations, if any.
532 procedure Freeze_Subprogram_Body
533 (Inst_Node : Node_Id;
534 Gen_Body : Node_Id;
535 Pack_Id : Entity_Id);
536 -- The generic body may appear textually after the instance, including
537 -- in the proper body of a stub, or within a different package instance.
538 -- Given that the instance can only be elaborated after the generic, we
539 -- place freeze_nodes for the instance and/or for packages that may enclose
540 -- the instance and the generic, so that the back-end can establish the
541 -- proper order of elaboration.
543 procedure Init_Env;
544 -- Establish environment for subsequent instantiation. Separated from
545 -- Save_Env because data-structures for visibility handling must be
546 -- initialized before call to Check_Generic_Child_Unit.
548 procedure Install_Formal_Packages (Par : Entity_Id);
549 -- Install the visible part of any formal of the parent that is a formal
550 -- package. Note that for the case of a formal package with a box, this
551 -- includes the formal part of the formal package (12.7(10/2)).
553 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
554 -- When compiling an instance of a child unit the parent (which is
555 -- itself an instance) is an enclosing scope that must be made
556 -- immediately visible. This procedure is also used to install the non-
557 -- generic parent of a generic child unit when compiling its body, so
558 -- that full views of types in the parent are made visible.
560 procedure Remove_Parent (In_Body : Boolean := False);
561 -- Reverse effect after instantiation of child is complete
563 procedure Inline_Instance_Body
564 (N : Node_Id;
565 Gen_Unit : Entity_Id;
566 Act_Decl : Node_Id);
567 -- If front-end inlining is requested, instantiate the package body,
568 -- and preserve the visibility of its compilation unit, to insure
569 -- that successive instantiations succeed.
571 -- The functions Instantiate_XXX perform various legality checks and build
572 -- the declarations for instantiated generic parameters. In all of these
573 -- Formal is the entity in the generic unit, Actual is the entity of
574 -- expression in the generic associations, and Analyzed_Formal is the
575 -- formal in the generic copy, which contains the semantic information to
576 -- be used to validate the actual.
578 function Instantiate_Object
579 (Formal : Node_Id;
580 Actual : Node_Id;
581 Analyzed_Formal : Node_Id) return List_Id;
583 function Instantiate_Type
584 (Formal : Node_Id;
585 Actual : Node_Id;
586 Analyzed_Formal : Node_Id;
587 Actual_Decls : List_Id) return List_Id;
589 function Instantiate_Formal_Subprogram
590 (Formal : Node_Id;
591 Actual : Node_Id;
592 Analyzed_Formal : Node_Id) return Node_Id;
594 function Instantiate_Formal_Package
595 (Formal : Node_Id;
596 Actual : Node_Id;
597 Analyzed_Formal : Node_Id) return List_Id;
598 -- If the formal package is declared with a box, special visibility rules
599 -- apply to its formals: they are in the visible part of the package. This
600 -- is true in the declarative region of the formal package, that is to say
601 -- in the enclosing generic or instantiation. For an instantiation, the
602 -- parameters of the formal package are made visible in an explicit step.
603 -- Furthermore, if the actual has a visible USE clause, these formals must
604 -- be made potentially use-visible as well. On exit from the enclosing
605 -- instantiation, the reverse must be done.
607 -- For a formal package declared without a box, there are conformance rules
608 -- that apply to the actuals in the generic declaration and the actuals of
609 -- the actual package in the enclosing instantiation. The simplest way to
610 -- apply these rules is to repeat the instantiation of the formal package
611 -- in the context of the enclosing instance, and compare the generic
612 -- associations of this instantiation with those of the actual package.
613 -- This internal instantiation only needs to contain the renamings of the
614 -- formals: the visible and private declarations themselves need not be
615 -- created.
617 -- In Ada 2005, the formal package may be only partially parametrized. In
618 -- that case the visibility step must make visible those actuals whose
619 -- corresponding formals were given with a box. A final complication
620 -- involves inherited operations from formal derived types, which must be
621 -- visible if the type is.
623 function Is_In_Main_Unit (N : Node_Id) return Boolean;
624 -- Test if given node is in the main unit
626 procedure Load_Parent_Of_Generic
627 (N : Node_Id;
628 Spec : Node_Id;
629 Body_Optional : Boolean := False);
630 -- If the generic appears in a separate non-generic library unit, load the
631 -- corresponding body to retrieve the body of the generic. N is the node
632 -- for the generic instantiation, Spec is the generic package declaration.
634 -- Body_Optional is a flag that indicates that the body is being loaded to
635 -- ensure that temporaries are generated consistently when there are other
636 -- instances in the current declarative part that precede the one being
637 -- loaded. In that case a missing body is acceptable.
639 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
640 -- Add the context clause of the unit containing a generic unit to a
641 -- compilation unit that is, or contains, an instantiation.
643 function Get_Associated_Node (N : Node_Id) return Node_Id;
644 -- In order to propagate semantic information back from the analyzed copy
645 -- to the original generic, we maintain links between selected nodes in the
646 -- generic and their corresponding copies. At the end of generic analysis,
647 -- the routine Save_Global_References traverses the generic tree, examines
648 -- the semantic information, and preserves the links to those nodes that
649 -- contain global information. At instantiation, the information from the
650 -- associated node is placed on the new copy, so that name resolution is
651 -- not repeated.
653 -- Three kinds of source nodes have associated nodes:
655 -- a) those that can reference (denote) entities, that is identifiers,
656 -- character literals, expanded_names, operator symbols, operators,
657 -- and attribute reference nodes. These nodes have an Entity field
658 -- and are the set of nodes that are in N_Has_Entity.
660 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
662 -- c) selected components (N_Selected_Component)
664 -- For the first class, the associated node preserves the entity if it is
665 -- global. If the generic contains nested instantiations, the associated
666 -- node itself has been recopied, and a chain of them must be followed.
668 -- For aggregates, the associated node allows retrieval of the type, which
669 -- may otherwise not appear in the generic. The view of this type may be
670 -- different between generic and instantiation, and the full view can be
671 -- installed before the instantiation is analyzed. For aggregates of type
672 -- extensions, the same view exchange may have to be performed for some of
673 -- the ancestor types, if their view is private at the point of
674 -- instantiation.
676 -- Nodes that are selected components in the parse tree may be rewritten
677 -- as expanded names after resolution, and must be treated as potential
678 -- entity holders, which is why they also have an Associated_Node.
680 -- Nodes that do not come from source, such as freeze nodes, do not appear
681 -- in the generic tree, and need not have an associated node.
683 -- The associated node is stored in the Associated_Node field. Note that
684 -- this field overlaps Entity, which is fine, because the whole point is
685 -- that we don't need or want the normal Entity field in this situation.
687 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
688 -- Within the generic part, entities in the formal package are
689 -- visible. To validate subsequent type declarations, indicate
690 -- the correspondence between the entities in the analyzed formal,
691 -- and the entities in the actual package. There are three packages
692 -- involved in the instantiation of a formal package: the parent
693 -- generic P1 which appears in the generic declaration, the fake
694 -- instantiation P2 which appears in the analyzed generic, and whose
695 -- visible entities may be used in subsequent formals, and the actual
696 -- P3 in the instance. To validate subsequent formals, me indicate
697 -- that the entities in P2 are mapped into those of P3. The mapping of
698 -- entities has to be done recursively for nested packages.
700 procedure Move_Freeze_Nodes
701 (Out_Of : Entity_Id;
702 After : Node_Id;
703 L : List_Id);
704 -- Freeze nodes can be generated in the analysis of a generic unit, but
705 -- will not be seen by the back-end. It is necessary to move those nodes
706 -- to the enclosing scope if they freeze an outer entity. We place them
707 -- at the end of the enclosing generic package, which is semantically
708 -- neutral.
710 procedure Preanalyze_Actuals (N : Node_Id);
711 -- Analyze actuals to perform name resolution. Full resolution is done
712 -- later, when the expected types are known, but names have to be captured
713 -- before installing parents of generics, that are not visible for the
714 -- actuals themselves.
716 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
717 -- Verify that an attribute that appears as the default for a formal
718 -- subprogram is a function or procedure with the correct profile.
720 -------------------------------------------
721 -- Data Structures for Generic Renamings --
722 -------------------------------------------
724 -- The map Generic_Renamings associates generic entities with their
725 -- corresponding actuals. Currently used to validate type instances. It
726 -- will eventually be used for all generic parameters to eliminate the
727 -- need for overload resolution in the instance.
729 type Assoc_Ptr is new Int;
731 Assoc_Null : constant Assoc_Ptr := -1;
733 type Assoc is record
734 Gen_Id : Entity_Id;
735 Act_Id : Entity_Id;
736 Next_In_HTable : Assoc_Ptr;
737 end record;
739 package Generic_Renamings is new Table.Table
740 (Table_Component_Type => Assoc,
741 Table_Index_Type => Assoc_Ptr,
742 Table_Low_Bound => 0,
743 Table_Initial => 10,
744 Table_Increment => 100,
745 Table_Name => "Generic_Renamings");
747 -- Variable to hold enclosing instantiation. When the environment is
748 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
750 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
752 -- Hash table for associations
754 HTable_Size : constant := 37;
755 type HTable_Range is range 0 .. HTable_Size - 1;
757 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
758 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
759 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
760 function Hash (F : Entity_Id) return HTable_Range;
762 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
763 Header_Num => HTable_Range,
764 Element => Assoc,
765 Elmt_Ptr => Assoc_Ptr,
766 Null_Ptr => Assoc_Null,
767 Set_Next => Set_Next_Assoc,
768 Next => Next_Assoc,
769 Key => Entity_Id,
770 Get_Key => Get_Gen_Id,
771 Hash => Hash,
772 Equal => "=");
774 Exchanged_Views : Elist_Id;
775 -- This list holds the private views that have been exchanged during
776 -- instantiation to restore the visibility of the generic declaration.
777 -- (see comments above). After instantiation, the current visibility is
778 -- reestablished by means of a traversal of this list.
780 Hidden_Entities : Elist_Id;
781 -- This list holds the entities of the current scope that are removed
782 -- from immediate visibility when instantiating a child unit. Their
783 -- visibility is restored in Remove_Parent.
785 -- Because instantiations can be recursive, the following must be saved
786 -- on entry and restored on exit from an instantiation (spec or body).
787 -- This is done by the two procedures Save_Env and Restore_Env. For
788 -- package and subprogram instantiations (but not for the body instances)
789 -- the action of Save_Env is done in two steps: Init_Env is called before
790 -- Check_Generic_Child_Unit, because setting the parent instances requires
791 -- that the visibility data structures be properly initialized. Once the
792 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
794 Parent_Unit_Visible : Boolean := False;
795 -- Parent_Unit_Visible is used when the generic is a child unit, and
796 -- indicates whether the ultimate parent of the generic is visible in the
797 -- instantiation environment. It is used to reset the visibility of the
798 -- parent at the end of the instantiation (see Remove_Parent).
800 Instance_Parent_Unit : Entity_Id := Empty;
801 -- This records the ultimate parent unit of an instance of a generic
802 -- child unit and is used in conjunction with Parent_Unit_Visible to
803 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
805 type Instance_Env is record
806 Instantiated_Parent : Assoc;
807 Exchanged_Views : Elist_Id;
808 Hidden_Entities : Elist_Id;
809 Current_Sem_Unit : Unit_Number_Type;
810 Parent_Unit_Visible : Boolean := False;
811 Instance_Parent_Unit : Entity_Id := Empty;
812 Switches : Config_Switches_Type;
813 end record;
815 package Instance_Envs is new Table.Table (
816 Table_Component_Type => Instance_Env,
817 Table_Index_Type => Int,
818 Table_Low_Bound => 0,
819 Table_Initial => 32,
820 Table_Increment => 100,
821 Table_Name => "Instance_Envs");
823 procedure Restore_Private_Views
824 (Pack_Id : Entity_Id;
825 Is_Package : Boolean := True);
826 -- Restore the private views of external types, and unmark the generic
827 -- renamings of actuals, so that they become compatible subtypes again.
828 -- For subprograms, Pack_Id is the package constructed to hold the
829 -- renamings.
831 procedure Switch_View (T : Entity_Id);
832 -- Switch the partial and full views of a type and its private
833 -- dependents (i.e. its subtypes and derived types).
835 ------------------------------------
836 -- Structures for Error Reporting --
837 ------------------------------------
839 Instantiation_Node : Node_Id;
840 -- Used by subprograms that validate instantiation of formal parameters
841 -- where there might be no actual on which to place the error message.
842 -- Also used to locate the instantiation node for generic subunits.
844 Instantiation_Error : exception;
845 -- When there is a semantic error in the generic parameter matching,
846 -- there is no point in continuing the instantiation, because the
847 -- number of cascaded errors is unpredictable. This exception aborts
848 -- the instantiation process altogether.
850 S_Adjustment : Sloc_Adjustment;
851 -- Offset created for each node in an instantiation, in order to keep
852 -- track of the source position of the instantiation in each of its nodes.
853 -- A subsequent semantic error or warning on a construct of the instance
854 -- points to both places: the original generic node, and the point of
855 -- instantiation. See Sinput and Sinput.L for additional details.
857 ------------------------------------------------------------
858 -- Data structure for keeping track when inside a Generic --
859 ------------------------------------------------------------
861 -- The following table is used to save values of the Inside_A_Generic
862 -- flag (see spec of Sem) when they are saved by Start_Generic.
864 package Generic_Flags is new Table.Table (
865 Table_Component_Type => Boolean,
866 Table_Index_Type => Int,
867 Table_Low_Bound => 0,
868 Table_Initial => 32,
869 Table_Increment => 200,
870 Table_Name => "Generic_Flags");
872 ---------------------------
873 -- Abandon_Instantiation --
874 ---------------------------
876 procedure Abandon_Instantiation (N : Node_Id) is
877 begin
878 Error_Msg_N ("\instantiation abandoned!", N);
879 raise Instantiation_Error;
880 end Abandon_Instantiation;
882 --------------------------
883 -- Analyze_Associations --
884 --------------------------
886 function Analyze_Associations
887 (I_Node : Node_Id;
888 Formals : List_Id;
889 F_Copy : List_Id) return List_Id
892 Actual_Types : constant Elist_Id := New_Elmt_List;
893 Assoc : constant List_Id := New_List;
894 Default_Actuals : constant Elist_Id := New_Elmt_List;
895 Gen_Unit : constant Entity_Id :=
896 Defining_Entity (Parent (F_Copy));
898 Actuals : List_Id;
899 Actual : Node_Id;
900 Formal : Node_Id;
901 Next_Formal : Node_Id;
902 Temp_Formal : Node_Id;
903 Analyzed_Formal : Node_Id;
904 Match : Node_Id;
905 Named : Node_Id;
906 First_Named : Node_Id := Empty;
908 Default_Formals : constant List_Id := New_List;
909 -- If an Others_Choice is present, some of the formals may be defaulted.
910 -- To simplify the treatment of visibility in an instance, we introduce
911 -- individual defaults for each such formal. These defaults are
912 -- appended to the list of associations and replace the Others_Choice.
914 Found_Assoc : Node_Id;
915 -- Association for the current formal being match. Empty if there are
916 -- no remaining actuals, or if there is no named association with the
917 -- name of the formal.
919 Is_Named_Assoc : Boolean;
920 Num_Matched : Int := 0;
921 Num_Actuals : Int := 0;
923 Others_Present : Boolean := False;
924 -- In Ada 2005, indicates partial parametrization of a formal
925 -- package. As usual an other association must be last in the list.
927 function Matching_Actual
928 (F : Entity_Id;
929 A_F : Entity_Id) return Node_Id;
930 -- Find actual that corresponds to a given a formal parameter. If the
931 -- actuals are positional, return the next one, if any. If the actuals
932 -- are named, scan the parameter associations to find the right one.
933 -- A_F is the corresponding entity in the analyzed generic,which is
934 -- placed on the selector name for ASIS use.
936 -- In Ada 2005, a named association may be given with a box, in which
937 -- case Matching_Actual sets Found_Assoc to the generic association,
938 -- but return Empty for the actual itself. In this case the code below
939 -- creates a corresponding declaration for the formal.
941 function Partial_Parametrization return Boolean;
942 -- Ada 2005: if no match is found for a given formal, check if the
943 -- association for it includes a box, or whether the associations
944 -- include an Others clause.
946 procedure Process_Default (F : Entity_Id);
947 -- Add a copy of the declaration of generic formal F to the list of
948 -- associations, and add an explicit box association for F if there
949 -- is none yet, and the default comes from an Others_Choice.
951 procedure Set_Analyzed_Formal;
952 -- Find the node in the generic copy that corresponds to a given formal.
953 -- The semantic information on this node is used to perform legality
954 -- checks on the actuals. Because semantic analysis can introduce some
955 -- anonymous entities or modify the declaration node itself, the
956 -- correspondence between the two lists is not one-one. In addition to
957 -- anonymous types, the presence a formal equality will introduce an
958 -- implicit declaration for the corresponding inequality.
960 ---------------------
961 -- Matching_Actual --
962 ---------------------
964 function Matching_Actual
965 (F : Entity_Id;
966 A_F : Entity_Id) return Node_Id
968 Prev : Node_Id;
969 Act : Node_Id;
971 begin
972 Is_Named_Assoc := False;
974 -- End of list of purely positional parameters
976 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
977 Found_Assoc := Empty;
978 Act := Empty;
980 -- Case of positional parameter corresponding to current formal
982 elsif No (Selector_Name (Actual)) then
983 Found_Assoc := Actual;
984 Act := Explicit_Generic_Actual_Parameter (Actual);
985 Num_Matched := Num_Matched + 1;
986 Next (Actual);
988 -- Otherwise scan list of named actuals to find the one with the
989 -- desired name. All remaining actuals have explicit names.
991 else
992 Is_Named_Assoc := True;
993 Found_Assoc := Empty;
994 Act := Empty;
995 Prev := Empty;
997 while Present (Actual) loop
998 if Chars (Selector_Name (Actual)) = Chars (F) then
999 Set_Entity (Selector_Name (Actual), A_F);
1000 Set_Etype (Selector_Name (Actual), Etype (A_F));
1001 Generate_Reference (A_F, Selector_Name (Actual));
1002 Found_Assoc := Actual;
1003 Act := Explicit_Generic_Actual_Parameter (Actual);
1004 Num_Matched := Num_Matched + 1;
1005 exit;
1006 end if;
1008 Prev := Actual;
1009 Next (Actual);
1010 end loop;
1012 -- Reset for subsequent searches. In most cases the named
1013 -- associations are in order. If they are not, we reorder them
1014 -- to avoid scanning twice the same actual. This is not just a
1015 -- question of efficiency: there may be multiple defaults with
1016 -- boxes that have the same name. In a nested instantiation we
1017 -- insert actuals for those defaults, and cannot rely on their
1018 -- names to disambiguate them.
1020 if Actual = First_Named then
1021 Next (First_Named);
1023 elsif Present (Actual) then
1024 Insert_Before (First_Named, Remove_Next (Prev));
1025 end if;
1027 Actual := First_Named;
1028 end if;
1030 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1031 Set_Used_As_Generic_Actual (Entity (Act));
1032 end if;
1034 return Act;
1035 end Matching_Actual;
1037 -----------------------------
1038 -- Partial_Parametrization --
1039 -----------------------------
1041 function Partial_Parametrization return Boolean is
1042 begin
1043 return Others_Present
1044 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1045 end Partial_Parametrization;
1047 ---------------------
1048 -- Process_Default --
1049 ---------------------
1051 procedure Process_Default (F : Entity_Id) is
1052 Loc : constant Source_Ptr := Sloc (I_Node);
1053 F_Id : constant Entity_Id := Defining_Entity (F);
1054 Decl : Node_Id;
1055 Default : Node_Id;
1056 Id : Entity_Id;
1058 begin
1059 -- Append copy of formal declaration to associations, and create new
1060 -- defining identifier for it.
1062 Decl := New_Copy_Tree (F);
1063 Id := Make_Defining_Identifier (Sloc (F_Id), Chars => Chars (F_Id));
1065 if Nkind (F) in N_Formal_Subprogram_Declaration then
1066 Set_Defining_Unit_Name (Specification (Decl), Id);
1068 else
1069 Set_Defining_Identifier (Decl, Id);
1070 end if;
1072 Append (Decl, Assoc);
1074 if No (Found_Assoc) then
1075 Default :=
1076 Make_Generic_Association (Loc,
1077 Selector_Name => New_Occurrence_Of (Id, Loc),
1078 Explicit_Generic_Actual_Parameter => Empty);
1079 Set_Box_Present (Default);
1080 Append (Default, Default_Formals);
1081 end if;
1082 end Process_Default;
1084 -------------------------
1085 -- Set_Analyzed_Formal --
1086 -------------------------
1088 procedure Set_Analyzed_Formal is
1089 Kind : Node_Kind;
1091 begin
1092 while Present (Analyzed_Formal) loop
1093 Kind := Nkind (Analyzed_Formal);
1095 case Nkind (Formal) is
1097 when N_Formal_Subprogram_Declaration =>
1098 exit when Kind in N_Formal_Subprogram_Declaration
1099 and then
1100 Chars
1101 (Defining_Unit_Name (Specification (Formal))) =
1102 Chars
1103 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1105 when N_Formal_Package_Declaration =>
1106 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1107 N_Generic_Package_Declaration,
1108 N_Package_Declaration);
1110 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1112 when others =>
1114 -- Skip freeze nodes, and nodes inserted to replace
1115 -- unrecognized pragmas.
1117 exit when
1118 Kind not in N_Formal_Subprogram_Declaration
1119 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1120 N_Freeze_Entity,
1121 N_Null_Statement,
1122 N_Itype_Reference)
1123 and then Chars (Defining_Identifier (Formal)) =
1124 Chars (Defining_Identifier (Analyzed_Formal));
1125 end case;
1127 Next (Analyzed_Formal);
1128 end loop;
1129 end Set_Analyzed_Formal;
1131 -- Start of processing for Analyze_Associations
1133 begin
1134 Actuals := Generic_Associations (I_Node);
1136 if Present (Actuals) then
1138 -- Check for an Others choice, indicating a partial parametrization
1139 -- for a formal package.
1141 Actual := First (Actuals);
1142 while Present (Actual) loop
1143 if Nkind (Actual) = N_Others_Choice then
1144 Others_Present := True;
1146 if Present (Next (Actual)) then
1147 Error_Msg_N ("others must be last association", Actual);
1148 end if;
1150 -- This subprogram is used both for formal packages and for
1151 -- instantiations. For the latter, associations must all be
1152 -- explicit.
1154 if Nkind (I_Node) /= N_Formal_Package_Declaration
1155 and then Comes_From_Source (I_Node)
1156 then
1157 Error_Msg_N
1158 ("others association not allowed in an instance",
1159 Actual);
1160 end if;
1162 -- In any case, nothing to do after the others association
1164 exit;
1166 elsif Box_Present (Actual)
1167 and then Comes_From_Source (I_Node)
1168 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1169 then
1170 Error_Msg_N
1171 ("box association not allowed in an instance", Actual);
1172 end if;
1174 Next (Actual);
1175 end loop;
1177 -- If named associations are present, save first named association
1178 -- (it may of course be Empty) to facilitate subsequent name search.
1180 First_Named := First (Actuals);
1181 while Present (First_Named)
1182 and then Nkind (First_Named) /= N_Others_Choice
1183 and then No (Selector_Name (First_Named))
1184 loop
1185 Num_Actuals := Num_Actuals + 1;
1186 Next (First_Named);
1187 end loop;
1188 end if;
1190 Named := First_Named;
1191 while Present (Named) loop
1192 if Nkind (Named) /= N_Others_Choice
1193 and then No (Selector_Name (Named))
1194 then
1195 Error_Msg_N ("invalid positional actual after named one", Named);
1196 Abandon_Instantiation (Named);
1197 end if;
1199 -- A named association may lack an actual parameter, if it was
1200 -- introduced for a default subprogram that turns out to be local
1201 -- to the outer instantiation.
1203 if Nkind (Named) /= N_Others_Choice
1204 and then Present (Explicit_Generic_Actual_Parameter (Named))
1205 then
1206 Num_Actuals := Num_Actuals + 1;
1207 end if;
1209 Next (Named);
1210 end loop;
1212 if Present (Formals) then
1213 Formal := First_Non_Pragma (Formals);
1214 Analyzed_Formal := First_Non_Pragma (F_Copy);
1216 if Present (Actuals) then
1217 Actual := First (Actuals);
1219 -- All formals should have default values
1221 else
1222 Actual := Empty;
1223 end if;
1225 while Present (Formal) loop
1226 Set_Analyzed_Formal;
1227 Next_Formal := Next_Non_Pragma (Formal);
1229 case Nkind (Formal) is
1230 when N_Formal_Object_Declaration =>
1231 Match :=
1232 Matching_Actual (
1233 Defining_Identifier (Formal),
1234 Defining_Identifier (Analyzed_Formal));
1236 if No (Match) and then Partial_Parametrization then
1237 Process_Default (Formal);
1238 else
1239 Append_List
1240 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1241 Assoc);
1242 end if;
1244 when N_Formal_Type_Declaration =>
1245 Match :=
1246 Matching_Actual (
1247 Defining_Identifier (Formal),
1248 Defining_Identifier (Analyzed_Formal));
1250 if No (Match) then
1251 if Partial_Parametrization then
1252 Process_Default (Formal);
1254 else
1255 Error_Msg_Sloc := Sloc (Gen_Unit);
1256 Error_Msg_NE
1257 ("missing actual&",
1258 Instantiation_Node,
1259 Defining_Identifier (Formal));
1260 Error_Msg_NE ("\in instantiation of & declared#",
1261 Instantiation_Node, Gen_Unit);
1262 Abandon_Instantiation (Instantiation_Node);
1263 end if;
1265 else
1266 Analyze (Match);
1267 Append_List
1268 (Instantiate_Type
1269 (Formal, Match, Analyzed_Formal, Assoc),
1270 Assoc);
1272 -- An instantiation is a freeze point for the actuals,
1273 -- unless this is a rewritten formal package.
1275 if Nkind (I_Node) /= N_Formal_Package_Declaration then
1276 Append_Elmt (Entity (Match), Actual_Types);
1277 end if;
1278 end if;
1280 -- A remote access-to-class-wide type must not be an
1281 -- actual parameter for a generic formal of an access
1282 -- type (E.2.2 (17)).
1284 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1285 and then
1286 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1287 N_Access_To_Object_Definition
1288 then
1289 Validate_Remote_Access_To_Class_Wide_Type (Match);
1290 end if;
1292 when N_Formal_Subprogram_Declaration =>
1293 Match :=
1294 Matching_Actual (
1295 Defining_Unit_Name (Specification (Formal)),
1296 Defining_Unit_Name (Specification (Analyzed_Formal)));
1298 -- If the formal subprogram has the same name as another
1299 -- formal subprogram of the generic, then a named
1300 -- association is illegal (12.3(9)). Exclude named
1301 -- associations that are generated for a nested instance.
1303 if Present (Match)
1304 and then Is_Named_Assoc
1305 and then Comes_From_Source (Found_Assoc)
1306 then
1307 Temp_Formal := First (Formals);
1308 while Present (Temp_Formal) loop
1309 if Nkind (Temp_Formal) in
1310 N_Formal_Subprogram_Declaration
1311 and then Temp_Formal /= Formal
1312 and then
1313 Chars (Selector_Name (Found_Assoc)) =
1314 Chars (Defining_Unit_Name
1315 (Specification (Temp_Formal)))
1316 then
1317 Error_Msg_N
1318 ("name not allowed for overloaded formal",
1319 Found_Assoc);
1320 Abandon_Instantiation (Instantiation_Node);
1321 end if;
1323 Next (Temp_Formal);
1324 end loop;
1325 end if;
1327 -- If there is no corresponding actual, this may be case of
1328 -- partial parametrization, or else the formal has a default
1329 -- or a box.
1331 if No (Match)
1332 and then Partial_Parametrization
1333 then
1334 Process_Default (Formal);
1335 else
1336 Append_To (Assoc,
1337 Instantiate_Formal_Subprogram
1338 (Formal, Match, Analyzed_Formal));
1339 end if;
1341 -- If this is a nested generic, preserve default for later
1342 -- instantiations.
1344 if No (Match)
1345 and then Box_Present (Formal)
1346 then
1347 Append_Elmt
1348 (Defining_Unit_Name (Specification (Last (Assoc))),
1349 Default_Actuals);
1350 end if;
1352 when N_Formal_Package_Declaration =>
1353 Match :=
1354 Matching_Actual (
1355 Defining_Identifier (Formal),
1356 Defining_Identifier (Original_Node (Analyzed_Formal)));
1358 if No (Match) then
1359 if Partial_Parametrization then
1360 Process_Default (Formal);
1362 else
1363 Error_Msg_Sloc := Sloc (Gen_Unit);
1364 Error_Msg_NE
1365 ("missing actual&",
1366 Instantiation_Node, Defining_Identifier (Formal));
1367 Error_Msg_NE ("\in instantiation of & declared#",
1368 Instantiation_Node, Gen_Unit);
1370 Abandon_Instantiation (Instantiation_Node);
1371 end if;
1373 else
1374 Analyze (Match);
1375 Append_List
1376 (Instantiate_Formal_Package
1377 (Formal, Match, Analyzed_Formal),
1378 Assoc);
1379 end if;
1381 -- For use type and use package appearing in the generic part,
1382 -- we have already copied them, so we can just move them where
1383 -- they belong (we mustn't recopy them since this would mess up
1384 -- the Sloc values).
1386 when N_Use_Package_Clause |
1387 N_Use_Type_Clause =>
1388 if Nkind (Original_Node (I_Node)) =
1389 N_Formal_Package_Declaration
1390 then
1391 Append (New_Copy_Tree (Formal), Assoc);
1392 else
1393 Remove (Formal);
1394 Append (Formal, Assoc);
1395 end if;
1397 when others =>
1398 raise Program_Error;
1400 end case;
1402 Formal := Next_Formal;
1403 Next_Non_Pragma (Analyzed_Formal);
1404 end loop;
1406 if Num_Actuals > Num_Matched then
1407 Error_Msg_Sloc := Sloc (Gen_Unit);
1409 if Present (Selector_Name (Actual)) then
1410 Error_Msg_NE
1411 ("unmatched actual&",
1412 Actual, Selector_Name (Actual));
1413 Error_Msg_NE ("\in instantiation of& declared#",
1414 Actual, Gen_Unit);
1415 else
1416 Error_Msg_NE
1417 ("unmatched actual in instantiation of& declared#",
1418 Actual, Gen_Unit);
1419 end if;
1420 end if;
1422 elsif Present (Actuals) then
1423 Error_Msg_N
1424 ("too many actuals in generic instantiation", Instantiation_Node);
1425 end if;
1427 declare
1428 Elmt : Elmt_Id := First_Elmt (Actual_Types);
1429 begin
1430 while Present (Elmt) loop
1431 Freeze_Before (I_Node, Node (Elmt));
1432 Next_Elmt (Elmt);
1433 end loop;
1434 end;
1436 -- If there are default subprograms, normalize the tree by adding
1437 -- explicit associations for them. This is required if the instance
1438 -- appears within a generic.
1440 declare
1441 Elmt : Elmt_Id;
1442 Subp : Entity_Id;
1443 New_D : Node_Id;
1445 begin
1446 Elmt := First_Elmt (Default_Actuals);
1447 while Present (Elmt) loop
1448 if No (Actuals) then
1449 Actuals := New_List;
1450 Set_Generic_Associations (I_Node, Actuals);
1451 end if;
1453 Subp := Node (Elmt);
1454 New_D :=
1455 Make_Generic_Association (Sloc (Subp),
1456 Selector_Name => New_Occurrence_Of (Subp, Sloc (Subp)),
1457 Explicit_Generic_Actual_Parameter =>
1458 New_Occurrence_Of (Subp, Sloc (Subp)));
1459 Mark_Rewrite_Insertion (New_D);
1460 Append_To (Actuals, New_D);
1461 Next_Elmt (Elmt);
1462 end loop;
1463 end;
1465 -- If this is a formal package, normalize the parameter list by adding
1466 -- explicit box associations for the formals that are covered by an
1467 -- Others_Choice.
1469 if not Is_Empty_List (Default_Formals) then
1470 Append_List (Default_Formals, Formals);
1471 end if;
1473 return Assoc;
1474 end Analyze_Associations;
1476 -------------------------------
1477 -- Analyze_Formal_Array_Type --
1478 -------------------------------
1480 procedure Analyze_Formal_Array_Type
1481 (T : in out Entity_Id;
1482 Def : Node_Id)
1484 DSS : Node_Id;
1486 begin
1487 -- Treated like a non-generic array declaration, with additional
1488 -- semantic checks.
1490 Enter_Name (T);
1492 if Nkind (Def) = N_Constrained_Array_Definition then
1493 DSS := First (Discrete_Subtype_Definitions (Def));
1494 while Present (DSS) loop
1495 if Nkind_In (DSS, N_Subtype_Indication,
1496 N_Range,
1497 N_Attribute_Reference)
1498 then
1499 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1500 end if;
1502 Next (DSS);
1503 end loop;
1504 end if;
1506 Array_Type_Declaration (T, Def);
1507 Set_Is_Generic_Type (Base_Type (T));
1509 if Ekind (Component_Type (T)) = E_Incomplete_Type
1510 and then No (Full_View (Component_Type (T)))
1511 then
1512 Error_Msg_N ("premature usage of incomplete type", Def);
1514 -- Check that range constraint is not allowed on the component type
1515 -- of a generic formal array type (AARM 12.5.3(3))
1517 elsif Is_Internal (Component_Type (T))
1518 and then Present (Subtype_Indication (Component_Definition (Def)))
1519 and then Nkind (Original_Node
1520 (Subtype_Indication (Component_Definition (Def)))) =
1521 N_Subtype_Indication
1522 then
1523 Error_Msg_N
1524 ("in a formal, a subtype indication can only be "
1525 & "a subtype mark (RM 12.5.3(3))",
1526 Subtype_Indication (Component_Definition (Def)));
1527 end if;
1529 end Analyze_Formal_Array_Type;
1531 ---------------------------------------------
1532 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1533 ---------------------------------------------
1535 -- As for other generic types, we create a valid type representation with
1536 -- legal but arbitrary attributes, whose values are never considered
1537 -- static. For all scalar types we introduce an anonymous base type, with
1538 -- the same attributes. We choose the corresponding integer type to be
1539 -- Standard_Integer.
1541 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1542 (T : Entity_Id;
1543 Def : Node_Id)
1545 Loc : constant Source_Ptr := Sloc (Def);
1546 Base : constant Entity_Id :=
1547 New_Internal_Entity
1548 (E_Decimal_Fixed_Point_Type,
1549 Current_Scope, Sloc (Def), 'G');
1550 Int_Base : constant Entity_Id := Standard_Integer;
1551 Delta_Val : constant Ureal := Ureal_1;
1552 Digs_Val : constant Uint := Uint_6;
1554 begin
1555 Enter_Name (T);
1557 Set_Etype (Base, Base);
1558 Set_Size_Info (Base, Int_Base);
1559 Set_RM_Size (Base, RM_Size (Int_Base));
1560 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1561 Set_Digits_Value (Base, Digs_Val);
1562 Set_Delta_Value (Base, Delta_Val);
1563 Set_Small_Value (Base, Delta_Val);
1564 Set_Scalar_Range (Base,
1565 Make_Range (Loc,
1566 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1567 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1569 Set_Is_Generic_Type (Base);
1570 Set_Parent (Base, Parent (Def));
1572 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1573 Set_Etype (T, Base);
1574 Set_Size_Info (T, Int_Base);
1575 Set_RM_Size (T, RM_Size (Int_Base));
1576 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1577 Set_Digits_Value (T, Digs_Val);
1578 Set_Delta_Value (T, Delta_Val);
1579 Set_Small_Value (T, Delta_Val);
1580 Set_Scalar_Range (T, Scalar_Range (Base));
1581 Set_Is_Constrained (T);
1583 Check_Restriction (No_Fixed_Point, Def);
1584 end Analyze_Formal_Decimal_Fixed_Point_Type;
1586 -------------------------------------------
1587 -- Analyze_Formal_Derived_Interface_Type --
1588 -------------------------------------------
1590 procedure Analyze_Formal_Derived_Interface_Type
1591 (N : Node_Id;
1592 T : Entity_Id;
1593 Def : Node_Id)
1595 Loc : constant Source_Ptr := Sloc (Def);
1597 begin
1598 -- Rewrite as a type declaration of a derived type. This ensures that
1599 -- the interface list and primitive operations are properly captured.
1601 Rewrite (N,
1602 Make_Full_Type_Declaration (Loc,
1603 Defining_Identifier => T,
1604 Type_Definition => Def));
1605 Analyze (N);
1606 Set_Is_Generic_Type (T);
1607 end Analyze_Formal_Derived_Interface_Type;
1609 ---------------------------------
1610 -- Analyze_Formal_Derived_Type --
1611 ---------------------------------
1613 procedure Analyze_Formal_Derived_Type
1614 (N : Node_Id;
1615 T : Entity_Id;
1616 Def : Node_Id)
1618 Loc : constant Source_Ptr := Sloc (Def);
1619 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
1620 New_N : Node_Id;
1622 begin
1623 Set_Is_Generic_Type (T);
1625 if Private_Present (Def) then
1626 New_N :=
1627 Make_Private_Extension_Declaration (Loc,
1628 Defining_Identifier => T,
1629 Discriminant_Specifications => Discriminant_Specifications (N),
1630 Unknown_Discriminants_Present => Unk_Disc,
1631 Subtype_Indication => Subtype_Mark (Def),
1632 Interface_List => Interface_List (Def));
1634 Set_Abstract_Present (New_N, Abstract_Present (Def));
1635 Set_Limited_Present (New_N, Limited_Present (Def));
1636 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
1638 else
1639 New_N :=
1640 Make_Full_Type_Declaration (Loc,
1641 Defining_Identifier => T,
1642 Discriminant_Specifications =>
1643 Discriminant_Specifications (Parent (T)),
1644 Type_Definition =>
1645 Make_Derived_Type_Definition (Loc,
1646 Subtype_Indication => Subtype_Mark (Def)));
1648 Set_Abstract_Present
1649 (Type_Definition (New_N), Abstract_Present (Def));
1650 Set_Limited_Present
1651 (Type_Definition (New_N), Limited_Present (Def));
1652 end if;
1654 Rewrite (N, New_N);
1655 Analyze (N);
1657 if Unk_Disc then
1658 if not Is_Composite_Type (T) then
1659 Error_Msg_N
1660 ("unknown discriminants not allowed for elementary types", N);
1661 else
1662 Set_Has_Unknown_Discriminants (T);
1663 Set_Is_Constrained (T, False);
1664 end if;
1665 end if;
1667 -- If the parent type has a known size, so does the formal, which makes
1668 -- legal representation clauses that involve the formal.
1670 Set_Size_Known_At_Compile_Time
1671 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
1672 end Analyze_Formal_Derived_Type;
1674 ----------------------------------
1675 -- Analyze_Formal_Discrete_Type --
1676 ----------------------------------
1678 -- The operations defined for a discrete types are those of an enumeration
1679 -- type. The size is set to an arbitrary value, for use in analyzing the
1680 -- generic unit.
1682 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
1683 Loc : constant Source_Ptr := Sloc (Def);
1684 Lo : Node_Id;
1685 Hi : Node_Id;
1687 Base : constant Entity_Id :=
1688 New_Internal_Entity
1689 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1690 begin
1691 Enter_Name (T);
1692 Set_Ekind (T, E_Enumeration_Subtype);
1693 Set_Etype (T, Base);
1694 Init_Size (T, 8);
1695 Init_Alignment (T);
1696 Set_Is_Generic_Type (T);
1697 Set_Is_Constrained (T);
1699 -- For semantic analysis, the bounds of the type must be set to some
1700 -- non-static value. The simplest is to create attribute nodes for those
1701 -- bounds, that refer to the type itself. These bounds are never
1702 -- analyzed but serve as place-holders.
1704 Lo :=
1705 Make_Attribute_Reference (Loc,
1706 Attribute_Name => Name_First,
1707 Prefix => New_Reference_To (T, Loc));
1708 Set_Etype (Lo, T);
1710 Hi :=
1711 Make_Attribute_Reference (Loc,
1712 Attribute_Name => Name_Last,
1713 Prefix => New_Reference_To (T, Loc));
1714 Set_Etype (Hi, T);
1716 Set_Scalar_Range (T,
1717 Make_Range (Loc,
1718 Low_Bound => Lo,
1719 High_Bound => Hi));
1721 Set_Ekind (Base, E_Enumeration_Type);
1722 Set_Etype (Base, Base);
1723 Init_Size (Base, 8);
1724 Init_Alignment (Base);
1725 Set_Is_Generic_Type (Base);
1726 Set_Scalar_Range (Base, Scalar_Range (T));
1727 Set_Parent (Base, Parent (Def));
1728 end Analyze_Formal_Discrete_Type;
1730 ----------------------------------
1731 -- Analyze_Formal_Floating_Type --
1732 ---------------------------------
1734 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
1735 Base : constant Entity_Id :=
1736 New_Internal_Entity
1737 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1739 begin
1740 -- The various semantic attributes are taken from the predefined type
1741 -- Float, just so that all of them are initialized. Their values are
1742 -- never used because no constant folding or expansion takes place in
1743 -- the generic itself.
1745 Enter_Name (T);
1746 Set_Ekind (T, E_Floating_Point_Subtype);
1747 Set_Etype (T, Base);
1748 Set_Size_Info (T, (Standard_Float));
1749 Set_RM_Size (T, RM_Size (Standard_Float));
1750 Set_Digits_Value (T, Digits_Value (Standard_Float));
1751 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
1752 Set_Is_Constrained (T);
1754 Set_Is_Generic_Type (Base);
1755 Set_Etype (Base, Base);
1756 Set_Size_Info (Base, (Standard_Float));
1757 Set_RM_Size (Base, RM_Size (Standard_Float));
1758 Set_Digits_Value (Base, Digits_Value (Standard_Float));
1759 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
1760 Set_Parent (Base, Parent (Def));
1762 Check_Restriction (No_Floating_Point, Def);
1763 end Analyze_Formal_Floating_Type;
1765 -----------------------------------
1766 -- Analyze_Formal_Interface_Type;--
1767 -----------------------------------
1769 procedure Analyze_Formal_Interface_Type
1770 (N : Node_Id;
1771 T : Entity_Id;
1772 Def : Node_Id)
1774 Loc : constant Source_Ptr := Sloc (N);
1775 New_N : Node_Id;
1777 begin
1778 New_N :=
1779 Make_Full_Type_Declaration (Loc,
1780 Defining_Identifier => T,
1781 Type_Definition => Def);
1783 Rewrite (N, New_N);
1784 Analyze (N);
1785 Set_Is_Generic_Type (T);
1786 end Analyze_Formal_Interface_Type;
1788 ---------------------------------
1789 -- Analyze_Formal_Modular_Type --
1790 ---------------------------------
1792 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
1793 begin
1794 -- Apart from their entity kind, generic modular types are treated like
1795 -- signed integer types, and have the same attributes.
1797 Analyze_Formal_Signed_Integer_Type (T, Def);
1798 Set_Ekind (T, E_Modular_Integer_Subtype);
1799 Set_Ekind (Etype (T), E_Modular_Integer_Type);
1801 end Analyze_Formal_Modular_Type;
1803 ---------------------------------------
1804 -- Analyze_Formal_Object_Declaration --
1805 ---------------------------------------
1807 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
1808 E : constant Node_Id := Default_Expression (N);
1809 Id : constant Node_Id := Defining_Identifier (N);
1810 K : Entity_Kind;
1811 T : Node_Id;
1813 begin
1814 Enter_Name (Id);
1816 -- Determine the mode of the formal object
1818 if Out_Present (N) then
1819 K := E_Generic_In_Out_Parameter;
1821 if not In_Present (N) then
1822 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
1823 end if;
1825 else
1826 K := E_Generic_In_Parameter;
1827 end if;
1829 if Present (Subtype_Mark (N)) then
1830 Find_Type (Subtype_Mark (N));
1831 T := Entity (Subtype_Mark (N));
1833 -- Verify that there is no redundant null exclusion
1835 if Null_Exclusion_Present (N) then
1836 if not Is_Access_Type (T) then
1837 Error_Msg_N
1838 ("null exclusion can only apply to an access type", N);
1840 elsif Can_Never_Be_Null (T) then
1841 Error_Msg_NE
1842 ("`NOT NULL` not allowed (& already excludes null)",
1843 N, T);
1844 end if;
1845 end if;
1847 -- Ada 2005 (AI-423): Formal object with an access definition
1849 else
1850 Check_Access_Definition (N);
1851 T := Access_Definition
1852 (Related_Nod => N,
1853 N => Access_Definition (N));
1854 end if;
1856 if Ekind (T) = E_Incomplete_Type then
1857 declare
1858 Error_Node : Node_Id;
1860 begin
1861 if Present (Subtype_Mark (N)) then
1862 Error_Node := Subtype_Mark (N);
1863 else
1864 Check_Access_Definition (N);
1865 Error_Node := Access_Definition (N);
1866 end if;
1868 Error_Msg_N ("premature usage of incomplete type", Error_Node);
1869 end;
1870 end if;
1872 if K = E_Generic_In_Parameter then
1874 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
1876 if Ada_Version < Ada_05 and then Is_Limited_Type (T) then
1877 Error_Msg_N
1878 ("generic formal of mode IN must not be of limited type", N);
1879 Explain_Limited_Type (T, N);
1880 end if;
1882 if Is_Abstract_Type (T) then
1883 Error_Msg_N
1884 ("generic formal of mode IN must not be of abstract type", N);
1885 end if;
1887 if Present (E) then
1888 Preanalyze_Spec_Expression (E, T);
1890 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
1891 Error_Msg_N
1892 ("initialization not allowed for limited types", E);
1893 Explain_Limited_Type (T, E);
1894 end if;
1895 end if;
1897 Set_Ekind (Id, K);
1898 Set_Etype (Id, T);
1900 -- Case of generic IN OUT parameter
1902 else
1903 -- If the formal has an unconstrained type, construct its actual
1904 -- subtype, as is done for subprogram formals. In this fashion, all
1905 -- its uses can refer to specific bounds.
1907 Set_Ekind (Id, K);
1908 Set_Etype (Id, T);
1910 if (Is_Array_Type (T)
1911 and then not Is_Constrained (T))
1912 or else
1913 (Ekind (T) = E_Record_Type
1914 and then Has_Discriminants (T))
1915 then
1916 declare
1917 Non_Freezing_Ref : constant Node_Id :=
1918 New_Reference_To (Id, Sloc (Id));
1919 Decl : Node_Id;
1921 begin
1922 -- Make sure the actual subtype doesn't generate bogus freezing
1924 Set_Must_Not_Freeze (Non_Freezing_Ref);
1925 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
1926 Insert_Before_And_Analyze (N, Decl);
1927 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
1928 end;
1929 else
1930 Set_Actual_Subtype (Id, T);
1931 end if;
1933 if Present (E) then
1934 Error_Msg_N
1935 ("initialization not allowed for `IN OUT` formals", N);
1936 end if;
1937 end if;
1938 end Analyze_Formal_Object_Declaration;
1940 ----------------------------------------------
1941 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
1942 ----------------------------------------------
1944 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
1945 (T : Entity_Id;
1946 Def : Node_Id)
1948 Loc : constant Source_Ptr := Sloc (Def);
1949 Base : constant Entity_Id :=
1950 New_Internal_Entity
1951 (E_Ordinary_Fixed_Point_Type, Current_Scope, Sloc (Def), 'G');
1952 begin
1953 -- The semantic attributes are set for completeness only, their values
1954 -- will never be used, since all properties of the type are non-static.
1956 Enter_Name (T);
1957 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
1958 Set_Etype (T, Base);
1959 Set_Size_Info (T, Standard_Integer);
1960 Set_RM_Size (T, RM_Size (Standard_Integer));
1961 Set_Small_Value (T, Ureal_1);
1962 Set_Delta_Value (T, Ureal_1);
1963 Set_Scalar_Range (T,
1964 Make_Range (Loc,
1965 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1966 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1967 Set_Is_Constrained (T);
1969 Set_Is_Generic_Type (Base);
1970 Set_Etype (Base, Base);
1971 Set_Size_Info (Base, Standard_Integer);
1972 Set_RM_Size (Base, RM_Size (Standard_Integer));
1973 Set_Small_Value (Base, Ureal_1);
1974 Set_Delta_Value (Base, Ureal_1);
1975 Set_Scalar_Range (Base, Scalar_Range (T));
1976 Set_Parent (Base, Parent (Def));
1978 Check_Restriction (No_Fixed_Point, Def);
1979 end Analyze_Formal_Ordinary_Fixed_Point_Type;
1981 ----------------------------
1982 -- Analyze_Formal_Package --
1983 ----------------------------
1985 procedure Analyze_Formal_Package (N : Node_Id) is
1986 Loc : constant Source_Ptr := Sloc (N);
1987 Pack_Id : constant Entity_Id := Defining_Identifier (N);
1988 Formal : Entity_Id;
1989 Gen_Id : constant Node_Id := Name (N);
1990 Gen_Decl : Node_Id;
1991 Gen_Unit : Entity_Id;
1992 New_N : Node_Id;
1993 Parent_Installed : Boolean := False;
1994 Renaming : Node_Id;
1995 Parent_Instance : Entity_Id;
1996 Renaming_In_Par : Entity_Id;
1997 No_Associations : Boolean := False;
1999 function Build_Local_Package return Node_Id;
2000 -- The formal package is rewritten so that its parameters are replaced
2001 -- with corresponding declarations. For parameters with bona fide
2002 -- associations these declarations are created by Analyze_Associations
2003 -- as for a regular instantiation. For boxed parameters, we preserve
2004 -- the formal declarations and analyze them, in order to introduce
2005 -- entities of the right kind in the environment of the formal.
2007 -------------------------
2008 -- Build_Local_Package --
2009 -------------------------
2011 function Build_Local_Package return Node_Id is
2012 Decls : List_Id;
2013 Pack_Decl : Node_Id;
2015 begin
2016 -- Within the formal, the name of the generic package is a renaming
2017 -- of the formal (as for a regular instantiation).
2019 Pack_Decl :=
2020 Make_Package_Declaration (Loc,
2021 Specification =>
2022 Copy_Generic_Node
2023 (Specification (Original_Node (Gen_Decl)),
2024 Empty, Instantiating => True));
2026 Renaming := Make_Package_Renaming_Declaration (Loc,
2027 Defining_Unit_Name =>
2028 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2029 Name => New_Occurrence_Of (Formal, Loc));
2031 if Nkind (Gen_Id) = N_Identifier
2032 and then Chars (Gen_Id) = Chars (Pack_Id)
2033 then
2034 Error_Msg_NE
2035 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2036 end if;
2038 -- If the formal is declared with a box, or with an others choice,
2039 -- create corresponding declarations for all entities in the formal
2040 -- part, so that names with the proper types are available in the
2041 -- specification of the formal package.
2043 -- On the other hand, if there are no associations, then all the
2044 -- formals must have defaults, and this will be checked by the
2045 -- call to Analyze_Associations.
2047 if Box_Present (N)
2048 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2049 then
2050 declare
2051 Formal_Decl : Node_Id;
2053 begin
2054 -- TBA : for a formal package, need to recurse ???
2056 Decls := New_List;
2057 Formal_Decl :=
2058 First
2059 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2060 while Present (Formal_Decl) loop
2061 Append_To
2062 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2063 Next (Formal_Decl);
2064 end loop;
2065 end;
2067 -- If generic associations are present, use Analyze_Associations to
2068 -- create the proper renaming declarations.
2070 else
2071 declare
2072 Act_Tree : constant Node_Id :=
2073 Copy_Generic_Node
2074 (Original_Node (Gen_Decl), Empty,
2075 Instantiating => True);
2077 begin
2078 Generic_Renamings.Set_Last (0);
2079 Generic_Renamings_HTable.Reset;
2080 Instantiation_Node := N;
2082 Decls :=
2083 Analyze_Associations
2084 (Original_Node (N),
2085 Generic_Formal_Declarations (Act_Tree),
2086 Generic_Formal_Declarations (Gen_Decl));
2087 end;
2088 end if;
2090 Append (Renaming, To => Decls);
2092 -- Add generated declarations ahead of local declarations in
2093 -- the package.
2095 if No (Visible_Declarations (Specification (Pack_Decl))) then
2096 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2097 else
2098 Insert_List_Before
2099 (First (Visible_Declarations (Specification (Pack_Decl))),
2100 Decls);
2101 end if;
2103 return Pack_Decl;
2104 end Build_Local_Package;
2106 -- Start of processing for Analyze_Formal_Package
2108 begin
2109 Text_IO_Kludge (Gen_Id);
2111 Init_Env;
2112 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2113 Gen_Unit := Entity (Gen_Id);
2115 -- Check for a formal package that is a package renaming
2117 if Present (Renamed_Object (Gen_Unit)) then
2118 Gen_Unit := Renamed_Object (Gen_Unit);
2119 end if;
2121 if Ekind (Gen_Unit) /= E_Generic_Package then
2122 Error_Msg_N ("expect generic package name", Gen_Id);
2123 Restore_Env;
2124 return;
2126 elsif Gen_Unit = Current_Scope then
2127 Error_Msg_N
2128 ("generic package cannot be used as a formal package of itself",
2129 Gen_Id);
2130 Restore_Env;
2131 return;
2133 elsif In_Open_Scopes (Gen_Unit) then
2134 if Is_Compilation_Unit (Gen_Unit)
2135 and then Is_Child_Unit (Current_Scope)
2136 then
2137 -- Special-case the error when the formal is a parent, and
2138 -- continue analysis to minimize cascaded errors.
2140 Error_Msg_N
2141 ("generic parent cannot be used as formal package "
2142 & "of a child unit",
2143 Gen_Id);
2145 else
2146 Error_Msg_N
2147 ("generic package cannot be used as a formal package "
2148 & "within itself",
2149 Gen_Id);
2150 Restore_Env;
2151 return;
2152 end if;
2153 end if;
2155 if Box_Present (N)
2156 or else No (Generic_Associations (N))
2157 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2158 then
2159 No_Associations := True;
2160 end if;
2162 -- If there are no generic associations, the generic parameters appear
2163 -- as local entities and are instantiated like them. We copy the generic
2164 -- package declaration as if it were an instantiation, and analyze it
2165 -- like a regular package, except that we treat the formals as
2166 -- additional visible components.
2168 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2170 if In_Extended_Main_Source_Unit (N) then
2171 Set_Is_Instantiated (Gen_Unit);
2172 Generate_Reference (Gen_Unit, N);
2173 end if;
2175 Formal := New_Copy (Pack_Id);
2176 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2178 begin
2179 -- Make local generic without formals. The formals will be replaced
2180 -- with internal declarations.
2182 New_N := Build_Local_Package;
2184 -- If there are errors in the parameter list, Analyze_Associations
2185 -- raises Instantiation_Error. Patch the declaration to prevent
2186 -- further exception propagation.
2188 exception
2189 when Instantiation_Error =>
2191 Enter_Name (Formal);
2192 Set_Ekind (Formal, E_Variable);
2193 Set_Etype (Formal, Any_Type);
2195 if Parent_Installed then
2196 Remove_Parent;
2197 end if;
2199 return;
2200 end;
2202 Rewrite (N, New_N);
2203 Set_Defining_Unit_Name (Specification (New_N), Formal);
2204 Set_Generic_Parent (Specification (N), Gen_Unit);
2205 Set_Instance_Env (Gen_Unit, Formal);
2206 Set_Is_Generic_Instance (Formal);
2208 Enter_Name (Formal);
2209 Set_Ekind (Formal, E_Package);
2210 Set_Etype (Formal, Standard_Void_Type);
2211 Set_Inner_Instances (Formal, New_Elmt_List);
2212 Push_Scope (Formal);
2214 if Is_Child_Unit (Gen_Unit)
2215 and then Parent_Installed
2216 then
2217 -- Similarly, we have to make the name of the formal visible in the
2218 -- parent instance, to resolve properly fully qualified names that
2219 -- may appear in the generic unit. The parent instance has been
2220 -- placed on the scope stack ahead of the current scope.
2222 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2224 Renaming_In_Par :=
2225 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2226 Set_Ekind (Renaming_In_Par, E_Package);
2227 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2228 Set_Scope (Renaming_In_Par, Parent_Instance);
2229 Set_Parent (Renaming_In_Par, Parent (Formal));
2230 Set_Renamed_Object (Renaming_In_Par, Formal);
2231 Append_Entity (Renaming_In_Par, Parent_Instance);
2232 end if;
2234 Analyze (Specification (N));
2236 -- The formals for which associations are provided are not visible
2237 -- outside of the formal package. The others are still declared by a
2238 -- formal parameter declaration.
2240 if not No_Associations then
2241 declare
2242 E : Entity_Id;
2244 begin
2245 E := First_Entity (Formal);
2246 while Present (E) loop
2247 exit when Ekind (E) = E_Package
2248 and then Renamed_Entity (E) = Formal;
2250 if not Is_Generic_Formal (E) then
2251 Set_Is_Hidden (E);
2252 end if;
2254 Next_Entity (E);
2255 end loop;
2256 end;
2257 end if;
2259 End_Package_Scope (Formal);
2261 if Parent_Installed then
2262 Remove_Parent;
2263 end if;
2265 Restore_Env;
2267 -- Inside the generic unit, the formal package is a regular package, but
2268 -- no body is needed for it. Note that after instantiation, the defining
2269 -- unit name we need is in the new tree and not in the original (see
2270 -- Package_Instantiation). A generic formal package is an instance, and
2271 -- can be used as an actual for an inner instance.
2273 Set_Has_Completion (Formal, True);
2275 -- Add semantic information to the original defining identifier.
2276 -- for ASIS use.
2278 Set_Ekind (Pack_Id, E_Package);
2279 Set_Etype (Pack_Id, Standard_Void_Type);
2280 Set_Scope (Pack_Id, Scope (Formal));
2281 Set_Has_Completion (Pack_Id, True);
2282 end Analyze_Formal_Package;
2284 ---------------------------------
2285 -- Analyze_Formal_Private_Type --
2286 ---------------------------------
2288 procedure Analyze_Formal_Private_Type
2289 (N : Node_Id;
2290 T : Entity_Id;
2291 Def : Node_Id)
2293 begin
2294 New_Private_Type (N, T, Def);
2296 -- Set the size to an arbitrary but legal value
2298 Set_Size_Info (T, Standard_Integer);
2299 Set_RM_Size (T, RM_Size (Standard_Integer));
2300 end Analyze_Formal_Private_Type;
2302 ----------------------------------------
2303 -- Analyze_Formal_Signed_Integer_Type --
2304 ----------------------------------------
2306 procedure Analyze_Formal_Signed_Integer_Type
2307 (T : Entity_Id;
2308 Def : Node_Id)
2310 Base : constant Entity_Id :=
2311 New_Internal_Entity
2312 (E_Signed_Integer_Type, Current_Scope, Sloc (Def), 'G');
2314 begin
2315 Enter_Name (T);
2317 Set_Ekind (T, E_Signed_Integer_Subtype);
2318 Set_Etype (T, Base);
2319 Set_Size_Info (T, Standard_Integer);
2320 Set_RM_Size (T, RM_Size (Standard_Integer));
2321 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2322 Set_Is_Constrained (T);
2324 Set_Is_Generic_Type (Base);
2325 Set_Size_Info (Base, Standard_Integer);
2326 Set_RM_Size (Base, RM_Size (Standard_Integer));
2327 Set_Etype (Base, Base);
2328 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2329 Set_Parent (Base, Parent (Def));
2330 end Analyze_Formal_Signed_Integer_Type;
2332 -------------------------------
2333 -- Analyze_Formal_Subprogram --
2334 -------------------------------
2336 procedure Analyze_Formal_Subprogram (N : Node_Id) is
2337 Spec : constant Node_Id := Specification (N);
2338 Def : constant Node_Id := Default_Name (N);
2339 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2340 Subp : Entity_Id;
2342 begin
2343 if Nam = Error then
2344 return;
2345 end if;
2347 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2348 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2349 return;
2350 end if;
2352 Analyze_Subprogram_Declaration (N);
2353 Set_Is_Formal_Subprogram (Nam);
2354 Set_Has_Completion (Nam);
2356 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2357 Set_Is_Abstract_Subprogram (Nam);
2358 Set_Is_Dispatching_Operation (Nam);
2360 declare
2361 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2362 begin
2363 if No (Ctrl_Type) then
2364 Error_Msg_N
2365 ("abstract formal subprogram must have a controlling type",
2367 else
2368 Check_Controlling_Formals (Ctrl_Type, Nam);
2369 end if;
2370 end;
2371 end if;
2373 -- Default name is resolved at the point of instantiation
2375 if Box_Present (N) then
2376 null;
2378 -- Else default is bound at the point of generic declaration
2380 elsif Present (Def) then
2381 if Nkind (Def) = N_Operator_Symbol then
2382 Find_Direct_Name (Def);
2384 elsif Nkind (Def) /= N_Attribute_Reference then
2385 Analyze (Def);
2387 else
2388 -- For an attribute reference, analyze the prefix and verify
2389 -- that it has the proper profile for the subprogram.
2391 Analyze (Prefix (Def));
2392 Valid_Default_Attribute (Nam, Def);
2393 return;
2394 end if;
2396 -- Default name may be overloaded, in which case the interpretation
2397 -- with the correct profile must be selected, as for a renaming.
2398 -- If the definition is an indexed component, it must denote a
2399 -- member of an entry family. If it is a selected component, it
2400 -- can be a protected operation.
2402 if Etype (Def) = Any_Type then
2403 return;
2405 elsif Nkind (Def) = N_Selected_Component then
2406 if not Is_Overloadable (Entity (Selector_Name (Def))) then
2407 Error_Msg_N ("expect valid subprogram name as default", Def);
2408 end if;
2410 elsif Nkind (Def) = N_Indexed_Component then
2411 if Is_Entity_Name (Prefix (Def)) then
2412 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
2413 Error_Msg_N ("expect valid subprogram name as default", Def);
2414 end if;
2416 elsif Nkind (Prefix (Def)) = N_Selected_Component then
2417 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
2418 E_Entry_Family
2419 then
2420 Error_Msg_N ("expect valid subprogram name as default", Def);
2421 end if;
2423 else
2424 Error_Msg_N ("expect valid subprogram name as default", Def);
2425 return;
2426 end if;
2428 elsif Nkind (Def) = N_Character_Literal then
2430 -- Needs some type checks: subprogram should be parameterless???
2432 Resolve (Def, (Etype (Nam)));
2434 elsif not Is_Entity_Name (Def)
2435 or else not Is_Overloadable (Entity (Def))
2436 then
2437 Error_Msg_N ("expect valid subprogram name as default", Def);
2438 return;
2440 elsif not Is_Overloaded (Def) then
2441 Subp := Entity (Def);
2443 if Subp = Nam then
2444 Error_Msg_N ("premature usage of formal subprogram", Def);
2446 elsif not Entity_Matches_Spec (Subp, Nam) then
2447 Error_Msg_N ("no visible entity matches specification", Def);
2448 end if;
2450 -- More than one interpretation, so disambiguate as for a renaming
2452 else
2453 declare
2454 I : Interp_Index;
2455 I1 : Interp_Index := 0;
2456 It : Interp;
2457 It1 : Interp;
2459 begin
2460 Subp := Any_Id;
2461 Get_First_Interp (Def, I, It);
2462 while Present (It.Nam) loop
2463 if Entity_Matches_Spec (It.Nam, Nam) then
2464 if Subp /= Any_Id then
2465 It1 := Disambiguate (Def, I1, I, Etype (Subp));
2467 if It1 = No_Interp then
2468 Error_Msg_N ("ambiguous default subprogram", Def);
2469 else
2470 Subp := It1.Nam;
2471 end if;
2473 exit;
2475 else
2476 I1 := I;
2477 Subp := It.Nam;
2478 end if;
2479 end if;
2481 Get_Next_Interp (I, It);
2482 end loop;
2483 end;
2485 if Subp /= Any_Id then
2486 Set_Entity (Def, Subp);
2488 if Subp = Nam then
2489 Error_Msg_N ("premature usage of formal subprogram", Def);
2491 elsif Ekind (Subp) /= E_Operator then
2492 Check_Mode_Conformant (Subp, Nam);
2493 end if;
2495 else
2496 Error_Msg_N ("no visible subprogram matches specification", N);
2497 end if;
2498 end if;
2499 end if;
2500 end Analyze_Formal_Subprogram;
2502 -------------------------------------
2503 -- Analyze_Formal_Type_Declaration --
2504 -------------------------------------
2506 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
2507 Def : constant Node_Id := Formal_Type_Definition (N);
2508 T : Entity_Id;
2510 begin
2511 T := Defining_Identifier (N);
2513 if Present (Discriminant_Specifications (N))
2514 and then Nkind (Def) /= N_Formal_Private_Type_Definition
2515 then
2516 Error_Msg_N
2517 ("discriminants not allowed for this formal type", T);
2518 end if;
2520 -- Enter the new name, and branch to specific routine
2522 case Nkind (Def) is
2523 when N_Formal_Private_Type_Definition =>
2524 Analyze_Formal_Private_Type (N, T, Def);
2526 when N_Formal_Derived_Type_Definition =>
2527 Analyze_Formal_Derived_Type (N, T, Def);
2529 when N_Formal_Discrete_Type_Definition =>
2530 Analyze_Formal_Discrete_Type (T, Def);
2532 when N_Formal_Signed_Integer_Type_Definition =>
2533 Analyze_Formal_Signed_Integer_Type (T, Def);
2535 when N_Formal_Modular_Type_Definition =>
2536 Analyze_Formal_Modular_Type (T, Def);
2538 when N_Formal_Floating_Point_Definition =>
2539 Analyze_Formal_Floating_Type (T, Def);
2541 when N_Formal_Ordinary_Fixed_Point_Definition =>
2542 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
2544 when N_Formal_Decimal_Fixed_Point_Definition =>
2545 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
2547 when N_Array_Type_Definition =>
2548 Analyze_Formal_Array_Type (T, Def);
2550 when N_Access_To_Object_Definition |
2551 N_Access_Function_Definition |
2552 N_Access_Procedure_Definition =>
2553 Analyze_Generic_Access_Type (T, Def);
2555 -- Ada 2005: a interface declaration is encoded as an abstract
2556 -- record declaration or a abstract type derivation.
2558 when N_Record_Definition =>
2559 Analyze_Formal_Interface_Type (N, T, Def);
2561 when N_Derived_Type_Definition =>
2562 Analyze_Formal_Derived_Interface_Type (N, T, Def);
2564 when N_Error =>
2565 null;
2567 when others =>
2568 raise Program_Error;
2570 end case;
2572 Set_Is_Generic_Type (T);
2573 end Analyze_Formal_Type_Declaration;
2575 ------------------------------------
2576 -- Analyze_Function_Instantiation --
2577 ------------------------------------
2579 procedure Analyze_Function_Instantiation (N : Node_Id) is
2580 begin
2581 Analyze_Subprogram_Instantiation (N, E_Function);
2582 end Analyze_Function_Instantiation;
2584 ---------------------------------
2585 -- Analyze_Generic_Access_Type --
2586 ---------------------------------
2588 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
2589 begin
2590 Enter_Name (T);
2592 if Nkind (Def) = N_Access_To_Object_Definition then
2593 Access_Type_Declaration (T, Def);
2595 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
2596 and then No (Full_View (Designated_Type (T)))
2597 and then not Is_Generic_Type (Designated_Type (T))
2598 then
2599 Error_Msg_N ("premature usage of incomplete type", Def);
2601 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
2602 Error_Msg_N
2603 ("only a subtype mark is allowed in a formal", Def);
2604 end if;
2606 else
2607 Access_Subprogram_Declaration (T, Def);
2608 end if;
2609 end Analyze_Generic_Access_Type;
2611 ---------------------------------
2612 -- Analyze_Generic_Formal_Part --
2613 ---------------------------------
2615 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
2616 Gen_Parm_Decl : Node_Id;
2618 begin
2619 -- The generic formals are processed in the scope of the generic unit,
2620 -- where they are immediately visible. The scope is installed by the
2621 -- caller.
2623 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
2625 while Present (Gen_Parm_Decl) loop
2626 Analyze (Gen_Parm_Decl);
2627 Next (Gen_Parm_Decl);
2628 end loop;
2630 Generate_Reference_To_Generic_Formals (Current_Scope);
2631 end Analyze_Generic_Formal_Part;
2633 ------------------------------------------
2634 -- Analyze_Generic_Package_Declaration --
2635 ------------------------------------------
2637 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
2638 Loc : constant Source_Ptr := Sloc (N);
2639 Id : Entity_Id;
2640 New_N : Node_Id;
2641 Save_Parent : Node_Id;
2642 Renaming : Node_Id;
2643 Decls : constant List_Id :=
2644 Visible_Declarations (Specification (N));
2645 Decl : Node_Id;
2647 begin
2648 -- We introduce a renaming of the enclosing package, to have a usable
2649 -- entity as the prefix of an expanded name for a local entity of the
2650 -- form Par.P.Q, where P is the generic package. This is because a local
2651 -- entity named P may hide it, so that the usual visibility rules in
2652 -- the instance will not resolve properly.
2654 Renaming :=
2655 Make_Package_Renaming_Declaration (Loc,
2656 Defining_Unit_Name =>
2657 Make_Defining_Identifier (Loc,
2658 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
2659 Name => Make_Identifier (Loc, Chars (Defining_Entity (N))));
2661 if Present (Decls) then
2662 Decl := First (Decls);
2663 while Present (Decl)
2664 and then Nkind (Decl) = N_Pragma
2665 loop
2666 Next (Decl);
2667 end loop;
2669 if Present (Decl) then
2670 Insert_Before (Decl, Renaming);
2671 else
2672 Append (Renaming, Visible_Declarations (Specification (N)));
2673 end if;
2675 else
2676 Set_Visible_Declarations (Specification (N), New_List (Renaming));
2677 end if;
2679 -- Create copy of generic unit, and save for instantiation. If the unit
2680 -- is a child unit, do not copy the specifications for the parent, which
2681 -- are not part of the generic tree.
2683 Save_Parent := Parent_Spec (N);
2684 Set_Parent_Spec (N, Empty);
2686 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2687 Set_Parent_Spec (New_N, Save_Parent);
2688 Rewrite (N, New_N);
2689 Id := Defining_Entity (N);
2690 Generate_Definition (Id);
2692 -- Expansion is not applied to generic units
2694 Start_Generic;
2696 Enter_Name (Id);
2697 Set_Ekind (Id, E_Generic_Package);
2698 Set_Etype (Id, Standard_Void_Type);
2699 Push_Scope (Id);
2700 Enter_Generic_Scope (Id);
2701 Set_Inner_Instances (Id, New_Elmt_List);
2703 Set_Categorization_From_Pragmas (N);
2704 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2706 -- Link the declaration of the generic homonym in the generic copy to
2707 -- the package it renames, so that it is always resolved properly.
2709 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
2710 Set_Entity (Associated_Node (Name (Renaming)), Id);
2712 -- For a library unit, we have reconstructed the entity for the unit,
2713 -- and must reset it in the library tables.
2715 if Nkind (Parent (N)) = N_Compilation_Unit then
2716 Set_Cunit_Entity (Current_Sem_Unit, Id);
2717 end if;
2719 Analyze_Generic_Formal_Part (N);
2721 -- After processing the generic formals, analysis proceeds as for a
2722 -- non-generic package.
2724 Analyze (Specification (N));
2726 Validate_Categorization_Dependency (N, Id);
2728 End_Generic;
2730 End_Package_Scope (Id);
2731 Exit_Generic_Scope (Id);
2733 if Nkind (Parent (N)) /= N_Compilation_Unit then
2734 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
2735 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
2736 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
2738 else
2739 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2740 Validate_RT_RAT_Component (N);
2742 -- If this is a spec without a body, check that generic parameters
2743 -- are referenced.
2745 if not Body_Required (Parent (N)) then
2746 Check_References (Id);
2747 end if;
2748 end if;
2749 end Analyze_Generic_Package_Declaration;
2751 --------------------------------------------
2752 -- Analyze_Generic_Subprogram_Declaration --
2753 --------------------------------------------
2755 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
2756 Spec : Node_Id;
2757 Id : Entity_Id;
2758 Formals : List_Id;
2759 New_N : Node_Id;
2760 Result_Type : Entity_Id;
2761 Save_Parent : Node_Id;
2762 Typ : Entity_Id;
2764 begin
2765 -- Create copy of generic unit, and save for instantiation. If the unit
2766 -- is a child unit, do not copy the specifications for the parent, which
2767 -- are not part of the generic tree.
2769 Save_Parent := Parent_Spec (N);
2770 Set_Parent_Spec (N, Empty);
2772 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2773 Set_Parent_Spec (New_N, Save_Parent);
2774 Rewrite (N, New_N);
2776 Spec := Specification (N);
2777 Id := Defining_Entity (Spec);
2778 Generate_Definition (Id);
2780 if Nkind (Id) = N_Defining_Operator_Symbol then
2781 Error_Msg_N
2782 ("operator symbol not allowed for generic subprogram", Id);
2783 end if;
2785 Start_Generic;
2787 Enter_Name (Id);
2789 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
2790 Push_Scope (Id);
2791 Enter_Generic_Scope (Id);
2792 Set_Inner_Instances (Id, New_Elmt_List);
2793 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2795 Analyze_Generic_Formal_Part (N);
2797 Formals := Parameter_Specifications (Spec);
2799 if Present (Formals) then
2800 Process_Formals (Formals, Spec);
2801 end if;
2803 if Nkind (Spec) = N_Function_Specification then
2804 Set_Ekind (Id, E_Generic_Function);
2806 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
2807 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
2808 Set_Etype (Id, Result_Type);
2809 else
2810 Find_Type (Result_Definition (Spec));
2811 Typ := Entity (Result_Definition (Spec));
2813 -- If a null exclusion is imposed on the result type, then create
2814 -- a null-excluding itype (an access subtype) and use it as the
2815 -- function's Etype.
2817 if Is_Access_Type (Typ)
2818 and then Null_Exclusion_Present (Spec)
2819 then
2820 Set_Etype (Id,
2821 Create_Null_Excluding_Itype
2822 (T => Typ,
2823 Related_Nod => Spec,
2824 Scope_Id => Defining_Unit_Name (Spec)));
2825 else
2826 Set_Etype (Id, Typ);
2827 end if;
2828 end if;
2830 else
2831 Set_Ekind (Id, E_Generic_Procedure);
2832 Set_Etype (Id, Standard_Void_Type);
2833 end if;
2835 -- For a library unit, we have reconstructed the entity for the unit,
2836 -- and must reset it in the library tables. We also make sure that
2837 -- Body_Required is set properly in the original compilation unit node.
2839 if Nkind (Parent (N)) = N_Compilation_Unit then
2840 Set_Cunit_Entity (Current_Sem_Unit, Id);
2841 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2842 end if;
2844 Set_Categorization_From_Pragmas (N);
2845 Validate_Categorization_Dependency (N, Id);
2847 Save_Global_References (Original_Node (N));
2849 End_Generic;
2850 End_Scope;
2851 Exit_Generic_Scope (Id);
2852 Generate_Reference_To_Formals (Id);
2853 end Analyze_Generic_Subprogram_Declaration;
2855 -----------------------------------
2856 -- Analyze_Package_Instantiation --
2857 -----------------------------------
2859 procedure Analyze_Package_Instantiation (N : Node_Id) is
2860 Loc : constant Source_Ptr := Sloc (N);
2861 Gen_Id : constant Node_Id := Name (N);
2863 Act_Decl : Node_Id;
2864 Act_Decl_Name : Node_Id;
2865 Act_Decl_Id : Entity_Id;
2866 Act_Spec : Node_Id;
2867 Act_Tree : Node_Id;
2869 Gen_Decl : Node_Id;
2870 Gen_Unit : Entity_Id;
2872 Is_Actual_Pack : constant Boolean :=
2873 Is_Internal (Defining_Entity (N));
2875 Env_Installed : Boolean := False;
2876 Parent_Installed : Boolean := False;
2877 Renaming_List : List_Id;
2878 Unit_Renaming : Node_Id;
2879 Needs_Body : Boolean;
2880 Inline_Now : Boolean := False;
2882 procedure Delay_Descriptors (E : Entity_Id);
2883 -- Delay generation of subprogram descriptors for given entity
2885 function Might_Inline_Subp return Boolean;
2886 -- If inlining is active and the generic contains inlined subprograms,
2887 -- we instantiate the body. This may cause superfluous instantiations,
2888 -- but it is simpler than detecting the need for the body at the point
2889 -- of inlining, when the context of the instance is not available.
2891 -----------------------
2892 -- Delay_Descriptors --
2893 -----------------------
2895 procedure Delay_Descriptors (E : Entity_Id) is
2896 begin
2897 if not Delay_Subprogram_Descriptors (E) then
2898 Set_Delay_Subprogram_Descriptors (E);
2899 Pending_Descriptor.Append (E);
2900 end if;
2901 end Delay_Descriptors;
2903 -----------------------
2904 -- Might_Inline_Subp --
2905 -----------------------
2907 function Might_Inline_Subp return Boolean is
2908 E : Entity_Id;
2910 begin
2911 if not Inline_Processing_Required then
2912 return False;
2914 else
2915 E := First_Entity (Gen_Unit);
2916 while Present (E) loop
2917 if Is_Subprogram (E)
2918 and then Is_Inlined (E)
2919 then
2920 return True;
2921 end if;
2923 Next_Entity (E);
2924 end loop;
2925 end if;
2927 return False;
2928 end Might_Inline_Subp;
2930 -- Start of processing for Analyze_Package_Instantiation
2932 begin
2933 -- Very first thing: apply the special kludge for Text_IO processing
2934 -- in case we are instantiating one of the children of [Wide_]Text_IO.
2936 Text_IO_Kludge (Name (N));
2938 -- Make node global for error reporting
2940 Instantiation_Node := N;
2942 -- Case of instantiation of a generic package
2944 if Nkind (N) = N_Package_Instantiation then
2945 Act_Decl_Id := New_Copy (Defining_Entity (N));
2946 Set_Comes_From_Source (Act_Decl_Id, True);
2948 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
2949 Act_Decl_Name :=
2950 Make_Defining_Program_Unit_Name (Loc,
2951 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
2952 Defining_Identifier => Act_Decl_Id);
2953 else
2954 Act_Decl_Name := Act_Decl_Id;
2955 end if;
2957 -- Case of instantiation of a formal package
2959 else
2960 Act_Decl_Id := Defining_Identifier (N);
2961 Act_Decl_Name := Act_Decl_Id;
2962 end if;
2964 Generate_Definition (Act_Decl_Id);
2965 Preanalyze_Actuals (N);
2967 Init_Env;
2968 Env_Installed := True;
2970 -- Reset renaming map for formal types. The mapping is established
2971 -- when analyzing the generic associations, but some mappings are
2972 -- inherited from formal packages of parent units, and these are
2973 -- constructed when the parents are installed.
2975 Generic_Renamings.Set_Last (0);
2976 Generic_Renamings_HTable.Reset;
2978 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2979 Gen_Unit := Entity (Gen_Id);
2981 -- Verify that it is the name of a generic package
2983 -- A visibility glitch: if the instance is a child unit and the generic
2984 -- is the generic unit of a parent instance (i.e. both the parent and
2985 -- the child units are instances of the same package) the name now
2986 -- denotes the renaming within the parent, not the intended generic
2987 -- unit. See if there is a homonym that is the desired generic. The
2988 -- renaming declaration must be visible inside the instance of the
2989 -- child, but not when analyzing the name in the instantiation itself.
2991 if Ekind (Gen_Unit) = E_Package
2992 and then Present (Renamed_Entity (Gen_Unit))
2993 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
2994 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
2995 and then Present (Homonym (Gen_Unit))
2996 then
2997 Gen_Unit := Homonym (Gen_Unit);
2998 end if;
3000 if Etype (Gen_Unit) = Any_Type then
3001 Restore_Env;
3002 return;
3004 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3006 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3008 if From_With_Type (Gen_Unit) then
3009 Error_Msg_N
3010 ("cannot instantiate a limited withed package", Gen_Id);
3011 else
3012 Error_Msg_N
3013 ("expect name of generic package in instantiation", Gen_Id);
3014 end if;
3016 Restore_Env;
3017 return;
3018 end if;
3020 if In_Extended_Main_Source_Unit (N) then
3021 Set_Is_Instantiated (Gen_Unit);
3022 Generate_Reference (Gen_Unit, N);
3024 if Present (Renamed_Object (Gen_Unit)) then
3025 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3026 Generate_Reference (Renamed_Object (Gen_Unit), N);
3027 end if;
3028 end if;
3030 if Nkind (Gen_Id) = N_Identifier
3031 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3032 then
3033 Error_Msg_NE
3034 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3036 elsif Nkind (Gen_Id) = N_Expanded_Name
3037 and then Is_Child_Unit (Gen_Unit)
3038 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3039 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3040 then
3041 Error_Msg_N
3042 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3043 end if;
3045 Set_Entity (Gen_Id, Gen_Unit);
3047 -- If generic is a renaming, get original generic unit
3049 if Present (Renamed_Object (Gen_Unit))
3050 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3051 then
3052 Gen_Unit := Renamed_Object (Gen_Unit);
3053 end if;
3055 -- Verify that there are no circular instantiations
3057 if In_Open_Scopes (Gen_Unit) then
3058 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3059 Restore_Env;
3060 return;
3062 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3063 Error_Msg_Node_2 := Current_Scope;
3064 Error_Msg_NE
3065 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3066 Circularity_Detected := True;
3067 Restore_Env;
3068 return;
3070 else
3071 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3073 -- Initialize renamings map, for error checking, and the list that
3074 -- holds private entities whose views have changed between generic
3075 -- definition and instantiation. If this is the instance created to
3076 -- validate an actual package, the instantiation environment is that
3077 -- of the enclosing instance.
3079 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3081 -- Copy original generic tree, to produce text for instantiation
3083 Act_Tree :=
3084 Copy_Generic_Node
3085 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3087 Act_Spec := Specification (Act_Tree);
3089 -- If this is the instance created to validate an actual package,
3090 -- only the formals matter, do not examine the package spec itself.
3092 if Is_Actual_Pack then
3093 Set_Visible_Declarations (Act_Spec, New_List);
3094 Set_Private_Declarations (Act_Spec, New_List);
3095 end if;
3097 Renaming_List :=
3098 Analyze_Associations
3100 Generic_Formal_Declarations (Act_Tree),
3101 Generic_Formal_Declarations (Gen_Decl));
3103 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3104 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3105 Set_Is_Generic_Instance (Act_Decl_Id);
3107 Set_Generic_Parent (Act_Spec, Gen_Unit);
3109 -- References to the generic in its own declaration or its body are
3110 -- references to the instance. Add a renaming declaration for the
3111 -- generic unit itself. This declaration, as well as the renaming
3112 -- declarations for the generic formals, must remain private to the
3113 -- unit: the formals, because this is the language semantics, and
3114 -- the unit because its use is an artifact of the implementation.
3116 Unit_Renaming :=
3117 Make_Package_Renaming_Declaration (Loc,
3118 Defining_Unit_Name =>
3119 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3120 Name => New_Reference_To (Act_Decl_Id, Loc));
3122 Append (Unit_Renaming, Renaming_List);
3124 -- The renaming declarations are the first local declarations of
3125 -- the new unit.
3127 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3128 Insert_List_Before
3129 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3130 else
3131 Set_Visible_Declarations (Act_Spec, Renaming_List);
3132 end if;
3134 Act_Decl :=
3135 Make_Package_Declaration (Loc,
3136 Specification => Act_Spec);
3138 -- Save the instantiation node, for subsequent instantiation of the
3139 -- body, if there is one and we are generating code for the current
3140 -- unit. Mark the unit as having a body, to avoid a premature error
3141 -- message.
3143 -- We instantiate the body if we are generating code, if we are
3144 -- generating cross-reference information, or if we are building
3145 -- trees for ASIS use.
3147 declare
3148 Enclosing_Body_Present : Boolean := False;
3149 -- If the generic unit is not a compilation unit, then a body may
3150 -- be present in its parent even if none is required. We create a
3151 -- tentative pending instantiation for the body, which will be
3152 -- discarded if none is actually present.
3154 Scop : Entity_Id;
3156 begin
3157 if Scope (Gen_Unit) /= Standard_Standard
3158 and then not Is_Child_Unit (Gen_Unit)
3159 then
3160 Scop := Scope (Gen_Unit);
3162 while Present (Scop)
3163 and then Scop /= Standard_Standard
3164 loop
3165 if Unit_Requires_Body (Scop) then
3166 Enclosing_Body_Present := True;
3167 exit;
3169 elsif In_Open_Scopes (Scop)
3170 and then In_Package_Body (Scop)
3171 then
3172 Enclosing_Body_Present := True;
3173 exit;
3174 end if;
3176 exit when Is_Compilation_Unit (Scop);
3177 Scop := Scope (Scop);
3178 end loop;
3179 end if;
3181 -- If front-end inlining is enabled, and this is a unit for which
3182 -- code will be generated, we instantiate the body at once.
3184 -- This is done if the instance is not the main unit, and if the
3185 -- generic is not a child unit of another generic, to avoid scope
3186 -- problems and the reinstallation of parent instances.
3188 if Expander_Active
3189 and then (not Is_Child_Unit (Gen_Unit)
3190 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3191 and then Might_Inline_Subp
3192 and then not Is_Actual_Pack
3193 then
3194 if Front_End_Inlining
3195 and then (Is_In_Main_Unit (N)
3196 or else In_Main_Context (Current_Scope))
3197 and then Nkind (Parent (N)) /= N_Compilation_Unit
3198 then
3199 Inline_Now := True;
3201 -- In configurable_run_time mode we force the inlining of
3202 -- predefined subprograms marked Inline_Always, to minimize
3203 -- the use of the run-time library.
3205 elsif Is_Predefined_File_Name
3206 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3207 and then Configurable_Run_Time_Mode
3208 and then Nkind (Parent (N)) /= N_Compilation_Unit
3209 then
3210 Inline_Now := True;
3211 end if;
3213 -- If the current scope is itself an instance within a child
3214 -- unit, there will be duplications in the scope stack, and the
3215 -- unstacking mechanism in Inline_Instance_Body will fail.
3216 -- This loses some rare cases of optimization, and might be
3217 -- improved some day, if we can find a proper abstraction for
3218 -- "the complete compilation context" that can be saved and
3219 -- restored. ???
3221 if Is_Generic_Instance (Current_Scope) then
3222 declare
3223 Curr_Unit : constant Entity_Id :=
3224 Cunit_Entity (Current_Sem_Unit);
3225 begin
3226 if Curr_Unit /= Current_Scope
3227 and then Is_Child_Unit (Curr_Unit)
3228 then
3229 Inline_Now := False;
3230 end if;
3231 end;
3232 end if;
3233 end if;
3235 Needs_Body :=
3236 (Unit_Requires_Body (Gen_Unit)
3237 or else Enclosing_Body_Present
3238 or else Present (Corresponding_Body (Gen_Decl)))
3239 and then (Is_In_Main_Unit (N)
3240 or else Might_Inline_Subp
3241 or else CodePeer_Mode)
3242 and then not Is_Actual_Pack
3243 and then not Inline_Now
3244 and then (Operating_Mode = Generate_Code
3245 or else (Operating_Mode = Check_Semantics
3246 and then ASIS_Mode));
3248 -- If front_end_inlining is enabled, do not instantiate body if
3249 -- within a generic context.
3251 if (Front_End_Inlining
3252 and then not Expander_Active)
3253 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
3254 then
3255 Needs_Body := False;
3256 end if;
3258 -- If the current context is generic, and the package being
3259 -- instantiated is declared within a formal package, there is no
3260 -- body to instantiate until the enclosing generic is instantiated
3261 -- and there is an actual for the formal package. If the formal
3262 -- package has parameters, we build a regular package instance for
3263 -- it, that precedes the original formal package declaration.
3265 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
3266 declare
3267 Decl : constant Node_Id :=
3268 Original_Node
3269 (Unit_Declaration_Node (Scope (Gen_Unit)));
3270 begin
3271 if Nkind (Decl) = N_Formal_Package_Declaration
3272 or else (Nkind (Decl) = N_Package_Declaration
3273 and then Is_List_Member (Decl)
3274 and then Present (Next (Decl))
3275 and then
3276 Nkind (Next (Decl)) =
3277 N_Formal_Package_Declaration)
3278 then
3279 Needs_Body := False;
3280 end if;
3281 end;
3282 end if;
3283 end;
3285 -- If we are generating the calling stubs from the instantiation of
3286 -- a generic RCI package, we will not use the body of the generic
3287 -- package.
3289 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
3290 and then Is_Compilation_Unit (Defining_Entity (N))
3291 then
3292 Needs_Body := False;
3293 end if;
3295 if Needs_Body then
3297 -- Here is a defence against a ludicrous number of instantiations
3298 -- caused by a circular set of instantiation attempts.
3300 if Pending_Instantiations.Last >
3301 Hostparm.Max_Instantiations
3302 then
3303 Error_Msg_N ("too many instantiations", N);
3304 raise Unrecoverable_Error;
3305 end if;
3307 -- Indicate that the enclosing scopes contain an instantiation,
3308 -- and that cleanup actions should be delayed until after the
3309 -- instance body is expanded.
3311 Check_Forward_Instantiation (Gen_Decl);
3312 if Nkind (N) = N_Package_Instantiation then
3313 declare
3314 Enclosing_Master : Entity_Id;
3316 begin
3317 -- Loop to search enclosing masters
3319 Enclosing_Master := Current_Scope;
3320 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
3321 if Ekind (Enclosing_Master) = E_Package then
3322 if Is_Compilation_Unit (Enclosing_Master) then
3323 if In_Package_Body (Enclosing_Master) then
3324 Delay_Descriptors
3325 (Body_Entity (Enclosing_Master));
3326 else
3327 Delay_Descriptors
3328 (Enclosing_Master);
3329 end if;
3331 exit Scope_Loop;
3333 else
3334 Enclosing_Master := Scope (Enclosing_Master);
3335 end if;
3337 elsif Ekind (Enclosing_Master) = E_Generic_Package then
3338 Enclosing_Master := Scope (Enclosing_Master);
3340 elsif Is_Generic_Subprogram (Enclosing_Master)
3341 or else Ekind (Enclosing_Master) = E_Void
3342 then
3343 -- Cleanup actions will eventually be performed on the
3344 -- enclosing instance, if any. Enclosing scope is void
3345 -- in the formal part of a generic subprogram.
3347 exit Scope_Loop;
3349 else
3350 if Ekind (Enclosing_Master) = E_Entry
3351 and then
3352 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
3353 then
3354 if not Expander_Active then
3355 exit Scope_Loop;
3356 else
3357 Enclosing_Master :=
3358 Protected_Body_Subprogram (Enclosing_Master);
3359 end if;
3360 end if;
3362 Set_Delay_Cleanups (Enclosing_Master);
3364 while Ekind (Enclosing_Master) = E_Block loop
3365 Enclosing_Master := Scope (Enclosing_Master);
3366 end loop;
3368 if Is_Subprogram (Enclosing_Master) then
3369 Delay_Descriptors (Enclosing_Master);
3371 elsif Is_Task_Type (Enclosing_Master) then
3372 declare
3373 TBP : constant Node_Id :=
3374 Get_Task_Body_Procedure
3375 (Enclosing_Master);
3376 begin
3377 if Present (TBP) then
3378 Delay_Descriptors (TBP);
3379 Set_Delay_Cleanups (TBP);
3380 end if;
3381 end;
3382 end if;
3384 exit Scope_Loop;
3385 end if;
3386 end loop Scope_Loop;
3387 end;
3389 -- Make entry in table
3391 Pending_Instantiations.Append
3392 ((Inst_Node => N,
3393 Act_Decl => Act_Decl,
3394 Expander_Status => Expander_Active,
3395 Current_Sem_Unit => Current_Sem_Unit,
3396 Scope_Suppress => Scope_Suppress,
3397 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
3398 Version => Ada_Version));
3399 end if;
3400 end if;
3402 Set_Categorization_From_Pragmas (Act_Decl);
3404 if Parent_Installed then
3405 Hide_Current_Scope;
3406 end if;
3408 Set_Instance_Spec (N, Act_Decl);
3410 -- If not a compilation unit, insert the package declaration before
3411 -- the original instantiation node.
3413 if Nkind (Parent (N)) /= N_Compilation_Unit then
3414 Mark_Rewrite_Insertion (Act_Decl);
3415 Insert_Before (N, Act_Decl);
3416 Analyze (Act_Decl);
3418 -- For an instantiation that is a compilation unit, place declaration
3419 -- on current node so context is complete for analysis (including
3420 -- nested instantiations). If this is the main unit, the declaration
3421 -- eventually replaces the instantiation node. If the instance body
3422 -- is created later, it replaces the instance node, and the
3423 -- declaration is attached to it (see
3424 -- Build_Instance_Compilation_Unit_Nodes).
3426 else
3427 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
3429 -- The entity for the current unit is the newly created one,
3430 -- and all semantic information is attached to it.
3432 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
3434 -- If this is the main unit, replace the main entity as well
3436 if Current_Sem_Unit = Main_Unit then
3437 Main_Unit_Entity := Act_Decl_Id;
3438 end if;
3439 end if;
3441 Set_Unit (Parent (N), Act_Decl);
3442 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
3443 Set_Package_Instantiation (Act_Decl_Id, N);
3444 Analyze (Act_Decl);
3445 Set_Unit (Parent (N), N);
3446 Set_Body_Required (Parent (N), False);
3448 -- We never need elaboration checks on instantiations, since by
3449 -- definition, the body instantiation is elaborated at the same
3450 -- time as the spec instantiation.
3452 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
3453 Set_Kill_Elaboration_Checks (Act_Decl_Id);
3454 end if;
3456 Check_Elab_Instantiation (N);
3458 if ABE_Is_Certain (N) and then Needs_Body then
3459 Pending_Instantiations.Decrement_Last;
3460 end if;
3462 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
3464 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
3465 First_Private_Entity (Act_Decl_Id));
3467 -- If the instantiation will receive a body, the unit will be
3468 -- transformed into a package body, and receive its own elaboration
3469 -- entity. Otherwise, the nature of the unit is now a package
3470 -- declaration.
3472 if Nkind (Parent (N)) = N_Compilation_Unit
3473 and then not Needs_Body
3474 then
3475 Rewrite (N, Act_Decl);
3476 end if;
3478 if Present (Corresponding_Body (Gen_Decl))
3479 or else Unit_Requires_Body (Gen_Unit)
3480 then
3481 Set_Has_Completion (Act_Decl_Id);
3482 end if;
3484 Check_Formal_Packages (Act_Decl_Id);
3486 Restore_Private_Views (Act_Decl_Id);
3488 Inherit_Context (Gen_Decl, N);
3490 if Parent_Installed then
3491 Remove_Parent;
3492 end if;
3494 Restore_Env;
3495 Env_Installed := False;
3496 end if;
3498 Validate_Categorization_Dependency (N, Act_Decl_Id);
3500 -- There used to be a check here to prevent instantiations in local
3501 -- contexts if the No_Local_Allocators restriction was active. This
3502 -- check was removed by a binding interpretation in AI-95-00130/07,
3503 -- but we retain the code for documentation purposes.
3505 -- if Ekind (Act_Decl_Id) /= E_Void
3506 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
3507 -- then
3508 -- Check_Restriction (No_Local_Allocators, N);
3509 -- end if;
3511 if Inline_Now then
3512 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
3513 end if;
3515 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
3516 -- be used as defining identifiers for a formal package and for the
3517 -- corresponding expanded package.
3519 if Nkind (N) = N_Formal_Package_Declaration then
3520 Act_Decl_Id := New_Copy (Defining_Entity (N));
3521 Set_Comes_From_Source (Act_Decl_Id, True);
3522 Set_Is_Generic_Instance (Act_Decl_Id, False);
3523 Set_Defining_Identifier (N, Act_Decl_Id);
3524 end if;
3526 exception
3527 when Instantiation_Error =>
3528 if Parent_Installed then
3529 Remove_Parent;
3530 end if;
3532 if Env_Installed then
3533 Restore_Env;
3534 end if;
3535 end Analyze_Package_Instantiation;
3537 --------------------------
3538 -- Inline_Instance_Body --
3539 --------------------------
3541 procedure Inline_Instance_Body
3542 (N : Node_Id;
3543 Gen_Unit : Entity_Id;
3544 Act_Decl : Node_Id)
3546 Vis : Boolean;
3547 Gen_Comp : constant Entity_Id :=
3548 Cunit_Entity (Get_Source_Unit (Gen_Unit));
3549 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
3550 Curr_Scope : Entity_Id := Empty;
3551 Curr_Unit : constant Entity_Id :=
3552 Cunit_Entity (Current_Sem_Unit);
3553 Removed : Boolean := False;
3554 Num_Scopes : Int := 0;
3556 Scope_Stack_Depth : constant Int :=
3557 Scope_Stack.Last - Scope_Stack.First + 1;
3559 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
3560 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
3561 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
3562 Num_Inner : Int := 0;
3563 N_Instances : Int := 0;
3564 S : Entity_Id;
3566 begin
3567 -- Case of generic unit defined in another unit. We must remove the
3568 -- complete context of the current unit to install that of the generic.
3570 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
3572 -- Add some comments for the following two loops ???
3574 S := Current_Scope;
3575 while Present (S) and then S /= Standard_Standard loop
3576 loop
3577 Num_Scopes := Num_Scopes + 1;
3579 Use_Clauses (Num_Scopes) :=
3580 (Scope_Stack.Table
3581 (Scope_Stack.Last - Num_Scopes + 1).
3582 First_Use_Clause);
3583 End_Use_Clauses (Use_Clauses (Num_Scopes));
3585 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
3586 or else Scope_Stack.Table
3587 (Scope_Stack.Last - Num_Scopes).Entity
3588 = Scope (S);
3589 end loop;
3591 exit when Is_Generic_Instance (S)
3592 and then (In_Package_Body (S)
3593 or else Ekind (S) = E_Procedure
3594 or else Ekind (S) = E_Function);
3595 S := Scope (S);
3596 end loop;
3598 Vis := Is_Immediately_Visible (Gen_Comp);
3600 -- Find and save all enclosing instances
3602 S := Current_Scope;
3604 while Present (S)
3605 and then S /= Standard_Standard
3606 loop
3607 if Is_Generic_Instance (S) then
3608 N_Instances := N_Instances + 1;
3609 Instances (N_Instances) := S;
3611 exit when In_Package_Body (S);
3612 end if;
3614 S := Scope (S);
3615 end loop;
3617 -- Remove context of current compilation unit, unless we are within a
3618 -- nested package instantiation, in which case the context has been
3619 -- removed previously.
3621 -- If current scope is the body of a child unit, remove context of
3622 -- spec as well. If an enclosing scope is an instance body, the
3623 -- context has already been removed, but the entities in the body
3624 -- must be made invisible as well.
3626 S := Current_Scope;
3628 while Present (S)
3629 and then S /= Standard_Standard
3630 loop
3631 if Is_Generic_Instance (S)
3632 and then (In_Package_Body (S)
3633 or else Ekind (S) = E_Procedure
3634 or else Ekind (S) = E_Function)
3635 then
3636 -- We still have to remove the entities of the enclosing
3637 -- instance from direct visibility.
3639 declare
3640 E : Entity_Id;
3641 begin
3642 E := First_Entity (S);
3643 while Present (E) loop
3644 Set_Is_Immediately_Visible (E, False);
3645 Next_Entity (E);
3646 end loop;
3647 end;
3649 exit;
3650 end if;
3652 if S = Curr_Unit
3653 or else (Ekind (Curr_Unit) = E_Package_Body
3654 and then S = Spec_Entity (Curr_Unit))
3655 or else (Ekind (Curr_Unit) = E_Subprogram_Body
3656 and then S =
3657 Corresponding_Spec
3658 (Unit_Declaration_Node (Curr_Unit)))
3659 then
3660 Removed := True;
3662 -- Remove entities in current scopes from visibility, so that
3663 -- instance body is compiled in a clean environment.
3665 Save_Scope_Stack (Handle_Use => False);
3667 if Is_Child_Unit (S) then
3669 -- Remove child unit from stack, as well as inner scopes.
3670 -- Removing the context of a child unit removes parent units
3671 -- as well.
3673 while Current_Scope /= S loop
3674 Num_Inner := Num_Inner + 1;
3675 Inner_Scopes (Num_Inner) := Current_Scope;
3676 Pop_Scope;
3677 end loop;
3679 Pop_Scope;
3680 Remove_Context (Curr_Comp);
3681 Curr_Scope := S;
3683 else
3684 Remove_Context (Curr_Comp);
3685 end if;
3687 if Ekind (Curr_Unit) = E_Package_Body then
3688 Remove_Context (Library_Unit (Curr_Comp));
3689 end if;
3690 end if;
3692 S := Scope (S);
3693 end loop;
3694 pragma Assert (Num_Inner < Num_Scopes);
3696 Push_Scope (Standard_Standard);
3697 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
3698 Instantiate_Package_Body
3699 (Body_Info =>
3700 ((Inst_Node => N,
3701 Act_Decl => Act_Decl,
3702 Expander_Status => Expander_Active,
3703 Current_Sem_Unit => Current_Sem_Unit,
3704 Scope_Suppress => Scope_Suppress,
3705 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
3706 Version => Ada_Version)),
3707 Inlined_Body => True);
3709 Pop_Scope;
3711 -- Restore context
3713 Set_Is_Immediately_Visible (Gen_Comp, Vis);
3715 -- Reset Generic_Instance flag so that use clauses can be installed
3716 -- in the proper order. (See Use_One_Package for effect of enclosing
3717 -- instances on processing of use clauses).
3719 for J in 1 .. N_Instances loop
3720 Set_Is_Generic_Instance (Instances (J), False);
3721 end loop;
3723 if Removed then
3724 Install_Context (Curr_Comp);
3726 if Present (Curr_Scope)
3727 and then Is_Child_Unit (Curr_Scope)
3728 then
3729 Push_Scope (Curr_Scope);
3730 Set_Is_Immediately_Visible (Curr_Scope);
3732 -- Finally, restore inner scopes as well
3734 for J in reverse 1 .. Num_Inner loop
3735 Push_Scope (Inner_Scopes (J));
3736 end loop;
3737 end if;
3739 Restore_Scope_Stack (Handle_Use => False);
3741 if Present (Curr_Scope)
3742 and then
3743 (In_Private_Part (Curr_Scope)
3744 or else In_Package_Body (Curr_Scope))
3745 then
3746 -- Install private declaration of ancestor units, which are
3747 -- currently available. Restore_Scope_Stack and Install_Context
3748 -- only install the visible part of parents.
3750 declare
3751 Par : Entity_Id;
3752 begin
3753 Par := Scope (Curr_Scope);
3754 while (Present (Par))
3755 and then Par /= Standard_Standard
3756 loop
3757 Install_Private_Declarations (Par);
3758 Par := Scope (Par);
3759 end loop;
3760 end;
3761 end if;
3762 end if;
3764 -- Restore use clauses. For a child unit, use clauses in the parents
3765 -- are restored when installing the context, so only those in inner
3766 -- scopes (and those local to the child unit itself) need to be
3767 -- installed explicitly.
3769 if Is_Child_Unit (Curr_Unit)
3770 and then Removed
3771 then
3772 for J in reverse 1 .. Num_Inner + 1 loop
3773 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3774 Use_Clauses (J);
3775 Install_Use_Clauses (Use_Clauses (J));
3776 end loop;
3778 else
3779 for J in reverse 1 .. Num_Scopes loop
3780 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3781 Use_Clauses (J);
3782 Install_Use_Clauses (Use_Clauses (J));
3783 end loop;
3784 end if;
3786 -- Restore status of instances. If one of them is a body, make
3787 -- its local entities visible again.
3789 declare
3790 E : Entity_Id;
3791 Inst : Entity_Id;
3793 begin
3794 for J in 1 .. N_Instances loop
3795 Inst := Instances (J);
3796 Set_Is_Generic_Instance (Inst, True);
3798 if In_Package_Body (Inst)
3799 or else Ekind (S) = E_Procedure
3800 or else Ekind (S) = E_Function
3801 then
3802 E := First_Entity (Instances (J));
3803 while Present (E) loop
3804 Set_Is_Immediately_Visible (E);
3805 Next_Entity (E);
3806 end loop;
3807 end if;
3808 end loop;
3809 end;
3811 -- If generic unit is in current unit, current context is correct
3813 else
3814 Instantiate_Package_Body
3815 (Body_Info =>
3816 ((Inst_Node => N,
3817 Act_Decl => Act_Decl,
3818 Expander_Status => Expander_Active,
3819 Current_Sem_Unit => Current_Sem_Unit,
3820 Scope_Suppress => Scope_Suppress,
3821 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
3822 Version => Ada_Version)),
3823 Inlined_Body => True);
3824 end if;
3825 end Inline_Instance_Body;
3827 -------------------------------------
3828 -- Analyze_Procedure_Instantiation --
3829 -------------------------------------
3831 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
3832 begin
3833 Analyze_Subprogram_Instantiation (N, E_Procedure);
3834 end Analyze_Procedure_Instantiation;
3836 -----------------------------------
3837 -- Need_Subprogram_Instance_Body --
3838 -----------------------------------
3840 function Need_Subprogram_Instance_Body
3841 (N : Node_Id;
3842 Subp : Entity_Id) return Boolean
3844 begin
3845 if (Is_In_Main_Unit (N)
3846 or else Is_Inlined (Subp)
3847 or else Is_Inlined (Alias (Subp)))
3848 and then (Operating_Mode = Generate_Code
3849 or else (Operating_Mode = Check_Semantics
3850 and then ASIS_Mode))
3851 and then (Expander_Active or else ASIS_Mode)
3852 and then not ABE_Is_Certain (N)
3853 and then not Is_Eliminated (Subp)
3854 then
3855 Pending_Instantiations.Append
3856 ((Inst_Node => N,
3857 Act_Decl => Unit_Declaration_Node (Subp),
3858 Expander_Status => Expander_Active,
3859 Current_Sem_Unit => Current_Sem_Unit,
3860 Scope_Suppress => Scope_Suppress,
3861 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
3862 Version => Ada_Version));
3863 return True;
3864 else
3865 return False;
3866 end if;
3867 end Need_Subprogram_Instance_Body;
3869 --------------------------------------
3870 -- Analyze_Subprogram_Instantiation --
3871 --------------------------------------
3873 procedure Analyze_Subprogram_Instantiation
3874 (N : Node_Id;
3875 K : Entity_Kind)
3877 Loc : constant Source_Ptr := Sloc (N);
3878 Gen_Id : constant Node_Id := Name (N);
3880 Anon_Id : constant Entity_Id :=
3881 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
3882 Chars => New_External_Name
3883 (Chars (Defining_Entity (N)), 'R'));
3885 Act_Decl_Id : Entity_Id;
3886 Act_Decl : Node_Id;
3887 Act_Spec : Node_Id;
3888 Act_Tree : Node_Id;
3890 Env_Installed : Boolean := False;
3891 Gen_Unit : Entity_Id;
3892 Gen_Decl : Node_Id;
3893 Pack_Id : Entity_Id;
3894 Parent_Installed : Boolean := False;
3895 Renaming_List : List_Id;
3897 procedure Analyze_Instance_And_Renamings;
3898 -- The instance must be analyzed in a context that includes the mappings
3899 -- of generic parameters into actuals. We create a package declaration
3900 -- for this purpose, and a subprogram with an internal name within the
3901 -- package. The subprogram instance is simply an alias for the internal
3902 -- subprogram, declared in the current scope.
3904 ------------------------------------
3905 -- Analyze_Instance_And_Renamings --
3906 ------------------------------------
3908 procedure Analyze_Instance_And_Renamings is
3909 Def_Ent : constant Entity_Id := Defining_Entity (N);
3910 Pack_Decl : Node_Id;
3912 begin
3913 if Nkind (Parent (N)) = N_Compilation_Unit then
3915 -- For the case of a compilation unit, the container package has
3916 -- the same name as the instantiation, to insure that the binder
3917 -- calls the elaboration procedure with the right name. Copy the
3918 -- entity of the instance, which may have compilation level flags
3919 -- (e.g. Is_Child_Unit) set.
3921 Pack_Id := New_Copy (Def_Ent);
3923 else
3924 -- Otherwise we use the name of the instantiation concatenated
3925 -- with its source position to ensure uniqueness if there are
3926 -- several instantiations with the same name.
3928 Pack_Id :=
3929 Make_Defining_Identifier (Loc,
3930 Chars => New_External_Name
3931 (Related_Id => Chars (Def_Ent),
3932 Suffix => "GP",
3933 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
3934 end if;
3936 Pack_Decl := Make_Package_Declaration (Loc,
3937 Specification => Make_Package_Specification (Loc,
3938 Defining_Unit_Name => Pack_Id,
3939 Visible_Declarations => Renaming_List,
3940 End_Label => Empty));
3942 Set_Instance_Spec (N, Pack_Decl);
3943 Set_Is_Generic_Instance (Pack_Id);
3944 Set_Debug_Info_Needed (Pack_Id);
3946 -- Case of not a compilation unit
3948 if Nkind (Parent (N)) /= N_Compilation_Unit then
3949 Mark_Rewrite_Insertion (Pack_Decl);
3950 Insert_Before (N, Pack_Decl);
3951 Set_Has_Completion (Pack_Id);
3953 -- Case of an instantiation that is a compilation unit
3955 -- Place declaration on current node so context is complete for
3956 -- analysis (including nested instantiations), and for use in a
3957 -- context_clause (see Analyze_With_Clause).
3959 else
3960 Set_Unit (Parent (N), Pack_Decl);
3961 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
3962 end if;
3964 Analyze (Pack_Decl);
3965 Check_Formal_Packages (Pack_Id);
3966 Set_Is_Generic_Instance (Pack_Id, False);
3968 -- Body of the enclosing package is supplied when instantiating the
3969 -- subprogram body, after semantic analysis is completed.
3971 if Nkind (Parent (N)) = N_Compilation_Unit then
3973 -- Remove package itself from visibility, so it does not
3974 -- conflict with subprogram.
3976 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
3978 -- Set name and scope of internal subprogram so that the proper
3979 -- external name will be generated. The proper scope is the scope
3980 -- of the wrapper package. We need to generate debugging info for
3981 -- the internal subprogram, so set flag accordingly.
3983 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
3984 Set_Scope (Anon_Id, Scope (Pack_Id));
3986 -- Mark wrapper package as referenced, to avoid spurious warnings
3987 -- if the instantiation appears in various with_ clauses of
3988 -- subunits of the main unit.
3990 Set_Referenced (Pack_Id);
3991 end if;
3993 Set_Is_Generic_Instance (Anon_Id);
3994 Set_Debug_Info_Needed (Anon_Id);
3995 Act_Decl_Id := New_Copy (Anon_Id);
3997 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3998 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
3999 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
4000 Set_Comes_From_Source (Act_Decl_Id, True);
4002 -- The signature may involve types that are not frozen yet, but the
4003 -- subprogram will be frozen at the point the wrapper package is
4004 -- frozen, so it does not need its own freeze node. In fact, if one
4005 -- is created, it might conflict with the freezing actions from the
4006 -- wrapper package.
4008 Set_Has_Delayed_Freeze (Anon_Id, False);
4010 -- If the instance is a child unit, mark the Id accordingly. Mark
4011 -- the anonymous entity as well, which is the real subprogram and
4012 -- which is used when the instance appears in a context clause.
4013 -- Similarly, propagate the Is_Eliminated flag to handle properly
4014 -- nested eliminated subprograms.
4016 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
4017 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
4018 New_Overloaded_Entity (Act_Decl_Id);
4019 Check_Eliminated (Act_Decl_Id);
4020 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
4022 -- In compilation unit case, kill elaboration checks on the
4023 -- instantiation, since they are never needed -- the body is
4024 -- instantiated at the same point as the spec.
4026 if Nkind (Parent (N)) = N_Compilation_Unit then
4027 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4028 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4029 Set_Is_Compilation_Unit (Anon_Id);
4031 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
4032 end if;
4034 -- The instance is not a freezing point for the new subprogram
4036 Set_Is_Frozen (Act_Decl_Id, False);
4038 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
4039 Valid_Operator_Definition (Act_Decl_Id);
4040 end if;
4042 Set_Alias (Act_Decl_Id, Anon_Id);
4043 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4044 Set_Has_Completion (Act_Decl_Id);
4045 Set_Related_Instance (Pack_Id, Act_Decl_Id);
4047 if Nkind (Parent (N)) = N_Compilation_Unit then
4048 Set_Body_Required (Parent (N), False);
4049 end if;
4050 end Analyze_Instance_And_Renamings;
4052 -- Start of processing for Analyze_Subprogram_Instantiation
4054 begin
4055 -- Very first thing: apply the special kludge for Text_IO processing
4056 -- in case we are instantiating one of the children of [Wide_]Text_IO.
4057 -- Of course such an instantiation is bogus (these are packages, not
4058 -- subprograms), but we get a better error message if we do this.
4060 Text_IO_Kludge (Gen_Id);
4062 -- Make node global for error reporting
4064 Instantiation_Node := N;
4065 Preanalyze_Actuals (N);
4067 Init_Env;
4068 Env_Installed := True;
4069 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4070 Gen_Unit := Entity (Gen_Id);
4072 Generate_Reference (Gen_Unit, Gen_Id);
4074 if Nkind (Gen_Id) = N_Identifier
4075 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4076 then
4077 Error_Msg_NE
4078 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4079 end if;
4081 if Etype (Gen_Unit) = Any_Type then
4082 Restore_Env;
4083 return;
4084 end if;
4086 -- Verify that it is a generic subprogram of the right kind, and that
4087 -- it does not lead to a circular instantiation.
4089 if not Ekind_In (Gen_Unit, E_Generic_Procedure, E_Generic_Function) then
4090 Error_Msg_N ("expect generic subprogram in instantiation", Gen_Id);
4092 elsif In_Open_Scopes (Gen_Unit) then
4093 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4095 elsif K = E_Procedure
4096 and then Ekind (Gen_Unit) /= E_Generic_Procedure
4097 then
4098 if Ekind (Gen_Unit) = E_Generic_Function then
4099 Error_Msg_N
4100 ("cannot instantiate generic function as procedure", Gen_Id);
4101 else
4102 Error_Msg_N
4103 ("expect name of generic procedure in instantiation", Gen_Id);
4104 end if;
4106 elsif K = E_Function
4107 and then Ekind (Gen_Unit) /= E_Generic_Function
4108 then
4109 if Ekind (Gen_Unit) = E_Generic_Procedure then
4110 Error_Msg_N
4111 ("cannot instantiate generic procedure as function", Gen_Id);
4112 else
4113 Error_Msg_N
4114 ("expect name of generic function in instantiation", Gen_Id);
4115 end if;
4117 else
4118 Set_Entity (Gen_Id, Gen_Unit);
4119 Set_Is_Instantiated (Gen_Unit);
4121 if In_Extended_Main_Source_Unit (N) then
4122 Generate_Reference (Gen_Unit, N);
4123 end if;
4125 -- If renaming, get original unit
4127 if Present (Renamed_Object (Gen_Unit))
4128 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
4129 or else
4130 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
4131 then
4132 Gen_Unit := Renamed_Object (Gen_Unit);
4133 Set_Is_Instantiated (Gen_Unit);
4134 Generate_Reference (Gen_Unit, N);
4135 end if;
4137 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4138 Error_Msg_Node_2 := Current_Scope;
4139 Error_Msg_NE
4140 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4141 Circularity_Detected := True;
4142 return;
4143 end if;
4145 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4147 -- Initialize renamings map, for error checking
4149 Generic_Renamings.Set_Last (0);
4150 Generic_Renamings_HTable.Reset;
4152 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
4154 -- Copy original generic tree, to produce text for instantiation
4156 Act_Tree :=
4157 Copy_Generic_Node
4158 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4160 -- Inherit overriding indicator from instance node
4162 Act_Spec := Specification (Act_Tree);
4163 Set_Must_Override (Act_Spec, Must_Override (N));
4164 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
4166 Renaming_List :=
4167 Analyze_Associations
4169 Generic_Formal_Declarations (Act_Tree),
4170 Generic_Formal_Declarations (Gen_Decl));
4172 -- The subprogram itself cannot contain a nested instance, so the
4173 -- current parent is left empty.
4175 Set_Instance_Env (Gen_Unit, Empty);
4177 -- Build the subprogram declaration, which does not appear in the
4178 -- generic template, and give it a sloc consistent with that of the
4179 -- template.
4181 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
4182 Set_Generic_Parent (Act_Spec, Gen_Unit);
4183 Act_Decl :=
4184 Make_Subprogram_Declaration (Sloc (Act_Spec),
4185 Specification => Act_Spec);
4187 Set_Categorization_From_Pragmas (Act_Decl);
4189 if Parent_Installed then
4190 Hide_Current_Scope;
4191 end if;
4193 Append (Act_Decl, Renaming_List);
4194 Analyze_Instance_And_Renamings;
4196 -- If the generic is marked Import (Intrinsic), then so is the
4197 -- instance. This indicates that there is no body to instantiate. If
4198 -- generic is marked inline, so it the instance, and the anonymous
4199 -- subprogram it renames. If inlined, or else if inlining is enabled
4200 -- for the compilation, we generate the instance body even if it is
4201 -- not within the main unit.
4203 -- Any other pragmas might also be inherited ???
4205 if Is_Intrinsic_Subprogram (Gen_Unit) then
4206 Set_Is_Intrinsic_Subprogram (Anon_Id);
4207 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
4209 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
4210 Validate_Unchecked_Conversion (N, Act_Decl_Id);
4211 end if;
4212 end if;
4214 Generate_Definition (Act_Decl_Id);
4216 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
4217 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
4219 if not Is_Intrinsic_Subprogram (Gen_Unit) then
4220 Check_Elab_Instantiation (N);
4221 end if;
4223 if Is_Dispatching_Operation (Act_Decl_Id)
4224 and then Ada_Version >= Ada_05
4225 then
4226 declare
4227 Formal : Entity_Id;
4229 begin
4230 Formal := First_Formal (Act_Decl_Id);
4231 while Present (Formal) loop
4232 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
4233 and then Is_Controlling_Formal (Formal)
4234 and then not Can_Never_Be_Null (Formal)
4235 then
4236 Error_Msg_NE ("access parameter& is controlling,",
4237 N, Formal);
4238 Error_Msg_NE
4239 ("\corresponding parameter of & must be"
4240 & " explicitly null-excluding", N, Gen_Id);
4241 end if;
4243 Next_Formal (Formal);
4244 end loop;
4245 end;
4246 end if;
4248 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4250 -- Subject to change, pending on if other pragmas are inherited ???
4252 Validate_Categorization_Dependency (N, Act_Decl_Id);
4254 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
4255 Inherit_Context (Gen_Decl, N);
4257 Restore_Private_Views (Pack_Id, False);
4259 -- If the context requires a full instantiation, mark node for
4260 -- subsequent construction of the body.
4262 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
4264 Check_Forward_Instantiation (Gen_Decl);
4266 -- The wrapper package is always delayed, because it does not
4267 -- constitute a freeze point, but to insure that the freeze
4268 -- node is placed properly, it is created directly when
4269 -- instantiating the body (otherwise the freeze node might
4270 -- appear to early for nested instantiations).
4272 elsif Nkind (Parent (N)) = N_Compilation_Unit then
4274 -- For ASIS purposes, indicate that the wrapper package has
4275 -- replaced the instantiation node.
4277 Rewrite (N, Unit (Parent (N)));
4278 Set_Unit (Parent (N), N);
4279 end if;
4281 elsif Nkind (Parent (N)) = N_Compilation_Unit then
4283 -- Replace instance node for library-level instantiations of
4284 -- intrinsic subprograms, for ASIS use.
4286 Rewrite (N, Unit (Parent (N)));
4287 Set_Unit (Parent (N), N);
4288 end if;
4290 if Parent_Installed then
4291 Remove_Parent;
4292 end if;
4294 Restore_Env;
4295 Env_Installed := False;
4296 Generic_Renamings.Set_Last (0);
4297 Generic_Renamings_HTable.Reset;
4298 end if;
4300 exception
4301 when Instantiation_Error =>
4302 if Parent_Installed then
4303 Remove_Parent;
4304 end if;
4306 if Env_Installed then
4307 Restore_Env;
4308 end if;
4309 end Analyze_Subprogram_Instantiation;
4311 -------------------------
4312 -- Get_Associated_Node --
4313 -------------------------
4315 function Get_Associated_Node (N : Node_Id) return Node_Id is
4316 Assoc : Node_Id;
4318 begin
4319 Assoc := Associated_Node (N);
4321 if Nkind (Assoc) /= Nkind (N) then
4322 return Assoc;
4324 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
4325 return Assoc;
4327 else
4328 -- If the node is part of an inner generic, it may itself have been
4329 -- remapped into a further generic copy. Associated_Node is otherwise
4330 -- used for the entity of the node, and will be of a different node
4331 -- kind, or else N has been rewritten as a literal or function call.
4333 while Present (Associated_Node (Assoc))
4334 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
4335 loop
4336 Assoc := Associated_Node (Assoc);
4337 end loop;
4339 -- Follow and additional link in case the final node was rewritten.
4340 -- This can only happen with nested generic units.
4342 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
4343 and then Present (Associated_Node (Assoc))
4344 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
4345 N_Explicit_Dereference,
4346 N_Integer_Literal,
4347 N_Real_Literal,
4348 N_String_Literal))
4349 then
4350 Assoc := Associated_Node (Assoc);
4351 end if;
4353 return Assoc;
4354 end if;
4355 end Get_Associated_Node;
4357 -------------------------------------------
4358 -- Build_Instance_Compilation_Unit_Nodes --
4359 -------------------------------------------
4361 procedure Build_Instance_Compilation_Unit_Nodes
4362 (N : Node_Id;
4363 Act_Body : Node_Id;
4364 Act_Decl : Node_Id)
4366 Decl_Cunit : Node_Id;
4367 Body_Cunit : Node_Id;
4368 Citem : Node_Id;
4369 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
4370 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
4372 begin
4373 -- A new compilation unit node is built for the instance declaration
4375 Decl_Cunit :=
4376 Make_Compilation_Unit (Sloc (N),
4377 Context_Items => Empty_List,
4378 Unit => Act_Decl,
4379 Aux_Decls_Node =>
4380 Make_Compilation_Unit_Aux (Sloc (N)));
4382 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4384 -- The new compilation unit is linked to its body, but both share the
4385 -- same file, so we do not set Body_Required on the new unit so as not
4386 -- to create a spurious dependency on a non-existent body in the ali.
4387 -- This simplifies CodePeer unit traversal.
4389 -- We use the original instantiation compilation unit as the resulting
4390 -- compilation unit of the instance, since this is the main unit.
4392 Rewrite (N, Act_Body);
4393 Body_Cunit := Parent (N);
4395 -- The two compilation unit nodes are linked by the Library_Unit field
4397 Set_Library_Unit (Decl_Cunit, Body_Cunit);
4398 Set_Library_Unit (Body_Cunit, Decl_Cunit);
4400 -- Preserve the private nature of the package if needed
4402 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
4404 -- If the instance is not the main unit, its context, categorization
4405 -- and elaboration entity are not relevant to the compilation.
4407 if Body_Cunit /= Cunit (Main_Unit) then
4408 Make_Instance_Unit (Body_Cunit, In_Main => False);
4409 return;
4410 end if;
4412 -- The context clause items on the instantiation, which are now attached
4413 -- to the body compilation unit (since the body overwrote the original
4414 -- instantiation node), semantically belong on the spec, so copy them
4415 -- there. It's harmless to leave them on the body as well. In fact one
4416 -- could argue that they belong in both places.
4418 Citem := First (Context_Items (Body_Cunit));
4419 while Present (Citem) loop
4420 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
4421 Next (Citem);
4422 end loop;
4424 -- Propagate categorization flags on packages, so that they appear in
4425 -- the ali file for the spec of the unit.
4427 if Ekind (New_Main) = E_Package then
4428 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
4429 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
4430 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
4431 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
4432 Set_Is_Remote_Call_Interface
4433 (Old_Main, Is_Remote_Call_Interface (New_Main));
4434 end if;
4436 -- Make entry in Units table, so that binder can generate call to
4437 -- elaboration procedure for body, if any.
4439 Make_Instance_Unit (Body_Cunit, In_Main => True);
4440 Main_Unit_Entity := New_Main;
4441 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
4443 -- Build elaboration entity, since the instance may certainly generate
4444 -- elaboration code requiring a flag for protection.
4446 Build_Elaboration_Entity (Decl_Cunit, New_Main);
4447 end Build_Instance_Compilation_Unit_Nodes;
4449 -----------------------------
4450 -- Check_Access_Definition --
4451 -----------------------------
4453 procedure Check_Access_Definition (N : Node_Id) is
4454 begin
4455 pragma Assert
4456 (Ada_Version >= Ada_05
4457 and then Present (Access_Definition (N)));
4458 null;
4459 end Check_Access_Definition;
4461 -----------------------------------
4462 -- Check_Formal_Package_Instance --
4463 -----------------------------------
4465 -- If the formal has specific parameters, they must match those of the
4466 -- actual. Both of them are instances, and the renaming declarations for
4467 -- their formal parameters appear in the same order in both. The analyzed
4468 -- formal has been analyzed in the context of the current instance.
4470 procedure Check_Formal_Package_Instance
4471 (Formal_Pack : Entity_Id;
4472 Actual_Pack : Entity_Id)
4474 E1 : Entity_Id := First_Entity (Actual_Pack);
4475 E2 : Entity_Id := First_Entity (Formal_Pack);
4477 Expr1 : Node_Id;
4478 Expr2 : Node_Id;
4480 procedure Check_Mismatch (B : Boolean);
4481 -- Common error routine for mismatch between the parameters of the
4482 -- actual instance and those of the formal package.
4484 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
4485 -- The formal may come from a nested formal package, and the actual may
4486 -- have been constant-folded. To determine whether the two denote the
4487 -- same entity we may have to traverse several definitions to recover
4488 -- the ultimate entity that they refer to.
4490 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
4491 -- Similarly, if the formal comes from a nested formal package, the
4492 -- actual may designate the formal through multiple renamings, which
4493 -- have to be followed to determine the original variable in question.
4495 --------------------
4496 -- Check_Mismatch --
4497 --------------------
4499 procedure Check_Mismatch (B : Boolean) is
4500 Kind : constant Node_Kind := Nkind (Parent (E2));
4502 begin
4503 if Kind = N_Formal_Type_Declaration then
4504 return;
4506 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
4507 N_Formal_Package_Declaration)
4508 or else Kind in N_Formal_Subprogram_Declaration
4509 then
4510 null;
4512 elsif B then
4513 Error_Msg_NE
4514 ("actual for & in actual instance does not match formal",
4515 Parent (Actual_Pack), E1);
4516 end if;
4517 end Check_Mismatch;
4519 --------------------------------
4520 -- Same_Instantiated_Constant --
4521 --------------------------------
4523 function Same_Instantiated_Constant
4524 (E1, E2 : Entity_Id) return Boolean
4526 Ent : Entity_Id;
4528 begin
4529 Ent := E2;
4530 while Present (Ent) loop
4531 if E1 = Ent then
4532 return True;
4534 elsif Ekind (Ent) /= E_Constant then
4535 return False;
4537 elsif Is_Entity_Name (Constant_Value (Ent)) then
4538 if Entity (Constant_Value (Ent)) = E1 then
4539 return True;
4540 else
4541 Ent := Entity (Constant_Value (Ent));
4542 end if;
4544 -- The actual may be a constant that has been folded. Recover
4545 -- original name.
4547 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
4548 Ent := Entity (Original_Node (Constant_Value (Ent)));
4549 else
4550 return False;
4551 end if;
4552 end loop;
4554 return False;
4555 end Same_Instantiated_Constant;
4557 --------------------------------
4558 -- Same_Instantiated_Variable --
4559 --------------------------------
4561 function Same_Instantiated_Variable
4562 (E1, E2 : Entity_Id) return Boolean
4564 function Original_Entity (E : Entity_Id) return Entity_Id;
4565 -- Follow chain of renamings to the ultimate ancestor
4567 ---------------------
4568 -- Original_Entity --
4569 ---------------------
4571 function Original_Entity (E : Entity_Id) return Entity_Id is
4572 Orig : Entity_Id;
4574 begin
4575 Orig := E;
4576 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
4577 and then Present (Renamed_Object (Orig))
4578 and then Is_Entity_Name (Renamed_Object (Orig))
4579 loop
4580 Orig := Entity (Renamed_Object (Orig));
4581 end loop;
4583 return Orig;
4584 end Original_Entity;
4586 -- Start of processing for Same_Instantiated_Variable
4588 begin
4589 return Ekind (E1) = Ekind (E2)
4590 and then Original_Entity (E1) = Original_Entity (E2);
4591 end Same_Instantiated_Variable;
4593 -- Start of processing for Check_Formal_Package_Instance
4595 begin
4596 while Present (E1)
4597 and then Present (E2)
4598 loop
4599 exit when Ekind (E1) = E_Package
4600 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
4602 -- If the formal is the renaming of the formal package, this
4603 -- is the end of its formal part, which may occur before the
4604 -- end of the formal part in the actual in the presence of
4605 -- defaulted parameters in the formal package.
4607 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
4608 and then Renamed_Entity (E2) = Scope (E2);
4610 -- The analysis of the actual may generate additional internal
4611 -- entities. If the formal is defaulted, there is no corresponding
4612 -- analysis and the internal entities must be skipped, until we
4613 -- find corresponding entities again.
4615 if Comes_From_Source (E2)
4616 and then not Comes_From_Source (E1)
4617 and then Chars (E1) /= Chars (E2)
4618 then
4619 while Present (E1)
4620 and then Chars (E1) /= Chars (E2)
4621 loop
4622 Next_Entity (E1);
4623 end loop;
4624 end if;
4626 if No (E1) then
4627 return;
4629 -- If the formal entity comes from a formal declaration, it was
4630 -- defaulted in the formal package, and no check is needed on it.
4632 elsif Nkind (Parent (E2)) = N_Formal_Object_Declaration then
4633 goto Next_E;
4635 elsif Is_Type (E1) then
4637 -- Subtypes must statically match. E1, E2 are the local entities
4638 -- that are subtypes of the actuals. Itypes generated for other
4639 -- parameters need not be checked, the check will be performed
4640 -- on the parameters themselves.
4642 -- If E2 is a formal type declaration, it is a defaulted parameter
4643 -- and needs no checking.
4645 if not Is_Itype (E1)
4646 and then not Is_Itype (E2)
4647 then
4648 Check_Mismatch
4649 (not Is_Type (E2)
4650 or else Etype (E1) /= Etype (E2)
4651 or else not Subtypes_Statically_Match (E1, E2));
4652 end if;
4654 elsif Ekind (E1) = E_Constant then
4656 -- IN parameters must denote the same static value, or the same
4657 -- constant, or the literal null.
4659 Expr1 := Expression (Parent (E1));
4661 if Ekind (E2) /= E_Constant then
4662 Check_Mismatch (True);
4663 goto Next_E;
4664 else
4665 Expr2 := Expression (Parent (E2));
4666 end if;
4668 if Is_Static_Expression (Expr1) then
4670 if not Is_Static_Expression (Expr2) then
4671 Check_Mismatch (True);
4673 elsif Is_Discrete_Type (Etype (E1)) then
4674 declare
4675 V1 : constant Uint := Expr_Value (Expr1);
4676 V2 : constant Uint := Expr_Value (Expr2);
4677 begin
4678 Check_Mismatch (V1 /= V2);
4679 end;
4681 elsif Is_Real_Type (Etype (E1)) then
4682 declare
4683 V1 : constant Ureal := Expr_Value_R (Expr1);
4684 V2 : constant Ureal := Expr_Value_R (Expr2);
4685 begin
4686 Check_Mismatch (V1 /= V2);
4687 end;
4689 elsif Is_String_Type (Etype (E1))
4690 and then Nkind (Expr1) = N_String_Literal
4691 then
4692 if Nkind (Expr2) /= N_String_Literal then
4693 Check_Mismatch (True);
4694 else
4695 Check_Mismatch
4696 (not String_Equal (Strval (Expr1), Strval (Expr2)));
4697 end if;
4698 end if;
4700 elsif Is_Entity_Name (Expr1) then
4701 if Is_Entity_Name (Expr2) then
4702 if Entity (Expr1) = Entity (Expr2) then
4703 null;
4704 else
4705 Check_Mismatch
4706 (not Same_Instantiated_Constant
4707 (Entity (Expr1), Entity (Expr2)));
4708 end if;
4709 else
4710 Check_Mismatch (True);
4711 end if;
4713 elsif Is_Entity_Name (Original_Node (Expr1))
4714 and then Is_Entity_Name (Expr2)
4715 and then
4716 Same_Instantiated_Constant
4717 (Entity (Original_Node (Expr1)), Entity (Expr2))
4718 then
4719 null;
4721 elsif Nkind (Expr1) = N_Null then
4722 Check_Mismatch (Nkind (Expr1) /= N_Null);
4724 else
4725 Check_Mismatch (True);
4726 end if;
4728 elsif Ekind (E1) = E_Variable then
4729 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
4731 elsif Ekind (E1) = E_Package then
4732 Check_Mismatch
4733 (Ekind (E1) /= Ekind (E2)
4734 or else Renamed_Object (E1) /= Renamed_Object (E2));
4736 elsif Is_Overloadable (E1) then
4738 -- Verify that the actual subprograms match. Note that actuals
4739 -- that are attributes are rewritten as subprograms. If the
4740 -- subprogram in the formal package is defaulted, no check is
4741 -- needed. Note that this can only happen in Ada 2005 when the
4742 -- formal package can be partially parametrized.
4744 if Nkind (Unit_Declaration_Node (E1)) =
4745 N_Subprogram_Renaming_Declaration
4746 and then From_Default (Unit_Declaration_Node (E1))
4747 then
4748 null;
4750 else
4751 Check_Mismatch
4752 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
4753 end if;
4755 else
4756 raise Program_Error;
4757 end if;
4759 <<Next_E>>
4760 Next_Entity (E1);
4761 Next_Entity (E2);
4762 end loop;
4763 end Check_Formal_Package_Instance;
4765 ---------------------------
4766 -- Check_Formal_Packages --
4767 ---------------------------
4769 procedure Check_Formal_Packages (P_Id : Entity_Id) is
4770 E : Entity_Id;
4771 Formal_P : Entity_Id;
4773 begin
4774 -- Iterate through the declarations in the instance, looking for package
4775 -- renaming declarations that denote instances of formal packages. Stop
4776 -- when we find the renaming of the current package itself. The
4777 -- declaration for a formal package without a box is followed by an
4778 -- internal entity that repeats the instantiation.
4780 E := First_Entity (P_Id);
4781 while Present (E) loop
4782 if Ekind (E) = E_Package then
4783 if Renamed_Object (E) = P_Id then
4784 exit;
4786 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
4787 null;
4789 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
4790 Formal_P := Next_Entity (E);
4791 Check_Formal_Package_Instance (Formal_P, E);
4793 -- After checking, remove the internal validating package. It
4794 -- is only needed for semantic checks, and as it may contain
4795 -- generic formal declarations it should not reach gigi.
4797 Remove (Unit_Declaration_Node (Formal_P));
4798 end if;
4799 end if;
4801 Next_Entity (E);
4802 end loop;
4803 end Check_Formal_Packages;
4805 ---------------------------------
4806 -- Check_Forward_Instantiation --
4807 ---------------------------------
4809 procedure Check_Forward_Instantiation (Decl : Node_Id) is
4810 S : Entity_Id;
4811 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
4813 begin
4814 -- The instantiation appears before the generic body if we are in the
4815 -- scope of the unit containing the generic, either in its spec or in
4816 -- the package body, and before the generic body.
4818 if Ekind (Gen_Comp) = E_Package_Body then
4819 Gen_Comp := Spec_Entity (Gen_Comp);
4820 end if;
4822 if In_Open_Scopes (Gen_Comp)
4823 and then No (Corresponding_Body (Decl))
4824 then
4825 S := Current_Scope;
4827 while Present (S)
4828 and then not Is_Compilation_Unit (S)
4829 and then not Is_Child_Unit (S)
4830 loop
4831 if Ekind (S) = E_Package then
4832 Set_Has_Forward_Instantiation (S);
4833 end if;
4835 S := Scope (S);
4836 end loop;
4837 end if;
4838 end Check_Forward_Instantiation;
4840 ---------------------------
4841 -- Check_Generic_Actuals --
4842 ---------------------------
4844 -- The visibility of the actuals may be different between the point of
4845 -- generic instantiation and the instantiation of the body.
4847 procedure Check_Generic_Actuals
4848 (Instance : Entity_Id;
4849 Is_Formal_Box : Boolean)
4851 E : Entity_Id;
4852 Astype : Entity_Id;
4854 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
4855 -- For a formal that is an array type, the component type is often a
4856 -- previous formal in the same unit. The privacy status of the component
4857 -- type will have been examined earlier in the traversal of the
4858 -- corresponding actuals, and this status should not be modified for the
4859 -- array type itself.
4861 -- To detect this case we have to rescan the list of formals, which
4862 -- is usually short enough to ignore the resulting inefficiency.
4864 -----------------------------
4865 -- Denotes_Previous_Actual --
4866 -----------------------------
4868 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
4869 Prev : Entity_Id;
4871 begin
4872 Prev := First_Entity (Instance);
4873 while Present (Prev) loop
4874 if Is_Type (Prev)
4875 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
4876 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
4877 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
4878 then
4879 return True;
4881 elsif Prev = E then
4882 return False;
4884 else
4885 Next_Entity (Prev);
4886 end if;
4887 end loop;
4889 return False;
4890 end Denotes_Previous_Actual;
4892 -- Start of processing for Check_Generic_Actuals
4894 begin
4895 E := First_Entity (Instance);
4896 while Present (E) loop
4897 if Is_Type (E)
4898 and then Nkind (Parent (E)) = N_Subtype_Declaration
4899 and then Scope (Etype (E)) /= Instance
4900 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
4901 then
4902 if Is_Array_Type (E)
4903 and then Denotes_Previous_Actual (Component_Type (E))
4904 then
4905 null;
4906 else
4907 Check_Private_View (Subtype_Indication (Parent (E)));
4908 end if;
4909 Set_Is_Generic_Actual_Type (E, True);
4910 Set_Is_Hidden (E, False);
4911 Set_Is_Potentially_Use_Visible (E,
4912 In_Use (Instance));
4914 -- We constructed the generic actual type as a subtype of the
4915 -- supplied type. This means that it normally would not inherit
4916 -- subtype specific attributes of the actual, which is wrong for
4917 -- the generic case.
4919 Astype := Ancestor_Subtype (E);
4921 if No (Astype) then
4923 -- This can happen when E is an itype that is the full view of
4924 -- a private type completed, e.g. with a constrained array. In
4925 -- that case, use the first subtype, which will carry size
4926 -- information. The base type itself is unconstrained and will
4927 -- not carry it.
4929 Astype := First_Subtype (E);
4930 end if;
4932 Set_Size_Info (E, (Astype));
4933 Set_RM_Size (E, RM_Size (Astype));
4934 Set_First_Rep_Item (E, First_Rep_Item (Astype));
4936 if Is_Discrete_Or_Fixed_Point_Type (E) then
4937 Set_RM_Size (E, RM_Size (Astype));
4939 -- In nested instances, the base type of an access actual
4940 -- may itself be private, and need to be exchanged.
4942 elsif Is_Access_Type (E)
4943 and then Is_Private_Type (Etype (E))
4944 then
4945 Check_Private_View
4946 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
4947 end if;
4949 elsif Ekind (E) = E_Package then
4951 -- If this is the renaming for the current instance, we're done.
4952 -- Otherwise it is a formal package. If the corresponding formal
4953 -- was declared with a box, the (instantiations of the) generic
4954 -- formal part are also visible. Otherwise, ignore the entity
4955 -- created to validate the actuals.
4957 if Renamed_Object (E) = Instance then
4958 exit;
4960 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
4961 null;
4963 -- The visibility of a formal of an enclosing generic is already
4964 -- correct.
4966 elsif Denotes_Formal_Package (E) then
4967 null;
4969 elsif Present (Associated_Formal_Package (E))
4970 and then not Is_Generic_Formal (E)
4971 then
4972 if Box_Present (Parent (Associated_Formal_Package (E))) then
4973 Check_Generic_Actuals (Renamed_Object (E), True);
4975 else
4976 Check_Generic_Actuals (Renamed_Object (E), False);
4977 end if;
4979 Set_Is_Hidden (E, False);
4980 end if;
4982 -- If this is a subprogram instance (in a wrapper package) the
4983 -- actual is fully visible.
4985 elsif Is_Wrapper_Package (Instance) then
4986 Set_Is_Hidden (E, False);
4988 -- If the formal package is declared with a box, or if the formal
4989 -- parameter is defaulted, it is visible in the body.
4991 elsif Is_Formal_Box
4992 or else Is_Visible_Formal (E)
4993 then
4994 Set_Is_Hidden (E, False);
4995 end if;
4997 Next_Entity (E);
4998 end loop;
4999 end Check_Generic_Actuals;
5001 ------------------------------
5002 -- Check_Generic_Child_Unit --
5003 ------------------------------
5005 procedure Check_Generic_Child_Unit
5006 (Gen_Id : Node_Id;
5007 Parent_Installed : in out Boolean)
5009 Loc : constant Source_Ptr := Sloc (Gen_Id);
5010 Gen_Par : Entity_Id := Empty;
5011 E : Entity_Id;
5012 Inst_Par : Entity_Id;
5013 S : Node_Id;
5015 function Find_Generic_Child
5016 (Scop : Entity_Id;
5017 Id : Node_Id) return Entity_Id;
5018 -- Search generic parent for possible child unit with the given name
5020 function In_Enclosing_Instance return Boolean;
5021 -- Within an instance of the parent, the child unit may be denoted
5022 -- by a simple name, or an abbreviated expanded name. Examine enclosing
5023 -- scopes to locate a possible parent instantiation.
5025 ------------------------
5026 -- Find_Generic_Child --
5027 ------------------------
5029 function Find_Generic_Child
5030 (Scop : Entity_Id;
5031 Id : Node_Id) return Entity_Id
5033 E : Entity_Id;
5035 begin
5036 -- If entity of name is already set, instance has already been
5037 -- resolved, e.g. in an enclosing instantiation.
5039 if Present (Entity (Id)) then
5040 if Scope (Entity (Id)) = Scop then
5041 return Entity (Id);
5042 else
5043 return Empty;
5044 end if;
5046 else
5047 E := First_Entity (Scop);
5048 while Present (E) loop
5049 if Chars (E) = Chars (Id)
5050 and then Is_Child_Unit (E)
5051 then
5052 if Is_Child_Unit (E)
5053 and then not Is_Visible_Child_Unit (E)
5054 then
5055 Error_Msg_NE
5056 ("generic child unit& is not visible", Gen_Id, E);
5057 end if;
5059 Set_Entity (Id, E);
5060 return E;
5061 end if;
5063 Next_Entity (E);
5064 end loop;
5066 return Empty;
5067 end if;
5068 end Find_Generic_Child;
5070 ---------------------------
5071 -- In_Enclosing_Instance --
5072 ---------------------------
5074 function In_Enclosing_Instance return Boolean is
5075 Enclosing_Instance : Node_Id;
5076 Instance_Decl : Node_Id;
5078 begin
5079 -- We do not inline any call that contains instantiations, except
5080 -- for instantiations of Unchecked_Conversion, so if we are within
5081 -- an inlined body the current instance does not require parents.
5083 if In_Inlined_Body then
5084 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
5085 return False;
5086 end if;
5088 -- Loop to check enclosing scopes
5090 Enclosing_Instance := Current_Scope;
5091 while Present (Enclosing_Instance) loop
5092 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
5094 if Ekind (Enclosing_Instance) = E_Package
5095 and then Is_Generic_Instance (Enclosing_Instance)
5096 and then Present
5097 (Generic_Parent (Specification (Instance_Decl)))
5098 then
5099 -- Check whether the generic we are looking for is a child of
5100 -- this instance.
5102 E := Find_Generic_Child
5103 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
5104 exit when Present (E);
5106 else
5107 E := Empty;
5108 end if;
5110 Enclosing_Instance := Scope (Enclosing_Instance);
5111 end loop;
5113 if No (E) then
5115 -- Not a child unit
5117 Analyze (Gen_Id);
5118 return False;
5120 else
5121 Rewrite (Gen_Id,
5122 Make_Expanded_Name (Loc,
5123 Chars => Chars (E),
5124 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
5125 Selector_Name => New_Occurrence_Of (E, Loc)));
5127 Set_Entity (Gen_Id, E);
5128 Set_Etype (Gen_Id, Etype (E));
5129 Parent_Installed := False; -- Already in scope.
5130 return True;
5131 end if;
5132 end In_Enclosing_Instance;
5134 -- Start of processing for Check_Generic_Child_Unit
5136 begin
5137 -- If the name of the generic is given by a selected component, it may
5138 -- be the name of a generic child unit, and the prefix is the name of an
5139 -- instance of the parent, in which case the child unit must be visible.
5140 -- If this instance is not in scope, it must be placed there and removed
5141 -- after instantiation, because what is being instantiated is not the
5142 -- original child, but the corresponding child present in the instance
5143 -- of the parent.
5145 -- If the child is instantiated within the parent, it can be given by
5146 -- a simple name. In this case the instance is already in scope, but
5147 -- the child generic must be recovered from the generic parent as well.
5149 if Nkind (Gen_Id) = N_Selected_Component then
5150 S := Selector_Name (Gen_Id);
5151 Analyze (Prefix (Gen_Id));
5152 Inst_Par := Entity (Prefix (Gen_Id));
5154 if Ekind (Inst_Par) = E_Package
5155 and then Present (Renamed_Object (Inst_Par))
5156 then
5157 Inst_Par := Renamed_Object (Inst_Par);
5158 end if;
5160 if Ekind (Inst_Par) = E_Package then
5161 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
5162 Gen_Par := Generic_Parent (Parent (Inst_Par));
5164 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
5165 and then
5166 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
5167 then
5168 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
5169 end if;
5171 elsif Ekind (Inst_Par) = E_Generic_Package
5172 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
5173 then
5174 -- A formal package may be a real child package, and not the
5175 -- implicit instance within a parent. In this case the child is
5176 -- not visible and has to be retrieved explicitly as well.
5178 Gen_Par := Inst_Par;
5179 end if;
5181 if Present (Gen_Par) then
5183 -- The prefix denotes an instantiation. The entity itself may be a
5184 -- nested generic, or a child unit.
5186 E := Find_Generic_Child (Gen_Par, S);
5188 if Present (E) then
5189 Change_Selected_Component_To_Expanded_Name (Gen_Id);
5190 Set_Entity (Gen_Id, E);
5191 Set_Etype (Gen_Id, Etype (E));
5192 Set_Entity (S, E);
5193 Set_Etype (S, Etype (E));
5195 -- Indicate that this is a reference to the parent
5197 if In_Extended_Main_Source_Unit (Gen_Id) then
5198 Set_Is_Instantiated (Inst_Par);
5199 end if;
5201 -- A common mistake is to replicate the naming scheme of a
5202 -- hierarchy by instantiating a generic child directly, rather
5203 -- than the implicit child in a parent instance:
5205 -- generic .. package Gpar is ..
5206 -- generic .. package Gpar.Child is ..
5207 -- package Par is new Gpar ();
5209 -- with Gpar.Child;
5210 -- package Par.Child is new Gpar.Child ();
5211 -- rather than Par.Child
5213 -- In this case the instantiation is within Par, which is an
5214 -- instance, but Gpar does not denote Par because we are not IN
5215 -- the instance of Gpar, so this is illegal. The test below
5216 -- recognizes this particular case.
5218 if Is_Child_Unit (E)
5219 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
5220 and then (not In_Instance
5221 or else Nkind (Parent (Parent (Gen_Id))) =
5222 N_Compilation_Unit)
5223 then
5224 Error_Msg_N
5225 ("prefix of generic child unit must be instance of parent",
5226 Gen_Id);
5227 end if;
5229 if not In_Open_Scopes (Inst_Par)
5230 and then Nkind (Parent (Gen_Id)) not in
5231 N_Generic_Renaming_Declaration
5232 then
5233 Install_Parent (Inst_Par);
5234 Parent_Installed := True;
5236 elsif In_Open_Scopes (Inst_Par) then
5238 -- If the parent is already installed, install the actuals
5239 -- for its formal packages. This is necessary when the
5240 -- child instance is a child of the parent instance:
5241 -- in this case, the parent is placed on the scope stack
5242 -- but the formal packages are not made visible.
5244 Install_Formal_Packages (Inst_Par);
5245 end if;
5247 else
5248 -- If the generic parent does not contain an entity that
5249 -- corresponds to the selector, the instance doesn't either.
5250 -- Analyzing the node will yield the appropriate error message.
5251 -- If the entity is not a child unit, then it is an inner
5252 -- generic in the parent.
5254 Analyze (Gen_Id);
5255 end if;
5257 else
5258 Analyze (Gen_Id);
5260 if Is_Child_Unit (Entity (Gen_Id))
5261 and then
5262 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
5263 and then not In_Open_Scopes (Inst_Par)
5264 then
5265 Install_Parent (Inst_Par);
5266 Parent_Installed := True;
5267 end if;
5268 end if;
5270 elsif Nkind (Gen_Id) = N_Expanded_Name then
5272 -- Entity already present, analyze prefix, whose meaning may be
5273 -- an instance in the current context. If it is an instance of
5274 -- a relative within another, the proper parent may still have
5275 -- to be installed, if they are not of the same generation.
5277 Analyze (Prefix (Gen_Id));
5279 -- In the unlikely case that a local declaration hides the name
5280 -- of the parent package, locate it on the homonym chain. If the
5281 -- context is an instance of the parent, the renaming entity is
5282 -- flagged as such.
5284 Inst_Par := Entity (Prefix (Gen_Id));
5285 while Present (Inst_Par)
5286 and then not Is_Package_Or_Generic_Package (Inst_Par)
5287 loop
5288 Inst_Par := Homonym (Inst_Par);
5289 end loop;
5291 pragma Assert (Present (Inst_Par));
5292 Set_Entity (Prefix (Gen_Id), Inst_Par);
5294 if In_Enclosing_Instance then
5295 null;
5297 elsif Present (Entity (Gen_Id))
5298 and then Is_Child_Unit (Entity (Gen_Id))
5299 and then not In_Open_Scopes (Inst_Par)
5300 then
5301 Install_Parent (Inst_Par);
5302 Parent_Installed := True;
5303 end if;
5305 elsif In_Enclosing_Instance then
5307 -- The child unit is found in some enclosing scope
5309 null;
5311 else
5312 Analyze (Gen_Id);
5314 -- If this is the renaming of the implicit child in a parent
5315 -- instance, recover the parent name and install it.
5317 if Is_Entity_Name (Gen_Id) then
5318 E := Entity (Gen_Id);
5320 if Is_Generic_Unit (E)
5321 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
5322 and then Is_Child_Unit (Renamed_Object (E))
5323 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
5324 and then Nkind (Name (Parent (E))) = N_Expanded_Name
5325 then
5326 Rewrite (Gen_Id,
5327 New_Copy_Tree (Name (Parent (E))));
5328 Inst_Par := Entity (Prefix (Gen_Id));
5330 if not In_Open_Scopes (Inst_Par) then
5331 Install_Parent (Inst_Par);
5332 Parent_Installed := True;
5333 end if;
5335 -- If it is a child unit of a non-generic parent, it may be
5336 -- use-visible and given by a direct name. Install parent as
5337 -- for other cases.
5339 elsif Is_Generic_Unit (E)
5340 and then Is_Child_Unit (E)
5341 and then
5342 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
5343 and then not Is_Generic_Unit (Scope (E))
5344 then
5345 if not In_Open_Scopes (Scope (E)) then
5346 Install_Parent (Scope (E));
5347 Parent_Installed := True;
5348 end if;
5349 end if;
5350 end if;
5351 end if;
5352 end Check_Generic_Child_Unit;
5354 -----------------------------
5355 -- Check_Hidden_Child_Unit --
5356 -----------------------------
5358 procedure Check_Hidden_Child_Unit
5359 (N : Node_Id;
5360 Gen_Unit : Entity_Id;
5361 Act_Decl_Id : Entity_Id)
5363 Gen_Id : constant Node_Id := Name (N);
5365 begin
5366 if Is_Child_Unit (Gen_Unit)
5367 and then Is_Child_Unit (Act_Decl_Id)
5368 and then Nkind (Gen_Id) = N_Expanded_Name
5369 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
5370 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
5371 then
5372 Error_Msg_Node_2 := Scope (Act_Decl_Id);
5373 Error_Msg_NE
5374 ("generic unit & is implicitly declared in &",
5375 Defining_Unit_Name (N), Gen_Unit);
5376 Error_Msg_N ("\instance must have different name",
5377 Defining_Unit_Name (N));
5378 end if;
5379 end Check_Hidden_Child_Unit;
5381 ------------------------
5382 -- Check_Private_View --
5383 ------------------------
5385 procedure Check_Private_View (N : Node_Id) is
5386 T : constant Entity_Id := Etype (N);
5387 BT : Entity_Id;
5389 begin
5390 -- Exchange views if the type was not private in the generic but is
5391 -- private at the point of instantiation. Do not exchange views if
5392 -- the scope of the type is in scope. This can happen if both generic
5393 -- and instance are sibling units, or if type is defined in a parent.
5394 -- In this case the visibility of the type will be correct for all
5395 -- semantic checks.
5397 if Present (T) then
5398 BT := Base_Type (T);
5400 if Is_Private_Type (T)
5401 and then not Has_Private_View (N)
5402 and then Present (Full_View (T))
5403 and then not In_Open_Scopes (Scope (T))
5404 then
5405 -- In the generic, the full type was visible. Save the private
5406 -- entity, for subsequent exchange.
5408 Switch_View (T);
5410 elsif Has_Private_View (N)
5411 and then not Is_Private_Type (T)
5412 and then not Has_Been_Exchanged (T)
5413 and then Etype (Get_Associated_Node (N)) /= T
5414 then
5415 -- Only the private declaration was visible in the generic. If
5416 -- the type appears in a subtype declaration, the subtype in the
5417 -- instance must have a view compatible with that of its parent,
5418 -- which must be exchanged (see corresponding code in Restore_
5419 -- Private_Views). Otherwise, if the type is defined in a parent
5420 -- unit, leave full visibility within instance, which is safe.
5422 if In_Open_Scopes (Scope (Base_Type (T)))
5423 and then not Is_Private_Type (Base_Type (T))
5424 and then Comes_From_Source (Base_Type (T))
5425 then
5426 null;
5428 elsif Nkind (Parent (N)) = N_Subtype_Declaration
5429 or else not In_Private_Part (Scope (Base_Type (T)))
5430 then
5431 Prepend_Elmt (T, Exchanged_Views);
5432 Exchange_Declarations (Etype (Get_Associated_Node (N)));
5433 end if;
5435 -- For composite types with inconsistent representation exchange
5436 -- component types accordingly.
5438 elsif Is_Access_Type (T)
5439 and then Is_Private_Type (Designated_Type (T))
5440 and then not Has_Private_View (N)
5441 and then Present (Full_View (Designated_Type (T)))
5442 then
5443 Switch_View (Designated_Type (T));
5445 elsif Is_Array_Type (T) then
5446 if Is_Private_Type (Component_Type (T))
5447 and then not Has_Private_View (N)
5448 and then Present (Full_View (Component_Type (T)))
5449 then
5450 Switch_View (Component_Type (T));
5451 end if;
5453 -- The normal exchange mechanism relies on the setting of a
5454 -- flag on the reference in the generic. However, an additional
5455 -- mechanism is needed for types that are not explicitly mentioned
5456 -- in the generic, but may be needed in expanded code in the
5457 -- instance. This includes component types of arrays and
5458 -- designated types of access types. This processing must also
5459 -- include the index types of arrays which we take care of here.
5461 declare
5462 Indx : Node_Id;
5463 Typ : Entity_Id;
5465 begin
5466 Indx := First_Index (T);
5467 Typ := Base_Type (Etype (Indx));
5468 while Present (Indx) loop
5469 if Is_Private_Type (Typ)
5470 and then Present (Full_View (Typ))
5471 then
5472 Switch_View (Typ);
5473 end if;
5475 Next_Index (Indx);
5476 end loop;
5477 end;
5479 elsif Is_Private_Type (T)
5480 and then Present (Full_View (T))
5481 and then Is_Array_Type (Full_View (T))
5482 and then Is_Private_Type (Component_Type (Full_View (T)))
5483 then
5484 Switch_View (T);
5486 -- Finally, a non-private subtype may have a private base type, which
5487 -- must be exchanged for consistency. This can happen when a package
5488 -- body is instantiated, when the scope stack is empty but in fact
5489 -- the subtype and the base type are declared in an enclosing scope.
5491 -- Note that in this case we introduce an inconsistency in the view
5492 -- set, because we switch the base type BT, but there could be some
5493 -- private dependent subtypes of BT which remain unswitched. Such
5494 -- subtypes might need to be switched at a later point (see specific
5495 -- provision for that case in Switch_View).
5497 elsif not Is_Private_Type (T)
5498 and then not Has_Private_View (N)
5499 and then Is_Private_Type (BT)
5500 and then Present (Full_View (BT))
5501 and then not Is_Generic_Type (BT)
5502 and then not In_Open_Scopes (BT)
5503 then
5504 Prepend_Elmt (Full_View (BT), Exchanged_Views);
5505 Exchange_Declarations (BT);
5506 end if;
5507 end if;
5508 end Check_Private_View;
5510 --------------------------
5511 -- Contains_Instance_Of --
5512 --------------------------
5514 function Contains_Instance_Of
5515 (Inner : Entity_Id;
5516 Outer : Entity_Id;
5517 N : Node_Id) return Boolean
5519 Elmt : Elmt_Id;
5520 Scop : Entity_Id;
5522 begin
5523 Scop := Outer;
5525 -- Verify that there are no circular instantiations. We check whether
5526 -- the unit contains an instance of the current scope or some enclosing
5527 -- scope (in case one of the instances appears in a subunit). Longer
5528 -- circularities involving subunits might seem too pathological to
5529 -- consider, but they were not too pathological for the authors of
5530 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
5531 -- enclosing generic scopes as containing an instance.
5533 loop
5534 -- Within a generic subprogram body, the scope is not generic, to
5535 -- allow for recursive subprograms. Use the declaration to determine
5536 -- whether this is a generic unit.
5538 if Ekind (Scop) = E_Generic_Package
5539 or else (Is_Subprogram (Scop)
5540 and then Nkind (Unit_Declaration_Node (Scop)) =
5541 N_Generic_Subprogram_Declaration)
5542 then
5543 Elmt := First_Elmt (Inner_Instances (Inner));
5545 while Present (Elmt) loop
5546 if Node (Elmt) = Scop then
5547 Error_Msg_Node_2 := Inner;
5548 Error_Msg_NE
5549 ("circular Instantiation: & instantiated within &!",
5550 N, Scop);
5551 return True;
5553 elsif Node (Elmt) = Inner then
5554 return True;
5556 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
5557 Error_Msg_Node_2 := Inner;
5558 Error_Msg_NE
5559 ("circular Instantiation: & instantiated within &!",
5560 N, Node (Elmt));
5561 return True;
5562 end if;
5564 Next_Elmt (Elmt);
5565 end loop;
5567 -- Indicate that Inner is being instantiated within Scop
5569 Append_Elmt (Inner, Inner_Instances (Scop));
5570 end if;
5572 if Scop = Standard_Standard then
5573 exit;
5574 else
5575 Scop := Scope (Scop);
5576 end if;
5577 end loop;
5579 return False;
5580 end Contains_Instance_Of;
5582 -----------------------
5583 -- Copy_Generic_Node --
5584 -----------------------
5586 function Copy_Generic_Node
5587 (N : Node_Id;
5588 Parent_Id : Node_Id;
5589 Instantiating : Boolean) return Node_Id
5591 Ent : Entity_Id;
5592 New_N : Node_Id;
5594 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
5595 -- Check the given value of one of the Fields referenced by the
5596 -- current node to determine whether to copy it recursively. The
5597 -- field may hold a Node_Id, a List_Id, or an Elist_Id, or a plain
5598 -- value (Sloc, Uint, Char) in which case it need not be copied.
5600 procedure Copy_Descendants;
5601 -- Common utility for various nodes
5603 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
5604 -- Make copy of element list
5606 function Copy_Generic_List
5607 (L : List_Id;
5608 Parent_Id : Node_Id) return List_Id;
5609 -- Apply Copy_Node recursively to the members of a node list
5611 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
5612 -- True if an identifier is part of the defining program unit name
5613 -- of a child unit. The entity of such an identifier must be kept
5614 -- (for ASIS use) even though as the name of an enclosing generic
5615 -- it would otherwise not be preserved in the generic tree.
5617 ----------------------
5618 -- Copy_Descendants --
5619 ----------------------
5621 procedure Copy_Descendants is
5623 use Atree.Unchecked_Access;
5624 -- This code section is part of the implementation of an untyped
5625 -- tree traversal, so it needs direct access to node fields.
5627 begin
5628 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
5629 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
5630 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
5631 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
5632 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
5633 end Copy_Descendants;
5635 -----------------------------
5636 -- Copy_Generic_Descendant --
5637 -----------------------------
5639 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
5640 begin
5641 if D = Union_Id (Empty) then
5642 return D;
5644 elsif D in Node_Range then
5645 return Union_Id
5646 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
5648 elsif D in List_Range then
5649 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
5651 elsif D in Elist_Range then
5652 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
5654 -- Nothing else is copyable (e.g. Uint values), return as is
5656 else
5657 return D;
5658 end if;
5659 end Copy_Generic_Descendant;
5661 ------------------------
5662 -- Copy_Generic_Elist --
5663 ------------------------
5665 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
5666 M : Elmt_Id;
5667 L : Elist_Id;
5669 begin
5670 if Present (E) then
5671 L := New_Elmt_List;
5672 M := First_Elmt (E);
5673 while Present (M) loop
5674 Append_Elmt
5675 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
5676 Next_Elmt (M);
5677 end loop;
5679 return L;
5681 else
5682 return No_Elist;
5683 end if;
5684 end Copy_Generic_Elist;
5686 -----------------------
5687 -- Copy_Generic_List --
5688 -----------------------
5690 function Copy_Generic_List
5691 (L : List_Id;
5692 Parent_Id : Node_Id) return List_Id
5694 N : Node_Id;
5695 New_L : List_Id;
5697 begin
5698 if Present (L) then
5699 New_L := New_List;
5700 Set_Parent (New_L, Parent_Id);
5702 N := First (L);
5703 while Present (N) loop
5704 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
5705 Next (N);
5706 end loop;
5708 return New_L;
5710 else
5711 return No_List;
5712 end if;
5713 end Copy_Generic_List;
5715 ---------------------------
5716 -- In_Defining_Unit_Name --
5717 ---------------------------
5719 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
5720 begin
5721 return Present (Parent (Nam))
5722 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
5723 or else
5724 (Nkind (Parent (Nam)) = N_Expanded_Name
5725 and then In_Defining_Unit_Name (Parent (Nam))));
5726 end In_Defining_Unit_Name;
5728 -- Start of processing for Copy_Generic_Node
5730 begin
5731 if N = Empty then
5732 return N;
5733 end if;
5735 New_N := New_Copy (N);
5737 if Instantiating then
5738 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
5739 end if;
5741 if not Is_List_Member (N) then
5742 Set_Parent (New_N, Parent_Id);
5743 end if;
5745 -- If defining identifier, then all fields have been copied already
5747 if Nkind (New_N) in N_Entity then
5748 null;
5750 -- Special casing for identifiers and other entity names and operators
5752 elsif Nkind_In (New_N, N_Identifier,
5753 N_Character_Literal,
5754 N_Expanded_Name,
5755 N_Operator_Symbol)
5756 or else Nkind (New_N) in N_Op
5757 then
5758 if not Instantiating then
5760 -- Link both nodes in order to assign subsequently the entity of
5761 -- the copy to the original node, in case this is a global
5762 -- reference.
5764 Set_Associated_Node (N, New_N);
5766 -- If we are within an instantiation, this is a nested generic
5767 -- that has already been analyzed at the point of definition. We
5768 -- must preserve references that were global to the enclosing
5769 -- parent at that point. Other occurrences, whether global or
5770 -- local to the current generic, must be resolved anew, so we
5771 -- reset the entity in the generic copy. A global reference has a
5772 -- smaller depth than the parent, or else the same depth in case
5773 -- both are distinct compilation units.
5774 -- A child unit is implicitly declared within the enclosing parent
5775 -- but is in fact global to it, and must be preserved.
5777 -- It is also possible for Current_Instantiated_Parent to be
5778 -- defined, and for this not to be a nested generic, namely if the
5779 -- unit is loaded through Rtsfind. In that case, the entity of
5780 -- New_N is only a link to the associated node, and not a defining
5781 -- occurrence.
5783 -- The entities for parent units in the defining_program_unit of a
5784 -- generic child unit are established when the context of the unit
5785 -- is first analyzed, before the generic copy is made. They are
5786 -- preserved in the copy for use in ASIS queries.
5788 Ent := Entity (New_N);
5790 if No (Current_Instantiated_Parent.Gen_Id) then
5791 if No (Ent)
5792 or else Nkind (Ent) /= N_Defining_Identifier
5793 or else not In_Defining_Unit_Name (N)
5794 then
5795 Set_Associated_Node (New_N, Empty);
5796 end if;
5798 elsif No (Ent)
5799 or else
5800 not Nkind_In (Ent, N_Defining_Identifier,
5801 N_Defining_Character_Literal,
5802 N_Defining_Operator_Symbol)
5803 or else No (Scope (Ent))
5804 or else
5805 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
5806 and then not Is_Child_Unit (Ent))
5807 or else
5808 (Scope_Depth (Scope (Ent)) >
5809 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
5810 and then
5811 Get_Source_Unit (Ent) =
5812 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
5813 then
5814 Set_Associated_Node (New_N, Empty);
5815 end if;
5817 -- Case of instantiating identifier or some other name or operator
5819 else
5820 -- If the associated node is still defined, the entity in it is
5821 -- global, and must be copied to the instance. If this copy is
5822 -- being made for a body to inline, it is applied to an
5823 -- instantiated tree, and the entity is already present and must
5824 -- be also preserved.
5826 declare
5827 Assoc : constant Node_Id := Get_Associated_Node (N);
5829 begin
5830 if Present (Assoc) then
5831 if Nkind (Assoc) = Nkind (N) then
5832 Set_Entity (New_N, Entity (Assoc));
5833 Check_Private_View (N);
5835 elsif Nkind (Assoc) = N_Function_Call then
5836 Set_Entity (New_N, Entity (Name (Assoc)));
5838 elsif Nkind_In (Assoc, N_Defining_Identifier,
5839 N_Defining_Character_Literal,
5840 N_Defining_Operator_Symbol)
5841 and then Expander_Active
5842 then
5843 -- Inlining case: we are copying a tree that contains
5844 -- global entities, which are preserved in the copy to be
5845 -- used for subsequent inlining.
5847 null;
5849 else
5850 Set_Entity (New_N, Empty);
5851 end if;
5852 end if;
5853 end;
5854 end if;
5856 -- For expanded name, we must copy the Prefix and Selector_Name
5858 if Nkind (N) = N_Expanded_Name then
5859 Set_Prefix
5860 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
5862 Set_Selector_Name (New_N,
5863 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
5865 -- For operators, we must copy the right operand
5867 elsif Nkind (N) in N_Op then
5868 Set_Right_Opnd (New_N,
5869 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
5871 -- And for binary operators, the left operand as well
5873 if Nkind (N) in N_Binary_Op then
5874 Set_Left_Opnd (New_N,
5875 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
5876 end if;
5877 end if;
5879 -- Special casing for stubs
5881 elsif Nkind (N) in N_Body_Stub then
5883 -- In any case, we must copy the specification or defining
5884 -- identifier as appropriate.
5886 if Nkind (N) = N_Subprogram_Body_Stub then
5887 Set_Specification (New_N,
5888 Copy_Generic_Node (Specification (N), New_N, Instantiating));
5890 else
5891 Set_Defining_Identifier (New_N,
5892 Copy_Generic_Node
5893 (Defining_Identifier (N), New_N, Instantiating));
5894 end if;
5896 -- If we are not instantiating, then this is where we load and
5897 -- analyze subunits, i.e. at the point where the stub occurs. A
5898 -- more permissive system might defer this analysis to the point
5899 -- of instantiation, but this seems to complicated for now.
5901 if not Instantiating then
5902 declare
5903 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
5904 Subunit : Node_Id;
5905 Unum : Unit_Number_Type;
5906 New_Body : Node_Id;
5908 begin
5909 Unum :=
5910 Load_Unit
5911 (Load_Name => Subunit_Name,
5912 Required => False,
5913 Subunit => True,
5914 Error_Node => N);
5916 -- If the proper body is not found, a warning message will be
5917 -- emitted when analyzing the stub, or later at the point
5918 -- of instantiation. Here we just leave the stub as is.
5920 if Unum = No_Unit then
5921 Subunits_Missing := True;
5922 goto Subunit_Not_Found;
5923 end if;
5925 Subunit := Cunit (Unum);
5927 if Nkind (Unit (Subunit)) /= N_Subunit then
5928 Error_Msg_N
5929 ("found child unit instead of expected SEPARATE subunit",
5930 Subunit);
5931 Error_Msg_Sloc := Sloc (N);
5932 Error_Msg_N ("\to complete stub #", Subunit);
5933 goto Subunit_Not_Found;
5934 end if;
5936 -- We must create a generic copy of the subunit, in order to
5937 -- perform semantic analysis on it, and we must replace the
5938 -- stub in the original generic unit with the subunit, in order
5939 -- to preserve non-local references within.
5941 -- Only the proper body needs to be copied. Library_Unit and
5942 -- context clause are simply inherited by the generic copy.
5943 -- Note that the copy (which may be recursive if there are
5944 -- nested subunits) must be done first, before attaching it to
5945 -- the enclosing generic.
5947 New_Body :=
5948 Copy_Generic_Node
5949 (Proper_Body (Unit (Subunit)),
5950 Empty, Instantiating => False);
5952 -- Now place the original proper body in the original generic
5953 -- unit. This is a body, not a compilation unit.
5955 Rewrite (N, Proper_Body (Unit (Subunit)));
5956 Set_Is_Compilation_Unit (Defining_Entity (N), False);
5957 Set_Was_Originally_Stub (N);
5959 -- Finally replace the body of the subunit with its copy, and
5960 -- make this new subunit into the library unit of the generic
5961 -- copy, which does not have stubs any longer.
5963 Set_Proper_Body (Unit (Subunit), New_Body);
5964 Set_Library_Unit (New_N, Subunit);
5965 Inherit_Context (Unit (Subunit), N);
5966 end;
5968 -- If we are instantiating, this must be an error case, since
5969 -- otherwise we would have replaced the stub node by the proper body
5970 -- that corresponds. So just ignore it in the copy (i.e. we have
5971 -- copied it, and that is good enough).
5973 else
5974 null;
5975 end if;
5977 <<Subunit_Not_Found>> null;
5979 -- If the node is a compilation unit, it is the subunit of a stub, which
5980 -- has been loaded already (see code below). In this case, the library
5981 -- unit field of N points to the parent unit (which is a compilation
5982 -- unit) and need not (and cannot!) be copied.
5984 -- When the proper body of the stub is analyzed, the library_unit link
5985 -- is used to establish the proper context (see sem_ch10).
5987 -- The other fields of a compilation unit are copied as usual
5989 elsif Nkind (N) = N_Compilation_Unit then
5991 -- This code can only be executed when not instantiating, because in
5992 -- the copy made for an instantiation, the compilation unit node has
5993 -- disappeared at the point that a stub is replaced by its proper
5994 -- body.
5996 pragma Assert (not Instantiating);
5998 Set_Context_Items (New_N,
5999 Copy_Generic_List (Context_Items (N), New_N));
6001 Set_Unit (New_N,
6002 Copy_Generic_Node (Unit (N), New_N, False));
6004 Set_First_Inlined_Subprogram (New_N,
6005 Copy_Generic_Node
6006 (First_Inlined_Subprogram (N), New_N, False));
6008 Set_Aux_Decls_Node (New_N,
6009 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
6011 -- For an assignment node, the assignment is known to be semantically
6012 -- legal if we are instantiating the template. This avoids incorrect
6013 -- diagnostics in generated code.
6015 elsif Nkind (N) = N_Assignment_Statement then
6017 -- Copy name and expression fields in usual manner
6019 Set_Name (New_N,
6020 Copy_Generic_Node (Name (N), New_N, Instantiating));
6022 Set_Expression (New_N,
6023 Copy_Generic_Node (Expression (N), New_N, Instantiating));
6025 if Instantiating then
6026 Set_Assignment_OK (Name (New_N), True);
6027 end if;
6029 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
6030 if not Instantiating then
6031 Set_Associated_Node (N, New_N);
6033 else
6034 if Present (Get_Associated_Node (N))
6035 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
6036 then
6037 -- In the generic the aggregate has some composite type. If at
6038 -- the point of instantiation the type has a private view,
6039 -- install the full view (and that of its ancestors, if any).
6041 declare
6042 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
6043 Rt : Entity_Id;
6045 begin
6046 if Present (T)
6047 and then Is_Private_Type (T)
6048 then
6049 Switch_View (T);
6050 end if;
6052 if Present (T)
6053 and then Is_Tagged_Type (T)
6054 and then Is_Derived_Type (T)
6055 then
6056 Rt := Root_Type (T);
6058 loop
6059 T := Etype (T);
6061 if Is_Private_Type (T) then
6062 Switch_View (T);
6063 end if;
6065 exit when T = Rt;
6066 end loop;
6067 end if;
6068 end;
6069 end if;
6070 end if;
6072 -- Do not copy the associated node, which points to
6073 -- the generic copy of the aggregate.
6075 declare
6076 use Atree.Unchecked_Access;
6077 -- This code section is part of the implementation of an untyped
6078 -- tree traversal, so it needs direct access to node fields.
6080 begin
6081 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
6082 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
6083 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
6084 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
6085 end;
6087 -- Allocators do not have an identifier denoting the access type,
6088 -- so we must locate it through the expression to check whether
6089 -- the views are consistent.
6091 elsif Nkind (N) = N_Allocator
6092 and then Nkind (Expression (N)) = N_Qualified_Expression
6093 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
6094 and then Instantiating
6095 then
6096 declare
6097 T : constant Node_Id :=
6098 Get_Associated_Node (Subtype_Mark (Expression (N)));
6099 Acc_T : Entity_Id;
6101 begin
6102 if Present (T) then
6104 -- Retrieve the allocator node in the generic copy
6106 Acc_T := Etype (Parent (Parent (T)));
6107 if Present (Acc_T)
6108 and then Is_Private_Type (Acc_T)
6109 then
6110 Switch_View (Acc_T);
6111 end if;
6112 end if;
6114 Copy_Descendants;
6115 end;
6117 -- For a proper body, we must catch the case of a proper body that
6118 -- replaces a stub. This represents the point at which a separate
6119 -- compilation unit, and hence template file, may be referenced, so we
6120 -- must make a new source instantiation entry for the template of the
6121 -- subunit, and ensure that all nodes in the subunit are adjusted using
6122 -- this new source instantiation entry.
6124 elsif Nkind (N) in N_Proper_Body then
6125 declare
6126 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
6128 begin
6129 if Instantiating and then Was_Originally_Stub (N) then
6130 Create_Instantiation_Source
6131 (Instantiation_Node,
6132 Defining_Entity (N),
6133 False,
6134 S_Adjustment);
6135 end if;
6137 -- Now copy the fields of the proper body, using the new
6138 -- adjustment factor if one was needed as per test above.
6140 Copy_Descendants;
6142 -- Restore the original adjustment factor in case changed
6144 S_Adjustment := Save_Adjustment;
6145 end;
6147 -- Don't copy Ident or Comment pragmas, since the comment belongs to the
6148 -- generic unit, not to the instantiating unit.
6150 elsif Nkind (N) = N_Pragma
6151 and then Instantiating
6152 then
6153 declare
6154 Prag_Id : constant Pragma_Id := Get_Pragma_Id (N);
6155 begin
6156 if Prag_Id = Pragma_Ident
6157 or else Prag_Id = Pragma_Comment
6158 then
6159 New_N := Make_Null_Statement (Sloc (N));
6160 else
6161 Copy_Descendants;
6162 end if;
6163 end;
6165 elsif Nkind_In (N, N_Integer_Literal,
6166 N_Real_Literal,
6167 N_String_Literal)
6168 then
6169 -- No descendant fields need traversing
6171 null;
6173 -- For the remaining nodes, copy recursively their descendants
6175 else
6176 Copy_Descendants;
6178 if Instantiating
6179 and then Nkind (N) = N_Subprogram_Body
6180 then
6181 Set_Generic_Parent (Specification (New_N), N);
6182 end if;
6183 end if;
6185 return New_N;
6186 end Copy_Generic_Node;
6188 ----------------------------
6189 -- Denotes_Formal_Package --
6190 ----------------------------
6192 function Denotes_Formal_Package
6193 (Pack : Entity_Id;
6194 On_Exit : Boolean := False;
6195 Instance : Entity_Id := Empty) return Boolean
6197 Par : Entity_Id;
6198 Scop : constant Entity_Id := Scope (Pack);
6199 E : Entity_Id;
6201 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
6202 -- The package in question may be an actual for a previous formal
6203 -- package P of the current instance, so examine its actuals as well.
6204 -- This must be recursive over other formal packages.
6206 ----------------------------------
6207 -- Is_Actual_Of_Previous_Formal --
6208 ----------------------------------
6210 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
6211 E1 : Entity_Id;
6213 begin
6214 E1 := First_Entity (P);
6215 while Present (E1) and then E1 /= Instance loop
6216 if Ekind (E1) = E_Package
6217 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
6218 then
6219 if Renamed_Object (E1) = Pack then
6220 return True;
6222 elsif E1 = P
6223 or else Renamed_Object (E1) = P
6224 then
6225 return False;
6227 elsif Is_Actual_Of_Previous_Formal (E1) then
6228 return True;
6229 end if;
6230 end if;
6232 Next_Entity (E1);
6233 end loop;
6235 return False;
6236 end Is_Actual_Of_Previous_Formal;
6238 -- Start of processing for Denotes_Formal_Package
6240 begin
6241 if On_Exit then
6242 Par :=
6243 Instance_Envs.Table
6244 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
6245 else
6246 Par := Current_Instantiated_Parent.Act_Id;
6247 end if;
6249 if Ekind (Scop) = E_Generic_Package
6250 or else Nkind (Unit_Declaration_Node (Scop)) =
6251 N_Generic_Subprogram_Declaration
6252 then
6253 return True;
6255 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
6256 N_Formal_Package_Declaration
6257 then
6258 return True;
6260 elsif No (Par) then
6261 return False;
6263 else
6264 -- Check whether this package is associated with a formal package of
6265 -- the enclosing instantiation. Iterate over the list of renamings.
6267 E := First_Entity (Par);
6268 while Present (E) loop
6269 if Ekind (E) /= E_Package
6270 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
6271 then
6272 null;
6274 elsif Renamed_Object (E) = Par then
6275 return False;
6277 elsif Renamed_Object (E) = Pack then
6278 return True;
6280 elsif Is_Actual_Of_Previous_Formal (E) then
6281 return True;
6283 end if;
6285 Next_Entity (E);
6286 end loop;
6288 return False;
6289 end if;
6290 end Denotes_Formal_Package;
6292 -----------------
6293 -- End_Generic --
6294 -----------------
6296 procedure End_Generic is
6297 begin
6298 -- ??? More things could be factored out in this routine. Should
6299 -- probably be done at a later stage.
6301 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
6302 Generic_Flags.Decrement_Last;
6304 Expander_Mode_Restore;
6305 end End_Generic;
6307 ----------------------
6308 -- Find_Actual_Type --
6309 ----------------------
6311 function Find_Actual_Type
6312 (Typ : Entity_Id;
6313 Gen_Type : Entity_Id) return Entity_Id
6315 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
6316 T : Entity_Id;
6318 begin
6319 -- Special processing only applies to child units
6321 if not Is_Child_Unit (Gen_Scope) then
6322 return Get_Instance_Of (Typ);
6324 -- If designated or component type is itself a formal of the child unit,
6325 -- its instance is available.
6327 elsif Scope (Typ) = Gen_Scope then
6328 return Get_Instance_Of (Typ);
6330 -- If the array or access type is not declared in the parent unit,
6331 -- no special processing needed.
6333 elsif not Is_Generic_Type (Typ)
6334 and then Scope (Gen_Scope) /= Scope (Typ)
6335 then
6336 return Get_Instance_Of (Typ);
6338 -- Otherwise, retrieve designated or component type by visibility
6340 else
6341 T := Current_Entity (Typ);
6342 while Present (T) loop
6343 if In_Open_Scopes (Scope (T)) then
6344 return T;
6346 elsif Is_Generic_Actual_Type (T) then
6347 return T;
6348 end if;
6350 T := Homonym (T);
6351 end loop;
6353 return Typ;
6354 end if;
6355 end Find_Actual_Type;
6357 ----------------------------
6358 -- Freeze_Subprogram_Body --
6359 ----------------------------
6361 procedure Freeze_Subprogram_Body
6362 (Inst_Node : Node_Id;
6363 Gen_Body : Node_Id;
6364 Pack_Id : Entity_Id)
6366 F_Node : Node_Id;
6367 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
6368 Par : constant Entity_Id := Scope (Gen_Unit);
6369 Enc_G : Entity_Id;
6370 Enc_I : Node_Id;
6371 E_G_Id : Entity_Id;
6373 function Earlier (N1, N2 : Node_Id) return Boolean;
6374 -- Yields True if N1 and N2 appear in the same compilation unit,
6375 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
6376 -- traversal of the tree for the unit.
6378 function Enclosing_Body (N : Node_Id) return Node_Id;
6379 -- Find innermost package body that encloses the given node, and which
6380 -- is not a compilation unit. Freeze nodes for the instance, or for its
6381 -- enclosing body, may be inserted after the enclosing_body of the
6382 -- generic unit.
6384 function Package_Freeze_Node (B : Node_Id) return Node_Id;
6385 -- Find entity for given package body, and locate or create a freeze
6386 -- node for it.
6388 function True_Parent (N : Node_Id) return Node_Id;
6389 -- For a subunit, return parent of corresponding stub
6391 -------------
6392 -- Earlier --
6393 -------------
6395 function Earlier (N1, N2 : Node_Id) return Boolean is
6396 D1 : Integer := 0;
6397 D2 : Integer := 0;
6398 P1 : Node_Id := N1;
6399 P2 : Node_Id := N2;
6401 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
6402 -- Find distance from given node to enclosing compilation unit
6404 ----------------
6405 -- Find_Depth --
6406 ----------------
6408 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
6409 begin
6410 while Present (P)
6411 and then Nkind (P) /= N_Compilation_Unit
6412 loop
6413 P := True_Parent (P);
6414 D := D + 1;
6415 end loop;
6416 end Find_Depth;
6418 -- Start of processing for Earlier
6420 begin
6421 Find_Depth (P1, D1);
6422 Find_Depth (P2, D2);
6424 if P1 /= P2 then
6425 return False;
6426 else
6427 P1 := N1;
6428 P2 := N2;
6429 end if;
6431 while D1 > D2 loop
6432 P1 := True_Parent (P1);
6433 D1 := D1 - 1;
6434 end loop;
6436 while D2 > D1 loop
6437 P2 := True_Parent (P2);
6438 D2 := D2 - 1;
6439 end loop;
6441 -- At this point P1 and P2 are at the same distance from the root.
6442 -- We examine their parents until we find a common declarative
6443 -- list, at which point we can establish their relative placement
6444 -- by comparing their ultimate slocs. If we reach the root,
6445 -- N1 and N2 do not descend from the same declarative list (e.g.
6446 -- one is nested in the declarative part and the other is in a block
6447 -- in the statement part) and the earlier one is already frozen.
6449 while not Is_List_Member (P1)
6450 or else not Is_List_Member (P2)
6451 or else List_Containing (P1) /= List_Containing (P2)
6452 loop
6453 P1 := True_Parent (P1);
6454 P2 := True_Parent (P2);
6456 if Nkind (Parent (P1)) = N_Subunit then
6457 P1 := Corresponding_Stub (Parent (P1));
6458 end if;
6460 if Nkind (Parent (P2)) = N_Subunit then
6461 P2 := Corresponding_Stub (Parent (P2));
6462 end if;
6464 if P1 = P2 then
6465 return False;
6466 end if;
6467 end loop;
6469 return
6470 Top_Level_Location (Sloc (P1)) < Top_Level_Location (Sloc (P2));
6471 end Earlier;
6473 --------------------
6474 -- Enclosing_Body --
6475 --------------------
6477 function Enclosing_Body (N : Node_Id) return Node_Id is
6478 P : Node_Id := Parent (N);
6480 begin
6481 while Present (P)
6482 and then Nkind (Parent (P)) /= N_Compilation_Unit
6483 loop
6484 if Nkind (P) = N_Package_Body then
6486 if Nkind (Parent (P)) = N_Subunit then
6487 return Corresponding_Stub (Parent (P));
6488 else
6489 return P;
6490 end if;
6491 end if;
6493 P := True_Parent (P);
6494 end loop;
6496 return Empty;
6497 end Enclosing_Body;
6499 -------------------------
6500 -- Package_Freeze_Node --
6501 -------------------------
6503 function Package_Freeze_Node (B : Node_Id) return Node_Id is
6504 Id : Entity_Id;
6506 begin
6507 if Nkind (B) = N_Package_Body then
6508 Id := Corresponding_Spec (B);
6510 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
6511 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
6512 end if;
6514 Ensure_Freeze_Node (Id);
6515 return Freeze_Node (Id);
6516 end Package_Freeze_Node;
6518 -----------------
6519 -- True_Parent --
6520 -----------------
6522 function True_Parent (N : Node_Id) return Node_Id is
6523 begin
6524 if Nkind (Parent (N)) = N_Subunit then
6525 return Parent (Corresponding_Stub (Parent (N)));
6526 else
6527 return Parent (N);
6528 end if;
6529 end True_Parent;
6531 -- Start of processing of Freeze_Subprogram_Body
6533 begin
6534 -- If the instance and the generic body appear within the same unit, and
6535 -- the instance precedes the generic, the freeze node for the instance
6536 -- must appear after that of the generic. If the generic is nested
6537 -- within another instance I2, then current instance must be frozen
6538 -- after I2. In both cases, the freeze nodes are those of enclosing
6539 -- packages. Otherwise, the freeze node is placed at the end of the
6540 -- current declarative part.
6542 Enc_G := Enclosing_Body (Gen_Body);
6543 Enc_I := Enclosing_Body (Inst_Node);
6544 Ensure_Freeze_Node (Pack_Id);
6545 F_Node := Freeze_Node (Pack_Id);
6547 if Is_Generic_Instance (Par)
6548 and then Present (Freeze_Node (Par))
6549 and then
6550 In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
6551 then
6552 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
6554 -- The parent was a premature instantiation. Insert freeze node at
6555 -- the end the current declarative part.
6557 Insert_After_Last_Decl (Inst_Node, F_Node);
6559 else
6560 Insert_After (Freeze_Node (Par), F_Node);
6561 end if;
6563 -- The body enclosing the instance should be frozen after the body that
6564 -- includes the generic, because the body of the instance may make
6565 -- references to entities therein. If the two are not in the same
6566 -- declarative part, or if the one enclosing the instance is frozen
6567 -- already, freeze the instance at the end of the current declarative
6568 -- part.
6570 elsif Is_Generic_Instance (Par)
6571 and then Present (Freeze_Node (Par))
6572 and then Present (Enc_I)
6573 then
6574 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
6575 or else
6576 (Nkind (Enc_I) = N_Package_Body
6577 and then
6578 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
6579 then
6580 -- The enclosing package may contain several instances. Rather
6581 -- than computing the earliest point at which to insert its
6582 -- freeze node, we place it at the end of the declarative part
6583 -- of the parent of the generic.
6585 Insert_After_Last_Decl
6586 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
6587 end if;
6589 Insert_After_Last_Decl (Inst_Node, F_Node);
6591 elsif Present (Enc_G)
6592 and then Present (Enc_I)
6593 and then Enc_G /= Enc_I
6594 and then Earlier (Inst_Node, Gen_Body)
6595 then
6596 if Nkind (Enc_G) = N_Package_Body then
6597 E_G_Id := Corresponding_Spec (Enc_G);
6598 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
6599 E_G_Id :=
6600 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
6601 end if;
6603 -- Freeze package that encloses instance, and place node after
6604 -- package that encloses generic. If enclosing package is already
6605 -- frozen we have to assume it is at the proper place. This may be
6606 -- a potential ABE that requires dynamic checking. Do not add a
6607 -- freeze node if the package that encloses the generic is inside
6608 -- the body that encloses the instance, because the freeze node
6609 -- would be in the wrong scope. Additional contortions needed if
6610 -- the bodies are within a subunit.
6612 declare
6613 Enclosing_Body : Node_Id;
6615 begin
6616 if Nkind (Enc_I) = N_Package_Body_Stub then
6617 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
6618 else
6619 Enclosing_Body := Enc_I;
6620 end if;
6622 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
6623 Insert_After_Last_Decl (Enc_G, Package_Freeze_Node (Enc_I));
6624 end if;
6625 end;
6627 -- Freeze enclosing subunit before instance
6629 Ensure_Freeze_Node (E_G_Id);
6631 if not Is_List_Member (Freeze_Node (E_G_Id)) then
6632 Insert_After (Enc_G, Freeze_Node (E_G_Id));
6633 end if;
6635 Insert_After_Last_Decl (Inst_Node, F_Node);
6637 else
6638 -- If none of the above, insert freeze node at the end of the current
6639 -- declarative part.
6641 Insert_After_Last_Decl (Inst_Node, F_Node);
6642 end if;
6643 end Freeze_Subprogram_Body;
6645 ----------------
6646 -- Get_Gen_Id --
6647 ----------------
6649 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
6650 begin
6651 return Generic_Renamings.Table (E).Gen_Id;
6652 end Get_Gen_Id;
6654 ---------------------
6655 -- Get_Instance_Of --
6656 ---------------------
6658 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
6659 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
6661 begin
6662 if Res /= Assoc_Null then
6663 return Generic_Renamings.Table (Res).Act_Id;
6664 else
6665 -- On exit, entity is not instantiated: not a generic parameter, or
6666 -- else parameter of an inner generic unit.
6668 return A;
6669 end if;
6670 end Get_Instance_Of;
6672 ------------------------------------
6673 -- Get_Package_Instantiation_Node --
6674 ------------------------------------
6676 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
6677 Decl : Node_Id := Unit_Declaration_Node (A);
6678 Inst : Node_Id;
6680 begin
6681 -- If the Package_Instantiation attribute has been set on the package
6682 -- entity, then use it directly when it (or its Original_Node) refers
6683 -- to an N_Package_Instantiation node. In principle it should be
6684 -- possible to have this field set in all cases, which should be
6685 -- investigated, and would allow this function to be significantly
6686 -- simplified. ???
6688 if Present (Package_Instantiation (A)) then
6689 if Nkind (Package_Instantiation (A)) = N_Package_Instantiation then
6690 return Package_Instantiation (A);
6692 elsif Nkind (Original_Node (Package_Instantiation (A))) =
6693 N_Package_Instantiation
6694 then
6695 return Original_Node (Package_Instantiation (A));
6696 end if;
6697 end if;
6699 -- If the instantiation is a compilation unit that does not need body
6700 -- then the instantiation node has been rewritten as a package
6701 -- declaration for the instance, and we return the original node.
6703 -- If it is a compilation unit and the instance node has not been
6704 -- rewritten, then it is still the unit of the compilation. Finally, if
6705 -- a body is present, this is a parent of the main unit whose body has
6706 -- been compiled for inlining purposes, and the instantiation node has
6707 -- been rewritten with the instance body.
6709 -- Otherwise the instantiation node appears after the declaration. If
6710 -- the entity is a formal package, the declaration may have been
6711 -- rewritten as a generic declaration (in the case of a formal with box)
6712 -- or left as a formal package declaration if it has actuals, and is
6713 -- found with a forward search.
6715 if Nkind (Parent (Decl)) = N_Compilation_Unit then
6716 if Nkind (Decl) = N_Package_Declaration
6717 and then Present (Corresponding_Body (Decl))
6718 then
6719 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
6720 end if;
6722 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
6723 return Original_Node (Decl);
6724 else
6725 return Unit (Parent (Decl));
6726 end if;
6728 elsif Nkind (Decl) = N_Package_Declaration
6729 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
6730 then
6731 return Original_Node (Decl);
6733 else
6734 Inst := Next (Decl);
6735 while not Nkind_In (Inst, N_Package_Instantiation,
6736 N_Formal_Package_Declaration)
6737 loop
6738 Next (Inst);
6739 end loop;
6741 return Inst;
6742 end if;
6743 end Get_Package_Instantiation_Node;
6745 ------------------------
6746 -- Has_Been_Exchanged --
6747 ------------------------
6749 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
6750 Next : Elmt_Id;
6752 begin
6753 Next := First_Elmt (Exchanged_Views);
6754 while Present (Next) loop
6755 if Full_View (Node (Next)) = E then
6756 return True;
6757 end if;
6759 Next_Elmt (Next);
6760 end loop;
6762 return False;
6763 end Has_Been_Exchanged;
6765 ----------
6766 -- Hash --
6767 ----------
6769 function Hash (F : Entity_Id) return HTable_Range is
6770 begin
6771 return HTable_Range (F mod HTable_Size);
6772 end Hash;
6774 ------------------------
6775 -- Hide_Current_Scope --
6776 ------------------------
6778 procedure Hide_Current_Scope is
6779 C : constant Entity_Id := Current_Scope;
6780 E : Entity_Id;
6782 begin
6783 Set_Is_Hidden_Open_Scope (C);
6785 E := First_Entity (C);
6786 while Present (E) loop
6787 if Is_Immediately_Visible (E) then
6788 Set_Is_Immediately_Visible (E, False);
6789 Append_Elmt (E, Hidden_Entities);
6790 end if;
6792 Next_Entity (E);
6793 end loop;
6795 -- Make the scope name invisible as well. This is necessary, but might
6796 -- conflict with calls to Rtsfind later on, in case the scope is a
6797 -- predefined one. There is no clean solution to this problem, so for
6798 -- now we depend on the user not redefining Standard itself in one of
6799 -- the parent units.
6801 if Is_Immediately_Visible (C)
6802 and then C /= Standard_Standard
6803 then
6804 Set_Is_Immediately_Visible (C, False);
6805 Append_Elmt (C, Hidden_Entities);
6806 end if;
6808 end Hide_Current_Scope;
6810 --------------
6811 -- Init_Env --
6812 --------------
6814 procedure Init_Env is
6815 Saved : Instance_Env;
6817 begin
6818 Saved.Instantiated_Parent := Current_Instantiated_Parent;
6819 Saved.Exchanged_Views := Exchanged_Views;
6820 Saved.Hidden_Entities := Hidden_Entities;
6821 Saved.Current_Sem_Unit := Current_Sem_Unit;
6822 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
6823 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
6825 -- Save configuration switches. These may be reset if the unit is a
6826 -- predefined unit, and the current mode is not Ada 2005.
6828 Save_Opt_Config_Switches (Saved.Switches);
6830 Instance_Envs.Append (Saved);
6832 Exchanged_Views := New_Elmt_List;
6833 Hidden_Entities := New_Elmt_List;
6835 -- Make dummy entry for Instantiated parent. If generic unit is legal,
6836 -- this is set properly in Set_Instance_Env.
6838 Current_Instantiated_Parent :=
6839 (Current_Scope, Current_Scope, Assoc_Null);
6840 end Init_Env;
6842 ------------------------------
6843 -- In_Same_Declarative_Part --
6844 ------------------------------
6846 function In_Same_Declarative_Part
6847 (F_Node : Node_Id;
6848 Inst : Node_Id) return Boolean
6850 Decls : constant Node_Id := Parent (F_Node);
6851 Nod : Node_Id := Parent (Inst);
6853 begin
6854 while Present (Nod) loop
6855 if Nod = Decls then
6856 return True;
6858 elsif Nkind_In (Nod, N_Subprogram_Body,
6859 N_Package_Body,
6860 N_Task_Body,
6861 N_Protected_Body,
6862 N_Block_Statement)
6863 then
6864 return False;
6866 elsif Nkind (Nod) = N_Subunit then
6867 Nod := Corresponding_Stub (Nod);
6869 elsif Nkind (Nod) = N_Compilation_Unit then
6870 return False;
6872 else
6873 Nod := Parent (Nod);
6874 end if;
6875 end loop;
6877 return False;
6878 end In_Same_Declarative_Part;
6880 ---------------------
6881 -- In_Main_Context --
6882 ---------------------
6884 function In_Main_Context (E : Entity_Id) return Boolean is
6885 Context : List_Id;
6886 Clause : Node_Id;
6887 Nam : Node_Id;
6889 begin
6890 if not Is_Compilation_Unit (E)
6891 or else Ekind (E) /= E_Package
6892 or else In_Private_Part (E)
6893 then
6894 return False;
6895 end if;
6897 Context := Context_Items (Cunit (Main_Unit));
6899 Clause := First (Context);
6900 while Present (Clause) loop
6901 if Nkind (Clause) = N_With_Clause then
6902 Nam := Name (Clause);
6904 -- If the current scope is part of the context of the main unit,
6905 -- analysis of the corresponding with_clause is not complete, and
6906 -- the entity is not set. We use the Chars field directly, which
6907 -- might produce false positives in rare cases, but guarantees
6908 -- that we produce all the instance bodies we will need.
6910 if (Is_Entity_Name (Nam)
6911 and then Chars (Nam) = Chars (E))
6912 or else (Nkind (Nam) = N_Selected_Component
6913 and then Chars (Selector_Name (Nam)) = Chars (E))
6914 then
6915 return True;
6916 end if;
6917 end if;
6919 Next (Clause);
6920 end loop;
6922 return False;
6923 end In_Main_Context;
6925 ---------------------
6926 -- Inherit_Context --
6927 ---------------------
6929 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
6930 Current_Context : List_Id;
6931 Current_Unit : Node_Id;
6932 Item : Node_Id;
6933 New_I : Node_Id;
6935 begin
6936 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
6938 -- The inherited context is attached to the enclosing compilation
6939 -- unit. This is either the main unit, or the declaration for the
6940 -- main unit (in case the instantiation appears within the package
6941 -- declaration and the main unit is its body).
6943 Current_Unit := Parent (Inst);
6944 while Present (Current_Unit)
6945 and then Nkind (Current_Unit) /= N_Compilation_Unit
6946 loop
6947 Current_Unit := Parent (Current_Unit);
6948 end loop;
6950 Current_Context := Context_Items (Current_Unit);
6952 Item := First (Context_Items (Parent (Gen_Decl)));
6953 while Present (Item) loop
6954 if Nkind (Item) = N_With_Clause then
6956 -- Take care to prevent direct cyclic with's, which can happen
6957 -- if the generic body with's the current unit. Such a case
6958 -- would result in binder errors (or run-time errors if the
6959 -- -gnatE switch is in effect), but we want to prevent it here,
6960 -- because Sem.Walk_Library_Items doesn't like cycles. Note
6961 -- that we don't bother to detect indirect cycles.
6963 if Library_Unit (Item) /= Current_Unit then
6964 New_I := New_Copy (Item);
6965 Set_Implicit_With (New_I, True);
6966 Append (New_I, Current_Context);
6967 end if;
6968 end if;
6970 Next (Item);
6971 end loop;
6972 end if;
6973 end Inherit_Context;
6975 ----------------
6976 -- Initialize --
6977 ----------------
6979 procedure Initialize is
6980 begin
6981 Generic_Renamings.Init;
6982 Instance_Envs.Init;
6983 Generic_Flags.Init;
6984 Generic_Renamings_HTable.Reset;
6985 Circularity_Detected := False;
6986 Exchanged_Views := No_Elist;
6987 Hidden_Entities := No_Elist;
6988 end Initialize;
6990 ----------------------------
6991 -- Insert_After_Last_Decl --
6992 ----------------------------
6994 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id) is
6995 L : List_Id := List_Containing (N);
6996 P : constant Node_Id := Parent (L);
6998 begin
6999 if not Is_List_Member (F_Node) then
7000 if Nkind (P) = N_Package_Specification
7001 and then L = Visible_Declarations (P)
7002 and then Present (Private_Declarations (P))
7003 and then not Is_Empty_List (Private_Declarations (P))
7004 then
7005 L := Private_Declarations (P);
7006 end if;
7008 Insert_After (Last (L), F_Node);
7009 end if;
7010 end Insert_After_Last_Decl;
7012 ------------------
7013 -- Install_Body --
7014 ------------------
7016 procedure Install_Body
7017 (Act_Body : Node_Id;
7018 N : Node_Id;
7019 Gen_Body : Node_Id;
7020 Gen_Decl : Node_Id)
7022 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
7023 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
7024 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
7025 Par : constant Entity_Id := Scope (Gen_Id);
7026 Gen_Unit : constant Node_Id :=
7027 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
7028 Orig_Body : Node_Id := Gen_Body;
7029 F_Node : Node_Id;
7030 Body_Unit : Node_Id;
7032 Must_Delay : Boolean;
7034 function Enclosing_Subp (Id : Entity_Id) return Entity_Id;
7035 -- Find subprogram (if any) that encloses instance and/or generic body
7037 function True_Sloc (N : Node_Id) return Source_Ptr;
7038 -- If the instance is nested inside a generic unit, the Sloc of the
7039 -- instance indicates the place of the original definition, not the
7040 -- point of the current enclosing instance. Pending a better usage of
7041 -- Slocs to indicate instantiation places, we determine the place of
7042 -- origin of a node by finding the maximum sloc of any ancestor node.
7043 -- Why is this not equivalent to Top_Level_Location ???
7045 --------------------
7046 -- Enclosing_Subp --
7047 --------------------
7049 function Enclosing_Subp (Id : Entity_Id) return Entity_Id is
7050 Scop : Entity_Id := Scope (Id);
7052 begin
7053 while Scop /= Standard_Standard
7054 and then not Is_Overloadable (Scop)
7055 loop
7056 Scop := Scope (Scop);
7057 end loop;
7059 return Scop;
7060 end Enclosing_Subp;
7062 ---------------
7063 -- True_Sloc --
7064 ---------------
7066 function True_Sloc (N : Node_Id) return Source_Ptr is
7067 Res : Source_Ptr;
7068 N1 : Node_Id;
7070 begin
7071 Res := Sloc (N);
7072 N1 := N;
7073 while Present (N1) and then N1 /= Act_Unit loop
7074 if Sloc (N1) > Res then
7075 Res := Sloc (N1);
7076 end if;
7078 N1 := Parent (N1);
7079 end loop;
7081 return Res;
7082 end True_Sloc;
7084 -- Start of processing for Install_Body
7086 begin
7088 -- If the body is a subunit, the freeze point is the corresponding
7089 -- stub in the current compilation, not the subunit itself.
7091 if Nkind (Parent (Gen_Body)) = N_Subunit then
7092 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
7093 else
7094 Orig_Body := Gen_Body;
7095 end if;
7097 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
7099 -- If the instantiation and the generic definition appear in the same
7100 -- package declaration, this is an early instantiation. If they appear
7101 -- in the same declarative part, it is an early instantiation only if
7102 -- the generic body appears textually later, and the generic body is
7103 -- also in the main unit.
7105 -- If instance is nested within a subprogram, and the generic body is
7106 -- not, the instance is delayed because the enclosing body is. If
7107 -- instance and body are within the same scope, or the same sub-
7108 -- program body, indicate explicitly that the instance is delayed.
7110 Must_Delay :=
7111 (Gen_Unit = Act_Unit
7112 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
7113 N_Generic_Package_Declaration)
7114 or else (Gen_Unit = Body_Unit
7115 and then True_Sloc (N) < Sloc (Orig_Body)))
7116 and then Is_In_Main_Unit (Gen_Unit)
7117 and then (Scope (Act_Id) = Scope (Gen_Id)
7118 or else
7119 Enclosing_Subp (Act_Id) = Enclosing_Subp (Gen_Id)));
7121 -- If this is an early instantiation, the freeze node is placed after
7122 -- the generic body. Otherwise, if the generic appears in an instance,
7123 -- we cannot freeze the current instance until the outer one is frozen.
7124 -- This is only relevant if the current instance is nested within some
7125 -- inner scope not itself within the outer instance. If this scope is
7126 -- a package body in the same declarative part as the outer instance,
7127 -- then that body needs to be frozen after the outer instance. Finally,
7128 -- if no delay is needed, we place the freeze node at the end of the
7129 -- current declarative part.
7131 if Expander_Active then
7132 Ensure_Freeze_Node (Act_Id);
7133 F_Node := Freeze_Node (Act_Id);
7135 if Must_Delay then
7136 Insert_After (Orig_Body, F_Node);
7138 elsif Is_Generic_Instance (Par)
7139 and then Present (Freeze_Node (Par))
7140 and then Scope (Act_Id) /= Par
7141 then
7142 -- Freeze instance of inner generic after instance of enclosing
7143 -- generic.
7145 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
7146 Insert_After (Freeze_Node (Par), F_Node);
7148 -- Freeze package enclosing instance of inner generic after
7149 -- instance of enclosing generic.
7151 elsif Nkind (Parent (N)) = N_Package_Body
7152 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
7153 then
7155 declare
7156 Enclosing : constant Entity_Id :=
7157 Corresponding_Spec (Parent (N));
7159 begin
7160 Insert_After_Last_Decl (N, F_Node);
7161 Ensure_Freeze_Node (Enclosing);
7163 if not Is_List_Member (Freeze_Node (Enclosing)) then
7164 Insert_After (Freeze_Node (Par), Freeze_Node (Enclosing));
7165 end if;
7166 end;
7168 else
7169 Insert_After_Last_Decl (N, F_Node);
7170 end if;
7172 else
7173 Insert_After_Last_Decl (N, F_Node);
7174 end if;
7175 end if;
7177 Set_Is_Frozen (Act_Id);
7178 Insert_Before (N, Act_Body);
7179 Mark_Rewrite_Insertion (Act_Body);
7180 end Install_Body;
7182 -----------------------------
7183 -- Install_Formal_Packages --
7184 -----------------------------
7186 procedure Install_Formal_Packages (Par : Entity_Id) is
7187 E : Entity_Id;
7188 Gen : Entity_Id;
7189 Gen_E : Entity_Id := Empty;
7191 begin
7192 E := First_Entity (Par);
7194 -- In we are installing an instance parent, locate the formal packages
7195 -- of its generic parent.
7197 if Is_Generic_Instance (Par) then
7198 Gen := Generic_Parent (Specification (Unit_Declaration_Node (Par)));
7199 Gen_E := First_Entity (Gen);
7200 end if;
7202 while Present (E) loop
7203 if Ekind (E) = E_Package
7204 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
7205 then
7206 -- If this is the renaming for the parent instance, done
7208 if Renamed_Object (E) = Par then
7209 exit;
7211 -- The visibility of a formal of an enclosing generic is already
7212 -- correct.
7214 elsif Denotes_Formal_Package (E) then
7215 null;
7217 elsif Present (Associated_Formal_Package (E)) then
7218 Check_Generic_Actuals (Renamed_Object (E), True);
7219 Set_Is_Hidden (E, False);
7221 -- Find formal package in generic unit that corresponds to
7222 -- (instance of) formal package in instance.
7224 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
7225 Next_Entity (Gen_E);
7226 end loop;
7228 if Present (Gen_E) then
7229 Map_Formal_Package_Entities (Gen_E, E);
7230 end if;
7231 end if;
7232 end if;
7234 Next_Entity (E);
7235 if Present (Gen_E) then
7236 Next_Entity (Gen_E);
7237 end if;
7238 end loop;
7239 end Install_Formal_Packages;
7241 --------------------
7242 -- Install_Parent --
7243 --------------------
7245 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
7246 Ancestors : constant Elist_Id := New_Elmt_List;
7247 S : constant Entity_Id := Current_Scope;
7248 Inst_Par : Entity_Id;
7249 First_Par : Entity_Id;
7250 Inst_Node : Node_Id;
7251 Gen_Par : Entity_Id;
7252 First_Gen : Entity_Id;
7253 Elmt : Elmt_Id;
7255 procedure Install_Noninstance_Specs (Par : Entity_Id);
7256 -- Install the scopes of noninstance parent units ending with Par
7258 procedure Install_Spec (Par : Entity_Id);
7259 -- The child unit is within the declarative part of the parent, so
7260 -- the declarations within the parent are immediately visible.
7262 -------------------------------
7263 -- Install_Noninstance_Specs --
7264 -------------------------------
7266 procedure Install_Noninstance_Specs (Par : Entity_Id) is
7267 begin
7268 if Present (Par)
7269 and then Par /= Standard_Standard
7270 and then not In_Open_Scopes (Par)
7271 then
7272 Install_Noninstance_Specs (Scope (Par));
7273 Install_Spec (Par);
7274 end if;
7275 end Install_Noninstance_Specs;
7277 ------------------
7278 -- Install_Spec --
7279 ------------------
7281 procedure Install_Spec (Par : Entity_Id) is
7282 Spec : constant Node_Id :=
7283 Specification (Unit_Declaration_Node (Par));
7285 begin
7286 -- If this parent of the child instance is a top-level unit,
7287 -- then record the unit and its visibility for later resetting
7288 -- in Remove_Parent. We exclude units that are generic instances,
7289 -- as we only want to record this information for the ultimate
7290 -- top-level noninstance parent (is that always correct???).
7292 if Scope (Par) = Standard_Standard
7293 and then not Is_Generic_Instance (Par)
7294 then
7295 Parent_Unit_Visible := Is_Immediately_Visible (Par);
7296 Instance_Parent_Unit := Par;
7297 end if;
7299 -- Open the parent scope and make it and its declarations visible.
7300 -- If this point is not within a body, then only the visible
7301 -- declarations should be made visible, and installation of the
7302 -- private declarations is deferred until the appropriate point
7303 -- within analysis of the spec being instantiated (see the handling
7304 -- of parent visibility in Analyze_Package_Specification). This is
7305 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
7306 -- private view problems that occur when compiling instantiations of
7307 -- a generic child of that package (Generic_Dispatching_Constructor).
7308 -- If the instance freezes a tagged type, inlinings of operations
7309 -- from Ada.Tags may need the full view of type Tag. If inlining took
7310 -- proper account of establishing visibility of inlined subprograms'
7311 -- parents then it should be possible to remove this
7312 -- special check. ???
7314 Push_Scope (Par);
7315 Set_Is_Immediately_Visible (Par);
7316 Install_Visible_Declarations (Par);
7317 Set_Use (Visible_Declarations (Spec));
7319 if In_Body or else Is_RTU (Par, Ada_Tags) then
7320 Install_Private_Declarations (Par);
7321 Set_Use (Private_Declarations (Spec));
7322 end if;
7323 end Install_Spec;
7325 -- Start of processing for Install_Parent
7327 begin
7328 -- We need to install the parent instance to compile the instantiation
7329 -- of the child, but the child instance must appear in the current
7330 -- scope. Given that we cannot place the parent above the current scope
7331 -- in the scope stack, we duplicate the current scope and unstack both
7332 -- after the instantiation is complete.
7334 -- If the parent is itself the instantiation of a child unit, we must
7335 -- also stack the instantiation of its parent, and so on. Each such
7336 -- ancestor is the prefix of the name in a prior instantiation.
7338 -- If this is a nested instance, the parent unit itself resolves to
7339 -- a renaming of the parent instance, whose declaration we need.
7341 -- Finally, the parent may be a generic (not an instance) when the
7342 -- child unit appears as a formal package.
7344 Inst_Par := P;
7346 if Present (Renamed_Entity (Inst_Par)) then
7347 Inst_Par := Renamed_Entity (Inst_Par);
7348 end if;
7350 First_Par := Inst_Par;
7352 Gen_Par :=
7353 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
7355 First_Gen := Gen_Par;
7357 while Present (Gen_Par)
7358 and then Is_Child_Unit (Gen_Par)
7359 loop
7360 -- Load grandparent instance as well
7362 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
7364 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
7365 Inst_Par := Entity (Prefix (Name (Inst_Node)));
7367 if Present (Renamed_Entity (Inst_Par)) then
7368 Inst_Par := Renamed_Entity (Inst_Par);
7369 end if;
7371 Gen_Par :=
7372 Generic_Parent
7373 (Specification (Unit_Declaration_Node (Inst_Par)));
7375 if Present (Gen_Par) then
7376 Prepend_Elmt (Inst_Par, Ancestors);
7378 else
7379 -- Parent is not the name of an instantiation
7381 Install_Noninstance_Specs (Inst_Par);
7383 exit;
7384 end if;
7386 else
7387 -- Previous error
7389 exit;
7390 end if;
7391 end loop;
7393 if Present (First_Gen) then
7394 Append_Elmt (First_Par, Ancestors);
7396 else
7397 Install_Noninstance_Specs (First_Par);
7398 end if;
7400 if not Is_Empty_Elmt_List (Ancestors) then
7401 Elmt := First_Elmt (Ancestors);
7403 while Present (Elmt) loop
7404 Install_Spec (Node (Elmt));
7405 Install_Formal_Packages (Node (Elmt));
7407 Next_Elmt (Elmt);
7408 end loop;
7409 end if;
7411 if not In_Body then
7412 Push_Scope (S);
7413 end if;
7414 end Install_Parent;
7416 --------------------------------
7417 -- Instantiate_Formal_Package --
7418 --------------------------------
7420 function Instantiate_Formal_Package
7421 (Formal : Node_Id;
7422 Actual : Node_Id;
7423 Analyzed_Formal : Node_Id) return List_Id
7425 Loc : constant Source_Ptr := Sloc (Actual);
7426 Actual_Pack : Entity_Id;
7427 Formal_Pack : Entity_Id;
7428 Gen_Parent : Entity_Id;
7429 Decls : List_Id;
7430 Nod : Node_Id;
7431 Parent_Spec : Node_Id;
7433 procedure Find_Matching_Actual
7434 (F : Node_Id;
7435 Act : in out Entity_Id);
7436 -- We need to associate each formal entity in the formal package
7437 -- with the corresponding entity in the actual package. The actual
7438 -- package has been analyzed and possibly expanded, and as a result
7439 -- there is no one-to-one correspondence between the two lists (for
7440 -- example, the actual may include subtypes, itypes, and inherited
7441 -- primitive operations, interspersed among the renaming declarations
7442 -- for the actuals) . We retrieve the corresponding actual by name
7443 -- because each actual has the same name as the formal, and they do
7444 -- appear in the same order.
7446 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
7447 -- Retrieve entity of defining entity of generic formal parameter.
7448 -- Only the declarations of formals need to be considered when
7449 -- linking them to actuals, but the declarative list may include
7450 -- internal entities generated during analysis, and those are ignored.
7452 procedure Match_Formal_Entity
7453 (Formal_Node : Node_Id;
7454 Formal_Ent : Entity_Id;
7455 Actual_Ent : Entity_Id);
7456 -- Associates the formal entity with the actual. In the case
7457 -- where Formal_Ent is a formal package, this procedure iterates
7458 -- through all of its formals and enters associations between the
7459 -- actuals occurring in the formal package's corresponding actual
7460 -- package (given by Actual_Ent) and the formal package's formal
7461 -- parameters. This procedure recurses if any of the parameters is
7462 -- itself a package.
7464 function Is_Instance_Of
7465 (Act_Spec : Entity_Id;
7466 Gen_Anc : Entity_Id) return Boolean;
7467 -- The actual can be an instantiation of a generic within another
7468 -- instance, in which case there is no direct link from it to the
7469 -- original generic ancestor. In that case, we recognize that the
7470 -- ultimate ancestor is the same by examining names and scopes.
7472 procedure Process_Nested_Formal (Formal : Entity_Id);
7473 -- If the current formal is declared with a box, its own formals are
7474 -- visible in the instance, as they were in the generic, and their
7475 -- Hidden flag must be reset. If some of these formals are themselves
7476 -- packages declared with a box, the processing must be recursive.
7478 --------------------------
7479 -- Find_Matching_Actual --
7480 --------------------------
7482 procedure Find_Matching_Actual
7483 (F : Node_Id;
7484 Act : in out Entity_Id)
7486 Formal_Ent : Entity_Id;
7488 begin
7489 case Nkind (Original_Node (F)) is
7490 when N_Formal_Object_Declaration |
7491 N_Formal_Type_Declaration =>
7492 Formal_Ent := Defining_Identifier (F);
7494 while Chars (Act) /= Chars (Formal_Ent) loop
7495 Next_Entity (Act);
7496 end loop;
7498 when N_Formal_Subprogram_Declaration |
7499 N_Formal_Package_Declaration |
7500 N_Package_Declaration |
7501 N_Generic_Package_Declaration =>
7502 Formal_Ent := Defining_Entity (F);
7504 while Chars (Act) /= Chars (Formal_Ent) loop
7505 Next_Entity (Act);
7506 end loop;
7508 when others =>
7509 raise Program_Error;
7510 end case;
7511 end Find_Matching_Actual;
7513 -------------------------
7514 -- Match_Formal_Entity --
7515 -------------------------
7517 procedure Match_Formal_Entity
7518 (Formal_Node : Node_Id;
7519 Formal_Ent : Entity_Id;
7520 Actual_Ent : Entity_Id)
7522 Act_Pkg : Entity_Id;
7524 begin
7525 Set_Instance_Of (Formal_Ent, Actual_Ent);
7527 if Ekind (Actual_Ent) = E_Package then
7529 -- Record associations for each parameter
7531 Act_Pkg := Actual_Ent;
7533 declare
7534 A_Ent : Entity_Id := First_Entity (Act_Pkg);
7535 F_Ent : Entity_Id;
7536 F_Node : Node_Id;
7538 Gen_Decl : Node_Id;
7539 Formals : List_Id;
7540 Actual : Entity_Id;
7542 begin
7543 -- Retrieve the actual given in the formal package declaration
7545 Actual := Entity (Name (Original_Node (Formal_Node)));
7547 -- The actual in the formal package declaration may be a
7548 -- renamed generic package, in which case we want to retrieve
7549 -- the original generic in order to traverse its formal part.
7551 if Present (Renamed_Entity (Actual)) then
7552 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
7553 else
7554 Gen_Decl := Unit_Declaration_Node (Actual);
7555 end if;
7557 Formals := Generic_Formal_Declarations (Gen_Decl);
7559 if Present (Formals) then
7560 F_Node := First_Non_Pragma (Formals);
7561 else
7562 F_Node := Empty;
7563 end if;
7565 while Present (A_Ent)
7566 and then Present (F_Node)
7567 and then A_Ent /= First_Private_Entity (Act_Pkg)
7568 loop
7569 F_Ent := Get_Formal_Entity (F_Node);
7571 if Present (F_Ent) then
7573 -- This is a formal of the original package. Record
7574 -- association and recurse.
7576 Find_Matching_Actual (F_Node, A_Ent);
7577 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
7578 Next_Entity (A_Ent);
7579 end if;
7581 Next_Non_Pragma (F_Node);
7582 end loop;
7583 end;
7584 end if;
7585 end Match_Formal_Entity;
7587 -----------------------
7588 -- Get_Formal_Entity --
7589 -----------------------
7591 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
7592 Kind : constant Node_Kind := Nkind (Original_Node (N));
7593 begin
7594 case Kind is
7595 when N_Formal_Object_Declaration =>
7596 return Defining_Identifier (N);
7598 when N_Formal_Type_Declaration =>
7599 return Defining_Identifier (N);
7601 when N_Formal_Subprogram_Declaration =>
7602 return Defining_Unit_Name (Specification (N));
7604 when N_Formal_Package_Declaration =>
7605 return Defining_Identifier (Original_Node (N));
7607 when N_Generic_Package_Declaration =>
7608 return Defining_Identifier (Original_Node (N));
7610 -- All other declarations are introduced by semantic analysis and
7611 -- have no match in the actual.
7613 when others =>
7614 return Empty;
7615 end case;
7616 end Get_Formal_Entity;
7618 --------------------
7619 -- Is_Instance_Of --
7620 --------------------
7622 function Is_Instance_Of
7623 (Act_Spec : Entity_Id;
7624 Gen_Anc : Entity_Id) return Boolean
7626 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
7628 begin
7629 if No (Gen_Par) then
7630 return False;
7632 -- Simplest case: the generic parent of the actual is the formal
7634 elsif Gen_Par = Gen_Anc then
7635 return True;
7637 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
7638 return False;
7640 -- The actual may be obtained through several instantiations. Its
7641 -- scope must itself be an instance of a generic declared in the
7642 -- same scope as the formal. Any other case is detected above.
7644 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
7645 return False;
7647 else
7648 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
7649 end if;
7650 end Is_Instance_Of;
7652 ---------------------------
7653 -- Process_Nested_Formal --
7654 ---------------------------
7656 procedure Process_Nested_Formal (Formal : Entity_Id) is
7657 Ent : Entity_Id;
7659 begin
7660 if Present (Associated_Formal_Package (Formal))
7661 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
7662 then
7663 Ent := First_Entity (Formal);
7664 while Present (Ent) loop
7665 Set_Is_Hidden (Ent, False);
7666 Set_Is_Visible_Formal (Ent);
7667 Set_Is_Potentially_Use_Visible
7668 (Ent, Is_Potentially_Use_Visible (Formal));
7670 if Ekind (Ent) = E_Package then
7671 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
7672 Process_Nested_Formal (Ent);
7673 end if;
7675 Next_Entity (Ent);
7676 end loop;
7677 end if;
7678 end Process_Nested_Formal;
7680 -- Start of processing for Instantiate_Formal_Package
7682 begin
7683 Analyze (Actual);
7685 if not Is_Entity_Name (Actual)
7686 or else Ekind (Entity (Actual)) /= E_Package
7687 then
7688 Error_Msg_N
7689 ("expect package instance to instantiate formal", Actual);
7690 Abandon_Instantiation (Actual);
7691 raise Program_Error;
7693 else
7694 Actual_Pack := Entity (Actual);
7695 Set_Is_Instantiated (Actual_Pack);
7697 -- The actual may be a renamed package, or an outer generic formal
7698 -- package whose instantiation is converted into a renaming.
7700 if Present (Renamed_Object (Actual_Pack)) then
7701 Actual_Pack := Renamed_Object (Actual_Pack);
7702 end if;
7704 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
7705 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
7706 Formal_Pack := Defining_Identifier (Analyzed_Formal);
7707 else
7708 Gen_Parent :=
7709 Generic_Parent (Specification (Analyzed_Formal));
7710 Formal_Pack :=
7711 Defining_Unit_Name (Specification (Analyzed_Formal));
7712 end if;
7714 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
7715 Parent_Spec := Specification (Unit_Declaration_Node (Actual_Pack));
7716 else
7717 Parent_Spec := Parent (Actual_Pack);
7718 end if;
7720 if Gen_Parent = Any_Id then
7721 Error_Msg_N
7722 ("previous error in declaration of formal package", Actual);
7723 Abandon_Instantiation (Actual);
7725 elsif
7726 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
7727 then
7728 null;
7730 else
7731 Error_Msg_NE
7732 ("actual parameter must be instance of&", Actual, Gen_Parent);
7733 Abandon_Instantiation (Actual);
7734 end if;
7736 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
7737 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
7739 Nod :=
7740 Make_Package_Renaming_Declaration (Loc,
7741 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
7742 Name => New_Reference_To (Actual_Pack, Loc));
7744 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
7745 Defining_Identifier (Formal));
7746 Decls := New_List (Nod);
7748 -- If the formal F has a box, then the generic declarations are
7749 -- visible in the generic G. In an instance of G, the corresponding
7750 -- entities in the actual for F (which are the actuals for the
7751 -- instantiation of the generic that F denotes) must also be made
7752 -- visible for analysis of the current instance. On exit from the
7753 -- current instance, those entities are made private again. If the
7754 -- actual is currently in use, these entities are also use-visible.
7756 -- The loop through the actual entities also steps through the formal
7757 -- entities and enters associations from formals to actuals into the
7758 -- renaming map. This is necessary to properly handle checking of
7759 -- actual parameter associations for later formals that depend on
7760 -- actuals declared in the formal package.
7762 -- In Ada 2005, partial parametrization requires that we make visible
7763 -- the actuals corresponding to formals that were defaulted in the
7764 -- formal package. There formals are identified because they remain
7765 -- formal generics within the formal package, rather than being
7766 -- renamings of the actuals supplied.
7768 declare
7769 Gen_Decl : constant Node_Id :=
7770 Unit_Declaration_Node (Gen_Parent);
7771 Formals : constant List_Id :=
7772 Generic_Formal_Declarations (Gen_Decl);
7774 Actual_Ent : Entity_Id;
7775 Actual_Of_Formal : Node_Id;
7776 Formal_Node : Node_Id;
7777 Formal_Ent : Entity_Id;
7779 begin
7780 if Present (Formals) then
7781 Formal_Node := First_Non_Pragma (Formals);
7782 else
7783 Formal_Node := Empty;
7784 end if;
7786 Actual_Ent := First_Entity (Actual_Pack);
7787 Actual_Of_Formal :=
7788 First (Visible_Declarations (Specification (Analyzed_Formal)));
7789 while Present (Actual_Ent)
7790 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
7791 loop
7792 if Present (Formal_Node) then
7793 Formal_Ent := Get_Formal_Entity (Formal_Node);
7795 if Present (Formal_Ent) then
7796 Find_Matching_Actual (Formal_Node, Actual_Ent);
7797 Match_Formal_Entity
7798 (Formal_Node, Formal_Ent, Actual_Ent);
7800 -- We iterate at the same time over the actuals of the
7801 -- local package created for the formal, to determine
7802 -- which one of the formals of the original generic were
7803 -- defaulted in the formal. The corresponding actual
7804 -- entities are visible in the enclosing instance.
7806 if Box_Present (Formal)
7807 or else
7808 (Present (Actual_Of_Formal)
7809 and then
7810 Is_Generic_Formal
7811 (Get_Formal_Entity (Actual_Of_Formal)))
7812 then
7813 Set_Is_Hidden (Actual_Ent, False);
7814 Set_Is_Visible_Formal (Actual_Ent);
7815 Set_Is_Potentially_Use_Visible
7816 (Actual_Ent, In_Use (Actual_Pack));
7818 if Ekind (Actual_Ent) = E_Package then
7819 Process_Nested_Formal (Actual_Ent);
7820 end if;
7822 else
7823 Set_Is_Hidden (Actual_Ent);
7824 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
7825 end if;
7826 end if;
7828 Next_Non_Pragma (Formal_Node);
7829 Next (Actual_Of_Formal);
7831 else
7832 -- No further formals to match, but the generic part may
7833 -- contain inherited operation that are not hidden in the
7834 -- enclosing instance.
7836 Next_Entity (Actual_Ent);
7837 end if;
7838 end loop;
7840 -- Inherited subprograms generated by formal derived types are
7841 -- also visible if the types are.
7843 Actual_Ent := First_Entity (Actual_Pack);
7844 while Present (Actual_Ent)
7845 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
7846 loop
7847 if Is_Overloadable (Actual_Ent)
7848 and then
7849 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
7850 and then
7851 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
7852 then
7853 Set_Is_Hidden (Actual_Ent, False);
7854 Set_Is_Potentially_Use_Visible
7855 (Actual_Ent, In_Use (Actual_Pack));
7856 end if;
7858 Next_Entity (Actual_Ent);
7859 end loop;
7860 end;
7862 -- If the formal is not declared with a box, reanalyze it as an
7863 -- abbreviated instantiation, to verify the matching rules of 12.7.
7864 -- The actual checks are performed after the generic associations
7865 -- have been analyzed, to guarantee the same visibility for this
7866 -- instantiation and for the actuals.
7868 -- In Ada 2005, the generic associations for the formal can include
7869 -- defaulted parameters. These are ignored during check. This
7870 -- internal instantiation is removed from the tree after conformance
7871 -- checking, because it contains formal declarations for those
7872 -- defaulted parameters, and those should not reach the back-end.
7874 if not Box_Present (Formal) then
7875 declare
7876 I_Pack : constant Entity_Id :=
7877 Make_Temporary (Sloc (Actual), 'P');
7879 begin
7880 Set_Is_Internal (I_Pack);
7882 Append_To (Decls,
7883 Make_Package_Instantiation (Sloc (Actual),
7884 Defining_Unit_Name => I_Pack,
7885 Name =>
7886 New_Occurrence_Of
7887 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
7888 Generic_Associations =>
7889 Generic_Associations (Formal)));
7890 end;
7891 end if;
7893 return Decls;
7894 end if;
7895 end Instantiate_Formal_Package;
7897 -----------------------------------
7898 -- Instantiate_Formal_Subprogram --
7899 -----------------------------------
7901 function Instantiate_Formal_Subprogram
7902 (Formal : Node_Id;
7903 Actual : Node_Id;
7904 Analyzed_Formal : Node_Id) return Node_Id
7906 Loc : Source_Ptr;
7907 Formal_Sub : constant Entity_Id :=
7908 Defining_Unit_Name (Specification (Formal));
7909 Analyzed_S : constant Entity_Id :=
7910 Defining_Unit_Name (Specification (Analyzed_Formal));
7911 Decl_Node : Node_Id;
7912 Nam : Node_Id;
7913 New_Spec : Node_Id;
7915 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
7916 -- If the generic is a child unit, the parent has been installed on the
7917 -- scope stack, but a default subprogram cannot resolve to something on
7918 -- the parent because that parent is not really part of the visible
7919 -- context (it is there to resolve explicit local entities). If the
7920 -- default has resolved in this way, we remove the entity from
7921 -- immediate visibility and analyze the node again to emit an error
7922 -- message or find another visible candidate.
7924 procedure Valid_Actual_Subprogram (Act : Node_Id);
7925 -- Perform legality check and raise exception on failure
7927 -----------------------
7928 -- From_Parent_Scope --
7929 -----------------------
7931 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
7932 Gen_Scope : Node_Id;
7934 begin
7935 Gen_Scope := Scope (Analyzed_S);
7936 while Present (Gen_Scope)
7937 and then Is_Child_Unit (Gen_Scope)
7938 loop
7939 if Scope (Subp) = Scope (Gen_Scope) then
7940 return True;
7941 end if;
7943 Gen_Scope := Scope (Gen_Scope);
7944 end loop;
7946 return False;
7947 end From_Parent_Scope;
7949 -----------------------------
7950 -- Valid_Actual_Subprogram --
7951 -----------------------------
7953 procedure Valid_Actual_Subprogram (Act : Node_Id) is
7954 Act_E : Entity_Id;
7956 begin
7957 if Is_Entity_Name (Act) then
7958 Act_E := Entity (Act);
7960 elsif Nkind (Act) = N_Selected_Component
7961 and then Is_Entity_Name (Selector_Name (Act))
7962 then
7963 Act_E := Entity (Selector_Name (Act));
7965 else
7966 Act_E := Empty;
7967 end if;
7969 if (Present (Act_E) and then Is_Overloadable (Act_E))
7970 or else Nkind_In (Act, N_Attribute_Reference,
7971 N_Indexed_Component,
7972 N_Character_Literal,
7973 N_Explicit_Dereference)
7974 then
7975 return;
7976 end if;
7978 Error_Msg_NE
7979 ("expect subprogram or entry name in instantiation of&",
7980 Instantiation_Node, Formal_Sub);
7981 Abandon_Instantiation (Instantiation_Node);
7983 end Valid_Actual_Subprogram;
7985 -- Start of processing for Instantiate_Formal_Subprogram
7987 begin
7988 New_Spec := New_Copy_Tree (Specification (Formal));
7990 -- The tree copy has created the proper instantiation sloc for the
7991 -- new specification. Use this location for all other constructed
7992 -- declarations.
7994 Loc := Sloc (Defining_Unit_Name (New_Spec));
7996 -- Create new entity for the actual (New_Copy_Tree does not)
7998 Set_Defining_Unit_Name
7999 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
8001 -- Create new entities for the each of the formals in the
8002 -- specification of the renaming declaration built for the actual.
8004 if Present (Parameter_Specifications (New_Spec)) then
8005 declare
8006 F : Node_Id;
8007 begin
8008 F := First (Parameter_Specifications (New_Spec));
8009 while Present (F) loop
8010 Set_Defining_Identifier (F,
8011 Make_Defining_Identifier (Sloc (F),
8012 Chars => Chars (Defining_Identifier (F))));
8013 Next (F);
8014 end loop;
8015 end;
8016 end if;
8018 -- Find entity of actual. If the actual is an attribute reference, it
8019 -- cannot be resolved here (its formal is missing) but is handled
8020 -- instead in Attribute_Renaming. If the actual is overloaded, it is
8021 -- fully resolved subsequently, when the renaming declaration for the
8022 -- formal is analyzed. If it is an explicit dereference, resolve the
8023 -- prefix but not the actual itself, to prevent interpretation as call.
8025 if Present (Actual) then
8026 Loc := Sloc (Actual);
8027 Set_Sloc (New_Spec, Loc);
8029 if Nkind (Actual) = N_Operator_Symbol then
8030 Find_Direct_Name (Actual);
8032 elsif Nkind (Actual) = N_Explicit_Dereference then
8033 Analyze (Prefix (Actual));
8035 elsif Nkind (Actual) /= N_Attribute_Reference then
8036 Analyze (Actual);
8037 end if;
8039 Valid_Actual_Subprogram (Actual);
8040 Nam := Actual;
8042 elsif Present (Default_Name (Formal)) then
8043 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
8044 N_Selected_Component,
8045 N_Indexed_Component,
8046 N_Character_Literal)
8047 and then Present (Entity (Default_Name (Formal)))
8048 then
8049 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
8050 else
8051 Nam := New_Copy (Default_Name (Formal));
8052 Set_Sloc (Nam, Loc);
8053 end if;
8055 elsif Box_Present (Formal) then
8057 -- Actual is resolved at the point of instantiation. Create an
8058 -- identifier or operator with the same name as the formal.
8060 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
8061 Nam := Make_Operator_Symbol (Loc,
8062 Chars => Chars (Formal_Sub),
8063 Strval => No_String);
8064 else
8065 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
8066 end if;
8068 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
8069 and then Null_Present (Specification (Formal))
8070 then
8071 -- Generate null body for procedure, for use in the instance
8073 Decl_Node :=
8074 Make_Subprogram_Body (Loc,
8075 Specification => New_Spec,
8076 Declarations => New_List,
8077 Handled_Statement_Sequence =>
8078 Make_Handled_Sequence_Of_Statements (Loc,
8079 Statements => New_List (Make_Null_Statement (Loc))));
8081 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
8082 return Decl_Node;
8084 else
8085 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
8086 Error_Msg_NE
8087 ("missing actual&", Instantiation_Node, Formal_Sub);
8088 Error_Msg_NE
8089 ("\in instantiation of & declared#",
8090 Instantiation_Node, Scope (Analyzed_S));
8091 Abandon_Instantiation (Instantiation_Node);
8092 end if;
8094 Decl_Node :=
8095 Make_Subprogram_Renaming_Declaration (Loc,
8096 Specification => New_Spec,
8097 Name => Nam);
8099 -- If we do not have an actual and the formal specified <> then set to
8100 -- get proper default.
8102 if No (Actual) and then Box_Present (Formal) then
8103 Set_From_Default (Decl_Node);
8104 end if;
8106 -- Gather possible interpretations for the actual before analyzing the
8107 -- instance. If overloaded, it will be resolved when analyzing the
8108 -- renaming declaration.
8110 if Box_Present (Formal)
8111 and then No (Actual)
8112 then
8113 Analyze (Nam);
8115 if Is_Child_Unit (Scope (Analyzed_S))
8116 and then Present (Entity (Nam))
8117 then
8118 if not Is_Overloaded (Nam) then
8120 if From_Parent_Scope (Entity (Nam)) then
8121 Set_Is_Immediately_Visible (Entity (Nam), False);
8122 Set_Entity (Nam, Empty);
8123 Set_Etype (Nam, Empty);
8125 Analyze (Nam);
8127 Set_Is_Immediately_Visible (Entity (Nam));
8128 end if;
8130 else
8131 declare
8132 I : Interp_Index;
8133 It : Interp;
8135 begin
8136 Get_First_Interp (Nam, I, It);
8138 while Present (It.Nam) loop
8139 if From_Parent_Scope (It.Nam) then
8140 Remove_Interp (I);
8141 end if;
8143 Get_Next_Interp (I, It);
8144 end loop;
8145 end;
8146 end if;
8147 end if;
8148 end if;
8150 -- The generic instantiation freezes the actual. This can only be done
8151 -- once the actual is resolved, in the analysis of the renaming
8152 -- declaration. To make the formal subprogram entity available, we set
8153 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
8154 -- This is also needed in Analyze_Subprogram_Renaming for the processing
8155 -- of formal abstract subprograms.
8157 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
8159 -- We cannot analyze the renaming declaration, and thus find the actual,
8160 -- until all the actuals are assembled in the instance. For subsequent
8161 -- checks of other actuals, indicate the node that will hold the
8162 -- instance of this formal.
8164 Set_Instance_Of (Analyzed_S, Nam);
8166 if Nkind (Actual) = N_Selected_Component
8167 and then Is_Task_Type (Etype (Prefix (Actual)))
8168 and then not Is_Frozen (Etype (Prefix (Actual)))
8169 then
8170 -- The renaming declaration will create a body, which must appear
8171 -- outside of the instantiation, We move the renaming declaration
8172 -- out of the instance, and create an additional renaming inside,
8173 -- to prevent freezing anomalies.
8175 declare
8176 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
8178 begin
8179 Set_Defining_Unit_Name (New_Spec, Anon_Id);
8180 Insert_Before (Instantiation_Node, Decl_Node);
8181 Analyze (Decl_Node);
8183 -- Now create renaming within the instance
8185 Decl_Node :=
8186 Make_Subprogram_Renaming_Declaration (Loc,
8187 Specification => New_Copy_Tree (New_Spec),
8188 Name => New_Occurrence_Of (Anon_Id, Loc));
8190 Set_Defining_Unit_Name (Specification (Decl_Node),
8191 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
8192 end;
8193 end if;
8195 return Decl_Node;
8196 end Instantiate_Formal_Subprogram;
8198 ------------------------
8199 -- Instantiate_Object --
8200 ------------------------
8202 function Instantiate_Object
8203 (Formal : Node_Id;
8204 Actual : Node_Id;
8205 Analyzed_Formal : Node_Id) return List_Id
8207 Acc_Def : Node_Id := Empty;
8208 Act_Assoc : constant Node_Id := Parent (Actual);
8209 Actual_Decl : Node_Id := Empty;
8210 Formal_Id : constant Entity_Id := Defining_Identifier (Formal);
8211 Decl_Node : Node_Id;
8212 Def : Node_Id;
8213 Ftyp : Entity_Id;
8214 List : constant List_Id := New_List;
8215 Loc : constant Source_Ptr := Sloc (Actual);
8216 Orig_Ftyp : constant Entity_Id :=
8217 Etype (Defining_Identifier (Analyzed_Formal));
8218 Subt_Decl : Node_Id := Empty;
8219 Subt_Mark : Node_Id := Empty;
8221 begin
8222 if Present (Subtype_Mark (Formal)) then
8223 Subt_Mark := Subtype_Mark (Formal);
8224 else
8225 Check_Access_Definition (Formal);
8226 Acc_Def := Access_Definition (Formal);
8227 end if;
8229 -- Sloc for error message on missing actual
8231 Error_Msg_Sloc := Sloc (Scope (Defining_Identifier (Analyzed_Formal)));
8233 if Get_Instance_Of (Formal_Id) /= Formal_Id then
8234 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
8235 end if;
8237 Set_Parent (List, Parent (Actual));
8239 -- OUT present
8241 if Out_Present (Formal) then
8243 -- An IN OUT generic actual must be a name. The instantiation is a
8244 -- renaming declaration. The actual is the name being renamed. We
8245 -- use the actual directly, rather than a copy, because it is not
8246 -- used further in the list of actuals, and because a copy or a use
8247 -- of relocate_node is incorrect if the instance is nested within a
8248 -- generic. In order to simplify ASIS searches, the Generic_Parent
8249 -- field links the declaration to the generic association.
8251 if No (Actual) then
8252 Error_Msg_NE
8253 ("missing actual&",
8254 Instantiation_Node, Formal_Id);
8255 Error_Msg_NE
8256 ("\in instantiation of & declared#",
8257 Instantiation_Node,
8258 Scope (Defining_Identifier (Analyzed_Formal)));
8259 Abandon_Instantiation (Instantiation_Node);
8260 end if;
8262 if Present (Subt_Mark) then
8263 Decl_Node :=
8264 Make_Object_Renaming_Declaration (Loc,
8265 Defining_Identifier => New_Copy (Formal_Id),
8266 Subtype_Mark => New_Copy_Tree (Subt_Mark),
8267 Name => Actual);
8269 else pragma Assert (Present (Acc_Def));
8270 Decl_Node :=
8271 Make_Object_Renaming_Declaration (Loc,
8272 Defining_Identifier => New_Copy (Formal_Id),
8273 Access_Definition => New_Copy_Tree (Acc_Def),
8274 Name => Actual);
8275 end if;
8277 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
8279 -- The analysis of the actual may produce insert_action nodes, so
8280 -- the declaration must have a context in which to attach them.
8282 Append (Decl_Node, List);
8283 Analyze (Actual);
8285 -- Return if the analysis of the actual reported some error
8287 if Etype (Actual) = Any_Type then
8288 return List;
8289 end if;
8291 -- This check is performed here because Analyze_Object_Renaming will
8292 -- not check it when Comes_From_Source is False. Note though that the
8293 -- check for the actual being the name of an object will be performed
8294 -- in Analyze_Object_Renaming.
8296 if Is_Object_Reference (Actual)
8297 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
8298 then
8299 Error_Msg_N
8300 ("illegal discriminant-dependent component for in out parameter",
8301 Actual);
8302 end if;
8304 -- The actual has to be resolved in order to check that it is a
8305 -- variable (due to cases such as F(1), where F returns
8306 -- access to an array, and for overloaded prefixes).
8308 Ftyp :=
8309 Get_Instance_Of (Etype (Defining_Identifier (Analyzed_Formal)));
8311 if Is_Private_Type (Ftyp)
8312 and then not Is_Private_Type (Etype (Actual))
8313 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
8314 or else Base_Type (Etype (Actual)) = Ftyp)
8315 then
8316 -- If the actual has the type of the full view of the formal, or
8317 -- else a non-private subtype of the formal, then the visibility
8318 -- of the formal type has changed. Add to the actuals a subtype
8319 -- declaration that will force the exchange of views in the body
8320 -- of the instance as well.
8322 Subt_Decl :=
8323 Make_Subtype_Declaration (Loc,
8324 Defining_Identifier => Make_Temporary (Loc, 'P'),
8325 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
8327 Prepend (Subt_Decl, List);
8329 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
8330 Exchange_Declarations (Ftyp);
8331 end if;
8333 Resolve (Actual, Ftyp);
8335 if not Denotes_Variable (Actual) then
8336 Error_Msg_NE
8337 ("actual for& must be a variable", Actual, Formal_Id);
8339 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
8341 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
8342 -- the type of the actual shall resolve to a specific anonymous
8343 -- access type.
8345 if Ada_Version < Ada_05
8346 or else
8347 Ekind (Base_Type (Ftyp)) /=
8348 E_Anonymous_Access_Type
8349 or else
8350 Ekind (Base_Type (Etype (Actual))) /=
8351 E_Anonymous_Access_Type
8352 then
8353 Error_Msg_NE ("type of actual does not match type of&",
8354 Actual, Formal_Id);
8355 end if;
8356 end if;
8358 Note_Possible_Modification (Actual, Sure => True);
8360 -- Check for instantiation of atomic/volatile actual for
8361 -- non-atomic/volatile formal (RM C.6 (12)).
8363 if Is_Atomic_Object (Actual)
8364 and then not Is_Atomic (Orig_Ftyp)
8365 then
8366 Error_Msg_N
8367 ("cannot instantiate non-atomic formal object " &
8368 "with atomic actual", Actual);
8370 elsif Is_Volatile_Object (Actual)
8371 and then not Is_Volatile (Orig_Ftyp)
8372 then
8373 Error_Msg_N
8374 ("cannot instantiate non-volatile formal object " &
8375 "with volatile actual", Actual);
8376 end if;
8378 -- Formal in-parameter
8380 else
8381 -- The instantiation of a generic formal in-parameter is constant
8382 -- declaration. The actual is the expression for that declaration.
8384 if Present (Actual) then
8385 if Present (Subt_Mark) then
8386 Def := Subt_Mark;
8387 else pragma Assert (Present (Acc_Def));
8388 Def := Acc_Def;
8389 end if;
8391 Decl_Node :=
8392 Make_Object_Declaration (Loc,
8393 Defining_Identifier => New_Copy (Formal_Id),
8394 Constant_Present => True,
8395 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
8396 Object_Definition => New_Copy_Tree (Def),
8397 Expression => Actual);
8399 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
8401 -- A generic formal object of a tagged type is defined to be
8402 -- aliased so the new constant must also be treated as aliased.
8404 if Is_Tagged_Type
8405 (Etype (Defining_Identifier (Analyzed_Formal)))
8406 then
8407 Set_Aliased_Present (Decl_Node);
8408 end if;
8410 Append (Decl_Node, List);
8412 -- No need to repeat (pre-)analysis of some expression nodes
8413 -- already handled in Preanalyze_Actuals.
8415 if Nkind (Actual) /= N_Allocator then
8416 Analyze (Actual);
8418 -- Return if the analysis of the actual reported some error
8420 if Etype (Actual) = Any_Type then
8421 return List;
8422 end if;
8423 end if;
8425 declare
8426 Formal_Object : constant Entity_Id :=
8427 Defining_Identifier (Analyzed_Formal);
8428 Formal_Type : constant Entity_Id := Etype (Formal_Object);
8430 Typ : Entity_Id;
8432 begin
8433 Typ := Get_Instance_Of (Formal_Type);
8435 Freeze_Before (Instantiation_Node, Typ);
8437 -- If the actual is an aggregate, perform name resolution on
8438 -- its components (the analysis of an aggregate does not do it)
8439 -- to capture local names that may be hidden if the generic is
8440 -- a child unit.
8442 if Nkind (Actual) = N_Aggregate then
8443 Preanalyze_And_Resolve (Actual, Typ);
8444 end if;
8446 if Is_Limited_Type (Typ)
8447 and then not OK_For_Limited_Init (Typ, Actual)
8448 then
8449 Error_Msg_N
8450 ("initialization not allowed for limited types", Actual);
8451 Explain_Limited_Type (Typ, Actual);
8452 end if;
8453 end;
8455 elsif Present (Default_Expression (Formal)) then
8457 -- Use default to construct declaration
8459 if Present (Subt_Mark) then
8460 Def := Subt_Mark;
8461 else pragma Assert (Present (Acc_Def));
8462 Def := Acc_Def;
8463 end if;
8465 Decl_Node :=
8466 Make_Object_Declaration (Sloc (Formal),
8467 Defining_Identifier => New_Copy (Formal_Id),
8468 Constant_Present => True,
8469 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
8470 Object_Definition => New_Copy (Def),
8471 Expression => New_Copy_Tree
8472 (Default_Expression (Formal)));
8474 Append (Decl_Node, List);
8475 Set_Analyzed (Expression (Decl_Node), False);
8477 else
8478 Error_Msg_NE
8479 ("missing actual&",
8480 Instantiation_Node, Formal_Id);
8481 Error_Msg_NE ("\in instantiation of & declared#",
8482 Instantiation_Node,
8483 Scope (Defining_Identifier (Analyzed_Formal)));
8485 if Is_Scalar_Type
8486 (Etype (Defining_Identifier (Analyzed_Formal)))
8487 then
8488 -- Create dummy constant declaration so that instance can be
8489 -- analyzed, to minimize cascaded visibility errors.
8491 if Present (Subt_Mark) then
8492 Def := Subt_Mark;
8493 else pragma Assert (Present (Acc_Def));
8494 Def := Acc_Def;
8495 end if;
8497 Decl_Node :=
8498 Make_Object_Declaration (Loc,
8499 Defining_Identifier => New_Copy (Formal_Id),
8500 Constant_Present => True,
8501 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
8502 Object_Definition => New_Copy (Def),
8503 Expression =>
8504 Make_Attribute_Reference (Sloc (Formal_Id),
8505 Attribute_Name => Name_First,
8506 Prefix => New_Copy (Def)));
8508 Append (Decl_Node, List);
8510 else
8511 Abandon_Instantiation (Instantiation_Node);
8512 end if;
8513 end if;
8514 end if;
8516 if Nkind (Actual) in N_Has_Entity then
8517 Actual_Decl := Parent (Entity (Actual));
8518 end if;
8520 -- Ada 2005 (AI-423): For a formal object declaration with a null
8521 -- exclusion or an access definition that has a null exclusion: If the
8522 -- actual matching the formal object declaration denotes a generic
8523 -- formal object of another generic unit G, and the instantiation
8524 -- containing the actual occurs within the body of G or within the body
8525 -- of a generic unit declared within the declarative region of G, then
8526 -- the declaration of the formal object of G must have a null exclusion.
8527 -- Otherwise, the subtype of the actual matching the formal object
8528 -- declaration shall exclude null.
8530 if Ada_Version >= Ada_05
8531 and then Present (Actual_Decl)
8532 and then
8533 Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
8534 N_Object_Declaration)
8535 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
8536 and then not Has_Null_Exclusion (Actual_Decl)
8537 and then Has_Null_Exclusion (Analyzed_Formal)
8538 then
8539 Error_Msg_Sloc := Sloc (Analyzed_Formal);
8540 Error_Msg_N
8541 ("actual must exclude null to match generic formal#", Actual);
8542 end if;
8544 return List;
8545 end Instantiate_Object;
8547 ------------------------------
8548 -- Instantiate_Package_Body --
8549 ------------------------------
8551 procedure Instantiate_Package_Body
8552 (Body_Info : Pending_Body_Info;
8553 Inlined_Body : Boolean := False;
8554 Body_Optional : Boolean := False)
8556 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
8557 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
8558 Loc : constant Source_Ptr := Sloc (Inst_Node);
8560 Gen_Id : constant Node_Id := Name (Inst_Node);
8561 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8562 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
8563 Act_Spec : constant Node_Id := Specification (Act_Decl);
8564 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
8566 Act_Body_Name : Node_Id;
8567 Gen_Body : Node_Id;
8568 Gen_Body_Id : Node_Id;
8569 Act_Body : Node_Id;
8570 Act_Body_Id : Entity_Id;
8572 Parent_Installed : Boolean := False;
8573 Save_Style_Check : constant Boolean := Style_Check;
8575 Par_Ent : Entity_Id := Empty;
8576 Par_Vis : Boolean := False;
8578 begin
8579 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8581 -- The instance body may already have been processed, as the parent of
8582 -- another instance that is inlined (Load_Parent_Of_Generic).
8584 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
8585 return;
8586 end if;
8588 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
8590 -- Re-establish the state of information on which checks are suppressed.
8591 -- This information was set in Body_Info at the point of instantiation,
8592 -- and now we restore it so that the instance is compiled using the
8593 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
8595 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
8596 Scope_Suppress := Body_Info.Scope_Suppress;
8597 Opt.Ada_Version := Body_Info.Version;
8599 if No (Gen_Body_Id) then
8600 Load_Parent_Of_Generic
8601 (Inst_Node, Specification (Gen_Decl), Body_Optional);
8602 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8603 end if;
8605 Mark_Context (Act_Decl, Gen_Decl);
8607 -- Establish global variable for sloc adjustment and for error recovery
8609 Instantiation_Node := Inst_Node;
8611 if Present (Gen_Body_Id) then
8612 Save_Env (Gen_Unit, Act_Decl_Id);
8613 Style_Check := False;
8614 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
8616 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
8618 Create_Instantiation_Source
8619 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
8621 Act_Body :=
8622 Copy_Generic_Node
8623 (Original_Node (Gen_Body), Empty, Instantiating => True);
8625 -- Build new name (possibly qualified) for body declaration
8627 Act_Body_Id := New_Copy (Act_Decl_Id);
8629 -- Some attributes of spec entity are not inherited by body entity
8631 Set_Handler_Records (Act_Body_Id, No_List);
8633 if Nkind (Defining_Unit_Name (Act_Spec)) =
8634 N_Defining_Program_Unit_Name
8635 then
8636 Act_Body_Name :=
8637 Make_Defining_Program_Unit_Name (Loc,
8638 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
8639 Defining_Identifier => Act_Body_Id);
8640 else
8641 Act_Body_Name := Act_Body_Id;
8642 end if;
8644 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
8646 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
8647 Check_Generic_Actuals (Act_Decl_Id, False);
8649 -- If it is a child unit, make the parent instance (which is an
8650 -- instance of the parent of the generic) visible. The parent
8651 -- instance is the prefix of the name of the generic unit.
8653 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
8654 and then Nkind (Gen_Id) = N_Expanded_Name
8655 then
8656 Par_Ent := Entity (Prefix (Gen_Id));
8657 Par_Vis := Is_Immediately_Visible (Par_Ent);
8658 Install_Parent (Par_Ent, In_Body => True);
8659 Parent_Installed := True;
8661 elsif Is_Child_Unit (Gen_Unit) then
8662 Par_Ent := Scope (Gen_Unit);
8663 Par_Vis := Is_Immediately_Visible (Par_Ent);
8664 Install_Parent (Par_Ent, In_Body => True);
8665 Parent_Installed := True;
8666 end if;
8668 -- If the instantiation is a library unit, and this is the main unit,
8669 -- then build the resulting compilation unit nodes for the instance.
8670 -- If this is a compilation unit but it is not the main unit, then it
8671 -- is the body of a unit in the context, that is being compiled
8672 -- because it is encloses some inlined unit or another generic unit
8673 -- being instantiated. In that case, this body is not part of the
8674 -- current compilation, and is not attached to the tree, but its
8675 -- parent must be set for analysis.
8677 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
8679 -- Replace instance node with body of instance, and create new
8680 -- node for corresponding instance declaration.
8682 Build_Instance_Compilation_Unit_Nodes
8683 (Inst_Node, Act_Body, Act_Decl);
8684 Analyze (Inst_Node);
8686 if Parent (Inst_Node) = Cunit (Main_Unit) then
8688 -- If the instance is a child unit itself, then set the scope
8689 -- of the expanded body to be the parent of the instantiation
8690 -- (ensuring that the fully qualified name will be generated
8691 -- for the elaboration subprogram).
8693 if Nkind (Defining_Unit_Name (Act_Spec)) =
8694 N_Defining_Program_Unit_Name
8695 then
8696 Set_Scope
8697 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
8698 end if;
8699 end if;
8701 -- Case where instantiation is not a library unit
8703 else
8704 -- If this is an early instantiation, i.e. appears textually
8705 -- before the corresponding body and must be elaborated first,
8706 -- indicate that the body instance is to be delayed.
8708 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
8710 -- Now analyze the body. We turn off all checks if this is an
8711 -- internal unit, since there is no reason to have checks on for
8712 -- any predefined run-time library code. All such code is designed
8713 -- to be compiled with checks off.
8715 -- Note that we do NOT apply this criterion to children of GNAT
8716 -- (or on VMS, children of DEC). The latter units must suppress
8717 -- checks explicitly if this is needed.
8719 if Is_Predefined_File_Name
8720 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
8721 then
8722 Analyze (Act_Body, Suppress => All_Checks);
8723 else
8724 Analyze (Act_Body);
8725 end if;
8726 end if;
8728 Inherit_Context (Gen_Body, Inst_Node);
8730 -- Remove the parent instances if they have been placed on the scope
8731 -- stack to compile the body.
8733 if Parent_Installed then
8734 Remove_Parent (In_Body => True);
8736 -- Restore the previous visibility of the parent
8738 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
8739 end if;
8741 Restore_Private_Views (Act_Decl_Id);
8743 -- Remove the current unit from visibility if this is an instance
8744 -- that is not elaborated on the fly for inlining purposes.
8746 if not Inlined_Body then
8747 Set_Is_Immediately_Visible (Act_Decl_Id, False);
8748 end if;
8750 Restore_Env;
8751 Style_Check := Save_Style_Check;
8753 -- If we have no body, and the unit requires a body, then complain. This
8754 -- complaint is suppressed if we have detected other errors (since a
8755 -- common reason for missing the body is that it had errors).
8756 -- In CodePeer mode, a warning has been emitted already, no need for
8757 -- further messages.
8759 elsif Unit_Requires_Body (Gen_Unit)
8760 and then not Body_Optional
8761 then
8762 if CodePeer_Mode then
8763 null;
8765 elsif Serious_Errors_Detected = 0 then
8766 Error_Msg_NE
8767 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
8769 -- Don't attempt to perform any cleanup actions if some other error
8770 -- was already detected, since this can cause blowups.
8772 else
8773 return;
8774 end if;
8776 -- Case of package that does not need a body
8778 else
8779 -- If the instantiation of the declaration is a library unit, rewrite
8780 -- the original package instantiation as a package declaration in the
8781 -- compilation unit node.
8783 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
8784 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
8785 Rewrite (Inst_Node, Act_Decl);
8787 -- Generate elaboration entity, in case spec has elaboration code.
8788 -- This cannot be done when the instance is analyzed, because it
8789 -- is not known yet whether the body exists.
8791 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
8792 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
8794 -- If the instantiation is not a library unit, then append the
8795 -- declaration to the list of implicitly generated entities, unless
8796 -- it is already a list member which means that it was already
8797 -- processed
8799 elsif not Is_List_Member (Act_Decl) then
8800 Mark_Rewrite_Insertion (Act_Decl);
8801 Insert_Before (Inst_Node, Act_Decl);
8802 end if;
8803 end if;
8805 Expander_Mode_Restore;
8806 end Instantiate_Package_Body;
8808 ---------------------------------
8809 -- Instantiate_Subprogram_Body --
8810 ---------------------------------
8812 procedure Instantiate_Subprogram_Body
8813 (Body_Info : Pending_Body_Info;
8814 Body_Optional : Boolean := False)
8816 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
8817 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
8818 Loc : constant Source_Ptr := Sloc (Inst_Node);
8819 Gen_Id : constant Node_Id := Name (Inst_Node);
8820 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8821 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
8822 Anon_Id : constant Entity_Id :=
8823 Defining_Unit_Name (Specification (Act_Decl));
8824 Pack_Id : constant Entity_Id :=
8825 Defining_Unit_Name (Parent (Act_Decl));
8826 Decls : List_Id;
8827 Gen_Body : Node_Id;
8828 Gen_Body_Id : Node_Id;
8829 Act_Body : Node_Id;
8830 Pack_Body : Node_Id;
8831 Prev_Formal : Entity_Id;
8832 Ret_Expr : Node_Id;
8833 Unit_Renaming : Node_Id;
8835 Parent_Installed : Boolean := False;
8836 Save_Style_Check : constant Boolean := Style_Check;
8838 Par_Ent : Entity_Id := Empty;
8839 Par_Vis : Boolean := False;
8841 begin
8842 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8844 -- Subprogram body may have been created already because of an inline
8845 -- pragma, or because of multiple elaborations of the enclosing package
8846 -- when several instances of the subprogram appear in the main unit.
8848 if Present (Corresponding_Body (Act_Decl)) then
8849 return;
8850 end if;
8852 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
8854 -- Re-establish the state of information on which checks are suppressed.
8855 -- This information was set in Body_Info at the point of instantiation,
8856 -- and now we restore it so that the instance is compiled using the
8857 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
8859 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
8860 Scope_Suppress := Body_Info.Scope_Suppress;
8861 Opt.Ada_Version := Body_Info.Version;
8863 if No (Gen_Body_Id) then
8865 -- For imported generic subprogram, no body to compile, complete
8866 -- the spec entity appropriately.
8868 if Is_Imported (Gen_Unit) then
8869 Set_Is_Imported (Anon_Id);
8870 Set_First_Rep_Item (Anon_Id, First_Rep_Item (Gen_Unit));
8871 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
8872 Set_Convention (Anon_Id, Convention (Gen_Unit));
8873 Set_Has_Completion (Anon_Id);
8874 return;
8876 -- For other cases, compile the body
8878 else
8879 Load_Parent_Of_Generic
8880 (Inst_Node, Specification (Gen_Decl), Body_Optional);
8881 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8882 end if;
8883 end if;
8885 Instantiation_Node := Inst_Node;
8887 if Present (Gen_Body_Id) then
8888 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
8889 Mark_Context (Inst_Node, Gen_Decl);
8891 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
8893 -- Either body is not present, or context is non-expanding, as
8894 -- when compiling a subunit. Mark the instance as completed, and
8895 -- diagnose a missing body when needed.
8897 if Expander_Active
8898 and then Operating_Mode = Generate_Code
8899 then
8900 Error_Msg_N
8901 ("missing proper body for instantiation", Gen_Body);
8902 end if;
8904 Set_Has_Completion (Anon_Id);
8905 return;
8906 end if;
8908 Save_Env (Gen_Unit, Anon_Id);
8909 Style_Check := False;
8910 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
8911 Create_Instantiation_Source
8912 (Inst_Node,
8913 Gen_Body_Id,
8914 False,
8915 S_Adjustment);
8917 Act_Body :=
8918 Copy_Generic_Node
8919 (Original_Node (Gen_Body), Empty, Instantiating => True);
8921 -- Create proper defining name for the body, to correspond to
8922 -- the one in the spec.
8924 Set_Defining_Unit_Name (Specification (Act_Body),
8925 Make_Defining_Identifier
8926 (Sloc (Defining_Entity (Inst_Node)), Chars (Anon_Id)));
8927 Set_Corresponding_Spec (Act_Body, Anon_Id);
8928 Set_Has_Completion (Anon_Id);
8929 Check_Generic_Actuals (Pack_Id, False);
8931 -- Generate a reference to link the visible subprogram instance to
8932 -- the generic body, which for navigation purposes is the only
8933 -- available source for the instance.
8935 Generate_Reference
8936 (Related_Instance (Pack_Id),
8937 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
8939 -- If it is a child unit, make the parent instance (which is an
8940 -- instance of the parent of the generic) visible. The parent
8941 -- instance is the prefix of the name of the generic unit.
8943 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
8944 and then Nkind (Gen_Id) = N_Expanded_Name
8945 then
8946 Par_Ent := Entity (Prefix (Gen_Id));
8947 Par_Vis := Is_Immediately_Visible (Par_Ent);
8948 Install_Parent (Par_Ent, In_Body => True);
8949 Parent_Installed := True;
8951 elsif Is_Child_Unit (Gen_Unit) then
8952 Par_Ent := Scope (Gen_Unit);
8953 Par_Vis := Is_Immediately_Visible (Par_Ent);
8954 Install_Parent (Par_Ent, In_Body => True);
8955 Parent_Installed := True;
8956 end if;
8958 -- Inside its body, a reference to the generic unit is a reference
8959 -- to the instance. The corresponding renaming is the first
8960 -- declaration in the body.
8962 Unit_Renaming :=
8963 Make_Subprogram_Renaming_Declaration (Loc,
8964 Specification =>
8965 Copy_Generic_Node (
8966 Specification (Original_Node (Gen_Body)),
8967 Empty,
8968 Instantiating => True),
8969 Name => New_Occurrence_Of (Anon_Id, Loc));
8971 -- If there is a formal subprogram with the same name as the unit
8972 -- itself, do not add this renaming declaration. This is a temporary
8973 -- fix for one ACVC test. ???
8975 Prev_Formal := First_Entity (Pack_Id);
8976 while Present (Prev_Formal) loop
8977 if Chars (Prev_Formal) = Chars (Gen_Unit)
8978 and then Is_Overloadable (Prev_Formal)
8979 then
8980 exit;
8981 end if;
8983 Next_Entity (Prev_Formal);
8984 end loop;
8986 if Present (Prev_Formal) then
8987 Decls := New_List (Act_Body);
8988 else
8989 Decls := New_List (Unit_Renaming, Act_Body);
8990 end if;
8992 -- The subprogram body is placed in the body of a dummy package body,
8993 -- whose spec contains the subprogram declaration as well as the
8994 -- renaming declarations for the generic parameters.
8996 Pack_Body := Make_Package_Body (Loc,
8997 Defining_Unit_Name => New_Copy (Pack_Id),
8998 Declarations => Decls);
9000 Set_Corresponding_Spec (Pack_Body, Pack_Id);
9002 -- If the instantiation is a library unit, then build resulting
9003 -- compilation unit nodes for the instance. The declaration of
9004 -- the enclosing package is the grandparent of the subprogram
9005 -- declaration. First replace the instantiation node as the unit
9006 -- of the corresponding compilation.
9008 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
9009 if Parent (Inst_Node) = Cunit (Main_Unit) then
9010 Set_Unit (Parent (Inst_Node), Inst_Node);
9011 Build_Instance_Compilation_Unit_Nodes
9012 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
9013 Analyze (Inst_Node);
9014 else
9015 Set_Parent (Pack_Body, Parent (Inst_Node));
9016 Analyze (Pack_Body);
9017 end if;
9019 else
9020 Insert_Before (Inst_Node, Pack_Body);
9021 Mark_Rewrite_Insertion (Pack_Body);
9022 Analyze (Pack_Body);
9024 if Expander_Active then
9025 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
9026 end if;
9027 end if;
9029 Inherit_Context (Gen_Body, Inst_Node);
9031 Restore_Private_Views (Pack_Id, False);
9033 if Parent_Installed then
9034 Remove_Parent (In_Body => True);
9036 -- Restore the previous visibility of the parent
9038 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
9039 end if;
9041 Restore_Env;
9042 Style_Check := Save_Style_Check;
9044 -- Body not found. Error was emitted already. If there were no previous
9045 -- errors, this may be an instance whose scope is a premature instance.
9046 -- In that case we must insure that the (legal) program does raise
9047 -- program error if executed. We generate a subprogram body for this
9048 -- purpose. See DEC ac30vso.
9050 -- Should not reference proprietary DEC tests in comments ???
9052 elsif Serious_Errors_Detected = 0
9053 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
9054 then
9055 if Body_Optional then
9056 return;
9058 elsif Ekind (Anon_Id) = E_Procedure then
9059 Act_Body :=
9060 Make_Subprogram_Body (Loc,
9061 Specification =>
9062 Make_Procedure_Specification (Loc,
9063 Defining_Unit_Name =>
9064 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
9065 Parameter_Specifications =>
9066 New_Copy_List
9067 (Parameter_Specifications (Parent (Anon_Id)))),
9069 Declarations => Empty_List,
9070 Handled_Statement_Sequence =>
9071 Make_Handled_Sequence_Of_Statements (Loc,
9072 Statements =>
9073 New_List (
9074 Make_Raise_Program_Error (Loc,
9075 Reason =>
9076 PE_Access_Before_Elaboration))));
9078 else
9079 Ret_Expr :=
9080 Make_Raise_Program_Error (Loc,
9081 Reason => PE_Access_Before_Elaboration);
9083 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
9084 Set_Analyzed (Ret_Expr);
9086 Act_Body :=
9087 Make_Subprogram_Body (Loc,
9088 Specification =>
9089 Make_Function_Specification (Loc,
9090 Defining_Unit_Name =>
9091 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
9092 Parameter_Specifications =>
9093 New_Copy_List
9094 (Parameter_Specifications (Parent (Anon_Id))),
9095 Result_Definition =>
9096 New_Occurrence_Of (Etype (Anon_Id), Loc)),
9098 Declarations => Empty_List,
9099 Handled_Statement_Sequence =>
9100 Make_Handled_Sequence_Of_Statements (Loc,
9101 Statements =>
9102 New_List
9103 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
9104 end if;
9106 Pack_Body := Make_Package_Body (Loc,
9107 Defining_Unit_Name => New_Copy (Pack_Id),
9108 Declarations => New_List (Act_Body));
9110 Insert_After (Inst_Node, Pack_Body);
9111 Set_Corresponding_Spec (Pack_Body, Pack_Id);
9112 Analyze (Pack_Body);
9113 end if;
9115 Expander_Mode_Restore;
9116 end Instantiate_Subprogram_Body;
9118 ----------------------
9119 -- Instantiate_Type --
9120 ----------------------
9122 function Instantiate_Type
9123 (Formal : Node_Id;
9124 Actual : Node_Id;
9125 Analyzed_Formal : Node_Id;
9126 Actual_Decls : List_Id) return List_Id
9128 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
9129 A_Gen_T : constant Entity_Id :=
9130 Defining_Identifier (Analyzed_Formal);
9131 Ancestor : Entity_Id := Empty;
9132 Def : constant Node_Id := Formal_Type_Definition (Formal);
9133 Act_T : Entity_Id;
9134 Decl_Node : Node_Id;
9135 Decl_Nodes : List_Id;
9136 Loc : Source_Ptr;
9137 Subt : Entity_Id;
9139 procedure Validate_Array_Type_Instance;
9140 procedure Validate_Access_Subprogram_Instance;
9141 procedure Validate_Access_Type_Instance;
9142 procedure Validate_Derived_Type_Instance;
9143 procedure Validate_Derived_Interface_Type_Instance;
9144 procedure Validate_Interface_Type_Instance;
9145 procedure Validate_Private_Type_Instance;
9146 -- These procedures perform validation tests for the named case
9148 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
9149 -- Check that base types are the same and that the subtypes match
9150 -- statically. Used in several of the above.
9152 --------------------
9153 -- Subtypes_Match --
9154 --------------------
9156 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
9157 T : constant Entity_Id := Get_Instance_Of (Gen_T);
9159 begin
9160 return (Base_Type (T) = Base_Type (Act_T)
9161 and then Subtypes_Statically_Match (T, Act_T))
9163 or else (Is_Class_Wide_Type (Gen_T)
9164 and then Is_Class_Wide_Type (Act_T)
9165 and then
9166 Subtypes_Match
9167 (Get_Instance_Of (Root_Type (Gen_T)),
9168 Root_Type (Act_T)))
9170 or else
9171 ((Ekind (Gen_T) = E_Anonymous_Access_Subprogram_Type
9172 or else Ekind (Gen_T) = E_Anonymous_Access_Type)
9173 and then Ekind (Act_T) = Ekind (Gen_T)
9174 and then
9175 Subtypes_Statically_Match
9176 (Designated_Type (Gen_T), Designated_Type (Act_T)));
9177 end Subtypes_Match;
9179 -----------------------------------------
9180 -- Validate_Access_Subprogram_Instance --
9181 -----------------------------------------
9183 procedure Validate_Access_Subprogram_Instance is
9184 begin
9185 if not Is_Access_Type (Act_T)
9186 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
9187 then
9188 Error_Msg_NE
9189 ("expect access type in instantiation of &", Actual, Gen_T);
9190 Abandon_Instantiation (Actual);
9191 end if;
9193 Check_Mode_Conformant
9194 (Designated_Type (Act_T),
9195 Designated_Type (A_Gen_T),
9196 Actual,
9197 Get_Inst => True);
9199 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
9200 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
9201 Error_Msg_NE
9202 ("protected access type not allowed for formal &",
9203 Actual, Gen_T);
9204 end if;
9206 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
9207 Error_Msg_NE
9208 ("expect protected access type for formal &",
9209 Actual, Gen_T);
9210 end if;
9211 end Validate_Access_Subprogram_Instance;
9213 -----------------------------------
9214 -- Validate_Access_Type_Instance --
9215 -----------------------------------
9217 procedure Validate_Access_Type_Instance is
9218 Desig_Type : constant Entity_Id :=
9219 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
9220 Desig_Act : Entity_Id;
9222 begin
9223 if not Is_Access_Type (Act_T) then
9224 Error_Msg_NE
9225 ("expect access type in instantiation of &", Actual, Gen_T);
9226 Abandon_Instantiation (Actual);
9227 end if;
9229 if Is_Access_Constant (A_Gen_T) then
9230 if not Is_Access_Constant (Act_T) then
9231 Error_Msg_N
9232 ("actual type must be access-to-constant type", Actual);
9233 Abandon_Instantiation (Actual);
9234 end if;
9235 else
9236 if Is_Access_Constant (Act_T) then
9237 Error_Msg_N
9238 ("actual type must be access-to-variable type", Actual);
9239 Abandon_Instantiation (Actual);
9241 elsif Ekind (A_Gen_T) = E_General_Access_Type
9242 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
9243 then
9244 Error_Msg_N -- CODEFIX
9245 ("actual must be general access type!", Actual);
9246 Error_Msg_NE -- CODEFIX
9247 ("add ALL to }!", Actual, Act_T);
9248 Abandon_Instantiation (Actual);
9249 end if;
9250 end if;
9252 -- The designated subtypes, that is to say the subtypes introduced
9253 -- by an access type declaration (and not by a subtype declaration)
9254 -- must match.
9256 Desig_Act := Designated_Type (Base_Type (Act_T));
9258 -- The designated type may have been introduced through a limited_
9259 -- with clause, in which case retrieve the non-limited view. This
9260 -- applies to incomplete types as well as to class-wide types.
9262 if From_With_Type (Desig_Act) then
9263 Desig_Act := Available_View (Desig_Act);
9264 end if;
9266 if not Subtypes_Match
9267 (Desig_Type, Desig_Act) then
9268 Error_Msg_NE
9269 ("designated type of actual does not match that of formal &",
9270 Actual, Gen_T);
9271 Abandon_Instantiation (Actual);
9273 elsif Is_Access_Type (Designated_Type (Act_T))
9274 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
9276 Is_Constrained (Designated_Type (Desig_Type))
9277 then
9278 Error_Msg_NE
9279 ("designated type of actual does not match that of formal &",
9280 Actual, Gen_T);
9281 Abandon_Instantiation (Actual);
9282 end if;
9284 -- Ada 2005: null-exclusion indicators of the two types must agree
9286 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
9287 Error_Msg_NE
9288 ("non null exclusion of actual and formal & do not match",
9289 Actual, Gen_T);
9290 end if;
9291 end Validate_Access_Type_Instance;
9293 ----------------------------------
9294 -- Validate_Array_Type_Instance --
9295 ----------------------------------
9297 procedure Validate_Array_Type_Instance is
9298 I1 : Node_Id;
9299 I2 : Node_Id;
9300 T2 : Entity_Id;
9302 function Formal_Dimensions return Int;
9303 -- Count number of dimensions in array type formal
9305 -----------------------
9306 -- Formal_Dimensions --
9307 -----------------------
9309 function Formal_Dimensions return Int is
9310 Num : Int := 0;
9311 Index : Node_Id;
9313 begin
9314 if Nkind (Def) = N_Constrained_Array_Definition then
9315 Index := First (Discrete_Subtype_Definitions (Def));
9316 else
9317 Index := First (Subtype_Marks (Def));
9318 end if;
9320 while Present (Index) loop
9321 Num := Num + 1;
9322 Next_Index (Index);
9323 end loop;
9325 return Num;
9326 end Formal_Dimensions;
9328 -- Start of processing for Validate_Array_Type_Instance
9330 begin
9331 if not Is_Array_Type (Act_T) then
9332 Error_Msg_NE
9333 ("expect array type in instantiation of &", Actual, Gen_T);
9334 Abandon_Instantiation (Actual);
9336 elsif Nkind (Def) = N_Constrained_Array_Definition then
9337 if not (Is_Constrained (Act_T)) then
9338 Error_Msg_NE
9339 ("expect constrained array in instantiation of &",
9340 Actual, Gen_T);
9341 Abandon_Instantiation (Actual);
9342 end if;
9344 else
9345 if Is_Constrained (Act_T) then
9346 Error_Msg_NE
9347 ("expect unconstrained array in instantiation of &",
9348 Actual, Gen_T);
9349 Abandon_Instantiation (Actual);
9350 end if;
9351 end if;
9353 if Formal_Dimensions /= Number_Dimensions (Act_T) then
9354 Error_Msg_NE
9355 ("dimensions of actual do not match formal &", Actual, Gen_T);
9356 Abandon_Instantiation (Actual);
9357 end if;
9359 I1 := First_Index (A_Gen_T);
9360 I2 := First_Index (Act_T);
9361 for J in 1 .. Formal_Dimensions loop
9363 -- If the indices of the actual were given by a subtype_mark,
9364 -- the index was transformed into a range attribute. Retrieve
9365 -- the original type mark for checking.
9367 if Is_Entity_Name (Original_Node (I2)) then
9368 T2 := Entity (Original_Node (I2));
9369 else
9370 T2 := Etype (I2);
9371 end if;
9373 if not Subtypes_Match
9374 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
9375 then
9376 Error_Msg_NE
9377 ("index types of actual do not match those of formal &",
9378 Actual, Gen_T);
9379 Abandon_Instantiation (Actual);
9380 end if;
9382 Next_Index (I1);
9383 Next_Index (I2);
9384 end loop;
9386 -- Check matching subtypes. Note that there are complex visibility
9387 -- issues when the generic is a child unit and some aspect of the
9388 -- generic type is declared in a parent unit of the generic. We do
9389 -- the test to handle this special case only after a direct check
9390 -- for static matching has failed.
9392 if Subtypes_Match
9393 (Component_Type (A_Gen_T), Component_Type (Act_T))
9394 or else Subtypes_Match
9395 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
9396 Component_Type (Act_T))
9397 then
9398 null;
9399 else
9400 Error_Msg_NE
9401 ("component subtype of actual does not match that of formal &",
9402 Actual, Gen_T);
9403 Abandon_Instantiation (Actual);
9404 end if;
9406 if Has_Aliased_Components (A_Gen_T)
9407 and then not Has_Aliased_Components (Act_T)
9408 then
9409 Error_Msg_NE
9410 ("actual must have aliased components to match formal type &",
9411 Actual, Gen_T);
9412 end if;
9413 end Validate_Array_Type_Instance;
9415 -----------------------------------------------
9416 -- Validate_Derived_Interface_Type_Instance --
9417 -----------------------------------------------
9419 procedure Validate_Derived_Interface_Type_Instance is
9420 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
9421 Elmt : Elmt_Id;
9423 begin
9424 -- First apply interface instance checks
9426 Validate_Interface_Type_Instance;
9428 -- Verify that immediate parent interface is an ancestor of
9429 -- the actual.
9431 if Present (Par)
9432 and then not Interface_Present_In_Ancestor (Act_T, Par)
9433 then
9434 Error_Msg_NE
9435 ("interface actual must include progenitor&", Actual, Par);
9436 end if;
9438 -- Now verify that the actual includes all other ancestors of
9439 -- the formal.
9441 Elmt := First_Elmt (Interfaces (A_Gen_T));
9442 while Present (Elmt) loop
9443 if not Interface_Present_In_Ancestor
9444 (Act_T, Get_Instance_Of (Node (Elmt)))
9445 then
9446 Error_Msg_NE
9447 ("interface actual must include progenitor&",
9448 Actual, Node (Elmt));
9449 end if;
9451 Next_Elmt (Elmt);
9452 end loop;
9453 end Validate_Derived_Interface_Type_Instance;
9455 ------------------------------------
9456 -- Validate_Derived_Type_Instance --
9457 ------------------------------------
9459 procedure Validate_Derived_Type_Instance is
9460 Actual_Discr : Entity_Id;
9461 Ancestor_Discr : Entity_Id;
9463 begin
9464 -- If the parent type in the generic declaration is itself a previous
9465 -- formal type, then it is local to the generic and absent from the
9466 -- analyzed generic definition. In that case the ancestor is the
9467 -- instance of the formal (which must have been instantiated
9468 -- previously), unless the ancestor is itself a formal derived type.
9469 -- In this latter case (which is the subject of Corrigendum 8652/0038
9470 -- (AI-202) the ancestor of the formals is the ancestor of its
9471 -- parent. Otherwise, the analyzed generic carries the parent type.
9472 -- If the parent type is defined in a previous formal package, then
9473 -- the scope of that formal package is that of the generic type
9474 -- itself, and it has already been mapped into the corresponding type
9475 -- in the actual package.
9477 -- Common case: parent type defined outside of the generic
9479 if Is_Entity_Name (Subtype_Mark (Def))
9480 and then Present (Entity (Subtype_Mark (Def)))
9481 then
9482 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
9484 -- Check whether parent is defined in a previous formal package
9486 elsif
9487 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
9488 then
9489 Ancestor :=
9490 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
9492 -- The type may be a local derivation, or a type extension of a
9493 -- previous formal, or of a formal of a parent package.
9495 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
9496 or else
9497 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
9498 then
9499 -- Check whether the parent is another derived formal type in the
9500 -- same generic unit.
9502 if Etype (A_Gen_T) /= A_Gen_T
9503 and then Is_Generic_Type (Etype (A_Gen_T))
9504 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
9505 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
9506 then
9507 -- Locate ancestor of parent from the subtype declaration
9508 -- created for the actual.
9510 declare
9511 Decl : Node_Id;
9513 begin
9514 Decl := First (Actual_Decls);
9515 while Present (Decl) loop
9516 if Nkind (Decl) = N_Subtype_Declaration
9517 and then Chars (Defining_Identifier (Decl)) =
9518 Chars (Etype (A_Gen_T))
9519 then
9520 Ancestor := Generic_Parent_Type (Decl);
9521 exit;
9522 else
9523 Next (Decl);
9524 end if;
9525 end loop;
9526 end;
9528 pragma Assert (Present (Ancestor));
9530 else
9531 Ancestor :=
9532 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
9533 end if;
9535 else
9536 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
9537 end if;
9539 -- If the formal derived type has pragma Preelaborable_Initialization
9540 -- then the actual type must have preelaborable initialization.
9542 if Known_To_Have_Preelab_Init (A_Gen_T)
9543 and then not Has_Preelaborable_Initialization (Act_T)
9544 then
9545 Error_Msg_NE
9546 ("actual for & must have preelaborable initialization",
9547 Actual, Gen_T);
9548 end if;
9550 -- Ada 2005 (AI-251)
9552 if Ada_Version >= Ada_05
9553 and then Is_Interface (Ancestor)
9554 then
9555 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
9556 Error_Msg_NE
9557 ("(Ada 2005) expected type implementing & in instantiation",
9558 Actual, Ancestor);
9559 end if;
9561 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
9562 Error_Msg_NE
9563 ("expect type derived from & in instantiation",
9564 Actual, First_Subtype (Ancestor));
9565 Abandon_Instantiation (Actual);
9566 end if;
9568 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
9569 -- that the formal type declaration has been rewritten as a private
9570 -- extension.
9572 if Ada_Version >= Ada_05
9573 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
9574 and then Synchronized_Present (Parent (A_Gen_T))
9575 then
9576 -- The actual must be a synchronized tagged type
9578 if not Is_Tagged_Type (Act_T) then
9579 Error_Msg_N
9580 ("actual of synchronized type must be tagged", Actual);
9581 Abandon_Instantiation (Actual);
9583 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
9584 and then Nkind (Type_Definition (Parent (Act_T))) =
9585 N_Derived_Type_Definition
9586 and then not Synchronized_Present (Type_Definition
9587 (Parent (Act_T)))
9588 then
9589 Error_Msg_N
9590 ("actual of synchronized type must be synchronized", Actual);
9591 Abandon_Instantiation (Actual);
9592 end if;
9593 end if;
9595 -- Perform atomic/volatile checks (RM C.6(12))
9597 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
9598 Error_Msg_N
9599 ("cannot have atomic actual type for non-atomic formal type",
9600 Actual);
9602 elsif Is_Volatile (Act_T)
9603 and then not Is_Volatile (Ancestor)
9604 and then Is_By_Reference_Type (Ancestor)
9605 then
9606 Error_Msg_N
9607 ("cannot have volatile actual type for non-volatile formal type",
9608 Actual);
9609 end if;
9611 -- It should not be necessary to check for unknown discriminants on
9612 -- Formal, but for some reason Has_Unknown_Discriminants is false for
9613 -- A_Gen_T, so Is_Indefinite_Subtype incorrectly returns False. This
9614 -- needs fixing. ???
9616 if not Is_Indefinite_Subtype (A_Gen_T)
9617 and then not Unknown_Discriminants_Present (Formal)
9618 and then Is_Indefinite_Subtype (Act_T)
9619 then
9620 Error_Msg_N
9621 ("actual subtype must be constrained", Actual);
9622 Abandon_Instantiation (Actual);
9623 end if;
9625 if not Unknown_Discriminants_Present (Formal) then
9626 if Is_Constrained (Ancestor) then
9627 if not Is_Constrained (Act_T) then
9628 Error_Msg_N
9629 ("actual subtype must be constrained", Actual);
9630 Abandon_Instantiation (Actual);
9631 end if;
9633 -- Ancestor is unconstrained, Check if generic formal and actual
9634 -- agree on constrainedness. The check only applies to array types
9635 -- and discriminated types.
9637 elsif Is_Constrained (Act_T) then
9638 if Ekind (Ancestor) = E_Access_Type
9639 or else
9640 (not Is_Constrained (A_Gen_T)
9641 and then Is_Composite_Type (A_Gen_T))
9642 then
9643 Error_Msg_N
9644 ("actual subtype must be unconstrained", Actual);
9645 Abandon_Instantiation (Actual);
9646 end if;
9648 -- A class-wide type is only allowed if the formal has unknown
9649 -- discriminants.
9651 elsif Is_Class_Wide_Type (Act_T)
9652 and then not Has_Unknown_Discriminants (Ancestor)
9653 then
9654 Error_Msg_NE
9655 ("actual for & cannot be a class-wide type", Actual, Gen_T);
9656 Abandon_Instantiation (Actual);
9658 -- Otherwise, the formal and actual shall have the same number
9659 -- of discriminants and each discriminant of the actual must
9660 -- correspond to a discriminant of the formal.
9662 elsif Has_Discriminants (Act_T)
9663 and then not Has_Unknown_Discriminants (Act_T)
9664 and then Has_Discriminants (Ancestor)
9665 then
9666 Actual_Discr := First_Discriminant (Act_T);
9667 Ancestor_Discr := First_Discriminant (Ancestor);
9668 while Present (Actual_Discr)
9669 and then Present (Ancestor_Discr)
9670 loop
9671 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
9672 No (Corresponding_Discriminant (Actual_Discr))
9673 then
9674 Error_Msg_NE
9675 ("discriminant & does not correspond " &
9676 "to ancestor discriminant", Actual, Actual_Discr);
9677 Abandon_Instantiation (Actual);
9678 end if;
9680 Next_Discriminant (Actual_Discr);
9681 Next_Discriminant (Ancestor_Discr);
9682 end loop;
9684 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
9685 Error_Msg_NE
9686 ("actual for & must have same number of discriminants",
9687 Actual, Gen_T);
9688 Abandon_Instantiation (Actual);
9689 end if;
9691 -- This case should be caught by the earlier check for
9692 -- constrainedness, but the check here is added for completeness.
9694 elsif Has_Discriminants (Act_T)
9695 and then not Has_Unknown_Discriminants (Act_T)
9696 then
9697 Error_Msg_NE
9698 ("actual for & must not have discriminants", Actual, Gen_T);
9699 Abandon_Instantiation (Actual);
9701 elsif Has_Discriminants (Ancestor) then
9702 Error_Msg_NE
9703 ("actual for & must have known discriminants", Actual, Gen_T);
9704 Abandon_Instantiation (Actual);
9705 end if;
9707 if not Subtypes_Statically_Compatible (Act_T, Ancestor) then
9708 Error_Msg_N
9709 ("constraint on actual is incompatible with formal", Actual);
9710 Abandon_Instantiation (Actual);
9711 end if;
9712 end if;
9714 -- If the formal and actual types are abstract, check that there
9715 -- are no abstract primitives of the actual type that correspond to
9716 -- nonabstract primitives of the formal type (second sentence of
9717 -- RM95-3.9.3(9)).
9719 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
9720 Check_Abstract_Primitives : declare
9721 Gen_Prims : constant Elist_Id :=
9722 Primitive_Operations (A_Gen_T);
9723 Gen_Elmt : Elmt_Id;
9724 Gen_Subp : Entity_Id;
9725 Anc_Subp : Entity_Id;
9726 Anc_Formal : Entity_Id;
9727 Anc_F_Type : Entity_Id;
9729 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
9730 Act_Elmt : Elmt_Id;
9731 Act_Subp : Entity_Id;
9732 Act_Formal : Entity_Id;
9733 Act_F_Type : Entity_Id;
9735 Subprograms_Correspond : Boolean;
9737 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
9738 -- Returns true if T2 is derived directly or indirectly from
9739 -- T1, including derivations from interfaces. T1 and T2 are
9740 -- required to be specific tagged base types.
9742 ------------------------
9743 -- Is_Tagged_Ancestor --
9744 ------------------------
9746 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
9748 Intfc_Elmt : Elmt_Id;
9750 begin
9751 -- The predicate is satisfied if the types are the same
9753 if T1 = T2 then
9754 return True;
9756 -- If we've reached the top of the derivation chain then
9757 -- we know that T1 is not an ancestor of T2.
9759 elsif Etype (T2) = T2 then
9760 return False;
9762 -- Proceed to check T2's immediate parent
9764 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
9765 return True;
9767 -- Finally, check to see if T1 is an ancestor of any of T2's
9768 -- progenitors.
9770 else
9771 Intfc_Elmt := First_Elmt (Interfaces (T2));
9772 while Present (Intfc_Elmt) loop
9773 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
9774 return True;
9775 end if;
9777 Next_Elmt (Intfc_Elmt);
9778 end loop;
9779 end if;
9781 return False;
9782 end Is_Tagged_Ancestor;
9784 -- Start of processing for Check_Abstract_Primitives
9786 begin
9787 -- Loop over all of the formal derived type's primitives
9789 Gen_Elmt := First_Elmt (Gen_Prims);
9790 while Present (Gen_Elmt) loop
9791 Gen_Subp := Node (Gen_Elmt);
9793 -- If the primitive of the formal is not abstract, then
9794 -- determine whether there is a corresponding primitive of
9795 -- the actual type that's abstract.
9797 if not Is_Abstract_Subprogram (Gen_Subp) then
9798 Act_Elmt := First_Elmt (Act_Prims);
9799 while Present (Act_Elmt) loop
9800 Act_Subp := Node (Act_Elmt);
9802 -- If we find an abstract primitive of the actual,
9803 -- then we need to test whether it corresponds to the
9804 -- subprogram from which the generic formal primitive
9805 -- is inherited.
9807 if Is_Abstract_Subprogram (Act_Subp) then
9808 Anc_Subp := Alias (Gen_Subp);
9810 -- Test whether we have a corresponding primitive
9811 -- by comparing names, kinds, formal types, and
9812 -- result types.
9814 if Chars (Anc_Subp) = Chars (Act_Subp)
9815 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
9816 then
9817 Anc_Formal := First_Formal (Anc_Subp);
9818 Act_Formal := First_Formal (Act_Subp);
9819 while Present (Anc_Formal)
9820 and then Present (Act_Formal)
9821 loop
9822 Anc_F_Type := Etype (Anc_Formal);
9823 Act_F_Type := Etype (Act_Formal);
9825 if Ekind (Anc_F_Type)
9826 = E_Anonymous_Access_Type
9827 then
9828 Anc_F_Type := Designated_Type (Anc_F_Type);
9830 if Ekind (Act_F_Type)
9831 = E_Anonymous_Access_Type
9832 then
9833 Act_F_Type :=
9834 Designated_Type (Act_F_Type);
9835 else
9836 exit;
9837 end if;
9839 elsif
9840 Ekind (Act_F_Type) = E_Anonymous_Access_Type
9841 then
9842 exit;
9843 end if;
9845 Anc_F_Type := Base_Type (Anc_F_Type);
9846 Act_F_Type := Base_Type (Act_F_Type);
9848 -- If the formal is controlling, then the
9849 -- the type of the actual primitive's formal
9850 -- must be derived directly or indirectly
9851 -- from the type of the ancestor primitive's
9852 -- formal.
9854 if Is_Controlling_Formal (Anc_Formal) then
9855 if not Is_Tagged_Ancestor
9856 (Anc_F_Type, Act_F_Type)
9857 then
9858 exit;
9859 end if;
9861 -- Otherwise the types of the formals must
9862 -- be the same.
9864 elsif Anc_F_Type /= Act_F_Type then
9865 exit;
9866 end if;
9868 Next_Entity (Anc_Formal);
9869 Next_Entity (Act_Formal);
9870 end loop;
9872 -- If we traversed through all of the formals
9873 -- then so far the subprograms correspond, so
9874 -- now check that any result types correspond.
9876 if No (Anc_Formal) and then No (Act_Formal) then
9877 Subprograms_Correspond := True;
9879 if Ekind (Act_Subp) = E_Function then
9880 Anc_F_Type := Etype (Anc_Subp);
9881 Act_F_Type := Etype (Act_Subp);
9883 if Ekind (Anc_F_Type)
9884 = E_Anonymous_Access_Type
9885 then
9886 Anc_F_Type :=
9887 Designated_Type (Anc_F_Type);
9889 if Ekind (Act_F_Type)
9890 = E_Anonymous_Access_Type
9891 then
9892 Act_F_Type :=
9893 Designated_Type (Act_F_Type);
9894 else
9895 Subprograms_Correspond := False;
9896 end if;
9898 elsif
9899 Ekind (Act_F_Type)
9900 = E_Anonymous_Access_Type
9901 then
9902 Subprograms_Correspond := False;
9903 end if;
9905 Anc_F_Type := Base_Type (Anc_F_Type);
9906 Act_F_Type := Base_Type (Act_F_Type);
9908 -- Now either the result types must be
9909 -- the same or, if the result type is
9910 -- controlling, the result type of the
9911 -- actual primitive must descend from the
9912 -- result type of the ancestor primitive.
9914 if Subprograms_Correspond
9915 and then Anc_F_Type /= Act_F_Type
9916 and then
9917 Has_Controlling_Result (Anc_Subp)
9918 and then
9919 not Is_Tagged_Ancestor
9920 (Anc_F_Type, Act_F_Type)
9921 then
9922 Subprograms_Correspond := False;
9923 end if;
9924 end if;
9926 -- Found a matching subprogram belonging to
9927 -- formal ancestor type, so actual subprogram
9928 -- corresponds and this violates 3.9.3(9).
9930 if Subprograms_Correspond then
9931 Error_Msg_NE
9932 ("abstract subprogram & overrides " &
9933 "nonabstract subprogram of ancestor",
9934 Actual,
9935 Act_Subp);
9936 end if;
9937 end if;
9938 end if;
9939 end if;
9941 Next_Elmt (Act_Elmt);
9942 end loop;
9943 end if;
9945 Next_Elmt (Gen_Elmt);
9946 end loop;
9947 end Check_Abstract_Primitives;
9948 end if;
9950 -- Verify that limitedness matches. If parent is a limited
9951 -- interface then the generic formal is not unless declared
9952 -- explicitly so. If not declared limited, the actual cannot be
9953 -- limited (see AI05-0087).
9954 -- Disable check for now, limited interfaces implemented by
9955 -- protected types are common, Need to update tests ???
9957 if Is_Limited_Type (Act_T)
9958 and then not Is_Limited_Type (A_Gen_T)
9959 and then False
9960 then
9961 Error_Msg_NE
9962 ("actual for non-limited & cannot be a limited type", Actual,
9963 Gen_T);
9964 Explain_Limited_Type (Act_T, Actual);
9965 Abandon_Instantiation (Actual);
9966 end if;
9967 end Validate_Derived_Type_Instance;
9969 --------------------------------------
9970 -- Validate_Interface_Type_Instance --
9971 --------------------------------------
9973 procedure Validate_Interface_Type_Instance is
9974 begin
9975 if not Is_Interface (Act_T) then
9976 Error_Msg_NE
9977 ("actual for formal interface type must be an interface",
9978 Actual, Gen_T);
9980 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
9981 or else
9982 Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
9983 or else
9984 Is_Protected_Interface (A_Gen_T) /=
9985 Is_Protected_Interface (Act_T)
9986 or else
9987 Is_Synchronized_Interface (A_Gen_T) /=
9988 Is_Synchronized_Interface (Act_T)
9989 then
9990 Error_Msg_NE
9991 ("actual for interface& does not match (RM 12.5.5(4))",
9992 Actual, Gen_T);
9993 end if;
9994 end Validate_Interface_Type_Instance;
9996 ------------------------------------
9997 -- Validate_Private_Type_Instance --
9998 ------------------------------------
10000 procedure Validate_Private_Type_Instance is
10001 Formal_Discr : Entity_Id;
10002 Actual_Discr : Entity_Id;
10003 Formal_Subt : Entity_Id;
10005 begin
10006 if Is_Limited_Type (Act_T)
10007 and then not Is_Limited_Type (A_Gen_T)
10008 then
10009 Error_Msg_NE
10010 ("actual for non-limited & cannot be a limited type", Actual,
10011 Gen_T);
10012 Explain_Limited_Type (Act_T, Actual);
10013 Abandon_Instantiation (Actual);
10015 elsif Known_To_Have_Preelab_Init (A_Gen_T)
10016 and then not Has_Preelaborable_Initialization (Act_T)
10017 then
10018 Error_Msg_NE
10019 ("actual for & must have preelaborable initialization", Actual,
10020 Gen_T);
10022 elsif Is_Indefinite_Subtype (Act_T)
10023 and then not Is_Indefinite_Subtype (A_Gen_T)
10024 and then Ada_Version >= Ada_95
10025 then
10026 Error_Msg_NE
10027 ("actual for & must be a definite subtype", Actual, Gen_T);
10029 elsif not Is_Tagged_Type (Act_T)
10030 and then Is_Tagged_Type (A_Gen_T)
10031 then
10032 Error_Msg_NE
10033 ("actual for & must be a tagged type", Actual, Gen_T);
10035 elsif Has_Discriminants (A_Gen_T) then
10036 if not Has_Discriminants (Act_T) then
10037 Error_Msg_NE
10038 ("actual for & must have discriminants", Actual, Gen_T);
10039 Abandon_Instantiation (Actual);
10041 elsif Is_Constrained (Act_T) then
10042 Error_Msg_NE
10043 ("actual for & must be unconstrained", Actual, Gen_T);
10044 Abandon_Instantiation (Actual);
10046 else
10047 Formal_Discr := First_Discriminant (A_Gen_T);
10048 Actual_Discr := First_Discriminant (Act_T);
10049 while Formal_Discr /= Empty loop
10050 if Actual_Discr = Empty then
10051 Error_Msg_NE
10052 ("discriminants on actual do not match formal",
10053 Actual, Gen_T);
10054 Abandon_Instantiation (Actual);
10055 end if;
10057 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
10059 -- Access discriminants match if designated types do
10061 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
10062 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
10063 E_Anonymous_Access_Type
10064 and then
10065 Get_Instance_Of
10066 (Designated_Type (Base_Type (Formal_Subt))) =
10067 Designated_Type (Base_Type (Etype (Actual_Discr)))
10068 then
10069 null;
10071 elsif Base_Type (Formal_Subt) /=
10072 Base_Type (Etype (Actual_Discr))
10073 then
10074 Error_Msg_NE
10075 ("types of actual discriminants must match formal",
10076 Actual, Gen_T);
10077 Abandon_Instantiation (Actual);
10079 elsif not Subtypes_Statically_Match
10080 (Formal_Subt, Etype (Actual_Discr))
10081 and then Ada_Version >= Ada_95
10082 then
10083 Error_Msg_NE
10084 ("subtypes of actual discriminants must match formal",
10085 Actual, Gen_T);
10086 Abandon_Instantiation (Actual);
10087 end if;
10089 Next_Discriminant (Formal_Discr);
10090 Next_Discriminant (Actual_Discr);
10091 end loop;
10093 if Actual_Discr /= Empty then
10094 Error_Msg_NE
10095 ("discriminants on actual do not match formal",
10096 Actual, Gen_T);
10097 Abandon_Instantiation (Actual);
10098 end if;
10099 end if;
10101 end if;
10103 Ancestor := Gen_T;
10104 end Validate_Private_Type_Instance;
10106 -- Start of processing for Instantiate_Type
10108 begin
10109 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
10110 Error_Msg_N ("duplicate instantiation of generic type", Actual);
10111 return New_List (Error);
10113 elsif not Is_Entity_Name (Actual)
10114 or else not Is_Type (Entity (Actual))
10115 then
10116 Error_Msg_NE
10117 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
10118 Abandon_Instantiation (Actual);
10120 else
10121 Act_T := Entity (Actual);
10123 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
10124 -- as a generic actual parameter if the corresponding formal type
10125 -- does not have a known_discriminant_part, or is a formal derived
10126 -- type that is an Unchecked_Union type.
10128 if Is_Unchecked_Union (Base_Type (Act_T)) then
10129 if not Has_Discriminants (A_Gen_T)
10130 or else
10131 (Is_Derived_Type (A_Gen_T)
10132 and then
10133 Is_Unchecked_Union (A_Gen_T))
10134 then
10135 null;
10136 else
10137 Error_Msg_N ("Unchecked_Union cannot be the actual for a" &
10138 " discriminated formal type", Act_T);
10140 end if;
10141 end if;
10143 -- Deal with fixed/floating restrictions
10145 if Is_Floating_Point_Type (Act_T) then
10146 Check_Restriction (No_Floating_Point, Actual);
10147 elsif Is_Fixed_Point_Type (Act_T) then
10148 Check_Restriction (No_Fixed_Point, Actual);
10149 end if;
10151 -- Deal with error of using incomplete type as generic actual.
10152 -- This includes limited views of a type, even if the non-limited
10153 -- view may be available.
10155 if Ekind (Act_T) = E_Incomplete_Type
10156 or else (Is_Class_Wide_Type (Act_T)
10157 and then
10158 Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
10159 then
10160 if Is_Class_Wide_Type (Act_T)
10161 or else No (Full_View (Act_T))
10162 then
10163 Error_Msg_N ("premature use of incomplete type", Actual);
10164 Abandon_Instantiation (Actual);
10165 else
10166 Act_T := Full_View (Act_T);
10167 Set_Entity (Actual, Act_T);
10169 if Has_Private_Component (Act_T) then
10170 Error_Msg_N
10171 ("premature use of type with private component", Actual);
10172 end if;
10173 end if;
10175 -- Deal with error of premature use of private type as generic actual
10177 elsif Is_Private_Type (Act_T)
10178 and then Is_Private_Type (Base_Type (Act_T))
10179 and then not Is_Generic_Type (Act_T)
10180 and then not Is_Derived_Type (Act_T)
10181 and then No (Full_View (Root_Type (Act_T)))
10182 then
10183 Error_Msg_N ("premature use of private type", Actual);
10185 elsif Has_Private_Component (Act_T) then
10186 Error_Msg_N
10187 ("premature use of type with private component", Actual);
10188 end if;
10190 Set_Instance_Of (A_Gen_T, Act_T);
10192 -- If the type is generic, the class-wide type may also be used
10194 if Is_Tagged_Type (A_Gen_T)
10195 and then Is_Tagged_Type (Act_T)
10196 and then not Is_Class_Wide_Type (A_Gen_T)
10197 then
10198 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
10199 Class_Wide_Type (Act_T));
10200 end if;
10202 if not Is_Abstract_Type (A_Gen_T)
10203 and then Is_Abstract_Type (Act_T)
10204 then
10205 Error_Msg_N
10206 ("actual of non-abstract formal cannot be abstract", Actual);
10207 end if;
10209 -- A generic scalar type is a first subtype for which we generate
10210 -- an anonymous base type. Indicate that the instance of this base
10211 -- is the base type of the actual.
10213 if Is_Scalar_Type (A_Gen_T) then
10214 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
10215 end if;
10216 end if;
10218 if Error_Posted (Act_T) then
10219 null;
10220 else
10221 case Nkind (Def) is
10222 when N_Formal_Private_Type_Definition =>
10223 Validate_Private_Type_Instance;
10225 when N_Formal_Derived_Type_Definition =>
10226 Validate_Derived_Type_Instance;
10228 when N_Formal_Discrete_Type_Definition =>
10229 if not Is_Discrete_Type (Act_T) then
10230 Error_Msg_NE
10231 ("expect discrete type in instantiation of&",
10232 Actual, Gen_T);
10233 Abandon_Instantiation (Actual);
10234 end if;
10236 when N_Formal_Signed_Integer_Type_Definition =>
10237 if not Is_Signed_Integer_Type (Act_T) then
10238 Error_Msg_NE
10239 ("expect signed integer type in instantiation of&",
10240 Actual, Gen_T);
10241 Abandon_Instantiation (Actual);
10242 end if;
10244 when N_Formal_Modular_Type_Definition =>
10245 if not Is_Modular_Integer_Type (Act_T) then
10246 Error_Msg_NE
10247 ("expect modular type in instantiation of &",
10248 Actual, Gen_T);
10249 Abandon_Instantiation (Actual);
10250 end if;
10252 when N_Formal_Floating_Point_Definition =>
10253 if not Is_Floating_Point_Type (Act_T) then
10254 Error_Msg_NE
10255 ("expect float type in instantiation of &", Actual, Gen_T);
10256 Abandon_Instantiation (Actual);
10257 end if;
10259 when N_Formal_Ordinary_Fixed_Point_Definition =>
10260 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
10261 Error_Msg_NE
10262 ("expect ordinary fixed point type in instantiation of &",
10263 Actual, Gen_T);
10264 Abandon_Instantiation (Actual);
10265 end if;
10267 when N_Formal_Decimal_Fixed_Point_Definition =>
10268 if not Is_Decimal_Fixed_Point_Type (Act_T) then
10269 Error_Msg_NE
10270 ("expect decimal type in instantiation of &",
10271 Actual, Gen_T);
10272 Abandon_Instantiation (Actual);
10273 end if;
10275 when N_Array_Type_Definition =>
10276 Validate_Array_Type_Instance;
10278 when N_Access_To_Object_Definition =>
10279 Validate_Access_Type_Instance;
10281 when N_Access_Function_Definition |
10282 N_Access_Procedure_Definition =>
10283 Validate_Access_Subprogram_Instance;
10285 when N_Record_Definition =>
10286 Validate_Interface_Type_Instance;
10288 when N_Derived_Type_Definition =>
10289 Validate_Derived_Interface_Type_Instance;
10291 when others =>
10292 raise Program_Error;
10294 end case;
10295 end if;
10297 Subt := New_Copy (Gen_T);
10299 -- Use adjusted sloc of subtype name as the location for other nodes in
10300 -- the subtype declaration.
10302 Loc := Sloc (Subt);
10304 Decl_Node :=
10305 Make_Subtype_Declaration (Loc,
10306 Defining_Identifier => Subt,
10307 Subtype_Indication => New_Reference_To (Act_T, Loc));
10309 if Is_Private_Type (Act_T) then
10310 Set_Has_Private_View (Subtype_Indication (Decl_Node));
10312 elsif Is_Access_Type (Act_T)
10313 and then Is_Private_Type (Designated_Type (Act_T))
10314 then
10315 Set_Has_Private_View (Subtype_Indication (Decl_Node));
10316 end if;
10318 Decl_Nodes := New_List (Decl_Node);
10320 -- Flag actual derived types so their elaboration produces the
10321 -- appropriate renamings for the primitive operations of the ancestor.
10322 -- Flag actual for formal private types as well, to determine whether
10323 -- operations in the private part may override inherited operations.
10324 -- If the formal has an interface list, the ancestor is not the
10325 -- parent, but the analyzed formal that includes the interface
10326 -- operations of all its progenitors.
10328 if Nkind (Def) = N_Formal_Derived_Type_Definition then
10329 if Present (Interface_List (Def)) then
10330 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
10331 else
10332 Set_Generic_Parent_Type (Decl_Node, Ancestor);
10333 end if;
10335 elsif Nkind (Def) = N_Formal_Private_Type_Definition then
10336 Set_Generic_Parent_Type (Decl_Node, Ancestor);
10337 end if;
10339 -- If the actual is a synchronized type that implements an interface,
10340 -- the primitive operations are attached to the corresponding record,
10341 -- and we have to treat it as an additional generic actual, so that its
10342 -- primitive operations become visible in the instance. The task or
10343 -- protected type itself does not carry primitive operations.
10345 if Is_Concurrent_Type (Act_T)
10346 and then Is_Tagged_Type (Act_T)
10347 and then Present (Corresponding_Record_Type (Act_T))
10348 and then Present (Ancestor)
10349 and then Is_Interface (Ancestor)
10350 then
10351 declare
10352 Corr_Rec : constant Entity_Id :=
10353 Corresponding_Record_Type (Act_T);
10354 New_Corr : Entity_Id;
10355 Corr_Decl : Node_Id;
10357 begin
10358 New_Corr := Make_Temporary (Loc, 'S');
10359 Corr_Decl :=
10360 Make_Subtype_Declaration (Loc,
10361 Defining_Identifier => New_Corr,
10362 Subtype_Indication =>
10363 New_Reference_To (Corr_Rec, Loc));
10364 Append_To (Decl_Nodes, Corr_Decl);
10366 if Ekind (Act_T) = E_Task_Type then
10367 Set_Ekind (Subt, E_Task_Subtype);
10368 else
10369 Set_Ekind (Subt, E_Protected_Subtype);
10370 end if;
10372 Set_Corresponding_Record_Type (Subt, Corr_Rec);
10373 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
10374 Set_Generic_Parent_Type (Decl_Node, Empty);
10375 end;
10376 end if;
10378 return Decl_Nodes;
10379 end Instantiate_Type;
10381 -----------------------
10382 -- Is_Generic_Formal --
10383 -----------------------
10385 function Is_Generic_Formal (E : Entity_Id) return Boolean is
10386 Kind : Node_Kind;
10387 begin
10388 if No (E) then
10389 return False;
10390 else
10391 Kind := Nkind (Parent (E));
10392 return
10393 Nkind_In (Kind, N_Formal_Object_Declaration,
10394 N_Formal_Package_Declaration,
10395 N_Formal_Type_Declaration)
10396 or else
10397 (Is_Formal_Subprogram (E)
10398 and then
10399 Nkind (Parent (Parent (E))) in
10400 N_Formal_Subprogram_Declaration);
10401 end if;
10402 end Is_Generic_Formal;
10404 ------------------
10405 -- Mark_Context --
10406 ------------------
10408 procedure Mark_Context (Inst_Decl : Node_Id; Gen_Decl : Node_Id) is
10409 Loc : constant Source_Ptr := Sloc (Inst_Decl);
10410 Inst_CU : constant Unit_Number_Type := Get_Code_Unit (Inst_Decl);
10412 -- Note that we use Get_Code_Unit to determine the position of the
10413 -- instantiation, because it may itself appear within another instance
10414 -- and we need to mark the context of the enclosing unit, not that of
10415 -- the unit that contains the generic.
10417 Gen_CU : constant Unit_Number_Type := Get_Source_Unit (Gen_Decl);
10418 Inst : Entity_Id;
10419 Clause : Node_Id;
10420 Scop : Entity_Id;
10422 procedure Add_Implicit_With (CU : Unit_Number_Type);
10423 -- If a generic is instantiated in the direct or indirect context of
10424 -- the current unit, but there is no with_clause for it in the current
10425 -- context, add a with_clause for it to indicate that the body of the
10426 -- generic should be examined before the current unit.
10428 procedure Add_Implicit_With (CU : Unit_Number_Type) is
10429 Withn : constant Node_Id :=
10430 Make_With_Clause (Loc,
10431 Name => New_Occurrence_Of (Cunit_Entity (CU), Loc));
10432 begin
10433 Set_Implicit_With (Withn);
10434 Set_Library_Unit (Withn, Cunit (CU));
10435 Set_Withed_Body (Withn, Cunit (CU));
10436 Prepend (Withn, Context_Items (Cunit (Inst_CU)));
10437 end Add_Implicit_With;
10439 begin
10440 -- This is only relevant when compiling for CodePeer. In what follows,
10441 -- C is the current unit containing the instance body, and G is the
10442 -- generic unit in that instance.
10444 if not CodePeer_Mode then
10445 return;
10446 end if;
10448 -- Nothing to do if G is local.
10450 if Inst_CU = Gen_CU then
10451 return;
10452 end if;
10454 -- If G is itself declared within an instance, indicate that the
10455 -- generic body of that instance is also needed by C. This must be
10456 -- done recursively.
10458 Scop := Scope (Defining_Entity (Gen_Decl));
10460 while Is_Generic_Instance (Scop)
10461 and then Ekind (Scop) = E_Package
10462 loop
10463 Mark_Context
10464 (Inst_Decl,
10465 Unit_Declaration_Node
10466 (Generic_Parent
10467 (Specification (Unit_Declaration_Node (Scop)))));
10468 Scop := Scope (Scop);
10469 end loop;
10471 -- Add references to other generic units in the context of G, because
10472 -- they may be instantiated within G, and their bodies needed by C.
10474 Clause := First (Context_Items (Cunit (Gen_CU)));
10476 while Present (Clause) loop
10477 if Nkind (Clause) = N_With_Clause
10478 and then
10479 Nkind (Unit (Library_Unit (Clause)))
10480 = N_Generic_Package_Declaration
10481 then
10482 Add_Implicit_With (Get_Source_Unit (Library_Unit (Clause)));
10483 end if;
10485 Next (Clause);
10486 end loop;
10488 -- Now indicate that the body of G is needed by C
10490 Clause := First (Context_Items (Cunit (Inst_CU)));
10491 while Present (Clause) loop
10492 if Nkind (Clause) = N_With_Clause
10493 and then Library_Unit (Clause) = Cunit (Gen_CU)
10494 then
10495 Set_Withed_Body (Clause, Cunit (Gen_CU));
10496 return;
10497 end if;
10499 Next (Clause);
10500 end loop;
10502 -- If the with-clause for G is not in the context of C, it may appear in
10503 -- some ancestor of C.
10505 Inst := Cunit_Entity (Inst_CU);
10506 while Is_Child_Unit (Inst) loop
10507 Inst := Scope (Inst);
10509 Clause :=
10510 First (Context_Items (Parent (Unit_Declaration_Node (Inst))));
10511 while Present (Clause) loop
10512 if Nkind (Clause) = N_With_Clause
10513 and then Library_Unit (Clause) = Cunit (Gen_CU)
10514 then
10515 Set_Withed_Body (Clause, Cunit (Gen_CU));
10516 return;
10517 end if;
10519 Next (Clause);
10520 end loop;
10521 end loop;
10523 -- If not found, G comes from an instance elsewhere in the context. Make
10524 -- the dependence explicit in the context of C.
10526 Add_Implicit_With (Gen_CU);
10527 end Mark_Context;
10529 ---------------------
10530 -- Is_In_Main_Unit --
10531 ---------------------
10533 function Is_In_Main_Unit (N : Node_Id) return Boolean is
10534 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
10535 Current_Unit : Node_Id;
10537 begin
10538 if Unum = Main_Unit then
10539 return True;
10541 -- If the current unit is a subunit then it is either the main unit or
10542 -- is being compiled as part of the main unit.
10544 elsif Nkind (N) = N_Compilation_Unit then
10545 return Nkind (Unit (N)) = N_Subunit;
10546 end if;
10548 Current_Unit := Parent (N);
10549 while Present (Current_Unit)
10550 and then Nkind (Current_Unit) /= N_Compilation_Unit
10551 loop
10552 Current_Unit := Parent (Current_Unit);
10553 end loop;
10555 -- The instantiation node is in the main unit, or else the current node
10556 -- (perhaps as the result of nested instantiations) is in the main unit,
10557 -- or in the declaration of the main unit, which in this last case must
10558 -- be a body.
10560 return Unum = Main_Unit
10561 or else Current_Unit = Cunit (Main_Unit)
10562 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
10563 or else (Present (Library_Unit (Current_Unit))
10564 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
10565 end Is_In_Main_Unit;
10567 ----------------------------
10568 -- Load_Parent_Of_Generic --
10569 ----------------------------
10571 procedure Load_Parent_Of_Generic
10572 (N : Node_Id;
10573 Spec : Node_Id;
10574 Body_Optional : Boolean := False)
10576 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
10577 Save_Style_Check : constant Boolean := Style_Check;
10578 True_Parent : Node_Id;
10579 Inst_Node : Node_Id;
10580 OK : Boolean;
10581 Previous_Instances : constant Elist_Id := New_Elmt_List;
10583 procedure Collect_Previous_Instances (Decls : List_Id);
10584 -- Collect all instantiations in the given list of declarations, that
10585 -- precede the generic that we need to load. If the bodies of these
10586 -- instantiations are available, we must analyze them, to ensure that
10587 -- the public symbols generated are the same when the unit is compiled
10588 -- to generate code, and when it is compiled in the context of a unit
10589 -- that needs a particular nested instance. This process is applied to
10590 -- both package and subprogram instances.
10592 --------------------------------
10593 -- Collect_Previous_Instances --
10594 --------------------------------
10596 procedure Collect_Previous_Instances (Decls : List_Id) is
10597 Decl : Node_Id;
10599 begin
10600 Decl := First (Decls);
10601 while Present (Decl) loop
10602 if Sloc (Decl) >= Sloc (Inst_Node) then
10603 return;
10605 -- If Decl is an instantiation, then record it as requiring
10606 -- instantiation of the corresponding body, except if it is an
10607 -- abbreviated instantiation generated internally for conformance
10608 -- checking purposes only for the case of a formal package
10609 -- declared without a box (see Instantiate_Formal_Package). Such
10610 -- an instantiation does not generate any code (the actual code
10611 -- comes from actual) and thus does not need to be analyzed here.
10612 -- If the instantiation appears with a generic package body it is
10613 -- not analyzed here either.
10615 elsif Nkind (Decl) = N_Package_Instantiation
10616 and then not Is_Internal (Defining_Entity (Decl))
10617 then
10618 Append_Elmt (Decl, Previous_Instances);
10620 -- For a subprogram instantiation, omit instantiations intrinsic
10621 -- operations (Unchecked_Conversions, etc.) that have no bodies.
10623 elsif Nkind_In (Decl, N_Function_Instantiation,
10624 N_Procedure_Instantiation)
10625 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
10626 then
10627 Append_Elmt (Decl, Previous_Instances);
10629 elsif Nkind (Decl) = N_Package_Declaration then
10630 Collect_Previous_Instances
10631 (Visible_Declarations (Specification (Decl)));
10632 Collect_Previous_Instances
10633 (Private_Declarations (Specification (Decl)));
10635 -- Previous non-generic bodies may contain instances as well
10637 elsif Nkind (Decl) = N_Package_Body
10638 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
10639 then
10640 Collect_Previous_Instances (Declarations (Decl));
10642 elsif Nkind (Decl) = N_Subprogram_Body
10643 and then not Acts_As_Spec (Decl)
10644 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
10645 then
10646 Collect_Previous_Instances (Declarations (Decl));
10647 end if;
10649 Next (Decl);
10650 end loop;
10651 end Collect_Previous_Instances;
10653 -- Start of processing for Load_Parent_Of_Generic
10655 begin
10656 if not In_Same_Source_Unit (N, Spec)
10657 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
10658 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
10659 and then not Is_In_Main_Unit (Spec))
10660 then
10661 -- Find body of parent of spec, and analyze it. A special case arises
10662 -- when the parent is an instantiation, that is to say when we are
10663 -- currently instantiating a nested generic. In that case, there is
10664 -- no separate file for the body of the enclosing instance. Instead,
10665 -- the enclosing body must be instantiated as if it were a pending
10666 -- instantiation, in order to produce the body for the nested generic
10667 -- we require now. Note that in that case the generic may be defined
10668 -- in a package body, the instance defined in the same package body,
10669 -- and the original enclosing body may not be in the main unit.
10671 Inst_Node := Empty;
10673 True_Parent := Parent (Spec);
10674 while Present (True_Parent)
10675 and then Nkind (True_Parent) /= N_Compilation_Unit
10676 loop
10677 if Nkind (True_Parent) = N_Package_Declaration
10678 and then
10679 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
10680 then
10681 -- Parent is a compilation unit that is an instantiation.
10682 -- Instantiation node has been replaced with package decl.
10684 Inst_Node := Original_Node (True_Parent);
10685 exit;
10687 elsif Nkind (True_Parent) = N_Package_Declaration
10688 and then Present (Generic_Parent (Specification (True_Parent)))
10689 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
10690 then
10691 -- Parent is an instantiation within another specification.
10692 -- Declaration for instance has been inserted before original
10693 -- instantiation node. A direct link would be preferable?
10695 Inst_Node := Next (True_Parent);
10696 while Present (Inst_Node)
10697 and then Nkind (Inst_Node) /= N_Package_Instantiation
10698 loop
10699 Next (Inst_Node);
10700 end loop;
10702 -- If the instance appears within a generic, and the generic
10703 -- unit is defined within a formal package of the enclosing
10704 -- generic, there is no generic body available, and none
10705 -- needed. A more precise test should be used ???
10707 if No (Inst_Node) then
10708 return;
10709 end if;
10711 exit;
10713 else
10714 True_Parent := Parent (True_Parent);
10715 end if;
10716 end loop;
10718 -- Case where we are currently instantiating a nested generic
10720 if Present (Inst_Node) then
10721 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
10723 -- Instantiation node and declaration of instantiated package
10724 -- were exchanged when only the declaration was needed.
10725 -- Restore instantiation node before proceeding with body.
10727 Set_Unit (Parent (True_Parent), Inst_Node);
10728 end if;
10730 -- Now complete instantiation of enclosing body, if it appears in
10731 -- some other unit. If it appears in the current unit, the body
10732 -- will have been instantiated already.
10734 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
10736 -- We need to determine the expander mode to instantiate the
10737 -- enclosing body. Because the generic body we need may use
10738 -- global entities declared in the enclosing package (including
10739 -- aggregates) it is in general necessary to compile this body
10740 -- with expansion enabled, except if we are within a generic
10741 -- package, in which case the usual generic rule applies.
10743 declare
10744 Exp_Status : Boolean := True;
10745 Scop : Entity_Id;
10747 begin
10748 -- Loop through scopes looking for generic package
10750 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
10751 while Present (Scop)
10752 and then Scop /= Standard_Standard
10753 loop
10754 if Ekind (Scop) = E_Generic_Package then
10755 Exp_Status := False;
10756 exit;
10757 end if;
10759 Scop := Scope (Scop);
10760 end loop;
10762 -- Collect previous instantiations in the unit that contains
10763 -- the desired generic.
10765 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
10766 and then not Body_Optional
10767 then
10768 declare
10769 Decl : Elmt_Id;
10770 Info : Pending_Body_Info;
10771 Par : Node_Id;
10773 begin
10774 Par := Parent (Inst_Node);
10775 while Present (Par) loop
10776 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
10777 Par := Parent (Par);
10778 end loop;
10780 pragma Assert (Present (Par));
10782 if Nkind (Par) = N_Package_Body then
10783 Collect_Previous_Instances (Declarations (Par));
10785 elsif Nkind (Par) = N_Package_Declaration then
10786 Collect_Previous_Instances
10787 (Visible_Declarations (Specification (Par)));
10788 Collect_Previous_Instances
10789 (Private_Declarations (Specification (Par)));
10791 else
10792 -- Enclosing unit is a subprogram body. In this
10793 -- case all instance bodies are processed in order
10794 -- and there is no need to collect them separately.
10796 null;
10797 end if;
10799 Decl := First_Elmt (Previous_Instances);
10800 while Present (Decl) loop
10801 Info :=
10802 (Inst_Node => Node (Decl),
10803 Act_Decl =>
10804 Instance_Spec (Node (Decl)),
10805 Expander_Status => Exp_Status,
10806 Current_Sem_Unit =>
10807 Get_Code_Unit (Sloc (Node (Decl))),
10808 Scope_Suppress => Scope_Suppress,
10809 Local_Suppress_Stack_Top =>
10810 Local_Suppress_Stack_Top,
10811 Version => Ada_Version);
10813 -- Package instance
10816 Nkind (Node (Decl)) = N_Package_Instantiation
10817 then
10818 Instantiate_Package_Body
10819 (Info, Body_Optional => True);
10821 -- Subprogram instance
10823 else
10824 -- The instance_spec is the wrapper package,
10825 -- and the subprogram declaration is the last
10826 -- declaration in the wrapper.
10828 Info.Act_Decl :=
10829 Last
10830 (Visible_Declarations
10831 (Specification (Info.Act_Decl)));
10833 Instantiate_Subprogram_Body
10834 (Info, Body_Optional => True);
10835 end if;
10837 Next_Elmt (Decl);
10838 end loop;
10839 end;
10840 end if;
10842 Instantiate_Package_Body
10843 (Body_Info =>
10844 ((Inst_Node => Inst_Node,
10845 Act_Decl => True_Parent,
10846 Expander_Status => Exp_Status,
10847 Current_Sem_Unit =>
10848 Get_Code_Unit (Sloc (Inst_Node)),
10849 Scope_Suppress => Scope_Suppress,
10850 Local_Suppress_Stack_Top =>
10851 Local_Suppress_Stack_Top,
10852 Version => Ada_Version)),
10853 Body_Optional => Body_Optional);
10854 end;
10855 end if;
10857 -- Case where we are not instantiating a nested generic
10859 else
10860 Opt.Style_Check := False;
10861 Expander_Mode_Save_And_Set (True);
10862 Load_Needed_Body (Comp_Unit, OK);
10863 Opt.Style_Check := Save_Style_Check;
10864 Expander_Mode_Restore;
10866 if not OK
10867 and then Unit_Requires_Body (Defining_Entity (Spec))
10868 and then not Body_Optional
10869 then
10870 declare
10871 Bname : constant Unit_Name_Type :=
10872 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
10874 begin
10875 -- In CodePeer mode, the missing body may make the analysis
10876 -- incomplete, but we do not treat it as fatal.
10878 if CodePeer_Mode then
10879 return;
10881 else
10882 Error_Msg_Unit_1 := Bname;
10883 Error_Msg_N ("this instantiation requires$!", N);
10884 Error_Msg_File_1 :=
10885 Get_File_Name (Bname, Subunit => False);
10886 Error_Msg_N ("\but file{ was not found!", N);
10887 raise Unrecoverable_Error;
10888 end if;
10889 end;
10890 end if;
10891 end if;
10892 end if;
10894 -- If loading parent of the generic caused an instantiation circularity,
10895 -- we abandon compilation at this point, because otherwise in some cases
10896 -- we get into trouble with infinite recursions after this point.
10898 if Circularity_Detected then
10899 raise Unrecoverable_Error;
10900 end if;
10901 end Load_Parent_Of_Generic;
10903 ---------------------------------
10904 -- Map_Formal_Package_Entities --
10905 ---------------------------------
10907 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
10908 E1 : Entity_Id;
10909 E2 : Entity_Id;
10911 begin
10912 Set_Instance_Of (Form, Act);
10914 -- Traverse formal and actual package to map the corresponding entities.
10915 -- We skip over internal entities that may be generated during semantic
10916 -- analysis, and find the matching entities by name, given that they
10917 -- must appear in the same order.
10919 E1 := First_Entity (Form);
10920 E2 := First_Entity (Act);
10921 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
10922 -- Could this test be a single condition???
10923 -- Seems like it could, and isn't FPE (Form) a constant anyway???
10925 if not Is_Internal (E1)
10926 and then Present (Parent (E1))
10927 and then not Is_Class_Wide_Type (E1)
10928 and then not Is_Internal_Name (Chars (E1))
10929 then
10930 while Present (E2) and then Chars (E2) /= Chars (E1) loop
10931 Next_Entity (E2);
10932 end loop;
10934 if No (E2) then
10935 exit;
10936 else
10937 Set_Instance_Of (E1, E2);
10939 if Is_Type (E1) and then Is_Tagged_Type (E2) then
10940 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
10941 end if;
10943 if Is_Constrained (E1) then
10944 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
10945 end if;
10947 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
10948 Map_Formal_Package_Entities (E1, E2);
10949 end if;
10950 end if;
10951 end if;
10953 Next_Entity (E1);
10954 end loop;
10955 end Map_Formal_Package_Entities;
10957 -----------------------
10958 -- Move_Freeze_Nodes --
10959 -----------------------
10961 procedure Move_Freeze_Nodes
10962 (Out_Of : Entity_Id;
10963 After : Node_Id;
10964 L : List_Id)
10966 Decl : Node_Id;
10967 Next_Decl : Node_Id;
10968 Next_Node : Node_Id := After;
10969 Spec : Node_Id;
10971 function Is_Outer_Type (T : Entity_Id) return Boolean;
10972 -- Check whether entity is declared in a scope external to that of the
10973 -- generic unit.
10975 -------------------
10976 -- Is_Outer_Type --
10977 -------------------
10979 function Is_Outer_Type (T : Entity_Id) return Boolean is
10980 Scop : Entity_Id := Scope (T);
10982 begin
10983 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
10984 return True;
10986 else
10987 while Scop /= Standard_Standard loop
10988 if Scop = Out_Of then
10989 return False;
10990 else
10991 Scop := Scope (Scop);
10992 end if;
10993 end loop;
10995 return True;
10996 end if;
10997 end Is_Outer_Type;
10999 -- Start of processing for Move_Freeze_Nodes
11001 begin
11002 if No (L) then
11003 return;
11004 end if;
11006 -- First remove the freeze nodes that may appear before all other
11007 -- declarations.
11009 Decl := First (L);
11010 while Present (Decl)
11011 and then Nkind (Decl) = N_Freeze_Entity
11012 and then Is_Outer_Type (Entity (Decl))
11013 loop
11014 Decl := Remove_Head (L);
11015 Insert_After (Next_Node, Decl);
11016 Set_Analyzed (Decl, False);
11017 Next_Node := Decl;
11018 Decl := First (L);
11019 end loop;
11021 -- Next scan the list of declarations and remove each freeze node that
11022 -- appears ahead of the current node.
11024 while Present (Decl) loop
11025 while Present (Next (Decl))
11026 and then Nkind (Next (Decl)) = N_Freeze_Entity
11027 and then Is_Outer_Type (Entity (Next (Decl)))
11028 loop
11029 Next_Decl := Remove_Next (Decl);
11030 Insert_After (Next_Node, Next_Decl);
11031 Set_Analyzed (Next_Decl, False);
11032 Next_Node := Next_Decl;
11033 end loop;
11035 -- If the declaration is a nested package or concurrent type, then
11036 -- recurse. Nested generic packages will have been processed from the
11037 -- inside out.
11039 case Nkind (Decl) is
11040 when N_Package_Declaration =>
11041 Spec := Specification (Decl);
11043 when N_Task_Type_Declaration =>
11044 Spec := Task_Definition (Decl);
11046 when N_Protected_Type_Declaration =>
11047 Spec := Protected_Definition (Decl);
11049 when others =>
11050 Spec := Empty;
11051 end case;
11053 if Present (Spec) then
11054 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
11055 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
11056 end if;
11058 Next (Decl);
11059 end loop;
11060 end Move_Freeze_Nodes;
11062 ----------------
11063 -- Next_Assoc --
11064 ----------------
11066 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
11067 begin
11068 return Generic_Renamings.Table (E).Next_In_HTable;
11069 end Next_Assoc;
11071 ------------------------
11072 -- Preanalyze_Actuals --
11073 ------------------------
11075 procedure Preanalyze_Actuals (N : Node_Id) is
11076 Assoc : Node_Id;
11077 Act : Node_Id;
11078 Errs : constant Int := Serious_Errors_Detected;
11080 Cur : Entity_Id := Empty;
11081 -- Current homograph of the instance name
11083 Vis : Boolean;
11084 -- Saved visibility status of the current homograph
11086 begin
11087 Assoc := First (Generic_Associations (N));
11089 -- If the instance is a child unit, its name may hide an outer homonym,
11090 -- so make it invisible to perform name resolution on the actuals.
11092 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
11093 and then Present
11094 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
11095 then
11096 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
11098 if Is_Compilation_Unit (Cur) then
11099 Vis := Is_Immediately_Visible (Cur);
11100 Set_Is_Immediately_Visible (Cur, False);
11101 else
11102 Cur := Empty;
11103 end if;
11104 end if;
11106 while Present (Assoc) loop
11107 if Nkind (Assoc) /= N_Others_Choice then
11108 Act := Explicit_Generic_Actual_Parameter (Assoc);
11110 -- Within a nested instantiation, a defaulted actual is an empty
11111 -- association, so nothing to analyze. If the subprogram actual
11112 -- is an attribute, analyze prefix only, because actual is not a
11113 -- complete attribute reference.
11115 -- If actual is an allocator, analyze expression only. The full
11116 -- analysis can generate code, and if instance is a compilation
11117 -- unit we have to wait until the package instance is installed
11118 -- to have a proper place to insert this code.
11120 -- String literals may be operators, but at this point we do not
11121 -- know whether the actual is a formal subprogram or a string.
11123 if No (Act) then
11124 null;
11126 elsif Nkind (Act) = N_Attribute_Reference then
11127 Analyze (Prefix (Act));
11129 elsif Nkind (Act) = N_Explicit_Dereference then
11130 Analyze (Prefix (Act));
11132 elsif Nkind (Act) = N_Allocator then
11133 declare
11134 Expr : constant Node_Id := Expression (Act);
11136 begin
11137 if Nkind (Expr) = N_Subtype_Indication then
11138 Analyze (Subtype_Mark (Expr));
11140 -- Analyze separately each discriminant constraint, when
11141 -- given with a named association.
11143 declare
11144 Constr : Node_Id;
11146 begin
11147 Constr := First (Constraints (Constraint (Expr)));
11148 while Present (Constr) loop
11149 if Nkind (Constr) = N_Discriminant_Association then
11150 Analyze (Expression (Constr));
11151 else
11152 Analyze (Constr);
11153 end if;
11155 Next (Constr);
11156 end loop;
11157 end;
11159 else
11160 Analyze (Expr);
11161 end if;
11162 end;
11164 elsif Nkind (Act) /= N_Operator_Symbol then
11165 Analyze (Act);
11166 end if;
11168 if Errs /= Serious_Errors_Detected then
11170 -- Do a minimal analysis of the generic, to prevent spurious
11171 -- warnings complaining about the generic being unreferenced,
11172 -- before abandoning the instantiation.
11174 Analyze (Name (N));
11176 if Is_Entity_Name (Name (N))
11177 and then Etype (Name (N)) /= Any_Type
11178 then
11179 Generate_Reference (Entity (Name (N)), Name (N));
11180 Set_Is_Instantiated (Entity (Name (N)));
11181 end if;
11183 if Present (Cur) then
11185 -- For the case of a child instance hiding an outer homonym,
11186 -- provide additional warning which might explain the error.
11188 Set_Is_Immediately_Visible (Cur, Vis);
11189 Error_Msg_NE ("& hides outer unit with the same name?",
11190 N, Defining_Unit_Name (N));
11191 end if;
11193 Abandon_Instantiation (Act);
11194 end if;
11195 end if;
11197 Next (Assoc);
11198 end loop;
11200 if Present (Cur) then
11201 Set_Is_Immediately_Visible (Cur, Vis);
11202 end if;
11203 end Preanalyze_Actuals;
11205 -------------------
11206 -- Remove_Parent --
11207 -------------------
11209 procedure Remove_Parent (In_Body : Boolean := False) is
11210 S : Entity_Id := Current_Scope;
11211 -- S is the scope containing the instantiation just completed. The scope
11212 -- stack contains the parent instances of the instantiation, followed by
11213 -- the original S.
11215 E : Entity_Id;
11216 P : Entity_Id;
11217 Hidden : Elmt_Id;
11219 begin
11220 -- After child instantiation is complete, remove from scope stack the
11221 -- extra copy of the current scope, and then remove parent instances.
11223 if not In_Body then
11224 Pop_Scope;
11226 while Current_Scope /= S loop
11227 P := Current_Scope;
11228 End_Package_Scope (Current_Scope);
11230 if In_Open_Scopes (P) then
11231 E := First_Entity (P);
11232 while Present (E) loop
11233 Set_Is_Immediately_Visible (E, True);
11234 Next_Entity (E);
11235 end loop;
11237 if Is_Generic_Instance (Current_Scope)
11238 and then P /= Current_Scope
11239 then
11240 -- We are within an instance of some sibling. Retain
11241 -- visibility of parent, for proper subsequent cleanup, and
11242 -- reinstall private declarations as well.
11244 Set_In_Private_Part (P);
11245 Install_Private_Declarations (P);
11246 end if;
11248 -- If the ultimate parent is a top-level unit recorded in
11249 -- Instance_Parent_Unit, then reset its visibility to what is was
11250 -- before instantiation. (It's not clear what the purpose is of
11251 -- testing whether Scope (P) is In_Open_Scopes, but that test was
11252 -- present before the ultimate parent test was added.???)
11254 elsif not In_Open_Scopes (Scope (P))
11255 or else (P = Instance_Parent_Unit
11256 and then not Parent_Unit_Visible)
11257 then
11258 Set_Is_Immediately_Visible (P, False);
11260 -- If the current scope is itself an instantiation of a generic
11261 -- nested within P, and we are in the private part of body of this
11262 -- instantiation, restore the full views of P, that were removed
11263 -- in End_Package_Scope above. This obscure case can occur when a
11264 -- subunit of a generic contains an instance of a child unit of
11265 -- its generic parent unit.
11267 elsif S = Current_Scope and then Is_Generic_Instance (S) then
11268 declare
11269 Par : constant Entity_Id :=
11270 Generic_Parent
11271 (Specification (Unit_Declaration_Node (S)));
11272 begin
11273 if Present (Par)
11274 and then P = Scope (Par)
11275 and then (In_Package_Body (S) or else In_Private_Part (S))
11276 then
11277 Set_In_Private_Part (P);
11278 Install_Private_Declarations (P);
11279 end if;
11280 end;
11281 end if;
11282 end loop;
11284 -- Reset visibility of entities in the enclosing scope
11286 Set_Is_Hidden_Open_Scope (Current_Scope, False);
11288 Hidden := First_Elmt (Hidden_Entities);
11289 while Present (Hidden) loop
11290 Set_Is_Immediately_Visible (Node (Hidden), True);
11291 Next_Elmt (Hidden);
11292 end loop;
11294 else
11295 -- Each body is analyzed separately, and there is no context that
11296 -- needs preserving from one body instance to the next, so remove all
11297 -- parent scopes that have been installed.
11299 while Present (S) loop
11300 End_Package_Scope (S);
11301 Set_Is_Immediately_Visible (S, False);
11302 S := Current_Scope;
11303 exit when S = Standard_Standard;
11304 end loop;
11305 end if;
11306 end Remove_Parent;
11308 -----------------
11309 -- Restore_Env --
11310 -----------------
11312 procedure Restore_Env is
11313 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
11315 begin
11316 if No (Current_Instantiated_Parent.Act_Id) then
11317 -- Restore environment after subprogram inlining
11319 Restore_Private_Views (Empty);
11320 end if;
11322 Current_Instantiated_Parent := Saved.Instantiated_Parent;
11323 Exchanged_Views := Saved.Exchanged_Views;
11324 Hidden_Entities := Saved.Hidden_Entities;
11325 Current_Sem_Unit := Saved.Current_Sem_Unit;
11326 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
11327 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
11329 Restore_Opt_Config_Switches (Saved.Switches);
11331 Instance_Envs.Decrement_Last;
11332 end Restore_Env;
11334 ---------------------------
11335 -- Restore_Private_Views --
11336 ---------------------------
11338 procedure Restore_Private_Views
11339 (Pack_Id : Entity_Id;
11340 Is_Package : Boolean := True)
11342 M : Elmt_Id;
11343 E : Entity_Id;
11344 Typ : Entity_Id;
11345 Dep_Elmt : Elmt_Id;
11346 Dep_Typ : Node_Id;
11348 procedure Restore_Nested_Formal (Formal : Entity_Id);
11349 -- Hide the generic formals of formal packages declared with box which
11350 -- were reachable in the current instantiation.
11352 ---------------------------
11353 -- Restore_Nested_Formal --
11354 ---------------------------
11356 procedure Restore_Nested_Formal (Formal : Entity_Id) is
11357 Ent : Entity_Id;
11359 begin
11360 if Present (Renamed_Object (Formal))
11361 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
11362 then
11363 return;
11365 elsif Present (Associated_Formal_Package (Formal)) then
11366 Ent := First_Entity (Formal);
11367 while Present (Ent) loop
11368 exit when Ekind (Ent) = E_Package
11369 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
11371 Set_Is_Hidden (Ent);
11372 Set_Is_Potentially_Use_Visible (Ent, False);
11374 -- If package, then recurse
11376 if Ekind (Ent) = E_Package then
11377 Restore_Nested_Formal (Ent);
11378 end if;
11380 Next_Entity (Ent);
11381 end loop;
11382 end if;
11383 end Restore_Nested_Formal;
11385 -- Start of processing for Restore_Private_Views
11387 begin
11388 M := First_Elmt (Exchanged_Views);
11389 while Present (M) loop
11390 Typ := Node (M);
11392 -- Subtypes of types whose views have been exchanged, and that
11393 -- are defined within the instance, were not on the list of
11394 -- Private_Dependents on entry to the instance, so they have to be
11395 -- exchanged explicitly now, in order to remain consistent with the
11396 -- view of the parent type.
11398 if Ekind_In (Typ, E_Private_Type,
11399 E_Limited_Private_Type,
11400 E_Record_Type_With_Private)
11401 then
11402 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
11403 while Present (Dep_Elmt) loop
11404 Dep_Typ := Node (Dep_Elmt);
11406 if Scope (Dep_Typ) = Pack_Id
11407 and then Present (Full_View (Dep_Typ))
11408 then
11409 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
11410 Exchange_Declarations (Dep_Typ);
11411 end if;
11413 Next_Elmt (Dep_Elmt);
11414 end loop;
11415 end if;
11417 Exchange_Declarations (Node (M));
11418 Next_Elmt (M);
11419 end loop;
11421 if No (Pack_Id) then
11422 return;
11423 end if;
11425 -- Make the generic formal parameters private, and make the formal types
11426 -- into subtypes of the actuals again.
11428 E := First_Entity (Pack_Id);
11429 while Present (E) loop
11430 Set_Is_Hidden (E, True);
11432 if Is_Type (E)
11433 and then Nkind (Parent (E)) = N_Subtype_Declaration
11434 then
11435 Set_Is_Generic_Actual_Type (E, False);
11437 -- An unusual case of aliasing: the actual may also be directly
11438 -- visible in the generic, and be private there, while it is fully
11439 -- visible in the context of the instance. The internal subtype
11440 -- is private in the instance, but has full visibility like its
11441 -- parent in the enclosing scope. This enforces the invariant that
11442 -- the privacy status of all private dependents of a type coincide
11443 -- with that of the parent type. This can only happen when a
11444 -- generic child unit is instantiated within sibling.
11446 if Is_Private_Type (E)
11447 and then not Is_Private_Type (Etype (E))
11448 then
11449 Exchange_Declarations (E);
11450 end if;
11452 elsif Ekind (E) = E_Package then
11454 -- The end of the renaming list is the renaming of the generic
11455 -- package itself. If the instance is a subprogram, all entities
11456 -- in the corresponding package are renamings. If this entity is
11457 -- a formal package, make its own formals private as well. The
11458 -- actual in this case is itself the renaming of an instantiation.
11459 -- If the entity is not a package renaming, it is the entity
11460 -- created to validate formal package actuals: ignore.
11462 -- If the actual is itself a formal package for the enclosing
11463 -- generic, or the actual for such a formal package, it remains
11464 -- visible on exit from the instance, and therefore nothing needs
11465 -- to be done either, except to keep it accessible.
11467 if Is_Package
11468 and then Renamed_Object (E) = Pack_Id
11469 then
11470 exit;
11472 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
11473 null;
11475 elsif
11476 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
11477 then
11478 Set_Is_Hidden (E, False);
11480 else
11481 declare
11482 Act_P : constant Entity_Id := Renamed_Object (E);
11483 Id : Entity_Id;
11485 begin
11486 Id := First_Entity (Act_P);
11487 while Present (Id)
11488 and then Id /= First_Private_Entity (Act_P)
11489 loop
11490 exit when Ekind (Id) = E_Package
11491 and then Renamed_Object (Id) = Act_P;
11493 Set_Is_Hidden (Id, True);
11494 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
11496 if Ekind (Id) = E_Package then
11497 Restore_Nested_Formal (Id);
11498 end if;
11500 Next_Entity (Id);
11501 end loop;
11502 end;
11503 end if;
11504 end if;
11506 Next_Entity (E);
11507 end loop;
11508 end Restore_Private_Views;
11510 --------------
11511 -- Save_Env --
11512 --------------
11514 procedure Save_Env
11515 (Gen_Unit : Entity_Id;
11516 Act_Unit : Entity_Id)
11518 begin
11519 Init_Env;
11520 Set_Instance_Env (Gen_Unit, Act_Unit);
11521 end Save_Env;
11523 ----------------------------
11524 -- Save_Global_References --
11525 ----------------------------
11527 procedure Save_Global_References (N : Node_Id) is
11528 Gen_Scope : Entity_Id;
11529 E : Entity_Id;
11530 N2 : Node_Id;
11532 function Is_Global (E : Entity_Id) return Boolean;
11533 -- Check whether entity is defined outside of generic unit. Examine the
11534 -- scope of an entity, and the scope of the scope, etc, until we find
11535 -- either Standard, in which case the entity is global, or the generic
11536 -- unit itself, which indicates that the entity is local. If the entity
11537 -- is the generic unit itself, as in the case of a recursive call, or
11538 -- the enclosing generic unit, if different from the current scope, then
11539 -- it is local as well, because it will be replaced at the point of
11540 -- instantiation. On the other hand, if it is a reference to a child
11541 -- unit of a common ancestor, which appears in an instantiation, it is
11542 -- global because it is used to denote a specific compilation unit at
11543 -- the time the instantiations will be analyzed.
11545 procedure Reset_Entity (N : Node_Id);
11546 -- Save semantic information on global entity so that it is not resolved
11547 -- again at instantiation time.
11549 procedure Save_Entity_Descendants (N : Node_Id);
11550 -- Apply Save_Global_References to the two syntactic descendants of
11551 -- non-terminal nodes that carry an Associated_Node and are processed
11552 -- through Reset_Entity. Once the global entity (if any) has been
11553 -- captured together with its type, only two syntactic descendants need
11554 -- to be traversed to complete the processing of the tree rooted at N.
11555 -- This applies to Selected_Components, Expanded_Names, and to Operator
11556 -- nodes. N can also be a character literal, identifier, or operator
11557 -- symbol node, but the call has no effect in these cases.
11559 procedure Save_Global_Defaults (N1, N2 : Node_Id);
11560 -- Default actuals in nested instances must be handled specially
11561 -- because there is no link to them from the original tree. When an
11562 -- actual subprogram is given by a default, we add an explicit generic
11563 -- association for it in the instantiation node. When we save the
11564 -- global references on the name of the instance, we recover the list
11565 -- of generic associations, and add an explicit one to the original
11566 -- generic tree, through which a global actual can be preserved.
11567 -- Similarly, if a child unit is instantiated within a sibling, in the
11568 -- context of the parent, we must preserve the identifier of the parent
11569 -- so that it can be properly resolved in a subsequent instantiation.
11571 procedure Save_Global_Descendant (D : Union_Id);
11572 -- Apply Save_Global_References recursively to the descendents of the
11573 -- current node.
11575 procedure Save_References (N : Node_Id);
11576 -- This is the recursive procedure that does the work, once the
11577 -- enclosing generic scope has been established.
11579 ---------------
11580 -- Is_Global --
11581 ---------------
11583 function Is_Global (E : Entity_Id) return Boolean is
11584 Se : Entity_Id;
11586 function Is_Instance_Node (Decl : Node_Id) return Boolean;
11587 -- Determine whether the parent node of a reference to a child unit
11588 -- denotes an instantiation or a formal package, in which case the
11589 -- reference to the child unit is global, even if it appears within
11590 -- the current scope (e.g. when the instance appears within the body
11591 -- of an ancestor).
11593 ----------------------
11594 -- Is_Instance_Node --
11595 ----------------------
11597 function Is_Instance_Node (Decl : Node_Id) return Boolean is
11598 begin
11599 return Nkind (Decl) in N_Generic_Instantiation
11600 or else
11601 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
11602 end Is_Instance_Node;
11604 -- Start of processing for Is_Global
11606 begin
11607 if E = Gen_Scope then
11608 return False;
11610 elsif E = Standard_Standard then
11611 return True;
11613 elsif Is_Child_Unit (E)
11614 and then (Is_Instance_Node (Parent (N2))
11615 or else (Nkind (Parent (N2)) = N_Expanded_Name
11616 and then N2 = Selector_Name (Parent (N2))
11617 and then
11618 Is_Instance_Node (Parent (Parent (N2)))))
11619 then
11620 return True;
11622 else
11623 Se := Scope (E);
11624 while Se /= Gen_Scope loop
11625 if Se = Standard_Standard then
11626 return True;
11627 else
11628 Se := Scope (Se);
11629 end if;
11630 end loop;
11632 return False;
11633 end if;
11634 end Is_Global;
11636 ------------------
11637 -- Reset_Entity --
11638 ------------------
11640 procedure Reset_Entity (N : Node_Id) is
11642 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
11643 -- If the type of N2 is global to the generic unit. Save the type in
11644 -- the generic node.
11645 -- What does this comment mean???
11647 function Top_Ancestor (E : Entity_Id) return Entity_Id;
11648 -- Find the ultimate ancestor of the current unit. If it is not a
11649 -- generic unit, then the name of the current unit in the prefix of
11650 -- an expanded name must be replaced with its generic homonym to
11651 -- ensure that it will be properly resolved in an instance.
11653 ---------------------
11654 -- Set_Global_Type --
11655 ---------------------
11657 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
11658 Typ : constant Entity_Id := Etype (N2);
11660 begin
11661 Set_Etype (N, Typ);
11663 if Entity (N) /= N2
11664 and then Has_Private_View (Entity (N))
11665 then
11666 -- If the entity of N is not the associated node, this is a
11667 -- nested generic and it has an associated node as well, whose
11668 -- type is already the full view (see below). Indicate that the
11669 -- original node has a private view.
11671 Set_Has_Private_View (N);
11672 end if;
11674 -- If not a private type, nothing else to do
11676 if not Is_Private_Type (Typ) then
11677 if Is_Array_Type (Typ)
11678 and then Is_Private_Type (Component_Type (Typ))
11679 then
11680 Set_Has_Private_View (N);
11681 end if;
11683 -- If it is a derivation of a private type in a context where no
11684 -- full view is needed, nothing to do either.
11686 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
11687 null;
11689 -- Otherwise mark the type for flipping and use the full view when
11690 -- available.
11692 else
11693 Set_Has_Private_View (N);
11695 if Present (Full_View (Typ)) then
11696 Set_Etype (N2, Full_View (Typ));
11697 end if;
11698 end if;
11699 end Set_Global_Type;
11701 ------------------
11702 -- Top_Ancestor --
11703 ------------------
11705 function Top_Ancestor (E : Entity_Id) return Entity_Id is
11706 Par : Entity_Id;
11708 begin
11709 Par := E;
11710 while Is_Child_Unit (Par) loop
11711 Par := Scope (Par);
11712 end loop;
11714 return Par;
11715 end Top_Ancestor;
11717 -- Start of processing for Reset_Entity
11719 begin
11720 N2 := Get_Associated_Node (N);
11721 E := Entity (N2);
11723 if Present (E) then
11724 if Is_Global (E) then
11725 Set_Global_Type (N, N2);
11727 elsif Nkind (N) = N_Op_Concat
11728 and then Is_Generic_Type (Etype (N2))
11729 and then
11730 (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
11731 or else Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
11732 and then Is_Intrinsic_Subprogram (E)
11733 then
11734 null;
11736 else
11737 -- Entity is local. Mark generic node as unresolved.
11738 -- Note that now it does not have an entity.
11740 Set_Associated_Node (N, Empty);
11741 Set_Etype (N, Empty);
11742 end if;
11744 if Nkind (Parent (N)) in N_Generic_Instantiation
11745 and then N = Name (Parent (N))
11746 then
11747 Save_Global_Defaults (Parent (N), Parent (N2));
11748 end if;
11750 elsif Nkind (Parent (N)) = N_Selected_Component
11751 and then Nkind (Parent (N2)) = N_Expanded_Name
11752 then
11753 if Is_Global (Entity (Parent (N2))) then
11754 Change_Selected_Component_To_Expanded_Name (Parent (N));
11755 Set_Associated_Node (Parent (N), Parent (N2));
11756 Set_Global_Type (Parent (N), Parent (N2));
11757 Save_Entity_Descendants (N);
11759 -- If this is a reference to the current generic entity, replace
11760 -- by the name of the generic homonym of the current package. This
11761 -- is because in an instantiation Par.P.Q will not resolve to the
11762 -- name of the instance, whose enclosing scope is not necessarily
11763 -- Par. We use the generic homonym rather that the name of the
11764 -- generic itself because it may be hidden by a local declaration.
11766 elsif In_Open_Scopes (Entity (Parent (N2)))
11767 and then not
11768 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
11769 then
11770 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
11771 Rewrite (Parent (N),
11772 Make_Identifier (Sloc (N),
11773 Chars =>
11774 Chars (Generic_Homonym (Entity (Parent (N2))))));
11775 else
11776 Rewrite (Parent (N),
11777 Make_Identifier (Sloc (N),
11778 Chars => Chars (Selector_Name (Parent (N2)))));
11779 end if;
11780 end if;
11782 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
11783 and then Parent (N) = Name (Parent (Parent (N)))
11784 then
11785 Save_Global_Defaults
11786 (Parent (Parent (N)), Parent (Parent ((N2))));
11787 end if;
11789 -- A selected component may denote a static constant that has been
11790 -- folded. If the static constant is global to the generic, capture
11791 -- its value. Otherwise the folding will happen in any instantiation.
11793 elsif Nkind (Parent (N)) = N_Selected_Component
11794 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
11795 then
11796 if Present (Entity (Original_Node (Parent (N2))))
11797 and then Is_Global (Entity (Original_Node (Parent (N2))))
11798 then
11799 Rewrite (Parent (N), New_Copy (Parent (N2)));
11800 Set_Analyzed (Parent (N), False);
11802 else
11803 null;
11804 end if;
11806 -- A selected component may be transformed into a parameterless
11807 -- function call. If the called entity is global, rewrite the node
11808 -- appropriately, i.e. as an extended name for the global entity.
11810 elsif Nkind (Parent (N)) = N_Selected_Component
11811 and then Nkind (Parent (N2)) = N_Function_Call
11812 and then N = Selector_Name (Parent (N))
11813 then
11814 if No (Parameter_Associations (Parent (N2))) then
11815 if Is_Global (Entity (Name (Parent (N2)))) then
11816 Change_Selected_Component_To_Expanded_Name (Parent (N));
11817 Set_Associated_Node (Parent (N), Name (Parent (N2)));
11818 Set_Global_Type (Parent (N), Name (Parent (N2)));
11819 Save_Entity_Descendants (N);
11821 else
11822 Set_Associated_Node (N, Empty);
11823 Set_Etype (N, Empty);
11824 end if;
11826 -- In Ada 2005, X.F may be a call to a primitive operation,
11827 -- rewritten as F (X). This rewriting will be done again in an
11828 -- instance, so keep the original node. Global entities will be
11829 -- captured as for other constructs.
11831 else
11832 null;
11833 end if;
11835 -- Entity is local. Reset in generic unit, so that node is resolved
11836 -- anew at the point of instantiation.
11838 else
11839 Set_Associated_Node (N, Empty);
11840 Set_Etype (N, Empty);
11841 end if;
11842 end Reset_Entity;
11844 -----------------------------
11845 -- Save_Entity_Descendants --
11846 -----------------------------
11848 procedure Save_Entity_Descendants (N : Node_Id) is
11849 begin
11850 case Nkind (N) is
11851 when N_Binary_Op =>
11852 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
11853 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
11855 when N_Unary_Op =>
11856 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
11858 when N_Expanded_Name | N_Selected_Component =>
11859 Save_Global_Descendant (Union_Id (Prefix (N)));
11860 Save_Global_Descendant (Union_Id (Selector_Name (N)));
11862 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
11863 null;
11865 when others =>
11866 raise Program_Error;
11867 end case;
11868 end Save_Entity_Descendants;
11870 --------------------------
11871 -- Save_Global_Defaults --
11872 --------------------------
11874 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
11875 Loc : constant Source_Ptr := Sloc (N1);
11876 Assoc2 : constant List_Id := Generic_Associations (N2);
11877 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
11878 Assoc1 : List_Id;
11879 Act1 : Node_Id;
11880 Act2 : Node_Id;
11881 Def : Node_Id;
11882 Ndec : Node_Id;
11883 Subp : Entity_Id;
11884 Actual : Entity_Id;
11886 begin
11887 Assoc1 := Generic_Associations (N1);
11889 if Present (Assoc1) then
11890 Act1 := First (Assoc1);
11891 else
11892 Act1 := Empty;
11893 Set_Generic_Associations (N1, New_List);
11894 Assoc1 := Generic_Associations (N1);
11895 end if;
11897 if Present (Assoc2) then
11898 Act2 := First (Assoc2);
11899 else
11900 return;
11901 end if;
11903 while Present (Act1) and then Present (Act2) loop
11904 Next (Act1);
11905 Next (Act2);
11906 end loop;
11908 -- Find the associations added for default subprograms
11910 if Present (Act2) then
11911 while Nkind (Act2) /= N_Generic_Association
11912 or else No (Entity (Selector_Name (Act2)))
11913 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
11914 loop
11915 Next (Act2);
11916 end loop;
11918 -- Add a similar association if the default is global. The
11919 -- renaming declaration for the actual has been analyzed, and
11920 -- its alias is the program it renames. Link the actual in the
11921 -- original generic tree with the node in the analyzed tree.
11923 while Present (Act2) loop
11924 Subp := Entity (Selector_Name (Act2));
11925 Def := Explicit_Generic_Actual_Parameter (Act2);
11927 -- Following test is defence against rubbish errors
11929 if No (Alias (Subp)) then
11930 return;
11931 end if;
11933 -- Retrieve the resolved actual from the renaming declaration
11934 -- created for the instantiated formal.
11936 Actual := Entity (Name (Parent (Parent (Subp))));
11937 Set_Entity (Def, Actual);
11938 Set_Etype (Def, Etype (Actual));
11940 if Is_Global (Actual) then
11941 Ndec :=
11942 Make_Generic_Association (Loc,
11943 Selector_Name => New_Occurrence_Of (Subp, Loc),
11944 Explicit_Generic_Actual_Parameter =>
11945 New_Occurrence_Of (Actual, Loc));
11947 Set_Associated_Node
11948 (Explicit_Generic_Actual_Parameter (Ndec), Def);
11950 Append (Ndec, Assoc1);
11952 -- If there are other defaults, add a dummy association in case
11953 -- there are other defaulted formals with the same name.
11955 elsif Present (Next (Act2)) then
11956 Ndec :=
11957 Make_Generic_Association (Loc,
11958 Selector_Name => New_Occurrence_Of (Subp, Loc),
11959 Explicit_Generic_Actual_Parameter => Empty);
11961 Append (Ndec, Assoc1);
11962 end if;
11964 Next (Act2);
11965 end loop;
11966 end if;
11968 if Nkind (Name (N1)) = N_Identifier
11969 and then Is_Child_Unit (Gen_Id)
11970 and then Is_Global (Gen_Id)
11971 and then Is_Generic_Unit (Scope (Gen_Id))
11972 and then In_Open_Scopes (Scope (Gen_Id))
11973 then
11974 -- This is an instantiation of a child unit within a sibling,
11975 -- so that the generic parent is in scope. An eventual instance
11976 -- must occur within the scope of an instance of the parent.
11977 -- Make name in instance into an expanded name, to preserve the
11978 -- identifier of the parent, so it can be resolved subsequently.
11980 Rewrite (Name (N2),
11981 Make_Expanded_Name (Loc,
11982 Chars => Chars (Gen_Id),
11983 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
11984 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
11985 Set_Entity (Name (N2), Gen_Id);
11987 Rewrite (Name (N1),
11988 Make_Expanded_Name (Loc,
11989 Chars => Chars (Gen_Id),
11990 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
11991 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
11993 Set_Associated_Node (Name (N1), Name (N2));
11994 Set_Associated_Node (Prefix (Name (N1)), Empty);
11995 Set_Associated_Node
11996 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
11997 Set_Etype (Name (N1), Etype (Gen_Id));
11998 end if;
12000 end Save_Global_Defaults;
12002 ----------------------------
12003 -- Save_Global_Descendant --
12004 ----------------------------
12006 procedure Save_Global_Descendant (D : Union_Id) is
12007 N1 : Node_Id;
12009 begin
12010 if D in Node_Range then
12011 if D = Union_Id (Empty) then
12012 null;
12014 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
12015 Save_References (Node_Id (D));
12016 end if;
12018 elsif D in List_Range then
12019 if D = Union_Id (No_List)
12020 or else Is_Empty_List (List_Id (D))
12021 then
12022 null;
12024 else
12025 N1 := First (List_Id (D));
12026 while Present (N1) loop
12027 Save_References (N1);
12028 Next (N1);
12029 end loop;
12030 end if;
12032 -- Element list or other non-node field, nothing to do
12034 else
12035 null;
12036 end if;
12037 end Save_Global_Descendant;
12039 ---------------------
12040 -- Save_References --
12041 ---------------------
12043 -- This is the recursive procedure that does the work once the enclosing
12044 -- generic scope has been established. We have to treat specially a
12045 -- number of node rewritings that are required by semantic processing
12046 -- and which change the kind of nodes in the generic copy: typically
12047 -- constant-folding, replacing an operator node by a string literal, or
12048 -- a selected component by an expanded name. In each of those cases, the
12049 -- transformation is propagated to the generic unit.
12051 procedure Save_References (N : Node_Id) is
12052 Loc : constant Source_Ptr := Sloc (N);
12054 begin
12055 if N = Empty then
12056 null;
12058 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
12059 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
12060 Reset_Entity (N);
12062 elsif Nkind (N) = N_Operator_Symbol
12063 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
12064 then
12065 Change_Operator_Symbol_To_String_Literal (N);
12066 end if;
12068 elsif Nkind (N) in N_Op then
12069 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
12070 if Nkind (N) = N_Op_Concat then
12071 Set_Is_Component_Left_Opnd (N,
12072 Is_Component_Left_Opnd (Get_Associated_Node (N)));
12074 Set_Is_Component_Right_Opnd (N,
12075 Is_Component_Right_Opnd (Get_Associated_Node (N)));
12076 end if;
12078 Reset_Entity (N);
12080 else
12081 -- Node may be transformed into call to a user-defined operator
12083 N2 := Get_Associated_Node (N);
12085 if Nkind (N2) = N_Function_Call then
12086 E := Entity (Name (N2));
12088 if Present (E)
12089 and then Is_Global (E)
12090 then
12091 Set_Etype (N, Etype (N2));
12092 else
12093 Set_Associated_Node (N, Empty);
12094 Set_Etype (N, Empty);
12095 end if;
12097 elsif Nkind_In (N2, N_Integer_Literal,
12098 N_Real_Literal,
12099 N_String_Literal)
12100 then
12101 if Present (Original_Node (N2))
12102 and then Nkind (Original_Node (N2)) = Nkind (N)
12103 then
12105 -- Operation was constant-folded. Whenever possible,
12106 -- recover semantic information from unfolded node,
12107 -- for ASIS use.
12109 Set_Associated_Node (N, Original_Node (N2));
12111 if Nkind (N) = N_Op_Concat then
12112 Set_Is_Component_Left_Opnd (N,
12113 Is_Component_Left_Opnd (Get_Associated_Node (N)));
12114 Set_Is_Component_Right_Opnd (N,
12115 Is_Component_Right_Opnd (Get_Associated_Node (N)));
12116 end if;
12118 Reset_Entity (N);
12120 else
12121 -- If original node is already modified, propagate
12122 -- constant-folding to template.
12124 Rewrite (N, New_Copy (N2));
12125 Set_Analyzed (N, False);
12126 end if;
12128 elsif Nkind (N2) = N_Identifier
12129 and then Ekind (Entity (N2)) = E_Enumeration_Literal
12130 then
12131 -- Same if call was folded into a literal, but in this case
12132 -- retain the entity to avoid spurious ambiguities if it is
12133 -- overloaded at the point of instantiation or inlining.
12135 Rewrite (N, New_Copy (N2));
12136 Set_Analyzed (N, False);
12137 end if;
12138 end if;
12140 -- Complete operands check if node has not been constant-folded
12142 if Nkind (N) in N_Op then
12143 Save_Entity_Descendants (N);
12144 end if;
12146 elsif Nkind (N) = N_Identifier then
12147 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
12149 -- If this is a discriminant reference, always save it. It is
12150 -- used in the instance to find the corresponding discriminant
12151 -- positionally rather than by name.
12153 Set_Original_Discriminant
12154 (N, Original_Discriminant (Get_Associated_Node (N)));
12155 Reset_Entity (N);
12157 else
12158 N2 := Get_Associated_Node (N);
12160 if Nkind (N2) = N_Function_Call then
12161 E := Entity (Name (N2));
12163 -- Name resolves to a call to parameterless function. If
12164 -- original entity is global, mark node as resolved.
12166 if Present (E)
12167 and then Is_Global (E)
12168 then
12169 Set_Etype (N, Etype (N2));
12170 else
12171 Set_Associated_Node (N, Empty);
12172 Set_Etype (N, Empty);
12173 end if;
12175 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
12176 and then Is_Entity_Name (Original_Node (N2))
12177 then
12178 -- Name resolves to named number that is constant-folded,
12179 -- We must preserve the original name for ASIS use, and
12180 -- undo the constant-folding, which will be repeated in
12181 -- each instance.
12183 Set_Associated_Node (N, Original_Node (N2));
12184 Reset_Entity (N);
12186 elsif Nkind (N2) = N_String_Literal then
12188 -- Name resolves to string literal. Perform the same
12189 -- replacement in generic.
12191 Rewrite (N, New_Copy (N2));
12193 elsif Nkind (N2) = N_Explicit_Dereference then
12195 -- An identifier is rewritten as a dereference if it is the
12196 -- prefix in an implicit dereference (call or attribute).
12197 -- The analysis of an instantiation will expand the node
12198 -- again, so we preserve the original tree but link it to
12199 -- the resolved entity in case it is global.
12201 if Is_Entity_Name (Prefix (N2))
12202 and then Present (Entity (Prefix (N2)))
12203 and then Is_Global (Entity (Prefix (N2)))
12204 then
12205 Set_Associated_Node (N, Prefix (N2));
12207 elsif Nkind (Prefix (N2)) = N_Function_Call
12208 and then Is_Global (Entity (Name (Prefix (N2))))
12209 then
12210 Rewrite (N,
12211 Make_Explicit_Dereference (Loc,
12212 Prefix => Make_Function_Call (Loc,
12213 Name =>
12214 New_Occurrence_Of (Entity (Name (Prefix (N2))),
12215 Loc))));
12217 else
12218 Set_Associated_Node (N, Empty);
12219 Set_Etype (N, Empty);
12220 end if;
12222 -- The subtype mark of a nominally unconstrained object is
12223 -- rewritten as a subtype indication using the bounds of the
12224 -- expression. Recover the original subtype mark.
12226 elsif Nkind (N2) = N_Subtype_Indication
12227 and then Is_Entity_Name (Original_Node (N2))
12228 then
12229 Set_Associated_Node (N, Original_Node (N2));
12230 Reset_Entity (N);
12232 else
12233 null;
12234 end if;
12235 end if;
12237 elsif Nkind (N) in N_Entity then
12238 null;
12240 else
12241 declare
12242 Qual : Node_Id := Empty;
12243 Typ : Entity_Id := Empty;
12244 Nam : Node_Id;
12246 use Atree.Unchecked_Access;
12247 -- This code section is part of implementing an untyped tree
12248 -- traversal, so it needs direct access to node fields.
12250 begin
12251 if Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
12252 N2 := Get_Associated_Node (N);
12254 if No (N2) then
12255 Typ := Empty;
12256 else
12257 Typ := Etype (N2);
12259 -- In an instance within a generic, use the name of the
12260 -- actual and not the original generic parameter. If the
12261 -- actual is global in the current generic it must be
12262 -- preserved for its instantiation.
12264 if Nkind (Parent (Typ)) = N_Subtype_Declaration
12265 and then
12266 Present (Generic_Parent_Type (Parent (Typ)))
12267 then
12268 Typ := Base_Type (Typ);
12269 Set_Etype (N2, Typ);
12270 end if;
12271 end if;
12273 if No (N2)
12274 or else No (Typ)
12275 or else not Is_Global (Typ)
12276 then
12277 Set_Associated_Node (N, Empty);
12279 -- If the aggregate is an actual in a call, it has been
12280 -- resolved in the current context, to some local type.
12281 -- The enclosing call may have been disambiguated by the
12282 -- aggregate, and this disambiguation might fail at
12283 -- instantiation time because the type to which the
12284 -- aggregate did resolve is not preserved. In order to
12285 -- preserve some of this information, we wrap the
12286 -- aggregate in a qualified expression, using the id of
12287 -- its type. For further disambiguation we qualify the
12288 -- type name with its scope (if visible) because both
12289 -- id's will have corresponding entities in an instance.
12290 -- This resolves most of the problems with missing type
12291 -- information on aggregates in instances.
12293 if Nkind (N2) = Nkind (N)
12294 and then
12295 Nkind_In (Parent (N2), N_Procedure_Call_Statement,
12296 N_Function_Call)
12297 and then Comes_From_Source (Typ)
12298 then
12299 if Is_Immediately_Visible (Scope (Typ)) then
12300 Nam := Make_Selected_Component (Loc,
12301 Prefix =>
12302 Make_Identifier (Loc, Chars (Scope (Typ))),
12303 Selector_Name =>
12304 Make_Identifier (Loc, Chars (Typ)));
12305 else
12306 Nam := Make_Identifier (Loc, Chars (Typ));
12307 end if;
12309 Qual :=
12310 Make_Qualified_Expression (Loc,
12311 Subtype_Mark => Nam,
12312 Expression => Relocate_Node (N));
12313 end if;
12314 end if;
12316 Save_Global_Descendant (Field1 (N));
12317 Save_Global_Descendant (Field2 (N));
12318 Save_Global_Descendant (Field3 (N));
12319 Save_Global_Descendant (Field5 (N));
12321 if Present (Qual) then
12322 Rewrite (N, Qual);
12323 end if;
12325 -- All other cases than aggregates
12327 else
12328 -- For pragmas, we propagate the Enabled status for the
12329 -- relevant pragmas to the original generic tree. This was
12330 -- originally needed for SCO generation. It is no longer
12331 -- needed there (since we use the Sloc value in calls to
12332 -- Set_SCO_Pragma_Enabled), but it seems a generally good
12333 -- idea to have this flag set properly.
12335 if Nkind (N) = N_Pragma
12336 and then
12337 (Pragma_Name (N) = Name_Assert or else
12338 Pragma_Name (N) = Name_Check or else
12339 Pragma_Name (N) = Name_Precondition or else
12340 Pragma_Name (N) = Name_Postcondition)
12341 and then Present (Associated_Node (Pragma_Identifier (N)))
12342 then
12343 Set_Pragma_Enabled (N,
12344 Pragma_Enabled
12345 (Parent (Associated_Node (Pragma_Identifier (N)))));
12346 end if;
12348 Save_Global_Descendant (Field1 (N));
12349 Save_Global_Descendant (Field2 (N));
12350 Save_Global_Descendant (Field3 (N));
12351 Save_Global_Descendant (Field4 (N));
12352 Save_Global_Descendant (Field5 (N));
12353 end if;
12354 end;
12355 end if;
12356 end Save_References;
12358 -- Start of processing for Save_Global_References
12360 begin
12361 Gen_Scope := Current_Scope;
12363 -- If the generic unit is a child unit, references to entities in the
12364 -- parent are treated as local, because they will be resolved anew in
12365 -- the context of the instance of the parent.
12367 while Is_Child_Unit (Gen_Scope)
12368 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
12369 loop
12370 Gen_Scope := Scope (Gen_Scope);
12371 end loop;
12373 Save_References (N);
12374 end Save_Global_References;
12376 --------------------------------------
12377 -- Set_Copied_Sloc_For_Inlined_Body --
12378 --------------------------------------
12380 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
12381 begin
12382 Create_Instantiation_Source (N, E, True, S_Adjustment);
12383 end Set_Copied_Sloc_For_Inlined_Body;
12385 ---------------------
12386 -- Set_Instance_Of --
12387 ---------------------
12389 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
12390 begin
12391 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
12392 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
12393 Generic_Renamings.Increment_Last;
12394 end Set_Instance_Of;
12396 --------------------
12397 -- Set_Next_Assoc --
12398 --------------------
12400 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
12401 begin
12402 Generic_Renamings.Table (E).Next_In_HTable := Next;
12403 end Set_Next_Assoc;
12405 -------------------
12406 -- Start_Generic --
12407 -------------------
12409 procedure Start_Generic is
12410 begin
12411 -- ??? More things could be factored out in this routine.
12412 -- Should probably be done at a later stage.
12414 Generic_Flags.Append (Inside_A_Generic);
12415 Inside_A_Generic := True;
12417 Expander_Mode_Save_And_Set (False);
12418 end Start_Generic;
12420 ----------------------
12421 -- Set_Instance_Env --
12422 ----------------------
12424 procedure Set_Instance_Env
12425 (Gen_Unit : Entity_Id;
12426 Act_Unit : Entity_Id)
12428 begin
12429 -- Regardless of the current mode, predefined units are analyzed in the
12430 -- most current Ada mode, and earlier version Ada checks do not apply
12431 -- to predefined units. Nothing needs to be done for non-internal units.
12432 -- These are always analyzed in the current mode.
12434 if Is_Internal_File_Name
12435 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
12436 Renamings_Included => True)
12437 then
12438 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
12439 end if;
12441 Current_Instantiated_Parent :=
12442 (Gen_Id => Gen_Unit,
12443 Act_Id => Act_Unit,
12444 Next_In_HTable => Assoc_Null);
12445 end Set_Instance_Env;
12447 -----------------
12448 -- Switch_View --
12449 -----------------
12451 procedure Switch_View (T : Entity_Id) is
12452 BT : constant Entity_Id := Base_Type (T);
12453 Priv_Elmt : Elmt_Id := No_Elmt;
12454 Priv_Sub : Entity_Id;
12456 begin
12457 -- T may be private but its base type may have been exchanged through
12458 -- some other occurrence, in which case there is nothing to switch
12459 -- besides T itself. Note that a private dependent subtype of a private
12460 -- type might not have been switched even if the base type has been,
12461 -- because of the last branch of Check_Private_View (see comment there).
12463 if not Is_Private_Type (BT) then
12464 Prepend_Elmt (Full_View (T), Exchanged_Views);
12465 Exchange_Declarations (T);
12466 return;
12467 end if;
12469 Priv_Elmt := First_Elmt (Private_Dependents (BT));
12471 if Present (Full_View (BT)) then
12472 Prepend_Elmt (Full_View (BT), Exchanged_Views);
12473 Exchange_Declarations (BT);
12474 end if;
12476 while Present (Priv_Elmt) loop
12477 Priv_Sub := (Node (Priv_Elmt));
12479 -- We avoid flipping the subtype if the Etype of its full view is
12480 -- private because this would result in a malformed subtype. This
12481 -- occurs when the Etype of the subtype full view is the full view of
12482 -- the base type (and since the base types were just switched, the
12483 -- subtype is pointing to the wrong view). This is currently the case
12484 -- for tagged record types, access types (maybe more?) and needs to
12485 -- be resolved. ???
12487 if Present (Full_View (Priv_Sub))
12488 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
12489 then
12490 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
12491 Exchange_Declarations (Priv_Sub);
12492 end if;
12494 Next_Elmt (Priv_Elmt);
12495 end loop;
12496 end Switch_View;
12498 -----------------------------
12499 -- Valid_Default_Attribute --
12500 -----------------------------
12502 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
12503 Attr_Id : constant Attribute_Id :=
12504 Get_Attribute_Id (Attribute_Name (Def));
12505 T : constant Entity_Id := Entity (Prefix (Def));
12506 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
12507 F : Entity_Id;
12508 Num_F : Int;
12509 OK : Boolean;
12511 begin
12512 if No (T)
12513 or else T = Any_Id
12514 then
12515 return;
12516 end if;
12518 Num_F := 0;
12519 F := First_Formal (Nam);
12520 while Present (F) loop
12521 Num_F := Num_F + 1;
12522 Next_Formal (F);
12523 end loop;
12525 case Attr_Id is
12526 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
12527 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
12528 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
12529 Attribute_Unbiased_Rounding =>
12530 OK := Is_Fun
12531 and then Num_F = 1
12532 and then Is_Floating_Point_Type (T);
12534 when Attribute_Image | Attribute_Pred | Attribute_Succ |
12535 Attribute_Value | Attribute_Wide_Image |
12536 Attribute_Wide_Value =>
12537 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
12539 when Attribute_Max | Attribute_Min =>
12540 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
12542 when Attribute_Input =>
12543 OK := (Is_Fun and then Num_F = 1);
12545 when Attribute_Output | Attribute_Read | Attribute_Write =>
12546 OK := (not Is_Fun and then Num_F = 2);
12548 when others =>
12549 OK := False;
12550 end case;
12552 if not OK then
12553 Error_Msg_N ("attribute reference has wrong profile for subprogram",
12554 Def);
12555 end if;
12556 end Valid_Default_Attribute;
12558 end Sem_Ch12;