* tree-ssa-address.c (create_mem_ref): Remove ", bsi" from
[official-gcc.git] / gcc / ada / sem_ch12.adb
blob4a2e283b5cfed3db43737fea0eeb47c52666fde7
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-2006, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Expander; use Expander;
32 with Fname; use Fname;
33 with Fname.UF; use Fname.UF;
34 with Freeze; use Freeze;
35 with Hostparm;
36 with Lib; use Lib;
37 with Lib.Load; use Lib.Load;
38 with Lib.Xref; use Lib.Xref;
39 with Nlists; use Nlists;
40 with Namet; use Namet;
41 with Nmake; use Nmake;
42 with Opt; use Opt;
43 with Rident; use Rident;
44 with Restrict; use Restrict;
45 with Rtsfind; use Rtsfind;
46 with Sem; use Sem;
47 with Sem_Cat; use Sem_Cat;
48 with Sem_Ch3; use Sem_Ch3;
49 with Sem_Ch6; use Sem_Ch6;
50 with Sem_Ch7; use Sem_Ch7;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Ch10; use Sem_Ch10;
53 with Sem_Ch13; use Sem_Ch13;
54 with Sem_Disp; use Sem_Disp;
55 with Sem_Elab; use Sem_Elab;
56 with Sem_Elim; use Sem_Elim;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Res; use Sem_Res;
59 with Sem_Type; use Sem_Type;
60 with Sem_Util; use Sem_Util;
61 with Sem_Warn; use Sem_Warn;
62 with Stand; use Stand;
63 with Sinfo; use Sinfo;
64 with Sinfo.CN; use Sinfo.CN;
65 with Sinput; use Sinput;
66 with Sinput.L; use Sinput.L;
67 with Snames; use Snames;
68 with Stringt; use Stringt;
69 with Uname; use Uname;
70 with Table;
71 with Tbuild; use Tbuild;
72 with Uintp; use Uintp;
73 with Urealp; use Urealp;
75 with GNAT.HTable;
77 package body Sem_Ch12 is
79 ----------------------------------------------------------
80 -- Implementation of Generic Analysis and Instantiation --
81 ----------------------------------------------------------
83 -- GNAT implements generics by macro expansion. No attempt is made to share
84 -- generic instantiations (for now). Analysis of a generic definition does
85 -- not perform any expansion action, but the expander must be called on the
86 -- tree for each instantiation, because the expansion may of course depend
87 -- on the generic actuals. All of this is best achieved as follows:
89 -- a) Semantic analysis of a generic unit is performed on a copy of the
90 -- tree for the generic unit. All tree modifications that follow analysis
91 -- do not affect the original tree. Links are kept between the original
92 -- tree and the copy, in order to recognize non-local references within
93 -- the generic, and propagate them to each instance (recall that name
94 -- resolution is done on the generic declaration: generics are not really
95 -- macros!). This is summarized in the following diagram:
97 -- .-----------. .----------.
98 -- | semantic |<--------------| generic |
99 -- | copy | | unit |
100 -- | |==============>| |
101 -- |___________| global |__________|
102 -- references | | |
103 -- | | |
104 -- .-----|--|.
105 -- | .-----|---.
106 -- | | .----------.
107 -- | | | generic |
108 -- |__| | |
109 -- |__| instance |
110 -- |__________|
112 -- b) Each instantiation copies the original tree, and inserts into it a
113 -- series of declarations that describe the mapping between generic formals
114 -- and actuals. For example, a generic In OUT parameter is an object
115 -- renaming of the corresponing actual, etc. Generic IN parameters are
116 -- constant declarations.
118 -- c) In order to give the right visibility for these renamings, we use
119 -- a different scheme for package and subprogram instantiations. For
120 -- packages, the list of renamings is inserted into the package
121 -- specification, before the visible declarations of the package. The
122 -- renamings are analyzed before any of the text of the instance, and are
123 -- thus visible at the right place. Furthermore, outside of the instance,
124 -- the generic parameters are visible and denote their corresponding
125 -- actuals.
127 -- For subprograms, we create a container package to hold the renamings
128 -- and the subprogram instance itself. Analysis of the package makes the
129 -- renaming declarations visible to the subprogram. After analyzing the
130 -- package, the defining entity for the subprogram is touched-up so that
131 -- it appears declared in the current scope, and not inside the container
132 -- package.
134 -- If the instantiation is a compilation unit, the container package is
135 -- given the same name as the subprogram instance. This ensures that
136 -- the elaboration procedure called by the binder, using the compilation
137 -- unit name, calls in fact the elaboration procedure for the package.
139 -- Not surprisingly, private types complicate this approach. By saving in
140 -- the original generic object the non-local references, we guarantee that
141 -- the proper entities are referenced at the point of instantiation.
142 -- However, for private types, this by itself does not insure that the
143 -- proper VIEW of the entity is used (the full type may be visible at the
144 -- point of generic definition, but not at instantiation, or vice-versa).
145 -- In order to reference the proper view, we special-case any reference
146 -- to private types in the generic object, by saving both views, one in
147 -- the generic and one in the semantic copy. At time of instantiation, we
148 -- check whether the two views are consistent, and exchange declarations if
149 -- necessary, in order to restore the correct visibility. Similarly, if
150 -- the instance view is private when the generic view was not, we perform
151 -- the exchange. After completing the instantiation, we restore the
152 -- current visibility. The flag Has_Private_View marks identifiers in the
153 -- the generic unit that require checking.
155 -- Visibility within nested generic units requires special handling.
156 -- Consider the following scheme:
158 -- type Global is ... -- outside of generic unit.
159 -- generic ...
160 -- package Outer is
161 -- ...
162 -- type Semi_Global is ... -- global to inner.
164 -- generic ... -- 1
165 -- procedure inner (X1 : Global; X2 : Semi_Global);
167 -- procedure in2 is new inner (...); -- 4
168 -- end Outer;
170 -- package New_Outer is new Outer (...); -- 2
171 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
173 -- The semantic analysis of Outer captures all occurrences of Global.
174 -- The semantic analysis of Inner (at 1) captures both occurrences of
175 -- Global and Semi_Global.
177 -- At point 2 (instantiation of Outer), we also produce a generic copy
178 -- of Inner, even though Inner is, at that point, not being instantiated.
179 -- (This is just part of the semantic analysis of New_Outer).
181 -- Critically, references to Global within Inner must be preserved, while
182 -- references to Semi_Global should not preserved, because they must now
183 -- resolve to an entity within New_Outer. To distinguish between these, we
184 -- use a global variable, Current_Instantiated_Parent, which is set when
185 -- performing a generic copy during instantiation (at 2). This variable is
186 -- used when performing a generic copy that is not an instantiation, but
187 -- that is nested within one, as the occurrence of 1 within 2. The analysis
188 -- of a nested generic only preserves references that are global to the
189 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
190 -- determine whether a reference is external to the given parent.
192 -- The instantiation at point 3 requires no special treatment. The method
193 -- works as well for further nestings of generic units, but of course the
194 -- variable Current_Instantiated_Parent must be stacked because nested
195 -- instantiations can occur, e.g. the occurrence of 4 within 2.
197 -- The instantiation of package and subprogram bodies is handled in a
198 -- similar manner, except that it is delayed until after semantic
199 -- analysis is complete. In this fashion complex cross-dependencies
200 -- between several package declarations and bodies containing generics
201 -- can be compiled which otherwise would diagnose spurious circularities.
203 -- For example, it is possible to compile two packages A and B that
204 -- have the following structure:
206 -- package A is package B is
207 -- generic ... generic ...
208 -- package G_A is package G_B is
210 -- with B; with A;
211 -- package body A is package body B is
212 -- package N_B is new G_B (..) package N_A is new G_A (..)
214 -- The table Pending_Instantiations in package Inline is used to keep
215 -- track of body instantiations that are delayed in this manner. Inline
216 -- handles the actual calls to do the body instantiations. This activity
217 -- is part of Inline, since the processing occurs at the same point, and
218 -- for essentially the same reason, as the handling of inlined routines.
220 ----------------------------------------------
221 -- Detection of Instantiation Circularities --
222 ----------------------------------------------
224 -- If we have a chain of instantiations that is circular, this is static
225 -- error which must be detected at compile time. The detection of these
226 -- circularities is carried out at the point that we insert a generic
227 -- instance spec or body. If there is a circularity, then the analysis of
228 -- the offending spec or body will eventually result in trying to load the
229 -- same unit again, and we detect this problem as we analyze the package
230 -- instantiation for the second time.
232 -- At least in some cases after we have detected the circularity, we get
233 -- into trouble if we try to keep going. The following flag is set if a
234 -- circularity is detected, and used to abandon compilation after the
235 -- messages have been posted.
237 Circularity_Detected : Boolean := False;
238 -- This should really be reset on encountering a new main unit, but in
239 -- practice we are not using multiple main units so it is not critical.
241 -------------------------------------------------
242 -- Formal packages and partial parametrization --
243 -------------------------------------------------
245 -- When compiling a generic, a formal package is a local instantiation. If
246 -- declared with a box, its generic formals are visible in the enclosing
247 -- generic. If declared with a partial list of actuals, those actuals that
248 -- are defaulted (covered by an Others clause, or given an explicit box
249 -- initialization) are also visible in the enclosing generic, while those
250 -- that have a corresponding actual are not.
252 -- In our source model of instantiation, the same visibility must be
253 -- present in the spec and body of an instance: the names of the formals
254 -- that are defaulted must be made visible within the instance, and made
255 -- invisible (hidden) after the instantiation is complete, so that they
256 -- are not accessible outside of the instance.
258 -- In a generic, a formal package is treated like a special instantiation.
259 -- Our Ada95 compiler handled formals with and without box in different
260 -- ways. With partial parametrization, we use a single model for both.
261 -- We create a package declaration that consists of the specification of
262 -- the generic package, and a set of declarations that map the actuals
263 -- into local renamings, just as we do for bona fide instantiations. For
264 -- defaulted parameters and formals with a box, we copy directly the
265 -- declarations of the formal into this local package. The result is a
266 -- a package whose visible declarations may include generic formals. This
267 -- package is only used for type checking and visibility analysis, and
268 -- never reaches the back-end, so it can freely violate the placement
269 -- rules for generic formal declarations.
271 -- The list of declarations (renamings and copies of formals) is built
272 -- by Analyze_Associations, just as for regular instantiations.
274 -- At the point of instantiation, conformance checking must be applied only
275 -- to those parameters that were specified in the formal. We perform this
276 -- checking by creating another internal instantiation, this one including
277 -- only the renamings and the formals (the rest of the package spec is not
278 -- relevant to conformance checking). We can then traverse two lists: the
279 -- list of actuals in the instance that corresponds to the formal package,
280 -- and the list of actuals produced for this bogus instantiation. We apply
281 -- the conformance rules to those actuals that are not defaulted (i.e.
282 -- which still appear as generic formals.
284 -- When we compile an instance body we must make the right parameters
285 -- visible again. The predicate Is_Generic_Formal indicates which of the
286 -- formals should have its Is_Hidden flag reset.
288 -----------------------
289 -- Local subprograms --
290 -----------------------
292 procedure Abandon_Instantiation (N : Node_Id);
293 pragma No_Return (Abandon_Instantiation);
294 -- Posts an error message "instantiation abandoned" at the indicated node
295 -- and then raises the exception Instantiation_Error to do it.
297 procedure Analyze_Formal_Array_Type
298 (T : in out Entity_Id;
299 Def : Node_Id);
300 -- A formal array type is treated like an array type declaration, and
301 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
302 -- in-out, because in the case of an anonymous type the entity is
303 -- actually created in the procedure.
305 -- The following procedures treat other kinds of formal parameters
307 procedure Analyze_Formal_Derived_Interface_Type
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 -- The following subprograms create abbreviated declarations for formal
317 -- scalar types. We introduce an anonymous base of the proper class for
318 -- each of them, and define the formals as constrained first subtypes of
319 -- their bases. The bounds are expressions that are non-static in the
320 -- generic.
322 procedure Analyze_Formal_Decimal_Fixed_Point_Type
323 (T : Entity_Id; Def : Node_Id);
324 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
325 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
326 procedure Analyze_Formal_Interface_Type (T : Entity_Id; Def : Node_Id);
327 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
328 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
329 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
330 (T : Entity_Id; Def : Node_Id);
332 procedure Analyze_Formal_Private_Type
333 (N : Node_Id;
334 T : Entity_Id;
335 Def : Node_Id);
336 -- Creates a new private type, which does not require completion
338 procedure Analyze_Generic_Formal_Part (N : Node_Id);
340 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
341 -- Create a new access type with the given designated type
343 function Analyze_Associations
344 (I_Node : Node_Id;
345 Formals : List_Id;
346 F_Copy : List_Id) return List_Id;
347 -- At instantiation time, build the list of associations between formals
348 -- and actuals. Each association becomes a renaming declaration for the
349 -- formal entity. F_Copy is the analyzed list of formals in the generic
350 -- copy. It is used to apply legality checks to the actuals. I_Node is the
351 -- instantiation node itself.
353 procedure Analyze_Subprogram_Instantiation
354 (N : Node_Id;
355 K : Entity_Kind);
357 procedure Build_Instance_Compilation_Unit_Nodes
358 (N : Node_Id;
359 Act_Body : Node_Id;
360 Act_Decl : Node_Id);
361 -- This procedure is used in the case where the generic instance of a
362 -- subprogram body or package body is a library unit. In this case, the
363 -- original library unit node for the generic instantiation must be
364 -- replaced by the resulting generic body, and a link made to a new
365 -- compilation unit node for the generic declaration. The argument N is
366 -- the original generic instantiation. Act_Body and Act_Decl are the body
367 -- and declaration of the instance (either package body and declaration
368 -- nodes or subprogram body and declaration nodes depending on the case).
369 -- On return, the node N has been rewritten with the actual body.
371 procedure Check_Access_Definition (N : Node_Id);
372 -- Subsidiary routine to null exclusion processing. Perform an assertion
373 -- check on Ada version and the presence of an access definition in N.
375 procedure Check_Formal_Packages (P_Id : Entity_Id);
376 -- Apply the following to all formal packages in generic associations
378 procedure Check_Formal_Package_Instance
379 (Formal_Pack : Entity_Id;
380 Actual_Pack : Entity_Id);
381 -- Verify that the actuals of the actual instance match the actuals of
382 -- the template for a formal package that is not declared with a box.
384 procedure Check_Forward_Instantiation (Decl : Node_Id);
385 -- If the generic is a local entity and the corresponding body has not
386 -- been seen yet, flag enclosing packages to indicate that it will be
387 -- elaborated after the generic body. Subprograms declared in the same
388 -- package cannot be inlined by the front-end because front-end inlining
389 -- requires a strict linear order of elaboration.
391 procedure Check_Hidden_Child_Unit
392 (N : Node_Id;
393 Gen_Unit : Entity_Id;
394 Act_Decl_Id : Entity_Id);
395 -- If the generic unit is an implicit child instance within a parent
396 -- instance, we need to make an explicit test that it is not hidden by
397 -- a child instance of the same name and parent.
399 procedure Check_Generic_Actuals
400 (Instance : Entity_Id;
401 Is_Formal_Box : Boolean);
402 -- Similar to previous one. Check the actuals in the instantiation,
403 -- whose views can change between the point of instantiation and the point
404 -- of instantiation of the body. In addition, mark the generic renamings
405 -- as generic actuals, so that they are not compatible with other actuals.
406 -- Recurse on an actual that is a formal package whose declaration has
407 -- a box.
409 function Contains_Instance_Of
410 (Inner : Entity_Id;
411 Outer : Entity_Id;
412 N : Node_Id) return Boolean;
413 -- Inner is instantiated within the generic Outer. Check whether Inner
414 -- directly or indirectly contains an instance of Outer or of one of its
415 -- parents, in the case of a subunit. Each generic unit holds a list of
416 -- the entities instantiated within (at any depth). This procedure
417 -- determines whether the set of such lists contains a cycle, i.e. an
418 -- illegal circular instantiation.
420 function Denotes_Formal_Package
421 (Pack : Entity_Id;
422 On_Exit : Boolean := False) return Boolean;
423 -- Returns True if E is a formal package of an enclosing generic, or
424 -- the actual for such a formal in an enclosing instantiation. If such
425 -- a package is used as a formal in an nested generic, or as an actual
426 -- in a nested instantiation, the visibility of ITS formals should not
427 -- be modified. When called from within Restore_Private_Views, the flag
428 -- On_Exit is true, to indicate that the search for a possible enclosing
429 -- instance should ignore the current one.
431 function Find_Actual_Type
432 (Typ : Entity_Id;
433 Gen_Scope : Entity_Id) return Entity_Id;
434 -- When validating the actual types of a child instance, check whether
435 -- the formal is a formal type of the parent unit, and retrieve the current
436 -- actual for it. Typ is the entity in the analyzed formal type declaration
437 -- (component or index type of an array type, or designated type of an
438 -- access formal) and Gen_Scope is the scope of the analyzed formal array
439 -- or access type. The desired actual may be a formal of a parent, or may
440 -- be declared in a formal package of a parent. In both cases it is a
441 -- generic actual type because it appears within a visible instance.
442 -- Ambiguities may still arise if two homonyms are declared in two formal
443 -- packages, and the prefix of the formal type may be needed to resolve
444 -- the ambiguity in the instance ???
446 function In_Same_Declarative_Part
447 (F_Node : Node_Id;
448 Inst : Node_Id) return Boolean;
449 -- True if the instantiation Inst and the given freeze_node F_Node appear
450 -- within the same declarative part, ignoring subunits, but with no inter-
451 -- vening suprograms or concurrent units. If true, the freeze node
452 -- of the instance can be placed after the freeze node of the parent,
453 -- which it itself an instance.
455 function In_Main_Context (E : Entity_Id) return Boolean;
456 -- Check whether an instantiation is in the context of the main unit.
457 -- Used to determine whether its body should be elaborated to allow
458 -- front-end inlining.
460 function Is_Generic_Formal (E : Entity_Id) return Boolean;
461 -- Utility to determine whether a given entity is declared by means of
462 -- of a formal parameter declaration. Used to set properly the visiblity
463 -- of generic formals of a generic package declared with a box or with
464 -- partial parametrization.
466 procedure Set_Instance_Env
467 (Gen_Unit : Entity_Id;
468 Act_Unit : Entity_Id);
469 -- Save current instance on saved environment, to be used to determine
470 -- the global status of entities in nested instances. Part of Save_Env.
471 -- called after verifying that the generic unit is legal for the instance.
473 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
474 -- Associate analyzed generic parameter with corresponding
475 -- instance. Used for semantic checks at instantiation time.
477 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
478 -- Traverse the Exchanged_Views list to see if a type was private
479 -- and has already been flipped during this phase of instantiation.
481 procedure Hide_Current_Scope;
482 -- When compiling a generic child unit, the parent context must be
483 -- present, but the instance and all entities that may be generated
484 -- must be inserted in the current scope. We leave the current scope
485 -- on the stack, but make its entities invisible to avoid visibility
486 -- problems. This is reversed at the end of instantiations. This is
487 -- not done for the instantiation of the bodies, which only require the
488 -- instances of the generic parents to be in scope.
490 procedure Install_Body
491 (Act_Body : Node_Id;
492 N : Node_Id;
493 Gen_Body : Node_Id;
494 Gen_Decl : Node_Id);
495 -- If the instantiation happens textually before the body of the generic,
496 -- the instantiation of the body must be analyzed after the generic body,
497 -- and not at the point of instantiation. Such early instantiations can
498 -- happen if the generic and the instance appear in a package declaration
499 -- because the generic body can only appear in the corresponding package
500 -- body. Early instantiations can also appear if generic, instance and
501 -- body are all in the declarative part of a subprogram or entry. Entities
502 -- of packages that are early instantiations are delayed, and their freeze
503 -- node appears after the generic body.
505 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id);
506 -- Insert freeze node at the end of the declarative part that includes the
507 -- instance node N. If N is in the visible part of an enclosing package
508 -- declaration, the freeze node has to be inserted at the end of the
509 -- private declarations, if any.
511 procedure Freeze_Subprogram_Body
512 (Inst_Node : Node_Id;
513 Gen_Body : Node_Id;
514 Pack_Id : Entity_Id);
515 -- The generic body may appear textually after the instance, including
516 -- in the proper body of a stub, or within a different package instance.
517 -- Given that the instance can only be elaborated after the generic, we
518 -- place freeze_nodes for the instance and/or for packages that may enclose
519 -- the instance and the generic, so that the back-end can establish the
520 -- proper order of elaboration.
522 procedure Init_Env;
523 -- Establish environment for subsequent instantiation. Separated from
524 -- Save_Env because data-structures for visibility handling must be
525 -- initialized before call to Check_Generic_Child_Unit.
527 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
528 -- When compiling an instance of a child unit the parent (which is
529 -- itself an instance) is an enclosing scope that must be made
530 -- immediately visible. This procedure is also used to install the non-
531 -- generic parent of a generic child unit when compiling its body, so
532 -- that full views of types in the parent are made visible.
534 procedure Remove_Parent (In_Body : Boolean := False);
535 -- Reverse effect after instantiation of child is complete
537 procedure Inline_Instance_Body
538 (N : Node_Id;
539 Gen_Unit : Entity_Id;
540 Act_Decl : Node_Id);
541 -- If front-end inlining is requested, instantiate the package body,
542 -- and preserve the visibility of its compilation unit, to insure
543 -- that successive instantiations succeed.
545 -- The functions Instantiate_XXX perform various legality checks and build
546 -- the declarations for instantiated generic parameters. In all of these
547 -- Formal is the entity in the generic unit, Actual is the entity of
548 -- expression in the generic associations, and Analyzed_Formal is the
549 -- formal in the generic copy, which contains the semantic information to
550 -- be used to validate the actual.
552 function Instantiate_Object
553 (Formal : Node_Id;
554 Actual : Node_Id;
555 Analyzed_Formal : Node_Id) return List_Id;
557 function Instantiate_Type
558 (Formal : Node_Id;
559 Actual : Node_Id;
560 Analyzed_Formal : Node_Id;
561 Actual_Decls : List_Id) return Node_Id;
563 function Instantiate_Formal_Subprogram
564 (Formal : Node_Id;
565 Actual : Node_Id;
566 Analyzed_Formal : Node_Id) return Node_Id;
568 function Instantiate_Formal_Package
569 (Formal : Node_Id;
570 Actual : Node_Id;
571 Analyzed_Formal : Node_Id) return List_Id;
572 -- If the formal package is declared with a box, special visibility rules
573 -- apply to its formals: they are in the visible part of the package. This
574 -- is true in the declarative region of the formal package, that is to say
575 -- in the enclosing generic or instantiation. For an instantiation, the
576 -- parameters of the formal package are made visible in an explicit step.
577 -- Furthermore, if the actual is a visible use_clause, these formals must
578 -- be made potentially use_visible as well. On exit from the enclosing
579 -- instantiation, the reverse must be done.
581 -- For a formal package declared without a box, there are conformance rules
582 -- that apply to the actuals in the generic declaration and the actuals of
583 -- the actual package in the enclosing instantiation. The simplest way to
584 -- apply these rules is to repeat the instantiation of the formal package
585 -- in the context of the enclosing instance, and compare the generic
586 -- associations of this instantiation with those of the actual package.
587 -- This internal instantiation only needs to contain the renamings of the
588 -- formals: the visible and private declarations themselves need not be
589 -- created.
591 -- In Ada2005, the formal package may be only partially parametrized. In
592 -- that case the visibility step must make visible those actuals whose
593 -- corresponding formals were given with a box. A final complication
594 -- involves inherited operations from formal derived types, which must be
595 -- visible if the type is.
597 function Is_In_Main_Unit (N : Node_Id) return Boolean;
598 -- Test if given node is in the main unit
600 procedure Load_Parent_Of_Generic (N : Node_Id; Spec : Node_Id);
601 -- If the generic appears in a separate non-generic library unit,
602 -- load the corresponding body to retrieve the body of the generic.
603 -- N is the node for the generic instantiation, Spec is the generic
604 -- package declaration.
606 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
607 -- Add the context clause of the unit containing a generic unit to
608 -- an instantiation that is a compilation unit.
610 function Get_Associated_Node (N : Node_Id) return Node_Id;
611 -- In order to propagate semantic information back from the analyzed
612 -- copy to the original generic, we maintain links between selected nodes
613 -- in the generic and their corresponding copies. At the end of generic
614 -- analysis, the routine Save_Global_References traverses the generic
615 -- tree, examines the semantic information, and preserves the links to
616 -- those nodes that contain global information. At instantiation, the
617 -- information from the associated node is placed on the new copy, so
618 -- that name resolution is not repeated.
620 -- Three kinds of source nodes have associated nodes:
622 -- a) those that can reference (denote) entities, that is identifiers,
623 -- character literals, expanded_names, operator symbols, operators,
624 -- and attribute reference nodes. These nodes have an Entity field
625 -- and are the set of nodes that are in N_Has_Entity.
627 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
629 -- c) selected components (N_Selected_Component)
631 -- For the first class, the associated node preserves the entity if it is
632 -- global. If the generic contains nested instantiations, the associated
633 -- node itself has been recopied, and a chain of them must be followed.
635 -- For aggregates, the associated node allows retrieval of the type, which
636 -- may otherwise not appear in the generic. The view of this type may be
637 -- different between generic and instantiation, and the full view can be
638 -- installed before the instantiation is analyzed. For aggregates of
639 -- type extensions, the same view exchange may have to be performed for
640 -- some of the ancestor types, if their view is private at the point of
641 -- instantiation.
643 -- Nodes that are selected components in the parse tree may be rewritten
644 -- as expanded names after resolution, and must be treated as potential
645 -- entity holders. which is why they also have an Associated_Node.
647 -- Nodes that do not come from source, such as freeze nodes, do not appear
648 -- in the generic tree, and need not have an associated node.
650 -- The associated node is stored in the Associated_Node field. Note that
651 -- this field overlaps Entity, which is fine, because the whole point is
652 -- that we don't need or want the normal Entity field in this situation.
654 procedure Move_Freeze_Nodes
655 (Out_Of : Entity_Id;
656 After : Node_Id;
657 L : List_Id);
658 -- Freeze nodes can be generated in the analysis of a generic unit, but
659 -- will not be seen by the back-end. It is necessary to move those nodes
660 -- to the enclosing scope if they freeze an outer entity. We place them
661 -- at the end of the enclosing generic package, which is semantically
662 -- neutral.
664 procedure Pre_Analyze_Actuals (N : Node_Id);
665 -- Analyze actuals to perform name resolution. Full resolution is done
666 -- later, when the expected types are known, but names have to be captured
667 -- before installing parents of generics, that are not visible for the
668 -- actuals themselves.
670 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
671 -- Verify that an attribute that appears as the default for a formal
672 -- subprogram is a function or procedure with the correct profile.
674 -------------------------------------------
675 -- Data Structures for Generic Renamings --
676 -------------------------------------------
678 -- The map Generic_Renamings associates generic entities with their
679 -- corresponding actuals. Currently used to validate type instances.
680 -- It will eventually be used for all generic parameters to eliminate
681 -- the need for overload resolution in the instance.
683 type Assoc_Ptr is new Int;
685 Assoc_Null : constant Assoc_Ptr := -1;
687 type Assoc is record
688 Gen_Id : Entity_Id;
689 Act_Id : Entity_Id;
690 Next_In_HTable : Assoc_Ptr;
691 end record;
693 package Generic_Renamings is new Table.Table
694 (Table_Component_Type => Assoc,
695 Table_Index_Type => Assoc_Ptr,
696 Table_Low_Bound => 0,
697 Table_Initial => 10,
698 Table_Increment => 100,
699 Table_Name => "Generic_Renamings");
701 -- Variable to hold enclosing instantiation. When the environment is
702 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
704 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
706 -- Hash table for associations
708 HTable_Size : constant := 37;
709 type HTable_Range is range 0 .. HTable_Size - 1;
711 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
712 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
713 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
714 function Hash (F : Entity_Id) return HTable_Range;
716 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
717 Header_Num => HTable_Range,
718 Element => Assoc,
719 Elmt_Ptr => Assoc_Ptr,
720 Null_Ptr => Assoc_Null,
721 Set_Next => Set_Next_Assoc,
722 Next => Next_Assoc,
723 Key => Entity_Id,
724 Get_Key => Get_Gen_Id,
725 Hash => Hash,
726 Equal => "=");
728 Exchanged_Views : Elist_Id;
729 -- This list holds the private views that have been exchanged during
730 -- instantiation to restore the visibility of the generic declaration.
731 -- (see comments above). After instantiation, the current visibility is
732 -- reestablished by means of a traversal of this list.
734 Hidden_Entities : Elist_Id;
735 -- This list holds the entities of the current scope that are removed
736 -- from immediate visibility when instantiating a child unit. Their
737 -- visibility is restored in Remove_Parent.
739 -- Because instantiations can be recursive, the following must be saved
740 -- on entry and restored on exit from an instantiation (spec or body).
741 -- This is done by the two procedures Save_Env and Restore_Env. For
742 -- package and subprogram instantiations (but not for the body instances)
743 -- the action of Save_Env is done in two steps: Init_Env is called before
744 -- Check_Generic_Child_Unit, because setting the parent instances requires
745 -- that the visibility data structures be properly initialized. Once the
746 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
748 Parent_Unit_Visible : Boolean := False;
749 -- Parent_Unit_Visible is used when the generic is a child unit, and
750 -- indicates whether the ultimate parent of the generic is visible in the
751 -- instantiation environment. It is used to reset the visibility of the
752 -- parent at the end of the instantiation (see Remove_Parent).
754 Instance_Parent_Unit : Entity_Id := Empty;
755 -- This records the ultimate parent unit of an instance of a generic
756 -- child unit and is used in conjunction with Parent_Unit_Visible to
757 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
759 type Instance_Env is record
760 Ada_Version : Ada_Version_Type;
761 Ada_Version_Explicit : Ada_Version_Type;
762 Instantiated_Parent : Assoc;
763 Exchanged_Views : Elist_Id;
764 Hidden_Entities : Elist_Id;
765 Current_Sem_Unit : Unit_Number_Type;
766 Parent_Unit_Visible : Boolean := False;
767 Instance_Parent_Unit : Entity_Id := Empty;
768 end record;
770 package Instance_Envs is new Table.Table (
771 Table_Component_Type => Instance_Env,
772 Table_Index_Type => Int,
773 Table_Low_Bound => 0,
774 Table_Initial => 32,
775 Table_Increment => 100,
776 Table_Name => "Instance_Envs");
778 procedure Restore_Private_Views
779 (Pack_Id : Entity_Id;
780 Is_Package : Boolean := True);
781 -- Restore the private views of external types, and unmark the generic
782 -- renamings of actuals, so that they become comptible subtypes again.
783 -- For subprograms, Pack_Id is the package constructed to hold the
784 -- renamings.
786 procedure Switch_View (T : Entity_Id);
787 -- Switch the partial and full views of a type and its private
788 -- dependents (i.e. its subtypes and derived types).
790 ------------------------------------
791 -- Structures for Error Reporting --
792 ------------------------------------
794 Instantiation_Node : Node_Id;
795 -- Used by subprograms that validate instantiation of formal parameters
796 -- where there might be no actual on which to place the error message.
797 -- Also used to locate the instantiation node for generic subunits.
799 Instantiation_Error : exception;
800 -- When there is a semantic error in the generic parameter matching,
801 -- there is no point in continuing the instantiation, because the
802 -- number of cascaded errors is unpredictable. This exception aborts
803 -- the instantiation process altogether.
805 S_Adjustment : Sloc_Adjustment;
806 -- Offset created for each node in an instantiation, in order to keep
807 -- track of the source position of the instantiation in each of its nodes.
808 -- A subsequent semantic error or warning on a construct of the instance
809 -- points to both places: the original generic node, and the point of
810 -- instantiation. See Sinput and Sinput.L for additional details.
812 ------------------------------------------------------------
813 -- Data structure for keeping track when inside a Generic --
814 ------------------------------------------------------------
816 -- The following table is used to save values of the Inside_A_Generic
817 -- flag (see spec of Sem) when they are saved by Start_Generic.
819 package Generic_Flags is new Table.Table (
820 Table_Component_Type => Boolean,
821 Table_Index_Type => Int,
822 Table_Low_Bound => 0,
823 Table_Initial => 32,
824 Table_Increment => 200,
825 Table_Name => "Generic_Flags");
827 ---------------------------
828 -- Abandon_Instantiation --
829 ---------------------------
831 procedure Abandon_Instantiation (N : Node_Id) is
832 begin
833 Error_Msg_N ("\instantiation abandoned!", N);
834 raise Instantiation_Error;
835 end Abandon_Instantiation;
837 --------------------------
838 -- Analyze_Associations --
839 --------------------------
841 function Analyze_Associations
842 (I_Node : Node_Id;
843 Formals : List_Id;
844 F_Copy : List_Id) return List_Id
846 Actual_Types : constant Elist_Id := New_Elmt_List;
847 Assoc : constant List_Id := New_List;
848 Default_Actuals : constant Elist_Id := New_Elmt_List;
849 Gen_Unit : constant Entity_Id := Defining_Entity (Parent (F_Copy));
850 Actuals : List_Id;
851 Actual : Node_Id;
852 Formal : Node_Id;
853 Next_Formal : Node_Id;
854 Temp_Formal : Node_Id;
855 Analyzed_Formal : Node_Id;
856 Match : Node_Id;
857 Named : Node_Id;
858 First_Named : Node_Id := Empty;
860 Default_Formals : constant List_Id := New_List;
861 -- If an Other_Choice is present, some of the formals may be defaulted.
862 -- To simplify the treatement of visibility in an instance, we introduce
863 -- individual defaults for each such formal. These defaults are
864 -- appended to the list of associations and replace the Others_Choice.
866 Found_Assoc : Node_Id;
867 -- Association for the current formal being match. Empty if there are
868 -- no remaining actuals, or if there is no named association with the
869 -- name of the formal.
871 Is_Named_Assoc : Boolean;
872 Num_Matched : Int := 0;
873 Num_Actuals : Int := 0;
875 Others_Present : Boolean := False;
876 -- In Ada 2005, indicates partial parametrization of of a formal
877 -- package. As usual an others association must be last in the list.
879 function Matching_Actual
880 (F : Entity_Id;
881 A_F : Entity_Id) return Node_Id;
882 -- Find actual that corresponds to a given a formal parameter. If the
883 -- actuals are positional, return the next one, if any. If the actuals
884 -- are named, scan the parameter associations to find the right one.
885 -- A_F is the corresponding entity in the analyzed generic,which is
886 -- placed on the selector name for ASIS use.
888 -- In Ada 2005, a named association may be given with a box, in which
889 -- case Matching_Actual sets Found_Assoc to the generic association,
890 -- but return Empty for the actual itself. In this case the code below
891 -- creates a corresponding declaration for the formal.
893 function Partial_Parametrization return Boolean;
894 -- Ada 2005: if no match is found for a given formal, check if the
895 -- association for it includes a box, or whether the associations
896 -- include an Others clause.
898 procedure Process_Default (F : Entity_Id);
899 -- Add a copy of the declaration of generic formal F to the list of
900 -- associations, and add an explicit box association for F if there
901 -- is none yet, and the default comes from an Others_Choice.
903 procedure Set_Analyzed_Formal;
904 -- Find the node in the generic copy that corresponds to a given formal.
905 -- The semantic information on this node is used to perform legality
906 -- checks on the actuals. Because semantic analysis can introduce some
907 -- anonymous entities or modify the declaration node itself, the
908 -- correspondence between the two lists is not one-one. In addition to
909 -- anonymous types, the presence a formal equality will introduce an
910 -- implicit declaration for the corresponding inequality.
912 ---------------------
913 -- Matching_Actual --
914 ---------------------
916 function Matching_Actual
917 (F : Entity_Id;
918 A_F : Entity_Id) return Node_Id
920 Prev : Node_Id;
921 Act : Node_Id;
923 begin
924 Is_Named_Assoc := False;
926 -- End of list of purely positional parameters
928 if No (Actual) then
929 Found_Assoc := Empty;
930 Act := Empty;
932 -- Case of positional parameter corresponding to current formal
934 elsif No (Selector_Name (Actual)) then
935 Found_Assoc := Actual;
936 Act := Explicit_Generic_Actual_Parameter (Actual);
937 Num_Matched := Num_Matched + 1;
938 Next (Actual);
940 -- Otherwise scan list of named actuals to find the one with the
941 -- desired name. All remaining actuals have explicit names.
943 else
944 Is_Named_Assoc := True;
945 Found_Assoc := Empty;
946 Act := Empty;
947 Prev := Empty;
949 while Present (Actual) loop
950 if Chars (Selector_Name (Actual)) = Chars (F) then
951 Set_Entity (Selector_Name (Actual), A_F);
952 Set_Etype (Selector_Name (Actual), Etype (A_F));
953 Generate_Reference (A_F, Selector_Name (Actual));
954 Found_Assoc := Actual;
955 Act := Explicit_Generic_Actual_Parameter (Actual);
956 Num_Matched := Num_Matched + 1;
957 exit;
958 end if;
960 Prev := Actual;
961 Next (Actual);
962 end loop;
964 -- Reset for subsequent searches. In most cases the named
965 -- associations are in order. If they are not, we reorder them
966 -- to avoid scanning twice the same actual. This is not just a
967 -- question of efficiency: there may be multiple defaults with
968 -- boxes that have the same name. In a nested instantiation we
969 -- insert actuals for those defaults, and cannot rely on their
970 -- names to disambiguate them.
972 if Actual = First_Named then
973 Next (First_Named);
975 elsif Present (Actual) then
976 Insert_Before (First_Named, Remove_Next (Prev));
977 end if;
979 Actual := First_Named;
980 end if;
982 return Act;
983 end Matching_Actual;
985 -----------------------------
986 -- Partial_Parametrization --
987 -----------------------------
989 function Partial_Parametrization return Boolean is
990 begin
991 return Others_Present
992 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
993 end Partial_Parametrization;
995 ---------------------
996 -- Process_Default --
997 ---------------------
999 procedure Process_Default (F : Entity_Id) is
1000 Loc : constant Source_Ptr := Sloc (I_Node);
1001 Default : Node_Id;
1003 begin
1004 Append (Copy_Generic_Node (F, Empty, True), Assoc);
1006 if No (Found_Assoc) then
1007 Default :=
1008 Make_Generic_Association (Loc,
1009 Selector_Name =>
1010 New_Occurrence_Of (Defining_Identifier (F), Loc),
1011 Explicit_Generic_Actual_Parameter => Empty);
1012 Set_Box_Present (Default);
1013 Append (Default, Default_Formals);
1014 end if;
1015 end Process_Default;
1017 -------------------------
1018 -- Set_Analyzed_Formal --
1019 -------------------------
1021 procedure Set_Analyzed_Formal is
1022 Kind : Node_Kind;
1023 begin
1024 while Present (Analyzed_Formal) loop
1025 Kind := Nkind (Analyzed_Formal);
1027 case Nkind (Formal) is
1029 when N_Formal_Subprogram_Declaration =>
1030 exit when Kind in N_Formal_Subprogram_Declaration
1031 and then
1032 Chars
1033 (Defining_Unit_Name (Specification (Formal))) =
1034 Chars
1035 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1037 when N_Formal_Package_Declaration =>
1038 exit when
1039 Kind = N_Formal_Package_Declaration
1040 or else
1041 Kind = N_Generic_Package_Declaration
1042 or else
1043 Kind = N_Package_Declaration;
1045 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1047 when others =>
1049 -- Skip freeze nodes, and nodes inserted to replace
1050 -- unrecognized pragmas.
1052 exit when
1053 Kind not in N_Formal_Subprogram_Declaration
1054 and then Kind /= N_Subprogram_Declaration
1055 and then Kind /= N_Freeze_Entity
1056 and then Kind /= N_Null_Statement
1057 and then Kind /= N_Itype_Reference
1058 and then Chars (Defining_Identifier (Formal)) =
1059 Chars (Defining_Identifier (Analyzed_Formal));
1060 end case;
1062 Next (Analyzed_Formal);
1063 end loop;
1064 end Set_Analyzed_Formal;
1066 -- Start of processing for Analyze_Associations
1068 begin
1069 Actuals := Generic_Associations (I_Node);
1071 if Present (Actuals) then
1073 -- check for an Others choice, indicating a partial parametrization
1074 -- for a formal package.
1076 Actual := First (Actuals);
1077 while Present (Actual) loop
1078 if Nkind (Actual) = N_Others_Choice then
1079 Others_Present := True;
1080 if Present (Next (Actual)) then
1081 Error_Msg_N ("others must be last association", Actual);
1082 end if;
1084 Remove (Actual);
1085 exit;
1086 end if;
1088 Next (Actual);
1089 end loop;
1091 -- If named associations are present, save first named association
1092 -- (it may of course be Empty) to facilitate subsequent name search.
1094 First_Named := First (Actuals);
1095 while Present (First_Named)
1096 and then No (Selector_Name (First_Named))
1097 loop
1098 Num_Actuals := Num_Actuals + 1;
1099 Next (First_Named);
1100 end loop;
1101 end if;
1103 Named := First_Named;
1104 while Present (Named) loop
1105 if No (Selector_Name (Named)) then
1106 Error_Msg_N ("invalid positional actual after named one", Named);
1107 Abandon_Instantiation (Named);
1108 end if;
1110 -- A named association may lack an actual parameter, if it was
1111 -- introduced for a default subprogram that turns out to be local
1112 -- to the outer instantiation.
1114 if Present (Explicit_Generic_Actual_Parameter (Named)) then
1115 Num_Actuals := Num_Actuals + 1;
1116 end if;
1118 Next (Named);
1119 end loop;
1121 if Present (Formals) then
1122 Formal := First_Non_Pragma (Formals);
1123 Analyzed_Formal := First_Non_Pragma (F_Copy);
1125 if Present (Actuals) then
1126 Actual := First (Actuals);
1128 -- All formals should have default values
1130 else
1131 Actual := Empty;
1132 end if;
1134 while Present (Formal) loop
1135 Set_Analyzed_Formal;
1136 Next_Formal := Next_Non_Pragma (Formal);
1138 case Nkind (Formal) is
1139 when N_Formal_Object_Declaration =>
1140 Match :=
1141 Matching_Actual (
1142 Defining_Identifier (Formal),
1143 Defining_Identifier (Analyzed_Formal));
1145 if No (Match) and then Partial_Parametrization then
1146 Process_Default (Formal);
1147 else
1148 Append_List
1149 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1150 Assoc);
1151 end if;
1153 when N_Formal_Type_Declaration =>
1154 Match :=
1155 Matching_Actual (
1156 Defining_Identifier (Formal),
1157 Defining_Identifier (Analyzed_Formal));
1159 if No (Match) then
1160 if Partial_Parametrization then
1161 Process_Default (Formal);
1163 else
1164 Error_Msg_Sloc := Sloc (Gen_Unit);
1165 Error_Msg_NE
1166 ("missing actual&",
1167 Instantiation_Node,
1168 Defining_Identifier (Formal));
1169 Error_Msg_NE ("\in instantiation of & declared#",
1170 Instantiation_Node, Gen_Unit);
1171 Abandon_Instantiation (Instantiation_Node);
1172 end if;
1174 else
1175 Analyze (Match);
1176 Append_To (Assoc,
1177 Instantiate_Type
1178 (Formal, Match, Analyzed_Formal, Assoc));
1180 -- An instantiation is a freeze point for the actuals,
1181 -- unless this is a rewritten formal package.
1183 if Nkind (I_Node) /= N_Formal_Package_Declaration then
1184 Append_Elmt (Entity (Match), Actual_Types);
1185 end if;
1186 end if;
1188 -- A remote access-to-class-wide type must not be an
1189 -- actual parameter for a generic formal of an access
1190 -- type (E.2.2 (17)).
1192 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1193 and then
1194 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1195 N_Access_To_Object_Definition
1196 then
1197 Validate_Remote_Access_To_Class_Wide_Type (Match);
1198 end if;
1200 when N_Formal_Subprogram_Declaration =>
1201 Match :=
1202 Matching_Actual (
1203 Defining_Unit_Name (Specification (Formal)),
1204 Defining_Unit_Name (Specification (Analyzed_Formal)));
1206 -- If the formal subprogram has the same name as
1207 -- another formal subprogram of the generic, then
1208 -- a named association is illegal (12.3(9)). Exclude
1209 -- named associations that are generated for a nested
1210 -- instance.
1212 if Present (Match)
1213 and then Is_Named_Assoc
1214 and then Comes_From_Source (Found_Assoc)
1215 then
1216 Temp_Formal := First (Formals);
1217 while Present (Temp_Formal) loop
1218 if Nkind (Temp_Formal) in
1219 N_Formal_Subprogram_Declaration
1220 and then Temp_Formal /= Formal
1221 and then
1222 Chars (Selector_Name (Found_Assoc)) =
1223 Chars (Defining_Unit_Name
1224 (Specification (Temp_Formal)))
1225 then
1226 Error_Msg_N
1227 ("name not allowed for overloaded formal",
1228 Found_Assoc);
1229 Abandon_Instantiation (Instantiation_Node);
1230 end if;
1232 Next (Temp_Formal);
1233 end loop;
1234 end if;
1236 Append_To (Assoc,
1237 Instantiate_Formal_Subprogram
1238 (Formal, Match, Analyzed_Formal));
1240 if No (Match) then
1241 if Partial_Parametrization then
1242 Process_Default (Formal);
1244 elsif Box_Present (Formal) then
1245 Append_Elmt
1246 (Defining_Unit_Name (Specification (Last (Assoc))),
1247 Default_Actuals);
1248 end if;
1249 end if;
1251 when N_Formal_Package_Declaration =>
1252 Match :=
1253 Matching_Actual (
1254 Defining_Identifier (Formal),
1255 Defining_Identifier (Original_Node (Analyzed_Formal)));
1257 if No (Match) then
1258 if Partial_Parametrization then
1259 Process_Default (Formal);
1261 else
1262 Error_Msg_Sloc := Sloc (Gen_Unit);
1263 Error_Msg_NE
1264 ("missing actual&",
1265 Instantiation_Node, Defining_Identifier (Formal));
1266 Error_Msg_NE ("\in instantiation of & declared#",
1267 Instantiation_Node, Gen_Unit);
1269 Abandon_Instantiation (Instantiation_Node);
1270 end if;
1272 else
1273 Analyze (Match);
1274 Append_List
1275 (Instantiate_Formal_Package
1276 (Formal, Match, Analyzed_Formal),
1277 Assoc);
1278 end if;
1280 -- For use type and use package appearing in the generic
1281 -- part, we have already copied them, so we can just
1282 -- move them where they belong (we mustn't recopy them
1283 -- since this would mess up the Sloc values).
1285 when N_Use_Package_Clause |
1286 N_Use_Type_Clause =>
1287 if Nkind (Original_Node (I_Node)) =
1288 N_Formal_Package_Declaration
1289 then
1290 Append (New_Copy_Tree (Formal), Assoc);
1291 else
1292 Remove (Formal);
1293 Append (Formal, Assoc);
1294 end if;
1296 when others =>
1297 raise Program_Error;
1299 end case;
1301 Formal := Next_Formal;
1302 Next_Non_Pragma (Analyzed_Formal);
1303 end loop;
1305 if Num_Actuals > Num_Matched then
1306 Error_Msg_Sloc := Sloc (Gen_Unit);
1308 if Present (Selector_Name (Actual)) then
1309 Error_Msg_NE
1310 ("unmatched actual&",
1311 Actual, Selector_Name (Actual));
1312 Error_Msg_NE ("\in instantiation of& declared#",
1313 Actual, Gen_Unit);
1314 else
1315 Error_Msg_NE
1316 ("unmatched actual in instantiation of& declared#",
1317 Actual, Gen_Unit);
1318 end if;
1319 end if;
1321 elsif Present (Actuals) then
1322 Error_Msg_N
1323 ("too many actuals in generic instantiation", Instantiation_Node);
1324 end if;
1326 declare
1327 Elmt : Elmt_Id := First_Elmt (Actual_Types);
1329 begin
1330 while Present (Elmt) loop
1331 Freeze_Before (I_Node, Node (Elmt));
1332 Next_Elmt (Elmt);
1333 end loop;
1334 end;
1336 -- If there are default subprograms, normalize the tree by adding
1337 -- explicit associations for them. This is required if the instance
1338 -- appears within a generic.
1340 declare
1341 Elmt : Elmt_Id;
1342 Subp : Entity_Id;
1343 New_D : Node_Id;
1345 begin
1346 Elmt := First_Elmt (Default_Actuals);
1347 while Present (Elmt) loop
1348 if No (Actuals) then
1349 Actuals := New_List;
1350 Set_Generic_Associations (I_Node, Actuals);
1351 end if;
1353 Subp := Node (Elmt);
1354 New_D :=
1355 Make_Generic_Association (Sloc (Subp),
1356 Selector_Name => New_Occurrence_Of (Subp, Sloc (Subp)),
1357 Explicit_Generic_Actual_Parameter =>
1358 New_Occurrence_Of (Subp, Sloc (Subp)));
1359 Mark_Rewrite_Insertion (New_D);
1360 Append_To (Actuals, New_D);
1361 Next_Elmt (Elmt);
1362 end loop;
1363 end;
1365 -- If this is a formal package. normalize the parameter list by
1366 -- adding explicit box asssociations for the formals that are covered
1367 -- by an Others_Choice.
1369 if not Is_Empty_List (Default_Formals) then
1370 Append_List (Default_Formals, Formals);
1371 end if;
1373 return Assoc;
1374 end Analyze_Associations;
1376 -------------------------------
1377 -- Analyze_Formal_Array_Type --
1378 -------------------------------
1380 procedure Analyze_Formal_Array_Type
1381 (T : in out Entity_Id;
1382 Def : Node_Id)
1384 DSS : Node_Id;
1386 begin
1387 -- Treated like a non-generic array declaration, with
1388 -- additional semantic checks.
1390 Enter_Name (T);
1392 if Nkind (Def) = N_Constrained_Array_Definition then
1393 DSS := First (Discrete_Subtype_Definitions (Def));
1394 while Present (DSS) loop
1395 if Nkind (DSS) = N_Subtype_Indication
1396 or else Nkind (DSS) = N_Range
1397 or else Nkind (DSS) = N_Attribute_Reference
1398 then
1399 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1400 end if;
1402 Next (DSS);
1403 end loop;
1404 end if;
1406 Array_Type_Declaration (T, Def);
1407 Set_Is_Generic_Type (Base_Type (T));
1409 if Ekind (Component_Type (T)) = E_Incomplete_Type
1410 and then No (Full_View (Component_Type (T)))
1411 then
1412 Error_Msg_N ("premature usage of incomplete type", Def);
1414 -- Check that range constraint is not allowed on the component type
1415 -- of a generic formal array type (AARM 12.5.3(3))
1417 elsif Is_Internal (Component_Type (T))
1418 and then Present (Subtype_Indication (Component_Definition (Def)))
1419 and then Nkind (Original_Node
1420 (Subtype_Indication (Component_Definition (Def))))
1421 = N_Subtype_Indication
1422 then
1423 Error_Msg_N
1424 ("in a formal, a subtype indication can only be "
1425 & "a subtype mark ('R'M 12.5.3(3))",
1426 Subtype_Indication (Component_Definition (Def)));
1427 end if;
1429 end Analyze_Formal_Array_Type;
1431 ---------------------------------------------
1432 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1433 ---------------------------------------------
1435 -- As for other generic types, we create a valid type representation
1436 -- with legal but arbitrary attributes, whose values are never considered
1437 -- static. For all scalar types we introduce an anonymous base type, with
1438 -- the same attributes. We choose the corresponding integer type to be
1439 -- Standard_Integer.
1441 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1442 (T : Entity_Id;
1443 Def : Node_Id)
1445 Loc : constant Source_Ptr := Sloc (Def);
1446 Base : constant Entity_Id :=
1447 New_Internal_Entity
1448 (E_Decimal_Fixed_Point_Type,
1449 Current_Scope, Sloc (Def), 'G');
1450 Int_Base : constant Entity_Id := Standard_Integer;
1451 Delta_Val : constant Ureal := Ureal_1;
1452 Digs_Val : constant Uint := Uint_6;
1454 begin
1455 Enter_Name (T);
1457 Set_Etype (Base, Base);
1458 Set_Size_Info (Base, Int_Base);
1459 Set_RM_Size (Base, RM_Size (Int_Base));
1460 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1461 Set_Digits_Value (Base, Digs_Val);
1462 Set_Delta_Value (Base, Delta_Val);
1463 Set_Small_Value (Base, Delta_Val);
1464 Set_Scalar_Range (Base,
1465 Make_Range (Loc,
1466 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1467 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1469 Set_Is_Generic_Type (Base);
1470 Set_Parent (Base, Parent (Def));
1472 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1473 Set_Etype (T, Base);
1474 Set_Size_Info (T, Int_Base);
1475 Set_RM_Size (T, RM_Size (Int_Base));
1476 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1477 Set_Digits_Value (T, Digs_Val);
1478 Set_Delta_Value (T, Delta_Val);
1479 Set_Small_Value (T, Delta_Val);
1480 Set_Scalar_Range (T, Scalar_Range (Base));
1481 Set_Is_Constrained (T);
1483 Check_Restriction (No_Fixed_Point, Def);
1484 end Analyze_Formal_Decimal_Fixed_Point_Type;
1486 -------------------------------------------
1487 -- Analyze_Formal_Derived_Interface_Type --
1488 -------------------------------------------
1490 procedure Analyze_Formal_Derived_Interface_Type
1491 (T : Entity_Id;
1492 Def : Node_Id)
1494 Ifaces_List : Elist_Id;
1496 begin
1497 Enter_Name (T);
1498 Set_Ekind (T, E_Record_Type);
1499 Set_Etype (T, T);
1500 Analyze (Subtype_Indication (Def));
1501 Analyze_Interface_Declaration (T, Def);
1502 Make_Class_Wide_Type (T);
1503 Analyze_List (Interface_List (Def));
1505 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
1506 -- already covered by the parents.
1508 Collect_Abstract_Interfaces
1509 (T => T,
1510 Ifaces_List => Ifaces_List,
1511 Exclude_Parent_Interfaces => True);
1513 Set_Abstract_Interfaces (T, Ifaces_List);
1514 end Analyze_Formal_Derived_Interface_Type;
1516 ---------------------------------
1517 -- Analyze_Formal_Derived_Type --
1518 ---------------------------------
1520 procedure Analyze_Formal_Derived_Type
1521 (N : Node_Id;
1522 T : Entity_Id;
1523 Def : Node_Id)
1525 Loc : constant Source_Ptr := Sloc (Def);
1526 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
1527 New_N : Node_Id;
1529 begin
1530 Set_Is_Generic_Type (T);
1532 if Private_Present (Def) then
1533 New_N :=
1534 Make_Private_Extension_Declaration (Loc,
1535 Defining_Identifier => T,
1536 Discriminant_Specifications => Discriminant_Specifications (N),
1537 Unknown_Discriminants_Present => Unk_Disc,
1538 Subtype_Indication => Subtype_Mark (Def),
1539 Interface_List => Interface_List (Def));
1541 Set_Abstract_Present (New_N, Abstract_Present (Def));
1542 Set_Limited_Present (New_N, Limited_Present (Def));
1543 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
1545 else
1546 New_N :=
1547 Make_Full_Type_Declaration (Loc,
1548 Defining_Identifier => T,
1549 Discriminant_Specifications =>
1550 Discriminant_Specifications (Parent (T)),
1551 Type_Definition =>
1552 Make_Derived_Type_Definition (Loc,
1553 Subtype_Indication => Subtype_Mark (Def)));
1555 Set_Abstract_Present
1556 (Type_Definition (New_N), Abstract_Present (Def));
1557 Set_Limited_Present
1558 (Type_Definition (New_N), Limited_Present (Def));
1559 end if;
1561 Rewrite (N, New_N);
1562 Analyze (N);
1564 if Unk_Disc then
1565 if not Is_Composite_Type (T) then
1566 Error_Msg_N
1567 ("unknown discriminants not allowed for elementary types", N);
1568 else
1569 Set_Has_Unknown_Discriminants (T);
1570 Set_Is_Constrained (T, False);
1571 end if;
1572 end if;
1574 -- If the parent type has a known size, so does the formal, which
1575 -- makes legal representation clauses that involve the formal.
1577 Set_Size_Known_At_Compile_Time
1578 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
1580 end Analyze_Formal_Derived_Type;
1582 ----------------------------------
1583 -- Analyze_Formal_Discrete_Type --
1584 ----------------------------------
1586 -- The operations defined for a discrete types are those of an
1587 -- enumeration type. The size is set to an arbitrary value, for use
1588 -- in analyzing the generic unit.
1590 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
1591 Loc : constant Source_Ptr := Sloc (Def);
1592 Lo : Node_Id;
1593 Hi : Node_Id;
1595 Base : constant Entity_Id :=
1596 New_Internal_Entity
1597 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1598 begin
1599 Enter_Name (T);
1600 Set_Ekind (T, E_Enumeration_Subtype);
1601 Set_Etype (T, Base);
1602 Init_Size (T, 8);
1603 Init_Alignment (T);
1604 Set_Is_Generic_Type (T);
1605 Set_Is_Constrained (T);
1607 -- For semantic analysis, the bounds of the type must be set to some
1608 -- non-static value. The simplest is to create attribute nodes for
1609 -- those bounds, that refer to the type itself. These bounds are never
1610 -- analyzed but serve as place-holders.
1612 Lo :=
1613 Make_Attribute_Reference (Loc,
1614 Attribute_Name => Name_First,
1615 Prefix => New_Reference_To (T, Loc));
1616 Set_Etype (Lo, T);
1618 Hi :=
1619 Make_Attribute_Reference (Loc,
1620 Attribute_Name => Name_Last,
1621 Prefix => New_Reference_To (T, Loc));
1622 Set_Etype (Hi, T);
1624 Set_Scalar_Range (T,
1625 Make_Range (Loc,
1626 Low_Bound => Lo,
1627 High_Bound => Hi));
1629 Set_Ekind (Base, E_Enumeration_Type);
1630 Set_Etype (Base, Base);
1631 Init_Size (Base, 8);
1632 Init_Alignment (Base);
1633 Set_Is_Generic_Type (Base);
1634 Set_Scalar_Range (Base, Scalar_Range (T));
1635 Set_Parent (Base, Parent (Def));
1637 end Analyze_Formal_Discrete_Type;
1639 ----------------------------------
1640 -- Analyze_Formal_Floating_Type --
1641 ---------------------------------
1643 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
1644 Base : constant Entity_Id :=
1645 New_Internal_Entity
1646 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1648 begin
1649 -- The various semantic attributes are taken from the predefined type
1650 -- Float, just so that all of them are initialized. Their values are
1651 -- never used because no constant folding or expansion takes place in
1652 -- the generic itself.
1654 Enter_Name (T);
1655 Set_Ekind (T, E_Floating_Point_Subtype);
1656 Set_Etype (T, Base);
1657 Set_Size_Info (T, (Standard_Float));
1658 Set_RM_Size (T, RM_Size (Standard_Float));
1659 Set_Digits_Value (T, Digits_Value (Standard_Float));
1660 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
1661 Set_Is_Constrained (T);
1663 Set_Is_Generic_Type (Base);
1664 Set_Etype (Base, Base);
1665 Set_Size_Info (Base, (Standard_Float));
1666 Set_RM_Size (Base, RM_Size (Standard_Float));
1667 Set_Digits_Value (Base, Digits_Value (Standard_Float));
1668 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
1669 Set_Parent (Base, Parent (Def));
1671 Check_Restriction (No_Floating_Point, Def);
1672 end Analyze_Formal_Floating_Type;
1674 -----------------------------------
1675 -- Analyze_Formal_Interface_Type;--
1676 -----------------------------------
1678 procedure Analyze_Formal_Interface_Type (T : Entity_Id; Def : Node_Id) is
1679 begin
1680 Enter_Name (T);
1681 Set_Ekind (T, E_Record_Type);
1682 Set_Etype (T, T);
1683 Analyze_Interface_Declaration (T, Def);
1684 Make_Class_Wide_Type (T);
1685 Set_Primitive_Operations (T, New_Elmt_List);
1686 end Analyze_Formal_Interface_Type;
1688 ---------------------------------
1689 -- Analyze_Formal_Modular_Type --
1690 ---------------------------------
1692 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
1693 begin
1694 -- Apart from their entity kind, generic modular types are treated
1695 -- like signed integer types, and have the same attributes.
1697 Analyze_Formal_Signed_Integer_Type (T, Def);
1698 Set_Ekind (T, E_Modular_Integer_Subtype);
1699 Set_Ekind (Etype (T), E_Modular_Integer_Type);
1701 end Analyze_Formal_Modular_Type;
1703 ---------------------------------------
1704 -- Analyze_Formal_Object_Declaration --
1705 ---------------------------------------
1707 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
1708 E : constant Node_Id := Default_Expression (N);
1709 Id : constant Node_Id := Defining_Identifier (N);
1710 K : Entity_Kind;
1711 T : Node_Id;
1713 begin
1714 Enter_Name (Id);
1716 -- Determine the mode of the formal object
1718 if Out_Present (N) then
1719 K := E_Generic_In_Out_Parameter;
1721 if not In_Present (N) then
1722 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
1723 end if;
1725 else
1726 K := E_Generic_In_Parameter;
1727 end if;
1729 if Present (Subtype_Mark (N)) then
1730 Find_Type (Subtype_Mark (N));
1731 T := Entity (Subtype_Mark (N));
1733 -- Ada 2005 (AI-423): Formal object with an access definition
1735 else
1736 Check_Access_Definition (N);
1737 T := Access_Definition
1738 (Related_Nod => N,
1739 N => Access_Definition (N));
1740 end if;
1742 if Ekind (T) = E_Incomplete_Type then
1743 declare
1744 Error_Node : Node_Id;
1746 begin
1747 if Present (Subtype_Mark (N)) then
1748 Error_Node := Subtype_Mark (N);
1749 else
1750 Check_Access_Definition (N);
1751 Error_Node := Access_Definition (N);
1752 end if;
1754 Error_Msg_N ("premature usage of incomplete type", Error_Node);
1755 end;
1756 end if;
1758 if K = E_Generic_In_Parameter then
1760 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
1762 if Ada_Version < Ada_05 and then Is_Limited_Type (T) then
1763 Error_Msg_N
1764 ("generic formal of mode IN must not be of limited type", N);
1765 Explain_Limited_Type (T, N);
1766 end if;
1768 if Is_Abstract (T) then
1769 Error_Msg_N
1770 ("generic formal of mode IN must not be of abstract type", N);
1771 end if;
1773 if Present (E) then
1774 Analyze_Per_Use_Expression (E, T);
1775 end if;
1777 Set_Ekind (Id, K);
1778 Set_Etype (Id, T);
1780 -- Case of generic IN OUT parameter
1782 else
1783 -- If the formal has an unconstrained type, construct its
1784 -- actual subtype, as is done for subprogram formals. In this
1785 -- fashion, all its uses can refer to specific bounds.
1787 Set_Ekind (Id, K);
1788 Set_Etype (Id, T);
1790 if (Is_Array_Type (T)
1791 and then not Is_Constrained (T))
1792 or else
1793 (Ekind (T) = E_Record_Type
1794 and then Has_Discriminants (T))
1795 then
1796 declare
1797 Non_Freezing_Ref : constant Node_Id :=
1798 New_Reference_To (Id, Sloc (Id));
1799 Decl : Node_Id;
1801 begin
1802 -- Make sure that the actual subtype doesn't generate
1803 -- bogus freezing.
1805 Set_Must_Not_Freeze (Non_Freezing_Ref);
1806 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
1807 Insert_Before_And_Analyze (N, Decl);
1808 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
1809 end;
1810 else
1811 Set_Actual_Subtype (Id, T);
1812 end if;
1814 if Present (E) then
1815 Error_Msg_N
1816 ("initialization not allowed for `IN OUT` formals", N);
1817 end if;
1818 end if;
1820 end Analyze_Formal_Object_Declaration;
1822 ----------------------------------------------
1823 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
1824 ----------------------------------------------
1826 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
1827 (T : Entity_Id;
1828 Def : Node_Id)
1830 Loc : constant Source_Ptr := Sloc (Def);
1831 Base : constant Entity_Id :=
1832 New_Internal_Entity
1833 (E_Ordinary_Fixed_Point_Type, Current_Scope, Sloc (Def), 'G');
1834 begin
1835 -- The semantic attributes are set for completeness only, their
1836 -- values will never be used, because all properties of the type
1837 -- are non-static.
1839 Enter_Name (T);
1840 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
1841 Set_Etype (T, Base);
1842 Set_Size_Info (T, Standard_Integer);
1843 Set_RM_Size (T, RM_Size (Standard_Integer));
1844 Set_Small_Value (T, Ureal_1);
1845 Set_Delta_Value (T, Ureal_1);
1846 Set_Scalar_Range (T,
1847 Make_Range (Loc,
1848 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1849 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1850 Set_Is_Constrained (T);
1852 Set_Is_Generic_Type (Base);
1853 Set_Etype (Base, Base);
1854 Set_Size_Info (Base, Standard_Integer);
1855 Set_RM_Size (Base, RM_Size (Standard_Integer));
1856 Set_Small_Value (Base, Ureal_1);
1857 Set_Delta_Value (Base, Ureal_1);
1858 Set_Scalar_Range (Base, Scalar_Range (T));
1859 Set_Parent (Base, Parent (Def));
1861 Check_Restriction (No_Fixed_Point, Def);
1862 end Analyze_Formal_Ordinary_Fixed_Point_Type;
1864 ----------------------------
1865 -- Analyze_Formal_Package --
1866 ----------------------------
1868 procedure Analyze_Formal_Package (N : Node_Id) is
1869 Loc : constant Source_Ptr := Sloc (N);
1870 Pack_Id : constant Entity_Id := Defining_Identifier (N);
1871 Formal : Entity_Id;
1872 Gen_Id : constant Node_Id := Name (N);
1873 Gen_Decl : Node_Id;
1874 Gen_Unit : Entity_Id;
1875 New_N : Node_Id;
1876 Parent_Installed : Boolean := False;
1877 Renaming : Node_Id;
1878 Parent_Instance : Entity_Id;
1879 Renaming_In_Par : Entity_Id;
1880 No_Associations : Boolean := False;
1882 function Build_Local_Package return Node_Id;
1883 -- The formal package is rewritten so that its parameters are replaced
1884 -- with corresponding declarations. For parameters with bona fide
1885 -- associations these declarations are created by Analyze_Associations
1886 -- as for aa regular instantiation. For boxed parameters, we preserve
1887 -- the formal declarations and analyze them, in order to introduce
1888 -- entities of the right kind in the environment of the formal.
1890 -------------------------
1891 -- Build_Local_Package --
1892 -------------------------
1894 function Build_Local_Package return Node_Id is
1895 Decls : List_Id;
1896 Pack_Decl : Node_Id;
1898 begin
1899 -- Within the formal, the name of the generic package is a renaming
1900 -- of the formal (as for a regular instantiation).
1902 Pack_Decl :=
1903 Make_Package_Declaration (Loc,
1904 Specification =>
1905 Copy_Generic_Node
1906 (Specification (Original_Node (Gen_Decl)),
1907 Empty, Instantiating => True));
1909 Renaming := Make_Package_Renaming_Declaration (Loc,
1910 Defining_Unit_Name =>
1911 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
1912 Name => New_Occurrence_Of (Formal, Loc));
1914 if Nkind (Gen_Id) = N_Identifier
1915 and then Chars (Gen_Id) = Chars (Pack_Id)
1916 then
1917 Error_Msg_NE
1918 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
1919 end if;
1921 -- If the formal is declared with a box, or with an others choice,
1922 -- create corresponding declarations for all entities in the formal
1923 -- part, so that names with the proper types are available in the
1924 -- specification of the formal package.
1926 if No_Associations then
1927 declare
1928 Formal_Decl : Node_Id;
1930 begin
1931 -- TBA : for a formal package, need to recurse
1933 Decls := New_List;
1934 Formal_Decl :=
1935 First
1936 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
1937 while Present (Formal_Decl) loop
1938 Append_To
1939 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
1940 Next (Formal_Decl);
1941 end loop;
1942 end;
1944 -- If generic associations are present, use Analyze_Associations to
1945 -- create the proper renaming declarations.
1947 else
1948 declare
1949 Act_Tree : constant Node_Id :=
1950 Copy_Generic_Node
1951 (Original_Node (Gen_Decl), Empty,
1952 Instantiating => True);
1954 begin
1955 Generic_Renamings.Set_Last (0);
1956 Generic_Renamings_HTable.Reset;
1957 Instantiation_Node := N;
1959 Decls :=
1960 Analyze_Associations
1961 (Original_Node (N),
1962 Generic_Formal_Declarations (Act_Tree),
1963 Generic_Formal_Declarations (Gen_Decl));
1964 end;
1965 end if;
1967 Append (Renaming, To => Decls);
1969 -- Add generated declarations ahead of local declarations in
1970 -- the package.
1972 if No (Visible_Declarations (Specification (Pack_Decl))) then
1973 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
1974 else
1975 Insert_List_Before
1976 (First (Visible_Declarations (Specification (Pack_Decl))),
1977 Decls);
1978 end if;
1980 return Pack_Decl;
1981 end Build_Local_Package;
1983 -- Start of processing for Analyze_Formal_Package
1985 begin
1986 Text_IO_Kludge (Gen_Id);
1988 Init_Env;
1989 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
1990 Gen_Unit := Entity (Gen_Id);
1992 -- Check for a formal package that is a package renaming
1994 if Present (Renamed_Object (Gen_Unit)) then
1995 Gen_Unit := Renamed_Object (Gen_Unit);
1996 end if;
1998 if Ekind (Gen_Unit) /= E_Generic_Package then
1999 Error_Msg_N ("expect generic package name", Gen_Id);
2000 Restore_Env;
2001 return;
2003 elsif Gen_Unit = Current_Scope then
2004 Error_Msg_N
2005 ("generic package cannot be used as a formal package of itself",
2006 Gen_Id);
2007 Restore_Env;
2008 return;
2010 elsif In_Open_Scopes (Gen_Unit) then
2011 if Is_Compilation_Unit (Gen_Unit)
2012 and then Is_Child_Unit (Current_Scope)
2013 then
2014 -- Special-case the error when the formal is a parent, and
2015 -- continue analysis to minimize cascaded errors.
2017 Error_Msg_N
2018 ("generic parent cannot be used as formal package "
2019 & "of a child unit",
2020 Gen_Id);
2022 else
2023 Error_Msg_N
2024 ("generic package cannot be used as a formal package "
2025 & "within itself",
2026 Gen_Id);
2027 Restore_Env;
2028 return;
2029 end if;
2030 end if;
2032 if Box_Present (N)
2033 or else No (Generic_Associations (N))
2034 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2035 then
2036 No_Associations := True;
2037 end if;
2039 -- If there are no generic associations, the generic parameters
2040 -- appear as local entities and are instantiated like them. We copy
2041 -- the generic package declaration as if it were an instantiation,
2042 -- and analyze it like a regular package, except that we treat the
2043 -- formals as additional visible components.
2045 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2047 if In_Extended_Main_Source_Unit (N) then
2048 Set_Is_Instantiated (Gen_Unit);
2049 Generate_Reference (Gen_Unit, N);
2050 end if;
2052 Formal := New_Copy (Pack_Id);
2053 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2055 -- Make local generic without formals. The formals will be replaced
2056 -- with internal declarations..
2058 New_N := Build_Local_Package;
2059 Rewrite (N, New_N);
2060 Set_Defining_Unit_Name (Specification (New_N), Formal);
2061 Set_Generic_Parent (Specification (N), Gen_Unit);
2062 Set_Instance_Env (Gen_Unit, Formal);
2063 Set_Is_Generic_Instance (Formal);
2065 Enter_Name (Formal);
2066 Set_Ekind (Formal, E_Package);
2067 Set_Etype (Formal, Standard_Void_Type);
2068 Set_Inner_Instances (Formal, New_Elmt_List);
2069 New_Scope (Formal);
2071 if Is_Child_Unit (Gen_Unit)
2072 and then Parent_Installed
2073 then
2074 -- Similarly, we have to make the name of the formal visible in
2075 -- the parent instance, to resolve properly fully qualified names
2076 -- that may appear in the generic unit. The parent instance has
2077 -- been placed on the scope stack ahead of the current scope.
2079 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2081 Renaming_In_Par :=
2082 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2083 Set_Ekind (Renaming_In_Par, E_Package);
2084 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2085 Set_Scope (Renaming_In_Par, Parent_Instance);
2086 Set_Parent (Renaming_In_Par, Parent (Formal));
2087 Set_Renamed_Object (Renaming_In_Par, Formal);
2088 Append_Entity (Renaming_In_Par, Parent_Instance);
2089 end if;
2091 Analyze (Specification (N));
2093 -- The formals for which associations are provided are not visible
2094 -- outside of the formal package. The others are still declared by
2095 -- a formal parameter declaration.
2097 if not No_Associations then
2098 declare
2099 E : Entity_Id;
2101 begin
2102 E := First_Entity (Formal);
2103 while Present (E) loop
2104 exit when Ekind (E) = E_Package
2105 and then Renamed_Entity (E) = Formal;
2107 if not Is_Generic_Formal (E) then
2108 Set_Is_Hidden (E);
2109 end if;
2111 Next_Entity (E);
2112 end loop;
2113 end;
2114 end if;
2116 End_Package_Scope (Formal);
2118 if Parent_Installed then
2119 Remove_Parent;
2120 end if;
2122 Restore_Env;
2124 -- Inside the generic unit, the formal package is a regular
2125 -- package, but no body is needed for it. Note that after
2126 -- instantiation, the defining_unit_name we need is in the
2127 -- new tree and not in the original. (see Package_Instantiation).
2128 -- A generic formal package is an instance, and can be used as
2129 -- an actual for an inner instance.
2131 Set_Has_Completion (Formal, True);
2133 -- Add semantic information to the original defining identifier.
2134 -- for ASIS use.
2136 Set_Ekind (Pack_Id, E_Package);
2137 Set_Etype (Pack_Id, Standard_Void_Type);
2138 Set_Scope (Pack_Id, Scope (Formal));
2139 Set_Has_Completion (Pack_Id, True);
2140 end Analyze_Formal_Package;
2142 ---------------------------------
2143 -- Analyze_Formal_Private_Type --
2144 ---------------------------------
2146 procedure Analyze_Formal_Private_Type
2147 (N : Node_Id;
2148 T : Entity_Id;
2149 Def : Node_Id)
2151 begin
2152 New_Private_Type (N, T, Def);
2154 -- Set the size to an arbitrary but legal value
2156 Set_Size_Info (T, Standard_Integer);
2157 Set_RM_Size (T, RM_Size (Standard_Integer));
2158 end Analyze_Formal_Private_Type;
2160 ----------------------------------------
2161 -- Analyze_Formal_Signed_Integer_Type --
2162 ----------------------------------------
2164 procedure Analyze_Formal_Signed_Integer_Type
2165 (T : Entity_Id;
2166 Def : Node_Id)
2168 Base : constant Entity_Id :=
2169 New_Internal_Entity
2170 (E_Signed_Integer_Type, Current_Scope, Sloc (Def), 'G');
2172 begin
2173 Enter_Name (T);
2175 Set_Ekind (T, E_Signed_Integer_Subtype);
2176 Set_Etype (T, Base);
2177 Set_Size_Info (T, Standard_Integer);
2178 Set_RM_Size (T, RM_Size (Standard_Integer));
2179 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2180 Set_Is_Constrained (T);
2182 Set_Is_Generic_Type (Base);
2183 Set_Size_Info (Base, Standard_Integer);
2184 Set_RM_Size (Base, RM_Size (Standard_Integer));
2185 Set_Etype (Base, Base);
2186 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2187 Set_Parent (Base, Parent (Def));
2188 end Analyze_Formal_Signed_Integer_Type;
2190 -------------------------------
2191 -- Analyze_Formal_Subprogram --
2192 -------------------------------
2194 procedure Analyze_Formal_Subprogram (N : Node_Id) is
2195 Spec : constant Node_Id := Specification (N);
2196 Def : constant Node_Id := Default_Name (N);
2197 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2198 Subp : Entity_Id;
2200 begin
2201 if Nam = Error then
2202 return;
2203 end if;
2205 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2206 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2207 return;
2208 end if;
2210 Analyze_Subprogram_Declaration (N);
2211 Set_Is_Formal_Subprogram (Nam);
2212 Set_Has_Completion (Nam);
2214 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2215 Set_Is_Abstract (Nam);
2216 Set_Is_Dispatching_Operation (Nam);
2218 declare
2219 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2221 begin
2222 if No (Ctrl_Type) then
2223 Error_Msg_N
2224 ("abstract formal subprogram must have a controlling type",
2227 else
2228 Check_Controlling_Formals (Ctrl_Type, Nam);
2229 end if;
2230 end;
2231 end if;
2233 -- Default name is resolved at the point of instantiation
2235 if Box_Present (N) then
2236 null;
2238 -- Else default is bound at the point of generic declaration
2240 elsif Present (Def) then
2241 if Nkind (Def) = N_Operator_Symbol then
2242 Find_Direct_Name (Def);
2244 elsif Nkind (Def) /= N_Attribute_Reference then
2245 Analyze (Def);
2247 else
2248 -- For an attribute reference, analyze the prefix and verify
2249 -- that it has the proper profile for the subprogram.
2251 Analyze (Prefix (Def));
2252 Valid_Default_Attribute (Nam, Def);
2253 return;
2254 end if;
2256 -- Default name may be overloaded, in which case the interpretation
2257 -- with the correct profile must be selected, as for a renaming.
2259 if Etype (Def) = Any_Type then
2260 return;
2262 elsif Nkind (Def) = N_Selected_Component then
2263 Subp := Entity (Selector_Name (Def));
2265 if Ekind (Subp) /= E_Entry then
2266 Error_Msg_N ("expect valid subprogram name as default", Def);
2267 return;
2268 end if;
2270 elsif Nkind (Def) = N_Indexed_Component then
2272 if Nkind (Prefix (Def)) /= N_Selected_Component then
2273 Error_Msg_N ("expect valid subprogram name as default", Def);
2274 return;
2276 else
2277 Subp := Entity (Selector_Name (Prefix (Def)));
2279 if Ekind (Subp) /= E_Entry_Family then
2280 Error_Msg_N ("expect valid subprogram name as default", Def);
2281 return;
2282 end if;
2283 end if;
2285 elsif Nkind (Def) = N_Character_Literal then
2287 -- Needs some type checks: subprogram should be parameterless???
2289 Resolve (Def, (Etype (Nam)));
2291 elsif not Is_Entity_Name (Def)
2292 or else not Is_Overloadable (Entity (Def))
2293 then
2294 Error_Msg_N ("expect valid subprogram name as default", Def);
2295 return;
2297 elsif not Is_Overloaded (Def) then
2298 Subp := Entity (Def);
2300 if Subp = Nam then
2301 Error_Msg_N ("premature usage of formal subprogram", Def);
2303 elsif not Entity_Matches_Spec (Subp, Nam) then
2304 Error_Msg_N ("no visible entity matches specification", Def);
2305 end if;
2307 else
2308 declare
2309 I : Interp_Index;
2310 I1 : Interp_Index := 0;
2311 It : Interp;
2312 It1 : Interp;
2314 begin
2315 Subp := Any_Id;
2316 Get_First_Interp (Def, I, It);
2317 while Present (It.Nam) loop
2319 if Entity_Matches_Spec (It.Nam, Nam) then
2320 if Subp /= Any_Id then
2321 It1 := Disambiguate (Def, I1, I, Etype (Subp));
2323 if It1 = No_Interp then
2324 Error_Msg_N ("ambiguous default subprogram", Def);
2325 else
2326 Subp := It1.Nam;
2327 end if;
2329 exit;
2331 else
2332 I1 := I;
2333 Subp := It.Nam;
2334 end if;
2335 end if;
2337 Get_Next_Interp (I, It);
2338 end loop;
2339 end;
2341 if Subp /= Any_Id then
2342 Set_Entity (Def, Subp);
2344 if Subp = Nam then
2345 Error_Msg_N ("premature usage of formal subprogram", Def);
2347 elsif Ekind (Subp) /= E_Operator then
2348 Check_Mode_Conformant (Subp, Nam);
2349 end if;
2351 else
2352 Error_Msg_N ("no visible subprogram matches specification", N);
2353 end if;
2354 end if;
2355 end if;
2356 end Analyze_Formal_Subprogram;
2358 -------------------------------------
2359 -- Analyze_Formal_Type_Declaration --
2360 -------------------------------------
2362 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
2363 Def : constant Node_Id := Formal_Type_Definition (N);
2364 T : Entity_Id;
2366 begin
2367 T := Defining_Identifier (N);
2369 if Present (Discriminant_Specifications (N))
2370 and then Nkind (Def) /= N_Formal_Private_Type_Definition
2371 then
2372 Error_Msg_N
2373 ("discriminants not allowed for this formal type",
2374 Defining_Identifier (First (Discriminant_Specifications (N))));
2375 end if;
2377 -- Enter the new name, and branch to specific routine
2379 case Nkind (Def) is
2380 when N_Formal_Private_Type_Definition =>
2381 Analyze_Formal_Private_Type (N, T, Def);
2383 when N_Formal_Derived_Type_Definition =>
2384 Analyze_Formal_Derived_Type (N, T, Def);
2386 when N_Formal_Discrete_Type_Definition =>
2387 Analyze_Formal_Discrete_Type (T, Def);
2389 when N_Formal_Signed_Integer_Type_Definition =>
2390 Analyze_Formal_Signed_Integer_Type (T, Def);
2392 when N_Formal_Modular_Type_Definition =>
2393 Analyze_Formal_Modular_Type (T, Def);
2395 when N_Formal_Floating_Point_Definition =>
2396 Analyze_Formal_Floating_Type (T, Def);
2398 when N_Formal_Ordinary_Fixed_Point_Definition =>
2399 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
2401 when N_Formal_Decimal_Fixed_Point_Definition =>
2402 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
2404 when N_Array_Type_Definition =>
2405 Analyze_Formal_Array_Type (T, Def);
2407 when N_Access_To_Object_Definition |
2408 N_Access_Function_Definition |
2409 N_Access_Procedure_Definition =>
2410 Analyze_Generic_Access_Type (T, Def);
2412 -- Ada 2005: a interface declaration is encoded as an abstract
2413 -- record declaration or a abstract type derivation.
2415 when N_Record_Definition =>
2416 Analyze_Formal_Interface_Type (T, Def);
2418 when N_Derived_Type_Definition =>
2419 Analyze_Formal_Derived_Interface_Type (T, Def);
2421 when N_Error =>
2422 null;
2424 when others =>
2425 raise Program_Error;
2427 end case;
2429 Set_Is_Generic_Type (T);
2430 end Analyze_Formal_Type_Declaration;
2432 ------------------------------------
2433 -- Analyze_Function_Instantiation --
2434 ------------------------------------
2436 procedure Analyze_Function_Instantiation (N : Node_Id) is
2437 begin
2438 Analyze_Subprogram_Instantiation (N, E_Function);
2439 end Analyze_Function_Instantiation;
2441 ---------------------------------
2442 -- Analyze_Generic_Access_Type --
2443 ---------------------------------
2445 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
2446 begin
2447 Enter_Name (T);
2449 if Nkind (Def) = N_Access_To_Object_Definition then
2450 Access_Type_Declaration (T, Def);
2452 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
2453 and then No (Full_View (Designated_Type (T)))
2454 and then not Is_Generic_Type (Designated_Type (T))
2455 then
2456 Error_Msg_N ("premature usage of incomplete type", Def);
2458 elsif Is_Internal (Designated_Type (T)) then
2459 Error_Msg_N
2460 ("only a subtype mark is allowed in a formal", Def);
2461 end if;
2463 else
2464 Access_Subprogram_Declaration (T, Def);
2465 end if;
2466 end Analyze_Generic_Access_Type;
2468 ---------------------------------
2469 -- Analyze_Generic_Formal_Part --
2470 ---------------------------------
2472 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
2473 Gen_Parm_Decl : Node_Id;
2475 begin
2476 -- The generic formals are processed in the scope of the generic
2477 -- unit, where they are immediately visible. The scope is installed
2478 -- by the caller.
2480 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
2482 while Present (Gen_Parm_Decl) loop
2483 Analyze (Gen_Parm_Decl);
2484 Next (Gen_Parm_Decl);
2485 end loop;
2487 Generate_Reference_To_Generic_Formals (Current_Scope);
2488 end Analyze_Generic_Formal_Part;
2490 ------------------------------------------
2491 -- Analyze_Generic_Package_Declaration --
2492 ------------------------------------------
2494 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
2495 Loc : constant Source_Ptr := Sloc (N);
2496 Id : Entity_Id;
2497 New_N : Node_Id;
2498 Save_Parent : Node_Id;
2499 Renaming : Node_Id;
2500 Decls : constant List_Id :=
2501 Visible_Declarations (Specification (N));
2502 Decl : Node_Id;
2504 begin
2505 -- We introduce a renaming of the enclosing package, to have a usable
2506 -- entity as the prefix of an expanded name for a local entity of the
2507 -- form Par.P.Q, where P is the generic package. This is because a local
2508 -- entity named P may hide it, so that the usual visibility rules in
2509 -- the instance will not resolve properly.
2511 Renaming :=
2512 Make_Package_Renaming_Declaration (Loc,
2513 Defining_Unit_Name =>
2514 Make_Defining_Identifier (Loc,
2515 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
2516 Name => Make_Identifier (Loc, Chars (Defining_Entity (N))));
2518 if Present (Decls) then
2519 Decl := First (Decls);
2520 while Present (Decl)
2521 and then Nkind (Decl) = N_Pragma
2522 loop
2523 Next (Decl);
2524 end loop;
2526 if Present (Decl) then
2527 Insert_Before (Decl, Renaming);
2528 else
2529 Append (Renaming, Visible_Declarations (Specification (N)));
2530 end if;
2532 else
2533 Set_Visible_Declarations (Specification (N), New_List (Renaming));
2534 end if;
2536 -- Create copy of generic unit, and save for instantiation.
2537 -- If the unit is a child unit, do not copy the specifications
2538 -- for the parent, which are not part of the generic tree.
2540 Save_Parent := Parent_Spec (N);
2541 Set_Parent_Spec (N, Empty);
2543 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2544 Set_Parent_Spec (New_N, Save_Parent);
2545 Rewrite (N, New_N);
2546 Id := Defining_Entity (N);
2547 Generate_Definition (Id);
2549 -- Expansion is not applied to generic units
2551 Start_Generic;
2553 Enter_Name (Id);
2554 Set_Ekind (Id, E_Generic_Package);
2555 Set_Etype (Id, Standard_Void_Type);
2556 New_Scope (Id);
2557 Enter_Generic_Scope (Id);
2558 Set_Inner_Instances (Id, New_Elmt_List);
2560 Set_Categorization_From_Pragmas (N);
2561 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2563 -- Link the declaration of the generic homonym in the generic copy
2564 -- to the package it renames, so that it is always resolved properly.
2566 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
2567 Set_Entity (Associated_Node (Name (Renaming)), Id);
2569 -- For a library unit, we have reconstructed the entity for the
2570 -- unit, and must reset it in the library tables.
2572 if Nkind (Parent (N)) = N_Compilation_Unit then
2573 Set_Cunit_Entity (Current_Sem_Unit, Id);
2574 end if;
2576 Analyze_Generic_Formal_Part (N);
2578 -- After processing the generic formals, analysis proceeds
2579 -- as for a non-generic package.
2581 Analyze (Specification (N));
2583 Validate_Categorization_Dependency (N, Id);
2585 End_Generic;
2587 End_Package_Scope (Id);
2588 Exit_Generic_Scope (Id);
2590 if Nkind (Parent (N)) /= N_Compilation_Unit then
2591 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
2592 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
2593 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
2595 else
2596 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2597 Validate_RT_RAT_Component (N);
2599 -- If this is a spec without a body, check that generic parameters
2600 -- are referenced.
2602 if not Body_Required (Parent (N)) then
2603 Check_References (Id);
2604 end if;
2605 end if;
2606 end Analyze_Generic_Package_Declaration;
2608 --------------------------------------------
2609 -- Analyze_Generic_Subprogram_Declaration --
2610 --------------------------------------------
2612 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
2613 Spec : Node_Id;
2614 Id : Entity_Id;
2615 Formals : List_Id;
2616 New_N : Node_Id;
2617 Result_Type : Entity_Id;
2618 Save_Parent : Node_Id;
2620 begin
2621 -- Create copy of generic unit,and save for instantiation.
2622 -- If the unit is a child unit, do not copy the specifications
2623 -- for the parent, which are not part of the generic tree.
2625 Save_Parent := Parent_Spec (N);
2626 Set_Parent_Spec (N, Empty);
2628 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2629 Set_Parent_Spec (New_N, Save_Parent);
2630 Rewrite (N, New_N);
2632 Spec := Specification (N);
2633 Id := Defining_Entity (Spec);
2634 Generate_Definition (Id);
2636 if Nkind (Id) = N_Defining_Operator_Symbol then
2637 Error_Msg_N
2638 ("operator symbol not allowed for generic subprogram", Id);
2639 end if;
2641 Start_Generic;
2643 Enter_Name (Id);
2645 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
2646 New_Scope (Id);
2647 Enter_Generic_Scope (Id);
2648 Set_Inner_Instances (Id, New_Elmt_List);
2649 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2651 Analyze_Generic_Formal_Part (N);
2653 Formals := Parameter_Specifications (Spec);
2655 if Present (Formals) then
2656 Process_Formals (Formals, Spec);
2657 end if;
2659 if Nkind (Spec) = N_Function_Specification then
2660 Set_Ekind (Id, E_Generic_Function);
2662 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
2663 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
2664 Set_Etype (Id, Result_Type);
2665 else
2666 Find_Type (Result_Definition (Spec));
2667 Set_Etype (Id, Entity (Result_Definition (Spec)));
2668 end if;
2670 else
2671 Set_Ekind (Id, E_Generic_Procedure);
2672 Set_Etype (Id, Standard_Void_Type);
2673 end if;
2675 -- For a library unit, we have reconstructed the entity for the unit,
2676 -- and must reset it in the library tables. We also make sure that
2677 -- Body_Required is set properly in the original compilation unit node.
2679 if Nkind (Parent (N)) = N_Compilation_Unit then
2680 Set_Cunit_Entity (Current_Sem_Unit, Id);
2681 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2682 end if;
2684 Set_Categorization_From_Pragmas (N);
2685 Validate_Categorization_Dependency (N, Id);
2687 Save_Global_References (Original_Node (N));
2689 End_Generic;
2690 End_Scope;
2691 Exit_Generic_Scope (Id);
2692 Generate_Reference_To_Formals (Id);
2693 end Analyze_Generic_Subprogram_Declaration;
2695 -----------------------------------
2696 -- Analyze_Package_Instantiation --
2697 -----------------------------------
2699 procedure Analyze_Package_Instantiation (N : Node_Id) is
2700 Loc : constant Source_Ptr := Sloc (N);
2701 Gen_Id : constant Node_Id := Name (N);
2703 Act_Decl : Node_Id;
2704 Act_Decl_Name : Node_Id;
2705 Act_Decl_Id : Entity_Id;
2706 Act_Spec : Node_Id;
2707 Act_Tree : Node_Id;
2709 Gen_Decl : Node_Id;
2710 Gen_Unit : Entity_Id;
2712 Is_Actual_Pack : constant Boolean :=
2713 Is_Internal (Defining_Entity (N));
2715 Env_Installed : Boolean := False;
2716 Parent_Installed : Boolean := False;
2717 Renaming_List : List_Id;
2718 Unit_Renaming : Node_Id;
2719 Needs_Body : Boolean;
2720 Inline_Now : Boolean := False;
2722 procedure Delay_Descriptors (E : Entity_Id);
2723 -- Delay generation of subprogram descriptors for given entity
2725 function Might_Inline_Subp return Boolean;
2726 -- If inlining is active and the generic contains inlined subprograms,
2727 -- we instantiate the body. This may cause superfluous instantiations,
2728 -- but it is simpler than detecting the need for the body at the point
2729 -- of inlining, when the context of the instance is not available.
2731 -----------------------
2732 -- Delay_Descriptors --
2733 -----------------------
2735 procedure Delay_Descriptors (E : Entity_Id) is
2736 begin
2737 if not Delay_Subprogram_Descriptors (E) then
2738 Set_Delay_Subprogram_Descriptors (E);
2739 Pending_Descriptor.Increment_Last;
2740 Pending_Descriptor.Table (Pending_Descriptor.Last) := E;
2741 end if;
2742 end Delay_Descriptors;
2744 -----------------------
2745 -- Might_Inline_Subp --
2746 -----------------------
2748 function Might_Inline_Subp return Boolean is
2749 E : Entity_Id;
2751 begin
2752 if not Inline_Processing_Required then
2753 return False;
2755 else
2756 E := First_Entity (Gen_Unit);
2757 while Present (E) loop
2758 if Is_Subprogram (E)
2759 and then Is_Inlined (E)
2760 then
2761 return True;
2762 end if;
2764 Next_Entity (E);
2765 end loop;
2766 end if;
2768 return False;
2769 end Might_Inline_Subp;
2771 -- Start of processing for Analyze_Package_Instantiation
2773 begin
2774 -- Very first thing: apply the special kludge for Text_IO processing
2775 -- in case we are instantiating one of the children of [Wide_]Text_IO.
2777 Text_IO_Kludge (Name (N));
2779 -- Make node global for error reporting
2781 Instantiation_Node := N;
2783 -- Case of instantiation of a generic package
2785 if Nkind (N) = N_Package_Instantiation then
2786 Act_Decl_Id := New_Copy (Defining_Entity (N));
2787 Set_Comes_From_Source (Act_Decl_Id, True);
2789 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
2790 Act_Decl_Name :=
2791 Make_Defining_Program_Unit_Name (Loc,
2792 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
2793 Defining_Identifier => Act_Decl_Id);
2794 else
2795 Act_Decl_Name := Act_Decl_Id;
2796 end if;
2798 -- Case of instantiation of a formal package
2800 else
2801 Act_Decl_Id := Defining_Identifier (N);
2802 Act_Decl_Name := Act_Decl_Id;
2803 end if;
2805 Generate_Definition (Act_Decl_Id);
2806 Pre_Analyze_Actuals (N);
2808 Init_Env;
2809 Env_Installed := True;
2810 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2811 Gen_Unit := Entity (Gen_Id);
2813 -- Verify that it is the name of a generic package
2815 if Etype (Gen_Unit) = Any_Type then
2816 Restore_Env;
2817 return;
2819 elsif Ekind (Gen_Unit) /= E_Generic_Package then
2821 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
2823 if From_With_Type (Gen_Unit) then
2824 Error_Msg_N
2825 ("cannot instantiate a limited withed package", Gen_Id);
2826 else
2827 Error_Msg_N
2828 ("expect name of generic package in instantiation", Gen_Id);
2829 end if;
2831 Restore_Env;
2832 return;
2833 end if;
2835 if In_Extended_Main_Source_Unit (N) then
2836 Set_Is_Instantiated (Gen_Unit);
2837 Generate_Reference (Gen_Unit, N);
2839 if Present (Renamed_Object (Gen_Unit)) then
2840 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
2841 Generate_Reference (Renamed_Object (Gen_Unit), N);
2842 end if;
2843 end if;
2845 if Nkind (Gen_Id) = N_Identifier
2846 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
2847 then
2848 Error_Msg_NE
2849 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2851 elsif Nkind (Gen_Id) = N_Expanded_Name
2852 and then Is_Child_Unit (Gen_Unit)
2853 and then Nkind (Prefix (Gen_Id)) = N_Identifier
2854 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
2855 then
2856 Error_Msg_N
2857 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
2858 end if;
2860 Set_Entity (Gen_Id, Gen_Unit);
2862 -- If generic is a renaming, get original generic unit
2864 if Present (Renamed_Object (Gen_Unit))
2865 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
2866 then
2867 Gen_Unit := Renamed_Object (Gen_Unit);
2868 end if;
2870 -- Verify that there are no circular instantiations
2872 if In_Open_Scopes (Gen_Unit) then
2873 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
2874 Restore_Env;
2875 return;
2877 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
2878 Error_Msg_Node_2 := Current_Scope;
2879 Error_Msg_NE
2880 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
2881 Circularity_Detected := True;
2882 Restore_Env;
2883 return;
2885 else
2886 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
2887 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2889 -- Initialize renamings map, for error checking, and the list
2890 -- that holds private entities whose views have changed between
2891 -- generic definition and instantiation. If this is the instance
2892 -- created to validate an actual package, the instantiation
2893 -- environment is that of the enclosing instance.
2895 Generic_Renamings.Set_Last (0);
2896 Generic_Renamings_HTable.Reset;
2898 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2900 -- Copy original generic tree, to produce text for instantiation
2902 Act_Tree :=
2903 Copy_Generic_Node
2904 (Original_Node (Gen_Decl), Empty, Instantiating => True);
2906 Act_Spec := Specification (Act_Tree);
2908 -- If this is the instance created to validate an actual package,
2909 -- only the formals matter, do not examine the package spec itself.
2911 if Is_Actual_Pack then
2912 Set_Visible_Declarations (Act_Spec, New_List);
2913 Set_Private_Declarations (Act_Spec, New_List);
2914 end if;
2916 Renaming_List :=
2917 Analyze_Associations
2919 Generic_Formal_Declarations (Act_Tree),
2920 Generic_Formal_Declarations (Gen_Decl));
2922 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
2923 Set_Is_Generic_Instance (Act_Decl_Id);
2925 Set_Generic_Parent (Act_Spec, Gen_Unit);
2927 -- References to the generic in its own declaration or its body
2928 -- are references to the instance. Add a renaming declaration for
2929 -- the generic unit itself. This declaration, as well as the renaming
2930 -- declarations for the generic formals, must remain private to the
2931 -- unit: the formals, because this is the language semantics, and
2932 -- the unit because its use is an artifact of the implementation.
2934 Unit_Renaming :=
2935 Make_Package_Renaming_Declaration (Loc,
2936 Defining_Unit_Name =>
2937 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2938 Name => New_Reference_To (Act_Decl_Id, Loc));
2940 Append (Unit_Renaming, Renaming_List);
2942 -- The renaming declarations are the first local declarations of
2943 -- the new unit.
2945 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
2946 Insert_List_Before
2947 (First (Visible_Declarations (Act_Spec)), Renaming_List);
2948 else
2949 Set_Visible_Declarations (Act_Spec, Renaming_List);
2950 end if;
2952 Act_Decl :=
2953 Make_Package_Declaration (Loc,
2954 Specification => Act_Spec);
2956 -- Save the instantiation node, for subsequent instantiation
2957 -- of the body, if there is one and we are generating code for
2958 -- the current unit. Mark the unit as having a body, to avoid
2959 -- a premature error message.
2961 -- We instantiate the body if we are generating code, if we are
2962 -- generating cross-reference information, or if we are building
2963 -- trees for ASIS use.
2965 declare
2966 Enclosing_Body_Present : Boolean := False;
2967 -- If the generic unit is not a compilation unit, then a body
2968 -- may be present in its parent even if none is required. We
2969 -- create a tentative pending instantiation for the body, which
2970 -- will be discarded if none is actually present.
2972 Scop : Entity_Id;
2974 begin
2975 if Scope (Gen_Unit) /= Standard_Standard
2976 and then not Is_Child_Unit (Gen_Unit)
2977 then
2978 Scop := Scope (Gen_Unit);
2980 while Present (Scop)
2981 and then Scop /= Standard_Standard
2982 loop
2983 if Unit_Requires_Body (Scop) then
2984 Enclosing_Body_Present := True;
2985 exit;
2987 elsif In_Open_Scopes (Scop)
2988 and then In_Package_Body (Scop)
2989 then
2990 Enclosing_Body_Present := True;
2991 exit;
2992 end if;
2994 exit when Is_Compilation_Unit (Scop);
2995 Scop := Scope (Scop);
2996 end loop;
2997 end if;
2999 -- If front-end inlining is enabled, and this is a unit for which
3000 -- code will be generated, we instantiate the body at once.
3001 -- This is done if the instance is not the main unit, and if the
3002 -- generic is not a child unit of another generic, to avoid scope
3003 -- problems and the reinstallation of parent instances.
3005 if Expander_Active
3006 and then (not Is_Child_Unit (Gen_Unit)
3007 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3008 and then Might_Inline_Subp
3009 and then not Is_Actual_Pack
3010 then
3011 if Front_End_Inlining
3012 and then (Is_In_Main_Unit (N)
3013 or else In_Main_Context (Current_Scope))
3014 and then Nkind (Parent (N)) /= N_Compilation_Unit
3015 then
3016 Inline_Now := True;
3018 -- In configurable_run_time mode we force the inlining of
3019 -- predefined subprogram marked Inline_Always, to minimize
3020 -- the use of the run-time library.
3022 elsif Is_Predefined_File_Name
3023 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3024 and then Configurable_Run_Time_Mode
3025 and then Nkind (Parent (N)) /= N_Compilation_Unit
3026 then
3027 Inline_Now := True;
3028 end if;
3030 -- If the current scope is itself an instance within a child
3031 -- unit,there will be duplications in the scope stack, and the
3032 -- unstacking mechanism in Inline_Instance_Body will fail.
3033 -- This loses some rare cases of optimization, and might be
3034 -- improved some day, if we can find a proper abstraction for
3035 -- "the complete compilation context" that can be saved and
3036 -- restored ???
3038 if Is_Generic_Instance (Current_Scope) then
3039 declare
3040 Curr_Unit : constant Entity_Id :=
3041 Cunit_Entity (Current_Sem_Unit);
3042 begin
3043 if Curr_Unit /= Current_Scope
3044 and then Is_Child_Unit (Curr_Unit)
3045 then
3046 Inline_Now := False;
3047 end if;
3048 end;
3049 end if;
3050 end if;
3052 Needs_Body :=
3053 (Unit_Requires_Body (Gen_Unit)
3054 or else Enclosing_Body_Present
3055 or else Present (Corresponding_Body (Gen_Decl)))
3056 and then (Is_In_Main_Unit (N)
3057 or else Might_Inline_Subp)
3058 and then not Is_Actual_Pack
3059 and then not Inline_Now
3060 and then (Operating_Mode = Generate_Code
3061 or else (Operating_Mode = Check_Semantics
3062 and then ASIS_Mode));
3064 -- If front_end_inlining is enabled, do not instantiate a
3065 -- body if within a generic context.
3067 if (Front_End_Inlining
3068 and then not Expander_Active)
3069 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
3070 then
3071 Needs_Body := False;
3072 end if;
3074 -- If the current context is generic, and the package being
3075 -- instantiated is declared within a formal package, there is no
3076 -- body to instantiate until the enclosing generic is instantiated
3077 -- and there is an actual for the formal package. If the formal
3078 -- package has parameters, we build regular package instance for
3079 -- it, that preceeds the original formal package declaration.
3081 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
3082 declare
3083 Decl : constant Node_Id :=
3084 Original_Node
3085 (Unit_Declaration_Node (Scope (Gen_Unit)));
3086 begin
3087 if Nkind (Decl) = N_Formal_Package_Declaration
3088 or else (Nkind (Decl) = N_Package_Declaration
3089 and then Is_List_Member (Decl)
3090 and then Present (Next (Decl))
3091 and then
3092 Nkind (Next (Decl)) = N_Formal_Package_Declaration)
3093 then
3094 Needs_Body := False;
3095 end if;
3096 end;
3097 end if;
3098 end;
3100 -- If we are generating the calling stubs from the instantiation of
3101 -- a generic RCI package, we will not use the body of the generic
3102 -- package.
3104 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
3105 and then Is_Compilation_Unit (Defining_Entity (N))
3106 then
3107 Needs_Body := False;
3108 end if;
3110 if Needs_Body then
3112 -- Here is a defence against a ludicrous number of instantiations
3113 -- caused by a circular set of instantiation attempts.
3115 if Pending_Instantiations.Last >
3116 Hostparm.Max_Instantiations
3117 then
3118 Error_Msg_N ("too many instantiations", N);
3119 raise Unrecoverable_Error;
3120 end if;
3122 -- Indicate that the enclosing scopes contain an instantiation,
3123 -- and that cleanup actions should be delayed until after the
3124 -- instance body is expanded.
3126 Check_Forward_Instantiation (Gen_Decl);
3127 if Nkind (N) = N_Package_Instantiation then
3128 declare
3129 Enclosing_Master : Entity_Id := Current_Scope;
3131 begin
3132 while Enclosing_Master /= Standard_Standard loop
3134 if Ekind (Enclosing_Master) = E_Package then
3135 if Is_Compilation_Unit (Enclosing_Master) then
3136 if In_Package_Body (Enclosing_Master) then
3137 Delay_Descriptors
3138 (Body_Entity (Enclosing_Master));
3139 else
3140 Delay_Descriptors
3141 (Enclosing_Master);
3142 end if;
3144 exit;
3146 else
3147 Enclosing_Master := Scope (Enclosing_Master);
3148 end if;
3150 elsif Ekind (Enclosing_Master) = E_Generic_Package then
3151 Enclosing_Master := Scope (Enclosing_Master);
3153 elsif Is_Generic_Subprogram (Enclosing_Master)
3154 or else Ekind (Enclosing_Master) = E_Void
3155 then
3156 -- Cleanup actions will eventually be performed on
3157 -- the enclosing instance, if any. enclosing scope
3158 -- is void in the formal part of a generic subp.
3160 exit;
3162 else
3163 if Ekind (Enclosing_Master) = E_Entry
3164 and then
3165 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
3166 then
3167 Enclosing_Master :=
3168 Protected_Body_Subprogram (Enclosing_Master);
3169 end if;
3171 Set_Delay_Cleanups (Enclosing_Master);
3173 while Ekind (Enclosing_Master) = E_Block loop
3174 Enclosing_Master := Scope (Enclosing_Master);
3175 end loop;
3177 if Is_Subprogram (Enclosing_Master) then
3178 Delay_Descriptors (Enclosing_Master);
3180 elsif Is_Task_Type (Enclosing_Master) then
3181 declare
3182 TBP : constant Node_Id :=
3183 Get_Task_Body_Procedure
3184 (Enclosing_Master);
3186 begin
3187 if Present (TBP) then
3188 Delay_Descriptors (TBP);
3189 Set_Delay_Cleanups (TBP);
3190 end if;
3191 end;
3192 end if;
3194 exit;
3195 end if;
3196 end loop;
3197 end;
3199 -- Make entry in table
3201 Pending_Instantiations.Increment_Last;
3202 Pending_Instantiations.Table (Pending_Instantiations.Last) :=
3203 (N, Act_Decl, Expander_Active, Current_Sem_Unit);
3204 end if;
3205 end if;
3207 Set_Categorization_From_Pragmas (Act_Decl);
3209 if Parent_Installed then
3210 Hide_Current_Scope;
3211 end if;
3213 Set_Instance_Spec (N, Act_Decl);
3215 -- If not a compilation unit, insert the package declaration
3216 -- before the original instantiation node.
3218 if Nkind (Parent (N)) /= N_Compilation_Unit then
3219 Mark_Rewrite_Insertion (Act_Decl);
3220 Insert_Before (N, Act_Decl);
3221 Analyze (Act_Decl);
3223 -- For an instantiation that is a compilation unit, place
3224 -- declaration on current node so context is complete
3225 -- for analysis (including nested instantiations). It this
3226 -- is the main unit, the declaration eventually replaces the
3227 -- instantiation node. If the instance body is later created, it
3228 -- replaces the instance node, and the declation is attached to
3229 -- it (see Build_Instance_Compilation_Unit_Nodes).
3231 else
3232 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
3234 -- The entity for the current unit is the newly created one,
3235 -- and all semantic information is attached to it.
3237 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
3239 -- If this is the main unit, replace the main entity as well
3241 if Current_Sem_Unit = Main_Unit then
3242 Main_Unit_Entity := Act_Decl_Id;
3243 end if;
3244 end if;
3246 Set_Unit (Parent (N), Act_Decl);
3247 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
3248 Set_Package_Instantiation (Act_Decl_Id, N);
3249 Analyze (Act_Decl);
3250 Set_Unit (Parent (N), N);
3251 Set_Body_Required (Parent (N), False);
3253 -- We never need elaboration checks on instantiations, since
3254 -- by definition, the body instantiation is elaborated at the
3255 -- same time as the spec instantiation.
3257 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
3258 Set_Kill_Elaboration_Checks (Act_Decl_Id);
3259 end if;
3261 Check_Elab_Instantiation (N);
3263 if ABE_Is_Certain (N) and then Needs_Body then
3264 Pending_Instantiations.Decrement_Last;
3265 end if;
3266 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
3268 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
3269 First_Private_Entity (Act_Decl_Id));
3271 -- If the instantiation will receive a body, the unit will
3272 -- be transformed into a package body, and receive its own
3273 -- elaboration entity. Otherwise, the nature of the unit is
3274 -- now a package declaration.
3276 if Nkind (Parent (N)) = N_Compilation_Unit
3277 and then not Needs_Body
3278 then
3279 Rewrite (N, Act_Decl);
3280 end if;
3282 if Present (Corresponding_Body (Gen_Decl))
3283 or else Unit_Requires_Body (Gen_Unit)
3284 then
3285 Set_Has_Completion (Act_Decl_Id);
3286 end if;
3288 Check_Formal_Packages (Act_Decl_Id);
3290 Restore_Private_Views (Act_Decl_Id);
3292 if not Generic_Separately_Compiled (Gen_Unit) then
3293 Inherit_Context (Gen_Decl, N);
3294 end if;
3296 if Parent_Installed then
3297 Remove_Parent;
3298 end if;
3300 Restore_Env;
3301 Env_Installed := False;
3302 end if;
3304 Validate_Categorization_Dependency (N, Act_Decl_Id);
3306 -- Check restriction, but skip this if something went wrong in
3307 -- the above analysis, indicated by Act_Decl_Id being void.
3309 if Ekind (Act_Decl_Id) /= E_Void
3310 and then not Is_Library_Level_Entity (Act_Decl_Id)
3311 then
3312 Check_Restriction (No_Local_Allocators, N);
3313 end if;
3315 if Inline_Now then
3316 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
3317 end if;
3319 -- The following is a tree patch for ASIS: ASIS needs separate nodes
3320 -- to be used as defining identifiers for a formal package and for the
3321 -- corresponding expanded package
3323 if Nkind (N) = N_Formal_Package_Declaration then
3324 Act_Decl_Id := New_Copy (Defining_Entity (N));
3325 Set_Comes_From_Source (Act_Decl_Id, True);
3326 Set_Is_Generic_Instance (Act_Decl_Id, False);
3327 Set_Defining_Identifier (N, Act_Decl_Id);
3328 end if;
3330 exception
3331 when Instantiation_Error =>
3332 if Parent_Installed then
3333 Remove_Parent;
3334 end if;
3336 if Env_Installed then
3337 Restore_Env;
3338 end if;
3339 end Analyze_Package_Instantiation;
3341 --------------------------
3342 -- Inline_Instance_Body --
3343 --------------------------
3345 procedure Inline_Instance_Body
3346 (N : Node_Id;
3347 Gen_Unit : Entity_Id;
3348 Act_Decl : Node_Id)
3350 Vis : Boolean;
3351 Gen_Comp : constant Entity_Id :=
3352 Cunit_Entity (Get_Source_Unit (Gen_Unit));
3353 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
3354 Curr_Scope : Entity_Id := Empty;
3355 Curr_Unit : constant Entity_Id :=
3356 Cunit_Entity (Current_Sem_Unit);
3357 Removed : Boolean := False;
3358 Num_Scopes : Int := 0;
3360 Scope_Stack_Depth : constant Int :=
3361 Scope_Stack.Last - Scope_Stack.First + 1;
3363 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
3364 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
3365 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
3366 Num_Inner : Int := 0;
3367 N_Instances : Int := 0;
3368 S : Entity_Id;
3370 begin
3371 -- Case of generic unit defined in another unit. We must remove the
3372 -- complete context of the current unit to install that of the generic.
3374 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
3376 -- Add some comments for the following two loops ???
3378 S := Current_Scope;
3379 while Present (S) and then S /= Standard_Standard loop
3380 loop
3381 Num_Scopes := Num_Scopes + 1;
3383 Use_Clauses (Num_Scopes) :=
3384 (Scope_Stack.Table
3385 (Scope_Stack.Last - Num_Scopes + 1).
3386 First_Use_Clause);
3387 End_Use_Clauses (Use_Clauses (Num_Scopes));
3389 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
3390 or else Scope_Stack.Table
3391 (Scope_Stack.Last - Num_Scopes).Entity
3392 = Scope (S);
3393 end loop;
3395 exit when Is_Generic_Instance (S)
3396 and then (In_Package_Body (S)
3397 or else Ekind (S) = E_Procedure
3398 or else Ekind (S) = E_Function);
3399 S := Scope (S);
3400 end loop;
3402 Vis := Is_Immediately_Visible (Gen_Comp);
3404 -- Find and save all enclosing instances
3406 S := Current_Scope;
3408 while Present (S)
3409 and then S /= Standard_Standard
3410 loop
3411 if Is_Generic_Instance (S) then
3412 N_Instances := N_Instances + 1;
3413 Instances (N_Instances) := S;
3415 exit when In_Package_Body (S);
3416 end if;
3418 S := Scope (S);
3419 end loop;
3421 -- Remove context of current compilation unit, unless we are within a
3422 -- nested package instantiation, in which case the context has been
3423 -- removed previously.
3425 -- If current scope is the body of a child unit, remove context of
3426 -- spec as well.
3428 S := Current_Scope;
3430 while Present (S)
3431 and then S /= Standard_Standard
3432 loop
3433 exit when Is_Generic_Instance (S)
3434 and then (In_Package_Body (S)
3435 or else Ekind (S) = E_Procedure
3436 or else Ekind (S) = E_Function);
3438 if S = Curr_Unit
3439 or else (Ekind (Curr_Unit) = E_Package_Body
3440 and then S = Spec_Entity (Curr_Unit))
3441 or else (Ekind (Curr_Unit) = E_Subprogram_Body
3442 and then S =
3443 Corresponding_Spec
3444 (Unit_Declaration_Node (Curr_Unit)))
3445 then
3446 Removed := True;
3448 -- Remove entities in current scopes from visibility, so
3449 -- that instance body is compiled in a clean environment.
3451 Save_Scope_Stack (Handle_Use => False);
3453 if Is_Child_Unit (S) then
3455 -- Remove child unit from stack, as well as inner scopes.
3456 -- Removing the context of a child unit removes parent
3457 -- units as well.
3459 while Current_Scope /= S loop
3460 Num_Inner := Num_Inner + 1;
3461 Inner_Scopes (Num_Inner) := Current_Scope;
3462 Pop_Scope;
3463 end loop;
3465 Pop_Scope;
3466 Remove_Context (Curr_Comp);
3467 Curr_Scope := S;
3469 else
3470 Remove_Context (Curr_Comp);
3471 end if;
3473 if Ekind (Curr_Unit) = E_Package_Body then
3474 Remove_Context (Library_Unit (Curr_Comp));
3475 end if;
3476 end if;
3478 S := Scope (S);
3479 end loop;
3480 pragma Assert (Num_Inner < Num_Scopes);
3482 New_Scope (Standard_Standard);
3483 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
3484 Instantiate_Package_Body
3485 ((N, Act_Decl, Expander_Active, Current_Sem_Unit), True);
3486 Pop_Scope;
3488 -- Restore context
3490 Set_Is_Immediately_Visible (Gen_Comp, Vis);
3492 -- Reset Generic_Instance flag so that use clauses can be installed
3493 -- in the proper order. (See Use_One_Package for effect of enclosing
3494 -- instances on processing of use clauses).
3496 for J in 1 .. N_Instances loop
3497 Set_Is_Generic_Instance (Instances (J), False);
3498 end loop;
3500 if Removed then
3501 Install_Context (Curr_Comp);
3503 if Present (Curr_Scope)
3504 and then Is_Child_Unit (Curr_Scope)
3505 then
3506 New_Scope (Curr_Scope);
3507 Set_Is_Immediately_Visible (Curr_Scope);
3509 -- Finally, restore inner scopes as well
3511 for J in reverse 1 .. Num_Inner loop
3512 New_Scope (Inner_Scopes (J));
3513 end loop;
3514 end if;
3516 Restore_Scope_Stack (Handle_Use => False);
3518 if Present (Curr_Scope)
3519 and then
3520 (In_Private_Part (Curr_Scope)
3521 or else In_Package_Body (Curr_Scope))
3522 then
3523 -- Install private declaration of ancestor units, which
3524 -- are currently available. Restore_Scope_Stack and
3525 -- Install_Context only install the visible part of parents.
3527 declare
3528 Par : Entity_Id;
3529 begin
3530 Par := Scope (Curr_Scope);
3531 while (Present (Par))
3532 and then Par /= Standard_Standard
3533 loop
3534 Install_Private_Declarations (Par);
3535 Par := Scope (Par);
3536 end loop;
3537 end;
3538 end if;
3539 end if;
3541 -- Restore use clauses. For a child unit, use clauses in the parents
3542 -- are restored when installing the context, so only those in inner
3543 -- scopes (and those local to the child unit itself) need to be
3544 -- installed explicitly.
3546 if Is_Child_Unit (Curr_Unit)
3547 and then Removed
3548 then
3549 for J in reverse 1 .. Num_Inner + 1 loop
3550 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3551 Use_Clauses (J);
3552 Install_Use_Clauses (Use_Clauses (J));
3553 end loop;
3555 else
3556 for J in reverse 1 .. Num_Scopes loop
3557 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3558 Use_Clauses (J);
3559 Install_Use_Clauses (Use_Clauses (J));
3560 end loop;
3561 end if;
3563 for J in 1 .. N_Instances loop
3564 Set_Is_Generic_Instance (Instances (J), True);
3565 end loop;
3567 -- If generic unit is in current unit, current context is correct
3569 else
3570 Instantiate_Package_Body
3571 ((N, Act_Decl, Expander_Active, Current_Sem_Unit), True);
3572 end if;
3573 end Inline_Instance_Body;
3575 -------------------------------------
3576 -- Analyze_Procedure_Instantiation --
3577 -------------------------------------
3579 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
3580 begin
3581 Analyze_Subprogram_Instantiation (N, E_Procedure);
3582 end Analyze_Procedure_Instantiation;
3584 --------------------------------------
3585 -- Analyze_Subprogram_Instantiation --
3586 --------------------------------------
3588 procedure Analyze_Subprogram_Instantiation
3589 (N : Node_Id;
3590 K : Entity_Kind)
3592 Loc : constant Source_Ptr := Sloc (N);
3593 Gen_Id : constant Node_Id := Name (N);
3595 Anon_Id : constant Entity_Id :=
3596 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
3597 Chars => New_External_Name
3598 (Chars (Defining_Entity (N)), 'R'));
3600 Act_Decl_Id : Entity_Id;
3601 Act_Decl : Node_Id;
3602 Act_Spec : Node_Id;
3603 Act_Tree : Node_Id;
3605 Env_Installed : Boolean := False;
3606 Gen_Unit : Entity_Id;
3607 Gen_Decl : Node_Id;
3608 Pack_Id : Entity_Id;
3609 Parent_Installed : Boolean := False;
3610 Renaming_List : List_Id;
3612 procedure Analyze_Instance_And_Renamings;
3613 -- The instance must be analyzed in a context that includes the
3614 -- mappings of generic parameters into actuals. We create a package
3615 -- declaration for this purpose, and a subprogram with an internal
3616 -- name within the package. The subprogram instance is simply an
3617 -- alias for the internal subprogram, declared in the current scope.
3619 ------------------------------------
3620 -- Analyze_Instance_And_Renamings --
3621 ------------------------------------
3623 procedure Analyze_Instance_And_Renamings is
3624 Def_Ent : constant Entity_Id := Defining_Entity (N);
3625 Pack_Decl : Node_Id;
3627 begin
3628 if Nkind (Parent (N)) = N_Compilation_Unit then
3630 -- For the case of a compilation unit, the container package
3631 -- has the same name as the instantiation, to insure that the
3632 -- binder calls the elaboration procedure with the right name.
3633 -- Copy the entity of the instance, which may have compilation
3634 -- level flags (e.g. Is_Child_Unit) set.
3636 Pack_Id := New_Copy (Def_Ent);
3638 else
3639 -- Otherwise we use the name of the instantiation concatenated
3640 -- with its source position to ensure uniqueness if there are
3641 -- several instantiations with the same name.
3643 Pack_Id :=
3644 Make_Defining_Identifier (Loc,
3645 Chars => New_External_Name
3646 (Related_Id => Chars (Def_Ent),
3647 Suffix => "GP",
3648 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
3649 end if;
3651 Pack_Decl := Make_Package_Declaration (Loc,
3652 Specification => Make_Package_Specification (Loc,
3653 Defining_Unit_Name => Pack_Id,
3654 Visible_Declarations => Renaming_List,
3655 End_Label => Empty));
3657 Set_Instance_Spec (N, Pack_Decl);
3658 Set_Is_Generic_Instance (Pack_Id);
3659 Set_Needs_Debug_Info (Pack_Id);
3661 -- Case of not a compilation unit
3663 if Nkind (Parent (N)) /= N_Compilation_Unit then
3664 Mark_Rewrite_Insertion (Pack_Decl);
3665 Insert_Before (N, Pack_Decl);
3666 Set_Has_Completion (Pack_Id);
3668 -- Case of an instantiation that is a compilation unit
3670 -- Place declaration on current node so context is complete
3671 -- for analysis (including nested instantiations), and for
3672 -- use in a context_clause (see Analyze_With_Clause).
3674 else
3675 Set_Unit (Parent (N), Pack_Decl);
3676 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
3677 end if;
3679 Analyze (Pack_Decl);
3680 Check_Formal_Packages (Pack_Id);
3681 Set_Is_Generic_Instance (Pack_Id, False);
3683 -- Body of the enclosing package is supplied when instantiating
3684 -- the subprogram body, after semantic analysis is completed.
3686 if Nkind (Parent (N)) = N_Compilation_Unit then
3688 -- Remove package itself from visibility, so it does not
3689 -- conflict with subprogram.
3691 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
3693 -- Set name and scope of internal subprogram so that the
3694 -- proper external name will be generated. The proper scope
3695 -- is the scope of the wrapper package. We need to generate
3696 -- debugging information for the internal subprogram, so set
3697 -- flag accordingly.
3699 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
3700 Set_Scope (Anon_Id, Scope (Pack_Id));
3702 -- Mark wrapper package as referenced, to avoid spurious
3703 -- warnings if the instantiation appears in various with_
3704 -- clauses of subunits of the main unit.
3706 Set_Referenced (Pack_Id);
3707 end if;
3709 Set_Is_Generic_Instance (Anon_Id);
3710 Set_Needs_Debug_Info (Anon_Id);
3711 Act_Decl_Id := New_Copy (Anon_Id);
3713 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3714 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
3715 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
3716 Set_Comes_From_Source (Act_Decl_Id, True);
3718 -- The signature may involve types that are not frozen yet, but
3719 -- the subprogram will be frozen at the point the wrapper package
3720 -- is frozen, so it does not need its own freeze node. In fact, if
3721 -- one is created, it might conflict with the freezing actions from
3722 -- the wrapper package (see 7206-013).
3724 Set_Has_Delayed_Freeze (Anon_Id, False);
3726 -- If the instance is a child unit, mark the Id accordingly. Mark
3727 -- the anonymous entity as well, which is the real subprogram and
3728 -- which is used when the instance appears in a context clause.
3730 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
3731 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
3732 New_Overloaded_Entity (Act_Decl_Id);
3733 Check_Eliminated (Act_Decl_Id);
3735 -- In compilation unit case, kill elaboration checks on the
3736 -- instantiation, since they are never needed -- the body is
3737 -- instantiated at the same point as the spec.
3739 if Nkind (Parent (N)) = N_Compilation_Unit then
3740 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
3741 Set_Kill_Elaboration_Checks (Act_Decl_Id);
3742 Set_Is_Compilation_Unit (Anon_Id);
3744 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
3745 end if;
3747 -- The instance is not a freezing point for the new subprogram
3749 Set_Is_Frozen (Act_Decl_Id, False);
3751 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
3752 Valid_Operator_Definition (Act_Decl_Id);
3753 end if;
3755 Set_Alias (Act_Decl_Id, Anon_Id);
3756 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3757 Set_Has_Completion (Act_Decl_Id);
3758 Set_Related_Instance (Pack_Id, Act_Decl_Id);
3760 if Nkind (Parent (N)) = N_Compilation_Unit then
3761 Set_Body_Required (Parent (N), False);
3762 end if;
3764 end Analyze_Instance_And_Renamings;
3766 -- Start of processing for Analyze_Subprogram_Instantiation
3768 begin
3769 -- Very first thing: apply the special kludge for Text_IO processing
3770 -- in case we are instantiating one of the children of [Wide_]Text_IO.
3771 -- Of course such an instantiation is bogus (these are packages, not
3772 -- subprograms), but we get a better error message if we do this.
3774 Text_IO_Kludge (Gen_Id);
3776 -- Make node global for error reporting
3778 Instantiation_Node := N;
3779 Pre_Analyze_Actuals (N);
3781 Init_Env;
3782 Env_Installed := True;
3783 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3784 Gen_Unit := Entity (Gen_Id);
3786 Generate_Reference (Gen_Unit, Gen_Id);
3788 if Nkind (Gen_Id) = N_Identifier
3789 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3790 then
3791 Error_Msg_NE
3792 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3793 end if;
3795 if Etype (Gen_Unit) = Any_Type then
3796 Restore_Env;
3797 return;
3798 end if;
3800 -- Verify that it is a generic subprogram of the right kind, and that
3801 -- it does not lead to a circular instantiation.
3803 if Ekind (Gen_Unit) /= E_Generic_Procedure
3804 and then Ekind (Gen_Unit) /= E_Generic_Function
3805 then
3806 Error_Msg_N ("expect generic subprogram in instantiation", Gen_Id);
3808 elsif In_Open_Scopes (Gen_Unit) then
3809 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3811 elsif K = E_Procedure
3812 and then Ekind (Gen_Unit) /= E_Generic_Procedure
3813 then
3814 if Ekind (Gen_Unit) = E_Generic_Function then
3815 Error_Msg_N
3816 ("cannot instantiate generic function as procedure", Gen_Id);
3817 else
3818 Error_Msg_N
3819 ("expect name of generic procedure in instantiation", Gen_Id);
3820 end if;
3822 elsif K = E_Function
3823 and then Ekind (Gen_Unit) /= E_Generic_Function
3824 then
3825 if Ekind (Gen_Unit) = E_Generic_Procedure then
3826 Error_Msg_N
3827 ("cannot instantiate generic procedure as function", Gen_Id);
3828 else
3829 Error_Msg_N
3830 ("expect name of generic function in instantiation", Gen_Id);
3831 end if;
3833 else
3834 Set_Entity (Gen_Id, Gen_Unit);
3835 Set_Is_Instantiated (Gen_Unit);
3837 if In_Extended_Main_Source_Unit (N) then
3838 Generate_Reference (Gen_Unit, N);
3839 end if;
3841 -- If renaming, get original unit
3843 if Present (Renamed_Object (Gen_Unit))
3844 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
3845 or else
3846 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
3847 then
3848 Gen_Unit := Renamed_Object (Gen_Unit);
3849 Set_Is_Instantiated (Gen_Unit);
3850 Generate_Reference (Gen_Unit, N);
3851 end if;
3853 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3854 Error_Msg_Node_2 := Current_Scope;
3855 Error_Msg_NE
3856 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3857 Circularity_Detected := True;
3858 return;
3859 end if;
3861 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3863 -- The subprogram itself cannot contain a nested instance, so
3864 -- the current parent is left empty.
3866 Set_Instance_Env (Gen_Unit, Empty);
3868 -- Initialize renamings map, for error checking
3870 Generic_Renamings.Set_Last (0);
3871 Generic_Renamings_HTable.Reset;
3873 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3875 -- Copy original generic tree, to produce text for instantiation
3877 Act_Tree :=
3878 Copy_Generic_Node
3879 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3881 Act_Spec := Specification (Act_Tree);
3882 Renaming_List :=
3883 Analyze_Associations
3885 Generic_Formal_Declarations (Act_Tree),
3886 Generic_Formal_Declarations (Gen_Decl));
3888 -- Build the subprogram declaration, which does not appear
3889 -- in the generic template, and give it a sloc consistent
3890 -- with that of the template.
3892 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
3893 Set_Generic_Parent (Act_Spec, Gen_Unit);
3894 Act_Decl :=
3895 Make_Subprogram_Declaration (Sloc (Act_Spec),
3896 Specification => Act_Spec);
3898 Set_Categorization_From_Pragmas (Act_Decl);
3900 if Parent_Installed then
3901 Hide_Current_Scope;
3902 end if;
3904 Append (Act_Decl, Renaming_List);
3905 Analyze_Instance_And_Renamings;
3907 -- If the generic is marked Import (Intrinsic), then so is the
3908 -- instance. This indicates that there is no body to instantiate.
3909 -- If generic is marked inline, so it the instance, and the
3910 -- anonymous subprogram it renames. If inlined, or else if inlining
3911 -- is enabled for the compilation, we generate the instance body
3912 -- even if it is not within the main unit.
3914 -- Any other pragmas might also be inherited ???
3916 if Is_Intrinsic_Subprogram (Gen_Unit) then
3917 Set_Is_Intrinsic_Subprogram (Anon_Id);
3918 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
3920 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
3921 Validate_Unchecked_Conversion (N, Act_Decl_Id);
3922 end if;
3923 end if;
3925 Generate_Definition (Act_Decl_Id);
3927 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
3928 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
3930 if not Is_Intrinsic_Subprogram (Gen_Unit) then
3931 Check_Elab_Instantiation (N);
3932 end if;
3934 if Is_Dispatching_Operation (Act_Decl_Id)
3935 and then Ada_Version >= Ada_05
3936 then
3937 declare
3938 Formal : Entity_Id;
3940 begin
3941 Formal := First_Formal (Act_Decl_Id);
3942 while Present (Formal) loop
3943 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
3944 and then Is_Controlling_Formal (Formal)
3945 and then not Can_Never_Be_Null (Formal)
3946 then
3947 Error_Msg_NE ("access parameter& is controlling,",
3948 N, Formal);
3949 Error_Msg_NE ("\corresponding parameter of & must be"
3950 & " explicitly null-excluding", N, Gen_Id);
3951 end if;
3953 Next_Formal (Formal);
3954 end loop;
3955 end;
3956 end if;
3958 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
3960 -- Subject to change, pending on if other pragmas are inherited ???
3962 Validate_Categorization_Dependency (N, Act_Decl_Id);
3964 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
3965 if not Generic_Separately_Compiled (Gen_Unit) then
3966 Inherit_Context (Gen_Decl, N);
3967 end if;
3969 Restore_Private_Views (Pack_Id, False);
3971 -- If the context requires a full instantiation, mark node for
3972 -- subsequent construction of the body.
3974 if (Is_In_Main_Unit (N)
3975 or else Is_Inlined (Act_Decl_Id))
3976 and then (Operating_Mode = Generate_Code
3977 or else (Operating_Mode = Check_Semantics
3978 and then ASIS_Mode))
3979 and then (Expander_Active or else ASIS_Mode)
3980 and then not ABE_Is_Certain (N)
3981 and then not Is_Eliminated (Act_Decl_Id)
3982 then
3983 Pending_Instantiations.Increment_Last;
3984 Pending_Instantiations.Table (Pending_Instantiations.Last) :=
3985 (N, Act_Decl, Expander_Active, Current_Sem_Unit);
3986 Check_Forward_Instantiation (Gen_Decl);
3988 -- The wrapper package is always delayed, because it does
3989 -- not constitute a freeze point, but to insure that the
3990 -- freeze node is placed properly, it is created directly
3991 -- when instantiating the body (otherwise the freeze node
3992 -- might appear to early for nested instantiations).
3994 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3996 -- For ASIS purposes, indicate that the wrapper package has
3997 -- replaced the instantiation node.
3999 Rewrite (N, Unit (Parent (N)));
4000 Set_Unit (Parent (N), N);
4001 end if;
4003 elsif Nkind (Parent (N)) = N_Compilation_Unit then
4005 -- Replace instance node for library-level instantiations
4006 -- of intrinsic subprograms, for ASIS use.
4008 Rewrite (N, Unit (Parent (N)));
4009 Set_Unit (Parent (N), N);
4010 end if;
4012 if Parent_Installed then
4013 Remove_Parent;
4014 end if;
4016 Restore_Env;
4017 Env_Installed := False;
4018 Generic_Renamings.Set_Last (0);
4019 Generic_Renamings_HTable.Reset;
4020 end if;
4022 exception
4023 when Instantiation_Error =>
4024 if Parent_Installed then
4025 Remove_Parent;
4026 end if;
4028 if Env_Installed then
4029 Restore_Env;
4030 end if;
4031 end Analyze_Subprogram_Instantiation;
4033 -------------------------
4034 -- Get_Associated_Node --
4035 -------------------------
4037 function Get_Associated_Node (N : Node_Id) return Node_Id is
4038 Assoc : Node_Id := Associated_Node (N);
4040 begin
4041 if Nkind (Assoc) /= Nkind (N) then
4042 return Assoc;
4044 elsif Nkind (Assoc) = N_Aggregate
4045 or else Nkind (Assoc) = N_Extension_Aggregate
4046 then
4047 return Assoc;
4049 else
4050 -- If the node is part of an inner generic, it may itself have been
4051 -- remapped into a further generic copy. Associated_Node is otherwise
4052 -- used for the entity of the node, and will be of a different node
4053 -- kind, or else N has been rewritten as a literal or function call.
4055 while Present (Associated_Node (Assoc))
4056 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
4057 loop
4058 Assoc := Associated_Node (Assoc);
4059 end loop;
4061 -- Follow and additional link in case the final node was rewritten.
4062 -- This can only happen with nested generic units.
4064 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
4065 and then Present (Associated_Node (Assoc))
4066 and then (Nkind (Associated_Node (Assoc)) = N_Function_Call
4067 or else
4068 Nkind (Associated_Node (Assoc)) = N_Explicit_Dereference
4069 or else
4070 Nkind (Associated_Node (Assoc)) = N_Integer_Literal
4071 or else
4072 Nkind (Associated_Node (Assoc)) = N_Real_Literal
4073 or else
4074 Nkind (Associated_Node (Assoc)) = N_String_Literal)
4075 then
4076 Assoc := Associated_Node (Assoc);
4077 end if;
4079 return Assoc;
4080 end if;
4081 end Get_Associated_Node;
4083 -------------------------------------------
4084 -- Build_Instance_Compilation_Unit_Nodes --
4085 -------------------------------------------
4087 procedure Build_Instance_Compilation_Unit_Nodes
4088 (N : Node_Id;
4089 Act_Body : Node_Id;
4090 Act_Decl : Node_Id)
4092 Decl_Cunit : Node_Id;
4093 Body_Cunit : Node_Id;
4094 Citem : Node_Id;
4095 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
4096 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
4098 begin
4099 -- A new compilation unit node is built for the instance declaration
4101 Decl_Cunit :=
4102 Make_Compilation_Unit (Sloc (N),
4103 Context_Items => Empty_List,
4104 Unit => Act_Decl,
4105 Aux_Decls_Node =>
4106 Make_Compilation_Unit_Aux (Sloc (N)));
4108 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4109 Set_Body_Required (Decl_Cunit, True);
4111 -- We use the original instantiation compilation unit as the resulting
4112 -- compilation unit of the instance, since this is the main unit.
4114 Rewrite (N, Act_Body);
4115 Body_Cunit := Parent (N);
4117 -- The two compilation unit nodes are linked by the Library_Unit field
4119 Set_Library_Unit (Decl_Cunit, Body_Cunit);
4120 Set_Library_Unit (Body_Cunit, Decl_Cunit);
4122 -- Preserve the private nature of the package if needed
4124 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
4126 -- If the instance is not the main unit, its context, categorization,
4127 -- and elaboration entity are not relevant to the compilation.
4129 if Parent (N) /= Cunit (Main_Unit) then
4130 return;
4131 end if;
4133 -- The context clause items on the instantiation, which are now
4134 -- attached to the body compilation unit (since the body overwrote
4135 -- the original instantiation node), semantically belong on the spec,
4136 -- so copy them there. It's harmless to leave them on the body as well.
4137 -- In fact one could argue that they belong in both places.
4139 Citem := First (Context_Items (Body_Cunit));
4140 while Present (Citem) loop
4141 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
4142 Next (Citem);
4143 end loop;
4145 -- Propagate categorization flags on packages, so that they appear
4146 -- in ali file for the spec of the unit.
4148 if Ekind (New_Main) = E_Package then
4149 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
4150 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
4151 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
4152 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
4153 Set_Is_Remote_Call_Interface
4154 (Old_Main, Is_Remote_Call_Interface (New_Main));
4155 end if;
4157 -- Make entry in Units table, so that binder can generate call to
4158 -- elaboration procedure for body, if any.
4160 Make_Instance_Unit (Body_Cunit);
4161 Main_Unit_Entity := New_Main;
4162 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
4164 -- Build elaboration entity, since the instance may certainly
4165 -- generate elaboration code requiring a flag for protection.
4167 Build_Elaboration_Entity (Decl_Cunit, New_Main);
4168 end Build_Instance_Compilation_Unit_Nodes;
4170 -----------------------------
4171 -- Check_Access_Definition --
4172 -----------------------------
4174 procedure Check_Access_Definition (N : Node_Id) is
4175 begin
4176 pragma Assert
4177 (Ada_Version >= Ada_05
4178 and then Present (Access_Definition (N)));
4179 null;
4180 end Check_Access_Definition;
4182 -----------------------------------
4183 -- Check_Formal_Package_Instance --
4184 -----------------------------------
4186 -- If the formal has specific parameters, they must match those of the
4187 -- actual. Both of them are instances, and the renaming declarations
4188 -- for their formal parameters appear in the same order in both. The
4189 -- analyzed formal has been analyzed in the context of the current
4190 -- instance.
4192 procedure Check_Formal_Package_Instance
4193 (Formal_Pack : Entity_Id;
4194 Actual_Pack : Entity_Id)
4196 E1 : Entity_Id := First_Entity (Actual_Pack);
4197 E2 : Entity_Id := First_Entity (Formal_Pack);
4199 Expr1 : Node_Id;
4200 Expr2 : Node_Id;
4202 procedure Check_Mismatch (B : Boolean);
4203 -- Common error routine for mismatch between the parameters of
4204 -- the actual instance and those of the formal package.
4206 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
4207 -- The formal may come from a nested formal package, and the actual
4208 -- may have been constant-folded. To determine whether the two denote
4209 -- the same entity we may have to traverse several definitions to
4210 -- recover the ultimate entity that they refer to.
4212 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
4213 -- Similarly, if the formal comes from a nested formal package, the
4214 -- actual may designate the formal through multiple renamings, which
4215 -- have to be followed to determine the original variable in question.
4217 --------------------
4218 -- Check_Mismatch --
4219 --------------------
4221 procedure Check_Mismatch (B : Boolean) is
4222 Kind : constant Node_Kind := Nkind (Parent (E2));
4224 begin
4225 if Kind = N_Formal_Type_Declaration then
4226 return;
4228 elsif Kind = N_Formal_Object_Declaration
4229 or else Kind in N_Formal_Subprogram_Declaration
4230 or else Kind = N_Formal_Package_Declaration
4231 then
4232 null;
4234 elsif B then
4235 Error_Msg_NE
4236 ("actual for & in actual instance does not match formal",
4237 Parent (Actual_Pack), E1);
4238 end if;
4239 end Check_Mismatch;
4241 --------------------------------
4242 -- Same_Instantiated_Constant --
4243 --------------------------------
4245 function Same_Instantiated_Constant
4246 (E1, E2 : Entity_Id) return Boolean
4248 Ent : Entity_Id;
4250 begin
4251 Ent := E2;
4252 while Present (Ent) loop
4253 if E1 = Ent then
4254 return True;
4256 elsif Ekind (Ent) /= E_Constant then
4257 return False;
4259 elsif Is_Entity_Name (Constant_Value (Ent)) then
4260 if Entity (Constant_Value (Ent)) = E1 then
4261 return True;
4262 else
4263 Ent := Entity (Constant_Value (Ent));
4264 end if;
4266 -- The actual may be a constant that has been folded. Recover
4267 -- original name.
4269 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
4270 Ent := Entity (Original_Node (Constant_Value (Ent)));
4271 else
4272 return False;
4273 end if;
4274 end loop;
4276 return False;
4277 end Same_Instantiated_Constant;
4279 --------------------------------
4280 -- Same_Instantiated_Variable --
4281 --------------------------------
4283 function Same_Instantiated_Variable
4284 (E1, E2 : Entity_Id) return Boolean
4286 function Original_Entity (E : Entity_Id) return Entity_Id;
4287 -- Follow chain of renamings to the ultimate ancestor
4289 ---------------------
4290 -- Original_Entity --
4291 ---------------------
4293 function Original_Entity (E : Entity_Id) return Entity_Id is
4294 Orig : Entity_Id;
4296 begin
4297 Orig := E;
4298 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
4299 and then Present (Renamed_Object (Orig))
4300 and then Is_Entity_Name (Renamed_Object (Orig))
4301 loop
4302 Orig := Entity (Renamed_Object (Orig));
4303 end loop;
4305 return Orig;
4306 end Original_Entity;
4308 -- Start of processing for Same_Instantiated_Variable
4310 begin
4311 return Ekind (E1) = Ekind (E2)
4312 and then Original_Entity (E1) = Original_Entity (E2);
4313 end Same_Instantiated_Variable;
4315 -- Start of processing for Check_Formal_Package_Instance
4317 begin
4318 while Present (E1)
4319 and then Present (E2)
4320 loop
4321 exit when Ekind (E1) = E_Package
4322 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
4324 if Is_Type (E1) then
4326 -- Subtypes must statically match. E1 and E2 are the
4327 -- local entities that are subtypes of the actuals.
4328 -- Itypes generated for other parameters need not be checked,
4329 -- the check will be performed on the parameters themselves.
4331 -- If E2 is a formal type declaration, it is a defaulted
4332 -- parameter and needs no checking.
4334 if not Is_Itype (E1)
4335 and then not Is_Itype (E2)
4336 then
4337 Check_Mismatch
4338 (not Is_Type (E2)
4339 or else Etype (E1) /= Etype (E2)
4340 or else not Subtypes_Statically_Match (E1, E2));
4341 end if;
4343 elsif Ekind (E1) = E_Constant then
4345 -- IN parameters must denote the same static value, or
4346 -- the same constant, or the literal null.
4348 Expr1 := Expression (Parent (E1));
4350 if Ekind (E2) /= E_Constant then
4351 Check_Mismatch (True);
4352 goto Next_E;
4353 else
4354 Expr2 := Expression (Parent (E2));
4355 end if;
4357 if Is_Static_Expression (Expr1) then
4359 if not Is_Static_Expression (Expr2) then
4360 Check_Mismatch (True);
4362 elsif Is_Integer_Type (Etype (E1)) then
4364 declare
4365 V1 : constant Uint := Expr_Value (Expr1);
4366 V2 : constant Uint := Expr_Value (Expr2);
4367 begin
4368 Check_Mismatch (V1 /= V2);
4369 end;
4371 elsif Is_Real_Type (Etype (E1)) then
4372 declare
4373 V1 : constant Ureal := Expr_Value_R (Expr1);
4374 V2 : constant Ureal := Expr_Value_R (Expr2);
4375 begin
4376 Check_Mismatch (V1 /= V2);
4377 end;
4379 elsif Is_String_Type (Etype (E1))
4380 and then Nkind (Expr1) = N_String_Literal
4381 then
4383 if Nkind (Expr2) /= N_String_Literal then
4384 Check_Mismatch (True);
4385 else
4386 Check_Mismatch
4387 (not String_Equal (Strval (Expr1), Strval (Expr2)));
4388 end if;
4389 end if;
4391 elsif Is_Entity_Name (Expr1) then
4392 if Is_Entity_Name (Expr2) then
4393 if Entity (Expr1) = Entity (Expr2) then
4394 null;
4395 else
4396 Check_Mismatch
4397 (not Same_Instantiated_Constant
4398 (Entity (Expr1), Entity (Expr2)));
4399 end if;
4400 else
4401 Check_Mismatch (True);
4402 end if;
4404 elsif Is_Entity_Name (Original_Node (Expr1))
4405 and then Is_Entity_Name (Expr2)
4406 and then
4407 Same_Instantiated_Constant
4408 (Entity (Original_Node (Expr1)), Entity (Expr2))
4409 then
4410 null;
4412 elsif Nkind (Expr1) = N_Null then
4413 Check_Mismatch (Nkind (Expr1) /= N_Null);
4415 else
4416 Check_Mismatch (True);
4417 end if;
4419 elsif Ekind (E1) = E_Variable then
4420 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
4422 elsif Ekind (E1) = E_Package then
4423 Check_Mismatch
4424 (Ekind (E1) /= Ekind (E2)
4425 or else Renamed_Object (E1) /= Renamed_Object (E2));
4427 elsif Is_Overloadable (E1) then
4429 -- Verify that the names of the entities match.
4430 -- Note that actuals that are attributes are rewritten
4431 -- as subprograms.
4433 Check_Mismatch
4434 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
4436 else
4437 raise Program_Error;
4438 end if;
4440 <<Next_E>>
4441 Next_Entity (E1);
4442 Next_Entity (E2);
4443 end loop;
4444 end Check_Formal_Package_Instance;
4446 ---------------------------
4447 -- Check_Formal_Packages --
4448 ---------------------------
4450 procedure Check_Formal_Packages (P_Id : Entity_Id) is
4451 E : Entity_Id;
4452 Formal_P : Entity_Id;
4454 begin
4455 -- Iterate through the declarations in the instance, looking for
4456 -- package renaming declarations that denote instances of formal
4457 -- packages. Stop when we find the renaming of the current package
4458 -- itself. The declaration for a formal package without a box is
4459 -- followed by an internal entity that repeats the instantiation.
4461 E := First_Entity (P_Id);
4462 while Present (E) loop
4463 if Ekind (E) = E_Package then
4464 if Renamed_Object (E) = P_Id then
4465 exit;
4467 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
4468 null;
4470 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
4471 Formal_P := Next_Entity (E);
4472 Check_Formal_Package_Instance (Formal_P, E);
4474 -- After checking, remove the internal validating package. It
4475 -- is only needed for semantic checks, and as it may contain
4476 -- generic formal declarations it should not reach gigi.
4478 Remove (Unit_Declaration_Node (Formal_P));
4479 end if;
4480 end if;
4482 Next_Entity (E);
4483 end loop;
4484 end Check_Formal_Packages;
4486 ---------------------------------
4487 -- Check_Forward_Instantiation --
4488 ---------------------------------
4490 procedure Check_Forward_Instantiation (Decl : Node_Id) is
4491 S : Entity_Id;
4492 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
4494 begin
4495 -- The instantiation appears before the generic body if we are in the
4496 -- scope of the unit containing the generic, either in its spec or in
4497 -- the package body. and before the generic body.
4499 if Ekind (Gen_Comp) = E_Package_Body then
4500 Gen_Comp := Spec_Entity (Gen_Comp);
4501 end if;
4503 if In_Open_Scopes (Gen_Comp)
4504 and then No (Corresponding_Body (Decl))
4505 then
4506 S := Current_Scope;
4508 while Present (S)
4509 and then not Is_Compilation_Unit (S)
4510 and then not Is_Child_Unit (S)
4511 loop
4512 if Ekind (S) = E_Package then
4513 Set_Has_Forward_Instantiation (S);
4514 end if;
4516 S := Scope (S);
4517 end loop;
4518 end if;
4519 end Check_Forward_Instantiation;
4521 ---------------------------
4522 -- Check_Generic_Actuals --
4523 ---------------------------
4525 -- The visibility of the actuals may be different between the
4526 -- point of generic instantiation and the instantiation of the body.
4528 procedure Check_Generic_Actuals
4529 (Instance : Entity_Id;
4530 Is_Formal_Box : Boolean)
4532 E : Entity_Id;
4533 Astype : Entity_Id;
4535 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
4536 -- For a formal that is an array type, the component type is often
4537 -- a previous formal in the same unit. The privacy status of the
4538 -- component type will have been examined earlier in the traversal
4539 -- of the corresponding actuals, and this status should not be
4540 -- modified for the array type itself.
4541 -- To detect this case we have to rescan the list of formals, which
4542 -- is usually short enough to ignore the resulting inefficiency.
4544 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
4545 Prev : Entity_Id;
4546 begin
4547 Prev := First_Entity (Instance);
4548 while Present (Prev) loop
4549 if Is_Type (Prev)
4550 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
4551 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
4552 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
4553 then
4554 return True;
4555 elsif Prev = E then
4556 return False;
4557 else
4558 Next_Entity (Prev);
4559 end if;
4560 end loop;
4561 return False;
4562 end Denotes_Previous_Actual;
4564 -- Start of processing for Check_Generic_Actuals
4566 begin
4567 E := First_Entity (Instance);
4568 while Present (E) loop
4569 if Is_Type (E)
4570 and then Nkind (Parent (E)) = N_Subtype_Declaration
4571 and then Scope (Etype (E)) /= Instance
4572 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
4573 then
4574 if Is_Array_Type (E)
4575 and then Denotes_Previous_Actual (Component_Type (E))
4576 then
4577 null;
4578 else
4579 Check_Private_View (Subtype_Indication (Parent (E)));
4580 end if;
4581 Set_Is_Generic_Actual_Type (E, True);
4582 Set_Is_Hidden (E, False);
4583 Set_Is_Potentially_Use_Visible (E,
4584 In_Use (Instance));
4586 -- We constructed the generic actual type as a subtype of
4587 -- the supplied type. This means that it normally would not
4588 -- inherit subtype specific attributes of the actual, which
4589 -- is wrong for the generic case.
4591 Astype := Ancestor_Subtype (E);
4593 if No (Astype) then
4595 -- can happen when E is an itype that is the full view of
4596 -- a private type completed, e.g. with a constrained array.
4598 Astype := Base_Type (E);
4599 end if;
4601 Set_Size_Info (E, (Astype));
4602 Set_RM_Size (E, RM_Size (Astype));
4603 Set_First_Rep_Item (E, First_Rep_Item (Astype));
4605 if Is_Discrete_Or_Fixed_Point_Type (E) then
4606 Set_RM_Size (E, RM_Size (Astype));
4608 -- In nested instances, the base type of an access actual
4609 -- may itself be private, and need to be exchanged.
4611 elsif Is_Access_Type (E)
4612 and then Is_Private_Type (Etype (E))
4613 then
4614 Check_Private_View
4615 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
4616 end if;
4618 elsif Ekind (E) = E_Package then
4620 -- If this is the renaming for the current instance, we're done.
4621 -- Otherwise it is a formal package. If the corresponding formal
4622 -- was declared with a box, the (instantiations of the) generic
4623 -- formal part are also visible. Otherwise, ignore the entity
4624 -- created to validate the actuals.
4626 if Renamed_Object (E) = Instance then
4627 exit;
4629 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
4630 null;
4632 -- The visibility of a formal of an enclosing generic is already
4633 -- correct.
4635 elsif Denotes_Formal_Package (E) then
4636 null;
4638 elsif Present (Associated_Formal_Package (E))
4639 and then not Is_Generic_Formal (E)
4640 then
4641 if Box_Present (Parent (Associated_Formal_Package (E))) then
4642 Check_Generic_Actuals (Renamed_Object (E), True);
4644 else
4645 Check_Generic_Actuals (Renamed_Object (E), False);
4646 end if;
4648 Set_Is_Hidden (E, False);
4649 end if;
4651 -- If this is a subprogram instance (in a wrapper package) the
4652 -- actual is fully visible.
4654 elsif Is_Wrapper_Package (Instance) then
4655 Set_Is_Hidden (E, False);
4657 -- If the formal package is declared with a box, or if the formal
4658 -- parameter is defaulted, it is visible in the body.
4660 elsif Is_Formal_Box
4661 or else Is_Visible_Formal (E)
4662 then
4663 Set_Is_Hidden (E, False);
4664 end if;
4666 Next_Entity (E);
4667 end loop;
4668 end Check_Generic_Actuals;
4670 ------------------------------
4671 -- Check_Generic_Child_Unit --
4672 ------------------------------
4674 procedure Check_Generic_Child_Unit
4675 (Gen_Id : Node_Id;
4676 Parent_Installed : in out Boolean)
4678 Loc : constant Source_Ptr := Sloc (Gen_Id);
4679 Gen_Par : Entity_Id := Empty;
4680 Inst_Par : Entity_Id;
4681 E : Entity_Id;
4682 S : Node_Id;
4684 function Find_Generic_Child
4685 (Scop : Entity_Id;
4686 Id : Node_Id) return Entity_Id;
4687 -- Search generic parent for possible child unit with the given name
4689 function In_Enclosing_Instance return Boolean;
4690 -- Within an instance of the parent, the child unit may be denoted
4691 -- by a simple name, or an abbreviated expanded name. Examine enclosing
4692 -- scopes to locate a possible parent instantiation.
4694 ------------------------
4695 -- Find_Generic_Child --
4696 ------------------------
4698 function Find_Generic_Child
4699 (Scop : Entity_Id;
4700 Id : Node_Id) return Entity_Id
4702 E : Entity_Id;
4704 begin
4705 -- If entity of name is already set, instance has already been
4706 -- resolved, e.g. in an enclosing instantiation.
4708 if Present (Entity (Id)) then
4709 if Scope (Entity (Id)) = Scop then
4710 return Entity (Id);
4711 else
4712 return Empty;
4713 end if;
4715 else
4716 E := First_Entity (Scop);
4717 while Present (E) loop
4718 if Chars (E) = Chars (Id)
4719 and then Is_Child_Unit (E)
4720 then
4721 if Is_Child_Unit (E)
4722 and then not Is_Visible_Child_Unit (E)
4723 then
4724 Error_Msg_NE
4725 ("generic child unit& is not visible", Gen_Id, E);
4726 end if;
4728 Set_Entity (Id, E);
4729 return E;
4730 end if;
4732 Next_Entity (E);
4733 end loop;
4735 return Empty;
4736 end if;
4737 end Find_Generic_Child;
4739 ---------------------------
4740 -- In_Enclosing_Instance --
4741 ---------------------------
4743 function In_Enclosing_Instance return Boolean is
4744 Enclosing_Instance : Node_Id;
4745 Instance_Decl : Node_Id;
4747 begin
4748 -- We do not inline any call that contains instantiations, except
4749 -- for instantiations of Unchecked_Conversion, so if we are within
4750 -- an inlined body the current instance does not require parents.
4752 if In_Inlined_Body then
4753 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
4754 return False;
4755 end if;
4757 -- Loop to check enclosing scopes
4759 Enclosing_Instance := Current_Scope;
4760 while Present (Enclosing_Instance) loop
4761 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
4763 if Ekind (Enclosing_Instance) = E_Package
4764 and then Is_Generic_Instance (Enclosing_Instance)
4765 and then Present
4766 (Generic_Parent (Specification (Instance_Decl)))
4767 then
4768 -- Check whether the generic we are looking for is a child
4769 -- of this instance.
4771 E := Find_Generic_Child
4772 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
4773 exit when Present (E);
4775 else
4776 E := Empty;
4777 end if;
4779 Enclosing_Instance := Scope (Enclosing_Instance);
4780 end loop;
4782 if No (E) then
4784 -- Not a child unit
4786 Analyze (Gen_Id);
4787 return False;
4789 else
4790 Rewrite (Gen_Id,
4791 Make_Expanded_Name (Loc,
4792 Chars => Chars (E),
4793 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
4794 Selector_Name => New_Occurrence_Of (E, Loc)));
4796 Set_Entity (Gen_Id, E);
4797 Set_Etype (Gen_Id, Etype (E));
4798 Parent_Installed := False; -- Already in scope.
4799 return True;
4800 end if;
4801 end In_Enclosing_Instance;
4803 -- Start of processing for Check_Generic_Child_Unit
4805 begin
4806 -- If the name of the generic is given by a selected component, it
4807 -- may be the name of a generic child unit, and the prefix is the name
4808 -- of an instance of the parent, in which case the child unit must be
4809 -- visible. If this instance is not in scope, it must be placed there
4810 -- and removed after instantiation, because what is being instantiated
4811 -- is not the original child, but the corresponding child present in
4812 -- the instance of the parent.
4814 -- If the child is instantiated within the parent, it can be given by
4815 -- a simple name. In this case the instance is already in scope, but
4816 -- the child generic must be recovered from the generic parent as well.
4818 if Nkind (Gen_Id) = N_Selected_Component then
4819 S := Selector_Name (Gen_Id);
4820 Analyze (Prefix (Gen_Id));
4821 Inst_Par := Entity (Prefix (Gen_Id));
4823 if Ekind (Inst_Par) = E_Package
4824 and then Present (Renamed_Object (Inst_Par))
4825 then
4826 Inst_Par := Renamed_Object (Inst_Par);
4827 end if;
4829 if Ekind (Inst_Par) = E_Package then
4830 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
4831 Gen_Par := Generic_Parent (Parent (Inst_Par));
4833 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
4834 and then
4835 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
4836 then
4837 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
4838 end if;
4840 elsif Ekind (Inst_Par) = E_Generic_Package
4841 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
4842 then
4843 -- A formal package may be a real child package, and not the
4844 -- implicit instance within a parent. In this case the child is
4845 -- not visible and has to be retrieved explicitly as well.
4847 Gen_Par := Inst_Par;
4848 end if;
4850 if Present (Gen_Par) then
4852 -- The prefix denotes an instantiation. The entity itself
4853 -- may be a nested generic, or a child unit.
4855 E := Find_Generic_Child (Gen_Par, S);
4857 if Present (E) then
4858 Change_Selected_Component_To_Expanded_Name (Gen_Id);
4859 Set_Entity (Gen_Id, E);
4860 Set_Etype (Gen_Id, Etype (E));
4861 Set_Entity (S, E);
4862 Set_Etype (S, Etype (E));
4864 -- Indicate that this is a reference to the parent
4866 if In_Extended_Main_Source_Unit (Gen_Id) then
4867 Set_Is_Instantiated (Inst_Par);
4868 end if;
4870 -- A common mistake is to replicate the naming scheme of
4871 -- a hierarchy by instantiating a generic child directly,
4872 -- rather than the implicit child in a parent instance:
4874 -- generic .. package Gpar is ..
4875 -- generic .. package Gpar.Child is ..
4876 -- package Par is new Gpar ();
4878 -- with Gpar.Child;
4879 -- package Par.Child is new Gpar.Child ();
4880 -- rather than Par.Child
4882 -- In this case the instantiation is within Par, which is
4883 -- an instance, but Gpar does not denote Par because we are
4884 -- not IN the instance of Gpar, so this is illegal. The test
4885 -- below recognizes this particular case.
4887 if Is_Child_Unit (E)
4888 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
4889 and then (not In_Instance
4890 or else Nkind (Parent (Parent (Gen_Id))) =
4891 N_Compilation_Unit)
4892 then
4893 Error_Msg_N
4894 ("prefix of generic child unit must be instance of parent",
4895 Gen_Id);
4896 end if;
4898 if not In_Open_Scopes (Inst_Par)
4899 and then Nkind (Parent (Gen_Id)) not in
4900 N_Generic_Renaming_Declaration
4901 then
4902 Install_Parent (Inst_Par);
4903 Parent_Installed := True;
4904 end if;
4906 else
4907 -- If the generic parent does not contain an entity that
4908 -- corresponds to the selector, the instance doesn't either.
4909 -- Analyzing the node will yield the appropriate error message.
4910 -- If the entity is not a child unit, then it is an inner
4911 -- generic in the parent.
4913 Analyze (Gen_Id);
4914 end if;
4916 else
4917 Analyze (Gen_Id);
4919 if Is_Child_Unit (Entity (Gen_Id))
4920 and then
4921 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
4922 and then not In_Open_Scopes (Inst_Par)
4923 then
4924 Install_Parent (Inst_Par);
4925 Parent_Installed := True;
4926 end if;
4927 end if;
4929 elsif Nkind (Gen_Id) = N_Expanded_Name then
4931 -- Entity already present, analyze prefix, whose meaning may be
4932 -- an instance in the current context. If it is an instance of
4933 -- a relative within another, the proper parent may still have
4934 -- to be installed, if they are not of the same generation.
4936 Analyze (Prefix (Gen_Id));
4937 Inst_Par := Entity (Prefix (Gen_Id));
4939 if In_Enclosing_Instance then
4940 null;
4942 elsif Present (Entity (Gen_Id))
4943 and then Is_Child_Unit (Entity (Gen_Id))
4944 and then not In_Open_Scopes (Inst_Par)
4945 then
4946 Install_Parent (Inst_Par);
4947 Parent_Installed := True;
4948 end if;
4950 elsif In_Enclosing_Instance then
4952 -- The child unit is found in some enclosing scope
4954 null;
4956 else
4957 Analyze (Gen_Id);
4959 -- If this is the renaming of the implicit child in a parent
4960 -- instance, recover the parent name and install it.
4962 if Is_Entity_Name (Gen_Id) then
4963 E := Entity (Gen_Id);
4965 if Is_Generic_Unit (E)
4966 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
4967 and then Is_Child_Unit (Renamed_Object (E))
4968 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
4969 and then Nkind (Name (Parent (E))) = N_Expanded_Name
4970 then
4971 Rewrite (Gen_Id,
4972 New_Copy_Tree (Name (Parent (E))));
4973 Inst_Par := Entity (Prefix (Gen_Id));
4975 if not In_Open_Scopes (Inst_Par) then
4976 Install_Parent (Inst_Par);
4977 Parent_Installed := True;
4978 end if;
4980 -- If it is a child unit of a non-generic parent, it may be
4981 -- use-visible and given by a direct name. Install parent as
4982 -- for other cases.
4984 elsif Is_Generic_Unit (E)
4985 and then Is_Child_Unit (E)
4986 and then
4987 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
4988 and then not Is_Generic_Unit (Scope (E))
4989 then
4990 if not In_Open_Scopes (Scope (E)) then
4991 Install_Parent (Scope (E));
4992 Parent_Installed := True;
4993 end if;
4994 end if;
4995 end if;
4996 end if;
4997 end Check_Generic_Child_Unit;
4999 -----------------------------
5000 -- Check_Hidden_Child_Unit --
5001 -----------------------------
5003 procedure Check_Hidden_Child_Unit
5004 (N : Node_Id;
5005 Gen_Unit : Entity_Id;
5006 Act_Decl_Id : Entity_Id)
5008 Gen_Id : constant Node_Id := Name (N);
5010 begin
5011 if Is_Child_Unit (Gen_Unit)
5012 and then Is_Child_Unit (Act_Decl_Id)
5013 and then Nkind (Gen_Id) = N_Expanded_Name
5014 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
5015 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
5016 then
5017 Error_Msg_Node_2 := Scope (Act_Decl_Id);
5018 Error_Msg_NE
5019 ("generic unit & is implicitly declared in &",
5020 Defining_Unit_Name (N), Gen_Unit);
5021 Error_Msg_N ("\instance must have different name",
5022 Defining_Unit_Name (N));
5023 end if;
5024 end Check_Hidden_Child_Unit;
5026 ------------------------
5027 -- Check_Private_View --
5028 ------------------------
5030 procedure Check_Private_View (N : Node_Id) is
5031 T : constant Entity_Id := Etype (N);
5032 BT : Entity_Id;
5034 begin
5035 -- Exchange views if the type was not private in the generic but is
5036 -- private at the point of instantiation. Do not exchange views if
5037 -- the scope of the type is in scope. This can happen if both generic
5038 -- and instance are sibling units, or if type is defined in a parent.
5039 -- In this case the visibility of the type will be correct for all
5040 -- semantic checks.
5042 if Present (T) then
5043 BT := Base_Type (T);
5045 if Is_Private_Type (T)
5046 and then not Has_Private_View (N)
5047 and then Present (Full_View (T))
5048 and then not In_Open_Scopes (Scope (T))
5049 then
5050 -- In the generic, the full type was visible. Save the
5051 -- private entity, for subsequent exchange.
5053 Switch_View (T);
5055 elsif Has_Private_View (N)
5056 and then not Is_Private_Type (T)
5057 and then not Has_Been_Exchanged (T)
5058 and then Etype (Get_Associated_Node (N)) /= T
5059 then
5060 -- Only the private declaration was visible in the generic. If
5061 -- the type appears in a subtype declaration, the subtype in the
5062 -- instance must have a view compatible with that of its parent,
5063 -- which must be exchanged (see corresponding code in Restore_
5064 -- Private_Views). Otherwise, if the type is defined in a parent
5065 -- unit, leave full visibility within instance, which is safe.
5067 if In_Open_Scopes (Scope (Base_Type (T)))
5068 and then not Is_Private_Type (Base_Type (T))
5069 and then Comes_From_Source (Base_Type (T))
5070 then
5071 null;
5073 elsif Nkind (Parent (N)) = N_Subtype_Declaration
5074 or else not In_Private_Part (Scope (Base_Type (T)))
5075 then
5076 Prepend_Elmt (T, Exchanged_Views);
5077 Exchange_Declarations (Etype (Get_Associated_Node (N)));
5078 end if;
5080 -- For composite types with inconsistent representation
5081 -- exchange component types accordingly.
5083 elsif Is_Access_Type (T)
5084 and then Is_Private_Type (Designated_Type (T))
5085 and then not Has_Private_View (N)
5086 and then Present (Full_View (Designated_Type (T)))
5087 then
5088 Switch_View (Designated_Type (T));
5090 elsif Is_Array_Type (T)
5091 and then Is_Private_Type (Component_Type (T))
5092 and then not Has_Private_View (N)
5093 and then Present (Full_View (Component_Type (T)))
5094 then
5095 Switch_View (Component_Type (T));
5097 elsif Is_Private_Type (T)
5098 and then Present (Full_View (T))
5099 and then Is_Array_Type (Full_View (T))
5100 and then Is_Private_Type (Component_Type (Full_View (T)))
5101 then
5102 Switch_View (T);
5104 -- Finally, a non-private subtype may have a private base type, which
5105 -- must be exchanged for consistency. This can happen when
5106 -- instantiating a package body, when the scope stack is empty but in
5107 -- fact the subtype and the base type are declared in an enclosing
5108 -- scope.
5110 -- Note that in this case we introduce an inconsistency in the view
5111 -- set, because we switch the base type BT, but there could be some
5112 -- private dependent subtypes of BT which remain unswitched. Such
5113 -- subtypes might need to be switched at a later point (see specific
5114 -- provision for that case in Switch_View).
5116 elsif not Is_Private_Type (T)
5117 and then not Has_Private_View (N)
5118 and then Is_Private_Type (BT)
5119 and then Present (Full_View (BT))
5120 and then not Is_Generic_Type (BT)
5121 and then not In_Open_Scopes (BT)
5122 then
5123 Prepend_Elmt (Full_View (BT), Exchanged_Views);
5124 Exchange_Declarations (BT);
5125 end if;
5126 end if;
5127 end Check_Private_View;
5129 --------------------------
5130 -- Contains_Instance_Of --
5131 --------------------------
5133 function Contains_Instance_Of
5134 (Inner : Entity_Id;
5135 Outer : Entity_Id;
5136 N : Node_Id) return Boolean
5138 Elmt : Elmt_Id;
5139 Scop : Entity_Id;
5141 begin
5142 Scop := Outer;
5144 -- Verify that there are no circular instantiations. We check whether
5145 -- the unit contains an instance of the current scope or some enclosing
5146 -- scope (in case one of the instances appears in a subunit). Longer
5147 -- circularities involving subunits might seem too pathological to
5148 -- consider, but they were not too pathological for the authors of
5149 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
5150 -- enclosing generic scopes as containing an instance.
5152 loop
5153 -- Within a generic subprogram body, the scope is not generic, to
5154 -- allow for recursive subprograms. Use the declaration to determine
5155 -- whether this is a generic unit.
5157 if Ekind (Scop) = E_Generic_Package
5158 or else (Is_Subprogram (Scop)
5159 and then Nkind (Unit_Declaration_Node (Scop)) =
5160 N_Generic_Subprogram_Declaration)
5161 then
5162 Elmt := First_Elmt (Inner_Instances (Inner));
5164 while Present (Elmt) loop
5165 if Node (Elmt) = Scop then
5166 Error_Msg_Node_2 := Inner;
5167 Error_Msg_NE
5168 ("circular Instantiation: & instantiated within &!",
5169 N, Scop);
5170 return True;
5172 elsif Node (Elmt) = Inner then
5173 return True;
5175 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
5176 Error_Msg_Node_2 := Inner;
5177 Error_Msg_NE
5178 ("circular Instantiation: & instantiated within &!",
5179 N, Node (Elmt));
5180 return True;
5181 end if;
5183 Next_Elmt (Elmt);
5184 end loop;
5186 -- Indicate that Inner is being instantiated within Scop
5188 Append_Elmt (Inner, Inner_Instances (Scop));
5189 end if;
5191 if Scop = Standard_Standard then
5192 exit;
5193 else
5194 Scop := Scope (Scop);
5195 end if;
5196 end loop;
5198 return False;
5199 end Contains_Instance_Of;
5201 -----------------------
5202 -- Copy_Generic_Node --
5203 -----------------------
5205 function Copy_Generic_Node
5206 (N : Node_Id;
5207 Parent_Id : Node_Id;
5208 Instantiating : Boolean) return Node_Id
5210 Ent : Entity_Id;
5211 New_N : Node_Id;
5213 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
5214 -- Check the given value of one of the Fields referenced by the
5215 -- current node to determine whether to copy it recursively. The
5216 -- field may hold a Node_Id, a List_Id, or an Elist_Id, or a plain
5217 -- value (Sloc, Uint, Char) in which case it need not be copied.
5219 procedure Copy_Descendants;
5220 -- Common utility for various nodes
5222 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
5223 -- Make copy of element list
5225 function Copy_Generic_List
5226 (L : List_Id;
5227 Parent_Id : Node_Id) return List_Id;
5228 -- Apply Copy_Node recursively to the members of a node list
5230 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
5231 -- True if an identifier is part of the defining program unit name
5232 -- of a child unit. The entity of such an identifier must be kept
5233 -- (for ASIS use) even though as the name of an enclosing generic
5234 -- it would otherwise not be preserved in the generic tree.
5236 ----------------------
5237 -- Copy_Descendants --
5238 ----------------------
5240 procedure Copy_Descendants is
5242 use Atree.Unchecked_Access;
5243 -- This code section is part of the implementation of an untyped
5244 -- tree traversal, so it needs direct access to node fields.
5246 begin
5247 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
5248 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
5249 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
5250 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
5251 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
5252 end Copy_Descendants;
5254 -----------------------------
5255 -- Copy_Generic_Descendant --
5256 -----------------------------
5258 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
5259 begin
5260 if D = Union_Id (Empty) then
5261 return D;
5263 elsif D in Node_Range then
5264 return Union_Id
5265 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
5267 elsif D in List_Range then
5268 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
5270 elsif D in Elist_Range then
5271 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
5273 -- Nothing else is copyable (e.g. Uint values), return as is
5275 else
5276 return D;
5277 end if;
5278 end Copy_Generic_Descendant;
5280 ------------------------
5281 -- Copy_Generic_Elist --
5282 ------------------------
5284 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
5285 M : Elmt_Id;
5286 L : Elist_Id;
5288 begin
5289 if Present (E) then
5290 L := New_Elmt_List;
5291 M := First_Elmt (E);
5292 while Present (M) loop
5293 Append_Elmt
5294 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
5295 Next_Elmt (M);
5296 end loop;
5298 return L;
5300 else
5301 return No_Elist;
5302 end if;
5303 end Copy_Generic_Elist;
5305 -----------------------
5306 -- Copy_Generic_List --
5307 -----------------------
5309 function Copy_Generic_List
5310 (L : List_Id;
5311 Parent_Id : Node_Id) return List_Id
5313 N : Node_Id;
5314 New_L : List_Id;
5316 begin
5317 if Present (L) then
5318 New_L := New_List;
5319 Set_Parent (New_L, Parent_Id);
5321 N := First (L);
5322 while Present (N) loop
5323 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
5324 Next (N);
5325 end loop;
5327 return New_L;
5329 else
5330 return No_List;
5331 end if;
5332 end Copy_Generic_List;
5334 ---------------------------
5335 -- In_Defining_Unit_Name --
5336 ---------------------------
5338 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
5339 begin
5340 return Present (Parent (Nam))
5341 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
5342 or else
5343 (Nkind (Parent (Nam)) = N_Expanded_Name
5344 and then In_Defining_Unit_Name (Parent (Nam))));
5345 end In_Defining_Unit_Name;
5347 -- Start of processing for Copy_Generic_Node
5349 begin
5350 if N = Empty then
5351 return N;
5352 end if;
5354 New_N := New_Copy (N);
5356 if Instantiating then
5357 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
5358 end if;
5360 if not Is_List_Member (N) then
5361 Set_Parent (New_N, Parent_Id);
5362 end if;
5364 -- If defining identifier, then all fields have been copied already
5366 if Nkind (New_N) in N_Entity then
5367 null;
5369 -- Special casing for identifiers and other entity names and operators
5371 elsif Nkind (New_N) = N_Identifier
5372 or else Nkind (New_N) = N_Character_Literal
5373 or else Nkind (New_N) = N_Expanded_Name
5374 or else Nkind (New_N) = N_Operator_Symbol
5375 or else Nkind (New_N) in N_Op
5376 then
5377 if not Instantiating then
5379 -- Link both nodes in order to assign subsequently the
5380 -- entity of the copy to the original node, in case this
5381 -- is a global reference.
5383 Set_Associated_Node (N, New_N);
5385 -- If we are within an instantiation, this is a nested generic
5386 -- that has already been analyzed at the point of definition. We
5387 -- must preserve references that were global to the enclosing
5388 -- parent at that point. Other occurrences, whether global or
5389 -- local to the current generic, must be resolved anew, so we
5390 -- reset the entity in the generic copy. A global reference has
5391 -- a smaller depth than the parent, or else the same depth in
5392 -- case both are distinct compilation units.
5394 -- It is also possible for Current_Instantiated_Parent to be
5395 -- defined, and for this not to be a nested generic, namely
5396 -- if the unit is loaded through Rtsfind. In that case, the
5397 -- entity of New_N is only a link to the associated node, and
5398 -- not a defining occurrence.
5400 -- The entities for parent units in the defining_program_unit
5401 -- of a generic child unit are established when the context of
5402 -- the unit is first analyzed, before the generic copy is made.
5403 -- They are preserved in the copy for use in ASIS queries.
5405 Ent := Entity (New_N);
5407 if No (Current_Instantiated_Parent.Gen_Id) then
5408 if No (Ent)
5409 or else Nkind (Ent) /= N_Defining_Identifier
5410 or else not In_Defining_Unit_Name (N)
5411 then
5412 Set_Associated_Node (New_N, Empty);
5413 end if;
5415 elsif No (Ent)
5416 or else
5417 not (Nkind (Ent) = N_Defining_Identifier
5418 or else
5419 Nkind (Ent) = N_Defining_Character_Literal
5420 or else
5421 Nkind (Ent) = N_Defining_Operator_Symbol)
5422 or else No (Scope (Ent))
5423 or else Scope (Ent) = Current_Instantiated_Parent.Gen_Id
5424 or else (Scope_Depth (Scope (Ent)) >
5425 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
5426 and then
5427 Get_Source_Unit (Ent) =
5428 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
5429 then
5430 Set_Associated_Node (New_N, Empty);
5431 end if;
5433 -- Case of instantiating identifier or some other name or operator
5435 else
5436 -- If the associated node is still defined, the entity in
5437 -- it is global, and must be copied to the instance.
5438 -- If this copy is being made for a body to inline, it is
5439 -- applied to an instantiated tree, and the entity is already
5440 -- present and must be also preserved.
5442 declare
5443 Assoc : constant Node_Id := Get_Associated_Node (N);
5444 begin
5445 if Present (Assoc) then
5446 if Nkind (Assoc) = Nkind (N) then
5447 Set_Entity (New_N, Entity (Assoc));
5448 Check_Private_View (N);
5450 elsif Nkind (Assoc) = N_Function_Call then
5451 Set_Entity (New_N, Entity (Name (Assoc)));
5453 elsif (Nkind (Assoc) = N_Defining_Identifier
5454 or else Nkind (Assoc) = N_Defining_Character_Literal
5455 or else Nkind (Assoc) = N_Defining_Operator_Symbol)
5456 and then Expander_Active
5457 then
5458 -- Inlining case: we are copying a tree that contains
5459 -- global entities, which are preserved in the copy
5460 -- to be used for subsequent inlining.
5462 null;
5464 else
5465 Set_Entity (New_N, Empty);
5466 end if;
5467 end if;
5468 end;
5469 end if;
5471 -- For expanded name, we must copy the Prefix and Selector_Name
5473 if Nkind (N) = N_Expanded_Name then
5474 Set_Prefix
5475 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
5477 Set_Selector_Name (New_N,
5478 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
5480 -- For operators, we must copy the right operand
5482 elsif Nkind (N) in N_Op then
5483 Set_Right_Opnd (New_N,
5484 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
5486 -- And for binary operators, the left operand as well
5488 if Nkind (N) in N_Binary_Op then
5489 Set_Left_Opnd (New_N,
5490 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
5491 end if;
5492 end if;
5494 -- Special casing for stubs
5496 elsif Nkind (N) in N_Body_Stub then
5498 -- In any case, we must copy the specification or defining
5499 -- identifier as appropriate.
5501 if Nkind (N) = N_Subprogram_Body_Stub then
5502 Set_Specification (New_N,
5503 Copy_Generic_Node (Specification (N), New_N, Instantiating));
5505 else
5506 Set_Defining_Identifier (New_N,
5507 Copy_Generic_Node
5508 (Defining_Identifier (N), New_N, Instantiating));
5509 end if;
5511 -- If we are not instantiating, then this is where we load and
5512 -- analyze subunits, i.e. at the point where the stub occurs. A
5513 -- more permissivle system might defer this analysis to the point
5514 -- of instantiation, but this seems to complicated for now.
5516 if not Instantiating then
5517 declare
5518 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
5519 Subunit : Node_Id;
5520 Unum : Unit_Number_Type;
5521 New_Body : Node_Id;
5523 begin
5524 Unum :=
5525 Load_Unit
5526 (Load_Name => Subunit_Name,
5527 Required => False,
5528 Subunit => True,
5529 Error_Node => N);
5531 -- If the proper body is not found, a warning message will
5532 -- be emitted when analyzing the stub, or later at the the
5533 -- point of instantiation. Here we just leave the stub as is.
5535 if Unum = No_Unit then
5536 Subunits_Missing := True;
5537 goto Subunit_Not_Found;
5538 end if;
5540 Subunit := Cunit (Unum);
5542 if Nkind (Unit (Subunit)) /= N_Subunit then
5543 Error_Msg_Sloc := Sloc (N);
5544 Error_Msg_N
5545 ("expected SEPARATE subunit to complete stub at#,"
5546 & " found child unit", Subunit);
5547 goto Subunit_Not_Found;
5548 end if;
5550 -- We must create a generic copy of the subunit, in order
5551 -- to perform semantic analysis on it, and we must replace
5552 -- the stub in the original generic unit with the subunit,
5553 -- in order to preserve non-local references within.
5555 -- Only the proper body needs to be copied. Library_Unit and
5556 -- context clause are simply inherited by the generic copy.
5557 -- Note that the copy (which may be recursive if there are
5558 -- nested subunits) must be done first, before attaching it
5559 -- to the enclosing generic.
5561 New_Body :=
5562 Copy_Generic_Node
5563 (Proper_Body (Unit (Subunit)),
5564 Empty, Instantiating => False);
5566 -- Now place the original proper body in the original
5567 -- generic unit. This is a body, not a compilation unit.
5569 Rewrite (N, Proper_Body (Unit (Subunit)));
5570 Set_Is_Compilation_Unit (Defining_Entity (N), False);
5571 Set_Was_Originally_Stub (N);
5573 -- Finally replace the body of the subunit with its copy,
5574 -- and make this new subunit into the library unit of the
5575 -- generic copy, which does not have stubs any longer.
5577 Set_Proper_Body (Unit (Subunit), New_Body);
5578 Set_Library_Unit (New_N, Subunit);
5579 Inherit_Context (Unit (Subunit), N);
5580 end;
5582 -- If we are instantiating, this must be an error case, since
5583 -- otherwise we would have replaced the stub node by the proper
5584 -- body that corresponds. So just ignore it in the copy (i.e.
5585 -- we have copied it, and that is good enough).
5587 else
5588 null;
5589 end if;
5591 <<Subunit_Not_Found>> null;
5593 -- If the node is a compilation unit, it is the subunit of a stub,
5594 -- which has been loaded already (see code below). In this case,
5595 -- the library unit field of N points to the parent unit (which
5596 -- is a compilation unit) and need not (and cannot!) be copied.
5598 -- When the proper body of the stub is analyzed, thie library_unit
5599 -- link is used to establish the proper context (see sem_ch10).
5601 -- The other fields of a compilation unit are copied as usual
5603 elsif Nkind (N) = N_Compilation_Unit then
5605 -- This code can only be executed when not instantiating, because
5606 -- in the copy made for an instantiation, the compilation unit
5607 -- node has disappeared at the point that a stub is replaced by
5608 -- its proper body.
5610 pragma Assert (not Instantiating);
5612 Set_Context_Items (New_N,
5613 Copy_Generic_List (Context_Items (N), New_N));
5615 Set_Unit (New_N,
5616 Copy_Generic_Node (Unit (N), New_N, False));
5618 Set_First_Inlined_Subprogram (New_N,
5619 Copy_Generic_Node
5620 (First_Inlined_Subprogram (N), New_N, False));
5622 Set_Aux_Decls_Node (New_N,
5623 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
5625 -- For an assignment node, the assignment is known to be semantically
5626 -- legal if we are instantiating the template. This avoids incorrect
5627 -- diagnostics in generated code.
5629 elsif Nkind (N) = N_Assignment_Statement then
5631 -- Copy name and expression fields in usual manner
5633 Set_Name (New_N,
5634 Copy_Generic_Node (Name (N), New_N, Instantiating));
5636 Set_Expression (New_N,
5637 Copy_Generic_Node (Expression (N), New_N, Instantiating));
5639 if Instantiating then
5640 Set_Assignment_OK (Name (New_N), True);
5641 end if;
5643 elsif Nkind (N) = N_Aggregate
5644 or else Nkind (N) = N_Extension_Aggregate
5645 then
5647 if not Instantiating then
5648 Set_Associated_Node (N, New_N);
5650 else
5651 if Present (Get_Associated_Node (N))
5652 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
5653 then
5654 -- In the generic the aggregate has some composite type. If at
5655 -- the point of instantiation the type has a private view,
5656 -- install the full view (and that of its ancestors, if any).
5658 declare
5659 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
5660 Rt : Entity_Id;
5662 begin
5663 if Present (T)
5664 and then Is_Private_Type (T)
5665 then
5666 Switch_View (T);
5667 end if;
5669 if Present (T)
5670 and then Is_Tagged_Type (T)
5671 and then Is_Derived_Type (T)
5672 then
5673 Rt := Root_Type (T);
5675 loop
5676 T := Etype (T);
5678 if Is_Private_Type (T) then
5679 Switch_View (T);
5680 end if;
5682 exit when T = Rt;
5683 end loop;
5684 end if;
5685 end;
5686 end if;
5687 end if;
5689 -- Do not copy the associated node, which points to
5690 -- the generic copy of the aggregate.
5692 declare
5693 use Atree.Unchecked_Access;
5694 -- This code section is part of the implementation of an untyped
5695 -- tree traversal, so it needs direct access to node fields.
5697 begin
5698 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
5699 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
5700 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
5701 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
5702 end;
5704 -- Allocators do not have an identifier denoting the access type,
5705 -- so we must locate it through the expression to check whether
5706 -- the views are consistent.
5708 elsif Nkind (N) = N_Allocator
5709 and then Nkind (Expression (N)) = N_Qualified_Expression
5710 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
5711 and then Instantiating
5712 then
5713 declare
5714 T : constant Node_Id :=
5715 Get_Associated_Node (Subtype_Mark (Expression (N)));
5716 Acc_T : Entity_Id;
5718 begin
5719 if Present (T) then
5720 -- Retrieve the allocator node in the generic copy
5722 Acc_T := Etype (Parent (Parent (T)));
5723 if Present (Acc_T)
5724 and then Is_Private_Type (Acc_T)
5725 then
5726 Switch_View (Acc_T);
5727 end if;
5728 end if;
5730 Copy_Descendants;
5731 end;
5733 -- For a proper body, we must catch the case of a proper body that
5734 -- replaces a stub. This represents the point at which a separate
5735 -- compilation unit, and hence template file, may be referenced, so
5736 -- we must make a new source instantiation entry for the template
5737 -- of the subunit, and ensure that all nodes in the subunit are
5738 -- adjusted using this new source instantiation entry.
5740 elsif Nkind (N) in N_Proper_Body then
5741 declare
5742 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
5744 begin
5745 if Instantiating and then Was_Originally_Stub (N) then
5746 Create_Instantiation_Source
5747 (Instantiation_Node,
5748 Defining_Entity (N),
5749 False,
5750 S_Adjustment);
5751 end if;
5753 -- Now copy the fields of the proper body, using the new
5754 -- adjustment factor if one was needed as per test above.
5756 Copy_Descendants;
5758 -- Restore the original adjustment factor in case changed
5760 S_Adjustment := Save_Adjustment;
5761 end;
5763 -- Don't copy Ident or Comment pragmas, since the comment belongs
5764 -- to the generic unit, not to the instantiating unit.
5766 elsif Nkind (N) = N_Pragma
5767 and then Instantiating
5768 then
5769 declare
5770 Prag_Id : constant Pragma_Id := Get_Pragma_Id (Chars (N));
5772 begin
5773 if Prag_Id = Pragma_Ident
5774 or else Prag_Id = Pragma_Comment
5775 then
5776 New_N := Make_Null_Statement (Sloc (N));
5778 else
5779 Copy_Descendants;
5780 end if;
5781 end;
5783 elsif Nkind (N) = N_Integer_Literal
5784 or else Nkind (N) = N_Real_Literal
5785 then
5786 -- No descendant fields need traversing
5788 null;
5790 -- For the remaining nodes, copy recursively their descendants
5792 else
5793 Copy_Descendants;
5795 if Instantiating
5796 and then Nkind (N) = N_Subprogram_Body
5797 then
5798 Set_Generic_Parent (Specification (New_N), N);
5799 end if;
5800 end if;
5802 return New_N;
5803 end Copy_Generic_Node;
5805 ----------------------------
5806 -- Denotes_Formal_Package --
5807 ----------------------------
5809 function Denotes_Formal_Package
5810 (Pack : Entity_Id;
5811 On_Exit : Boolean := False) return Boolean
5813 Par : Entity_Id;
5814 Scop : constant Entity_Id := Scope (Pack);
5815 E : Entity_Id;
5817 begin
5818 if On_Exit then
5819 Par :=
5820 Instance_Envs.Table
5821 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
5822 else
5823 Par := Current_Instantiated_Parent.Act_Id;
5824 end if;
5826 if Ekind (Scop) = E_Generic_Package
5827 or else Nkind (Unit_Declaration_Node (Scop)) =
5828 N_Generic_Subprogram_Declaration
5829 then
5830 return True;
5832 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
5833 N_Formal_Package_Declaration
5834 then
5835 return True;
5837 elsif No (Par) then
5838 return False;
5840 else
5841 -- Check whether this package is associated with a formal
5842 -- package of the enclosing instantiation. Iterate over the
5843 -- list of renamings.
5845 E := First_Entity (Par);
5846 while Present (E) loop
5847 if Ekind (E) /= E_Package
5848 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
5849 then
5850 null;
5852 elsif Renamed_Object (E) = Par then
5853 return False;
5855 elsif Renamed_Object (E) = Pack then
5856 return True;
5857 end if;
5859 Next_Entity (E);
5860 end loop;
5862 return False;
5863 end if;
5864 end Denotes_Formal_Package;
5866 -----------------
5867 -- End_Generic --
5868 -----------------
5870 procedure End_Generic is
5871 begin
5872 -- ??? More things could be factored out in this
5873 -- routine. Should probably be done at a later stage.
5875 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
5876 Generic_Flags.Decrement_Last;
5878 Expander_Mode_Restore;
5879 end End_Generic;
5881 ----------------------
5882 -- Find_Actual_Type --
5883 ----------------------
5885 function Find_Actual_Type
5886 (Typ : Entity_Id;
5887 Gen_Scope : Entity_Id) return Entity_Id
5889 T : Entity_Id;
5891 begin
5892 if not Is_Child_Unit (Gen_Scope) then
5893 return Get_Instance_Of (Typ);
5895 elsif not Is_Generic_Type (Typ)
5896 or else Scope (Typ) = Gen_Scope
5897 then
5898 return Get_Instance_Of (Typ);
5900 else
5901 T := Current_Entity (Typ);
5902 while Present (T) loop
5903 if In_Open_Scopes (Scope (T)) then
5904 return T;
5906 elsif Is_Generic_Actual_Type (T) then
5907 return T;
5908 end if;
5910 T := Homonym (T);
5911 end loop;
5913 return Typ;
5914 end if;
5915 end Find_Actual_Type;
5917 ----------------------------
5918 -- Freeze_Subprogram_Body --
5919 ----------------------------
5921 procedure Freeze_Subprogram_Body
5922 (Inst_Node : Node_Id;
5923 Gen_Body : Node_Id;
5924 Pack_Id : Entity_Id)
5926 F_Node : Node_Id;
5927 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
5928 Par : constant Entity_Id := Scope (Gen_Unit);
5929 Enc_G : Entity_Id;
5930 Enc_I : Node_Id;
5931 E_G_Id : Entity_Id;
5933 function Earlier (N1, N2 : Node_Id) return Boolean;
5934 -- Yields True if N1 and N2 appear in the same compilation unit,
5935 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
5936 -- traversal of the tree for the unit.
5938 function Enclosing_Body (N : Node_Id) return Node_Id;
5939 -- Find innermost package body that encloses the given node, and which
5940 -- is not a compilation unit. Freeze nodes for the instance, or for its
5941 -- enclosing body, may be inserted after the enclosing_body of the
5942 -- generic unit.
5944 function Package_Freeze_Node (B : Node_Id) return Node_Id;
5945 -- Find entity for given package body, and locate or create a freeze
5946 -- node for it.
5948 function True_Parent (N : Node_Id) return Node_Id;
5949 -- For a subunit, return parent of corresponding stub
5951 -------------
5952 -- Earlier --
5953 -------------
5955 function Earlier (N1, N2 : Node_Id) return Boolean is
5956 D1 : Integer := 0;
5957 D2 : Integer := 0;
5958 P1 : Node_Id := N1;
5959 P2 : Node_Id := N2;
5961 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
5962 -- Find distance from given node to enclosing compilation unit
5964 ----------------
5965 -- Find_Depth --
5966 ----------------
5968 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
5969 begin
5970 while Present (P)
5971 and then Nkind (P) /= N_Compilation_Unit
5972 loop
5973 P := True_Parent (P);
5974 D := D + 1;
5975 end loop;
5976 end Find_Depth;
5978 -- Start of procesing for Earlier
5980 begin
5981 Find_Depth (P1, D1);
5982 Find_Depth (P2, D2);
5984 if P1 /= P2 then
5985 return False;
5986 else
5987 P1 := N1;
5988 P2 := N2;
5989 end if;
5991 while D1 > D2 loop
5992 P1 := True_Parent (P1);
5993 D1 := D1 - 1;
5994 end loop;
5996 while D2 > D1 loop
5997 P2 := True_Parent (P2);
5998 D2 := D2 - 1;
5999 end loop;
6001 -- At this point P1 and P2 are at the same distance from the root.
6002 -- We examine their parents until we find a common declarative
6003 -- list, at which point we can establish their relative placement
6004 -- by comparing their ultimate slocs. If we reach the root,
6005 -- N1 and N2 do not descend from the same declarative list (e.g.
6006 -- one is nested in the declarative part and the other is in a block
6007 -- in the statement part) and the earlier one is already frozen.
6009 while not Is_List_Member (P1)
6010 or else not Is_List_Member (P2)
6011 or else List_Containing (P1) /= List_Containing (P2)
6012 loop
6013 P1 := True_Parent (P1);
6014 P2 := True_Parent (P2);
6016 if Nkind (Parent (P1)) = N_Subunit then
6017 P1 := Corresponding_Stub (Parent (P1));
6018 end if;
6020 if Nkind (Parent (P2)) = N_Subunit then
6021 P2 := Corresponding_Stub (Parent (P2));
6022 end if;
6024 if P1 = P2 then
6025 return False;
6026 end if;
6027 end loop;
6029 return
6030 Top_Level_Location (Sloc (P1)) < Top_Level_Location (Sloc (P2));
6031 end Earlier;
6033 --------------------
6034 -- Enclosing_Body --
6035 --------------------
6037 function Enclosing_Body (N : Node_Id) return Node_Id is
6038 P : Node_Id := Parent (N);
6040 begin
6041 while Present (P)
6042 and then Nkind (Parent (P)) /= N_Compilation_Unit
6043 loop
6044 if Nkind (P) = N_Package_Body then
6046 if Nkind (Parent (P)) = N_Subunit then
6047 return Corresponding_Stub (Parent (P));
6048 else
6049 return P;
6050 end if;
6051 end if;
6053 P := True_Parent (P);
6054 end loop;
6056 return Empty;
6057 end Enclosing_Body;
6059 -------------------------
6060 -- Package_Freeze_Node --
6061 -------------------------
6063 function Package_Freeze_Node (B : Node_Id) return Node_Id is
6064 Id : Entity_Id;
6066 begin
6067 if Nkind (B) = N_Package_Body then
6068 Id := Corresponding_Spec (B);
6070 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
6071 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
6072 end if;
6074 Ensure_Freeze_Node (Id);
6075 return Freeze_Node (Id);
6076 end Package_Freeze_Node;
6078 -----------------
6079 -- True_Parent --
6080 -----------------
6082 function True_Parent (N : Node_Id) return Node_Id is
6083 begin
6084 if Nkind (Parent (N)) = N_Subunit then
6085 return Parent (Corresponding_Stub (Parent (N)));
6086 else
6087 return Parent (N);
6088 end if;
6089 end True_Parent;
6091 -- Start of processing of Freeze_Subprogram_Body
6093 begin
6094 -- If the instance and the generic body appear within the same
6095 -- unit, and the instance preceeds the generic, the freeze node for
6096 -- the instance must appear after that of the generic. If the generic
6097 -- is nested within another instance I2, then current instance must
6098 -- be frozen after I2. In both cases, the freeze nodes are those of
6099 -- enclosing packages. Otherwise, the freeze node is placed at the end
6100 -- of the current declarative part.
6102 Enc_G := Enclosing_Body (Gen_Body);
6103 Enc_I := Enclosing_Body (Inst_Node);
6104 Ensure_Freeze_Node (Pack_Id);
6105 F_Node := Freeze_Node (Pack_Id);
6107 if Is_Generic_Instance (Par)
6108 and then Present (Freeze_Node (Par))
6109 and then
6110 In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
6111 then
6112 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
6114 -- The parent was a premature instantiation. Insert freeze
6115 -- node at the end the current declarative part.
6117 Insert_After_Last_Decl (Inst_Node, F_Node);
6119 else
6120 Insert_After (Freeze_Node (Par), F_Node);
6121 end if;
6123 -- The body enclosing the instance should be frozen after the body
6124 -- that includes the generic, because the body of the instance may
6125 -- make references to entities therein. If the two are not in the
6126 -- same declarative part, or if the one enclosing the instance is
6127 -- frozen already, freeze the instance at the end of the current
6128 -- declarative part.
6130 elsif Is_Generic_Instance (Par)
6131 and then Present (Freeze_Node (Par))
6132 and then Present (Enc_I)
6133 then
6134 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
6135 or else
6136 (Nkind (Enc_I) = N_Package_Body
6137 and then
6138 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
6139 then
6140 -- The enclosing package may contain several instances. Rather
6141 -- than computing the earliest point at which to insert its
6142 -- freeze node, we place it at the end of the declarative part
6143 -- of the parent of the generic.
6145 Insert_After_Last_Decl
6146 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
6147 end if;
6149 Insert_After_Last_Decl (Inst_Node, F_Node);
6151 elsif Present (Enc_G)
6152 and then Present (Enc_I)
6153 and then Enc_G /= Enc_I
6154 and then Earlier (Inst_Node, Gen_Body)
6155 then
6156 if Nkind (Enc_G) = N_Package_Body then
6157 E_G_Id := Corresponding_Spec (Enc_G);
6158 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
6159 E_G_Id :=
6160 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
6161 end if;
6163 -- Freeze package that encloses instance, and place node after
6164 -- package that encloses generic. If enclosing package is already
6165 -- frozen we have to assume it is at the proper place. This may
6166 -- be a potential ABE that requires dynamic checking.
6168 Insert_After_Last_Decl (Enc_G, Package_Freeze_Node (Enc_I));
6170 -- Freeze enclosing subunit before instance
6172 Ensure_Freeze_Node (E_G_Id);
6174 if not Is_List_Member (Freeze_Node (E_G_Id)) then
6175 Insert_After (Enc_G, Freeze_Node (E_G_Id));
6176 end if;
6178 Insert_After_Last_Decl (Inst_Node, F_Node);
6180 else
6181 -- If none of the above, insert freeze node at the end of the
6182 -- current declarative part.
6184 Insert_After_Last_Decl (Inst_Node, F_Node);
6185 end if;
6186 end Freeze_Subprogram_Body;
6188 ----------------
6189 -- Get_Gen_Id --
6190 ----------------
6192 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
6193 begin
6194 return Generic_Renamings.Table (E).Gen_Id;
6195 end Get_Gen_Id;
6197 ---------------------
6198 -- Get_Instance_Of --
6199 ---------------------
6201 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
6202 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
6204 begin
6205 if Res /= Assoc_Null then
6206 return Generic_Renamings.Table (Res).Act_Id;
6207 else
6208 -- On exit, entity is not instantiated: not a generic parameter,
6209 -- or else parameter of an inner generic unit.
6211 return A;
6212 end if;
6213 end Get_Instance_Of;
6215 ------------------------------------
6216 -- Get_Package_Instantiation_Node --
6217 ------------------------------------
6219 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
6220 Decl : Node_Id := Unit_Declaration_Node (A);
6221 Inst : Node_Id;
6223 begin
6224 -- If the Package_Instantiation attribute has been set on the package
6225 -- entity, then use it directly when it (or its Original_Node) refers
6226 -- to an N_Package_Instantiation node. In principle it should be
6227 -- possible to have this field set in all cases, which should be
6228 -- investigated, and would allow this function to be significantly
6229 -- simplified. ???
6231 if Present (Package_Instantiation (A)) then
6232 if Nkind (Package_Instantiation (A)) = N_Package_Instantiation then
6233 return Package_Instantiation (A);
6235 elsif Nkind (Original_Node (Package_Instantiation (A)))
6236 = N_Package_Instantiation
6237 then
6238 return Original_Node (Package_Instantiation (A));
6239 end if;
6240 end if;
6242 -- If the instantiation is a compilation unit that does not need a
6243 -- body then the instantiation node has been rewritten as a package
6244 -- declaration for the instance, and we return the original node.
6246 -- If it is a compilation unit and the instance node has not been
6247 -- rewritten, then it is still the unit of the compilation. Finally,
6248 -- if a body is present, this is a parent of the main unit whose body
6249 -- has been compiled for inlining purposes, and the instantiation node
6250 -- has been rewritten with the instance body.
6252 -- Otherwise the instantiation node appears after the declaration.
6253 -- If the entity is a formal package, the declaration may have been
6254 -- rewritten as a generic declaration (in the case of a formal with a
6255 -- box) or left as a formal package declaration if it has actuals, and
6256 -- is found with a forward search.
6258 if Nkind (Parent (Decl)) = N_Compilation_Unit then
6259 if Nkind (Decl) = N_Package_Declaration
6260 and then Present (Corresponding_Body (Decl))
6261 then
6262 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
6263 end if;
6265 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
6266 return Original_Node (Decl);
6267 else
6268 return Unit (Parent (Decl));
6269 end if;
6271 elsif Nkind (Decl) = N_Package_Declaration
6272 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
6273 then
6274 return Original_Node (Decl);
6276 else
6277 Inst := Next (Decl);
6278 while Nkind (Inst) /= N_Package_Instantiation
6279 and then Nkind (Inst) /= N_Formal_Package_Declaration
6280 loop
6281 Next (Inst);
6282 end loop;
6284 return Inst;
6285 end if;
6286 end Get_Package_Instantiation_Node;
6288 ------------------------
6289 -- Has_Been_Exchanged --
6290 ------------------------
6292 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
6293 Next : Elmt_Id := First_Elmt (Exchanged_Views);
6295 begin
6296 while Present (Next) loop
6297 if Full_View (Node (Next)) = E then
6298 return True;
6299 end if;
6301 Next_Elmt (Next);
6302 end loop;
6304 return False;
6305 end Has_Been_Exchanged;
6307 ----------
6308 -- Hash --
6309 ----------
6311 function Hash (F : Entity_Id) return HTable_Range is
6312 begin
6313 return HTable_Range (F mod HTable_Size);
6314 end Hash;
6316 ------------------------
6317 -- Hide_Current_Scope --
6318 ------------------------
6320 procedure Hide_Current_Scope is
6321 C : constant Entity_Id := Current_Scope;
6322 E : Entity_Id;
6324 begin
6325 Set_Is_Hidden_Open_Scope (C);
6326 E := First_Entity (C);
6328 while Present (E) loop
6329 if Is_Immediately_Visible (E) then
6330 Set_Is_Immediately_Visible (E, False);
6331 Append_Elmt (E, Hidden_Entities);
6332 end if;
6334 Next_Entity (E);
6335 end loop;
6337 -- Make the scope name invisible as well. This is necessary, but
6338 -- might conflict with calls to Rtsfind later on, in case the scope
6339 -- is a predefined one. There is no clean solution to this problem, so
6340 -- for now we depend on the user not redefining Standard itself in one
6341 -- of the parent units.
6343 if Is_Immediately_Visible (C)
6344 and then C /= Standard_Standard
6345 then
6346 Set_Is_Immediately_Visible (C, False);
6347 Append_Elmt (C, Hidden_Entities);
6348 end if;
6350 end Hide_Current_Scope;
6352 --------------
6353 -- Init_Env --
6354 --------------
6356 procedure Init_Env is
6357 Saved : Instance_Env;
6359 begin
6360 Saved.Ada_Version := Ada_Version;
6361 Saved.Ada_Version_Explicit := Ada_Version_Explicit;
6362 Saved.Instantiated_Parent := Current_Instantiated_Parent;
6363 Saved.Exchanged_Views := Exchanged_Views;
6364 Saved.Hidden_Entities := Hidden_Entities;
6365 Saved.Current_Sem_Unit := Current_Sem_Unit;
6366 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
6367 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
6368 Instance_Envs.Increment_Last;
6369 Instance_Envs.Table (Instance_Envs.Last) := Saved;
6371 Exchanged_Views := New_Elmt_List;
6372 Hidden_Entities := New_Elmt_List;
6374 -- Make dummy entry for Instantiated parent. If generic unit is
6375 -- legal, this is set properly in Set_Instance_Env.
6377 Current_Instantiated_Parent :=
6378 (Current_Scope, Current_Scope, Assoc_Null);
6379 end Init_Env;
6381 ------------------------------
6382 -- In_Same_Declarative_Part --
6383 ------------------------------
6385 function In_Same_Declarative_Part
6386 (F_Node : Node_Id;
6387 Inst : Node_Id) return Boolean
6389 Decls : constant Node_Id := Parent (F_Node);
6390 Nod : Node_Id := Parent (Inst);
6392 begin
6393 while Present (Nod) loop
6394 if Nod = Decls then
6395 return True;
6397 elsif Nkind (Nod) = N_Subprogram_Body
6398 or else Nkind (Nod) = N_Package_Body
6399 or else Nkind (Nod) = N_Task_Body
6400 or else Nkind (Nod) = N_Protected_Body
6401 or else Nkind (Nod) = N_Block_Statement
6402 then
6403 return False;
6405 elsif Nkind (Nod) = N_Subunit then
6406 Nod := Corresponding_Stub (Nod);
6408 elsif Nkind (Nod) = N_Compilation_Unit then
6409 return False;
6410 else
6411 Nod := Parent (Nod);
6412 end if;
6413 end loop;
6415 return False;
6416 end In_Same_Declarative_Part;
6418 ---------------------
6419 -- In_Main_Context --
6420 ---------------------
6422 function In_Main_Context (E : Entity_Id) return Boolean is
6423 Context : List_Id;
6424 Clause : Node_Id;
6425 Nam : Node_Id;
6427 begin
6428 if not Is_Compilation_Unit (E)
6429 or else Ekind (E) /= E_Package
6430 or else In_Private_Part (E)
6431 then
6432 return False;
6433 end if;
6435 Context := Context_Items (Cunit (Main_Unit));
6437 Clause := First (Context);
6438 while Present (Clause) loop
6439 if Nkind (Clause) = N_With_Clause then
6440 Nam := Name (Clause);
6442 -- If the current scope is part of the context of the main unit,
6443 -- analysis of the corresponding with_clause is not complete, and
6444 -- the entity is not set. We use the Chars field directly, which
6445 -- might produce false positives in rare cases, but guarantees
6446 -- that we produce all the instance bodies we will need.
6448 if (Nkind (Nam) = N_Identifier
6449 and then Chars (Nam) = Chars (E))
6450 or else (Nkind (Nam) = N_Selected_Component
6451 and then Chars (Selector_Name (Nam)) = Chars (E))
6452 then
6453 return True;
6454 end if;
6455 end if;
6457 Next (Clause);
6458 end loop;
6460 return False;
6461 end In_Main_Context;
6463 ---------------------
6464 -- Inherit_Context --
6465 ---------------------
6467 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
6468 Current_Context : List_Id;
6469 Current_Unit : Node_Id;
6470 Item : Node_Id;
6471 New_I : Node_Id;
6473 begin
6474 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
6476 -- The inherited context is attached to the enclosing compilation
6477 -- unit. This is either the main unit, or the declaration for the
6478 -- main unit (in case the instantation appears within the package
6479 -- declaration and the main unit is its body).
6481 Current_Unit := Parent (Inst);
6482 while Present (Current_Unit)
6483 and then Nkind (Current_Unit) /= N_Compilation_Unit
6484 loop
6485 Current_Unit := Parent (Current_Unit);
6486 end loop;
6488 Current_Context := Context_Items (Current_Unit);
6490 Item := First (Context_Items (Parent (Gen_Decl)));
6491 while Present (Item) loop
6492 if Nkind (Item) = N_With_Clause then
6493 New_I := New_Copy (Item);
6494 Set_Implicit_With (New_I, True);
6495 Append (New_I, Current_Context);
6496 end if;
6498 Next (Item);
6499 end loop;
6500 end if;
6501 end Inherit_Context;
6503 ----------------
6504 -- Initialize --
6505 ----------------
6507 procedure Initialize is
6508 begin
6509 Generic_Renamings.Init;
6510 Instance_Envs.Init;
6511 Generic_Flags.Init;
6512 Generic_Renamings_HTable.Reset;
6513 Circularity_Detected := False;
6514 Exchanged_Views := No_Elist;
6515 Hidden_Entities := No_Elist;
6516 end Initialize;
6518 ----------------------------
6519 -- Insert_After_Last_Decl --
6520 ----------------------------
6522 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id) is
6523 L : List_Id := List_Containing (N);
6524 P : constant Node_Id := Parent (L);
6526 begin
6527 if not Is_List_Member (F_Node) then
6528 if Nkind (P) = N_Package_Specification
6529 and then L = Visible_Declarations (P)
6530 and then Present (Private_Declarations (P))
6531 and then not Is_Empty_List (Private_Declarations (P))
6532 then
6533 L := Private_Declarations (P);
6534 end if;
6536 Insert_After (Last (L), F_Node);
6537 end if;
6538 end Insert_After_Last_Decl;
6540 ------------------
6541 -- Install_Body --
6542 ------------------
6544 procedure Install_Body
6545 (Act_Body : Node_Id;
6546 N : Node_Id;
6547 Gen_Body : Node_Id;
6548 Gen_Decl : Node_Id)
6550 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
6551 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
6552 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
6553 Par : constant Entity_Id := Scope (Gen_Id);
6554 Gen_Unit : constant Node_Id :=
6555 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
6556 Orig_Body : Node_Id := Gen_Body;
6557 F_Node : Node_Id;
6558 Body_Unit : Node_Id;
6560 Must_Delay : Boolean;
6562 function Enclosing_Subp (Id : Entity_Id) return Entity_Id;
6563 -- Find subprogram (if any) that encloses instance and/or generic body
6565 function True_Sloc (N : Node_Id) return Source_Ptr;
6566 -- If the instance is nested inside a generic unit, the Sloc of the
6567 -- instance indicates the place of the original definition, not the
6568 -- point of the current enclosing instance. Pending a better usage of
6569 -- Slocs to indicate instantiation places, we determine the place of
6570 -- origin of a node by finding the maximum sloc of any ancestor node.
6571 -- Why is this not equivalent to Top_Level_Location ???
6573 --------------------
6574 -- Enclosing_Subp --
6575 --------------------
6577 function Enclosing_Subp (Id : Entity_Id) return Entity_Id is
6578 Scop : Entity_Id := Scope (Id);
6580 begin
6581 while Scop /= Standard_Standard
6582 and then not Is_Overloadable (Scop)
6583 loop
6584 Scop := Scope (Scop);
6585 end loop;
6587 return Scop;
6588 end Enclosing_Subp;
6590 ---------------
6591 -- True_Sloc --
6592 ---------------
6594 function True_Sloc (N : Node_Id) return Source_Ptr is
6595 Res : Source_Ptr;
6596 N1 : Node_Id;
6598 begin
6599 Res := Sloc (N);
6600 N1 := N;
6601 while Present (N1) and then N1 /= Act_Unit loop
6602 if Sloc (N1) > Res then
6603 Res := Sloc (N1);
6604 end if;
6606 N1 := Parent (N1);
6607 end loop;
6609 return Res;
6610 end True_Sloc;
6612 -- Start of processing for Install_Body
6614 begin
6615 -- If the body is a subunit, the freeze point is the corresponding
6616 -- stub in the current compilation, not the subunit itself.
6618 if Nkind (Parent (Gen_Body)) = N_Subunit then
6619 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
6620 else
6621 Orig_Body := Gen_Body;
6622 end if;
6624 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
6626 -- If the instantiation and the generic definition appear in the
6627 -- same package declaration, this is an early instantiation.
6628 -- If they appear in the same declarative part, it is an early
6629 -- instantiation only if the generic body appears textually later,
6630 -- and the generic body is also in the main unit.
6632 -- If instance is nested within a subprogram, and the generic body is
6633 -- not, the instance is delayed because the enclosing body is. If
6634 -- instance and body are within the same scope, or the same sub-
6635 -- program body, indicate explicitly that the instance is delayed.
6637 Must_Delay :=
6638 (Gen_Unit = Act_Unit
6639 and then ((Nkind (Gen_Unit) = N_Package_Declaration)
6640 or else Nkind (Gen_Unit) = N_Generic_Package_Declaration
6641 or else (Gen_Unit = Body_Unit
6642 and then True_Sloc (N) < Sloc (Orig_Body)))
6643 and then Is_In_Main_Unit (Gen_Unit)
6644 and then (Scope (Act_Id) = Scope (Gen_Id)
6645 or else
6646 Enclosing_Subp (Act_Id) = Enclosing_Subp (Gen_Id)));
6648 -- If this is an early instantiation, the freeze node is placed after
6649 -- the generic body. Otherwise, if the generic appears in an instance,
6650 -- we cannot freeze the current instance until the outer one is frozen.
6651 -- This is only relevant if the current instance is nested within some
6652 -- inner scope not itself within the outer instance. If this scope is
6653 -- a package body in the same declarative part as the outer instance,
6654 -- then that body needs to be frozen after the outer instance. Finally,
6655 -- if no delay is needed, we place the freeze node at the end of the
6656 -- current declarative part.
6658 if Expander_Active then
6659 Ensure_Freeze_Node (Act_Id);
6660 F_Node := Freeze_Node (Act_Id);
6662 if Must_Delay then
6663 Insert_After (Orig_Body, F_Node);
6665 elsif Is_Generic_Instance (Par)
6666 and then Present (Freeze_Node (Par))
6667 and then Scope (Act_Id) /= Par
6668 then
6669 -- Freeze instance of inner generic after instance of enclosing
6670 -- generic.
6672 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
6673 Insert_After (Freeze_Node (Par), F_Node);
6675 -- Freeze package enclosing instance of inner generic after
6676 -- instance of enclosing generic.
6678 elsif Nkind (Parent (N)) = N_Package_Body
6679 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
6680 then
6682 declare
6683 Enclosing : constant Entity_Id :=
6684 Corresponding_Spec (Parent (N));
6686 begin
6687 Insert_After_Last_Decl (N, F_Node);
6688 Ensure_Freeze_Node (Enclosing);
6690 if not Is_List_Member (Freeze_Node (Enclosing)) then
6691 Insert_After (Freeze_Node (Par), Freeze_Node (Enclosing));
6692 end if;
6693 end;
6695 else
6696 Insert_After_Last_Decl (N, F_Node);
6697 end if;
6699 else
6700 Insert_After_Last_Decl (N, F_Node);
6701 end if;
6702 end if;
6704 Set_Is_Frozen (Act_Id);
6705 Insert_Before (N, Act_Body);
6706 Mark_Rewrite_Insertion (Act_Body);
6707 end Install_Body;
6709 --------------------
6710 -- Install_Parent --
6711 --------------------
6713 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
6714 Ancestors : constant Elist_Id := New_Elmt_List;
6715 S : constant Entity_Id := Current_Scope;
6716 Inst_Par : Entity_Id;
6717 First_Par : Entity_Id;
6718 Inst_Node : Node_Id;
6719 Gen_Par : Entity_Id;
6720 First_Gen : Entity_Id;
6721 Elmt : Elmt_Id;
6723 procedure Install_Formal_Packages (Par : Entity_Id);
6724 -- If any of the formals of the parent are formal packages with box,
6725 -- their formal parts are visible in the parent and thus in the child
6726 -- unit as well. Analogous to what is done in Check_Generic_Actuals
6727 -- for the unit itself.
6729 procedure Install_Noninstance_Specs (Par : Entity_Id);
6730 -- Install the scopes of noninstance parent units ending with Par
6732 procedure Install_Spec (Par : Entity_Id);
6733 -- The child unit is within the declarative part of the parent, so
6734 -- the declarations within the parent are immediately visible.
6736 -----------------------------
6737 -- Install_Formal_Packages --
6738 -----------------------------
6740 procedure Install_Formal_Packages (Par : Entity_Id) is
6741 E : Entity_Id;
6743 begin
6744 E := First_Entity (Par);
6745 while Present (E) loop
6746 if Ekind (E) = E_Package
6747 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
6748 then
6749 -- If this is the renaming for the parent instance, done
6751 if Renamed_Object (E) = Par then
6752 exit;
6754 -- The visibility of a formal of an enclosing generic is
6755 -- already correct.
6757 elsif Denotes_Formal_Package (E) then
6758 null;
6760 elsif Present (Associated_Formal_Package (E))
6761 and then Box_Present (Parent (Associated_Formal_Package (E)))
6762 then
6763 Check_Generic_Actuals (Renamed_Object (E), True);
6764 Set_Is_Hidden (E, False);
6765 end if;
6766 end if;
6768 Next_Entity (E);
6769 end loop;
6770 end Install_Formal_Packages;
6772 -------------------------------
6773 -- Install_Noninstance_Specs --
6774 -------------------------------
6776 procedure Install_Noninstance_Specs (Par : Entity_Id) is
6777 begin
6778 if Present (Par)
6779 and then Par /= Standard_Standard
6780 and then not In_Open_Scopes (Par)
6781 then
6782 Install_Noninstance_Specs (Scope (Par));
6783 Install_Spec (Par);
6784 end if;
6785 end Install_Noninstance_Specs;
6787 ------------------
6788 -- Install_Spec --
6789 ------------------
6791 procedure Install_Spec (Par : Entity_Id) is
6792 Spec : constant Node_Id :=
6793 Specification (Unit_Declaration_Node (Par));
6795 begin
6796 -- If this parent of the child instance is a top-level unit,
6797 -- then record the unit and its visibility for later resetting
6798 -- in Remove_Parent. We exclude units that are generic instances,
6799 -- as we only want to record this information for the ultimate
6800 -- top-level noninstance parent (is that always correct???).
6802 if Scope (Par) = Standard_Standard
6803 and then not Is_Generic_Instance (Par)
6804 then
6805 Parent_Unit_Visible := Is_Immediately_Visible (Par);
6806 Instance_Parent_Unit := Par;
6807 end if;
6809 -- Open the parent scope and make it and its declarations visible.
6810 -- If this point is not within a body, then only the visible
6811 -- declarations should be made visible, and installation of the
6812 -- private declarations is deferred until the appropriate point
6813 -- within analysis of the spec being instantiated (see the handling
6814 -- of parent visibility in Analyze_Package_Specification). This is
6815 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
6816 -- private view problems that occur when compiling instantiations of
6817 -- a generic child of that package (Generic_Dispatching_Constructor).
6818 -- If the instance freezes a tagged type, inlinings of operations
6819 -- from Ada.Tags may need the full view of type Tag. If inlining
6820 -- took proper account of establishing visibility of inlined
6821 -- subprograms' parents then it should be possible to remove this
6822 -- special check. ???
6824 New_Scope (Par);
6825 Set_Is_Immediately_Visible (Par);
6826 Install_Visible_Declarations (Par);
6827 Set_Use (Visible_Declarations (Spec));
6829 if In_Body or else Is_RTU (Par, Ada_Tags) then
6830 Install_Private_Declarations (Par);
6831 Set_Use (Private_Declarations (Spec));
6832 end if;
6833 end Install_Spec;
6835 -- Start of processing for Install_Parent
6837 begin
6838 -- We need to install the parent instance to compile the instantiation
6839 -- of the child, but the child instance must appear in the current
6840 -- scope. Given that we cannot place the parent above the current
6841 -- scope in the scope stack, we duplicate the current scope and unstack
6842 -- both after the instantiation is complete.
6844 -- If the parent is itself the instantiation of a child unit, we must
6845 -- also stack the instantiation of its parent, and so on. Each such
6846 -- ancestor is the prefix of the name in a prior instantiation.
6848 -- If this is a nested instance, the parent unit itself resolves to
6849 -- a renaming of the parent instance, whose declaration we need.
6851 -- Finally, the parent may be a generic (not an instance) when the
6852 -- child unit appears as a formal package.
6854 Inst_Par := P;
6856 if Present (Renamed_Entity (Inst_Par)) then
6857 Inst_Par := Renamed_Entity (Inst_Par);
6858 end if;
6860 First_Par := Inst_Par;
6862 Gen_Par :=
6863 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
6865 First_Gen := Gen_Par;
6867 while Present (Gen_Par)
6868 and then Is_Child_Unit (Gen_Par)
6869 loop
6870 -- Load grandparent instance as well
6872 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
6874 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
6875 Inst_Par := Entity (Prefix (Name (Inst_Node)));
6877 if Present (Renamed_Entity (Inst_Par)) then
6878 Inst_Par := Renamed_Entity (Inst_Par);
6879 end if;
6881 Gen_Par :=
6882 Generic_Parent
6883 (Specification (Unit_Declaration_Node (Inst_Par)));
6885 if Present (Gen_Par) then
6886 Prepend_Elmt (Inst_Par, Ancestors);
6888 else
6889 -- Parent is not the name of an instantiation
6891 Install_Noninstance_Specs (Inst_Par);
6893 exit;
6894 end if;
6896 else
6897 -- Previous error
6899 exit;
6900 end if;
6901 end loop;
6903 if Present (First_Gen) then
6904 Append_Elmt (First_Par, Ancestors);
6906 else
6907 Install_Noninstance_Specs (First_Par);
6908 end if;
6910 if not Is_Empty_Elmt_List (Ancestors) then
6911 Elmt := First_Elmt (Ancestors);
6913 while Present (Elmt) loop
6914 Install_Spec (Node (Elmt));
6915 Install_Formal_Packages (Node (Elmt));
6917 Next_Elmt (Elmt);
6918 end loop;
6919 end if;
6921 if not In_Body then
6922 New_Scope (S);
6923 end if;
6924 end Install_Parent;
6926 --------------------------------
6927 -- Instantiate_Formal_Package --
6928 --------------------------------
6930 function Instantiate_Formal_Package
6931 (Formal : Node_Id;
6932 Actual : Node_Id;
6933 Analyzed_Formal : Node_Id) return List_Id
6935 Loc : constant Source_Ptr := Sloc (Actual);
6936 Actual_Pack : Entity_Id;
6937 Formal_Pack : Entity_Id;
6938 Gen_Parent : Entity_Id;
6939 Decls : List_Id;
6940 Nod : Node_Id;
6941 Parent_Spec : Node_Id;
6943 procedure Find_Matching_Actual
6944 (F : Node_Id;
6945 Act : in out Entity_Id);
6946 -- We need to associate each formal entity in the formal package
6947 -- with the corresponding entity in the actual package. The actual
6948 -- package has been analyzed and possibly expanded, and as a result
6949 -- there is no one-to-one correspondence between the two lists (for
6950 -- example, the actual may include subtypes, itypes, and inherited
6951 -- primitive operations, interspersed among the renaming declarations
6952 -- for the actuals) . We retrieve the corresponding actual by name
6953 -- because each actual has the same name as the formal, and they do
6954 -- appear in the same order.
6956 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
6957 -- Retrieve entity of defining entity of generic formal parameter.
6958 -- Only the declarations of formals need to be considered when
6959 -- linking them to actuals, but the declarative list may include
6960 -- internal entities generated during analysis, and those are ignored.
6962 procedure Match_Formal_Entity
6963 (Formal_Node : Node_Id;
6964 Formal_Ent : Entity_Id;
6965 Actual_Ent : Entity_Id);
6966 -- Associates the formal entity with the actual. In the case
6967 -- where Formal_Ent is a formal package, this procedure iterates
6968 -- through all of its formals and enters associations betwen the
6969 -- actuals occurring in the formal package's corresponding actual
6970 -- package (given by Actual_Ent) and the formal package's formal
6971 -- parameters. This procedure recurses if any of the parameters is
6972 -- itself a package.
6974 function Is_Instance_Of
6975 (Act_Spec : Entity_Id;
6976 Gen_Anc : Entity_Id) return Boolean;
6977 -- The actual can be an instantiation of a generic within another
6978 -- instance, in which case there is no direct link from it to the
6979 -- original generic ancestor. In that case, we recognize that the
6980 -- ultimate ancestor is the same by examining names and scopes.
6982 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id);
6983 -- Within the generic part, entities in the formal package are
6984 -- visible. To validate subsequent type declarations, indicate
6985 -- the correspondence betwen the entities in the analyzed formal,
6986 -- and the entities in the actual package. There are three packages
6987 -- involved in the instantiation of a formal package: the parent
6988 -- generic P1 which appears in the generic declaration, the fake
6989 -- instantiation P2 which appears in the analyzed generic, and whose
6990 -- visible entities may be used in subsequent formals, and the actual
6991 -- P3 in the instance. To validate subsequent formals, me indicate
6992 -- that the entities in P2 are mapped into those of P3. The mapping of
6993 -- entities has to be done recursively for nested packages.
6995 procedure Process_Nested_Formal (Formal : Entity_Id);
6996 -- If the current formal is declared with a box, its own formals are
6997 -- visible in the instance, as they were in the generic, and their
6998 -- Hidden flag must be reset. If some of these formals are themselves
6999 -- packages declared with a box, the processing must be recursive.
7001 --------------------------
7002 -- Find_Matching_Actual --
7003 --------------------------
7005 procedure Find_Matching_Actual
7006 (F : Node_Id;
7007 Act : in out Entity_Id)
7009 Formal_Ent : Entity_Id;
7011 begin
7012 case Nkind (Original_Node (F)) is
7013 when N_Formal_Object_Declaration |
7014 N_Formal_Type_Declaration =>
7015 Formal_Ent := Defining_Identifier (F);
7017 while Chars (Act) /= Chars (Formal_Ent) loop
7018 Next_Entity (Act);
7019 end loop;
7021 when N_Formal_Subprogram_Declaration |
7022 N_Formal_Package_Declaration |
7023 N_Package_Declaration |
7024 N_Generic_Package_Declaration =>
7025 Formal_Ent := Defining_Entity (F);
7027 while Chars (Act) /= Chars (Formal_Ent) loop
7028 Next_Entity (Act);
7029 end loop;
7031 when others =>
7032 raise Program_Error;
7033 end case;
7034 end Find_Matching_Actual;
7036 -------------------------
7037 -- Match_Formal_Entity --
7038 -------------------------
7040 procedure Match_Formal_Entity
7041 (Formal_Node : Node_Id;
7042 Formal_Ent : Entity_Id;
7043 Actual_Ent : Entity_Id)
7045 Act_Pkg : Entity_Id;
7047 begin
7048 Set_Instance_Of (Formal_Ent, Actual_Ent);
7050 if Ekind (Actual_Ent) = E_Package then
7051 -- Record associations for each parameter
7053 Act_Pkg := Actual_Ent;
7055 declare
7056 A_Ent : Entity_Id := First_Entity (Act_Pkg);
7057 F_Ent : Entity_Id;
7058 F_Node : Node_Id;
7060 Gen_Decl : Node_Id;
7061 Formals : List_Id;
7062 Actual : Entity_Id;
7064 begin
7065 -- Retrieve the actual given in the formal package declaration
7067 Actual := Entity (Name (Original_Node (Formal_Node)));
7069 -- The actual in the formal package declaration may be a
7070 -- renamed generic package, in which case we want to retrieve
7071 -- the original generic in order to traverse its formal part.
7073 if Present (Renamed_Entity (Actual)) then
7074 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
7075 else
7076 Gen_Decl := Unit_Declaration_Node (Actual);
7077 end if;
7079 Formals := Generic_Formal_Declarations (Gen_Decl);
7081 if Present (Formals) then
7082 F_Node := First_Non_Pragma (Formals);
7083 else
7084 F_Node := Empty;
7085 end if;
7087 while Present (A_Ent)
7088 and then Present (F_Node)
7089 and then A_Ent /= First_Private_Entity (Act_Pkg)
7090 loop
7091 F_Ent := Get_Formal_Entity (F_Node);
7093 if Present (F_Ent) then
7095 -- This is a formal of the original package. Record
7096 -- association and recurse.
7098 Find_Matching_Actual (F_Node, A_Ent);
7099 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
7100 Next_Entity (A_Ent);
7101 end if;
7103 Next_Non_Pragma (F_Node);
7104 end loop;
7105 end;
7106 end if;
7107 end Match_Formal_Entity;
7109 -----------------------
7110 -- Get_Formal_Entity --
7111 -----------------------
7113 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
7114 Kind : constant Node_Kind := Nkind (Original_Node (N));
7115 begin
7116 case Kind is
7117 when N_Formal_Object_Declaration =>
7118 return Defining_Identifier (N);
7120 when N_Formal_Type_Declaration =>
7121 return Defining_Identifier (N);
7123 when N_Formal_Subprogram_Declaration =>
7124 return Defining_Unit_Name (Specification (N));
7126 when N_Formal_Package_Declaration =>
7127 return Defining_Identifier (Original_Node (N));
7129 when N_Generic_Package_Declaration =>
7130 return Defining_Identifier (Original_Node (N));
7132 -- All other declarations are introduced by semantic analysis
7133 -- and have no match in the actual.
7135 when others =>
7136 return Empty;
7137 end case;
7138 end Get_Formal_Entity;
7140 --------------------
7141 -- Is_Instance_Of --
7142 --------------------
7144 function Is_Instance_Of
7145 (Act_Spec : Entity_Id;
7146 Gen_Anc : Entity_Id) return Boolean
7148 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
7150 begin
7151 if No (Gen_Par) then
7152 return False;
7154 -- Simplest case: the generic parent of the actual is the formal
7156 elsif Gen_Par = Gen_Anc then
7157 return True;
7159 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
7160 return False;
7162 -- The actual may be obtained through several instantiations. Its
7163 -- scope must itself be an instance of a generic declared in the
7164 -- same scope as the formal. Any other case is detected above.
7166 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
7167 return False;
7169 else
7170 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
7171 end if;
7172 end Is_Instance_Of;
7174 ------------------
7175 -- Map_Entities --
7176 ------------------
7178 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id) is
7179 E1 : Entity_Id;
7180 E2 : Entity_Id;
7182 begin
7183 Set_Instance_Of (Form, Act);
7185 -- Traverse formal and actual package to map the corresponding
7186 -- entities. We skip over internal entities that may be generated
7187 -- during semantic analysis, and find the matching entities by
7188 -- name, given that they must appear in the same order.
7190 E1 := First_Entity (Form);
7191 E2 := First_Entity (Act);
7192 while Present (E1)
7193 and then E1 /= First_Private_Entity (Form)
7194 loop
7195 -- Could this test be a single condition???
7196 -- Seems like it could, and isn't FPE (Form) a constant anyway???
7198 if not Is_Internal (E1)
7199 and then Present (Parent (E1))
7200 and then not Is_Class_Wide_Type (E1)
7201 and then not Is_Internal_Name (Chars (E1))
7202 then
7203 while Present (E2)
7204 and then Chars (E2) /= Chars (E1)
7205 loop
7206 Next_Entity (E2);
7207 end loop;
7209 if No (E2) then
7210 exit;
7211 else
7212 Set_Instance_Of (E1, E2);
7214 if Is_Type (E1)
7215 and then Is_Tagged_Type (E2)
7216 then
7217 Set_Instance_Of
7218 (Class_Wide_Type (E1), Class_Wide_Type (E2));
7219 end if;
7221 if Ekind (E1) = E_Package
7222 and then No (Renamed_Object (E1))
7223 then
7224 Map_Entities (E1, E2);
7225 end if;
7226 end if;
7227 end if;
7229 Next_Entity (E1);
7230 end loop;
7231 end Map_Entities;
7233 ---------------------------
7234 -- Process_Nested_Formal --
7235 ---------------------------
7237 procedure Process_Nested_Formal (Formal : Entity_Id) is
7238 Ent : Entity_Id;
7240 begin
7241 if Present (Associated_Formal_Package (Formal))
7242 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
7243 then
7244 Ent := First_Entity (Formal);
7245 while Present (Ent) loop
7246 Set_Is_Hidden (Ent, False);
7247 Set_Is_Visible_Formal (Ent);
7248 Set_Is_Potentially_Use_Visible
7249 (Ent, Is_Potentially_Use_Visible (Formal));
7251 if Ekind (Ent) = E_Package then
7252 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
7253 Process_Nested_Formal (Ent);
7254 end if;
7256 Next_Entity (Ent);
7257 end loop;
7258 end if;
7259 end Process_Nested_Formal;
7261 -- Start of processing for Instantiate_Formal_Package
7263 begin
7264 Analyze (Actual);
7266 if not Is_Entity_Name (Actual)
7267 or else Ekind (Entity (Actual)) /= E_Package
7268 then
7269 Error_Msg_N
7270 ("expect package instance to instantiate formal", Actual);
7271 Abandon_Instantiation (Actual);
7272 raise Program_Error;
7274 else
7275 Actual_Pack := Entity (Actual);
7276 Set_Is_Instantiated (Actual_Pack);
7278 -- The actual may be a renamed package, or an outer generic
7279 -- formal package whose instantiation is converted into a renaming.
7281 if Present (Renamed_Object (Actual_Pack)) then
7282 Actual_Pack := Renamed_Object (Actual_Pack);
7283 end if;
7285 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
7286 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
7287 Formal_Pack := Defining_Identifier (Analyzed_Formal);
7288 else
7289 Gen_Parent :=
7290 Generic_Parent (Specification (Analyzed_Formal));
7291 Formal_Pack :=
7292 Defining_Unit_Name (Specification (Analyzed_Formal));
7293 end if;
7295 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
7296 Parent_Spec := Specification (Unit_Declaration_Node (Actual_Pack));
7297 else
7298 Parent_Spec := Parent (Actual_Pack);
7299 end if;
7301 if Gen_Parent = Any_Id then
7302 Error_Msg_N
7303 ("previous error in declaration of formal package", Actual);
7304 Abandon_Instantiation (Actual);
7306 elsif
7307 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
7308 then
7309 null;
7311 else
7312 Error_Msg_NE
7313 ("actual parameter must be instance of&", Actual, Gen_Parent);
7314 Abandon_Instantiation (Actual);
7315 end if;
7317 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
7318 Map_Entities (Formal_Pack, Actual_Pack);
7320 Nod :=
7321 Make_Package_Renaming_Declaration (Loc,
7322 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
7323 Name => New_Reference_To (Actual_Pack, Loc));
7325 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
7326 Defining_Identifier (Formal));
7327 Decls := New_List (Nod);
7329 -- If the formal F has a box, then the generic declarations are
7330 -- visible in the generic G. In an instance of G, the corresponding
7331 -- entities in the actual for F (which are the actuals for the
7332 -- instantiation of the generic that F denotes) must also be made
7333 -- visible for analysis of the current instance. On exit from the
7334 -- current instance, those entities are made private again. If the
7335 -- actual is currently in use, these entities are also use-visible.
7337 -- The loop through the actual entities also steps through the
7338 -- formal entities and enters associations from formals to
7339 -- actuals into the renaming map. This is necessary to properly
7340 -- handle checking of actual parameter associations for later
7341 -- formals that depend on actuals declared in the formal package.
7343 -- In Ada 2005, partial parametrization requires that we make
7344 -- visible the actuals corresponding to formals that were defaulted
7345 -- in the formal package. There formals are identified because they
7346 -- remain formal generics within the formal package, rather than
7347 -- being renamings of the actuals supplied.
7349 declare
7350 Gen_Decl : constant Node_Id :=
7351 Unit_Declaration_Node (Gen_Parent);
7352 Formals : constant List_Id :=
7353 Generic_Formal_Declarations (Gen_Decl);
7354 Actual_Ent : Entity_Id;
7355 Formal_Node : Node_Id;
7356 Formal_Ent : Entity_Id;
7358 begin
7359 if Present (Formals) then
7360 Formal_Node := First_Non_Pragma (Formals);
7361 else
7362 Formal_Node := Empty;
7363 end if;
7365 Actual_Ent := First_Entity (Actual_Pack);
7366 while Present (Actual_Ent)
7367 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
7368 loop
7369 if Present (Formal_Node) then
7370 Formal_Ent := Get_Formal_Entity (Formal_Node);
7372 if Present (Formal_Ent) then
7373 Find_Matching_Actual (Formal_Node, Actual_Ent);
7374 Match_Formal_Entity
7375 (Formal_Node, Formal_Ent, Actual_Ent);
7377 if Box_Present (Formal)
7378 or else
7379 (Present (Formal_Node)
7380 and then Is_Generic_Formal (Formal_Ent))
7381 then
7382 -- This may make too many formal entities visible,
7383 -- but it's hard to build an example that exposes
7384 -- this excess visibility. If a reference in the
7385 -- generic resolved to a global variable then the
7386 -- extra visibility in an instance does not affect
7387 -- the captured entity. If the reference resolved
7388 -- to a local entity it will resolve again in the
7389 -- instance. Nevertheless, we should build tests
7390 -- to make sure that hidden entities in the generic
7391 -- remain hidden in the instance.
7393 Set_Is_Hidden (Actual_Ent, False);
7394 Set_Is_Visible_Formal (Actual_Ent);
7395 Set_Is_Potentially_Use_Visible
7396 (Actual_Ent, In_Use (Actual_Pack));
7398 if Ekind (Actual_Ent) = E_Package then
7399 Process_Nested_Formal (Actual_Ent);
7400 end if;
7401 end if;
7402 end if;
7404 Next_Non_Pragma (Formal_Node);
7406 else
7407 -- No further formals to match, but the generic
7408 -- part may contain inherited operation that are
7409 -- not hidden in the enclosing instance.
7411 Next_Entity (Actual_Ent);
7412 end if;
7414 end loop;
7416 -- Inherited subprograms generated by formal derived types are
7417 -- also visible if the types are.
7419 Actual_Ent := First_Entity (Actual_Pack);
7420 while Present (Actual_Ent)
7421 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
7422 loop
7423 if Is_Overloadable (Actual_Ent)
7424 and then
7425 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
7426 and then
7427 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
7428 then
7429 Set_Is_Hidden (Actual_Ent, False);
7430 Set_Is_Potentially_Use_Visible
7431 (Actual_Ent, In_Use (Actual_Pack));
7432 end if;
7434 Next_Entity (Actual_Ent);
7435 end loop;
7436 end;
7438 -- If the formal is not declared with a box, reanalyze it as
7439 -- an abbreviated instantiation, to verify the matching rules
7440 -- of 12.7. The actual checks are performed after the generic
7441 -- associations have been analyzed, to guarantee the same
7442 -- visibility for this instantiation and for the actuals.
7444 -- In Ada 2005, the generic associations for the formal can include
7445 -- defaulted parameters. These are ignored during check. This
7446 -- internal instantiation is removed from the tree after conformance
7447 -- checking, because it contains formal declarations for those
7448 -- defaulted parameters, and those should not reach the back-end.
7450 if not Box_Present (Formal) then
7451 declare
7452 I_Pack : constant Entity_Id :=
7453 Make_Defining_Identifier (Sloc (Actual),
7454 Chars => New_Internal_Name ('P'));
7456 begin
7457 Set_Is_Internal (I_Pack);
7459 Append_To (Decls,
7460 Make_Package_Instantiation (Sloc (Actual),
7461 Defining_Unit_Name => I_Pack,
7462 Name =>
7463 New_Occurrence_Of
7464 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
7465 Generic_Associations =>
7466 Generic_Associations (Formal)));
7467 end;
7468 end if;
7470 return Decls;
7471 end if;
7472 end Instantiate_Formal_Package;
7474 -----------------------------------
7475 -- Instantiate_Formal_Subprogram --
7476 -----------------------------------
7478 function Instantiate_Formal_Subprogram
7479 (Formal : Node_Id;
7480 Actual : Node_Id;
7481 Analyzed_Formal : Node_Id) return Node_Id
7483 Loc : Source_Ptr;
7484 Formal_Sub : constant Entity_Id :=
7485 Defining_Unit_Name (Specification (Formal));
7486 Analyzed_S : constant Entity_Id :=
7487 Defining_Unit_Name (Specification (Analyzed_Formal));
7488 Decl_Node : Node_Id;
7489 Nam : Node_Id;
7490 New_Spec : Node_Id;
7492 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
7493 -- If the generic is a child unit, the parent has been installed on the
7494 -- scope stack, but a default subprogram cannot resolve to something on
7495 -- the parent because that parent is not really part of the visible
7496 -- context (it is there to resolve explicit local entities). If the
7497 -- default has resolved in this way, we remove the entity from
7498 -- immediate visibility and analyze the node again to emit an error
7499 -- message or find another visible candidate.
7501 procedure Valid_Actual_Subprogram (Act : Node_Id);
7502 -- Perform legality check and raise exception on failure
7504 -----------------------
7505 -- From_Parent_Scope --
7506 -----------------------
7508 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
7509 Gen_Scope : Node_Id := Scope (Analyzed_S);
7511 begin
7512 while Present (Gen_Scope)
7513 and then Is_Child_Unit (Gen_Scope)
7514 loop
7515 if Scope (Subp) = Scope (Gen_Scope) then
7516 return True;
7517 end if;
7519 Gen_Scope := Scope (Gen_Scope);
7520 end loop;
7522 return False;
7523 end From_Parent_Scope;
7525 -----------------------------
7526 -- Valid_Actual_Subprogram --
7527 -----------------------------
7529 procedure Valid_Actual_Subprogram (Act : Node_Id) is
7530 Act_E : Entity_Id := Empty;
7532 begin
7533 if Is_Entity_Name (Act) then
7534 Act_E := Entity (Act);
7535 elsif Nkind (Act) = N_Selected_Component
7536 and then Is_Entity_Name (Selector_Name (Act))
7537 then
7538 Act_E := Entity (Selector_Name (Act));
7539 end if;
7541 if (Present (Act_E) and then Is_Overloadable (Act_E))
7542 or else Nkind (Act) = N_Attribute_Reference
7543 or else Nkind (Act) = N_Indexed_Component
7544 or else Nkind (Act) = N_Character_Literal
7545 or else Nkind (Act) = N_Explicit_Dereference
7546 then
7547 return;
7548 end if;
7550 Error_Msg_NE
7551 ("expect subprogram or entry name in instantiation of&",
7552 Instantiation_Node, Formal_Sub);
7553 Abandon_Instantiation (Instantiation_Node);
7555 end Valid_Actual_Subprogram;
7557 -- Start of processing for Instantiate_Formal_Subprogram
7559 begin
7560 New_Spec := New_Copy_Tree (Specification (Formal));
7562 -- The tree copy has created the proper instantiation sloc for the
7563 -- new specification. Use this location for all other constructed
7564 -- declarations.
7566 Loc := Sloc (Defining_Unit_Name (New_Spec));
7568 -- Create new entity for the actual (New_Copy_Tree does not)
7570 Set_Defining_Unit_Name
7571 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
7573 -- Create new entities for the each of the formals in the
7574 -- specification of the renaming declaration built for the actual.
7576 if Present (Parameter_Specifications (New_Spec)) then
7577 declare
7578 F : Node_Id;
7579 begin
7580 F := First (Parameter_Specifications (New_Spec));
7581 while Present (F) loop
7582 Set_Defining_Identifier (F,
7583 Make_Defining_Identifier (Loc,
7584 Chars => Chars (Defining_Identifier (F))));
7585 Next (F);
7586 end loop;
7587 end;
7588 end if;
7590 -- Find entity of actual. If the actual is an attribute reference, it
7591 -- cannot be resolved here (its formal is missing) but is handled
7592 -- instead in Attribute_Renaming. If the actual is overloaded, it is
7593 -- fully resolved subsequently, when the renaming declaration for the
7594 -- formal is analyzed. If it is an explicit dereference, resolve the
7595 -- prefix but not the actual itself, to prevent interpretation as a
7596 -- call.
7598 if Present (Actual) then
7599 Loc := Sloc (Actual);
7600 Set_Sloc (New_Spec, Loc);
7602 if Nkind (Actual) = N_Operator_Symbol then
7603 Find_Direct_Name (Actual);
7605 elsif Nkind (Actual) = N_Explicit_Dereference then
7606 Analyze (Prefix (Actual));
7608 elsif Nkind (Actual) /= N_Attribute_Reference then
7609 Analyze (Actual);
7610 end if;
7612 Valid_Actual_Subprogram (Actual);
7613 Nam := Actual;
7615 elsif Present (Default_Name (Formal)) then
7616 if Nkind (Default_Name (Formal)) /= N_Attribute_Reference
7617 and then Nkind (Default_Name (Formal)) /= N_Selected_Component
7618 and then Nkind (Default_Name (Formal)) /= N_Indexed_Component
7619 and then Nkind (Default_Name (Formal)) /= N_Character_Literal
7620 and then Present (Entity (Default_Name (Formal)))
7621 then
7622 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
7623 else
7624 Nam := New_Copy (Default_Name (Formal));
7625 Set_Sloc (Nam, Loc);
7626 end if;
7628 elsif Box_Present (Formal) then
7630 -- Actual is resolved at the point of instantiation. Create
7631 -- an identifier or operator with the same name as the formal.
7633 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
7634 Nam := Make_Operator_Symbol (Loc,
7635 Chars => Chars (Formal_Sub),
7636 Strval => No_String);
7637 else
7638 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
7639 end if;
7641 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
7642 and then Null_Present (Specification (Formal))
7643 then
7644 -- Generate null body for procedure, for use in the instance
7646 Decl_Node :=
7647 Make_Subprogram_Body (Loc,
7648 Specification => New_Spec,
7649 Declarations => New_List,
7650 Handled_Statement_Sequence =>
7651 Make_Handled_Sequence_Of_Statements (Loc,
7652 Statements => New_List (Make_Null_Statement (Loc))));
7654 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
7655 return Decl_Node;
7657 else
7658 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
7659 Error_Msg_NE
7660 ("missing actual&", Instantiation_Node, Formal_Sub);
7661 Error_Msg_NE
7662 ("\in instantiation of & declared#",
7663 Instantiation_Node, Scope (Analyzed_S));
7664 Abandon_Instantiation (Instantiation_Node);
7665 end if;
7667 Decl_Node :=
7668 Make_Subprogram_Renaming_Declaration (Loc,
7669 Specification => New_Spec,
7670 Name => Nam);
7672 -- If we do not have an actual and the formal specified <> then
7673 -- set to get proper default.
7675 if No (Actual) and then Box_Present (Formal) then
7676 Set_From_Default (Decl_Node);
7677 end if;
7679 -- Gather possible interpretations for the actual before analyzing the
7680 -- instance. If overloaded, it will be resolved when analyzing the
7681 -- renaming declaration.
7683 if Box_Present (Formal)
7684 and then No (Actual)
7685 then
7686 Analyze (Nam);
7688 if Is_Child_Unit (Scope (Analyzed_S))
7689 and then Present (Entity (Nam))
7690 then
7691 if not Is_Overloaded (Nam) then
7693 if From_Parent_Scope (Entity (Nam)) then
7694 Set_Is_Immediately_Visible (Entity (Nam), False);
7695 Set_Entity (Nam, Empty);
7696 Set_Etype (Nam, Empty);
7698 Analyze (Nam);
7700 Set_Is_Immediately_Visible (Entity (Nam));
7701 end if;
7703 else
7704 declare
7705 I : Interp_Index;
7706 It : Interp;
7708 begin
7709 Get_First_Interp (Nam, I, It);
7711 while Present (It.Nam) loop
7712 if From_Parent_Scope (It.Nam) then
7713 Remove_Interp (I);
7714 end if;
7716 Get_Next_Interp (I, It);
7717 end loop;
7718 end;
7719 end if;
7720 end if;
7721 end if;
7723 -- The generic instantiation freezes the actual. This can only be
7724 -- done once the actual is resolved, in the analysis of the renaming
7725 -- declaration. To make the formal subprogram entity available, we set
7726 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
7727 -- This is also needed in Analyze_Subprogram_Renaming for the processing
7728 -- of formal abstract subprograms.
7730 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
7732 -- We cannot analyze the renaming declaration, and thus find the
7733 -- actual, until the all the actuals are assembled in the instance.
7734 -- For subsequent checks of other actuals, indicate the node that
7735 -- will hold the instance of this formal.
7737 Set_Instance_Of (Analyzed_S, Nam);
7739 if Nkind (Actual) = N_Selected_Component
7740 and then Is_Task_Type (Etype (Prefix (Actual)))
7741 and then not Is_Frozen (Etype (Prefix (Actual)))
7742 then
7743 -- The renaming declaration will create a body, which must appear
7744 -- outside of the instantiation, We move the renaming declaration
7745 -- out of the instance, and create an additional renaming inside,
7746 -- to prevent freezing anomalies.
7748 declare
7749 Anon_Id : constant Entity_Id :=
7750 Make_Defining_Identifier
7751 (Loc, New_Internal_Name ('E'));
7752 begin
7753 Set_Defining_Unit_Name (New_Spec, Anon_Id);
7754 Insert_Before (Instantiation_Node, Decl_Node);
7755 Analyze (Decl_Node);
7757 -- Now create renaming within the instance
7759 Decl_Node :=
7760 Make_Subprogram_Renaming_Declaration (Loc,
7761 Specification => New_Copy_Tree (New_Spec),
7762 Name => New_Occurrence_Of (Anon_Id, Loc));
7764 Set_Defining_Unit_Name (Specification (Decl_Node),
7765 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
7766 end;
7767 end if;
7769 return Decl_Node;
7770 end Instantiate_Formal_Subprogram;
7772 ------------------------
7773 -- Instantiate_Object --
7774 ------------------------
7776 function Instantiate_Object
7777 (Formal : Node_Id;
7778 Actual : Node_Id;
7779 Analyzed_Formal : Node_Id) return List_Id
7781 Acc_Def : Node_Id := Empty;
7782 Act_Assoc : constant Node_Id := Parent (Actual);
7783 Actual_Decl : Node_Id := Empty;
7784 Formal_Id : constant Entity_Id := Defining_Identifier (Formal);
7785 Decl_Node : Node_Id;
7786 Def : Node_Id;
7787 Ftyp : Entity_Id;
7788 List : constant List_Id := New_List;
7789 Loc : constant Source_Ptr := Sloc (Actual);
7790 Orig_Ftyp : constant Entity_Id :=
7791 Etype (Defining_Identifier (Analyzed_Formal));
7792 Subt_Decl : Node_Id := Empty;
7793 Subt_Mark : Node_Id := Empty;
7795 begin
7796 if Present (Subtype_Mark (Formal)) then
7797 Subt_Mark := Subtype_Mark (Formal);
7798 else
7799 Check_Access_Definition (Formal);
7800 Acc_Def := Access_Definition (Formal);
7801 end if;
7803 -- Sloc for error message on missing actual
7805 Error_Msg_Sloc := Sloc (Scope (Defining_Identifier (Analyzed_Formal)));
7807 if Get_Instance_Of (Formal_Id) /= Formal_Id then
7808 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
7809 end if;
7811 Set_Parent (List, Parent (Actual));
7813 -- OUT present
7815 if Out_Present (Formal) then
7817 -- An IN OUT generic actual must be a name. The instantiation is a
7818 -- renaming declaration. The actual is the name being renamed. We
7819 -- use the actual directly, rather than a copy, because it is not
7820 -- used further in the list of actuals, and because a copy or a use
7821 -- of relocate_node is incorrect if the instance is nested within a
7822 -- generic. In order to simplify ASIS searches, the Generic_Parent
7823 -- field links the declaration to the generic association.
7825 if No (Actual) then
7826 Error_Msg_NE
7827 ("missing actual&",
7828 Instantiation_Node, Formal_Id);
7829 Error_Msg_NE
7830 ("\in instantiation of & declared#",
7831 Instantiation_Node,
7832 Scope (Defining_Identifier (Analyzed_Formal)));
7833 Abandon_Instantiation (Instantiation_Node);
7834 end if;
7836 if Present (Subt_Mark) then
7837 Decl_Node :=
7838 Make_Object_Renaming_Declaration (Loc,
7839 Defining_Identifier => New_Copy (Formal_Id),
7840 Subtype_Mark => New_Copy_Tree (Subt_Mark),
7841 Name => Actual);
7843 else pragma Assert (Present (Acc_Def));
7844 Decl_Node :=
7845 Make_Object_Renaming_Declaration (Loc,
7846 Defining_Identifier => New_Copy (Formal_Id),
7847 Access_Definition => New_Copy_Tree (Acc_Def),
7848 Name => Actual);
7849 end if;
7851 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
7853 -- The analysis of the actual may produce insert_action nodes, so
7854 -- the declaration must have a context in which to attach them.
7856 Append (Decl_Node, List);
7857 Analyze (Actual);
7859 -- Return if the analysis of the actual reported some error
7861 if Etype (Actual) = Any_Type then
7862 return List;
7863 end if;
7865 -- This check is performed here because Analyze_Object_Renaming
7866 -- will not check it when Comes_From_Source is False. Note
7867 -- though that the check for the actual being the name of an
7868 -- object will be performed in Analyze_Object_Renaming.
7870 if Is_Object_Reference (Actual)
7871 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
7872 then
7873 Error_Msg_N
7874 ("illegal discriminant-dependent component for in out parameter",
7875 Actual);
7876 end if;
7878 -- The actual has to be resolved in order to check that it is
7879 -- a variable (due to cases such as F(1), where F returns
7880 -- access to an array, and for overloaded prefixes).
7882 Ftyp :=
7883 Get_Instance_Of (Etype (Defining_Identifier (Analyzed_Formal)));
7885 if Is_Private_Type (Ftyp)
7886 and then not Is_Private_Type (Etype (Actual))
7887 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
7888 or else Base_Type (Etype (Actual)) = Ftyp)
7889 then
7890 -- If the actual has the type of the full view of the formal,
7891 -- or else a non-private subtype of the formal, then
7892 -- the visibility of the formal type has changed. Add to the
7893 -- actuals a subtype declaration that will force the exchange
7894 -- of views in the body of the instance as well.
7896 Subt_Decl :=
7897 Make_Subtype_Declaration (Loc,
7898 Defining_Identifier =>
7899 Make_Defining_Identifier (Loc, New_Internal_Name ('P')),
7900 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
7902 Prepend (Subt_Decl, List);
7904 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
7905 Exchange_Declarations (Ftyp);
7906 end if;
7908 Resolve (Actual, Ftyp);
7910 if not Is_Variable (Actual) or else Paren_Count (Actual) > 0 then
7911 Error_Msg_NE
7912 ("actual for& must be a variable", Actual, Formal_Id);
7914 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
7916 -- Ada 2005 (AI-423): For a generic formal object of mode in
7917 -- out, the type of the actual shall resolve to a specific
7918 -- anonymous access type.
7920 if Ada_Version < Ada_05
7921 or else
7922 Ekind (Base_Type (Ftyp)) /=
7923 E_Anonymous_Access_Type
7924 or else
7925 Ekind (Base_Type (Etype (Actual))) /=
7926 E_Anonymous_Access_Type
7927 then
7928 Error_Msg_NE ("type of actual does not match type of&",
7929 Actual, Formal_Id);
7930 end if;
7931 end if;
7933 Note_Possible_Modification (Actual);
7935 -- Check for instantiation of atomic/volatile actual for
7936 -- non-atomic/volatile formal (RM C.6 (12)).
7938 if Is_Atomic_Object (Actual)
7939 and then not Is_Atomic (Orig_Ftyp)
7940 then
7941 Error_Msg_N
7942 ("cannot instantiate non-atomic formal object " &
7943 "with atomic actual", Actual);
7945 elsif Is_Volatile_Object (Actual)
7946 and then not Is_Volatile (Orig_Ftyp)
7947 then
7948 Error_Msg_N
7949 ("cannot instantiate non-volatile formal object " &
7950 "with volatile actual", Actual);
7951 end if;
7953 -- OUT not present
7955 else
7956 -- The instantiation of a generic formal in-parameter is a
7957 -- constant declaration. The actual is the expression for
7958 -- that declaration.
7960 if Present (Actual) then
7961 if Present (Subt_Mark) then
7962 Def := Subt_Mark;
7963 else pragma Assert (Present (Acc_Def));
7964 Def := Acc_Def;
7965 end if;
7967 Decl_Node :=
7968 Make_Object_Declaration (Loc,
7969 Defining_Identifier => New_Copy (Formal_Id),
7970 Constant_Present => True,
7971 Object_Definition => New_Copy_Tree (Def),
7972 Expression => Actual);
7974 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
7976 -- A generic formal object of a tagged type is defined
7977 -- to be aliased so the new constant must also be treated
7978 -- as aliased.
7980 if Is_Tagged_Type
7981 (Etype (Defining_Identifier (Analyzed_Formal)))
7982 then
7983 Set_Aliased_Present (Decl_Node);
7984 end if;
7986 Append (Decl_Node, List);
7988 -- No need to repeat (pre-)analysis of some expression nodes
7989 -- already handled in Pre_Analyze_Actuals.
7991 if Nkind (Actual) /= N_Allocator then
7992 Analyze (Actual);
7994 -- Return if the analysis of the actual reported some error
7996 if Etype (Actual) = Any_Type then
7997 return List;
7998 end if;
7999 end if;
8001 declare
8002 Typ : constant Entity_Id :=
8003 Get_Instance_Of
8004 (Etype (Defining_Identifier (Analyzed_Formal)));
8006 begin
8007 Freeze_Before (Instantiation_Node, Typ);
8009 -- If the actual is an aggregate, perform name resolution on
8010 -- its components (the analysis of an aggregate does not do
8011 -- it) to capture local names that may be hidden if the
8012 -- generic is a child unit.
8014 if Nkind (Actual) = N_Aggregate then
8015 Pre_Analyze_And_Resolve (Actual, Typ);
8016 end if;
8017 end;
8019 elsif Present (Default_Expression (Formal)) then
8021 -- Use default to construct declaration
8023 if Present (Subt_Mark) then
8024 Def := Subt_Mark;
8025 else pragma Assert (Present (Acc_Def));
8026 Def := Acc_Def;
8027 end if;
8029 Decl_Node :=
8030 Make_Object_Declaration (Sloc (Formal),
8031 Defining_Identifier => New_Copy (Formal_Id),
8032 Constant_Present => True,
8033 Object_Definition => New_Copy (Def),
8034 Expression => New_Copy_Tree (Default_Expression
8035 (Formal)));
8037 Append (Decl_Node, List);
8038 Set_Analyzed (Expression (Decl_Node), False);
8040 else
8041 Error_Msg_NE
8042 ("missing actual&",
8043 Instantiation_Node, Formal_Id);
8044 Error_Msg_NE ("\in instantiation of & declared#",
8045 Instantiation_Node,
8046 Scope (Defining_Identifier (Analyzed_Formal)));
8048 if Is_Scalar_Type
8049 (Etype (Defining_Identifier (Analyzed_Formal)))
8050 then
8051 -- Create dummy constant declaration so that instance can
8052 -- be analyzed, to minimize cascaded visibility errors.
8054 if Present (Subt_Mark) then
8055 Def := Subt_Mark;
8056 else pragma Assert (Present (Acc_Def));
8057 Def := Acc_Def;
8058 end if;
8060 Decl_Node :=
8061 Make_Object_Declaration (Loc,
8062 Defining_Identifier => New_Copy (Formal_Id),
8063 Constant_Present => True,
8064 Object_Definition => New_Copy (Def),
8065 Expression =>
8066 Make_Attribute_Reference (Sloc (Formal_Id),
8067 Attribute_Name => Name_First,
8068 Prefix => New_Copy (Def)));
8070 Append (Decl_Node, List);
8072 else
8073 Abandon_Instantiation (Instantiation_Node);
8074 end if;
8075 end if;
8076 end if;
8078 if Nkind (Actual) in N_Has_Entity then
8079 Actual_Decl := Parent (Entity (Actual));
8080 end if;
8082 -- Ada 2005 (AI-423): For a formal object declaration with a null
8083 -- exclusion or an access definition that has a null exclusion: If
8084 -- the actual matching the formal object declaration denotes a generic
8085 -- formal object of another generic unit G, and the instantiation
8086 -- containing the actual occurs within the body of G or within the
8087 -- body of a generic unit declared within the declarative region of G,
8088 -- then the declaration of the formal object of G shall have a null
8089 -- exclusion. Otherwise, the subtype of the actual matching the formal
8090 -- object declaration shall exclude null.
8092 if Ada_Version >= Ada_05
8093 and then Present (Actual_Decl)
8094 and then
8095 (Nkind (Actual_Decl) = N_Formal_Object_Declaration
8096 or else Nkind (Actual_Decl) = N_Object_Declaration)
8097 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
8098 and then Has_Null_Exclusion (Actual_Decl)
8099 and then not Has_Null_Exclusion (Analyzed_Formal)
8100 then
8101 Error_Msg_N ("null-exclusion required in formal object declaration",
8102 Analyzed_Formal);
8103 end if;
8105 return List;
8106 end Instantiate_Object;
8108 ------------------------------
8109 -- Instantiate_Package_Body --
8110 ------------------------------
8112 procedure Instantiate_Package_Body
8113 (Body_Info : Pending_Body_Info;
8114 Inlined_Body : Boolean := False)
8116 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
8117 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
8118 Loc : constant Source_Ptr := Sloc (Inst_Node);
8120 Gen_Id : constant Node_Id := Name (Inst_Node);
8121 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8122 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
8123 Act_Spec : constant Node_Id := Specification (Act_Decl);
8124 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
8126 Act_Body_Name : Node_Id;
8127 Gen_Body : Node_Id;
8128 Gen_Body_Id : Node_Id;
8129 Act_Body : Node_Id;
8130 Act_Body_Id : Entity_Id;
8132 Parent_Installed : Boolean := False;
8133 Save_Style_Check : constant Boolean := Style_Check;
8135 begin
8136 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8138 -- The instance body may already have been processed, as the parent
8139 -- of another instance that is inlined. (Load_Parent_Of_Generic).
8141 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
8142 return;
8143 end if;
8145 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
8147 if No (Gen_Body_Id) then
8148 Load_Parent_Of_Generic (Inst_Node, Specification (Gen_Decl));
8149 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8150 end if;
8152 -- Establish global variable for sloc adjustment and for error
8153 -- recovery.
8155 Instantiation_Node := Inst_Node;
8157 if Present (Gen_Body_Id) then
8158 Save_Env (Gen_Unit, Act_Decl_Id);
8159 Style_Check := False;
8160 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
8162 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
8164 Create_Instantiation_Source
8165 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
8167 Act_Body :=
8168 Copy_Generic_Node
8169 (Original_Node (Gen_Body), Empty, Instantiating => True);
8171 -- Build new name (possibly qualified) for body declaration
8173 Act_Body_Id := New_Copy (Act_Decl_Id);
8175 -- Some attributes of the spec entity are not inherited by the
8176 -- body entity.
8178 Set_Handler_Records (Act_Body_Id, No_List);
8180 if Nkind (Defining_Unit_Name (Act_Spec)) =
8181 N_Defining_Program_Unit_Name
8182 then
8183 Act_Body_Name :=
8184 Make_Defining_Program_Unit_Name (Loc,
8185 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
8186 Defining_Identifier => Act_Body_Id);
8187 else
8188 Act_Body_Name := Act_Body_Id;
8189 end if;
8191 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
8193 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
8194 Check_Generic_Actuals (Act_Decl_Id, False);
8196 -- If it is a child unit, make the parent instance (which is an
8197 -- instance of the parent of the generic) visible. The parent
8198 -- instance is the prefix of the name of the generic unit.
8200 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
8201 and then Nkind (Gen_Id) = N_Expanded_Name
8202 then
8203 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
8204 Parent_Installed := True;
8206 elsif Is_Child_Unit (Gen_Unit) then
8207 Install_Parent (Scope (Gen_Unit), In_Body => True);
8208 Parent_Installed := True;
8209 end if;
8211 -- If the instantiation is a library unit, and this is the main
8212 -- unit, then build the resulting compilation unit nodes for the
8213 -- instance. If this is a compilation unit but it is not the main
8214 -- unit, then it is the body of a unit in the context, that is being
8215 -- compiled because it is encloses some inlined unit or another
8216 -- generic unit being instantiated. In that case, this body is not
8217 -- part of the current compilation, and is not attached to the tree,
8218 -- but its parent must be set for analysis.
8220 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
8222 -- Replace instance node with body of instance, and create
8223 -- new node for corresponding instance declaration.
8225 Build_Instance_Compilation_Unit_Nodes
8226 (Inst_Node, Act_Body, Act_Decl);
8227 Analyze (Inst_Node);
8229 if Parent (Inst_Node) = Cunit (Main_Unit) then
8231 -- If the instance is a child unit itself, then set the
8232 -- scope of the expanded body to be the parent of the
8233 -- instantiation (ensuring that the fully qualified name
8234 -- will be generated for the elaboration subprogram).
8236 if Nkind (Defining_Unit_Name (Act_Spec)) =
8237 N_Defining_Program_Unit_Name
8238 then
8239 Set_Scope
8240 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
8241 end if;
8242 end if;
8244 -- Case where instantiation is not a library unit
8246 else
8247 -- If this is an early instantiation, i.e. appears textually
8248 -- before the corresponding body and must be elaborated first,
8249 -- indicate that the body instance is to be delayed.
8251 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
8253 -- Now analyze the body. We turn off all checks if this is
8254 -- an internal unit, since there is no reason to have checks
8255 -- on for any predefined run-time library code. All such
8256 -- code is designed to be compiled with checks off.
8258 -- Note that we do NOT apply this criterion to children of
8259 -- GNAT (or on VMS, children of DEC). The latter units must
8260 -- suppress checks explicitly if this is needed.
8262 if Is_Predefined_File_Name
8263 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
8264 then
8265 Analyze (Act_Body, Suppress => All_Checks);
8266 else
8267 Analyze (Act_Body);
8268 end if;
8269 end if;
8271 if not Generic_Separately_Compiled (Gen_Unit) then
8272 Inherit_Context (Gen_Body, Inst_Node);
8273 end if;
8275 -- Remove the parent instances if they have been placed on the
8276 -- scope stack to compile the body.
8278 if Parent_Installed then
8279 Remove_Parent (In_Body => True);
8280 end if;
8282 Restore_Private_Views (Act_Decl_Id);
8284 -- Remove the current unit from visibility if this is an instance
8285 -- that is not elaborated on the fly for inlining purposes.
8287 if not Inlined_Body then
8288 Set_Is_Immediately_Visible (Act_Decl_Id, False);
8289 end if;
8291 Restore_Env;
8292 Style_Check := Save_Style_Check;
8294 -- If we have no body, and the unit requires a body, then complain.
8295 -- This complaint is suppressed if we have detected other errors
8296 -- (since a common reason for missing the body is that it had errors).
8298 elsif Unit_Requires_Body (Gen_Unit) then
8299 if Serious_Errors_Detected = 0 then
8300 Error_Msg_NE
8301 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
8303 -- Don't attempt to perform any cleanup actions if some other
8304 -- error was aready detected, since this can cause blowups.
8306 else
8307 return;
8308 end if;
8310 -- Case of package that does not need a body
8312 else
8313 -- If the instantiation of the declaration is a library unit,
8314 -- rewrite the original package instantiation as a package
8315 -- declaration in the compilation unit node.
8317 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
8318 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
8319 Rewrite (Inst_Node, Act_Decl);
8321 -- Generate elaboration entity, in case spec has elaboration
8322 -- code. This cannot be done when the instance is analyzed,
8323 -- because it is not known yet whether the body exists.
8325 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
8326 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
8328 -- If the instantiation is not a library unit, then append the
8329 -- declaration to the list of implicitly generated entities.
8330 -- unless it is already a list member which means that it was
8331 -- already processed
8333 elsif not Is_List_Member (Act_Decl) then
8334 Mark_Rewrite_Insertion (Act_Decl);
8335 Insert_Before (Inst_Node, Act_Decl);
8336 end if;
8337 end if;
8339 Expander_Mode_Restore;
8340 end Instantiate_Package_Body;
8342 ---------------------------------
8343 -- Instantiate_Subprogram_Body --
8344 ---------------------------------
8346 procedure Instantiate_Subprogram_Body
8347 (Body_Info : Pending_Body_Info)
8349 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
8350 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
8351 Loc : constant Source_Ptr := Sloc (Inst_Node);
8352 Gen_Id : constant Node_Id := Name (Inst_Node);
8353 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8354 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
8355 Anon_Id : constant Entity_Id :=
8356 Defining_Unit_Name (Specification (Act_Decl));
8357 Pack_Id : constant Entity_Id :=
8358 Defining_Unit_Name (Parent (Act_Decl));
8359 Decls : List_Id;
8360 Gen_Body : Node_Id;
8361 Gen_Body_Id : Node_Id;
8362 Act_Body : Node_Id;
8363 Act_Body_Id : Entity_Id;
8364 Pack_Body : Node_Id;
8365 Prev_Formal : Entity_Id;
8366 Ret_Expr : Node_Id;
8367 Unit_Renaming : Node_Id;
8369 Parent_Installed : Boolean := False;
8370 Save_Style_Check : constant Boolean := Style_Check;
8372 begin
8373 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8375 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
8377 if No (Gen_Body_Id) then
8378 Load_Parent_Of_Generic (Inst_Node, Specification (Gen_Decl));
8379 Gen_Body_Id := Corresponding_Body (Gen_Decl);
8380 end if;
8382 Instantiation_Node := Inst_Node;
8384 if Present (Gen_Body_Id) then
8385 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
8387 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
8389 -- Either body is not present, or context is non-expanding, as
8390 -- when compiling a subunit. Mark the instance as completed, and
8391 -- diagnose a missing body when needed.
8393 if Expander_Active
8394 and then Operating_Mode = Generate_Code
8395 then
8396 Error_Msg_N
8397 ("missing proper body for instantiation", Gen_Body);
8398 end if;
8400 Set_Has_Completion (Anon_Id);
8401 return;
8402 end if;
8404 Save_Env (Gen_Unit, Anon_Id);
8405 Style_Check := False;
8406 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
8407 Create_Instantiation_Source
8408 (Inst_Node,
8409 Gen_Body_Id,
8410 False,
8411 S_Adjustment);
8413 Act_Body :=
8414 Copy_Generic_Node
8415 (Original_Node (Gen_Body), Empty, Instantiating => True);
8416 Act_Body_Id := Defining_Entity (Act_Body);
8417 Set_Chars (Act_Body_Id, Chars (Anon_Id));
8418 Set_Sloc (Act_Body_Id, Sloc (Defining_Entity (Inst_Node)));
8419 Set_Corresponding_Spec (Act_Body, Anon_Id);
8420 Set_Has_Completion (Anon_Id);
8421 Check_Generic_Actuals (Pack_Id, False);
8423 -- Generate a reference to link the visible subprogram instance to
8424 -- the the generic body, which for navigation purposes is the only
8425 -- available source for the instance.
8427 Generate_Reference
8428 (Related_Instance (Pack_Id),
8429 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
8431 -- If it is a child unit, make the parent instance (which is an
8432 -- instance of the parent of the generic) visible. The parent
8433 -- instance is the prefix of the name of the generic unit.
8435 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
8436 and then Nkind (Gen_Id) = N_Expanded_Name
8437 then
8438 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
8439 Parent_Installed := True;
8441 elsif Is_Child_Unit (Gen_Unit) then
8442 Install_Parent (Scope (Gen_Unit), In_Body => True);
8443 Parent_Installed := True;
8444 end if;
8446 -- Inside its body, a reference to the generic unit is a reference
8447 -- to the instance. The corresponding renaming is the first
8448 -- declaration in the body.
8450 Unit_Renaming :=
8451 Make_Subprogram_Renaming_Declaration (Loc,
8452 Specification =>
8453 Copy_Generic_Node (
8454 Specification (Original_Node (Gen_Body)),
8455 Empty,
8456 Instantiating => True),
8457 Name => New_Occurrence_Of (Anon_Id, Loc));
8459 -- If there is a formal subprogram with the same name as the
8460 -- unit itself, do not add this renaming declaration. This is
8461 -- a temporary fix for one ACVC test. ???
8463 Prev_Formal := First_Entity (Pack_Id);
8464 while Present (Prev_Formal) loop
8465 if Chars (Prev_Formal) = Chars (Gen_Unit)
8466 and then Is_Overloadable (Prev_Formal)
8467 then
8468 exit;
8469 end if;
8471 Next_Entity (Prev_Formal);
8472 end loop;
8474 if Present (Prev_Formal) then
8475 Decls := New_List (Act_Body);
8476 else
8477 Decls := New_List (Unit_Renaming, Act_Body);
8478 end if;
8480 -- The subprogram body is placed in the body of a dummy package
8481 -- body, whose spec contains the subprogram declaration as well
8482 -- as the renaming declarations for the generic parameters.
8484 Pack_Body := Make_Package_Body (Loc,
8485 Defining_Unit_Name => New_Copy (Pack_Id),
8486 Declarations => Decls);
8488 Set_Corresponding_Spec (Pack_Body, Pack_Id);
8490 -- If the instantiation is a library unit, then build resulting
8491 -- compilation unit nodes for the instance. The declaration of
8492 -- the enclosing package is the grandparent of the subprogram
8493 -- declaration. First replace the instantiation node as the unit
8494 -- of the corresponding compilation.
8496 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
8497 if Parent (Inst_Node) = Cunit (Main_Unit) then
8498 Set_Unit (Parent (Inst_Node), Inst_Node);
8499 Build_Instance_Compilation_Unit_Nodes
8500 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
8501 Analyze (Inst_Node);
8502 else
8503 Set_Parent (Pack_Body, Parent (Inst_Node));
8504 Analyze (Pack_Body);
8505 end if;
8507 else
8508 Insert_Before (Inst_Node, Pack_Body);
8509 Mark_Rewrite_Insertion (Pack_Body);
8510 Analyze (Pack_Body);
8512 if Expander_Active then
8513 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
8514 end if;
8515 end if;
8517 if not Generic_Separately_Compiled (Gen_Unit) then
8518 Inherit_Context (Gen_Body, Inst_Node);
8519 end if;
8521 Restore_Private_Views (Pack_Id, False);
8523 if Parent_Installed then
8524 Remove_Parent (In_Body => True);
8525 end if;
8527 Restore_Env;
8528 Style_Check := Save_Style_Check;
8530 -- Body not found. Error was emitted already. If there were no
8531 -- previous errors, this may be an instance whose scope is a premature
8532 -- instance. In that case we must insure that the (legal) program does
8533 -- raise program error if executed. We generate a subprogram body for
8534 -- this purpose. See DEC ac30vso.
8536 elsif Serious_Errors_Detected = 0
8537 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
8538 then
8539 if Ekind (Anon_Id) = E_Procedure then
8540 Act_Body :=
8541 Make_Subprogram_Body (Loc,
8542 Specification =>
8543 Make_Procedure_Specification (Loc,
8544 Defining_Unit_Name =>
8545 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
8546 Parameter_Specifications =>
8547 New_Copy_List
8548 (Parameter_Specifications (Parent (Anon_Id)))),
8550 Declarations => Empty_List,
8551 Handled_Statement_Sequence =>
8552 Make_Handled_Sequence_Of_Statements (Loc,
8553 Statements =>
8554 New_List (
8555 Make_Raise_Program_Error (Loc,
8556 Reason =>
8557 PE_Access_Before_Elaboration))));
8559 else
8560 Ret_Expr :=
8561 Make_Raise_Program_Error (Loc,
8562 Reason => PE_Access_Before_Elaboration);
8564 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
8565 Set_Analyzed (Ret_Expr);
8567 Act_Body :=
8568 Make_Subprogram_Body (Loc,
8569 Specification =>
8570 Make_Function_Specification (Loc,
8571 Defining_Unit_Name =>
8572 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
8573 Parameter_Specifications =>
8574 New_Copy_List
8575 (Parameter_Specifications (Parent (Anon_Id))),
8576 Result_Definition =>
8577 New_Occurrence_Of (Etype (Anon_Id), Loc)),
8579 Declarations => Empty_List,
8580 Handled_Statement_Sequence =>
8581 Make_Handled_Sequence_Of_Statements (Loc,
8582 Statements =>
8583 New_List (Make_Return_Statement (Loc, Ret_Expr))));
8584 end if;
8586 Pack_Body := Make_Package_Body (Loc,
8587 Defining_Unit_Name => New_Copy (Pack_Id),
8588 Declarations => New_List (Act_Body));
8590 Insert_After (Inst_Node, Pack_Body);
8591 Set_Corresponding_Spec (Pack_Body, Pack_Id);
8592 Analyze (Pack_Body);
8593 end if;
8595 Expander_Mode_Restore;
8596 end Instantiate_Subprogram_Body;
8598 ----------------------
8599 -- Instantiate_Type --
8600 ----------------------
8602 function Instantiate_Type
8603 (Formal : Node_Id;
8604 Actual : Node_Id;
8605 Analyzed_Formal : Node_Id;
8606 Actual_Decls : List_Id) return Node_Id
8608 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
8609 A_Gen_T : constant Entity_Id := Defining_Identifier (Analyzed_Formal);
8610 Ancestor : Entity_Id := Empty;
8611 Def : constant Node_Id := Formal_Type_Definition (Formal);
8612 Act_T : Entity_Id;
8613 Decl_Node : Node_Id;
8614 Loc : Source_Ptr;
8615 Subt : Entity_Id;
8617 procedure Validate_Array_Type_Instance;
8618 procedure Validate_Access_Subprogram_Instance;
8619 procedure Validate_Access_Type_Instance;
8620 procedure Validate_Derived_Type_Instance;
8621 procedure Validate_Derived_Interface_Type_Instance;
8622 procedure Validate_Interface_Type_Instance;
8623 procedure Validate_Private_Type_Instance;
8624 -- These procedures perform validation tests for the named case
8626 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
8627 -- Check that base types are the same and that the subtypes match
8628 -- statically. Used in several of the above.
8630 --------------------
8631 -- Subtypes_Match --
8632 --------------------
8634 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
8635 T : constant Entity_Id := Get_Instance_Of (Gen_T);
8637 begin
8638 return (Base_Type (T) = Base_Type (Act_T)
8639 and then Subtypes_Statically_Match (T, Act_T))
8641 or else (Is_Class_Wide_Type (Gen_T)
8642 and then Is_Class_Wide_Type (Act_T)
8643 and then
8644 Subtypes_Match
8645 (Get_Instance_Of (Root_Type (Gen_T)),
8646 Root_Type (Act_T)))
8648 or else
8649 ((Ekind (Gen_T) = E_Anonymous_Access_Subprogram_Type
8650 or else Ekind (Gen_T) = E_Anonymous_Access_Type)
8651 and then Ekind (Act_T) = Ekind (Gen_T)
8652 and then
8653 Subtypes_Statically_Match
8654 (Designated_Type (Gen_T), Designated_Type (Act_T)));
8655 end Subtypes_Match;
8657 -----------------------------------------
8658 -- Validate_Access_Subprogram_Instance --
8659 -----------------------------------------
8661 procedure Validate_Access_Subprogram_Instance is
8662 begin
8663 if not Is_Access_Type (Act_T)
8664 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
8665 then
8666 Error_Msg_NE
8667 ("expect access type in instantiation of &", Actual, Gen_T);
8668 Abandon_Instantiation (Actual);
8669 end if;
8671 Check_Mode_Conformant
8672 (Designated_Type (Act_T),
8673 Designated_Type (A_Gen_T),
8674 Actual,
8675 Get_Inst => True);
8677 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
8678 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
8679 Error_Msg_NE
8680 ("protected access type not allowed for formal &",
8681 Actual, Gen_T);
8682 end if;
8684 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
8685 Error_Msg_NE
8686 ("expect protected access type for formal &",
8687 Actual, Gen_T);
8688 end if;
8689 end Validate_Access_Subprogram_Instance;
8691 -----------------------------------
8692 -- Validate_Access_Type_Instance --
8693 -----------------------------------
8695 procedure Validate_Access_Type_Instance is
8696 Desig_Type : constant Entity_Id :=
8697 Find_Actual_Type
8698 (Designated_Type (A_Gen_T), Scope (A_Gen_T));
8700 begin
8701 if not Is_Access_Type (Act_T) then
8702 Error_Msg_NE
8703 ("expect access type in instantiation of &", Actual, Gen_T);
8704 Abandon_Instantiation (Actual);
8705 end if;
8707 if Is_Access_Constant (A_Gen_T) then
8708 if not Is_Access_Constant (Act_T) then
8709 Error_Msg_N
8710 ("actual type must be access-to-constant type", Actual);
8711 Abandon_Instantiation (Actual);
8712 end if;
8713 else
8714 if Is_Access_Constant (Act_T) then
8715 Error_Msg_N
8716 ("actual type must be access-to-variable type", Actual);
8717 Abandon_Instantiation (Actual);
8719 elsif Ekind (A_Gen_T) = E_General_Access_Type
8720 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
8721 then
8722 Error_Msg_N ("actual must be general access type!", Actual);
8723 Error_Msg_NE ("add ALL to }!", Actual, Act_T);
8724 Abandon_Instantiation (Actual);
8725 end if;
8726 end if;
8728 -- The designated subtypes, that is to say the subtypes introduced
8729 -- by an access type declaration (and not by a subtype declaration)
8730 -- must match.
8732 if not Subtypes_Match
8733 (Desig_Type, Designated_Type (Base_Type (Act_T)))
8734 then
8735 Error_Msg_NE
8736 ("designated type of actual does not match that of formal &",
8737 Actual, Gen_T);
8738 Abandon_Instantiation (Actual);
8740 elsif Is_Access_Type (Designated_Type (Act_T))
8741 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
8743 Is_Constrained (Designated_Type (Desig_Type))
8744 then
8745 Error_Msg_NE
8746 ("designated type of actual does not match that of formal &",
8747 Actual, Gen_T);
8748 Abandon_Instantiation (Actual);
8749 end if;
8750 end Validate_Access_Type_Instance;
8752 ----------------------------------
8753 -- Validate_Array_Type_Instance --
8754 ----------------------------------
8756 procedure Validate_Array_Type_Instance is
8757 I1 : Node_Id;
8758 I2 : Node_Id;
8759 T2 : Entity_Id;
8761 function Formal_Dimensions return Int;
8762 -- Count number of dimensions in array type formal
8764 -----------------------
8765 -- Formal_Dimensions --
8766 -----------------------
8768 function Formal_Dimensions return Int is
8769 Num : Int := 0;
8770 Index : Node_Id;
8772 begin
8773 if Nkind (Def) = N_Constrained_Array_Definition then
8774 Index := First (Discrete_Subtype_Definitions (Def));
8775 else
8776 Index := First (Subtype_Marks (Def));
8777 end if;
8779 while Present (Index) loop
8780 Num := Num + 1;
8781 Next_Index (Index);
8782 end loop;
8784 return Num;
8785 end Formal_Dimensions;
8787 -- Start of processing for Validate_Array_Type_Instance
8789 begin
8790 if not Is_Array_Type (Act_T) then
8791 Error_Msg_NE
8792 ("expect array type in instantiation of &", Actual, Gen_T);
8793 Abandon_Instantiation (Actual);
8795 elsif Nkind (Def) = N_Constrained_Array_Definition then
8796 if not (Is_Constrained (Act_T)) then
8797 Error_Msg_NE
8798 ("expect constrained array in instantiation of &",
8799 Actual, Gen_T);
8800 Abandon_Instantiation (Actual);
8801 end if;
8803 else
8804 if Is_Constrained (Act_T) then
8805 Error_Msg_NE
8806 ("expect unconstrained array in instantiation of &",
8807 Actual, Gen_T);
8808 Abandon_Instantiation (Actual);
8809 end if;
8810 end if;
8812 if Formal_Dimensions /= Number_Dimensions (Act_T) then
8813 Error_Msg_NE
8814 ("dimensions of actual do not match formal &", Actual, Gen_T);
8815 Abandon_Instantiation (Actual);
8816 end if;
8818 I1 := First_Index (A_Gen_T);
8819 I2 := First_Index (Act_T);
8820 for J in 1 .. Formal_Dimensions loop
8822 -- If the indices of the actual were given by a subtype_mark,
8823 -- the index was transformed into a range attribute. Retrieve
8824 -- the original type mark for checking.
8826 if Is_Entity_Name (Original_Node (I2)) then
8827 T2 := Entity (Original_Node (I2));
8828 else
8829 T2 := Etype (I2);
8830 end if;
8832 if not Subtypes_Match
8833 (Find_Actual_Type (Etype (I1), Scope (A_Gen_T)), T2)
8834 then
8835 Error_Msg_NE
8836 ("index types of actual do not match those of formal &",
8837 Actual, Gen_T);
8838 Abandon_Instantiation (Actual);
8839 end if;
8841 Next_Index (I1);
8842 Next_Index (I2);
8843 end loop;
8845 if not Subtypes_Match (
8846 Find_Actual_Type (Component_Type (A_Gen_T), Scope (A_Gen_T)),
8847 Component_Type (Act_T))
8848 then
8849 Error_Msg_NE
8850 ("component subtype of actual does not match that of formal &",
8851 Actual, Gen_T);
8852 Abandon_Instantiation (Actual);
8853 end if;
8855 if Has_Aliased_Components (A_Gen_T)
8856 and then not Has_Aliased_Components (Act_T)
8857 then
8858 Error_Msg_NE
8859 ("actual must have aliased components to match formal type &",
8860 Actual, Gen_T);
8861 end if;
8863 end Validate_Array_Type_Instance;
8865 -----------------------------------------------
8866 -- Validate_Derived_Interface_Type_Instance --
8867 -----------------------------------------------
8869 procedure Validate_Derived_Interface_Type_Instance is
8870 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
8871 Elmt : Elmt_Id;
8873 begin
8874 -- First apply interface instance checks
8876 Validate_Interface_Type_Instance;
8878 -- Verify that immediate parent interface is an ancestor of
8879 -- the actual.
8881 if Present (Par)
8882 and then not Interface_Present_In_Ancestor (Act_T, Par)
8883 then
8884 Error_Msg_NE
8885 ("interface actual must include progenitor&", Actual, Par);
8886 end if;
8888 -- Now verify that the actual includes all other ancestors of
8889 -- the formal.
8891 Elmt := First_Elmt (Abstract_Interfaces (A_Gen_T));
8892 while Present (Elmt) loop
8893 if not Interface_Present_In_Ancestor (Act_T, Node (Elmt)) then
8894 Error_Msg_NE
8895 ("interface actual must include progenitor&",
8896 Actual, Node (Elmt));
8897 end if;
8899 Next_Elmt (Elmt);
8900 end loop;
8901 end Validate_Derived_Interface_Type_Instance;
8903 ------------------------------------
8904 -- Validate_Derived_Type_Instance --
8905 ------------------------------------
8907 procedure Validate_Derived_Type_Instance is
8908 Actual_Discr : Entity_Id;
8909 Ancestor_Discr : Entity_Id;
8911 begin
8912 -- If the parent type in the generic declaration is itself a previous
8913 -- formal type, then it is local to the generic and absent from the
8914 -- analyzed generic definition. In that case the ancestor is the
8915 -- instance of the formal (which must have been instantiated
8916 -- previously), unless the ancestor is itself a formal derived type.
8917 -- In this latter case (which is the subject of Corrigendum 8652/0038
8918 -- (AI-202) the ancestor of the formals is the ancestor of its
8919 -- parent. Otherwise, the analyzed generic carries the parent type.
8920 -- If the parent type is defined in a previous formal package, then
8921 -- the scope of that formal package is that of the generic type
8922 -- itself, and it has already been mapped into the corresponding type
8923 -- in the actual package.
8925 -- Common case: parent type defined outside of the generic
8927 if Is_Entity_Name (Subtype_Mark (Def))
8928 and then Present (Entity (Subtype_Mark (Def)))
8929 then
8930 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
8932 -- Check whether parent is defined in a previous formal package
8934 elsif
8935 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
8936 then
8937 Ancestor :=
8938 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
8940 -- The type may be a local derivation, or a type extension of
8941 -- a previous formal, or of a formal of a parent package.
8943 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
8944 or else
8945 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
8946 then
8947 -- Check whether the parent is another derived formal type
8948 -- in the same generic unit.
8950 if Etype (A_Gen_T) /= A_Gen_T
8951 and then Is_Generic_Type (Etype (A_Gen_T))
8952 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
8953 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
8954 then
8955 -- Locate ancestor of parent from the subtype declaration
8956 -- created for the actual.
8958 declare
8959 Decl : Node_Id;
8961 begin
8962 Decl := First (Actual_Decls);
8963 while Present (Decl) loop
8964 if Nkind (Decl) = N_Subtype_Declaration
8965 and then Chars (Defining_Identifier (Decl)) =
8966 Chars (Etype (A_Gen_T))
8967 then
8968 Ancestor := Generic_Parent_Type (Decl);
8969 exit;
8970 else
8971 Next (Decl);
8972 end if;
8973 end loop;
8974 end;
8976 pragma Assert (Present (Ancestor));
8978 else
8979 Ancestor :=
8980 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
8981 end if;
8983 else
8984 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
8985 end if;
8987 -- Ada 2005 (AI-251)
8989 if Ada_Version >= Ada_05
8990 and then Is_Interface (Ancestor)
8991 then
8992 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
8993 Error_Msg_NE
8994 ("(Ada 2005) expected type implementing & in instantiation",
8995 Actual, Ancestor);
8996 end if;
8998 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
8999 Error_Msg_NE
9000 ("expect type derived from & in instantiation",
9001 Actual, First_Subtype (Ancestor));
9002 Abandon_Instantiation (Actual);
9003 end if;
9005 -- Ada 2005 (AI-443): Synchronized formal derived type ckecks. Note
9006 -- that the formal type declaration has been rewritten as a private
9007 -- extension.
9009 if Ada_Version >= Ada_05
9010 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
9011 and then Synchronized_Present (Parent (A_Gen_T))
9012 then
9013 -- The actual must be a synchronized tagged type
9015 if not Is_Tagged_Type (Act_T) then
9016 Error_Msg_N
9017 ("actual of synchronized type must be tagged", Actual);
9018 Abandon_Instantiation (Actual);
9020 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
9021 and then Nkind (Type_Definition (Parent (Act_T))) =
9022 N_Derived_Type_Definition
9023 and then not Synchronized_Present (Type_Definition
9024 (Parent (Act_T)))
9025 then
9026 Error_Msg_N
9027 ("actual of synchronized type must be synchronized", Actual);
9028 Abandon_Instantiation (Actual);
9029 end if;
9030 end if;
9032 -- Perform atomic/volatile checks (RM C.6(12))
9034 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
9035 Error_Msg_N
9036 ("cannot have atomic actual type for non-atomic formal type",
9037 Actual);
9039 elsif Is_Volatile (Act_T)
9040 and then not Is_Volatile (Ancestor)
9041 and then Is_By_Reference_Type (Ancestor)
9042 then
9043 Error_Msg_N
9044 ("cannot have volatile actual type for non-volatile formal type",
9045 Actual);
9046 end if;
9048 -- It should not be necessary to check for unknown discriminants
9049 -- on Formal, but for some reason Has_Unknown_Discriminants is
9050 -- false for A_Gen_T, so Is_Indefinite_Subtype incorrectly
9051 -- returns False. This needs fixing. ???
9053 if not Is_Indefinite_Subtype (A_Gen_T)
9054 and then not Unknown_Discriminants_Present (Formal)
9055 and then Is_Indefinite_Subtype (Act_T)
9056 then
9057 Error_Msg_N
9058 ("actual subtype must be constrained", Actual);
9059 Abandon_Instantiation (Actual);
9060 end if;
9062 if not Unknown_Discriminants_Present (Formal) then
9063 if Is_Constrained (Ancestor) then
9064 if not Is_Constrained (Act_T) then
9065 Error_Msg_N
9066 ("actual subtype must be constrained", Actual);
9067 Abandon_Instantiation (Actual);
9068 end if;
9070 -- Ancestor is unconstrained, Check if generic formal and
9071 -- actual agree on constrainedness. The check only applies
9072 -- to array types and discriminated types.
9074 elsif Is_Constrained (Act_T) then
9075 if Ekind (Ancestor) = E_Access_Type
9076 or else
9077 (not Is_Constrained (A_Gen_T)
9078 and then Is_Composite_Type (A_Gen_T))
9079 then
9080 Error_Msg_N
9081 ("actual subtype must be unconstrained", Actual);
9082 Abandon_Instantiation (Actual);
9083 end if;
9085 -- A class-wide type is only allowed if the formal has
9086 -- unknown discriminants.
9088 elsif Is_Class_Wide_Type (Act_T)
9089 and then not Has_Unknown_Discriminants (Ancestor)
9090 then
9091 Error_Msg_NE
9092 ("actual for & cannot be a class-wide type", Actual, Gen_T);
9093 Abandon_Instantiation (Actual);
9095 -- Otherwise, the formal and actual shall have the same
9096 -- number of discriminants and each discriminant of the
9097 -- actual must correspond to a discriminant of the formal.
9099 elsif Has_Discriminants (Act_T)
9100 and then not Has_Unknown_Discriminants (Act_T)
9101 and then Has_Discriminants (Ancestor)
9102 then
9103 Actual_Discr := First_Discriminant (Act_T);
9104 Ancestor_Discr := First_Discriminant (Ancestor);
9105 while Present (Actual_Discr)
9106 and then Present (Ancestor_Discr)
9107 loop
9108 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
9109 No (Corresponding_Discriminant (Actual_Discr))
9110 then
9111 Error_Msg_NE
9112 ("discriminant & does not correspond " &
9113 "to ancestor discriminant", Actual, Actual_Discr);
9114 Abandon_Instantiation (Actual);
9115 end if;
9117 Next_Discriminant (Actual_Discr);
9118 Next_Discriminant (Ancestor_Discr);
9119 end loop;
9121 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
9122 Error_Msg_NE
9123 ("actual for & must have same number of discriminants",
9124 Actual, Gen_T);
9125 Abandon_Instantiation (Actual);
9126 end if;
9128 -- This case should be caught by the earlier check for
9129 -- for constrainedness, but the check here is added for
9130 -- completeness.
9132 elsif Has_Discriminants (Act_T)
9133 and then not Has_Unknown_Discriminants (Act_T)
9134 then
9135 Error_Msg_NE
9136 ("actual for & must not have discriminants", Actual, Gen_T);
9137 Abandon_Instantiation (Actual);
9139 elsif Has_Discriminants (Ancestor) then
9140 Error_Msg_NE
9141 ("actual for & must have known discriminants", Actual, Gen_T);
9142 Abandon_Instantiation (Actual);
9143 end if;
9145 if not Subtypes_Statically_Compatible (Act_T, Ancestor) then
9146 Error_Msg_N
9147 ("constraint on actual is incompatible with formal", Actual);
9148 Abandon_Instantiation (Actual);
9149 end if;
9150 end if;
9151 end Validate_Derived_Type_Instance;
9153 --------------------------------------
9154 -- Validate_Interface_Type_Instance --
9155 --------------------------------------
9157 procedure Validate_Interface_Type_Instance is
9158 begin
9159 if not Is_Interface (Act_T) then
9160 Error_Msg_NE
9161 ("actual for formal interface type must be an interface",
9162 Actual, Gen_T);
9164 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
9165 or else
9166 Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
9167 or else
9168 Is_Protected_Interface (A_Gen_T) /=
9169 Is_Protected_Interface (Act_T)
9170 or else
9171 Is_Synchronized_Interface (A_Gen_T) /=
9172 Is_Synchronized_Interface (Act_T)
9173 then
9174 Error_Msg_NE
9175 ("actual for interface& does not match ('R'M 12.5.5(5))",
9176 Actual, Gen_T);
9177 end if;
9178 end Validate_Interface_Type_Instance;
9180 ------------------------------------
9181 -- Validate_Private_Type_Instance --
9182 ------------------------------------
9184 procedure Validate_Private_Type_Instance is
9185 Formal_Discr : Entity_Id;
9186 Actual_Discr : Entity_Id;
9187 Formal_Subt : Entity_Id;
9189 begin
9190 if Is_Limited_Type (Act_T)
9191 and then not Is_Limited_Type (A_Gen_T)
9192 then
9193 Error_Msg_NE
9194 ("actual for non-limited & cannot be a limited type", Actual,
9195 Gen_T);
9196 Explain_Limited_Type (Act_T, Actual);
9197 Abandon_Instantiation (Actual);
9199 elsif Known_To_Have_Preelab_Init (A_Gen_T)
9200 and then not Has_Preelaborable_Initialization (Act_T)
9201 then
9202 Error_Msg_NE
9203 ("actual for & must have preelaborable initialization", Actual,
9204 Gen_T);
9206 elsif Is_Indefinite_Subtype (Act_T)
9207 and then not Is_Indefinite_Subtype (A_Gen_T)
9208 and then Ada_Version >= Ada_95
9209 then
9210 Error_Msg_NE
9211 ("actual for & must be a definite subtype", Actual, Gen_T);
9213 elsif not Is_Tagged_Type (Act_T)
9214 and then Is_Tagged_Type (A_Gen_T)
9215 then
9216 Error_Msg_NE
9217 ("actual for & must be a tagged type", Actual, Gen_T);
9219 elsif Has_Discriminants (A_Gen_T) then
9220 if not Has_Discriminants (Act_T) then
9221 Error_Msg_NE
9222 ("actual for & must have discriminants", Actual, Gen_T);
9223 Abandon_Instantiation (Actual);
9225 elsif Is_Constrained (Act_T) then
9226 Error_Msg_NE
9227 ("actual for & must be unconstrained", Actual, Gen_T);
9228 Abandon_Instantiation (Actual);
9230 else
9231 Formal_Discr := First_Discriminant (A_Gen_T);
9232 Actual_Discr := First_Discriminant (Act_T);
9233 while Formal_Discr /= Empty loop
9234 if Actual_Discr = Empty then
9235 Error_Msg_NE
9236 ("discriminants on actual do not match formal",
9237 Actual, Gen_T);
9238 Abandon_Instantiation (Actual);
9239 end if;
9241 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
9243 -- Access discriminants match if designated types do
9245 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
9246 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
9247 E_Anonymous_Access_Type
9248 and then
9249 Get_Instance_Of
9250 (Designated_Type (Base_Type (Formal_Subt))) =
9251 Designated_Type (Base_Type (Etype (Actual_Discr)))
9252 then
9253 null;
9255 elsif Base_Type (Formal_Subt) /=
9256 Base_Type (Etype (Actual_Discr))
9257 then
9258 Error_Msg_NE
9259 ("types of actual discriminants must match formal",
9260 Actual, Gen_T);
9261 Abandon_Instantiation (Actual);
9263 elsif not Subtypes_Statically_Match
9264 (Formal_Subt, Etype (Actual_Discr))
9265 and then Ada_Version >= Ada_95
9266 then
9267 Error_Msg_NE
9268 ("subtypes of actual discriminants must match formal",
9269 Actual, Gen_T);
9270 Abandon_Instantiation (Actual);
9271 end if;
9273 Next_Discriminant (Formal_Discr);
9274 Next_Discriminant (Actual_Discr);
9275 end loop;
9277 if Actual_Discr /= Empty then
9278 Error_Msg_NE
9279 ("discriminants on actual do not match formal",
9280 Actual, Gen_T);
9281 Abandon_Instantiation (Actual);
9282 end if;
9283 end if;
9285 end if;
9287 Ancestor := Gen_T;
9288 end Validate_Private_Type_Instance;
9290 -- Start of processing for Instantiate_Type
9292 begin
9293 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
9294 Error_Msg_N ("duplicate instantiation of generic type", Actual);
9295 return Error;
9297 elsif not Is_Entity_Name (Actual)
9298 or else not Is_Type (Entity (Actual))
9299 then
9300 Error_Msg_NE
9301 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
9302 Abandon_Instantiation (Actual);
9304 else
9305 Act_T := Entity (Actual);
9307 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
9308 -- as a generic actual parameter if the corresponding formal type
9309 -- does not have a known_discriminant_part, or is a formal derived
9310 -- type that is an Unchecked_Union type.
9312 if Is_Unchecked_Union (Base_Type (Act_T)) then
9313 if not Has_Discriminants (A_Gen_T)
9314 or else
9315 (Is_Derived_Type (A_Gen_T)
9316 and then
9317 Is_Unchecked_Union (A_Gen_T))
9318 then
9319 null;
9320 else
9321 Error_Msg_N ("Unchecked_Union cannot be the actual for a" &
9322 " discriminated formal type", Act_T);
9324 end if;
9325 end if;
9327 -- Deal with fixed/floating restrictions
9329 if Is_Floating_Point_Type (Act_T) then
9330 Check_Restriction (No_Floating_Point, Actual);
9331 elsif Is_Fixed_Point_Type (Act_T) then
9332 Check_Restriction (No_Fixed_Point, Actual);
9333 end if;
9335 -- Deal with error of using incomplete type as generic actual
9337 if Ekind (Act_T) = E_Incomplete_Type
9338 or else (Is_Class_Wide_Type (Act_T)
9339 and then
9340 Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
9341 then
9342 if Is_Class_Wide_Type (Act_T)
9343 or else No (Underlying_Type (Act_T))
9344 then
9345 Error_Msg_N ("premature use of incomplete type", Actual);
9346 Abandon_Instantiation (Actual);
9347 else
9348 Act_T := Full_View (Act_T);
9349 Set_Entity (Actual, Act_T);
9351 if Has_Private_Component (Act_T) then
9352 Error_Msg_N
9353 ("premature use of type with private component", Actual);
9354 end if;
9355 end if;
9357 -- Deal with error of premature use of private type as generic actual
9359 elsif Is_Private_Type (Act_T)
9360 and then Is_Private_Type (Base_Type (Act_T))
9361 and then not Is_Generic_Type (Act_T)
9362 and then not Is_Derived_Type (Act_T)
9363 and then No (Full_View (Root_Type (Act_T)))
9364 then
9365 Error_Msg_N ("premature use of private type", Actual);
9367 elsif Has_Private_Component (Act_T) then
9368 Error_Msg_N
9369 ("premature use of type with private component", Actual);
9370 end if;
9372 Set_Instance_Of (A_Gen_T, Act_T);
9374 -- If the type is generic, the class-wide type may also be used
9376 if Is_Tagged_Type (A_Gen_T)
9377 and then Is_Tagged_Type (Act_T)
9378 and then not Is_Class_Wide_Type (A_Gen_T)
9379 then
9380 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
9381 Class_Wide_Type (Act_T));
9382 end if;
9384 if not Is_Abstract (A_Gen_T)
9385 and then Is_Abstract (Act_T)
9386 then
9387 Error_Msg_N
9388 ("actual of non-abstract formal cannot be abstract", Actual);
9389 end if;
9391 if Is_Scalar_Type (Gen_T) then
9392 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
9393 end if;
9394 end if;
9396 case Nkind (Def) is
9397 when N_Formal_Private_Type_Definition =>
9398 Validate_Private_Type_Instance;
9400 when N_Formal_Derived_Type_Definition =>
9401 Validate_Derived_Type_Instance;
9403 when N_Formal_Discrete_Type_Definition =>
9404 if not Is_Discrete_Type (Act_T) then
9405 Error_Msg_NE
9406 ("expect discrete type in instantiation of&", Actual, Gen_T);
9407 Abandon_Instantiation (Actual);
9408 end if;
9410 when N_Formal_Signed_Integer_Type_Definition =>
9411 if not Is_Signed_Integer_Type (Act_T) then
9412 Error_Msg_NE
9413 ("expect signed integer type in instantiation of&",
9414 Actual, Gen_T);
9415 Abandon_Instantiation (Actual);
9416 end if;
9418 when N_Formal_Modular_Type_Definition =>
9419 if not Is_Modular_Integer_Type (Act_T) then
9420 Error_Msg_NE
9421 ("expect modular type in instantiation of &", Actual, Gen_T);
9422 Abandon_Instantiation (Actual);
9423 end if;
9425 when N_Formal_Floating_Point_Definition =>
9426 if not Is_Floating_Point_Type (Act_T) then
9427 Error_Msg_NE
9428 ("expect float type in instantiation of &", Actual, Gen_T);
9429 Abandon_Instantiation (Actual);
9430 end if;
9432 when N_Formal_Ordinary_Fixed_Point_Definition =>
9433 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
9434 Error_Msg_NE
9435 ("expect ordinary fixed point type in instantiation of &",
9436 Actual, Gen_T);
9437 Abandon_Instantiation (Actual);
9438 end if;
9440 when N_Formal_Decimal_Fixed_Point_Definition =>
9441 if not Is_Decimal_Fixed_Point_Type (Act_T) then
9442 Error_Msg_NE
9443 ("expect decimal type in instantiation of &",
9444 Actual, Gen_T);
9445 Abandon_Instantiation (Actual);
9446 end if;
9448 when N_Array_Type_Definition =>
9449 Validate_Array_Type_Instance;
9451 when N_Access_To_Object_Definition =>
9452 Validate_Access_Type_Instance;
9454 when N_Access_Function_Definition |
9455 N_Access_Procedure_Definition =>
9456 Validate_Access_Subprogram_Instance;
9458 when N_Record_Definition =>
9459 Validate_Interface_Type_Instance;
9461 when N_Derived_Type_Definition =>
9462 Validate_Derived_Interface_Type_Instance;
9464 when others =>
9465 raise Program_Error;
9467 end case;
9469 Subt := New_Copy (Gen_T);
9471 -- Use adjusted sloc of subtype name as the location for other
9472 -- nodes in the subtype declaration.
9474 Loc := Sloc (Subt);
9476 Decl_Node :=
9477 Make_Subtype_Declaration (Loc,
9478 Defining_Identifier => Subt,
9479 Subtype_Indication => New_Reference_To (Act_T, Loc));
9481 if Is_Private_Type (Act_T) then
9482 Set_Has_Private_View (Subtype_Indication (Decl_Node));
9484 elsif Is_Access_Type (Act_T)
9485 and then Is_Private_Type (Designated_Type (Act_T))
9486 then
9487 Set_Has_Private_View (Subtype_Indication (Decl_Node));
9488 end if;
9490 -- Flag actual derived types so their elaboration produces the
9491 -- appropriate renamings for the primitive operations of the ancestor.
9492 -- Flag actual for formal private types as well, to determine whether
9493 -- operations in the private part may override inherited operations.
9495 if Nkind (Def) = N_Formal_Derived_Type_Definition
9496 or else Nkind (Def) = N_Formal_Private_Type_Definition
9497 then
9498 Set_Generic_Parent_Type (Decl_Node, Ancestor);
9499 end if;
9501 return Decl_Node;
9502 end Instantiate_Type;
9504 -----------------------
9505 -- Is_Generic_Formal --
9506 -----------------------
9508 function Is_Generic_Formal (E : Entity_Id) return Boolean is
9509 Kind : constant Node_Kind := Nkind (Parent (E));
9510 begin
9511 return
9512 Kind = N_Formal_Object_Declaration
9513 or else Kind = N_Formal_Package_Declaration
9514 or else Kind in N_Formal_Subprogram_Declaration
9515 or else Kind = N_Formal_Type_Declaration;
9516 end Is_Generic_Formal;
9518 ---------------------
9519 -- Is_In_Main_Unit --
9520 ---------------------
9522 function Is_In_Main_Unit (N : Node_Id) return Boolean is
9523 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
9524 Current_Unit : Node_Id;
9526 begin
9527 if Unum = Main_Unit then
9528 return True;
9530 -- If the current unit is a subunit then it is either the main unit
9531 -- or is being compiled as part of the main unit.
9533 elsif Nkind (N) = N_Compilation_Unit then
9534 return Nkind (Unit (N)) = N_Subunit;
9535 end if;
9537 Current_Unit := Parent (N);
9538 while Present (Current_Unit)
9539 and then Nkind (Current_Unit) /= N_Compilation_Unit
9540 loop
9541 Current_Unit := Parent (Current_Unit);
9542 end loop;
9544 -- The instantiation node is in the main unit, or else the current
9545 -- node (perhaps as the result of nested instantiations) is in the
9546 -- main unit, or in the declaration of the main unit, which in this
9547 -- last case must be a body.
9549 return Unum = Main_Unit
9550 or else Current_Unit = Cunit (Main_Unit)
9551 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
9552 or else (Present (Library_Unit (Current_Unit))
9553 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
9554 end Is_In_Main_Unit;
9556 ----------------------------
9557 -- Load_Parent_Of_Generic --
9558 ----------------------------
9560 procedure Load_Parent_Of_Generic (N : Node_Id; Spec : Node_Id) is
9561 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
9562 Save_Style_Check : constant Boolean := Style_Check;
9563 True_Parent : Node_Id;
9564 Inst_Node : Node_Id;
9565 OK : Boolean;
9567 begin
9568 if not In_Same_Source_Unit (N, Spec)
9569 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
9570 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
9571 and then not Is_In_Main_Unit (Spec))
9572 then
9573 -- Find body of parent of spec, and analyze it. A special case
9574 -- arises when the parent is an instantiation, that is to say when
9575 -- we are currently instantiating a nested generic. In that case,
9576 -- there is no separate file for the body of the enclosing instance.
9577 -- Instead, the enclosing body must be instantiated as if it were
9578 -- a pending instantiation, in order to produce the body for the
9579 -- nested generic we require now. Note that in that case the
9580 -- generic may be defined in a package body, the instance defined
9581 -- in the same package body, and the original enclosing body may not
9582 -- be in the main unit.
9584 True_Parent := Parent (Spec);
9585 Inst_Node := Empty;
9587 while Present (True_Parent)
9588 and then Nkind (True_Parent) /= N_Compilation_Unit
9589 loop
9590 if Nkind (True_Parent) = N_Package_Declaration
9591 and then
9592 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
9593 then
9594 -- Parent is a compilation unit that is an instantiation.
9595 -- Instantiation node has been replaced with package decl.
9597 Inst_Node := Original_Node (True_Parent);
9598 exit;
9600 elsif Nkind (True_Parent) = N_Package_Declaration
9601 and then Present (Generic_Parent (Specification (True_Parent)))
9602 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
9603 then
9604 -- Parent is an instantiation within another specification.
9605 -- Declaration for instance has been inserted before original
9606 -- instantiation node. A direct link would be preferable?
9608 Inst_Node := Next (True_Parent);
9610 while Present (Inst_Node)
9611 and then Nkind (Inst_Node) /= N_Package_Instantiation
9612 loop
9613 Next (Inst_Node);
9614 end loop;
9616 -- If the instance appears within a generic, and the generic
9617 -- unit is defined within a formal package of the enclosing
9618 -- generic, there is no generic body available, and none
9619 -- needed. A more precise test should be used ???
9621 if No (Inst_Node) then
9622 return;
9623 end if;
9625 exit;
9626 else
9627 True_Parent := Parent (True_Parent);
9628 end if;
9629 end loop;
9631 -- Case where we are currently instantiating a nested generic
9633 if Present (Inst_Node) then
9634 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
9636 -- Instantiation node and declaration of instantiated package
9637 -- were exchanged when only the declaration was needed.
9638 -- Restore instantiation node before proceeding with body.
9640 Set_Unit (Parent (True_Parent), Inst_Node);
9641 end if;
9643 -- Now complete instantiation of enclosing body, if it appears
9644 -- in some other unit. If it appears in the current unit, the
9645 -- body will have been instantiated already.
9647 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
9649 -- We need to determine the expander mode to instantiate
9650 -- the enclosing body. Because the generic body we need
9651 -- may use global entities declared in the enclosing package
9652 -- (including aggregates) it is in general necessary to
9653 -- compile this body with expansion enabled. The exception
9654 -- is if we are within a generic package, in which case
9655 -- the usual generic rule applies.
9657 declare
9658 Exp_Status : Boolean := True;
9659 Scop : Entity_Id;
9661 begin
9662 -- Loop through scopes looking for generic package
9664 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
9665 while Present (Scop)
9666 and then Scop /= Standard_Standard
9667 loop
9668 if Ekind (Scop) = E_Generic_Package then
9669 Exp_Status := False;
9670 exit;
9671 end if;
9673 Scop := Scope (Scop);
9674 end loop;
9676 Instantiate_Package_Body
9677 (Pending_Body_Info'(
9678 Inst_Node, True_Parent, Exp_Status,
9679 Get_Code_Unit (Sloc (Inst_Node))));
9680 end;
9681 end if;
9683 -- Case where we are not instantiating a nested generic
9685 else
9686 Opt.Style_Check := False;
9687 Expander_Mode_Save_And_Set (True);
9688 Load_Needed_Body (Comp_Unit, OK);
9689 Opt.Style_Check := Save_Style_Check;
9690 Expander_Mode_Restore;
9692 if not OK
9693 and then Unit_Requires_Body (Defining_Entity (Spec))
9694 then
9695 declare
9696 Bname : constant Unit_Name_Type :=
9697 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
9699 begin
9700 Error_Msg_Unit_1 := Bname;
9701 Error_Msg_N ("this instantiation requires$!", N);
9702 Error_Msg_Name_1 :=
9703 Get_File_Name (Bname, Subunit => False);
9704 Error_Msg_N ("\but file{ was not found!", N);
9705 raise Unrecoverable_Error;
9706 end;
9707 end if;
9708 end if;
9709 end if;
9711 -- If loading the parent of the generic caused an instantiation
9712 -- circularity, we abandon compilation at this point, because
9713 -- otherwise in some cases we get into trouble with infinite
9714 -- recursions after this point.
9716 if Circularity_Detected then
9717 raise Unrecoverable_Error;
9718 end if;
9719 end Load_Parent_Of_Generic;
9721 -----------------------
9722 -- Move_Freeze_Nodes --
9723 -----------------------
9725 procedure Move_Freeze_Nodes
9726 (Out_Of : Entity_Id;
9727 After : Node_Id;
9728 L : List_Id)
9730 Decl : Node_Id;
9731 Next_Decl : Node_Id;
9732 Next_Node : Node_Id := After;
9733 Spec : Node_Id;
9735 function Is_Outer_Type (T : Entity_Id) return Boolean;
9736 -- Check whether entity is declared in a scope external to that
9737 -- of the generic unit.
9739 -------------------
9740 -- Is_Outer_Type --
9741 -------------------
9743 function Is_Outer_Type (T : Entity_Id) return Boolean is
9744 Scop : Entity_Id := Scope (T);
9746 begin
9747 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
9748 return True;
9750 else
9751 while Scop /= Standard_Standard loop
9753 if Scop = Out_Of then
9754 return False;
9755 else
9756 Scop := Scope (Scop);
9757 end if;
9758 end loop;
9760 return True;
9761 end if;
9762 end Is_Outer_Type;
9764 -- Start of processing for Move_Freeze_Nodes
9766 begin
9767 if No (L) then
9768 return;
9769 end if;
9771 -- First remove the freeze nodes that may appear before all other
9772 -- declarations.
9774 Decl := First (L);
9775 while Present (Decl)
9776 and then Nkind (Decl) = N_Freeze_Entity
9777 and then Is_Outer_Type (Entity (Decl))
9778 loop
9779 Decl := Remove_Head (L);
9780 Insert_After (Next_Node, Decl);
9781 Set_Analyzed (Decl, False);
9782 Next_Node := Decl;
9783 Decl := First (L);
9784 end loop;
9786 -- Next scan the list of declarations and remove each freeze node that
9787 -- appears ahead of the current node.
9789 while Present (Decl) loop
9790 while Present (Next (Decl))
9791 and then Nkind (Next (Decl)) = N_Freeze_Entity
9792 and then Is_Outer_Type (Entity (Next (Decl)))
9793 loop
9794 Next_Decl := Remove_Next (Decl);
9795 Insert_After (Next_Node, Next_Decl);
9796 Set_Analyzed (Next_Decl, False);
9797 Next_Node := Next_Decl;
9798 end loop;
9800 -- If the declaration is a nested package or concurrent type, then
9801 -- recurse. Nested generic packages will have been processed from the
9802 -- inside out.
9804 if Nkind (Decl) = N_Package_Declaration then
9805 Spec := Specification (Decl);
9807 elsif Nkind (Decl) = N_Task_Type_Declaration then
9808 Spec := Task_Definition (Decl);
9810 elsif Nkind (Decl) = N_Protected_Type_Declaration then
9811 Spec := Protected_Definition (Decl);
9813 else
9814 Spec := Empty;
9815 end if;
9817 if Present (Spec) then
9818 Move_Freeze_Nodes (Out_Of, Next_Node,
9819 Visible_Declarations (Spec));
9820 Move_Freeze_Nodes (Out_Of, Next_Node,
9821 Private_Declarations (Spec));
9822 end if;
9824 Next (Decl);
9825 end loop;
9826 end Move_Freeze_Nodes;
9828 ----------------
9829 -- Next_Assoc --
9830 ----------------
9832 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
9833 begin
9834 return Generic_Renamings.Table (E).Next_In_HTable;
9835 end Next_Assoc;
9837 ------------------------
9838 -- Preanalyze_Actuals --
9839 ------------------------
9841 procedure Pre_Analyze_Actuals (N : Node_Id) is
9842 Assoc : Node_Id;
9843 Act : Node_Id;
9844 Errs : constant Int := Serious_Errors_Detected;
9846 begin
9847 Assoc := First (Generic_Associations (N));
9848 while Present (Assoc) loop
9849 if Nkind (Assoc) /= N_Others_Choice then
9850 Act := Explicit_Generic_Actual_Parameter (Assoc);
9852 -- Within a nested instantiation, a defaulted actual is an empty
9853 -- association, so nothing to analyze. If the subprogram actual
9854 -- isan attribute, analyze prefix only, because actual is not a
9855 -- complete attribute reference.
9857 -- If actual is an allocator, analyze expression only. The full
9858 -- analysis can generate code, and if instance is a compilation
9859 -- unit we have to wait until the package instance is installed
9860 -- to have a proper place to insert this code.
9862 -- String literals may be operators, but at this point we do not
9863 -- know whether the actual is a formal subprogram or a string.
9865 if No (Act) then
9866 null;
9868 elsif Nkind (Act) = N_Attribute_Reference then
9869 Analyze (Prefix (Act));
9871 elsif Nkind (Act) = N_Explicit_Dereference then
9872 Analyze (Prefix (Act));
9874 elsif Nkind (Act) = N_Allocator then
9875 declare
9876 Expr : constant Node_Id := Expression (Act);
9878 begin
9879 if Nkind (Expr) = N_Subtype_Indication then
9880 Analyze (Subtype_Mark (Expr));
9881 Analyze_List (Constraints (Constraint (Expr)));
9882 else
9883 Analyze (Expr);
9884 end if;
9885 end;
9887 elsif Nkind (Act) /= N_Operator_Symbol then
9888 Analyze (Act);
9889 end if;
9891 if Errs /= Serious_Errors_Detected then
9892 Abandon_Instantiation (Act);
9893 end if;
9894 end if;
9896 Next (Assoc);
9897 end loop;
9898 end Pre_Analyze_Actuals;
9900 -------------------
9901 -- Remove_Parent --
9902 -------------------
9904 procedure Remove_Parent (In_Body : Boolean := False) is
9905 S : Entity_Id := Current_Scope;
9906 E : Entity_Id;
9907 P : Entity_Id;
9908 Hidden : Elmt_Id;
9910 begin
9911 -- After child instantiation is complete, remove from scope stack
9912 -- the extra copy of the current scope, and then remove parent
9913 -- instances.
9915 if not In_Body then
9916 Pop_Scope;
9918 while Current_Scope /= S loop
9919 P := Current_Scope;
9920 End_Package_Scope (Current_Scope);
9922 if In_Open_Scopes (P) then
9923 E := First_Entity (P);
9925 while Present (E) loop
9926 Set_Is_Immediately_Visible (E, True);
9927 Next_Entity (E);
9928 end loop;
9930 if Is_Generic_Instance (Current_Scope)
9931 and then P /= Current_Scope
9932 then
9933 -- We are within an instance of some sibling. Retain
9934 -- visibility of parent, for proper subsequent cleanup,
9935 -- and reinstall private declarations as well.
9937 Set_In_Private_Part (P);
9938 Install_Private_Declarations (P);
9939 end if;
9941 -- If the ultimate parent is a top-level unit recorded in
9942 -- Instance_Parent_Unit, then reset its visibility to what
9943 -- it was before instantiation. (It's not clear what the
9944 -- purpose is of testing whether Scope (P) is In_Open_Scopes,
9945 -- but that test was present before the ultimate parent test
9946 -- was added.???)
9948 elsif not In_Open_Scopes (Scope (P))
9949 or else (P = Instance_Parent_Unit
9950 and then not Parent_Unit_Visible)
9951 then
9952 Set_Is_Immediately_Visible (P, False);
9953 end if;
9954 end loop;
9956 -- Reset visibility of entities in the enclosing scope
9958 Set_Is_Hidden_Open_Scope (Current_Scope, False);
9959 Hidden := First_Elmt (Hidden_Entities);
9961 while Present (Hidden) loop
9962 Set_Is_Immediately_Visible (Node (Hidden), True);
9963 Next_Elmt (Hidden);
9964 end loop;
9966 else
9967 -- Each body is analyzed separately, and there is no context
9968 -- that needs preserving from one body instance to the next,
9969 -- so remove all parent scopes that have been installed.
9971 while Present (S) loop
9972 End_Package_Scope (S);
9973 Set_Is_Immediately_Visible (S, False);
9974 S := Current_Scope;
9975 exit when S = Standard_Standard;
9976 end loop;
9977 end if;
9979 end Remove_Parent;
9981 -----------------
9982 -- Restore_Env --
9983 -----------------
9985 procedure Restore_Env is
9986 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
9988 begin
9989 Ada_Version := Saved.Ada_Version;
9990 Ada_Version_Explicit := Saved.Ada_Version_Explicit;
9992 if No (Current_Instantiated_Parent.Act_Id) then
9994 -- Restore environment after subprogram inlining
9996 Restore_Private_Views (Empty);
9997 end if;
9999 Current_Instantiated_Parent := Saved.Instantiated_Parent;
10000 Exchanged_Views := Saved.Exchanged_Views;
10001 Hidden_Entities := Saved.Hidden_Entities;
10002 Current_Sem_Unit := Saved.Current_Sem_Unit;
10003 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
10004 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
10006 Instance_Envs.Decrement_Last;
10007 end Restore_Env;
10009 ---------------------------
10010 -- Restore_Private_Views --
10011 ---------------------------
10013 procedure Restore_Private_Views
10014 (Pack_Id : Entity_Id;
10015 Is_Package : Boolean := True)
10017 M : Elmt_Id;
10018 E : Entity_Id;
10019 Typ : Entity_Id;
10020 Dep_Elmt : Elmt_Id;
10021 Dep_Typ : Node_Id;
10023 procedure Restore_Nested_Formal (Formal : Entity_Id);
10024 -- Hide the generic formals of formal packages declared with box
10025 -- which were reachable in the current instantiation.
10027 procedure Restore_Nested_Formal (Formal : Entity_Id) is
10028 Ent : Entity_Id;
10030 begin
10031 if Present (Renamed_Object (Formal))
10032 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
10033 then
10034 return;
10036 elsif Present (Associated_Formal_Package (Formal)) then
10038 Ent := First_Entity (Formal);
10039 while Present (Ent) loop
10040 exit when Ekind (Ent) = E_Package
10041 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
10043 Set_Is_Hidden (Ent);
10044 Set_Is_Potentially_Use_Visible (Ent, False);
10046 -- If package, then recurse
10048 if Ekind (Ent) = E_Package then
10049 Restore_Nested_Formal (Ent);
10050 end if;
10052 Next_Entity (Ent);
10053 end loop;
10054 end if;
10055 end Restore_Nested_Formal;
10057 -- Start of processing for Restore_Private_Views
10059 begin
10060 M := First_Elmt (Exchanged_Views);
10061 while Present (M) loop
10062 Typ := Node (M);
10064 -- Subtypes of types whose views have been exchanged, and that
10065 -- are defined within the instance, were not on the list of
10066 -- Private_Dependents on entry to the instance, so they have to
10067 -- be exchanged explicitly now, in order to remain consistent with
10068 -- the view of the parent type.
10070 if Ekind (Typ) = E_Private_Type
10071 or else Ekind (Typ) = E_Limited_Private_Type
10072 or else Ekind (Typ) = E_Record_Type_With_Private
10073 then
10074 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
10075 while Present (Dep_Elmt) loop
10076 Dep_Typ := Node (Dep_Elmt);
10078 if Scope (Dep_Typ) = Pack_Id
10079 and then Present (Full_View (Dep_Typ))
10080 then
10081 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
10082 Exchange_Declarations (Dep_Typ);
10083 end if;
10085 Next_Elmt (Dep_Elmt);
10086 end loop;
10087 end if;
10089 Exchange_Declarations (Node (M));
10090 Next_Elmt (M);
10091 end loop;
10093 if No (Pack_Id) then
10094 return;
10095 end if;
10097 -- Make the generic formal parameters private, and make the formal
10098 -- types into subtypes of the actuals again.
10100 E := First_Entity (Pack_Id);
10101 while Present (E) loop
10102 Set_Is_Hidden (E, True);
10104 if Is_Type (E)
10105 and then Nkind (Parent (E)) = N_Subtype_Declaration
10106 then
10107 Set_Is_Generic_Actual_Type (E, False);
10109 -- An unusual case of aliasing: the actual may also be directly
10110 -- visible in the generic, and be private there, while it is
10111 -- fully visible in the context of the instance. The internal
10112 -- subtype is private in the instance, but has full visibility
10113 -- like its parent in the enclosing scope. This enforces the
10114 -- invariant that the privacy status of all private dependents of
10115 -- a type coincide with that of the parent type. This can only
10116 -- happen when a generic child unit is instantiated within a
10117 -- sibling.
10119 if Is_Private_Type (E)
10120 and then not Is_Private_Type (Etype (E))
10121 then
10122 Exchange_Declarations (E);
10123 end if;
10125 elsif Ekind (E) = E_Package then
10127 -- The end of the renaming list is the renaming of the generic
10128 -- package itself. If the instance is a subprogram, all entities
10129 -- in the corresponding package are renamings. If this entity is
10130 -- a formal package, make its own formals private as well. The
10131 -- actual in this case is itself the renaming of an instantation.
10132 -- If the entity is not a package renaming, it is the entity
10133 -- created to validate formal package actuals: ignore.
10135 -- If the actual is itself a formal package for the enclosing
10136 -- generic, or the actual for such a formal package, it remains
10137 -- visible on exit from the instance, and therefore nothing
10138 -- needs to be done either, except to keep it accessible.
10140 if Is_Package
10141 and then Renamed_Object (E) = Pack_Id
10142 then
10143 exit;
10145 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
10146 null;
10148 elsif Denotes_Formal_Package (Renamed_Object (E), True) then
10149 Set_Is_Hidden (E, False);
10151 else
10152 declare
10153 Act_P : constant Entity_Id := Renamed_Object (E);
10154 Id : Entity_Id;
10156 begin
10157 Id := First_Entity (Act_P);
10158 while Present (Id)
10159 and then Id /= First_Private_Entity (Act_P)
10160 loop
10161 exit when Ekind (Id) = E_Package
10162 and then Renamed_Object (Id) = Act_P;
10164 Set_Is_Hidden (Id, True);
10165 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
10167 if Ekind (Id) = E_Package then
10168 Restore_Nested_Formal (Id);
10169 end if;
10171 Next_Entity (Id);
10172 end loop;
10173 end;
10174 end if;
10175 end if;
10177 Next_Entity (E);
10178 end loop;
10179 end Restore_Private_Views;
10181 --------------
10182 -- Save_Env --
10183 --------------
10185 procedure Save_Env
10186 (Gen_Unit : Entity_Id;
10187 Act_Unit : Entity_Id)
10189 begin
10190 Init_Env;
10191 Set_Instance_Env (Gen_Unit, Act_Unit);
10192 end Save_Env;
10194 ----------------------------
10195 -- Save_Global_References --
10196 ----------------------------
10198 procedure Save_Global_References (N : Node_Id) is
10199 Gen_Scope : Entity_Id;
10200 E : Entity_Id;
10201 N2 : Node_Id;
10203 function Is_Global (E : Entity_Id) return Boolean;
10204 -- Check whether entity is defined outside of generic unit.
10205 -- Examine the scope of an entity, and the scope of the scope,
10206 -- etc, until we find either Standard, in which case the entity
10207 -- is global, or the generic unit itself, which indicates that
10208 -- the entity is local. If the entity is the generic unit itself,
10209 -- as in the case of a recursive call, or the enclosing generic unit,
10210 -- if different from the current scope, then it is local as well,
10211 -- because it will be replaced at the point of instantiation. On
10212 -- the other hand, if it is a reference to a child unit of a common
10213 -- ancestor, which appears in an instantiation, it is global because
10214 -- it is used to denote a specific compilation unit at the time the
10215 -- instantiations will be analyzed.
10217 procedure Reset_Entity (N : Node_Id);
10218 -- Save semantic information on global entity, so that it is not
10219 -- resolved again at instantiation time.
10221 procedure Save_Entity_Descendants (N : Node_Id);
10222 -- Apply Save_Global_References to the two syntactic descendants of
10223 -- non-terminal nodes that carry an Associated_Node and are processed
10224 -- through Reset_Entity. Once the global entity (if any) has been
10225 -- captured together with its type, only two syntactic descendants
10226 -- need to be traversed to complete the processing of the tree rooted
10227 -- at N. This applies to Selected_Components, Expanded_Names, and to
10228 -- Operator nodes. N can also be a character literal, identifier, or
10229 -- operator symbol node, but the call has no effect in these cases.
10231 procedure Save_Global_Defaults (N1, N2 : Node_Id);
10232 -- Default actuals in nested instances must be handled specially
10233 -- because there is no link to them from the original tree. When an
10234 -- actual subprogram is given by a default, we add an explicit generic
10235 -- association for it in the instantiation node. When we save the
10236 -- global references on the name of the instance, we recover the list
10237 -- of generic associations, and add an explicit one to the original
10238 -- generic tree, through which a global actual can be preserved.
10239 -- Similarly, if a child unit is instantiated within a sibling, in the
10240 -- context of the parent, we must preserve the identifier of the parent
10241 -- so that it can be properly resolved in a subsequent instantiation.
10243 procedure Save_Global_Descendant (D : Union_Id);
10244 -- Apply Save_Global_References recursively to the descendents of
10245 -- current node.
10247 procedure Save_References (N : Node_Id);
10248 -- This is the recursive procedure that does the work, once the
10249 -- enclosing generic scope has been established.
10251 ---------------
10252 -- Is_Global --
10253 ---------------
10255 function Is_Global (E : Entity_Id) return Boolean is
10256 Se : Entity_Id := Scope (E);
10258 function Is_Instance_Node (Decl : Node_Id) return Boolean;
10259 -- Determine whether the parent node of a reference to a child unit
10260 -- denotes an instantiation or a formal package, in which case the
10261 -- reference to the child unit is global, even if it appears within
10262 -- the current scope (e.g. when the instance appears within the body
10263 -- of an ancestor).
10265 ----------------------
10266 -- Is_Instance_Node --
10267 ----------------------
10269 function Is_Instance_Node (Decl : Node_Id) return Boolean is
10270 begin
10271 return (Nkind (Decl) in N_Generic_Instantiation
10272 or else
10273 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration);
10274 end Is_Instance_Node;
10276 -- Start of processing for Is_Global
10278 begin
10279 if E = Gen_Scope then
10280 return False;
10282 elsif E = Standard_Standard then
10283 return True;
10285 elsif Is_Child_Unit (E)
10286 and then (Is_Instance_Node (Parent (N2))
10287 or else (Nkind (Parent (N2)) = N_Expanded_Name
10288 and then N2 = Selector_Name (Parent (N2))
10289 and then Is_Instance_Node (Parent (Parent (N2)))))
10290 then
10291 return True;
10293 else
10294 while Se /= Gen_Scope loop
10295 if Se = Standard_Standard then
10296 return True;
10297 else
10298 Se := Scope (Se);
10299 end if;
10300 end loop;
10302 return False;
10303 end if;
10304 end Is_Global;
10306 ------------------
10307 -- Reset_Entity --
10308 ------------------
10310 procedure Reset_Entity (N : Node_Id) is
10312 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
10313 -- The type of N2 is global to the generic unit. Save the
10314 -- type in the generic node.
10316 function Top_Ancestor (E : Entity_Id) return Entity_Id;
10317 -- Find the ultimate ancestor of the current unit. If it is
10318 -- not a generic unit, then the name of the current unit
10319 -- in the prefix of an expanded name must be replaced with
10320 -- its generic homonym to ensure that it will be properly
10321 -- resolved in an instance.
10323 ---------------------
10324 -- Set_Global_Type --
10325 ---------------------
10327 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
10328 Typ : constant Entity_Id := Etype (N2);
10330 begin
10331 Set_Etype (N, Typ);
10333 if Entity (N) /= N2
10334 and then Has_Private_View (Entity (N))
10335 then
10336 -- If the entity of N is not the associated node, this is
10337 -- a nested generic and it has an associated node as well,
10338 -- whose type is already the full view (see below). Indicate
10339 -- that the original node has a private view.
10341 Set_Has_Private_View (N);
10342 end if;
10344 -- If not a private type, nothing else to do
10346 if not Is_Private_Type (Typ) then
10347 if Is_Array_Type (Typ)
10348 and then Is_Private_Type (Component_Type (Typ))
10349 then
10350 Set_Has_Private_View (N);
10351 end if;
10353 -- If it is a derivation of a private type in a context where
10354 -- no full view is needed, nothing to do either.
10356 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
10357 null;
10359 -- Otherwise mark the type for flipping and use the full_view
10360 -- when available.
10362 else
10363 Set_Has_Private_View (N);
10365 if Present (Full_View (Typ)) then
10366 Set_Etype (N2, Full_View (Typ));
10367 end if;
10368 end if;
10369 end Set_Global_Type;
10371 ------------------
10372 -- Top_Ancestor --
10373 ------------------
10375 function Top_Ancestor (E : Entity_Id) return Entity_Id is
10376 Par : Entity_Id := E;
10378 begin
10379 while Is_Child_Unit (Par) loop
10380 Par := Scope (Par);
10381 end loop;
10383 return Par;
10384 end Top_Ancestor;
10386 -- Start of processing for Reset_Entity
10388 begin
10389 N2 := Get_Associated_Node (N);
10390 E := Entity (N2);
10392 if Present (E) then
10393 if Is_Global (E) then
10394 Set_Global_Type (N, N2);
10396 elsif Nkind (N) = N_Op_Concat
10397 and then Is_Generic_Type (Etype (N2))
10398 and then
10399 (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
10400 or else Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
10401 and then Is_Intrinsic_Subprogram (E)
10402 then
10403 null;
10405 else
10406 -- Entity is local. Mark generic node as unresolved.
10407 -- Note that now it does not have an entity.
10409 Set_Associated_Node (N, Empty);
10410 Set_Etype (N, Empty);
10411 end if;
10413 if Nkind (Parent (N)) in N_Generic_Instantiation
10414 and then N = Name (Parent (N))
10415 then
10416 Save_Global_Defaults (Parent (N), Parent (N2));
10417 end if;
10419 elsif Nkind (Parent (N)) = N_Selected_Component
10420 and then Nkind (Parent (N2)) = N_Expanded_Name
10421 then
10422 if Is_Global (Entity (Parent (N2))) then
10423 Change_Selected_Component_To_Expanded_Name (Parent (N));
10424 Set_Associated_Node (Parent (N), Parent (N2));
10425 Set_Global_Type (Parent (N), Parent (N2));
10426 Save_Entity_Descendants (N);
10428 -- If this is a reference to the current generic entity,
10429 -- replace by the name of the generic homonym of the current
10430 -- package. This is because in an instantiation Par.P.Q will
10431 -- not resolve to the name of the instance, whose enclosing
10432 -- scope is not necessarily Par. We use the generic homonym
10433 -- rather that the name of the generic itself, because it may
10434 -- be hidden by a local declaration.
10436 elsif In_Open_Scopes (Entity (Parent (N2)))
10437 and then not
10438 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
10439 then
10440 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
10441 Rewrite (Parent (N),
10442 Make_Identifier (Sloc (N),
10443 Chars =>
10444 Chars (Generic_Homonym (Entity (Parent (N2))))));
10445 else
10446 Rewrite (Parent (N),
10447 Make_Identifier (Sloc (N),
10448 Chars => Chars (Selector_Name (Parent (N2)))));
10449 end if;
10450 end if;
10452 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
10453 and then Parent (N) = Name (Parent (Parent (N)))
10454 then
10455 Save_Global_Defaults
10456 (Parent (Parent (N)), Parent (Parent ((N2))));
10457 end if;
10459 -- A selected component may denote a static constant that has
10460 -- been folded. Make the same replacement in original tree.
10462 elsif Nkind (Parent (N)) = N_Selected_Component
10463 and then (Nkind (Parent (N2)) = N_Integer_Literal
10464 or else Nkind (Parent (N2)) = N_Real_Literal)
10465 then
10466 Rewrite (Parent (N),
10467 New_Copy (Parent (N2)));
10468 Set_Analyzed (Parent (N), False);
10470 -- A selected component may be transformed into a parameterless
10471 -- function call. If the called entity is global, rewrite the
10472 -- node appropriately, i.e. as an extended name for the global
10473 -- entity.
10475 elsif Nkind (Parent (N)) = N_Selected_Component
10476 and then Nkind (Parent (N2)) = N_Function_Call
10477 and then Is_Global (Entity (Name (Parent (N2))))
10478 then
10479 Change_Selected_Component_To_Expanded_Name (Parent (N));
10480 Set_Associated_Node (Parent (N), Name (Parent (N2)));
10481 Set_Global_Type (Parent (N), Name (Parent (N2)));
10482 Save_Entity_Descendants (N);
10484 else
10485 -- Entity is local. Reset in generic unit, so that node
10486 -- is resolved anew at the point of instantiation.
10488 Set_Associated_Node (N, Empty);
10489 Set_Etype (N, Empty);
10490 end if;
10491 end Reset_Entity;
10493 -----------------------------
10494 -- Save_Entity_Descendants --
10495 -----------------------------
10497 procedure Save_Entity_Descendants (N : Node_Id) is
10498 begin
10499 case Nkind (N) is
10500 when N_Binary_Op =>
10501 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
10502 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
10504 when N_Unary_Op =>
10505 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
10507 when N_Expanded_Name | N_Selected_Component =>
10508 Save_Global_Descendant (Union_Id (Prefix (N)));
10509 Save_Global_Descendant (Union_Id (Selector_Name (N)));
10511 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
10512 null;
10514 when others =>
10515 raise Program_Error;
10516 end case;
10517 end Save_Entity_Descendants;
10519 --------------------------
10520 -- Save_Global_Defaults --
10521 --------------------------
10523 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
10524 Loc : constant Source_Ptr := Sloc (N1);
10525 Assoc2 : constant List_Id := Generic_Associations (N2);
10526 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
10527 Assoc1 : List_Id;
10528 Act1 : Node_Id;
10529 Act2 : Node_Id;
10530 Def : Node_Id;
10531 Ndec : Node_Id;
10532 Subp : Entity_Id;
10533 Actual : Entity_Id;
10535 begin
10536 Assoc1 := Generic_Associations (N1);
10538 if Present (Assoc1) then
10539 Act1 := First (Assoc1);
10540 else
10541 Act1 := Empty;
10542 Set_Generic_Associations (N1, New_List);
10543 Assoc1 := Generic_Associations (N1);
10544 end if;
10546 if Present (Assoc2) then
10547 Act2 := First (Assoc2);
10548 else
10549 return;
10550 end if;
10552 while Present (Act1) and then Present (Act2) loop
10553 Next (Act1);
10554 Next (Act2);
10555 end loop;
10557 -- Find the associations added for default suprograms
10559 if Present (Act2) then
10560 while Nkind (Act2) /= N_Generic_Association
10561 or else No (Entity (Selector_Name (Act2)))
10562 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
10563 loop
10564 Next (Act2);
10565 end loop;
10567 -- Add a similar association if the default is global. The
10568 -- renaming declaration for the actual has been analyzed, and
10569 -- its alias is the program it renames. Link the actual in the
10570 -- original generic tree with the node in the analyzed tree.
10572 while Present (Act2) loop
10573 Subp := Entity (Selector_Name (Act2));
10574 Def := Explicit_Generic_Actual_Parameter (Act2);
10576 -- Following test is defence against rubbish errors
10578 if No (Alias (Subp)) then
10579 return;
10580 end if;
10582 -- Retrieve the resolved actual from the renaming declaration
10583 -- created for the instantiated formal.
10585 Actual := Entity (Name (Parent (Parent (Subp))));
10586 Set_Entity (Def, Actual);
10587 Set_Etype (Def, Etype (Actual));
10589 if Is_Global (Actual) then
10590 Ndec :=
10591 Make_Generic_Association (Loc,
10592 Selector_Name => New_Occurrence_Of (Subp, Loc),
10593 Explicit_Generic_Actual_Parameter =>
10594 New_Occurrence_Of (Actual, Loc));
10596 Set_Associated_Node
10597 (Explicit_Generic_Actual_Parameter (Ndec), Def);
10599 Append (Ndec, Assoc1);
10601 -- If there are other defaults, add a dummy association
10602 -- in case there are other defaulted formals with the same
10603 -- name.
10605 elsif Present (Next (Act2)) then
10606 Ndec :=
10607 Make_Generic_Association (Loc,
10608 Selector_Name => New_Occurrence_Of (Subp, Loc),
10609 Explicit_Generic_Actual_Parameter => Empty);
10611 Append (Ndec, Assoc1);
10612 end if;
10614 Next (Act2);
10615 end loop;
10616 end if;
10618 if Nkind (Name (N1)) = N_Identifier
10619 and then Is_Child_Unit (Gen_Id)
10620 and then Is_Global (Gen_Id)
10621 and then Is_Generic_Unit (Scope (Gen_Id))
10622 and then In_Open_Scopes (Scope (Gen_Id))
10623 then
10624 -- This is an instantiation of a child unit within a sibling,
10625 -- so that the generic parent is in scope. An eventual instance
10626 -- must occur within the scope of an instance of the parent.
10627 -- Make name in instance into an expanded name, to preserve the
10628 -- identifier of the parent, so it can be resolved subsequently.
10630 Rewrite (Name (N2),
10631 Make_Expanded_Name (Loc,
10632 Chars => Chars (Gen_Id),
10633 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
10634 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
10635 Set_Entity (Name (N2), Gen_Id);
10637 Rewrite (Name (N1),
10638 Make_Expanded_Name (Loc,
10639 Chars => Chars (Gen_Id),
10640 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
10641 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
10643 Set_Associated_Node (Name (N1), Name (N2));
10644 Set_Associated_Node (Prefix (Name (N1)), Empty);
10645 Set_Associated_Node
10646 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
10647 Set_Etype (Name (N1), Etype (Gen_Id));
10648 end if;
10650 end Save_Global_Defaults;
10652 ----------------------------
10653 -- Save_Global_Descendant --
10654 ----------------------------
10656 procedure Save_Global_Descendant (D : Union_Id) is
10657 N1 : Node_Id;
10659 begin
10660 if D in Node_Range then
10661 if D = Union_Id (Empty) then
10662 null;
10664 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
10665 Save_References (Node_Id (D));
10666 end if;
10668 elsif D in List_Range then
10669 if D = Union_Id (No_List)
10670 or else Is_Empty_List (List_Id (D))
10671 then
10672 null;
10674 else
10675 N1 := First (List_Id (D));
10676 while Present (N1) loop
10677 Save_References (N1);
10678 Next (N1);
10679 end loop;
10680 end if;
10682 -- Element list or other non-node field, nothing to do
10684 else
10685 null;
10686 end if;
10687 end Save_Global_Descendant;
10689 ---------------------
10690 -- Save_References --
10691 ---------------------
10693 -- This is the recursive procedure that does the work, once the
10694 -- enclosing generic scope has been established. We have to treat
10695 -- specially a number of node rewritings that are required by semantic
10696 -- processing and which change the kind of nodes in the generic copy:
10697 -- typically constant-folding, replacing an operator node by a string
10698 -- literal, or a selected component by an expanded name. In each of
10699 -- those cases, the transformation is propagated to the generic unit.
10701 procedure Save_References (N : Node_Id) is
10702 begin
10703 if N = Empty then
10704 null;
10706 elsif Nkind (N) = N_Character_Literal
10707 or else Nkind (N) = N_Operator_Symbol
10708 then
10709 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
10710 Reset_Entity (N);
10712 elsif Nkind (N) = N_Operator_Symbol
10713 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
10714 then
10715 Change_Operator_Symbol_To_String_Literal (N);
10716 end if;
10718 elsif Nkind (N) in N_Op then
10720 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
10722 if Nkind (N) = N_Op_Concat then
10723 Set_Is_Component_Left_Opnd (N,
10724 Is_Component_Left_Opnd (Get_Associated_Node (N)));
10726 Set_Is_Component_Right_Opnd (N,
10727 Is_Component_Right_Opnd (Get_Associated_Node (N)));
10728 end if;
10730 Reset_Entity (N);
10731 else
10732 -- Node may be transformed into call to a user-defined operator
10734 N2 := Get_Associated_Node (N);
10736 if Nkind (N2) = N_Function_Call then
10737 E := Entity (Name (N2));
10739 if Present (E)
10740 and then Is_Global (E)
10741 then
10742 Set_Etype (N, Etype (N2));
10743 else
10744 Set_Associated_Node (N, Empty);
10745 Set_Etype (N, Empty);
10746 end if;
10748 elsif Nkind (N2) = N_Integer_Literal
10749 or else Nkind (N2) = N_Real_Literal
10750 or else Nkind (N2) = N_String_Literal
10751 then
10752 if Present (Original_Node (N2))
10753 and then Nkind (Original_Node (N2)) = Nkind (N)
10754 then
10756 -- Operation was constant-folded. Whenever possible,
10757 -- recover semantic information from unfolded node,
10758 -- for ASIS use.
10760 Set_Associated_Node (N, Original_Node (N2));
10762 if Nkind (N) = N_Op_Concat then
10763 Set_Is_Component_Left_Opnd (N,
10764 Is_Component_Left_Opnd (Get_Associated_Node (N)));
10765 Set_Is_Component_Right_Opnd (N,
10766 Is_Component_Right_Opnd (Get_Associated_Node (N)));
10767 end if;
10769 Reset_Entity (N);
10771 else
10772 -- If original node is already modified, propagate
10773 -- constant-folding to template.
10775 Rewrite (N, New_Copy (N2));
10776 Set_Analyzed (N, False);
10777 end if;
10779 elsif Nkind (N2) = N_Identifier
10780 and then Ekind (Entity (N2)) = E_Enumeration_Literal
10781 then
10782 -- Same if call was folded into a literal, but in this case
10783 -- retain the entity to avoid spurious ambiguities if id is
10784 -- overloaded at the point of instantiation or inlining.
10786 Rewrite (N, New_Copy (N2));
10787 Set_Analyzed (N, False);
10788 end if;
10789 end if;
10791 -- Complete the check on operands, if node has not been
10792 -- constant-folded.
10794 if Nkind (N) in N_Op then
10795 Save_Entity_Descendants (N);
10796 end if;
10798 elsif Nkind (N) = N_Identifier then
10799 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
10801 -- If this is a discriminant reference, always save it. It is
10802 -- used in the instance to find the corresponding discriminant
10803 -- positionally rather than by name.
10805 Set_Original_Discriminant
10806 (N, Original_Discriminant (Get_Associated_Node (N)));
10807 Reset_Entity (N);
10809 else
10810 N2 := Get_Associated_Node (N);
10812 if Nkind (N2) = N_Function_Call then
10813 E := Entity (Name (N2));
10815 -- Name resolves to a call to parameterless function. If
10816 -- original entity is global, mark node as resolved.
10818 if Present (E)
10819 and then Is_Global (E)
10820 then
10821 Set_Etype (N, Etype (N2));
10822 else
10823 Set_Associated_Node (N, Empty);
10824 Set_Etype (N, Empty);
10825 end if;
10827 elsif
10828 (Nkind (N2) = N_Integer_Literal
10829 or else
10830 Nkind (N2) = N_Real_Literal)
10831 and then Is_Entity_Name (Original_Node (N2))
10832 then
10833 -- Name resolves to named number that is constant-folded,
10834 -- We must preserve the original name for ASIS use, and
10835 -- undo the constant-folding, which will be repeated in
10836 -- each instance.
10838 Set_Associated_Node (N, Original_Node (N2));
10839 Reset_Entity (N);
10841 elsif Nkind (N2) = N_String_Literal then
10843 -- Name resolves to string literal. Perform the same
10844 -- replacement in generic.
10846 Rewrite (N, New_Copy (N2));
10848 elsif Nkind (N2) = N_Explicit_Dereference then
10850 -- An identifier is rewritten as a dereference if it is
10851 -- the prefix in a selected component, and it denotes an
10852 -- access to a composite type, or a parameterless function
10853 -- call that returns an access type.
10855 -- Check whether corresponding entity in prefix is global
10857 if Is_Entity_Name (Prefix (N2))
10858 and then Present (Entity (Prefix (N2)))
10859 and then Is_Global (Entity (Prefix (N2)))
10860 then
10861 Rewrite (N,
10862 Make_Explicit_Dereference (Sloc (N),
10863 Prefix => Make_Identifier (Sloc (N),
10864 Chars => Chars (N))));
10865 Set_Associated_Node (Prefix (N), Prefix (N2));
10867 elsif Nkind (Prefix (N2)) = N_Function_Call
10868 and then Is_Global (Entity (Name (Prefix (N2))))
10869 then
10870 Rewrite (N,
10871 Make_Explicit_Dereference (Sloc (N),
10872 Prefix => Make_Function_Call (Sloc (N),
10873 Name =>
10874 Make_Identifier (Sloc (N),
10875 Chars => Chars (N)))));
10877 Set_Associated_Node
10878 (Name (Prefix (N)), Name (Prefix (N2)));
10880 else
10881 Set_Associated_Node (N, Empty);
10882 Set_Etype (N, Empty);
10883 end if;
10885 -- The subtype mark of a nominally unconstrained object
10886 -- is rewritten as a subtype indication using the bounds
10887 -- of the expression. Recover the original subtype mark.
10889 elsif Nkind (N2) = N_Subtype_Indication
10890 and then Is_Entity_Name (Original_Node (N2))
10891 then
10892 Set_Associated_Node (N, Original_Node (N2));
10893 Reset_Entity (N);
10895 else
10896 null;
10897 end if;
10898 end if;
10900 elsif Nkind (N) in N_Entity then
10901 null;
10903 else
10904 declare
10905 Loc : constant Source_Ptr := Sloc (N);
10906 Qual : Node_Id := Empty;
10907 Typ : Entity_Id := Empty;
10908 Nam : Node_Id;
10910 use Atree.Unchecked_Access;
10911 -- This code section is part of implementing an untyped tree
10912 -- traversal, so it needs direct access to node fields.
10914 begin
10915 if Nkind (N) = N_Aggregate
10916 or else
10917 Nkind (N) = N_Extension_Aggregate
10918 then
10919 N2 := Get_Associated_Node (N);
10921 if No (N2) then
10922 Typ := Empty;
10923 else
10924 Typ := Etype (N2);
10926 -- In an instance within a generic, use the name of
10927 -- the actual and not the original generic parameter.
10928 -- If the actual is global in the current generic it
10929 -- must be preserved for its instantiation.
10931 if Nkind (Parent (Typ)) = N_Subtype_Declaration
10932 and then
10933 Present (Generic_Parent_Type (Parent (Typ)))
10934 then
10935 Typ := Base_Type (Typ);
10936 Set_Etype (N2, Typ);
10937 end if;
10938 end if;
10940 if No (N2)
10941 or else No (Typ)
10942 or else not Is_Global (Typ)
10943 then
10944 Set_Associated_Node (N, Empty);
10946 -- If the aggregate is an actual in a call, it has been
10947 -- resolved in the current context, to some local type.
10948 -- The enclosing call may have been disambiguated by
10949 -- the aggregate, and this disambiguation might fail at
10950 -- instantiation time because the type to which the
10951 -- aggregate did resolve is not preserved. In order to
10952 -- preserve some of this information, we wrap the
10953 -- aggregate in a qualified expression, using the id of
10954 -- its type. For further disambiguation we qualify the
10955 -- type name with its scope (if visible) because both
10956 -- id's will have corresponding entities in an instance.
10957 -- This resolves most of the problems with missing type
10958 -- information on aggregates in instances.
10960 if Nkind (N2) = Nkind (N)
10961 and then
10962 (Nkind (Parent (N2)) = N_Procedure_Call_Statement
10963 or else Nkind (Parent (N2)) = N_Function_Call)
10964 and then Comes_From_Source (Typ)
10965 then
10966 if Is_Immediately_Visible (Scope (Typ)) then
10967 Nam := Make_Selected_Component (Loc,
10968 Prefix =>
10969 Make_Identifier (Loc, Chars (Scope (Typ))),
10970 Selector_Name =>
10971 Make_Identifier (Loc, Chars (Typ)));
10972 else
10973 Nam := Make_Identifier (Loc, Chars (Typ));
10974 end if;
10976 Qual :=
10977 Make_Qualified_Expression (Loc,
10978 Subtype_Mark => Nam,
10979 Expression => Relocate_Node (N));
10980 end if;
10981 end if;
10983 Save_Global_Descendant (Field1 (N));
10984 Save_Global_Descendant (Field2 (N));
10985 Save_Global_Descendant (Field3 (N));
10986 Save_Global_Descendant (Field5 (N));
10988 if Present (Qual) then
10989 Rewrite (N, Qual);
10990 end if;
10992 -- All other cases than aggregates
10994 else
10995 Save_Global_Descendant (Field1 (N));
10996 Save_Global_Descendant (Field2 (N));
10997 Save_Global_Descendant (Field3 (N));
10998 Save_Global_Descendant (Field4 (N));
10999 Save_Global_Descendant (Field5 (N));
11000 end if;
11001 end;
11002 end if;
11003 end Save_References;
11005 -- Start of processing for Save_Global_References
11007 begin
11008 Gen_Scope := Current_Scope;
11010 -- If the generic unit is a child unit, references to entities in
11011 -- the parent are treated as local, because they will be resolved
11012 -- anew in the context of the instance of the parent.
11014 while Is_Child_Unit (Gen_Scope)
11015 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
11016 loop
11017 Gen_Scope := Scope (Gen_Scope);
11018 end loop;
11020 Save_References (N);
11021 end Save_Global_References;
11023 --------------------------------------
11024 -- Set_Copied_Sloc_For_Inlined_Body --
11025 --------------------------------------
11027 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
11028 begin
11029 Create_Instantiation_Source (N, E, True, S_Adjustment);
11030 end Set_Copied_Sloc_For_Inlined_Body;
11032 ---------------------
11033 -- Set_Instance_Of --
11034 ---------------------
11036 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
11037 begin
11038 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
11039 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
11040 Generic_Renamings.Increment_Last;
11041 end Set_Instance_Of;
11043 --------------------
11044 -- Set_Next_Assoc --
11045 --------------------
11047 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
11048 begin
11049 Generic_Renamings.Table (E).Next_In_HTable := Next;
11050 end Set_Next_Assoc;
11052 -------------------
11053 -- Start_Generic --
11054 -------------------
11056 procedure Start_Generic is
11057 begin
11058 -- ??? I am sure more things could be factored out in this
11059 -- routine. Should probably be done at a later stage.
11061 Generic_Flags.Increment_Last;
11062 Generic_Flags.Table (Generic_Flags.Last) := Inside_A_Generic;
11063 Inside_A_Generic := True;
11065 Expander_Mode_Save_And_Set (False);
11066 end Start_Generic;
11068 ----------------------
11069 -- Set_Instance_Env --
11070 ----------------------
11072 procedure Set_Instance_Env
11073 (Gen_Unit : Entity_Id;
11074 Act_Unit : Entity_Id)
11076 begin
11077 -- Regardless of the current mode, predefined units are analyzed in
11078 -- the most current Ada mode, and earlier version Ada checks do not
11079 -- apply to predefined units.
11081 -- Why is this not using the routine Opt.Set_Opt_Config_Switches ???
11083 if Is_Internal_File_Name
11084 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
11085 Renamings_Included => True) then
11086 Ada_Version := Ada_Version_Type'Last;
11087 end if;
11089 Current_Instantiated_Parent := (Gen_Unit, Act_Unit, Assoc_Null);
11090 end Set_Instance_Env;
11092 -----------------
11093 -- Switch_View --
11094 -----------------
11096 procedure Switch_View (T : Entity_Id) is
11097 BT : constant Entity_Id := Base_Type (T);
11098 Priv_Elmt : Elmt_Id := No_Elmt;
11099 Priv_Sub : Entity_Id;
11101 begin
11102 -- T may be private but its base type may have been exchanged through
11103 -- some other occurrence, in which case there is nothing to switch
11104 -- besides T itself. Note that a private dependent subtype of a private
11105 -- type might not have been switched even if the base type has been,
11106 -- because of the last branch of Check_Private_View (see comment there).
11108 if not Is_Private_Type (BT) then
11109 Prepend_Elmt (Full_View (T), Exchanged_Views);
11110 Exchange_Declarations (T);
11111 return;
11112 end if;
11114 Priv_Elmt := First_Elmt (Private_Dependents (BT));
11116 if Present (Full_View (BT)) then
11117 Prepend_Elmt (Full_View (BT), Exchanged_Views);
11118 Exchange_Declarations (BT);
11119 end if;
11121 while Present (Priv_Elmt) loop
11122 Priv_Sub := (Node (Priv_Elmt));
11124 -- We avoid flipping the subtype if the Etype of its full
11125 -- view is private because this would result in a malformed
11126 -- subtype. This occurs when the Etype of the subtype full
11127 -- view is the full view of the base type (and since the
11128 -- base types were just switched, the subtype is pointing
11129 -- to the wrong view). This is currently the case for
11130 -- tagged record types, access types (maybe more?) and
11131 -- needs to be resolved. ???
11133 if Present (Full_View (Priv_Sub))
11134 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
11135 then
11136 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
11137 Exchange_Declarations (Priv_Sub);
11138 end if;
11140 Next_Elmt (Priv_Elmt);
11141 end loop;
11142 end Switch_View;
11144 -----------------------------
11145 -- Valid_Default_Attribute --
11146 -----------------------------
11148 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
11149 Attr_Id : constant Attribute_Id :=
11150 Get_Attribute_Id (Attribute_Name (Def));
11151 T : constant Entity_Id := Entity (Prefix (Def));
11152 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
11153 F : Entity_Id;
11154 Num_F : Int;
11155 OK : Boolean;
11157 begin
11158 if No (T)
11159 or else T = Any_Id
11160 then
11161 return;
11162 end if;
11164 Num_F := 0;
11165 F := First_Formal (Nam);
11166 while Present (F) loop
11167 Num_F := Num_F + 1;
11168 Next_Formal (F);
11169 end loop;
11171 case Attr_Id is
11172 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
11173 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
11174 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
11175 Attribute_Unbiased_Rounding =>
11176 OK := Is_Fun
11177 and then Num_F = 1
11178 and then Is_Floating_Point_Type (T);
11180 when Attribute_Image | Attribute_Pred | Attribute_Succ |
11181 Attribute_Value | Attribute_Wide_Image |
11182 Attribute_Wide_Value =>
11183 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
11185 when Attribute_Max | Attribute_Min =>
11186 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
11188 when Attribute_Input =>
11189 OK := (Is_Fun and then Num_F = 1);
11191 when Attribute_Output | Attribute_Read | Attribute_Write =>
11192 OK := (not Is_Fun and then Num_F = 2);
11194 when others =>
11195 OK := False;
11196 end case;
11198 if not OK then
11199 Error_Msg_N ("attribute reference has wrong profile for subprogram",
11200 Def);
11201 end if;
11202 end Valid_Default_Attribute;
11204 end Sem_Ch12;