* gcc.dg/compat/struct-layout-1_generate.c (dg_options): New. Moved
[official-gcc.git] / gcc / ada / sem_ch12.adb
blob549d5b4ee3c0fea18cda6abcb150b8f2a6fa3511
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-2008, 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 Lib; use Lib;
36 with Lib.Load; use Lib.Load;
37 with Lib.Xref; use Lib.Xref;
38 with Nlists; use Nlists;
39 with Namet; use Namet;
40 with Nmake; use Nmake;
41 with Opt; use Opt;
42 with Rident; use Rident;
43 with Restrict; use Restrict;
44 with Rtsfind; use Rtsfind;
45 with Sem; use Sem;
46 with Sem_Cat; use Sem_Cat;
47 with Sem_Ch3; use Sem_Ch3;
48 with Sem_Ch6; use Sem_Ch6;
49 with Sem_Ch7; use Sem_Ch7;
50 with Sem_Ch8; use Sem_Ch8;
51 with Sem_Ch10; use Sem_Ch10;
52 with Sem_Ch13; use Sem_Ch13;
53 with Sem_Disp; use Sem_Disp;
54 with Sem_Elab; use Sem_Elab;
55 with Sem_Elim; use Sem_Elim;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Res; use Sem_Res;
58 with Sem_Type; use Sem_Type;
59 with Sem_Util; use Sem_Util;
60 with Sem_Warn; use Sem_Warn;
61 with Stand; use Stand;
62 with Sinfo; use Sinfo;
63 with Sinfo.CN; use Sinfo.CN;
64 with Sinput; use Sinput;
65 with Sinput.L; use Sinput.L;
66 with Snames; use Snames;
67 with Stringt; use Stringt;
68 with Uname; use Uname;
69 with Table;
70 with Tbuild; use Tbuild;
71 with Uintp; use Uintp;
72 with Urealp; use Urealp;
74 with GNAT.HTable;
76 package body Sem_Ch12 is
78 ----------------------------------------------------------
79 -- Implementation of Generic Analysis and Instantiation --
80 ----------------------------------------------------------
82 -- GNAT implements generics by macro expansion. No attempt is made to share
83 -- generic instantiations (for now). Analysis of a generic definition does
84 -- not perform any expansion action, but the expander must be called on the
85 -- tree for each instantiation, because the expansion may of course depend
86 -- on the generic actuals. All of this is best achieved as follows:
88 -- a) Semantic analysis of a generic unit is performed on a copy of the
89 -- tree for the generic unit. All tree modifications that follow analysis
90 -- do not affect the original tree. Links are kept between the original
91 -- tree and the copy, in order to recognize non-local references within
92 -- the generic, and propagate them to each instance (recall that name
93 -- resolution is done on the generic declaration: generics are not really
94 -- macros!). This is summarized in the following diagram:
96 -- .-----------. .----------.
97 -- | semantic |<--------------| generic |
98 -- | copy | | unit |
99 -- | |==============>| |
100 -- |___________| global |__________|
101 -- references | | |
102 -- | | |
103 -- .-----|--|.
104 -- | .-----|---.
105 -- | | .----------.
106 -- | | | generic |
107 -- |__| | |
108 -- |__| instance |
109 -- |__________|
111 -- b) Each instantiation copies the original tree, and inserts into it a
112 -- series of declarations that describe the mapping between generic formals
113 -- and actuals. For example, a generic In OUT parameter is an object
114 -- renaming of the corresponding actual, etc. Generic IN parameters are
115 -- constant declarations.
117 -- c) In order to give the right visibility for these renamings, we use
118 -- a different scheme for package and subprogram instantiations. For
119 -- packages, the list of renamings is inserted into the package
120 -- specification, before the visible declarations of the package. The
121 -- renamings are analyzed before any of the text of the instance, and are
122 -- thus visible at the right place. Furthermore, outside of the instance,
123 -- the generic parameters are visible and denote their corresponding
124 -- actuals.
126 -- For subprograms, we create a container package to hold the renamings
127 -- and the subprogram instance itself. Analysis of the package makes the
128 -- renaming declarations visible to the subprogram. After analyzing the
129 -- package, the defining entity for the subprogram is touched-up so that
130 -- it appears declared in the current scope, and not inside the container
131 -- package.
133 -- If the instantiation is a compilation unit, the container package is
134 -- given the same name as the subprogram instance. This ensures that
135 -- the elaboration procedure called by the binder, using the compilation
136 -- unit name, calls in fact the elaboration procedure for the package.
138 -- Not surprisingly, private types complicate this approach. By saving in
139 -- the original generic object the non-local references, we guarantee that
140 -- the proper entities are referenced at the point of instantiation.
141 -- However, for private types, this by itself does not insure that the
142 -- proper VIEW of the entity is used (the full type may be visible at the
143 -- point of generic definition, but not at instantiation, or vice-versa).
144 -- In order to reference the proper view, we special-case any reference
145 -- to private types in the generic object, by saving both views, one in
146 -- the generic and one in the semantic copy. At time of instantiation, we
147 -- check whether the two views are consistent, and exchange declarations if
148 -- necessary, in order to restore the correct visibility. Similarly, if
149 -- the instance view is private when the generic view was not, we perform
150 -- the exchange. After completing the instantiation, we restore the
151 -- current visibility. The flag Has_Private_View marks identifiers in the
152 -- the generic unit that require checking.
154 -- Visibility within nested generic units requires special handling.
155 -- Consider the following scheme:
157 -- type Global is ... -- outside of generic unit.
158 -- generic ...
159 -- package Outer is
160 -- ...
161 -- type Semi_Global is ... -- global to inner.
163 -- generic ... -- 1
164 -- procedure inner (X1 : Global; X2 : Semi_Global);
166 -- procedure in2 is new inner (...); -- 4
167 -- end Outer;
169 -- package New_Outer is new Outer (...); -- 2
170 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
172 -- The semantic analysis of Outer captures all occurrences of Global.
173 -- The semantic analysis of Inner (at 1) captures both occurrences of
174 -- Global and Semi_Global.
176 -- At point 2 (instantiation of Outer), we also produce a generic copy
177 -- of Inner, even though Inner is, at that point, not being instantiated.
178 -- (This is just part of the semantic analysis of New_Outer).
180 -- Critically, references to Global within Inner must be preserved, while
181 -- references to Semi_Global should not preserved, because they must now
182 -- resolve to an entity within New_Outer. To distinguish between these, we
183 -- use a global variable, Current_Instantiated_Parent, which is set when
184 -- performing a generic copy during instantiation (at 2). This variable is
185 -- used when performing a generic copy that is not an instantiation, but
186 -- that is nested within one, as the occurrence of 1 within 2. The analysis
187 -- of a nested generic only preserves references that are global to the
188 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
189 -- determine whether a reference is external to the given parent.
191 -- The instantiation at point 3 requires no special treatment. The method
192 -- works as well for further nestings of generic units, but of course the
193 -- variable Current_Instantiated_Parent must be stacked because nested
194 -- instantiations can occur, e.g. the occurrence of 4 within 2.
196 -- The instantiation of package and subprogram bodies is handled in a
197 -- similar manner, except that it is delayed until after semantic
198 -- analysis is complete. In this fashion complex cross-dependencies
199 -- between several package declarations and bodies containing generics
200 -- can be compiled which otherwise would diagnose spurious circularities.
202 -- For example, it is possible to compile two packages A and B that
203 -- have the following structure:
205 -- package A is package B is
206 -- generic ... generic ...
207 -- package G_A is package G_B is
209 -- with B; with A;
210 -- package body A is package body B is
211 -- package N_B is new G_B (..) package N_A is new G_A (..)
213 -- The table Pending_Instantiations in package Inline is used to keep
214 -- track of body instantiations that are delayed in this manner. Inline
215 -- handles the actual calls to do the body instantiations. This activity
216 -- is part of Inline, since the processing occurs at the same point, and
217 -- for essentially the same reason, as the handling of inlined routines.
219 ----------------------------------------------
220 -- Detection of Instantiation Circularities --
221 ----------------------------------------------
223 -- If we have a chain of instantiations that is circular, this is static
224 -- error which must be detected at compile time. The detection of these
225 -- circularities is carried out at the point that we insert a generic
226 -- instance spec or body. If there is a circularity, then the analysis of
227 -- the offending spec or body will eventually result in trying to load the
228 -- same unit again, and we detect this problem as we analyze the package
229 -- instantiation for the second time.
231 -- At least in some cases after we have detected the circularity, we get
232 -- into trouble if we try to keep going. The following flag is set if a
233 -- circularity is detected, and used to abandon compilation after the
234 -- messages have been posted.
236 Circularity_Detected : Boolean := False;
237 -- This should really be reset on encountering a new main unit, but in
238 -- practice we are not using multiple main units so it is not critical.
240 -------------------------------------------------
241 -- Formal packages and partial parametrization --
242 -------------------------------------------------
244 -- When compiling a generic, a formal package is a local instantiation. If
245 -- declared with a box, its generic formals are visible in the enclosing
246 -- generic. If declared with a partial list of actuals, those actuals that
247 -- are defaulted (covered by an Others clause, or given an explicit box
248 -- initialization) are also visible in the enclosing generic, while those
249 -- that have a corresponding actual are not.
251 -- In our source model of instantiation, the same visibility must be
252 -- present in the spec and body of an instance: the names of the formals
253 -- that are defaulted must be made visible within the instance, and made
254 -- invisible (hidden) after the instantiation is complete, so that they
255 -- are not accessible outside of the instance.
257 -- In a generic, a formal package is treated like a special instantiation.
258 -- Our Ada95 compiler handled formals with and without box in different
259 -- ways. With partial parametrization, we use a single model for both.
260 -- We create a package declaration that consists of the specification of
261 -- the generic package, and a set of declarations that map the actuals
262 -- into local renamings, just as we do for bona fide instantiations. For
263 -- defaulted parameters and formals with a box, we copy directly the
264 -- declarations of the formal into this local package. The result is a
265 -- a package whose visible declarations may include generic formals. This
266 -- package is only used for type checking and visibility analysis, and
267 -- never reaches the back-end, so it can freely violate the placement
268 -- rules for generic formal declarations.
270 -- The list of declarations (renamings and copies of formals) is built
271 -- by Analyze_Associations, just as for regular instantiations.
273 -- At the point of instantiation, conformance checking must be applied only
274 -- to those parameters that were specified in the formal. We perform this
275 -- checking by creating another internal instantiation, this one including
276 -- only the renamings and the formals (the rest of the package spec is not
277 -- relevant to conformance checking). We can then traverse two lists: the
278 -- list of actuals in the instance that corresponds to the formal package,
279 -- and the list of actuals produced for this bogus instantiation. We apply
280 -- the conformance rules to those actuals that are not defaulted (i.e.
281 -- which still appear as generic formals.
283 -- When we compile an instance body we must make the right parameters
284 -- visible again. The predicate Is_Generic_Formal indicates which of the
285 -- formals should have its Is_Hidden flag reset.
287 -----------------------
288 -- Local subprograms --
289 -----------------------
291 procedure Abandon_Instantiation (N : Node_Id);
292 pragma No_Return (Abandon_Instantiation);
293 -- Posts an error message "instantiation abandoned" at the indicated node
294 -- and then raises the exception Instantiation_Error to do it.
296 procedure Analyze_Formal_Array_Type
297 (T : in out Entity_Id;
298 Def : Node_Id);
299 -- A formal array type is treated like an array type declaration, and
300 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
301 -- in-out, because in the case of an anonymous type the entity is
302 -- actually created in the procedure.
304 -- The following procedures treat other kinds of formal parameters
306 procedure Analyze_Formal_Derived_Interface_Type
307 (N : Node_Id;
308 T : Entity_Id;
309 Def : Node_Id);
311 procedure Analyze_Formal_Derived_Type
312 (N : Node_Id;
313 T : Entity_Id;
314 Def : Node_Id);
316 procedure Analyze_Formal_Interface_Type
317 (N : Node_Id;
318 T : Entity_Id;
319 Def : Node_Id);
321 -- The following subprograms create abbreviated declarations for formal
322 -- scalar types. We introduce an anonymous base of the proper class for
323 -- each of them, and define the formals as constrained first subtypes of
324 -- their bases. The bounds are expressions that are non-static in the
325 -- generic.
327 procedure Analyze_Formal_Decimal_Fixed_Point_Type
328 (T : Entity_Id; Def : Node_Id);
329 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
330 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
331 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
332 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
333 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
334 (T : Entity_Id; Def : Node_Id);
336 procedure Analyze_Formal_Private_Type
337 (N : Node_Id;
338 T : Entity_Id;
339 Def : Node_Id);
340 -- Creates a new private type, which does not require completion
342 procedure Analyze_Generic_Formal_Part (N : Node_Id);
344 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
345 -- Create a new access type with the given designated type
347 function Analyze_Associations
348 (I_Node : Node_Id;
349 Formals : List_Id;
350 F_Copy : List_Id) return List_Id;
351 -- At instantiation time, build the list of associations between formals
352 -- and actuals. Each association becomes a renaming declaration for the
353 -- formal entity. F_Copy is the analyzed list of formals in the generic
354 -- copy. It is used to apply legality checks to the actuals. I_Node is the
355 -- instantiation node itself.
357 procedure Analyze_Subprogram_Instantiation
358 (N : Node_Id;
359 K : Entity_Kind);
361 procedure Build_Instance_Compilation_Unit_Nodes
362 (N : Node_Id;
363 Act_Body : Node_Id;
364 Act_Decl : Node_Id);
365 -- This procedure is used in the case where the generic instance of a
366 -- subprogram body or package body is a library unit. In this case, the
367 -- original library unit node for the generic instantiation must be
368 -- replaced by the resulting generic body, and a link made to a new
369 -- compilation unit node for the generic declaration. The argument N is
370 -- the original generic instantiation. Act_Body and Act_Decl are the body
371 -- and declaration of the instance (either package body and declaration
372 -- nodes or subprogram body and declaration nodes depending on the case).
373 -- On return, the node N has been rewritten with the actual body.
375 procedure Check_Access_Definition (N : Node_Id);
376 -- Subsidiary routine to null exclusion processing. Perform an assertion
377 -- check on Ada version and the presence of an access definition in N.
379 procedure Check_Formal_Packages (P_Id : Entity_Id);
380 -- Apply the following to all formal packages in generic associations
382 procedure Check_Formal_Package_Instance
383 (Formal_Pack : Entity_Id;
384 Actual_Pack : Entity_Id);
385 -- Verify that the actuals of the actual instance match the actuals of
386 -- the template for a formal package that is not declared with a box.
388 procedure Check_Forward_Instantiation (Decl : Node_Id);
389 -- If the generic is a local entity and the corresponding body has not
390 -- been seen yet, flag enclosing packages to indicate that it will be
391 -- elaborated after the generic body. Subprograms declared in the same
392 -- package cannot be inlined by the front-end because front-end inlining
393 -- requires a strict linear order of elaboration.
395 procedure Check_Hidden_Child_Unit
396 (N : Node_Id;
397 Gen_Unit : Entity_Id;
398 Act_Decl_Id : Entity_Id);
399 -- If the generic unit is an implicit child instance within a parent
400 -- instance, we need to make an explicit test that it is not hidden by
401 -- a child instance of the same name and parent.
403 procedure Check_Generic_Actuals
404 (Instance : Entity_Id;
405 Is_Formal_Box : Boolean);
406 -- Similar to previous one. Check the actuals in the instantiation,
407 -- whose views can change between the point of instantiation and the point
408 -- of instantiation of the body. In addition, mark the generic renamings
409 -- as generic actuals, so that they are not compatible with other actuals.
410 -- Recurse on an actual that is a formal package whose declaration has
411 -- a box.
413 function Contains_Instance_Of
414 (Inner : Entity_Id;
415 Outer : Entity_Id;
416 N : Node_Id) return Boolean;
417 -- Inner is instantiated within the generic Outer. Check whether Inner
418 -- directly or indirectly contains an instance of Outer or of one of its
419 -- parents, in the case of a subunit. Each generic unit holds a list of
420 -- the entities instantiated within (at any depth). This procedure
421 -- determines whether the set of such lists contains a cycle, i.e. an
422 -- illegal circular instantiation.
424 function Denotes_Formal_Package
425 (Pack : Entity_Id;
426 On_Exit : Boolean := False) return Boolean;
427 -- Returns True if E is a formal package of an enclosing generic, or
428 -- the actual for such a formal in an enclosing instantiation. If such
429 -- a package is used as a formal in an nested generic, or as an actual
430 -- in a nested instantiation, the visibility of ITS formals should not
431 -- be modified. When called from within Restore_Private_Views, the flag
432 -- On_Exit is true, to indicate that the search for a possible enclosing
433 -- instance should ignore the current one.
435 function Find_Actual_Type
436 (Typ : Entity_Id;
437 Gen_Type : Entity_Id) return Entity_Id;
438 -- When validating the actual types of a child instance, check whether
439 -- the formal is a formal type of the parent unit, and retrieve the current
440 -- actual for it. Typ is the entity in the analyzed formal type declaration
441 -- (component or index type of an array type, or designated type of an
442 -- access formal) and Gen_Type is the enclosing analyzed formal array
443 -- or access type. The desired actual may be a formal of a parent, or may
444 -- be declared in a formal package of a parent. In both cases it is a
445 -- generic actual type because it appears within a visible instance.
446 -- Finally, it may be declared in a parent unit without being a formal
447 -- of that unit, in which case it must be retrieved by visibility.
448 -- Ambiguities may still arise if two homonyms are declared in two formal
449 -- packages, and the prefix of the formal type may be needed to resolve
450 -- the ambiguity in the instance ???
452 function In_Same_Declarative_Part
453 (F_Node : Node_Id;
454 Inst : Node_Id) return Boolean;
455 -- True if the instantiation Inst and the given freeze_node F_Node appear
456 -- within the same declarative part, ignoring subunits, but with no inter-
457 -- vening subprograms or concurrent units. If true, the freeze node
458 -- of the instance can be placed after the freeze node of the parent,
459 -- which it itself an instance.
461 function In_Main_Context (E : Entity_Id) return Boolean;
462 -- Check whether an instantiation is in the context of the main unit.
463 -- Used to determine whether its body should be elaborated to allow
464 -- front-end inlining.
466 function Is_Generic_Formal (E : Entity_Id) return Boolean;
467 -- Utility to determine whether a given entity is declared by means of
468 -- of a formal parameter declaration. Used to set properly the visibility
469 -- of generic formals of a generic package declared with a box or with
470 -- partial parametrization.
472 procedure Set_Instance_Env
473 (Gen_Unit : Entity_Id;
474 Act_Unit : Entity_Id);
475 -- Save current instance on saved environment, to be used to determine
476 -- the global status of entities in nested instances. Part of Save_Env.
477 -- called after verifying that the generic unit is legal for the instance,
478 -- The procedure also examines whether the generic unit is a predefined
479 -- unit, in order to set configuration switches accordingly. As a result
480 -- the procedure must be called after analyzing and freezing the actuals.
482 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
483 -- Associate analyzed generic parameter with corresponding
484 -- instance. Used for semantic checks at instantiation time.
486 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
487 -- Traverse the Exchanged_Views list to see if a type was private
488 -- and has already been flipped during this phase of instantiation.
490 procedure Hide_Current_Scope;
491 -- When instantiating a generic child unit, the parent context must be
492 -- present, but the instance and all entities that may be generated
493 -- must be inserted in the current scope. We leave the current scope
494 -- on the stack, but make its entities invisible to avoid visibility
495 -- problems. This is reversed at the end of the instantiation. This is
496 -- not done for the instantiation of the bodies, which only require the
497 -- instances of the generic parents to be in scope.
499 procedure Install_Body
500 (Act_Body : Node_Id;
501 N : Node_Id;
502 Gen_Body : Node_Id;
503 Gen_Decl : Node_Id);
504 -- If the instantiation happens textually before the body of the generic,
505 -- the instantiation of the body must be analyzed after the generic body,
506 -- and not at the point of instantiation. Such early instantiations can
507 -- happen if the generic and the instance appear in a package declaration
508 -- because the generic body can only appear in the corresponding package
509 -- body. Early instantiations can also appear if generic, instance and
510 -- body are all in the declarative part of a subprogram or entry. Entities
511 -- of packages that are early instantiations are delayed, and their freeze
512 -- node appears after the generic body.
514 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id);
515 -- Insert freeze node at the end of the declarative part that includes the
516 -- instance node N. If N is in the visible part of an enclosing package
517 -- declaration, the freeze node has to be inserted at the end of the
518 -- private declarations, if any.
520 procedure Freeze_Subprogram_Body
521 (Inst_Node : Node_Id;
522 Gen_Body : Node_Id;
523 Pack_Id : Entity_Id);
524 -- The generic body may appear textually after the instance, including
525 -- in the proper body of a stub, or within a different package instance.
526 -- Given that the instance can only be elaborated after the generic, we
527 -- place freeze_nodes for the instance and/or for packages that may enclose
528 -- the instance and the generic, so that the back-end can establish the
529 -- proper order of elaboration.
531 procedure Init_Env;
532 -- Establish environment for subsequent instantiation. Separated from
533 -- Save_Env because data-structures for visibility handling must be
534 -- initialized before call to Check_Generic_Child_Unit.
536 procedure Install_Formal_Packages (Par : Entity_Id);
537 -- If any of the formals of the parent are formal packages with box,
538 -- their formal parts are visible in the parent and thus in the child
539 -- unit as well. Analogous to what is done in Check_Generic_Actuals
540 -- for the unit itself. This procedure is also used in an instance, to
541 -- make visible the proper entities of the actual for a formal package
542 -- declared with a box.
544 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
545 -- When compiling an instance of a child unit the parent (which is
546 -- itself an instance) is an enclosing scope that must be made
547 -- immediately visible. This procedure is also used to install the non-
548 -- generic parent of a generic child unit when compiling its body, so
549 -- that full views of types in the parent are made visible.
551 procedure Remove_Parent (In_Body : Boolean := False);
552 -- Reverse effect after instantiation of child is complete
554 procedure Inline_Instance_Body
555 (N : Node_Id;
556 Gen_Unit : Entity_Id;
557 Act_Decl : Node_Id);
558 -- If front-end inlining is requested, instantiate the package body,
559 -- and preserve the visibility of its compilation unit, to insure
560 -- that successive instantiations succeed.
562 -- The functions Instantiate_XXX perform various legality checks and build
563 -- the declarations for instantiated generic parameters. In all of these
564 -- Formal is the entity in the generic unit, Actual is the entity of
565 -- expression in the generic associations, and Analyzed_Formal is the
566 -- formal in the generic copy, which contains the semantic information to
567 -- be used to validate the actual.
569 function Instantiate_Object
570 (Formal : Node_Id;
571 Actual : Node_Id;
572 Analyzed_Formal : Node_Id) return List_Id;
574 function Instantiate_Type
575 (Formal : Node_Id;
576 Actual : Node_Id;
577 Analyzed_Formal : Node_Id;
578 Actual_Decls : List_Id) return List_Id;
580 function Instantiate_Formal_Subprogram
581 (Formal : Node_Id;
582 Actual : Node_Id;
583 Analyzed_Formal : Node_Id) return Node_Id;
585 function Instantiate_Formal_Package
586 (Formal : Node_Id;
587 Actual : Node_Id;
588 Analyzed_Formal : Node_Id) return List_Id;
589 -- If the formal package is declared with a box, special visibility rules
590 -- apply to its formals: they are in the visible part of the package. This
591 -- is true in the declarative region of the formal package, that is to say
592 -- in the enclosing generic or instantiation. For an instantiation, the
593 -- parameters of the formal package are made visible in an explicit step.
594 -- Furthermore, if the actual has a visible USE clause, these formals must
595 -- be made potentially use-visible as well. On exit from the enclosing
596 -- instantiation, the reverse must be done.
598 -- For a formal package declared without a box, there are conformance rules
599 -- that apply to the actuals in the generic declaration and the actuals of
600 -- the actual package in the enclosing instantiation. The simplest way to
601 -- apply these rules is to repeat the instantiation of the formal package
602 -- in the context of the enclosing instance, and compare the generic
603 -- associations of this instantiation with those of the actual package.
604 -- This internal instantiation only needs to contain the renamings of the
605 -- formals: the visible and private declarations themselves need not be
606 -- created.
608 -- In Ada 2005, the formal package may be only partially parametrized. In
609 -- that case the visibility step must make visible those actuals whose
610 -- corresponding formals were given with a box. A final complication
611 -- involves inherited operations from formal derived types, which must be
612 -- visible if the type is.
614 function Is_In_Main_Unit (N : Node_Id) return Boolean;
615 -- Test if given node is in the main unit
617 procedure Load_Parent_Of_Generic
618 (N : Node_Id;
619 Spec : Node_Id;
620 Body_Optional : Boolean := False);
621 -- If the generic appears in a separate non-generic library unit, load the
622 -- corresponding body to retrieve the body of the generic. N is the node
623 -- for the generic instantiation, Spec is the generic package declaration.
625 -- Body_Optional is a flag that indicates that the body is being loaded to
626 -- ensure that temporaries are generated consistently when there are other
627 -- instances in the current declarative part that precede the one being
628 -- loaded. In that case a missing body is acceptable.
630 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
631 -- Add the context clause of the unit containing a generic unit to an
632 -- instantiation that is a compilation unit.
634 function Get_Associated_Node (N : Node_Id) return Node_Id;
635 -- In order to propagate semantic information back from the analyzed copy
636 -- to the original generic, we maintain links between selected nodes in the
637 -- generic and their corresponding copies. At the end of generic analysis,
638 -- the routine Save_Global_References traverses the generic tree, examines
639 -- the semantic information, and preserves the links to those nodes that
640 -- contain global information. At instantiation, the information from the
641 -- associated node is placed on the new copy, so that name resolution is
642 -- not repeated.
644 -- Three kinds of source nodes have associated nodes:
646 -- a) those that can reference (denote) entities, that is identifiers,
647 -- character literals, expanded_names, operator symbols, operators,
648 -- and attribute reference nodes. These nodes have an Entity field
649 -- and are the set of nodes that are in N_Has_Entity.
651 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
653 -- c) selected components (N_Selected_Component)
655 -- For the first class, the associated node preserves the entity if it is
656 -- global. If the generic contains nested instantiations, the associated
657 -- node itself has been recopied, and a chain of them must be followed.
659 -- For aggregates, the associated node allows retrieval of the type, which
660 -- may otherwise not appear in the generic. The view of this type may be
661 -- different between generic and instantiation, and the full view can be
662 -- installed before the instantiation is analyzed. For aggregates of type
663 -- extensions, the same view exchange may have to be performed for some of
664 -- the ancestor types, if their view is private at the point of
665 -- instantiation.
667 -- Nodes that are selected components in the parse tree may be rewritten
668 -- as expanded names after resolution, and must be treated as potential
669 -- entity holders, which is why they also have an Associated_Node.
671 -- Nodes that do not come from source, such as freeze nodes, do not appear
672 -- in the generic tree, and need not have an associated node.
674 -- The associated node is stored in the Associated_Node field. Note that
675 -- this field overlaps Entity, which is fine, because the whole point is
676 -- that we don't need or want the normal Entity field in this situation.
678 procedure Move_Freeze_Nodes
679 (Out_Of : Entity_Id;
680 After : Node_Id;
681 L : List_Id);
682 -- Freeze nodes can be generated in the analysis of a generic unit, but
683 -- will not be seen by the back-end. It is necessary to move those nodes
684 -- to the enclosing scope if they freeze an outer entity. We place them
685 -- at the end of the enclosing generic package, which is semantically
686 -- neutral.
688 procedure Preanalyze_Actuals (N : Node_Id);
689 -- Analyze actuals to perform name resolution. Full resolution is done
690 -- later, when the expected types are known, but names have to be captured
691 -- before installing parents of generics, that are not visible for the
692 -- actuals themselves.
694 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
695 -- Verify that an attribute that appears as the default for a formal
696 -- subprogram is a function or procedure with the correct profile.
698 -------------------------------------------
699 -- Data Structures for Generic Renamings --
700 -------------------------------------------
702 -- The map Generic_Renamings associates generic entities with their
703 -- corresponding actuals. Currently used to validate type instances. It
704 -- will eventually be used for all generic parameters to eliminate the
705 -- need for overload resolution in the instance.
707 type Assoc_Ptr is new Int;
709 Assoc_Null : constant Assoc_Ptr := -1;
711 type Assoc is record
712 Gen_Id : Entity_Id;
713 Act_Id : Entity_Id;
714 Next_In_HTable : Assoc_Ptr;
715 end record;
717 package Generic_Renamings is new Table.Table
718 (Table_Component_Type => Assoc,
719 Table_Index_Type => Assoc_Ptr,
720 Table_Low_Bound => 0,
721 Table_Initial => 10,
722 Table_Increment => 100,
723 Table_Name => "Generic_Renamings");
725 -- Variable to hold enclosing instantiation. When the environment is
726 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
728 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
730 -- Hash table for associations
732 HTable_Size : constant := 37;
733 type HTable_Range is range 0 .. HTable_Size - 1;
735 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
736 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
737 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
738 function Hash (F : Entity_Id) return HTable_Range;
740 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
741 Header_Num => HTable_Range,
742 Element => Assoc,
743 Elmt_Ptr => Assoc_Ptr,
744 Null_Ptr => Assoc_Null,
745 Set_Next => Set_Next_Assoc,
746 Next => Next_Assoc,
747 Key => Entity_Id,
748 Get_Key => Get_Gen_Id,
749 Hash => Hash,
750 Equal => "=");
752 Exchanged_Views : Elist_Id;
753 -- This list holds the private views that have been exchanged during
754 -- instantiation to restore the visibility of the generic declaration.
755 -- (see comments above). After instantiation, the current visibility is
756 -- reestablished by means of a traversal of this list.
758 Hidden_Entities : Elist_Id;
759 -- This list holds the entities of the current scope that are removed
760 -- from immediate visibility when instantiating a child unit. Their
761 -- visibility is restored in Remove_Parent.
763 -- Because instantiations can be recursive, the following must be saved
764 -- on entry and restored on exit from an instantiation (spec or body).
765 -- This is done by the two procedures Save_Env and Restore_Env. For
766 -- package and subprogram instantiations (but not for the body instances)
767 -- the action of Save_Env is done in two steps: Init_Env is called before
768 -- Check_Generic_Child_Unit, because setting the parent instances requires
769 -- that the visibility data structures be properly initialized. Once the
770 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
772 Parent_Unit_Visible : Boolean := False;
773 -- Parent_Unit_Visible is used when the generic is a child unit, and
774 -- indicates whether the ultimate parent of the generic is visible in the
775 -- instantiation environment. It is used to reset the visibility of the
776 -- parent at the end of the instantiation (see Remove_Parent).
778 Instance_Parent_Unit : Entity_Id := Empty;
779 -- This records the ultimate parent unit of an instance of a generic
780 -- child unit and is used in conjunction with Parent_Unit_Visible to
781 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
783 type Instance_Env is record
784 Instantiated_Parent : Assoc;
785 Exchanged_Views : Elist_Id;
786 Hidden_Entities : Elist_Id;
787 Current_Sem_Unit : Unit_Number_Type;
788 Parent_Unit_Visible : Boolean := False;
789 Instance_Parent_Unit : Entity_Id := Empty;
790 Switches : Config_Switches_Type;
791 end record;
793 package Instance_Envs is new Table.Table (
794 Table_Component_Type => Instance_Env,
795 Table_Index_Type => Int,
796 Table_Low_Bound => 0,
797 Table_Initial => 32,
798 Table_Increment => 100,
799 Table_Name => "Instance_Envs");
801 procedure Restore_Private_Views
802 (Pack_Id : Entity_Id;
803 Is_Package : Boolean := True);
804 -- Restore the private views of external types, and unmark the generic
805 -- renamings of actuals, so that they become compatible subtypes again.
806 -- For subprograms, Pack_Id is the package constructed to hold the
807 -- renamings.
809 procedure Switch_View (T : Entity_Id);
810 -- Switch the partial and full views of a type and its private
811 -- dependents (i.e. its subtypes and derived types).
813 ------------------------------------
814 -- Structures for Error Reporting --
815 ------------------------------------
817 Instantiation_Node : Node_Id;
818 -- Used by subprograms that validate instantiation of formal parameters
819 -- where there might be no actual on which to place the error message.
820 -- Also used to locate the instantiation node for generic subunits.
822 Instantiation_Error : exception;
823 -- When there is a semantic error in the generic parameter matching,
824 -- there is no point in continuing the instantiation, because the
825 -- number of cascaded errors is unpredictable. This exception aborts
826 -- the instantiation process altogether.
828 S_Adjustment : Sloc_Adjustment;
829 -- Offset created for each node in an instantiation, in order to keep
830 -- track of the source position of the instantiation in each of its nodes.
831 -- A subsequent semantic error or warning on a construct of the instance
832 -- points to both places: the original generic node, and the point of
833 -- instantiation. See Sinput and Sinput.L for additional details.
835 ------------------------------------------------------------
836 -- Data structure for keeping track when inside a Generic --
837 ------------------------------------------------------------
839 -- The following table is used to save values of the Inside_A_Generic
840 -- flag (see spec of Sem) when they are saved by Start_Generic.
842 package Generic_Flags is new Table.Table (
843 Table_Component_Type => Boolean,
844 Table_Index_Type => Int,
845 Table_Low_Bound => 0,
846 Table_Initial => 32,
847 Table_Increment => 200,
848 Table_Name => "Generic_Flags");
850 ---------------------------
851 -- Abandon_Instantiation --
852 ---------------------------
854 procedure Abandon_Instantiation (N : Node_Id) is
855 begin
856 Error_Msg_N ("\instantiation abandoned!", N);
857 raise Instantiation_Error;
858 end Abandon_Instantiation;
860 --------------------------
861 -- Analyze_Associations --
862 --------------------------
864 function Analyze_Associations
865 (I_Node : Node_Id;
866 Formals : List_Id;
867 F_Copy : List_Id) return List_Id
869 Actual_Types : constant Elist_Id := New_Elmt_List;
870 Assoc : constant List_Id := New_List;
871 Default_Actuals : constant Elist_Id := New_Elmt_List;
872 Gen_Unit : constant Entity_Id := Defining_Entity (Parent (F_Copy));
873 Actuals : List_Id;
874 Actual : Node_Id;
875 Formal : Node_Id;
876 Next_Formal : Node_Id;
877 Temp_Formal : Node_Id;
878 Analyzed_Formal : Node_Id;
879 Match : Node_Id;
880 Named : Node_Id;
881 First_Named : Node_Id := Empty;
883 Default_Formals : constant List_Id := New_List;
884 -- If an Other_Choice is present, some of the formals may be defaulted.
885 -- To simplify the treatment of visibility in an instance, we introduce
886 -- individual defaults for each such formal. These defaults are
887 -- appended to the list of associations and replace the Others_Choice.
889 Found_Assoc : Node_Id;
890 -- Association for the current formal being match. Empty if there are
891 -- no remaining actuals, or if there is no named association with the
892 -- name of the formal.
894 Is_Named_Assoc : Boolean;
895 Num_Matched : Int := 0;
896 Num_Actuals : Int := 0;
898 Others_Present : Boolean := False;
899 -- In Ada 2005, indicates partial parametrization of a formal
900 -- package. As usual an other association must be last in the list.
902 function Matching_Actual
903 (F : Entity_Id;
904 A_F : Entity_Id) return Node_Id;
905 -- Find actual that corresponds to a given a formal parameter. If the
906 -- actuals are positional, return the next one, if any. If the actuals
907 -- are named, scan the parameter associations to find the right one.
908 -- A_F is the corresponding entity in the analyzed generic,which is
909 -- placed on the selector name for ASIS use.
911 -- In Ada 2005, a named association may be given with a box, in which
912 -- case Matching_Actual sets Found_Assoc to the generic association,
913 -- but return Empty for the actual itself. In this case the code below
914 -- creates a corresponding declaration for the formal.
916 function Partial_Parametrization return Boolean;
917 -- Ada 2005: if no match is found for a given formal, check if the
918 -- association for it includes a box, or whether the associations
919 -- include an Others clause.
921 procedure Process_Default (F : Entity_Id);
922 -- Add a copy of the declaration of generic formal F to the list of
923 -- associations, and add an explicit box association for F if there
924 -- is none yet, and the default comes from an Others_Choice.
926 procedure Set_Analyzed_Formal;
927 -- Find the node in the generic copy that corresponds to a given formal.
928 -- The semantic information on this node is used to perform legality
929 -- checks on the actuals. Because semantic analysis can introduce some
930 -- anonymous entities or modify the declaration node itself, the
931 -- correspondence between the two lists is not one-one. In addition to
932 -- anonymous types, the presence a formal equality will introduce an
933 -- implicit declaration for the corresponding inequality.
935 ---------------------
936 -- Matching_Actual --
937 ---------------------
939 function Matching_Actual
940 (F : Entity_Id;
941 A_F : Entity_Id) return Node_Id
943 Prev : Node_Id;
944 Act : Node_Id;
946 begin
947 Is_Named_Assoc := False;
949 -- End of list of purely positional parameters
951 if No (Actual)
952 or else Nkind (Actual) = N_Others_Choice
953 then
954 Found_Assoc := Empty;
955 Act := Empty;
957 -- Case of positional parameter corresponding to current formal
959 elsif No (Selector_Name (Actual)) then
960 Found_Assoc := Actual;
961 Act := Explicit_Generic_Actual_Parameter (Actual);
962 Num_Matched := Num_Matched + 1;
963 Next (Actual);
965 -- Otherwise scan list of named actuals to find the one with the
966 -- desired name. All remaining actuals have explicit names.
968 else
969 Is_Named_Assoc := True;
970 Found_Assoc := Empty;
971 Act := Empty;
972 Prev := Empty;
974 while Present (Actual) loop
975 if Chars (Selector_Name (Actual)) = Chars (F) then
976 Set_Entity (Selector_Name (Actual), A_F);
977 Set_Etype (Selector_Name (Actual), Etype (A_F));
978 Generate_Reference (A_F, Selector_Name (Actual));
979 Found_Assoc := Actual;
980 Act := Explicit_Generic_Actual_Parameter (Actual);
981 Num_Matched := Num_Matched + 1;
982 exit;
983 end if;
985 Prev := Actual;
986 Next (Actual);
987 end loop;
989 -- Reset for subsequent searches. In most cases the named
990 -- associations are in order. If they are not, we reorder them
991 -- to avoid scanning twice the same actual. This is not just a
992 -- question of efficiency: there may be multiple defaults with
993 -- boxes that have the same name. In a nested instantiation we
994 -- insert actuals for those defaults, and cannot rely on their
995 -- names to disambiguate them.
997 if Actual = First_Named then
998 Next (First_Named);
1000 elsif Present (Actual) then
1001 Insert_Before (First_Named, Remove_Next (Prev));
1002 end if;
1004 Actual := First_Named;
1005 end if;
1007 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1008 Set_Used_As_Generic_Actual (Entity (Act));
1009 end if;
1011 return Act;
1012 end Matching_Actual;
1014 -----------------------------
1015 -- Partial_Parametrization --
1016 -----------------------------
1018 function Partial_Parametrization return Boolean is
1019 begin
1020 return Others_Present
1021 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1022 end Partial_Parametrization;
1024 ---------------------
1025 -- Process_Default --
1026 ---------------------
1028 procedure Process_Default (F : Entity_Id) is
1029 Loc : constant Source_Ptr := Sloc (I_Node);
1030 F_Id : constant Entity_Id := Defining_Entity (F);
1032 Decl : Node_Id;
1033 Default : Node_Id;
1034 Id : Entity_Id;
1036 begin
1037 -- Append copy of formal declaration to associations, and create
1038 -- new defining identifier for it.
1040 Decl := New_Copy_Tree (F);
1041 Id := Make_Defining_Identifier (Sloc (F_Id), Chars => Chars (F_Id));
1043 if Nkind (F) in N_Formal_Subprogram_Declaration then
1044 Set_Defining_Unit_Name (Specification (Decl), Id);
1046 else
1047 Set_Defining_Identifier (Decl, Id);
1048 end if;
1050 Append (Decl, Assoc);
1052 if No (Found_Assoc) then
1053 Default :=
1054 Make_Generic_Association (Loc,
1055 Selector_Name => New_Occurrence_Of (Id, Loc),
1056 Explicit_Generic_Actual_Parameter => Empty);
1057 Set_Box_Present (Default);
1058 Append (Default, Default_Formals);
1059 end if;
1060 end Process_Default;
1062 -------------------------
1063 -- Set_Analyzed_Formal --
1064 -------------------------
1066 procedure Set_Analyzed_Formal is
1067 Kind : Node_Kind;
1069 begin
1070 while Present (Analyzed_Formal) loop
1071 Kind := Nkind (Analyzed_Formal);
1073 case Nkind (Formal) is
1075 when N_Formal_Subprogram_Declaration =>
1076 exit when Kind in N_Formal_Subprogram_Declaration
1077 and then
1078 Chars
1079 (Defining_Unit_Name (Specification (Formal))) =
1080 Chars
1081 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1083 when N_Formal_Package_Declaration =>
1084 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1085 N_Generic_Package_Declaration,
1086 N_Package_Declaration);
1088 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1090 when others =>
1092 -- Skip freeze nodes, and nodes inserted to replace
1093 -- unrecognized pragmas.
1095 exit when
1096 Kind not in N_Formal_Subprogram_Declaration
1097 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1098 N_Freeze_Entity,
1099 N_Null_Statement,
1100 N_Itype_Reference)
1101 and then Chars (Defining_Identifier (Formal)) =
1102 Chars (Defining_Identifier (Analyzed_Formal));
1103 end case;
1105 Next (Analyzed_Formal);
1106 end loop;
1107 end Set_Analyzed_Formal;
1109 -- Start of processing for Analyze_Associations
1111 begin
1112 Actuals := Generic_Associations (I_Node);
1114 if Present (Actuals) then
1116 -- check for an Others choice, indicating a partial parametrization
1117 -- for a formal package.
1119 Actual := First (Actuals);
1120 while Present (Actual) loop
1121 if Nkind (Actual) = N_Others_Choice then
1122 Others_Present := True;
1124 if Present (Next (Actual)) then
1125 Error_Msg_N ("others must be last association", Actual);
1126 end if;
1128 -- This subprogram is used both for formal packages and for
1129 -- instantiations. For the latter, associations must all be
1130 -- explicit.
1132 if Nkind (I_Node) /= N_Formal_Package_Declaration
1133 and then Comes_From_Source (I_Node)
1134 then
1135 Error_Msg_N
1136 ("others association not allowed in an instance",
1137 Actual);
1138 end if;
1140 -- In any case, nothing to do after the others association
1142 exit;
1144 elsif Box_Present (Actual)
1145 and then Comes_From_Source (I_Node)
1146 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1147 then
1148 Error_Msg_N
1149 ("box association not allowed in an instance", Actual);
1150 end if;
1152 Next (Actual);
1153 end loop;
1155 -- If named associations are present, save first named association
1156 -- (it may of course be Empty) to facilitate subsequent name search.
1158 First_Named := First (Actuals);
1159 while Present (First_Named)
1160 and then Nkind (First_Named) /= N_Others_Choice
1161 and then No (Selector_Name (First_Named))
1162 loop
1163 Num_Actuals := Num_Actuals + 1;
1164 Next (First_Named);
1165 end loop;
1166 end if;
1168 Named := First_Named;
1169 while Present (Named) loop
1170 if Nkind (Named) /= N_Others_Choice
1171 and then No (Selector_Name (Named))
1172 then
1173 Error_Msg_N ("invalid positional actual after named one", Named);
1174 Abandon_Instantiation (Named);
1175 end if;
1177 -- A named association may lack an actual parameter, if it was
1178 -- introduced for a default subprogram that turns out to be local
1179 -- to the outer instantiation.
1181 if Nkind (Named) /= N_Others_Choice
1182 and then Present (Explicit_Generic_Actual_Parameter (Named))
1183 then
1184 Num_Actuals := Num_Actuals + 1;
1185 end if;
1187 Next (Named);
1188 end loop;
1190 if Present (Formals) then
1191 Formal := First_Non_Pragma (Formals);
1192 Analyzed_Formal := First_Non_Pragma (F_Copy);
1194 if Present (Actuals) then
1195 Actual := First (Actuals);
1197 -- All formals should have default values
1199 else
1200 Actual := Empty;
1201 end if;
1203 while Present (Formal) loop
1204 Set_Analyzed_Formal;
1205 Next_Formal := Next_Non_Pragma (Formal);
1207 case Nkind (Formal) is
1208 when N_Formal_Object_Declaration =>
1209 Match :=
1210 Matching_Actual (
1211 Defining_Identifier (Formal),
1212 Defining_Identifier (Analyzed_Formal));
1214 if No (Match) and then Partial_Parametrization then
1215 Process_Default (Formal);
1216 else
1217 Append_List
1218 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1219 Assoc);
1220 end if;
1222 when N_Formal_Type_Declaration =>
1223 Match :=
1224 Matching_Actual (
1225 Defining_Identifier (Formal),
1226 Defining_Identifier (Analyzed_Formal));
1228 if No (Match) then
1229 if Partial_Parametrization then
1230 Process_Default (Formal);
1232 else
1233 Error_Msg_Sloc := Sloc (Gen_Unit);
1234 Error_Msg_NE
1235 ("missing actual&",
1236 Instantiation_Node,
1237 Defining_Identifier (Formal));
1238 Error_Msg_NE ("\in instantiation of & declared#",
1239 Instantiation_Node, Gen_Unit);
1240 Abandon_Instantiation (Instantiation_Node);
1241 end if;
1243 else
1244 Analyze (Match);
1245 Append_List
1246 (Instantiate_Type
1247 (Formal, Match, Analyzed_Formal, Assoc),
1248 Assoc);
1250 -- An instantiation is a freeze point for the actuals,
1251 -- unless this is a rewritten formal package.
1253 if Nkind (I_Node) /= N_Formal_Package_Declaration then
1254 Append_Elmt (Entity (Match), Actual_Types);
1255 end if;
1256 end if;
1258 -- A remote access-to-class-wide type must not be an
1259 -- actual parameter for a generic formal of an access
1260 -- type (E.2.2 (17)).
1262 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1263 and then
1264 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1265 N_Access_To_Object_Definition
1266 then
1267 Validate_Remote_Access_To_Class_Wide_Type (Match);
1268 end if;
1270 when N_Formal_Subprogram_Declaration =>
1271 Match :=
1272 Matching_Actual (
1273 Defining_Unit_Name (Specification (Formal)),
1274 Defining_Unit_Name (Specification (Analyzed_Formal)));
1276 -- If the formal subprogram has the same name as
1277 -- another formal subprogram of the generic, then
1278 -- a named association is illegal (12.3(9)). Exclude
1279 -- named associations that are generated for a nested
1280 -- instance.
1282 if Present (Match)
1283 and then Is_Named_Assoc
1284 and then Comes_From_Source (Found_Assoc)
1285 then
1286 Temp_Formal := First (Formals);
1287 while Present (Temp_Formal) loop
1288 if Nkind (Temp_Formal) in
1289 N_Formal_Subprogram_Declaration
1290 and then Temp_Formal /= Formal
1291 and then
1292 Chars (Selector_Name (Found_Assoc)) =
1293 Chars (Defining_Unit_Name
1294 (Specification (Temp_Formal)))
1295 then
1296 Error_Msg_N
1297 ("name not allowed for overloaded formal",
1298 Found_Assoc);
1299 Abandon_Instantiation (Instantiation_Node);
1300 end if;
1302 Next (Temp_Formal);
1303 end loop;
1304 end if;
1306 -- If there is no corresponding actual, this may be case of
1307 -- partial parametrization, or else the formal has a default
1308 -- or a box.
1310 if No (Match)
1311 and then Partial_Parametrization
1312 then
1313 Process_Default (Formal);
1314 else
1315 Append_To (Assoc,
1316 Instantiate_Formal_Subprogram
1317 (Formal, Match, Analyzed_Formal));
1318 end if;
1320 -- If this is a nested generic, preserve default for later
1321 -- instantiations.
1323 if No (Match)
1324 and then Box_Present (Formal)
1325 then
1326 Append_Elmt
1327 (Defining_Unit_Name (Specification (Last (Assoc))),
1328 Default_Actuals);
1329 end if;
1331 when N_Formal_Package_Declaration =>
1332 Match :=
1333 Matching_Actual (
1334 Defining_Identifier (Formal),
1335 Defining_Identifier (Original_Node (Analyzed_Formal)));
1337 if No (Match) then
1338 if Partial_Parametrization then
1339 Process_Default (Formal);
1341 else
1342 Error_Msg_Sloc := Sloc (Gen_Unit);
1343 Error_Msg_NE
1344 ("missing actual&",
1345 Instantiation_Node, Defining_Identifier (Formal));
1346 Error_Msg_NE ("\in instantiation of & declared#",
1347 Instantiation_Node, Gen_Unit);
1349 Abandon_Instantiation (Instantiation_Node);
1350 end if;
1352 else
1353 Analyze (Match);
1354 Append_List
1355 (Instantiate_Formal_Package
1356 (Formal, Match, Analyzed_Formal),
1357 Assoc);
1358 end if;
1360 -- For use type and use package appearing in the generic part,
1361 -- we have already copied them, so we can just move them where
1362 -- they belong (we mustn't recopy them since this would mess up
1363 -- the Sloc values).
1365 when N_Use_Package_Clause |
1366 N_Use_Type_Clause =>
1367 if Nkind (Original_Node (I_Node)) =
1368 N_Formal_Package_Declaration
1369 then
1370 Append (New_Copy_Tree (Formal), Assoc);
1371 else
1372 Remove (Formal);
1373 Append (Formal, Assoc);
1374 end if;
1376 when others =>
1377 raise Program_Error;
1379 end case;
1381 Formal := Next_Formal;
1382 Next_Non_Pragma (Analyzed_Formal);
1383 end loop;
1385 if Num_Actuals > Num_Matched then
1386 Error_Msg_Sloc := Sloc (Gen_Unit);
1388 if Present (Selector_Name (Actual)) then
1389 Error_Msg_NE
1390 ("unmatched actual&",
1391 Actual, Selector_Name (Actual));
1392 Error_Msg_NE ("\in instantiation of& declared#",
1393 Actual, Gen_Unit);
1394 else
1395 Error_Msg_NE
1396 ("unmatched actual in instantiation of& declared#",
1397 Actual, Gen_Unit);
1398 end if;
1399 end if;
1401 elsif Present (Actuals) then
1402 Error_Msg_N
1403 ("too many actuals in generic instantiation", Instantiation_Node);
1404 end if;
1406 declare
1407 Elmt : Elmt_Id := First_Elmt (Actual_Types);
1409 begin
1410 while Present (Elmt) loop
1411 Freeze_Before (I_Node, Node (Elmt));
1412 Next_Elmt (Elmt);
1413 end loop;
1414 end;
1416 -- If there are default subprograms, normalize the tree by adding
1417 -- explicit associations for them. This is required if the instance
1418 -- appears within a generic.
1420 declare
1421 Elmt : Elmt_Id;
1422 Subp : Entity_Id;
1423 New_D : Node_Id;
1425 begin
1426 Elmt := First_Elmt (Default_Actuals);
1427 while Present (Elmt) loop
1428 if No (Actuals) then
1429 Actuals := New_List;
1430 Set_Generic_Associations (I_Node, Actuals);
1431 end if;
1433 Subp := Node (Elmt);
1434 New_D :=
1435 Make_Generic_Association (Sloc (Subp),
1436 Selector_Name => New_Occurrence_Of (Subp, Sloc (Subp)),
1437 Explicit_Generic_Actual_Parameter =>
1438 New_Occurrence_Of (Subp, Sloc (Subp)));
1439 Mark_Rewrite_Insertion (New_D);
1440 Append_To (Actuals, New_D);
1441 Next_Elmt (Elmt);
1442 end loop;
1443 end;
1445 -- If this is a formal package, normalize the parameter list by adding
1446 -- explicit box associations for the formals that are covered by an
1447 -- Others_Choice.
1449 if not Is_Empty_List (Default_Formals) then
1450 Append_List (Default_Formals, Formals);
1451 end if;
1453 return Assoc;
1454 end Analyze_Associations;
1456 -------------------------------
1457 -- Analyze_Formal_Array_Type --
1458 -------------------------------
1460 procedure Analyze_Formal_Array_Type
1461 (T : in out Entity_Id;
1462 Def : Node_Id)
1464 DSS : Node_Id;
1466 begin
1467 -- Treated like a non-generic array declaration, with additional
1468 -- semantic checks.
1470 Enter_Name (T);
1472 if Nkind (Def) = N_Constrained_Array_Definition then
1473 DSS := First (Discrete_Subtype_Definitions (Def));
1474 while Present (DSS) loop
1475 if Nkind_In (DSS, N_Subtype_Indication,
1476 N_Range,
1477 N_Attribute_Reference)
1478 then
1479 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1480 end if;
1482 Next (DSS);
1483 end loop;
1484 end if;
1486 Array_Type_Declaration (T, Def);
1487 Set_Is_Generic_Type (Base_Type (T));
1489 if Ekind (Component_Type (T)) = E_Incomplete_Type
1490 and then No (Full_View (Component_Type (T)))
1491 then
1492 Error_Msg_N ("premature usage of incomplete type", Def);
1494 -- Check that range constraint is not allowed on the component type
1495 -- of a generic formal array type (AARM 12.5.3(3))
1497 elsif Is_Internal (Component_Type (T))
1498 and then Present (Subtype_Indication (Component_Definition (Def)))
1499 and then Nkind (Original_Node
1500 (Subtype_Indication (Component_Definition (Def)))) =
1501 N_Subtype_Indication
1502 then
1503 Error_Msg_N
1504 ("in a formal, a subtype indication can only be "
1505 & "a subtype mark (RM 12.5.3(3))",
1506 Subtype_Indication (Component_Definition (Def)));
1507 end if;
1509 end Analyze_Formal_Array_Type;
1511 ---------------------------------------------
1512 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1513 ---------------------------------------------
1515 -- As for other generic types, we create a valid type representation with
1516 -- legal but arbitrary attributes, whose values are never considered
1517 -- static. For all scalar types we introduce an anonymous base type, with
1518 -- the same attributes. We choose the corresponding integer type to be
1519 -- Standard_Integer.
1521 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1522 (T : Entity_Id;
1523 Def : Node_Id)
1525 Loc : constant Source_Ptr := Sloc (Def);
1526 Base : constant Entity_Id :=
1527 New_Internal_Entity
1528 (E_Decimal_Fixed_Point_Type,
1529 Current_Scope, Sloc (Def), 'G');
1530 Int_Base : constant Entity_Id := Standard_Integer;
1531 Delta_Val : constant Ureal := Ureal_1;
1532 Digs_Val : constant Uint := Uint_6;
1534 begin
1535 Enter_Name (T);
1537 Set_Etype (Base, Base);
1538 Set_Size_Info (Base, Int_Base);
1539 Set_RM_Size (Base, RM_Size (Int_Base));
1540 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1541 Set_Digits_Value (Base, Digs_Val);
1542 Set_Delta_Value (Base, Delta_Val);
1543 Set_Small_Value (Base, Delta_Val);
1544 Set_Scalar_Range (Base,
1545 Make_Range (Loc,
1546 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1547 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1549 Set_Is_Generic_Type (Base);
1550 Set_Parent (Base, Parent (Def));
1552 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1553 Set_Etype (T, Base);
1554 Set_Size_Info (T, Int_Base);
1555 Set_RM_Size (T, RM_Size (Int_Base));
1556 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1557 Set_Digits_Value (T, Digs_Val);
1558 Set_Delta_Value (T, Delta_Val);
1559 Set_Small_Value (T, Delta_Val);
1560 Set_Scalar_Range (T, Scalar_Range (Base));
1561 Set_Is_Constrained (T);
1563 Check_Restriction (No_Fixed_Point, Def);
1564 end Analyze_Formal_Decimal_Fixed_Point_Type;
1566 -------------------------------------------
1567 -- Analyze_Formal_Derived_Interface_Type --
1568 -------------------------------------------
1570 procedure Analyze_Formal_Derived_Interface_Type
1571 (N : Node_Id;
1572 T : Entity_Id;
1573 Def : Node_Id)
1575 Loc : constant Source_Ptr := Sloc (Def);
1577 begin
1578 -- Rewrite as a type declaration of a derived type. This ensures that
1579 -- the interface list and primitive operations are properly captured.
1581 Rewrite (N,
1582 Make_Full_Type_Declaration (Loc,
1583 Defining_Identifier => T,
1584 Type_Definition => Def));
1585 Analyze (N);
1586 Set_Is_Generic_Type (T);
1587 end Analyze_Formal_Derived_Interface_Type;
1589 ---------------------------------
1590 -- Analyze_Formal_Derived_Type --
1591 ---------------------------------
1593 procedure Analyze_Formal_Derived_Type
1594 (N : Node_Id;
1595 T : Entity_Id;
1596 Def : Node_Id)
1598 Loc : constant Source_Ptr := Sloc (Def);
1599 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
1600 New_N : Node_Id;
1602 begin
1603 Set_Is_Generic_Type (T);
1605 if Private_Present (Def) then
1606 New_N :=
1607 Make_Private_Extension_Declaration (Loc,
1608 Defining_Identifier => T,
1609 Discriminant_Specifications => Discriminant_Specifications (N),
1610 Unknown_Discriminants_Present => Unk_Disc,
1611 Subtype_Indication => Subtype_Mark (Def),
1612 Interface_List => Interface_List (Def));
1614 Set_Abstract_Present (New_N, Abstract_Present (Def));
1615 Set_Limited_Present (New_N, Limited_Present (Def));
1616 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
1618 else
1619 New_N :=
1620 Make_Full_Type_Declaration (Loc,
1621 Defining_Identifier => T,
1622 Discriminant_Specifications =>
1623 Discriminant_Specifications (Parent (T)),
1624 Type_Definition =>
1625 Make_Derived_Type_Definition (Loc,
1626 Subtype_Indication => Subtype_Mark (Def)));
1628 Set_Abstract_Present
1629 (Type_Definition (New_N), Abstract_Present (Def));
1630 Set_Limited_Present
1631 (Type_Definition (New_N), Limited_Present (Def));
1632 end if;
1634 Rewrite (N, New_N);
1635 Analyze (N);
1637 if Unk_Disc then
1638 if not Is_Composite_Type (T) then
1639 Error_Msg_N
1640 ("unknown discriminants not allowed for elementary types", N);
1641 else
1642 Set_Has_Unknown_Discriminants (T);
1643 Set_Is_Constrained (T, False);
1644 end if;
1645 end if;
1647 -- If the parent type has a known size, so does the formal, which makes
1648 -- legal representation clauses that involve the formal.
1650 Set_Size_Known_At_Compile_Time
1651 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
1652 end Analyze_Formal_Derived_Type;
1654 ----------------------------------
1655 -- Analyze_Formal_Discrete_Type --
1656 ----------------------------------
1658 -- The operations defined for a discrete types are those of an enumeration
1659 -- type. The size is set to an arbitrary value, for use in analyzing the
1660 -- generic unit.
1662 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
1663 Loc : constant Source_Ptr := Sloc (Def);
1664 Lo : Node_Id;
1665 Hi : Node_Id;
1667 Base : constant Entity_Id :=
1668 New_Internal_Entity
1669 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1670 begin
1671 Enter_Name (T);
1672 Set_Ekind (T, E_Enumeration_Subtype);
1673 Set_Etype (T, Base);
1674 Init_Size (T, 8);
1675 Init_Alignment (T);
1676 Set_Is_Generic_Type (T);
1677 Set_Is_Constrained (T);
1679 -- For semantic analysis, the bounds of the type must be set to some
1680 -- non-static value. The simplest is to create attribute nodes for those
1681 -- bounds, that refer to the type itself. These bounds are never
1682 -- analyzed but serve as place-holders.
1684 Lo :=
1685 Make_Attribute_Reference (Loc,
1686 Attribute_Name => Name_First,
1687 Prefix => New_Reference_To (T, Loc));
1688 Set_Etype (Lo, T);
1690 Hi :=
1691 Make_Attribute_Reference (Loc,
1692 Attribute_Name => Name_Last,
1693 Prefix => New_Reference_To (T, Loc));
1694 Set_Etype (Hi, T);
1696 Set_Scalar_Range (T,
1697 Make_Range (Loc,
1698 Low_Bound => Lo,
1699 High_Bound => Hi));
1701 Set_Ekind (Base, E_Enumeration_Type);
1702 Set_Etype (Base, Base);
1703 Init_Size (Base, 8);
1704 Init_Alignment (Base);
1705 Set_Is_Generic_Type (Base);
1706 Set_Scalar_Range (Base, Scalar_Range (T));
1707 Set_Parent (Base, Parent (Def));
1708 end Analyze_Formal_Discrete_Type;
1710 ----------------------------------
1711 -- Analyze_Formal_Floating_Type --
1712 ---------------------------------
1714 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
1715 Base : constant Entity_Id :=
1716 New_Internal_Entity
1717 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1719 begin
1720 -- The various semantic attributes are taken from the predefined type
1721 -- Float, just so that all of them are initialized. Their values are
1722 -- never used because no constant folding or expansion takes place in
1723 -- the generic itself.
1725 Enter_Name (T);
1726 Set_Ekind (T, E_Floating_Point_Subtype);
1727 Set_Etype (T, Base);
1728 Set_Size_Info (T, (Standard_Float));
1729 Set_RM_Size (T, RM_Size (Standard_Float));
1730 Set_Digits_Value (T, Digits_Value (Standard_Float));
1731 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
1732 Set_Is_Constrained (T);
1734 Set_Is_Generic_Type (Base);
1735 Set_Etype (Base, Base);
1736 Set_Size_Info (Base, (Standard_Float));
1737 Set_RM_Size (Base, RM_Size (Standard_Float));
1738 Set_Digits_Value (Base, Digits_Value (Standard_Float));
1739 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
1740 Set_Parent (Base, Parent (Def));
1742 Check_Restriction (No_Floating_Point, Def);
1743 end Analyze_Formal_Floating_Type;
1745 -----------------------------------
1746 -- Analyze_Formal_Interface_Type;--
1747 -----------------------------------
1749 procedure Analyze_Formal_Interface_Type
1750 (N : Node_Id;
1751 T : Entity_Id;
1752 Def : Node_Id)
1754 Loc : constant Source_Ptr := Sloc (N);
1755 New_N : Node_Id;
1757 begin
1758 New_N :=
1759 Make_Full_Type_Declaration (Loc,
1760 Defining_Identifier => T,
1761 Type_Definition => Def);
1763 Rewrite (N, New_N);
1764 Analyze (N);
1765 Set_Is_Generic_Type (T);
1766 end Analyze_Formal_Interface_Type;
1768 ---------------------------------
1769 -- Analyze_Formal_Modular_Type --
1770 ---------------------------------
1772 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
1773 begin
1774 -- Apart from their entity kind, generic modular types are treated like
1775 -- signed integer types, and have the same attributes.
1777 Analyze_Formal_Signed_Integer_Type (T, Def);
1778 Set_Ekind (T, E_Modular_Integer_Subtype);
1779 Set_Ekind (Etype (T), E_Modular_Integer_Type);
1781 end Analyze_Formal_Modular_Type;
1783 ---------------------------------------
1784 -- Analyze_Formal_Object_Declaration --
1785 ---------------------------------------
1787 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
1788 E : constant Node_Id := Default_Expression (N);
1789 Id : constant Node_Id := Defining_Identifier (N);
1790 K : Entity_Kind;
1791 T : Node_Id;
1793 begin
1794 Enter_Name (Id);
1796 -- Determine the mode of the formal object
1798 if Out_Present (N) then
1799 K := E_Generic_In_Out_Parameter;
1801 if not In_Present (N) then
1802 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
1803 end if;
1805 else
1806 K := E_Generic_In_Parameter;
1807 end if;
1809 if Present (Subtype_Mark (N)) then
1810 Find_Type (Subtype_Mark (N));
1811 T := Entity (Subtype_Mark (N));
1813 -- Verify that there is no redundant null exclusion.
1815 if Null_Exclusion_Present (N) then
1816 if not Is_Access_Type (T) then
1817 Error_Msg_N
1818 ("null exclusion can only apply to an access type", N);
1820 elsif Can_Never_Be_Null (T) then
1821 Error_Msg_NE
1822 ("`NOT NULL` not allowed (& already excludes null)",
1823 N, T);
1824 end if;
1825 end if;
1827 -- Ada 2005 (AI-423): Formal object with an access definition
1829 else
1830 Check_Access_Definition (N);
1831 T := Access_Definition
1832 (Related_Nod => N,
1833 N => Access_Definition (N));
1834 end if;
1836 if Ekind (T) = E_Incomplete_Type then
1837 declare
1838 Error_Node : Node_Id;
1840 begin
1841 if Present (Subtype_Mark (N)) then
1842 Error_Node := Subtype_Mark (N);
1843 else
1844 Check_Access_Definition (N);
1845 Error_Node := Access_Definition (N);
1846 end if;
1848 Error_Msg_N ("premature usage of incomplete type", Error_Node);
1849 end;
1850 end if;
1852 if K = E_Generic_In_Parameter then
1854 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
1856 if Ada_Version < Ada_05 and then Is_Limited_Type (T) then
1857 Error_Msg_N
1858 ("generic formal of mode IN must not be of limited type", N);
1859 Explain_Limited_Type (T, N);
1860 end if;
1862 if Is_Abstract_Type (T) then
1863 Error_Msg_N
1864 ("generic formal of mode IN must not be of abstract type", N);
1865 end if;
1867 if Present (E) then
1868 Preanalyze_Spec_Expression (E, T);
1870 if Is_Limited_Type (T) and then not OK_For_Limited_Init (E) then
1871 Error_Msg_N
1872 ("initialization not allowed for limited types", E);
1873 Explain_Limited_Type (T, E);
1874 end if;
1875 end if;
1877 Set_Ekind (Id, K);
1878 Set_Etype (Id, T);
1880 -- Case of generic IN OUT parameter
1882 else
1883 -- If the formal has an unconstrained type, construct its actual
1884 -- subtype, as is done for subprogram formals. In this fashion, all
1885 -- its uses can refer to specific bounds.
1887 Set_Ekind (Id, K);
1888 Set_Etype (Id, T);
1890 if (Is_Array_Type (T)
1891 and then not Is_Constrained (T))
1892 or else
1893 (Ekind (T) = E_Record_Type
1894 and then Has_Discriminants (T))
1895 then
1896 declare
1897 Non_Freezing_Ref : constant Node_Id :=
1898 New_Reference_To (Id, Sloc (Id));
1899 Decl : Node_Id;
1901 begin
1902 -- Make sure the actual subtype doesn't generate bogus freezing
1904 Set_Must_Not_Freeze (Non_Freezing_Ref);
1905 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
1906 Insert_Before_And_Analyze (N, Decl);
1907 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
1908 end;
1909 else
1910 Set_Actual_Subtype (Id, T);
1911 end if;
1913 if Present (E) then
1914 Error_Msg_N
1915 ("initialization not allowed for `IN OUT` formals", N);
1916 end if;
1917 end if;
1919 end Analyze_Formal_Object_Declaration;
1921 ----------------------------------------------
1922 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
1923 ----------------------------------------------
1925 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
1926 (T : Entity_Id;
1927 Def : Node_Id)
1929 Loc : constant Source_Ptr := Sloc (Def);
1930 Base : constant Entity_Id :=
1931 New_Internal_Entity
1932 (E_Ordinary_Fixed_Point_Type, Current_Scope, Sloc (Def), 'G');
1933 begin
1934 -- The semantic attributes are set for completeness only, their values
1935 -- will never be used, since all properties of the type are non-static.
1937 Enter_Name (T);
1938 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
1939 Set_Etype (T, Base);
1940 Set_Size_Info (T, Standard_Integer);
1941 Set_RM_Size (T, RM_Size (Standard_Integer));
1942 Set_Small_Value (T, Ureal_1);
1943 Set_Delta_Value (T, Ureal_1);
1944 Set_Scalar_Range (T,
1945 Make_Range (Loc,
1946 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1947 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1948 Set_Is_Constrained (T);
1950 Set_Is_Generic_Type (Base);
1951 Set_Etype (Base, Base);
1952 Set_Size_Info (Base, Standard_Integer);
1953 Set_RM_Size (Base, RM_Size (Standard_Integer));
1954 Set_Small_Value (Base, Ureal_1);
1955 Set_Delta_Value (Base, Ureal_1);
1956 Set_Scalar_Range (Base, Scalar_Range (T));
1957 Set_Parent (Base, Parent (Def));
1959 Check_Restriction (No_Fixed_Point, Def);
1960 end Analyze_Formal_Ordinary_Fixed_Point_Type;
1962 ----------------------------
1963 -- Analyze_Formal_Package --
1964 ----------------------------
1966 procedure Analyze_Formal_Package (N : Node_Id) is
1967 Loc : constant Source_Ptr := Sloc (N);
1968 Pack_Id : constant Entity_Id := Defining_Identifier (N);
1969 Formal : Entity_Id;
1970 Gen_Id : constant Node_Id := Name (N);
1971 Gen_Decl : Node_Id;
1972 Gen_Unit : Entity_Id;
1973 New_N : Node_Id;
1974 Parent_Installed : Boolean := False;
1975 Renaming : Node_Id;
1976 Parent_Instance : Entity_Id;
1977 Renaming_In_Par : Entity_Id;
1978 No_Associations : Boolean := False;
1980 function Build_Local_Package return Node_Id;
1981 -- The formal package is rewritten so that its parameters are replaced
1982 -- with corresponding declarations. For parameters with bona fide
1983 -- associations these declarations are created by Analyze_Associations
1984 -- as for a regular instantiation. For boxed parameters, we preserve
1985 -- the formal declarations and analyze them, in order to introduce
1986 -- entities of the right kind in the environment of the formal.
1988 -------------------------
1989 -- Build_Local_Package --
1990 -------------------------
1992 function Build_Local_Package return Node_Id is
1993 Decls : List_Id;
1994 Pack_Decl : Node_Id;
1996 begin
1997 -- Within the formal, the name of the generic package is a renaming
1998 -- of the formal (as for a regular instantiation).
2000 Pack_Decl :=
2001 Make_Package_Declaration (Loc,
2002 Specification =>
2003 Copy_Generic_Node
2004 (Specification (Original_Node (Gen_Decl)),
2005 Empty, Instantiating => True));
2007 Renaming := Make_Package_Renaming_Declaration (Loc,
2008 Defining_Unit_Name =>
2009 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2010 Name => New_Occurrence_Of (Formal, Loc));
2012 if Nkind (Gen_Id) = N_Identifier
2013 and then Chars (Gen_Id) = Chars (Pack_Id)
2014 then
2015 Error_Msg_NE
2016 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2017 end if;
2019 -- If the formal is declared with a box, or with an others choice,
2020 -- create corresponding declarations for all entities in the formal
2021 -- part, so that names with the proper types are available in the
2022 -- specification of the formal package.
2023 -- On the other hand, if there are no associations, then all the
2024 -- formals must have defaults, and this will be checked by the
2025 -- call to Analyze_Associations.
2027 if Box_Present (N)
2028 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2029 then
2030 declare
2031 Formal_Decl : Node_Id;
2033 begin
2034 -- TBA : for a formal package, need to recurse ???
2036 Decls := New_List;
2037 Formal_Decl :=
2038 First
2039 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2040 while Present (Formal_Decl) loop
2041 Append_To
2042 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2043 Next (Formal_Decl);
2044 end loop;
2045 end;
2047 -- If generic associations are present, use Analyze_Associations to
2048 -- create the proper renaming declarations.
2050 else
2051 declare
2052 Act_Tree : constant Node_Id :=
2053 Copy_Generic_Node
2054 (Original_Node (Gen_Decl), Empty,
2055 Instantiating => True);
2057 begin
2058 Generic_Renamings.Set_Last (0);
2059 Generic_Renamings_HTable.Reset;
2060 Instantiation_Node := N;
2062 Decls :=
2063 Analyze_Associations
2064 (Original_Node (N),
2065 Generic_Formal_Declarations (Act_Tree),
2066 Generic_Formal_Declarations (Gen_Decl));
2067 end;
2068 end if;
2070 Append (Renaming, To => Decls);
2072 -- Add generated declarations ahead of local declarations in
2073 -- the package.
2075 if No (Visible_Declarations (Specification (Pack_Decl))) then
2076 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2077 else
2078 Insert_List_Before
2079 (First (Visible_Declarations (Specification (Pack_Decl))),
2080 Decls);
2081 end if;
2083 return Pack_Decl;
2084 end Build_Local_Package;
2086 -- Start of processing for Analyze_Formal_Package
2088 begin
2089 Text_IO_Kludge (Gen_Id);
2091 Init_Env;
2092 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2093 Gen_Unit := Entity (Gen_Id);
2095 -- Check for a formal package that is a package renaming
2097 if Present (Renamed_Object (Gen_Unit)) then
2098 Gen_Unit := Renamed_Object (Gen_Unit);
2099 end if;
2101 if Ekind (Gen_Unit) /= E_Generic_Package then
2102 Error_Msg_N ("expect generic package name", Gen_Id);
2103 Restore_Env;
2104 return;
2106 elsif Gen_Unit = Current_Scope then
2107 Error_Msg_N
2108 ("generic package cannot be used as a formal package of itself",
2109 Gen_Id);
2110 Restore_Env;
2111 return;
2113 elsif In_Open_Scopes (Gen_Unit) then
2114 if Is_Compilation_Unit (Gen_Unit)
2115 and then Is_Child_Unit (Current_Scope)
2116 then
2117 -- Special-case the error when the formal is a parent, and
2118 -- continue analysis to minimize cascaded errors.
2120 Error_Msg_N
2121 ("generic parent cannot be used as formal package "
2122 & "of a child unit",
2123 Gen_Id);
2125 else
2126 Error_Msg_N
2127 ("generic package cannot be used as a formal package "
2128 & "within itself",
2129 Gen_Id);
2130 Restore_Env;
2131 return;
2132 end if;
2133 end if;
2135 if Box_Present (N)
2136 or else No (Generic_Associations (N))
2137 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2138 then
2139 No_Associations := True;
2140 end if;
2142 -- If there are no generic associations, the generic parameters appear
2143 -- as local entities and are instantiated like them. We copy the generic
2144 -- package declaration as if it were an instantiation, and analyze it
2145 -- like a regular package, except that we treat the formals as
2146 -- additional visible components.
2148 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2150 if In_Extended_Main_Source_Unit (N) then
2151 Set_Is_Instantiated (Gen_Unit);
2152 Generate_Reference (Gen_Unit, N);
2153 end if;
2155 Formal := New_Copy (Pack_Id);
2156 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2158 begin
2159 -- Make local generic without formals. The formals will be replaced
2160 -- with internal declarations.
2162 New_N := Build_Local_Package;
2164 -- If there are errors in the parameter list, Analyze_Associations
2165 -- raises Instantiation_Error. Patch the declaration to prevent
2166 -- further exception propagation.
2168 exception
2169 when Instantiation_Error =>
2171 Enter_Name (Formal);
2172 Set_Ekind (Formal, E_Variable);
2173 Set_Etype (Formal, Any_Type);
2175 if Parent_Installed then
2176 Remove_Parent;
2177 end if;
2179 return;
2180 end;
2182 Rewrite (N, New_N);
2183 Set_Defining_Unit_Name (Specification (New_N), Formal);
2184 Set_Generic_Parent (Specification (N), Gen_Unit);
2185 Set_Instance_Env (Gen_Unit, Formal);
2186 Set_Is_Generic_Instance (Formal);
2188 Enter_Name (Formal);
2189 Set_Ekind (Formal, E_Package);
2190 Set_Etype (Formal, Standard_Void_Type);
2191 Set_Inner_Instances (Formal, New_Elmt_List);
2192 Push_Scope (Formal);
2194 if Is_Child_Unit (Gen_Unit)
2195 and then Parent_Installed
2196 then
2197 -- Similarly, we have to make the name of the formal visible in the
2198 -- parent instance, to resolve properly fully qualified names that
2199 -- may appear in the generic unit. The parent instance has been
2200 -- placed on the scope stack ahead of the current scope.
2202 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2204 Renaming_In_Par :=
2205 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2206 Set_Ekind (Renaming_In_Par, E_Package);
2207 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2208 Set_Scope (Renaming_In_Par, Parent_Instance);
2209 Set_Parent (Renaming_In_Par, Parent (Formal));
2210 Set_Renamed_Object (Renaming_In_Par, Formal);
2211 Append_Entity (Renaming_In_Par, Parent_Instance);
2212 end if;
2214 Analyze (Specification (N));
2216 -- The formals for which associations are provided are not visible
2217 -- outside of the formal package. The others are still declared by a
2218 -- formal parameter declaration.
2220 if not No_Associations then
2221 declare
2222 E : Entity_Id;
2224 begin
2225 E := First_Entity (Formal);
2226 while Present (E) loop
2227 exit when Ekind (E) = E_Package
2228 and then Renamed_Entity (E) = Formal;
2230 if not Is_Generic_Formal (E) then
2231 Set_Is_Hidden (E);
2232 end if;
2234 Next_Entity (E);
2235 end loop;
2236 end;
2237 end if;
2239 End_Package_Scope (Formal);
2241 if Parent_Installed then
2242 Remove_Parent;
2243 end if;
2245 Restore_Env;
2247 -- Inside the generic unit, the formal package is a regular package, but
2248 -- no body is needed for it. Note that after instantiation, the defining
2249 -- unit name we need is in the new tree and not in the original (see
2250 -- Package_Instantiation). A generic formal package is an instance, and
2251 -- can be used as an actual for an inner instance.
2253 Set_Has_Completion (Formal, True);
2255 -- Add semantic information to the original defining identifier.
2256 -- for ASIS use.
2258 Set_Ekind (Pack_Id, E_Package);
2259 Set_Etype (Pack_Id, Standard_Void_Type);
2260 Set_Scope (Pack_Id, Scope (Formal));
2261 Set_Has_Completion (Pack_Id, True);
2262 end Analyze_Formal_Package;
2264 ---------------------------------
2265 -- Analyze_Formal_Private_Type --
2266 ---------------------------------
2268 procedure Analyze_Formal_Private_Type
2269 (N : Node_Id;
2270 T : Entity_Id;
2271 Def : Node_Id)
2273 begin
2274 New_Private_Type (N, T, Def);
2276 -- Set the size to an arbitrary but legal value
2278 Set_Size_Info (T, Standard_Integer);
2279 Set_RM_Size (T, RM_Size (Standard_Integer));
2280 end Analyze_Formal_Private_Type;
2282 ----------------------------------------
2283 -- Analyze_Formal_Signed_Integer_Type --
2284 ----------------------------------------
2286 procedure Analyze_Formal_Signed_Integer_Type
2287 (T : Entity_Id;
2288 Def : Node_Id)
2290 Base : constant Entity_Id :=
2291 New_Internal_Entity
2292 (E_Signed_Integer_Type, Current_Scope, Sloc (Def), 'G');
2294 begin
2295 Enter_Name (T);
2297 Set_Ekind (T, E_Signed_Integer_Subtype);
2298 Set_Etype (T, Base);
2299 Set_Size_Info (T, Standard_Integer);
2300 Set_RM_Size (T, RM_Size (Standard_Integer));
2301 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2302 Set_Is_Constrained (T);
2304 Set_Is_Generic_Type (Base);
2305 Set_Size_Info (Base, Standard_Integer);
2306 Set_RM_Size (Base, RM_Size (Standard_Integer));
2307 Set_Etype (Base, Base);
2308 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2309 Set_Parent (Base, Parent (Def));
2310 end Analyze_Formal_Signed_Integer_Type;
2312 -------------------------------
2313 -- Analyze_Formal_Subprogram --
2314 -------------------------------
2316 procedure Analyze_Formal_Subprogram (N : Node_Id) is
2317 Spec : constant Node_Id := Specification (N);
2318 Def : constant Node_Id := Default_Name (N);
2319 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2320 Subp : Entity_Id;
2322 begin
2323 if Nam = Error then
2324 return;
2325 end if;
2327 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2328 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2329 return;
2330 end if;
2332 Analyze_Subprogram_Declaration (N);
2333 Set_Is_Formal_Subprogram (Nam);
2334 Set_Has_Completion (Nam);
2336 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2337 Set_Is_Abstract_Subprogram (Nam);
2338 Set_Is_Dispatching_Operation (Nam);
2340 declare
2341 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2342 begin
2343 if No (Ctrl_Type) then
2344 Error_Msg_N
2345 ("abstract formal subprogram must have a controlling type",
2347 else
2348 Check_Controlling_Formals (Ctrl_Type, Nam);
2349 end if;
2350 end;
2351 end if;
2353 -- Default name is resolved at the point of instantiation
2355 if Box_Present (N) then
2356 null;
2358 -- Else default is bound at the point of generic declaration
2360 elsif Present (Def) then
2361 if Nkind (Def) = N_Operator_Symbol then
2362 Find_Direct_Name (Def);
2364 elsif Nkind (Def) /= N_Attribute_Reference then
2365 Analyze (Def);
2367 else
2368 -- For an attribute reference, analyze the prefix and verify
2369 -- that it has the proper profile for the subprogram.
2371 Analyze (Prefix (Def));
2372 Valid_Default_Attribute (Nam, Def);
2373 return;
2374 end if;
2376 -- Default name may be overloaded, in which case the interpretation
2377 -- with the correct profile must be selected, as for a renaming.
2378 -- If the definition is an indexed component, it must denote a
2379 -- member of an entry family. If it is a selected component, it
2380 -- can be a protected operation.
2382 if Etype (Def) = Any_Type then
2383 return;
2385 elsif Nkind (Def) = N_Selected_Component then
2386 if not Is_Overloadable (Entity (Selector_Name (Def))) then
2387 Error_Msg_N ("expect valid subprogram name as default", Def);
2388 end if;
2390 elsif Nkind (Def) = N_Indexed_Component then
2391 if Is_Entity_Name (Prefix (Def)) then
2392 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
2393 Error_Msg_N ("expect valid subprogram name as default", Def);
2394 end if;
2396 elsif Nkind (Prefix (Def)) = N_Selected_Component then
2397 if Ekind (Entity (Selector_Name (Prefix (Def))))
2398 /= E_Entry_Family
2399 then
2400 Error_Msg_N ("expect valid subprogram name as default", Def);
2401 end if;
2403 else
2404 Error_Msg_N ("expect valid subprogram name as default", Def);
2405 return;
2406 end if;
2408 elsif Nkind (Def) = N_Character_Literal then
2410 -- Needs some type checks: subprogram should be parameterless???
2412 Resolve (Def, (Etype (Nam)));
2414 elsif not Is_Entity_Name (Def)
2415 or else not Is_Overloadable (Entity (Def))
2416 then
2417 Error_Msg_N ("expect valid subprogram name as default", Def);
2418 return;
2420 elsif not Is_Overloaded (Def) then
2421 Subp := Entity (Def);
2423 if Subp = Nam then
2424 Error_Msg_N ("premature usage of formal subprogram", Def);
2426 elsif not Entity_Matches_Spec (Subp, Nam) then
2427 Error_Msg_N ("no visible entity matches specification", Def);
2428 end if;
2430 -- More than one interpretation, so disambiguate as for a renaming
2432 else
2433 declare
2434 I : Interp_Index;
2435 I1 : Interp_Index := 0;
2436 It : Interp;
2437 It1 : Interp;
2439 begin
2440 Subp := Any_Id;
2441 Get_First_Interp (Def, I, It);
2442 while Present (It.Nam) loop
2443 if Entity_Matches_Spec (It.Nam, Nam) then
2444 if Subp /= Any_Id then
2445 It1 := Disambiguate (Def, I1, I, Etype (Subp));
2447 if It1 = No_Interp then
2448 Error_Msg_N ("ambiguous default subprogram", Def);
2449 else
2450 Subp := It1.Nam;
2451 end if;
2453 exit;
2455 else
2456 I1 := I;
2457 Subp := It.Nam;
2458 end if;
2459 end if;
2461 Get_Next_Interp (I, It);
2462 end loop;
2463 end;
2465 if Subp /= Any_Id then
2466 Set_Entity (Def, Subp);
2468 if Subp = Nam then
2469 Error_Msg_N ("premature usage of formal subprogram", Def);
2471 elsif Ekind (Subp) /= E_Operator then
2472 Check_Mode_Conformant (Subp, Nam);
2473 end if;
2475 else
2476 Error_Msg_N ("no visible subprogram matches specification", N);
2477 end if;
2478 end if;
2479 end if;
2480 end Analyze_Formal_Subprogram;
2482 -------------------------------------
2483 -- Analyze_Formal_Type_Declaration --
2484 -------------------------------------
2486 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
2487 Def : constant Node_Id := Formal_Type_Definition (N);
2488 T : Entity_Id;
2490 begin
2491 T := Defining_Identifier (N);
2493 if Present (Discriminant_Specifications (N))
2494 and then Nkind (Def) /= N_Formal_Private_Type_Definition
2495 then
2496 Error_Msg_N
2497 ("discriminants not allowed for this formal type", T);
2498 end if;
2500 -- Enter the new name, and branch to specific routine
2502 case Nkind (Def) is
2503 when N_Formal_Private_Type_Definition =>
2504 Analyze_Formal_Private_Type (N, T, Def);
2506 when N_Formal_Derived_Type_Definition =>
2507 Analyze_Formal_Derived_Type (N, T, Def);
2509 when N_Formal_Discrete_Type_Definition =>
2510 Analyze_Formal_Discrete_Type (T, Def);
2512 when N_Formal_Signed_Integer_Type_Definition =>
2513 Analyze_Formal_Signed_Integer_Type (T, Def);
2515 when N_Formal_Modular_Type_Definition =>
2516 Analyze_Formal_Modular_Type (T, Def);
2518 when N_Formal_Floating_Point_Definition =>
2519 Analyze_Formal_Floating_Type (T, Def);
2521 when N_Formal_Ordinary_Fixed_Point_Definition =>
2522 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
2524 when N_Formal_Decimal_Fixed_Point_Definition =>
2525 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
2527 when N_Array_Type_Definition =>
2528 Analyze_Formal_Array_Type (T, Def);
2530 when N_Access_To_Object_Definition |
2531 N_Access_Function_Definition |
2532 N_Access_Procedure_Definition =>
2533 Analyze_Generic_Access_Type (T, Def);
2535 -- Ada 2005: a interface declaration is encoded as an abstract
2536 -- record declaration or a abstract type derivation.
2538 when N_Record_Definition =>
2539 Analyze_Formal_Interface_Type (N, T, Def);
2541 when N_Derived_Type_Definition =>
2542 Analyze_Formal_Derived_Interface_Type (N, T, Def);
2544 when N_Error =>
2545 null;
2547 when others =>
2548 raise Program_Error;
2550 end case;
2552 Set_Is_Generic_Type (T);
2553 end Analyze_Formal_Type_Declaration;
2555 ------------------------------------
2556 -- Analyze_Function_Instantiation --
2557 ------------------------------------
2559 procedure Analyze_Function_Instantiation (N : Node_Id) is
2560 begin
2561 Analyze_Subprogram_Instantiation (N, E_Function);
2562 end Analyze_Function_Instantiation;
2564 ---------------------------------
2565 -- Analyze_Generic_Access_Type --
2566 ---------------------------------
2568 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
2569 begin
2570 Enter_Name (T);
2572 if Nkind (Def) = N_Access_To_Object_Definition then
2573 Access_Type_Declaration (T, Def);
2575 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
2576 and then No (Full_View (Designated_Type (T)))
2577 and then not Is_Generic_Type (Designated_Type (T))
2578 then
2579 Error_Msg_N ("premature usage of incomplete type", Def);
2581 elsif Is_Internal (Designated_Type (T)) then
2582 Error_Msg_N
2583 ("only a subtype mark is allowed in a formal", Def);
2584 end if;
2586 else
2587 Access_Subprogram_Declaration (T, Def);
2588 end if;
2589 end Analyze_Generic_Access_Type;
2591 ---------------------------------
2592 -- Analyze_Generic_Formal_Part --
2593 ---------------------------------
2595 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
2596 Gen_Parm_Decl : Node_Id;
2598 begin
2599 -- The generic formals are processed in the scope of the generic unit,
2600 -- where they are immediately visible. The scope is installed by the
2601 -- caller.
2603 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
2605 while Present (Gen_Parm_Decl) loop
2606 Analyze (Gen_Parm_Decl);
2607 Next (Gen_Parm_Decl);
2608 end loop;
2610 Generate_Reference_To_Generic_Formals (Current_Scope);
2611 end Analyze_Generic_Formal_Part;
2613 ------------------------------------------
2614 -- Analyze_Generic_Package_Declaration --
2615 ------------------------------------------
2617 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
2618 Loc : constant Source_Ptr := Sloc (N);
2619 Id : Entity_Id;
2620 New_N : Node_Id;
2621 Save_Parent : Node_Id;
2622 Renaming : Node_Id;
2623 Decls : constant List_Id :=
2624 Visible_Declarations (Specification (N));
2625 Decl : Node_Id;
2627 begin
2628 -- We introduce a renaming of the enclosing package, to have a usable
2629 -- entity as the prefix of an expanded name for a local entity of the
2630 -- form Par.P.Q, where P is the generic package. This is because a local
2631 -- entity named P may hide it, so that the usual visibility rules in
2632 -- the instance will not resolve properly.
2634 Renaming :=
2635 Make_Package_Renaming_Declaration (Loc,
2636 Defining_Unit_Name =>
2637 Make_Defining_Identifier (Loc,
2638 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
2639 Name => Make_Identifier (Loc, Chars (Defining_Entity (N))));
2641 if Present (Decls) then
2642 Decl := First (Decls);
2643 while Present (Decl)
2644 and then Nkind (Decl) = N_Pragma
2645 loop
2646 Next (Decl);
2647 end loop;
2649 if Present (Decl) then
2650 Insert_Before (Decl, Renaming);
2651 else
2652 Append (Renaming, Visible_Declarations (Specification (N)));
2653 end if;
2655 else
2656 Set_Visible_Declarations (Specification (N), New_List (Renaming));
2657 end if;
2659 -- Create copy of generic unit, and save for instantiation. If the unit
2660 -- is a child unit, do not copy the specifications for the parent, which
2661 -- are not part of the generic tree.
2663 Save_Parent := Parent_Spec (N);
2664 Set_Parent_Spec (N, Empty);
2666 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2667 Set_Parent_Spec (New_N, Save_Parent);
2668 Rewrite (N, New_N);
2669 Id := Defining_Entity (N);
2670 Generate_Definition (Id);
2672 -- Expansion is not applied to generic units
2674 Start_Generic;
2676 Enter_Name (Id);
2677 Set_Ekind (Id, E_Generic_Package);
2678 Set_Etype (Id, Standard_Void_Type);
2679 Push_Scope (Id);
2680 Enter_Generic_Scope (Id);
2681 Set_Inner_Instances (Id, New_Elmt_List);
2683 Set_Categorization_From_Pragmas (N);
2684 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2686 -- Link the declaration of the generic homonym in the generic copy to
2687 -- the package it renames, so that it is always resolved properly.
2689 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
2690 Set_Entity (Associated_Node (Name (Renaming)), Id);
2692 -- For a library unit, we have reconstructed the entity for the unit,
2693 -- and must reset it in the library tables.
2695 if Nkind (Parent (N)) = N_Compilation_Unit then
2696 Set_Cunit_Entity (Current_Sem_Unit, Id);
2697 end if;
2699 Analyze_Generic_Formal_Part (N);
2701 -- After processing the generic formals, analysis proceeds as for a
2702 -- non-generic package.
2704 Analyze (Specification (N));
2706 Validate_Categorization_Dependency (N, Id);
2708 End_Generic;
2710 End_Package_Scope (Id);
2711 Exit_Generic_Scope (Id);
2713 if Nkind (Parent (N)) /= N_Compilation_Unit then
2714 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
2715 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
2716 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
2718 else
2719 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2720 Validate_RT_RAT_Component (N);
2722 -- If this is a spec without a body, check that generic parameters
2723 -- are referenced.
2725 if not Body_Required (Parent (N)) then
2726 Check_References (Id);
2727 end if;
2728 end if;
2729 end Analyze_Generic_Package_Declaration;
2731 --------------------------------------------
2732 -- Analyze_Generic_Subprogram_Declaration --
2733 --------------------------------------------
2735 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
2736 Spec : Node_Id;
2737 Id : Entity_Id;
2738 Formals : List_Id;
2739 New_N : Node_Id;
2740 Result_Type : Entity_Id;
2741 Save_Parent : Node_Id;
2743 begin
2744 -- Create copy of generic unit, and save for instantiation. If the unit
2745 -- is a child unit, do not copy the specifications for the parent, which
2746 -- are not part of the generic tree.
2748 Save_Parent := Parent_Spec (N);
2749 Set_Parent_Spec (N, Empty);
2751 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2752 Set_Parent_Spec (New_N, Save_Parent);
2753 Rewrite (N, New_N);
2755 Spec := Specification (N);
2756 Id := Defining_Entity (Spec);
2757 Generate_Definition (Id);
2759 if Nkind (Id) = N_Defining_Operator_Symbol then
2760 Error_Msg_N
2761 ("operator symbol not allowed for generic subprogram", Id);
2762 end if;
2764 Start_Generic;
2766 Enter_Name (Id);
2768 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
2769 Push_Scope (Id);
2770 Enter_Generic_Scope (Id);
2771 Set_Inner_Instances (Id, New_Elmt_List);
2772 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2774 Analyze_Generic_Formal_Part (N);
2776 Formals := Parameter_Specifications (Spec);
2778 if Present (Formals) then
2779 Process_Formals (Formals, Spec);
2780 end if;
2782 if Nkind (Spec) = N_Function_Specification then
2783 Set_Ekind (Id, E_Generic_Function);
2785 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
2786 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
2787 Set_Etype (Id, Result_Type);
2788 else
2789 Find_Type (Result_Definition (Spec));
2790 Set_Etype (Id, Entity (Result_Definition (Spec)));
2791 end if;
2793 else
2794 Set_Ekind (Id, E_Generic_Procedure);
2795 Set_Etype (Id, Standard_Void_Type);
2796 end if;
2798 -- For a library unit, we have reconstructed the entity for the unit,
2799 -- and must reset it in the library tables. We also make sure that
2800 -- Body_Required is set properly in the original compilation unit node.
2802 if Nkind (Parent (N)) = N_Compilation_Unit then
2803 Set_Cunit_Entity (Current_Sem_Unit, Id);
2804 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2805 end if;
2807 Set_Categorization_From_Pragmas (N);
2808 Validate_Categorization_Dependency (N, Id);
2810 Save_Global_References (Original_Node (N));
2812 End_Generic;
2813 End_Scope;
2814 Exit_Generic_Scope (Id);
2815 Generate_Reference_To_Formals (Id);
2816 end Analyze_Generic_Subprogram_Declaration;
2818 -----------------------------------
2819 -- Analyze_Package_Instantiation --
2820 -----------------------------------
2822 procedure Analyze_Package_Instantiation (N : Node_Id) is
2823 Loc : constant Source_Ptr := Sloc (N);
2824 Gen_Id : constant Node_Id := Name (N);
2826 Act_Decl : Node_Id;
2827 Act_Decl_Name : Node_Id;
2828 Act_Decl_Id : Entity_Id;
2829 Act_Spec : Node_Id;
2830 Act_Tree : Node_Id;
2832 Gen_Decl : Node_Id;
2833 Gen_Unit : Entity_Id;
2835 Is_Actual_Pack : constant Boolean :=
2836 Is_Internal (Defining_Entity (N));
2838 Env_Installed : Boolean := False;
2839 Parent_Installed : Boolean := False;
2840 Renaming_List : List_Id;
2841 Unit_Renaming : Node_Id;
2842 Needs_Body : Boolean;
2843 Inline_Now : Boolean := False;
2845 procedure Delay_Descriptors (E : Entity_Id);
2846 -- Delay generation of subprogram descriptors for given entity
2848 function Might_Inline_Subp return Boolean;
2849 -- If inlining is active and the generic contains inlined subprograms,
2850 -- we instantiate the body. This may cause superfluous instantiations,
2851 -- but it is simpler than detecting the need for the body at the point
2852 -- of inlining, when the context of the instance is not available.
2854 -----------------------
2855 -- Delay_Descriptors --
2856 -----------------------
2858 procedure Delay_Descriptors (E : Entity_Id) is
2859 begin
2860 if not Delay_Subprogram_Descriptors (E) then
2861 Set_Delay_Subprogram_Descriptors (E);
2862 Pending_Descriptor.Append (E);
2863 end if;
2864 end Delay_Descriptors;
2866 -----------------------
2867 -- Might_Inline_Subp --
2868 -----------------------
2870 function Might_Inline_Subp return Boolean is
2871 E : Entity_Id;
2873 begin
2874 if not Inline_Processing_Required then
2875 return False;
2877 else
2878 E := First_Entity (Gen_Unit);
2879 while Present (E) loop
2880 if Is_Subprogram (E)
2881 and then Is_Inlined (E)
2882 then
2883 return True;
2884 end if;
2886 Next_Entity (E);
2887 end loop;
2888 end if;
2890 return False;
2891 end Might_Inline_Subp;
2893 -- Start of processing for Analyze_Package_Instantiation
2895 begin
2896 -- Very first thing: apply the special kludge for Text_IO processing
2897 -- in case we are instantiating one of the children of [Wide_]Text_IO.
2899 Text_IO_Kludge (Name (N));
2901 -- Make node global for error reporting
2903 Instantiation_Node := N;
2905 -- Case of instantiation of a generic package
2907 if Nkind (N) = N_Package_Instantiation then
2908 Act_Decl_Id := New_Copy (Defining_Entity (N));
2909 Set_Comes_From_Source (Act_Decl_Id, True);
2911 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
2912 Act_Decl_Name :=
2913 Make_Defining_Program_Unit_Name (Loc,
2914 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
2915 Defining_Identifier => Act_Decl_Id);
2916 else
2917 Act_Decl_Name := Act_Decl_Id;
2918 end if;
2920 -- Case of instantiation of a formal package
2922 else
2923 Act_Decl_Id := Defining_Identifier (N);
2924 Act_Decl_Name := Act_Decl_Id;
2925 end if;
2927 Generate_Definition (Act_Decl_Id);
2928 Preanalyze_Actuals (N);
2930 Init_Env;
2931 Env_Installed := True;
2932 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2933 Gen_Unit := Entity (Gen_Id);
2935 -- Verify that it is the name of a generic package
2937 if Etype (Gen_Unit) = Any_Type then
2938 Restore_Env;
2939 return;
2941 elsif Ekind (Gen_Unit) /= E_Generic_Package then
2943 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
2945 if From_With_Type (Gen_Unit) then
2946 Error_Msg_N
2947 ("cannot instantiate a limited withed package", Gen_Id);
2948 else
2949 Error_Msg_N
2950 ("expect name of generic package in instantiation", Gen_Id);
2951 end if;
2953 Restore_Env;
2954 return;
2955 end if;
2957 if In_Extended_Main_Source_Unit (N) then
2958 Set_Is_Instantiated (Gen_Unit);
2959 Generate_Reference (Gen_Unit, N);
2961 if Present (Renamed_Object (Gen_Unit)) then
2962 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
2963 Generate_Reference (Renamed_Object (Gen_Unit), N);
2964 end if;
2965 end if;
2967 if Nkind (Gen_Id) = N_Identifier
2968 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
2969 then
2970 Error_Msg_NE
2971 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2973 elsif Nkind (Gen_Id) = N_Expanded_Name
2974 and then Is_Child_Unit (Gen_Unit)
2975 and then Nkind (Prefix (Gen_Id)) = N_Identifier
2976 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
2977 then
2978 Error_Msg_N
2979 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
2980 end if;
2982 Set_Entity (Gen_Id, Gen_Unit);
2984 -- If generic is a renaming, get original generic unit
2986 if Present (Renamed_Object (Gen_Unit))
2987 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
2988 then
2989 Gen_Unit := Renamed_Object (Gen_Unit);
2990 end if;
2992 -- Verify that there are no circular instantiations
2994 if In_Open_Scopes (Gen_Unit) then
2995 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
2996 Restore_Env;
2997 return;
2999 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3000 Error_Msg_Node_2 := Current_Scope;
3001 Error_Msg_NE
3002 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3003 Circularity_Detected := True;
3004 Restore_Env;
3005 return;
3007 else
3008 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3010 -- Initialize renamings map, for error checking, and the list that
3011 -- holds private entities whose views have changed between generic
3012 -- definition and instantiation. If this is the instance created to
3013 -- validate an actual package, the instantiation environment is that
3014 -- of the enclosing instance.
3016 Generic_Renamings.Set_Last (0);
3017 Generic_Renamings_HTable.Reset;
3019 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3021 -- Copy original generic tree, to produce text for instantiation
3023 Act_Tree :=
3024 Copy_Generic_Node
3025 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3027 Act_Spec := Specification (Act_Tree);
3029 -- If this is the instance created to validate an actual package,
3030 -- only the formals matter, do not examine the package spec itself.
3032 if Is_Actual_Pack then
3033 Set_Visible_Declarations (Act_Spec, New_List);
3034 Set_Private_Declarations (Act_Spec, New_List);
3035 end if;
3037 Renaming_List :=
3038 Analyze_Associations
3040 Generic_Formal_Declarations (Act_Tree),
3041 Generic_Formal_Declarations (Gen_Decl));
3043 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3044 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3045 Set_Is_Generic_Instance (Act_Decl_Id);
3047 Set_Generic_Parent (Act_Spec, Gen_Unit);
3049 -- References to the generic in its own declaration or its body are
3050 -- references to the instance. Add a renaming declaration for the
3051 -- generic unit itself. This declaration, as well as the renaming
3052 -- declarations for the generic formals, must remain private to the
3053 -- unit: the formals, because this is the language semantics, and
3054 -- the unit because its use is an artifact of the implementation.
3056 Unit_Renaming :=
3057 Make_Package_Renaming_Declaration (Loc,
3058 Defining_Unit_Name =>
3059 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3060 Name => New_Reference_To (Act_Decl_Id, Loc));
3062 Append (Unit_Renaming, Renaming_List);
3064 -- The renaming declarations are the first local declarations of
3065 -- the new unit.
3067 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3068 Insert_List_Before
3069 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3070 else
3071 Set_Visible_Declarations (Act_Spec, Renaming_List);
3072 end if;
3074 Act_Decl :=
3075 Make_Package_Declaration (Loc,
3076 Specification => Act_Spec);
3078 -- Save the instantiation node, for subsequent instantiation of the
3079 -- body, if there is one and we are generating code for the current
3080 -- unit. Mark the unit as having a body, to avoid a premature error
3081 -- message.
3083 -- We instantiate the body if we are generating code, if we are
3084 -- generating cross-reference information, or if we are building
3085 -- trees for ASIS use.
3087 declare
3088 Enclosing_Body_Present : Boolean := False;
3089 -- If the generic unit is not a compilation unit, then a body may
3090 -- be present in its parent even if none is required. We create a
3091 -- tentative pending instantiation for the body, which will be
3092 -- discarded if none is actually present.
3094 Scop : Entity_Id;
3096 begin
3097 if Scope (Gen_Unit) /= Standard_Standard
3098 and then not Is_Child_Unit (Gen_Unit)
3099 then
3100 Scop := Scope (Gen_Unit);
3102 while Present (Scop)
3103 and then Scop /= Standard_Standard
3104 loop
3105 if Unit_Requires_Body (Scop) then
3106 Enclosing_Body_Present := True;
3107 exit;
3109 elsif In_Open_Scopes (Scop)
3110 and then In_Package_Body (Scop)
3111 then
3112 Enclosing_Body_Present := True;
3113 exit;
3114 end if;
3116 exit when Is_Compilation_Unit (Scop);
3117 Scop := Scope (Scop);
3118 end loop;
3119 end if;
3121 -- If front-end inlining is enabled, and this is a unit for which
3122 -- code will be generated, we instantiate the body at once.
3124 -- This is done if the instance is not the main unit, and if the
3125 -- generic is not a child unit of another generic, to avoid scope
3126 -- problems and the reinstallation of parent instances.
3128 if Expander_Active
3129 and then (not Is_Child_Unit (Gen_Unit)
3130 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3131 and then Might_Inline_Subp
3132 and then not Is_Actual_Pack
3133 then
3134 if Front_End_Inlining
3135 and then (Is_In_Main_Unit (N)
3136 or else In_Main_Context (Current_Scope))
3137 and then Nkind (Parent (N)) /= N_Compilation_Unit
3138 then
3139 Inline_Now := True;
3141 -- In configurable_run_time mode we force the inlining of
3142 -- predefined subprograms marked Inline_Always, to minimize
3143 -- the use of the run-time library.
3145 elsif Is_Predefined_File_Name
3146 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3147 and then Configurable_Run_Time_Mode
3148 and then Nkind (Parent (N)) /= N_Compilation_Unit
3149 then
3150 Inline_Now := True;
3151 end if;
3153 -- If the current scope is itself an instance within a child
3154 -- unit, there will be duplications in the scope stack, and the
3155 -- unstacking mechanism in Inline_Instance_Body will fail.
3156 -- This loses some rare cases of optimization, and might be
3157 -- improved some day, if we can find a proper abstraction for
3158 -- "the complete compilation context" that can be saved and
3159 -- restored. ???
3161 if Is_Generic_Instance (Current_Scope) then
3162 declare
3163 Curr_Unit : constant Entity_Id :=
3164 Cunit_Entity (Current_Sem_Unit);
3165 begin
3166 if Curr_Unit /= Current_Scope
3167 and then Is_Child_Unit (Curr_Unit)
3168 then
3169 Inline_Now := False;
3170 end if;
3171 end;
3172 end if;
3173 end if;
3175 Needs_Body :=
3176 (Unit_Requires_Body (Gen_Unit)
3177 or else Enclosing_Body_Present
3178 or else Present (Corresponding_Body (Gen_Decl)))
3179 and then (Is_In_Main_Unit (N)
3180 or else Might_Inline_Subp)
3181 and then not Is_Actual_Pack
3182 and then not Inline_Now
3183 and then (Operating_Mode = Generate_Code
3184 or else (Operating_Mode = Check_Semantics
3185 and then ASIS_Mode));
3187 -- If front_end_inlining is enabled, do not instantiate body if
3188 -- within a generic context.
3190 if (Front_End_Inlining
3191 and then not Expander_Active)
3192 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
3193 then
3194 Needs_Body := False;
3195 end if;
3197 -- If the current context is generic, and the package being
3198 -- instantiated is declared within a formal package, there is no
3199 -- body to instantiate until the enclosing generic is instantiated
3200 -- and there is an actual for the formal package. If the formal
3201 -- package has parameters, we build a regular package instance for
3202 -- it, that precedes the original formal package declaration.
3204 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
3205 declare
3206 Decl : constant Node_Id :=
3207 Original_Node
3208 (Unit_Declaration_Node (Scope (Gen_Unit)));
3209 begin
3210 if Nkind (Decl) = N_Formal_Package_Declaration
3211 or else (Nkind (Decl) = N_Package_Declaration
3212 and then Is_List_Member (Decl)
3213 and then Present (Next (Decl))
3214 and then
3215 Nkind (Next (Decl)) =
3216 N_Formal_Package_Declaration)
3217 then
3218 Needs_Body := False;
3219 end if;
3220 end;
3221 end if;
3222 end;
3224 -- If we are generating the calling stubs from the instantiation of
3225 -- a generic RCI package, we will not use the body of the generic
3226 -- package.
3228 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
3229 and then Is_Compilation_Unit (Defining_Entity (N))
3230 then
3231 Needs_Body := False;
3232 end if;
3234 if Needs_Body then
3236 -- Here is a defence against a ludicrous number of instantiations
3237 -- caused by a circular set of instantiation attempts.
3239 if Pending_Instantiations.Last >
3240 Hostparm.Max_Instantiations
3241 then
3242 Error_Msg_N ("too many instantiations", N);
3243 raise Unrecoverable_Error;
3244 end if;
3246 -- Indicate that the enclosing scopes contain an instantiation,
3247 -- and that cleanup actions should be delayed until after the
3248 -- instance body is expanded.
3250 Check_Forward_Instantiation (Gen_Decl);
3251 if Nkind (N) = N_Package_Instantiation then
3252 declare
3253 Enclosing_Master : Entity_Id;
3255 begin
3256 -- Loop to search enclosing masters
3258 Enclosing_Master := Current_Scope;
3259 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
3260 if Ekind (Enclosing_Master) = E_Package then
3261 if Is_Compilation_Unit (Enclosing_Master) then
3262 if In_Package_Body (Enclosing_Master) then
3263 Delay_Descriptors
3264 (Body_Entity (Enclosing_Master));
3265 else
3266 Delay_Descriptors
3267 (Enclosing_Master);
3268 end if;
3270 exit Scope_Loop;
3272 else
3273 Enclosing_Master := Scope (Enclosing_Master);
3274 end if;
3276 elsif Ekind (Enclosing_Master) = E_Generic_Package then
3277 Enclosing_Master := Scope (Enclosing_Master);
3279 elsif Is_Generic_Subprogram (Enclosing_Master)
3280 or else Ekind (Enclosing_Master) = E_Void
3281 then
3282 -- Cleanup actions will eventually be performed on the
3283 -- enclosing instance, if any. Enclosing scope is void
3284 -- in the formal part of a generic subprogram.
3286 exit Scope_Loop;
3288 else
3289 if Ekind (Enclosing_Master) = E_Entry
3290 and then
3291 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
3292 then
3293 if not Expander_Active then
3294 exit Scope_Loop;
3295 else
3296 Enclosing_Master :=
3297 Protected_Body_Subprogram (Enclosing_Master);
3298 end if;
3299 end if;
3301 Set_Delay_Cleanups (Enclosing_Master);
3303 while Ekind (Enclosing_Master) = E_Block loop
3304 Enclosing_Master := Scope (Enclosing_Master);
3305 end loop;
3307 if Is_Subprogram (Enclosing_Master) then
3308 Delay_Descriptors (Enclosing_Master);
3310 elsif Is_Task_Type (Enclosing_Master) then
3311 declare
3312 TBP : constant Node_Id :=
3313 Get_Task_Body_Procedure
3314 (Enclosing_Master);
3315 begin
3316 if Present (TBP) then
3317 Delay_Descriptors (TBP);
3318 Set_Delay_Cleanups (TBP);
3319 end if;
3320 end;
3321 end if;
3323 exit Scope_Loop;
3324 end if;
3325 end loop Scope_Loop;
3326 end;
3328 -- Make entry in table
3330 Pending_Instantiations.Append
3331 ((Inst_Node => N,
3332 Act_Decl => Act_Decl,
3333 Expander_Status => Expander_Active,
3334 Current_Sem_Unit => Current_Sem_Unit,
3335 Scope_Suppress => Scope_Suppress,
3336 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top));
3337 end if;
3338 end if;
3340 Set_Categorization_From_Pragmas (Act_Decl);
3342 if Parent_Installed then
3343 Hide_Current_Scope;
3344 end if;
3346 Set_Instance_Spec (N, Act_Decl);
3348 -- If not a compilation unit, insert the package declaration before
3349 -- the original instantiation node.
3351 if Nkind (Parent (N)) /= N_Compilation_Unit then
3352 Mark_Rewrite_Insertion (Act_Decl);
3353 Insert_Before (N, Act_Decl);
3354 Analyze (Act_Decl);
3356 -- For an instantiation that is a compilation unit, place declaration
3357 -- on current node so context is complete for analysis (including
3358 -- nested instantiations). If this is the main unit, the declaration
3359 -- eventually replaces the instantiation node. If the instance body
3360 -- is created later, it replaces the instance node, and the
3361 -- declaration is attached to it (see
3362 -- Build_Instance_Compilation_Unit_Nodes).
3364 else
3365 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
3367 -- The entity for the current unit is the newly created one,
3368 -- and all semantic information is attached to it.
3370 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
3372 -- If this is the main unit, replace the main entity as well
3374 if Current_Sem_Unit = Main_Unit then
3375 Main_Unit_Entity := Act_Decl_Id;
3376 end if;
3377 end if;
3379 Set_Unit (Parent (N), Act_Decl);
3380 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
3381 Set_Package_Instantiation (Act_Decl_Id, N);
3382 Analyze (Act_Decl);
3383 Set_Unit (Parent (N), N);
3384 Set_Body_Required (Parent (N), False);
3386 -- We never need elaboration checks on instantiations, since by
3387 -- definition, the body instantiation is elaborated at the same
3388 -- time as the spec instantiation.
3390 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
3391 Set_Kill_Elaboration_Checks (Act_Decl_Id);
3392 end if;
3394 Check_Elab_Instantiation (N);
3396 if ABE_Is_Certain (N) and then Needs_Body then
3397 Pending_Instantiations.Decrement_Last;
3398 end if;
3400 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
3402 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
3403 First_Private_Entity (Act_Decl_Id));
3405 -- If the instantiation will receive a body, the unit will be
3406 -- transformed into a package body, and receive its own elaboration
3407 -- entity. Otherwise, the nature of the unit is now a package
3408 -- declaration.
3410 if Nkind (Parent (N)) = N_Compilation_Unit
3411 and then not Needs_Body
3412 then
3413 Rewrite (N, Act_Decl);
3414 end if;
3416 if Present (Corresponding_Body (Gen_Decl))
3417 or else Unit_Requires_Body (Gen_Unit)
3418 then
3419 Set_Has_Completion (Act_Decl_Id);
3420 end if;
3422 Check_Formal_Packages (Act_Decl_Id);
3424 Restore_Private_Views (Act_Decl_Id);
3426 Inherit_Context (Gen_Decl, N);
3428 if Parent_Installed then
3429 Remove_Parent;
3430 end if;
3432 Restore_Env;
3433 Env_Installed := False;
3434 end if;
3436 Validate_Categorization_Dependency (N, Act_Decl_Id);
3438 -- Check restriction, but skip this if something went wrong in the above
3439 -- analysis, indicated by Act_Decl_Id being void.
3441 if Ekind (Act_Decl_Id) /= E_Void
3442 and then not Is_Library_Level_Entity (Act_Decl_Id)
3443 then
3444 Check_Restriction (No_Local_Allocators, N);
3445 end if;
3447 if Inline_Now then
3448 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
3449 end if;
3451 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
3452 -- be used as defining identifiers for a formal package and for the
3453 -- corresponding expanded package.
3455 if Nkind (N) = N_Formal_Package_Declaration then
3456 Act_Decl_Id := New_Copy (Defining_Entity (N));
3457 Set_Comes_From_Source (Act_Decl_Id, True);
3458 Set_Is_Generic_Instance (Act_Decl_Id, False);
3459 Set_Defining_Identifier (N, Act_Decl_Id);
3460 end if;
3462 exception
3463 when Instantiation_Error =>
3464 if Parent_Installed then
3465 Remove_Parent;
3466 end if;
3468 if Env_Installed then
3469 Restore_Env;
3470 end if;
3471 end Analyze_Package_Instantiation;
3473 --------------------------
3474 -- Inline_Instance_Body --
3475 --------------------------
3477 procedure Inline_Instance_Body
3478 (N : Node_Id;
3479 Gen_Unit : Entity_Id;
3480 Act_Decl : Node_Id)
3482 Vis : Boolean;
3483 Gen_Comp : constant Entity_Id :=
3484 Cunit_Entity (Get_Source_Unit (Gen_Unit));
3485 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
3486 Curr_Scope : Entity_Id := Empty;
3487 Curr_Unit : constant Entity_Id :=
3488 Cunit_Entity (Current_Sem_Unit);
3489 Removed : Boolean := False;
3490 Num_Scopes : Int := 0;
3492 Scope_Stack_Depth : constant Int :=
3493 Scope_Stack.Last - Scope_Stack.First + 1;
3495 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
3496 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
3497 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
3498 Num_Inner : Int := 0;
3499 N_Instances : Int := 0;
3500 S : Entity_Id;
3502 begin
3503 -- Case of generic unit defined in another unit. We must remove the
3504 -- complete context of the current unit to install that of the generic.
3506 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
3508 -- Add some comments for the following two loops ???
3510 S := Current_Scope;
3511 while Present (S) and then S /= Standard_Standard loop
3512 loop
3513 Num_Scopes := Num_Scopes + 1;
3515 Use_Clauses (Num_Scopes) :=
3516 (Scope_Stack.Table
3517 (Scope_Stack.Last - Num_Scopes + 1).
3518 First_Use_Clause);
3519 End_Use_Clauses (Use_Clauses (Num_Scopes));
3521 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
3522 or else Scope_Stack.Table
3523 (Scope_Stack.Last - Num_Scopes).Entity
3524 = Scope (S);
3525 end loop;
3527 exit when Is_Generic_Instance (S)
3528 and then (In_Package_Body (S)
3529 or else Ekind (S) = E_Procedure
3530 or else Ekind (S) = E_Function);
3531 S := Scope (S);
3532 end loop;
3534 Vis := Is_Immediately_Visible (Gen_Comp);
3536 -- Find and save all enclosing instances
3538 S := Current_Scope;
3540 while Present (S)
3541 and then S /= Standard_Standard
3542 loop
3543 if Is_Generic_Instance (S) then
3544 N_Instances := N_Instances + 1;
3545 Instances (N_Instances) := S;
3547 exit when In_Package_Body (S);
3548 end if;
3550 S := Scope (S);
3551 end loop;
3553 -- Remove context of current compilation unit, unless we are within a
3554 -- nested package instantiation, in which case the context has been
3555 -- removed previously.
3557 -- If current scope is the body of a child unit, remove context of
3558 -- spec as well. If an enclosing scope is an instance body, the
3559 -- context has already been removed, but the entities in the body
3560 -- must be made invisible as well.
3562 S := Current_Scope;
3564 while Present (S)
3565 and then S /= Standard_Standard
3566 loop
3567 if Is_Generic_Instance (S)
3568 and then (In_Package_Body (S)
3569 or else Ekind (S) = E_Procedure
3570 or else Ekind (S) = E_Function)
3571 then
3572 -- We still have to remove the entities of the enclosing
3573 -- instance from direct visibility.
3575 declare
3576 E : Entity_Id;
3577 begin
3578 E := First_Entity (S);
3579 while Present (E) loop
3580 Set_Is_Immediately_Visible (E, False);
3581 Next_Entity (E);
3582 end loop;
3583 end;
3585 exit;
3586 end if;
3588 if S = Curr_Unit
3589 or else (Ekind (Curr_Unit) = E_Package_Body
3590 and then S = Spec_Entity (Curr_Unit))
3591 or else (Ekind (Curr_Unit) = E_Subprogram_Body
3592 and then S =
3593 Corresponding_Spec
3594 (Unit_Declaration_Node (Curr_Unit)))
3595 then
3596 Removed := True;
3598 -- Remove entities in current scopes from visibility, so that
3599 -- instance body is compiled in a clean environment.
3601 Save_Scope_Stack (Handle_Use => False);
3603 if Is_Child_Unit (S) then
3605 -- Remove child unit from stack, as well as inner scopes.
3606 -- Removing the context of a child unit removes parent units
3607 -- as well.
3609 while Current_Scope /= S loop
3610 Num_Inner := Num_Inner + 1;
3611 Inner_Scopes (Num_Inner) := Current_Scope;
3612 Pop_Scope;
3613 end loop;
3615 Pop_Scope;
3616 Remove_Context (Curr_Comp);
3617 Curr_Scope := S;
3619 else
3620 Remove_Context (Curr_Comp);
3621 end if;
3623 if Ekind (Curr_Unit) = E_Package_Body then
3624 Remove_Context (Library_Unit (Curr_Comp));
3625 end if;
3626 end if;
3628 S := Scope (S);
3629 end loop;
3630 pragma Assert (Num_Inner < Num_Scopes);
3632 Push_Scope (Standard_Standard);
3633 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
3634 Instantiate_Package_Body
3635 (Body_Info =>
3636 ((Inst_Node => N,
3637 Act_Decl => Act_Decl,
3638 Expander_Status => Expander_Active,
3639 Current_Sem_Unit => Current_Sem_Unit,
3640 Scope_Suppress => Scope_Suppress,
3641 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top)),
3642 Inlined_Body => True);
3644 Pop_Scope;
3646 -- Restore context
3648 Set_Is_Immediately_Visible (Gen_Comp, Vis);
3650 -- Reset Generic_Instance flag so that use clauses can be installed
3651 -- in the proper order. (See Use_One_Package for effect of enclosing
3652 -- instances on processing of use clauses).
3654 for J in 1 .. N_Instances loop
3655 Set_Is_Generic_Instance (Instances (J), False);
3656 end loop;
3658 if Removed then
3659 Install_Context (Curr_Comp);
3661 if Present (Curr_Scope)
3662 and then Is_Child_Unit (Curr_Scope)
3663 then
3664 Push_Scope (Curr_Scope);
3665 Set_Is_Immediately_Visible (Curr_Scope);
3667 -- Finally, restore inner scopes as well
3669 for J in reverse 1 .. Num_Inner loop
3670 Push_Scope (Inner_Scopes (J));
3671 end loop;
3672 end if;
3674 Restore_Scope_Stack (Handle_Use => False);
3676 if Present (Curr_Scope)
3677 and then
3678 (In_Private_Part (Curr_Scope)
3679 or else In_Package_Body (Curr_Scope))
3680 then
3681 -- Install private declaration of ancestor units, which are
3682 -- currently available. Restore_Scope_Stack and Install_Context
3683 -- only install the visible part of parents.
3685 declare
3686 Par : Entity_Id;
3687 begin
3688 Par := Scope (Curr_Scope);
3689 while (Present (Par))
3690 and then Par /= Standard_Standard
3691 loop
3692 Install_Private_Declarations (Par);
3693 Par := Scope (Par);
3694 end loop;
3695 end;
3696 end if;
3697 end if;
3699 -- Restore use clauses. For a child unit, use clauses in the parents
3700 -- are restored when installing the context, so only those in inner
3701 -- scopes (and those local to the child unit itself) need to be
3702 -- installed explicitly.
3704 if Is_Child_Unit (Curr_Unit)
3705 and then Removed
3706 then
3707 for J in reverse 1 .. Num_Inner + 1 loop
3708 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3709 Use_Clauses (J);
3710 Install_Use_Clauses (Use_Clauses (J));
3711 end loop;
3713 else
3714 for J in reverse 1 .. Num_Scopes loop
3715 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3716 Use_Clauses (J);
3717 Install_Use_Clauses (Use_Clauses (J));
3718 end loop;
3719 end if;
3721 -- Restore status of instances. If one of them is a body, make
3722 -- its local entities visible again.
3724 declare
3725 E : Entity_Id;
3726 Inst : Entity_Id;
3728 begin
3729 for J in 1 .. N_Instances loop
3730 Inst := Instances (J);
3731 Set_Is_Generic_Instance (Inst, True);
3733 if In_Package_Body (Inst)
3734 or else Ekind (S) = E_Procedure
3735 or else Ekind (S) = E_Function
3736 then
3737 E := First_Entity (Instances (J));
3738 while Present (E) loop
3739 Set_Is_Immediately_Visible (E);
3740 Next_Entity (E);
3741 end loop;
3742 end if;
3743 end loop;
3744 end;
3746 -- If generic unit is in current unit, current context is correct
3748 else
3749 Instantiate_Package_Body
3750 (Body_Info =>
3751 ((Inst_Node => N,
3752 Act_Decl => Act_Decl,
3753 Expander_Status => Expander_Active,
3754 Current_Sem_Unit => Current_Sem_Unit,
3755 Scope_Suppress => Scope_Suppress,
3756 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top)),
3757 Inlined_Body => True);
3758 end if;
3759 end Inline_Instance_Body;
3761 -------------------------------------
3762 -- Analyze_Procedure_Instantiation --
3763 -------------------------------------
3765 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
3766 begin
3767 Analyze_Subprogram_Instantiation (N, E_Procedure);
3768 end Analyze_Procedure_Instantiation;
3770 -----------------------------------
3771 -- Need_Subprogram_Instance_Body --
3772 -----------------------------------
3774 function Need_Subprogram_Instance_Body
3775 (N : Node_Id;
3776 Subp : Entity_Id) return Boolean
3778 begin
3779 if (Is_In_Main_Unit (N)
3780 or else Is_Inlined (Subp)
3781 or else Is_Inlined (Alias (Subp)))
3782 and then (Operating_Mode = Generate_Code
3783 or else (Operating_Mode = Check_Semantics
3784 and then ASIS_Mode))
3785 and then (Expander_Active or else ASIS_Mode)
3786 and then not ABE_Is_Certain (N)
3787 and then not Is_Eliminated (Subp)
3788 then
3789 Pending_Instantiations.Append
3790 ((Inst_Node => N,
3791 Act_Decl => Unit_Declaration_Node (Subp),
3792 Expander_Status => Expander_Active,
3793 Current_Sem_Unit => Current_Sem_Unit,
3794 Scope_Suppress => Scope_Suppress,
3795 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top));
3796 return True;
3797 else
3798 return False;
3799 end if;
3800 end Need_Subprogram_Instance_Body;
3802 --------------------------------------
3803 -- Analyze_Subprogram_Instantiation --
3804 --------------------------------------
3806 procedure Analyze_Subprogram_Instantiation
3807 (N : Node_Id;
3808 K : Entity_Kind)
3810 Loc : constant Source_Ptr := Sloc (N);
3811 Gen_Id : constant Node_Id := Name (N);
3813 Anon_Id : constant Entity_Id :=
3814 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
3815 Chars => New_External_Name
3816 (Chars (Defining_Entity (N)), 'R'));
3818 Act_Decl_Id : Entity_Id;
3819 Act_Decl : Node_Id;
3820 Act_Spec : Node_Id;
3821 Act_Tree : Node_Id;
3823 Env_Installed : Boolean := False;
3824 Gen_Unit : Entity_Id;
3825 Gen_Decl : Node_Id;
3826 Pack_Id : Entity_Id;
3827 Parent_Installed : Boolean := False;
3828 Renaming_List : List_Id;
3830 procedure Analyze_Instance_And_Renamings;
3831 -- The instance must be analyzed in a context that includes the mappings
3832 -- of generic parameters into actuals. We create a package declaration
3833 -- for this purpose, and a subprogram with an internal name within the
3834 -- package. The subprogram instance is simply an alias for the internal
3835 -- subprogram, declared in the current scope.
3837 ------------------------------------
3838 -- Analyze_Instance_And_Renamings --
3839 ------------------------------------
3841 procedure Analyze_Instance_And_Renamings is
3842 Def_Ent : constant Entity_Id := Defining_Entity (N);
3843 Pack_Decl : Node_Id;
3845 begin
3846 if Nkind (Parent (N)) = N_Compilation_Unit then
3848 -- For the case of a compilation unit, the container package has
3849 -- the same name as the instantiation, to insure that the binder
3850 -- calls the elaboration procedure with the right name. Copy the
3851 -- entity of the instance, which may have compilation level flags
3852 -- (e.g. Is_Child_Unit) set.
3854 Pack_Id := New_Copy (Def_Ent);
3856 else
3857 -- Otherwise we use the name of the instantiation concatenated
3858 -- with its source position to ensure uniqueness if there are
3859 -- several instantiations with the same name.
3861 Pack_Id :=
3862 Make_Defining_Identifier (Loc,
3863 Chars => New_External_Name
3864 (Related_Id => Chars (Def_Ent),
3865 Suffix => "GP",
3866 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
3867 end if;
3869 Pack_Decl := Make_Package_Declaration (Loc,
3870 Specification => Make_Package_Specification (Loc,
3871 Defining_Unit_Name => Pack_Id,
3872 Visible_Declarations => Renaming_List,
3873 End_Label => Empty));
3875 Set_Instance_Spec (N, Pack_Decl);
3876 Set_Is_Generic_Instance (Pack_Id);
3877 Set_Debug_Info_Needed (Pack_Id);
3879 -- Case of not a compilation unit
3881 if Nkind (Parent (N)) /= N_Compilation_Unit then
3882 Mark_Rewrite_Insertion (Pack_Decl);
3883 Insert_Before (N, Pack_Decl);
3884 Set_Has_Completion (Pack_Id);
3886 -- Case of an instantiation that is a compilation unit
3888 -- Place declaration on current node so context is complete for
3889 -- analysis (including nested instantiations), and for use in a
3890 -- context_clause (see Analyze_With_Clause).
3892 else
3893 Set_Unit (Parent (N), Pack_Decl);
3894 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
3895 end if;
3897 Analyze (Pack_Decl);
3898 Check_Formal_Packages (Pack_Id);
3899 Set_Is_Generic_Instance (Pack_Id, False);
3901 -- Body of the enclosing package is supplied when instantiating the
3902 -- subprogram body, after semantic analysis is completed.
3904 if Nkind (Parent (N)) = N_Compilation_Unit then
3906 -- Remove package itself from visibility, so it does not
3907 -- conflict with subprogram.
3909 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
3911 -- Set name and scope of internal subprogram so that the proper
3912 -- external name will be generated. The proper scope is the scope
3913 -- of the wrapper package. We need to generate debugging info for
3914 -- the internal subprogram, so set flag accordingly.
3916 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
3917 Set_Scope (Anon_Id, Scope (Pack_Id));
3919 -- Mark wrapper package as referenced, to avoid spurious warnings
3920 -- if the instantiation appears in various with_ clauses of
3921 -- subunits of the main unit.
3923 Set_Referenced (Pack_Id);
3924 end if;
3926 Set_Is_Generic_Instance (Anon_Id);
3927 Set_Debug_Info_Needed (Anon_Id);
3928 Act_Decl_Id := New_Copy (Anon_Id);
3930 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3931 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
3932 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
3933 Set_Comes_From_Source (Act_Decl_Id, True);
3935 -- The signature may involve types that are not frozen yet, but the
3936 -- subprogram will be frozen at the point the wrapper package is
3937 -- frozen, so it does not need its own freeze node. In fact, if one
3938 -- is created, it might conflict with the freezing actions from the
3939 -- wrapper package.
3941 Set_Has_Delayed_Freeze (Anon_Id, False);
3943 -- If the instance is a child unit, mark the Id accordingly. Mark
3944 -- the anonymous entity as well, which is the real subprogram and
3945 -- which is used when the instance appears in a context clause.
3947 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
3948 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
3949 New_Overloaded_Entity (Act_Decl_Id);
3950 Check_Eliminated (Act_Decl_Id);
3952 -- In compilation unit case, kill elaboration checks on the
3953 -- instantiation, since they are never needed -- the body is
3954 -- instantiated at the same point as the spec.
3956 if Nkind (Parent (N)) = N_Compilation_Unit then
3957 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
3958 Set_Kill_Elaboration_Checks (Act_Decl_Id);
3959 Set_Is_Compilation_Unit (Anon_Id);
3961 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
3962 end if;
3964 -- The instance is not a freezing point for the new subprogram
3966 Set_Is_Frozen (Act_Decl_Id, False);
3968 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
3969 Valid_Operator_Definition (Act_Decl_Id);
3970 end if;
3972 Set_Alias (Act_Decl_Id, Anon_Id);
3973 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3974 Set_Has_Completion (Act_Decl_Id);
3975 Set_Related_Instance (Pack_Id, Act_Decl_Id);
3977 if Nkind (Parent (N)) = N_Compilation_Unit then
3978 Set_Body_Required (Parent (N), False);
3979 end if;
3980 end Analyze_Instance_And_Renamings;
3982 -- Start of processing for Analyze_Subprogram_Instantiation
3984 begin
3985 -- Very first thing: apply the special kludge for Text_IO processing
3986 -- in case we are instantiating one of the children of [Wide_]Text_IO.
3987 -- Of course such an instantiation is bogus (these are packages, not
3988 -- subprograms), but we get a better error message if we do this.
3990 Text_IO_Kludge (Gen_Id);
3992 -- Make node global for error reporting
3994 Instantiation_Node := N;
3995 Preanalyze_Actuals (N);
3997 Init_Env;
3998 Env_Installed := True;
3999 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4000 Gen_Unit := Entity (Gen_Id);
4002 Generate_Reference (Gen_Unit, Gen_Id);
4004 if Nkind (Gen_Id) = N_Identifier
4005 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4006 then
4007 Error_Msg_NE
4008 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4009 end if;
4011 if Etype (Gen_Unit) = Any_Type then
4012 Restore_Env;
4013 return;
4014 end if;
4016 -- Verify that it is a generic subprogram of the right kind, and that
4017 -- it does not lead to a circular instantiation.
4019 if Ekind (Gen_Unit) /= E_Generic_Procedure
4020 and then Ekind (Gen_Unit) /= E_Generic_Function
4021 then
4022 Error_Msg_N ("expect generic subprogram in instantiation", Gen_Id);
4024 elsif In_Open_Scopes (Gen_Unit) then
4025 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4027 elsif K = E_Procedure
4028 and then Ekind (Gen_Unit) /= E_Generic_Procedure
4029 then
4030 if Ekind (Gen_Unit) = E_Generic_Function then
4031 Error_Msg_N
4032 ("cannot instantiate generic function as procedure", Gen_Id);
4033 else
4034 Error_Msg_N
4035 ("expect name of generic procedure in instantiation", Gen_Id);
4036 end if;
4038 elsif K = E_Function
4039 and then Ekind (Gen_Unit) /= E_Generic_Function
4040 then
4041 if Ekind (Gen_Unit) = E_Generic_Procedure then
4042 Error_Msg_N
4043 ("cannot instantiate generic procedure as function", Gen_Id);
4044 else
4045 Error_Msg_N
4046 ("expect name of generic function in instantiation", Gen_Id);
4047 end if;
4049 else
4050 Set_Entity (Gen_Id, Gen_Unit);
4051 Set_Is_Instantiated (Gen_Unit);
4053 if In_Extended_Main_Source_Unit (N) then
4054 Generate_Reference (Gen_Unit, N);
4055 end if;
4057 -- If renaming, get original unit
4059 if Present (Renamed_Object (Gen_Unit))
4060 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
4061 or else
4062 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
4063 then
4064 Gen_Unit := Renamed_Object (Gen_Unit);
4065 Set_Is_Instantiated (Gen_Unit);
4066 Generate_Reference (Gen_Unit, N);
4067 end if;
4069 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4070 Error_Msg_Node_2 := Current_Scope;
4071 Error_Msg_NE
4072 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4073 Circularity_Detected := True;
4074 return;
4075 end if;
4077 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4079 -- Initialize renamings map, for error checking
4081 Generic_Renamings.Set_Last (0);
4082 Generic_Renamings_HTable.Reset;
4084 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
4086 -- Copy original generic tree, to produce text for instantiation
4088 Act_Tree :=
4089 Copy_Generic_Node
4090 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4092 -- Inherit overriding indicator from instance node
4094 Act_Spec := Specification (Act_Tree);
4095 Set_Must_Override (Act_Spec, Must_Override (N));
4096 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
4098 Renaming_List :=
4099 Analyze_Associations
4101 Generic_Formal_Declarations (Act_Tree),
4102 Generic_Formal_Declarations (Gen_Decl));
4104 -- The subprogram itself cannot contain a nested instance, so the
4105 -- current parent is left empty.
4107 Set_Instance_Env (Gen_Unit, Empty);
4109 -- Build the subprogram declaration, which does not appear in the
4110 -- generic template, and give it a sloc consistent with that of the
4111 -- template.
4113 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
4114 Set_Generic_Parent (Act_Spec, Gen_Unit);
4115 Act_Decl :=
4116 Make_Subprogram_Declaration (Sloc (Act_Spec),
4117 Specification => Act_Spec);
4119 Set_Categorization_From_Pragmas (Act_Decl);
4121 if Parent_Installed then
4122 Hide_Current_Scope;
4123 end if;
4125 Append (Act_Decl, Renaming_List);
4126 Analyze_Instance_And_Renamings;
4128 -- If the generic is marked Import (Intrinsic), then so is the
4129 -- instance. This indicates that there is no body to instantiate. If
4130 -- generic is marked inline, so it the instance, and the anonymous
4131 -- subprogram it renames. If inlined, or else if inlining is enabled
4132 -- for the compilation, we generate the instance body even if it is
4133 -- not within the main unit.
4135 -- Any other pragmas might also be inherited ???
4137 if Is_Intrinsic_Subprogram (Gen_Unit) then
4138 Set_Is_Intrinsic_Subprogram (Anon_Id);
4139 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
4141 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
4142 Validate_Unchecked_Conversion (N, Act_Decl_Id);
4143 end if;
4144 end if;
4146 Generate_Definition (Act_Decl_Id);
4148 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
4149 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
4151 if not Is_Intrinsic_Subprogram (Gen_Unit) then
4152 Check_Elab_Instantiation (N);
4153 end if;
4155 if Is_Dispatching_Operation (Act_Decl_Id)
4156 and then Ada_Version >= Ada_05
4157 then
4158 declare
4159 Formal : Entity_Id;
4161 begin
4162 Formal := First_Formal (Act_Decl_Id);
4163 while Present (Formal) loop
4164 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
4165 and then Is_Controlling_Formal (Formal)
4166 and then not Can_Never_Be_Null (Formal)
4167 then
4168 Error_Msg_NE ("access parameter& is controlling,",
4169 N, Formal);
4170 Error_Msg_NE ("\corresponding parameter of & must be"
4171 & " explicitly null-excluding", N, Gen_Id);
4172 end if;
4174 Next_Formal (Formal);
4175 end loop;
4176 end;
4177 end if;
4179 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4181 -- Subject to change, pending on if other pragmas are inherited ???
4183 Validate_Categorization_Dependency (N, Act_Decl_Id);
4185 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
4186 Inherit_Context (Gen_Decl, N);
4188 Restore_Private_Views (Pack_Id, False);
4190 -- If the context requires a full instantiation, mark node for
4191 -- subsequent construction of the body.
4193 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
4195 Check_Forward_Instantiation (Gen_Decl);
4197 -- The wrapper package is always delayed, because it does not
4198 -- constitute a freeze point, but to insure that the freeze
4199 -- node is placed properly, it is created directly when
4200 -- instantiating the body (otherwise the freeze node might
4201 -- appear to early for nested instantiations).
4203 elsif Nkind (Parent (N)) = N_Compilation_Unit then
4205 -- For ASIS purposes, indicate that the wrapper package has
4206 -- replaced the instantiation node.
4208 Rewrite (N, Unit (Parent (N)));
4209 Set_Unit (Parent (N), N);
4210 end if;
4212 elsif Nkind (Parent (N)) = N_Compilation_Unit then
4214 -- Replace instance node for library-level instantiations of
4215 -- intrinsic subprograms, for ASIS use.
4217 Rewrite (N, Unit (Parent (N)));
4218 Set_Unit (Parent (N), N);
4219 end if;
4221 if Parent_Installed then
4222 Remove_Parent;
4223 end if;
4225 Restore_Env;
4226 Env_Installed := False;
4227 Generic_Renamings.Set_Last (0);
4228 Generic_Renamings_HTable.Reset;
4229 end if;
4231 exception
4232 when Instantiation_Error =>
4233 if Parent_Installed then
4234 Remove_Parent;
4235 end if;
4237 if Env_Installed then
4238 Restore_Env;
4239 end if;
4240 end Analyze_Subprogram_Instantiation;
4242 -------------------------
4243 -- Get_Associated_Node --
4244 -------------------------
4246 function Get_Associated_Node (N : Node_Id) return Node_Id is
4247 Assoc : Node_Id;
4249 begin
4250 Assoc := Associated_Node (N);
4252 if Nkind (Assoc) /= Nkind (N) then
4253 return Assoc;
4255 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
4256 return Assoc;
4258 else
4259 -- If the node is part of an inner generic, it may itself have been
4260 -- remapped into a further generic copy. Associated_Node is otherwise
4261 -- used for the entity of the node, and will be of a different node
4262 -- kind, or else N has been rewritten as a literal or function call.
4264 while Present (Associated_Node (Assoc))
4265 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
4266 loop
4267 Assoc := Associated_Node (Assoc);
4268 end loop;
4270 -- Follow and additional link in case the final node was rewritten.
4271 -- This can only happen with nested generic units.
4273 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
4274 and then Present (Associated_Node (Assoc))
4275 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
4276 N_Explicit_Dereference,
4277 N_Integer_Literal,
4278 N_Real_Literal,
4279 N_String_Literal))
4280 then
4281 Assoc := Associated_Node (Assoc);
4282 end if;
4284 return Assoc;
4285 end if;
4286 end Get_Associated_Node;
4288 -------------------------------------------
4289 -- Build_Instance_Compilation_Unit_Nodes --
4290 -------------------------------------------
4292 procedure Build_Instance_Compilation_Unit_Nodes
4293 (N : Node_Id;
4294 Act_Body : Node_Id;
4295 Act_Decl : Node_Id)
4297 Decl_Cunit : Node_Id;
4298 Body_Cunit : Node_Id;
4299 Citem : Node_Id;
4300 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
4301 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
4303 begin
4304 -- A new compilation unit node is built for the instance declaration
4306 Decl_Cunit :=
4307 Make_Compilation_Unit (Sloc (N),
4308 Context_Items => Empty_List,
4309 Unit => Act_Decl,
4310 Aux_Decls_Node =>
4311 Make_Compilation_Unit_Aux (Sloc (N)));
4313 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4314 Set_Body_Required (Decl_Cunit, True);
4316 -- We use the original instantiation compilation unit as the resulting
4317 -- compilation unit of the instance, since this is the main unit.
4319 Rewrite (N, Act_Body);
4320 Body_Cunit := Parent (N);
4322 -- The two compilation unit nodes are linked by the Library_Unit field
4324 Set_Library_Unit (Decl_Cunit, Body_Cunit);
4325 Set_Library_Unit (Body_Cunit, Decl_Cunit);
4327 -- Preserve the private nature of the package if needed
4329 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
4331 -- If the instance is not the main unit, its context, categorization,
4332 -- and elaboration entity are not relevant to the compilation.
4334 if Parent (N) /= Cunit (Main_Unit) then
4335 return;
4336 end if;
4338 -- The context clause items on the instantiation, which are now attached
4339 -- to the body compilation unit (since the body overwrote the original
4340 -- instantiation node), semantically belong on the spec, so copy them
4341 -- there. It's harmless to leave them on the body as well. In fact one
4342 -- could argue that they belong in both places.
4344 Citem := First (Context_Items (Body_Cunit));
4345 while Present (Citem) loop
4346 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
4347 Next (Citem);
4348 end loop;
4350 -- Propagate categorization flags on packages, so that they appear in
4351 -- the ali file for the spec of the unit.
4353 if Ekind (New_Main) = E_Package then
4354 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
4355 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
4356 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
4357 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
4358 Set_Is_Remote_Call_Interface
4359 (Old_Main, Is_Remote_Call_Interface (New_Main));
4360 end if;
4362 -- Make entry in Units table, so that binder can generate call to
4363 -- elaboration procedure for body, if any.
4365 Make_Instance_Unit (Body_Cunit);
4366 Main_Unit_Entity := New_Main;
4367 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
4369 -- Build elaboration entity, since the instance may certainly generate
4370 -- elaboration code requiring a flag for protection.
4372 Build_Elaboration_Entity (Decl_Cunit, New_Main);
4373 end Build_Instance_Compilation_Unit_Nodes;
4375 -----------------------------
4376 -- Check_Access_Definition --
4377 -----------------------------
4379 procedure Check_Access_Definition (N : Node_Id) is
4380 begin
4381 pragma Assert
4382 (Ada_Version >= Ada_05
4383 and then Present (Access_Definition (N)));
4384 null;
4385 end Check_Access_Definition;
4387 -----------------------------------
4388 -- Check_Formal_Package_Instance --
4389 -----------------------------------
4391 -- If the formal has specific parameters, they must match those of the
4392 -- actual. Both of them are instances, and the renaming declarations for
4393 -- their formal parameters appear in the same order in both. The analyzed
4394 -- formal has been analyzed in the context of the current instance.
4396 procedure Check_Formal_Package_Instance
4397 (Formal_Pack : Entity_Id;
4398 Actual_Pack : Entity_Id)
4400 E1 : Entity_Id := First_Entity (Actual_Pack);
4401 E2 : Entity_Id := First_Entity (Formal_Pack);
4403 Expr1 : Node_Id;
4404 Expr2 : Node_Id;
4406 procedure Check_Mismatch (B : Boolean);
4407 -- Common error routine for mismatch between the parameters of the
4408 -- actual instance and those of the formal package.
4410 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
4411 -- The formal may come from a nested formal package, and the actual may
4412 -- have been constant-folded. To determine whether the two denote the
4413 -- same entity we may have to traverse several definitions to recover
4414 -- the ultimate entity that they refer to.
4416 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
4417 -- Similarly, if the formal comes from a nested formal package, the
4418 -- actual may designate the formal through multiple renamings, which
4419 -- have to be followed to determine the original variable in question.
4421 --------------------
4422 -- Check_Mismatch --
4423 --------------------
4425 procedure Check_Mismatch (B : Boolean) is
4426 Kind : constant Node_Kind := Nkind (Parent (E2));
4428 begin
4429 if Kind = N_Formal_Type_Declaration then
4430 return;
4432 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
4433 N_Formal_Package_Declaration)
4434 or else Kind in N_Formal_Subprogram_Declaration
4435 then
4436 null;
4438 elsif B then
4439 Error_Msg_NE
4440 ("actual for & in actual instance does not match formal",
4441 Parent (Actual_Pack), E1);
4442 end if;
4443 end Check_Mismatch;
4445 --------------------------------
4446 -- Same_Instantiated_Constant --
4447 --------------------------------
4449 function Same_Instantiated_Constant
4450 (E1, E2 : Entity_Id) return Boolean
4452 Ent : Entity_Id;
4454 begin
4455 Ent := E2;
4456 while Present (Ent) loop
4457 if E1 = Ent then
4458 return True;
4460 elsif Ekind (Ent) /= E_Constant then
4461 return False;
4463 elsif Is_Entity_Name (Constant_Value (Ent)) then
4464 if Entity (Constant_Value (Ent)) = E1 then
4465 return True;
4466 else
4467 Ent := Entity (Constant_Value (Ent));
4468 end if;
4470 -- The actual may be a constant that has been folded. Recover
4471 -- original name.
4473 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
4474 Ent := Entity (Original_Node (Constant_Value (Ent)));
4475 else
4476 return False;
4477 end if;
4478 end loop;
4480 return False;
4481 end Same_Instantiated_Constant;
4483 --------------------------------
4484 -- Same_Instantiated_Variable --
4485 --------------------------------
4487 function Same_Instantiated_Variable
4488 (E1, E2 : Entity_Id) return Boolean
4490 function Original_Entity (E : Entity_Id) return Entity_Id;
4491 -- Follow chain of renamings to the ultimate ancestor
4493 ---------------------
4494 -- Original_Entity --
4495 ---------------------
4497 function Original_Entity (E : Entity_Id) return Entity_Id is
4498 Orig : Entity_Id;
4500 begin
4501 Orig := E;
4502 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
4503 and then Present (Renamed_Object (Orig))
4504 and then Is_Entity_Name (Renamed_Object (Orig))
4505 loop
4506 Orig := Entity (Renamed_Object (Orig));
4507 end loop;
4509 return Orig;
4510 end Original_Entity;
4512 -- Start of processing for Same_Instantiated_Variable
4514 begin
4515 return Ekind (E1) = Ekind (E2)
4516 and then Original_Entity (E1) = Original_Entity (E2);
4517 end Same_Instantiated_Variable;
4519 -- Start of processing for Check_Formal_Package_Instance
4521 begin
4522 while Present (E1)
4523 and then Present (E2)
4524 loop
4525 exit when Ekind (E1) = E_Package
4526 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
4528 -- If the formal is the renaming of the formal package, this
4529 -- is the end of its formal part, which may occur before the
4530 -- end of the formal part in the actual in the presence of
4531 -- defaulted parameters in the formal package.
4533 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
4534 and then Renamed_Entity (E2) = Scope (E2);
4536 -- The analysis of the actual may generate additional internal
4537 -- entities. If the formal is defaulted, there is no corresponding
4538 -- analysis and the internal entities must be skipped, until we
4539 -- find corresponding entities again.
4541 if Comes_From_Source (E2)
4542 and then not Comes_From_Source (E1)
4543 and then Chars (E1) /= Chars (E2)
4544 then
4545 while Present (E1)
4546 and then Chars (E1) /= Chars (E2)
4547 loop
4548 Next_Entity (E1);
4549 end loop;
4550 end if;
4552 if No (E1) then
4553 return;
4555 -- If the formal entity comes from a formal declaration, it was
4556 -- defaulted in the formal package, and no check is needed on it.
4558 elsif Nkind (Parent (E2)) = N_Formal_Object_Declaration then
4559 goto Next_E;
4561 elsif Is_Type (E1) then
4563 -- Subtypes must statically match. E1, E2 are the local entities
4564 -- that are subtypes of the actuals. Itypes generated for other
4565 -- parameters need not be checked, the check will be performed
4566 -- on the parameters themselves.
4568 -- If E2 is a formal type declaration, it is a defaulted parameter
4569 -- and needs no checking.
4571 if not Is_Itype (E1)
4572 and then not Is_Itype (E2)
4573 then
4574 Check_Mismatch
4575 (not Is_Type (E2)
4576 or else Etype (E1) /= Etype (E2)
4577 or else not Subtypes_Statically_Match (E1, E2));
4578 end if;
4580 elsif Ekind (E1) = E_Constant then
4582 -- IN parameters must denote the same static value, or the same
4583 -- constant, or the literal null.
4585 Expr1 := Expression (Parent (E1));
4587 if Ekind (E2) /= E_Constant then
4588 Check_Mismatch (True);
4589 goto Next_E;
4590 else
4591 Expr2 := Expression (Parent (E2));
4592 end if;
4594 if Is_Static_Expression (Expr1) then
4596 if not Is_Static_Expression (Expr2) then
4597 Check_Mismatch (True);
4599 elsif Is_Discrete_Type (Etype (E1)) then
4600 declare
4601 V1 : constant Uint := Expr_Value (Expr1);
4602 V2 : constant Uint := Expr_Value (Expr2);
4603 begin
4604 Check_Mismatch (V1 /= V2);
4605 end;
4607 elsif Is_Real_Type (Etype (E1)) then
4608 declare
4609 V1 : constant Ureal := Expr_Value_R (Expr1);
4610 V2 : constant Ureal := Expr_Value_R (Expr2);
4611 begin
4612 Check_Mismatch (V1 /= V2);
4613 end;
4615 elsif Is_String_Type (Etype (E1))
4616 and then Nkind (Expr1) = N_String_Literal
4617 then
4618 if Nkind (Expr2) /= N_String_Literal then
4619 Check_Mismatch (True);
4620 else
4621 Check_Mismatch
4622 (not String_Equal (Strval (Expr1), Strval (Expr2)));
4623 end if;
4624 end if;
4626 elsif Is_Entity_Name (Expr1) then
4627 if Is_Entity_Name (Expr2) then
4628 if Entity (Expr1) = Entity (Expr2) then
4629 null;
4630 else
4631 Check_Mismatch
4632 (not Same_Instantiated_Constant
4633 (Entity (Expr1), Entity (Expr2)));
4634 end if;
4635 else
4636 Check_Mismatch (True);
4637 end if;
4639 elsif Is_Entity_Name (Original_Node (Expr1))
4640 and then Is_Entity_Name (Expr2)
4641 and then
4642 Same_Instantiated_Constant
4643 (Entity (Original_Node (Expr1)), Entity (Expr2))
4644 then
4645 null;
4647 elsif Nkind (Expr1) = N_Null then
4648 Check_Mismatch (Nkind (Expr1) /= N_Null);
4650 else
4651 Check_Mismatch (True);
4652 end if;
4654 elsif Ekind (E1) = E_Variable then
4655 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
4657 elsif Ekind (E1) = E_Package then
4658 Check_Mismatch
4659 (Ekind (E1) /= Ekind (E2)
4660 or else Renamed_Object (E1) /= Renamed_Object (E2));
4662 elsif Is_Overloadable (E1) then
4664 -- Verify that the actual subprograms match. Note that actuals
4665 -- that are attributes are rewritten as subprograms. If the
4666 -- subprogram in the formal package is defaulted, no check is
4667 -- needed. Note that this can only happen in Ada 2005 when the
4668 -- formal package can be partially parametrized.
4670 if Nkind (Unit_Declaration_Node (E1)) =
4671 N_Subprogram_Renaming_Declaration
4672 and then From_Default (Unit_Declaration_Node (E1))
4673 then
4674 null;
4676 else
4677 Check_Mismatch
4678 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
4679 end if;
4681 else
4682 raise Program_Error;
4683 end if;
4685 <<Next_E>>
4686 Next_Entity (E1);
4687 Next_Entity (E2);
4688 end loop;
4689 end Check_Formal_Package_Instance;
4691 ---------------------------
4692 -- Check_Formal_Packages --
4693 ---------------------------
4695 procedure Check_Formal_Packages (P_Id : Entity_Id) is
4696 E : Entity_Id;
4697 Formal_P : Entity_Id;
4699 begin
4700 -- Iterate through the declarations in the instance, looking for package
4701 -- renaming declarations that denote instances of formal packages. Stop
4702 -- when we find the renaming of the current package itself. The
4703 -- declaration for a formal package without a box is followed by an
4704 -- internal entity that repeats the instantiation.
4706 E := First_Entity (P_Id);
4707 while Present (E) loop
4708 if Ekind (E) = E_Package then
4709 if Renamed_Object (E) = P_Id then
4710 exit;
4712 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
4713 null;
4715 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
4716 Formal_P := Next_Entity (E);
4717 Check_Formal_Package_Instance (Formal_P, E);
4719 -- After checking, remove the internal validating package. It
4720 -- is only needed for semantic checks, and as it may contain
4721 -- generic formal declarations it should not reach gigi.
4723 Remove (Unit_Declaration_Node (Formal_P));
4724 end if;
4725 end if;
4727 Next_Entity (E);
4728 end loop;
4729 end Check_Formal_Packages;
4731 ---------------------------------
4732 -- Check_Forward_Instantiation --
4733 ---------------------------------
4735 procedure Check_Forward_Instantiation (Decl : Node_Id) is
4736 S : Entity_Id;
4737 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
4739 begin
4740 -- The instantiation appears before the generic body if we are in the
4741 -- scope of the unit containing the generic, either in its spec or in
4742 -- the package body, and before the generic body.
4744 if Ekind (Gen_Comp) = E_Package_Body then
4745 Gen_Comp := Spec_Entity (Gen_Comp);
4746 end if;
4748 if In_Open_Scopes (Gen_Comp)
4749 and then No (Corresponding_Body (Decl))
4750 then
4751 S := Current_Scope;
4753 while Present (S)
4754 and then not Is_Compilation_Unit (S)
4755 and then not Is_Child_Unit (S)
4756 loop
4757 if Ekind (S) = E_Package then
4758 Set_Has_Forward_Instantiation (S);
4759 end if;
4761 S := Scope (S);
4762 end loop;
4763 end if;
4764 end Check_Forward_Instantiation;
4766 ---------------------------
4767 -- Check_Generic_Actuals --
4768 ---------------------------
4770 -- The visibility of the actuals may be different between the point of
4771 -- generic instantiation and the instantiation of the body.
4773 procedure Check_Generic_Actuals
4774 (Instance : Entity_Id;
4775 Is_Formal_Box : Boolean)
4777 E : Entity_Id;
4778 Astype : Entity_Id;
4780 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
4781 -- For a formal that is an array type, the component type is often a
4782 -- previous formal in the same unit. The privacy status of the component
4783 -- type will have been examined earlier in the traversal of the
4784 -- corresponding actuals, and this status should not be modified for the
4785 -- array type itself.
4787 -- To detect this case we have to rescan the list of formals, which
4788 -- is usually short enough to ignore the resulting inefficiency.
4790 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
4791 Prev : Entity_Id;
4792 begin
4793 Prev := First_Entity (Instance);
4794 while Present (Prev) loop
4795 if Is_Type (Prev)
4796 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
4797 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
4798 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
4799 then
4800 return True;
4801 elsif Prev = E then
4802 return False;
4803 else
4804 Next_Entity (Prev);
4805 end if;
4806 end loop;
4807 return False;
4808 end Denotes_Previous_Actual;
4810 -- Start of processing for Check_Generic_Actuals
4812 begin
4813 E := First_Entity (Instance);
4814 while Present (E) loop
4815 if Is_Type (E)
4816 and then Nkind (Parent (E)) = N_Subtype_Declaration
4817 and then Scope (Etype (E)) /= Instance
4818 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
4819 then
4820 if Is_Array_Type (E)
4821 and then Denotes_Previous_Actual (Component_Type (E))
4822 then
4823 null;
4824 else
4825 Check_Private_View (Subtype_Indication (Parent (E)));
4826 end if;
4827 Set_Is_Generic_Actual_Type (E, True);
4828 Set_Is_Hidden (E, False);
4829 Set_Is_Potentially_Use_Visible (E,
4830 In_Use (Instance));
4832 -- We constructed the generic actual type as a subtype of the
4833 -- supplied type. This means that it normally would not inherit
4834 -- subtype specific attributes of the actual, which is wrong for
4835 -- the generic case.
4837 Astype := Ancestor_Subtype (E);
4839 if No (Astype) then
4841 -- This can happen when E is an itype that is the full view of
4842 -- a private type completed, e.g. with a constrained array. In
4843 -- that case, use the first subtype, which will carry size
4844 -- information. The base type itself is unconstrained and will
4845 -- not carry it.
4847 Astype := First_Subtype (E);
4848 end if;
4850 Set_Size_Info (E, (Astype));
4851 Set_RM_Size (E, RM_Size (Astype));
4852 Set_First_Rep_Item (E, First_Rep_Item (Astype));
4854 if Is_Discrete_Or_Fixed_Point_Type (E) then
4855 Set_RM_Size (E, RM_Size (Astype));
4857 -- In nested instances, the base type of an access actual
4858 -- may itself be private, and need to be exchanged.
4860 elsif Is_Access_Type (E)
4861 and then Is_Private_Type (Etype (E))
4862 then
4863 Check_Private_View
4864 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
4865 end if;
4867 elsif Ekind (E) = E_Package then
4869 -- If this is the renaming for the current instance, we're done.
4870 -- Otherwise it is a formal package. If the corresponding formal
4871 -- was declared with a box, the (instantiations of the) generic
4872 -- formal part are also visible. Otherwise, ignore the entity
4873 -- created to validate the actuals.
4875 if Renamed_Object (E) = Instance then
4876 exit;
4878 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
4879 null;
4881 -- The visibility of a formal of an enclosing generic is already
4882 -- correct.
4884 elsif Denotes_Formal_Package (E) then
4885 null;
4887 elsif Present (Associated_Formal_Package (E))
4888 and then not Is_Generic_Formal (E)
4889 then
4890 if Box_Present (Parent (Associated_Formal_Package (E))) then
4891 Check_Generic_Actuals (Renamed_Object (E), True);
4893 else
4894 Check_Generic_Actuals (Renamed_Object (E), False);
4895 end if;
4897 Set_Is_Hidden (E, False);
4898 end if;
4900 -- If this is a subprogram instance (in a wrapper package) the
4901 -- actual is fully visible.
4903 elsif Is_Wrapper_Package (Instance) then
4904 Set_Is_Hidden (E, False);
4906 -- If the formal package is declared with a box, or if the formal
4907 -- parameter is defaulted, it is visible in the body.
4909 elsif Is_Formal_Box
4910 or else Is_Visible_Formal (E)
4911 then
4912 Set_Is_Hidden (E, False);
4913 end if;
4915 Next_Entity (E);
4916 end loop;
4917 end Check_Generic_Actuals;
4919 ------------------------------
4920 -- Check_Generic_Child_Unit --
4921 ------------------------------
4923 procedure Check_Generic_Child_Unit
4924 (Gen_Id : Node_Id;
4925 Parent_Installed : in out Boolean)
4927 Loc : constant Source_Ptr := Sloc (Gen_Id);
4928 Gen_Par : Entity_Id := Empty;
4929 E : Entity_Id;
4930 Inst_Par : Entity_Id;
4931 S : Node_Id;
4933 function Find_Generic_Child
4934 (Scop : Entity_Id;
4935 Id : Node_Id) return Entity_Id;
4936 -- Search generic parent for possible child unit with the given name
4938 function In_Enclosing_Instance return Boolean;
4939 -- Within an instance of the parent, the child unit may be denoted
4940 -- by a simple name, or an abbreviated expanded name. Examine enclosing
4941 -- scopes to locate a possible parent instantiation.
4943 ------------------------
4944 -- Find_Generic_Child --
4945 ------------------------
4947 function Find_Generic_Child
4948 (Scop : Entity_Id;
4949 Id : Node_Id) return Entity_Id
4951 E : Entity_Id;
4953 begin
4954 -- If entity of name is already set, instance has already been
4955 -- resolved, e.g. in an enclosing instantiation.
4957 if Present (Entity (Id)) then
4958 if Scope (Entity (Id)) = Scop then
4959 return Entity (Id);
4960 else
4961 return Empty;
4962 end if;
4964 else
4965 E := First_Entity (Scop);
4966 while Present (E) loop
4967 if Chars (E) = Chars (Id)
4968 and then Is_Child_Unit (E)
4969 then
4970 if Is_Child_Unit (E)
4971 and then not Is_Visible_Child_Unit (E)
4972 then
4973 Error_Msg_NE
4974 ("generic child unit& is not visible", Gen_Id, E);
4975 end if;
4977 Set_Entity (Id, E);
4978 return E;
4979 end if;
4981 Next_Entity (E);
4982 end loop;
4984 return Empty;
4985 end if;
4986 end Find_Generic_Child;
4988 ---------------------------
4989 -- In_Enclosing_Instance --
4990 ---------------------------
4992 function In_Enclosing_Instance return Boolean is
4993 Enclosing_Instance : Node_Id;
4994 Instance_Decl : Node_Id;
4996 begin
4997 -- We do not inline any call that contains instantiations, except
4998 -- for instantiations of Unchecked_Conversion, so if we are within
4999 -- an inlined body the current instance does not require parents.
5001 if In_Inlined_Body then
5002 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
5003 return False;
5004 end if;
5006 -- Loop to check enclosing scopes
5008 Enclosing_Instance := Current_Scope;
5009 while Present (Enclosing_Instance) loop
5010 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
5012 if Ekind (Enclosing_Instance) = E_Package
5013 and then Is_Generic_Instance (Enclosing_Instance)
5014 and then Present
5015 (Generic_Parent (Specification (Instance_Decl)))
5016 then
5017 -- Check whether the generic we are looking for is a child of
5018 -- this instance.
5020 E := Find_Generic_Child
5021 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
5022 exit when Present (E);
5024 else
5025 E := Empty;
5026 end if;
5028 Enclosing_Instance := Scope (Enclosing_Instance);
5029 end loop;
5031 if No (E) then
5033 -- Not a child unit
5035 Analyze (Gen_Id);
5036 return False;
5038 else
5039 Rewrite (Gen_Id,
5040 Make_Expanded_Name (Loc,
5041 Chars => Chars (E),
5042 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
5043 Selector_Name => New_Occurrence_Of (E, Loc)));
5045 Set_Entity (Gen_Id, E);
5046 Set_Etype (Gen_Id, Etype (E));
5047 Parent_Installed := False; -- Already in scope.
5048 return True;
5049 end if;
5050 end In_Enclosing_Instance;
5052 -- Start of processing for Check_Generic_Child_Unit
5054 begin
5055 -- If the name of the generic is given by a selected component, it may
5056 -- be the name of a generic child unit, and the prefix is the name of an
5057 -- instance of the parent, in which case the child unit must be visible.
5058 -- If this instance is not in scope, it must be placed there and removed
5059 -- after instantiation, because what is being instantiated is not the
5060 -- original child, but the corresponding child present in the instance
5061 -- of the parent.
5063 -- If the child is instantiated within the parent, it can be given by
5064 -- a simple name. In this case the instance is already in scope, but
5065 -- the child generic must be recovered from the generic parent as well.
5067 if Nkind (Gen_Id) = N_Selected_Component then
5068 S := Selector_Name (Gen_Id);
5069 Analyze (Prefix (Gen_Id));
5070 Inst_Par := Entity (Prefix (Gen_Id));
5072 if Ekind (Inst_Par) = E_Package
5073 and then Present (Renamed_Object (Inst_Par))
5074 then
5075 Inst_Par := Renamed_Object (Inst_Par);
5076 end if;
5078 if Ekind (Inst_Par) = E_Package then
5079 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
5080 Gen_Par := Generic_Parent (Parent (Inst_Par));
5082 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
5083 and then
5084 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
5085 then
5086 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
5087 end if;
5089 elsif Ekind (Inst_Par) = E_Generic_Package
5090 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
5091 then
5092 -- A formal package may be a real child package, and not the
5093 -- implicit instance within a parent. In this case the child is
5094 -- not visible and has to be retrieved explicitly as well.
5096 Gen_Par := Inst_Par;
5097 end if;
5099 if Present (Gen_Par) then
5101 -- The prefix denotes an instantiation. The entity itself may be a
5102 -- nested generic, or a child unit.
5104 E := Find_Generic_Child (Gen_Par, S);
5106 if Present (E) then
5107 Change_Selected_Component_To_Expanded_Name (Gen_Id);
5108 Set_Entity (Gen_Id, E);
5109 Set_Etype (Gen_Id, Etype (E));
5110 Set_Entity (S, E);
5111 Set_Etype (S, Etype (E));
5113 -- Indicate that this is a reference to the parent
5115 if In_Extended_Main_Source_Unit (Gen_Id) then
5116 Set_Is_Instantiated (Inst_Par);
5117 end if;
5119 -- A common mistake is to replicate the naming scheme of a
5120 -- hierarchy by instantiating a generic child directly, rather
5121 -- than the implicit child in a parent instance:
5123 -- generic .. package Gpar is ..
5124 -- generic .. package Gpar.Child is ..
5125 -- package Par is new Gpar ();
5127 -- with Gpar.Child;
5128 -- package Par.Child is new Gpar.Child ();
5129 -- rather than Par.Child
5131 -- In this case the instantiation is within Par, which is an
5132 -- instance, but Gpar does not denote Par because we are not IN
5133 -- the instance of Gpar, so this is illegal. The test below
5134 -- recognizes this particular case.
5136 if Is_Child_Unit (E)
5137 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
5138 and then (not In_Instance
5139 or else Nkind (Parent (Parent (Gen_Id))) =
5140 N_Compilation_Unit)
5141 then
5142 Error_Msg_N
5143 ("prefix of generic child unit must be instance of parent",
5144 Gen_Id);
5145 end if;
5147 if not In_Open_Scopes (Inst_Par)
5148 and then Nkind (Parent (Gen_Id)) not in
5149 N_Generic_Renaming_Declaration
5150 then
5151 Install_Parent (Inst_Par);
5152 Parent_Installed := True;
5154 elsif In_Open_Scopes (Inst_Par) then
5156 -- If the parent is already installed verify that the
5157 -- actuals for its formal packages declared with a box
5158 -- are already installed. This is necessary when the
5159 -- child instance is a child of the parent instance.
5160 -- In this case the parent is placed on the scope stack
5161 -- but the formal packages are not made visible.
5163 Install_Formal_Packages (Inst_Par);
5164 end if;
5166 else
5167 -- If the generic parent does not contain an entity that
5168 -- corresponds to the selector, the instance doesn't either.
5169 -- Analyzing the node will yield the appropriate error message.
5170 -- If the entity is not a child unit, then it is an inner
5171 -- generic in the parent.
5173 Analyze (Gen_Id);
5174 end if;
5176 else
5177 Analyze (Gen_Id);
5179 if Is_Child_Unit (Entity (Gen_Id))
5180 and then
5181 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
5182 and then not In_Open_Scopes (Inst_Par)
5183 then
5184 Install_Parent (Inst_Par);
5185 Parent_Installed := True;
5186 end if;
5187 end if;
5189 elsif Nkind (Gen_Id) = N_Expanded_Name then
5191 -- Entity already present, analyze prefix, whose meaning may be
5192 -- an instance in the current context. If it is an instance of
5193 -- a relative within another, the proper parent may still have
5194 -- to be installed, if they are not of the same generation.
5196 Analyze (Prefix (Gen_Id));
5198 -- In the unlikely case that a local declaration hides the name
5199 -- of the parent package, locate it on the homonym chain. If the
5200 -- context is an instance of the parent, the renaming entity is
5201 -- flagged as such.
5203 Inst_Par := Entity (Prefix (Gen_Id));
5204 while Present (Inst_Par)
5205 and then not Is_Package_Or_Generic_Package (Inst_Par)
5206 loop
5207 Inst_Par := Homonym (Inst_Par);
5208 end loop;
5210 pragma Assert (Present (Inst_Par));
5211 Set_Entity (Prefix (Gen_Id), Inst_Par);
5213 if In_Enclosing_Instance then
5214 null;
5216 elsif Present (Entity (Gen_Id))
5217 and then Is_Child_Unit (Entity (Gen_Id))
5218 and then not In_Open_Scopes (Inst_Par)
5219 then
5220 Install_Parent (Inst_Par);
5221 Parent_Installed := True;
5222 end if;
5224 elsif In_Enclosing_Instance then
5226 -- The child unit is found in some enclosing scope
5228 null;
5230 else
5231 Analyze (Gen_Id);
5233 -- If this is the renaming of the implicit child in a parent
5234 -- instance, recover the parent name and install it.
5236 if Is_Entity_Name (Gen_Id) then
5237 E := Entity (Gen_Id);
5239 if Is_Generic_Unit (E)
5240 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
5241 and then Is_Child_Unit (Renamed_Object (E))
5242 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
5243 and then Nkind (Name (Parent (E))) = N_Expanded_Name
5244 then
5245 Rewrite (Gen_Id,
5246 New_Copy_Tree (Name (Parent (E))));
5247 Inst_Par := Entity (Prefix (Gen_Id));
5249 if not In_Open_Scopes (Inst_Par) then
5250 Install_Parent (Inst_Par);
5251 Parent_Installed := True;
5252 end if;
5254 -- If it is a child unit of a non-generic parent, it may be
5255 -- use-visible and given by a direct name. Install parent as
5256 -- for other cases.
5258 elsif Is_Generic_Unit (E)
5259 and then Is_Child_Unit (E)
5260 and then
5261 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
5262 and then not Is_Generic_Unit (Scope (E))
5263 then
5264 if not In_Open_Scopes (Scope (E)) then
5265 Install_Parent (Scope (E));
5266 Parent_Installed := True;
5267 end if;
5268 end if;
5269 end if;
5270 end if;
5271 end Check_Generic_Child_Unit;
5273 -----------------------------
5274 -- Check_Hidden_Child_Unit --
5275 -----------------------------
5277 procedure Check_Hidden_Child_Unit
5278 (N : Node_Id;
5279 Gen_Unit : Entity_Id;
5280 Act_Decl_Id : Entity_Id)
5282 Gen_Id : constant Node_Id := Name (N);
5284 begin
5285 if Is_Child_Unit (Gen_Unit)
5286 and then Is_Child_Unit (Act_Decl_Id)
5287 and then Nkind (Gen_Id) = N_Expanded_Name
5288 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
5289 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
5290 then
5291 Error_Msg_Node_2 := Scope (Act_Decl_Id);
5292 Error_Msg_NE
5293 ("generic unit & is implicitly declared in &",
5294 Defining_Unit_Name (N), Gen_Unit);
5295 Error_Msg_N ("\instance must have different name",
5296 Defining_Unit_Name (N));
5297 end if;
5298 end Check_Hidden_Child_Unit;
5300 ------------------------
5301 -- Check_Private_View --
5302 ------------------------
5304 procedure Check_Private_View (N : Node_Id) is
5305 T : constant Entity_Id := Etype (N);
5306 BT : Entity_Id;
5308 begin
5309 -- Exchange views if the type was not private in the generic but is
5310 -- private at the point of instantiation. Do not exchange views if
5311 -- the scope of the type is in scope. This can happen if both generic
5312 -- and instance are sibling units, or if type is defined in a parent.
5313 -- In this case the visibility of the type will be correct for all
5314 -- semantic checks.
5316 if Present (T) then
5317 BT := Base_Type (T);
5319 if Is_Private_Type (T)
5320 and then not Has_Private_View (N)
5321 and then Present (Full_View (T))
5322 and then not In_Open_Scopes (Scope (T))
5323 then
5324 -- In the generic, the full type was visible. Save the private
5325 -- entity, for subsequent exchange.
5327 Switch_View (T);
5329 elsif Has_Private_View (N)
5330 and then not Is_Private_Type (T)
5331 and then not Has_Been_Exchanged (T)
5332 and then Etype (Get_Associated_Node (N)) /= T
5333 then
5334 -- Only the private declaration was visible in the generic. If
5335 -- the type appears in a subtype declaration, the subtype in the
5336 -- instance must have a view compatible with that of its parent,
5337 -- which must be exchanged (see corresponding code in Restore_
5338 -- Private_Views). Otherwise, if the type is defined in a parent
5339 -- unit, leave full visibility within instance, which is safe.
5341 if In_Open_Scopes (Scope (Base_Type (T)))
5342 and then not Is_Private_Type (Base_Type (T))
5343 and then Comes_From_Source (Base_Type (T))
5344 then
5345 null;
5347 elsif Nkind (Parent (N)) = N_Subtype_Declaration
5348 or else not In_Private_Part (Scope (Base_Type (T)))
5349 then
5350 Prepend_Elmt (T, Exchanged_Views);
5351 Exchange_Declarations (Etype (Get_Associated_Node (N)));
5352 end if;
5354 -- For composite types with inconsistent representation exchange
5355 -- component types accordingly.
5357 elsif Is_Access_Type (T)
5358 and then Is_Private_Type (Designated_Type (T))
5359 and then not Has_Private_View (N)
5360 and then Present (Full_View (Designated_Type (T)))
5361 then
5362 Switch_View (Designated_Type (T));
5364 elsif Is_Array_Type (T) then
5365 if Is_Private_Type (Component_Type (T))
5366 and then not Has_Private_View (N)
5367 and then Present (Full_View (Component_Type (T)))
5368 then
5369 Switch_View (Component_Type (T));
5370 end if;
5372 -- The normal exchange mechanism relies on the setting of a
5373 -- flag on the reference in the generic. However, an additional
5374 -- mechanism is needed for types that are not explicitly mentioned
5375 -- in the generic, but may be needed in expanded code in the
5376 -- instance. This includes component types of arrays and
5377 -- designated types of access types. This processing must also
5378 -- include the index types of arrays which we take care of here.
5380 declare
5381 Indx : Node_Id;
5382 Typ : Entity_Id;
5384 begin
5385 Indx := First_Index (T);
5386 Typ := Base_Type (Etype (Indx));
5387 while Present (Indx) loop
5388 if Is_Private_Type (Typ)
5389 and then Present (Full_View (Typ))
5390 then
5391 Switch_View (Typ);
5392 end if;
5394 Next_Index (Indx);
5395 end loop;
5396 end;
5398 elsif Is_Private_Type (T)
5399 and then Present (Full_View (T))
5400 and then Is_Array_Type (Full_View (T))
5401 and then Is_Private_Type (Component_Type (Full_View (T)))
5402 then
5403 Switch_View (T);
5405 -- Finally, a non-private subtype may have a private base type, which
5406 -- must be exchanged for consistency. This can happen when a package
5407 -- body is instantiated, when the scope stack is empty but in fact
5408 -- the subtype and the base type are declared in an enclosing scope.
5410 -- Note that in this case we introduce an inconsistency in the view
5411 -- set, because we switch the base type BT, but there could be some
5412 -- private dependent subtypes of BT which remain unswitched. Such
5413 -- subtypes might need to be switched at a later point (see specific
5414 -- provision for that case in Switch_View).
5416 elsif not Is_Private_Type (T)
5417 and then not Has_Private_View (N)
5418 and then Is_Private_Type (BT)
5419 and then Present (Full_View (BT))
5420 and then not Is_Generic_Type (BT)
5421 and then not In_Open_Scopes (BT)
5422 then
5423 Prepend_Elmt (Full_View (BT), Exchanged_Views);
5424 Exchange_Declarations (BT);
5425 end if;
5426 end if;
5427 end Check_Private_View;
5429 --------------------------
5430 -- Contains_Instance_Of --
5431 --------------------------
5433 function Contains_Instance_Of
5434 (Inner : Entity_Id;
5435 Outer : Entity_Id;
5436 N : Node_Id) return Boolean
5438 Elmt : Elmt_Id;
5439 Scop : Entity_Id;
5441 begin
5442 Scop := Outer;
5444 -- Verify that there are no circular instantiations. We check whether
5445 -- the unit contains an instance of the current scope or some enclosing
5446 -- scope (in case one of the instances appears in a subunit). Longer
5447 -- circularities involving subunits might seem too pathological to
5448 -- consider, but they were not too pathological for the authors of
5449 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
5450 -- enclosing generic scopes as containing an instance.
5452 loop
5453 -- Within a generic subprogram body, the scope is not generic, to
5454 -- allow for recursive subprograms. Use the declaration to determine
5455 -- whether this is a generic unit.
5457 if Ekind (Scop) = E_Generic_Package
5458 or else (Is_Subprogram (Scop)
5459 and then Nkind (Unit_Declaration_Node (Scop)) =
5460 N_Generic_Subprogram_Declaration)
5461 then
5462 Elmt := First_Elmt (Inner_Instances (Inner));
5464 while Present (Elmt) loop
5465 if Node (Elmt) = Scop then
5466 Error_Msg_Node_2 := Inner;
5467 Error_Msg_NE
5468 ("circular Instantiation: & instantiated within &!",
5469 N, Scop);
5470 return True;
5472 elsif Node (Elmt) = Inner then
5473 return True;
5475 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
5476 Error_Msg_Node_2 := Inner;
5477 Error_Msg_NE
5478 ("circular Instantiation: & instantiated within &!",
5479 N, Node (Elmt));
5480 return True;
5481 end if;
5483 Next_Elmt (Elmt);
5484 end loop;
5486 -- Indicate that Inner is being instantiated within Scop
5488 Append_Elmt (Inner, Inner_Instances (Scop));
5489 end if;
5491 if Scop = Standard_Standard then
5492 exit;
5493 else
5494 Scop := Scope (Scop);
5495 end if;
5496 end loop;
5498 return False;
5499 end Contains_Instance_Of;
5501 -----------------------
5502 -- Copy_Generic_Node --
5503 -----------------------
5505 function Copy_Generic_Node
5506 (N : Node_Id;
5507 Parent_Id : Node_Id;
5508 Instantiating : Boolean) return Node_Id
5510 Ent : Entity_Id;
5511 New_N : Node_Id;
5513 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
5514 -- Check the given value of one of the Fields referenced by the
5515 -- current node to determine whether to copy it recursively. The
5516 -- field may hold a Node_Id, a List_Id, or an Elist_Id, or a plain
5517 -- value (Sloc, Uint, Char) in which case it need not be copied.
5519 procedure Copy_Descendants;
5520 -- Common utility for various nodes
5522 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
5523 -- Make copy of element list
5525 function Copy_Generic_List
5526 (L : List_Id;
5527 Parent_Id : Node_Id) return List_Id;
5528 -- Apply Copy_Node recursively to the members of a node list
5530 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
5531 -- True if an identifier is part of the defining program unit name
5532 -- of a child unit. The entity of such an identifier must be kept
5533 -- (for ASIS use) even though as the name of an enclosing generic
5534 -- it would otherwise not be preserved in the generic tree.
5536 ----------------------
5537 -- Copy_Descendants --
5538 ----------------------
5540 procedure Copy_Descendants is
5542 use Atree.Unchecked_Access;
5543 -- This code section is part of the implementation of an untyped
5544 -- tree traversal, so it needs direct access to node fields.
5546 begin
5547 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
5548 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
5549 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
5550 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
5551 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
5552 end Copy_Descendants;
5554 -----------------------------
5555 -- Copy_Generic_Descendant --
5556 -----------------------------
5558 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
5559 begin
5560 if D = Union_Id (Empty) then
5561 return D;
5563 elsif D in Node_Range then
5564 return Union_Id
5565 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
5567 elsif D in List_Range then
5568 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
5570 elsif D in Elist_Range then
5571 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
5573 -- Nothing else is copyable (e.g. Uint values), return as is
5575 else
5576 return D;
5577 end if;
5578 end Copy_Generic_Descendant;
5580 ------------------------
5581 -- Copy_Generic_Elist --
5582 ------------------------
5584 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
5585 M : Elmt_Id;
5586 L : Elist_Id;
5588 begin
5589 if Present (E) then
5590 L := New_Elmt_List;
5591 M := First_Elmt (E);
5592 while Present (M) loop
5593 Append_Elmt
5594 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
5595 Next_Elmt (M);
5596 end loop;
5598 return L;
5600 else
5601 return No_Elist;
5602 end if;
5603 end Copy_Generic_Elist;
5605 -----------------------
5606 -- Copy_Generic_List --
5607 -----------------------
5609 function Copy_Generic_List
5610 (L : List_Id;
5611 Parent_Id : Node_Id) return List_Id
5613 N : Node_Id;
5614 New_L : List_Id;
5616 begin
5617 if Present (L) then
5618 New_L := New_List;
5619 Set_Parent (New_L, Parent_Id);
5621 N := First (L);
5622 while Present (N) loop
5623 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
5624 Next (N);
5625 end loop;
5627 return New_L;
5629 else
5630 return No_List;
5631 end if;
5632 end Copy_Generic_List;
5634 ---------------------------
5635 -- In_Defining_Unit_Name --
5636 ---------------------------
5638 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
5639 begin
5640 return Present (Parent (Nam))
5641 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
5642 or else
5643 (Nkind (Parent (Nam)) = N_Expanded_Name
5644 and then In_Defining_Unit_Name (Parent (Nam))));
5645 end In_Defining_Unit_Name;
5647 -- Start of processing for Copy_Generic_Node
5649 begin
5650 if N = Empty then
5651 return N;
5652 end if;
5654 New_N := New_Copy (N);
5656 if Instantiating then
5657 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
5658 end if;
5660 if not Is_List_Member (N) then
5661 Set_Parent (New_N, Parent_Id);
5662 end if;
5664 -- If defining identifier, then all fields have been copied already
5666 if Nkind (New_N) in N_Entity then
5667 null;
5669 -- Special casing for identifiers and other entity names and operators
5671 elsif Nkind_In (New_N, N_Identifier,
5672 N_Character_Literal,
5673 N_Expanded_Name,
5674 N_Operator_Symbol)
5675 or else Nkind (New_N) in N_Op
5676 then
5677 if not Instantiating then
5679 -- Link both nodes in order to assign subsequently the
5680 -- entity of the copy to the original node, in case this
5681 -- is a global reference.
5683 Set_Associated_Node (N, New_N);
5685 -- If we are within an instantiation, this is a nested generic
5686 -- that has already been analyzed at the point of definition. We
5687 -- must preserve references that were global to the enclosing
5688 -- parent at that point. Other occurrences, whether global or
5689 -- local to the current generic, must be resolved anew, so we
5690 -- reset the entity in the generic copy. A global reference has a
5691 -- smaller depth than the parent, or else the same depth in case
5692 -- both are distinct compilation units.
5693 -- A child unit is implicitly declared within the enclosing parent
5694 -- but is in fact global to it, and must be preserved.
5696 -- It is also possible for Current_Instantiated_Parent to be
5697 -- defined, and for this not to be a nested generic, namely if the
5698 -- unit is loaded through Rtsfind. In that case, the entity of
5699 -- New_N is only a link to the associated node, and not a defining
5700 -- occurrence.
5702 -- The entities for parent units in the defining_program_unit of a
5703 -- generic child unit are established when the context of the unit
5704 -- is first analyzed, before the generic copy is made. They are
5705 -- preserved in the copy for use in ASIS queries.
5707 Ent := Entity (New_N);
5709 if No (Current_Instantiated_Parent.Gen_Id) then
5710 if No (Ent)
5711 or else Nkind (Ent) /= N_Defining_Identifier
5712 or else not In_Defining_Unit_Name (N)
5713 then
5714 Set_Associated_Node (New_N, Empty);
5715 end if;
5717 elsif No (Ent)
5718 or else
5719 not Nkind_In (Ent, N_Defining_Identifier,
5720 N_Defining_Character_Literal,
5721 N_Defining_Operator_Symbol)
5722 or else No (Scope (Ent))
5723 or else
5724 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
5725 and then not Is_Child_Unit (Ent))
5726 or else
5727 (Scope_Depth (Scope (Ent)) >
5728 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
5729 and then
5730 Get_Source_Unit (Ent) =
5731 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
5732 then
5733 Set_Associated_Node (New_N, Empty);
5734 end if;
5736 -- Case of instantiating identifier or some other name or operator
5738 else
5739 -- If the associated node is still defined, the entity in it is
5740 -- global, and must be copied to the instance. If this copy is
5741 -- being made for a body to inline, it is applied to an
5742 -- instantiated tree, and the entity is already present and must
5743 -- be also preserved.
5745 declare
5746 Assoc : constant Node_Id := Get_Associated_Node (N);
5748 begin
5749 if Present (Assoc) then
5750 if Nkind (Assoc) = Nkind (N) then
5751 Set_Entity (New_N, Entity (Assoc));
5752 Check_Private_View (N);
5754 elsif Nkind (Assoc) = N_Function_Call then
5755 Set_Entity (New_N, Entity (Name (Assoc)));
5757 elsif Nkind_In (Assoc, N_Defining_Identifier,
5758 N_Defining_Character_Literal,
5759 N_Defining_Operator_Symbol)
5760 and then Expander_Active
5761 then
5762 -- Inlining case: we are copying a tree that contains
5763 -- global entities, which are preserved in the copy to be
5764 -- used for subsequent inlining.
5766 null;
5768 else
5769 Set_Entity (New_N, Empty);
5770 end if;
5771 end if;
5772 end;
5773 end if;
5775 -- For expanded name, we must copy the Prefix and Selector_Name
5777 if Nkind (N) = N_Expanded_Name then
5778 Set_Prefix
5779 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
5781 Set_Selector_Name (New_N,
5782 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
5784 -- For operators, we must copy the right operand
5786 elsif Nkind (N) in N_Op then
5787 Set_Right_Opnd (New_N,
5788 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
5790 -- And for binary operators, the left operand as well
5792 if Nkind (N) in N_Binary_Op then
5793 Set_Left_Opnd (New_N,
5794 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
5795 end if;
5796 end if;
5798 -- Special casing for stubs
5800 elsif Nkind (N) in N_Body_Stub then
5802 -- In any case, we must copy the specification or defining
5803 -- identifier as appropriate.
5805 if Nkind (N) = N_Subprogram_Body_Stub then
5806 Set_Specification (New_N,
5807 Copy_Generic_Node (Specification (N), New_N, Instantiating));
5809 else
5810 Set_Defining_Identifier (New_N,
5811 Copy_Generic_Node
5812 (Defining_Identifier (N), New_N, Instantiating));
5813 end if;
5815 -- If we are not instantiating, then this is where we load and
5816 -- analyze subunits, i.e. at the point where the stub occurs. A
5817 -- more permissible system might defer this analysis to the point
5818 -- of instantiation, but this seems to complicated for now.
5820 if not Instantiating then
5821 declare
5822 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
5823 Subunit : Node_Id;
5824 Unum : Unit_Number_Type;
5825 New_Body : Node_Id;
5827 begin
5828 Unum :=
5829 Load_Unit
5830 (Load_Name => Subunit_Name,
5831 Required => False,
5832 Subunit => True,
5833 Error_Node => N);
5835 -- If the proper body is not found, a warning message will be
5836 -- emitted when analyzing the stub, or later at the point
5837 -- of instantiation. Here we just leave the stub as is.
5839 if Unum = No_Unit then
5840 Subunits_Missing := True;
5841 goto Subunit_Not_Found;
5842 end if;
5844 Subunit := Cunit (Unum);
5846 if Nkind (Unit (Subunit)) /= N_Subunit then
5847 Error_Msg_N
5848 ("found child unit instead of expected SEPARATE subunit",
5849 Subunit);
5850 Error_Msg_Sloc := Sloc (N);
5851 Error_Msg_N ("\to complete stub #", Subunit);
5852 goto Subunit_Not_Found;
5853 end if;
5855 -- We must create a generic copy of the subunit, in order to
5856 -- perform semantic analysis on it, and we must replace the
5857 -- stub in the original generic unit with the subunit, in order
5858 -- to preserve non-local references within.
5860 -- Only the proper body needs to be copied. Library_Unit and
5861 -- context clause are simply inherited by the generic copy.
5862 -- Note that the copy (which may be recursive if there are
5863 -- nested subunits) must be done first, before attaching it to
5864 -- the enclosing generic.
5866 New_Body :=
5867 Copy_Generic_Node
5868 (Proper_Body (Unit (Subunit)),
5869 Empty, Instantiating => False);
5871 -- Now place the original proper body in the original generic
5872 -- unit. This is a body, not a compilation unit.
5874 Rewrite (N, Proper_Body (Unit (Subunit)));
5875 Set_Is_Compilation_Unit (Defining_Entity (N), False);
5876 Set_Was_Originally_Stub (N);
5878 -- Finally replace the body of the subunit with its copy, and
5879 -- make this new subunit into the library unit of the generic
5880 -- copy, which does not have stubs any longer.
5882 Set_Proper_Body (Unit (Subunit), New_Body);
5883 Set_Library_Unit (New_N, Subunit);
5884 Inherit_Context (Unit (Subunit), N);
5885 end;
5887 -- If we are instantiating, this must be an error case, since
5888 -- otherwise we would have replaced the stub node by the proper body
5889 -- that corresponds. So just ignore it in the copy (i.e. we have
5890 -- copied it, and that is good enough).
5892 else
5893 null;
5894 end if;
5896 <<Subunit_Not_Found>> null;
5898 -- If the node is a compilation unit, it is the subunit of a stub, which
5899 -- has been loaded already (see code below). In this case, the library
5900 -- unit field of N points to the parent unit (which is a compilation
5901 -- unit) and need not (and cannot!) be copied.
5903 -- When the proper body of the stub is analyzed, the library_unit link
5904 -- is used to establish the proper context (see sem_ch10).
5906 -- The other fields of a compilation unit are copied as usual
5908 elsif Nkind (N) = N_Compilation_Unit then
5910 -- This code can only be executed when not instantiating, because in
5911 -- the copy made for an instantiation, the compilation unit node has
5912 -- disappeared at the point that a stub is replaced by its proper
5913 -- body.
5915 pragma Assert (not Instantiating);
5917 Set_Context_Items (New_N,
5918 Copy_Generic_List (Context_Items (N), New_N));
5920 Set_Unit (New_N,
5921 Copy_Generic_Node (Unit (N), New_N, False));
5923 Set_First_Inlined_Subprogram (New_N,
5924 Copy_Generic_Node
5925 (First_Inlined_Subprogram (N), New_N, False));
5927 Set_Aux_Decls_Node (New_N,
5928 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
5930 -- For an assignment node, the assignment is known to be semantically
5931 -- legal if we are instantiating the template. This avoids incorrect
5932 -- diagnostics in generated code.
5934 elsif Nkind (N) = N_Assignment_Statement then
5936 -- Copy name and expression fields in usual manner
5938 Set_Name (New_N,
5939 Copy_Generic_Node (Name (N), New_N, Instantiating));
5941 Set_Expression (New_N,
5942 Copy_Generic_Node (Expression (N), New_N, Instantiating));
5944 if Instantiating then
5945 Set_Assignment_OK (Name (New_N), True);
5946 end if;
5948 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
5949 if not Instantiating then
5950 Set_Associated_Node (N, New_N);
5952 else
5953 if Present (Get_Associated_Node (N))
5954 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
5955 then
5956 -- In the generic the aggregate has some composite type. If at
5957 -- the point of instantiation the type has a private view,
5958 -- install the full view (and that of its ancestors, if any).
5960 declare
5961 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
5962 Rt : Entity_Id;
5964 begin
5965 if Present (T)
5966 and then Is_Private_Type (T)
5967 then
5968 Switch_View (T);
5969 end if;
5971 if Present (T)
5972 and then Is_Tagged_Type (T)
5973 and then Is_Derived_Type (T)
5974 then
5975 Rt := Root_Type (T);
5977 loop
5978 T := Etype (T);
5980 if Is_Private_Type (T) then
5981 Switch_View (T);
5982 end if;
5984 exit when T = Rt;
5985 end loop;
5986 end if;
5987 end;
5988 end if;
5989 end if;
5991 -- Do not copy the associated node, which points to
5992 -- the generic copy of the aggregate.
5994 declare
5995 use Atree.Unchecked_Access;
5996 -- This code section is part of the implementation of an untyped
5997 -- tree traversal, so it needs direct access to node fields.
5999 begin
6000 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
6001 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
6002 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
6003 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
6004 end;
6006 -- Allocators do not have an identifier denoting the access type,
6007 -- so we must locate it through the expression to check whether
6008 -- the views are consistent.
6010 elsif Nkind (N) = N_Allocator
6011 and then Nkind (Expression (N)) = N_Qualified_Expression
6012 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
6013 and then Instantiating
6014 then
6015 declare
6016 T : constant Node_Id :=
6017 Get_Associated_Node (Subtype_Mark (Expression (N)));
6018 Acc_T : Entity_Id;
6020 begin
6021 if Present (T) then
6023 -- Retrieve the allocator node in the generic copy
6025 Acc_T := Etype (Parent (Parent (T)));
6026 if Present (Acc_T)
6027 and then Is_Private_Type (Acc_T)
6028 then
6029 Switch_View (Acc_T);
6030 end if;
6031 end if;
6033 Copy_Descendants;
6034 end;
6036 -- For a proper body, we must catch the case of a proper body that
6037 -- replaces a stub. This represents the point at which a separate
6038 -- compilation unit, and hence template file, may be referenced, so we
6039 -- must make a new source instantiation entry for the template of the
6040 -- subunit, and ensure that all nodes in the subunit are adjusted using
6041 -- this new source instantiation entry.
6043 elsif Nkind (N) in N_Proper_Body then
6044 declare
6045 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
6047 begin
6048 if Instantiating and then Was_Originally_Stub (N) then
6049 Create_Instantiation_Source
6050 (Instantiation_Node,
6051 Defining_Entity (N),
6052 False,
6053 S_Adjustment);
6054 end if;
6056 -- Now copy the fields of the proper body, using the new
6057 -- adjustment factor if one was needed as per test above.
6059 Copy_Descendants;
6061 -- Restore the original adjustment factor in case changed
6063 S_Adjustment := Save_Adjustment;
6064 end;
6066 -- Don't copy Ident or Comment pragmas, since the comment belongs to the
6067 -- generic unit, not to the instantiating unit.
6069 elsif Nkind (N) = N_Pragma
6070 and then Instantiating
6071 then
6072 declare
6073 Prag_Id : constant Pragma_Id := Get_Pragma_Id (N);
6074 begin
6075 if Prag_Id = Pragma_Ident
6076 or else Prag_Id = Pragma_Comment
6077 then
6078 New_N := Make_Null_Statement (Sloc (N));
6079 else
6080 Copy_Descendants;
6081 end if;
6082 end;
6084 elsif Nkind_In (N, N_Integer_Literal,
6085 N_Real_Literal,
6086 N_String_Literal)
6087 then
6088 -- No descendant fields need traversing
6090 null;
6092 -- For the remaining nodes, copy recursively their descendants
6094 else
6095 Copy_Descendants;
6097 if Instantiating
6098 and then Nkind (N) = N_Subprogram_Body
6099 then
6100 Set_Generic_Parent (Specification (New_N), N);
6101 end if;
6102 end if;
6104 return New_N;
6105 end Copy_Generic_Node;
6107 ----------------------------
6108 -- Denotes_Formal_Package --
6109 ----------------------------
6111 function Denotes_Formal_Package
6112 (Pack : Entity_Id;
6113 On_Exit : Boolean := False) return Boolean
6115 Par : Entity_Id;
6116 Scop : constant Entity_Id := Scope (Pack);
6117 E : Entity_Id;
6119 begin
6120 if On_Exit then
6121 Par :=
6122 Instance_Envs.Table
6123 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
6124 else
6125 Par := Current_Instantiated_Parent.Act_Id;
6126 end if;
6128 if Ekind (Scop) = E_Generic_Package
6129 or else Nkind (Unit_Declaration_Node (Scop)) =
6130 N_Generic_Subprogram_Declaration
6131 then
6132 return True;
6134 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
6135 N_Formal_Package_Declaration
6136 then
6137 return True;
6139 elsif No (Par) then
6140 return False;
6142 else
6143 -- Check whether this package is associated with a formal package of
6144 -- the enclosing instantiation. Iterate over the list of renamings.
6146 E := First_Entity (Par);
6147 while Present (E) loop
6148 if Ekind (E) /= E_Package
6149 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
6150 then
6151 null;
6153 elsif Renamed_Object (E) = Par then
6154 return False;
6156 elsif Renamed_Object (E) = Pack then
6157 return True;
6158 end if;
6160 Next_Entity (E);
6161 end loop;
6163 return False;
6164 end if;
6165 end Denotes_Formal_Package;
6167 -----------------
6168 -- End_Generic --
6169 -----------------
6171 procedure End_Generic is
6172 begin
6173 -- ??? More things could be factored out in this routine. Should
6174 -- probably be done at a later stage.
6176 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
6177 Generic_Flags.Decrement_Last;
6179 Expander_Mode_Restore;
6180 end End_Generic;
6182 ----------------------
6183 -- Find_Actual_Type --
6184 ----------------------
6186 function Find_Actual_Type
6187 (Typ : Entity_Id;
6188 Gen_Type : Entity_Id) return Entity_Id
6190 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
6191 T : Entity_Id;
6193 begin
6194 -- Special processing only applies to child units
6196 if not Is_Child_Unit (Gen_Scope) then
6197 return Get_Instance_Of (Typ);
6199 -- If designated or component type is itself a formal of the child unit,
6200 -- its instance is available.
6202 elsif Scope (Typ) = Gen_Scope then
6203 return Get_Instance_Of (Typ);
6205 -- If the array or access type is not declared in the parent unit,
6206 -- no special processing needed.
6208 elsif not Is_Generic_Type (Typ)
6209 and then Scope (Gen_Scope) /= Scope (Typ)
6210 then
6211 return Get_Instance_Of (Typ);
6213 -- Otherwise, retrieve designated or component type by visibility
6215 else
6216 T := Current_Entity (Typ);
6217 while Present (T) loop
6218 if In_Open_Scopes (Scope (T)) then
6219 return T;
6221 elsif Is_Generic_Actual_Type (T) then
6222 return T;
6223 end if;
6225 T := Homonym (T);
6226 end loop;
6228 return Typ;
6229 end if;
6230 end Find_Actual_Type;
6232 ----------------------------
6233 -- Freeze_Subprogram_Body --
6234 ----------------------------
6236 procedure Freeze_Subprogram_Body
6237 (Inst_Node : Node_Id;
6238 Gen_Body : Node_Id;
6239 Pack_Id : Entity_Id)
6241 F_Node : Node_Id;
6242 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
6243 Par : constant Entity_Id := Scope (Gen_Unit);
6244 Enc_G : Entity_Id;
6245 Enc_I : Node_Id;
6246 E_G_Id : Entity_Id;
6248 function Earlier (N1, N2 : Node_Id) return Boolean;
6249 -- Yields True if N1 and N2 appear in the same compilation unit,
6250 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
6251 -- traversal of the tree for the unit.
6253 function Enclosing_Body (N : Node_Id) return Node_Id;
6254 -- Find innermost package body that encloses the given node, and which
6255 -- is not a compilation unit. Freeze nodes for the instance, or for its
6256 -- enclosing body, may be inserted after the enclosing_body of the
6257 -- generic unit.
6259 function Package_Freeze_Node (B : Node_Id) return Node_Id;
6260 -- Find entity for given package body, and locate or create a freeze
6261 -- node for it.
6263 function True_Parent (N : Node_Id) return Node_Id;
6264 -- For a subunit, return parent of corresponding stub
6266 -------------
6267 -- Earlier --
6268 -------------
6270 function Earlier (N1, N2 : Node_Id) return Boolean is
6271 D1 : Integer := 0;
6272 D2 : Integer := 0;
6273 P1 : Node_Id := N1;
6274 P2 : Node_Id := N2;
6276 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
6277 -- Find distance from given node to enclosing compilation unit
6279 ----------------
6280 -- Find_Depth --
6281 ----------------
6283 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
6284 begin
6285 while Present (P)
6286 and then Nkind (P) /= N_Compilation_Unit
6287 loop
6288 P := True_Parent (P);
6289 D := D + 1;
6290 end loop;
6291 end Find_Depth;
6293 -- Start of processing for Earlier
6295 begin
6296 Find_Depth (P1, D1);
6297 Find_Depth (P2, D2);
6299 if P1 /= P2 then
6300 return False;
6301 else
6302 P1 := N1;
6303 P2 := N2;
6304 end if;
6306 while D1 > D2 loop
6307 P1 := True_Parent (P1);
6308 D1 := D1 - 1;
6309 end loop;
6311 while D2 > D1 loop
6312 P2 := True_Parent (P2);
6313 D2 := D2 - 1;
6314 end loop;
6316 -- At this point P1 and P2 are at the same distance from the root.
6317 -- We examine their parents until we find a common declarative
6318 -- list, at which point we can establish their relative placement
6319 -- by comparing their ultimate slocs. If we reach the root,
6320 -- N1 and N2 do not descend from the same declarative list (e.g.
6321 -- one is nested in the declarative part and the other is in a block
6322 -- in the statement part) and the earlier one is already frozen.
6324 while not Is_List_Member (P1)
6325 or else not Is_List_Member (P2)
6326 or else List_Containing (P1) /= List_Containing (P2)
6327 loop
6328 P1 := True_Parent (P1);
6329 P2 := True_Parent (P2);
6331 if Nkind (Parent (P1)) = N_Subunit then
6332 P1 := Corresponding_Stub (Parent (P1));
6333 end if;
6335 if Nkind (Parent (P2)) = N_Subunit then
6336 P2 := Corresponding_Stub (Parent (P2));
6337 end if;
6339 if P1 = P2 then
6340 return False;
6341 end if;
6342 end loop;
6344 return
6345 Top_Level_Location (Sloc (P1)) < Top_Level_Location (Sloc (P2));
6346 end Earlier;
6348 --------------------
6349 -- Enclosing_Body --
6350 --------------------
6352 function Enclosing_Body (N : Node_Id) return Node_Id is
6353 P : Node_Id := Parent (N);
6355 begin
6356 while Present (P)
6357 and then Nkind (Parent (P)) /= N_Compilation_Unit
6358 loop
6359 if Nkind (P) = N_Package_Body then
6361 if Nkind (Parent (P)) = N_Subunit then
6362 return Corresponding_Stub (Parent (P));
6363 else
6364 return P;
6365 end if;
6366 end if;
6368 P := True_Parent (P);
6369 end loop;
6371 return Empty;
6372 end Enclosing_Body;
6374 -------------------------
6375 -- Package_Freeze_Node --
6376 -------------------------
6378 function Package_Freeze_Node (B : Node_Id) return Node_Id is
6379 Id : Entity_Id;
6381 begin
6382 if Nkind (B) = N_Package_Body then
6383 Id := Corresponding_Spec (B);
6385 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
6386 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
6387 end if;
6389 Ensure_Freeze_Node (Id);
6390 return Freeze_Node (Id);
6391 end Package_Freeze_Node;
6393 -----------------
6394 -- True_Parent --
6395 -----------------
6397 function True_Parent (N : Node_Id) return Node_Id is
6398 begin
6399 if Nkind (Parent (N)) = N_Subunit then
6400 return Parent (Corresponding_Stub (Parent (N)));
6401 else
6402 return Parent (N);
6403 end if;
6404 end True_Parent;
6406 -- Start of processing of Freeze_Subprogram_Body
6408 begin
6409 -- If the instance and the generic body appear within the same unit, and
6410 -- the instance precedes the generic, the freeze node for the instance
6411 -- must appear after that of the generic. If the generic is nested
6412 -- within another instance I2, then current instance must be frozen
6413 -- after I2. In both cases, the freeze nodes are those of enclosing
6414 -- packages. Otherwise, the freeze node is placed at the end of the
6415 -- current declarative part.
6417 Enc_G := Enclosing_Body (Gen_Body);
6418 Enc_I := Enclosing_Body (Inst_Node);
6419 Ensure_Freeze_Node (Pack_Id);
6420 F_Node := Freeze_Node (Pack_Id);
6422 if Is_Generic_Instance (Par)
6423 and then Present (Freeze_Node (Par))
6424 and then
6425 In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
6426 then
6427 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
6429 -- The parent was a premature instantiation. Insert freeze node at
6430 -- the end the current declarative part.
6432 Insert_After_Last_Decl (Inst_Node, F_Node);
6434 else
6435 Insert_After (Freeze_Node (Par), F_Node);
6436 end if;
6438 -- The body enclosing the instance should be frozen after the body that
6439 -- includes the generic, because the body of the instance may make
6440 -- references to entities therein. If the two are not in the same
6441 -- declarative part, or if the one enclosing the instance is frozen
6442 -- already, freeze the instance at the end of the current declarative
6443 -- part.
6445 elsif Is_Generic_Instance (Par)
6446 and then Present (Freeze_Node (Par))
6447 and then Present (Enc_I)
6448 then
6449 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
6450 or else
6451 (Nkind (Enc_I) = N_Package_Body
6452 and then
6453 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
6454 then
6455 -- The enclosing package may contain several instances. Rather
6456 -- than computing the earliest point at which to insert its
6457 -- freeze node, we place it at the end of the declarative part
6458 -- of the parent of the generic.
6460 Insert_After_Last_Decl
6461 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
6462 end if;
6464 Insert_After_Last_Decl (Inst_Node, F_Node);
6466 elsif Present (Enc_G)
6467 and then Present (Enc_I)
6468 and then Enc_G /= Enc_I
6469 and then Earlier (Inst_Node, Gen_Body)
6470 then
6471 if Nkind (Enc_G) = N_Package_Body then
6472 E_G_Id := Corresponding_Spec (Enc_G);
6473 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
6474 E_G_Id :=
6475 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
6476 end if;
6478 -- Freeze package that encloses instance, and place node after
6479 -- package that encloses generic. If enclosing package is already
6480 -- frozen we have to assume it is at the proper place. This may be
6481 -- a potential ABE that requires dynamic checking. Do not add a
6482 -- freeze node if the package that encloses the generic is inside
6483 -- the body that encloses the instance, because the freeze node
6484 -- would be in the wrong scope. Additional contortions needed if
6485 -- the bodies are within a subunit.
6487 declare
6488 Enclosing_Body : Node_Id;
6490 begin
6491 if Nkind (Enc_I) = N_Package_Body_Stub then
6492 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
6493 else
6494 Enclosing_Body := Enc_I;
6495 end if;
6497 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
6498 Insert_After_Last_Decl (Enc_G, Package_Freeze_Node (Enc_I));
6499 end if;
6500 end;
6502 -- Freeze enclosing subunit before instance
6504 Ensure_Freeze_Node (E_G_Id);
6506 if not Is_List_Member (Freeze_Node (E_G_Id)) then
6507 Insert_After (Enc_G, Freeze_Node (E_G_Id));
6508 end if;
6510 Insert_After_Last_Decl (Inst_Node, F_Node);
6512 else
6513 -- If none of the above, insert freeze node at the end of the current
6514 -- declarative part.
6516 Insert_After_Last_Decl (Inst_Node, F_Node);
6517 end if;
6518 end Freeze_Subprogram_Body;
6520 ----------------
6521 -- Get_Gen_Id --
6522 ----------------
6524 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
6525 begin
6526 return Generic_Renamings.Table (E).Gen_Id;
6527 end Get_Gen_Id;
6529 ---------------------
6530 -- Get_Instance_Of --
6531 ---------------------
6533 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
6534 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
6536 begin
6537 if Res /= Assoc_Null then
6538 return Generic_Renamings.Table (Res).Act_Id;
6539 else
6540 -- On exit, entity is not instantiated: not a generic parameter, or
6541 -- else parameter of an inner generic unit.
6543 return A;
6544 end if;
6545 end Get_Instance_Of;
6547 ------------------------------------
6548 -- Get_Package_Instantiation_Node --
6549 ------------------------------------
6551 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
6552 Decl : Node_Id := Unit_Declaration_Node (A);
6553 Inst : Node_Id;
6555 begin
6556 -- If the Package_Instantiation attribute has been set on the package
6557 -- entity, then use it directly when it (or its Original_Node) refers
6558 -- to an N_Package_Instantiation node. In principle it should be
6559 -- possible to have this field set in all cases, which should be
6560 -- investigated, and would allow this function to be significantly
6561 -- simplified. ???
6563 if Present (Package_Instantiation (A)) then
6564 if Nkind (Package_Instantiation (A)) = N_Package_Instantiation then
6565 return Package_Instantiation (A);
6567 elsif Nkind (Original_Node (Package_Instantiation (A))) =
6568 N_Package_Instantiation
6569 then
6570 return Original_Node (Package_Instantiation (A));
6571 end if;
6572 end if;
6574 -- If the instantiation is a compilation unit that does not need body
6575 -- then the instantiation node has been rewritten as a package
6576 -- declaration for the instance, and we return the original node.
6578 -- If it is a compilation unit and the instance node has not been
6579 -- rewritten, then it is still the unit of the compilation. Finally, if
6580 -- a body is present, this is a parent of the main unit whose body has
6581 -- been compiled for inlining purposes, and the instantiation node has
6582 -- been rewritten with the instance body.
6584 -- Otherwise the instantiation node appears after the declaration. If
6585 -- the entity is a formal package, the declaration may have been
6586 -- rewritten as a generic declaration (in the case of a formal with box)
6587 -- or left as a formal package declaration if it has actuals, and is
6588 -- found with a forward search.
6590 if Nkind (Parent (Decl)) = N_Compilation_Unit then
6591 if Nkind (Decl) = N_Package_Declaration
6592 and then Present (Corresponding_Body (Decl))
6593 then
6594 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
6595 end if;
6597 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
6598 return Original_Node (Decl);
6599 else
6600 return Unit (Parent (Decl));
6601 end if;
6603 elsif Nkind (Decl) = N_Package_Declaration
6604 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
6605 then
6606 return Original_Node (Decl);
6608 else
6609 Inst := Next (Decl);
6610 while not Nkind_In (Inst, N_Package_Instantiation,
6611 N_Formal_Package_Declaration)
6612 loop
6613 Next (Inst);
6614 end loop;
6616 return Inst;
6617 end if;
6618 end Get_Package_Instantiation_Node;
6620 ------------------------
6621 -- Has_Been_Exchanged --
6622 ------------------------
6624 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
6625 Next : Elmt_Id;
6627 begin
6628 Next := First_Elmt (Exchanged_Views);
6629 while Present (Next) loop
6630 if Full_View (Node (Next)) = E then
6631 return True;
6632 end if;
6634 Next_Elmt (Next);
6635 end loop;
6637 return False;
6638 end Has_Been_Exchanged;
6640 ----------
6641 -- Hash --
6642 ----------
6644 function Hash (F : Entity_Id) return HTable_Range is
6645 begin
6646 return HTable_Range (F mod HTable_Size);
6647 end Hash;
6649 ------------------------
6650 -- Hide_Current_Scope --
6651 ------------------------
6653 procedure Hide_Current_Scope is
6654 C : constant Entity_Id := Current_Scope;
6655 E : Entity_Id;
6657 begin
6658 Set_Is_Hidden_Open_Scope (C);
6660 E := First_Entity (C);
6661 while Present (E) loop
6662 if Is_Immediately_Visible (E) then
6663 Set_Is_Immediately_Visible (E, False);
6664 Append_Elmt (E, Hidden_Entities);
6665 end if;
6667 Next_Entity (E);
6668 end loop;
6670 -- Make the scope name invisible as well. This is necessary, but might
6671 -- conflict with calls to Rtsfind later on, in case the scope is a
6672 -- predefined one. There is no clean solution to this problem, so for
6673 -- now we depend on the user not redefining Standard itself in one of
6674 -- the parent units.
6676 if Is_Immediately_Visible (C)
6677 and then C /= Standard_Standard
6678 then
6679 Set_Is_Immediately_Visible (C, False);
6680 Append_Elmt (C, Hidden_Entities);
6681 end if;
6683 end Hide_Current_Scope;
6685 --------------
6686 -- Init_Env --
6687 --------------
6689 procedure Init_Env is
6690 Saved : Instance_Env;
6692 begin
6693 Saved.Instantiated_Parent := Current_Instantiated_Parent;
6694 Saved.Exchanged_Views := Exchanged_Views;
6695 Saved.Hidden_Entities := Hidden_Entities;
6696 Saved.Current_Sem_Unit := Current_Sem_Unit;
6697 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
6698 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
6700 -- Save configuration switches. These may be reset if the unit is a
6701 -- predefined unit, and the current mode is not Ada 2005.
6703 Save_Opt_Config_Switches (Saved.Switches);
6705 Instance_Envs.Append (Saved);
6707 Exchanged_Views := New_Elmt_List;
6708 Hidden_Entities := New_Elmt_List;
6710 -- Make dummy entry for Instantiated parent. If generic unit is legal,
6711 -- this is set properly in Set_Instance_Env.
6713 Current_Instantiated_Parent :=
6714 (Current_Scope, Current_Scope, Assoc_Null);
6715 end Init_Env;
6717 ------------------------------
6718 -- In_Same_Declarative_Part --
6719 ------------------------------
6721 function In_Same_Declarative_Part
6722 (F_Node : Node_Id;
6723 Inst : Node_Id) return Boolean
6725 Decls : constant Node_Id := Parent (F_Node);
6726 Nod : Node_Id := Parent (Inst);
6728 begin
6729 while Present (Nod) loop
6730 if Nod = Decls then
6731 return True;
6733 elsif Nkind_In (Nod, N_Subprogram_Body,
6734 N_Package_Body,
6735 N_Task_Body,
6736 N_Protected_Body,
6737 N_Block_Statement)
6738 then
6739 return False;
6741 elsif Nkind (Nod) = N_Subunit then
6742 Nod := Corresponding_Stub (Nod);
6744 elsif Nkind (Nod) = N_Compilation_Unit then
6745 return False;
6747 else
6748 Nod := Parent (Nod);
6749 end if;
6750 end loop;
6752 return False;
6753 end In_Same_Declarative_Part;
6755 ---------------------
6756 -- In_Main_Context --
6757 ---------------------
6759 function In_Main_Context (E : Entity_Id) return Boolean is
6760 Context : List_Id;
6761 Clause : Node_Id;
6762 Nam : Node_Id;
6764 begin
6765 if not Is_Compilation_Unit (E)
6766 or else Ekind (E) /= E_Package
6767 or else In_Private_Part (E)
6768 then
6769 return False;
6770 end if;
6772 Context := Context_Items (Cunit (Main_Unit));
6774 Clause := First (Context);
6775 while Present (Clause) loop
6776 if Nkind (Clause) = N_With_Clause then
6777 Nam := Name (Clause);
6779 -- If the current scope is part of the context of the main unit,
6780 -- analysis of the corresponding with_clause is not complete, and
6781 -- the entity is not set. We use the Chars field directly, which
6782 -- might produce false positives in rare cases, but guarantees
6783 -- that we produce all the instance bodies we will need.
6785 if (Is_Entity_Name (Nam)
6786 and then Chars (Nam) = Chars (E))
6787 or else (Nkind (Nam) = N_Selected_Component
6788 and then Chars (Selector_Name (Nam)) = Chars (E))
6789 then
6790 return True;
6791 end if;
6792 end if;
6794 Next (Clause);
6795 end loop;
6797 return False;
6798 end In_Main_Context;
6800 ---------------------
6801 -- Inherit_Context --
6802 ---------------------
6804 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
6805 Current_Context : List_Id;
6806 Current_Unit : Node_Id;
6807 Item : Node_Id;
6808 New_I : Node_Id;
6810 begin
6811 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
6813 -- The inherited context is attached to the enclosing compilation
6814 -- unit. This is either the main unit, or the declaration for the
6815 -- main unit (in case the instantiation appears within the package
6816 -- declaration and the main unit is its body).
6818 Current_Unit := Parent (Inst);
6819 while Present (Current_Unit)
6820 and then Nkind (Current_Unit) /= N_Compilation_Unit
6821 loop
6822 Current_Unit := Parent (Current_Unit);
6823 end loop;
6825 Current_Context := Context_Items (Current_Unit);
6827 Item := First (Context_Items (Parent (Gen_Decl)));
6828 while Present (Item) loop
6829 if Nkind (Item) = N_With_Clause then
6830 New_I := New_Copy (Item);
6831 Set_Implicit_With (New_I, True);
6832 Append (New_I, Current_Context);
6833 end if;
6835 Next (Item);
6836 end loop;
6837 end if;
6838 end Inherit_Context;
6840 ----------------
6841 -- Initialize --
6842 ----------------
6844 procedure Initialize is
6845 begin
6846 Generic_Renamings.Init;
6847 Instance_Envs.Init;
6848 Generic_Flags.Init;
6849 Generic_Renamings_HTable.Reset;
6850 Circularity_Detected := False;
6851 Exchanged_Views := No_Elist;
6852 Hidden_Entities := No_Elist;
6853 end Initialize;
6855 ----------------------------
6856 -- Insert_After_Last_Decl --
6857 ----------------------------
6859 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id) is
6860 L : List_Id := List_Containing (N);
6861 P : constant Node_Id := Parent (L);
6863 begin
6864 if not Is_List_Member (F_Node) then
6865 if Nkind (P) = N_Package_Specification
6866 and then L = Visible_Declarations (P)
6867 and then Present (Private_Declarations (P))
6868 and then not Is_Empty_List (Private_Declarations (P))
6869 then
6870 L := Private_Declarations (P);
6871 end if;
6873 Insert_After (Last (L), F_Node);
6874 end if;
6875 end Insert_After_Last_Decl;
6877 ------------------
6878 -- Install_Body --
6879 ------------------
6881 procedure Install_Body
6882 (Act_Body : Node_Id;
6883 N : Node_Id;
6884 Gen_Body : Node_Id;
6885 Gen_Decl : Node_Id)
6887 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
6888 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
6889 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
6890 Par : constant Entity_Id := Scope (Gen_Id);
6891 Gen_Unit : constant Node_Id :=
6892 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
6893 Orig_Body : Node_Id := Gen_Body;
6894 F_Node : Node_Id;
6895 Body_Unit : Node_Id;
6897 Must_Delay : Boolean;
6899 function Enclosing_Subp (Id : Entity_Id) return Entity_Id;
6900 -- Find subprogram (if any) that encloses instance and/or generic body
6902 function True_Sloc (N : Node_Id) return Source_Ptr;
6903 -- If the instance is nested inside a generic unit, the Sloc of the
6904 -- instance indicates the place of the original definition, not the
6905 -- point of the current enclosing instance. Pending a better usage of
6906 -- Slocs to indicate instantiation places, we determine the place of
6907 -- origin of a node by finding the maximum sloc of any ancestor node.
6908 -- Why is this not equivalent to Top_Level_Location ???
6910 --------------------
6911 -- Enclosing_Subp --
6912 --------------------
6914 function Enclosing_Subp (Id : Entity_Id) return Entity_Id is
6915 Scop : Entity_Id := Scope (Id);
6917 begin
6918 while Scop /= Standard_Standard
6919 and then not Is_Overloadable (Scop)
6920 loop
6921 Scop := Scope (Scop);
6922 end loop;
6924 return Scop;
6925 end Enclosing_Subp;
6927 ---------------
6928 -- True_Sloc --
6929 ---------------
6931 function True_Sloc (N : Node_Id) return Source_Ptr is
6932 Res : Source_Ptr;
6933 N1 : Node_Id;
6935 begin
6936 Res := Sloc (N);
6937 N1 := N;
6938 while Present (N1) and then N1 /= Act_Unit loop
6939 if Sloc (N1) > Res then
6940 Res := Sloc (N1);
6941 end if;
6943 N1 := Parent (N1);
6944 end loop;
6946 return Res;
6947 end True_Sloc;
6949 -- Start of processing for Install_Body
6951 begin
6953 -- If the body is a subunit, the freeze point is the corresponding
6954 -- stub in the current compilation, not the subunit itself.
6956 if Nkind (Parent (Gen_Body)) = N_Subunit then
6957 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
6958 else
6959 Orig_Body := Gen_Body;
6960 end if;
6962 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
6964 -- If the instantiation and the generic definition appear in the same
6965 -- package declaration, this is an early instantiation. If they appear
6966 -- in the same declarative part, it is an early instantiation only if
6967 -- the generic body appears textually later, and the generic body is
6968 -- also in the main unit.
6970 -- If instance is nested within a subprogram, and the generic body is
6971 -- not, the instance is delayed because the enclosing body is. If
6972 -- instance and body are within the same scope, or the same sub-
6973 -- program body, indicate explicitly that the instance is delayed.
6975 Must_Delay :=
6976 (Gen_Unit = Act_Unit
6977 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
6978 N_Generic_Package_Declaration)
6979 or else (Gen_Unit = Body_Unit
6980 and then True_Sloc (N) < Sloc (Orig_Body)))
6981 and then Is_In_Main_Unit (Gen_Unit)
6982 and then (Scope (Act_Id) = Scope (Gen_Id)
6983 or else
6984 Enclosing_Subp (Act_Id) = Enclosing_Subp (Gen_Id)));
6986 -- If this is an early instantiation, the freeze node is placed after
6987 -- the generic body. Otherwise, if the generic appears in an instance,
6988 -- we cannot freeze the current instance until the outer one is frozen.
6989 -- This is only relevant if the current instance is nested within some
6990 -- inner scope not itself within the outer instance. If this scope is
6991 -- a package body in the same declarative part as the outer instance,
6992 -- then that body needs to be frozen after the outer instance. Finally,
6993 -- if no delay is needed, we place the freeze node at the end of the
6994 -- current declarative part.
6996 if Expander_Active then
6997 Ensure_Freeze_Node (Act_Id);
6998 F_Node := Freeze_Node (Act_Id);
7000 if Must_Delay then
7001 Insert_After (Orig_Body, F_Node);
7003 elsif Is_Generic_Instance (Par)
7004 and then Present (Freeze_Node (Par))
7005 and then Scope (Act_Id) /= Par
7006 then
7007 -- Freeze instance of inner generic after instance of enclosing
7008 -- generic.
7010 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
7011 Insert_After (Freeze_Node (Par), F_Node);
7013 -- Freeze package enclosing instance of inner generic after
7014 -- instance of enclosing generic.
7016 elsif Nkind (Parent (N)) = N_Package_Body
7017 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
7018 then
7020 declare
7021 Enclosing : constant Entity_Id :=
7022 Corresponding_Spec (Parent (N));
7024 begin
7025 Insert_After_Last_Decl (N, F_Node);
7026 Ensure_Freeze_Node (Enclosing);
7028 if not Is_List_Member (Freeze_Node (Enclosing)) then
7029 Insert_After (Freeze_Node (Par), Freeze_Node (Enclosing));
7030 end if;
7031 end;
7033 else
7034 Insert_After_Last_Decl (N, F_Node);
7035 end if;
7037 else
7038 Insert_After_Last_Decl (N, F_Node);
7039 end if;
7040 end if;
7042 Set_Is_Frozen (Act_Id);
7043 Insert_Before (N, Act_Body);
7044 Mark_Rewrite_Insertion (Act_Body);
7045 end Install_Body;
7047 -----------------------------
7048 -- Install_Formal_Packages --
7049 -----------------------------
7051 procedure Install_Formal_Packages (Par : Entity_Id) is
7052 E : Entity_Id;
7054 begin
7055 E := First_Entity (Par);
7056 while Present (E) loop
7057 if Ekind (E) = E_Package
7058 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
7059 then
7060 -- If this is the renaming for the parent instance, done
7062 if Renamed_Object (E) = Par then
7063 exit;
7065 -- The visibility of a formal of an enclosing generic is
7066 -- already correct.
7068 elsif Denotes_Formal_Package (E) then
7069 null;
7071 elsif Present (Associated_Formal_Package (E))
7072 and then Box_Present (Parent (Associated_Formal_Package (E)))
7073 then
7074 Check_Generic_Actuals (Renamed_Object (E), True);
7075 Set_Is_Hidden (E, False);
7076 end if;
7077 end if;
7079 Next_Entity (E);
7080 end loop;
7081 end Install_Formal_Packages;
7083 --------------------
7084 -- Install_Parent --
7085 --------------------
7087 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
7088 Ancestors : constant Elist_Id := New_Elmt_List;
7089 S : constant Entity_Id := Current_Scope;
7090 Inst_Par : Entity_Id;
7091 First_Par : Entity_Id;
7092 Inst_Node : Node_Id;
7093 Gen_Par : Entity_Id;
7094 First_Gen : Entity_Id;
7095 Elmt : Elmt_Id;
7097 procedure Install_Noninstance_Specs (Par : Entity_Id);
7098 -- Install the scopes of noninstance parent units ending with Par
7100 procedure Install_Spec (Par : Entity_Id);
7101 -- The child unit is within the declarative part of the parent, so
7102 -- the declarations within the parent are immediately visible.
7104 -------------------------------
7105 -- Install_Noninstance_Specs --
7106 -------------------------------
7108 procedure Install_Noninstance_Specs (Par : Entity_Id) is
7109 begin
7110 if Present (Par)
7111 and then Par /= Standard_Standard
7112 and then not In_Open_Scopes (Par)
7113 then
7114 Install_Noninstance_Specs (Scope (Par));
7115 Install_Spec (Par);
7116 end if;
7117 end Install_Noninstance_Specs;
7119 ------------------
7120 -- Install_Spec --
7121 ------------------
7123 procedure Install_Spec (Par : Entity_Id) is
7124 Spec : constant Node_Id :=
7125 Specification (Unit_Declaration_Node (Par));
7127 begin
7128 -- If this parent of the child instance is a top-level unit,
7129 -- then record the unit and its visibility for later resetting
7130 -- in Remove_Parent. We exclude units that are generic instances,
7131 -- as we only want to record this information for the ultimate
7132 -- top-level noninstance parent (is that always correct???).
7134 if Scope (Par) = Standard_Standard
7135 and then not Is_Generic_Instance (Par)
7136 then
7137 Parent_Unit_Visible := Is_Immediately_Visible (Par);
7138 Instance_Parent_Unit := Par;
7139 end if;
7141 -- Open the parent scope and make it and its declarations visible.
7142 -- If this point is not within a body, then only the visible
7143 -- declarations should be made visible, and installation of the
7144 -- private declarations is deferred until the appropriate point
7145 -- within analysis of the spec being instantiated (see the handling
7146 -- of parent visibility in Analyze_Package_Specification). This is
7147 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
7148 -- private view problems that occur when compiling instantiations of
7149 -- a generic child of that package (Generic_Dispatching_Constructor).
7150 -- If the instance freezes a tagged type, inlinings of operations
7151 -- from Ada.Tags may need the full view of type Tag. If inlining took
7152 -- proper account of establishing visibility of inlined subprograms'
7153 -- parents then it should be possible to remove this
7154 -- special check. ???
7156 Push_Scope (Par);
7157 Set_Is_Immediately_Visible (Par);
7158 Install_Visible_Declarations (Par);
7159 Set_Use (Visible_Declarations (Spec));
7161 if In_Body or else Is_RTU (Par, Ada_Tags) then
7162 Install_Private_Declarations (Par);
7163 Set_Use (Private_Declarations (Spec));
7164 end if;
7165 end Install_Spec;
7167 -- Start of processing for Install_Parent
7169 begin
7170 -- We need to install the parent instance to compile the instantiation
7171 -- of the child, but the child instance must appear in the current
7172 -- scope. Given that we cannot place the parent above the current scope
7173 -- in the scope stack, we duplicate the current scope and unstack both
7174 -- after the instantiation is complete.
7176 -- If the parent is itself the instantiation of a child unit, we must
7177 -- also stack the instantiation of its parent, and so on. Each such
7178 -- ancestor is the prefix of the name in a prior instantiation.
7180 -- If this is a nested instance, the parent unit itself resolves to
7181 -- a renaming of the parent instance, whose declaration we need.
7183 -- Finally, the parent may be a generic (not an instance) when the
7184 -- child unit appears as a formal package.
7186 Inst_Par := P;
7188 if Present (Renamed_Entity (Inst_Par)) then
7189 Inst_Par := Renamed_Entity (Inst_Par);
7190 end if;
7192 First_Par := Inst_Par;
7194 Gen_Par :=
7195 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
7197 First_Gen := Gen_Par;
7199 while Present (Gen_Par)
7200 and then Is_Child_Unit (Gen_Par)
7201 loop
7202 -- Load grandparent instance as well
7204 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
7206 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
7207 Inst_Par := Entity (Prefix (Name (Inst_Node)));
7209 if Present (Renamed_Entity (Inst_Par)) then
7210 Inst_Par := Renamed_Entity (Inst_Par);
7211 end if;
7213 Gen_Par :=
7214 Generic_Parent
7215 (Specification (Unit_Declaration_Node (Inst_Par)));
7217 if Present (Gen_Par) then
7218 Prepend_Elmt (Inst_Par, Ancestors);
7220 else
7221 -- Parent is not the name of an instantiation
7223 Install_Noninstance_Specs (Inst_Par);
7225 exit;
7226 end if;
7228 else
7229 -- Previous error
7231 exit;
7232 end if;
7233 end loop;
7235 if Present (First_Gen) then
7236 Append_Elmt (First_Par, Ancestors);
7238 else
7239 Install_Noninstance_Specs (First_Par);
7240 end if;
7242 if not Is_Empty_Elmt_List (Ancestors) then
7243 Elmt := First_Elmt (Ancestors);
7245 while Present (Elmt) loop
7246 Install_Spec (Node (Elmt));
7247 Install_Formal_Packages (Node (Elmt));
7249 Next_Elmt (Elmt);
7250 end loop;
7251 end if;
7253 if not In_Body then
7254 Push_Scope (S);
7255 end if;
7256 end Install_Parent;
7258 --------------------------------
7259 -- Instantiate_Formal_Package --
7260 --------------------------------
7262 function Instantiate_Formal_Package
7263 (Formal : Node_Id;
7264 Actual : Node_Id;
7265 Analyzed_Formal : Node_Id) return List_Id
7267 Loc : constant Source_Ptr := Sloc (Actual);
7268 Actual_Pack : Entity_Id;
7269 Formal_Pack : Entity_Id;
7270 Gen_Parent : Entity_Id;
7271 Decls : List_Id;
7272 Nod : Node_Id;
7273 Parent_Spec : Node_Id;
7275 procedure Find_Matching_Actual
7276 (F : Node_Id;
7277 Act : in out Entity_Id);
7278 -- We need to associate each formal entity in the formal package
7279 -- with the corresponding entity in the actual package. The actual
7280 -- package has been analyzed and possibly expanded, and as a result
7281 -- there is no one-to-one correspondence between the two lists (for
7282 -- example, the actual may include subtypes, itypes, and inherited
7283 -- primitive operations, interspersed among the renaming declarations
7284 -- for the actuals) . We retrieve the corresponding actual by name
7285 -- because each actual has the same name as the formal, and they do
7286 -- appear in the same order.
7288 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
7289 -- Retrieve entity of defining entity of generic formal parameter.
7290 -- Only the declarations of formals need to be considered when
7291 -- linking them to actuals, but the declarative list may include
7292 -- internal entities generated during analysis, and those are ignored.
7294 procedure Match_Formal_Entity
7295 (Formal_Node : Node_Id;
7296 Formal_Ent : Entity_Id;
7297 Actual_Ent : Entity_Id);
7298 -- Associates the formal entity with the actual. In the case
7299 -- where Formal_Ent is a formal package, this procedure iterates
7300 -- through all of its formals and enters associations between the
7301 -- actuals occurring in the formal package's corresponding actual
7302 -- package (given by Actual_Ent) and the formal package's formal
7303 -- parameters. This procedure recurses if any of the parameters is
7304 -- itself a package.
7306 function Is_Instance_Of
7307 (Act_Spec : Entity_Id;
7308 Gen_Anc : Entity_Id) return Boolean;
7309 -- The actual can be an instantiation of a generic within another
7310 -- instance, in which case there is no direct link from it to the
7311 -- original generic ancestor. In that case, we recognize that the
7312 -- ultimate ancestor is the same by examining names and scopes.
7314 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id);
7315 -- Within the generic part, entities in the formal package are
7316 -- visible. To validate subsequent type declarations, indicate
7317 -- the correspondence between the entities in the analyzed formal,
7318 -- and the entities in the actual package. There are three packages
7319 -- involved in the instantiation of a formal package: the parent
7320 -- generic P1 which appears in the generic declaration, the fake
7321 -- instantiation P2 which appears in the analyzed generic, and whose
7322 -- visible entities may be used in subsequent formals, and the actual
7323 -- P3 in the instance. To validate subsequent formals, me indicate
7324 -- that the entities in P2 are mapped into those of P3. The mapping of
7325 -- entities has to be done recursively for nested packages.
7327 procedure Process_Nested_Formal (Formal : Entity_Id);
7328 -- If the current formal is declared with a box, its own formals are
7329 -- visible in the instance, as they were in the generic, and their
7330 -- Hidden flag must be reset. If some of these formals are themselves
7331 -- packages declared with a box, the processing must be recursive.
7333 --------------------------
7334 -- Find_Matching_Actual --
7335 --------------------------
7337 procedure Find_Matching_Actual
7338 (F : Node_Id;
7339 Act : in out Entity_Id)
7341 Formal_Ent : Entity_Id;
7343 begin
7344 case Nkind (Original_Node (F)) is
7345 when N_Formal_Object_Declaration |
7346 N_Formal_Type_Declaration =>
7347 Formal_Ent := Defining_Identifier (F);
7349 while Chars (Act) /= Chars (Formal_Ent) loop
7350 Next_Entity (Act);
7351 end loop;
7353 when N_Formal_Subprogram_Declaration |
7354 N_Formal_Package_Declaration |
7355 N_Package_Declaration |
7356 N_Generic_Package_Declaration =>
7357 Formal_Ent := Defining_Entity (F);
7359 while Chars (Act) /= Chars (Formal_Ent) loop
7360 Next_Entity (Act);
7361 end loop;
7363 when others =>
7364 raise Program_Error;
7365 end case;
7366 end Find_Matching_Actual;
7368 -------------------------
7369 -- Match_Formal_Entity --
7370 -------------------------
7372 procedure Match_Formal_Entity
7373 (Formal_Node : Node_Id;
7374 Formal_Ent : Entity_Id;
7375 Actual_Ent : Entity_Id)
7377 Act_Pkg : Entity_Id;
7379 begin
7380 Set_Instance_Of (Formal_Ent, Actual_Ent);
7382 if Ekind (Actual_Ent) = E_Package then
7384 -- Record associations for each parameter
7386 Act_Pkg := Actual_Ent;
7388 declare
7389 A_Ent : Entity_Id := First_Entity (Act_Pkg);
7390 F_Ent : Entity_Id;
7391 F_Node : Node_Id;
7393 Gen_Decl : Node_Id;
7394 Formals : List_Id;
7395 Actual : Entity_Id;
7397 begin
7398 -- Retrieve the actual given in the formal package declaration
7400 Actual := Entity (Name (Original_Node (Formal_Node)));
7402 -- The actual in the formal package declaration may be a
7403 -- renamed generic package, in which case we want to retrieve
7404 -- the original generic in order to traverse its formal part.
7406 if Present (Renamed_Entity (Actual)) then
7407 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
7408 else
7409 Gen_Decl := Unit_Declaration_Node (Actual);
7410 end if;
7412 Formals := Generic_Formal_Declarations (Gen_Decl);
7414 if Present (Formals) then
7415 F_Node := First_Non_Pragma (Formals);
7416 else
7417 F_Node := Empty;
7418 end if;
7420 while Present (A_Ent)
7421 and then Present (F_Node)
7422 and then A_Ent /= First_Private_Entity (Act_Pkg)
7423 loop
7424 F_Ent := Get_Formal_Entity (F_Node);
7426 if Present (F_Ent) then
7428 -- This is a formal of the original package. Record
7429 -- association and recurse.
7431 Find_Matching_Actual (F_Node, A_Ent);
7432 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
7433 Next_Entity (A_Ent);
7434 end if;
7436 Next_Non_Pragma (F_Node);
7437 end loop;
7438 end;
7439 end if;
7440 end Match_Formal_Entity;
7442 -----------------------
7443 -- Get_Formal_Entity --
7444 -----------------------
7446 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
7447 Kind : constant Node_Kind := Nkind (Original_Node (N));
7448 begin
7449 case Kind is
7450 when N_Formal_Object_Declaration =>
7451 return Defining_Identifier (N);
7453 when N_Formal_Type_Declaration =>
7454 return Defining_Identifier (N);
7456 when N_Formal_Subprogram_Declaration =>
7457 return Defining_Unit_Name (Specification (N));
7459 when N_Formal_Package_Declaration =>
7460 return Defining_Identifier (Original_Node (N));
7462 when N_Generic_Package_Declaration =>
7463 return Defining_Identifier (Original_Node (N));
7465 -- All other declarations are introduced by semantic analysis and
7466 -- have no match in the actual.
7468 when others =>
7469 return Empty;
7470 end case;
7471 end Get_Formal_Entity;
7473 --------------------
7474 -- Is_Instance_Of --
7475 --------------------
7477 function Is_Instance_Of
7478 (Act_Spec : Entity_Id;
7479 Gen_Anc : Entity_Id) return Boolean
7481 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
7483 begin
7484 if No (Gen_Par) then
7485 return False;
7487 -- Simplest case: the generic parent of the actual is the formal
7489 elsif Gen_Par = Gen_Anc then
7490 return True;
7492 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
7493 return False;
7495 -- The actual may be obtained through several instantiations. Its
7496 -- scope must itself be an instance of a generic declared in the
7497 -- same scope as the formal. Any other case is detected above.
7499 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
7500 return False;
7502 else
7503 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
7504 end if;
7505 end Is_Instance_Of;
7507 ------------------
7508 -- Map_Entities --
7509 ------------------
7511 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id) is
7512 E1 : Entity_Id;
7513 E2 : Entity_Id;
7515 begin
7516 Set_Instance_Of (Form, Act);
7518 -- Traverse formal and actual package to map the corresponding
7519 -- entities. We skip over internal entities that may be generated
7520 -- during semantic analysis, and find the matching entities by
7521 -- name, given that they must appear in the same order.
7523 E1 := First_Entity (Form);
7524 E2 := First_Entity (Act);
7525 while Present (E1)
7526 and then E1 /= First_Private_Entity (Form)
7527 loop
7528 -- Could this test be a single condition???
7529 -- Seems like it could, and isn't FPE (Form) a constant anyway???
7531 if not Is_Internal (E1)
7532 and then Present (Parent (E1))
7533 and then not Is_Class_Wide_Type (E1)
7534 and then not Is_Internal_Name (Chars (E1))
7535 then
7536 while Present (E2)
7537 and then Chars (E2) /= Chars (E1)
7538 loop
7539 Next_Entity (E2);
7540 end loop;
7542 if No (E2) then
7543 exit;
7544 else
7545 Set_Instance_Of (E1, E2);
7547 if Is_Type (E1)
7548 and then Is_Tagged_Type (E2)
7549 then
7550 Set_Instance_Of
7551 (Class_Wide_Type (E1), Class_Wide_Type (E2));
7552 end if;
7554 if Ekind (E1) = E_Package
7555 and then No (Renamed_Object (E1))
7556 then
7557 Map_Entities (E1, E2);
7558 end if;
7559 end if;
7560 end if;
7562 Next_Entity (E1);
7563 end loop;
7564 end Map_Entities;
7566 ---------------------------
7567 -- Process_Nested_Formal --
7568 ---------------------------
7570 procedure Process_Nested_Formal (Formal : Entity_Id) is
7571 Ent : Entity_Id;
7573 begin
7574 if Present (Associated_Formal_Package (Formal))
7575 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
7576 then
7577 Ent := First_Entity (Formal);
7578 while Present (Ent) loop
7579 Set_Is_Hidden (Ent, False);
7580 Set_Is_Visible_Formal (Ent);
7581 Set_Is_Potentially_Use_Visible
7582 (Ent, Is_Potentially_Use_Visible (Formal));
7584 if Ekind (Ent) = E_Package then
7585 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
7586 Process_Nested_Formal (Ent);
7587 end if;
7589 Next_Entity (Ent);
7590 end loop;
7591 end if;
7592 end Process_Nested_Formal;
7594 -- Start of processing for Instantiate_Formal_Package
7596 begin
7597 Analyze (Actual);
7599 if not Is_Entity_Name (Actual)
7600 or else Ekind (Entity (Actual)) /= E_Package
7601 then
7602 Error_Msg_N
7603 ("expect package instance to instantiate formal", Actual);
7604 Abandon_Instantiation (Actual);
7605 raise Program_Error;
7607 else
7608 Actual_Pack := Entity (Actual);
7609 Set_Is_Instantiated (Actual_Pack);
7611 -- The actual may be a renamed package, or an outer generic formal
7612 -- package whose instantiation is converted into a renaming.
7614 if Present (Renamed_Object (Actual_Pack)) then
7615 Actual_Pack := Renamed_Object (Actual_Pack);
7616 end if;
7618 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
7619 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
7620 Formal_Pack := Defining_Identifier (Analyzed_Formal);
7621 else
7622 Gen_Parent :=
7623 Generic_Parent (Specification (Analyzed_Formal));
7624 Formal_Pack :=
7625 Defining_Unit_Name (Specification (Analyzed_Formal));
7626 end if;
7628 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
7629 Parent_Spec := Specification (Unit_Declaration_Node (Actual_Pack));
7630 else
7631 Parent_Spec := Parent (Actual_Pack);
7632 end if;
7634 if Gen_Parent = Any_Id then
7635 Error_Msg_N
7636 ("previous error in declaration of formal package", Actual);
7637 Abandon_Instantiation (Actual);
7639 elsif
7640 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
7641 then
7642 null;
7644 else
7645 Error_Msg_NE
7646 ("actual parameter must be instance of&", Actual, Gen_Parent);
7647 Abandon_Instantiation (Actual);
7648 end if;
7650 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
7651 Map_Entities (Formal_Pack, Actual_Pack);
7653 Nod :=
7654 Make_Package_Renaming_Declaration (Loc,
7655 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
7656 Name => New_Reference_To (Actual_Pack, Loc));
7658 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
7659 Defining_Identifier (Formal));
7660 Decls := New_List (Nod);
7662 -- If the formal F has a box, then the generic declarations are
7663 -- visible in the generic G. In an instance of G, the corresponding
7664 -- entities in the actual for F (which are the actuals for the
7665 -- instantiation of the generic that F denotes) must also be made
7666 -- visible for analysis of the current instance. On exit from the
7667 -- current instance, those entities are made private again. If the
7668 -- actual is currently in use, these entities are also use-visible.
7670 -- The loop through the actual entities also steps through the formal
7671 -- entities and enters associations from formals to actuals into the
7672 -- renaming map. This is necessary to properly handle checking of
7673 -- actual parameter associations for later formals that depend on
7674 -- actuals declared in the formal package.
7676 -- In Ada 2005, partial parametrization requires that we make visible
7677 -- the actuals corresponding to formals that were defaulted in the
7678 -- formal package. There formals are identified because they remain
7679 -- formal generics within the formal package, rather than being
7680 -- renamings of the actuals supplied.
7682 declare
7683 Gen_Decl : constant Node_Id :=
7684 Unit_Declaration_Node (Gen_Parent);
7685 Formals : constant List_Id :=
7686 Generic_Formal_Declarations (Gen_Decl);
7688 Actual_Ent : Entity_Id;
7689 Actual_Of_Formal : Node_Id;
7690 Formal_Node : Node_Id;
7691 Formal_Ent : Entity_Id;
7693 begin
7694 if Present (Formals) then
7695 Formal_Node := First_Non_Pragma (Formals);
7696 else
7697 Formal_Node := Empty;
7698 end if;
7700 Actual_Ent := First_Entity (Actual_Pack);
7701 Actual_Of_Formal :=
7702 First (Visible_Declarations (Specification (Analyzed_Formal)));
7703 while Present (Actual_Ent)
7704 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
7705 loop
7706 if Present (Formal_Node) then
7707 Formal_Ent := Get_Formal_Entity (Formal_Node);
7709 if Present (Formal_Ent) then
7710 Find_Matching_Actual (Formal_Node, Actual_Ent);
7711 Match_Formal_Entity
7712 (Formal_Node, Formal_Ent, Actual_Ent);
7714 -- We iterate at the same time over the actuals of the
7715 -- local package created for the formal, to determine
7716 -- which one of the formals of the original generic were
7717 -- defaulted in the formal. The corresponding actual
7718 -- entities are visible in the enclosing instance.
7720 if Box_Present (Formal)
7721 or else
7722 (Present (Actual_Of_Formal)
7723 and then
7724 Is_Generic_Formal
7725 (Get_Formal_Entity (Actual_Of_Formal)))
7726 then
7727 Set_Is_Hidden (Actual_Ent, False);
7728 Set_Is_Visible_Formal (Actual_Ent);
7729 Set_Is_Potentially_Use_Visible
7730 (Actual_Ent, In_Use (Actual_Pack));
7732 if Ekind (Actual_Ent) = E_Package then
7733 Process_Nested_Formal (Actual_Ent);
7734 end if;
7736 else
7737 Set_Is_Hidden (Actual_Ent);
7738 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
7739 end if;
7740 end if;
7742 Next_Non_Pragma (Formal_Node);
7743 Next (Actual_Of_Formal);
7745 else
7746 -- No further formals to match, but the generic part may
7747 -- contain inherited operation that are not hidden in the
7748 -- enclosing instance.
7750 Next_Entity (Actual_Ent);
7751 end if;
7752 end loop;
7754 -- Inherited subprograms generated by formal derived types are
7755 -- also visible if the types are.
7757 Actual_Ent := First_Entity (Actual_Pack);
7758 while Present (Actual_Ent)
7759 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
7760 loop
7761 if Is_Overloadable (Actual_Ent)
7762 and then
7763 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
7764 and then
7765 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
7766 then
7767 Set_Is_Hidden (Actual_Ent, False);
7768 Set_Is_Potentially_Use_Visible
7769 (Actual_Ent, In_Use (Actual_Pack));
7770 end if;
7772 Next_Entity (Actual_Ent);
7773 end loop;
7774 end;
7776 -- If the formal is not declared with a box, reanalyze it as an
7777 -- abbreviated instantiation, to verify the matching rules of 12.7.
7778 -- The actual checks are performed after the generic associations
7779 -- have been analyzed, to guarantee the same visibility for this
7780 -- instantiation and for the actuals.
7782 -- In Ada 2005, the generic associations for the formal can include
7783 -- defaulted parameters. These are ignored during check. This
7784 -- internal instantiation is removed from the tree after conformance
7785 -- checking, because it contains formal declarations for those
7786 -- defaulted parameters, and those should not reach the back-end.
7788 if not Box_Present (Formal) then
7789 declare
7790 I_Pack : constant Entity_Id :=
7791 Make_Defining_Identifier (Sloc (Actual),
7792 Chars => New_Internal_Name ('P'));
7794 begin
7795 Set_Is_Internal (I_Pack);
7797 Append_To (Decls,
7798 Make_Package_Instantiation (Sloc (Actual),
7799 Defining_Unit_Name => I_Pack,
7800 Name =>
7801 New_Occurrence_Of
7802 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
7803 Generic_Associations =>
7804 Generic_Associations (Formal)));
7805 end;
7806 end if;
7808 return Decls;
7809 end if;
7810 end Instantiate_Formal_Package;
7812 -----------------------------------
7813 -- Instantiate_Formal_Subprogram --
7814 -----------------------------------
7816 function Instantiate_Formal_Subprogram
7817 (Formal : Node_Id;
7818 Actual : Node_Id;
7819 Analyzed_Formal : Node_Id) return Node_Id
7821 Loc : Source_Ptr;
7822 Formal_Sub : constant Entity_Id :=
7823 Defining_Unit_Name (Specification (Formal));
7824 Analyzed_S : constant Entity_Id :=
7825 Defining_Unit_Name (Specification (Analyzed_Formal));
7826 Decl_Node : Node_Id;
7827 Nam : Node_Id;
7828 New_Spec : Node_Id;
7830 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
7831 -- If the generic is a child unit, the parent has been installed on the
7832 -- scope stack, but a default subprogram cannot resolve to something on
7833 -- the parent because that parent is not really part of the visible
7834 -- context (it is there to resolve explicit local entities). If the
7835 -- default has resolved in this way, we remove the entity from
7836 -- immediate visibility and analyze the node again to emit an error
7837 -- message or find another visible candidate.
7839 procedure Valid_Actual_Subprogram (Act : Node_Id);
7840 -- Perform legality check and raise exception on failure
7842 -----------------------
7843 -- From_Parent_Scope --
7844 -----------------------
7846 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
7847 Gen_Scope : Node_Id;
7849 begin
7850 Gen_Scope := Scope (Analyzed_S);
7851 while Present (Gen_Scope)
7852 and then Is_Child_Unit (Gen_Scope)
7853 loop
7854 if Scope (Subp) = Scope (Gen_Scope) then
7855 return True;
7856 end if;
7858 Gen_Scope := Scope (Gen_Scope);
7859 end loop;
7861 return False;
7862 end From_Parent_Scope;
7864 -----------------------------
7865 -- Valid_Actual_Subprogram --
7866 -----------------------------
7868 procedure Valid_Actual_Subprogram (Act : Node_Id) is
7869 Act_E : Entity_Id;
7871 begin
7872 if Is_Entity_Name (Act) then
7873 Act_E := Entity (Act);
7875 elsif Nkind (Act) = N_Selected_Component
7876 and then Is_Entity_Name (Selector_Name (Act))
7877 then
7878 Act_E := Entity (Selector_Name (Act));
7880 else
7881 Act_E := Empty;
7882 end if;
7884 if (Present (Act_E) and then Is_Overloadable (Act_E))
7885 or else Nkind_In (Act, N_Attribute_Reference,
7886 N_Indexed_Component,
7887 N_Character_Literal,
7888 N_Explicit_Dereference)
7889 then
7890 return;
7891 end if;
7893 Error_Msg_NE
7894 ("expect subprogram or entry name in instantiation of&",
7895 Instantiation_Node, Formal_Sub);
7896 Abandon_Instantiation (Instantiation_Node);
7898 end Valid_Actual_Subprogram;
7900 -- Start of processing for Instantiate_Formal_Subprogram
7902 begin
7903 New_Spec := New_Copy_Tree (Specification (Formal));
7905 -- The tree copy has created the proper instantiation sloc for the
7906 -- new specification. Use this location for all other constructed
7907 -- declarations.
7909 Loc := Sloc (Defining_Unit_Name (New_Spec));
7911 -- Create new entity for the actual (New_Copy_Tree does not)
7913 Set_Defining_Unit_Name
7914 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
7916 -- Create new entities for the each of the formals in the
7917 -- specification of the renaming declaration built for the actual.
7919 if Present (Parameter_Specifications (New_Spec)) then
7920 declare
7921 F : Node_Id;
7922 begin
7923 F := First (Parameter_Specifications (New_Spec));
7924 while Present (F) loop
7925 Set_Defining_Identifier (F,
7926 Make_Defining_Identifier (Sloc (F),
7927 Chars => Chars (Defining_Identifier (F))));
7928 Next (F);
7929 end loop;
7930 end;
7931 end if;
7933 -- Find entity of actual. If the actual is an attribute reference, it
7934 -- cannot be resolved here (its formal is missing) but is handled
7935 -- instead in Attribute_Renaming. If the actual is overloaded, it is
7936 -- fully resolved subsequently, when the renaming declaration for the
7937 -- formal is analyzed. If it is an explicit dereference, resolve the
7938 -- prefix but not the actual itself, to prevent interpretation as call.
7940 if Present (Actual) then
7941 Loc := Sloc (Actual);
7942 Set_Sloc (New_Spec, Loc);
7944 if Nkind (Actual) = N_Operator_Symbol then
7945 Find_Direct_Name (Actual);
7947 elsif Nkind (Actual) = N_Explicit_Dereference then
7948 Analyze (Prefix (Actual));
7950 elsif Nkind (Actual) /= N_Attribute_Reference then
7951 Analyze (Actual);
7952 end if;
7954 Valid_Actual_Subprogram (Actual);
7955 Nam := Actual;
7957 elsif Present (Default_Name (Formal)) then
7958 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
7959 N_Selected_Component,
7960 N_Indexed_Component,
7961 N_Character_Literal)
7962 and then Present (Entity (Default_Name (Formal)))
7963 then
7964 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
7965 else
7966 Nam := New_Copy (Default_Name (Formal));
7967 Set_Sloc (Nam, Loc);
7968 end if;
7970 elsif Box_Present (Formal) then
7972 -- Actual is resolved at the point of instantiation. Create an
7973 -- identifier or operator with the same name as the formal.
7975 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
7976 Nam := Make_Operator_Symbol (Loc,
7977 Chars => Chars (Formal_Sub),
7978 Strval => No_String);
7979 else
7980 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
7981 end if;
7983 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
7984 and then Null_Present (Specification (Formal))
7985 then
7986 -- Generate null body for procedure, for use in the instance
7988 Decl_Node :=
7989 Make_Subprogram_Body (Loc,
7990 Specification => New_Spec,
7991 Declarations => New_List,
7992 Handled_Statement_Sequence =>
7993 Make_Handled_Sequence_Of_Statements (Loc,
7994 Statements => New_List (Make_Null_Statement (Loc))));
7996 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
7997 return Decl_Node;
7999 else
8000 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
8001 Error_Msg_NE
8002 ("missing actual&", Instantiation_Node, Formal_Sub);
8003 Error_Msg_NE
8004 ("\in instantiation of & declared#",
8005 Instantiation_Node, Scope (Analyzed_S));
8006 Abandon_Instantiation (Instantiation_Node);
8007 end if;
8009 Decl_Node :=
8010 Make_Subprogram_Renaming_Declaration (Loc,
8011 Specification => New_Spec,
8012 Name => Nam);
8014 -- If we do not have an actual and the formal specified <> then set to
8015 -- get proper default.
8017 if No (Actual) and then Box_Present (Formal) then
8018 Set_From_Default (Decl_Node);
8019 end if;
8021 -- Gather possible interpretations for the actual before analyzing the
8022 -- instance. If overloaded, it will be resolved when analyzing the
8023 -- renaming declaration.
8025 if Box_Present (Formal)
8026 and then No (Actual)
8027 then
8028 Analyze (Nam);
8030 if Is_Child_Unit (Scope (Analyzed_S))
8031 and then Present (Entity (Nam))
8032 then
8033 if not Is_Overloaded (Nam) then
8035 if From_Parent_Scope (Entity (Nam)) then
8036 Set_Is_Immediately_Visible (Entity (Nam), False);
8037 Set_Entity (Nam, Empty);
8038 Set_Etype (Nam, Empty);
8040 Analyze (Nam);
8042 Set_Is_Immediately_Visible (Entity (Nam));
8043 end if;
8045 else
8046 declare
8047 I : Interp_Index;
8048 It : Interp;
8050 begin
8051 Get_First_Interp (Nam, I, It);
8053 while Present (It.Nam) loop
8054 if From_Parent_Scope (It.Nam) then
8055 Remove_Interp (I);
8056 end if;
8058 Get_Next_Interp (I, It);
8059 end loop;
8060 end;
8061 end if;
8062 end if;
8063 end if;
8065 -- The generic instantiation freezes the actual. This can only be done
8066 -- once the actual is resolved, in the analysis of the renaming
8067 -- declaration. To make the formal subprogram entity available, we set
8068 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
8069 -- This is also needed in Analyze_Subprogram_Renaming for the processing
8070 -- of formal abstract subprograms.
8072 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
8074 -- We cannot analyze the renaming declaration, and thus find the actual,
8075 -- until all the actuals are assembled in the instance. For subsequent
8076 -- checks of other actuals, indicate the node that will hold the
8077 -- instance of this formal.
8079 Set_Instance_Of (Analyzed_S, Nam);
8081 if Nkind (Actual) = N_Selected_Component
8082 and then Is_Task_Type (Etype (Prefix (Actual)))
8083 and then not Is_Frozen (Etype (Prefix (Actual)))
8084 then
8085 -- The renaming declaration will create a body, which must appear
8086 -- outside of the instantiation, We move the renaming declaration
8087 -- out of the instance, and create an additional renaming inside,
8088 -- to prevent freezing anomalies.
8090 declare
8091 Anon_Id : constant Entity_Id :=
8092 Make_Defining_Identifier
8093 (Loc, New_Internal_Name ('E'));
8094 begin
8095 Set_Defining_Unit_Name (New_Spec, Anon_Id);
8096 Insert_Before (Instantiation_Node, Decl_Node);
8097 Analyze (Decl_Node);
8099 -- Now create renaming within the instance
8101 Decl_Node :=
8102 Make_Subprogram_Renaming_Declaration (Loc,
8103 Specification => New_Copy_Tree (New_Spec),
8104 Name => New_Occurrence_Of (Anon_Id, Loc));
8106 Set_Defining_Unit_Name (Specification (Decl_Node),
8107 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
8108 end;
8109 end if;
8111 return Decl_Node;
8112 end Instantiate_Formal_Subprogram;
8114 ------------------------
8115 -- Instantiate_Object --
8116 ------------------------
8118 function Instantiate_Object
8119 (Formal : Node_Id;
8120 Actual : Node_Id;
8121 Analyzed_Formal : Node_Id) return List_Id
8123 Acc_Def : Node_Id := Empty;
8124 Act_Assoc : constant Node_Id := Parent (Actual);
8125 Actual_Decl : Node_Id := Empty;
8126 Formal_Id : constant Entity_Id := Defining_Identifier (Formal);
8127 Decl_Node : Node_Id;
8128 Def : Node_Id;
8129 Ftyp : Entity_Id;
8130 List : constant List_Id := New_List;
8131 Loc : constant Source_Ptr := Sloc (Actual);
8132 Orig_Ftyp : constant Entity_Id :=
8133 Etype (Defining_Identifier (Analyzed_Formal));
8134 Subt_Decl : Node_Id := Empty;
8135 Subt_Mark : Node_Id := Empty;
8137 begin
8138 if Present (Subtype_Mark (Formal)) then
8139 Subt_Mark := Subtype_Mark (Formal);
8140 else
8141 Check_Access_Definition (Formal);
8142 Acc_Def := Access_Definition (Formal);
8143 end if;
8145 -- Sloc for error message on missing actual
8147 Error_Msg_Sloc := Sloc (Scope (Defining_Identifier (Analyzed_Formal)));
8149 if Get_Instance_Of (Formal_Id) /= Formal_Id then
8150 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
8151 end if;
8153 Set_Parent (List, Parent (Actual));
8155 -- OUT present
8157 if Out_Present (Formal) then
8159 -- An IN OUT generic actual must be a name. The instantiation is a
8160 -- renaming declaration. The actual is the name being renamed. We
8161 -- use the actual directly, rather than a copy, because it is not
8162 -- used further in the list of actuals, and because a copy or a use
8163 -- of relocate_node is incorrect if the instance is nested within a
8164 -- generic. In order to simplify ASIS searches, the Generic_Parent
8165 -- field links the declaration to the generic association.
8167 if No (Actual) then
8168 Error_Msg_NE
8169 ("missing actual&",
8170 Instantiation_Node, Formal_Id);
8171 Error_Msg_NE
8172 ("\in instantiation of & declared#",
8173 Instantiation_Node,
8174 Scope (Defining_Identifier (Analyzed_Formal)));
8175 Abandon_Instantiation (Instantiation_Node);
8176 end if;
8178 if Present (Subt_Mark) then
8179 Decl_Node :=
8180 Make_Object_Renaming_Declaration (Loc,
8181 Defining_Identifier => New_Copy (Formal_Id),
8182 Subtype_Mark => New_Copy_Tree (Subt_Mark),
8183 Name => Actual);
8185 else pragma Assert (Present (Acc_Def));
8186 Decl_Node :=
8187 Make_Object_Renaming_Declaration (Loc,
8188 Defining_Identifier => New_Copy (Formal_Id),
8189 Access_Definition => New_Copy_Tree (Acc_Def),
8190 Name => Actual);
8191 end if;
8193 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
8195 -- The analysis of the actual may produce insert_action nodes, so
8196 -- the declaration must have a context in which to attach them.
8198 Append (Decl_Node, List);
8199 Analyze (Actual);
8201 -- Return if the analysis of the actual reported some error
8203 if Etype (Actual) = Any_Type then
8204 return List;
8205 end if;
8207 -- This check is performed here because Analyze_Object_Renaming will
8208 -- not check it when Comes_From_Source is False. Note though that the
8209 -- check for the actual being the name of an object will be performed
8210 -- in Analyze_Object_Renaming.
8212 if Is_Object_Reference (Actual)
8213 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
8214 then
8215 Error_Msg_N
8216 ("illegal discriminant-dependent component for in out parameter",
8217 Actual);
8218 end if;
8220 -- The actual has to be resolved in order to check that it is a
8221 -- variable (due to cases such as F(1), where F returns
8222 -- access to an array, and for overloaded prefixes).
8224 Ftyp :=
8225 Get_Instance_Of (Etype (Defining_Identifier (Analyzed_Formal)));
8227 if Is_Private_Type (Ftyp)
8228 and then not Is_Private_Type (Etype (Actual))
8229 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
8230 or else Base_Type (Etype (Actual)) = Ftyp)
8231 then
8232 -- If the actual has the type of the full view of the formal, or
8233 -- else a non-private subtype of the formal, then the visibility
8234 -- of the formal type has changed. Add to the actuals a subtype
8235 -- declaration that will force the exchange of views in the body
8236 -- of the instance as well.
8238 Subt_Decl :=
8239 Make_Subtype_Declaration (Loc,
8240 Defining_Identifier =>
8241 Make_Defining_Identifier (Loc, New_Internal_Name ('P')),
8242 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
8244 Prepend (Subt_Decl, List);
8246 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
8247 Exchange_Declarations (Ftyp);
8248 end if;
8250 Resolve (Actual, Ftyp);
8252 if not Denotes_Variable (Actual) then
8253 Error_Msg_NE
8254 ("actual for& must be a variable", Actual, Formal_Id);
8256 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
8258 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
8259 -- the type of the actual shall resolve to a specific anonymous
8260 -- access type.
8262 if Ada_Version < Ada_05
8263 or else
8264 Ekind (Base_Type (Ftyp)) /=
8265 E_Anonymous_Access_Type
8266 or else
8267 Ekind (Base_Type (Etype (Actual))) /=
8268 E_Anonymous_Access_Type
8269 then
8270 Error_Msg_NE ("type of actual does not match type of&",
8271 Actual, Formal_Id);
8272 end if;
8273 end if;
8275 Note_Possible_Modification (Actual, Sure => True);
8277 -- Check for instantiation of atomic/volatile actual for
8278 -- non-atomic/volatile formal (RM C.6 (12)).
8280 if Is_Atomic_Object (Actual)
8281 and then not Is_Atomic (Orig_Ftyp)
8282 then
8283 Error_Msg_N
8284 ("cannot instantiate non-atomic formal object " &
8285 "with atomic actual", Actual);
8287 elsif Is_Volatile_Object (Actual)
8288 and then not Is_Volatile (Orig_Ftyp)
8289 then
8290 Error_Msg_N
8291 ("cannot instantiate non-volatile formal object " &
8292 "with volatile actual", Actual);
8293 end if;
8295 -- OUT not present
8297 else
8298 -- The instantiation of a generic formal in-parameter is constant
8299 -- declaration. The actual is the expression for that declaration.
8301 if Present (Actual) then
8302 if Present (Subt_Mark) then
8303 Def := Subt_Mark;
8304 else pragma Assert (Present (Acc_Def));
8305 Def := Acc_Def;
8306 end if;
8308 Decl_Node :=
8309 Make_Object_Declaration (Loc,
8310 Defining_Identifier => New_Copy (Formal_Id),
8311 Constant_Present => True,
8312 Object_Definition => New_Copy_Tree (Def),
8313 Expression => Actual);
8315 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
8317 -- A generic formal object of a tagged type is defined to be
8318 -- aliased so the new constant must also be treated as aliased.
8320 if Is_Tagged_Type
8321 (Etype (Defining_Identifier (Analyzed_Formal)))
8322 then
8323 Set_Aliased_Present (Decl_Node);
8324 end if;
8326 Append (Decl_Node, List);
8328 -- No need to repeat (pre-)analysis of some expression nodes
8329 -- already handled in Preanalyze_Actuals.
8331 if Nkind (Actual) /= N_Allocator then
8332 Analyze (Actual);
8334 -- Return if the analysis of the actual reported some error
8336 if Etype (Actual) = Any_Type then
8337 return List;
8338 end if;
8339 end if;
8341 declare
8342 Typ : constant Entity_Id :=
8343 Get_Instance_Of
8344 (Etype (Defining_Identifier (Analyzed_Formal)));
8346 begin
8347 Freeze_Before (Instantiation_Node, Typ);
8349 -- If the actual is an aggregate, perform name resolution on
8350 -- its components (the analysis of an aggregate does not do it)
8351 -- to capture local names that may be hidden if the generic is
8352 -- a child unit.
8354 if Nkind (Actual) = N_Aggregate then
8355 Preanalyze_And_Resolve (Actual, Typ);
8356 end if;
8358 if Is_Limited_Type (Typ)
8359 and then not OK_For_Limited_Init (Actual)
8360 then
8361 Error_Msg_N
8362 ("initialization not allowed for limited types", Actual);
8363 Explain_Limited_Type (Typ, Actual);
8364 end if;
8365 end;
8367 elsif Present (Default_Expression (Formal)) then
8369 -- Use default to construct declaration
8371 if Present (Subt_Mark) then
8372 Def := Subt_Mark;
8373 else pragma Assert (Present (Acc_Def));
8374 Def := Acc_Def;
8375 end if;
8377 Decl_Node :=
8378 Make_Object_Declaration (Sloc (Formal),
8379 Defining_Identifier => New_Copy (Formal_Id),
8380 Constant_Present => True,
8381 Object_Definition => New_Copy (Def),
8382 Expression => New_Copy_Tree
8383 (Default_Expression (Formal)));
8385 Append (Decl_Node, List);
8386 Set_Analyzed (Expression (Decl_Node), False);
8388 else
8389 Error_Msg_NE
8390 ("missing actual&",
8391 Instantiation_Node, Formal_Id);
8392 Error_Msg_NE ("\in instantiation of & declared#",
8393 Instantiation_Node,
8394 Scope (Defining_Identifier (Analyzed_Formal)));
8396 if Is_Scalar_Type
8397 (Etype (Defining_Identifier (Analyzed_Formal)))
8398 then
8399 -- Create dummy constant declaration so that instance can be
8400 -- analyzed, to minimize cascaded visibility errors.
8402 if Present (Subt_Mark) then
8403 Def := Subt_Mark;
8404 else pragma Assert (Present (Acc_Def));
8405 Def := Acc_Def;
8406 end if;
8408 Decl_Node :=
8409 Make_Object_Declaration (Loc,
8410 Defining_Identifier => New_Copy (Formal_Id),
8411 Constant_Present => True,
8412 Object_Definition => New_Copy (Def),
8413 Expression =>
8414 Make_Attribute_Reference (Sloc (Formal_Id),
8415 Attribute_Name => Name_First,
8416 Prefix => New_Copy (Def)));
8418 Append (Decl_Node, List);
8420 else
8421 Abandon_Instantiation (Instantiation_Node);
8422 end if;
8423 end if;
8424 end if;
8426 if Nkind (Actual) in N_Has_Entity then
8427 Actual_Decl := Parent (Entity (Actual));
8428 end if;
8430 -- Ada 2005 (AI-423): For a formal object declaration with a null
8431 -- exclusion or an access definition that has a null exclusion: If the
8432 -- actual matching the formal object declaration denotes a generic
8433 -- formal object of another generic unit G, and the instantiation
8434 -- containing the actual occurs within the body of G or within the body
8435 -- of a generic unit declared within the declarative region of G, then
8436 -- the declaration of the formal object of G must have a null exclusion.
8437 -- Otherwise, the subtype of the actual matching the formal object
8438 -- declaration shall exclude null.
8440 if Ada_Version >= Ada_05
8441 and then Present (Actual_Decl)
8442 and then
8443 Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
8444 N_Object_Declaration)
8445 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
8446 and then not Has_Null_Exclusion (Actual_Decl)
8447 and then Has_Null_Exclusion (Analyzed_Formal)
8448 then
8449 Error_Msg_Sloc := Sloc (Analyzed_Formal);
8450 Error_Msg_N
8451 ("actual must exclude null to match generic formal#", Actual);
8452 end if;
8454 return List;
8455 end Instantiate_Object;
8457 ------------------------------
8458 -- Instantiate_Package_Body --
8459 ------------------------------
8461 procedure Instantiate_Package_Body
8462 (Body_Info : Pending_Body_Info;
8463 Inlined_Body : Boolean := False;
8464 Body_Optional : Boolean := False)
8466 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
8467 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
8468 Loc : constant Source_Ptr := Sloc (Inst_Node);
8470 Gen_Id : constant Node_Id := Name (Inst_Node);
8471 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8472 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
8473 Act_Spec : constant Node_Id := Specification (Act_Decl);
8474 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
8476 Act_Body_Name : Node_Id;
8477 Gen_Body : Node_Id;
8478 Gen_Body_Id : Node_Id;
8479 Act_Body : Node_Id;
8480 Act_Body_Id : Entity_Id;
8482 Parent_Installed : Boolean := False;
8483 Save_Style_Check : constant Boolean := Style_Check;
8485 begin
8486 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8488 -- The instance body may already have been processed, as the parent of
8489 -- another instance that is inlined (Load_Parent_Of_Generic).
8491 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
8492 return;
8493 end if;
8495 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
8497 -- Re-establish the state of information on which checks are suppressed.
8498 -- This information was set in Body_Info at the point of instantiation,
8499 -- and now we restore it so that the instance is compiled using the
8500 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
8502 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
8503 Scope_Suppress := Body_Info.Scope_Suppress;
8505 if No (Gen_Body_Id) then
8506 Load_Parent_Of_Generic
8507 (Inst_Node, Specification (Gen_Decl), Body_Optional);
8508 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8509 end if;
8511 -- Establish global variable for sloc adjustment and for error recovery
8513 Instantiation_Node := Inst_Node;
8515 if Present (Gen_Body_Id) then
8516 Save_Env (Gen_Unit, Act_Decl_Id);
8517 Style_Check := False;
8518 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
8520 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
8522 Create_Instantiation_Source
8523 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
8525 Act_Body :=
8526 Copy_Generic_Node
8527 (Original_Node (Gen_Body), Empty, Instantiating => True);
8529 -- Build new name (possibly qualified) for body declaration
8531 Act_Body_Id := New_Copy (Act_Decl_Id);
8533 -- Some attributes of spec entity are not inherited by body entity
8535 Set_Handler_Records (Act_Body_Id, No_List);
8537 if Nkind (Defining_Unit_Name (Act_Spec)) =
8538 N_Defining_Program_Unit_Name
8539 then
8540 Act_Body_Name :=
8541 Make_Defining_Program_Unit_Name (Loc,
8542 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
8543 Defining_Identifier => Act_Body_Id);
8544 else
8545 Act_Body_Name := Act_Body_Id;
8546 end if;
8548 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
8550 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
8551 Check_Generic_Actuals (Act_Decl_Id, False);
8553 -- If it is a child unit, make the parent instance (which is an
8554 -- instance of the parent of the generic) visible. The parent
8555 -- instance is the prefix of the name of the generic unit.
8557 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
8558 and then Nkind (Gen_Id) = N_Expanded_Name
8559 then
8560 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
8561 Parent_Installed := True;
8563 elsif Is_Child_Unit (Gen_Unit) then
8564 Install_Parent (Scope (Gen_Unit), In_Body => True);
8565 Parent_Installed := True;
8566 end if;
8568 -- If the instantiation is a library unit, and this is the main unit,
8569 -- then build the resulting compilation unit nodes for the instance.
8570 -- If this is a compilation unit but it is not the main unit, then it
8571 -- is the body of a unit in the context, that is being compiled
8572 -- because it is encloses some inlined unit or another generic unit
8573 -- being instantiated. In that case, this body is not part of the
8574 -- current compilation, and is not attached to the tree, but its
8575 -- parent must be set for analysis.
8577 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
8579 -- Replace instance node with body of instance, and create new
8580 -- node for corresponding instance declaration.
8582 Build_Instance_Compilation_Unit_Nodes
8583 (Inst_Node, Act_Body, Act_Decl);
8584 Analyze (Inst_Node);
8586 if Parent (Inst_Node) = Cunit (Main_Unit) then
8588 -- If the instance is a child unit itself, then set the scope
8589 -- of the expanded body to be the parent of the instantiation
8590 -- (ensuring that the fully qualified name will be generated
8591 -- for the elaboration subprogram).
8593 if Nkind (Defining_Unit_Name (Act_Spec)) =
8594 N_Defining_Program_Unit_Name
8595 then
8596 Set_Scope
8597 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
8598 end if;
8599 end if;
8601 -- Case where instantiation is not a library unit
8603 else
8604 -- If this is an early instantiation, i.e. appears textually
8605 -- before the corresponding body and must be elaborated first,
8606 -- indicate that the body instance is to be delayed.
8608 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
8610 -- Now analyze the body. We turn off all checks if this is an
8611 -- internal unit, since there is no reason to have checks on for
8612 -- any predefined run-time library code. All such code is designed
8613 -- to be compiled with checks off.
8615 -- Note that we do NOT apply this criterion to children of GNAT
8616 -- (or on VMS, children of DEC). The latter units must suppress
8617 -- checks explicitly if this is needed.
8619 if Is_Predefined_File_Name
8620 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
8621 then
8622 Analyze (Act_Body, Suppress => All_Checks);
8623 else
8624 Analyze (Act_Body);
8625 end if;
8626 end if;
8628 Inherit_Context (Gen_Body, Inst_Node);
8630 -- Remove the parent instances if they have been placed on the scope
8631 -- stack to compile the body.
8633 if Parent_Installed then
8634 Remove_Parent (In_Body => True);
8635 end if;
8637 Restore_Private_Views (Act_Decl_Id);
8639 -- Remove the current unit from visibility if this is an instance
8640 -- that is not elaborated on the fly for inlining purposes.
8642 if not Inlined_Body then
8643 Set_Is_Immediately_Visible (Act_Decl_Id, False);
8644 end if;
8646 Restore_Env;
8647 Style_Check := Save_Style_Check;
8649 -- If we have no body, and the unit requires a body, then complain. This
8650 -- complaint is suppressed if we have detected other errors (since a
8651 -- common reason for missing the body is that it had errors).
8653 elsif Unit_Requires_Body (Gen_Unit)
8654 and then not Body_Optional
8655 then
8656 if Serious_Errors_Detected = 0 then
8657 Error_Msg_NE
8658 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
8660 -- Don't attempt to perform any cleanup actions if some other error
8661 -- was already detected, since this can cause blowups.
8663 else
8664 return;
8665 end if;
8667 -- Case of package that does not need a body
8669 else
8670 -- If the instantiation of the declaration is a library unit, rewrite
8671 -- the original package instantiation as a package declaration in the
8672 -- compilation unit node.
8674 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
8675 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
8676 Rewrite (Inst_Node, Act_Decl);
8678 -- Generate elaboration entity, in case spec has elaboration code.
8679 -- This cannot be done when the instance is analyzed, because it
8680 -- is not known yet whether the body exists.
8682 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
8683 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
8685 -- If the instantiation is not a library unit, then append the
8686 -- declaration to the list of implicitly generated entities, unless
8687 -- it is already a list member which means that it was already
8688 -- processed
8690 elsif not Is_List_Member (Act_Decl) then
8691 Mark_Rewrite_Insertion (Act_Decl);
8692 Insert_Before (Inst_Node, Act_Decl);
8693 end if;
8694 end if;
8696 Expander_Mode_Restore;
8697 end Instantiate_Package_Body;
8699 ---------------------------------
8700 -- Instantiate_Subprogram_Body --
8701 ---------------------------------
8703 procedure Instantiate_Subprogram_Body
8704 (Body_Info : Pending_Body_Info;
8705 Body_Optional : Boolean := False)
8707 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
8708 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
8709 Loc : constant Source_Ptr := Sloc (Inst_Node);
8710 Gen_Id : constant Node_Id := Name (Inst_Node);
8711 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8712 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
8713 Anon_Id : constant Entity_Id :=
8714 Defining_Unit_Name (Specification (Act_Decl));
8715 Pack_Id : constant Entity_Id :=
8716 Defining_Unit_Name (Parent (Act_Decl));
8717 Decls : List_Id;
8718 Gen_Body : Node_Id;
8719 Gen_Body_Id : Node_Id;
8720 Act_Body : Node_Id;
8721 Pack_Body : Node_Id;
8722 Prev_Formal : Entity_Id;
8723 Ret_Expr : Node_Id;
8724 Unit_Renaming : Node_Id;
8726 Parent_Installed : Boolean := False;
8727 Save_Style_Check : constant Boolean := Style_Check;
8729 begin
8730 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8732 -- Subprogram body may have been created already because of an inline
8733 -- pragma, or because of multiple elaborations of the enclosing package
8734 -- when several instances of the subprogram appear in the main unit.
8736 if Present (Corresponding_Body (Act_Decl)) then
8737 return;
8738 end if;
8740 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
8742 -- Re-establish the state of information on which checks are suppressed.
8743 -- This information was set in Body_Info at the point of instantiation,
8744 -- and now we restore it so that the instance is compiled using the
8745 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
8747 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
8748 Scope_Suppress := Body_Info.Scope_Suppress;
8750 if No (Gen_Body_Id) then
8752 -- For imported generic subprogram, no body to compile, complete
8753 -- the spec entity appropriately.
8755 if Is_Imported (Gen_Unit) then
8756 Set_Is_Imported (Anon_Id);
8757 Set_First_Rep_Item (Anon_Id, First_Rep_Item (Gen_Unit));
8758 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
8759 Set_Convention (Anon_Id, Convention (Gen_Unit));
8760 Set_Has_Completion (Anon_Id);
8761 return;
8763 -- For other cases, compile the body
8765 else
8766 Load_Parent_Of_Generic
8767 (Inst_Node, Specification (Gen_Decl), Body_Optional);
8768 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8769 end if;
8770 end if;
8772 Instantiation_Node := Inst_Node;
8774 if Present (Gen_Body_Id) then
8775 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
8777 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
8779 -- Either body is not present, or context is non-expanding, as
8780 -- when compiling a subunit. Mark the instance as completed, and
8781 -- diagnose a missing body when needed.
8783 if Expander_Active
8784 and then Operating_Mode = Generate_Code
8785 then
8786 Error_Msg_N
8787 ("missing proper body for instantiation", Gen_Body);
8788 end if;
8790 Set_Has_Completion (Anon_Id);
8791 return;
8792 end if;
8794 Save_Env (Gen_Unit, Anon_Id);
8795 Style_Check := False;
8796 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
8797 Create_Instantiation_Source
8798 (Inst_Node,
8799 Gen_Body_Id,
8800 False,
8801 S_Adjustment);
8803 Act_Body :=
8804 Copy_Generic_Node
8805 (Original_Node (Gen_Body), Empty, Instantiating => True);
8807 -- Create proper defining name for the body, to correspond to
8808 -- the one in the spec.
8810 Set_Defining_Unit_Name (Specification (Act_Body),
8811 Make_Defining_Identifier
8812 (Sloc (Defining_Entity (Inst_Node)), Chars (Anon_Id)));
8813 Set_Corresponding_Spec (Act_Body, Anon_Id);
8814 Set_Has_Completion (Anon_Id);
8815 Check_Generic_Actuals (Pack_Id, False);
8817 -- Generate a reference to link the visible subprogram instance to
8818 -- the generic body, which for navigation purposes is the only
8819 -- available source for the instance.
8821 Generate_Reference
8822 (Related_Instance (Pack_Id),
8823 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
8825 -- If it is a child unit, make the parent instance (which is an
8826 -- instance of the parent of the generic) visible. The parent
8827 -- instance is the prefix of the name of the generic unit.
8829 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
8830 and then Nkind (Gen_Id) = N_Expanded_Name
8831 then
8832 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
8833 Parent_Installed := True;
8835 elsif Is_Child_Unit (Gen_Unit) then
8836 Install_Parent (Scope (Gen_Unit), In_Body => True);
8837 Parent_Installed := True;
8838 end if;
8840 -- Inside its body, a reference to the generic unit is a reference
8841 -- to the instance. The corresponding renaming is the first
8842 -- declaration in the body.
8844 Unit_Renaming :=
8845 Make_Subprogram_Renaming_Declaration (Loc,
8846 Specification =>
8847 Copy_Generic_Node (
8848 Specification (Original_Node (Gen_Body)),
8849 Empty,
8850 Instantiating => True),
8851 Name => New_Occurrence_Of (Anon_Id, Loc));
8853 -- If there is a formal subprogram with the same name as the unit
8854 -- itself, do not add this renaming declaration. This is a temporary
8855 -- fix for one ACVC test. ???
8857 Prev_Formal := First_Entity (Pack_Id);
8858 while Present (Prev_Formal) loop
8859 if Chars (Prev_Formal) = Chars (Gen_Unit)
8860 and then Is_Overloadable (Prev_Formal)
8861 then
8862 exit;
8863 end if;
8865 Next_Entity (Prev_Formal);
8866 end loop;
8868 if Present (Prev_Formal) then
8869 Decls := New_List (Act_Body);
8870 else
8871 Decls := New_List (Unit_Renaming, Act_Body);
8872 end if;
8874 -- The subprogram body is placed in the body of a dummy package body,
8875 -- whose spec contains the subprogram declaration as well as the
8876 -- renaming declarations for the generic parameters.
8878 Pack_Body := Make_Package_Body (Loc,
8879 Defining_Unit_Name => New_Copy (Pack_Id),
8880 Declarations => Decls);
8882 Set_Corresponding_Spec (Pack_Body, Pack_Id);
8884 -- If the instantiation is a library unit, then build resulting
8885 -- compilation unit nodes for the instance. The declaration of
8886 -- the enclosing package is the grandparent of the subprogram
8887 -- declaration. First replace the instantiation node as the unit
8888 -- of the corresponding compilation.
8890 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
8891 if Parent (Inst_Node) = Cunit (Main_Unit) then
8892 Set_Unit (Parent (Inst_Node), Inst_Node);
8893 Build_Instance_Compilation_Unit_Nodes
8894 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
8895 Analyze (Inst_Node);
8896 else
8897 Set_Parent (Pack_Body, Parent (Inst_Node));
8898 Analyze (Pack_Body);
8899 end if;
8901 else
8902 Insert_Before (Inst_Node, Pack_Body);
8903 Mark_Rewrite_Insertion (Pack_Body);
8904 Analyze (Pack_Body);
8906 if Expander_Active then
8907 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
8908 end if;
8909 end if;
8911 Inherit_Context (Gen_Body, Inst_Node);
8913 Restore_Private_Views (Pack_Id, False);
8915 if Parent_Installed then
8916 Remove_Parent (In_Body => True);
8917 end if;
8919 Restore_Env;
8920 Style_Check := Save_Style_Check;
8922 -- Body not found. Error was emitted already. If there were no previous
8923 -- errors, this may be an instance whose scope is a premature instance.
8924 -- In that case we must insure that the (legal) program does raise
8925 -- program error if executed. We generate a subprogram body for this
8926 -- purpose. See DEC ac30vso.
8928 -- Should not reference proprietary DEC tests in comments ???
8930 elsif Serious_Errors_Detected = 0
8931 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
8932 then
8933 if Body_Optional then
8934 return;
8936 elsif Ekind (Anon_Id) = E_Procedure then
8937 Act_Body :=
8938 Make_Subprogram_Body (Loc,
8939 Specification =>
8940 Make_Procedure_Specification (Loc,
8941 Defining_Unit_Name =>
8942 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
8943 Parameter_Specifications =>
8944 New_Copy_List
8945 (Parameter_Specifications (Parent (Anon_Id)))),
8947 Declarations => Empty_List,
8948 Handled_Statement_Sequence =>
8949 Make_Handled_Sequence_Of_Statements (Loc,
8950 Statements =>
8951 New_List (
8952 Make_Raise_Program_Error (Loc,
8953 Reason =>
8954 PE_Access_Before_Elaboration))));
8956 else
8957 Ret_Expr :=
8958 Make_Raise_Program_Error (Loc,
8959 Reason => PE_Access_Before_Elaboration);
8961 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
8962 Set_Analyzed (Ret_Expr);
8964 Act_Body :=
8965 Make_Subprogram_Body (Loc,
8966 Specification =>
8967 Make_Function_Specification (Loc,
8968 Defining_Unit_Name =>
8969 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
8970 Parameter_Specifications =>
8971 New_Copy_List
8972 (Parameter_Specifications (Parent (Anon_Id))),
8973 Result_Definition =>
8974 New_Occurrence_Of (Etype (Anon_Id), Loc)),
8976 Declarations => Empty_List,
8977 Handled_Statement_Sequence =>
8978 Make_Handled_Sequence_Of_Statements (Loc,
8979 Statements =>
8980 New_List
8981 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
8982 end if;
8984 Pack_Body := Make_Package_Body (Loc,
8985 Defining_Unit_Name => New_Copy (Pack_Id),
8986 Declarations => New_List (Act_Body));
8988 Insert_After (Inst_Node, Pack_Body);
8989 Set_Corresponding_Spec (Pack_Body, Pack_Id);
8990 Analyze (Pack_Body);
8991 end if;
8993 Expander_Mode_Restore;
8994 end Instantiate_Subprogram_Body;
8996 ----------------------
8997 -- Instantiate_Type --
8998 ----------------------
9000 function Instantiate_Type
9001 (Formal : Node_Id;
9002 Actual : Node_Id;
9003 Analyzed_Formal : Node_Id;
9004 Actual_Decls : List_Id) return List_Id
9006 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
9007 A_Gen_T : constant Entity_Id :=
9008 Defining_Identifier (Analyzed_Formal);
9009 Ancestor : Entity_Id := Empty;
9010 Def : constant Node_Id := Formal_Type_Definition (Formal);
9011 Act_T : Entity_Id;
9012 Decl_Node : Node_Id;
9013 Decl_Nodes : List_Id;
9014 Loc : Source_Ptr;
9015 Subt : Entity_Id;
9017 procedure Validate_Array_Type_Instance;
9018 procedure Validate_Access_Subprogram_Instance;
9019 procedure Validate_Access_Type_Instance;
9020 procedure Validate_Derived_Type_Instance;
9021 procedure Validate_Derived_Interface_Type_Instance;
9022 procedure Validate_Interface_Type_Instance;
9023 procedure Validate_Private_Type_Instance;
9024 -- These procedures perform validation tests for the named case
9026 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
9027 -- Check that base types are the same and that the subtypes match
9028 -- statically. Used in several of the above.
9030 --------------------
9031 -- Subtypes_Match --
9032 --------------------
9034 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
9035 T : constant Entity_Id := Get_Instance_Of (Gen_T);
9037 begin
9038 return (Base_Type (T) = Base_Type (Act_T)
9039 and then Subtypes_Statically_Match (T, Act_T))
9041 or else (Is_Class_Wide_Type (Gen_T)
9042 and then Is_Class_Wide_Type (Act_T)
9043 and then
9044 Subtypes_Match
9045 (Get_Instance_Of (Root_Type (Gen_T)),
9046 Root_Type (Act_T)))
9048 or else
9049 ((Ekind (Gen_T) = E_Anonymous_Access_Subprogram_Type
9050 or else Ekind (Gen_T) = E_Anonymous_Access_Type)
9051 and then Ekind (Act_T) = Ekind (Gen_T)
9052 and then
9053 Subtypes_Statically_Match
9054 (Designated_Type (Gen_T), Designated_Type (Act_T)));
9055 end Subtypes_Match;
9057 -----------------------------------------
9058 -- Validate_Access_Subprogram_Instance --
9059 -----------------------------------------
9061 procedure Validate_Access_Subprogram_Instance is
9062 begin
9063 if not Is_Access_Type (Act_T)
9064 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
9065 then
9066 Error_Msg_NE
9067 ("expect access type in instantiation of &", Actual, Gen_T);
9068 Abandon_Instantiation (Actual);
9069 end if;
9071 Check_Mode_Conformant
9072 (Designated_Type (Act_T),
9073 Designated_Type (A_Gen_T),
9074 Actual,
9075 Get_Inst => True);
9077 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
9078 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
9079 Error_Msg_NE
9080 ("protected access type not allowed for formal &",
9081 Actual, Gen_T);
9082 end if;
9084 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
9085 Error_Msg_NE
9086 ("expect protected access type for formal &",
9087 Actual, Gen_T);
9088 end if;
9089 end Validate_Access_Subprogram_Instance;
9091 -----------------------------------
9092 -- Validate_Access_Type_Instance --
9093 -----------------------------------
9095 procedure Validate_Access_Type_Instance is
9096 Desig_Type : constant Entity_Id :=
9097 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
9098 Desig_Act : Entity_Id;
9100 begin
9101 if not Is_Access_Type (Act_T) then
9102 Error_Msg_NE
9103 ("expect access type in instantiation of &", Actual, Gen_T);
9104 Abandon_Instantiation (Actual);
9105 end if;
9107 if Is_Access_Constant (A_Gen_T) then
9108 if not Is_Access_Constant (Act_T) then
9109 Error_Msg_N
9110 ("actual type must be access-to-constant type", Actual);
9111 Abandon_Instantiation (Actual);
9112 end if;
9113 else
9114 if Is_Access_Constant (Act_T) then
9115 Error_Msg_N
9116 ("actual type must be access-to-variable type", Actual);
9117 Abandon_Instantiation (Actual);
9119 elsif Ekind (A_Gen_T) = E_General_Access_Type
9120 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
9121 then
9122 Error_Msg_N ("actual must be general access type!", Actual);
9123 Error_Msg_NE ("add ALL to }!", Actual, Act_T);
9124 Abandon_Instantiation (Actual);
9125 end if;
9126 end if;
9128 -- The designated subtypes, that is to say the subtypes introduced
9129 -- by an access type declaration (and not by a subtype declaration)
9130 -- must match.
9132 Desig_Act := Designated_Type (Base_Type (Act_T));
9134 -- The designated type may have been introduced through a limited_
9135 -- with clause, in which case retrieve the non-limited view. This
9136 -- applies to incomplete types as well as to class-wide types.
9138 if From_With_Type (Desig_Act) then
9139 Desig_Act := Available_View (Desig_Act);
9140 end if;
9142 if not Subtypes_Match
9143 (Desig_Type, Desig_Act) then
9144 Error_Msg_NE
9145 ("designated type of actual does not match that of formal &",
9146 Actual, Gen_T);
9147 Abandon_Instantiation (Actual);
9149 elsif Is_Access_Type (Designated_Type (Act_T))
9150 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
9152 Is_Constrained (Designated_Type (Desig_Type))
9153 then
9154 Error_Msg_NE
9155 ("designated type of actual does not match that of formal &",
9156 Actual, Gen_T);
9157 Abandon_Instantiation (Actual);
9158 end if;
9160 -- Ada 2005: null-exclusion indicators of the two types must agree
9162 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
9163 Error_Msg_NE
9164 ("non null exclusion of actual and formal & do not match",
9165 Actual, Gen_T);
9166 end if;
9167 end Validate_Access_Type_Instance;
9169 ----------------------------------
9170 -- Validate_Array_Type_Instance --
9171 ----------------------------------
9173 procedure Validate_Array_Type_Instance is
9174 I1 : Node_Id;
9175 I2 : Node_Id;
9176 T2 : Entity_Id;
9178 function Formal_Dimensions return Int;
9179 -- Count number of dimensions in array type formal
9181 -----------------------
9182 -- Formal_Dimensions --
9183 -----------------------
9185 function Formal_Dimensions return Int is
9186 Num : Int := 0;
9187 Index : Node_Id;
9189 begin
9190 if Nkind (Def) = N_Constrained_Array_Definition then
9191 Index := First (Discrete_Subtype_Definitions (Def));
9192 else
9193 Index := First (Subtype_Marks (Def));
9194 end if;
9196 while Present (Index) loop
9197 Num := Num + 1;
9198 Next_Index (Index);
9199 end loop;
9201 return Num;
9202 end Formal_Dimensions;
9204 -- Start of processing for Validate_Array_Type_Instance
9206 begin
9207 if not Is_Array_Type (Act_T) then
9208 Error_Msg_NE
9209 ("expect array type in instantiation of &", Actual, Gen_T);
9210 Abandon_Instantiation (Actual);
9212 elsif Nkind (Def) = N_Constrained_Array_Definition then
9213 if not (Is_Constrained (Act_T)) then
9214 Error_Msg_NE
9215 ("expect constrained array in instantiation of &",
9216 Actual, Gen_T);
9217 Abandon_Instantiation (Actual);
9218 end if;
9220 else
9221 if Is_Constrained (Act_T) then
9222 Error_Msg_NE
9223 ("expect unconstrained array in instantiation of &",
9224 Actual, Gen_T);
9225 Abandon_Instantiation (Actual);
9226 end if;
9227 end if;
9229 if Formal_Dimensions /= Number_Dimensions (Act_T) then
9230 Error_Msg_NE
9231 ("dimensions of actual do not match formal &", Actual, Gen_T);
9232 Abandon_Instantiation (Actual);
9233 end if;
9235 I1 := First_Index (A_Gen_T);
9236 I2 := First_Index (Act_T);
9237 for J in 1 .. Formal_Dimensions loop
9239 -- If the indices of the actual were given by a subtype_mark,
9240 -- the index was transformed into a range attribute. Retrieve
9241 -- the original type mark for checking.
9243 if Is_Entity_Name (Original_Node (I2)) then
9244 T2 := Entity (Original_Node (I2));
9245 else
9246 T2 := Etype (I2);
9247 end if;
9249 if not Subtypes_Match
9250 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
9251 then
9252 Error_Msg_NE
9253 ("index types of actual do not match those of formal &",
9254 Actual, Gen_T);
9255 Abandon_Instantiation (Actual);
9256 end if;
9258 Next_Index (I1);
9259 Next_Index (I2);
9260 end loop;
9262 -- Check matching subtypes. Note that there are complex visibility
9263 -- issues when the generic is a child unit and some aspect of the
9264 -- generic type is declared in a parent unit of the generic. We do
9265 -- the test to handle this special case only after a direct check
9266 -- for static matching has failed.
9268 if Subtypes_Match
9269 (Component_Type (A_Gen_T), Component_Type (Act_T))
9270 or else Subtypes_Match
9271 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
9272 Component_Type (Act_T))
9273 then
9274 null;
9275 else
9276 Error_Msg_NE
9277 ("component subtype of actual does not match that of formal &",
9278 Actual, Gen_T);
9279 Abandon_Instantiation (Actual);
9280 end if;
9282 if Has_Aliased_Components (A_Gen_T)
9283 and then not Has_Aliased_Components (Act_T)
9284 then
9285 Error_Msg_NE
9286 ("actual must have aliased components to match formal type &",
9287 Actual, Gen_T);
9288 end if;
9289 end Validate_Array_Type_Instance;
9291 -----------------------------------------------
9292 -- Validate_Derived_Interface_Type_Instance --
9293 -----------------------------------------------
9295 procedure Validate_Derived_Interface_Type_Instance is
9296 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
9297 Elmt : Elmt_Id;
9299 begin
9300 -- First apply interface instance checks
9302 Validate_Interface_Type_Instance;
9304 -- Verify that immediate parent interface is an ancestor of
9305 -- the actual.
9307 if Present (Par)
9308 and then not Interface_Present_In_Ancestor (Act_T, Par)
9309 then
9310 Error_Msg_NE
9311 ("interface actual must include progenitor&", Actual, Par);
9312 end if;
9314 -- Now verify that the actual includes all other ancestors of
9315 -- the formal.
9317 Elmt := First_Elmt (Interfaces (A_Gen_T));
9318 while Present (Elmt) loop
9319 if not Interface_Present_In_Ancestor
9320 (Act_T, Get_Instance_Of (Node (Elmt)))
9321 then
9322 Error_Msg_NE
9323 ("interface actual must include progenitor&",
9324 Actual, Node (Elmt));
9325 end if;
9327 Next_Elmt (Elmt);
9328 end loop;
9329 end Validate_Derived_Interface_Type_Instance;
9331 ------------------------------------
9332 -- Validate_Derived_Type_Instance --
9333 ------------------------------------
9335 procedure Validate_Derived_Type_Instance is
9336 Actual_Discr : Entity_Id;
9337 Ancestor_Discr : Entity_Id;
9339 begin
9340 -- If the parent type in the generic declaration is itself a previous
9341 -- formal type, then it is local to the generic and absent from the
9342 -- analyzed generic definition. In that case the ancestor is the
9343 -- instance of the formal (which must have been instantiated
9344 -- previously), unless the ancestor is itself a formal derived type.
9345 -- In this latter case (which is the subject of Corrigendum 8652/0038
9346 -- (AI-202) the ancestor of the formals is the ancestor of its
9347 -- parent. Otherwise, the analyzed generic carries the parent type.
9348 -- If the parent type is defined in a previous formal package, then
9349 -- the scope of that formal package is that of the generic type
9350 -- itself, and it has already been mapped into the corresponding type
9351 -- in the actual package.
9353 -- Common case: parent type defined outside of the generic
9355 if Is_Entity_Name (Subtype_Mark (Def))
9356 and then Present (Entity (Subtype_Mark (Def)))
9357 then
9358 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
9360 -- Check whether parent is defined in a previous formal package
9362 elsif
9363 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
9364 then
9365 Ancestor :=
9366 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
9368 -- The type may be a local derivation, or a type extension of a
9369 -- previous formal, or of a formal of a parent package.
9371 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
9372 or else
9373 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
9374 then
9375 -- Check whether the parent is another derived formal type in the
9376 -- same generic unit.
9378 if Etype (A_Gen_T) /= A_Gen_T
9379 and then Is_Generic_Type (Etype (A_Gen_T))
9380 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
9381 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
9382 then
9383 -- Locate ancestor of parent from the subtype declaration
9384 -- created for the actual.
9386 declare
9387 Decl : Node_Id;
9389 begin
9390 Decl := First (Actual_Decls);
9391 while Present (Decl) loop
9392 if Nkind (Decl) = N_Subtype_Declaration
9393 and then Chars (Defining_Identifier (Decl)) =
9394 Chars (Etype (A_Gen_T))
9395 then
9396 Ancestor := Generic_Parent_Type (Decl);
9397 exit;
9398 else
9399 Next (Decl);
9400 end if;
9401 end loop;
9402 end;
9404 pragma Assert (Present (Ancestor));
9406 else
9407 Ancestor :=
9408 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
9409 end if;
9411 else
9412 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
9413 end if;
9415 -- If the formal derived type has pragma Preelaborable_Initialization
9416 -- then the actual type must have preelaborable initialization.
9418 if Known_To_Have_Preelab_Init (A_Gen_T)
9419 and then not Has_Preelaborable_Initialization (Act_T)
9420 then
9421 Error_Msg_NE
9422 ("actual for & must have preelaborable initialization",
9423 Actual, Gen_T);
9424 end if;
9426 -- Ada 2005 (AI-251)
9428 if Ada_Version >= Ada_05
9429 and then Is_Interface (Ancestor)
9430 then
9431 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
9432 Error_Msg_NE
9433 ("(Ada 2005) expected type implementing & in instantiation",
9434 Actual, Ancestor);
9435 end if;
9437 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
9438 Error_Msg_NE
9439 ("expect type derived from & in instantiation",
9440 Actual, First_Subtype (Ancestor));
9441 Abandon_Instantiation (Actual);
9442 end if;
9444 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
9445 -- that the formal type declaration has been rewritten as a private
9446 -- extension.
9448 if Ada_Version >= Ada_05
9449 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
9450 and then Synchronized_Present (Parent (A_Gen_T))
9451 then
9452 -- The actual must be a synchronized tagged type
9454 if not Is_Tagged_Type (Act_T) then
9455 Error_Msg_N
9456 ("actual of synchronized type must be tagged", Actual);
9457 Abandon_Instantiation (Actual);
9459 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
9460 and then Nkind (Type_Definition (Parent (Act_T))) =
9461 N_Derived_Type_Definition
9462 and then not Synchronized_Present (Type_Definition
9463 (Parent (Act_T)))
9464 then
9465 Error_Msg_N
9466 ("actual of synchronized type must be synchronized", Actual);
9467 Abandon_Instantiation (Actual);
9468 end if;
9469 end if;
9471 -- Perform atomic/volatile checks (RM C.6(12))
9473 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
9474 Error_Msg_N
9475 ("cannot have atomic actual type for non-atomic formal type",
9476 Actual);
9478 elsif Is_Volatile (Act_T)
9479 and then not Is_Volatile (Ancestor)
9480 and then Is_By_Reference_Type (Ancestor)
9481 then
9482 Error_Msg_N
9483 ("cannot have volatile actual type for non-volatile formal type",
9484 Actual);
9485 end if;
9487 -- It should not be necessary to check for unknown discriminants on
9488 -- Formal, but for some reason Has_Unknown_Discriminants is false for
9489 -- A_Gen_T, so Is_Indefinite_Subtype incorrectly returns False. This
9490 -- needs fixing. ???
9492 if not Is_Indefinite_Subtype (A_Gen_T)
9493 and then not Unknown_Discriminants_Present (Formal)
9494 and then Is_Indefinite_Subtype (Act_T)
9495 then
9496 Error_Msg_N
9497 ("actual subtype must be constrained", Actual);
9498 Abandon_Instantiation (Actual);
9499 end if;
9501 if not Unknown_Discriminants_Present (Formal) then
9502 if Is_Constrained (Ancestor) then
9503 if not Is_Constrained (Act_T) then
9504 Error_Msg_N
9505 ("actual subtype must be constrained", Actual);
9506 Abandon_Instantiation (Actual);
9507 end if;
9509 -- Ancestor is unconstrained, Check if generic formal and actual
9510 -- agree on constrainedness. The check only applies to array types
9511 -- and discriminated types.
9513 elsif Is_Constrained (Act_T) then
9514 if Ekind (Ancestor) = E_Access_Type
9515 or else
9516 (not Is_Constrained (A_Gen_T)
9517 and then Is_Composite_Type (A_Gen_T))
9518 then
9519 Error_Msg_N
9520 ("actual subtype must be unconstrained", Actual);
9521 Abandon_Instantiation (Actual);
9522 end if;
9524 -- A class-wide type is only allowed if the formal has unknown
9525 -- discriminants.
9527 elsif Is_Class_Wide_Type (Act_T)
9528 and then not Has_Unknown_Discriminants (Ancestor)
9529 then
9530 Error_Msg_NE
9531 ("actual for & cannot be a class-wide type", Actual, Gen_T);
9532 Abandon_Instantiation (Actual);
9534 -- Otherwise, the formal and actual shall have the same number
9535 -- of discriminants and each discriminant of the actual must
9536 -- correspond to a discriminant of the formal.
9538 elsif Has_Discriminants (Act_T)
9539 and then not Has_Unknown_Discriminants (Act_T)
9540 and then Has_Discriminants (Ancestor)
9541 then
9542 Actual_Discr := First_Discriminant (Act_T);
9543 Ancestor_Discr := First_Discriminant (Ancestor);
9544 while Present (Actual_Discr)
9545 and then Present (Ancestor_Discr)
9546 loop
9547 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
9548 No (Corresponding_Discriminant (Actual_Discr))
9549 then
9550 Error_Msg_NE
9551 ("discriminant & does not correspond " &
9552 "to ancestor discriminant", Actual, Actual_Discr);
9553 Abandon_Instantiation (Actual);
9554 end if;
9556 Next_Discriminant (Actual_Discr);
9557 Next_Discriminant (Ancestor_Discr);
9558 end loop;
9560 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
9561 Error_Msg_NE
9562 ("actual for & must have same number of discriminants",
9563 Actual, Gen_T);
9564 Abandon_Instantiation (Actual);
9565 end if;
9567 -- This case should be caught by the earlier check for
9568 -- constrainedness, but the check here is added for completeness.
9570 elsif Has_Discriminants (Act_T)
9571 and then not Has_Unknown_Discriminants (Act_T)
9572 then
9573 Error_Msg_NE
9574 ("actual for & must not have discriminants", Actual, Gen_T);
9575 Abandon_Instantiation (Actual);
9577 elsif Has_Discriminants (Ancestor) then
9578 Error_Msg_NE
9579 ("actual for & must have known discriminants", Actual, Gen_T);
9580 Abandon_Instantiation (Actual);
9581 end if;
9583 if not Subtypes_Statically_Compatible (Act_T, Ancestor) then
9584 Error_Msg_N
9585 ("constraint on actual is incompatible with formal", Actual);
9586 Abandon_Instantiation (Actual);
9587 end if;
9588 end if;
9590 -- If the formal and actual types are abstract, check that there
9591 -- are no abstract primitives of the actual type that correspond to
9592 -- nonabstract primitives of the formal type (second sentence of
9593 -- RM95-3.9.3(9)).
9595 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
9596 Check_Abstract_Primitives : declare
9597 Gen_Prims : constant Elist_Id :=
9598 Primitive_Operations (A_Gen_T);
9599 Gen_Elmt : Elmt_Id;
9600 Gen_Subp : Entity_Id;
9601 Anc_Subp : Entity_Id;
9602 Anc_Formal : Entity_Id;
9603 Anc_F_Type : Entity_Id;
9605 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
9606 Act_Elmt : Elmt_Id;
9607 Act_Subp : Entity_Id;
9608 Act_Formal : Entity_Id;
9609 Act_F_Type : Entity_Id;
9611 Subprograms_Correspond : Boolean;
9613 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
9614 -- Returns true if T2 is derived directly or indirectly from
9615 -- T1, including derivations from interfaces. T1 and T2 are
9616 -- required to be specific tagged base types.
9618 ------------------------
9619 -- Is_Tagged_Ancestor --
9620 ------------------------
9622 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
9624 Intfc_Elmt : Elmt_Id;
9626 begin
9627 -- The predicate is satisfied if the types are the same
9629 if T1 = T2 then
9630 return True;
9632 -- If we've reached the top of the derivation chain then
9633 -- we know that T1 is not an ancestor of T2.
9635 elsif Etype (T2) = T2 then
9636 return False;
9638 -- Proceed to check T2's immediate parent
9640 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
9641 return True;
9643 -- Finally, check to see if T1 is an ancestor of any of T2's
9644 -- progenitors.
9646 else
9647 Intfc_Elmt := First_Elmt (Interfaces (T2));
9648 while Present (Intfc_Elmt) loop
9649 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
9650 return True;
9651 end if;
9653 Next_Elmt (Intfc_Elmt);
9654 end loop;
9655 end if;
9657 return False;
9658 end Is_Tagged_Ancestor;
9660 -- Start of processing for Check_Abstract_Primitives
9662 begin
9663 -- Loop over all of the formal derived type's primitives
9665 Gen_Elmt := First_Elmt (Gen_Prims);
9666 while Present (Gen_Elmt) loop
9667 Gen_Subp := Node (Gen_Elmt);
9669 -- If the primitive of the formal is not abstract, then
9670 -- determine whether there is a corresponding primitive of
9671 -- the actual type that's abstract.
9673 if not Is_Abstract_Subprogram (Gen_Subp) then
9674 Act_Elmt := First_Elmt (Act_Prims);
9675 while Present (Act_Elmt) loop
9676 Act_Subp := Node (Act_Elmt);
9678 -- If we find an abstract primitive of the actual,
9679 -- then we need to test whether it corresponds to the
9680 -- subprogram from which the generic formal primitive
9681 -- is inherited.
9683 if Is_Abstract_Subprogram (Act_Subp) then
9684 Anc_Subp := Alias (Gen_Subp);
9686 -- Test whether we have a corresponding primitive
9687 -- by comparing names, kinds, formal types, and
9688 -- result types.
9690 if Chars (Anc_Subp) = Chars (Act_Subp)
9691 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
9692 then
9693 Anc_Formal := First_Formal (Anc_Subp);
9694 Act_Formal := First_Formal (Act_Subp);
9695 while Present (Anc_Formal)
9696 and then Present (Act_Formal)
9697 loop
9698 Anc_F_Type := Etype (Anc_Formal);
9699 Act_F_Type := Etype (Act_Formal);
9701 if Ekind (Anc_F_Type)
9702 = E_Anonymous_Access_Type
9703 then
9704 Anc_F_Type := Designated_Type (Anc_F_Type);
9706 if Ekind (Act_F_Type)
9707 = E_Anonymous_Access_Type
9708 then
9709 Act_F_Type :=
9710 Designated_Type (Act_F_Type);
9711 else
9712 exit;
9713 end if;
9715 elsif
9716 Ekind (Act_F_Type) = E_Anonymous_Access_Type
9717 then
9718 exit;
9719 end if;
9721 Anc_F_Type := Base_Type (Anc_F_Type);
9722 Act_F_Type := Base_Type (Act_F_Type);
9724 -- If the formal is controlling, then the
9725 -- the type of the actual primitive's formal
9726 -- must be derived directly or indirectly
9727 -- from the type of the ancestor primitive's
9728 -- formal.
9730 if Is_Controlling_Formal (Anc_Formal) then
9731 if not Is_Tagged_Ancestor
9732 (Anc_F_Type, Act_F_Type)
9733 then
9734 exit;
9735 end if;
9737 -- Otherwise the types of the formals must
9738 -- be the same.
9740 elsif Anc_F_Type /= Act_F_Type then
9741 exit;
9742 end if;
9744 Next_Entity (Anc_Formal);
9745 Next_Entity (Act_Formal);
9746 end loop;
9748 -- If we traversed through all of the formals
9749 -- then so far the subprograms correspond, so
9750 -- now check that any result types correspond.
9752 if No (Anc_Formal)
9753 and then No (Act_Formal)
9754 then
9755 Subprograms_Correspond := True;
9757 if Ekind (Act_Subp) = E_Function then
9758 Anc_F_Type := Etype (Anc_Subp);
9759 Act_F_Type := Etype (Act_Subp);
9761 if Ekind (Anc_F_Type)
9762 = E_Anonymous_Access_Type
9763 then
9764 Anc_F_Type :=
9765 Designated_Type (Anc_F_Type);
9767 if Ekind (Act_F_Type)
9768 = E_Anonymous_Access_Type
9769 then
9770 Act_F_Type :=
9771 Designated_Type (Act_F_Type);
9772 else
9773 Subprograms_Correspond := False;
9774 end if;
9776 elsif
9777 Ekind (Act_F_Type)
9778 = E_Anonymous_Access_Type
9779 then
9780 Subprograms_Correspond := False;
9781 end if;
9783 Anc_F_Type := Base_Type (Anc_F_Type);
9784 Act_F_Type := Base_Type (Act_F_Type);
9786 -- Now either the result types must be
9787 -- the same or, if the result type is
9788 -- controlling, the result type of the
9789 -- actual primitive must descend from the
9790 -- result type of the ancestor primitive.
9792 if Subprograms_Correspond
9793 and then Anc_F_Type /= Act_F_Type
9794 and then
9795 Has_Controlling_Result (Anc_Subp)
9796 and then
9797 not Is_Tagged_Ancestor
9798 (Anc_F_Type, Act_F_Type)
9799 then
9800 Subprograms_Correspond := False;
9801 end if;
9802 end if;
9804 -- Found a matching subprogram belonging to
9805 -- formal ancestor type, so actual subprogram
9806 -- corresponds and this violates 3.9.3(9).
9808 if Subprograms_Correspond then
9809 Error_Msg_NE
9810 ("abstract subprogram & overrides " &
9811 "nonabstract subprogram of ancestor",
9812 Actual,
9813 Act_Subp);
9814 end if;
9815 end if;
9816 end if;
9817 end if;
9819 Next_Elmt (Act_Elmt);
9820 end loop;
9821 end if;
9823 Next_Elmt (Gen_Elmt);
9824 end loop;
9825 end Check_Abstract_Primitives;
9826 end if;
9828 -- Verify that limitedness matches. If parent is a limited
9829 -- interface then the generic formal is not unless declared
9830 -- explicitly so. If not declared limited, the actual cannot be
9831 -- limited (see AI05-0087).
9832 -- Disable check for now, limited interfaces implemented by
9833 -- protected types are common, Need to update tests ???
9835 if Is_Limited_Type (Act_T)
9836 and then not Is_Limited_Type (A_Gen_T)
9837 and then False
9838 then
9839 Error_Msg_NE
9840 ("actual for non-limited & cannot be a limited type", Actual,
9841 Gen_T);
9842 Explain_Limited_Type (Act_T, Actual);
9843 Abandon_Instantiation (Actual);
9844 end if;
9845 end Validate_Derived_Type_Instance;
9847 --------------------------------------
9848 -- Validate_Interface_Type_Instance --
9849 --------------------------------------
9851 procedure Validate_Interface_Type_Instance is
9852 begin
9853 if not Is_Interface (Act_T) then
9854 Error_Msg_NE
9855 ("actual for formal interface type must be an interface",
9856 Actual, Gen_T);
9858 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
9859 or else
9860 Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
9861 or else
9862 Is_Protected_Interface (A_Gen_T) /=
9863 Is_Protected_Interface (Act_T)
9864 or else
9865 Is_Synchronized_Interface (A_Gen_T) /=
9866 Is_Synchronized_Interface (Act_T)
9867 then
9868 Error_Msg_NE
9869 ("actual for interface& does not match (RM 12.5.5(4))",
9870 Actual, Gen_T);
9871 end if;
9872 end Validate_Interface_Type_Instance;
9874 ------------------------------------
9875 -- Validate_Private_Type_Instance --
9876 ------------------------------------
9878 procedure Validate_Private_Type_Instance is
9879 Formal_Discr : Entity_Id;
9880 Actual_Discr : Entity_Id;
9881 Formal_Subt : Entity_Id;
9883 begin
9884 if Is_Limited_Type (Act_T)
9885 and then not Is_Limited_Type (A_Gen_T)
9886 then
9887 Error_Msg_NE
9888 ("actual for non-limited & cannot be a limited type", Actual,
9889 Gen_T);
9890 Explain_Limited_Type (Act_T, Actual);
9891 Abandon_Instantiation (Actual);
9893 elsif Known_To_Have_Preelab_Init (A_Gen_T)
9894 and then not Has_Preelaborable_Initialization (Act_T)
9895 then
9896 Error_Msg_NE
9897 ("actual for & must have preelaborable initialization", Actual,
9898 Gen_T);
9900 elsif Is_Indefinite_Subtype (Act_T)
9901 and then not Is_Indefinite_Subtype (A_Gen_T)
9902 and then Ada_Version >= Ada_95
9903 then
9904 Error_Msg_NE
9905 ("actual for & must be a definite subtype", Actual, Gen_T);
9907 elsif not Is_Tagged_Type (Act_T)
9908 and then Is_Tagged_Type (A_Gen_T)
9909 then
9910 Error_Msg_NE
9911 ("actual for & must be a tagged type", Actual, Gen_T);
9913 elsif Has_Discriminants (A_Gen_T) then
9914 if not Has_Discriminants (Act_T) then
9915 Error_Msg_NE
9916 ("actual for & must have discriminants", Actual, Gen_T);
9917 Abandon_Instantiation (Actual);
9919 elsif Is_Constrained (Act_T) then
9920 Error_Msg_NE
9921 ("actual for & must be unconstrained", Actual, Gen_T);
9922 Abandon_Instantiation (Actual);
9924 else
9925 Formal_Discr := First_Discriminant (A_Gen_T);
9926 Actual_Discr := First_Discriminant (Act_T);
9927 while Formal_Discr /= Empty loop
9928 if Actual_Discr = Empty then
9929 Error_Msg_NE
9930 ("discriminants on actual do not match formal",
9931 Actual, Gen_T);
9932 Abandon_Instantiation (Actual);
9933 end if;
9935 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
9937 -- Access discriminants match if designated types do
9939 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
9940 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
9941 E_Anonymous_Access_Type
9942 and then
9943 Get_Instance_Of
9944 (Designated_Type (Base_Type (Formal_Subt))) =
9945 Designated_Type (Base_Type (Etype (Actual_Discr)))
9946 then
9947 null;
9949 elsif Base_Type (Formal_Subt) /=
9950 Base_Type (Etype (Actual_Discr))
9951 then
9952 Error_Msg_NE
9953 ("types of actual discriminants must match formal",
9954 Actual, Gen_T);
9955 Abandon_Instantiation (Actual);
9957 elsif not Subtypes_Statically_Match
9958 (Formal_Subt, Etype (Actual_Discr))
9959 and then Ada_Version >= Ada_95
9960 then
9961 Error_Msg_NE
9962 ("subtypes of actual discriminants must match formal",
9963 Actual, Gen_T);
9964 Abandon_Instantiation (Actual);
9965 end if;
9967 Next_Discriminant (Formal_Discr);
9968 Next_Discriminant (Actual_Discr);
9969 end loop;
9971 if Actual_Discr /= Empty then
9972 Error_Msg_NE
9973 ("discriminants on actual do not match formal",
9974 Actual, Gen_T);
9975 Abandon_Instantiation (Actual);
9976 end if;
9977 end if;
9979 end if;
9981 Ancestor := Gen_T;
9982 end Validate_Private_Type_Instance;
9984 -- Start of processing for Instantiate_Type
9986 begin
9987 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
9988 Error_Msg_N ("duplicate instantiation of generic type", Actual);
9989 return New_List (Error);
9991 elsif not Is_Entity_Name (Actual)
9992 or else not Is_Type (Entity (Actual))
9993 then
9994 Error_Msg_NE
9995 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
9996 Abandon_Instantiation (Actual);
9998 else
9999 Act_T := Entity (Actual);
10001 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
10002 -- as a generic actual parameter if the corresponding formal type
10003 -- does not have a known_discriminant_part, or is a formal derived
10004 -- type that is an Unchecked_Union type.
10006 if Is_Unchecked_Union (Base_Type (Act_T)) then
10007 if not Has_Discriminants (A_Gen_T)
10008 or else
10009 (Is_Derived_Type (A_Gen_T)
10010 and then
10011 Is_Unchecked_Union (A_Gen_T))
10012 then
10013 null;
10014 else
10015 Error_Msg_N ("Unchecked_Union cannot be the actual for a" &
10016 " discriminated formal type", Act_T);
10018 end if;
10019 end if;
10021 -- Deal with fixed/floating restrictions
10023 if Is_Floating_Point_Type (Act_T) then
10024 Check_Restriction (No_Floating_Point, Actual);
10025 elsif Is_Fixed_Point_Type (Act_T) then
10026 Check_Restriction (No_Fixed_Point, Actual);
10027 end if;
10029 -- Deal with error of using incomplete type as generic actual.
10030 -- This includes limited views of a type, even if the non-limited
10031 -- view may be available.
10033 if Ekind (Act_T) = E_Incomplete_Type
10034 or else (Is_Class_Wide_Type (Act_T)
10035 and then
10036 Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
10037 then
10038 if Is_Class_Wide_Type (Act_T)
10039 or else No (Full_View (Act_T))
10040 then
10041 Error_Msg_N ("premature use of incomplete type", Actual);
10042 Abandon_Instantiation (Actual);
10043 else
10044 Act_T := Full_View (Act_T);
10045 Set_Entity (Actual, Act_T);
10047 if Has_Private_Component (Act_T) then
10048 Error_Msg_N
10049 ("premature use of type with private component", Actual);
10050 end if;
10051 end if;
10053 -- Deal with error of premature use of private type as generic actual
10055 elsif Is_Private_Type (Act_T)
10056 and then Is_Private_Type (Base_Type (Act_T))
10057 and then not Is_Generic_Type (Act_T)
10058 and then not Is_Derived_Type (Act_T)
10059 and then No (Full_View (Root_Type (Act_T)))
10060 then
10061 Error_Msg_N ("premature use of private type", Actual);
10063 elsif Has_Private_Component (Act_T) then
10064 Error_Msg_N
10065 ("premature use of type with private component", Actual);
10066 end if;
10068 Set_Instance_Of (A_Gen_T, Act_T);
10070 -- If the type is generic, the class-wide type may also be used
10072 if Is_Tagged_Type (A_Gen_T)
10073 and then Is_Tagged_Type (Act_T)
10074 and then not Is_Class_Wide_Type (A_Gen_T)
10075 then
10076 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
10077 Class_Wide_Type (Act_T));
10078 end if;
10080 if not Is_Abstract_Type (A_Gen_T)
10081 and then Is_Abstract_Type (Act_T)
10082 then
10083 Error_Msg_N
10084 ("actual of non-abstract formal cannot be abstract", Actual);
10085 end if;
10087 -- A generic scalar type is a first subtype for which we generate
10088 -- an anonymous base type. Indicate that the instance of this base
10089 -- is the base type of the actual.
10091 if Is_Scalar_Type (A_Gen_T) then
10092 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
10093 end if;
10094 end if;
10096 if Error_Posted (Act_T) then
10097 null;
10098 else
10099 case Nkind (Def) is
10100 when N_Formal_Private_Type_Definition =>
10101 Validate_Private_Type_Instance;
10103 when N_Formal_Derived_Type_Definition =>
10104 Validate_Derived_Type_Instance;
10106 when N_Formal_Discrete_Type_Definition =>
10107 if not Is_Discrete_Type (Act_T) then
10108 Error_Msg_NE
10109 ("expect discrete type in instantiation of&",
10110 Actual, Gen_T);
10111 Abandon_Instantiation (Actual);
10112 end if;
10114 when N_Formal_Signed_Integer_Type_Definition =>
10115 if not Is_Signed_Integer_Type (Act_T) then
10116 Error_Msg_NE
10117 ("expect signed integer type in instantiation of&",
10118 Actual, Gen_T);
10119 Abandon_Instantiation (Actual);
10120 end if;
10122 when N_Formal_Modular_Type_Definition =>
10123 if not Is_Modular_Integer_Type (Act_T) then
10124 Error_Msg_NE
10125 ("expect modular type in instantiation of &",
10126 Actual, Gen_T);
10127 Abandon_Instantiation (Actual);
10128 end if;
10130 when N_Formal_Floating_Point_Definition =>
10131 if not Is_Floating_Point_Type (Act_T) then
10132 Error_Msg_NE
10133 ("expect float type in instantiation of &", Actual, Gen_T);
10134 Abandon_Instantiation (Actual);
10135 end if;
10137 when N_Formal_Ordinary_Fixed_Point_Definition =>
10138 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
10139 Error_Msg_NE
10140 ("expect ordinary fixed point type in instantiation of &",
10141 Actual, Gen_T);
10142 Abandon_Instantiation (Actual);
10143 end if;
10145 when N_Formal_Decimal_Fixed_Point_Definition =>
10146 if not Is_Decimal_Fixed_Point_Type (Act_T) then
10147 Error_Msg_NE
10148 ("expect decimal type in instantiation of &",
10149 Actual, Gen_T);
10150 Abandon_Instantiation (Actual);
10151 end if;
10153 when N_Array_Type_Definition =>
10154 Validate_Array_Type_Instance;
10156 when N_Access_To_Object_Definition =>
10157 Validate_Access_Type_Instance;
10159 when N_Access_Function_Definition |
10160 N_Access_Procedure_Definition =>
10161 Validate_Access_Subprogram_Instance;
10163 when N_Record_Definition =>
10164 Validate_Interface_Type_Instance;
10166 when N_Derived_Type_Definition =>
10167 Validate_Derived_Interface_Type_Instance;
10169 when others =>
10170 raise Program_Error;
10172 end case;
10173 end if;
10175 Subt := New_Copy (Gen_T);
10177 -- Use adjusted sloc of subtype name as the location for other nodes in
10178 -- the subtype declaration.
10180 Loc := Sloc (Subt);
10182 Decl_Node :=
10183 Make_Subtype_Declaration (Loc,
10184 Defining_Identifier => Subt,
10185 Subtype_Indication => New_Reference_To (Act_T, Loc));
10187 if Is_Private_Type (Act_T) then
10188 Set_Has_Private_View (Subtype_Indication (Decl_Node));
10190 elsif Is_Access_Type (Act_T)
10191 and then Is_Private_Type (Designated_Type (Act_T))
10192 then
10193 Set_Has_Private_View (Subtype_Indication (Decl_Node));
10194 end if;
10196 Decl_Nodes := New_List (Decl_Node);
10198 -- Flag actual derived types so their elaboration produces the
10199 -- appropriate renamings for the primitive operations of the ancestor.
10200 -- Flag actual for formal private types as well, to determine whether
10201 -- operations in the private part may override inherited operations.
10202 -- If the formal has an interface list, the ancestor is not the
10203 -- parent, but the analyzed formal that includes the interface
10204 -- operations of all its progenitors.
10206 if Nkind (Def) = N_Formal_Derived_Type_Definition then
10207 if Present (Interface_List (Def)) then
10208 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
10209 else
10210 Set_Generic_Parent_Type (Decl_Node, Ancestor);
10211 end if;
10213 elsif Nkind (Def) = N_Formal_Private_Type_Definition then
10214 Set_Generic_Parent_Type (Decl_Node, Ancestor);
10215 end if;
10217 -- If the actual is a synchronized type that implements an interface,
10218 -- the primitive operations are attached to the corresponding record,
10219 -- and we have to treat it as an additional generic actual, so that its
10220 -- primitive operations become visible in the instance. The task or
10221 -- protected type itself does not carry primitive operations.
10223 if Is_Concurrent_Type (Act_T)
10224 and then Is_Tagged_Type (Act_T)
10225 and then Present (Corresponding_Record_Type (Act_T))
10226 and then Present (Ancestor)
10227 and then Is_Interface (Ancestor)
10228 then
10229 declare
10230 Corr_Rec : constant Entity_Id :=
10231 Corresponding_Record_Type (Act_T);
10232 New_Corr : Entity_Id;
10233 Corr_Decl : Node_Id;
10235 begin
10236 New_Corr := Make_Defining_Identifier (Loc,
10237 Chars => New_Internal_Name ('S'));
10238 Corr_Decl :=
10239 Make_Subtype_Declaration (Loc,
10240 Defining_Identifier => New_Corr,
10241 Subtype_Indication =>
10242 New_Reference_To (Corr_Rec, Loc));
10243 Append_To (Decl_Nodes, Corr_Decl);
10245 if Ekind (Act_T) = E_Task_Type then
10246 Set_Ekind (Subt, E_Task_Subtype);
10247 else
10248 Set_Ekind (Subt, E_Protected_Subtype);
10249 end if;
10251 Set_Corresponding_Record_Type (Subt, Corr_Rec);
10252 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
10253 Set_Generic_Parent_Type (Decl_Node, Empty);
10254 end;
10255 end if;
10257 return Decl_Nodes;
10258 end Instantiate_Type;
10260 -----------------------
10261 -- Is_Generic_Formal --
10262 -----------------------
10264 function Is_Generic_Formal (E : Entity_Id) return Boolean is
10265 Kind : Node_Kind;
10266 begin
10267 if No (E) then
10268 return False;
10269 else
10270 Kind := Nkind (Parent (E));
10271 return
10272 Nkind_In (Kind, N_Formal_Object_Declaration,
10273 N_Formal_Package_Declaration,
10274 N_Formal_Type_Declaration)
10275 or else
10276 (Is_Formal_Subprogram (E)
10277 and then
10278 Nkind (Parent (Parent (E))) in
10279 N_Formal_Subprogram_Declaration);
10280 end if;
10281 end Is_Generic_Formal;
10283 ---------------------
10284 -- Is_In_Main_Unit --
10285 ---------------------
10287 function Is_In_Main_Unit (N : Node_Id) return Boolean is
10288 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
10289 Current_Unit : Node_Id;
10291 begin
10292 if Unum = Main_Unit then
10293 return True;
10295 -- If the current unit is a subunit then it is either the main unit or
10296 -- is being compiled as part of the main unit.
10298 elsif Nkind (N) = N_Compilation_Unit then
10299 return Nkind (Unit (N)) = N_Subunit;
10300 end if;
10302 Current_Unit := Parent (N);
10303 while Present (Current_Unit)
10304 and then Nkind (Current_Unit) /= N_Compilation_Unit
10305 loop
10306 Current_Unit := Parent (Current_Unit);
10307 end loop;
10309 -- The instantiation node is in the main unit, or else the current node
10310 -- (perhaps as the result of nested instantiations) is in the main unit,
10311 -- or in the declaration of the main unit, which in this last case must
10312 -- be a body.
10314 return Unum = Main_Unit
10315 or else Current_Unit = Cunit (Main_Unit)
10316 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
10317 or else (Present (Library_Unit (Current_Unit))
10318 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
10319 end Is_In_Main_Unit;
10321 ----------------------------
10322 -- Load_Parent_Of_Generic --
10323 ----------------------------
10325 procedure Load_Parent_Of_Generic
10326 (N : Node_Id;
10327 Spec : Node_Id;
10328 Body_Optional : Boolean := False)
10330 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
10331 Save_Style_Check : constant Boolean := Style_Check;
10332 True_Parent : Node_Id;
10333 Inst_Node : Node_Id;
10334 OK : Boolean;
10335 Previous_Instances : constant Elist_Id := New_Elmt_List;
10337 procedure Collect_Previous_Instances (Decls : List_Id);
10338 -- Collect all instantiations in the given list of declarations, that
10339 -- precede the generic that we need to load. If the bodies of these
10340 -- instantiations are available, we must analyze them, to ensure that
10341 -- the public symbols generated are the same when the unit is compiled
10342 -- to generate code, and when it is compiled in the context of a unit
10343 -- that needs a particular nested instance. This process is applied
10344 -- to both package and subprogram instances.
10346 --------------------------------
10347 -- Collect_Previous_Instances --
10348 --------------------------------
10350 procedure Collect_Previous_Instances (Decls : List_Id) is
10351 Decl : Node_Id;
10353 begin
10354 Decl := First (Decls);
10355 while Present (Decl) loop
10356 if Sloc (Decl) >= Sloc (Inst_Node) then
10357 return;
10359 -- If Decl is an instantiation, then record it as requiring
10360 -- instantiation of the corresponding body, except if it is an
10361 -- abbreviated instantiation generated internally for conformance
10362 -- checking purposes only for the case of a formal package
10363 -- declared without a box (see Instantiate_Formal_Package). Such
10364 -- an instantiation does not generate any code (the actual code
10365 -- comes from actual) and thus does not need to be analyzed here.
10367 elsif Nkind (Decl) = N_Package_Instantiation
10368 and then not Is_Internal (Defining_Entity (Decl))
10369 then
10370 Append_Elmt (Decl, Previous_Instances);
10372 -- For a subprogram instantiation, omit instantiations of
10373 -- intrinsic operations (Unchecked_Conversions, etc.) that
10374 -- have no bodies.
10376 elsif Nkind_In (Decl, N_Function_Instantiation,
10377 N_Procedure_Instantiation)
10378 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
10379 then
10380 Append_Elmt (Decl, Previous_Instances);
10382 elsif Nkind (Decl) = N_Package_Declaration then
10383 Collect_Previous_Instances
10384 (Visible_Declarations (Specification (Decl)));
10385 Collect_Previous_Instances
10386 (Private_Declarations (Specification (Decl)));
10388 elsif Nkind (Decl) = N_Package_Body then
10389 Collect_Previous_Instances (Declarations (Decl));
10390 end if;
10392 Next (Decl);
10393 end loop;
10394 end Collect_Previous_Instances;
10396 -- Start of processing for Load_Parent_Of_Generic
10398 begin
10399 if not In_Same_Source_Unit (N, Spec)
10400 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
10401 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
10402 and then not Is_In_Main_Unit (Spec))
10403 then
10404 -- Find body of parent of spec, and analyze it. A special case arises
10405 -- when the parent is an instantiation, that is to say when we are
10406 -- currently instantiating a nested generic. In that case, there is
10407 -- no separate file for the body of the enclosing instance. Instead,
10408 -- the enclosing body must be instantiated as if it were a pending
10409 -- instantiation, in order to produce the body for the nested generic
10410 -- we require now. Note that in that case the generic may be defined
10411 -- in a package body, the instance defined in the same package body,
10412 -- and the original enclosing body may not be in the main unit.
10414 Inst_Node := Empty;
10416 True_Parent := Parent (Spec);
10417 while Present (True_Parent)
10418 and then Nkind (True_Parent) /= N_Compilation_Unit
10419 loop
10420 if Nkind (True_Parent) = N_Package_Declaration
10421 and then
10422 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
10423 then
10424 -- Parent is a compilation unit that is an instantiation.
10425 -- Instantiation node has been replaced with package decl.
10427 Inst_Node := Original_Node (True_Parent);
10428 exit;
10430 elsif Nkind (True_Parent) = N_Package_Declaration
10431 and then Present (Generic_Parent (Specification (True_Parent)))
10432 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
10433 then
10434 -- Parent is an instantiation within another specification.
10435 -- Declaration for instance has been inserted before original
10436 -- instantiation node. A direct link would be preferable?
10438 Inst_Node := Next (True_Parent);
10439 while Present (Inst_Node)
10440 and then Nkind (Inst_Node) /= N_Package_Instantiation
10441 loop
10442 Next (Inst_Node);
10443 end loop;
10445 -- If the instance appears within a generic, and the generic
10446 -- unit is defined within a formal package of the enclosing
10447 -- generic, there is no generic body available, and none
10448 -- needed. A more precise test should be used ???
10450 if No (Inst_Node) then
10451 return;
10452 end if;
10454 exit;
10456 else
10457 True_Parent := Parent (True_Parent);
10458 end if;
10459 end loop;
10461 -- Case where we are currently instantiating a nested generic
10463 if Present (Inst_Node) then
10464 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
10466 -- Instantiation node and declaration of instantiated package
10467 -- were exchanged when only the declaration was needed.
10468 -- Restore instantiation node before proceeding with body.
10470 Set_Unit (Parent (True_Parent), Inst_Node);
10471 end if;
10473 -- Now complete instantiation of enclosing body, if it appears
10474 -- in some other unit. If it appears in the current unit, the
10475 -- body will have been instantiated already.
10477 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
10479 -- We need to determine the expander mode to instantiate the
10480 -- enclosing body. Because the generic body we need may use
10481 -- global entities declared in the enclosing package (including
10482 -- aggregates) it is in general necessary to compile this body
10483 -- with expansion enabled. The exception is if we are within a
10484 -- generic package, in which case the usual generic rule
10485 -- applies.
10487 declare
10488 Exp_Status : Boolean := True;
10489 Scop : Entity_Id;
10491 begin
10492 -- Loop through scopes looking for generic package
10494 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
10495 while Present (Scop)
10496 and then Scop /= Standard_Standard
10497 loop
10498 if Ekind (Scop) = E_Generic_Package then
10499 Exp_Status := False;
10500 exit;
10501 end if;
10503 Scop := Scope (Scop);
10504 end loop;
10506 -- Collect previous instantiations in the unit that
10507 -- contains the desired generic.
10509 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
10510 and then not Body_Optional
10511 then
10512 declare
10513 Decl : Elmt_Id;
10514 Info : Pending_Body_Info;
10515 Par : Node_Id;
10517 begin
10518 Par := Parent (Inst_Node);
10519 while Present (Par) loop
10520 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
10521 Par := Parent (Par);
10522 end loop;
10524 pragma Assert (Present (Par));
10526 if Nkind (Par) = N_Package_Body then
10527 Collect_Previous_Instances (Declarations (Par));
10529 elsif Nkind (Par) = N_Package_Declaration then
10530 Collect_Previous_Instances
10531 (Visible_Declarations (Specification (Par)));
10532 Collect_Previous_Instances
10533 (Private_Declarations (Specification (Par)));
10535 else
10536 -- Enclosing unit is a subprogram body, In this
10537 -- case all instance bodies are processed in order
10538 -- and there is no need to collect them separately.
10540 null;
10541 end if;
10543 Decl := First_Elmt (Previous_Instances);
10544 while Present (Decl) loop
10545 Info :=
10546 (Inst_Node => Node (Decl),
10547 Act_Decl =>
10548 Instance_Spec (Node (Decl)),
10549 Expander_Status => Exp_Status,
10550 Current_Sem_Unit =>
10551 Get_Code_Unit (Sloc (Node (Decl))),
10552 Scope_Suppress => Scope_Suppress,
10553 Local_Suppress_Stack_Top =>
10554 Local_Suppress_Stack_Top);
10556 -- Package instance
10559 Nkind (Node (Decl)) = N_Package_Instantiation
10560 then
10561 Instantiate_Package_Body
10562 (Info, Body_Optional => True);
10564 -- Subprogram instance
10566 else
10567 -- The instance_spec is the wrapper package,
10568 -- and the subprogram declaration is the last
10569 -- declaration in the wrapper.
10571 Info.Act_Decl :=
10572 Last
10573 (Visible_Declarations
10574 (Specification (Info.Act_Decl)));
10576 Instantiate_Subprogram_Body
10577 (Info, Body_Optional => True);
10578 end if;
10580 Next_Elmt (Decl);
10581 end loop;
10582 end;
10583 end if;
10585 Instantiate_Package_Body
10586 (Body_Info =>
10587 ((Inst_Node => Inst_Node,
10588 Act_Decl => True_Parent,
10589 Expander_Status => Exp_Status,
10590 Current_Sem_Unit =>
10591 Get_Code_Unit (Sloc (Inst_Node)),
10592 Scope_Suppress => Scope_Suppress,
10593 Local_Suppress_Stack_Top =>
10594 Local_Suppress_Stack_Top)),
10595 Body_Optional => Body_Optional);
10596 end;
10597 end if;
10599 -- Case where we are not instantiating a nested generic
10601 else
10602 Opt.Style_Check := False;
10603 Expander_Mode_Save_And_Set (True);
10604 Load_Needed_Body (Comp_Unit, OK);
10605 Opt.Style_Check := Save_Style_Check;
10606 Expander_Mode_Restore;
10608 if not OK
10609 and then Unit_Requires_Body (Defining_Entity (Spec))
10610 and then not Body_Optional
10611 then
10612 declare
10613 Bname : constant Unit_Name_Type :=
10614 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
10616 begin
10617 Error_Msg_Unit_1 := Bname;
10618 Error_Msg_N ("this instantiation requires$!", N);
10619 Error_Msg_File_1 := Get_File_Name (Bname, Subunit => False);
10620 Error_Msg_N ("\but file{ was not found!", N);
10621 raise Unrecoverable_Error;
10622 end;
10623 end if;
10624 end if;
10625 end if;
10627 -- If loading parent of the generic caused an instantiation circularity,
10628 -- we abandon compilation at this point, because otherwise in some cases
10629 -- we get into trouble with infinite recursions after this point.
10631 if Circularity_Detected then
10632 raise Unrecoverable_Error;
10633 end if;
10634 end Load_Parent_Of_Generic;
10636 -----------------------
10637 -- Move_Freeze_Nodes --
10638 -----------------------
10640 procedure Move_Freeze_Nodes
10641 (Out_Of : Entity_Id;
10642 After : Node_Id;
10643 L : List_Id)
10645 Decl : Node_Id;
10646 Next_Decl : Node_Id;
10647 Next_Node : Node_Id := After;
10648 Spec : Node_Id;
10650 function Is_Outer_Type (T : Entity_Id) return Boolean;
10651 -- Check whether entity is declared in a scope external to that
10652 -- of the generic unit.
10654 -------------------
10655 -- Is_Outer_Type --
10656 -------------------
10658 function Is_Outer_Type (T : Entity_Id) return Boolean is
10659 Scop : Entity_Id := Scope (T);
10661 begin
10662 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
10663 return True;
10665 else
10666 while Scop /= Standard_Standard loop
10667 if Scop = Out_Of then
10668 return False;
10669 else
10670 Scop := Scope (Scop);
10671 end if;
10672 end loop;
10674 return True;
10675 end if;
10676 end Is_Outer_Type;
10678 -- Start of processing for Move_Freeze_Nodes
10680 begin
10681 if No (L) then
10682 return;
10683 end if;
10685 -- First remove the freeze nodes that may appear before all other
10686 -- declarations.
10688 Decl := First (L);
10689 while Present (Decl)
10690 and then Nkind (Decl) = N_Freeze_Entity
10691 and then Is_Outer_Type (Entity (Decl))
10692 loop
10693 Decl := Remove_Head (L);
10694 Insert_After (Next_Node, Decl);
10695 Set_Analyzed (Decl, False);
10696 Next_Node := Decl;
10697 Decl := First (L);
10698 end loop;
10700 -- Next scan the list of declarations and remove each freeze node that
10701 -- appears ahead of the current node.
10703 while Present (Decl) loop
10704 while Present (Next (Decl))
10705 and then Nkind (Next (Decl)) = N_Freeze_Entity
10706 and then Is_Outer_Type (Entity (Next (Decl)))
10707 loop
10708 Next_Decl := Remove_Next (Decl);
10709 Insert_After (Next_Node, Next_Decl);
10710 Set_Analyzed (Next_Decl, False);
10711 Next_Node := Next_Decl;
10712 end loop;
10714 -- If the declaration is a nested package or concurrent type, then
10715 -- recurse. Nested generic packages will have been processed from the
10716 -- inside out.
10718 if Nkind (Decl) = N_Package_Declaration then
10719 Spec := Specification (Decl);
10721 elsif Nkind (Decl) = N_Task_Type_Declaration then
10722 Spec := Task_Definition (Decl);
10724 elsif Nkind (Decl) = N_Protected_Type_Declaration then
10725 Spec := Protected_Definition (Decl);
10727 else
10728 Spec := Empty;
10729 end if;
10731 if Present (Spec) then
10732 Move_Freeze_Nodes (Out_Of, Next_Node,
10733 Visible_Declarations (Spec));
10734 Move_Freeze_Nodes (Out_Of, Next_Node,
10735 Private_Declarations (Spec));
10736 end if;
10738 Next (Decl);
10739 end loop;
10740 end Move_Freeze_Nodes;
10742 ----------------
10743 -- Next_Assoc --
10744 ----------------
10746 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
10747 begin
10748 return Generic_Renamings.Table (E).Next_In_HTable;
10749 end Next_Assoc;
10751 ------------------------
10752 -- Preanalyze_Actuals --
10753 ------------------------
10755 procedure Preanalyze_Actuals (N : Node_Id) is
10756 Assoc : Node_Id;
10757 Act : Node_Id;
10758 Errs : constant Int := Serious_Errors_Detected;
10760 begin
10761 Assoc := First (Generic_Associations (N));
10762 while Present (Assoc) loop
10763 if Nkind (Assoc) /= N_Others_Choice then
10764 Act := Explicit_Generic_Actual_Parameter (Assoc);
10766 -- Within a nested instantiation, a defaulted actual is an empty
10767 -- association, so nothing to analyze. If the subprogram actual
10768 -- is an attribute, analyze prefix only, because actual is not a
10769 -- complete attribute reference.
10771 -- If actual is an allocator, analyze expression only. The full
10772 -- analysis can generate code, and if instance is a compilation
10773 -- unit we have to wait until the package instance is installed
10774 -- to have a proper place to insert this code.
10776 -- String literals may be operators, but at this point we do not
10777 -- know whether the actual is a formal subprogram or a string.
10779 if No (Act) then
10780 null;
10782 elsif Nkind (Act) = N_Attribute_Reference then
10783 Analyze (Prefix (Act));
10785 elsif Nkind (Act) = N_Explicit_Dereference then
10786 Analyze (Prefix (Act));
10788 elsif Nkind (Act) = N_Allocator then
10789 declare
10790 Expr : constant Node_Id := Expression (Act);
10792 begin
10793 if Nkind (Expr) = N_Subtype_Indication then
10794 Analyze (Subtype_Mark (Expr));
10796 -- Analyze separately each discriminant constraint,
10797 -- when given with a named association.
10799 declare
10800 Constr : Node_Id;
10802 begin
10803 Constr := First (Constraints (Constraint (Expr)));
10804 while Present (Constr) loop
10805 if Nkind (Constr) = N_Discriminant_Association then
10806 Analyze (Expression (Constr));
10807 else
10808 Analyze (Constr);
10809 end if;
10811 Next (Constr);
10812 end loop;
10813 end;
10815 else
10816 Analyze (Expr);
10817 end if;
10818 end;
10820 elsif Nkind (Act) /= N_Operator_Symbol then
10821 Analyze (Act);
10822 end if;
10824 if Errs /= Serious_Errors_Detected then
10826 -- Do a minimal analysis of the generic, to prevent spurious
10827 -- warnings complaining about the generic being unreferenced,
10828 -- before abandoning the instantiation.
10830 Analyze (Name (N));
10832 if Is_Entity_Name (Name (N))
10833 and then Etype (Name (N)) /= Any_Type
10834 then
10835 Generate_Reference (Entity (Name (N)), Name (N));
10836 Set_Is_Instantiated (Entity (Name (N)));
10837 end if;
10839 Abandon_Instantiation (Act);
10840 end if;
10841 end if;
10843 Next (Assoc);
10844 end loop;
10845 end Preanalyze_Actuals;
10847 -------------------
10848 -- Remove_Parent --
10849 -------------------
10851 procedure Remove_Parent (In_Body : Boolean := False) is
10852 S : Entity_Id := Current_Scope;
10853 -- S is the scope containing the instantiation just completed. The
10854 -- scope stack contains the parent instances of the instantiation,
10855 -- followed by the original S.
10857 E : Entity_Id;
10858 P : Entity_Id;
10859 Hidden : Elmt_Id;
10861 begin
10862 -- After child instantiation is complete, remove from scope stack the
10863 -- extra copy of the current scope, and then remove parent instances.
10865 if not In_Body then
10866 Pop_Scope;
10868 while Current_Scope /= S loop
10869 P := Current_Scope;
10870 End_Package_Scope (Current_Scope);
10872 if In_Open_Scopes (P) then
10873 E := First_Entity (P);
10874 while Present (E) loop
10875 Set_Is_Immediately_Visible (E, True);
10876 Next_Entity (E);
10877 end loop;
10879 if Is_Generic_Instance (Current_Scope)
10880 and then P /= Current_Scope
10881 then
10882 -- We are within an instance of some sibling. Retain
10883 -- visibility of parent, for proper subsequent cleanup,
10884 -- and reinstall private declarations as well.
10886 Set_In_Private_Part (P);
10887 Install_Private_Declarations (P);
10888 end if;
10890 -- If the ultimate parent is a top-level unit recorded in
10891 -- Instance_Parent_Unit, then reset its visibility to what
10892 -- it was before instantiation. (It's not clear what the
10893 -- purpose is of testing whether Scope (P) is In_Open_Scopes,
10894 -- but that test was present before the ultimate parent test
10895 -- was added.???)
10897 elsif not In_Open_Scopes (Scope (P))
10898 or else (P = Instance_Parent_Unit
10899 and then not Parent_Unit_Visible)
10900 then
10901 Set_Is_Immediately_Visible (P, False);
10903 -- If the current scope is itself an instantiation of a generic
10904 -- nested within P, and we are in the private part of body of this
10905 -- instantiation, restore the full views of P, that were removed
10906 -- in End_Package_Scope above. This obscure case can occur when a
10907 -- subunit of a generic contains an instance of a child unit of
10908 -- its generic parent unit.
10910 elsif S = Current_Scope
10911 and then Is_Generic_Instance (S)
10912 then
10913 declare
10914 Par : constant Entity_Id :=
10915 Generic_Parent
10916 (Specification (Unit_Declaration_Node (S)));
10917 begin
10918 if Present (Par)
10919 and then P = Scope (Par)
10920 and then (In_Package_Body (S) or else In_Private_Part (S))
10921 then
10922 Set_In_Private_Part (P);
10923 Install_Private_Declarations (P);
10924 end if;
10925 end;
10926 end if;
10927 end loop;
10929 -- Reset visibility of entities in the enclosing scope
10931 Set_Is_Hidden_Open_Scope (Current_Scope, False);
10933 Hidden := First_Elmt (Hidden_Entities);
10934 while Present (Hidden) loop
10935 Set_Is_Immediately_Visible (Node (Hidden), True);
10936 Next_Elmt (Hidden);
10937 end loop;
10939 else
10940 -- Each body is analyzed separately, and there is no context
10941 -- that needs preserving from one body instance to the next,
10942 -- so remove all parent scopes that have been installed.
10944 while Present (S) loop
10945 End_Package_Scope (S);
10946 Set_Is_Immediately_Visible (S, False);
10947 S := Current_Scope;
10948 exit when S = Standard_Standard;
10949 end loop;
10950 end if;
10951 end Remove_Parent;
10953 -----------------
10954 -- Restore_Env --
10955 -----------------
10957 procedure Restore_Env is
10958 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
10960 begin
10961 if No (Current_Instantiated_Parent.Act_Id) then
10963 -- Restore environment after subprogram inlining
10965 Restore_Private_Views (Empty);
10966 end if;
10968 Current_Instantiated_Parent := Saved.Instantiated_Parent;
10969 Exchanged_Views := Saved.Exchanged_Views;
10970 Hidden_Entities := Saved.Hidden_Entities;
10971 Current_Sem_Unit := Saved.Current_Sem_Unit;
10972 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
10973 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
10975 Restore_Opt_Config_Switches (Saved.Switches);
10977 Instance_Envs.Decrement_Last;
10978 end Restore_Env;
10980 ---------------------------
10981 -- Restore_Private_Views --
10982 ---------------------------
10984 procedure Restore_Private_Views
10985 (Pack_Id : Entity_Id;
10986 Is_Package : Boolean := True)
10988 M : Elmt_Id;
10989 E : Entity_Id;
10990 Typ : Entity_Id;
10991 Dep_Elmt : Elmt_Id;
10992 Dep_Typ : Node_Id;
10994 procedure Restore_Nested_Formal (Formal : Entity_Id);
10995 -- Hide the generic formals of formal packages declared with box
10996 -- which were reachable in the current instantiation.
10998 ---------------------------
10999 -- Restore_Nested_Formal --
11000 ---------------------------
11002 procedure Restore_Nested_Formal (Formal : Entity_Id) is
11003 Ent : Entity_Id;
11005 begin
11006 if Present (Renamed_Object (Formal))
11007 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
11008 then
11009 return;
11011 elsif Present (Associated_Formal_Package (Formal)) then
11012 Ent := First_Entity (Formal);
11013 while Present (Ent) loop
11014 exit when Ekind (Ent) = E_Package
11015 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
11017 Set_Is_Hidden (Ent);
11018 Set_Is_Potentially_Use_Visible (Ent, False);
11020 -- If package, then recurse
11022 if Ekind (Ent) = E_Package then
11023 Restore_Nested_Formal (Ent);
11024 end if;
11026 Next_Entity (Ent);
11027 end loop;
11028 end if;
11029 end Restore_Nested_Formal;
11031 -- Start of processing for Restore_Private_Views
11033 begin
11034 M := First_Elmt (Exchanged_Views);
11035 while Present (M) loop
11036 Typ := Node (M);
11038 -- Subtypes of types whose views have been exchanged, and that
11039 -- are defined within the instance, were not on the list of
11040 -- Private_Dependents on entry to the instance, so they have to
11041 -- be exchanged explicitly now, in order to remain consistent with
11042 -- the view of the parent type.
11044 if Ekind (Typ) = E_Private_Type
11045 or else Ekind (Typ) = E_Limited_Private_Type
11046 or else Ekind (Typ) = E_Record_Type_With_Private
11047 then
11048 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
11049 while Present (Dep_Elmt) loop
11050 Dep_Typ := Node (Dep_Elmt);
11052 if Scope (Dep_Typ) = Pack_Id
11053 and then Present (Full_View (Dep_Typ))
11054 then
11055 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
11056 Exchange_Declarations (Dep_Typ);
11057 end if;
11059 Next_Elmt (Dep_Elmt);
11060 end loop;
11061 end if;
11063 Exchange_Declarations (Node (M));
11064 Next_Elmt (M);
11065 end loop;
11067 if No (Pack_Id) then
11068 return;
11069 end if;
11071 -- Make the generic formal parameters private, and make the formal
11072 -- types into subtypes of the actuals again.
11074 E := First_Entity (Pack_Id);
11075 while Present (E) loop
11076 Set_Is_Hidden (E, True);
11078 if Is_Type (E)
11079 and then Nkind (Parent (E)) = N_Subtype_Declaration
11080 then
11081 Set_Is_Generic_Actual_Type (E, False);
11083 -- An unusual case of aliasing: the actual may also be directly
11084 -- visible in the generic, and be private there, while it is fully
11085 -- visible in the context of the instance. The internal subtype
11086 -- is private in the instance, but has full visibility like its
11087 -- parent in the enclosing scope. This enforces the invariant that
11088 -- the privacy status of all private dependents of a type coincide
11089 -- with that of the parent type. This can only happen when a
11090 -- generic child unit is instantiated within sibling.
11092 if Is_Private_Type (E)
11093 and then not Is_Private_Type (Etype (E))
11094 then
11095 Exchange_Declarations (E);
11096 end if;
11098 elsif Ekind (E) = E_Package then
11100 -- The end of the renaming list is the renaming of the generic
11101 -- package itself. If the instance is a subprogram, all entities
11102 -- in the corresponding package are renamings. If this entity is
11103 -- a formal package, make its own formals private as well. The
11104 -- actual in this case is itself the renaming of an instantiation.
11105 -- If the entity is not a package renaming, it is the entity
11106 -- created to validate formal package actuals: ignore.
11108 -- If the actual is itself a formal package for the enclosing
11109 -- generic, or the actual for such a formal package, it remains
11110 -- visible on exit from the instance, and therefore nothing needs
11111 -- to be done either, except to keep it accessible.
11113 if Is_Package
11114 and then Renamed_Object (E) = Pack_Id
11115 then
11116 exit;
11118 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
11119 null;
11121 elsif Denotes_Formal_Package (Renamed_Object (E), True) then
11122 Set_Is_Hidden (E, False);
11124 else
11125 declare
11126 Act_P : constant Entity_Id := Renamed_Object (E);
11127 Id : Entity_Id;
11129 begin
11130 Id := First_Entity (Act_P);
11131 while Present (Id)
11132 and then Id /= First_Private_Entity (Act_P)
11133 loop
11134 exit when Ekind (Id) = E_Package
11135 and then Renamed_Object (Id) = Act_P;
11137 Set_Is_Hidden (Id, True);
11138 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
11140 if Ekind (Id) = E_Package then
11141 Restore_Nested_Formal (Id);
11142 end if;
11144 Next_Entity (Id);
11145 end loop;
11146 end;
11147 end if;
11148 end if;
11150 Next_Entity (E);
11151 end loop;
11152 end Restore_Private_Views;
11154 --------------
11155 -- Save_Env --
11156 --------------
11158 procedure Save_Env
11159 (Gen_Unit : Entity_Id;
11160 Act_Unit : Entity_Id)
11162 begin
11163 Init_Env;
11164 Set_Instance_Env (Gen_Unit, Act_Unit);
11165 end Save_Env;
11167 ----------------------------
11168 -- Save_Global_References --
11169 ----------------------------
11171 procedure Save_Global_References (N : Node_Id) is
11172 Gen_Scope : Entity_Id;
11173 E : Entity_Id;
11174 N2 : Node_Id;
11176 function Is_Global (E : Entity_Id) return Boolean;
11177 -- Check whether entity is defined outside of generic unit. Examine the
11178 -- scope of an entity, and the scope of the scope, etc, until we find
11179 -- either Standard, in which case the entity is global, or the generic
11180 -- unit itself, which indicates that the entity is local. If the entity
11181 -- is the generic unit itself, as in the case of a recursive call, or
11182 -- the enclosing generic unit, if different from the current scope, then
11183 -- it is local as well, because it will be replaced at the point of
11184 -- instantiation. On the other hand, if it is a reference to a child
11185 -- unit of a common ancestor, which appears in an instantiation, it is
11186 -- global because it is used to denote a specific compilation unit at
11187 -- the time the instantiations will be analyzed.
11189 procedure Reset_Entity (N : Node_Id);
11190 -- Save semantic information on global entity, so that it is not
11191 -- resolved again at instantiation time.
11193 procedure Save_Entity_Descendants (N : Node_Id);
11194 -- Apply Save_Global_References to the two syntactic descendants of
11195 -- non-terminal nodes that carry an Associated_Node and are processed
11196 -- through Reset_Entity. Once the global entity (if any) has been
11197 -- captured together with its type, only two syntactic descendants need
11198 -- to be traversed to complete the processing of the tree rooted at N.
11199 -- This applies to Selected_Components, Expanded_Names, and to Operator
11200 -- nodes. N can also be a character literal, identifier, or operator
11201 -- symbol node, but the call has no effect in these cases.
11203 procedure Save_Global_Defaults (N1, N2 : Node_Id);
11204 -- Default actuals in nested instances must be handled specially
11205 -- because there is no link to them from the original tree. When an
11206 -- actual subprogram is given by a default, we add an explicit generic
11207 -- association for it in the instantiation node. When we save the
11208 -- global references on the name of the instance, we recover the list
11209 -- of generic associations, and add an explicit one to the original
11210 -- generic tree, through which a global actual can be preserved.
11211 -- Similarly, if a child unit is instantiated within a sibling, in the
11212 -- context of the parent, we must preserve the identifier of the parent
11213 -- so that it can be properly resolved in a subsequent instantiation.
11215 procedure Save_Global_Descendant (D : Union_Id);
11216 -- Apply Save_Global_References recursively to the descendents of the
11217 -- current node.
11219 procedure Save_References (N : Node_Id);
11220 -- This is the recursive procedure that does the work, once the
11221 -- enclosing generic scope has been established.
11223 ---------------
11224 -- Is_Global --
11225 ---------------
11227 function Is_Global (E : Entity_Id) return Boolean is
11228 Se : Entity_Id;
11230 function Is_Instance_Node (Decl : Node_Id) return Boolean;
11231 -- Determine whether the parent node of a reference to a child unit
11232 -- denotes an instantiation or a formal package, in which case the
11233 -- reference to the child unit is global, even if it appears within
11234 -- the current scope (e.g. when the instance appears within the body
11235 -- of an ancestor).
11237 ----------------------
11238 -- Is_Instance_Node --
11239 ----------------------
11241 function Is_Instance_Node (Decl : Node_Id) return Boolean is
11242 begin
11243 return (Nkind (Decl) in N_Generic_Instantiation
11244 or else
11245 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration);
11246 end Is_Instance_Node;
11248 -- Start of processing for Is_Global
11250 begin
11251 if E = Gen_Scope then
11252 return False;
11254 elsif E = Standard_Standard then
11255 return True;
11257 elsif Is_Child_Unit (E)
11258 and then (Is_Instance_Node (Parent (N2))
11259 or else (Nkind (Parent (N2)) = N_Expanded_Name
11260 and then N2 = Selector_Name (Parent (N2))
11261 and then
11262 Is_Instance_Node (Parent (Parent (N2)))))
11263 then
11264 return True;
11266 else
11267 Se := Scope (E);
11268 while Se /= Gen_Scope loop
11269 if Se = Standard_Standard then
11270 return True;
11271 else
11272 Se := Scope (Se);
11273 end if;
11274 end loop;
11276 return False;
11277 end if;
11278 end Is_Global;
11280 ------------------
11281 -- Reset_Entity --
11282 ------------------
11284 procedure Reset_Entity (N : Node_Id) is
11286 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
11287 -- If the type of N2 is global to the generic unit. Save
11288 -- the type in the generic node.
11290 function Top_Ancestor (E : Entity_Id) return Entity_Id;
11291 -- Find the ultimate ancestor of the current unit. If it is
11292 -- not a generic unit, then the name of the current unit
11293 -- in the prefix of an expanded name must be replaced with
11294 -- its generic homonym to ensure that it will be properly
11295 -- resolved in an instance.
11297 ---------------------
11298 -- Set_Global_Type --
11299 ---------------------
11301 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
11302 Typ : constant Entity_Id := Etype (N2);
11304 begin
11305 Set_Etype (N, Typ);
11307 if Entity (N) /= N2
11308 and then Has_Private_View (Entity (N))
11309 then
11310 -- If the entity of N is not the associated node, this is
11311 -- a nested generic and it has an associated node as well,
11312 -- whose type is already the full view (see below). Indicate
11313 -- that the original node has a private view.
11315 Set_Has_Private_View (N);
11316 end if;
11318 -- If not a private type, nothing else to do
11320 if not Is_Private_Type (Typ) then
11321 if Is_Array_Type (Typ)
11322 and then Is_Private_Type (Component_Type (Typ))
11323 then
11324 Set_Has_Private_View (N);
11325 end if;
11327 -- If it is a derivation of a private type in a context where
11328 -- no full view is needed, nothing to do either.
11330 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
11331 null;
11333 -- Otherwise mark the type for flipping and use the full_view
11334 -- when available.
11336 else
11337 Set_Has_Private_View (N);
11339 if Present (Full_View (Typ)) then
11340 Set_Etype (N2, Full_View (Typ));
11341 end if;
11342 end if;
11343 end Set_Global_Type;
11345 ------------------
11346 -- Top_Ancestor --
11347 ------------------
11349 function Top_Ancestor (E : Entity_Id) return Entity_Id is
11350 Par : Entity_Id;
11352 begin
11353 Par := E;
11354 while Is_Child_Unit (Par) loop
11355 Par := Scope (Par);
11356 end loop;
11358 return Par;
11359 end Top_Ancestor;
11361 -- Start of processing for Reset_Entity
11363 begin
11364 N2 := Get_Associated_Node (N);
11365 E := Entity (N2);
11367 if Present (E) then
11368 if Is_Global (E) then
11369 Set_Global_Type (N, N2);
11371 elsif Nkind (N) = N_Op_Concat
11372 and then Is_Generic_Type (Etype (N2))
11373 and then
11374 (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
11375 or else Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
11376 and then Is_Intrinsic_Subprogram (E)
11377 then
11378 null;
11380 else
11381 -- Entity is local. Mark generic node as unresolved.
11382 -- Note that now it does not have an entity.
11384 Set_Associated_Node (N, Empty);
11385 Set_Etype (N, Empty);
11386 end if;
11388 if Nkind (Parent (N)) in N_Generic_Instantiation
11389 and then N = Name (Parent (N))
11390 then
11391 Save_Global_Defaults (Parent (N), Parent (N2));
11392 end if;
11394 elsif Nkind (Parent (N)) = N_Selected_Component
11395 and then Nkind (Parent (N2)) = N_Expanded_Name
11396 then
11397 if Is_Global (Entity (Parent (N2))) then
11398 Change_Selected_Component_To_Expanded_Name (Parent (N));
11399 Set_Associated_Node (Parent (N), Parent (N2));
11400 Set_Global_Type (Parent (N), Parent (N2));
11401 Save_Entity_Descendants (N);
11403 -- If this is a reference to the current generic entity, replace
11404 -- by the name of the generic homonym of the current package. This
11405 -- is because in an instantiation Par.P.Q will not resolve to the
11406 -- name of the instance, whose enclosing scope is not necessarily
11407 -- Par. We use the generic homonym rather that the name of the
11408 -- generic itself, because it may be hidden by a local
11409 -- declaration.
11411 elsif In_Open_Scopes (Entity (Parent (N2)))
11412 and then not
11413 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
11414 then
11415 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
11416 Rewrite (Parent (N),
11417 Make_Identifier (Sloc (N),
11418 Chars =>
11419 Chars (Generic_Homonym (Entity (Parent (N2))))));
11420 else
11421 Rewrite (Parent (N),
11422 Make_Identifier (Sloc (N),
11423 Chars => Chars (Selector_Name (Parent (N2)))));
11424 end if;
11425 end if;
11427 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
11428 and then Parent (N) = Name (Parent (Parent (N)))
11429 then
11430 Save_Global_Defaults
11431 (Parent (Parent (N)), Parent (Parent ((N2))));
11432 end if;
11434 -- A selected component may denote a static constant that has been
11435 -- folded. If the static constant is global to the generic, capture
11436 -- its value. Otherwise the folding will happen in any instantiation,
11438 elsif Nkind (Parent (N)) = N_Selected_Component
11439 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
11440 then
11441 if Present (Entity (Original_Node (Parent (N2))))
11442 and then Is_Global (Entity (Original_Node (Parent (N2))))
11443 then
11444 Rewrite (Parent (N), New_Copy (Parent (N2)));
11445 Set_Analyzed (Parent (N), False);
11447 else
11448 null;
11449 end if;
11451 -- A selected component may be transformed into a parameterless
11452 -- function call. If the called entity is global, rewrite the node
11453 -- appropriately, i.e. as an extended name for the global entity.
11455 elsif Nkind (Parent (N)) = N_Selected_Component
11456 and then Nkind (Parent (N2)) = N_Function_Call
11457 and then N = Selector_Name (Parent (N))
11458 then
11459 if No (Parameter_Associations (Parent (N2))) then
11460 if Is_Global (Entity (Name (Parent (N2)))) then
11461 Change_Selected_Component_To_Expanded_Name (Parent (N));
11462 Set_Associated_Node (Parent (N), Name (Parent (N2)));
11463 Set_Global_Type (Parent (N), Name (Parent (N2)));
11464 Save_Entity_Descendants (N);
11466 else
11467 Set_Associated_Node (N, Empty);
11468 Set_Etype (N, Empty);
11469 end if;
11471 -- In Ada 2005, X.F may be a call to a primitive operation,
11472 -- rewritten as F (X). This rewriting will be done again in an
11473 -- instance, so keep the original node. Global entities will be
11474 -- captured as for other constructs.
11476 else
11477 null;
11478 end if;
11480 -- Entity is local. Reset in generic unit, so that node is resolved
11481 -- anew at the point of instantiation.
11483 else
11484 Set_Associated_Node (N, Empty);
11485 Set_Etype (N, Empty);
11486 end if;
11487 end Reset_Entity;
11489 -----------------------------
11490 -- Save_Entity_Descendants --
11491 -----------------------------
11493 procedure Save_Entity_Descendants (N : Node_Id) is
11494 begin
11495 case Nkind (N) is
11496 when N_Binary_Op =>
11497 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
11498 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
11500 when N_Unary_Op =>
11501 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
11503 when N_Expanded_Name | N_Selected_Component =>
11504 Save_Global_Descendant (Union_Id (Prefix (N)));
11505 Save_Global_Descendant (Union_Id (Selector_Name (N)));
11507 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
11508 null;
11510 when others =>
11511 raise Program_Error;
11512 end case;
11513 end Save_Entity_Descendants;
11515 --------------------------
11516 -- Save_Global_Defaults --
11517 --------------------------
11519 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
11520 Loc : constant Source_Ptr := Sloc (N1);
11521 Assoc2 : constant List_Id := Generic_Associations (N2);
11522 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
11523 Assoc1 : List_Id;
11524 Act1 : Node_Id;
11525 Act2 : Node_Id;
11526 Def : Node_Id;
11527 Ndec : Node_Id;
11528 Subp : Entity_Id;
11529 Actual : Entity_Id;
11531 begin
11532 Assoc1 := Generic_Associations (N1);
11534 if Present (Assoc1) then
11535 Act1 := First (Assoc1);
11536 else
11537 Act1 := Empty;
11538 Set_Generic_Associations (N1, New_List);
11539 Assoc1 := Generic_Associations (N1);
11540 end if;
11542 if Present (Assoc2) then
11543 Act2 := First (Assoc2);
11544 else
11545 return;
11546 end if;
11548 while Present (Act1) and then Present (Act2) loop
11549 Next (Act1);
11550 Next (Act2);
11551 end loop;
11553 -- Find the associations added for default subprograms
11555 if Present (Act2) then
11556 while Nkind (Act2) /= N_Generic_Association
11557 or else No (Entity (Selector_Name (Act2)))
11558 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
11559 loop
11560 Next (Act2);
11561 end loop;
11563 -- Add a similar association if the default is global. The
11564 -- renaming declaration for the actual has been analyzed, and
11565 -- its alias is the program it renames. Link the actual in the
11566 -- original generic tree with the node in the analyzed tree.
11568 while Present (Act2) loop
11569 Subp := Entity (Selector_Name (Act2));
11570 Def := Explicit_Generic_Actual_Parameter (Act2);
11572 -- Following test is defence against rubbish errors
11574 if No (Alias (Subp)) then
11575 return;
11576 end if;
11578 -- Retrieve the resolved actual from the renaming declaration
11579 -- created for the instantiated formal.
11581 Actual := Entity (Name (Parent (Parent (Subp))));
11582 Set_Entity (Def, Actual);
11583 Set_Etype (Def, Etype (Actual));
11585 if Is_Global (Actual) then
11586 Ndec :=
11587 Make_Generic_Association (Loc,
11588 Selector_Name => New_Occurrence_Of (Subp, Loc),
11589 Explicit_Generic_Actual_Parameter =>
11590 New_Occurrence_Of (Actual, Loc));
11592 Set_Associated_Node
11593 (Explicit_Generic_Actual_Parameter (Ndec), Def);
11595 Append (Ndec, Assoc1);
11597 -- If there are other defaults, add a dummy association in case
11598 -- there are other defaulted formals with the same name.
11600 elsif Present (Next (Act2)) then
11601 Ndec :=
11602 Make_Generic_Association (Loc,
11603 Selector_Name => New_Occurrence_Of (Subp, Loc),
11604 Explicit_Generic_Actual_Parameter => Empty);
11606 Append (Ndec, Assoc1);
11607 end if;
11609 Next (Act2);
11610 end loop;
11611 end if;
11613 if Nkind (Name (N1)) = N_Identifier
11614 and then Is_Child_Unit (Gen_Id)
11615 and then Is_Global (Gen_Id)
11616 and then Is_Generic_Unit (Scope (Gen_Id))
11617 and then In_Open_Scopes (Scope (Gen_Id))
11618 then
11619 -- This is an instantiation of a child unit within a sibling,
11620 -- so that the generic parent is in scope. An eventual instance
11621 -- must occur within the scope of an instance of the parent.
11622 -- Make name in instance into an expanded name, to preserve the
11623 -- identifier of the parent, so it can be resolved subsequently.
11625 Rewrite (Name (N2),
11626 Make_Expanded_Name (Loc,
11627 Chars => Chars (Gen_Id),
11628 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
11629 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
11630 Set_Entity (Name (N2), Gen_Id);
11632 Rewrite (Name (N1),
11633 Make_Expanded_Name (Loc,
11634 Chars => Chars (Gen_Id),
11635 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
11636 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
11638 Set_Associated_Node (Name (N1), Name (N2));
11639 Set_Associated_Node (Prefix (Name (N1)), Empty);
11640 Set_Associated_Node
11641 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
11642 Set_Etype (Name (N1), Etype (Gen_Id));
11643 end if;
11645 end Save_Global_Defaults;
11647 ----------------------------
11648 -- Save_Global_Descendant --
11649 ----------------------------
11651 procedure Save_Global_Descendant (D : Union_Id) is
11652 N1 : Node_Id;
11654 begin
11655 if D in Node_Range then
11656 if D = Union_Id (Empty) then
11657 null;
11659 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
11660 Save_References (Node_Id (D));
11661 end if;
11663 elsif D in List_Range then
11664 if D = Union_Id (No_List)
11665 or else Is_Empty_List (List_Id (D))
11666 then
11667 null;
11669 else
11670 N1 := First (List_Id (D));
11671 while Present (N1) loop
11672 Save_References (N1);
11673 Next (N1);
11674 end loop;
11675 end if;
11677 -- Element list or other non-node field, nothing to do
11679 else
11680 null;
11681 end if;
11682 end Save_Global_Descendant;
11684 ---------------------
11685 -- Save_References --
11686 ---------------------
11688 -- This is the recursive procedure that does the work, once the
11689 -- enclosing generic scope has been established. We have to treat
11690 -- specially a number of node rewritings that are required by semantic
11691 -- processing and which change the kind of nodes in the generic copy:
11692 -- typically constant-folding, replacing an operator node by a string
11693 -- literal, or a selected component by an expanded name. In each of
11694 -- those cases, the transformation is propagated to the generic unit.
11696 procedure Save_References (N : Node_Id) is
11697 begin
11698 if N = Empty then
11699 null;
11701 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
11702 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
11703 Reset_Entity (N);
11705 elsif Nkind (N) = N_Operator_Symbol
11706 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
11707 then
11708 Change_Operator_Symbol_To_String_Literal (N);
11709 end if;
11711 elsif Nkind (N) in N_Op then
11712 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
11713 if Nkind (N) = N_Op_Concat then
11714 Set_Is_Component_Left_Opnd (N,
11715 Is_Component_Left_Opnd (Get_Associated_Node (N)));
11717 Set_Is_Component_Right_Opnd (N,
11718 Is_Component_Right_Opnd (Get_Associated_Node (N)));
11719 end if;
11721 Reset_Entity (N);
11723 else
11724 -- Node may be transformed into call to a user-defined operator
11726 N2 := Get_Associated_Node (N);
11728 if Nkind (N2) = N_Function_Call then
11729 E := Entity (Name (N2));
11731 if Present (E)
11732 and then Is_Global (E)
11733 then
11734 Set_Etype (N, Etype (N2));
11735 else
11736 Set_Associated_Node (N, Empty);
11737 Set_Etype (N, Empty);
11738 end if;
11740 elsif Nkind_In (N2, N_Integer_Literal,
11741 N_Real_Literal,
11742 N_String_Literal)
11743 then
11744 if Present (Original_Node (N2))
11745 and then Nkind (Original_Node (N2)) = Nkind (N)
11746 then
11748 -- Operation was constant-folded. Whenever possible,
11749 -- recover semantic information from unfolded node,
11750 -- for ASIS use.
11752 Set_Associated_Node (N, Original_Node (N2));
11754 if Nkind (N) = N_Op_Concat then
11755 Set_Is_Component_Left_Opnd (N,
11756 Is_Component_Left_Opnd (Get_Associated_Node (N)));
11757 Set_Is_Component_Right_Opnd (N,
11758 Is_Component_Right_Opnd (Get_Associated_Node (N)));
11759 end if;
11761 Reset_Entity (N);
11763 else
11764 -- If original node is already modified, propagate
11765 -- constant-folding to template.
11767 Rewrite (N, New_Copy (N2));
11768 Set_Analyzed (N, False);
11769 end if;
11771 elsif Nkind (N2) = N_Identifier
11772 and then Ekind (Entity (N2)) = E_Enumeration_Literal
11773 then
11774 -- Same if call was folded into a literal, but in this case
11775 -- retain the entity to avoid spurious ambiguities if id is
11776 -- overloaded at the point of instantiation or inlining.
11778 Rewrite (N, New_Copy (N2));
11779 Set_Analyzed (N, False);
11780 end if;
11781 end if;
11783 -- Complete operands check if node has not been constant-folded
11785 if Nkind (N) in N_Op then
11786 Save_Entity_Descendants (N);
11787 end if;
11789 elsif Nkind (N) = N_Identifier then
11790 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
11792 -- If this is a discriminant reference, always save it. It is
11793 -- used in the instance to find the corresponding discriminant
11794 -- positionally rather than by name.
11796 Set_Original_Discriminant
11797 (N, Original_Discriminant (Get_Associated_Node (N)));
11798 Reset_Entity (N);
11800 else
11801 N2 := Get_Associated_Node (N);
11803 if Nkind (N2) = N_Function_Call then
11804 E := Entity (Name (N2));
11806 -- Name resolves to a call to parameterless function. If
11807 -- original entity is global, mark node as resolved.
11809 if Present (E)
11810 and then Is_Global (E)
11811 then
11812 Set_Etype (N, Etype (N2));
11813 else
11814 Set_Associated_Node (N, Empty);
11815 Set_Etype (N, Empty);
11816 end if;
11818 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
11819 and then Is_Entity_Name (Original_Node (N2))
11820 then
11821 -- Name resolves to named number that is constant-folded,
11822 -- We must preserve the original name for ASIS use, and
11823 -- undo the constant-folding, which will be repeated in
11824 -- each instance.
11826 Set_Associated_Node (N, Original_Node (N2));
11827 Reset_Entity (N);
11829 elsif Nkind (N2) = N_String_Literal then
11831 -- Name resolves to string literal. Perform the same
11832 -- replacement in generic.
11834 Rewrite (N, New_Copy (N2));
11836 elsif Nkind (N2) = N_Explicit_Dereference then
11838 -- An identifier is rewritten as a dereference if it is
11839 -- the prefix in a selected component, and it denotes an
11840 -- access to a composite type, or a parameterless function
11841 -- call that returns an access type.
11843 -- Check whether corresponding entity in prefix is global
11845 if Is_Entity_Name (Prefix (N2))
11846 and then Present (Entity (Prefix (N2)))
11847 and then Is_Global (Entity (Prefix (N2)))
11848 then
11849 Rewrite (N,
11850 Make_Explicit_Dereference (Sloc (N),
11851 Prefix => Make_Identifier (Sloc (N),
11852 Chars => Chars (N))));
11853 Set_Associated_Node (Prefix (N), Prefix (N2));
11855 elsif Nkind (Prefix (N2)) = N_Function_Call
11856 and then Is_Global (Entity (Name (Prefix (N2))))
11857 then
11858 Rewrite (N,
11859 Make_Explicit_Dereference (Sloc (N),
11860 Prefix => Make_Function_Call (Sloc (N),
11861 Name =>
11862 Make_Identifier (Sloc (N),
11863 Chars => Chars (N)))));
11865 Set_Associated_Node
11866 (Name (Prefix (N)), Name (Prefix (N2)));
11868 else
11869 Set_Associated_Node (N, Empty);
11870 Set_Etype (N, Empty);
11871 end if;
11873 -- The subtype mark of a nominally unconstrained object is
11874 -- rewritten as a subtype indication using the bounds of the
11875 -- expression. Recover the original subtype mark.
11877 elsif Nkind (N2) = N_Subtype_Indication
11878 and then Is_Entity_Name (Original_Node (N2))
11879 then
11880 Set_Associated_Node (N, Original_Node (N2));
11881 Reset_Entity (N);
11883 else
11884 null;
11885 end if;
11886 end if;
11888 elsif Nkind (N) in N_Entity then
11889 null;
11891 else
11892 declare
11893 Loc : constant Source_Ptr := Sloc (N);
11894 Qual : Node_Id := Empty;
11895 Typ : Entity_Id := Empty;
11896 Nam : Node_Id;
11898 use Atree.Unchecked_Access;
11899 -- This code section is part of implementing an untyped tree
11900 -- traversal, so it needs direct access to node fields.
11902 begin
11903 if Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
11904 N2 := Get_Associated_Node (N);
11906 if No (N2) then
11907 Typ := Empty;
11908 else
11909 Typ := Etype (N2);
11911 -- In an instance within a generic, use the name of the
11912 -- actual and not the original generic parameter. If the
11913 -- actual is global in the current generic it must be
11914 -- preserved for its instantiation.
11916 if Nkind (Parent (Typ)) = N_Subtype_Declaration
11917 and then
11918 Present (Generic_Parent_Type (Parent (Typ)))
11919 then
11920 Typ := Base_Type (Typ);
11921 Set_Etype (N2, Typ);
11922 end if;
11923 end if;
11925 if No (N2)
11926 or else No (Typ)
11927 or else not Is_Global (Typ)
11928 then
11929 Set_Associated_Node (N, Empty);
11931 -- If the aggregate is an actual in a call, it has been
11932 -- resolved in the current context, to some local type.
11933 -- The enclosing call may have been disambiguated by the
11934 -- aggregate, and this disambiguation might fail at
11935 -- instantiation time because the type to which the
11936 -- aggregate did resolve is not preserved. In order to
11937 -- preserve some of this information, we wrap the
11938 -- aggregate in a qualified expression, using the id of
11939 -- its type. For further disambiguation we qualify the
11940 -- type name with its scope (if visible) because both
11941 -- id's will have corresponding entities in an instance.
11942 -- This resolves most of the problems with missing type
11943 -- information on aggregates in instances.
11945 if Nkind (N2) = Nkind (N)
11946 and then
11947 Nkind_In (Parent (N2), N_Procedure_Call_Statement,
11948 N_Function_Call)
11949 and then Comes_From_Source (Typ)
11950 then
11951 if Is_Immediately_Visible (Scope (Typ)) then
11952 Nam := Make_Selected_Component (Loc,
11953 Prefix =>
11954 Make_Identifier (Loc, Chars (Scope (Typ))),
11955 Selector_Name =>
11956 Make_Identifier (Loc, Chars (Typ)));
11957 else
11958 Nam := Make_Identifier (Loc, Chars (Typ));
11959 end if;
11961 Qual :=
11962 Make_Qualified_Expression (Loc,
11963 Subtype_Mark => Nam,
11964 Expression => Relocate_Node (N));
11965 end if;
11966 end if;
11968 Save_Global_Descendant (Field1 (N));
11969 Save_Global_Descendant (Field2 (N));
11970 Save_Global_Descendant (Field3 (N));
11971 Save_Global_Descendant (Field5 (N));
11973 if Present (Qual) then
11974 Rewrite (N, Qual);
11975 end if;
11977 -- All other cases than aggregates
11979 else
11980 Save_Global_Descendant (Field1 (N));
11981 Save_Global_Descendant (Field2 (N));
11982 Save_Global_Descendant (Field3 (N));
11983 Save_Global_Descendant (Field4 (N));
11984 Save_Global_Descendant (Field5 (N));
11985 end if;
11986 end;
11987 end if;
11988 end Save_References;
11990 -- Start of processing for Save_Global_References
11992 begin
11993 Gen_Scope := Current_Scope;
11995 -- If the generic unit is a child unit, references to entities in the
11996 -- parent are treated as local, because they will be resolved anew in
11997 -- the context of the instance of the parent.
11999 while Is_Child_Unit (Gen_Scope)
12000 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
12001 loop
12002 Gen_Scope := Scope (Gen_Scope);
12003 end loop;
12005 Save_References (N);
12006 end Save_Global_References;
12008 --------------------------------------
12009 -- Set_Copied_Sloc_For_Inlined_Body --
12010 --------------------------------------
12012 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
12013 begin
12014 Create_Instantiation_Source (N, E, True, S_Adjustment);
12015 end Set_Copied_Sloc_For_Inlined_Body;
12017 ---------------------
12018 -- Set_Instance_Of --
12019 ---------------------
12021 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
12022 begin
12023 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
12024 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
12025 Generic_Renamings.Increment_Last;
12026 end Set_Instance_Of;
12028 --------------------
12029 -- Set_Next_Assoc --
12030 --------------------
12032 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
12033 begin
12034 Generic_Renamings.Table (E).Next_In_HTable := Next;
12035 end Set_Next_Assoc;
12037 -------------------
12038 -- Start_Generic --
12039 -------------------
12041 procedure Start_Generic is
12042 begin
12043 -- ??? More things could be factored out in this routine.
12044 -- Should probably be done at a later stage.
12046 Generic_Flags.Append (Inside_A_Generic);
12047 Inside_A_Generic := True;
12049 Expander_Mode_Save_And_Set (False);
12050 end Start_Generic;
12052 ----------------------
12053 -- Set_Instance_Env --
12054 ----------------------
12056 procedure Set_Instance_Env
12057 (Gen_Unit : Entity_Id;
12058 Act_Unit : Entity_Id)
12060 begin
12061 -- Regardless of the current mode, predefined units are analyzed in
12062 -- the most current Ada mode, and earlier version Ada checks do not
12063 -- apply to predefined units. Nothing needs to be done for non-internal
12064 -- units. These are always analyzed in the current mode.
12066 if Is_Internal_File_Name
12067 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
12068 Renamings_Included => True)
12069 then
12070 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
12071 end if;
12073 Current_Instantiated_Parent := (Gen_Unit, Act_Unit, Assoc_Null);
12074 end Set_Instance_Env;
12076 -----------------
12077 -- Switch_View --
12078 -----------------
12080 procedure Switch_View (T : Entity_Id) is
12081 BT : constant Entity_Id := Base_Type (T);
12082 Priv_Elmt : Elmt_Id := No_Elmt;
12083 Priv_Sub : Entity_Id;
12085 begin
12086 -- T may be private but its base type may have been exchanged through
12087 -- some other occurrence, in which case there is nothing to switch
12088 -- besides T itself. Note that a private dependent subtype of a private
12089 -- type might not have been switched even if the base type has been,
12090 -- because of the last branch of Check_Private_View (see comment there).
12092 if not Is_Private_Type (BT) then
12093 Prepend_Elmt (Full_View (T), Exchanged_Views);
12094 Exchange_Declarations (T);
12095 return;
12096 end if;
12098 Priv_Elmt := First_Elmt (Private_Dependents (BT));
12100 if Present (Full_View (BT)) then
12101 Prepend_Elmt (Full_View (BT), Exchanged_Views);
12102 Exchange_Declarations (BT);
12103 end if;
12105 while Present (Priv_Elmt) loop
12106 Priv_Sub := (Node (Priv_Elmt));
12108 -- We avoid flipping the subtype if the Etype of its full view is
12109 -- private because this would result in a malformed subtype. This
12110 -- occurs when the Etype of the subtype full view is the full view of
12111 -- the base type (and since the base types were just switched, the
12112 -- subtype is pointing to the wrong view). This is currently the case
12113 -- for tagged record types, access types (maybe more?) and needs to
12114 -- be resolved. ???
12116 if Present (Full_View (Priv_Sub))
12117 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
12118 then
12119 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
12120 Exchange_Declarations (Priv_Sub);
12121 end if;
12123 Next_Elmt (Priv_Elmt);
12124 end loop;
12125 end Switch_View;
12127 -----------------------------
12128 -- Valid_Default_Attribute --
12129 -----------------------------
12131 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
12132 Attr_Id : constant Attribute_Id :=
12133 Get_Attribute_Id (Attribute_Name (Def));
12134 T : constant Entity_Id := Entity (Prefix (Def));
12135 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
12136 F : Entity_Id;
12137 Num_F : Int;
12138 OK : Boolean;
12140 begin
12141 if No (T)
12142 or else T = Any_Id
12143 then
12144 return;
12145 end if;
12147 Num_F := 0;
12148 F := First_Formal (Nam);
12149 while Present (F) loop
12150 Num_F := Num_F + 1;
12151 Next_Formal (F);
12152 end loop;
12154 case Attr_Id is
12155 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
12156 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
12157 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
12158 Attribute_Unbiased_Rounding =>
12159 OK := Is_Fun
12160 and then Num_F = 1
12161 and then Is_Floating_Point_Type (T);
12163 when Attribute_Image | Attribute_Pred | Attribute_Succ |
12164 Attribute_Value | Attribute_Wide_Image |
12165 Attribute_Wide_Value =>
12166 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
12168 when Attribute_Max | Attribute_Min =>
12169 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
12171 when Attribute_Input =>
12172 OK := (Is_Fun and then Num_F = 1);
12174 when Attribute_Output | Attribute_Read | Attribute_Write =>
12175 OK := (not Is_Fun and then Num_F = 2);
12177 when others =>
12178 OK := False;
12179 end case;
12181 if not OK then
12182 Error_Msg_N ("attribute reference has wrong profile for subprogram",
12183 Def);
12184 end if;
12185 end Valid_Default_Attribute;
12187 end Sem_Ch12;