Remove outermost loop parameter.
[official-gcc/graphite-test-results.git] / gcc / ada / sem_ch12.adb
blob174811bb81a43986bea522283160de0cdb7d6637
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-2009, 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 Set_Instance_Env
479 (Gen_Unit : Entity_Id;
480 Act_Unit : Entity_Id);
481 -- Save current instance on saved environment, to be used to determine
482 -- the global status of entities in nested instances. Part of Save_Env.
483 -- called after verifying that the generic unit is legal for the instance,
484 -- The procedure also examines whether the generic unit is a predefined
485 -- unit, in order to set configuration switches accordingly. As a result
486 -- the procedure must be called after analyzing and freezing the actuals.
488 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
489 -- Associate analyzed generic parameter with corresponding
490 -- instance. Used for semantic checks at instantiation time.
492 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
493 -- Traverse the Exchanged_Views list to see if a type was private
494 -- and has already been flipped during this phase of instantiation.
496 procedure Hide_Current_Scope;
497 -- When instantiating a generic child unit, the parent context must be
498 -- present, but the instance and all entities that may be generated
499 -- must be inserted in the current scope. We leave the current scope
500 -- on the stack, but make its entities invisible to avoid visibility
501 -- problems. This is reversed at the end of the instantiation. This is
502 -- not done for the instantiation of the bodies, which only require the
503 -- instances of the generic parents to be in scope.
505 procedure Install_Body
506 (Act_Body : Node_Id;
507 N : Node_Id;
508 Gen_Body : Node_Id;
509 Gen_Decl : Node_Id);
510 -- If the instantiation happens textually before the body of the generic,
511 -- the instantiation of the body must be analyzed after the generic body,
512 -- and not at the point of instantiation. Such early instantiations can
513 -- happen if the generic and the instance appear in a package declaration
514 -- because the generic body can only appear in the corresponding package
515 -- body. Early instantiations can also appear if generic, instance and
516 -- body are all in the declarative part of a subprogram or entry. Entities
517 -- of packages that are early instantiations are delayed, and their freeze
518 -- node appears after the generic body.
520 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id);
521 -- Insert freeze node at the end of the declarative part that includes the
522 -- instance node N. If N is in the visible part of an enclosing package
523 -- declaration, the freeze node has to be inserted at the end of the
524 -- private declarations, if any.
526 procedure Freeze_Subprogram_Body
527 (Inst_Node : Node_Id;
528 Gen_Body : Node_Id;
529 Pack_Id : Entity_Id);
530 -- The generic body may appear textually after the instance, including
531 -- in the proper body of a stub, or within a different package instance.
532 -- Given that the instance can only be elaborated after the generic, we
533 -- place freeze_nodes for the instance and/or for packages that may enclose
534 -- the instance and the generic, so that the back-end can establish the
535 -- proper order of elaboration.
537 procedure Init_Env;
538 -- Establish environment for subsequent instantiation. Separated from
539 -- Save_Env because data-structures for visibility handling must be
540 -- initialized before call to Check_Generic_Child_Unit.
542 procedure Install_Formal_Packages (Par : Entity_Id);
543 -- Install the visible part of any formal of the parent that is a formal
544 -- package. Note that for the case of a formal package with a box, this
545 -- includes the formal part of the formal package (12.7(10/2)).
547 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
548 -- When compiling an instance of a child unit the parent (which is
549 -- itself an instance) is an enclosing scope that must be made
550 -- immediately visible. This procedure is also used to install the non-
551 -- generic parent of a generic child unit when compiling its body, so
552 -- that full views of types in the parent are made visible.
554 procedure Remove_Parent (In_Body : Boolean := False);
555 -- Reverse effect after instantiation of child is complete
557 procedure Inline_Instance_Body
558 (N : Node_Id;
559 Gen_Unit : Entity_Id;
560 Act_Decl : Node_Id);
561 -- If front-end inlining is requested, instantiate the package body,
562 -- and preserve the visibility of its compilation unit, to insure
563 -- that successive instantiations succeed.
565 -- The functions Instantiate_XXX perform various legality checks and build
566 -- the declarations for instantiated generic parameters. In all of these
567 -- Formal is the entity in the generic unit, Actual is the entity of
568 -- expression in the generic associations, and Analyzed_Formal is the
569 -- formal in the generic copy, which contains the semantic information to
570 -- be used to validate the actual.
572 function Instantiate_Object
573 (Formal : Node_Id;
574 Actual : Node_Id;
575 Analyzed_Formal : Node_Id) return List_Id;
577 function Instantiate_Type
578 (Formal : Node_Id;
579 Actual : Node_Id;
580 Analyzed_Formal : Node_Id;
581 Actual_Decls : List_Id) return List_Id;
583 function Instantiate_Formal_Subprogram
584 (Formal : Node_Id;
585 Actual : Node_Id;
586 Analyzed_Formal : Node_Id) return Node_Id;
588 function Instantiate_Formal_Package
589 (Formal : Node_Id;
590 Actual : Node_Id;
591 Analyzed_Formal : Node_Id) return List_Id;
592 -- If the formal package is declared with a box, special visibility rules
593 -- apply to its formals: they are in the visible part of the package. This
594 -- is true in the declarative region of the formal package, that is to say
595 -- in the enclosing generic or instantiation. For an instantiation, the
596 -- parameters of the formal package are made visible in an explicit step.
597 -- Furthermore, if the actual has a visible USE clause, these formals must
598 -- be made potentially use-visible as well. On exit from the enclosing
599 -- instantiation, the reverse must be done.
601 -- For a formal package declared without a box, there are conformance rules
602 -- that apply to the actuals in the generic declaration and the actuals of
603 -- the actual package in the enclosing instantiation. The simplest way to
604 -- apply these rules is to repeat the instantiation of the formal package
605 -- in the context of the enclosing instance, and compare the generic
606 -- associations of this instantiation with those of the actual package.
607 -- This internal instantiation only needs to contain the renamings of the
608 -- formals: the visible and private declarations themselves need not be
609 -- created.
611 -- In Ada 2005, the formal package may be only partially parametrized. In
612 -- that case the visibility step must make visible those actuals whose
613 -- corresponding formals were given with a box. A final complication
614 -- involves inherited operations from formal derived types, which must be
615 -- visible if the type is.
617 function Is_In_Main_Unit (N : Node_Id) return Boolean;
618 -- Test if given node is in the main unit
620 procedure Load_Parent_Of_Generic
621 (N : Node_Id;
622 Spec : Node_Id;
623 Body_Optional : Boolean := False);
624 -- If the generic appears in a separate non-generic library unit, load the
625 -- corresponding body to retrieve the body of the generic. N is the node
626 -- for the generic instantiation, Spec is the generic package declaration.
628 -- Body_Optional is a flag that indicates that the body is being loaded to
629 -- ensure that temporaries are generated consistently when there are other
630 -- instances in the current declarative part that precede the one being
631 -- loaded. In that case a missing body is acceptable.
633 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
634 -- Add the context clause of the unit containing a generic unit to a
635 -- compilation unit that is, or contains, an instantiation.
637 function Get_Associated_Node (N : Node_Id) return Node_Id;
638 -- In order to propagate semantic information back from the analyzed copy
639 -- to the original generic, we maintain links between selected nodes in the
640 -- generic and their corresponding copies. At the end of generic analysis,
641 -- the routine Save_Global_References traverses the generic tree, examines
642 -- the semantic information, and preserves the links to those nodes that
643 -- contain global information. At instantiation, the information from the
644 -- associated node is placed on the new copy, so that name resolution is
645 -- not repeated.
647 -- Three kinds of source nodes have associated nodes:
649 -- a) those that can reference (denote) entities, that is identifiers,
650 -- character literals, expanded_names, operator symbols, operators,
651 -- and attribute reference nodes. These nodes have an Entity field
652 -- and are the set of nodes that are in N_Has_Entity.
654 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
656 -- c) selected components (N_Selected_Component)
658 -- For the first class, the associated node preserves the entity if it is
659 -- global. If the generic contains nested instantiations, the associated
660 -- node itself has been recopied, and a chain of them must be followed.
662 -- For aggregates, the associated node allows retrieval of the type, which
663 -- may otherwise not appear in the generic. The view of this type may be
664 -- different between generic and instantiation, and the full view can be
665 -- installed before the instantiation is analyzed. For aggregates of type
666 -- extensions, the same view exchange may have to be performed for some of
667 -- the ancestor types, if their view is private at the point of
668 -- instantiation.
670 -- Nodes that are selected components in the parse tree may be rewritten
671 -- as expanded names after resolution, and must be treated as potential
672 -- entity holders, which is why they also have an Associated_Node.
674 -- Nodes that do not come from source, such as freeze nodes, do not appear
675 -- in the generic tree, and need not have an associated node.
677 -- The associated node is stored in the Associated_Node field. Note that
678 -- this field overlaps Entity, which is fine, because the whole point is
679 -- that we don't need or want the normal Entity field in this situation.
681 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
682 -- Within the generic part, entities in the formal package are
683 -- visible. To validate subsequent type declarations, indicate
684 -- the correspondence between the entities in the analyzed formal,
685 -- and the entities in the actual package. There are three packages
686 -- involved in the instantiation of a formal package: the parent
687 -- generic P1 which appears in the generic declaration, the fake
688 -- instantiation P2 which appears in the analyzed generic, and whose
689 -- visible entities may be used in subsequent formals, and the actual
690 -- P3 in the instance. To validate subsequent formals, me indicate
691 -- that the entities in P2 are mapped into those of P3. The mapping of
692 -- entities has to be done recursively for nested packages.
694 procedure Move_Freeze_Nodes
695 (Out_Of : Entity_Id;
696 After : Node_Id;
697 L : List_Id);
698 -- Freeze nodes can be generated in the analysis of a generic unit, but
699 -- will not be seen by the back-end. It is necessary to move those nodes
700 -- to the enclosing scope if they freeze an outer entity. We place them
701 -- at the end of the enclosing generic package, which is semantically
702 -- neutral.
704 procedure Preanalyze_Actuals (N : Node_Id);
705 -- Analyze actuals to perform name resolution. Full resolution is done
706 -- later, when the expected types are known, but names have to be captured
707 -- before installing parents of generics, that are not visible for the
708 -- actuals themselves.
710 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
711 -- Verify that an attribute that appears as the default for a formal
712 -- subprogram is a function or procedure with the correct profile.
714 -------------------------------------------
715 -- Data Structures for Generic Renamings --
716 -------------------------------------------
718 -- The map Generic_Renamings associates generic entities with their
719 -- corresponding actuals. Currently used to validate type instances. It
720 -- will eventually be used for all generic parameters to eliminate the
721 -- need for overload resolution in the instance.
723 type Assoc_Ptr is new Int;
725 Assoc_Null : constant Assoc_Ptr := -1;
727 type Assoc is record
728 Gen_Id : Entity_Id;
729 Act_Id : Entity_Id;
730 Next_In_HTable : Assoc_Ptr;
731 end record;
733 package Generic_Renamings is new Table.Table
734 (Table_Component_Type => Assoc,
735 Table_Index_Type => Assoc_Ptr,
736 Table_Low_Bound => 0,
737 Table_Initial => 10,
738 Table_Increment => 100,
739 Table_Name => "Generic_Renamings");
741 -- Variable to hold enclosing instantiation. When the environment is
742 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
744 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
746 -- Hash table for associations
748 HTable_Size : constant := 37;
749 type HTable_Range is range 0 .. HTable_Size - 1;
751 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
752 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
753 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
754 function Hash (F : Entity_Id) return HTable_Range;
756 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
757 Header_Num => HTable_Range,
758 Element => Assoc,
759 Elmt_Ptr => Assoc_Ptr,
760 Null_Ptr => Assoc_Null,
761 Set_Next => Set_Next_Assoc,
762 Next => Next_Assoc,
763 Key => Entity_Id,
764 Get_Key => Get_Gen_Id,
765 Hash => Hash,
766 Equal => "=");
768 Exchanged_Views : Elist_Id;
769 -- This list holds the private views that have been exchanged during
770 -- instantiation to restore the visibility of the generic declaration.
771 -- (see comments above). After instantiation, the current visibility is
772 -- reestablished by means of a traversal of this list.
774 Hidden_Entities : Elist_Id;
775 -- This list holds the entities of the current scope that are removed
776 -- from immediate visibility when instantiating a child unit. Their
777 -- visibility is restored in Remove_Parent.
779 -- Because instantiations can be recursive, the following must be saved
780 -- on entry and restored on exit from an instantiation (spec or body).
781 -- This is done by the two procedures Save_Env and Restore_Env. For
782 -- package and subprogram instantiations (but not for the body instances)
783 -- the action of Save_Env is done in two steps: Init_Env is called before
784 -- Check_Generic_Child_Unit, because setting the parent instances requires
785 -- that the visibility data structures be properly initialized. Once the
786 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
788 Parent_Unit_Visible : Boolean := False;
789 -- Parent_Unit_Visible is used when the generic is a child unit, and
790 -- indicates whether the ultimate parent of the generic is visible in the
791 -- instantiation environment. It is used to reset the visibility of the
792 -- parent at the end of the instantiation (see Remove_Parent).
794 Instance_Parent_Unit : Entity_Id := Empty;
795 -- This records the ultimate parent unit of an instance of a generic
796 -- child unit and is used in conjunction with Parent_Unit_Visible to
797 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
799 type Instance_Env is record
800 Instantiated_Parent : Assoc;
801 Exchanged_Views : Elist_Id;
802 Hidden_Entities : Elist_Id;
803 Current_Sem_Unit : Unit_Number_Type;
804 Parent_Unit_Visible : Boolean := False;
805 Instance_Parent_Unit : Entity_Id := Empty;
806 Switches : Config_Switches_Type;
807 end record;
809 package Instance_Envs is new Table.Table (
810 Table_Component_Type => Instance_Env,
811 Table_Index_Type => Int,
812 Table_Low_Bound => 0,
813 Table_Initial => 32,
814 Table_Increment => 100,
815 Table_Name => "Instance_Envs");
817 procedure Restore_Private_Views
818 (Pack_Id : Entity_Id;
819 Is_Package : Boolean := True);
820 -- Restore the private views of external types, and unmark the generic
821 -- renamings of actuals, so that they become compatible subtypes again.
822 -- For subprograms, Pack_Id is the package constructed to hold the
823 -- renamings.
825 procedure Switch_View (T : Entity_Id);
826 -- Switch the partial and full views of a type and its private
827 -- dependents (i.e. its subtypes and derived types).
829 ------------------------------------
830 -- Structures for Error Reporting --
831 ------------------------------------
833 Instantiation_Node : Node_Id;
834 -- Used by subprograms that validate instantiation of formal parameters
835 -- where there might be no actual on which to place the error message.
836 -- Also used to locate the instantiation node for generic subunits.
838 Instantiation_Error : exception;
839 -- When there is a semantic error in the generic parameter matching,
840 -- there is no point in continuing the instantiation, because the
841 -- number of cascaded errors is unpredictable. This exception aborts
842 -- the instantiation process altogether.
844 S_Adjustment : Sloc_Adjustment;
845 -- Offset created for each node in an instantiation, in order to keep
846 -- track of the source position of the instantiation in each of its nodes.
847 -- A subsequent semantic error or warning on a construct of the instance
848 -- points to both places: the original generic node, and the point of
849 -- instantiation. See Sinput and Sinput.L for additional details.
851 ------------------------------------------------------------
852 -- Data structure for keeping track when inside a Generic --
853 ------------------------------------------------------------
855 -- The following table is used to save values of the Inside_A_Generic
856 -- flag (see spec of Sem) when they are saved by Start_Generic.
858 package Generic_Flags is new Table.Table (
859 Table_Component_Type => Boolean,
860 Table_Index_Type => Int,
861 Table_Low_Bound => 0,
862 Table_Initial => 32,
863 Table_Increment => 200,
864 Table_Name => "Generic_Flags");
866 ---------------------------
867 -- Abandon_Instantiation --
868 ---------------------------
870 procedure Abandon_Instantiation (N : Node_Id) is
871 begin
872 Error_Msg_N ("\instantiation abandoned!", N);
873 raise Instantiation_Error;
874 end Abandon_Instantiation;
876 --------------------------
877 -- Analyze_Associations --
878 --------------------------
880 function Analyze_Associations
881 (I_Node : Node_Id;
882 Formals : List_Id;
883 F_Copy : List_Id) return List_Id
886 Actual_Types : constant Elist_Id := New_Elmt_List;
887 Assoc : constant List_Id := New_List;
888 Default_Actuals : constant Elist_Id := New_Elmt_List;
889 Gen_Unit : constant Entity_Id :=
890 Defining_Entity (Parent (F_Copy));
892 Actuals : List_Id;
893 Actual : Node_Id;
894 Formal : Node_Id;
895 Next_Formal : Node_Id;
896 Temp_Formal : Node_Id;
897 Analyzed_Formal : Node_Id;
898 Match : Node_Id;
899 Named : Node_Id;
900 First_Named : Node_Id := Empty;
902 Default_Formals : constant List_Id := New_List;
903 -- If an Others_Choice is present, some of the formals may be defaulted.
904 -- To simplify the treatment of visibility in an instance, we introduce
905 -- individual defaults for each such formal. These defaults are
906 -- appended to the list of associations and replace the Others_Choice.
908 Found_Assoc : Node_Id;
909 -- Association for the current formal being match. Empty if there are
910 -- no remaining actuals, or if there is no named association with the
911 -- name of the formal.
913 Is_Named_Assoc : Boolean;
914 Num_Matched : Int := 0;
915 Num_Actuals : Int := 0;
917 Others_Present : Boolean := False;
918 -- In Ada 2005, indicates partial parametrization of a formal
919 -- package. As usual an other association must be last in the list.
921 function Matching_Actual
922 (F : Entity_Id;
923 A_F : Entity_Id) return Node_Id;
924 -- Find actual that corresponds to a given a formal parameter. If the
925 -- actuals are positional, return the next one, if any. If the actuals
926 -- are named, scan the parameter associations to find the right one.
927 -- A_F is the corresponding entity in the analyzed generic,which is
928 -- placed on the selector name for ASIS use.
930 -- In Ada 2005, a named association may be given with a box, in which
931 -- case Matching_Actual sets Found_Assoc to the generic association,
932 -- but return Empty for the actual itself. In this case the code below
933 -- creates a corresponding declaration for the formal.
935 function Partial_Parametrization return Boolean;
936 -- Ada 2005: if no match is found for a given formal, check if the
937 -- association for it includes a box, or whether the associations
938 -- include an Others clause.
940 procedure Process_Default (F : Entity_Id);
941 -- Add a copy of the declaration of generic formal F to the list of
942 -- associations, and add an explicit box association for F if there
943 -- is none yet, and the default comes from an Others_Choice.
945 procedure Set_Analyzed_Formal;
946 -- Find the node in the generic copy that corresponds to a given formal.
947 -- The semantic information on this node is used to perform legality
948 -- checks on the actuals. Because semantic analysis can introduce some
949 -- anonymous entities or modify the declaration node itself, the
950 -- correspondence between the two lists is not one-one. In addition to
951 -- anonymous types, the presence a formal equality will introduce an
952 -- implicit declaration for the corresponding inequality.
954 ---------------------
955 -- Matching_Actual --
956 ---------------------
958 function Matching_Actual
959 (F : Entity_Id;
960 A_F : Entity_Id) return Node_Id
962 Prev : Node_Id;
963 Act : Node_Id;
965 begin
966 Is_Named_Assoc := False;
968 -- End of list of purely positional parameters
970 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
971 Found_Assoc := Empty;
972 Act := Empty;
974 -- Case of positional parameter corresponding to current formal
976 elsif No (Selector_Name (Actual)) then
977 Found_Assoc := Actual;
978 Act := Explicit_Generic_Actual_Parameter (Actual);
979 Num_Matched := Num_Matched + 1;
980 Next (Actual);
982 -- Otherwise scan list of named actuals to find the one with the
983 -- desired name. All remaining actuals have explicit names.
985 else
986 Is_Named_Assoc := True;
987 Found_Assoc := Empty;
988 Act := Empty;
989 Prev := Empty;
991 while Present (Actual) loop
992 if Chars (Selector_Name (Actual)) = Chars (F) then
993 Set_Entity (Selector_Name (Actual), A_F);
994 Set_Etype (Selector_Name (Actual), Etype (A_F));
995 Generate_Reference (A_F, Selector_Name (Actual));
996 Found_Assoc := Actual;
997 Act := Explicit_Generic_Actual_Parameter (Actual);
998 Num_Matched := Num_Matched + 1;
999 exit;
1000 end if;
1002 Prev := Actual;
1003 Next (Actual);
1004 end loop;
1006 -- Reset for subsequent searches. In most cases the named
1007 -- associations are in order. If they are not, we reorder them
1008 -- to avoid scanning twice the same actual. This is not just a
1009 -- question of efficiency: there may be multiple defaults with
1010 -- boxes that have the same name. In a nested instantiation we
1011 -- insert actuals for those defaults, and cannot rely on their
1012 -- names to disambiguate them.
1014 if Actual = First_Named then
1015 Next (First_Named);
1017 elsif Present (Actual) then
1018 Insert_Before (First_Named, Remove_Next (Prev));
1019 end if;
1021 Actual := First_Named;
1022 end if;
1024 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1025 Set_Used_As_Generic_Actual (Entity (Act));
1026 end if;
1028 return Act;
1029 end Matching_Actual;
1031 -----------------------------
1032 -- Partial_Parametrization --
1033 -----------------------------
1035 function Partial_Parametrization return Boolean is
1036 begin
1037 return Others_Present
1038 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1039 end Partial_Parametrization;
1041 ---------------------
1042 -- Process_Default --
1043 ---------------------
1045 procedure Process_Default (F : Entity_Id) is
1046 Loc : constant Source_Ptr := Sloc (I_Node);
1047 F_Id : constant Entity_Id := Defining_Entity (F);
1048 Decl : Node_Id;
1049 Default : Node_Id;
1050 Id : Entity_Id;
1052 begin
1053 -- Append copy of formal declaration to associations, and create new
1054 -- defining identifier for it.
1056 Decl := New_Copy_Tree (F);
1057 Id := Make_Defining_Identifier (Sloc (F_Id), Chars => Chars (F_Id));
1059 if Nkind (F) in N_Formal_Subprogram_Declaration then
1060 Set_Defining_Unit_Name (Specification (Decl), Id);
1062 else
1063 Set_Defining_Identifier (Decl, Id);
1064 end if;
1066 Append (Decl, Assoc);
1068 if No (Found_Assoc) then
1069 Default :=
1070 Make_Generic_Association (Loc,
1071 Selector_Name => New_Occurrence_Of (Id, Loc),
1072 Explicit_Generic_Actual_Parameter => Empty);
1073 Set_Box_Present (Default);
1074 Append (Default, Default_Formals);
1075 end if;
1076 end Process_Default;
1078 -------------------------
1079 -- Set_Analyzed_Formal --
1080 -------------------------
1082 procedure Set_Analyzed_Formal is
1083 Kind : Node_Kind;
1085 begin
1086 while Present (Analyzed_Formal) loop
1087 Kind := Nkind (Analyzed_Formal);
1089 case Nkind (Formal) is
1091 when N_Formal_Subprogram_Declaration =>
1092 exit when Kind in N_Formal_Subprogram_Declaration
1093 and then
1094 Chars
1095 (Defining_Unit_Name (Specification (Formal))) =
1096 Chars
1097 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1099 when N_Formal_Package_Declaration =>
1100 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1101 N_Generic_Package_Declaration,
1102 N_Package_Declaration);
1104 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1106 when others =>
1108 -- Skip freeze nodes, and nodes inserted to replace
1109 -- unrecognized pragmas.
1111 exit when
1112 Kind not in N_Formal_Subprogram_Declaration
1113 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1114 N_Freeze_Entity,
1115 N_Null_Statement,
1116 N_Itype_Reference)
1117 and then Chars (Defining_Identifier (Formal)) =
1118 Chars (Defining_Identifier (Analyzed_Formal));
1119 end case;
1121 Next (Analyzed_Formal);
1122 end loop;
1123 end Set_Analyzed_Formal;
1125 -- Start of processing for Analyze_Associations
1127 begin
1128 Actuals := Generic_Associations (I_Node);
1130 if Present (Actuals) then
1132 -- Check for an Others choice, indicating a partial parametrization
1133 -- for a formal package.
1135 Actual := First (Actuals);
1136 while Present (Actual) loop
1137 if Nkind (Actual) = N_Others_Choice then
1138 Others_Present := True;
1140 if Present (Next (Actual)) then
1141 Error_Msg_N ("others must be last association", Actual);
1142 end if;
1144 -- This subprogram is used both for formal packages and for
1145 -- instantiations. For the latter, associations must all be
1146 -- explicit.
1148 if Nkind (I_Node) /= N_Formal_Package_Declaration
1149 and then Comes_From_Source (I_Node)
1150 then
1151 Error_Msg_N
1152 ("others association not allowed in an instance",
1153 Actual);
1154 end if;
1156 -- In any case, nothing to do after the others association
1158 exit;
1160 elsif Box_Present (Actual)
1161 and then Comes_From_Source (I_Node)
1162 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1163 then
1164 Error_Msg_N
1165 ("box association not allowed in an instance", Actual);
1166 end if;
1168 Next (Actual);
1169 end loop;
1171 -- If named associations are present, save first named association
1172 -- (it may of course be Empty) to facilitate subsequent name search.
1174 First_Named := First (Actuals);
1175 while Present (First_Named)
1176 and then Nkind (First_Named) /= N_Others_Choice
1177 and then No (Selector_Name (First_Named))
1178 loop
1179 Num_Actuals := Num_Actuals + 1;
1180 Next (First_Named);
1181 end loop;
1182 end if;
1184 Named := First_Named;
1185 while Present (Named) loop
1186 if Nkind (Named) /= N_Others_Choice
1187 and then No (Selector_Name (Named))
1188 then
1189 Error_Msg_N ("invalid positional actual after named one", Named);
1190 Abandon_Instantiation (Named);
1191 end if;
1193 -- A named association may lack an actual parameter, if it was
1194 -- introduced for a default subprogram that turns out to be local
1195 -- to the outer instantiation.
1197 if Nkind (Named) /= N_Others_Choice
1198 and then Present (Explicit_Generic_Actual_Parameter (Named))
1199 then
1200 Num_Actuals := Num_Actuals + 1;
1201 end if;
1203 Next (Named);
1204 end loop;
1206 if Present (Formals) then
1207 Formal := First_Non_Pragma (Formals);
1208 Analyzed_Formal := First_Non_Pragma (F_Copy);
1210 if Present (Actuals) then
1211 Actual := First (Actuals);
1213 -- All formals should have default values
1215 else
1216 Actual := Empty;
1217 end if;
1219 while Present (Formal) loop
1220 Set_Analyzed_Formal;
1221 Next_Formal := Next_Non_Pragma (Formal);
1223 case Nkind (Formal) is
1224 when N_Formal_Object_Declaration =>
1225 Match :=
1226 Matching_Actual (
1227 Defining_Identifier (Formal),
1228 Defining_Identifier (Analyzed_Formal));
1230 if No (Match) and then Partial_Parametrization then
1231 Process_Default (Formal);
1232 else
1233 Append_List
1234 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1235 Assoc);
1236 end if;
1238 when N_Formal_Type_Declaration =>
1239 Match :=
1240 Matching_Actual (
1241 Defining_Identifier (Formal),
1242 Defining_Identifier (Analyzed_Formal));
1244 if No (Match) then
1245 if Partial_Parametrization then
1246 Process_Default (Formal);
1248 else
1249 Error_Msg_Sloc := Sloc (Gen_Unit);
1250 Error_Msg_NE
1251 ("missing actual&",
1252 Instantiation_Node,
1253 Defining_Identifier (Formal));
1254 Error_Msg_NE ("\in instantiation of & declared#",
1255 Instantiation_Node, Gen_Unit);
1256 Abandon_Instantiation (Instantiation_Node);
1257 end if;
1259 else
1260 Analyze (Match);
1261 Append_List
1262 (Instantiate_Type
1263 (Formal, Match, Analyzed_Formal, Assoc),
1264 Assoc);
1266 -- An instantiation is a freeze point for the actuals,
1267 -- unless this is a rewritten formal package.
1269 if Nkind (I_Node) /= N_Formal_Package_Declaration then
1270 Append_Elmt (Entity (Match), Actual_Types);
1271 end if;
1272 end if;
1274 -- A remote access-to-class-wide type must not be an
1275 -- actual parameter for a generic formal of an access
1276 -- type (E.2.2 (17)).
1278 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1279 and then
1280 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1281 N_Access_To_Object_Definition
1282 then
1283 Validate_Remote_Access_To_Class_Wide_Type (Match);
1284 end if;
1286 when N_Formal_Subprogram_Declaration =>
1287 Match :=
1288 Matching_Actual (
1289 Defining_Unit_Name (Specification (Formal)),
1290 Defining_Unit_Name (Specification (Analyzed_Formal)));
1292 -- If the formal subprogram has the same name as another
1293 -- formal subprogram of the generic, then a named
1294 -- association is illegal (12.3(9)). Exclude named
1295 -- associations that are generated for a nested instance.
1297 if Present (Match)
1298 and then Is_Named_Assoc
1299 and then Comes_From_Source (Found_Assoc)
1300 then
1301 Temp_Formal := First (Formals);
1302 while Present (Temp_Formal) loop
1303 if Nkind (Temp_Formal) in
1304 N_Formal_Subprogram_Declaration
1305 and then Temp_Formal /= Formal
1306 and then
1307 Chars (Selector_Name (Found_Assoc)) =
1308 Chars (Defining_Unit_Name
1309 (Specification (Temp_Formal)))
1310 then
1311 Error_Msg_N
1312 ("name not allowed for overloaded formal",
1313 Found_Assoc);
1314 Abandon_Instantiation (Instantiation_Node);
1315 end if;
1317 Next (Temp_Formal);
1318 end loop;
1319 end if;
1321 -- If there is no corresponding actual, this may be case of
1322 -- partial parametrization, or else the formal has a default
1323 -- or a box.
1325 if No (Match)
1326 and then Partial_Parametrization
1327 then
1328 Process_Default (Formal);
1329 else
1330 Append_To (Assoc,
1331 Instantiate_Formal_Subprogram
1332 (Formal, Match, Analyzed_Formal));
1333 end if;
1335 -- If this is a nested generic, preserve default for later
1336 -- instantiations.
1338 if No (Match)
1339 and then Box_Present (Formal)
1340 then
1341 Append_Elmt
1342 (Defining_Unit_Name (Specification (Last (Assoc))),
1343 Default_Actuals);
1344 end if;
1346 when N_Formal_Package_Declaration =>
1347 Match :=
1348 Matching_Actual (
1349 Defining_Identifier (Formal),
1350 Defining_Identifier (Original_Node (Analyzed_Formal)));
1352 if No (Match) then
1353 if Partial_Parametrization then
1354 Process_Default (Formal);
1356 else
1357 Error_Msg_Sloc := Sloc (Gen_Unit);
1358 Error_Msg_NE
1359 ("missing actual&",
1360 Instantiation_Node, Defining_Identifier (Formal));
1361 Error_Msg_NE ("\in instantiation of & declared#",
1362 Instantiation_Node, Gen_Unit);
1364 Abandon_Instantiation (Instantiation_Node);
1365 end if;
1367 else
1368 Analyze (Match);
1369 Append_List
1370 (Instantiate_Formal_Package
1371 (Formal, Match, Analyzed_Formal),
1372 Assoc);
1373 end if;
1375 -- For use type and use package appearing in the generic part,
1376 -- we have already copied them, so we can just move them where
1377 -- they belong (we mustn't recopy them since this would mess up
1378 -- the Sloc values).
1380 when N_Use_Package_Clause |
1381 N_Use_Type_Clause =>
1382 if Nkind (Original_Node (I_Node)) =
1383 N_Formal_Package_Declaration
1384 then
1385 Append (New_Copy_Tree (Formal), Assoc);
1386 else
1387 Remove (Formal);
1388 Append (Formal, Assoc);
1389 end if;
1391 when others =>
1392 raise Program_Error;
1394 end case;
1396 Formal := Next_Formal;
1397 Next_Non_Pragma (Analyzed_Formal);
1398 end loop;
1400 if Num_Actuals > Num_Matched then
1401 Error_Msg_Sloc := Sloc (Gen_Unit);
1403 if Present (Selector_Name (Actual)) then
1404 Error_Msg_NE
1405 ("unmatched actual&",
1406 Actual, Selector_Name (Actual));
1407 Error_Msg_NE ("\in instantiation of& declared#",
1408 Actual, Gen_Unit);
1409 else
1410 Error_Msg_NE
1411 ("unmatched actual in instantiation of& declared#",
1412 Actual, Gen_Unit);
1413 end if;
1414 end if;
1416 elsif Present (Actuals) then
1417 Error_Msg_N
1418 ("too many actuals in generic instantiation", Instantiation_Node);
1419 end if;
1421 declare
1422 Elmt : Elmt_Id := First_Elmt (Actual_Types);
1423 begin
1424 while Present (Elmt) loop
1425 Freeze_Before (I_Node, Node (Elmt));
1426 Next_Elmt (Elmt);
1427 end loop;
1428 end;
1430 -- If there are default subprograms, normalize the tree by adding
1431 -- explicit associations for them. This is required if the instance
1432 -- appears within a generic.
1434 declare
1435 Elmt : Elmt_Id;
1436 Subp : Entity_Id;
1437 New_D : Node_Id;
1439 begin
1440 Elmt := First_Elmt (Default_Actuals);
1441 while Present (Elmt) loop
1442 if No (Actuals) then
1443 Actuals := New_List;
1444 Set_Generic_Associations (I_Node, Actuals);
1445 end if;
1447 Subp := Node (Elmt);
1448 New_D :=
1449 Make_Generic_Association (Sloc (Subp),
1450 Selector_Name => New_Occurrence_Of (Subp, Sloc (Subp)),
1451 Explicit_Generic_Actual_Parameter =>
1452 New_Occurrence_Of (Subp, Sloc (Subp)));
1453 Mark_Rewrite_Insertion (New_D);
1454 Append_To (Actuals, New_D);
1455 Next_Elmt (Elmt);
1456 end loop;
1457 end;
1459 -- If this is a formal package, normalize the parameter list by adding
1460 -- explicit box associations for the formals that are covered by an
1461 -- Others_Choice.
1463 if not Is_Empty_List (Default_Formals) then
1464 Append_List (Default_Formals, Formals);
1465 end if;
1467 return Assoc;
1468 end Analyze_Associations;
1470 -------------------------------
1471 -- Analyze_Formal_Array_Type --
1472 -------------------------------
1474 procedure Analyze_Formal_Array_Type
1475 (T : in out Entity_Id;
1476 Def : Node_Id)
1478 DSS : Node_Id;
1480 begin
1481 -- Treated like a non-generic array declaration, with additional
1482 -- semantic checks.
1484 Enter_Name (T);
1486 if Nkind (Def) = N_Constrained_Array_Definition then
1487 DSS := First (Discrete_Subtype_Definitions (Def));
1488 while Present (DSS) loop
1489 if Nkind_In (DSS, N_Subtype_Indication,
1490 N_Range,
1491 N_Attribute_Reference)
1492 then
1493 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1494 end if;
1496 Next (DSS);
1497 end loop;
1498 end if;
1500 Array_Type_Declaration (T, Def);
1501 Set_Is_Generic_Type (Base_Type (T));
1503 if Ekind (Component_Type (T)) = E_Incomplete_Type
1504 and then No (Full_View (Component_Type (T)))
1505 then
1506 Error_Msg_N ("premature usage of incomplete type", Def);
1508 -- Check that range constraint is not allowed on the component type
1509 -- of a generic formal array type (AARM 12.5.3(3))
1511 elsif Is_Internal (Component_Type (T))
1512 and then Present (Subtype_Indication (Component_Definition (Def)))
1513 and then Nkind (Original_Node
1514 (Subtype_Indication (Component_Definition (Def)))) =
1515 N_Subtype_Indication
1516 then
1517 Error_Msg_N
1518 ("in a formal, a subtype indication can only be "
1519 & "a subtype mark (RM 12.5.3(3))",
1520 Subtype_Indication (Component_Definition (Def)));
1521 end if;
1523 end Analyze_Formal_Array_Type;
1525 ---------------------------------------------
1526 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1527 ---------------------------------------------
1529 -- As for other generic types, we create a valid type representation with
1530 -- legal but arbitrary attributes, whose values are never considered
1531 -- static. For all scalar types we introduce an anonymous base type, with
1532 -- the same attributes. We choose the corresponding integer type to be
1533 -- Standard_Integer.
1535 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1536 (T : Entity_Id;
1537 Def : Node_Id)
1539 Loc : constant Source_Ptr := Sloc (Def);
1540 Base : constant Entity_Id :=
1541 New_Internal_Entity
1542 (E_Decimal_Fixed_Point_Type,
1543 Current_Scope, Sloc (Def), 'G');
1544 Int_Base : constant Entity_Id := Standard_Integer;
1545 Delta_Val : constant Ureal := Ureal_1;
1546 Digs_Val : constant Uint := Uint_6;
1548 begin
1549 Enter_Name (T);
1551 Set_Etype (Base, Base);
1552 Set_Size_Info (Base, Int_Base);
1553 Set_RM_Size (Base, RM_Size (Int_Base));
1554 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1555 Set_Digits_Value (Base, Digs_Val);
1556 Set_Delta_Value (Base, Delta_Val);
1557 Set_Small_Value (Base, Delta_Val);
1558 Set_Scalar_Range (Base,
1559 Make_Range (Loc,
1560 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1561 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1563 Set_Is_Generic_Type (Base);
1564 Set_Parent (Base, Parent (Def));
1566 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1567 Set_Etype (T, Base);
1568 Set_Size_Info (T, Int_Base);
1569 Set_RM_Size (T, RM_Size (Int_Base));
1570 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1571 Set_Digits_Value (T, Digs_Val);
1572 Set_Delta_Value (T, Delta_Val);
1573 Set_Small_Value (T, Delta_Val);
1574 Set_Scalar_Range (T, Scalar_Range (Base));
1575 Set_Is_Constrained (T);
1577 Check_Restriction (No_Fixed_Point, Def);
1578 end Analyze_Formal_Decimal_Fixed_Point_Type;
1580 -------------------------------------------
1581 -- Analyze_Formal_Derived_Interface_Type --
1582 -------------------------------------------
1584 procedure Analyze_Formal_Derived_Interface_Type
1585 (N : Node_Id;
1586 T : Entity_Id;
1587 Def : Node_Id)
1589 Loc : constant Source_Ptr := Sloc (Def);
1591 begin
1592 -- Rewrite as a type declaration of a derived type. This ensures that
1593 -- the interface list and primitive operations are properly captured.
1595 Rewrite (N,
1596 Make_Full_Type_Declaration (Loc,
1597 Defining_Identifier => T,
1598 Type_Definition => Def));
1599 Analyze (N);
1600 Set_Is_Generic_Type (T);
1601 end Analyze_Formal_Derived_Interface_Type;
1603 ---------------------------------
1604 -- Analyze_Formal_Derived_Type --
1605 ---------------------------------
1607 procedure Analyze_Formal_Derived_Type
1608 (N : Node_Id;
1609 T : Entity_Id;
1610 Def : Node_Id)
1612 Loc : constant Source_Ptr := Sloc (Def);
1613 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
1614 New_N : Node_Id;
1616 begin
1617 Set_Is_Generic_Type (T);
1619 if Private_Present (Def) then
1620 New_N :=
1621 Make_Private_Extension_Declaration (Loc,
1622 Defining_Identifier => T,
1623 Discriminant_Specifications => Discriminant_Specifications (N),
1624 Unknown_Discriminants_Present => Unk_Disc,
1625 Subtype_Indication => Subtype_Mark (Def),
1626 Interface_List => Interface_List (Def));
1628 Set_Abstract_Present (New_N, Abstract_Present (Def));
1629 Set_Limited_Present (New_N, Limited_Present (Def));
1630 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
1632 else
1633 New_N :=
1634 Make_Full_Type_Declaration (Loc,
1635 Defining_Identifier => T,
1636 Discriminant_Specifications =>
1637 Discriminant_Specifications (Parent (T)),
1638 Type_Definition =>
1639 Make_Derived_Type_Definition (Loc,
1640 Subtype_Indication => Subtype_Mark (Def)));
1642 Set_Abstract_Present
1643 (Type_Definition (New_N), Abstract_Present (Def));
1644 Set_Limited_Present
1645 (Type_Definition (New_N), Limited_Present (Def));
1646 end if;
1648 Rewrite (N, New_N);
1649 Analyze (N);
1651 if Unk_Disc then
1652 if not Is_Composite_Type (T) then
1653 Error_Msg_N
1654 ("unknown discriminants not allowed for elementary types", N);
1655 else
1656 Set_Has_Unknown_Discriminants (T);
1657 Set_Is_Constrained (T, False);
1658 end if;
1659 end if;
1661 -- If the parent type has a known size, so does the formal, which makes
1662 -- legal representation clauses that involve the formal.
1664 Set_Size_Known_At_Compile_Time
1665 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
1666 end Analyze_Formal_Derived_Type;
1668 ----------------------------------
1669 -- Analyze_Formal_Discrete_Type --
1670 ----------------------------------
1672 -- The operations defined for a discrete types are those of an enumeration
1673 -- type. The size is set to an arbitrary value, for use in analyzing the
1674 -- generic unit.
1676 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
1677 Loc : constant Source_Ptr := Sloc (Def);
1678 Lo : Node_Id;
1679 Hi : Node_Id;
1681 Base : constant Entity_Id :=
1682 New_Internal_Entity
1683 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1684 begin
1685 Enter_Name (T);
1686 Set_Ekind (T, E_Enumeration_Subtype);
1687 Set_Etype (T, Base);
1688 Init_Size (T, 8);
1689 Init_Alignment (T);
1690 Set_Is_Generic_Type (T);
1691 Set_Is_Constrained (T);
1693 -- For semantic analysis, the bounds of the type must be set to some
1694 -- non-static value. The simplest is to create attribute nodes for those
1695 -- bounds, that refer to the type itself. These bounds are never
1696 -- analyzed but serve as place-holders.
1698 Lo :=
1699 Make_Attribute_Reference (Loc,
1700 Attribute_Name => Name_First,
1701 Prefix => New_Reference_To (T, Loc));
1702 Set_Etype (Lo, T);
1704 Hi :=
1705 Make_Attribute_Reference (Loc,
1706 Attribute_Name => Name_Last,
1707 Prefix => New_Reference_To (T, Loc));
1708 Set_Etype (Hi, T);
1710 Set_Scalar_Range (T,
1711 Make_Range (Loc,
1712 Low_Bound => Lo,
1713 High_Bound => Hi));
1715 Set_Ekind (Base, E_Enumeration_Type);
1716 Set_Etype (Base, Base);
1717 Init_Size (Base, 8);
1718 Init_Alignment (Base);
1719 Set_Is_Generic_Type (Base);
1720 Set_Scalar_Range (Base, Scalar_Range (T));
1721 Set_Parent (Base, Parent (Def));
1722 end Analyze_Formal_Discrete_Type;
1724 ----------------------------------
1725 -- Analyze_Formal_Floating_Type --
1726 ---------------------------------
1728 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
1729 Base : constant Entity_Id :=
1730 New_Internal_Entity
1731 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1733 begin
1734 -- The various semantic attributes are taken from the predefined type
1735 -- Float, just so that all of them are initialized. Their values are
1736 -- never used because no constant folding or expansion takes place in
1737 -- the generic itself.
1739 Enter_Name (T);
1740 Set_Ekind (T, E_Floating_Point_Subtype);
1741 Set_Etype (T, Base);
1742 Set_Size_Info (T, (Standard_Float));
1743 Set_RM_Size (T, RM_Size (Standard_Float));
1744 Set_Digits_Value (T, Digits_Value (Standard_Float));
1745 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
1746 Set_Is_Constrained (T);
1748 Set_Is_Generic_Type (Base);
1749 Set_Etype (Base, Base);
1750 Set_Size_Info (Base, (Standard_Float));
1751 Set_RM_Size (Base, RM_Size (Standard_Float));
1752 Set_Digits_Value (Base, Digits_Value (Standard_Float));
1753 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
1754 Set_Parent (Base, Parent (Def));
1756 Check_Restriction (No_Floating_Point, Def);
1757 end Analyze_Formal_Floating_Type;
1759 -----------------------------------
1760 -- Analyze_Formal_Interface_Type;--
1761 -----------------------------------
1763 procedure Analyze_Formal_Interface_Type
1764 (N : Node_Id;
1765 T : Entity_Id;
1766 Def : Node_Id)
1768 Loc : constant Source_Ptr := Sloc (N);
1769 New_N : Node_Id;
1771 begin
1772 New_N :=
1773 Make_Full_Type_Declaration (Loc,
1774 Defining_Identifier => T,
1775 Type_Definition => Def);
1777 Rewrite (N, New_N);
1778 Analyze (N);
1779 Set_Is_Generic_Type (T);
1780 end Analyze_Formal_Interface_Type;
1782 ---------------------------------
1783 -- Analyze_Formal_Modular_Type --
1784 ---------------------------------
1786 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
1787 begin
1788 -- Apart from their entity kind, generic modular types are treated like
1789 -- signed integer types, and have the same attributes.
1791 Analyze_Formal_Signed_Integer_Type (T, Def);
1792 Set_Ekind (T, E_Modular_Integer_Subtype);
1793 Set_Ekind (Etype (T), E_Modular_Integer_Type);
1795 end Analyze_Formal_Modular_Type;
1797 ---------------------------------------
1798 -- Analyze_Formal_Object_Declaration --
1799 ---------------------------------------
1801 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
1802 E : constant Node_Id := Default_Expression (N);
1803 Id : constant Node_Id := Defining_Identifier (N);
1804 K : Entity_Kind;
1805 T : Node_Id;
1807 begin
1808 Enter_Name (Id);
1810 -- Determine the mode of the formal object
1812 if Out_Present (N) then
1813 K := E_Generic_In_Out_Parameter;
1815 if not In_Present (N) then
1816 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
1817 end if;
1819 else
1820 K := E_Generic_In_Parameter;
1821 end if;
1823 if Present (Subtype_Mark (N)) then
1824 Find_Type (Subtype_Mark (N));
1825 T := Entity (Subtype_Mark (N));
1827 -- Verify that there is no redundant null exclusion
1829 if Null_Exclusion_Present (N) then
1830 if not Is_Access_Type (T) then
1831 Error_Msg_N
1832 ("null exclusion can only apply to an access type", N);
1834 elsif Can_Never_Be_Null (T) then
1835 Error_Msg_NE
1836 ("`NOT NULL` not allowed (& already excludes null)",
1837 N, T);
1838 end if;
1839 end if;
1841 -- Ada 2005 (AI-423): Formal object with an access definition
1843 else
1844 Check_Access_Definition (N);
1845 T := Access_Definition
1846 (Related_Nod => N,
1847 N => Access_Definition (N));
1848 end if;
1850 if Ekind (T) = E_Incomplete_Type then
1851 declare
1852 Error_Node : Node_Id;
1854 begin
1855 if Present (Subtype_Mark (N)) then
1856 Error_Node := Subtype_Mark (N);
1857 else
1858 Check_Access_Definition (N);
1859 Error_Node := Access_Definition (N);
1860 end if;
1862 Error_Msg_N ("premature usage of incomplete type", Error_Node);
1863 end;
1864 end if;
1866 if K = E_Generic_In_Parameter then
1868 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
1870 if Ada_Version < Ada_05 and then Is_Limited_Type (T) then
1871 Error_Msg_N
1872 ("generic formal of mode IN must not be of limited type", N);
1873 Explain_Limited_Type (T, N);
1874 end if;
1876 if Is_Abstract_Type (T) then
1877 Error_Msg_N
1878 ("generic formal of mode IN must not be of abstract type", N);
1879 end if;
1881 if Present (E) then
1882 Preanalyze_Spec_Expression (E, T);
1884 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
1885 Error_Msg_N
1886 ("initialization not allowed for limited types", E);
1887 Explain_Limited_Type (T, E);
1888 end if;
1889 end if;
1891 Set_Ekind (Id, K);
1892 Set_Etype (Id, T);
1894 -- Case of generic IN OUT parameter
1896 else
1897 -- If the formal has an unconstrained type, construct its actual
1898 -- subtype, as is done for subprogram formals. In this fashion, all
1899 -- its uses can refer to specific bounds.
1901 Set_Ekind (Id, K);
1902 Set_Etype (Id, T);
1904 if (Is_Array_Type (T)
1905 and then not Is_Constrained (T))
1906 or else
1907 (Ekind (T) = E_Record_Type
1908 and then Has_Discriminants (T))
1909 then
1910 declare
1911 Non_Freezing_Ref : constant Node_Id :=
1912 New_Reference_To (Id, Sloc (Id));
1913 Decl : Node_Id;
1915 begin
1916 -- Make sure the actual subtype doesn't generate bogus freezing
1918 Set_Must_Not_Freeze (Non_Freezing_Ref);
1919 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
1920 Insert_Before_And_Analyze (N, Decl);
1921 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
1922 end;
1923 else
1924 Set_Actual_Subtype (Id, T);
1925 end if;
1927 if Present (E) then
1928 Error_Msg_N
1929 ("initialization not allowed for `IN OUT` formals", N);
1930 end if;
1931 end if;
1932 end Analyze_Formal_Object_Declaration;
1934 ----------------------------------------------
1935 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
1936 ----------------------------------------------
1938 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
1939 (T : Entity_Id;
1940 Def : Node_Id)
1942 Loc : constant Source_Ptr := Sloc (Def);
1943 Base : constant Entity_Id :=
1944 New_Internal_Entity
1945 (E_Ordinary_Fixed_Point_Type, Current_Scope, Sloc (Def), 'G');
1946 begin
1947 -- The semantic attributes are set for completeness only, their values
1948 -- will never be used, since all properties of the type are non-static.
1950 Enter_Name (T);
1951 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
1952 Set_Etype (T, Base);
1953 Set_Size_Info (T, Standard_Integer);
1954 Set_RM_Size (T, RM_Size (Standard_Integer));
1955 Set_Small_Value (T, Ureal_1);
1956 Set_Delta_Value (T, Ureal_1);
1957 Set_Scalar_Range (T,
1958 Make_Range (Loc,
1959 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1960 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1961 Set_Is_Constrained (T);
1963 Set_Is_Generic_Type (Base);
1964 Set_Etype (Base, Base);
1965 Set_Size_Info (Base, Standard_Integer);
1966 Set_RM_Size (Base, RM_Size (Standard_Integer));
1967 Set_Small_Value (Base, Ureal_1);
1968 Set_Delta_Value (Base, Ureal_1);
1969 Set_Scalar_Range (Base, Scalar_Range (T));
1970 Set_Parent (Base, Parent (Def));
1972 Check_Restriction (No_Fixed_Point, Def);
1973 end Analyze_Formal_Ordinary_Fixed_Point_Type;
1975 ----------------------------
1976 -- Analyze_Formal_Package --
1977 ----------------------------
1979 procedure Analyze_Formal_Package (N : Node_Id) is
1980 Loc : constant Source_Ptr := Sloc (N);
1981 Pack_Id : constant Entity_Id := Defining_Identifier (N);
1982 Formal : Entity_Id;
1983 Gen_Id : constant Node_Id := Name (N);
1984 Gen_Decl : Node_Id;
1985 Gen_Unit : Entity_Id;
1986 New_N : Node_Id;
1987 Parent_Installed : Boolean := False;
1988 Renaming : Node_Id;
1989 Parent_Instance : Entity_Id;
1990 Renaming_In_Par : Entity_Id;
1991 No_Associations : Boolean := False;
1993 function Build_Local_Package return Node_Id;
1994 -- The formal package is rewritten so that its parameters are replaced
1995 -- with corresponding declarations. For parameters with bona fide
1996 -- associations these declarations are created by Analyze_Associations
1997 -- as for a regular instantiation. For boxed parameters, we preserve
1998 -- the formal declarations and analyze them, in order to introduce
1999 -- entities of the right kind in the environment of the formal.
2001 -------------------------
2002 -- Build_Local_Package --
2003 -------------------------
2005 function Build_Local_Package return Node_Id is
2006 Decls : List_Id;
2007 Pack_Decl : Node_Id;
2009 begin
2010 -- Within the formal, the name of the generic package is a renaming
2011 -- of the formal (as for a regular instantiation).
2013 Pack_Decl :=
2014 Make_Package_Declaration (Loc,
2015 Specification =>
2016 Copy_Generic_Node
2017 (Specification (Original_Node (Gen_Decl)),
2018 Empty, Instantiating => True));
2020 Renaming := Make_Package_Renaming_Declaration (Loc,
2021 Defining_Unit_Name =>
2022 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2023 Name => New_Occurrence_Of (Formal, Loc));
2025 if Nkind (Gen_Id) = N_Identifier
2026 and then Chars (Gen_Id) = Chars (Pack_Id)
2027 then
2028 Error_Msg_NE
2029 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2030 end if;
2032 -- If the formal is declared with a box, or with an others choice,
2033 -- create corresponding declarations for all entities in the formal
2034 -- part, so that names with the proper types are available in the
2035 -- specification of the formal package.
2037 -- On the other hand, if there are no associations, then all the
2038 -- formals must have defaults, and this will be checked by the
2039 -- call to Analyze_Associations.
2041 if Box_Present (N)
2042 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2043 then
2044 declare
2045 Formal_Decl : Node_Id;
2047 begin
2048 -- TBA : for a formal package, need to recurse ???
2050 Decls := New_List;
2051 Formal_Decl :=
2052 First
2053 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2054 while Present (Formal_Decl) loop
2055 Append_To
2056 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2057 Next (Formal_Decl);
2058 end loop;
2059 end;
2061 -- If generic associations are present, use Analyze_Associations to
2062 -- create the proper renaming declarations.
2064 else
2065 declare
2066 Act_Tree : constant Node_Id :=
2067 Copy_Generic_Node
2068 (Original_Node (Gen_Decl), Empty,
2069 Instantiating => True);
2071 begin
2072 Generic_Renamings.Set_Last (0);
2073 Generic_Renamings_HTable.Reset;
2074 Instantiation_Node := N;
2076 Decls :=
2077 Analyze_Associations
2078 (Original_Node (N),
2079 Generic_Formal_Declarations (Act_Tree),
2080 Generic_Formal_Declarations (Gen_Decl));
2081 end;
2082 end if;
2084 Append (Renaming, To => Decls);
2086 -- Add generated declarations ahead of local declarations in
2087 -- the package.
2089 if No (Visible_Declarations (Specification (Pack_Decl))) then
2090 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2091 else
2092 Insert_List_Before
2093 (First (Visible_Declarations (Specification (Pack_Decl))),
2094 Decls);
2095 end if;
2097 return Pack_Decl;
2098 end Build_Local_Package;
2100 -- Start of processing for Analyze_Formal_Package
2102 begin
2103 Text_IO_Kludge (Gen_Id);
2105 Init_Env;
2106 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2107 Gen_Unit := Entity (Gen_Id);
2109 -- Check for a formal package that is a package renaming
2111 if Present (Renamed_Object (Gen_Unit)) then
2112 Gen_Unit := Renamed_Object (Gen_Unit);
2113 end if;
2115 if Ekind (Gen_Unit) /= E_Generic_Package then
2116 Error_Msg_N ("expect generic package name", Gen_Id);
2117 Restore_Env;
2118 return;
2120 elsif Gen_Unit = Current_Scope then
2121 Error_Msg_N
2122 ("generic package cannot be used as a formal package of itself",
2123 Gen_Id);
2124 Restore_Env;
2125 return;
2127 elsif In_Open_Scopes (Gen_Unit) then
2128 if Is_Compilation_Unit (Gen_Unit)
2129 and then Is_Child_Unit (Current_Scope)
2130 then
2131 -- Special-case the error when the formal is a parent, and
2132 -- continue analysis to minimize cascaded errors.
2134 Error_Msg_N
2135 ("generic parent cannot be used as formal package "
2136 & "of a child unit",
2137 Gen_Id);
2139 else
2140 Error_Msg_N
2141 ("generic package cannot be used as a formal package "
2142 & "within itself",
2143 Gen_Id);
2144 Restore_Env;
2145 return;
2146 end if;
2147 end if;
2149 if Box_Present (N)
2150 or else No (Generic_Associations (N))
2151 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2152 then
2153 No_Associations := True;
2154 end if;
2156 -- If there are no generic associations, the generic parameters appear
2157 -- as local entities and are instantiated like them. We copy the generic
2158 -- package declaration as if it were an instantiation, and analyze it
2159 -- like a regular package, except that we treat the formals as
2160 -- additional visible components.
2162 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2164 if In_Extended_Main_Source_Unit (N) then
2165 Set_Is_Instantiated (Gen_Unit);
2166 Generate_Reference (Gen_Unit, N);
2167 end if;
2169 Formal := New_Copy (Pack_Id);
2170 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2172 begin
2173 -- Make local generic without formals. The formals will be replaced
2174 -- with internal declarations.
2176 New_N := Build_Local_Package;
2178 -- If there are errors in the parameter list, Analyze_Associations
2179 -- raises Instantiation_Error. Patch the declaration to prevent
2180 -- further exception propagation.
2182 exception
2183 when Instantiation_Error =>
2185 Enter_Name (Formal);
2186 Set_Ekind (Formal, E_Variable);
2187 Set_Etype (Formal, Any_Type);
2189 if Parent_Installed then
2190 Remove_Parent;
2191 end if;
2193 return;
2194 end;
2196 Rewrite (N, New_N);
2197 Set_Defining_Unit_Name (Specification (New_N), Formal);
2198 Set_Generic_Parent (Specification (N), Gen_Unit);
2199 Set_Instance_Env (Gen_Unit, Formal);
2200 Set_Is_Generic_Instance (Formal);
2202 Enter_Name (Formal);
2203 Set_Ekind (Formal, E_Package);
2204 Set_Etype (Formal, Standard_Void_Type);
2205 Set_Inner_Instances (Formal, New_Elmt_List);
2206 Push_Scope (Formal);
2208 if Is_Child_Unit (Gen_Unit)
2209 and then Parent_Installed
2210 then
2211 -- Similarly, we have to make the name of the formal visible in the
2212 -- parent instance, to resolve properly fully qualified names that
2213 -- may appear in the generic unit. The parent instance has been
2214 -- placed on the scope stack ahead of the current scope.
2216 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2218 Renaming_In_Par :=
2219 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2220 Set_Ekind (Renaming_In_Par, E_Package);
2221 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2222 Set_Scope (Renaming_In_Par, Parent_Instance);
2223 Set_Parent (Renaming_In_Par, Parent (Formal));
2224 Set_Renamed_Object (Renaming_In_Par, Formal);
2225 Append_Entity (Renaming_In_Par, Parent_Instance);
2226 end if;
2228 Analyze (Specification (N));
2230 -- The formals for which associations are provided are not visible
2231 -- outside of the formal package. The others are still declared by a
2232 -- formal parameter declaration.
2234 if not No_Associations then
2235 declare
2236 E : Entity_Id;
2238 begin
2239 E := First_Entity (Formal);
2240 while Present (E) loop
2241 exit when Ekind (E) = E_Package
2242 and then Renamed_Entity (E) = Formal;
2244 if not Is_Generic_Formal (E) then
2245 Set_Is_Hidden (E);
2246 end if;
2248 Next_Entity (E);
2249 end loop;
2250 end;
2251 end if;
2253 End_Package_Scope (Formal);
2255 if Parent_Installed then
2256 Remove_Parent;
2257 end if;
2259 Restore_Env;
2261 -- Inside the generic unit, the formal package is a regular package, but
2262 -- no body is needed for it. Note that after instantiation, the defining
2263 -- unit name we need is in the new tree and not in the original (see
2264 -- Package_Instantiation). A generic formal package is an instance, and
2265 -- can be used as an actual for an inner instance.
2267 Set_Has_Completion (Formal, True);
2269 -- Add semantic information to the original defining identifier.
2270 -- for ASIS use.
2272 Set_Ekind (Pack_Id, E_Package);
2273 Set_Etype (Pack_Id, Standard_Void_Type);
2274 Set_Scope (Pack_Id, Scope (Formal));
2275 Set_Has_Completion (Pack_Id, True);
2276 end Analyze_Formal_Package;
2278 ---------------------------------
2279 -- Analyze_Formal_Private_Type --
2280 ---------------------------------
2282 procedure Analyze_Formal_Private_Type
2283 (N : Node_Id;
2284 T : Entity_Id;
2285 Def : Node_Id)
2287 begin
2288 New_Private_Type (N, T, Def);
2290 -- Set the size to an arbitrary but legal value
2292 Set_Size_Info (T, Standard_Integer);
2293 Set_RM_Size (T, RM_Size (Standard_Integer));
2294 end Analyze_Formal_Private_Type;
2296 ----------------------------------------
2297 -- Analyze_Formal_Signed_Integer_Type --
2298 ----------------------------------------
2300 procedure Analyze_Formal_Signed_Integer_Type
2301 (T : Entity_Id;
2302 Def : Node_Id)
2304 Base : constant Entity_Id :=
2305 New_Internal_Entity
2306 (E_Signed_Integer_Type, Current_Scope, Sloc (Def), 'G');
2308 begin
2309 Enter_Name (T);
2311 Set_Ekind (T, E_Signed_Integer_Subtype);
2312 Set_Etype (T, Base);
2313 Set_Size_Info (T, Standard_Integer);
2314 Set_RM_Size (T, RM_Size (Standard_Integer));
2315 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2316 Set_Is_Constrained (T);
2318 Set_Is_Generic_Type (Base);
2319 Set_Size_Info (Base, Standard_Integer);
2320 Set_RM_Size (Base, RM_Size (Standard_Integer));
2321 Set_Etype (Base, Base);
2322 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2323 Set_Parent (Base, Parent (Def));
2324 end Analyze_Formal_Signed_Integer_Type;
2326 -------------------------------
2327 -- Analyze_Formal_Subprogram --
2328 -------------------------------
2330 procedure Analyze_Formal_Subprogram (N : Node_Id) is
2331 Spec : constant Node_Id := Specification (N);
2332 Def : constant Node_Id := Default_Name (N);
2333 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2334 Subp : Entity_Id;
2336 begin
2337 if Nam = Error then
2338 return;
2339 end if;
2341 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2342 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2343 return;
2344 end if;
2346 Analyze_Subprogram_Declaration (N);
2347 Set_Is_Formal_Subprogram (Nam);
2348 Set_Has_Completion (Nam);
2350 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2351 Set_Is_Abstract_Subprogram (Nam);
2352 Set_Is_Dispatching_Operation (Nam);
2354 declare
2355 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2356 begin
2357 if No (Ctrl_Type) then
2358 Error_Msg_N
2359 ("abstract formal subprogram must have a controlling type",
2361 else
2362 Check_Controlling_Formals (Ctrl_Type, Nam);
2363 end if;
2364 end;
2365 end if;
2367 -- Default name is resolved at the point of instantiation
2369 if Box_Present (N) then
2370 null;
2372 -- Else default is bound at the point of generic declaration
2374 elsif Present (Def) then
2375 if Nkind (Def) = N_Operator_Symbol then
2376 Find_Direct_Name (Def);
2378 elsif Nkind (Def) /= N_Attribute_Reference then
2379 Analyze (Def);
2381 else
2382 -- For an attribute reference, analyze the prefix and verify
2383 -- that it has the proper profile for the subprogram.
2385 Analyze (Prefix (Def));
2386 Valid_Default_Attribute (Nam, Def);
2387 return;
2388 end if;
2390 -- Default name may be overloaded, in which case the interpretation
2391 -- with the correct profile must be selected, as for a renaming.
2392 -- If the definition is an indexed component, it must denote a
2393 -- member of an entry family. If it is a selected component, it
2394 -- can be a protected operation.
2396 if Etype (Def) = Any_Type then
2397 return;
2399 elsif Nkind (Def) = N_Selected_Component then
2400 if not Is_Overloadable (Entity (Selector_Name (Def))) then
2401 Error_Msg_N ("expect valid subprogram name as default", Def);
2402 end if;
2404 elsif Nkind (Def) = N_Indexed_Component then
2405 if Is_Entity_Name (Prefix (Def)) then
2406 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
2407 Error_Msg_N ("expect valid subprogram name as default", Def);
2408 end if;
2410 elsif Nkind (Prefix (Def)) = N_Selected_Component then
2411 if Ekind (Entity (Selector_Name (Prefix (Def))))
2412 /= E_Entry_Family
2413 then
2414 Error_Msg_N ("expect valid subprogram name as default", Def);
2415 end if;
2417 else
2418 Error_Msg_N ("expect valid subprogram name as default", Def);
2419 return;
2420 end if;
2422 elsif Nkind (Def) = N_Character_Literal then
2424 -- Needs some type checks: subprogram should be parameterless???
2426 Resolve (Def, (Etype (Nam)));
2428 elsif not Is_Entity_Name (Def)
2429 or else not Is_Overloadable (Entity (Def))
2430 then
2431 Error_Msg_N ("expect valid subprogram name as default", Def);
2432 return;
2434 elsif not Is_Overloaded (Def) then
2435 Subp := Entity (Def);
2437 if Subp = Nam then
2438 Error_Msg_N ("premature usage of formal subprogram", Def);
2440 elsif not Entity_Matches_Spec (Subp, Nam) then
2441 Error_Msg_N ("no visible entity matches specification", Def);
2442 end if;
2444 -- More than one interpretation, so disambiguate as for a renaming
2446 else
2447 declare
2448 I : Interp_Index;
2449 I1 : Interp_Index := 0;
2450 It : Interp;
2451 It1 : Interp;
2453 begin
2454 Subp := Any_Id;
2455 Get_First_Interp (Def, I, It);
2456 while Present (It.Nam) loop
2457 if Entity_Matches_Spec (It.Nam, Nam) then
2458 if Subp /= Any_Id then
2459 It1 := Disambiguate (Def, I1, I, Etype (Subp));
2461 if It1 = No_Interp then
2462 Error_Msg_N ("ambiguous default subprogram", Def);
2463 else
2464 Subp := It1.Nam;
2465 end if;
2467 exit;
2469 else
2470 I1 := I;
2471 Subp := It.Nam;
2472 end if;
2473 end if;
2475 Get_Next_Interp (I, It);
2476 end loop;
2477 end;
2479 if Subp /= Any_Id then
2480 Set_Entity (Def, Subp);
2482 if Subp = Nam then
2483 Error_Msg_N ("premature usage of formal subprogram", Def);
2485 elsif Ekind (Subp) /= E_Operator then
2486 Check_Mode_Conformant (Subp, Nam);
2487 end if;
2489 else
2490 Error_Msg_N ("no visible subprogram matches specification", N);
2491 end if;
2492 end if;
2493 end if;
2494 end Analyze_Formal_Subprogram;
2496 -------------------------------------
2497 -- Analyze_Formal_Type_Declaration --
2498 -------------------------------------
2500 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
2501 Def : constant Node_Id := Formal_Type_Definition (N);
2502 T : Entity_Id;
2504 begin
2505 T := Defining_Identifier (N);
2507 if Present (Discriminant_Specifications (N))
2508 and then Nkind (Def) /= N_Formal_Private_Type_Definition
2509 then
2510 Error_Msg_N
2511 ("discriminants not allowed for this formal type", T);
2512 end if;
2514 -- Enter the new name, and branch to specific routine
2516 case Nkind (Def) is
2517 when N_Formal_Private_Type_Definition =>
2518 Analyze_Formal_Private_Type (N, T, Def);
2520 when N_Formal_Derived_Type_Definition =>
2521 Analyze_Formal_Derived_Type (N, T, Def);
2523 when N_Formal_Discrete_Type_Definition =>
2524 Analyze_Formal_Discrete_Type (T, Def);
2526 when N_Formal_Signed_Integer_Type_Definition =>
2527 Analyze_Formal_Signed_Integer_Type (T, Def);
2529 when N_Formal_Modular_Type_Definition =>
2530 Analyze_Formal_Modular_Type (T, Def);
2532 when N_Formal_Floating_Point_Definition =>
2533 Analyze_Formal_Floating_Type (T, Def);
2535 when N_Formal_Ordinary_Fixed_Point_Definition =>
2536 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
2538 when N_Formal_Decimal_Fixed_Point_Definition =>
2539 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
2541 when N_Array_Type_Definition =>
2542 Analyze_Formal_Array_Type (T, Def);
2544 when N_Access_To_Object_Definition |
2545 N_Access_Function_Definition |
2546 N_Access_Procedure_Definition =>
2547 Analyze_Generic_Access_Type (T, Def);
2549 -- Ada 2005: a interface declaration is encoded as an abstract
2550 -- record declaration or a abstract type derivation.
2552 when N_Record_Definition =>
2553 Analyze_Formal_Interface_Type (N, T, Def);
2555 when N_Derived_Type_Definition =>
2556 Analyze_Formal_Derived_Interface_Type (N, T, Def);
2558 when N_Error =>
2559 null;
2561 when others =>
2562 raise Program_Error;
2564 end case;
2566 Set_Is_Generic_Type (T);
2567 end Analyze_Formal_Type_Declaration;
2569 ------------------------------------
2570 -- Analyze_Function_Instantiation --
2571 ------------------------------------
2573 procedure Analyze_Function_Instantiation (N : Node_Id) is
2574 begin
2575 Analyze_Subprogram_Instantiation (N, E_Function);
2576 end Analyze_Function_Instantiation;
2578 ---------------------------------
2579 -- Analyze_Generic_Access_Type --
2580 ---------------------------------
2582 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
2583 begin
2584 Enter_Name (T);
2586 if Nkind (Def) = N_Access_To_Object_Definition then
2587 Access_Type_Declaration (T, Def);
2589 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
2590 and then No (Full_View (Designated_Type (T)))
2591 and then not Is_Generic_Type (Designated_Type (T))
2592 then
2593 Error_Msg_N ("premature usage of incomplete type", Def);
2595 elsif Is_Internal (Designated_Type (T)) then
2596 Error_Msg_N
2597 ("only a subtype mark is allowed in a formal", Def);
2598 end if;
2600 else
2601 Access_Subprogram_Declaration (T, Def);
2602 end if;
2603 end Analyze_Generic_Access_Type;
2605 ---------------------------------
2606 -- Analyze_Generic_Formal_Part --
2607 ---------------------------------
2609 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
2610 Gen_Parm_Decl : Node_Id;
2612 begin
2613 -- The generic formals are processed in the scope of the generic unit,
2614 -- where they are immediately visible. The scope is installed by the
2615 -- caller.
2617 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
2619 while Present (Gen_Parm_Decl) loop
2620 Analyze (Gen_Parm_Decl);
2621 Next (Gen_Parm_Decl);
2622 end loop;
2624 Generate_Reference_To_Generic_Formals (Current_Scope);
2625 end Analyze_Generic_Formal_Part;
2627 ------------------------------------------
2628 -- Analyze_Generic_Package_Declaration --
2629 ------------------------------------------
2631 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
2632 Loc : constant Source_Ptr := Sloc (N);
2633 Id : Entity_Id;
2634 New_N : Node_Id;
2635 Save_Parent : Node_Id;
2636 Renaming : Node_Id;
2637 Decls : constant List_Id :=
2638 Visible_Declarations (Specification (N));
2639 Decl : Node_Id;
2641 begin
2642 -- We introduce a renaming of the enclosing package, to have a usable
2643 -- entity as the prefix of an expanded name for a local entity of the
2644 -- form Par.P.Q, where P is the generic package. This is because a local
2645 -- entity named P may hide it, so that the usual visibility rules in
2646 -- the instance will not resolve properly.
2648 Renaming :=
2649 Make_Package_Renaming_Declaration (Loc,
2650 Defining_Unit_Name =>
2651 Make_Defining_Identifier (Loc,
2652 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
2653 Name => Make_Identifier (Loc, Chars (Defining_Entity (N))));
2655 if Present (Decls) then
2656 Decl := First (Decls);
2657 while Present (Decl)
2658 and then Nkind (Decl) = N_Pragma
2659 loop
2660 Next (Decl);
2661 end loop;
2663 if Present (Decl) then
2664 Insert_Before (Decl, Renaming);
2665 else
2666 Append (Renaming, Visible_Declarations (Specification (N)));
2667 end if;
2669 else
2670 Set_Visible_Declarations (Specification (N), New_List (Renaming));
2671 end if;
2673 -- Create copy of generic unit, and save for instantiation. If the unit
2674 -- is a child unit, do not copy the specifications for the parent, which
2675 -- are not part of the generic tree.
2677 Save_Parent := Parent_Spec (N);
2678 Set_Parent_Spec (N, Empty);
2680 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2681 Set_Parent_Spec (New_N, Save_Parent);
2682 Rewrite (N, New_N);
2683 Id := Defining_Entity (N);
2684 Generate_Definition (Id);
2686 -- Expansion is not applied to generic units
2688 Start_Generic;
2690 Enter_Name (Id);
2691 Set_Ekind (Id, E_Generic_Package);
2692 Set_Etype (Id, Standard_Void_Type);
2693 Push_Scope (Id);
2694 Enter_Generic_Scope (Id);
2695 Set_Inner_Instances (Id, New_Elmt_List);
2697 Set_Categorization_From_Pragmas (N);
2698 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2700 -- Link the declaration of the generic homonym in the generic copy to
2701 -- the package it renames, so that it is always resolved properly.
2703 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
2704 Set_Entity (Associated_Node (Name (Renaming)), Id);
2706 -- For a library unit, we have reconstructed the entity for the unit,
2707 -- and must reset it in the library tables.
2709 if Nkind (Parent (N)) = N_Compilation_Unit then
2710 Set_Cunit_Entity (Current_Sem_Unit, Id);
2711 end if;
2713 Analyze_Generic_Formal_Part (N);
2715 -- After processing the generic formals, analysis proceeds as for a
2716 -- non-generic package.
2718 Analyze (Specification (N));
2720 Validate_Categorization_Dependency (N, Id);
2722 End_Generic;
2724 End_Package_Scope (Id);
2725 Exit_Generic_Scope (Id);
2727 if Nkind (Parent (N)) /= N_Compilation_Unit then
2728 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
2729 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
2730 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
2732 else
2733 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2734 Validate_RT_RAT_Component (N);
2736 -- If this is a spec without a body, check that generic parameters
2737 -- are referenced.
2739 if not Body_Required (Parent (N)) then
2740 Check_References (Id);
2741 end if;
2742 end if;
2743 end Analyze_Generic_Package_Declaration;
2745 --------------------------------------------
2746 -- Analyze_Generic_Subprogram_Declaration --
2747 --------------------------------------------
2749 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
2750 Spec : Node_Id;
2751 Id : Entity_Id;
2752 Formals : List_Id;
2753 New_N : Node_Id;
2754 Result_Type : Entity_Id;
2755 Save_Parent : Node_Id;
2756 Typ : Entity_Id;
2758 begin
2759 -- Create copy of generic unit, and save for instantiation. If the unit
2760 -- is a child unit, do not copy the specifications for the parent, which
2761 -- are not part of the generic tree.
2763 Save_Parent := Parent_Spec (N);
2764 Set_Parent_Spec (N, Empty);
2766 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2767 Set_Parent_Spec (New_N, Save_Parent);
2768 Rewrite (N, New_N);
2770 Spec := Specification (N);
2771 Id := Defining_Entity (Spec);
2772 Generate_Definition (Id);
2774 if Nkind (Id) = N_Defining_Operator_Symbol then
2775 Error_Msg_N
2776 ("operator symbol not allowed for generic subprogram", Id);
2777 end if;
2779 Start_Generic;
2781 Enter_Name (Id);
2783 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
2784 Push_Scope (Id);
2785 Enter_Generic_Scope (Id);
2786 Set_Inner_Instances (Id, New_Elmt_List);
2787 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2789 Analyze_Generic_Formal_Part (N);
2791 Formals := Parameter_Specifications (Spec);
2793 if Present (Formals) then
2794 Process_Formals (Formals, Spec);
2795 end if;
2797 if Nkind (Spec) = N_Function_Specification then
2798 Set_Ekind (Id, E_Generic_Function);
2800 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
2801 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
2802 Set_Etype (Id, Result_Type);
2803 else
2804 Find_Type (Result_Definition (Spec));
2805 Typ := Entity (Result_Definition (Spec));
2807 -- If a null exclusion is imposed on the result type, then create
2808 -- a null-excluding itype (an access subtype) and use it as the
2809 -- function's Etype.
2811 if Is_Access_Type (Typ)
2812 and then Null_Exclusion_Present (Spec)
2813 then
2814 Set_Etype (Id,
2815 Create_Null_Excluding_Itype
2816 (T => Typ,
2817 Related_Nod => Spec,
2818 Scope_Id => Defining_Unit_Name (Spec)));
2819 else
2820 Set_Etype (Id, Typ);
2821 end if;
2822 end if;
2824 else
2825 Set_Ekind (Id, E_Generic_Procedure);
2826 Set_Etype (Id, Standard_Void_Type);
2827 end if;
2829 -- For a library unit, we have reconstructed the entity for the unit,
2830 -- and must reset it in the library tables. We also make sure that
2831 -- Body_Required is set properly in the original compilation unit node.
2833 if Nkind (Parent (N)) = N_Compilation_Unit then
2834 Set_Cunit_Entity (Current_Sem_Unit, Id);
2835 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2836 end if;
2838 Set_Categorization_From_Pragmas (N);
2839 Validate_Categorization_Dependency (N, Id);
2841 Save_Global_References (Original_Node (N));
2843 End_Generic;
2844 End_Scope;
2845 Exit_Generic_Scope (Id);
2846 Generate_Reference_To_Formals (Id);
2847 end Analyze_Generic_Subprogram_Declaration;
2849 -----------------------------------
2850 -- Analyze_Package_Instantiation --
2851 -----------------------------------
2853 procedure Analyze_Package_Instantiation (N : Node_Id) is
2854 Loc : constant Source_Ptr := Sloc (N);
2855 Gen_Id : constant Node_Id := Name (N);
2857 Act_Decl : Node_Id;
2858 Act_Decl_Name : Node_Id;
2859 Act_Decl_Id : Entity_Id;
2860 Act_Spec : Node_Id;
2861 Act_Tree : Node_Id;
2863 Gen_Decl : Node_Id;
2864 Gen_Unit : Entity_Id;
2866 Is_Actual_Pack : constant Boolean :=
2867 Is_Internal (Defining_Entity (N));
2869 Env_Installed : Boolean := False;
2870 Parent_Installed : Boolean := False;
2871 Renaming_List : List_Id;
2872 Unit_Renaming : Node_Id;
2873 Needs_Body : Boolean;
2874 Inline_Now : Boolean := False;
2876 procedure Delay_Descriptors (E : Entity_Id);
2877 -- Delay generation of subprogram descriptors for given entity
2879 function Might_Inline_Subp return Boolean;
2880 -- If inlining is active and the generic contains inlined subprograms,
2881 -- we instantiate the body. This may cause superfluous instantiations,
2882 -- but it is simpler than detecting the need for the body at the point
2883 -- of inlining, when the context of the instance is not available.
2885 -----------------------
2886 -- Delay_Descriptors --
2887 -----------------------
2889 procedure Delay_Descriptors (E : Entity_Id) is
2890 begin
2891 if not Delay_Subprogram_Descriptors (E) then
2892 Set_Delay_Subprogram_Descriptors (E);
2893 Pending_Descriptor.Append (E);
2894 end if;
2895 end Delay_Descriptors;
2897 -----------------------
2898 -- Might_Inline_Subp --
2899 -----------------------
2901 function Might_Inline_Subp return Boolean is
2902 E : Entity_Id;
2904 begin
2905 if not Inline_Processing_Required then
2906 return False;
2908 else
2909 E := First_Entity (Gen_Unit);
2910 while Present (E) loop
2911 if Is_Subprogram (E)
2912 and then Is_Inlined (E)
2913 then
2914 return True;
2915 end if;
2917 Next_Entity (E);
2918 end loop;
2919 end if;
2921 return False;
2922 end Might_Inline_Subp;
2924 -- Start of processing for Analyze_Package_Instantiation
2926 begin
2927 -- Very first thing: apply the special kludge for Text_IO processing
2928 -- in case we are instantiating one of the children of [Wide_]Text_IO.
2930 Text_IO_Kludge (Name (N));
2932 -- Make node global for error reporting
2934 Instantiation_Node := N;
2936 -- Case of instantiation of a generic package
2938 if Nkind (N) = N_Package_Instantiation then
2939 Act_Decl_Id := New_Copy (Defining_Entity (N));
2940 Set_Comes_From_Source (Act_Decl_Id, True);
2942 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
2943 Act_Decl_Name :=
2944 Make_Defining_Program_Unit_Name (Loc,
2945 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
2946 Defining_Identifier => Act_Decl_Id);
2947 else
2948 Act_Decl_Name := Act_Decl_Id;
2949 end if;
2951 -- Case of instantiation of a formal package
2953 else
2954 Act_Decl_Id := Defining_Identifier (N);
2955 Act_Decl_Name := Act_Decl_Id;
2956 end if;
2958 Generate_Definition (Act_Decl_Id);
2959 Preanalyze_Actuals (N);
2961 Init_Env;
2962 Env_Installed := True;
2964 -- Reset renaming map for formal types. The mapping is established
2965 -- when analyzing the generic associations, but some mappings are
2966 -- inherited from formal packages of parent units, and these are
2967 -- constructed when the parents are installed.
2969 Generic_Renamings.Set_Last (0);
2970 Generic_Renamings_HTable.Reset;
2972 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2973 Gen_Unit := Entity (Gen_Id);
2975 -- Verify that it is the name of a generic package
2977 -- A visibility glitch: if the instance is a child unit and the generic
2978 -- is the generic unit of a parent instance (i.e. both the parent and
2979 -- the child units are instances of the same package) the name now
2980 -- denotes the renaming within the parent, not the intended generic
2981 -- unit. See if there is a homonym that is the desired generic. The
2982 -- renaming declaration must be visible inside the instance of the
2983 -- child, but not when analyzing the name in the instantiation itself.
2985 if Ekind (Gen_Unit) = E_Package
2986 and then Present (Renamed_Entity (Gen_Unit))
2987 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
2988 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
2989 and then Present (Homonym (Gen_Unit))
2990 then
2991 Gen_Unit := Homonym (Gen_Unit);
2992 end if;
2994 if Etype (Gen_Unit) = Any_Type then
2995 Restore_Env;
2996 return;
2998 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3000 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3002 if From_With_Type (Gen_Unit) then
3003 Error_Msg_N
3004 ("cannot instantiate a limited withed package", Gen_Id);
3005 else
3006 Error_Msg_N
3007 ("expect name of generic package in instantiation", Gen_Id);
3008 end if;
3010 Restore_Env;
3011 return;
3012 end if;
3014 if In_Extended_Main_Source_Unit (N) then
3015 Set_Is_Instantiated (Gen_Unit);
3016 Generate_Reference (Gen_Unit, N);
3018 if Present (Renamed_Object (Gen_Unit)) then
3019 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3020 Generate_Reference (Renamed_Object (Gen_Unit), N);
3021 end if;
3022 end if;
3024 if Nkind (Gen_Id) = N_Identifier
3025 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3026 then
3027 Error_Msg_NE
3028 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3030 elsif Nkind (Gen_Id) = N_Expanded_Name
3031 and then Is_Child_Unit (Gen_Unit)
3032 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3033 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3034 then
3035 Error_Msg_N
3036 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3037 end if;
3039 Set_Entity (Gen_Id, Gen_Unit);
3041 -- If generic is a renaming, get original generic unit
3043 if Present (Renamed_Object (Gen_Unit))
3044 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3045 then
3046 Gen_Unit := Renamed_Object (Gen_Unit);
3047 end if;
3049 -- Verify that there are no circular instantiations
3051 if In_Open_Scopes (Gen_Unit) then
3052 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3053 Restore_Env;
3054 return;
3056 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3057 Error_Msg_Node_2 := Current_Scope;
3058 Error_Msg_NE
3059 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3060 Circularity_Detected := True;
3061 Restore_Env;
3062 return;
3064 else
3065 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3067 -- Initialize renamings map, for error checking, and the list that
3068 -- holds private entities whose views have changed between generic
3069 -- definition and instantiation. If this is the instance created to
3070 -- validate an actual package, the instantiation environment is that
3071 -- of the enclosing instance.
3073 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3075 -- Copy original generic tree, to produce text for instantiation
3077 Act_Tree :=
3078 Copy_Generic_Node
3079 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3081 Act_Spec := Specification (Act_Tree);
3083 -- If this is the instance created to validate an actual package,
3084 -- only the formals matter, do not examine the package spec itself.
3086 if Is_Actual_Pack then
3087 Set_Visible_Declarations (Act_Spec, New_List);
3088 Set_Private_Declarations (Act_Spec, New_List);
3089 end if;
3091 Renaming_List :=
3092 Analyze_Associations
3094 Generic_Formal_Declarations (Act_Tree),
3095 Generic_Formal_Declarations (Gen_Decl));
3097 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3098 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3099 Set_Is_Generic_Instance (Act_Decl_Id);
3101 Set_Generic_Parent (Act_Spec, Gen_Unit);
3103 -- References to the generic in its own declaration or its body are
3104 -- references to the instance. Add a renaming declaration for the
3105 -- generic unit itself. This declaration, as well as the renaming
3106 -- declarations for the generic formals, must remain private to the
3107 -- unit: the formals, because this is the language semantics, and
3108 -- the unit because its use is an artifact of the implementation.
3110 Unit_Renaming :=
3111 Make_Package_Renaming_Declaration (Loc,
3112 Defining_Unit_Name =>
3113 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3114 Name => New_Reference_To (Act_Decl_Id, Loc));
3116 Append (Unit_Renaming, Renaming_List);
3118 -- The renaming declarations are the first local declarations of
3119 -- the new unit.
3121 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3122 Insert_List_Before
3123 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3124 else
3125 Set_Visible_Declarations (Act_Spec, Renaming_List);
3126 end if;
3128 Act_Decl :=
3129 Make_Package_Declaration (Loc,
3130 Specification => Act_Spec);
3132 -- Save the instantiation node, for subsequent instantiation of the
3133 -- body, if there is one and we are generating code for the current
3134 -- unit. Mark the unit as having a body, to avoid a premature error
3135 -- message.
3137 -- We instantiate the body if we are generating code, if we are
3138 -- generating cross-reference information, or if we are building
3139 -- trees for ASIS use.
3141 declare
3142 Enclosing_Body_Present : Boolean := False;
3143 -- If the generic unit is not a compilation unit, then a body may
3144 -- be present in its parent even if none is required. We create a
3145 -- tentative pending instantiation for the body, which will be
3146 -- discarded if none is actually present.
3148 Scop : Entity_Id;
3150 begin
3151 if Scope (Gen_Unit) /= Standard_Standard
3152 and then not Is_Child_Unit (Gen_Unit)
3153 then
3154 Scop := Scope (Gen_Unit);
3156 while Present (Scop)
3157 and then Scop /= Standard_Standard
3158 loop
3159 if Unit_Requires_Body (Scop) then
3160 Enclosing_Body_Present := True;
3161 exit;
3163 elsif In_Open_Scopes (Scop)
3164 and then In_Package_Body (Scop)
3165 then
3166 Enclosing_Body_Present := True;
3167 exit;
3168 end if;
3170 exit when Is_Compilation_Unit (Scop);
3171 Scop := Scope (Scop);
3172 end loop;
3173 end if;
3175 -- If front-end inlining is enabled, and this is a unit for which
3176 -- code will be generated, we instantiate the body at once.
3178 -- This is done if the instance is not the main unit, and if the
3179 -- generic is not a child unit of another generic, to avoid scope
3180 -- problems and the reinstallation of parent instances.
3182 if Expander_Active
3183 and then (not Is_Child_Unit (Gen_Unit)
3184 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3185 and then Might_Inline_Subp
3186 and then not Is_Actual_Pack
3187 then
3188 if Front_End_Inlining
3189 and then (Is_In_Main_Unit (N)
3190 or else In_Main_Context (Current_Scope))
3191 and then Nkind (Parent (N)) /= N_Compilation_Unit
3192 then
3193 Inline_Now := True;
3195 -- In configurable_run_time mode we force the inlining of
3196 -- predefined subprograms marked Inline_Always, to minimize
3197 -- the use of the run-time library.
3199 elsif Is_Predefined_File_Name
3200 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3201 and then Configurable_Run_Time_Mode
3202 and then Nkind (Parent (N)) /= N_Compilation_Unit
3203 then
3204 Inline_Now := True;
3205 end if;
3207 -- If the current scope is itself an instance within a child
3208 -- unit, there will be duplications in the scope stack, and the
3209 -- unstacking mechanism in Inline_Instance_Body will fail.
3210 -- This loses some rare cases of optimization, and might be
3211 -- improved some day, if we can find a proper abstraction for
3212 -- "the complete compilation context" that can be saved and
3213 -- restored. ???
3215 if Is_Generic_Instance (Current_Scope) then
3216 declare
3217 Curr_Unit : constant Entity_Id :=
3218 Cunit_Entity (Current_Sem_Unit);
3219 begin
3220 if Curr_Unit /= Current_Scope
3221 and then Is_Child_Unit (Curr_Unit)
3222 then
3223 Inline_Now := False;
3224 end if;
3225 end;
3226 end if;
3227 end if;
3229 Needs_Body :=
3230 (Unit_Requires_Body (Gen_Unit)
3231 or else Enclosing_Body_Present
3232 or else Present (Corresponding_Body (Gen_Decl)))
3233 and then (Is_In_Main_Unit (N)
3234 or else Might_Inline_Subp)
3235 and then not Is_Actual_Pack
3236 and then not Inline_Now
3237 and then (Operating_Mode = Generate_Code
3238 or else (Operating_Mode = Check_Semantics
3239 and then ASIS_Mode));
3241 -- If front_end_inlining is enabled, do not instantiate body if
3242 -- within a generic context.
3244 if (Front_End_Inlining
3245 and then not Expander_Active)
3246 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
3247 then
3248 Needs_Body := False;
3249 end if;
3251 -- If the current context is generic, and the package being
3252 -- instantiated is declared within a formal package, there is no
3253 -- body to instantiate until the enclosing generic is instantiated
3254 -- and there is an actual for the formal package. If the formal
3255 -- package has parameters, we build a regular package instance for
3256 -- it, that precedes the original formal package declaration.
3258 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
3259 declare
3260 Decl : constant Node_Id :=
3261 Original_Node
3262 (Unit_Declaration_Node (Scope (Gen_Unit)));
3263 begin
3264 if Nkind (Decl) = N_Formal_Package_Declaration
3265 or else (Nkind (Decl) = N_Package_Declaration
3266 and then Is_List_Member (Decl)
3267 and then Present (Next (Decl))
3268 and then
3269 Nkind (Next (Decl)) =
3270 N_Formal_Package_Declaration)
3271 then
3272 Needs_Body := False;
3273 end if;
3274 end;
3275 end if;
3276 end;
3278 -- If we are generating the calling stubs from the instantiation of
3279 -- a generic RCI package, we will not use the body of the generic
3280 -- package.
3282 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
3283 and then Is_Compilation_Unit (Defining_Entity (N))
3284 then
3285 Needs_Body := False;
3286 end if;
3288 if Needs_Body then
3290 -- Here is a defence against a ludicrous number of instantiations
3291 -- caused by a circular set of instantiation attempts.
3293 if Pending_Instantiations.Last >
3294 Hostparm.Max_Instantiations
3295 then
3296 Error_Msg_N ("too many instantiations", N);
3297 raise Unrecoverable_Error;
3298 end if;
3300 -- Indicate that the enclosing scopes contain an instantiation,
3301 -- and that cleanup actions should be delayed until after the
3302 -- instance body is expanded.
3304 Check_Forward_Instantiation (Gen_Decl);
3305 if Nkind (N) = N_Package_Instantiation then
3306 declare
3307 Enclosing_Master : Entity_Id;
3309 begin
3310 -- Loop to search enclosing masters
3312 Enclosing_Master := Current_Scope;
3313 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
3314 if Ekind (Enclosing_Master) = E_Package then
3315 if Is_Compilation_Unit (Enclosing_Master) then
3316 if In_Package_Body (Enclosing_Master) then
3317 Delay_Descriptors
3318 (Body_Entity (Enclosing_Master));
3319 else
3320 Delay_Descriptors
3321 (Enclosing_Master);
3322 end if;
3324 exit Scope_Loop;
3326 else
3327 Enclosing_Master := Scope (Enclosing_Master);
3328 end if;
3330 elsif Ekind (Enclosing_Master) = E_Generic_Package then
3331 Enclosing_Master := Scope (Enclosing_Master);
3333 elsif Is_Generic_Subprogram (Enclosing_Master)
3334 or else Ekind (Enclosing_Master) = E_Void
3335 then
3336 -- Cleanup actions will eventually be performed on the
3337 -- enclosing instance, if any. Enclosing scope is void
3338 -- in the formal part of a generic subprogram.
3340 exit Scope_Loop;
3342 else
3343 if Ekind (Enclosing_Master) = E_Entry
3344 and then
3345 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
3346 then
3347 if not Expander_Active then
3348 exit Scope_Loop;
3349 else
3350 Enclosing_Master :=
3351 Protected_Body_Subprogram (Enclosing_Master);
3352 end if;
3353 end if;
3355 Set_Delay_Cleanups (Enclosing_Master);
3357 while Ekind (Enclosing_Master) = E_Block loop
3358 Enclosing_Master := Scope (Enclosing_Master);
3359 end loop;
3361 if Is_Subprogram (Enclosing_Master) then
3362 Delay_Descriptors (Enclosing_Master);
3364 elsif Is_Task_Type (Enclosing_Master) then
3365 declare
3366 TBP : constant Node_Id :=
3367 Get_Task_Body_Procedure
3368 (Enclosing_Master);
3369 begin
3370 if Present (TBP) then
3371 Delay_Descriptors (TBP);
3372 Set_Delay_Cleanups (TBP);
3373 end if;
3374 end;
3375 end if;
3377 exit Scope_Loop;
3378 end if;
3379 end loop Scope_Loop;
3380 end;
3382 -- Make entry in table
3384 Pending_Instantiations.Append
3385 ((Inst_Node => N,
3386 Act_Decl => Act_Decl,
3387 Expander_Status => Expander_Active,
3388 Current_Sem_Unit => Current_Sem_Unit,
3389 Scope_Suppress => Scope_Suppress,
3390 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top));
3391 end if;
3392 end if;
3394 Set_Categorization_From_Pragmas (Act_Decl);
3396 if Parent_Installed then
3397 Hide_Current_Scope;
3398 end if;
3400 Set_Instance_Spec (N, Act_Decl);
3402 -- If not a compilation unit, insert the package declaration before
3403 -- the original instantiation node.
3405 if Nkind (Parent (N)) /= N_Compilation_Unit then
3406 Mark_Rewrite_Insertion (Act_Decl);
3407 Insert_Before (N, Act_Decl);
3408 Analyze (Act_Decl);
3410 -- For an instantiation that is a compilation unit, place declaration
3411 -- on current node so context is complete for analysis (including
3412 -- nested instantiations). If this is the main unit, the declaration
3413 -- eventually replaces the instantiation node. If the instance body
3414 -- is created later, it replaces the instance node, and the
3415 -- declaration is attached to it (see
3416 -- Build_Instance_Compilation_Unit_Nodes).
3418 else
3419 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
3421 -- The entity for the current unit is the newly created one,
3422 -- and all semantic information is attached to it.
3424 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
3426 -- If this is the main unit, replace the main entity as well
3428 if Current_Sem_Unit = Main_Unit then
3429 Main_Unit_Entity := Act_Decl_Id;
3430 end if;
3431 end if;
3433 Set_Unit (Parent (N), Act_Decl);
3434 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
3435 Set_Package_Instantiation (Act_Decl_Id, N);
3436 Analyze (Act_Decl);
3437 Set_Unit (Parent (N), N);
3438 Set_Body_Required (Parent (N), False);
3440 -- We never need elaboration checks on instantiations, since by
3441 -- definition, the body instantiation is elaborated at the same
3442 -- time as the spec instantiation.
3444 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
3445 Set_Kill_Elaboration_Checks (Act_Decl_Id);
3446 end if;
3448 Check_Elab_Instantiation (N);
3450 if ABE_Is_Certain (N) and then Needs_Body then
3451 Pending_Instantiations.Decrement_Last;
3452 end if;
3454 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
3456 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
3457 First_Private_Entity (Act_Decl_Id));
3459 -- If the instantiation will receive a body, the unit will be
3460 -- transformed into a package body, and receive its own elaboration
3461 -- entity. Otherwise, the nature of the unit is now a package
3462 -- declaration.
3464 if Nkind (Parent (N)) = N_Compilation_Unit
3465 and then not Needs_Body
3466 then
3467 Rewrite (N, Act_Decl);
3468 end if;
3470 if Present (Corresponding_Body (Gen_Decl))
3471 or else Unit_Requires_Body (Gen_Unit)
3472 then
3473 Set_Has_Completion (Act_Decl_Id);
3474 end if;
3476 Check_Formal_Packages (Act_Decl_Id);
3478 Restore_Private_Views (Act_Decl_Id);
3480 Inherit_Context (Gen_Decl, N);
3482 if Parent_Installed then
3483 Remove_Parent;
3484 end if;
3486 Restore_Env;
3487 Env_Installed := False;
3488 end if;
3490 Validate_Categorization_Dependency (N, Act_Decl_Id);
3492 -- There used to be a check here to prevent instantiations in local
3493 -- contexts if the No_Local_Allocators restriction was active. This
3494 -- check was removed by a binding interpretation in AI-95-00130/07,
3495 -- but we retain the code for documentation purposes.
3497 -- if Ekind (Act_Decl_Id) /= E_Void
3498 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
3499 -- then
3500 -- Check_Restriction (No_Local_Allocators, N);
3501 -- end if;
3503 if Inline_Now then
3504 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
3505 end if;
3507 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
3508 -- be used as defining identifiers for a formal package and for the
3509 -- corresponding expanded package.
3511 if Nkind (N) = N_Formal_Package_Declaration then
3512 Act_Decl_Id := New_Copy (Defining_Entity (N));
3513 Set_Comes_From_Source (Act_Decl_Id, True);
3514 Set_Is_Generic_Instance (Act_Decl_Id, False);
3515 Set_Defining_Identifier (N, Act_Decl_Id);
3516 end if;
3518 exception
3519 when Instantiation_Error =>
3520 if Parent_Installed then
3521 Remove_Parent;
3522 end if;
3524 if Env_Installed then
3525 Restore_Env;
3526 end if;
3527 end Analyze_Package_Instantiation;
3529 --------------------------
3530 -- Inline_Instance_Body --
3531 --------------------------
3533 procedure Inline_Instance_Body
3534 (N : Node_Id;
3535 Gen_Unit : Entity_Id;
3536 Act_Decl : Node_Id)
3538 Vis : Boolean;
3539 Gen_Comp : constant Entity_Id :=
3540 Cunit_Entity (Get_Source_Unit (Gen_Unit));
3541 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
3542 Curr_Scope : Entity_Id := Empty;
3543 Curr_Unit : constant Entity_Id :=
3544 Cunit_Entity (Current_Sem_Unit);
3545 Removed : Boolean := False;
3546 Num_Scopes : Int := 0;
3548 Scope_Stack_Depth : constant Int :=
3549 Scope_Stack.Last - Scope_Stack.First + 1;
3551 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
3552 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
3553 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
3554 Num_Inner : Int := 0;
3555 N_Instances : Int := 0;
3556 S : Entity_Id;
3558 begin
3559 -- Case of generic unit defined in another unit. We must remove the
3560 -- complete context of the current unit to install that of the generic.
3562 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
3564 -- Add some comments for the following two loops ???
3566 S := Current_Scope;
3567 while Present (S) and then S /= Standard_Standard loop
3568 loop
3569 Num_Scopes := Num_Scopes + 1;
3571 Use_Clauses (Num_Scopes) :=
3572 (Scope_Stack.Table
3573 (Scope_Stack.Last - Num_Scopes + 1).
3574 First_Use_Clause);
3575 End_Use_Clauses (Use_Clauses (Num_Scopes));
3577 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
3578 or else Scope_Stack.Table
3579 (Scope_Stack.Last - Num_Scopes).Entity
3580 = Scope (S);
3581 end loop;
3583 exit when Is_Generic_Instance (S)
3584 and then (In_Package_Body (S)
3585 or else Ekind (S) = E_Procedure
3586 or else Ekind (S) = E_Function);
3587 S := Scope (S);
3588 end loop;
3590 Vis := Is_Immediately_Visible (Gen_Comp);
3592 -- Find and save all enclosing instances
3594 S := Current_Scope;
3596 while Present (S)
3597 and then S /= Standard_Standard
3598 loop
3599 if Is_Generic_Instance (S) then
3600 N_Instances := N_Instances + 1;
3601 Instances (N_Instances) := S;
3603 exit when In_Package_Body (S);
3604 end if;
3606 S := Scope (S);
3607 end loop;
3609 -- Remove context of current compilation unit, unless we are within a
3610 -- nested package instantiation, in which case the context has been
3611 -- removed previously.
3613 -- If current scope is the body of a child unit, remove context of
3614 -- spec as well. If an enclosing scope is an instance body, the
3615 -- context has already been removed, but the entities in the body
3616 -- must be made invisible as well.
3618 S := Current_Scope;
3620 while Present (S)
3621 and then S /= Standard_Standard
3622 loop
3623 if Is_Generic_Instance (S)
3624 and then (In_Package_Body (S)
3625 or else Ekind (S) = E_Procedure
3626 or else Ekind (S) = E_Function)
3627 then
3628 -- We still have to remove the entities of the enclosing
3629 -- instance from direct visibility.
3631 declare
3632 E : Entity_Id;
3633 begin
3634 E := First_Entity (S);
3635 while Present (E) loop
3636 Set_Is_Immediately_Visible (E, False);
3637 Next_Entity (E);
3638 end loop;
3639 end;
3641 exit;
3642 end if;
3644 if S = Curr_Unit
3645 or else (Ekind (Curr_Unit) = E_Package_Body
3646 and then S = Spec_Entity (Curr_Unit))
3647 or else (Ekind (Curr_Unit) = E_Subprogram_Body
3648 and then S =
3649 Corresponding_Spec
3650 (Unit_Declaration_Node (Curr_Unit)))
3651 then
3652 Removed := True;
3654 -- Remove entities in current scopes from visibility, so that
3655 -- instance body is compiled in a clean environment.
3657 Save_Scope_Stack (Handle_Use => False);
3659 if Is_Child_Unit (S) then
3661 -- Remove child unit from stack, as well as inner scopes.
3662 -- Removing the context of a child unit removes parent units
3663 -- as well.
3665 while Current_Scope /= S loop
3666 Num_Inner := Num_Inner + 1;
3667 Inner_Scopes (Num_Inner) := Current_Scope;
3668 Pop_Scope;
3669 end loop;
3671 Pop_Scope;
3672 Remove_Context (Curr_Comp);
3673 Curr_Scope := S;
3675 else
3676 Remove_Context (Curr_Comp);
3677 end if;
3679 if Ekind (Curr_Unit) = E_Package_Body then
3680 Remove_Context (Library_Unit (Curr_Comp));
3681 end if;
3682 end if;
3684 S := Scope (S);
3685 end loop;
3686 pragma Assert (Num_Inner < Num_Scopes);
3688 Push_Scope (Standard_Standard);
3689 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
3690 Instantiate_Package_Body
3691 (Body_Info =>
3692 ((Inst_Node => N,
3693 Act_Decl => Act_Decl,
3694 Expander_Status => Expander_Active,
3695 Current_Sem_Unit => Current_Sem_Unit,
3696 Scope_Suppress => Scope_Suppress,
3697 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top)),
3698 Inlined_Body => True);
3700 Pop_Scope;
3702 -- Restore context
3704 Set_Is_Immediately_Visible (Gen_Comp, Vis);
3706 -- Reset Generic_Instance flag so that use clauses can be installed
3707 -- in the proper order. (See Use_One_Package for effect of enclosing
3708 -- instances on processing of use clauses).
3710 for J in 1 .. N_Instances loop
3711 Set_Is_Generic_Instance (Instances (J), False);
3712 end loop;
3714 if Removed then
3715 Install_Context (Curr_Comp);
3717 if Present (Curr_Scope)
3718 and then Is_Child_Unit (Curr_Scope)
3719 then
3720 Push_Scope (Curr_Scope);
3721 Set_Is_Immediately_Visible (Curr_Scope);
3723 -- Finally, restore inner scopes as well
3725 for J in reverse 1 .. Num_Inner loop
3726 Push_Scope (Inner_Scopes (J));
3727 end loop;
3728 end if;
3730 Restore_Scope_Stack (Handle_Use => False);
3732 if Present (Curr_Scope)
3733 and then
3734 (In_Private_Part (Curr_Scope)
3735 or else In_Package_Body (Curr_Scope))
3736 then
3737 -- Install private declaration of ancestor units, which are
3738 -- currently available. Restore_Scope_Stack and Install_Context
3739 -- only install the visible part of parents.
3741 declare
3742 Par : Entity_Id;
3743 begin
3744 Par := Scope (Curr_Scope);
3745 while (Present (Par))
3746 and then Par /= Standard_Standard
3747 loop
3748 Install_Private_Declarations (Par);
3749 Par := Scope (Par);
3750 end loop;
3751 end;
3752 end if;
3753 end if;
3755 -- Restore use clauses. For a child unit, use clauses in the parents
3756 -- are restored when installing the context, so only those in inner
3757 -- scopes (and those local to the child unit itself) need to be
3758 -- installed explicitly.
3760 if Is_Child_Unit (Curr_Unit)
3761 and then Removed
3762 then
3763 for J in reverse 1 .. Num_Inner + 1 loop
3764 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3765 Use_Clauses (J);
3766 Install_Use_Clauses (Use_Clauses (J));
3767 end loop;
3769 else
3770 for J in reverse 1 .. Num_Scopes loop
3771 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3772 Use_Clauses (J);
3773 Install_Use_Clauses (Use_Clauses (J));
3774 end loop;
3775 end if;
3777 -- Restore status of instances. If one of them is a body, make
3778 -- its local entities visible again.
3780 declare
3781 E : Entity_Id;
3782 Inst : Entity_Id;
3784 begin
3785 for J in 1 .. N_Instances loop
3786 Inst := Instances (J);
3787 Set_Is_Generic_Instance (Inst, True);
3789 if In_Package_Body (Inst)
3790 or else Ekind (S) = E_Procedure
3791 or else Ekind (S) = E_Function
3792 then
3793 E := First_Entity (Instances (J));
3794 while Present (E) loop
3795 Set_Is_Immediately_Visible (E);
3796 Next_Entity (E);
3797 end loop;
3798 end if;
3799 end loop;
3800 end;
3802 -- If generic unit is in current unit, current context is correct
3804 else
3805 Instantiate_Package_Body
3806 (Body_Info =>
3807 ((Inst_Node => N,
3808 Act_Decl => Act_Decl,
3809 Expander_Status => Expander_Active,
3810 Current_Sem_Unit => Current_Sem_Unit,
3811 Scope_Suppress => Scope_Suppress,
3812 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top)),
3813 Inlined_Body => True);
3814 end if;
3815 end Inline_Instance_Body;
3817 -------------------------------------
3818 -- Analyze_Procedure_Instantiation --
3819 -------------------------------------
3821 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
3822 begin
3823 Analyze_Subprogram_Instantiation (N, E_Procedure);
3824 end Analyze_Procedure_Instantiation;
3826 -----------------------------------
3827 -- Need_Subprogram_Instance_Body --
3828 -----------------------------------
3830 function Need_Subprogram_Instance_Body
3831 (N : Node_Id;
3832 Subp : Entity_Id) return Boolean
3834 begin
3835 if (Is_In_Main_Unit (N)
3836 or else Is_Inlined (Subp)
3837 or else Is_Inlined (Alias (Subp)))
3838 and then (Operating_Mode = Generate_Code
3839 or else (Operating_Mode = Check_Semantics
3840 and then ASIS_Mode))
3841 and then (Expander_Active or else ASIS_Mode)
3842 and then not ABE_Is_Certain (N)
3843 and then not Is_Eliminated (Subp)
3844 then
3845 Pending_Instantiations.Append
3846 ((Inst_Node => N,
3847 Act_Decl => Unit_Declaration_Node (Subp),
3848 Expander_Status => Expander_Active,
3849 Current_Sem_Unit => Current_Sem_Unit,
3850 Scope_Suppress => Scope_Suppress,
3851 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top));
3852 return True;
3853 else
3854 return False;
3855 end if;
3856 end Need_Subprogram_Instance_Body;
3858 --------------------------------------
3859 -- Analyze_Subprogram_Instantiation --
3860 --------------------------------------
3862 procedure Analyze_Subprogram_Instantiation
3863 (N : Node_Id;
3864 K : Entity_Kind)
3866 Loc : constant Source_Ptr := Sloc (N);
3867 Gen_Id : constant Node_Id := Name (N);
3869 Anon_Id : constant Entity_Id :=
3870 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
3871 Chars => New_External_Name
3872 (Chars (Defining_Entity (N)), 'R'));
3874 Act_Decl_Id : Entity_Id;
3875 Act_Decl : Node_Id;
3876 Act_Spec : Node_Id;
3877 Act_Tree : Node_Id;
3879 Env_Installed : Boolean := False;
3880 Gen_Unit : Entity_Id;
3881 Gen_Decl : Node_Id;
3882 Pack_Id : Entity_Id;
3883 Parent_Installed : Boolean := False;
3884 Renaming_List : List_Id;
3886 procedure Analyze_Instance_And_Renamings;
3887 -- The instance must be analyzed in a context that includes the mappings
3888 -- of generic parameters into actuals. We create a package declaration
3889 -- for this purpose, and a subprogram with an internal name within the
3890 -- package. The subprogram instance is simply an alias for the internal
3891 -- subprogram, declared in the current scope.
3893 ------------------------------------
3894 -- Analyze_Instance_And_Renamings --
3895 ------------------------------------
3897 procedure Analyze_Instance_And_Renamings is
3898 Def_Ent : constant Entity_Id := Defining_Entity (N);
3899 Pack_Decl : Node_Id;
3901 begin
3902 if Nkind (Parent (N)) = N_Compilation_Unit then
3904 -- For the case of a compilation unit, the container package has
3905 -- the same name as the instantiation, to insure that the binder
3906 -- calls the elaboration procedure with the right name. Copy the
3907 -- entity of the instance, which may have compilation level flags
3908 -- (e.g. Is_Child_Unit) set.
3910 Pack_Id := New_Copy (Def_Ent);
3912 else
3913 -- Otherwise we use the name of the instantiation concatenated
3914 -- with its source position to ensure uniqueness if there are
3915 -- several instantiations with the same name.
3917 Pack_Id :=
3918 Make_Defining_Identifier (Loc,
3919 Chars => New_External_Name
3920 (Related_Id => Chars (Def_Ent),
3921 Suffix => "GP",
3922 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
3923 end if;
3925 Pack_Decl := Make_Package_Declaration (Loc,
3926 Specification => Make_Package_Specification (Loc,
3927 Defining_Unit_Name => Pack_Id,
3928 Visible_Declarations => Renaming_List,
3929 End_Label => Empty));
3931 Set_Instance_Spec (N, Pack_Decl);
3932 Set_Is_Generic_Instance (Pack_Id);
3933 Set_Debug_Info_Needed (Pack_Id);
3935 -- Case of not a compilation unit
3937 if Nkind (Parent (N)) /= N_Compilation_Unit then
3938 Mark_Rewrite_Insertion (Pack_Decl);
3939 Insert_Before (N, Pack_Decl);
3940 Set_Has_Completion (Pack_Id);
3942 -- Case of an instantiation that is a compilation unit
3944 -- Place declaration on current node so context is complete for
3945 -- analysis (including nested instantiations), and for use in a
3946 -- context_clause (see Analyze_With_Clause).
3948 else
3949 Set_Unit (Parent (N), Pack_Decl);
3950 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
3951 end if;
3953 Analyze (Pack_Decl);
3954 Check_Formal_Packages (Pack_Id);
3955 Set_Is_Generic_Instance (Pack_Id, False);
3957 -- Body of the enclosing package is supplied when instantiating the
3958 -- subprogram body, after semantic analysis is completed.
3960 if Nkind (Parent (N)) = N_Compilation_Unit then
3962 -- Remove package itself from visibility, so it does not
3963 -- conflict with subprogram.
3965 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
3967 -- Set name and scope of internal subprogram so that the proper
3968 -- external name will be generated. The proper scope is the scope
3969 -- of the wrapper package. We need to generate debugging info for
3970 -- the internal subprogram, so set flag accordingly.
3972 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
3973 Set_Scope (Anon_Id, Scope (Pack_Id));
3975 -- Mark wrapper package as referenced, to avoid spurious warnings
3976 -- if the instantiation appears in various with_ clauses of
3977 -- subunits of the main unit.
3979 Set_Referenced (Pack_Id);
3980 end if;
3982 Set_Is_Generic_Instance (Anon_Id);
3983 Set_Debug_Info_Needed (Anon_Id);
3984 Act_Decl_Id := New_Copy (Anon_Id);
3986 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3987 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
3988 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
3989 Set_Comes_From_Source (Act_Decl_Id, True);
3991 -- The signature may involve types that are not frozen yet, but the
3992 -- subprogram will be frozen at the point the wrapper package is
3993 -- frozen, so it does not need its own freeze node. In fact, if one
3994 -- is created, it might conflict with the freezing actions from the
3995 -- wrapper package.
3997 Set_Has_Delayed_Freeze (Anon_Id, False);
3999 -- If the instance is a child unit, mark the Id accordingly. Mark
4000 -- the anonymous entity as well, which is the real subprogram and
4001 -- which is used when the instance appears in a context clause.
4003 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
4004 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
4005 New_Overloaded_Entity (Act_Decl_Id);
4006 Check_Eliminated (Act_Decl_Id);
4008 -- In compilation unit case, kill elaboration checks on the
4009 -- instantiation, since they are never needed -- the body is
4010 -- instantiated at the same point as the spec.
4012 if Nkind (Parent (N)) = N_Compilation_Unit then
4013 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4014 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4015 Set_Is_Compilation_Unit (Anon_Id);
4017 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
4018 end if;
4020 -- The instance is not a freezing point for the new subprogram
4022 Set_Is_Frozen (Act_Decl_Id, False);
4024 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
4025 Valid_Operator_Definition (Act_Decl_Id);
4026 end if;
4028 Set_Alias (Act_Decl_Id, Anon_Id);
4029 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4030 Set_Has_Completion (Act_Decl_Id);
4031 Set_Related_Instance (Pack_Id, Act_Decl_Id);
4033 if Nkind (Parent (N)) = N_Compilation_Unit then
4034 Set_Body_Required (Parent (N), False);
4035 end if;
4036 end Analyze_Instance_And_Renamings;
4038 -- Start of processing for Analyze_Subprogram_Instantiation
4040 begin
4041 -- Very first thing: apply the special kludge for Text_IO processing
4042 -- in case we are instantiating one of the children of [Wide_]Text_IO.
4043 -- Of course such an instantiation is bogus (these are packages, not
4044 -- subprograms), but we get a better error message if we do this.
4046 Text_IO_Kludge (Gen_Id);
4048 -- Make node global for error reporting
4050 Instantiation_Node := N;
4051 Preanalyze_Actuals (N);
4053 Init_Env;
4054 Env_Installed := True;
4055 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4056 Gen_Unit := Entity (Gen_Id);
4058 Generate_Reference (Gen_Unit, Gen_Id);
4060 if Nkind (Gen_Id) = N_Identifier
4061 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4062 then
4063 Error_Msg_NE
4064 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4065 end if;
4067 if Etype (Gen_Unit) = Any_Type then
4068 Restore_Env;
4069 return;
4070 end if;
4072 -- Verify that it is a generic subprogram of the right kind, and that
4073 -- it does not lead to a circular instantiation.
4075 if Ekind (Gen_Unit) /= E_Generic_Procedure
4076 and then Ekind (Gen_Unit) /= E_Generic_Function
4077 then
4078 Error_Msg_N ("expect generic subprogram in instantiation", Gen_Id);
4080 elsif In_Open_Scopes (Gen_Unit) then
4081 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4083 elsif K = E_Procedure
4084 and then Ekind (Gen_Unit) /= E_Generic_Procedure
4085 then
4086 if Ekind (Gen_Unit) = E_Generic_Function then
4087 Error_Msg_N
4088 ("cannot instantiate generic function as procedure", Gen_Id);
4089 else
4090 Error_Msg_N
4091 ("expect name of generic procedure in instantiation", Gen_Id);
4092 end if;
4094 elsif K = E_Function
4095 and then Ekind (Gen_Unit) /= E_Generic_Function
4096 then
4097 if Ekind (Gen_Unit) = E_Generic_Procedure then
4098 Error_Msg_N
4099 ("cannot instantiate generic procedure as function", Gen_Id);
4100 else
4101 Error_Msg_N
4102 ("expect name of generic function in instantiation", Gen_Id);
4103 end if;
4105 else
4106 Set_Entity (Gen_Id, Gen_Unit);
4107 Set_Is_Instantiated (Gen_Unit);
4109 if In_Extended_Main_Source_Unit (N) then
4110 Generate_Reference (Gen_Unit, N);
4111 end if;
4113 -- If renaming, get original unit
4115 if Present (Renamed_Object (Gen_Unit))
4116 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
4117 or else
4118 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
4119 then
4120 Gen_Unit := Renamed_Object (Gen_Unit);
4121 Set_Is_Instantiated (Gen_Unit);
4122 Generate_Reference (Gen_Unit, N);
4123 end if;
4125 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4126 Error_Msg_Node_2 := Current_Scope;
4127 Error_Msg_NE
4128 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4129 Circularity_Detected := True;
4130 return;
4131 end if;
4133 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4135 -- Initialize renamings map, for error checking
4137 Generic_Renamings.Set_Last (0);
4138 Generic_Renamings_HTable.Reset;
4140 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
4142 -- Copy original generic tree, to produce text for instantiation
4144 Act_Tree :=
4145 Copy_Generic_Node
4146 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4148 -- Inherit overriding indicator from instance node
4150 Act_Spec := Specification (Act_Tree);
4151 Set_Must_Override (Act_Spec, Must_Override (N));
4152 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
4154 Renaming_List :=
4155 Analyze_Associations
4157 Generic_Formal_Declarations (Act_Tree),
4158 Generic_Formal_Declarations (Gen_Decl));
4160 -- The subprogram itself cannot contain a nested instance, so the
4161 -- current parent is left empty.
4163 Set_Instance_Env (Gen_Unit, Empty);
4165 -- Build the subprogram declaration, which does not appear in the
4166 -- generic template, and give it a sloc consistent with that of the
4167 -- template.
4169 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
4170 Set_Generic_Parent (Act_Spec, Gen_Unit);
4171 Act_Decl :=
4172 Make_Subprogram_Declaration (Sloc (Act_Spec),
4173 Specification => Act_Spec);
4175 Set_Categorization_From_Pragmas (Act_Decl);
4177 if Parent_Installed then
4178 Hide_Current_Scope;
4179 end if;
4181 Append (Act_Decl, Renaming_List);
4182 Analyze_Instance_And_Renamings;
4184 -- If the generic is marked Import (Intrinsic), then so is the
4185 -- instance. This indicates that there is no body to instantiate. If
4186 -- generic is marked inline, so it the instance, and the anonymous
4187 -- subprogram it renames. If inlined, or else if inlining is enabled
4188 -- for the compilation, we generate the instance body even if it is
4189 -- not within the main unit.
4191 -- Any other pragmas might also be inherited ???
4193 if Is_Intrinsic_Subprogram (Gen_Unit) then
4194 Set_Is_Intrinsic_Subprogram (Anon_Id);
4195 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
4197 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
4198 Validate_Unchecked_Conversion (N, Act_Decl_Id);
4199 end if;
4200 end if;
4202 Generate_Definition (Act_Decl_Id);
4204 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
4205 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
4207 if not Is_Intrinsic_Subprogram (Gen_Unit) then
4208 Check_Elab_Instantiation (N);
4209 end if;
4211 if Is_Dispatching_Operation (Act_Decl_Id)
4212 and then Ada_Version >= Ada_05
4213 then
4214 declare
4215 Formal : Entity_Id;
4217 begin
4218 Formal := First_Formal (Act_Decl_Id);
4219 while Present (Formal) loop
4220 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
4221 and then Is_Controlling_Formal (Formal)
4222 and then not Can_Never_Be_Null (Formal)
4223 then
4224 Error_Msg_NE ("access parameter& is controlling,",
4225 N, Formal);
4226 Error_Msg_NE ("\corresponding parameter of & must be"
4227 & " explicitly null-excluding", N, Gen_Id);
4228 end if;
4230 Next_Formal (Formal);
4231 end loop;
4232 end;
4233 end if;
4235 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4237 -- Subject to change, pending on if other pragmas are inherited ???
4239 Validate_Categorization_Dependency (N, Act_Decl_Id);
4241 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
4242 Inherit_Context (Gen_Decl, N);
4244 Restore_Private_Views (Pack_Id, False);
4246 -- If the context requires a full instantiation, mark node for
4247 -- subsequent construction of the body.
4249 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
4251 Check_Forward_Instantiation (Gen_Decl);
4253 -- The wrapper package is always delayed, because it does not
4254 -- constitute a freeze point, but to insure that the freeze
4255 -- node is placed properly, it is created directly when
4256 -- instantiating the body (otherwise the freeze node might
4257 -- appear to early for nested instantiations).
4259 elsif Nkind (Parent (N)) = N_Compilation_Unit then
4261 -- For ASIS purposes, indicate that the wrapper package has
4262 -- replaced the instantiation node.
4264 Rewrite (N, Unit (Parent (N)));
4265 Set_Unit (Parent (N), N);
4266 end if;
4268 elsif Nkind (Parent (N)) = N_Compilation_Unit then
4270 -- Replace instance node for library-level instantiations of
4271 -- intrinsic subprograms, for ASIS use.
4273 Rewrite (N, Unit (Parent (N)));
4274 Set_Unit (Parent (N), N);
4275 end if;
4277 if Parent_Installed then
4278 Remove_Parent;
4279 end if;
4281 Restore_Env;
4282 Env_Installed := False;
4283 Generic_Renamings.Set_Last (0);
4284 Generic_Renamings_HTable.Reset;
4285 end if;
4287 exception
4288 when Instantiation_Error =>
4289 if Parent_Installed then
4290 Remove_Parent;
4291 end if;
4293 if Env_Installed then
4294 Restore_Env;
4295 end if;
4296 end Analyze_Subprogram_Instantiation;
4298 -------------------------
4299 -- Get_Associated_Node --
4300 -------------------------
4302 function Get_Associated_Node (N : Node_Id) return Node_Id is
4303 Assoc : Node_Id;
4305 begin
4306 Assoc := Associated_Node (N);
4308 if Nkind (Assoc) /= Nkind (N) then
4309 return Assoc;
4311 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
4312 return Assoc;
4314 else
4315 -- If the node is part of an inner generic, it may itself have been
4316 -- remapped into a further generic copy. Associated_Node is otherwise
4317 -- used for the entity of the node, and will be of a different node
4318 -- kind, or else N has been rewritten as a literal or function call.
4320 while Present (Associated_Node (Assoc))
4321 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
4322 loop
4323 Assoc := Associated_Node (Assoc);
4324 end loop;
4326 -- Follow and additional link in case the final node was rewritten.
4327 -- This can only happen with nested generic units.
4329 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
4330 and then Present (Associated_Node (Assoc))
4331 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
4332 N_Explicit_Dereference,
4333 N_Integer_Literal,
4334 N_Real_Literal,
4335 N_String_Literal))
4336 then
4337 Assoc := Associated_Node (Assoc);
4338 end if;
4340 return Assoc;
4341 end if;
4342 end Get_Associated_Node;
4344 -------------------------------------------
4345 -- Build_Instance_Compilation_Unit_Nodes --
4346 -------------------------------------------
4348 procedure Build_Instance_Compilation_Unit_Nodes
4349 (N : Node_Id;
4350 Act_Body : Node_Id;
4351 Act_Decl : Node_Id)
4353 Decl_Cunit : Node_Id;
4354 Body_Cunit : Node_Id;
4355 Citem : Node_Id;
4356 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
4357 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
4359 begin
4360 -- A new compilation unit node is built for the instance declaration
4362 Decl_Cunit :=
4363 Make_Compilation_Unit (Sloc (N),
4364 Context_Items => Empty_List,
4365 Unit => Act_Decl,
4366 Aux_Decls_Node =>
4367 Make_Compilation_Unit_Aux (Sloc (N)));
4369 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4371 -- The new compilation unit is linked to its body, but both share the
4372 -- same file, so we do not set Body_Required on the new unit so as not
4373 -- to create a spurious dependency on a non-existent body in the ali.
4374 -- This simplifies CodePeer unit traversal.
4376 -- We use the original instantiation compilation unit as the resulting
4377 -- compilation unit of the instance, since this is the main unit.
4379 Rewrite (N, Act_Body);
4380 Body_Cunit := Parent (N);
4382 -- The two compilation unit nodes are linked by the Library_Unit field
4384 Set_Library_Unit (Decl_Cunit, Body_Cunit);
4385 Set_Library_Unit (Body_Cunit, Decl_Cunit);
4387 -- Preserve the private nature of the package if needed
4389 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
4391 -- If the instance is not the main unit, its context, categorization
4392 -- and elaboration entity are not relevant to the compilation.
4394 if Body_Cunit /= Cunit (Main_Unit) then
4395 Make_Instance_Unit (Body_Cunit, In_Main => False);
4396 return;
4397 end if;
4399 -- The context clause items on the instantiation, which are now attached
4400 -- to the body compilation unit (since the body overwrote the original
4401 -- instantiation node), semantically belong on the spec, so copy them
4402 -- there. It's harmless to leave them on the body as well. In fact one
4403 -- could argue that they belong in both places.
4405 Citem := First (Context_Items (Body_Cunit));
4406 while Present (Citem) loop
4407 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
4408 Next (Citem);
4409 end loop;
4411 -- Propagate categorization flags on packages, so that they appear in
4412 -- the ali file for the spec of the unit.
4414 if Ekind (New_Main) = E_Package then
4415 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
4416 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
4417 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
4418 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
4419 Set_Is_Remote_Call_Interface
4420 (Old_Main, Is_Remote_Call_Interface (New_Main));
4421 end if;
4423 -- Make entry in Units table, so that binder can generate call to
4424 -- elaboration procedure for body, if any.
4426 Make_Instance_Unit (Body_Cunit, In_Main => True);
4427 Main_Unit_Entity := New_Main;
4428 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
4430 -- Build elaboration entity, since the instance may certainly generate
4431 -- elaboration code requiring a flag for protection.
4433 Build_Elaboration_Entity (Decl_Cunit, New_Main);
4434 end Build_Instance_Compilation_Unit_Nodes;
4436 -----------------------------
4437 -- Check_Access_Definition --
4438 -----------------------------
4440 procedure Check_Access_Definition (N : Node_Id) is
4441 begin
4442 pragma Assert
4443 (Ada_Version >= Ada_05
4444 and then Present (Access_Definition (N)));
4445 null;
4446 end Check_Access_Definition;
4448 -----------------------------------
4449 -- Check_Formal_Package_Instance --
4450 -----------------------------------
4452 -- If the formal has specific parameters, they must match those of the
4453 -- actual. Both of them are instances, and the renaming declarations for
4454 -- their formal parameters appear in the same order in both. The analyzed
4455 -- formal has been analyzed in the context of the current instance.
4457 procedure Check_Formal_Package_Instance
4458 (Formal_Pack : Entity_Id;
4459 Actual_Pack : Entity_Id)
4461 E1 : Entity_Id := First_Entity (Actual_Pack);
4462 E2 : Entity_Id := First_Entity (Formal_Pack);
4464 Expr1 : Node_Id;
4465 Expr2 : Node_Id;
4467 procedure Check_Mismatch (B : Boolean);
4468 -- Common error routine for mismatch between the parameters of the
4469 -- actual instance and those of the formal package.
4471 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
4472 -- The formal may come from a nested formal package, and the actual may
4473 -- have been constant-folded. To determine whether the two denote the
4474 -- same entity we may have to traverse several definitions to recover
4475 -- the ultimate entity that they refer to.
4477 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
4478 -- Similarly, if the formal comes from a nested formal package, the
4479 -- actual may designate the formal through multiple renamings, which
4480 -- have to be followed to determine the original variable in question.
4482 --------------------
4483 -- Check_Mismatch --
4484 --------------------
4486 procedure Check_Mismatch (B : Boolean) is
4487 Kind : constant Node_Kind := Nkind (Parent (E2));
4489 begin
4490 if Kind = N_Formal_Type_Declaration then
4491 return;
4493 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
4494 N_Formal_Package_Declaration)
4495 or else Kind in N_Formal_Subprogram_Declaration
4496 then
4497 null;
4499 elsif B then
4500 Error_Msg_NE
4501 ("actual for & in actual instance does not match formal",
4502 Parent (Actual_Pack), E1);
4503 end if;
4504 end Check_Mismatch;
4506 --------------------------------
4507 -- Same_Instantiated_Constant --
4508 --------------------------------
4510 function Same_Instantiated_Constant
4511 (E1, E2 : Entity_Id) return Boolean
4513 Ent : Entity_Id;
4515 begin
4516 Ent := E2;
4517 while Present (Ent) loop
4518 if E1 = Ent then
4519 return True;
4521 elsif Ekind (Ent) /= E_Constant then
4522 return False;
4524 elsif Is_Entity_Name (Constant_Value (Ent)) then
4525 if Entity (Constant_Value (Ent)) = E1 then
4526 return True;
4527 else
4528 Ent := Entity (Constant_Value (Ent));
4529 end if;
4531 -- The actual may be a constant that has been folded. Recover
4532 -- original name.
4534 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
4535 Ent := Entity (Original_Node (Constant_Value (Ent)));
4536 else
4537 return False;
4538 end if;
4539 end loop;
4541 return False;
4542 end Same_Instantiated_Constant;
4544 --------------------------------
4545 -- Same_Instantiated_Variable --
4546 --------------------------------
4548 function Same_Instantiated_Variable
4549 (E1, E2 : Entity_Id) return Boolean
4551 function Original_Entity (E : Entity_Id) return Entity_Id;
4552 -- Follow chain of renamings to the ultimate ancestor
4554 ---------------------
4555 -- Original_Entity --
4556 ---------------------
4558 function Original_Entity (E : Entity_Id) return Entity_Id is
4559 Orig : Entity_Id;
4561 begin
4562 Orig := E;
4563 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
4564 and then Present (Renamed_Object (Orig))
4565 and then Is_Entity_Name (Renamed_Object (Orig))
4566 loop
4567 Orig := Entity (Renamed_Object (Orig));
4568 end loop;
4570 return Orig;
4571 end Original_Entity;
4573 -- Start of processing for Same_Instantiated_Variable
4575 begin
4576 return Ekind (E1) = Ekind (E2)
4577 and then Original_Entity (E1) = Original_Entity (E2);
4578 end Same_Instantiated_Variable;
4580 -- Start of processing for Check_Formal_Package_Instance
4582 begin
4583 while Present (E1)
4584 and then Present (E2)
4585 loop
4586 exit when Ekind (E1) = E_Package
4587 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
4589 -- If the formal is the renaming of the formal package, this
4590 -- is the end of its formal part, which may occur before the
4591 -- end of the formal part in the actual in the presence of
4592 -- defaulted parameters in the formal package.
4594 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
4595 and then Renamed_Entity (E2) = Scope (E2);
4597 -- The analysis of the actual may generate additional internal
4598 -- entities. If the formal is defaulted, there is no corresponding
4599 -- analysis and the internal entities must be skipped, until we
4600 -- find corresponding entities again.
4602 if Comes_From_Source (E2)
4603 and then not Comes_From_Source (E1)
4604 and then Chars (E1) /= Chars (E2)
4605 then
4606 while Present (E1)
4607 and then Chars (E1) /= Chars (E2)
4608 loop
4609 Next_Entity (E1);
4610 end loop;
4611 end if;
4613 if No (E1) then
4614 return;
4616 -- If the formal entity comes from a formal declaration, it was
4617 -- defaulted in the formal package, and no check is needed on it.
4619 elsif Nkind (Parent (E2)) = N_Formal_Object_Declaration then
4620 goto Next_E;
4622 elsif Is_Type (E1) then
4624 -- Subtypes must statically match. E1, E2 are the local entities
4625 -- that are subtypes of the actuals. Itypes generated for other
4626 -- parameters need not be checked, the check will be performed
4627 -- on the parameters themselves.
4629 -- If E2 is a formal type declaration, it is a defaulted parameter
4630 -- and needs no checking.
4632 if not Is_Itype (E1)
4633 and then not Is_Itype (E2)
4634 then
4635 Check_Mismatch
4636 (not Is_Type (E2)
4637 or else Etype (E1) /= Etype (E2)
4638 or else not Subtypes_Statically_Match (E1, E2));
4639 end if;
4641 elsif Ekind (E1) = E_Constant then
4643 -- IN parameters must denote the same static value, or the same
4644 -- constant, or the literal null.
4646 Expr1 := Expression (Parent (E1));
4648 if Ekind (E2) /= E_Constant then
4649 Check_Mismatch (True);
4650 goto Next_E;
4651 else
4652 Expr2 := Expression (Parent (E2));
4653 end if;
4655 if Is_Static_Expression (Expr1) then
4657 if not Is_Static_Expression (Expr2) then
4658 Check_Mismatch (True);
4660 elsif Is_Discrete_Type (Etype (E1)) then
4661 declare
4662 V1 : constant Uint := Expr_Value (Expr1);
4663 V2 : constant Uint := Expr_Value (Expr2);
4664 begin
4665 Check_Mismatch (V1 /= V2);
4666 end;
4668 elsif Is_Real_Type (Etype (E1)) then
4669 declare
4670 V1 : constant Ureal := Expr_Value_R (Expr1);
4671 V2 : constant Ureal := Expr_Value_R (Expr2);
4672 begin
4673 Check_Mismatch (V1 /= V2);
4674 end;
4676 elsif Is_String_Type (Etype (E1))
4677 and then Nkind (Expr1) = N_String_Literal
4678 then
4679 if Nkind (Expr2) /= N_String_Literal then
4680 Check_Mismatch (True);
4681 else
4682 Check_Mismatch
4683 (not String_Equal (Strval (Expr1), Strval (Expr2)));
4684 end if;
4685 end if;
4687 elsif Is_Entity_Name (Expr1) then
4688 if Is_Entity_Name (Expr2) then
4689 if Entity (Expr1) = Entity (Expr2) then
4690 null;
4691 else
4692 Check_Mismatch
4693 (not Same_Instantiated_Constant
4694 (Entity (Expr1), Entity (Expr2)));
4695 end if;
4696 else
4697 Check_Mismatch (True);
4698 end if;
4700 elsif Is_Entity_Name (Original_Node (Expr1))
4701 and then Is_Entity_Name (Expr2)
4702 and then
4703 Same_Instantiated_Constant
4704 (Entity (Original_Node (Expr1)), Entity (Expr2))
4705 then
4706 null;
4708 elsif Nkind (Expr1) = N_Null then
4709 Check_Mismatch (Nkind (Expr1) /= N_Null);
4711 else
4712 Check_Mismatch (True);
4713 end if;
4715 elsif Ekind (E1) = E_Variable then
4716 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
4718 elsif Ekind (E1) = E_Package then
4719 Check_Mismatch
4720 (Ekind (E1) /= Ekind (E2)
4721 or else Renamed_Object (E1) /= Renamed_Object (E2));
4723 elsif Is_Overloadable (E1) then
4725 -- Verify that the actual subprograms match. Note that actuals
4726 -- that are attributes are rewritten as subprograms. If the
4727 -- subprogram in the formal package is defaulted, no check is
4728 -- needed. Note that this can only happen in Ada 2005 when the
4729 -- formal package can be partially parametrized.
4731 if Nkind (Unit_Declaration_Node (E1)) =
4732 N_Subprogram_Renaming_Declaration
4733 and then From_Default (Unit_Declaration_Node (E1))
4734 then
4735 null;
4737 else
4738 Check_Mismatch
4739 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
4740 end if;
4742 else
4743 raise Program_Error;
4744 end if;
4746 <<Next_E>>
4747 Next_Entity (E1);
4748 Next_Entity (E2);
4749 end loop;
4750 end Check_Formal_Package_Instance;
4752 ---------------------------
4753 -- Check_Formal_Packages --
4754 ---------------------------
4756 procedure Check_Formal_Packages (P_Id : Entity_Id) is
4757 E : Entity_Id;
4758 Formal_P : Entity_Id;
4760 begin
4761 -- Iterate through the declarations in the instance, looking for package
4762 -- renaming declarations that denote instances of formal packages. Stop
4763 -- when we find the renaming of the current package itself. The
4764 -- declaration for a formal package without a box is followed by an
4765 -- internal entity that repeats the instantiation.
4767 E := First_Entity (P_Id);
4768 while Present (E) loop
4769 if Ekind (E) = E_Package then
4770 if Renamed_Object (E) = P_Id then
4771 exit;
4773 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
4774 null;
4776 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
4777 Formal_P := Next_Entity (E);
4778 Check_Formal_Package_Instance (Formal_P, E);
4780 -- After checking, remove the internal validating package. It
4781 -- is only needed for semantic checks, and as it may contain
4782 -- generic formal declarations it should not reach gigi.
4784 Remove (Unit_Declaration_Node (Formal_P));
4785 end if;
4786 end if;
4788 Next_Entity (E);
4789 end loop;
4790 end Check_Formal_Packages;
4792 ---------------------------------
4793 -- Check_Forward_Instantiation --
4794 ---------------------------------
4796 procedure Check_Forward_Instantiation (Decl : Node_Id) is
4797 S : Entity_Id;
4798 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
4800 begin
4801 -- The instantiation appears before the generic body if we are in the
4802 -- scope of the unit containing the generic, either in its spec or in
4803 -- the package body, and before the generic body.
4805 if Ekind (Gen_Comp) = E_Package_Body then
4806 Gen_Comp := Spec_Entity (Gen_Comp);
4807 end if;
4809 if In_Open_Scopes (Gen_Comp)
4810 and then No (Corresponding_Body (Decl))
4811 then
4812 S := Current_Scope;
4814 while Present (S)
4815 and then not Is_Compilation_Unit (S)
4816 and then not Is_Child_Unit (S)
4817 loop
4818 if Ekind (S) = E_Package then
4819 Set_Has_Forward_Instantiation (S);
4820 end if;
4822 S := Scope (S);
4823 end loop;
4824 end if;
4825 end Check_Forward_Instantiation;
4827 ---------------------------
4828 -- Check_Generic_Actuals --
4829 ---------------------------
4831 -- The visibility of the actuals may be different between the point of
4832 -- generic instantiation and the instantiation of the body.
4834 procedure Check_Generic_Actuals
4835 (Instance : Entity_Id;
4836 Is_Formal_Box : Boolean)
4838 E : Entity_Id;
4839 Astype : Entity_Id;
4841 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
4842 -- For a formal that is an array type, the component type is often a
4843 -- previous formal in the same unit. The privacy status of the component
4844 -- type will have been examined earlier in the traversal of the
4845 -- corresponding actuals, and this status should not be modified for the
4846 -- array type itself.
4848 -- To detect this case we have to rescan the list of formals, which
4849 -- is usually short enough to ignore the resulting inefficiency.
4851 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
4852 Prev : Entity_Id;
4853 begin
4854 Prev := First_Entity (Instance);
4855 while Present (Prev) loop
4856 if Is_Type (Prev)
4857 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
4858 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
4859 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
4860 then
4861 return True;
4862 elsif Prev = E then
4863 return False;
4864 else
4865 Next_Entity (Prev);
4866 end if;
4867 end loop;
4868 return False;
4869 end Denotes_Previous_Actual;
4871 -- Start of processing for Check_Generic_Actuals
4873 begin
4874 E := First_Entity (Instance);
4875 while Present (E) loop
4876 if Is_Type (E)
4877 and then Nkind (Parent (E)) = N_Subtype_Declaration
4878 and then Scope (Etype (E)) /= Instance
4879 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
4880 then
4881 if Is_Array_Type (E)
4882 and then Denotes_Previous_Actual (Component_Type (E))
4883 then
4884 null;
4885 else
4886 Check_Private_View (Subtype_Indication (Parent (E)));
4887 end if;
4888 Set_Is_Generic_Actual_Type (E, True);
4889 Set_Is_Hidden (E, False);
4890 Set_Is_Potentially_Use_Visible (E,
4891 In_Use (Instance));
4893 -- We constructed the generic actual type as a subtype of the
4894 -- supplied type. This means that it normally would not inherit
4895 -- subtype specific attributes of the actual, which is wrong for
4896 -- the generic case.
4898 Astype := Ancestor_Subtype (E);
4900 if No (Astype) then
4902 -- This can happen when E is an itype that is the full view of
4903 -- a private type completed, e.g. with a constrained array. In
4904 -- that case, use the first subtype, which will carry size
4905 -- information. The base type itself is unconstrained and will
4906 -- not carry it.
4908 Astype := First_Subtype (E);
4909 end if;
4911 Set_Size_Info (E, (Astype));
4912 Set_RM_Size (E, RM_Size (Astype));
4913 Set_First_Rep_Item (E, First_Rep_Item (Astype));
4915 if Is_Discrete_Or_Fixed_Point_Type (E) then
4916 Set_RM_Size (E, RM_Size (Astype));
4918 -- In nested instances, the base type of an access actual
4919 -- may itself be private, and need to be exchanged.
4921 elsif Is_Access_Type (E)
4922 and then Is_Private_Type (Etype (E))
4923 then
4924 Check_Private_View
4925 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
4926 end if;
4928 elsif Ekind (E) = E_Package then
4930 -- If this is the renaming for the current instance, we're done.
4931 -- Otherwise it is a formal package. If the corresponding formal
4932 -- was declared with a box, the (instantiations of the) generic
4933 -- formal part are also visible. Otherwise, ignore the entity
4934 -- created to validate the actuals.
4936 if Renamed_Object (E) = Instance then
4937 exit;
4939 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
4940 null;
4942 -- The visibility of a formal of an enclosing generic is already
4943 -- correct.
4945 elsif Denotes_Formal_Package (E) then
4946 null;
4948 elsif Present (Associated_Formal_Package (E))
4949 and then not Is_Generic_Formal (E)
4950 then
4951 if Box_Present (Parent (Associated_Formal_Package (E))) then
4952 Check_Generic_Actuals (Renamed_Object (E), True);
4954 else
4955 Check_Generic_Actuals (Renamed_Object (E), False);
4956 end if;
4958 Set_Is_Hidden (E, False);
4959 end if;
4961 -- If this is a subprogram instance (in a wrapper package) the
4962 -- actual is fully visible.
4964 elsif Is_Wrapper_Package (Instance) then
4965 Set_Is_Hidden (E, False);
4967 -- If the formal package is declared with a box, or if the formal
4968 -- parameter is defaulted, it is visible in the body.
4970 elsif Is_Formal_Box
4971 or else Is_Visible_Formal (E)
4972 then
4973 Set_Is_Hidden (E, False);
4974 end if;
4976 Next_Entity (E);
4977 end loop;
4978 end Check_Generic_Actuals;
4980 ------------------------------
4981 -- Check_Generic_Child_Unit --
4982 ------------------------------
4984 procedure Check_Generic_Child_Unit
4985 (Gen_Id : Node_Id;
4986 Parent_Installed : in out Boolean)
4988 Loc : constant Source_Ptr := Sloc (Gen_Id);
4989 Gen_Par : Entity_Id := Empty;
4990 E : Entity_Id;
4991 Inst_Par : Entity_Id;
4992 S : Node_Id;
4994 function Find_Generic_Child
4995 (Scop : Entity_Id;
4996 Id : Node_Id) return Entity_Id;
4997 -- Search generic parent for possible child unit with the given name
4999 function In_Enclosing_Instance return Boolean;
5000 -- Within an instance of the parent, the child unit may be denoted
5001 -- by a simple name, or an abbreviated expanded name. Examine enclosing
5002 -- scopes to locate a possible parent instantiation.
5004 ------------------------
5005 -- Find_Generic_Child --
5006 ------------------------
5008 function Find_Generic_Child
5009 (Scop : Entity_Id;
5010 Id : Node_Id) return Entity_Id
5012 E : Entity_Id;
5014 begin
5015 -- If entity of name is already set, instance has already been
5016 -- resolved, e.g. in an enclosing instantiation.
5018 if Present (Entity (Id)) then
5019 if Scope (Entity (Id)) = Scop then
5020 return Entity (Id);
5021 else
5022 return Empty;
5023 end if;
5025 else
5026 E := First_Entity (Scop);
5027 while Present (E) loop
5028 if Chars (E) = Chars (Id)
5029 and then Is_Child_Unit (E)
5030 then
5031 if Is_Child_Unit (E)
5032 and then not Is_Visible_Child_Unit (E)
5033 then
5034 Error_Msg_NE
5035 ("generic child unit& is not visible", Gen_Id, E);
5036 end if;
5038 Set_Entity (Id, E);
5039 return E;
5040 end if;
5042 Next_Entity (E);
5043 end loop;
5045 return Empty;
5046 end if;
5047 end Find_Generic_Child;
5049 ---------------------------
5050 -- In_Enclosing_Instance --
5051 ---------------------------
5053 function In_Enclosing_Instance return Boolean is
5054 Enclosing_Instance : Node_Id;
5055 Instance_Decl : Node_Id;
5057 begin
5058 -- We do not inline any call that contains instantiations, except
5059 -- for instantiations of Unchecked_Conversion, so if we are within
5060 -- an inlined body the current instance does not require parents.
5062 if In_Inlined_Body then
5063 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
5064 return False;
5065 end if;
5067 -- Loop to check enclosing scopes
5069 Enclosing_Instance := Current_Scope;
5070 while Present (Enclosing_Instance) loop
5071 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
5073 if Ekind (Enclosing_Instance) = E_Package
5074 and then Is_Generic_Instance (Enclosing_Instance)
5075 and then Present
5076 (Generic_Parent (Specification (Instance_Decl)))
5077 then
5078 -- Check whether the generic we are looking for is a child of
5079 -- this instance.
5081 E := Find_Generic_Child
5082 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
5083 exit when Present (E);
5085 else
5086 E := Empty;
5087 end if;
5089 Enclosing_Instance := Scope (Enclosing_Instance);
5090 end loop;
5092 if No (E) then
5094 -- Not a child unit
5096 Analyze (Gen_Id);
5097 return False;
5099 else
5100 Rewrite (Gen_Id,
5101 Make_Expanded_Name (Loc,
5102 Chars => Chars (E),
5103 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
5104 Selector_Name => New_Occurrence_Of (E, Loc)));
5106 Set_Entity (Gen_Id, E);
5107 Set_Etype (Gen_Id, Etype (E));
5108 Parent_Installed := False; -- Already in scope.
5109 return True;
5110 end if;
5111 end In_Enclosing_Instance;
5113 -- Start of processing for Check_Generic_Child_Unit
5115 begin
5116 -- If the name of the generic is given by a selected component, it may
5117 -- be the name of a generic child unit, and the prefix is the name of an
5118 -- instance of the parent, in which case the child unit must be visible.
5119 -- If this instance is not in scope, it must be placed there and removed
5120 -- after instantiation, because what is being instantiated is not the
5121 -- original child, but the corresponding child present in the instance
5122 -- of the parent.
5124 -- If the child is instantiated within the parent, it can be given by
5125 -- a simple name. In this case the instance is already in scope, but
5126 -- the child generic must be recovered from the generic parent as well.
5128 if Nkind (Gen_Id) = N_Selected_Component then
5129 S := Selector_Name (Gen_Id);
5130 Analyze (Prefix (Gen_Id));
5131 Inst_Par := Entity (Prefix (Gen_Id));
5133 if Ekind (Inst_Par) = E_Package
5134 and then Present (Renamed_Object (Inst_Par))
5135 then
5136 Inst_Par := Renamed_Object (Inst_Par);
5137 end if;
5139 if Ekind (Inst_Par) = E_Package then
5140 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
5141 Gen_Par := Generic_Parent (Parent (Inst_Par));
5143 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
5144 and then
5145 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
5146 then
5147 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
5148 end if;
5150 elsif Ekind (Inst_Par) = E_Generic_Package
5151 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
5152 then
5153 -- A formal package may be a real child package, and not the
5154 -- implicit instance within a parent. In this case the child is
5155 -- not visible and has to be retrieved explicitly as well.
5157 Gen_Par := Inst_Par;
5158 end if;
5160 if Present (Gen_Par) then
5162 -- The prefix denotes an instantiation. The entity itself may be a
5163 -- nested generic, or a child unit.
5165 E := Find_Generic_Child (Gen_Par, S);
5167 if Present (E) then
5168 Change_Selected_Component_To_Expanded_Name (Gen_Id);
5169 Set_Entity (Gen_Id, E);
5170 Set_Etype (Gen_Id, Etype (E));
5171 Set_Entity (S, E);
5172 Set_Etype (S, Etype (E));
5174 -- Indicate that this is a reference to the parent
5176 if In_Extended_Main_Source_Unit (Gen_Id) then
5177 Set_Is_Instantiated (Inst_Par);
5178 end if;
5180 -- A common mistake is to replicate the naming scheme of a
5181 -- hierarchy by instantiating a generic child directly, rather
5182 -- than the implicit child in a parent instance:
5184 -- generic .. package Gpar is ..
5185 -- generic .. package Gpar.Child is ..
5186 -- package Par is new Gpar ();
5188 -- with Gpar.Child;
5189 -- package Par.Child is new Gpar.Child ();
5190 -- rather than Par.Child
5192 -- In this case the instantiation is within Par, which is an
5193 -- instance, but Gpar does not denote Par because we are not IN
5194 -- the instance of Gpar, so this is illegal. The test below
5195 -- recognizes this particular case.
5197 if Is_Child_Unit (E)
5198 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
5199 and then (not In_Instance
5200 or else Nkind (Parent (Parent (Gen_Id))) =
5201 N_Compilation_Unit)
5202 then
5203 Error_Msg_N
5204 ("prefix of generic child unit must be instance of parent",
5205 Gen_Id);
5206 end if;
5208 if not In_Open_Scopes (Inst_Par)
5209 and then Nkind (Parent (Gen_Id)) not in
5210 N_Generic_Renaming_Declaration
5211 then
5212 Install_Parent (Inst_Par);
5213 Parent_Installed := True;
5215 elsif In_Open_Scopes (Inst_Par) then
5217 -- If the parent is already installed, install the actuals
5218 -- for its formal packages. This is necessary when the
5219 -- child instance is a child of the parent instance:
5220 -- in this case, the parent is placed on the scope stack
5221 -- but the formal packages are not made visible.
5223 Install_Formal_Packages (Inst_Par);
5224 end if;
5226 else
5227 -- If the generic parent does not contain an entity that
5228 -- corresponds to the selector, the instance doesn't either.
5229 -- Analyzing the node will yield the appropriate error message.
5230 -- If the entity is not a child unit, then it is an inner
5231 -- generic in the parent.
5233 Analyze (Gen_Id);
5234 end if;
5236 else
5237 Analyze (Gen_Id);
5239 if Is_Child_Unit (Entity (Gen_Id))
5240 and then
5241 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
5242 and then not In_Open_Scopes (Inst_Par)
5243 then
5244 Install_Parent (Inst_Par);
5245 Parent_Installed := True;
5246 end if;
5247 end if;
5249 elsif Nkind (Gen_Id) = N_Expanded_Name then
5251 -- Entity already present, analyze prefix, whose meaning may be
5252 -- an instance in the current context. If it is an instance of
5253 -- a relative within another, the proper parent may still have
5254 -- to be installed, if they are not of the same generation.
5256 Analyze (Prefix (Gen_Id));
5258 -- In the unlikely case that a local declaration hides the name
5259 -- of the parent package, locate it on the homonym chain. If the
5260 -- context is an instance of the parent, the renaming entity is
5261 -- flagged as such.
5263 Inst_Par := Entity (Prefix (Gen_Id));
5264 while Present (Inst_Par)
5265 and then not Is_Package_Or_Generic_Package (Inst_Par)
5266 loop
5267 Inst_Par := Homonym (Inst_Par);
5268 end loop;
5270 pragma Assert (Present (Inst_Par));
5271 Set_Entity (Prefix (Gen_Id), Inst_Par);
5273 if In_Enclosing_Instance then
5274 null;
5276 elsif Present (Entity (Gen_Id))
5277 and then Is_Child_Unit (Entity (Gen_Id))
5278 and then not In_Open_Scopes (Inst_Par)
5279 then
5280 Install_Parent (Inst_Par);
5281 Parent_Installed := True;
5282 end if;
5284 elsif In_Enclosing_Instance then
5286 -- The child unit is found in some enclosing scope
5288 null;
5290 else
5291 Analyze (Gen_Id);
5293 -- If this is the renaming of the implicit child in a parent
5294 -- instance, recover the parent name and install it.
5296 if Is_Entity_Name (Gen_Id) then
5297 E := Entity (Gen_Id);
5299 if Is_Generic_Unit (E)
5300 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
5301 and then Is_Child_Unit (Renamed_Object (E))
5302 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
5303 and then Nkind (Name (Parent (E))) = N_Expanded_Name
5304 then
5305 Rewrite (Gen_Id,
5306 New_Copy_Tree (Name (Parent (E))));
5307 Inst_Par := Entity (Prefix (Gen_Id));
5309 if not In_Open_Scopes (Inst_Par) then
5310 Install_Parent (Inst_Par);
5311 Parent_Installed := True;
5312 end if;
5314 -- If it is a child unit of a non-generic parent, it may be
5315 -- use-visible and given by a direct name. Install parent as
5316 -- for other cases.
5318 elsif Is_Generic_Unit (E)
5319 and then Is_Child_Unit (E)
5320 and then
5321 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
5322 and then not Is_Generic_Unit (Scope (E))
5323 then
5324 if not In_Open_Scopes (Scope (E)) then
5325 Install_Parent (Scope (E));
5326 Parent_Installed := True;
5327 end if;
5328 end if;
5329 end if;
5330 end if;
5331 end Check_Generic_Child_Unit;
5333 -----------------------------
5334 -- Check_Hidden_Child_Unit --
5335 -----------------------------
5337 procedure Check_Hidden_Child_Unit
5338 (N : Node_Id;
5339 Gen_Unit : Entity_Id;
5340 Act_Decl_Id : Entity_Id)
5342 Gen_Id : constant Node_Id := Name (N);
5344 begin
5345 if Is_Child_Unit (Gen_Unit)
5346 and then Is_Child_Unit (Act_Decl_Id)
5347 and then Nkind (Gen_Id) = N_Expanded_Name
5348 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
5349 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
5350 then
5351 Error_Msg_Node_2 := Scope (Act_Decl_Id);
5352 Error_Msg_NE
5353 ("generic unit & is implicitly declared in &",
5354 Defining_Unit_Name (N), Gen_Unit);
5355 Error_Msg_N ("\instance must have different name",
5356 Defining_Unit_Name (N));
5357 end if;
5358 end Check_Hidden_Child_Unit;
5360 ------------------------
5361 -- Check_Private_View --
5362 ------------------------
5364 procedure Check_Private_View (N : Node_Id) is
5365 T : constant Entity_Id := Etype (N);
5366 BT : Entity_Id;
5368 begin
5369 -- Exchange views if the type was not private in the generic but is
5370 -- private at the point of instantiation. Do not exchange views if
5371 -- the scope of the type is in scope. This can happen if both generic
5372 -- and instance are sibling units, or if type is defined in a parent.
5373 -- In this case the visibility of the type will be correct for all
5374 -- semantic checks.
5376 if Present (T) then
5377 BT := Base_Type (T);
5379 if Is_Private_Type (T)
5380 and then not Has_Private_View (N)
5381 and then Present (Full_View (T))
5382 and then not In_Open_Scopes (Scope (T))
5383 then
5384 -- In the generic, the full type was visible. Save the private
5385 -- entity, for subsequent exchange.
5387 Switch_View (T);
5389 elsif Has_Private_View (N)
5390 and then not Is_Private_Type (T)
5391 and then not Has_Been_Exchanged (T)
5392 and then Etype (Get_Associated_Node (N)) /= T
5393 then
5394 -- Only the private declaration was visible in the generic. If
5395 -- the type appears in a subtype declaration, the subtype in the
5396 -- instance must have a view compatible with that of its parent,
5397 -- which must be exchanged (see corresponding code in Restore_
5398 -- Private_Views). Otherwise, if the type is defined in a parent
5399 -- unit, leave full visibility within instance, which is safe.
5401 if In_Open_Scopes (Scope (Base_Type (T)))
5402 and then not Is_Private_Type (Base_Type (T))
5403 and then Comes_From_Source (Base_Type (T))
5404 then
5405 null;
5407 elsif Nkind (Parent (N)) = N_Subtype_Declaration
5408 or else not In_Private_Part (Scope (Base_Type (T)))
5409 then
5410 Prepend_Elmt (T, Exchanged_Views);
5411 Exchange_Declarations (Etype (Get_Associated_Node (N)));
5412 end if;
5414 -- For composite types with inconsistent representation exchange
5415 -- component types accordingly.
5417 elsif Is_Access_Type (T)
5418 and then Is_Private_Type (Designated_Type (T))
5419 and then not Has_Private_View (N)
5420 and then Present (Full_View (Designated_Type (T)))
5421 then
5422 Switch_View (Designated_Type (T));
5424 elsif Is_Array_Type (T) then
5425 if Is_Private_Type (Component_Type (T))
5426 and then not Has_Private_View (N)
5427 and then Present (Full_View (Component_Type (T)))
5428 then
5429 Switch_View (Component_Type (T));
5430 end if;
5432 -- The normal exchange mechanism relies on the setting of a
5433 -- flag on the reference in the generic. However, an additional
5434 -- mechanism is needed for types that are not explicitly mentioned
5435 -- in the generic, but may be needed in expanded code in the
5436 -- instance. This includes component types of arrays and
5437 -- designated types of access types. This processing must also
5438 -- include the index types of arrays which we take care of here.
5440 declare
5441 Indx : Node_Id;
5442 Typ : Entity_Id;
5444 begin
5445 Indx := First_Index (T);
5446 Typ := Base_Type (Etype (Indx));
5447 while Present (Indx) loop
5448 if Is_Private_Type (Typ)
5449 and then Present (Full_View (Typ))
5450 then
5451 Switch_View (Typ);
5452 end if;
5454 Next_Index (Indx);
5455 end loop;
5456 end;
5458 elsif Is_Private_Type (T)
5459 and then Present (Full_View (T))
5460 and then Is_Array_Type (Full_View (T))
5461 and then Is_Private_Type (Component_Type (Full_View (T)))
5462 then
5463 Switch_View (T);
5465 -- Finally, a non-private subtype may have a private base type, which
5466 -- must be exchanged for consistency. This can happen when a package
5467 -- body is instantiated, when the scope stack is empty but in fact
5468 -- the subtype and the base type are declared in an enclosing scope.
5470 -- Note that in this case we introduce an inconsistency in the view
5471 -- set, because we switch the base type BT, but there could be some
5472 -- private dependent subtypes of BT which remain unswitched. Such
5473 -- subtypes might need to be switched at a later point (see specific
5474 -- provision for that case in Switch_View).
5476 elsif not Is_Private_Type (T)
5477 and then not Has_Private_View (N)
5478 and then Is_Private_Type (BT)
5479 and then Present (Full_View (BT))
5480 and then not Is_Generic_Type (BT)
5481 and then not In_Open_Scopes (BT)
5482 then
5483 Prepend_Elmt (Full_View (BT), Exchanged_Views);
5484 Exchange_Declarations (BT);
5485 end if;
5486 end if;
5487 end Check_Private_View;
5489 --------------------------
5490 -- Contains_Instance_Of --
5491 --------------------------
5493 function Contains_Instance_Of
5494 (Inner : Entity_Id;
5495 Outer : Entity_Id;
5496 N : Node_Id) return Boolean
5498 Elmt : Elmt_Id;
5499 Scop : Entity_Id;
5501 begin
5502 Scop := Outer;
5504 -- Verify that there are no circular instantiations. We check whether
5505 -- the unit contains an instance of the current scope or some enclosing
5506 -- scope (in case one of the instances appears in a subunit). Longer
5507 -- circularities involving subunits might seem too pathological to
5508 -- consider, but they were not too pathological for the authors of
5509 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
5510 -- enclosing generic scopes as containing an instance.
5512 loop
5513 -- Within a generic subprogram body, the scope is not generic, to
5514 -- allow for recursive subprograms. Use the declaration to determine
5515 -- whether this is a generic unit.
5517 if Ekind (Scop) = E_Generic_Package
5518 or else (Is_Subprogram (Scop)
5519 and then Nkind (Unit_Declaration_Node (Scop)) =
5520 N_Generic_Subprogram_Declaration)
5521 then
5522 Elmt := First_Elmt (Inner_Instances (Inner));
5524 while Present (Elmt) loop
5525 if Node (Elmt) = Scop then
5526 Error_Msg_Node_2 := Inner;
5527 Error_Msg_NE
5528 ("circular Instantiation: & instantiated within &!",
5529 N, Scop);
5530 return True;
5532 elsif Node (Elmt) = Inner then
5533 return True;
5535 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
5536 Error_Msg_Node_2 := Inner;
5537 Error_Msg_NE
5538 ("circular Instantiation: & instantiated within &!",
5539 N, Node (Elmt));
5540 return True;
5541 end if;
5543 Next_Elmt (Elmt);
5544 end loop;
5546 -- Indicate that Inner is being instantiated within Scop
5548 Append_Elmt (Inner, Inner_Instances (Scop));
5549 end if;
5551 if Scop = Standard_Standard then
5552 exit;
5553 else
5554 Scop := Scope (Scop);
5555 end if;
5556 end loop;
5558 return False;
5559 end Contains_Instance_Of;
5561 -----------------------
5562 -- Copy_Generic_Node --
5563 -----------------------
5565 function Copy_Generic_Node
5566 (N : Node_Id;
5567 Parent_Id : Node_Id;
5568 Instantiating : Boolean) return Node_Id
5570 Ent : Entity_Id;
5571 New_N : Node_Id;
5573 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
5574 -- Check the given value of one of the Fields referenced by the
5575 -- current node to determine whether to copy it recursively. The
5576 -- field may hold a Node_Id, a List_Id, or an Elist_Id, or a plain
5577 -- value (Sloc, Uint, Char) in which case it need not be copied.
5579 procedure Copy_Descendants;
5580 -- Common utility for various nodes
5582 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
5583 -- Make copy of element list
5585 function Copy_Generic_List
5586 (L : List_Id;
5587 Parent_Id : Node_Id) return List_Id;
5588 -- Apply Copy_Node recursively to the members of a node list
5590 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
5591 -- True if an identifier is part of the defining program unit name
5592 -- of a child unit. The entity of such an identifier must be kept
5593 -- (for ASIS use) even though as the name of an enclosing generic
5594 -- it would otherwise not be preserved in the generic tree.
5596 ----------------------
5597 -- Copy_Descendants --
5598 ----------------------
5600 procedure Copy_Descendants is
5602 use Atree.Unchecked_Access;
5603 -- This code section is part of the implementation of an untyped
5604 -- tree traversal, so it needs direct access to node fields.
5606 begin
5607 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
5608 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
5609 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
5610 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
5611 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
5612 end Copy_Descendants;
5614 -----------------------------
5615 -- Copy_Generic_Descendant --
5616 -----------------------------
5618 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
5619 begin
5620 if D = Union_Id (Empty) then
5621 return D;
5623 elsif D in Node_Range then
5624 return Union_Id
5625 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
5627 elsif D in List_Range then
5628 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
5630 elsif D in Elist_Range then
5631 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
5633 -- Nothing else is copyable (e.g. Uint values), return as is
5635 else
5636 return D;
5637 end if;
5638 end Copy_Generic_Descendant;
5640 ------------------------
5641 -- Copy_Generic_Elist --
5642 ------------------------
5644 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
5645 M : Elmt_Id;
5646 L : Elist_Id;
5648 begin
5649 if Present (E) then
5650 L := New_Elmt_List;
5651 M := First_Elmt (E);
5652 while Present (M) loop
5653 Append_Elmt
5654 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
5655 Next_Elmt (M);
5656 end loop;
5658 return L;
5660 else
5661 return No_Elist;
5662 end if;
5663 end Copy_Generic_Elist;
5665 -----------------------
5666 -- Copy_Generic_List --
5667 -----------------------
5669 function Copy_Generic_List
5670 (L : List_Id;
5671 Parent_Id : Node_Id) return List_Id
5673 N : Node_Id;
5674 New_L : List_Id;
5676 begin
5677 if Present (L) then
5678 New_L := New_List;
5679 Set_Parent (New_L, Parent_Id);
5681 N := First (L);
5682 while Present (N) loop
5683 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
5684 Next (N);
5685 end loop;
5687 return New_L;
5689 else
5690 return No_List;
5691 end if;
5692 end Copy_Generic_List;
5694 ---------------------------
5695 -- In_Defining_Unit_Name --
5696 ---------------------------
5698 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
5699 begin
5700 return Present (Parent (Nam))
5701 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
5702 or else
5703 (Nkind (Parent (Nam)) = N_Expanded_Name
5704 and then In_Defining_Unit_Name (Parent (Nam))));
5705 end In_Defining_Unit_Name;
5707 -- Start of processing for Copy_Generic_Node
5709 begin
5710 if N = Empty then
5711 return N;
5712 end if;
5714 New_N := New_Copy (N);
5716 if Instantiating then
5717 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
5718 end if;
5720 if not Is_List_Member (N) then
5721 Set_Parent (New_N, Parent_Id);
5722 end if;
5724 -- If defining identifier, then all fields have been copied already
5726 if Nkind (New_N) in N_Entity then
5727 null;
5729 -- Special casing for identifiers and other entity names and operators
5731 elsif Nkind_In (New_N, N_Identifier,
5732 N_Character_Literal,
5733 N_Expanded_Name,
5734 N_Operator_Symbol)
5735 or else Nkind (New_N) in N_Op
5736 then
5737 if not Instantiating then
5739 -- Link both nodes in order to assign subsequently the entity of
5740 -- the copy to the original node, in case this is a global
5741 -- reference.
5743 Set_Associated_Node (N, New_N);
5745 -- If we are within an instantiation, this is a nested generic
5746 -- that has already been analyzed at the point of definition. We
5747 -- must preserve references that were global to the enclosing
5748 -- parent at that point. Other occurrences, whether global or
5749 -- local to the current generic, must be resolved anew, so we
5750 -- reset the entity in the generic copy. A global reference has a
5751 -- smaller depth than the parent, or else the same depth in case
5752 -- both are distinct compilation units.
5753 -- A child unit is implicitly declared within the enclosing parent
5754 -- but is in fact global to it, and must be preserved.
5756 -- It is also possible for Current_Instantiated_Parent to be
5757 -- defined, and for this not to be a nested generic, namely if the
5758 -- unit is loaded through Rtsfind. In that case, the entity of
5759 -- New_N is only a link to the associated node, and not a defining
5760 -- occurrence.
5762 -- The entities for parent units in the defining_program_unit of a
5763 -- generic child unit are established when the context of the unit
5764 -- is first analyzed, before the generic copy is made. They are
5765 -- preserved in the copy for use in ASIS queries.
5767 Ent := Entity (New_N);
5769 if No (Current_Instantiated_Parent.Gen_Id) then
5770 if No (Ent)
5771 or else Nkind (Ent) /= N_Defining_Identifier
5772 or else not In_Defining_Unit_Name (N)
5773 then
5774 Set_Associated_Node (New_N, Empty);
5775 end if;
5777 elsif No (Ent)
5778 or else
5779 not Nkind_In (Ent, N_Defining_Identifier,
5780 N_Defining_Character_Literal,
5781 N_Defining_Operator_Symbol)
5782 or else No (Scope (Ent))
5783 or else
5784 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
5785 and then not Is_Child_Unit (Ent))
5786 or else
5787 (Scope_Depth (Scope (Ent)) >
5788 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
5789 and then
5790 Get_Source_Unit (Ent) =
5791 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
5792 then
5793 Set_Associated_Node (New_N, Empty);
5794 end if;
5796 -- Case of instantiating identifier or some other name or operator
5798 else
5799 -- If the associated node is still defined, the entity in it is
5800 -- global, and must be copied to the instance. If this copy is
5801 -- being made for a body to inline, it is applied to an
5802 -- instantiated tree, and the entity is already present and must
5803 -- be also preserved.
5805 declare
5806 Assoc : constant Node_Id := Get_Associated_Node (N);
5808 begin
5809 if Present (Assoc) then
5810 if Nkind (Assoc) = Nkind (N) then
5811 Set_Entity (New_N, Entity (Assoc));
5812 Check_Private_View (N);
5814 elsif Nkind (Assoc) = N_Function_Call then
5815 Set_Entity (New_N, Entity (Name (Assoc)));
5817 elsif Nkind_In (Assoc, N_Defining_Identifier,
5818 N_Defining_Character_Literal,
5819 N_Defining_Operator_Symbol)
5820 and then Expander_Active
5821 then
5822 -- Inlining case: we are copying a tree that contains
5823 -- global entities, which are preserved in the copy to be
5824 -- used for subsequent inlining.
5826 null;
5828 else
5829 Set_Entity (New_N, Empty);
5830 end if;
5831 end if;
5832 end;
5833 end if;
5835 -- For expanded name, we must copy the Prefix and Selector_Name
5837 if Nkind (N) = N_Expanded_Name then
5838 Set_Prefix
5839 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
5841 Set_Selector_Name (New_N,
5842 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
5844 -- For operators, we must copy the right operand
5846 elsif Nkind (N) in N_Op then
5847 Set_Right_Opnd (New_N,
5848 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
5850 -- And for binary operators, the left operand as well
5852 if Nkind (N) in N_Binary_Op then
5853 Set_Left_Opnd (New_N,
5854 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
5855 end if;
5856 end if;
5858 -- Special casing for stubs
5860 elsif Nkind (N) in N_Body_Stub then
5862 -- In any case, we must copy the specification or defining
5863 -- identifier as appropriate.
5865 if Nkind (N) = N_Subprogram_Body_Stub then
5866 Set_Specification (New_N,
5867 Copy_Generic_Node (Specification (N), New_N, Instantiating));
5869 else
5870 Set_Defining_Identifier (New_N,
5871 Copy_Generic_Node
5872 (Defining_Identifier (N), New_N, Instantiating));
5873 end if;
5875 -- If we are not instantiating, then this is where we load and
5876 -- analyze subunits, i.e. at the point where the stub occurs. A
5877 -- more permissible system might defer this analysis to the point
5878 -- of instantiation, but this seems to complicated for now.
5880 if not Instantiating then
5881 declare
5882 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
5883 Subunit : Node_Id;
5884 Unum : Unit_Number_Type;
5885 New_Body : Node_Id;
5887 begin
5888 Unum :=
5889 Load_Unit
5890 (Load_Name => Subunit_Name,
5891 Required => False,
5892 Subunit => True,
5893 Error_Node => N);
5895 -- If the proper body is not found, a warning message will be
5896 -- emitted when analyzing the stub, or later at the point
5897 -- of instantiation. Here we just leave the stub as is.
5899 if Unum = No_Unit then
5900 Subunits_Missing := True;
5901 goto Subunit_Not_Found;
5902 end if;
5904 Subunit := Cunit (Unum);
5906 if Nkind (Unit (Subunit)) /= N_Subunit then
5907 Error_Msg_N
5908 ("found child unit instead of expected SEPARATE subunit",
5909 Subunit);
5910 Error_Msg_Sloc := Sloc (N);
5911 Error_Msg_N ("\to complete stub #", Subunit);
5912 goto Subunit_Not_Found;
5913 end if;
5915 -- We must create a generic copy of the subunit, in order to
5916 -- perform semantic analysis on it, and we must replace the
5917 -- stub in the original generic unit with the subunit, in order
5918 -- to preserve non-local references within.
5920 -- Only the proper body needs to be copied. Library_Unit and
5921 -- context clause are simply inherited by the generic copy.
5922 -- Note that the copy (which may be recursive if there are
5923 -- nested subunits) must be done first, before attaching it to
5924 -- the enclosing generic.
5926 New_Body :=
5927 Copy_Generic_Node
5928 (Proper_Body (Unit (Subunit)),
5929 Empty, Instantiating => False);
5931 -- Now place the original proper body in the original generic
5932 -- unit. This is a body, not a compilation unit.
5934 Rewrite (N, Proper_Body (Unit (Subunit)));
5935 Set_Is_Compilation_Unit (Defining_Entity (N), False);
5936 Set_Was_Originally_Stub (N);
5938 -- Finally replace the body of the subunit with its copy, and
5939 -- make this new subunit into the library unit of the generic
5940 -- copy, which does not have stubs any longer.
5942 Set_Proper_Body (Unit (Subunit), New_Body);
5943 Set_Library_Unit (New_N, Subunit);
5944 Inherit_Context (Unit (Subunit), N);
5945 end;
5947 -- If we are instantiating, this must be an error case, since
5948 -- otherwise we would have replaced the stub node by the proper body
5949 -- that corresponds. So just ignore it in the copy (i.e. we have
5950 -- copied it, and that is good enough).
5952 else
5953 null;
5954 end if;
5956 <<Subunit_Not_Found>> null;
5958 -- If the node is a compilation unit, it is the subunit of a stub, which
5959 -- has been loaded already (see code below). In this case, the library
5960 -- unit field of N points to the parent unit (which is a compilation
5961 -- unit) and need not (and cannot!) be copied.
5963 -- When the proper body of the stub is analyzed, the library_unit link
5964 -- is used to establish the proper context (see sem_ch10).
5966 -- The other fields of a compilation unit are copied as usual
5968 elsif Nkind (N) = N_Compilation_Unit then
5970 -- This code can only be executed when not instantiating, because in
5971 -- the copy made for an instantiation, the compilation unit node has
5972 -- disappeared at the point that a stub is replaced by its proper
5973 -- body.
5975 pragma Assert (not Instantiating);
5977 Set_Context_Items (New_N,
5978 Copy_Generic_List (Context_Items (N), New_N));
5980 Set_Unit (New_N,
5981 Copy_Generic_Node (Unit (N), New_N, False));
5983 Set_First_Inlined_Subprogram (New_N,
5984 Copy_Generic_Node
5985 (First_Inlined_Subprogram (N), New_N, False));
5987 Set_Aux_Decls_Node (New_N,
5988 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
5990 -- For an assignment node, the assignment is known to be semantically
5991 -- legal if we are instantiating the template. This avoids incorrect
5992 -- diagnostics in generated code.
5994 elsif Nkind (N) = N_Assignment_Statement then
5996 -- Copy name and expression fields in usual manner
5998 Set_Name (New_N,
5999 Copy_Generic_Node (Name (N), New_N, Instantiating));
6001 Set_Expression (New_N,
6002 Copy_Generic_Node (Expression (N), New_N, Instantiating));
6004 if Instantiating then
6005 Set_Assignment_OK (Name (New_N), True);
6006 end if;
6008 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
6009 if not Instantiating then
6010 Set_Associated_Node (N, New_N);
6012 else
6013 if Present (Get_Associated_Node (N))
6014 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
6015 then
6016 -- In the generic the aggregate has some composite type. If at
6017 -- the point of instantiation the type has a private view,
6018 -- install the full view (and that of its ancestors, if any).
6020 declare
6021 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
6022 Rt : Entity_Id;
6024 begin
6025 if Present (T)
6026 and then Is_Private_Type (T)
6027 then
6028 Switch_View (T);
6029 end if;
6031 if Present (T)
6032 and then Is_Tagged_Type (T)
6033 and then Is_Derived_Type (T)
6034 then
6035 Rt := Root_Type (T);
6037 loop
6038 T := Etype (T);
6040 if Is_Private_Type (T) then
6041 Switch_View (T);
6042 end if;
6044 exit when T = Rt;
6045 end loop;
6046 end if;
6047 end;
6048 end if;
6049 end if;
6051 -- Do not copy the associated node, which points to
6052 -- the generic copy of the aggregate.
6054 declare
6055 use Atree.Unchecked_Access;
6056 -- This code section is part of the implementation of an untyped
6057 -- tree traversal, so it needs direct access to node fields.
6059 begin
6060 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
6061 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
6062 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
6063 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
6064 end;
6066 -- Allocators do not have an identifier denoting the access type,
6067 -- so we must locate it through the expression to check whether
6068 -- the views are consistent.
6070 elsif Nkind (N) = N_Allocator
6071 and then Nkind (Expression (N)) = N_Qualified_Expression
6072 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
6073 and then Instantiating
6074 then
6075 declare
6076 T : constant Node_Id :=
6077 Get_Associated_Node (Subtype_Mark (Expression (N)));
6078 Acc_T : Entity_Id;
6080 begin
6081 if Present (T) then
6083 -- Retrieve the allocator node in the generic copy
6085 Acc_T := Etype (Parent (Parent (T)));
6086 if Present (Acc_T)
6087 and then Is_Private_Type (Acc_T)
6088 then
6089 Switch_View (Acc_T);
6090 end if;
6091 end if;
6093 Copy_Descendants;
6094 end;
6096 -- For a proper body, we must catch the case of a proper body that
6097 -- replaces a stub. This represents the point at which a separate
6098 -- compilation unit, and hence template file, may be referenced, so we
6099 -- must make a new source instantiation entry for the template of the
6100 -- subunit, and ensure that all nodes in the subunit are adjusted using
6101 -- this new source instantiation entry.
6103 elsif Nkind (N) in N_Proper_Body then
6104 declare
6105 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
6107 begin
6108 if Instantiating and then Was_Originally_Stub (N) then
6109 Create_Instantiation_Source
6110 (Instantiation_Node,
6111 Defining_Entity (N),
6112 False,
6113 S_Adjustment);
6114 end if;
6116 -- Now copy the fields of the proper body, using the new
6117 -- adjustment factor if one was needed as per test above.
6119 Copy_Descendants;
6121 -- Restore the original adjustment factor in case changed
6123 S_Adjustment := Save_Adjustment;
6124 end;
6126 -- Don't copy Ident or Comment pragmas, since the comment belongs to the
6127 -- generic unit, not to the instantiating unit.
6129 elsif Nkind (N) = N_Pragma
6130 and then Instantiating
6131 then
6132 declare
6133 Prag_Id : constant Pragma_Id := Get_Pragma_Id (N);
6134 begin
6135 if Prag_Id = Pragma_Ident
6136 or else Prag_Id = Pragma_Comment
6137 then
6138 New_N := Make_Null_Statement (Sloc (N));
6139 else
6140 Copy_Descendants;
6141 end if;
6142 end;
6144 elsif Nkind_In (N, N_Integer_Literal,
6145 N_Real_Literal,
6146 N_String_Literal)
6147 then
6148 -- No descendant fields need traversing
6150 null;
6152 -- For the remaining nodes, copy recursively their descendants
6154 else
6155 Copy_Descendants;
6157 if Instantiating
6158 and then Nkind (N) = N_Subprogram_Body
6159 then
6160 Set_Generic_Parent (Specification (New_N), N);
6161 end if;
6162 end if;
6164 return New_N;
6165 end Copy_Generic_Node;
6167 ----------------------------
6168 -- Denotes_Formal_Package --
6169 ----------------------------
6171 function Denotes_Formal_Package
6172 (Pack : Entity_Id;
6173 On_Exit : Boolean := False;
6174 Instance : Entity_Id := Empty) return Boolean
6176 Par : Entity_Id;
6177 Scop : constant Entity_Id := Scope (Pack);
6178 E : Entity_Id;
6180 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
6181 -- The package in question may be an actual for a previous formal
6182 -- package P of the current instance, so examine its actuals as well.
6183 -- This must be recursive over other formal packages.
6185 ----------------------------------
6186 -- Is_Actual_Of_Previous_Formal --
6187 ----------------------------------
6189 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
6190 E1 : Entity_Id;
6192 begin
6193 E1 := First_Entity (P);
6194 while Present (E1) and then E1 /= Instance loop
6195 if Ekind (E1) = E_Package
6196 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
6197 then
6198 if Renamed_Object (E1) = Pack then
6199 return True;
6201 elsif E1 = P
6202 or else Renamed_Object (E1) = P
6203 then
6204 return False;
6206 elsif Is_Actual_Of_Previous_Formal (E1) then
6207 return True;
6208 end if;
6209 end if;
6211 Next_Entity (E1);
6212 end loop;
6214 return False;
6215 end Is_Actual_Of_Previous_Formal;
6217 -- Start of processing for Denotes_Formal_Package
6219 begin
6220 if On_Exit then
6221 Par :=
6222 Instance_Envs.Table
6223 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
6224 else
6225 Par := Current_Instantiated_Parent.Act_Id;
6226 end if;
6228 if Ekind (Scop) = E_Generic_Package
6229 or else Nkind (Unit_Declaration_Node (Scop)) =
6230 N_Generic_Subprogram_Declaration
6231 then
6232 return True;
6234 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
6235 N_Formal_Package_Declaration
6236 then
6237 return True;
6239 elsif No (Par) then
6240 return False;
6242 else
6243 -- Check whether this package is associated with a formal package of
6244 -- the enclosing instantiation. Iterate over the list of renamings.
6246 E := First_Entity (Par);
6247 while Present (E) loop
6248 if Ekind (E) /= E_Package
6249 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
6250 then
6251 null;
6253 elsif Renamed_Object (E) = Par then
6254 return False;
6256 elsif Renamed_Object (E) = Pack then
6257 return True;
6259 elsif Is_Actual_Of_Previous_Formal (E) then
6260 return True;
6262 end if;
6264 Next_Entity (E);
6265 end loop;
6267 return False;
6268 end if;
6269 end Denotes_Formal_Package;
6271 -----------------
6272 -- End_Generic --
6273 -----------------
6275 procedure End_Generic is
6276 begin
6277 -- ??? More things could be factored out in this routine. Should
6278 -- probably be done at a later stage.
6280 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
6281 Generic_Flags.Decrement_Last;
6283 Expander_Mode_Restore;
6284 end End_Generic;
6286 ----------------------
6287 -- Find_Actual_Type --
6288 ----------------------
6290 function Find_Actual_Type
6291 (Typ : Entity_Id;
6292 Gen_Type : Entity_Id) return Entity_Id
6294 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
6295 T : Entity_Id;
6297 begin
6298 -- Special processing only applies to child units
6300 if not Is_Child_Unit (Gen_Scope) then
6301 return Get_Instance_Of (Typ);
6303 -- If designated or component type is itself a formal of the child unit,
6304 -- its instance is available.
6306 elsif Scope (Typ) = Gen_Scope then
6307 return Get_Instance_Of (Typ);
6309 -- If the array or access type is not declared in the parent unit,
6310 -- no special processing needed.
6312 elsif not Is_Generic_Type (Typ)
6313 and then Scope (Gen_Scope) /= Scope (Typ)
6314 then
6315 return Get_Instance_Of (Typ);
6317 -- Otherwise, retrieve designated or component type by visibility
6319 else
6320 T := Current_Entity (Typ);
6321 while Present (T) loop
6322 if In_Open_Scopes (Scope (T)) then
6323 return T;
6325 elsif Is_Generic_Actual_Type (T) then
6326 return T;
6327 end if;
6329 T := Homonym (T);
6330 end loop;
6332 return Typ;
6333 end if;
6334 end Find_Actual_Type;
6336 ----------------------------
6337 -- Freeze_Subprogram_Body --
6338 ----------------------------
6340 procedure Freeze_Subprogram_Body
6341 (Inst_Node : Node_Id;
6342 Gen_Body : Node_Id;
6343 Pack_Id : Entity_Id)
6345 F_Node : Node_Id;
6346 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
6347 Par : constant Entity_Id := Scope (Gen_Unit);
6348 Enc_G : Entity_Id;
6349 Enc_I : Node_Id;
6350 E_G_Id : Entity_Id;
6352 function Earlier (N1, N2 : Node_Id) return Boolean;
6353 -- Yields True if N1 and N2 appear in the same compilation unit,
6354 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
6355 -- traversal of the tree for the unit.
6357 function Enclosing_Body (N : Node_Id) return Node_Id;
6358 -- Find innermost package body that encloses the given node, and which
6359 -- is not a compilation unit. Freeze nodes for the instance, or for its
6360 -- enclosing body, may be inserted after the enclosing_body of the
6361 -- generic unit.
6363 function Package_Freeze_Node (B : Node_Id) return Node_Id;
6364 -- Find entity for given package body, and locate or create a freeze
6365 -- node for it.
6367 function True_Parent (N : Node_Id) return Node_Id;
6368 -- For a subunit, return parent of corresponding stub
6370 -------------
6371 -- Earlier --
6372 -------------
6374 function Earlier (N1, N2 : Node_Id) return Boolean is
6375 D1 : Integer := 0;
6376 D2 : Integer := 0;
6377 P1 : Node_Id := N1;
6378 P2 : Node_Id := N2;
6380 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
6381 -- Find distance from given node to enclosing compilation unit
6383 ----------------
6384 -- Find_Depth --
6385 ----------------
6387 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
6388 begin
6389 while Present (P)
6390 and then Nkind (P) /= N_Compilation_Unit
6391 loop
6392 P := True_Parent (P);
6393 D := D + 1;
6394 end loop;
6395 end Find_Depth;
6397 -- Start of processing for Earlier
6399 begin
6400 Find_Depth (P1, D1);
6401 Find_Depth (P2, D2);
6403 if P1 /= P2 then
6404 return False;
6405 else
6406 P1 := N1;
6407 P2 := N2;
6408 end if;
6410 while D1 > D2 loop
6411 P1 := True_Parent (P1);
6412 D1 := D1 - 1;
6413 end loop;
6415 while D2 > D1 loop
6416 P2 := True_Parent (P2);
6417 D2 := D2 - 1;
6418 end loop;
6420 -- At this point P1 and P2 are at the same distance from the root.
6421 -- We examine their parents until we find a common declarative
6422 -- list, at which point we can establish their relative placement
6423 -- by comparing their ultimate slocs. If we reach the root,
6424 -- N1 and N2 do not descend from the same declarative list (e.g.
6425 -- one is nested in the declarative part and the other is in a block
6426 -- in the statement part) and the earlier one is already frozen.
6428 while not Is_List_Member (P1)
6429 or else not Is_List_Member (P2)
6430 or else List_Containing (P1) /= List_Containing (P2)
6431 loop
6432 P1 := True_Parent (P1);
6433 P2 := True_Parent (P2);
6435 if Nkind (Parent (P1)) = N_Subunit then
6436 P1 := Corresponding_Stub (Parent (P1));
6437 end if;
6439 if Nkind (Parent (P2)) = N_Subunit then
6440 P2 := Corresponding_Stub (Parent (P2));
6441 end if;
6443 if P1 = P2 then
6444 return False;
6445 end if;
6446 end loop;
6448 return
6449 Top_Level_Location (Sloc (P1)) < Top_Level_Location (Sloc (P2));
6450 end Earlier;
6452 --------------------
6453 -- Enclosing_Body --
6454 --------------------
6456 function Enclosing_Body (N : Node_Id) return Node_Id is
6457 P : Node_Id := Parent (N);
6459 begin
6460 while Present (P)
6461 and then Nkind (Parent (P)) /= N_Compilation_Unit
6462 loop
6463 if Nkind (P) = N_Package_Body then
6465 if Nkind (Parent (P)) = N_Subunit then
6466 return Corresponding_Stub (Parent (P));
6467 else
6468 return P;
6469 end if;
6470 end if;
6472 P := True_Parent (P);
6473 end loop;
6475 return Empty;
6476 end Enclosing_Body;
6478 -------------------------
6479 -- Package_Freeze_Node --
6480 -------------------------
6482 function Package_Freeze_Node (B : Node_Id) return Node_Id is
6483 Id : Entity_Id;
6485 begin
6486 if Nkind (B) = N_Package_Body then
6487 Id := Corresponding_Spec (B);
6489 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
6490 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
6491 end if;
6493 Ensure_Freeze_Node (Id);
6494 return Freeze_Node (Id);
6495 end Package_Freeze_Node;
6497 -----------------
6498 -- True_Parent --
6499 -----------------
6501 function True_Parent (N : Node_Id) return Node_Id is
6502 begin
6503 if Nkind (Parent (N)) = N_Subunit then
6504 return Parent (Corresponding_Stub (Parent (N)));
6505 else
6506 return Parent (N);
6507 end if;
6508 end True_Parent;
6510 -- Start of processing of Freeze_Subprogram_Body
6512 begin
6513 -- If the instance and the generic body appear within the same unit, and
6514 -- the instance precedes the generic, the freeze node for the instance
6515 -- must appear after that of the generic. If the generic is nested
6516 -- within another instance I2, then current instance must be frozen
6517 -- after I2. In both cases, the freeze nodes are those of enclosing
6518 -- packages. Otherwise, the freeze node is placed at the end of the
6519 -- current declarative part.
6521 Enc_G := Enclosing_Body (Gen_Body);
6522 Enc_I := Enclosing_Body (Inst_Node);
6523 Ensure_Freeze_Node (Pack_Id);
6524 F_Node := Freeze_Node (Pack_Id);
6526 if Is_Generic_Instance (Par)
6527 and then Present (Freeze_Node (Par))
6528 and then
6529 In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
6530 then
6531 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
6533 -- The parent was a premature instantiation. Insert freeze node at
6534 -- the end the current declarative part.
6536 Insert_After_Last_Decl (Inst_Node, F_Node);
6538 else
6539 Insert_After (Freeze_Node (Par), F_Node);
6540 end if;
6542 -- The body enclosing the instance should be frozen after the body that
6543 -- includes the generic, because the body of the instance may make
6544 -- references to entities therein. If the two are not in the same
6545 -- declarative part, or if the one enclosing the instance is frozen
6546 -- already, freeze the instance at the end of the current declarative
6547 -- part.
6549 elsif Is_Generic_Instance (Par)
6550 and then Present (Freeze_Node (Par))
6551 and then Present (Enc_I)
6552 then
6553 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
6554 or else
6555 (Nkind (Enc_I) = N_Package_Body
6556 and then
6557 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
6558 then
6559 -- The enclosing package may contain several instances. Rather
6560 -- than computing the earliest point at which to insert its
6561 -- freeze node, we place it at the end of the declarative part
6562 -- of the parent of the generic.
6564 Insert_After_Last_Decl
6565 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
6566 end if;
6568 Insert_After_Last_Decl (Inst_Node, F_Node);
6570 elsif Present (Enc_G)
6571 and then Present (Enc_I)
6572 and then Enc_G /= Enc_I
6573 and then Earlier (Inst_Node, Gen_Body)
6574 then
6575 if Nkind (Enc_G) = N_Package_Body then
6576 E_G_Id := Corresponding_Spec (Enc_G);
6577 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
6578 E_G_Id :=
6579 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
6580 end if;
6582 -- Freeze package that encloses instance, and place node after
6583 -- package that encloses generic. If enclosing package is already
6584 -- frozen we have to assume it is at the proper place. This may be
6585 -- a potential ABE that requires dynamic checking. Do not add a
6586 -- freeze node if the package that encloses the generic is inside
6587 -- the body that encloses the instance, because the freeze node
6588 -- would be in the wrong scope. Additional contortions needed if
6589 -- the bodies are within a subunit.
6591 declare
6592 Enclosing_Body : Node_Id;
6594 begin
6595 if Nkind (Enc_I) = N_Package_Body_Stub then
6596 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
6597 else
6598 Enclosing_Body := Enc_I;
6599 end if;
6601 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
6602 Insert_After_Last_Decl (Enc_G, Package_Freeze_Node (Enc_I));
6603 end if;
6604 end;
6606 -- Freeze enclosing subunit before instance
6608 Ensure_Freeze_Node (E_G_Id);
6610 if not Is_List_Member (Freeze_Node (E_G_Id)) then
6611 Insert_After (Enc_G, Freeze_Node (E_G_Id));
6612 end if;
6614 Insert_After_Last_Decl (Inst_Node, F_Node);
6616 else
6617 -- If none of the above, insert freeze node at the end of the current
6618 -- declarative part.
6620 Insert_After_Last_Decl (Inst_Node, F_Node);
6621 end if;
6622 end Freeze_Subprogram_Body;
6624 ----------------
6625 -- Get_Gen_Id --
6626 ----------------
6628 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
6629 begin
6630 return Generic_Renamings.Table (E).Gen_Id;
6631 end Get_Gen_Id;
6633 ---------------------
6634 -- Get_Instance_Of --
6635 ---------------------
6637 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
6638 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
6640 begin
6641 if Res /= Assoc_Null then
6642 return Generic_Renamings.Table (Res).Act_Id;
6643 else
6644 -- On exit, entity is not instantiated: not a generic parameter, or
6645 -- else parameter of an inner generic unit.
6647 return A;
6648 end if;
6649 end Get_Instance_Of;
6651 ------------------------------------
6652 -- Get_Package_Instantiation_Node --
6653 ------------------------------------
6655 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
6656 Decl : Node_Id := Unit_Declaration_Node (A);
6657 Inst : Node_Id;
6659 begin
6660 -- If the Package_Instantiation attribute has been set on the package
6661 -- entity, then use it directly when it (or its Original_Node) refers
6662 -- to an N_Package_Instantiation node. In principle it should be
6663 -- possible to have this field set in all cases, which should be
6664 -- investigated, and would allow this function to be significantly
6665 -- simplified. ???
6667 if Present (Package_Instantiation (A)) then
6668 if Nkind (Package_Instantiation (A)) = N_Package_Instantiation then
6669 return Package_Instantiation (A);
6671 elsif Nkind (Original_Node (Package_Instantiation (A))) =
6672 N_Package_Instantiation
6673 then
6674 return Original_Node (Package_Instantiation (A));
6675 end if;
6676 end if;
6678 -- If the instantiation is a compilation unit that does not need body
6679 -- then the instantiation node has been rewritten as a package
6680 -- declaration for the instance, and we return the original node.
6682 -- If it is a compilation unit and the instance node has not been
6683 -- rewritten, then it is still the unit of the compilation. Finally, if
6684 -- a body is present, this is a parent of the main unit whose body has
6685 -- been compiled for inlining purposes, and the instantiation node has
6686 -- been rewritten with the instance body.
6688 -- Otherwise the instantiation node appears after the declaration. If
6689 -- the entity is a formal package, the declaration may have been
6690 -- rewritten as a generic declaration (in the case of a formal with box)
6691 -- or left as a formal package declaration if it has actuals, and is
6692 -- found with a forward search.
6694 if Nkind (Parent (Decl)) = N_Compilation_Unit then
6695 if Nkind (Decl) = N_Package_Declaration
6696 and then Present (Corresponding_Body (Decl))
6697 then
6698 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
6699 end if;
6701 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
6702 return Original_Node (Decl);
6703 else
6704 return Unit (Parent (Decl));
6705 end if;
6707 elsif Nkind (Decl) = N_Package_Declaration
6708 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
6709 then
6710 return Original_Node (Decl);
6712 else
6713 Inst := Next (Decl);
6714 while not Nkind_In (Inst, N_Package_Instantiation,
6715 N_Formal_Package_Declaration)
6716 loop
6717 Next (Inst);
6718 end loop;
6720 return Inst;
6721 end if;
6722 end Get_Package_Instantiation_Node;
6724 ------------------------
6725 -- Has_Been_Exchanged --
6726 ------------------------
6728 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
6729 Next : Elmt_Id;
6731 begin
6732 Next := First_Elmt (Exchanged_Views);
6733 while Present (Next) loop
6734 if Full_View (Node (Next)) = E then
6735 return True;
6736 end if;
6738 Next_Elmt (Next);
6739 end loop;
6741 return False;
6742 end Has_Been_Exchanged;
6744 ----------
6745 -- Hash --
6746 ----------
6748 function Hash (F : Entity_Id) return HTable_Range is
6749 begin
6750 return HTable_Range (F mod HTable_Size);
6751 end Hash;
6753 ------------------------
6754 -- Hide_Current_Scope --
6755 ------------------------
6757 procedure Hide_Current_Scope is
6758 C : constant Entity_Id := Current_Scope;
6759 E : Entity_Id;
6761 begin
6762 Set_Is_Hidden_Open_Scope (C);
6764 E := First_Entity (C);
6765 while Present (E) loop
6766 if Is_Immediately_Visible (E) then
6767 Set_Is_Immediately_Visible (E, False);
6768 Append_Elmt (E, Hidden_Entities);
6769 end if;
6771 Next_Entity (E);
6772 end loop;
6774 -- Make the scope name invisible as well. This is necessary, but might
6775 -- conflict with calls to Rtsfind later on, in case the scope is a
6776 -- predefined one. There is no clean solution to this problem, so for
6777 -- now we depend on the user not redefining Standard itself in one of
6778 -- the parent units.
6780 if Is_Immediately_Visible (C)
6781 and then C /= Standard_Standard
6782 then
6783 Set_Is_Immediately_Visible (C, False);
6784 Append_Elmt (C, Hidden_Entities);
6785 end if;
6787 end Hide_Current_Scope;
6789 --------------
6790 -- Init_Env --
6791 --------------
6793 procedure Init_Env is
6794 Saved : Instance_Env;
6796 begin
6797 Saved.Instantiated_Parent := Current_Instantiated_Parent;
6798 Saved.Exchanged_Views := Exchanged_Views;
6799 Saved.Hidden_Entities := Hidden_Entities;
6800 Saved.Current_Sem_Unit := Current_Sem_Unit;
6801 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
6802 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
6804 -- Save configuration switches. These may be reset if the unit is a
6805 -- predefined unit, and the current mode is not Ada 2005.
6807 Save_Opt_Config_Switches (Saved.Switches);
6809 Instance_Envs.Append (Saved);
6811 Exchanged_Views := New_Elmt_List;
6812 Hidden_Entities := New_Elmt_List;
6814 -- Make dummy entry for Instantiated parent. If generic unit is legal,
6815 -- this is set properly in Set_Instance_Env.
6817 Current_Instantiated_Parent :=
6818 (Current_Scope, Current_Scope, Assoc_Null);
6819 end Init_Env;
6821 ------------------------------
6822 -- In_Same_Declarative_Part --
6823 ------------------------------
6825 function In_Same_Declarative_Part
6826 (F_Node : Node_Id;
6827 Inst : Node_Id) return Boolean
6829 Decls : constant Node_Id := Parent (F_Node);
6830 Nod : Node_Id := Parent (Inst);
6832 begin
6833 while Present (Nod) loop
6834 if Nod = Decls then
6835 return True;
6837 elsif Nkind_In (Nod, N_Subprogram_Body,
6838 N_Package_Body,
6839 N_Task_Body,
6840 N_Protected_Body,
6841 N_Block_Statement)
6842 then
6843 return False;
6845 elsif Nkind (Nod) = N_Subunit then
6846 Nod := Corresponding_Stub (Nod);
6848 elsif Nkind (Nod) = N_Compilation_Unit then
6849 return False;
6851 else
6852 Nod := Parent (Nod);
6853 end if;
6854 end loop;
6856 return False;
6857 end In_Same_Declarative_Part;
6859 ---------------------
6860 -- In_Main_Context --
6861 ---------------------
6863 function In_Main_Context (E : Entity_Id) return Boolean is
6864 Context : List_Id;
6865 Clause : Node_Id;
6866 Nam : Node_Id;
6868 begin
6869 if not Is_Compilation_Unit (E)
6870 or else Ekind (E) /= E_Package
6871 or else In_Private_Part (E)
6872 then
6873 return False;
6874 end if;
6876 Context := Context_Items (Cunit (Main_Unit));
6878 Clause := First (Context);
6879 while Present (Clause) loop
6880 if Nkind (Clause) = N_With_Clause then
6881 Nam := Name (Clause);
6883 -- If the current scope is part of the context of the main unit,
6884 -- analysis of the corresponding with_clause is not complete, and
6885 -- the entity is not set. We use the Chars field directly, which
6886 -- might produce false positives in rare cases, but guarantees
6887 -- that we produce all the instance bodies we will need.
6889 if (Is_Entity_Name (Nam)
6890 and then Chars (Nam) = Chars (E))
6891 or else (Nkind (Nam) = N_Selected_Component
6892 and then Chars (Selector_Name (Nam)) = Chars (E))
6893 then
6894 return True;
6895 end if;
6896 end if;
6898 Next (Clause);
6899 end loop;
6901 return False;
6902 end In_Main_Context;
6904 ---------------------
6905 -- Inherit_Context --
6906 ---------------------
6908 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
6909 Current_Context : List_Id;
6910 Current_Unit : Node_Id;
6911 Item : Node_Id;
6912 New_I : Node_Id;
6914 begin
6915 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
6917 -- The inherited context is attached to the enclosing compilation
6918 -- unit. This is either the main unit, or the declaration for the
6919 -- main unit (in case the instantiation appears within the package
6920 -- declaration and the main unit is its body).
6922 Current_Unit := Parent (Inst);
6923 while Present (Current_Unit)
6924 and then Nkind (Current_Unit) /= N_Compilation_Unit
6925 loop
6926 Current_Unit := Parent (Current_Unit);
6927 end loop;
6929 Current_Context := Context_Items (Current_Unit);
6931 Item := First (Context_Items (Parent (Gen_Decl)));
6932 while Present (Item) loop
6933 if Nkind (Item) = N_With_Clause then
6935 -- Take care to prevent direct cyclic with's, which can happen
6936 -- if the generic body with's the current unit. Such a case
6937 -- would result in binder errors (or run-time errors if the
6938 -- -gnatE switch is in effect), but we want to prevent it here,
6939 -- because Sem.Walk_Library_Items doesn't like cycles. Note
6940 -- that we don't bother to detect indirect cycles.
6942 if Library_Unit (Item) /= Current_Unit then
6943 New_I := New_Copy (Item);
6944 Set_Implicit_With (New_I, True);
6945 Append (New_I, Current_Context);
6946 end if;
6947 end if;
6949 Next (Item);
6950 end loop;
6951 end if;
6952 end Inherit_Context;
6954 ----------------
6955 -- Initialize --
6956 ----------------
6958 procedure Initialize is
6959 begin
6960 Generic_Renamings.Init;
6961 Instance_Envs.Init;
6962 Generic_Flags.Init;
6963 Generic_Renamings_HTable.Reset;
6964 Circularity_Detected := False;
6965 Exchanged_Views := No_Elist;
6966 Hidden_Entities := No_Elist;
6967 end Initialize;
6969 ----------------------------
6970 -- Insert_After_Last_Decl --
6971 ----------------------------
6973 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id) is
6974 L : List_Id := List_Containing (N);
6975 P : constant Node_Id := Parent (L);
6977 begin
6978 if not Is_List_Member (F_Node) then
6979 if Nkind (P) = N_Package_Specification
6980 and then L = Visible_Declarations (P)
6981 and then Present (Private_Declarations (P))
6982 and then not Is_Empty_List (Private_Declarations (P))
6983 then
6984 L := Private_Declarations (P);
6985 end if;
6987 Insert_After (Last (L), F_Node);
6988 end if;
6989 end Insert_After_Last_Decl;
6991 ------------------
6992 -- Install_Body --
6993 ------------------
6995 procedure Install_Body
6996 (Act_Body : Node_Id;
6997 N : Node_Id;
6998 Gen_Body : Node_Id;
6999 Gen_Decl : Node_Id)
7001 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
7002 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
7003 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
7004 Par : constant Entity_Id := Scope (Gen_Id);
7005 Gen_Unit : constant Node_Id :=
7006 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
7007 Orig_Body : Node_Id := Gen_Body;
7008 F_Node : Node_Id;
7009 Body_Unit : Node_Id;
7011 Must_Delay : Boolean;
7013 function Enclosing_Subp (Id : Entity_Id) return Entity_Id;
7014 -- Find subprogram (if any) that encloses instance and/or generic body
7016 function True_Sloc (N : Node_Id) return Source_Ptr;
7017 -- If the instance is nested inside a generic unit, the Sloc of the
7018 -- instance indicates the place of the original definition, not the
7019 -- point of the current enclosing instance. Pending a better usage of
7020 -- Slocs to indicate instantiation places, we determine the place of
7021 -- origin of a node by finding the maximum sloc of any ancestor node.
7022 -- Why is this not equivalent to Top_Level_Location ???
7024 --------------------
7025 -- Enclosing_Subp --
7026 --------------------
7028 function Enclosing_Subp (Id : Entity_Id) return Entity_Id is
7029 Scop : Entity_Id := Scope (Id);
7031 begin
7032 while Scop /= Standard_Standard
7033 and then not Is_Overloadable (Scop)
7034 loop
7035 Scop := Scope (Scop);
7036 end loop;
7038 return Scop;
7039 end Enclosing_Subp;
7041 ---------------
7042 -- True_Sloc --
7043 ---------------
7045 function True_Sloc (N : Node_Id) return Source_Ptr is
7046 Res : Source_Ptr;
7047 N1 : Node_Id;
7049 begin
7050 Res := Sloc (N);
7051 N1 := N;
7052 while Present (N1) and then N1 /= Act_Unit loop
7053 if Sloc (N1) > Res then
7054 Res := Sloc (N1);
7055 end if;
7057 N1 := Parent (N1);
7058 end loop;
7060 return Res;
7061 end True_Sloc;
7063 -- Start of processing for Install_Body
7065 begin
7067 -- If the body is a subunit, the freeze point is the corresponding
7068 -- stub in the current compilation, not the subunit itself.
7070 if Nkind (Parent (Gen_Body)) = N_Subunit then
7071 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
7072 else
7073 Orig_Body := Gen_Body;
7074 end if;
7076 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
7078 -- If the instantiation and the generic definition appear in the same
7079 -- package declaration, this is an early instantiation. If they appear
7080 -- in the same declarative part, it is an early instantiation only if
7081 -- the generic body appears textually later, and the generic body is
7082 -- also in the main unit.
7084 -- If instance is nested within a subprogram, and the generic body is
7085 -- not, the instance is delayed because the enclosing body is. If
7086 -- instance and body are within the same scope, or the same sub-
7087 -- program body, indicate explicitly that the instance is delayed.
7089 Must_Delay :=
7090 (Gen_Unit = Act_Unit
7091 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
7092 N_Generic_Package_Declaration)
7093 or else (Gen_Unit = Body_Unit
7094 and then True_Sloc (N) < Sloc (Orig_Body)))
7095 and then Is_In_Main_Unit (Gen_Unit)
7096 and then (Scope (Act_Id) = Scope (Gen_Id)
7097 or else
7098 Enclosing_Subp (Act_Id) = Enclosing_Subp (Gen_Id)));
7100 -- If this is an early instantiation, the freeze node is placed after
7101 -- the generic body. Otherwise, if the generic appears in an instance,
7102 -- we cannot freeze the current instance until the outer one is frozen.
7103 -- This is only relevant if the current instance is nested within some
7104 -- inner scope not itself within the outer instance. If this scope is
7105 -- a package body in the same declarative part as the outer instance,
7106 -- then that body needs to be frozen after the outer instance. Finally,
7107 -- if no delay is needed, we place the freeze node at the end of the
7108 -- current declarative part.
7110 if Expander_Active then
7111 Ensure_Freeze_Node (Act_Id);
7112 F_Node := Freeze_Node (Act_Id);
7114 if Must_Delay then
7115 Insert_After (Orig_Body, F_Node);
7117 elsif Is_Generic_Instance (Par)
7118 and then Present (Freeze_Node (Par))
7119 and then Scope (Act_Id) /= Par
7120 then
7121 -- Freeze instance of inner generic after instance of enclosing
7122 -- generic.
7124 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
7125 Insert_After (Freeze_Node (Par), F_Node);
7127 -- Freeze package enclosing instance of inner generic after
7128 -- instance of enclosing generic.
7130 elsif Nkind (Parent (N)) = N_Package_Body
7131 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
7132 then
7134 declare
7135 Enclosing : constant Entity_Id :=
7136 Corresponding_Spec (Parent (N));
7138 begin
7139 Insert_After_Last_Decl (N, F_Node);
7140 Ensure_Freeze_Node (Enclosing);
7142 if not Is_List_Member (Freeze_Node (Enclosing)) then
7143 Insert_After (Freeze_Node (Par), Freeze_Node (Enclosing));
7144 end if;
7145 end;
7147 else
7148 Insert_After_Last_Decl (N, F_Node);
7149 end if;
7151 else
7152 Insert_After_Last_Decl (N, F_Node);
7153 end if;
7154 end if;
7156 Set_Is_Frozen (Act_Id);
7157 Insert_Before (N, Act_Body);
7158 Mark_Rewrite_Insertion (Act_Body);
7159 end Install_Body;
7161 -----------------------------
7162 -- Install_Formal_Packages --
7163 -----------------------------
7165 procedure Install_Formal_Packages (Par : Entity_Id) is
7166 E : Entity_Id;
7167 Gen : Entity_Id;
7168 Gen_E : Entity_Id := Empty;
7170 begin
7171 E := First_Entity (Par);
7173 -- In we are installing an instance parent, locate the formal packages
7174 -- of its generic parent.
7176 if Is_Generic_Instance (Par) then
7177 Gen := Generic_Parent (Specification (Unit_Declaration_Node (Par)));
7178 Gen_E := First_Entity (Gen);
7179 end if;
7181 while Present (E) loop
7182 if Ekind (E) = E_Package
7183 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
7184 then
7185 -- If this is the renaming for the parent instance, done
7187 if Renamed_Object (E) = Par then
7188 exit;
7190 -- The visibility of a formal of an enclosing generic is already
7191 -- correct.
7193 elsif Denotes_Formal_Package (E) then
7194 null;
7196 elsif Present (Associated_Formal_Package (E)) then
7197 Check_Generic_Actuals (Renamed_Object (E), True);
7198 Set_Is_Hidden (E, False);
7200 -- Find formal package in generic unit that corresponds to
7201 -- (instance of) formal package in instance.
7203 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
7204 Next_Entity (Gen_E);
7205 end loop;
7207 if Present (Gen_E) then
7208 Map_Formal_Package_Entities (Gen_E, E);
7209 end if;
7210 end if;
7211 end if;
7213 Next_Entity (E);
7214 if Present (Gen_E) then
7215 Next_Entity (Gen_E);
7216 end if;
7217 end loop;
7218 end Install_Formal_Packages;
7220 --------------------
7221 -- Install_Parent --
7222 --------------------
7224 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
7225 Ancestors : constant Elist_Id := New_Elmt_List;
7226 S : constant Entity_Id := Current_Scope;
7227 Inst_Par : Entity_Id;
7228 First_Par : Entity_Id;
7229 Inst_Node : Node_Id;
7230 Gen_Par : Entity_Id;
7231 First_Gen : Entity_Id;
7232 Elmt : Elmt_Id;
7234 procedure Install_Noninstance_Specs (Par : Entity_Id);
7235 -- Install the scopes of noninstance parent units ending with Par
7237 procedure Install_Spec (Par : Entity_Id);
7238 -- The child unit is within the declarative part of the parent, so
7239 -- the declarations within the parent are immediately visible.
7241 -------------------------------
7242 -- Install_Noninstance_Specs --
7243 -------------------------------
7245 procedure Install_Noninstance_Specs (Par : Entity_Id) is
7246 begin
7247 if Present (Par)
7248 and then Par /= Standard_Standard
7249 and then not In_Open_Scopes (Par)
7250 then
7251 Install_Noninstance_Specs (Scope (Par));
7252 Install_Spec (Par);
7253 end if;
7254 end Install_Noninstance_Specs;
7256 ------------------
7257 -- Install_Spec --
7258 ------------------
7260 procedure Install_Spec (Par : Entity_Id) is
7261 Spec : constant Node_Id :=
7262 Specification (Unit_Declaration_Node (Par));
7264 begin
7265 -- If this parent of the child instance is a top-level unit,
7266 -- then record the unit and its visibility for later resetting
7267 -- in Remove_Parent. We exclude units that are generic instances,
7268 -- as we only want to record this information for the ultimate
7269 -- top-level noninstance parent (is that always correct???).
7271 if Scope (Par) = Standard_Standard
7272 and then not Is_Generic_Instance (Par)
7273 then
7274 Parent_Unit_Visible := Is_Immediately_Visible (Par);
7275 Instance_Parent_Unit := Par;
7276 end if;
7278 -- Open the parent scope and make it and its declarations visible.
7279 -- If this point is not within a body, then only the visible
7280 -- declarations should be made visible, and installation of the
7281 -- private declarations is deferred until the appropriate point
7282 -- within analysis of the spec being instantiated (see the handling
7283 -- of parent visibility in Analyze_Package_Specification). This is
7284 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
7285 -- private view problems that occur when compiling instantiations of
7286 -- a generic child of that package (Generic_Dispatching_Constructor).
7287 -- If the instance freezes a tagged type, inlinings of operations
7288 -- from Ada.Tags may need the full view of type Tag. If inlining took
7289 -- proper account of establishing visibility of inlined subprograms'
7290 -- parents then it should be possible to remove this
7291 -- special check. ???
7293 Push_Scope (Par);
7294 Set_Is_Immediately_Visible (Par);
7295 Install_Visible_Declarations (Par);
7296 Set_Use (Visible_Declarations (Spec));
7298 if In_Body or else Is_RTU (Par, Ada_Tags) then
7299 Install_Private_Declarations (Par);
7300 Set_Use (Private_Declarations (Spec));
7301 end if;
7302 end Install_Spec;
7304 -- Start of processing for Install_Parent
7306 begin
7307 -- We need to install the parent instance to compile the instantiation
7308 -- of the child, but the child instance must appear in the current
7309 -- scope. Given that we cannot place the parent above the current scope
7310 -- in the scope stack, we duplicate the current scope and unstack both
7311 -- after the instantiation is complete.
7313 -- If the parent is itself the instantiation of a child unit, we must
7314 -- also stack the instantiation of its parent, and so on. Each such
7315 -- ancestor is the prefix of the name in a prior instantiation.
7317 -- If this is a nested instance, the parent unit itself resolves to
7318 -- a renaming of the parent instance, whose declaration we need.
7320 -- Finally, the parent may be a generic (not an instance) when the
7321 -- child unit appears as a formal package.
7323 Inst_Par := P;
7325 if Present (Renamed_Entity (Inst_Par)) then
7326 Inst_Par := Renamed_Entity (Inst_Par);
7327 end if;
7329 First_Par := Inst_Par;
7331 Gen_Par :=
7332 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
7334 First_Gen := Gen_Par;
7336 while Present (Gen_Par)
7337 and then Is_Child_Unit (Gen_Par)
7338 loop
7339 -- Load grandparent instance as well
7341 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
7343 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
7344 Inst_Par := Entity (Prefix (Name (Inst_Node)));
7346 if Present (Renamed_Entity (Inst_Par)) then
7347 Inst_Par := Renamed_Entity (Inst_Par);
7348 end if;
7350 Gen_Par :=
7351 Generic_Parent
7352 (Specification (Unit_Declaration_Node (Inst_Par)));
7354 if Present (Gen_Par) then
7355 Prepend_Elmt (Inst_Par, Ancestors);
7357 else
7358 -- Parent is not the name of an instantiation
7360 Install_Noninstance_Specs (Inst_Par);
7362 exit;
7363 end if;
7365 else
7366 -- Previous error
7368 exit;
7369 end if;
7370 end loop;
7372 if Present (First_Gen) then
7373 Append_Elmt (First_Par, Ancestors);
7375 else
7376 Install_Noninstance_Specs (First_Par);
7377 end if;
7379 if not Is_Empty_Elmt_List (Ancestors) then
7380 Elmt := First_Elmt (Ancestors);
7382 while Present (Elmt) loop
7383 Install_Spec (Node (Elmt));
7384 Install_Formal_Packages (Node (Elmt));
7386 Next_Elmt (Elmt);
7387 end loop;
7388 end if;
7390 if not In_Body then
7391 Push_Scope (S);
7392 end if;
7393 end Install_Parent;
7395 --------------------------------
7396 -- Instantiate_Formal_Package --
7397 --------------------------------
7399 function Instantiate_Formal_Package
7400 (Formal : Node_Id;
7401 Actual : Node_Id;
7402 Analyzed_Formal : Node_Id) return List_Id
7404 Loc : constant Source_Ptr := Sloc (Actual);
7405 Actual_Pack : Entity_Id;
7406 Formal_Pack : Entity_Id;
7407 Gen_Parent : Entity_Id;
7408 Decls : List_Id;
7409 Nod : Node_Id;
7410 Parent_Spec : Node_Id;
7412 procedure Find_Matching_Actual
7413 (F : Node_Id;
7414 Act : in out Entity_Id);
7415 -- We need to associate each formal entity in the formal package
7416 -- with the corresponding entity in the actual package. The actual
7417 -- package has been analyzed and possibly expanded, and as a result
7418 -- there is no one-to-one correspondence between the two lists (for
7419 -- example, the actual may include subtypes, itypes, and inherited
7420 -- primitive operations, interspersed among the renaming declarations
7421 -- for the actuals) . We retrieve the corresponding actual by name
7422 -- because each actual has the same name as the formal, and they do
7423 -- appear in the same order.
7425 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
7426 -- Retrieve entity of defining entity of generic formal parameter.
7427 -- Only the declarations of formals need to be considered when
7428 -- linking them to actuals, but the declarative list may include
7429 -- internal entities generated during analysis, and those are ignored.
7431 procedure Match_Formal_Entity
7432 (Formal_Node : Node_Id;
7433 Formal_Ent : Entity_Id;
7434 Actual_Ent : Entity_Id);
7435 -- Associates the formal entity with the actual. In the case
7436 -- where Formal_Ent is a formal package, this procedure iterates
7437 -- through all of its formals and enters associations between the
7438 -- actuals occurring in the formal package's corresponding actual
7439 -- package (given by Actual_Ent) and the formal package's formal
7440 -- parameters. This procedure recurses if any of the parameters is
7441 -- itself a package.
7443 function Is_Instance_Of
7444 (Act_Spec : Entity_Id;
7445 Gen_Anc : Entity_Id) return Boolean;
7446 -- The actual can be an instantiation of a generic within another
7447 -- instance, in which case there is no direct link from it to the
7448 -- original generic ancestor. In that case, we recognize that the
7449 -- ultimate ancestor is the same by examining names and scopes.
7451 procedure Process_Nested_Formal (Formal : Entity_Id);
7452 -- If the current formal is declared with a box, its own formals are
7453 -- visible in the instance, as they were in the generic, and their
7454 -- Hidden flag must be reset. If some of these formals are themselves
7455 -- packages declared with a box, the processing must be recursive.
7457 --------------------------
7458 -- Find_Matching_Actual --
7459 --------------------------
7461 procedure Find_Matching_Actual
7462 (F : Node_Id;
7463 Act : in out Entity_Id)
7465 Formal_Ent : Entity_Id;
7467 begin
7468 case Nkind (Original_Node (F)) is
7469 when N_Formal_Object_Declaration |
7470 N_Formal_Type_Declaration =>
7471 Formal_Ent := Defining_Identifier (F);
7473 while Chars (Act) /= Chars (Formal_Ent) loop
7474 Next_Entity (Act);
7475 end loop;
7477 when N_Formal_Subprogram_Declaration |
7478 N_Formal_Package_Declaration |
7479 N_Package_Declaration |
7480 N_Generic_Package_Declaration =>
7481 Formal_Ent := Defining_Entity (F);
7483 while Chars (Act) /= Chars (Formal_Ent) loop
7484 Next_Entity (Act);
7485 end loop;
7487 when others =>
7488 raise Program_Error;
7489 end case;
7490 end Find_Matching_Actual;
7492 -------------------------
7493 -- Match_Formal_Entity --
7494 -------------------------
7496 procedure Match_Formal_Entity
7497 (Formal_Node : Node_Id;
7498 Formal_Ent : Entity_Id;
7499 Actual_Ent : Entity_Id)
7501 Act_Pkg : Entity_Id;
7503 begin
7504 Set_Instance_Of (Formal_Ent, Actual_Ent);
7506 if Ekind (Actual_Ent) = E_Package then
7508 -- Record associations for each parameter
7510 Act_Pkg := Actual_Ent;
7512 declare
7513 A_Ent : Entity_Id := First_Entity (Act_Pkg);
7514 F_Ent : Entity_Id;
7515 F_Node : Node_Id;
7517 Gen_Decl : Node_Id;
7518 Formals : List_Id;
7519 Actual : Entity_Id;
7521 begin
7522 -- Retrieve the actual given in the formal package declaration
7524 Actual := Entity (Name (Original_Node (Formal_Node)));
7526 -- The actual in the formal package declaration may be a
7527 -- renamed generic package, in which case we want to retrieve
7528 -- the original generic in order to traverse its formal part.
7530 if Present (Renamed_Entity (Actual)) then
7531 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
7532 else
7533 Gen_Decl := Unit_Declaration_Node (Actual);
7534 end if;
7536 Formals := Generic_Formal_Declarations (Gen_Decl);
7538 if Present (Formals) then
7539 F_Node := First_Non_Pragma (Formals);
7540 else
7541 F_Node := Empty;
7542 end if;
7544 while Present (A_Ent)
7545 and then Present (F_Node)
7546 and then A_Ent /= First_Private_Entity (Act_Pkg)
7547 loop
7548 F_Ent := Get_Formal_Entity (F_Node);
7550 if Present (F_Ent) then
7552 -- This is a formal of the original package. Record
7553 -- association and recurse.
7555 Find_Matching_Actual (F_Node, A_Ent);
7556 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
7557 Next_Entity (A_Ent);
7558 end if;
7560 Next_Non_Pragma (F_Node);
7561 end loop;
7562 end;
7563 end if;
7564 end Match_Formal_Entity;
7566 -----------------------
7567 -- Get_Formal_Entity --
7568 -----------------------
7570 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
7571 Kind : constant Node_Kind := Nkind (Original_Node (N));
7572 begin
7573 case Kind is
7574 when N_Formal_Object_Declaration =>
7575 return Defining_Identifier (N);
7577 when N_Formal_Type_Declaration =>
7578 return Defining_Identifier (N);
7580 when N_Formal_Subprogram_Declaration =>
7581 return Defining_Unit_Name (Specification (N));
7583 when N_Formal_Package_Declaration =>
7584 return Defining_Identifier (Original_Node (N));
7586 when N_Generic_Package_Declaration =>
7587 return Defining_Identifier (Original_Node (N));
7589 -- All other declarations are introduced by semantic analysis and
7590 -- have no match in the actual.
7592 when others =>
7593 return Empty;
7594 end case;
7595 end Get_Formal_Entity;
7597 --------------------
7598 -- Is_Instance_Of --
7599 --------------------
7601 function Is_Instance_Of
7602 (Act_Spec : Entity_Id;
7603 Gen_Anc : Entity_Id) return Boolean
7605 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
7607 begin
7608 if No (Gen_Par) then
7609 return False;
7611 -- Simplest case: the generic parent of the actual is the formal
7613 elsif Gen_Par = Gen_Anc then
7614 return True;
7616 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
7617 return False;
7619 -- The actual may be obtained through several instantiations. Its
7620 -- scope must itself be an instance of a generic declared in the
7621 -- same scope as the formal. Any other case is detected above.
7623 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
7624 return False;
7626 else
7627 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
7628 end if;
7629 end Is_Instance_Of;
7631 ---------------------------
7632 -- Process_Nested_Formal --
7633 ---------------------------
7635 procedure Process_Nested_Formal (Formal : Entity_Id) is
7636 Ent : Entity_Id;
7638 begin
7639 if Present (Associated_Formal_Package (Formal))
7640 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
7641 then
7642 Ent := First_Entity (Formal);
7643 while Present (Ent) loop
7644 Set_Is_Hidden (Ent, False);
7645 Set_Is_Visible_Formal (Ent);
7646 Set_Is_Potentially_Use_Visible
7647 (Ent, Is_Potentially_Use_Visible (Formal));
7649 if Ekind (Ent) = E_Package then
7650 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
7651 Process_Nested_Formal (Ent);
7652 end if;
7654 Next_Entity (Ent);
7655 end loop;
7656 end if;
7657 end Process_Nested_Formal;
7659 -- Start of processing for Instantiate_Formal_Package
7661 begin
7662 Analyze (Actual);
7664 if not Is_Entity_Name (Actual)
7665 or else Ekind (Entity (Actual)) /= E_Package
7666 then
7667 Error_Msg_N
7668 ("expect package instance to instantiate formal", Actual);
7669 Abandon_Instantiation (Actual);
7670 raise Program_Error;
7672 else
7673 Actual_Pack := Entity (Actual);
7674 Set_Is_Instantiated (Actual_Pack);
7676 -- The actual may be a renamed package, or an outer generic formal
7677 -- package whose instantiation is converted into a renaming.
7679 if Present (Renamed_Object (Actual_Pack)) then
7680 Actual_Pack := Renamed_Object (Actual_Pack);
7681 end if;
7683 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
7684 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
7685 Formal_Pack := Defining_Identifier (Analyzed_Formal);
7686 else
7687 Gen_Parent :=
7688 Generic_Parent (Specification (Analyzed_Formal));
7689 Formal_Pack :=
7690 Defining_Unit_Name (Specification (Analyzed_Formal));
7691 end if;
7693 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
7694 Parent_Spec := Specification (Unit_Declaration_Node (Actual_Pack));
7695 else
7696 Parent_Spec := Parent (Actual_Pack);
7697 end if;
7699 if Gen_Parent = Any_Id then
7700 Error_Msg_N
7701 ("previous error in declaration of formal package", Actual);
7702 Abandon_Instantiation (Actual);
7704 elsif
7705 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
7706 then
7707 null;
7709 else
7710 Error_Msg_NE
7711 ("actual parameter must be instance of&", Actual, Gen_Parent);
7712 Abandon_Instantiation (Actual);
7713 end if;
7715 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
7716 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
7718 Nod :=
7719 Make_Package_Renaming_Declaration (Loc,
7720 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
7721 Name => New_Reference_To (Actual_Pack, Loc));
7723 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
7724 Defining_Identifier (Formal));
7725 Decls := New_List (Nod);
7727 -- If the formal F has a box, then the generic declarations are
7728 -- visible in the generic G. In an instance of G, the corresponding
7729 -- entities in the actual for F (which are the actuals for the
7730 -- instantiation of the generic that F denotes) must also be made
7731 -- visible for analysis of the current instance. On exit from the
7732 -- current instance, those entities are made private again. If the
7733 -- actual is currently in use, these entities are also use-visible.
7735 -- The loop through the actual entities also steps through the formal
7736 -- entities and enters associations from formals to actuals into the
7737 -- renaming map. This is necessary to properly handle checking of
7738 -- actual parameter associations for later formals that depend on
7739 -- actuals declared in the formal package.
7741 -- In Ada 2005, partial parametrization requires that we make visible
7742 -- the actuals corresponding to formals that were defaulted in the
7743 -- formal package. There formals are identified because they remain
7744 -- formal generics within the formal package, rather than being
7745 -- renamings of the actuals supplied.
7747 declare
7748 Gen_Decl : constant Node_Id :=
7749 Unit_Declaration_Node (Gen_Parent);
7750 Formals : constant List_Id :=
7751 Generic_Formal_Declarations (Gen_Decl);
7753 Actual_Ent : Entity_Id;
7754 Actual_Of_Formal : Node_Id;
7755 Formal_Node : Node_Id;
7756 Formal_Ent : Entity_Id;
7758 begin
7759 if Present (Formals) then
7760 Formal_Node := First_Non_Pragma (Formals);
7761 else
7762 Formal_Node := Empty;
7763 end if;
7765 Actual_Ent := First_Entity (Actual_Pack);
7766 Actual_Of_Formal :=
7767 First (Visible_Declarations (Specification (Analyzed_Formal)));
7768 while Present (Actual_Ent)
7769 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
7770 loop
7771 if Present (Formal_Node) then
7772 Formal_Ent := Get_Formal_Entity (Formal_Node);
7774 if Present (Formal_Ent) then
7775 Find_Matching_Actual (Formal_Node, Actual_Ent);
7776 Match_Formal_Entity
7777 (Formal_Node, Formal_Ent, Actual_Ent);
7779 -- We iterate at the same time over the actuals of the
7780 -- local package created for the formal, to determine
7781 -- which one of the formals of the original generic were
7782 -- defaulted in the formal. The corresponding actual
7783 -- entities are visible in the enclosing instance.
7785 if Box_Present (Formal)
7786 or else
7787 (Present (Actual_Of_Formal)
7788 and then
7789 Is_Generic_Formal
7790 (Get_Formal_Entity (Actual_Of_Formal)))
7791 then
7792 Set_Is_Hidden (Actual_Ent, False);
7793 Set_Is_Visible_Formal (Actual_Ent);
7794 Set_Is_Potentially_Use_Visible
7795 (Actual_Ent, In_Use (Actual_Pack));
7797 if Ekind (Actual_Ent) = E_Package then
7798 Process_Nested_Formal (Actual_Ent);
7799 end if;
7801 else
7802 Set_Is_Hidden (Actual_Ent);
7803 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
7804 end if;
7805 end if;
7807 Next_Non_Pragma (Formal_Node);
7808 Next (Actual_Of_Formal);
7810 else
7811 -- No further formals to match, but the generic part may
7812 -- contain inherited operation that are not hidden in the
7813 -- enclosing instance.
7815 Next_Entity (Actual_Ent);
7816 end if;
7817 end loop;
7819 -- Inherited subprograms generated by formal derived types are
7820 -- also visible if the types are.
7822 Actual_Ent := First_Entity (Actual_Pack);
7823 while Present (Actual_Ent)
7824 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
7825 loop
7826 if Is_Overloadable (Actual_Ent)
7827 and then
7828 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
7829 and then
7830 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
7831 then
7832 Set_Is_Hidden (Actual_Ent, False);
7833 Set_Is_Potentially_Use_Visible
7834 (Actual_Ent, In_Use (Actual_Pack));
7835 end if;
7837 Next_Entity (Actual_Ent);
7838 end loop;
7839 end;
7841 -- If the formal is not declared with a box, reanalyze it as an
7842 -- abbreviated instantiation, to verify the matching rules of 12.7.
7843 -- The actual checks are performed after the generic associations
7844 -- have been analyzed, to guarantee the same visibility for this
7845 -- instantiation and for the actuals.
7847 -- In Ada 2005, the generic associations for the formal can include
7848 -- defaulted parameters. These are ignored during check. This
7849 -- internal instantiation is removed from the tree after conformance
7850 -- checking, because it contains formal declarations for those
7851 -- defaulted parameters, and those should not reach the back-end.
7853 if not Box_Present (Formal) then
7854 declare
7855 I_Pack : constant Entity_Id :=
7856 Make_Defining_Identifier (Sloc (Actual),
7857 Chars => New_Internal_Name ('P'));
7859 begin
7860 Set_Is_Internal (I_Pack);
7862 Append_To (Decls,
7863 Make_Package_Instantiation (Sloc (Actual),
7864 Defining_Unit_Name => I_Pack,
7865 Name =>
7866 New_Occurrence_Of
7867 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
7868 Generic_Associations =>
7869 Generic_Associations (Formal)));
7870 end;
7871 end if;
7873 return Decls;
7874 end if;
7875 end Instantiate_Formal_Package;
7877 -----------------------------------
7878 -- Instantiate_Formal_Subprogram --
7879 -----------------------------------
7881 function Instantiate_Formal_Subprogram
7882 (Formal : Node_Id;
7883 Actual : Node_Id;
7884 Analyzed_Formal : Node_Id) return Node_Id
7886 Loc : Source_Ptr;
7887 Formal_Sub : constant Entity_Id :=
7888 Defining_Unit_Name (Specification (Formal));
7889 Analyzed_S : constant Entity_Id :=
7890 Defining_Unit_Name (Specification (Analyzed_Formal));
7891 Decl_Node : Node_Id;
7892 Nam : Node_Id;
7893 New_Spec : Node_Id;
7895 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
7896 -- If the generic is a child unit, the parent has been installed on the
7897 -- scope stack, but a default subprogram cannot resolve to something on
7898 -- the parent because that parent is not really part of the visible
7899 -- context (it is there to resolve explicit local entities). If the
7900 -- default has resolved in this way, we remove the entity from
7901 -- immediate visibility and analyze the node again to emit an error
7902 -- message or find another visible candidate.
7904 procedure Valid_Actual_Subprogram (Act : Node_Id);
7905 -- Perform legality check and raise exception on failure
7907 -----------------------
7908 -- From_Parent_Scope --
7909 -----------------------
7911 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
7912 Gen_Scope : Node_Id;
7914 begin
7915 Gen_Scope := Scope (Analyzed_S);
7916 while Present (Gen_Scope)
7917 and then Is_Child_Unit (Gen_Scope)
7918 loop
7919 if Scope (Subp) = Scope (Gen_Scope) then
7920 return True;
7921 end if;
7923 Gen_Scope := Scope (Gen_Scope);
7924 end loop;
7926 return False;
7927 end From_Parent_Scope;
7929 -----------------------------
7930 -- Valid_Actual_Subprogram --
7931 -----------------------------
7933 procedure Valid_Actual_Subprogram (Act : Node_Id) is
7934 Act_E : Entity_Id;
7936 begin
7937 if Is_Entity_Name (Act) then
7938 Act_E := Entity (Act);
7940 elsif Nkind (Act) = N_Selected_Component
7941 and then Is_Entity_Name (Selector_Name (Act))
7942 then
7943 Act_E := Entity (Selector_Name (Act));
7945 else
7946 Act_E := Empty;
7947 end if;
7949 if (Present (Act_E) and then Is_Overloadable (Act_E))
7950 or else Nkind_In (Act, N_Attribute_Reference,
7951 N_Indexed_Component,
7952 N_Character_Literal,
7953 N_Explicit_Dereference)
7954 then
7955 return;
7956 end if;
7958 Error_Msg_NE
7959 ("expect subprogram or entry name in instantiation of&",
7960 Instantiation_Node, Formal_Sub);
7961 Abandon_Instantiation (Instantiation_Node);
7963 end Valid_Actual_Subprogram;
7965 -- Start of processing for Instantiate_Formal_Subprogram
7967 begin
7968 New_Spec := New_Copy_Tree (Specification (Formal));
7970 -- The tree copy has created the proper instantiation sloc for the
7971 -- new specification. Use this location for all other constructed
7972 -- declarations.
7974 Loc := Sloc (Defining_Unit_Name (New_Spec));
7976 -- Create new entity for the actual (New_Copy_Tree does not)
7978 Set_Defining_Unit_Name
7979 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
7981 -- Create new entities for the each of the formals in the
7982 -- specification of the renaming declaration built for the actual.
7984 if Present (Parameter_Specifications (New_Spec)) then
7985 declare
7986 F : Node_Id;
7987 begin
7988 F := First (Parameter_Specifications (New_Spec));
7989 while Present (F) loop
7990 Set_Defining_Identifier (F,
7991 Make_Defining_Identifier (Sloc (F),
7992 Chars => Chars (Defining_Identifier (F))));
7993 Next (F);
7994 end loop;
7995 end;
7996 end if;
7998 -- Find entity of actual. If the actual is an attribute reference, it
7999 -- cannot be resolved here (its formal is missing) but is handled
8000 -- instead in Attribute_Renaming. If the actual is overloaded, it is
8001 -- fully resolved subsequently, when the renaming declaration for the
8002 -- formal is analyzed. If it is an explicit dereference, resolve the
8003 -- prefix but not the actual itself, to prevent interpretation as call.
8005 if Present (Actual) then
8006 Loc := Sloc (Actual);
8007 Set_Sloc (New_Spec, Loc);
8009 if Nkind (Actual) = N_Operator_Symbol then
8010 Find_Direct_Name (Actual);
8012 elsif Nkind (Actual) = N_Explicit_Dereference then
8013 Analyze (Prefix (Actual));
8015 elsif Nkind (Actual) /= N_Attribute_Reference then
8016 Analyze (Actual);
8017 end if;
8019 Valid_Actual_Subprogram (Actual);
8020 Nam := Actual;
8022 elsif Present (Default_Name (Formal)) then
8023 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
8024 N_Selected_Component,
8025 N_Indexed_Component,
8026 N_Character_Literal)
8027 and then Present (Entity (Default_Name (Formal)))
8028 then
8029 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
8030 else
8031 Nam := New_Copy (Default_Name (Formal));
8032 Set_Sloc (Nam, Loc);
8033 end if;
8035 elsif Box_Present (Formal) then
8037 -- Actual is resolved at the point of instantiation. Create an
8038 -- identifier or operator with the same name as the formal.
8040 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
8041 Nam := Make_Operator_Symbol (Loc,
8042 Chars => Chars (Formal_Sub),
8043 Strval => No_String);
8044 else
8045 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
8046 end if;
8048 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
8049 and then Null_Present (Specification (Formal))
8050 then
8051 -- Generate null body for procedure, for use in the instance
8053 Decl_Node :=
8054 Make_Subprogram_Body (Loc,
8055 Specification => New_Spec,
8056 Declarations => New_List,
8057 Handled_Statement_Sequence =>
8058 Make_Handled_Sequence_Of_Statements (Loc,
8059 Statements => New_List (Make_Null_Statement (Loc))));
8061 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
8062 return Decl_Node;
8064 else
8065 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
8066 Error_Msg_NE
8067 ("missing actual&", Instantiation_Node, Formal_Sub);
8068 Error_Msg_NE
8069 ("\in instantiation of & declared#",
8070 Instantiation_Node, Scope (Analyzed_S));
8071 Abandon_Instantiation (Instantiation_Node);
8072 end if;
8074 Decl_Node :=
8075 Make_Subprogram_Renaming_Declaration (Loc,
8076 Specification => New_Spec,
8077 Name => Nam);
8079 -- If we do not have an actual and the formal specified <> then set to
8080 -- get proper default.
8082 if No (Actual) and then Box_Present (Formal) then
8083 Set_From_Default (Decl_Node);
8084 end if;
8086 -- Gather possible interpretations for the actual before analyzing the
8087 -- instance. If overloaded, it will be resolved when analyzing the
8088 -- renaming declaration.
8090 if Box_Present (Formal)
8091 and then No (Actual)
8092 then
8093 Analyze (Nam);
8095 if Is_Child_Unit (Scope (Analyzed_S))
8096 and then Present (Entity (Nam))
8097 then
8098 if not Is_Overloaded (Nam) then
8100 if From_Parent_Scope (Entity (Nam)) then
8101 Set_Is_Immediately_Visible (Entity (Nam), False);
8102 Set_Entity (Nam, Empty);
8103 Set_Etype (Nam, Empty);
8105 Analyze (Nam);
8107 Set_Is_Immediately_Visible (Entity (Nam));
8108 end if;
8110 else
8111 declare
8112 I : Interp_Index;
8113 It : Interp;
8115 begin
8116 Get_First_Interp (Nam, I, It);
8118 while Present (It.Nam) loop
8119 if From_Parent_Scope (It.Nam) then
8120 Remove_Interp (I);
8121 end if;
8123 Get_Next_Interp (I, It);
8124 end loop;
8125 end;
8126 end if;
8127 end if;
8128 end if;
8130 -- The generic instantiation freezes the actual. This can only be done
8131 -- once the actual is resolved, in the analysis of the renaming
8132 -- declaration. To make the formal subprogram entity available, we set
8133 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
8134 -- This is also needed in Analyze_Subprogram_Renaming for the processing
8135 -- of formal abstract subprograms.
8137 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
8139 -- We cannot analyze the renaming declaration, and thus find the actual,
8140 -- until all the actuals are assembled in the instance. For subsequent
8141 -- checks of other actuals, indicate the node that will hold the
8142 -- instance of this formal.
8144 Set_Instance_Of (Analyzed_S, Nam);
8146 if Nkind (Actual) = N_Selected_Component
8147 and then Is_Task_Type (Etype (Prefix (Actual)))
8148 and then not Is_Frozen (Etype (Prefix (Actual)))
8149 then
8150 -- The renaming declaration will create a body, which must appear
8151 -- outside of the instantiation, We move the renaming declaration
8152 -- out of the instance, and create an additional renaming inside,
8153 -- to prevent freezing anomalies.
8155 declare
8156 Anon_Id : constant Entity_Id :=
8157 Make_Defining_Identifier
8158 (Loc, New_Internal_Name ('E'));
8159 begin
8160 Set_Defining_Unit_Name (New_Spec, Anon_Id);
8161 Insert_Before (Instantiation_Node, Decl_Node);
8162 Analyze (Decl_Node);
8164 -- Now create renaming within the instance
8166 Decl_Node :=
8167 Make_Subprogram_Renaming_Declaration (Loc,
8168 Specification => New_Copy_Tree (New_Spec),
8169 Name => New_Occurrence_Of (Anon_Id, Loc));
8171 Set_Defining_Unit_Name (Specification (Decl_Node),
8172 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
8173 end;
8174 end if;
8176 return Decl_Node;
8177 end Instantiate_Formal_Subprogram;
8179 ------------------------
8180 -- Instantiate_Object --
8181 ------------------------
8183 function Instantiate_Object
8184 (Formal : Node_Id;
8185 Actual : Node_Id;
8186 Analyzed_Formal : Node_Id) return List_Id
8188 Acc_Def : Node_Id := Empty;
8189 Act_Assoc : constant Node_Id := Parent (Actual);
8190 Actual_Decl : Node_Id := Empty;
8191 Formal_Id : constant Entity_Id := Defining_Identifier (Formal);
8192 Decl_Node : Node_Id;
8193 Def : Node_Id;
8194 Ftyp : Entity_Id;
8195 List : constant List_Id := New_List;
8196 Loc : constant Source_Ptr := Sloc (Actual);
8197 Orig_Ftyp : constant Entity_Id :=
8198 Etype (Defining_Identifier (Analyzed_Formal));
8199 Subt_Decl : Node_Id := Empty;
8200 Subt_Mark : Node_Id := Empty;
8202 begin
8203 if Present (Subtype_Mark (Formal)) then
8204 Subt_Mark := Subtype_Mark (Formal);
8205 else
8206 Check_Access_Definition (Formal);
8207 Acc_Def := Access_Definition (Formal);
8208 end if;
8210 -- Sloc for error message on missing actual
8212 Error_Msg_Sloc := Sloc (Scope (Defining_Identifier (Analyzed_Formal)));
8214 if Get_Instance_Of (Formal_Id) /= Formal_Id then
8215 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
8216 end if;
8218 Set_Parent (List, Parent (Actual));
8220 -- OUT present
8222 if Out_Present (Formal) then
8224 -- An IN OUT generic actual must be a name. The instantiation is a
8225 -- renaming declaration. The actual is the name being renamed. We
8226 -- use the actual directly, rather than a copy, because it is not
8227 -- used further in the list of actuals, and because a copy or a use
8228 -- of relocate_node is incorrect if the instance is nested within a
8229 -- generic. In order to simplify ASIS searches, the Generic_Parent
8230 -- field links the declaration to the generic association.
8232 if No (Actual) then
8233 Error_Msg_NE
8234 ("missing actual&",
8235 Instantiation_Node, Formal_Id);
8236 Error_Msg_NE
8237 ("\in instantiation of & declared#",
8238 Instantiation_Node,
8239 Scope (Defining_Identifier (Analyzed_Formal)));
8240 Abandon_Instantiation (Instantiation_Node);
8241 end if;
8243 if Present (Subt_Mark) then
8244 Decl_Node :=
8245 Make_Object_Renaming_Declaration (Loc,
8246 Defining_Identifier => New_Copy (Formal_Id),
8247 Subtype_Mark => New_Copy_Tree (Subt_Mark),
8248 Name => Actual);
8250 else pragma Assert (Present (Acc_Def));
8251 Decl_Node :=
8252 Make_Object_Renaming_Declaration (Loc,
8253 Defining_Identifier => New_Copy (Formal_Id),
8254 Access_Definition => New_Copy_Tree (Acc_Def),
8255 Name => Actual);
8256 end if;
8258 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
8260 -- The analysis of the actual may produce insert_action nodes, so
8261 -- the declaration must have a context in which to attach them.
8263 Append (Decl_Node, List);
8264 Analyze (Actual);
8266 -- Return if the analysis of the actual reported some error
8268 if Etype (Actual) = Any_Type then
8269 return List;
8270 end if;
8272 -- This check is performed here because Analyze_Object_Renaming will
8273 -- not check it when Comes_From_Source is False. Note though that the
8274 -- check for the actual being the name of an object will be performed
8275 -- in Analyze_Object_Renaming.
8277 if Is_Object_Reference (Actual)
8278 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
8279 then
8280 Error_Msg_N
8281 ("illegal discriminant-dependent component for in out parameter",
8282 Actual);
8283 end if;
8285 -- The actual has to be resolved in order to check that it is a
8286 -- variable (due to cases such as F(1), where F returns
8287 -- access to an array, and for overloaded prefixes).
8289 Ftyp :=
8290 Get_Instance_Of (Etype (Defining_Identifier (Analyzed_Formal)));
8292 if Is_Private_Type (Ftyp)
8293 and then not Is_Private_Type (Etype (Actual))
8294 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
8295 or else Base_Type (Etype (Actual)) = Ftyp)
8296 then
8297 -- If the actual has the type of the full view of the formal, or
8298 -- else a non-private subtype of the formal, then the visibility
8299 -- of the formal type has changed. Add to the actuals a subtype
8300 -- declaration that will force the exchange of views in the body
8301 -- of the instance as well.
8303 Subt_Decl :=
8304 Make_Subtype_Declaration (Loc,
8305 Defining_Identifier =>
8306 Make_Defining_Identifier (Loc, New_Internal_Name ('P')),
8307 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
8309 Prepend (Subt_Decl, List);
8311 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
8312 Exchange_Declarations (Ftyp);
8313 end if;
8315 Resolve (Actual, Ftyp);
8317 if not Denotes_Variable (Actual) then
8318 Error_Msg_NE
8319 ("actual for& must be a variable", Actual, Formal_Id);
8321 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
8323 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
8324 -- the type of the actual shall resolve to a specific anonymous
8325 -- access type.
8327 if Ada_Version < Ada_05
8328 or else
8329 Ekind (Base_Type (Ftyp)) /=
8330 E_Anonymous_Access_Type
8331 or else
8332 Ekind (Base_Type (Etype (Actual))) /=
8333 E_Anonymous_Access_Type
8334 then
8335 Error_Msg_NE ("type of actual does not match type of&",
8336 Actual, Formal_Id);
8337 end if;
8338 end if;
8340 Note_Possible_Modification (Actual, Sure => True);
8342 -- Check for instantiation of atomic/volatile actual for
8343 -- non-atomic/volatile formal (RM C.6 (12)).
8345 if Is_Atomic_Object (Actual)
8346 and then not Is_Atomic (Orig_Ftyp)
8347 then
8348 Error_Msg_N
8349 ("cannot instantiate non-atomic formal object " &
8350 "with atomic actual", Actual);
8352 elsif Is_Volatile_Object (Actual)
8353 and then not Is_Volatile (Orig_Ftyp)
8354 then
8355 Error_Msg_N
8356 ("cannot instantiate non-volatile formal object " &
8357 "with volatile actual", Actual);
8358 end if;
8360 -- Formal in-parameter
8362 else
8363 -- The instantiation of a generic formal in-parameter is constant
8364 -- declaration. The actual is the expression for that declaration.
8366 if Present (Actual) then
8367 if Present (Subt_Mark) then
8368 Def := Subt_Mark;
8369 else pragma Assert (Present (Acc_Def));
8370 Def := Acc_Def;
8371 end if;
8373 Decl_Node :=
8374 Make_Object_Declaration (Loc,
8375 Defining_Identifier => New_Copy (Formal_Id),
8376 Constant_Present => True,
8377 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
8378 Object_Definition => New_Copy_Tree (Def),
8379 Expression => Actual);
8381 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
8383 -- A generic formal object of a tagged type is defined to be
8384 -- aliased so the new constant must also be treated as aliased.
8386 if Is_Tagged_Type
8387 (Etype (Defining_Identifier (Analyzed_Formal)))
8388 then
8389 Set_Aliased_Present (Decl_Node);
8390 end if;
8392 Append (Decl_Node, List);
8394 -- No need to repeat (pre-)analysis of some expression nodes
8395 -- already handled in Preanalyze_Actuals.
8397 if Nkind (Actual) /= N_Allocator then
8398 Analyze (Actual);
8400 -- Return if the analysis of the actual reported some error
8402 if Etype (Actual) = Any_Type then
8403 return List;
8404 end if;
8405 end if;
8407 declare
8408 Formal_Object : constant Entity_Id :=
8409 Defining_Identifier (Analyzed_Formal);
8410 Formal_Type : constant Entity_Id := Etype (Formal_Object);
8412 Typ : Entity_Id;
8414 begin
8415 Typ := Get_Instance_Of (Formal_Type);
8417 Freeze_Before (Instantiation_Node, Typ);
8419 -- If the actual is an aggregate, perform name resolution on
8420 -- its components (the analysis of an aggregate does not do it)
8421 -- to capture local names that may be hidden if the generic is
8422 -- a child unit.
8424 if Nkind (Actual) = N_Aggregate then
8425 Preanalyze_And_Resolve (Actual, Typ);
8426 end if;
8428 if Is_Limited_Type (Typ)
8429 and then not OK_For_Limited_Init (Typ, Actual)
8430 then
8431 Error_Msg_N
8432 ("initialization not allowed for limited types", Actual);
8433 Explain_Limited_Type (Typ, Actual);
8434 end if;
8435 end;
8437 elsif Present (Default_Expression (Formal)) then
8439 -- Use default to construct declaration
8441 if Present (Subt_Mark) then
8442 Def := Subt_Mark;
8443 else pragma Assert (Present (Acc_Def));
8444 Def := Acc_Def;
8445 end if;
8447 Decl_Node :=
8448 Make_Object_Declaration (Sloc (Formal),
8449 Defining_Identifier => New_Copy (Formal_Id),
8450 Constant_Present => True,
8451 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
8452 Object_Definition => New_Copy (Def),
8453 Expression => New_Copy_Tree
8454 (Default_Expression (Formal)));
8456 Append (Decl_Node, List);
8457 Set_Analyzed (Expression (Decl_Node), False);
8459 else
8460 Error_Msg_NE
8461 ("missing actual&",
8462 Instantiation_Node, Formal_Id);
8463 Error_Msg_NE ("\in instantiation of & declared#",
8464 Instantiation_Node,
8465 Scope (Defining_Identifier (Analyzed_Formal)));
8467 if Is_Scalar_Type
8468 (Etype (Defining_Identifier (Analyzed_Formal)))
8469 then
8470 -- Create dummy constant declaration so that instance can be
8471 -- analyzed, to minimize cascaded visibility errors.
8473 if Present (Subt_Mark) then
8474 Def := Subt_Mark;
8475 else pragma Assert (Present (Acc_Def));
8476 Def := Acc_Def;
8477 end if;
8479 Decl_Node :=
8480 Make_Object_Declaration (Loc,
8481 Defining_Identifier => New_Copy (Formal_Id),
8482 Constant_Present => True,
8483 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
8484 Object_Definition => New_Copy (Def),
8485 Expression =>
8486 Make_Attribute_Reference (Sloc (Formal_Id),
8487 Attribute_Name => Name_First,
8488 Prefix => New_Copy (Def)));
8490 Append (Decl_Node, List);
8492 else
8493 Abandon_Instantiation (Instantiation_Node);
8494 end if;
8495 end if;
8496 end if;
8498 if Nkind (Actual) in N_Has_Entity then
8499 Actual_Decl := Parent (Entity (Actual));
8500 end if;
8502 -- Ada 2005 (AI-423): For a formal object declaration with a null
8503 -- exclusion or an access definition that has a null exclusion: If the
8504 -- actual matching the formal object declaration denotes a generic
8505 -- formal object of another generic unit G, and the instantiation
8506 -- containing the actual occurs within the body of G or within the body
8507 -- of a generic unit declared within the declarative region of G, then
8508 -- the declaration of the formal object of G must have a null exclusion.
8509 -- Otherwise, the subtype of the actual matching the formal object
8510 -- declaration shall exclude null.
8512 if Ada_Version >= Ada_05
8513 and then Present (Actual_Decl)
8514 and then
8515 Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
8516 N_Object_Declaration)
8517 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
8518 and then not Has_Null_Exclusion (Actual_Decl)
8519 and then Has_Null_Exclusion (Analyzed_Formal)
8520 then
8521 Error_Msg_Sloc := Sloc (Analyzed_Formal);
8522 Error_Msg_N
8523 ("actual must exclude null to match generic formal#", Actual);
8524 end if;
8526 return List;
8527 end Instantiate_Object;
8529 ------------------------------
8530 -- Instantiate_Package_Body --
8531 ------------------------------
8533 procedure Instantiate_Package_Body
8534 (Body_Info : Pending_Body_Info;
8535 Inlined_Body : Boolean := False;
8536 Body_Optional : Boolean := False)
8538 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
8539 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
8540 Loc : constant Source_Ptr := Sloc (Inst_Node);
8542 Gen_Id : constant Node_Id := Name (Inst_Node);
8543 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8544 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
8545 Act_Spec : constant Node_Id := Specification (Act_Decl);
8546 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
8548 Act_Body_Name : Node_Id;
8549 Gen_Body : Node_Id;
8550 Gen_Body_Id : Node_Id;
8551 Act_Body : Node_Id;
8552 Act_Body_Id : Entity_Id;
8554 Parent_Installed : Boolean := False;
8555 Save_Style_Check : constant Boolean := Style_Check;
8557 Par_Ent : Entity_Id := Empty;
8558 Par_Vis : Boolean := False;
8560 begin
8561 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8563 -- The instance body may already have been processed, as the parent of
8564 -- another instance that is inlined (Load_Parent_Of_Generic).
8566 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
8567 return;
8568 end if;
8570 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
8572 -- Re-establish the state of information on which checks are suppressed.
8573 -- This information was set in Body_Info at the point of instantiation,
8574 -- and now we restore it so that the instance is compiled using the
8575 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
8577 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
8578 Scope_Suppress := Body_Info.Scope_Suppress;
8580 if No (Gen_Body_Id) then
8581 Load_Parent_Of_Generic
8582 (Inst_Node, Specification (Gen_Decl), Body_Optional);
8583 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8584 end if;
8586 -- Establish global variable for sloc adjustment and for error recovery
8588 Instantiation_Node := Inst_Node;
8590 if Present (Gen_Body_Id) then
8591 Save_Env (Gen_Unit, Act_Decl_Id);
8592 Style_Check := False;
8593 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
8595 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
8597 Create_Instantiation_Source
8598 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
8600 Act_Body :=
8601 Copy_Generic_Node
8602 (Original_Node (Gen_Body), Empty, Instantiating => True);
8604 -- Build new name (possibly qualified) for body declaration
8606 Act_Body_Id := New_Copy (Act_Decl_Id);
8608 -- Some attributes of spec entity are not inherited by body entity
8610 Set_Handler_Records (Act_Body_Id, No_List);
8612 if Nkind (Defining_Unit_Name (Act_Spec)) =
8613 N_Defining_Program_Unit_Name
8614 then
8615 Act_Body_Name :=
8616 Make_Defining_Program_Unit_Name (Loc,
8617 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
8618 Defining_Identifier => Act_Body_Id);
8619 else
8620 Act_Body_Name := Act_Body_Id;
8621 end if;
8623 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
8625 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
8626 Check_Generic_Actuals (Act_Decl_Id, False);
8628 -- If it is a child unit, make the parent instance (which is an
8629 -- instance of the parent of the generic) visible. The parent
8630 -- instance is the prefix of the name of the generic unit.
8632 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
8633 and then Nkind (Gen_Id) = N_Expanded_Name
8634 then
8635 Par_Ent := Entity (Prefix (Gen_Id));
8636 Par_Vis := Is_Immediately_Visible (Par_Ent);
8637 Install_Parent (Par_Ent, In_Body => True);
8638 Parent_Installed := True;
8640 elsif Is_Child_Unit (Gen_Unit) then
8641 Par_Ent := Scope (Gen_Unit);
8642 Par_Vis := Is_Immediately_Visible (Par_Ent);
8643 Install_Parent (Par_Ent, In_Body => True);
8644 Parent_Installed := True;
8645 end if;
8647 -- If the instantiation is a library unit, and this is the main unit,
8648 -- then build the resulting compilation unit nodes for the instance.
8649 -- If this is a compilation unit but it is not the main unit, then it
8650 -- is the body of a unit in the context, that is being compiled
8651 -- because it is encloses some inlined unit or another generic unit
8652 -- being instantiated. In that case, this body is not part of the
8653 -- current compilation, and is not attached to the tree, but its
8654 -- parent must be set for analysis.
8656 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
8658 -- Replace instance node with body of instance, and create new
8659 -- node for corresponding instance declaration.
8661 Build_Instance_Compilation_Unit_Nodes
8662 (Inst_Node, Act_Body, Act_Decl);
8663 Analyze (Inst_Node);
8665 if Parent (Inst_Node) = Cunit (Main_Unit) then
8667 -- If the instance is a child unit itself, then set the scope
8668 -- of the expanded body to be the parent of the instantiation
8669 -- (ensuring that the fully qualified name will be generated
8670 -- for the elaboration subprogram).
8672 if Nkind (Defining_Unit_Name (Act_Spec)) =
8673 N_Defining_Program_Unit_Name
8674 then
8675 Set_Scope
8676 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
8677 end if;
8678 end if;
8680 -- Case where instantiation is not a library unit
8682 else
8683 -- If this is an early instantiation, i.e. appears textually
8684 -- before the corresponding body and must be elaborated first,
8685 -- indicate that the body instance is to be delayed.
8687 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
8689 -- Now analyze the body. We turn off all checks if this is an
8690 -- internal unit, since there is no reason to have checks on for
8691 -- any predefined run-time library code. All such code is designed
8692 -- to be compiled with checks off.
8694 -- Note that we do NOT apply this criterion to children of GNAT
8695 -- (or on VMS, children of DEC). The latter units must suppress
8696 -- checks explicitly if this is needed.
8698 if Is_Predefined_File_Name
8699 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
8700 then
8701 Analyze (Act_Body, Suppress => All_Checks);
8702 else
8703 Analyze (Act_Body);
8704 end if;
8705 end if;
8707 Inherit_Context (Gen_Body, Inst_Node);
8709 -- Remove the parent instances if they have been placed on the scope
8710 -- stack to compile the body.
8712 if Parent_Installed then
8713 Remove_Parent (In_Body => True);
8715 -- Restore the previous visibility of the parent
8717 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
8718 end if;
8720 Restore_Private_Views (Act_Decl_Id);
8722 -- Remove the current unit from visibility if this is an instance
8723 -- that is not elaborated on the fly for inlining purposes.
8725 if not Inlined_Body then
8726 Set_Is_Immediately_Visible (Act_Decl_Id, False);
8727 end if;
8729 Restore_Env;
8730 Style_Check := Save_Style_Check;
8732 -- If we have no body, and the unit requires a body, then complain. This
8733 -- complaint is suppressed if we have detected other errors (since a
8734 -- common reason for missing the body is that it had errors).
8736 elsif Unit_Requires_Body (Gen_Unit)
8737 and then not Body_Optional
8738 then
8739 if Serious_Errors_Detected = 0 then
8740 Error_Msg_NE
8741 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
8743 -- Don't attempt to perform any cleanup actions if some other error
8744 -- was already detected, since this can cause blowups.
8746 else
8747 return;
8748 end if;
8750 -- Case of package that does not need a body
8752 else
8753 -- If the instantiation of the declaration is a library unit, rewrite
8754 -- the original package instantiation as a package declaration in the
8755 -- compilation unit node.
8757 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
8758 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
8759 Rewrite (Inst_Node, Act_Decl);
8761 -- Generate elaboration entity, in case spec has elaboration code.
8762 -- This cannot be done when the instance is analyzed, because it
8763 -- is not known yet whether the body exists.
8765 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
8766 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
8768 -- If the instantiation is not a library unit, then append the
8769 -- declaration to the list of implicitly generated entities, unless
8770 -- it is already a list member which means that it was already
8771 -- processed
8773 elsif not Is_List_Member (Act_Decl) then
8774 Mark_Rewrite_Insertion (Act_Decl);
8775 Insert_Before (Inst_Node, Act_Decl);
8776 end if;
8777 end if;
8779 Expander_Mode_Restore;
8780 end Instantiate_Package_Body;
8782 ---------------------------------
8783 -- Instantiate_Subprogram_Body --
8784 ---------------------------------
8786 procedure Instantiate_Subprogram_Body
8787 (Body_Info : Pending_Body_Info;
8788 Body_Optional : Boolean := False)
8790 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
8791 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
8792 Loc : constant Source_Ptr := Sloc (Inst_Node);
8793 Gen_Id : constant Node_Id := Name (Inst_Node);
8794 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8795 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
8796 Anon_Id : constant Entity_Id :=
8797 Defining_Unit_Name (Specification (Act_Decl));
8798 Pack_Id : constant Entity_Id :=
8799 Defining_Unit_Name (Parent (Act_Decl));
8800 Decls : List_Id;
8801 Gen_Body : Node_Id;
8802 Gen_Body_Id : Node_Id;
8803 Act_Body : Node_Id;
8804 Pack_Body : Node_Id;
8805 Prev_Formal : Entity_Id;
8806 Ret_Expr : Node_Id;
8807 Unit_Renaming : Node_Id;
8809 Parent_Installed : Boolean := False;
8810 Save_Style_Check : constant Boolean := Style_Check;
8812 Par_Ent : Entity_Id := Empty;
8813 Par_Vis : Boolean := False;
8815 begin
8816 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8818 -- Subprogram body may have been created already because of an inline
8819 -- pragma, or because of multiple elaborations of the enclosing package
8820 -- when several instances of the subprogram appear in the main unit.
8822 if Present (Corresponding_Body (Act_Decl)) then
8823 return;
8824 end if;
8826 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
8828 -- Re-establish the state of information on which checks are suppressed.
8829 -- This information was set in Body_Info at the point of instantiation,
8830 -- and now we restore it so that the instance is compiled using the
8831 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
8833 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
8834 Scope_Suppress := Body_Info.Scope_Suppress;
8836 if No (Gen_Body_Id) then
8838 -- For imported generic subprogram, no body to compile, complete
8839 -- the spec entity appropriately.
8841 if Is_Imported (Gen_Unit) then
8842 Set_Is_Imported (Anon_Id);
8843 Set_First_Rep_Item (Anon_Id, First_Rep_Item (Gen_Unit));
8844 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
8845 Set_Convention (Anon_Id, Convention (Gen_Unit));
8846 Set_Has_Completion (Anon_Id);
8847 return;
8849 -- For other cases, compile the body
8851 else
8852 Load_Parent_Of_Generic
8853 (Inst_Node, Specification (Gen_Decl), Body_Optional);
8854 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8855 end if;
8856 end if;
8858 Instantiation_Node := Inst_Node;
8860 if Present (Gen_Body_Id) then
8861 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
8863 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
8865 -- Either body is not present, or context is non-expanding, as
8866 -- when compiling a subunit. Mark the instance as completed, and
8867 -- diagnose a missing body when needed.
8869 if Expander_Active
8870 and then Operating_Mode = Generate_Code
8871 then
8872 Error_Msg_N
8873 ("missing proper body for instantiation", Gen_Body);
8874 end if;
8876 Set_Has_Completion (Anon_Id);
8877 return;
8878 end if;
8880 Save_Env (Gen_Unit, Anon_Id);
8881 Style_Check := False;
8882 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
8883 Create_Instantiation_Source
8884 (Inst_Node,
8885 Gen_Body_Id,
8886 False,
8887 S_Adjustment);
8889 Act_Body :=
8890 Copy_Generic_Node
8891 (Original_Node (Gen_Body), Empty, Instantiating => True);
8893 -- Create proper defining name for the body, to correspond to
8894 -- the one in the spec.
8896 Set_Defining_Unit_Name (Specification (Act_Body),
8897 Make_Defining_Identifier
8898 (Sloc (Defining_Entity (Inst_Node)), Chars (Anon_Id)));
8899 Set_Corresponding_Spec (Act_Body, Anon_Id);
8900 Set_Has_Completion (Anon_Id);
8901 Check_Generic_Actuals (Pack_Id, False);
8903 -- Generate a reference to link the visible subprogram instance to
8904 -- the generic body, which for navigation purposes is the only
8905 -- available source for the instance.
8907 Generate_Reference
8908 (Related_Instance (Pack_Id),
8909 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
8911 -- If it is a child unit, make the parent instance (which is an
8912 -- instance of the parent of the generic) visible. The parent
8913 -- instance is the prefix of the name of the generic unit.
8915 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
8916 and then Nkind (Gen_Id) = N_Expanded_Name
8917 then
8918 Par_Ent := Entity (Prefix (Gen_Id));
8919 Par_Vis := Is_Immediately_Visible (Par_Ent);
8920 Install_Parent (Par_Ent, In_Body => True);
8921 Parent_Installed := True;
8923 elsif Is_Child_Unit (Gen_Unit) then
8924 Par_Ent := Scope (Gen_Unit);
8925 Par_Vis := Is_Immediately_Visible (Par_Ent);
8926 Install_Parent (Par_Ent, In_Body => True);
8927 Parent_Installed := True;
8928 end if;
8930 -- Inside its body, a reference to the generic unit is a reference
8931 -- to the instance. The corresponding renaming is the first
8932 -- declaration in the body.
8934 Unit_Renaming :=
8935 Make_Subprogram_Renaming_Declaration (Loc,
8936 Specification =>
8937 Copy_Generic_Node (
8938 Specification (Original_Node (Gen_Body)),
8939 Empty,
8940 Instantiating => True),
8941 Name => New_Occurrence_Of (Anon_Id, Loc));
8943 -- If there is a formal subprogram with the same name as the unit
8944 -- itself, do not add this renaming declaration. This is a temporary
8945 -- fix for one ACVC test. ???
8947 Prev_Formal := First_Entity (Pack_Id);
8948 while Present (Prev_Formal) loop
8949 if Chars (Prev_Formal) = Chars (Gen_Unit)
8950 and then Is_Overloadable (Prev_Formal)
8951 then
8952 exit;
8953 end if;
8955 Next_Entity (Prev_Formal);
8956 end loop;
8958 if Present (Prev_Formal) then
8959 Decls := New_List (Act_Body);
8960 else
8961 Decls := New_List (Unit_Renaming, Act_Body);
8962 end if;
8964 -- The subprogram body is placed in the body of a dummy package body,
8965 -- whose spec contains the subprogram declaration as well as the
8966 -- renaming declarations for the generic parameters.
8968 Pack_Body := Make_Package_Body (Loc,
8969 Defining_Unit_Name => New_Copy (Pack_Id),
8970 Declarations => Decls);
8972 Set_Corresponding_Spec (Pack_Body, Pack_Id);
8974 -- If the instantiation is a library unit, then build resulting
8975 -- compilation unit nodes for the instance. The declaration of
8976 -- the enclosing package is the grandparent of the subprogram
8977 -- declaration. First replace the instantiation node as the unit
8978 -- of the corresponding compilation.
8980 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
8981 if Parent (Inst_Node) = Cunit (Main_Unit) then
8982 Set_Unit (Parent (Inst_Node), Inst_Node);
8983 Build_Instance_Compilation_Unit_Nodes
8984 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
8985 Analyze (Inst_Node);
8986 else
8987 Set_Parent (Pack_Body, Parent (Inst_Node));
8988 Analyze (Pack_Body);
8989 end if;
8991 else
8992 Insert_Before (Inst_Node, Pack_Body);
8993 Mark_Rewrite_Insertion (Pack_Body);
8994 Analyze (Pack_Body);
8996 if Expander_Active then
8997 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
8998 end if;
8999 end if;
9001 Inherit_Context (Gen_Body, Inst_Node);
9003 Restore_Private_Views (Pack_Id, False);
9005 if Parent_Installed then
9006 Remove_Parent (In_Body => True);
9008 -- Restore the previous visibility of the parent
9010 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
9011 end if;
9013 Restore_Env;
9014 Style_Check := Save_Style_Check;
9016 -- Body not found. Error was emitted already. If there were no previous
9017 -- errors, this may be an instance whose scope is a premature instance.
9018 -- In that case we must insure that the (legal) program does raise
9019 -- program error if executed. We generate a subprogram body for this
9020 -- purpose. See DEC ac30vso.
9022 -- Should not reference proprietary DEC tests in comments ???
9024 elsif Serious_Errors_Detected = 0
9025 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
9026 then
9027 if Body_Optional then
9028 return;
9030 elsif Ekind (Anon_Id) = E_Procedure then
9031 Act_Body :=
9032 Make_Subprogram_Body (Loc,
9033 Specification =>
9034 Make_Procedure_Specification (Loc,
9035 Defining_Unit_Name =>
9036 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
9037 Parameter_Specifications =>
9038 New_Copy_List
9039 (Parameter_Specifications (Parent (Anon_Id)))),
9041 Declarations => Empty_List,
9042 Handled_Statement_Sequence =>
9043 Make_Handled_Sequence_Of_Statements (Loc,
9044 Statements =>
9045 New_List (
9046 Make_Raise_Program_Error (Loc,
9047 Reason =>
9048 PE_Access_Before_Elaboration))));
9050 else
9051 Ret_Expr :=
9052 Make_Raise_Program_Error (Loc,
9053 Reason => PE_Access_Before_Elaboration);
9055 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
9056 Set_Analyzed (Ret_Expr);
9058 Act_Body :=
9059 Make_Subprogram_Body (Loc,
9060 Specification =>
9061 Make_Function_Specification (Loc,
9062 Defining_Unit_Name =>
9063 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
9064 Parameter_Specifications =>
9065 New_Copy_List
9066 (Parameter_Specifications (Parent (Anon_Id))),
9067 Result_Definition =>
9068 New_Occurrence_Of (Etype (Anon_Id), Loc)),
9070 Declarations => Empty_List,
9071 Handled_Statement_Sequence =>
9072 Make_Handled_Sequence_Of_Statements (Loc,
9073 Statements =>
9074 New_List
9075 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
9076 end if;
9078 Pack_Body := Make_Package_Body (Loc,
9079 Defining_Unit_Name => New_Copy (Pack_Id),
9080 Declarations => New_List (Act_Body));
9082 Insert_After (Inst_Node, Pack_Body);
9083 Set_Corresponding_Spec (Pack_Body, Pack_Id);
9084 Analyze (Pack_Body);
9085 end if;
9087 Expander_Mode_Restore;
9088 end Instantiate_Subprogram_Body;
9090 ----------------------
9091 -- Instantiate_Type --
9092 ----------------------
9094 function Instantiate_Type
9095 (Formal : Node_Id;
9096 Actual : Node_Id;
9097 Analyzed_Formal : Node_Id;
9098 Actual_Decls : List_Id) return List_Id
9100 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
9101 A_Gen_T : constant Entity_Id :=
9102 Defining_Identifier (Analyzed_Formal);
9103 Ancestor : Entity_Id := Empty;
9104 Def : constant Node_Id := Formal_Type_Definition (Formal);
9105 Act_T : Entity_Id;
9106 Decl_Node : Node_Id;
9107 Decl_Nodes : List_Id;
9108 Loc : Source_Ptr;
9109 Subt : Entity_Id;
9111 procedure Validate_Array_Type_Instance;
9112 procedure Validate_Access_Subprogram_Instance;
9113 procedure Validate_Access_Type_Instance;
9114 procedure Validate_Derived_Type_Instance;
9115 procedure Validate_Derived_Interface_Type_Instance;
9116 procedure Validate_Interface_Type_Instance;
9117 procedure Validate_Private_Type_Instance;
9118 -- These procedures perform validation tests for the named case
9120 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
9121 -- Check that base types are the same and that the subtypes match
9122 -- statically. Used in several of the above.
9124 --------------------
9125 -- Subtypes_Match --
9126 --------------------
9128 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
9129 T : constant Entity_Id := Get_Instance_Of (Gen_T);
9131 begin
9132 return (Base_Type (T) = Base_Type (Act_T)
9133 and then Subtypes_Statically_Match (T, Act_T))
9135 or else (Is_Class_Wide_Type (Gen_T)
9136 and then Is_Class_Wide_Type (Act_T)
9137 and then
9138 Subtypes_Match
9139 (Get_Instance_Of (Root_Type (Gen_T)),
9140 Root_Type (Act_T)))
9142 or else
9143 ((Ekind (Gen_T) = E_Anonymous_Access_Subprogram_Type
9144 or else Ekind (Gen_T) = E_Anonymous_Access_Type)
9145 and then Ekind (Act_T) = Ekind (Gen_T)
9146 and then
9147 Subtypes_Statically_Match
9148 (Designated_Type (Gen_T), Designated_Type (Act_T)));
9149 end Subtypes_Match;
9151 -----------------------------------------
9152 -- Validate_Access_Subprogram_Instance --
9153 -----------------------------------------
9155 procedure Validate_Access_Subprogram_Instance is
9156 begin
9157 if not Is_Access_Type (Act_T)
9158 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
9159 then
9160 Error_Msg_NE
9161 ("expect access type in instantiation of &", Actual, Gen_T);
9162 Abandon_Instantiation (Actual);
9163 end if;
9165 Check_Mode_Conformant
9166 (Designated_Type (Act_T),
9167 Designated_Type (A_Gen_T),
9168 Actual,
9169 Get_Inst => True);
9171 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
9172 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
9173 Error_Msg_NE
9174 ("protected access type not allowed for formal &",
9175 Actual, Gen_T);
9176 end if;
9178 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
9179 Error_Msg_NE
9180 ("expect protected access type for formal &",
9181 Actual, Gen_T);
9182 end if;
9183 end Validate_Access_Subprogram_Instance;
9185 -----------------------------------
9186 -- Validate_Access_Type_Instance --
9187 -----------------------------------
9189 procedure Validate_Access_Type_Instance is
9190 Desig_Type : constant Entity_Id :=
9191 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
9192 Desig_Act : Entity_Id;
9194 begin
9195 if not Is_Access_Type (Act_T) then
9196 Error_Msg_NE
9197 ("expect access type in instantiation of &", Actual, Gen_T);
9198 Abandon_Instantiation (Actual);
9199 end if;
9201 if Is_Access_Constant (A_Gen_T) then
9202 if not Is_Access_Constant (Act_T) then
9203 Error_Msg_N
9204 ("actual type must be access-to-constant type", Actual);
9205 Abandon_Instantiation (Actual);
9206 end if;
9207 else
9208 if Is_Access_Constant (Act_T) then
9209 Error_Msg_N
9210 ("actual type must be access-to-variable type", Actual);
9211 Abandon_Instantiation (Actual);
9213 elsif Ekind (A_Gen_T) = E_General_Access_Type
9214 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
9215 then
9216 Error_Msg_N ("actual must be general access type!", Actual);
9217 Error_Msg_NE ("add ALL to }!", Actual, Act_T);
9218 Abandon_Instantiation (Actual);
9219 end if;
9220 end if;
9222 -- The designated subtypes, that is to say the subtypes introduced
9223 -- by an access type declaration (and not by a subtype declaration)
9224 -- must match.
9226 Desig_Act := Designated_Type (Base_Type (Act_T));
9228 -- The designated type may have been introduced through a limited_
9229 -- with clause, in which case retrieve the non-limited view. This
9230 -- applies to incomplete types as well as to class-wide types.
9232 if From_With_Type (Desig_Act) then
9233 Desig_Act := Available_View (Desig_Act);
9234 end if;
9236 if not Subtypes_Match
9237 (Desig_Type, Desig_Act) then
9238 Error_Msg_NE
9239 ("designated type of actual does not match that of formal &",
9240 Actual, Gen_T);
9241 Abandon_Instantiation (Actual);
9243 elsif Is_Access_Type (Designated_Type (Act_T))
9244 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
9246 Is_Constrained (Designated_Type (Desig_Type))
9247 then
9248 Error_Msg_NE
9249 ("designated type of actual does not match that of formal &",
9250 Actual, Gen_T);
9251 Abandon_Instantiation (Actual);
9252 end if;
9254 -- Ada 2005: null-exclusion indicators of the two types must agree
9256 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
9257 Error_Msg_NE
9258 ("non null exclusion of actual and formal & do not match",
9259 Actual, Gen_T);
9260 end if;
9261 end Validate_Access_Type_Instance;
9263 ----------------------------------
9264 -- Validate_Array_Type_Instance --
9265 ----------------------------------
9267 procedure Validate_Array_Type_Instance is
9268 I1 : Node_Id;
9269 I2 : Node_Id;
9270 T2 : Entity_Id;
9272 function Formal_Dimensions return Int;
9273 -- Count number of dimensions in array type formal
9275 -----------------------
9276 -- Formal_Dimensions --
9277 -----------------------
9279 function Formal_Dimensions return Int is
9280 Num : Int := 0;
9281 Index : Node_Id;
9283 begin
9284 if Nkind (Def) = N_Constrained_Array_Definition then
9285 Index := First (Discrete_Subtype_Definitions (Def));
9286 else
9287 Index := First (Subtype_Marks (Def));
9288 end if;
9290 while Present (Index) loop
9291 Num := Num + 1;
9292 Next_Index (Index);
9293 end loop;
9295 return Num;
9296 end Formal_Dimensions;
9298 -- Start of processing for Validate_Array_Type_Instance
9300 begin
9301 if not Is_Array_Type (Act_T) then
9302 Error_Msg_NE
9303 ("expect array type in instantiation of &", Actual, Gen_T);
9304 Abandon_Instantiation (Actual);
9306 elsif Nkind (Def) = N_Constrained_Array_Definition then
9307 if not (Is_Constrained (Act_T)) then
9308 Error_Msg_NE
9309 ("expect constrained array in instantiation of &",
9310 Actual, Gen_T);
9311 Abandon_Instantiation (Actual);
9312 end if;
9314 else
9315 if Is_Constrained (Act_T) then
9316 Error_Msg_NE
9317 ("expect unconstrained array in instantiation of &",
9318 Actual, Gen_T);
9319 Abandon_Instantiation (Actual);
9320 end if;
9321 end if;
9323 if Formal_Dimensions /= Number_Dimensions (Act_T) then
9324 Error_Msg_NE
9325 ("dimensions of actual do not match formal &", Actual, Gen_T);
9326 Abandon_Instantiation (Actual);
9327 end if;
9329 I1 := First_Index (A_Gen_T);
9330 I2 := First_Index (Act_T);
9331 for J in 1 .. Formal_Dimensions loop
9333 -- If the indices of the actual were given by a subtype_mark,
9334 -- the index was transformed into a range attribute. Retrieve
9335 -- the original type mark for checking.
9337 if Is_Entity_Name (Original_Node (I2)) then
9338 T2 := Entity (Original_Node (I2));
9339 else
9340 T2 := Etype (I2);
9341 end if;
9343 if not Subtypes_Match
9344 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
9345 then
9346 Error_Msg_NE
9347 ("index types of actual do not match those of formal &",
9348 Actual, Gen_T);
9349 Abandon_Instantiation (Actual);
9350 end if;
9352 Next_Index (I1);
9353 Next_Index (I2);
9354 end loop;
9356 -- Check matching subtypes. Note that there are complex visibility
9357 -- issues when the generic is a child unit and some aspect of the
9358 -- generic type is declared in a parent unit of the generic. We do
9359 -- the test to handle this special case only after a direct check
9360 -- for static matching has failed.
9362 if Subtypes_Match
9363 (Component_Type (A_Gen_T), Component_Type (Act_T))
9364 or else Subtypes_Match
9365 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
9366 Component_Type (Act_T))
9367 then
9368 null;
9369 else
9370 Error_Msg_NE
9371 ("component subtype of actual does not match that of formal &",
9372 Actual, Gen_T);
9373 Abandon_Instantiation (Actual);
9374 end if;
9376 if Has_Aliased_Components (A_Gen_T)
9377 and then not Has_Aliased_Components (Act_T)
9378 then
9379 Error_Msg_NE
9380 ("actual must have aliased components to match formal type &",
9381 Actual, Gen_T);
9382 end if;
9383 end Validate_Array_Type_Instance;
9385 -----------------------------------------------
9386 -- Validate_Derived_Interface_Type_Instance --
9387 -----------------------------------------------
9389 procedure Validate_Derived_Interface_Type_Instance is
9390 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
9391 Elmt : Elmt_Id;
9393 begin
9394 -- First apply interface instance checks
9396 Validate_Interface_Type_Instance;
9398 -- Verify that immediate parent interface is an ancestor of
9399 -- the actual.
9401 if Present (Par)
9402 and then not Interface_Present_In_Ancestor (Act_T, Par)
9403 then
9404 Error_Msg_NE
9405 ("interface actual must include progenitor&", Actual, Par);
9406 end if;
9408 -- Now verify that the actual includes all other ancestors of
9409 -- the formal.
9411 Elmt := First_Elmt (Interfaces (A_Gen_T));
9412 while Present (Elmt) loop
9413 if not Interface_Present_In_Ancestor
9414 (Act_T, Get_Instance_Of (Node (Elmt)))
9415 then
9416 Error_Msg_NE
9417 ("interface actual must include progenitor&",
9418 Actual, Node (Elmt));
9419 end if;
9421 Next_Elmt (Elmt);
9422 end loop;
9423 end Validate_Derived_Interface_Type_Instance;
9425 ------------------------------------
9426 -- Validate_Derived_Type_Instance --
9427 ------------------------------------
9429 procedure Validate_Derived_Type_Instance is
9430 Actual_Discr : Entity_Id;
9431 Ancestor_Discr : Entity_Id;
9433 begin
9434 -- If the parent type in the generic declaration is itself a previous
9435 -- formal type, then it is local to the generic and absent from the
9436 -- analyzed generic definition. In that case the ancestor is the
9437 -- instance of the formal (which must have been instantiated
9438 -- previously), unless the ancestor is itself a formal derived type.
9439 -- In this latter case (which is the subject of Corrigendum 8652/0038
9440 -- (AI-202) the ancestor of the formals is the ancestor of its
9441 -- parent. Otherwise, the analyzed generic carries the parent type.
9442 -- If the parent type is defined in a previous formal package, then
9443 -- the scope of that formal package is that of the generic type
9444 -- itself, and it has already been mapped into the corresponding type
9445 -- in the actual package.
9447 -- Common case: parent type defined outside of the generic
9449 if Is_Entity_Name (Subtype_Mark (Def))
9450 and then Present (Entity (Subtype_Mark (Def)))
9451 then
9452 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
9454 -- Check whether parent is defined in a previous formal package
9456 elsif
9457 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
9458 then
9459 Ancestor :=
9460 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
9462 -- The type may be a local derivation, or a type extension of a
9463 -- previous formal, or of a formal of a parent package.
9465 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
9466 or else
9467 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
9468 then
9469 -- Check whether the parent is another derived formal type in the
9470 -- same generic unit.
9472 if Etype (A_Gen_T) /= A_Gen_T
9473 and then Is_Generic_Type (Etype (A_Gen_T))
9474 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
9475 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
9476 then
9477 -- Locate ancestor of parent from the subtype declaration
9478 -- created for the actual.
9480 declare
9481 Decl : Node_Id;
9483 begin
9484 Decl := First (Actual_Decls);
9485 while Present (Decl) loop
9486 if Nkind (Decl) = N_Subtype_Declaration
9487 and then Chars (Defining_Identifier (Decl)) =
9488 Chars (Etype (A_Gen_T))
9489 then
9490 Ancestor := Generic_Parent_Type (Decl);
9491 exit;
9492 else
9493 Next (Decl);
9494 end if;
9495 end loop;
9496 end;
9498 pragma Assert (Present (Ancestor));
9500 else
9501 Ancestor :=
9502 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
9503 end if;
9505 else
9506 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
9507 end if;
9509 -- If the formal derived type has pragma Preelaborable_Initialization
9510 -- then the actual type must have preelaborable initialization.
9512 if Known_To_Have_Preelab_Init (A_Gen_T)
9513 and then not Has_Preelaborable_Initialization (Act_T)
9514 then
9515 Error_Msg_NE
9516 ("actual for & must have preelaborable initialization",
9517 Actual, Gen_T);
9518 end if;
9520 -- Ada 2005 (AI-251)
9522 if Ada_Version >= Ada_05
9523 and then Is_Interface (Ancestor)
9524 then
9525 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
9526 Error_Msg_NE
9527 ("(Ada 2005) expected type implementing & in instantiation",
9528 Actual, Ancestor);
9529 end if;
9531 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
9532 Error_Msg_NE
9533 ("expect type derived from & in instantiation",
9534 Actual, First_Subtype (Ancestor));
9535 Abandon_Instantiation (Actual);
9536 end if;
9538 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
9539 -- that the formal type declaration has been rewritten as a private
9540 -- extension.
9542 if Ada_Version >= Ada_05
9543 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
9544 and then Synchronized_Present (Parent (A_Gen_T))
9545 then
9546 -- The actual must be a synchronized tagged type
9548 if not Is_Tagged_Type (Act_T) then
9549 Error_Msg_N
9550 ("actual of synchronized type must be tagged", Actual);
9551 Abandon_Instantiation (Actual);
9553 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
9554 and then Nkind (Type_Definition (Parent (Act_T))) =
9555 N_Derived_Type_Definition
9556 and then not Synchronized_Present (Type_Definition
9557 (Parent (Act_T)))
9558 then
9559 Error_Msg_N
9560 ("actual of synchronized type must be synchronized", Actual);
9561 Abandon_Instantiation (Actual);
9562 end if;
9563 end if;
9565 -- Perform atomic/volatile checks (RM C.6(12))
9567 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
9568 Error_Msg_N
9569 ("cannot have atomic actual type for non-atomic formal type",
9570 Actual);
9572 elsif Is_Volatile (Act_T)
9573 and then not Is_Volatile (Ancestor)
9574 and then Is_By_Reference_Type (Ancestor)
9575 then
9576 Error_Msg_N
9577 ("cannot have volatile actual type for non-volatile formal type",
9578 Actual);
9579 end if;
9581 -- It should not be necessary to check for unknown discriminants on
9582 -- Formal, but for some reason Has_Unknown_Discriminants is false for
9583 -- A_Gen_T, so Is_Indefinite_Subtype incorrectly returns False. This
9584 -- needs fixing. ???
9586 if not Is_Indefinite_Subtype (A_Gen_T)
9587 and then not Unknown_Discriminants_Present (Formal)
9588 and then Is_Indefinite_Subtype (Act_T)
9589 then
9590 Error_Msg_N
9591 ("actual subtype must be constrained", Actual);
9592 Abandon_Instantiation (Actual);
9593 end if;
9595 if not Unknown_Discriminants_Present (Formal) then
9596 if Is_Constrained (Ancestor) then
9597 if not Is_Constrained (Act_T) then
9598 Error_Msg_N
9599 ("actual subtype must be constrained", Actual);
9600 Abandon_Instantiation (Actual);
9601 end if;
9603 -- Ancestor is unconstrained, Check if generic formal and actual
9604 -- agree on constrainedness. The check only applies to array types
9605 -- and discriminated types.
9607 elsif Is_Constrained (Act_T) then
9608 if Ekind (Ancestor) = E_Access_Type
9609 or else
9610 (not Is_Constrained (A_Gen_T)
9611 and then Is_Composite_Type (A_Gen_T))
9612 then
9613 Error_Msg_N
9614 ("actual subtype must be unconstrained", Actual);
9615 Abandon_Instantiation (Actual);
9616 end if;
9618 -- A class-wide type is only allowed if the formal has unknown
9619 -- discriminants.
9621 elsif Is_Class_Wide_Type (Act_T)
9622 and then not Has_Unknown_Discriminants (Ancestor)
9623 then
9624 Error_Msg_NE
9625 ("actual for & cannot be a class-wide type", Actual, Gen_T);
9626 Abandon_Instantiation (Actual);
9628 -- Otherwise, the formal and actual shall have the same number
9629 -- of discriminants and each discriminant of the actual must
9630 -- correspond to a discriminant of the formal.
9632 elsif Has_Discriminants (Act_T)
9633 and then not Has_Unknown_Discriminants (Act_T)
9634 and then Has_Discriminants (Ancestor)
9635 then
9636 Actual_Discr := First_Discriminant (Act_T);
9637 Ancestor_Discr := First_Discriminant (Ancestor);
9638 while Present (Actual_Discr)
9639 and then Present (Ancestor_Discr)
9640 loop
9641 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
9642 No (Corresponding_Discriminant (Actual_Discr))
9643 then
9644 Error_Msg_NE
9645 ("discriminant & does not correspond " &
9646 "to ancestor discriminant", Actual, Actual_Discr);
9647 Abandon_Instantiation (Actual);
9648 end if;
9650 Next_Discriminant (Actual_Discr);
9651 Next_Discriminant (Ancestor_Discr);
9652 end loop;
9654 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
9655 Error_Msg_NE
9656 ("actual for & must have same number of discriminants",
9657 Actual, Gen_T);
9658 Abandon_Instantiation (Actual);
9659 end if;
9661 -- This case should be caught by the earlier check for
9662 -- constrainedness, but the check here is added for completeness.
9664 elsif Has_Discriminants (Act_T)
9665 and then not Has_Unknown_Discriminants (Act_T)
9666 then
9667 Error_Msg_NE
9668 ("actual for & must not have discriminants", Actual, Gen_T);
9669 Abandon_Instantiation (Actual);
9671 elsif Has_Discriminants (Ancestor) then
9672 Error_Msg_NE
9673 ("actual for & must have known discriminants", Actual, Gen_T);
9674 Abandon_Instantiation (Actual);
9675 end if;
9677 if not Subtypes_Statically_Compatible (Act_T, Ancestor) then
9678 Error_Msg_N
9679 ("constraint on actual is incompatible with formal", Actual);
9680 Abandon_Instantiation (Actual);
9681 end if;
9682 end if;
9684 -- If the formal and actual types are abstract, check that there
9685 -- are no abstract primitives of the actual type that correspond to
9686 -- nonabstract primitives of the formal type (second sentence of
9687 -- RM95-3.9.3(9)).
9689 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
9690 Check_Abstract_Primitives : declare
9691 Gen_Prims : constant Elist_Id :=
9692 Primitive_Operations (A_Gen_T);
9693 Gen_Elmt : Elmt_Id;
9694 Gen_Subp : Entity_Id;
9695 Anc_Subp : Entity_Id;
9696 Anc_Formal : Entity_Id;
9697 Anc_F_Type : Entity_Id;
9699 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
9700 Act_Elmt : Elmt_Id;
9701 Act_Subp : Entity_Id;
9702 Act_Formal : Entity_Id;
9703 Act_F_Type : Entity_Id;
9705 Subprograms_Correspond : Boolean;
9707 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
9708 -- Returns true if T2 is derived directly or indirectly from
9709 -- T1, including derivations from interfaces. T1 and T2 are
9710 -- required to be specific tagged base types.
9712 ------------------------
9713 -- Is_Tagged_Ancestor --
9714 ------------------------
9716 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
9718 Intfc_Elmt : Elmt_Id;
9720 begin
9721 -- The predicate is satisfied if the types are the same
9723 if T1 = T2 then
9724 return True;
9726 -- If we've reached the top of the derivation chain then
9727 -- we know that T1 is not an ancestor of T2.
9729 elsif Etype (T2) = T2 then
9730 return False;
9732 -- Proceed to check T2's immediate parent
9734 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
9735 return True;
9737 -- Finally, check to see if T1 is an ancestor of any of T2's
9738 -- progenitors.
9740 else
9741 Intfc_Elmt := First_Elmt (Interfaces (T2));
9742 while Present (Intfc_Elmt) loop
9743 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
9744 return True;
9745 end if;
9747 Next_Elmt (Intfc_Elmt);
9748 end loop;
9749 end if;
9751 return False;
9752 end Is_Tagged_Ancestor;
9754 -- Start of processing for Check_Abstract_Primitives
9756 begin
9757 -- Loop over all of the formal derived type's primitives
9759 Gen_Elmt := First_Elmt (Gen_Prims);
9760 while Present (Gen_Elmt) loop
9761 Gen_Subp := Node (Gen_Elmt);
9763 -- If the primitive of the formal is not abstract, then
9764 -- determine whether there is a corresponding primitive of
9765 -- the actual type that's abstract.
9767 if not Is_Abstract_Subprogram (Gen_Subp) then
9768 Act_Elmt := First_Elmt (Act_Prims);
9769 while Present (Act_Elmt) loop
9770 Act_Subp := Node (Act_Elmt);
9772 -- If we find an abstract primitive of the actual,
9773 -- then we need to test whether it corresponds to the
9774 -- subprogram from which the generic formal primitive
9775 -- is inherited.
9777 if Is_Abstract_Subprogram (Act_Subp) then
9778 Anc_Subp := Alias (Gen_Subp);
9780 -- Test whether we have a corresponding primitive
9781 -- by comparing names, kinds, formal types, and
9782 -- result types.
9784 if Chars (Anc_Subp) = Chars (Act_Subp)
9785 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
9786 then
9787 Anc_Formal := First_Formal (Anc_Subp);
9788 Act_Formal := First_Formal (Act_Subp);
9789 while Present (Anc_Formal)
9790 and then Present (Act_Formal)
9791 loop
9792 Anc_F_Type := Etype (Anc_Formal);
9793 Act_F_Type := Etype (Act_Formal);
9795 if Ekind (Anc_F_Type)
9796 = E_Anonymous_Access_Type
9797 then
9798 Anc_F_Type := Designated_Type (Anc_F_Type);
9800 if Ekind (Act_F_Type)
9801 = E_Anonymous_Access_Type
9802 then
9803 Act_F_Type :=
9804 Designated_Type (Act_F_Type);
9805 else
9806 exit;
9807 end if;
9809 elsif
9810 Ekind (Act_F_Type) = E_Anonymous_Access_Type
9811 then
9812 exit;
9813 end if;
9815 Anc_F_Type := Base_Type (Anc_F_Type);
9816 Act_F_Type := Base_Type (Act_F_Type);
9818 -- If the formal is controlling, then the
9819 -- the type of the actual primitive's formal
9820 -- must be derived directly or indirectly
9821 -- from the type of the ancestor primitive's
9822 -- formal.
9824 if Is_Controlling_Formal (Anc_Formal) then
9825 if not Is_Tagged_Ancestor
9826 (Anc_F_Type, Act_F_Type)
9827 then
9828 exit;
9829 end if;
9831 -- Otherwise the types of the formals must
9832 -- be the same.
9834 elsif Anc_F_Type /= Act_F_Type then
9835 exit;
9836 end if;
9838 Next_Entity (Anc_Formal);
9839 Next_Entity (Act_Formal);
9840 end loop;
9842 -- If we traversed through all of the formals
9843 -- then so far the subprograms correspond, so
9844 -- now check that any result types correspond.
9846 if No (Anc_Formal)
9847 and then No (Act_Formal)
9848 then
9849 Subprograms_Correspond := True;
9851 if Ekind (Act_Subp) = E_Function then
9852 Anc_F_Type := Etype (Anc_Subp);
9853 Act_F_Type := Etype (Act_Subp);
9855 if Ekind (Anc_F_Type)
9856 = E_Anonymous_Access_Type
9857 then
9858 Anc_F_Type :=
9859 Designated_Type (Anc_F_Type);
9861 if Ekind (Act_F_Type)
9862 = E_Anonymous_Access_Type
9863 then
9864 Act_F_Type :=
9865 Designated_Type (Act_F_Type);
9866 else
9867 Subprograms_Correspond := False;
9868 end if;
9870 elsif
9871 Ekind (Act_F_Type)
9872 = E_Anonymous_Access_Type
9873 then
9874 Subprograms_Correspond := False;
9875 end if;
9877 Anc_F_Type := Base_Type (Anc_F_Type);
9878 Act_F_Type := Base_Type (Act_F_Type);
9880 -- Now either the result types must be
9881 -- the same or, if the result type is
9882 -- controlling, the result type of the
9883 -- actual primitive must descend from the
9884 -- result type of the ancestor primitive.
9886 if Subprograms_Correspond
9887 and then Anc_F_Type /= Act_F_Type
9888 and then
9889 Has_Controlling_Result (Anc_Subp)
9890 and then
9891 not Is_Tagged_Ancestor
9892 (Anc_F_Type, Act_F_Type)
9893 then
9894 Subprograms_Correspond := False;
9895 end if;
9896 end if;
9898 -- Found a matching subprogram belonging to
9899 -- formal ancestor type, so actual subprogram
9900 -- corresponds and this violates 3.9.3(9).
9902 if Subprograms_Correspond then
9903 Error_Msg_NE
9904 ("abstract subprogram & overrides " &
9905 "nonabstract subprogram of ancestor",
9906 Actual,
9907 Act_Subp);
9908 end if;
9909 end if;
9910 end if;
9911 end if;
9913 Next_Elmt (Act_Elmt);
9914 end loop;
9915 end if;
9917 Next_Elmt (Gen_Elmt);
9918 end loop;
9919 end Check_Abstract_Primitives;
9920 end if;
9922 -- Verify that limitedness matches. If parent is a limited
9923 -- interface then the generic formal is not unless declared
9924 -- explicitly so. If not declared limited, the actual cannot be
9925 -- limited (see AI05-0087).
9926 -- Disable check for now, limited interfaces implemented by
9927 -- protected types are common, Need to update tests ???
9929 if Is_Limited_Type (Act_T)
9930 and then not Is_Limited_Type (A_Gen_T)
9931 and then False
9932 then
9933 Error_Msg_NE
9934 ("actual for non-limited & cannot be a limited type", Actual,
9935 Gen_T);
9936 Explain_Limited_Type (Act_T, Actual);
9937 Abandon_Instantiation (Actual);
9938 end if;
9939 end Validate_Derived_Type_Instance;
9941 --------------------------------------
9942 -- Validate_Interface_Type_Instance --
9943 --------------------------------------
9945 procedure Validate_Interface_Type_Instance is
9946 begin
9947 if not Is_Interface (Act_T) then
9948 Error_Msg_NE
9949 ("actual for formal interface type must be an interface",
9950 Actual, Gen_T);
9952 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
9953 or else
9954 Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
9955 or else
9956 Is_Protected_Interface (A_Gen_T) /=
9957 Is_Protected_Interface (Act_T)
9958 or else
9959 Is_Synchronized_Interface (A_Gen_T) /=
9960 Is_Synchronized_Interface (Act_T)
9961 then
9962 Error_Msg_NE
9963 ("actual for interface& does not match (RM 12.5.5(4))",
9964 Actual, Gen_T);
9965 end if;
9966 end Validate_Interface_Type_Instance;
9968 ------------------------------------
9969 -- Validate_Private_Type_Instance --
9970 ------------------------------------
9972 procedure Validate_Private_Type_Instance is
9973 Formal_Discr : Entity_Id;
9974 Actual_Discr : Entity_Id;
9975 Formal_Subt : Entity_Id;
9977 begin
9978 if Is_Limited_Type (Act_T)
9979 and then not Is_Limited_Type (A_Gen_T)
9980 then
9981 Error_Msg_NE
9982 ("actual for non-limited & cannot be a limited type", Actual,
9983 Gen_T);
9984 Explain_Limited_Type (Act_T, Actual);
9985 Abandon_Instantiation (Actual);
9987 elsif Known_To_Have_Preelab_Init (A_Gen_T)
9988 and then not Has_Preelaborable_Initialization (Act_T)
9989 then
9990 Error_Msg_NE
9991 ("actual for & must have preelaborable initialization", Actual,
9992 Gen_T);
9994 elsif Is_Indefinite_Subtype (Act_T)
9995 and then not Is_Indefinite_Subtype (A_Gen_T)
9996 and then Ada_Version >= Ada_95
9997 then
9998 Error_Msg_NE
9999 ("actual for & must be a definite subtype", Actual, Gen_T);
10001 elsif not Is_Tagged_Type (Act_T)
10002 and then Is_Tagged_Type (A_Gen_T)
10003 then
10004 Error_Msg_NE
10005 ("actual for & must be a tagged type", Actual, Gen_T);
10007 elsif Has_Discriminants (A_Gen_T) then
10008 if not Has_Discriminants (Act_T) then
10009 Error_Msg_NE
10010 ("actual for & must have discriminants", Actual, Gen_T);
10011 Abandon_Instantiation (Actual);
10013 elsif Is_Constrained (Act_T) then
10014 Error_Msg_NE
10015 ("actual for & must be unconstrained", Actual, Gen_T);
10016 Abandon_Instantiation (Actual);
10018 else
10019 Formal_Discr := First_Discriminant (A_Gen_T);
10020 Actual_Discr := First_Discriminant (Act_T);
10021 while Formal_Discr /= Empty loop
10022 if Actual_Discr = Empty then
10023 Error_Msg_NE
10024 ("discriminants on actual do not match formal",
10025 Actual, Gen_T);
10026 Abandon_Instantiation (Actual);
10027 end if;
10029 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
10031 -- Access discriminants match if designated types do
10033 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
10034 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
10035 E_Anonymous_Access_Type
10036 and then
10037 Get_Instance_Of
10038 (Designated_Type (Base_Type (Formal_Subt))) =
10039 Designated_Type (Base_Type (Etype (Actual_Discr)))
10040 then
10041 null;
10043 elsif Base_Type (Formal_Subt) /=
10044 Base_Type (Etype (Actual_Discr))
10045 then
10046 Error_Msg_NE
10047 ("types of actual discriminants must match formal",
10048 Actual, Gen_T);
10049 Abandon_Instantiation (Actual);
10051 elsif not Subtypes_Statically_Match
10052 (Formal_Subt, Etype (Actual_Discr))
10053 and then Ada_Version >= Ada_95
10054 then
10055 Error_Msg_NE
10056 ("subtypes of actual discriminants must match formal",
10057 Actual, Gen_T);
10058 Abandon_Instantiation (Actual);
10059 end if;
10061 Next_Discriminant (Formal_Discr);
10062 Next_Discriminant (Actual_Discr);
10063 end loop;
10065 if Actual_Discr /= Empty then
10066 Error_Msg_NE
10067 ("discriminants on actual do not match formal",
10068 Actual, Gen_T);
10069 Abandon_Instantiation (Actual);
10070 end if;
10071 end if;
10073 end if;
10075 Ancestor := Gen_T;
10076 end Validate_Private_Type_Instance;
10078 -- Start of processing for Instantiate_Type
10080 begin
10081 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
10082 Error_Msg_N ("duplicate instantiation of generic type", Actual);
10083 return New_List (Error);
10085 elsif not Is_Entity_Name (Actual)
10086 or else not Is_Type (Entity (Actual))
10087 then
10088 Error_Msg_NE
10089 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
10090 Abandon_Instantiation (Actual);
10092 else
10093 Act_T := Entity (Actual);
10095 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
10096 -- as a generic actual parameter if the corresponding formal type
10097 -- does not have a known_discriminant_part, or is a formal derived
10098 -- type that is an Unchecked_Union type.
10100 if Is_Unchecked_Union (Base_Type (Act_T)) then
10101 if not Has_Discriminants (A_Gen_T)
10102 or else
10103 (Is_Derived_Type (A_Gen_T)
10104 and then
10105 Is_Unchecked_Union (A_Gen_T))
10106 then
10107 null;
10108 else
10109 Error_Msg_N ("Unchecked_Union cannot be the actual for a" &
10110 " discriminated formal type", Act_T);
10112 end if;
10113 end if;
10115 -- Deal with fixed/floating restrictions
10117 if Is_Floating_Point_Type (Act_T) then
10118 Check_Restriction (No_Floating_Point, Actual);
10119 elsif Is_Fixed_Point_Type (Act_T) then
10120 Check_Restriction (No_Fixed_Point, Actual);
10121 end if;
10123 -- Deal with error of using incomplete type as generic actual.
10124 -- This includes limited views of a type, even if the non-limited
10125 -- view may be available.
10127 if Ekind (Act_T) = E_Incomplete_Type
10128 or else (Is_Class_Wide_Type (Act_T)
10129 and then
10130 Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
10131 then
10132 if Is_Class_Wide_Type (Act_T)
10133 or else No (Full_View (Act_T))
10134 then
10135 Error_Msg_N ("premature use of incomplete type", Actual);
10136 Abandon_Instantiation (Actual);
10137 else
10138 Act_T := Full_View (Act_T);
10139 Set_Entity (Actual, Act_T);
10141 if Has_Private_Component (Act_T) then
10142 Error_Msg_N
10143 ("premature use of type with private component", Actual);
10144 end if;
10145 end if;
10147 -- Deal with error of premature use of private type as generic actual
10149 elsif Is_Private_Type (Act_T)
10150 and then Is_Private_Type (Base_Type (Act_T))
10151 and then not Is_Generic_Type (Act_T)
10152 and then not Is_Derived_Type (Act_T)
10153 and then No (Full_View (Root_Type (Act_T)))
10154 then
10155 Error_Msg_N ("premature use of private type", Actual);
10157 elsif Has_Private_Component (Act_T) then
10158 Error_Msg_N
10159 ("premature use of type with private component", Actual);
10160 end if;
10162 Set_Instance_Of (A_Gen_T, Act_T);
10164 -- If the type is generic, the class-wide type may also be used
10166 if Is_Tagged_Type (A_Gen_T)
10167 and then Is_Tagged_Type (Act_T)
10168 and then not Is_Class_Wide_Type (A_Gen_T)
10169 then
10170 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
10171 Class_Wide_Type (Act_T));
10172 end if;
10174 if not Is_Abstract_Type (A_Gen_T)
10175 and then Is_Abstract_Type (Act_T)
10176 then
10177 Error_Msg_N
10178 ("actual of non-abstract formal cannot be abstract", Actual);
10179 end if;
10181 -- A generic scalar type is a first subtype for which we generate
10182 -- an anonymous base type. Indicate that the instance of this base
10183 -- is the base type of the actual.
10185 if Is_Scalar_Type (A_Gen_T) then
10186 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
10187 end if;
10188 end if;
10190 if Error_Posted (Act_T) then
10191 null;
10192 else
10193 case Nkind (Def) is
10194 when N_Formal_Private_Type_Definition =>
10195 Validate_Private_Type_Instance;
10197 when N_Formal_Derived_Type_Definition =>
10198 Validate_Derived_Type_Instance;
10200 when N_Formal_Discrete_Type_Definition =>
10201 if not Is_Discrete_Type (Act_T) then
10202 Error_Msg_NE
10203 ("expect discrete type in instantiation of&",
10204 Actual, Gen_T);
10205 Abandon_Instantiation (Actual);
10206 end if;
10208 when N_Formal_Signed_Integer_Type_Definition =>
10209 if not Is_Signed_Integer_Type (Act_T) then
10210 Error_Msg_NE
10211 ("expect signed integer type in instantiation of&",
10212 Actual, Gen_T);
10213 Abandon_Instantiation (Actual);
10214 end if;
10216 when N_Formal_Modular_Type_Definition =>
10217 if not Is_Modular_Integer_Type (Act_T) then
10218 Error_Msg_NE
10219 ("expect modular type in instantiation of &",
10220 Actual, Gen_T);
10221 Abandon_Instantiation (Actual);
10222 end if;
10224 when N_Formal_Floating_Point_Definition =>
10225 if not Is_Floating_Point_Type (Act_T) then
10226 Error_Msg_NE
10227 ("expect float type in instantiation of &", Actual, Gen_T);
10228 Abandon_Instantiation (Actual);
10229 end if;
10231 when N_Formal_Ordinary_Fixed_Point_Definition =>
10232 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
10233 Error_Msg_NE
10234 ("expect ordinary fixed point type in instantiation of &",
10235 Actual, Gen_T);
10236 Abandon_Instantiation (Actual);
10237 end if;
10239 when N_Formal_Decimal_Fixed_Point_Definition =>
10240 if not Is_Decimal_Fixed_Point_Type (Act_T) then
10241 Error_Msg_NE
10242 ("expect decimal type in instantiation of &",
10243 Actual, Gen_T);
10244 Abandon_Instantiation (Actual);
10245 end if;
10247 when N_Array_Type_Definition =>
10248 Validate_Array_Type_Instance;
10250 when N_Access_To_Object_Definition =>
10251 Validate_Access_Type_Instance;
10253 when N_Access_Function_Definition |
10254 N_Access_Procedure_Definition =>
10255 Validate_Access_Subprogram_Instance;
10257 when N_Record_Definition =>
10258 Validate_Interface_Type_Instance;
10260 when N_Derived_Type_Definition =>
10261 Validate_Derived_Interface_Type_Instance;
10263 when others =>
10264 raise Program_Error;
10266 end case;
10267 end if;
10269 Subt := New_Copy (Gen_T);
10271 -- Use adjusted sloc of subtype name as the location for other nodes in
10272 -- the subtype declaration.
10274 Loc := Sloc (Subt);
10276 Decl_Node :=
10277 Make_Subtype_Declaration (Loc,
10278 Defining_Identifier => Subt,
10279 Subtype_Indication => New_Reference_To (Act_T, Loc));
10281 if Is_Private_Type (Act_T) then
10282 Set_Has_Private_View (Subtype_Indication (Decl_Node));
10284 elsif Is_Access_Type (Act_T)
10285 and then Is_Private_Type (Designated_Type (Act_T))
10286 then
10287 Set_Has_Private_View (Subtype_Indication (Decl_Node));
10288 end if;
10290 Decl_Nodes := New_List (Decl_Node);
10292 -- Flag actual derived types so their elaboration produces the
10293 -- appropriate renamings for the primitive operations of the ancestor.
10294 -- Flag actual for formal private types as well, to determine whether
10295 -- operations in the private part may override inherited operations.
10296 -- If the formal has an interface list, the ancestor is not the
10297 -- parent, but the analyzed formal that includes the interface
10298 -- operations of all its progenitors.
10300 if Nkind (Def) = N_Formal_Derived_Type_Definition then
10301 if Present (Interface_List (Def)) then
10302 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
10303 else
10304 Set_Generic_Parent_Type (Decl_Node, Ancestor);
10305 end if;
10307 elsif Nkind (Def) = N_Formal_Private_Type_Definition then
10308 Set_Generic_Parent_Type (Decl_Node, Ancestor);
10309 end if;
10311 -- If the actual is a synchronized type that implements an interface,
10312 -- the primitive operations are attached to the corresponding record,
10313 -- and we have to treat it as an additional generic actual, so that its
10314 -- primitive operations become visible in the instance. The task or
10315 -- protected type itself does not carry primitive operations.
10317 if Is_Concurrent_Type (Act_T)
10318 and then Is_Tagged_Type (Act_T)
10319 and then Present (Corresponding_Record_Type (Act_T))
10320 and then Present (Ancestor)
10321 and then Is_Interface (Ancestor)
10322 then
10323 declare
10324 Corr_Rec : constant Entity_Id :=
10325 Corresponding_Record_Type (Act_T);
10326 New_Corr : Entity_Id;
10327 Corr_Decl : Node_Id;
10329 begin
10330 New_Corr := Make_Defining_Identifier (Loc,
10331 Chars => New_Internal_Name ('S'));
10332 Corr_Decl :=
10333 Make_Subtype_Declaration (Loc,
10334 Defining_Identifier => New_Corr,
10335 Subtype_Indication =>
10336 New_Reference_To (Corr_Rec, Loc));
10337 Append_To (Decl_Nodes, Corr_Decl);
10339 if Ekind (Act_T) = E_Task_Type then
10340 Set_Ekind (Subt, E_Task_Subtype);
10341 else
10342 Set_Ekind (Subt, E_Protected_Subtype);
10343 end if;
10345 Set_Corresponding_Record_Type (Subt, Corr_Rec);
10346 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
10347 Set_Generic_Parent_Type (Decl_Node, Empty);
10348 end;
10349 end if;
10351 return Decl_Nodes;
10352 end Instantiate_Type;
10354 -----------------------
10355 -- Is_Generic_Formal --
10356 -----------------------
10358 function Is_Generic_Formal (E : Entity_Id) return Boolean is
10359 Kind : Node_Kind;
10360 begin
10361 if No (E) then
10362 return False;
10363 else
10364 Kind := Nkind (Parent (E));
10365 return
10366 Nkind_In (Kind, N_Formal_Object_Declaration,
10367 N_Formal_Package_Declaration,
10368 N_Formal_Type_Declaration)
10369 or else
10370 (Is_Formal_Subprogram (E)
10371 and then
10372 Nkind (Parent (Parent (E))) in
10373 N_Formal_Subprogram_Declaration);
10374 end if;
10375 end Is_Generic_Formal;
10377 ---------------------
10378 -- Is_In_Main_Unit --
10379 ---------------------
10381 function Is_In_Main_Unit (N : Node_Id) return Boolean is
10382 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
10383 Current_Unit : Node_Id;
10385 begin
10386 if Unum = Main_Unit then
10387 return True;
10389 -- If the current unit is a subunit then it is either the main unit or
10390 -- is being compiled as part of the main unit.
10392 elsif Nkind (N) = N_Compilation_Unit then
10393 return Nkind (Unit (N)) = N_Subunit;
10394 end if;
10396 Current_Unit := Parent (N);
10397 while Present (Current_Unit)
10398 and then Nkind (Current_Unit) /= N_Compilation_Unit
10399 loop
10400 Current_Unit := Parent (Current_Unit);
10401 end loop;
10403 -- The instantiation node is in the main unit, or else the current node
10404 -- (perhaps as the result of nested instantiations) is in the main unit,
10405 -- or in the declaration of the main unit, which in this last case must
10406 -- be a body.
10408 return Unum = Main_Unit
10409 or else Current_Unit = Cunit (Main_Unit)
10410 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
10411 or else (Present (Library_Unit (Current_Unit))
10412 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
10413 end Is_In_Main_Unit;
10415 ----------------------------
10416 -- Load_Parent_Of_Generic --
10417 ----------------------------
10419 procedure Load_Parent_Of_Generic
10420 (N : Node_Id;
10421 Spec : Node_Id;
10422 Body_Optional : Boolean := False)
10424 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
10425 Save_Style_Check : constant Boolean := Style_Check;
10426 True_Parent : Node_Id;
10427 Inst_Node : Node_Id;
10428 OK : Boolean;
10429 Previous_Instances : constant Elist_Id := New_Elmt_List;
10431 procedure Collect_Previous_Instances (Decls : List_Id);
10432 -- Collect all instantiations in the given list of declarations, that
10433 -- precede the generic that we need to load. If the bodies of these
10434 -- instantiations are available, we must analyze them, to ensure that
10435 -- the public symbols generated are the same when the unit is compiled
10436 -- to generate code, and when it is compiled in the context of a unit
10437 -- that needs a particular nested instance. This process is applied
10438 -- to both package and subprogram instances.
10440 --------------------------------
10441 -- Collect_Previous_Instances --
10442 --------------------------------
10444 procedure Collect_Previous_Instances (Decls : List_Id) is
10445 Decl : Node_Id;
10447 begin
10448 Decl := First (Decls);
10449 while Present (Decl) loop
10450 if Sloc (Decl) >= Sloc (Inst_Node) then
10451 return;
10453 -- If Decl is an instantiation, then record it as requiring
10454 -- instantiation of the corresponding body, except if it is an
10455 -- abbreviated instantiation generated internally for conformance
10456 -- checking purposes only for the case of a formal package
10457 -- declared without a box (see Instantiate_Formal_Package). Such
10458 -- an instantiation does not generate any code (the actual code
10459 -- comes from actual) and thus does not need to be analyzed here.
10460 -- If the instantiation appears with a generic package body it is
10461 -- not analyzed here either.
10463 elsif Nkind (Decl) = N_Package_Instantiation
10464 and then not Is_Internal (Defining_Entity (Decl))
10465 then
10466 Append_Elmt (Decl, Previous_Instances);
10468 -- For a subprogram instantiation, omit instantiations intrinsic
10469 -- operations (Unchecked_Conversions, etc.) that have no bodies.
10471 elsif Nkind_In (Decl, N_Function_Instantiation,
10472 N_Procedure_Instantiation)
10473 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
10474 then
10475 Append_Elmt (Decl, Previous_Instances);
10477 elsif Nkind (Decl) = N_Package_Declaration then
10478 Collect_Previous_Instances
10479 (Visible_Declarations (Specification (Decl)));
10480 Collect_Previous_Instances
10481 (Private_Declarations (Specification (Decl)));
10483 elsif Nkind (Decl) = N_Package_Body
10484 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
10485 then
10486 Collect_Previous_Instances (Declarations (Decl));
10487 end if;
10489 Next (Decl);
10490 end loop;
10491 end Collect_Previous_Instances;
10493 -- Start of processing for Load_Parent_Of_Generic
10495 begin
10496 if not In_Same_Source_Unit (N, Spec)
10497 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
10498 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
10499 and then not Is_In_Main_Unit (Spec))
10500 then
10501 -- Find body of parent of spec, and analyze it. A special case arises
10502 -- when the parent is an instantiation, that is to say when we are
10503 -- currently instantiating a nested generic. In that case, there is
10504 -- no separate file for the body of the enclosing instance. Instead,
10505 -- the enclosing body must be instantiated as if it were a pending
10506 -- instantiation, in order to produce the body for the nested generic
10507 -- we require now. Note that in that case the generic may be defined
10508 -- in a package body, the instance defined in the same package body,
10509 -- and the original enclosing body may not be in the main unit.
10511 Inst_Node := Empty;
10513 True_Parent := Parent (Spec);
10514 while Present (True_Parent)
10515 and then Nkind (True_Parent) /= N_Compilation_Unit
10516 loop
10517 if Nkind (True_Parent) = N_Package_Declaration
10518 and then
10519 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
10520 then
10521 -- Parent is a compilation unit that is an instantiation.
10522 -- Instantiation node has been replaced with package decl.
10524 Inst_Node := Original_Node (True_Parent);
10525 exit;
10527 elsif Nkind (True_Parent) = N_Package_Declaration
10528 and then Present (Generic_Parent (Specification (True_Parent)))
10529 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
10530 then
10531 -- Parent is an instantiation within another specification.
10532 -- Declaration for instance has been inserted before original
10533 -- instantiation node. A direct link would be preferable?
10535 Inst_Node := Next (True_Parent);
10536 while Present (Inst_Node)
10537 and then Nkind (Inst_Node) /= N_Package_Instantiation
10538 loop
10539 Next (Inst_Node);
10540 end loop;
10542 -- If the instance appears within a generic, and the generic
10543 -- unit is defined within a formal package of the enclosing
10544 -- generic, there is no generic body available, and none
10545 -- needed. A more precise test should be used ???
10547 if No (Inst_Node) then
10548 return;
10549 end if;
10551 exit;
10553 else
10554 True_Parent := Parent (True_Parent);
10555 end if;
10556 end loop;
10558 -- Case where we are currently instantiating a nested generic
10560 if Present (Inst_Node) then
10561 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
10563 -- Instantiation node and declaration of instantiated package
10564 -- were exchanged when only the declaration was needed.
10565 -- Restore instantiation node before proceeding with body.
10567 Set_Unit (Parent (True_Parent), Inst_Node);
10568 end if;
10570 -- Now complete instantiation of enclosing body, if it appears in
10571 -- some other unit. If it appears in the current unit, the body
10572 -- will have been instantiated already.
10574 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
10576 -- We need to determine the expander mode to instantiate the
10577 -- enclosing body. Because the generic body we need may use
10578 -- global entities declared in the enclosing package (including
10579 -- aggregates) it is in general necessary to compile this body
10580 -- with expansion enabled. The exception is if we are within a
10581 -- generic package, in which case the usual generic rule
10582 -- applies.
10584 declare
10585 Exp_Status : Boolean := True;
10586 Scop : Entity_Id;
10588 begin
10589 -- Loop through scopes looking for generic package
10591 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
10592 while Present (Scop)
10593 and then Scop /= Standard_Standard
10594 loop
10595 if Ekind (Scop) = E_Generic_Package then
10596 Exp_Status := False;
10597 exit;
10598 end if;
10600 Scop := Scope (Scop);
10601 end loop;
10603 -- Collect previous instantiations in the unit that contains
10604 -- the desired generic.
10606 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
10607 and then not Body_Optional
10608 then
10609 declare
10610 Decl : Elmt_Id;
10611 Info : Pending_Body_Info;
10612 Par : Node_Id;
10614 begin
10615 Par := Parent (Inst_Node);
10616 while Present (Par) loop
10617 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
10618 Par := Parent (Par);
10619 end loop;
10621 pragma Assert (Present (Par));
10623 if Nkind (Par) = N_Package_Body then
10624 Collect_Previous_Instances (Declarations (Par));
10626 elsif Nkind (Par) = N_Package_Declaration then
10627 Collect_Previous_Instances
10628 (Visible_Declarations (Specification (Par)));
10629 Collect_Previous_Instances
10630 (Private_Declarations (Specification (Par)));
10632 else
10633 -- Enclosing unit is a subprogram body. In this
10634 -- case all instance bodies are processed in order
10635 -- and there is no need to collect them separately.
10637 null;
10638 end if;
10640 Decl := First_Elmt (Previous_Instances);
10641 while Present (Decl) loop
10642 Info :=
10643 (Inst_Node => Node (Decl),
10644 Act_Decl =>
10645 Instance_Spec (Node (Decl)),
10646 Expander_Status => Exp_Status,
10647 Current_Sem_Unit =>
10648 Get_Code_Unit (Sloc (Node (Decl))),
10649 Scope_Suppress => Scope_Suppress,
10650 Local_Suppress_Stack_Top =>
10651 Local_Suppress_Stack_Top);
10653 -- Package instance
10656 Nkind (Node (Decl)) = N_Package_Instantiation
10657 then
10658 Instantiate_Package_Body
10659 (Info, Body_Optional => True);
10661 -- Subprogram instance
10663 else
10664 -- The instance_spec is the wrapper package,
10665 -- and the subprogram declaration is the last
10666 -- declaration in the wrapper.
10668 Info.Act_Decl :=
10669 Last
10670 (Visible_Declarations
10671 (Specification (Info.Act_Decl)));
10673 Instantiate_Subprogram_Body
10674 (Info, Body_Optional => True);
10675 end if;
10677 Next_Elmt (Decl);
10678 end loop;
10679 end;
10680 end if;
10682 Instantiate_Package_Body
10683 (Body_Info =>
10684 ((Inst_Node => Inst_Node,
10685 Act_Decl => True_Parent,
10686 Expander_Status => Exp_Status,
10687 Current_Sem_Unit =>
10688 Get_Code_Unit (Sloc (Inst_Node)),
10689 Scope_Suppress => Scope_Suppress,
10690 Local_Suppress_Stack_Top =>
10691 Local_Suppress_Stack_Top)),
10692 Body_Optional => Body_Optional);
10693 end;
10694 end if;
10696 -- Case where we are not instantiating a nested generic
10698 else
10699 Opt.Style_Check := False;
10700 Expander_Mode_Save_And_Set (True);
10701 Load_Needed_Body (Comp_Unit, OK);
10702 Opt.Style_Check := Save_Style_Check;
10703 Expander_Mode_Restore;
10705 if not OK
10706 and then Unit_Requires_Body (Defining_Entity (Spec))
10707 and then not Body_Optional
10708 then
10709 declare
10710 Bname : constant Unit_Name_Type :=
10711 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
10713 begin
10714 Error_Msg_Unit_1 := Bname;
10715 Error_Msg_N ("this instantiation requires$!", N);
10716 Error_Msg_File_1 := Get_File_Name (Bname, Subunit => False);
10717 Error_Msg_N ("\but file{ was not found!", N);
10718 raise Unrecoverable_Error;
10719 end;
10720 end if;
10721 end if;
10722 end if;
10724 -- If loading parent of the generic caused an instantiation circularity,
10725 -- we abandon compilation at this point, because otherwise in some cases
10726 -- we get into trouble with infinite recursions after this point.
10728 if Circularity_Detected then
10729 raise Unrecoverable_Error;
10730 end if;
10731 end Load_Parent_Of_Generic;
10733 ---------------------------------
10734 -- Map_Formal_Package_Entities --
10735 ---------------------------------
10737 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
10738 E1 : Entity_Id;
10739 E2 : Entity_Id;
10741 begin
10742 Set_Instance_Of (Form, Act);
10744 -- Traverse formal and actual package to map the corresponding entities.
10745 -- We skip over internal entities that may be generated during semantic
10746 -- analysis, and find the matching entities by name, given that they
10747 -- must appear in the same order.
10749 E1 := First_Entity (Form);
10750 E2 := First_Entity (Act);
10751 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
10752 -- Could this test be a single condition???
10753 -- Seems like it could, and isn't FPE (Form) a constant anyway???
10755 if not Is_Internal (E1)
10756 and then Present (Parent (E1))
10757 and then not Is_Class_Wide_Type (E1)
10758 and then not Is_Internal_Name (Chars (E1))
10759 then
10760 while Present (E2) and then Chars (E2) /= Chars (E1) loop
10761 Next_Entity (E2);
10762 end loop;
10764 if No (E2) then
10765 exit;
10766 else
10767 Set_Instance_Of (E1, E2);
10769 if Is_Type (E1) and then Is_Tagged_Type (E2) then
10770 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
10771 end if;
10773 if Is_Constrained (E1) then
10774 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
10775 end if;
10777 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
10778 Map_Formal_Package_Entities (E1, E2);
10779 end if;
10780 end if;
10781 end if;
10783 Next_Entity (E1);
10784 end loop;
10785 end Map_Formal_Package_Entities;
10787 -----------------------
10788 -- Move_Freeze_Nodes --
10789 -----------------------
10791 procedure Move_Freeze_Nodes
10792 (Out_Of : Entity_Id;
10793 After : Node_Id;
10794 L : List_Id)
10796 Decl : Node_Id;
10797 Next_Decl : Node_Id;
10798 Next_Node : Node_Id := After;
10799 Spec : Node_Id;
10801 function Is_Outer_Type (T : Entity_Id) return Boolean;
10802 -- Check whether entity is declared in a scope external to that of the
10803 -- generic unit.
10805 -------------------
10806 -- Is_Outer_Type --
10807 -------------------
10809 function Is_Outer_Type (T : Entity_Id) return Boolean is
10810 Scop : Entity_Id := Scope (T);
10812 begin
10813 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
10814 return True;
10816 else
10817 while Scop /= Standard_Standard loop
10818 if Scop = Out_Of then
10819 return False;
10820 else
10821 Scop := Scope (Scop);
10822 end if;
10823 end loop;
10825 return True;
10826 end if;
10827 end Is_Outer_Type;
10829 -- Start of processing for Move_Freeze_Nodes
10831 begin
10832 if No (L) then
10833 return;
10834 end if;
10836 -- First remove the freeze nodes that may appear before all other
10837 -- declarations.
10839 Decl := First (L);
10840 while Present (Decl)
10841 and then Nkind (Decl) = N_Freeze_Entity
10842 and then Is_Outer_Type (Entity (Decl))
10843 loop
10844 Decl := Remove_Head (L);
10845 Insert_After (Next_Node, Decl);
10846 Set_Analyzed (Decl, False);
10847 Next_Node := Decl;
10848 Decl := First (L);
10849 end loop;
10851 -- Next scan the list of declarations and remove each freeze node that
10852 -- appears ahead of the current node.
10854 while Present (Decl) loop
10855 while Present (Next (Decl))
10856 and then Nkind (Next (Decl)) = N_Freeze_Entity
10857 and then Is_Outer_Type (Entity (Next (Decl)))
10858 loop
10859 Next_Decl := Remove_Next (Decl);
10860 Insert_After (Next_Node, Next_Decl);
10861 Set_Analyzed (Next_Decl, False);
10862 Next_Node := Next_Decl;
10863 end loop;
10865 -- If the declaration is a nested package or concurrent type, then
10866 -- recurse. Nested generic packages will have been processed from the
10867 -- inside out.
10869 case Nkind (Decl) is
10870 when N_Package_Declaration =>
10871 Spec := Specification (Decl);
10873 when N_Task_Type_Declaration =>
10874 Spec := Task_Definition (Decl);
10876 when N_Protected_Type_Declaration =>
10877 Spec := Protected_Definition (Decl);
10879 when others =>
10880 Spec := Empty;
10881 end case;
10883 if Present (Spec) then
10884 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
10885 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
10886 end if;
10888 Next (Decl);
10889 end loop;
10890 end Move_Freeze_Nodes;
10892 ----------------
10893 -- Next_Assoc --
10894 ----------------
10896 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
10897 begin
10898 return Generic_Renamings.Table (E).Next_In_HTable;
10899 end Next_Assoc;
10901 ------------------------
10902 -- Preanalyze_Actuals --
10903 ------------------------
10905 procedure Preanalyze_Actuals (N : Node_Id) is
10906 Assoc : Node_Id;
10907 Act : Node_Id;
10908 Errs : constant Int := Serious_Errors_Detected;
10910 Cur : Entity_Id := Empty;
10911 -- Current homograph of the instance name
10913 Vis : Boolean;
10914 -- Saved visibility status of the current homograph
10916 begin
10917 Assoc := First (Generic_Associations (N));
10919 -- If the instance is a child unit, its name may hide an outer homonym,
10920 -- so make it invisible to perform name resolution on the actuals.
10922 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
10923 and then Present
10924 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
10925 then
10926 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
10928 if Is_Compilation_Unit (Cur) then
10929 Vis := Is_Immediately_Visible (Cur);
10930 Set_Is_Immediately_Visible (Cur, False);
10931 else
10932 Cur := Empty;
10933 end if;
10934 end if;
10936 while Present (Assoc) loop
10937 if Nkind (Assoc) /= N_Others_Choice then
10938 Act := Explicit_Generic_Actual_Parameter (Assoc);
10940 -- Within a nested instantiation, a defaulted actual is an empty
10941 -- association, so nothing to analyze. If the subprogram actual
10942 -- is an attribute, analyze prefix only, because actual is not a
10943 -- complete attribute reference.
10945 -- If actual is an allocator, analyze expression only. The full
10946 -- analysis can generate code, and if instance is a compilation
10947 -- unit we have to wait until the package instance is installed
10948 -- to have a proper place to insert this code.
10950 -- String literals may be operators, but at this point we do not
10951 -- know whether the actual is a formal subprogram or a string.
10953 if No (Act) then
10954 null;
10956 elsif Nkind (Act) = N_Attribute_Reference then
10957 Analyze (Prefix (Act));
10959 elsif Nkind (Act) = N_Explicit_Dereference then
10960 Analyze (Prefix (Act));
10962 elsif Nkind (Act) = N_Allocator then
10963 declare
10964 Expr : constant Node_Id := Expression (Act);
10966 begin
10967 if Nkind (Expr) = N_Subtype_Indication then
10968 Analyze (Subtype_Mark (Expr));
10970 -- Analyze separately each discriminant constraint, when
10971 -- given with a named association.
10973 declare
10974 Constr : Node_Id;
10976 begin
10977 Constr := First (Constraints (Constraint (Expr)));
10978 while Present (Constr) loop
10979 if Nkind (Constr) = N_Discriminant_Association then
10980 Analyze (Expression (Constr));
10981 else
10982 Analyze (Constr);
10983 end if;
10985 Next (Constr);
10986 end loop;
10987 end;
10989 else
10990 Analyze (Expr);
10991 end if;
10992 end;
10994 elsif Nkind (Act) /= N_Operator_Symbol then
10995 Analyze (Act);
10996 end if;
10998 if Errs /= Serious_Errors_Detected then
11000 -- Do a minimal analysis of the generic, to prevent spurious
11001 -- warnings complaining about the generic being unreferenced,
11002 -- before abandoning the instantiation.
11004 Analyze (Name (N));
11006 if Is_Entity_Name (Name (N))
11007 and then Etype (Name (N)) /= Any_Type
11008 then
11009 Generate_Reference (Entity (Name (N)), Name (N));
11010 Set_Is_Instantiated (Entity (Name (N)));
11011 end if;
11013 if Present (Cur) then
11015 -- For the case of a child instance hiding an outer homonym,
11016 -- provide additional warning which might explain the error.
11018 Set_Is_Immediately_Visible (Cur, Vis);
11019 Error_Msg_NE ("& hides outer unit with the same name?",
11020 N, Defining_Unit_Name (N));
11021 end if;
11023 Abandon_Instantiation (Act);
11024 end if;
11025 end if;
11027 Next (Assoc);
11028 end loop;
11030 if Present (Cur) then
11031 Set_Is_Immediately_Visible (Cur, Vis);
11032 end if;
11033 end Preanalyze_Actuals;
11035 -------------------
11036 -- Remove_Parent --
11037 -------------------
11039 procedure Remove_Parent (In_Body : Boolean := False) is
11040 S : Entity_Id := Current_Scope;
11041 -- S is the scope containing the instantiation just completed. The scope
11042 -- stack contains the parent instances of the instantiation, followed by
11043 -- the original S.
11045 E : Entity_Id;
11046 P : Entity_Id;
11047 Hidden : Elmt_Id;
11049 begin
11050 -- After child instantiation is complete, remove from scope stack the
11051 -- extra copy of the current scope, and then remove parent instances.
11053 if not In_Body then
11054 Pop_Scope;
11056 while Current_Scope /= S loop
11057 P := Current_Scope;
11058 End_Package_Scope (Current_Scope);
11060 if In_Open_Scopes (P) then
11061 E := First_Entity (P);
11062 while Present (E) loop
11063 Set_Is_Immediately_Visible (E, True);
11064 Next_Entity (E);
11065 end loop;
11067 if Is_Generic_Instance (Current_Scope)
11068 and then P /= Current_Scope
11069 then
11070 -- We are within an instance of some sibling. Retain
11071 -- visibility of parent, for proper subsequent cleanup, and
11072 -- reinstall private declarations as well.
11074 Set_In_Private_Part (P);
11075 Install_Private_Declarations (P);
11076 end if;
11078 -- If the ultimate parent is a top-level unit recorded in
11079 -- Instance_Parent_Unit, then reset its visibility to what is was
11080 -- before instantiation. (It's not clear what the purpose is of
11081 -- testing whether Scope (P) is In_Open_Scopes, but that test was
11082 -- present before the ultimate parent test was added.???)
11084 elsif not In_Open_Scopes (Scope (P))
11085 or else (P = Instance_Parent_Unit
11086 and then not Parent_Unit_Visible)
11087 then
11088 Set_Is_Immediately_Visible (P, False);
11090 -- If the current scope is itself an instantiation of a generic
11091 -- nested within P, and we are in the private part of body of this
11092 -- instantiation, restore the full views of P, that were removed
11093 -- in End_Package_Scope above. This obscure case can occur when a
11094 -- subunit of a generic contains an instance of a child unit of
11095 -- its generic parent unit.
11097 elsif S = Current_Scope and then Is_Generic_Instance (S) then
11098 declare
11099 Par : constant Entity_Id :=
11100 Generic_Parent
11101 (Specification (Unit_Declaration_Node (S)));
11102 begin
11103 if Present (Par)
11104 and then P = Scope (Par)
11105 and then (In_Package_Body (S) or else In_Private_Part (S))
11106 then
11107 Set_In_Private_Part (P);
11108 Install_Private_Declarations (P);
11109 end if;
11110 end;
11111 end if;
11112 end loop;
11114 -- Reset visibility of entities in the enclosing scope
11116 Set_Is_Hidden_Open_Scope (Current_Scope, False);
11118 Hidden := First_Elmt (Hidden_Entities);
11119 while Present (Hidden) loop
11120 Set_Is_Immediately_Visible (Node (Hidden), True);
11121 Next_Elmt (Hidden);
11122 end loop;
11124 else
11125 -- Each body is analyzed separately, and there is no context that
11126 -- needs preserving from one body instance to the next, so remove all
11127 -- parent scopes that have been installed.
11129 while Present (S) loop
11130 End_Package_Scope (S);
11131 Set_Is_Immediately_Visible (S, False);
11132 S := Current_Scope;
11133 exit when S = Standard_Standard;
11134 end loop;
11135 end if;
11136 end Remove_Parent;
11138 -----------------
11139 -- Restore_Env --
11140 -----------------
11142 procedure Restore_Env is
11143 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
11145 begin
11146 if No (Current_Instantiated_Parent.Act_Id) then
11147 -- Restore environment after subprogram inlining
11149 Restore_Private_Views (Empty);
11150 end if;
11152 Current_Instantiated_Parent := Saved.Instantiated_Parent;
11153 Exchanged_Views := Saved.Exchanged_Views;
11154 Hidden_Entities := Saved.Hidden_Entities;
11155 Current_Sem_Unit := Saved.Current_Sem_Unit;
11156 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
11157 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
11159 Restore_Opt_Config_Switches (Saved.Switches);
11161 Instance_Envs.Decrement_Last;
11162 end Restore_Env;
11164 ---------------------------
11165 -- Restore_Private_Views --
11166 ---------------------------
11168 procedure Restore_Private_Views
11169 (Pack_Id : Entity_Id;
11170 Is_Package : Boolean := True)
11172 M : Elmt_Id;
11173 E : Entity_Id;
11174 Typ : Entity_Id;
11175 Dep_Elmt : Elmt_Id;
11176 Dep_Typ : Node_Id;
11178 procedure Restore_Nested_Formal (Formal : Entity_Id);
11179 -- Hide the generic formals of formal packages declared with box which
11180 -- were reachable in the current instantiation.
11182 ---------------------------
11183 -- Restore_Nested_Formal --
11184 ---------------------------
11186 procedure Restore_Nested_Formal (Formal : Entity_Id) is
11187 Ent : Entity_Id;
11189 begin
11190 if Present (Renamed_Object (Formal))
11191 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
11192 then
11193 return;
11195 elsif Present (Associated_Formal_Package (Formal)) then
11196 Ent := First_Entity (Formal);
11197 while Present (Ent) loop
11198 exit when Ekind (Ent) = E_Package
11199 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
11201 Set_Is_Hidden (Ent);
11202 Set_Is_Potentially_Use_Visible (Ent, False);
11204 -- If package, then recurse
11206 if Ekind (Ent) = E_Package then
11207 Restore_Nested_Formal (Ent);
11208 end if;
11210 Next_Entity (Ent);
11211 end loop;
11212 end if;
11213 end Restore_Nested_Formal;
11215 -- Start of processing for Restore_Private_Views
11217 begin
11218 M := First_Elmt (Exchanged_Views);
11219 while Present (M) loop
11220 Typ := Node (M);
11222 -- Subtypes of types whose views have been exchanged, and that
11223 -- are defined within the instance, were not on the list of
11224 -- Private_Dependents on entry to the instance, so they have to be
11225 -- exchanged explicitly now, in order to remain consistent with the
11226 -- view of the parent type.
11228 if Ekind (Typ) = E_Private_Type
11229 or else Ekind (Typ) = E_Limited_Private_Type
11230 or else Ekind (Typ) = E_Record_Type_With_Private
11231 then
11232 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
11233 while Present (Dep_Elmt) loop
11234 Dep_Typ := Node (Dep_Elmt);
11236 if Scope (Dep_Typ) = Pack_Id
11237 and then Present (Full_View (Dep_Typ))
11238 then
11239 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
11240 Exchange_Declarations (Dep_Typ);
11241 end if;
11243 Next_Elmt (Dep_Elmt);
11244 end loop;
11245 end if;
11247 Exchange_Declarations (Node (M));
11248 Next_Elmt (M);
11249 end loop;
11251 if No (Pack_Id) then
11252 return;
11253 end if;
11255 -- Make the generic formal parameters private, and make the formal types
11256 -- into subtypes of the actuals again.
11258 E := First_Entity (Pack_Id);
11259 while Present (E) loop
11260 Set_Is_Hidden (E, True);
11262 if Is_Type (E)
11263 and then Nkind (Parent (E)) = N_Subtype_Declaration
11264 then
11265 Set_Is_Generic_Actual_Type (E, False);
11267 -- An unusual case of aliasing: the actual may also be directly
11268 -- visible in the generic, and be private there, while it is fully
11269 -- visible in the context of the instance. The internal subtype
11270 -- is private in the instance, but has full visibility like its
11271 -- parent in the enclosing scope. This enforces the invariant that
11272 -- the privacy status of all private dependents of a type coincide
11273 -- with that of the parent type. This can only happen when a
11274 -- generic child unit is instantiated within sibling.
11276 if Is_Private_Type (E)
11277 and then not Is_Private_Type (Etype (E))
11278 then
11279 Exchange_Declarations (E);
11280 end if;
11282 elsif Ekind (E) = E_Package then
11284 -- The end of the renaming list is the renaming of the generic
11285 -- package itself. If the instance is a subprogram, all entities
11286 -- in the corresponding package are renamings. If this entity is
11287 -- a formal package, make its own formals private as well. The
11288 -- actual in this case is itself the renaming of an instantiation.
11289 -- If the entity is not a package renaming, it is the entity
11290 -- created to validate formal package actuals: ignore.
11292 -- If the actual is itself a formal package for the enclosing
11293 -- generic, or the actual for such a formal package, it remains
11294 -- visible on exit from the instance, and therefore nothing needs
11295 -- to be done either, except to keep it accessible.
11297 if Is_Package
11298 and then Renamed_Object (E) = Pack_Id
11299 then
11300 exit;
11302 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
11303 null;
11305 elsif
11306 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
11307 then
11308 Set_Is_Hidden (E, False);
11310 else
11311 declare
11312 Act_P : constant Entity_Id := Renamed_Object (E);
11313 Id : Entity_Id;
11315 begin
11316 Id := First_Entity (Act_P);
11317 while Present (Id)
11318 and then Id /= First_Private_Entity (Act_P)
11319 loop
11320 exit when Ekind (Id) = E_Package
11321 and then Renamed_Object (Id) = Act_P;
11323 Set_Is_Hidden (Id, True);
11324 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
11326 if Ekind (Id) = E_Package then
11327 Restore_Nested_Formal (Id);
11328 end if;
11330 Next_Entity (Id);
11331 end loop;
11332 end;
11333 end if;
11334 end if;
11336 Next_Entity (E);
11337 end loop;
11338 end Restore_Private_Views;
11340 --------------
11341 -- Save_Env --
11342 --------------
11344 procedure Save_Env
11345 (Gen_Unit : Entity_Id;
11346 Act_Unit : Entity_Id)
11348 begin
11349 Init_Env;
11350 Set_Instance_Env (Gen_Unit, Act_Unit);
11351 end Save_Env;
11353 ----------------------------
11354 -- Save_Global_References --
11355 ----------------------------
11357 procedure Save_Global_References (N : Node_Id) is
11358 Gen_Scope : Entity_Id;
11359 E : Entity_Id;
11360 N2 : Node_Id;
11362 function Is_Global (E : Entity_Id) return Boolean;
11363 -- Check whether entity is defined outside of generic unit. Examine the
11364 -- scope of an entity, and the scope of the scope, etc, until we find
11365 -- either Standard, in which case the entity is global, or the generic
11366 -- unit itself, which indicates that the entity is local. If the entity
11367 -- is the generic unit itself, as in the case of a recursive call, or
11368 -- the enclosing generic unit, if different from the current scope, then
11369 -- it is local as well, because it will be replaced at the point of
11370 -- instantiation. On the other hand, if it is a reference to a child
11371 -- unit of a common ancestor, which appears in an instantiation, it is
11372 -- global because it is used to denote a specific compilation unit at
11373 -- the time the instantiations will be analyzed.
11375 procedure Reset_Entity (N : Node_Id);
11376 -- Save semantic information on global entity so that it is not resolved
11377 -- again at instantiation time.
11379 procedure Save_Entity_Descendants (N : Node_Id);
11380 -- Apply Save_Global_References to the two syntactic descendants of
11381 -- non-terminal nodes that carry an Associated_Node and are processed
11382 -- through Reset_Entity. Once the global entity (if any) has been
11383 -- captured together with its type, only two syntactic descendants need
11384 -- to be traversed to complete the processing of the tree rooted at N.
11385 -- This applies to Selected_Components, Expanded_Names, and to Operator
11386 -- nodes. N can also be a character literal, identifier, or operator
11387 -- symbol node, but the call has no effect in these cases.
11389 procedure Save_Global_Defaults (N1, N2 : Node_Id);
11390 -- Default actuals in nested instances must be handled specially
11391 -- because there is no link to them from the original tree. When an
11392 -- actual subprogram is given by a default, we add an explicit generic
11393 -- association for it in the instantiation node. When we save the
11394 -- global references on the name of the instance, we recover the list
11395 -- of generic associations, and add an explicit one to the original
11396 -- generic tree, through which a global actual can be preserved.
11397 -- Similarly, if a child unit is instantiated within a sibling, in the
11398 -- context of the parent, we must preserve the identifier of the parent
11399 -- so that it can be properly resolved in a subsequent instantiation.
11401 procedure Save_Global_Descendant (D : Union_Id);
11402 -- Apply Save_Global_References recursively to the descendents of the
11403 -- current node.
11405 procedure Save_References (N : Node_Id);
11406 -- This is the recursive procedure that does the work, once the
11407 -- enclosing generic scope has been established.
11409 ---------------
11410 -- Is_Global --
11411 ---------------
11413 function Is_Global (E : Entity_Id) return Boolean is
11414 Se : Entity_Id;
11416 function Is_Instance_Node (Decl : Node_Id) return Boolean;
11417 -- Determine whether the parent node of a reference to a child unit
11418 -- denotes an instantiation or a formal package, in which case the
11419 -- reference to the child unit is global, even if it appears within
11420 -- the current scope (e.g. when the instance appears within the body
11421 -- of an ancestor).
11423 ----------------------
11424 -- Is_Instance_Node --
11425 ----------------------
11427 function Is_Instance_Node (Decl : Node_Id) return Boolean is
11428 begin
11429 return Nkind (Decl) in N_Generic_Instantiation
11430 or else
11431 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
11432 end Is_Instance_Node;
11434 -- Start of processing for Is_Global
11436 begin
11437 if E = Gen_Scope then
11438 return False;
11440 elsif E = Standard_Standard then
11441 return True;
11443 elsif Is_Child_Unit (E)
11444 and then (Is_Instance_Node (Parent (N2))
11445 or else (Nkind (Parent (N2)) = N_Expanded_Name
11446 and then N2 = Selector_Name (Parent (N2))
11447 and then
11448 Is_Instance_Node (Parent (Parent (N2)))))
11449 then
11450 return True;
11452 else
11453 Se := Scope (E);
11454 while Se /= Gen_Scope loop
11455 if Se = Standard_Standard then
11456 return True;
11457 else
11458 Se := Scope (Se);
11459 end if;
11460 end loop;
11462 return False;
11463 end if;
11464 end Is_Global;
11466 ------------------
11467 -- Reset_Entity --
11468 ------------------
11470 procedure Reset_Entity (N : Node_Id) is
11472 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
11473 -- If the type of N2 is global to the generic unit. Save the type in
11474 -- the generic node.
11475 -- What does this comment mean???
11477 function Top_Ancestor (E : Entity_Id) return Entity_Id;
11478 -- Find the ultimate ancestor of the current unit. If it is not a
11479 -- generic unit, then the name of the current unit in the prefix of
11480 -- an expanded name must be replaced with its generic homonym to
11481 -- ensure that it will be properly resolved in an instance.
11483 ---------------------
11484 -- Set_Global_Type --
11485 ---------------------
11487 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
11488 Typ : constant Entity_Id := Etype (N2);
11490 begin
11491 Set_Etype (N, Typ);
11493 if Entity (N) /= N2
11494 and then Has_Private_View (Entity (N))
11495 then
11496 -- If the entity of N is not the associated node, this is a
11497 -- nested generic and it has an associated node as well, whose
11498 -- type is already the full view (see below). Indicate that the
11499 -- original node has a private view.
11501 Set_Has_Private_View (N);
11502 end if;
11504 -- If not a private type, nothing else to do
11506 if not Is_Private_Type (Typ) then
11507 if Is_Array_Type (Typ)
11508 and then Is_Private_Type (Component_Type (Typ))
11509 then
11510 Set_Has_Private_View (N);
11511 end if;
11513 -- If it is a derivation of a private type in a context where no
11514 -- full view is needed, nothing to do either.
11516 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
11517 null;
11519 -- Otherwise mark the type for flipping and use the full view when
11520 -- available.
11522 else
11523 Set_Has_Private_View (N);
11525 if Present (Full_View (Typ)) then
11526 Set_Etype (N2, Full_View (Typ));
11527 end if;
11528 end if;
11529 end Set_Global_Type;
11531 ------------------
11532 -- Top_Ancestor --
11533 ------------------
11535 function Top_Ancestor (E : Entity_Id) return Entity_Id is
11536 Par : Entity_Id;
11538 begin
11539 Par := E;
11540 while Is_Child_Unit (Par) loop
11541 Par := Scope (Par);
11542 end loop;
11544 return Par;
11545 end Top_Ancestor;
11547 -- Start of processing for Reset_Entity
11549 begin
11550 N2 := Get_Associated_Node (N);
11551 E := Entity (N2);
11553 if Present (E) then
11554 if Is_Global (E) then
11555 Set_Global_Type (N, N2);
11557 elsif Nkind (N) = N_Op_Concat
11558 and then Is_Generic_Type (Etype (N2))
11559 and then
11560 (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
11561 or else Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
11562 and then Is_Intrinsic_Subprogram (E)
11563 then
11564 null;
11566 else
11567 -- Entity is local. Mark generic node as unresolved.
11568 -- Note that now it does not have an entity.
11570 Set_Associated_Node (N, Empty);
11571 Set_Etype (N, Empty);
11572 end if;
11574 if Nkind (Parent (N)) in N_Generic_Instantiation
11575 and then N = Name (Parent (N))
11576 then
11577 Save_Global_Defaults (Parent (N), Parent (N2));
11578 end if;
11580 elsif Nkind (Parent (N)) = N_Selected_Component
11581 and then Nkind (Parent (N2)) = N_Expanded_Name
11582 then
11583 if Is_Global (Entity (Parent (N2))) then
11584 Change_Selected_Component_To_Expanded_Name (Parent (N));
11585 Set_Associated_Node (Parent (N), Parent (N2));
11586 Set_Global_Type (Parent (N), Parent (N2));
11587 Save_Entity_Descendants (N);
11589 -- If this is a reference to the current generic entity, replace
11590 -- by the name of the generic homonym of the current package. This
11591 -- is because in an instantiation Par.P.Q will not resolve to the
11592 -- name of the instance, whose enclosing scope is not necessarily
11593 -- Par. We use the generic homonym rather that the name of the
11594 -- generic itself because it may be hidden by a local declaration.
11596 elsif In_Open_Scopes (Entity (Parent (N2)))
11597 and then not
11598 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
11599 then
11600 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
11601 Rewrite (Parent (N),
11602 Make_Identifier (Sloc (N),
11603 Chars =>
11604 Chars (Generic_Homonym (Entity (Parent (N2))))));
11605 else
11606 Rewrite (Parent (N),
11607 Make_Identifier (Sloc (N),
11608 Chars => Chars (Selector_Name (Parent (N2)))));
11609 end if;
11610 end if;
11612 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
11613 and then Parent (N) = Name (Parent (Parent (N)))
11614 then
11615 Save_Global_Defaults
11616 (Parent (Parent (N)), Parent (Parent ((N2))));
11617 end if;
11619 -- A selected component may denote a static constant that has been
11620 -- folded. If the static constant is global to the generic, capture
11621 -- its value. Otherwise the folding will happen in any instantiation.
11623 elsif Nkind (Parent (N)) = N_Selected_Component
11624 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
11625 then
11626 if Present (Entity (Original_Node (Parent (N2))))
11627 and then Is_Global (Entity (Original_Node (Parent (N2))))
11628 then
11629 Rewrite (Parent (N), New_Copy (Parent (N2)));
11630 Set_Analyzed (Parent (N), False);
11632 else
11633 null;
11634 end if;
11636 -- A selected component may be transformed into a parameterless
11637 -- function call. If the called entity is global, rewrite the node
11638 -- appropriately, i.e. as an extended name for the global entity.
11640 elsif Nkind (Parent (N)) = N_Selected_Component
11641 and then Nkind (Parent (N2)) = N_Function_Call
11642 and then N = Selector_Name (Parent (N))
11643 then
11644 if No (Parameter_Associations (Parent (N2))) then
11645 if Is_Global (Entity (Name (Parent (N2)))) then
11646 Change_Selected_Component_To_Expanded_Name (Parent (N));
11647 Set_Associated_Node (Parent (N), Name (Parent (N2)));
11648 Set_Global_Type (Parent (N), Name (Parent (N2)));
11649 Save_Entity_Descendants (N);
11651 else
11652 Set_Associated_Node (N, Empty);
11653 Set_Etype (N, Empty);
11654 end if;
11656 -- In Ada 2005, X.F may be a call to a primitive operation,
11657 -- rewritten as F (X). This rewriting will be done again in an
11658 -- instance, so keep the original node. Global entities will be
11659 -- captured as for other constructs.
11661 else
11662 null;
11663 end if;
11665 -- Entity is local. Reset in generic unit, so that node is resolved
11666 -- anew at the point of instantiation.
11668 else
11669 Set_Associated_Node (N, Empty);
11670 Set_Etype (N, Empty);
11671 end if;
11672 end Reset_Entity;
11674 -----------------------------
11675 -- Save_Entity_Descendants --
11676 -----------------------------
11678 procedure Save_Entity_Descendants (N : Node_Id) is
11679 begin
11680 case Nkind (N) is
11681 when N_Binary_Op =>
11682 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
11683 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
11685 when N_Unary_Op =>
11686 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
11688 when N_Expanded_Name | N_Selected_Component =>
11689 Save_Global_Descendant (Union_Id (Prefix (N)));
11690 Save_Global_Descendant (Union_Id (Selector_Name (N)));
11692 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
11693 null;
11695 when others =>
11696 raise Program_Error;
11697 end case;
11698 end Save_Entity_Descendants;
11700 --------------------------
11701 -- Save_Global_Defaults --
11702 --------------------------
11704 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
11705 Loc : constant Source_Ptr := Sloc (N1);
11706 Assoc2 : constant List_Id := Generic_Associations (N2);
11707 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
11708 Assoc1 : List_Id;
11709 Act1 : Node_Id;
11710 Act2 : Node_Id;
11711 Def : Node_Id;
11712 Ndec : Node_Id;
11713 Subp : Entity_Id;
11714 Actual : Entity_Id;
11716 begin
11717 Assoc1 := Generic_Associations (N1);
11719 if Present (Assoc1) then
11720 Act1 := First (Assoc1);
11721 else
11722 Act1 := Empty;
11723 Set_Generic_Associations (N1, New_List);
11724 Assoc1 := Generic_Associations (N1);
11725 end if;
11727 if Present (Assoc2) then
11728 Act2 := First (Assoc2);
11729 else
11730 return;
11731 end if;
11733 while Present (Act1) and then Present (Act2) loop
11734 Next (Act1);
11735 Next (Act2);
11736 end loop;
11738 -- Find the associations added for default subprograms
11740 if Present (Act2) then
11741 while Nkind (Act2) /= N_Generic_Association
11742 or else No (Entity (Selector_Name (Act2)))
11743 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
11744 loop
11745 Next (Act2);
11746 end loop;
11748 -- Add a similar association if the default is global. The
11749 -- renaming declaration for the actual has been analyzed, and
11750 -- its alias is the program it renames. Link the actual in the
11751 -- original generic tree with the node in the analyzed tree.
11753 while Present (Act2) loop
11754 Subp := Entity (Selector_Name (Act2));
11755 Def := Explicit_Generic_Actual_Parameter (Act2);
11757 -- Following test is defence against rubbish errors
11759 if No (Alias (Subp)) then
11760 return;
11761 end if;
11763 -- Retrieve the resolved actual from the renaming declaration
11764 -- created for the instantiated formal.
11766 Actual := Entity (Name (Parent (Parent (Subp))));
11767 Set_Entity (Def, Actual);
11768 Set_Etype (Def, Etype (Actual));
11770 if Is_Global (Actual) then
11771 Ndec :=
11772 Make_Generic_Association (Loc,
11773 Selector_Name => New_Occurrence_Of (Subp, Loc),
11774 Explicit_Generic_Actual_Parameter =>
11775 New_Occurrence_Of (Actual, Loc));
11777 Set_Associated_Node
11778 (Explicit_Generic_Actual_Parameter (Ndec), Def);
11780 Append (Ndec, Assoc1);
11782 -- If there are other defaults, add a dummy association in case
11783 -- there are other defaulted formals with the same name.
11785 elsif Present (Next (Act2)) then
11786 Ndec :=
11787 Make_Generic_Association (Loc,
11788 Selector_Name => New_Occurrence_Of (Subp, Loc),
11789 Explicit_Generic_Actual_Parameter => Empty);
11791 Append (Ndec, Assoc1);
11792 end if;
11794 Next (Act2);
11795 end loop;
11796 end if;
11798 if Nkind (Name (N1)) = N_Identifier
11799 and then Is_Child_Unit (Gen_Id)
11800 and then Is_Global (Gen_Id)
11801 and then Is_Generic_Unit (Scope (Gen_Id))
11802 and then In_Open_Scopes (Scope (Gen_Id))
11803 then
11804 -- This is an instantiation of a child unit within a sibling,
11805 -- so that the generic parent is in scope. An eventual instance
11806 -- must occur within the scope of an instance of the parent.
11807 -- Make name in instance into an expanded name, to preserve the
11808 -- identifier of the parent, so it can be resolved subsequently.
11810 Rewrite (Name (N2),
11811 Make_Expanded_Name (Loc,
11812 Chars => Chars (Gen_Id),
11813 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
11814 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
11815 Set_Entity (Name (N2), Gen_Id);
11817 Rewrite (Name (N1),
11818 Make_Expanded_Name (Loc,
11819 Chars => Chars (Gen_Id),
11820 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
11821 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
11823 Set_Associated_Node (Name (N1), Name (N2));
11824 Set_Associated_Node (Prefix (Name (N1)), Empty);
11825 Set_Associated_Node
11826 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
11827 Set_Etype (Name (N1), Etype (Gen_Id));
11828 end if;
11830 end Save_Global_Defaults;
11832 ----------------------------
11833 -- Save_Global_Descendant --
11834 ----------------------------
11836 procedure Save_Global_Descendant (D : Union_Id) is
11837 N1 : Node_Id;
11839 begin
11840 if D in Node_Range then
11841 if D = Union_Id (Empty) then
11842 null;
11844 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
11845 Save_References (Node_Id (D));
11846 end if;
11848 elsif D in List_Range then
11849 if D = Union_Id (No_List)
11850 or else Is_Empty_List (List_Id (D))
11851 then
11852 null;
11854 else
11855 N1 := First (List_Id (D));
11856 while Present (N1) loop
11857 Save_References (N1);
11858 Next (N1);
11859 end loop;
11860 end if;
11862 -- Element list or other non-node field, nothing to do
11864 else
11865 null;
11866 end if;
11867 end Save_Global_Descendant;
11869 ---------------------
11870 -- Save_References --
11871 ---------------------
11873 -- This is the recursive procedure that does the work once the enclosing
11874 -- generic scope has been established. We have to treat specially a
11875 -- number of node rewritings that are required by semantic processing
11876 -- and which change the kind of nodes in the generic copy: typically
11877 -- constant-folding, replacing an operator node by a string literal, or
11878 -- a selected component by an expanded name. In each of those cases, the
11879 -- transformation is propagated to the generic unit.
11881 procedure Save_References (N : Node_Id) is
11882 Loc : constant Source_Ptr := Sloc (N);
11884 begin
11885 if N = Empty then
11886 null;
11888 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
11889 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
11890 Reset_Entity (N);
11892 elsif Nkind (N) = N_Operator_Symbol
11893 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
11894 then
11895 Change_Operator_Symbol_To_String_Literal (N);
11896 end if;
11898 elsif Nkind (N) in N_Op then
11899 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
11900 if Nkind (N) = N_Op_Concat then
11901 Set_Is_Component_Left_Opnd (N,
11902 Is_Component_Left_Opnd (Get_Associated_Node (N)));
11904 Set_Is_Component_Right_Opnd (N,
11905 Is_Component_Right_Opnd (Get_Associated_Node (N)));
11906 end if;
11908 Reset_Entity (N);
11910 else
11911 -- Node may be transformed into call to a user-defined operator
11913 N2 := Get_Associated_Node (N);
11915 if Nkind (N2) = N_Function_Call then
11916 E := Entity (Name (N2));
11918 if Present (E)
11919 and then Is_Global (E)
11920 then
11921 Set_Etype (N, Etype (N2));
11922 else
11923 Set_Associated_Node (N, Empty);
11924 Set_Etype (N, Empty);
11925 end if;
11927 elsif Nkind_In (N2, N_Integer_Literal,
11928 N_Real_Literal,
11929 N_String_Literal)
11930 then
11931 if Present (Original_Node (N2))
11932 and then Nkind (Original_Node (N2)) = Nkind (N)
11933 then
11935 -- Operation was constant-folded. Whenever possible,
11936 -- recover semantic information from unfolded node,
11937 -- for ASIS use.
11939 Set_Associated_Node (N, Original_Node (N2));
11941 if Nkind (N) = N_Op_Concat then
11942 Set_Is_Component_Left_Opnd (N,
11943 Is_Component_Left_Opnd (Get_Associated_Node (N)));
11944 Set_Is_Component_Right_Opnd (N,
11945 Is_Component_Right_Opnd (Get_Associated_Node (N)));
11946 end if;
11948 Reset_Entity (N);
11950 else
11951 -- If original node is already modified, propagate
11952 -- constant-folding to template.
11954 Rewrite (N, New_Copy (N2));
11955 Set_Analyzed (N, False);
11956 end if;
11958 elsif Nkind (N2) = N_Identifier
11959 and then Ekind (Entity (N2)) = E_Enumeration_Literal
11960 then
11961 -- Same if call was folded into a literal, but in this case
11962 -- retain the entity to avoid spurious ambiguities if it is
11963 -- overloaded at the point of instantiation or inlining.
11965 Rewrite (N, New_Copy (N2));
11966 Set_Analyzed (N, False);
11967 end if;
11968 end if;
11970 -- Complete operands check if node has not been constant-folded
11972 if Nkind (N) in N_Op then
11973 Save_Entity_Descendants (N);
11974 end if;
11976 elsif Nkind (N) = N_Identifier then
11977 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
11979 -- If this is a discriminant reference, always save it. It is
11980 -- used in the instance to find the corresponding discriminant
11981 -- positionally rather than by name.
11983 Set_Original_Discriminant
11984 (N, Original_Discriminant (Get_Associated_Node (N)));
11985 Reset_Entity (N);
11987 else
11988 N2 := Get_Associated_Node (N);
11990 if Nkind (N2) = N_Function_Call then
11991 E := Entity (Name (N2));
11993 -- Name resolves to a call to parameterless function. If
11994 -- original entity is global, mark node as resolved.
11996 if Present (E)
11997 and then Is_Global (E)
11998 then
11999 Set_Etype (N, Etype (N2));
12000 else
12001 Set_Associated_Node (N, Empty);
12002 Set_Etype (N, Empty);
12003 end if;
12005 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
12006 and then Is_Entity_Name (Original_Node (N2))
12007 then
12008 -- Name resolves to named number that is constant-folded,
12009 -- We must preserve the original name for ASIS use, and
12010 -- undo the constant-folding, which will be repeated in
12011 -- each instance.
12013 Set_Associated_Node (N, Original_Node (N2));
12014 Reset_Entity (N);
12016 elsif Nkind (N2) = N_String_Literal then
12018 -- Name resolves to string literal. Perform the same
12019 -- replacement in generic.
12021 Rewrite (N, New_Copy (N2));
12023 elsif Nkind (N2) = N_Explicit_Dereference then
12025 -- An identifier is rewritten as a dereference if it is the
12026 -- prefix in an implicit dereference.
12028 -- Check whether corresponding entity in prefix is global
12030 if Is_Entity_Name (Prefix (N2))
12031 and then Present (Entity (Prefix (N2)))
12032 and then Is_Global (Entity (Prefix (N2)))
12033 then
12034 Rewrite (N,
12035 Make_Explicit_Dereference (Loc,
12036 Prefix =>
12037 New_Occurrence_Of (Entity (Prefix (N2)), Loc)));
12038 elsif Nkind (Prefix (N2)) = N_Function_Call
12039 and then Is_Global (Entity (Name (Prefix (N2))))
12040 then
12041 Rewrite (N,
12042 Make_Explicit_Dereference (Loc,
12043 Prefix => Make_Function_Call (Loc,
12044 Name =>
12045 New_Occurrence_Of (Entity (Name (Prefix (N2))),
12046 Loc))));
12048 else
12049 Set_Associated_Node (N, Empty);
12050 Set_Etype (N, Empty);
12051 end if;
12053 -- The subtype mark of a nominally unconstrained object is
12054 -- rewritten as a subtype indication using the bounds of the
12055 -- expression. Recover the original subtype mark.
12057 elsif Nkind (N2) = N_Subtype_Indication
12058 and then Is_Entity_Name (Original_Node (N2))
12059 then
12060 Set_Associated_Node (N, Original_Node (N2));
12061 Reset_Entity (N);
12063 else
12064 null;
12065 end if;
12066 end if;
12068 elsif Nkind (N) in N_Entity then
12069 null;
12071 else
12072 declare
12073 Qual : Node_Id := Empty;
12074 Typ : Entity_Id := Empty;
12075 Nam : Node_Id;
12077 use Atree.Unchecked_Access;
12078 -- This code section is part of implementing an untyped tree
12079 -- traversal, so it needs direct access to node fields.
12081 begin
12082 if Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
12083 N2 := Get_Associated_Node (N);
12085 if No (N2) then
12086 Typ := Empty;
12087 else
12088 Typ := Etype (N2);
12090 -- In an instance within a generic, use the name of the
12091 -- actual and not the original generic parameter. If the
12092 -- actual is global in the current generic it must be
12093 -- preserved for its instantiation.
12095 if Nkind (Parent (Typ)) = N_Subtype_Declaration
12096 and then
12097 Present (Generic_Parent_Type (Parent (Typ)))
12098 then
12099 Typ := Base_Type (Typ);
12100 Set_Etype (N2, Typ);
12101 end if;
12102 end if;
12104 if No (N2)
12105 or else No (Typ)
12106 or else not Is_Global (Typ)
12107 then
12108 Set_Associated_Node (N, Empty);
12110 -- If the aggregate is an actual in a call, it has been
12111 -- resolved in the current context, to some local type.
12112 -- The enclosing call may have been disambiguated by the
12113 -- aggregate, and this disambiguation might fail at
12114 -- instantiation time because the type to which the
12115 -- aggregate did resolve is not preserved. In order to
12116 -- preserve some of this information, we wrap the
12117 -- aggregate in a qualified expression, using the id of
12118 -- its type. For further disambiguation we qualify the
12119 -- type name with its scope (if visible) because both
12120 -- id's will have corresponding entities in an instance.
12121 -- This resolves most of the problems with missing type
12122 -- information on aggregates in instances.
12124 if Nkind (N2) = Nkind (N)
12125 and then
12126 Nkind_In (Parent (N2), N_Procedure_Call_Statement,
12127 N_Function_Call)
12128 and then Comes_From_Source (Typ)
12129 then
12130 if Is_Immediately_Visible (Scope (Typ)) then
12131 Nam := Make_Selected_Component (Loc,
12132 Prefix =>
12133 Make_Identifier (Loc, Chars (Scope (Typ))),
12134 Selector_Name =>
12135 Make_Identifier (Loc, Chars (Typ)));
12136 else
12137 Nam := Make_Identifier (Loc, Chars (Typ));
12138 end if;
12140 Qual :=
12141 Make_Qualified_Expression (Loc,
12142 Subtype_Mark => Nam,
12143 Expression => Relocate_Node (N));
12144 end if;
12145 end if;
12147 Save_Global_Descendant (Field1 (N));
12148 Save_Global_Descendant (Field2 (N));
12149 Save_Global_Descendant (Field3 (N));
12150 Save_Global_Descendant (Field5 (N));
12152 if Present (Qual) then
12153 Rewrite (N, Qual);
12154 end if;
12156 -- All other cases than aggregates
12158 else
12159 Save_Global_Descendant (Field1 (N));
12160 Save_Global_Descendant (Field2 (N));
12161 Save_Global_Descendant (Field3 (N));
12162 Save_Global_Descendant (Field4 (N));
12163 Save_Global_Descendant (Field5 (N));
12164 end if;
12165 end;
12166 end if;
12167 end Save_References;
12169 -- Start of processing for Save_Global_References
12171 begin
12172 Gen_Scope := Current_Scope;
12174 -- If the generic unit is a child unit, references to entities in the
12175 -- parent are treated as local, because they will be resolved anew in
12176 -- the context of the instance of the parent.
12178 while Is_Child_Unit (Gen_Scope)
12179 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
12180 loop
12181 Gen_Scope := Scope (Gen_Scope);
12182 end loop;
12184 Save_References (N);
12185 end Save_Global_References;
12187 --------------------------------------
12188 -- Set_Copied_Sloc_For_Inlined_Body --
12189 --------------------------------------
12191 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
12192 begin
12193 Create_Instantiation_Source (N, E, True, S_Adjustment);
12194 end Set_Copied_Sloc_For_Inlined_Body;
12196 ---------------------
12197 -- Set_Instance_Of --
12198 ---------------------
12200 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
12201 begin
12202 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
12203 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
12204 Generic_Renamings.Increment_Last;
12205 end Set_Instance_Of;
12207 --------------------
12208 -- Set_Next_Assoc --
12209 --------------------
12211 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
12212 begin
12213 Generic_Renamings.Table (E).Next_In_HTable := Next;
12214 end Set_Next_Assoc;
12216 -------------------
12217 -- Start_Generic --
12218 -------------------
12220 procedure Start_Generic is
12221 begin
12222 -- ??? More things could be factored out in this routine.
12223 -- Should probably be done at a later stage.
12225 Generic_Flags.Append (Inside_A_Generic);
12226 Inside_A_Generic := True;
12228 Expander_Mode_Save_And_Set (False);
12229 end Start_Generic;
12231 ----------------------
12232 -- Set_Instance_Env --
12233 ----------------------
12235 procedure Set_Instance_Env
12236 (Gen_Unit : Entity_Id;
12237 Act_Unit : Entity_Id)
12239 begin
12240 -- Regardless of the current mode, predefined units are analyzed in
12241 -- the most current Ada mode, and earlier version Ada checks do not
12242 -- apply to predefined units. Nothing needs to be done for non-internal
12243 -- units. These are always analyzed in the current mode.
12245 if Is_Internal_File_Name
12246 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
12247 Renamings_Included => True)
12248 then
12249 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
12250 end if;
12252 Current_Instantiated_Parent := (Gen_Unit, Act_Unit, Assoc_Null);
12253 end Set_Instance_Env;
12255 -----------------
12256 -- Switch_View --
12257 -----------------
12259 procedure Switch_View (T : Entity_Id) is
12260 BT : constant Entity_Id := Base_Type (T);
12261 Priv_Elmt : Elmt_Id := No_Elmt;
12262 Priv_Sub : Entity_Id;
12264 begin
12265 -- T may be private but its base type may have been exchanged through
12266 -- some other occurrence, in which case there is nothing to switch
12267 -- besides T itself. Note that a private dependent subtype of a private
12268 -- type might not have been switched even if the base type has been,
12269 -- because of the last branch of Check_Private_View (see comment there).
12271 if not Is_Private_Type (BT) then
12272 Prepend_Elmt (Full_View (T), Exchanged_Views);
12273 Exchange_Declarations (T);
12274 return;
12275 end if;
12277 Priv_Elmt := First_Elmt (Private_Dependents (BT));
12279 if Present (Full_View (BT)) then
12280 Prepend_Elmt (Full_View (BT), Exchanged_Views);
12281 Exchange_Declarations (BT);
12282 end if;
12284 while Present (Priv_Elmt) loop
12285 Priv_Sub := (Node (Priv_Elmt));
12287 -- We avoid flipping the subtype if the Etype of its full view is
12288 -- private because this would result in a malformed subtype. This
12289 -- occurs when the Etype of the subtype full view is the full view of
12290 -- the base type (and since the base types were just switched, the
12291 -- subtype is pointing to the wrong view). This is currently the case
12292 -- for tagged record types, access types (maybe more?) and needs to
12293 -- be resolved. ???
12295 if Present (Full_View (Priv_Sub))
12296 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
12297 then
12298 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
12299 Exchange_Declarations (Priv_Sub);
12300 end if;
12302 Next_Elmt (Priv_Elmt);
12303 end loop;
12304 end Switch_View;
12306 -----------------------------
12307 -- Valid_Default_Attribute --
12308 -----------------------------
12310 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
12311 Attr_Id : constant Attribute_Id :=
12312 Get_Attribute_Id (Attribute_Name (Def));
12313 T : constant Entity_Id := Entity (Prefix (Def));
12314 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
12315 F : Entity_Id;
12316 Num_F : Int;
12317 OK : Boolean;
12319 begin
12320 if No (T)
12321 or else T = Any_Id
12322 then
12323 return;
12324 end if;
12326 Num_F := 0;
12327 F := First_Formal (Nam);
12328 while Present (F) loop
12329 Num_F := Num_F + 1;
12330 Next_Formal (F);
12331 end loop;
12333 case Attr_Id is
12334 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
12335 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
12336 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
12337 Attribute_Unbiased_Rounding =>
12338 OK := Is_Fun
12339 and then Num_F = 1
12340 and then Is_Floating_Point_Type (T);
12342 when Attribute_Image | Attribute_Pred | Attribute_Succ |
12343 Attribute_Value | Attribute_Wide_Image |
12344 Attribute_Wide_Value =>
12345 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
12347 when Attribute_Max | Attribute_Min =>
12348 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
12350 when Attribute_Input =>
12351 OK := (Is_Fun and then Num_F = 1);
12353 when Attribute_Output | Attribute_Read | Attribute_Write =>
12354 OK := (not Is_Fun and then Num_F = 2);
12356 when others =>
12357 OK := False;
12358 end case;
12360 if not OK then
12361 Error_Msg_N ("attribute reference has wrong profile for subprogram",
12362 Def);
12363 end if;
12364 end Valid_Default_Attribute;
12366 end Sem_Ch12;