2005-12-29 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / ada / sem_ch12.adb
blob5e8e6dc1d9d3a4e16cfddcabe1960d9c98d1bdce
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-2005, 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
84 -- share generic instantiations (for now). Analysis of a generic definition
85 -- does not perform any expansion action, but the expander must be called
86 -- on the tree for each instantiation, because the expansion may of course
87 -- depend 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 a
225 -- static error which must be detected at compile time. The detection
226 -- of these circularities is carried out at the point that we insert
227 -- a generic instance spec or body. If there is a circularity, then
228 -- the analysis of the offending spec or body will eventually result
229 -- in trying to load the same unit again, and we detect this problem
230 -- as we analyze the package instantiation for the second time.
232 -- At least in some cases after we have detected the circularity, we
233 -- get into trouble if we try to keep going. The following flag is
234 -- set if a circularity is detected, and used to abandon compilation
235 -- after the 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 -- Local subprograms --
243 -----------------------
245 procedure Abandon_Instantiation (N : Node_Id);
246 pragma No_Return (Abandon_Instantiation);
247 -- Posts an error message "instantiation abandoned" at the indicated
248 -- node and then raises the exception Instantiation_Error to do it.
250 procedure Analyze_Formal_Array_Type
251 (T : in out Entity_Id;
252 Def : Node_Id);
253 -- A formal array type is treated like an array type declaration, and
254 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
255 -- in-out, because in the case of an anonymous type the entity is
256 -- actually created in the procedure.
258 -- The following procedures treat other kinds of formal parameters
260 procedure Analyze_Formal_Derived_Interface_Type
261 (T : Entity_Id;
262 Def : Node_Id);
264 procedure Analyze_Formal_Derived_Type
265 (N : Node_Id;
266 T : Entity_Id;
267 Def : Node_Id);
269 -- The following subprograms create abbreviated declarations for formal
270 -- scalar types. We introduce an anonymous base of the proper class for
271 -- each of them, and define the formals as constrained first subtypes of
272 -- their bases. The bounds are expressions that are non-static in the
273 -- generic.
275 procedure Analyze_Formal_Decimal_Fixed_Point_Type
276 (T : Entity_Id; Def : Node_Id);
277 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
278 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
279 procedure Analyze_Formal_Interface_Type (T : Entity_Id; Def : Node_Id);
280 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
281 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
282 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
283 (T : Entity_Id; Def : Node_Id);
285 procedure Analyze_Formal_Private_Type
286 (N : Node_Id;
287 T : Entity_Id;
288 Def : Node_Id);
289 -- This needs comments???
291 procedure Analyze_Generic_Formal_Part (N : Node_Id);
293 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
294 -- This needs comments ???
296 function Analyze_Associations
297 (I_Node : Node_Id;
298 Formals : List_Id;
299 F_Copy : List_Id) return List_Id;
300 -- At instantiation time, build the list of associations between formals
301 -- and actuals. Each association becomes a renaming declaration for the
302 -- formal entity. F_Copy is the analyzed list of formals in the generic
303 -- copy. It is used to apply legality checks to the actuals. I_Node is the
304 -- instantiation node itself.
306 procedure Analyze_Subprogram_Instantiation
307 (N : Node_Id;
308 K : Entity_Kind);
310 procedure Build_Instance_Compilation_Unit_Nodes
311 (N : Node_Id;
312 Act_Body : Node_Id;
313 Act_Decl : Node_Id);
314 -- This procedure is used in the case where the generic instance of a
315 -- subprogram body or package body is a library unit. In this case, the
316 -- original library unit node for the generic instantiation must be
317 -- replaced by the resulting generic body, and a link made to a new
318 -- compilation unit node for the generic declaration. The argument N is
319 -- the original generic instantiation. Act_Body and Act_Decl are the body
320 -- and declaration of the instance (either package body and declaration
321 -- nodes or subprogram body and declaration nodes depending on the case).
322 -- On return, the node N has been rewritten with the actual body.
324 procedure Check_Formal_Packages (P_Id : Entity_Id);
325 -- Apply the following to all formal packages in generic associations
327 procedure Check_Formal_Package_Instance
328 (Formal_Pack : Entity_Id;
329 Actual_Pack : Entity_Id);
330 -- Verify that the actuals of the actual instance match the actuals of
331 -- the template for a formal package that is not declared with a box.
333 procedure Check_Forward_Instantiation (Decl : Node_Id);
334 -- If the generic is a local entity and the corresponding body has not
335 -- been seen yet, flag enclosing packages to indicate that it will be
336 -- elaborated after the generic body. Subprograms declared in the same
337 -- package cannot be inlined by the front-end because front-end inlining
338 -- requires a strict linear order of elaboration.
340 procedure Check_Hidden_Child_Unit
341 (N : Node_Id;
342 Gen_Unit : Entity_Id;
343 Act_Decl_Id : Entity_Id);
344 -- If the generic unit is an implicit child instance within a parent
345 -- instance, we need to make an explicit test that it is not hidden by
346 -- a child instance of the same name and parent.
348 procedure Check_Private_View (N : Node_Id);
349 -- Check whether the type of a generic entity has a different view between
350 -- the point of generic analysis and the point of instantiation. If the
351 -- view has changed, then at the point of instantiation we restore the
352 -- correct view to perform semantic analysis of the instance, and reset
353 -- the current view after instantiation. The processing is driven by the
354 -- current private status of the type of the node, and Has_Private_View,
355 -- a flag that is set at the point of generic compilation. If view and
356 -- flag are inconsistent then the type is updated appropriately.
358 procedure Check_Generic_Actuals
359 (Instance : Entity_Id;
360 Is_Formal_Box : Boolean);
361 -- Similar to previous one. Check the actuals in the instantiation,
362 -- whose views can change between the point of instantiation and the point
363 -- of instantiation of the body. In addition, mark the generic renamings
364 -- as generic actuals, so that they are not compatible with other actuals.
365 -- Recurse on an actual that is a formal package whose declaration has
366 -- a box.
368 function Contains_Instance_Of
369 (Inner : Entity_Id;
370 Outer : Entity_Id;
371 N : Node_Id) return Boolean;
372 -- Inner is instantiated within the generic Outer. Check whether Inner
373 -- directly or indirectly contains an instance of Outer or of one of its
374 -- parents, in the case of a subunit. Each generic unit holds a list of
375 -- the entities instantiated within (at any depth). This procedure
376 -- determines whether the set of such lists contains a cycle, i.e. an
377 -- illegal circular instantiation.
379 function Denotes_Formal_Package
380 (Pack : Entity_Id;
381 On_Exit : Boolean := False) return Boolean;
382 -- Returns True if E is a formal package of an enclosing generic, or
383 -- the actual for such a formal in an enclosing instantiation. If such
384 -- a package is used as a formal in an nested generic, or as an actual
385 -- in a nested instantiation, the visibility of ITS formals should not
386 -- be modified. When called from within Restore_Private_Views, the flag
387 -- On_Exit is true, to indicate that the search for a possible enclosing
388 -- instance should ignore the current one.
390 function Find_Actual_Type
391 (Typ : Entity_Id;
392 Gen_Scope : Entity_Id) return Entity_Id;
393 -- When validating the actual types of a child instance, check whether
394 -- the formal is a formal type of the parent unit, and retrieve the current
395 -- actual for it. Typ is the entity in the analyzed formal type declaration
396 -- (component or index type of an array type) and Gen_Scope is the scope of
397 -- the analyzed formal array type.
399 function In_Same_Declarative_Part
400 (F_Node : Node_Id;
401 Inst : Node_Id) return Boolean;
402 -- True if the instantiation Inst and the given freeze_node F_Node appear
403 -- within the same declarative part, ignoring subunits, but with no inter-
404 -- vening suprograms or concurrent units. If true, the freeze node
405 -- of the instance can be placed after the freeze node of the parent,
406 -- which it itself an instance.
408 function In_Main_Context (E : Entity_Id) return Boolean;
409 -- Check whether an instantiation is in the context of the main unit.
410 -- Used to determine whether its body should be elaborated to allow
411 -- front-end inlining.
413 procedure Set_Instance_Env
414 (Gen_Unit : Entity_Id;
415 Act_Unit : Entity_Id);
416 -- Save current instance on saved environment, to be used to determine
417 -- the global status of entities in nested instances. Part of Save_Env.
418 -- called after verifying that the generic unit is legal for the instance.
420 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
421 -- Associate analyzed generic parameter with corresponding
422 -- instance. Used for semantic checks at instantiation time.
424 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
425 -- Traverse the Exchanged_Views list to see if a type was private
426 -- and has already been flipped during this phase of instantiation.
428 procedure Hide_Current_Scope;
429 -- When compiling a generic child unit, the parent context must be
430 -- present, but the instance and all entities that may be generated
431 -- must be inserted in the current scope. We leave the current scope
432 -- on the stack, but make its entities invisible to avoid visibility
433 -- problems. This is reversed at the end of instantiations. This is
434 -- not done for the instantiation of the bodies, which only require the
435 -- instances of the generic parents to be in scope.
437 procedure Install_Body
438 (Act_Body : Node_Id;
439 N : Node_Id;
440 Gen_Body : Node_Id;
441 Gen_Decl : Node_Id);
442 -- If the instantiation happens textually before the body of the generic,
443 -- the instantiation of the body must be analyzed after the generic body,
444 -- and not at the point of instantiation. Such early instantiations can
445 -- happen if the generic and the instance appear in a package declaration
446 -- because the generic body can only appear in the corresponding package
447 -- body. Early instantiations can also appear if generic, instance and
448 -- body are all in the declarative part of a subprogram or entry. Entities
449 -- of packages that are early instantiations are delayed, and their freeze
450 -- node appears after the generic body.
452 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id);
453 -- Insert freeze node at the end of the declarative part that includes the
454 -- instance node N. If N is in the visible part of an enclosing package
455 -- declaration, the freeze node has to be inserted at the end of the
456 -- private declarations, if any.
458 procedure Freeze_Subprogram_Body
459 (Inst_Node : Node_Id;
460 Gen_Body : Node_Id;
461 Pack_Id : Entity_Id);
462 -- The generic body may appear textually after the instance, including
463 -- in the proper body of a stub, or within a different package instance.
464 -- Given that the instance can only be elaborated after the generic, we
465 -- place freeze_nodes for the instance and/or for packages that may enclose
466 -- the instance and the generic, so that the back-end can establish the
467 -- proper order of elaboration.
469 procedure Init_Env;
470 -- Establish environment for subsequent instantiation. Separated from
471 -- Save_Env because data-structures for visibility handling must be
472 -- initialized before call to Check_Generic_Child_Unit.
474 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
475 -- When compiling an instance of a child unit the parent (which is
476 -- itself an instance) is an enclosing scope that must be made
477 -- immediately visible. This procedure is also used to install the non-
478 -- generic parent of a generic child unit when compiling its body, so
479 -- that full views of types in the parent are made visible.
481 procedure Remove_Parent (In_Body : Boolean := False);
482 -- Reverse effect after instantiation of child is complete
484 procedure Inline_Instance_Body
485 (N : Node_Id;
486 Gen_Unit : Entity_Id;
487 Act_Decl : Node_Id);
488 -- If front-end inlining is requested, instantiate the package body,
489 -- and preserve the visibility of its compilation unit, to insure
490 -- that successive instantiations succeed.
492 -- The functions Instantiate_XXX perform various legality checks and build
493 -- the declarations for instantiated generic parameters. In all of these
494 -- Formal is the entity in the generic unit, Actual is the entity of
495 -- expression in the generic associations, and Analyzed_Formal is the
496 -- formal in the generic copy, which contains the semantic information to
497 -- be used to validate the actual.
499 function Instantiate_Object
500 (Formal : Node_Id;
501 Actual : Node_Id;
502 Analyzed_Formal : Node_Id) return List_Id;
504 function Instantiate_Type
505 (Formal : Node_Id;
506 Actual : Node_Id;
507 Analyzed_Formal : Node_Id;
508 Actual_Decls : List_Id) return Node_Id;
510 function Instantiate_Formal_Subprogram
511 (Formal : Node_Id;
512 Actual : Node_Id;
513 Analyzed_Formal : Node_Id) return Node_Id;
515 function Instantiate_Formal_Package
516 (Formal : Node_Id;
517 Actual : Node_Id;
518 Analyzed_Formal : Node_Id) return List_Id;
519 -- If the formal package is declared with a box, special visibility rules
520 -- apply to its formals: they are in the visible part of the package. This
521 -- is true in the declarative region of the formal package, that is to say
522 -- in the enclosing generic or instantiation. For an instantiation, the
523 -- parameters of the formal package are made visible in an explicit step.
524 -- Furthermore, if the actual is a visible use_clause, these formals must
525 -- be made potentially use_visible as well. On exit from the enclosing
526 -- instantiation, the reverse must be done.
528 -- For a formal package declared without a box, there are conformance rules
529 -- that apply to the actuals in the generic declaration and the actuals of
530 -- the actual package in the enclosing instantiation. The simplest way to
531 -- apply these rules is to repeat the instantiation of the formal package
532 -- in the context of the enclosing instance, and compare the generic
533 -- associations of this instantiation with those of the actual package.
535 function Is_In_Main_Unit (N : Node_Id) return Boolean;
536 -- Test if given node is in the main unit
538 procedure Load_Parent_Of_Generic (N : Node_Id; Spec : Node_Id);
539 -- If the generic appears in a separate non-generic library unit,
540 -- load the corresponding body to retrieve the body of the generic.
541 -- N is the node for the generic instantiation, Spec is the generic
542 -- package declaration.
544 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
545 -- Add the context clause of the unit containing a generic unit to
546 -- an instantiation that is a compilation unit.
548 function Get_Associated_Node (N : Node_Id) return Node_Id;
549 -- In order to propagate semantic information back from the analyzed
550 -- copy to the original generic, we maintain links between selected nodes
551 -- in the generic and their corresponding copies. At the end of generic
552 -- analysis, the routine Save_Global_References traverses the generic
553 -- tree, examines the semantic information, and preserves the links to
554 -- those nodes that contain global information. At instantiation, the
555 -- information from the associated node is placed on the new copy, so
556 -- that name resolution is not repeated.
558 -- Three kinds of source nodes have associated nodes:
560 -- a) those that can reference (denote) entities, that is identifiers,
561 -- character literals, expanded_names, operator symbols, operators,
562 -- and attribute reference nodes. These nodes have an Entity field
563 -- and are the set of nodes that are in N_Has_Entity.
565 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
567 -- c) selected components (N_Selected_Component)
569 -- For the first class, the associated node preserves the entity if it is
570 -- global. If the generic contains nested instantiations, the associated
571 -- node itself has been recopied, and a chain of them must be followed.
573 -- For aggregates, the associated node allows retrieval of the type, which
574 -- may otherwise not appear in the generic. The view of this type may be
575 -- different between generic and instantiation, and the full view can be
576 -- installed before the instantiation is analyzed. For aggregates of
577 -- type extensions, the same view exchange may have to be performed for
578 -- some of the ancestor types, if their view is private at the point of
579 -- instantiation.
581 -- Nodes that are selected components in the parse tree may be rewritten
582 -- as expanded names after resolution, and must be treated as potential
583 -- entity holders. which is why they also have an Associated_Node.
585 -- Nodes that do not come from source, such as freeze nodes, do not appear
586 -- in the generic tree, and need not have an associated node.
588 -- The associated node is stored in the Associated_Node field. Note that
589 -- this field overlaps Entity, which is fine, because the whole point is
590 -- that we don't need or want the normal Entity field in this situation.
592 procedure Move_Freeze_Nodes
593 (Out_Of : Entity_Id;
594 After : Node_Id;
595 L : List_Id);
596 -- Freeze nodes can be generated in the analysis of a generic unit, but
597 -- will not be seen by the back-end. It is necessary to move those nodes
598 -- to the enclosing scope if they freeze an outer entity. We place them
599 -- at the end of the enclosing generic package, which is semantically
600 -- neutral.
602 procedure Pre_Analyze_Actuals (N : Node_Id);
603 -- Analyze actuals to perform name resolution. Full resolution is done
604 -- later, when the expected types are known, but names have to be captured
605 -- before installing parents of generics, that are not visible for the
606 -- actuals themselves.
608 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
609 -- Verify that an attribute that appears as the default for a formal
610 -- subprogram is a function or procedure with the correct profile.
612 -------------------------------------------
613 -- Data Structures for Generic Renamings --
614 -------------------------------------------
616 -- The map Generic_Renamings associates generic entities with their
617 -- corresponding actuals. Currently used to validate type instances.
618 -- It will eventually be used for all generic parameters to eliminate
619 -- the need for overload resolution in the instance.
621 type Assoc_Ptr is new Int;
623 Assoc_Null : constant Assoc_Ptr := -1;
625 type Assoc is record
626 Gen_Id : Entity_Id;
627 Act_Id : Entity_Id;
628 Next_In_HTable : Assoc_Ptr;
629 end record;
631 package Generic_Renamings is new Table.Table
632 (Table_Component_Type => Assoc,
633 Table_Index_Type => Assoc_Ptr,
634 Table_Low_Bound => 0,
635 Table_Initial => 10,
636 Table_Increment => 100,
637 Table_Name => "Generic_Renamings");
639 -- Variable to hold enclosing instantiation. When the environment is
640 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
642 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
644 -- Hash table for associations
646 HTable_Size : constant := 37;
647 type HTable_Range is range 0 .. HTable_Size - 1;
649 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
650 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
651 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
652 function Hash (F : Entity_Id) return HTable_Range;
654 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
655 Header_Num => HTable_Range,
656 Element => Assoc,
657 Elmt_Ptr => Assoc_Ptr,
658 Null_Ptr => Assoc_Null,
659 Set_Next => Set_Next_Assoc,
660 Next => Next_Assoc,
661 Key => Entity_Id,
662 Get_Key => Get_Gen_Id,
663 Hash => Hash,
664 Equal => "=");
666 Exchanged_Views : Elist_Id;
667 -- This list holds the private views that have been exchanged during
668 -- instantiation to restore the visibility of the generic declaration.
669 -- (see comments above). After instantiation, the current visibility is
670 -- reestablished by means of a traversal of this list.
672 Hidden_Entities : Elist_Id;
673 -- This list holds the entities of the current scope that are removed
674 -- from immediate visibility when instantiating a child unit. Their
675 -- visibility is restored in Remove_Parent.
677 -- Because instantiations can be recursive, the following must be saved
678 -- on entry and restored on exit from an instantiation (spec or body).
679 -- This is done by the two procedures Save_Env and Restore_Env. For
680 -- package and subprogram instantiations (but not for the body instances)
681 -- the action of Save_Env is done in two steps: Init_Env is called before
682 -- Check_Generic_Child_Unit, because setting the parent instances requires
683 -- that the visibility data structures be properly initialized. Once the
684 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
686 Parent_Unit_Visible : Boolean := False;
687 -- Parent_Unit_Visible is used when the generic is a child unit, and
688 -- indicates whether the ultimate parent of the generic is visible in the
689 -- instantiation environment. It is used to reset the visibility of the
690 -- parent at the end of the instantiation (see Remove_Parent).
692 Instance_Parent_Unit : Entity_Id := Empty;
693 -- This records the ultimate parent unit of an instance of a generic
694 -- child unit and is used in conjunction with Parent_Unit_Visible to
695 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
697 type Instance_Env is record
698 Ada_Version : Ada_Version_Type;
699 Ada_Version_Explicit : Ada_Version_Type;
700 Instantiated_Parent : Assoc;
701 Exchanged_Views : Elist_Id;
702 Hidden_Entities : Elist_Id;
703 Current_Sem_Unit : Unit_Number_Type;
704 Parent_Unit_Visible : Boolean := False;
705 Instance_Parent_Unit : Entity_Id := Empty;
706 end record;
708 package Instance_Envs is new Table.Table (
709 Table_Component_Type => Instance_Env,
710 Table_Index_Type => Int,
711 Table_Low_Bound => 0,
712 Table_Initial => 32,
713 Table_Increment => 100,
714 Table_Name => "Instance_Envs");
716 procedure Restore_Private_Views
717 (Pack_Id : Entity_Id;
718 Is_Package : Boolean := True);
719 -- Restore the private views of external types, and unmark the generic
720 -- renamings of actuals, so that they become comptible subtypes again.
721 -- For subprograms, Pack_Id is the package constructed to hold the
722 -- renamings.
724 procedure Switch_View (T : Entity_Id);
725 -- Switch the partial and full views of a type and its private
726 -- dependents (i.e. its subtypes and derived types).
728 ------------------------------------
729 -- Structures for Error Reporting --
730 ------------------------------------
732 Instantiation_Node : Node_Id;
733 -- Used by subprograms that validate instantiation of formal parameters
734 -- where there might be no actual on which to place the error message.
735 -- Also used to locate the instantiation node for generic subunits.
737 Instantiation_Error : exception;
738 -- When there is a semantic error in the generic parameter matching,
739 -- there is no point in continuing the instantiation, because the
740 -- number of cascaded errors is unpredictable. This exception aborts
741 -- the instantiation process altogether.
743 S_Adjustment : Sloc_Adjustment;
744 -- Offset created for each node in an instantiation, in order to keep
745 -- track of the source position of the instantiation in each of its nodes.
746 -- A subsequent semantic error or warning on a construct of the instance
747 -- points to both places: the original generic node, and the point of
748 -- instantiation. See Sinput and Sinput.L for additional details.
750 ------------------------------------------------------------
751 -- Data structure for keeping track when inside a Generic --
752 ------------------------------------------------------------
754 -- The following table is used to save values of the Inside_A_Generic
755 -- flag (see spec of Sem) when they are saved by Start_Generic.
757 package Generic_Flags is new Table.Table (
758 Table_Component_Type => Boolean,
759 Table_Index_Type => Int,
760 Table_Low_Bound => 0,
761 Table_Initial => 32,
762 Table_Increment => 200,
763 Table_Name => "Generic_Flags");
765 ---------------------------
766 -- Abandon_Instantiation --
767 ---------------------------
769 procedure Abandon_Instantiation (N : Node_Id) is
770 begin
771 Error_Msg_N ("instantiation abandoned!", N);
772 raise Instantiation_Error;
773 end Abandon_Instantiation;
775 --------------------------
776 -- Analyze_Associations --
777 --------------------------
779 function Analyze_Associations
780 (I_Node : Node_Id;
781 Formals : List_Id;
782 F_Copy : List_Id) return List_Id
784 Actual_Types : constant Elist_Id := New_Elmt_List;
785 Assoc : constant List_Id := New_List;
786 Defaults : constant Elist_Id := New_Elmt_List;
787 Gen_Unit : constant Entity_Id := Defining_Entity (Parent (F_Copy));
788 Actuals : List_Id;
789 Actual : Node_Id;
790 Formal : Node_Id;
791 Next_Formal : Node_Id;
792 Temp_Formal : Node_Id;
793 Analyzed_Formal : Node_Id;
794 Match : Node_Id;
795 Named : Node_Id;
796 First_Named : Node_Id := Empty;
797 Found_Assoc : Node_Id;
798 Is_Named_Assoc : Boolean;
799 Num_Matched : Int := 0;
800 Num_Actuals : Int := 0;
802 function Matching_Actual
803 (F : Entity_Id;
804 A_F : Entity_Id) return Node_Id;
805 -- Find actual that corresponds to a given a formal parameter. If the
806 -- actuals are positional, return the next one, if any. If the actuals
807 -- are named, scan the parameter associations to find the right one.
808 -- A_F is the corresponding entity in the analyzed generic,which is
809 -- placed on the selector name for ASIS use.
811 procedure Set_Analyzed_Formal;
812 -- Find the node in the generic copy that corresponds to a given formal.
813 -- The semantic information on this node is used to perform legality
814 -- checks on the actuals. Because semantic analysis can introduce some
815 -- anonymous entities or modify the declaration node itself, the
816 -- correspondence between the two lists is not one-one. In addition to
817 -- anonymous types, the presence a formal equality will introduce an
818 -- implicit declaration for the corresponding inequality.
820 ---------------------
821 -- Matching_Actual --
822 ---------------------
824 function Matching_Actual
825 (F : Entity_Id;
826 A_F : Entity_Id) return Node_Id
828 Found : Node_Id;
829 Prev : Node_Id;
831 begin
832 Is_Named_Assoc := False;
834 -- End of list of purely positional parameters
836 if No (Actual) then
837 Found := Empty;
839 -- Case of positional parameter corresponding to current formal
841 elsif No (Selector_Name (Actual)) then
842 Found := Explicit_Generic_Actual_Parameter (Actual);
843 Found_Assoc := Actual;
844 Num_Matched := Num_Matched + 1;
845 Next (Actual);
847 -- Otherwise scan list of named actuals to find the one with the
848 -- desired name. All remaining actuals have explicit names.
850 else
851 Is_Named_Assoc := True;
852 Found := Empty;
853 Prev := Empty;
855 while Present (Actual) loop
856 if Chars (Selector_Name (Actual)) = Chars (F) then
857 Found := Explicit_Generic_Actual_Parameter (Actual);
858 Set_Entity (Selector_Name (Actual), A_F);
859 Set_Etype (Selector_Name (Actual), Etype (A_F));
860 Generate_Reference (A_F, Selector_Name (Actual));
861 Found_Assoc := Actual;
862 Num_Matched := Num_Matched + 1;
863 exit;
864 end if;
866 Prev := Actual;
867 Next (Actual);
868 end loop;
870 -- Reset for subsequent searches. In most cases the named
871 -- associations are in order. If they are not, we reorder them
872 -- to avoid scanning twice the same actual. This is not just a
873 -- question of efficiency: there may be multiple defaults with
874 -- boxes that have the same name. In a nested instantiation we
875 -- insert actuals for those defaults, and cannot rely on their
876 -- names to disambiguate them.
878 if Actual = First_Named then
879 Next (First_Named);
881 elsif Present (Actual) then
882 Insert_Before (First_Named, Remove_Next (Prev));
883 end if;
885 Actual := First_Named;
886 end if;
888 return Found;
889 end Matching_Actual;
891 -------------------------
892 -- Set_Analyzed_Formal --
893 -------------------------
895 procedure Set_Analyzed_Formal is
896 Kind : Node_Kind;
897 begin
898 while Present (Analyzed_Formal) loop
899 Kind := Nkind (Analyzed_Formal);
901 case Nkind (Formal) is
903 when N_Formal_Subprogram_Declaration =>
904 exit when Kind in N_Formal_Subprogram_Declaration
905 and then
906 Chars
907 (Defining_Unit_Name (Specification (Formal))) =
908 Chars
909 (Defining_Unit_Name (Specification (Analyzed_Formal)));
911 when N_Formal_Package_Declaration =>
912 exit when
913 Kind = N_Formal_Package_Declaration
914 or else
915 Kind = N_Generic_Package_Declaration;
917 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
919 when others =>
921 -- Skip freeze nodes, and nodes inserted to replace
922 -- unrecognized pragmas.
924 exit when
925 Kind not in N_Formal_Subprogram_Declaration
926 and then Kind /= N_Subprogram_Declaration
927 and then Kind /= N_Freeze_Entity
928 and then Kind /= N_Null_Statement
929 and then Kind /= N_Itype_Reference
930 and then Chars (Defining_Identifier (Formal)) =
931 Chars (Defining_Identifier (Analyzed_Formal));
932 end case;
934 Next (Analyzed_Formal);
935 end loop;
937 end Set_Analyzed_Formal;
939 -- Start of processing for Analyze_Associations
941 begin
942 -- If named associations are present, save the first named association
943 -- (it may of course be Empty) to facilitate subsequent name search.
945 Actuals := Generic_Associations (I_Node);
947 if Present (Actuals) then
948 First_Named := First (Actuals);
950 while Present (First_Named)
951 and then No (Selector_Name (First_Named))
952 loop
953 Num_Actuals := Num_Actuals + 1;
954 Next (First_Named);
955 end loop;
956 end if;
958 Named := First_Named;
959 while Present (Named) loop
960 if No (Selector_Name (Named)) then
961 Error_Msg_N ("invalid positional actual after named one", Named);
962 Abandon_Instantiation (Named);
963 end if;
965 -- A named association may lack an actual parameter, if it was
966 -- introduced for a default subprogram that turns out to be local
967 -- to the outer instantiation.
969 if Present (Explicit_Generic_Actual_Parameter (Named)) then
970 Num_Actuals := Num_Actuals + 1;
971 end if;
973 Next (Named);
974 end loop;
976 if Present (Formals) then
977 Formal := First_Non_Pragma (Formals);
978 Analyzed_Formal := First_Non_Pragma (F_Copy);
980 if Present (Actuals) then
981 Actual := First (Actuals);
983 -- All formals should have default values
985 else
986 Actual := Empty;
987 end if;
989 while Present (Formal) loop
990 Set_Analyzed_Formal;
991 Next_Formal := Next_Non_Pragma (Formal);
993 case Nkind (Formal) is
994 when N_Formal_Object_Declaration =>
995 Match :=
996 Matching_Actual (
997 Defining_Identifier (Formal),
998 Defining_Identifier (Analyzed_Formal));
1000 Append_List
1001 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1002 Assoc);
1004 when N_Formal_Type_Declaration =>
1005 Match :=
1006 Matching_Actual (
1007 Defining_Identifier (Formal),
1008 Defining_Identifier (Analyzed_Formal));
1010 if No (Match) then
1011 Error_Msg_Sloc := Sloc (Gen_Unit);
1012 Error_Msg_NE
1013 ("missing actual&",
1014 Instantiation_Node, Defining_Identifier (Formal));
1015 Error_Msg_NE ("\in instantiation of & declared#",
1016 Instantiation_Node, Gen_Unit);
1017 Abandon_Instantiation (Instantiation_Node);
1019 else
1020 Analyze (Match);
1021 Append_To (Assoc,
1022 Instantiate_Type
1023 (Formal, Match, Analyzed_Formal, Assoc));
1025 -- An instantiation is a freeze point for the actuals,
1026 -- unless this is a rewritten formal package.
1028 if Nkind (I_Node) /= N_Formal_Package_Declaration then
1029 Append_Elmt (Entity (Match), Actual_Types);
1030 end if;
1031 end if;
1033 -- A remote access-to-class-wide type must not be an
1034 -- actual parameter for a generic formal of an access
1035 -- type (E.2.2 (17)).
1037 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1038 and then
1039 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1040 N_Access_To_Object_Definition
1041 then
1042 Validate_Remote_Access_To_Class_Wide_Type (Match);
1043 end if;
1045 when N_Formal_Subprogram_Declaration =>
1046 Match :=
1047 Matching_Actual (
1048 Defining_Unit_Name (Specification (Formal)),
1049 Defining_Unit_Name (Specification (Analyzed_Formal)));
1051 -- If the formal subprogram has the same name as
1052 -- another formal subprogram of the generic, then
1053 -- a named association is illegal (12.3(9)). Exclude
1054 -- named associations that are generated for a nested
1055 -- instance.
1057 if Present (Match)
1058 and then Is_Named_Assoc
1059 and then Comes_From_Source (Found_Assoc)
1060 then
1061 Temp_Formal := First (Formals);
1062 while Present (Temp_Formal) loop
1063 if Nkind (Temp_Formal) in
1064 N_Formal_Subprogram_Declaration
1065 and then Temp_Formal /= Formal
1066 and then
1067 Chars (Selector_Name (Found_Assoc)) =
1068 Chars (Defining_Unit_Name
1069 (Specification (Temp_Formal)))
1070 then
1071 Error_Msg_N
1072 ("name not allowed for overloaded formal",
1073 Found_Assoc);
1074 Abandon_Instantiation (Instantiation_Node);
1075 end if;
1077 Next (Temp_Formal);
1078 end loop;
1079 end if;
1081 Append_To (Assoc,
1082 Instantiate_Formal_Subprogram
1083 (Formal, Match, Analyzed_Formal));
1085 if No (Match)
1086 and then Box_Present (Formal)
1087 then
1088 Append_Elmt
1089 (Defining_Unit_Name (Specification (Last (Assoc))),
1090 Defaults);
1091 end if;
1093 when N_Formal_Package_Declaration =>
1094 Match :=
1095 Matching_Actual (
1096 Defining_Identifier (Formal),
1097 Defining_Identifier (Original_Node (Analyzed_Formal)));
1099 if No (Match) then
1100 Error_Msg_Sloc := Sloc (Gen_Unit);
1101 Error_Msg_NE
1102 ("missing actual&",
1103 Instantiation_Node, Defining_Identifier (Formal));
1104 Error_Msg_NE ("\in instantiation of & declared#",
1105 Instantiation_Node, Gen_Unit);
1107 Abandon_Instantiation (Instantiation_Node);
1109 else
1110 Analyze (Match);
1111 Append_List
1112 (Instantiate_Formal_Package
1113 (Formal, Match, Analyzed_Formal),
1114 Assoc);
1115 end if;
1117 -- For use type and use package appearing in the context
1118 -- clause, we have already copied them, so we can just
1119 -- move them where they belong (we mustn't recopy them
1120 -- since this would mess up the Sloc values).
1122 when N_Use_Package_Clause |
1123 N_Use_Type_Clause =>
1124 Remove (Formal);
1125 Append (Formal, Assoc);
1127 when others =>
1128 raise Program_Error;
1130 end case;
1132 Formal := Next_Formal;
1133 Next_Non_Pragma (Analyzed_Formal);
1134 end loop;
1136 if Num_Actuals > Num_Matched then
1137 Error_Msg_Sloc := Sloc (Gen_Unit);
1139 if Present (Selector_Name (Actual)) then
1140 Error_Msg_NE
1141 ("unmatched actual&",
1142 Actual, Selector_Name (Actual));
1143 Error_Msg_NE ("\in instantiation of& declared#",
1144 Actual, Gen_Unit);
1145 else
1146 Error_Msg_NE
1147 ("unmatched actual in instantiation of& declared#",
1148 Actual, Gen_Unit);
1149 end if;
1150 end if;
1152 elsif Present (Actuals) then
1153 Error_Msg_N
1154 ("too many actuals in generic instantiation", Instantiation_Node);
1155 end if;
1157 declare
1158 Elmt : Elmt_Id := First_Elmt (Actual_Types);
1160 begin
1161 while Present (Elmt) loop
1162 Freeze_Before (I_Node, Node (Elmt));
1163 Next_Elmt (Elmt);
1164 end loop;
1165 end;
1167 -- If there are default subprograms, normalize the tree by adding
1168 -- explicit associations for them. This is required if the instance
1169 -- appears within a generic.
1171 declare
1172 Elmt : Elmt_Id;
1173 Subp : Entity_Id;
1174 New_D : Node_Id;
1176 begin
1177 Elmt := First_Elmt (Defaults);
1178 while Present (Elmt) loop
1179 if No (Actuals) then
1180 Actuals := New_List;
1181 Set_Generic_Associations (I_Node, Actuals);
1182 end if;
1184 Subp := Node (Elmt);
1185 New_D :=
1186 Make_Generic_Association (Sloc (Subp),
1187 Selector_Name => New_Occurrence_Of (Subp, Sloc (Subp)),
1188 Explicit_Generic_Actual_Parameter =>
1189 New_Occurrence_Of (Subp, Sloc (Subp)));
1190 Mark_Rewrite_Insertion (New_D);
1191 Append_To (Actuals, New_D);
1192 Next_Elmt (Elmt);
1193 end loop;
1194 end;
1196 return Assoc;
1197 end Analyze_Associations;
1199 -------------------------------
1200 -- Analyze_Formal_Array_Type --
1201 -------------------------------
1203 procedure Analyze_Formal_Array_Type
1204 (T : in out Entity_Id;
1205 Def : Node_Id)
1207 DSS : Node_Id;
1209 begin
1210 -- Treated like a non-generic array declaration, with
1211 -- additional semantic checks.
1213 Enter_Name (T);
1215 if Nkind (Def) = N_Constrained_Array_Definition then
1216 DSS := First (Discrete_Subtype_Definitions (Def));
1217 while Present (DSS) loop
1218 if Nkind (DSS) = N_Subtype_Indication
1219 or else Nkind (DSS) = N_Range
1220 or else Nkind (DSS) = N_Attribute_Reference
1221 then
1222 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1223 end if;
1225 Next (DSS);
1226 end loop;
1227 end if;
1229 Array_Type_Declaration (T, Def);
1230 Set_Is_Generic_Type (Base_Type (T));
1232 if Ekind (Component_Type (T)) = E_Incomplete_Type
1233 and then No (Full_View (Component_Type (T)))
1234 then
1235 Error_Msg_N ("premature usage of incomplete type", Def);
1237 -- Check that range constraint is not allowed on the component type
1238 -- of a generic formal array type (AARM 12.5.3(3))
1240 elsif Is_Internal (Component_Type (T))
1241 and then Present (Subtype_Indication (Component_Definition (Def)))
1242 and then Nkind (Original_Node
1243 (Subtype_Indication (Component_Definition (Def))))
1244 = N_Subtype_Indication
1245 then
1246 Error_Msg_N
1247 ("in a formal, a subtype indication can only be "
1248 & "a subtype mark ('R'M 12.5.3(3))",
1249 Subtype_Indication (Component_Definition (Def)));
1250 end if;
1252 end Analyze_Formal_Array_Type;
1254 ---------------------------------------------
1255 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1256 ---------------------------------------------
1258 -- As for other generic types, we create a valid type representation
1259 -- with legal but arbitrary attributes, whose values are never considered
1260 -- static. For all scalar types we introduce an anonymous base type, with
1261 -- the same attributes. We choose the corresponding integer type to be
1262 -- Standard_Integer.
1264 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1265 (T : Entity_Id;
1266 Def : Node_Id)
1268 Loc : constant Source_Ptr := Sloc (Def);
1269 Base : constant Entity_Id :=
1270 New_Internal_Entity
1271 (E_Decimal_Fixed_Point_Type,
1272 Current_Scope, Sloc (Def), 'G');
1273 Int_Base : constant Entity_Id := Standard_Integer;
1274 Delta_Val : constant Ureal := Ureal_1;
1275 Digs_Val : constant Uint := Uint_6;
1277 begin
1278 Enter_Name (T);
1280 Set_Etype (Base, Base);
1281 Set_Size_Info (Base, Int_Base);
1282 Set_RM_Size (Base, RM_Size (Int_Base));
1283 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1284 Set_Digits_Value (Base, Digs_Val);
1285 Set_Delta_Value (Base, Delta_Val);
1286 Set_Small_Value (Base, Delta_Val);
1287 Set_Scalar_Range (Base,
1288 Make_Range (Loc,
1289 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1290 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1292 Set_Is_Generic_Type (Base);
1293 Set_Parent (Base, Parent (Def));
1295 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1296 Set_Etype (T, Base);
1297 Set_Size_Info (T, Int_Base);
1298 Set_RM_Size (T, RM_Size (Int_Base));
1299 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1300 Set_Digits_Value (T, Digs_Val);
1301 Set_Delta_Value (T, Delta_Val);
1302 Set_Small_Value (T, Delta_Val);
1303 Set_Scalar_Range (T, Scalar_Range (Base));
1304 Set_Is_Constrained (T);
1306 Check_Restriction (No_Fixed_Point, Def);
1307 end Analyze_Formal_Decimal_Fixed_Point_Type;
1309 -------------------------------------------
1310 -- Analyze_Formal_Derived_Interface_Type --
1311 -------------------------------------------
1313 procedure Analyze_Formal_Derived_Interface_Type
1314 (T : Entity_Id;
1315 Def : Node_Id)
1317 begin
1318 Enter_Name (T);
1319 Set_Ekind (T, E_Record_Type);
1320 Set_Etype (T, T);
1321 Analyze (Subtype_Indication (Def));
1322 Analyze_Interface_Declaration (T, Def);
1323 Make_Class_Wide_Type (T);
1324 Set_Primitive_Operations (T, New_Elmt_List);
1325 Analyze_List (Interface_List (Def));
1326 Collect_Interfaces (Def, T);
1327 end Analyze_Formal_Derived_Interface_Type;
1329 ---------------------------------
1330 -- Analyze_Formal_Derived_Type --
1331 ---------------------------------
1333 procedure Analyze_Formal_Derived_Type
1334 (N : Node_Id;
1335 T : Entity_Id;
1336 Def : Node_Id)
1338 Loc : constant Source_Ptr := Sloc (Def);
1339 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
1340 New_N : Node_Id;
1342 begin
1343 Set_Is_Generic_Type (T);
1345 if Private_Present (Def) then
1346 New_N :=
1347 Make_Private_Extension_Declaration (Loc,
1348 Defining_Identifier => T,
1349 Discriminant_Specifications => Discriminant_Specifications (N),
1350 Unknown_Discriminants_Present => Unk_Disc,
1351 Subtype_Indication => Subtype_Mark (Def));
1353 Set_Abstract_Present (New_N, Abstract_Present (Def));
1355 else
1356 New_N :=
1357 Make_Full_Type_Declaration (Loc,
1358 Defining_Identifier => T,
1359 Discriminant_Specifications =>
1360 Discriminant_Specifications (Parent (T)),
1361 Type_Definition =>
1362 Make_Derived_Type_Definition (Loc,
1363 Subtype_Indication => Subtype_Mark (Def)));
1365 Set_Abstract_Present
1366 (Type_Definition (New_N), Abstract_Present (Def));
1367 end if;
1369 Rewrite (N, New_N);
1370 Analyze (N);
1372 if Unk_Disc then
1373 if not Is_Composite_Type (T) then
1374 Error_Msg_N
1375 ("unknown discriminants not allowed for elementary types", N);
1376 else
1377 Set_Has_Unknown_Discriminants (T);
1378 Set_Is_Constrained (T, False);
1379 end if;
1380 end if;
1382 -- If the parent type has a known size, so does the formal, which
1383 -- makes legal representation clauses that involve the formal.
1385 Set_Size_Known_At_Compile_Time
1386 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
1388 end Analyze_Formal_Derived_Type;
1390 ----------------------------------
1391 -- Analyze_Formal_Discrete_Type --
1392 ----------------------------------
1394 -- The operations defined for a discrete types are those of an
1395 -- enumeration type. The size is set to an arbitrary value, for use
1396 -- in analyzing the generic unit.
1398 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
1399 Loc : constant Source_Ptr := Sloc (Def);
1400 Lo : Node_Id;
1401 Hi : Node_Id;
1403 Base : constant Entity_Id :=
1404 New_Internal_Entity
1405 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1406 begin
1407 Enter_Name (T);
1408 Set_Ekind (T, E_Enumeration_Subtype);
1409 Set_Etype (T, Base);
1410 Init_Size (T, 8);
1411 Init_Alignment (T);
1412 Set_Is_Generic_Type (T);
1413 Set_Is_Constrained (T);
1415 -- For semantic analysis, the bounds of the type must be set to some
1416 -- non-static value. The simplest is to create attribute nodes for
1417 -- those bounds, that refer to the type itself. These bounds are never
1418 -- analyzed but serve as place-holders.
1420 Lo :=
1421 Make_Attribute_Reference (Loc,
1422 Attribute_Name => Name_First,
1423 Prefix => New_Reference_To (T, Loc));
1424 Set_Etype (Lo, T);
1426 Hi :=
1427 Make_Attribute_Reference (Loc,
1428 Attribute_Name => Name_Last,
1429 Prefix => New_Reference_To (T, Loc));
1430 Set_Etype (Hi, T);
1432 Set_Scalar_Range (T,
1433 Make_Range (Loc,
1434 Low_Bound => Lo,
1435 High_Bound => Hi));
1437 Set_Ekind (Base, E_Enumeration_Type);
1438 Set_Etype (Base, Base);
1439 Init_Size (Base, 8);
1440 Init_Alignment (Base);
1441 Set_Is_Generic_Type (Base);
1442 Set_Scalar_Range (Base, Scalar_Range (T));
1443 Set_Parent (Base, Parent (Def));
1445 end Analyze_Formal_Discrete_Type;
1447 ----------------------------------
1448 -- Analyze_Formal_Floating_Type --
1449 ---------------------------------
1451 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
1452 Base : constant Entity_Id :=
1453 New_Internal_Entity
1454 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1456 begin
1457 -- The various semantic attributes are taken from the predefined type
1458 -- Float, just so that all of them are initialized. Their values are
1459 -- never used because no constant folding or expansion takes place in
1460 -- the generic itself.
1462 Enter_Name (T);
1463 Set_Ekind (T, E_Floating_Point_Subtype);
1464 Set_Etype (T, Base);
1465 Set_Size_Info (T, (Standard_Float));
1466 Set_RM_Size (T, RM_Size (Standard_Float));
1467 Set_Digits_Value (T, Digits_Value (Standard_Float));
1468 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
1469 Set_Is_Constrained (T);
1471 Set_Is_Generic_Type (Base);
1472 Set_Etype (Base, Base);
1473 Set_Size_Info (Base, (Standard_Float));
1474 Set_RM_Size (Base, RM_Size (Standard_Float));
1475 Set_Digits_Value (Base, Digits_Value (Standard_Float));
1476 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
1477 Set_Parent (Base, Parent (Def));
1479 Check_Restriction (No_Floating_Point, Def);
1480 end Analyze_Formal_Floating_Type;
1482 -----------------------------------
1483 -- Analyze_Formal_Interface_Type;--
1484 -----------------------------------
1486 procedure Analyze_Formal_Interface_Type (T : Entity_Id; Def : Node_Id) is
1487 begin
1488 Enter_Name (T);
1489 Set_Ekind (T, E_Record_Type);
1490 Set_Etype (T, T);
1491 Analyze_Interface_Declaration (T, Def);
1492 Make_Class_Wide_Type (T);
1493 Set_Primitive_Operations (T, New_Elmt_List);
1494 end Analyze_Formal_Interface_Type;
1496 ---------------------------------
1497 -- Analyze_Formal_Modular_Type --
1498 ---------------------------------
1500 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
1501 begin
1502 -- Apart from their entity kind, generic modular types are treated
1503 -- like signed integer types, and have the same attributes.
1505 Analyze_Formal_Signed_Integer_Type (T, Def);
1506 Set_Ekind (T, E_Modular_Integer_Subtype);
1507 Set_Ekind (Etype (T), E_Modular_Integer_Type);
1509 end Analyze_Formal_Modular_Type;
1511 ---------------------------------------
1512 -- Analyze_Formal_Object_Declaration --
1513 ---------------------------------------
1515 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
1516 E : constant Node_Id := Expression (N);
1517 Id : constant Node_Id := Defining_Identifier (N);
1518 K : Entity_Kind;
1519 T : Node_Id;
1521 begin
1522 Enter_Name (Id);
1524 -- Determine the mode of the formal object
1526 if Out_Present (N) then
1527 K := E_Generic_In_Out_Parameter;
1529 if not In_Present (N) then
1530 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
1531 end if;
1533 else
1534 K := E_Generic_In_Parameter;
1535 end if;
1537 Find_Type (Subtype_Mark (N));
1538 T := Entity (Subtype_Mark (N));
1540 if Ekind (T) = E_Incomplete_Type then
1541 Error_Msg_N ("premature usage of incomplete type", Subtype_Mark (N));
1542 end if;
1544 if K = E_Generic_In_Parameter then
1546 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
1548 if Ada_Version < Ada_05 and then Is_Limited_Type (T) then
1549 Error_Msg_N
1550 ("generic formal of mode IN must not be of limited type", N);
1551 Explain_Limited_Type (T, N);
1552 end if;
1554 if Is_Abstract (T) then
1555 Error_Msg_N
1556 ("generic formal of mode IN must not be of abstract type", N);
1557 end if;
1559 if Present (E) then
1560 Analyze_Per_Use_Expression (E, T);
1561 end if;
1563 Set_Ekind (Id, K);
1564 Set_Etype (Id, T);
1566 -- Case of generic IN OUT parameter
1568 else
1569 -- If the formal has an unconstrained type, construct its
1570 -- actual subtype, as is done for subprogram formals. In this
1571 -- fashion, all its uses can refer to specific bounds.
1573 Set_Ekind (Id, K);
1574 Set_Etype (Id, T);
1576 if (Is_Array_Type (T)
1577 and then not Is_Constrained (T))
1578 or else
1579 (Ekind (T) = E_Record_Type
1580 and then Has_Discriminants (T))
1581 then
1582 declare
1583 Non_Freezing_Ref : constant Node_Id :=
1584 New_Reference_To (Id, Sloc (Id));
1585 Decl : Node_Id;
1587 begin
1588 -- Make sure that the actual subtype doesn't generate
1589 -- bogus freezing.
1591 Set_Must_Not_Freeze (Non_Freezing_Ref);
1592 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
1593 Insert_Before_And_Analyze (N, Decl);
1594 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
1595 end;
1596 else
1597 Set_Actual_Subtype (Id, T);
1598 end if;
1600 if Present (E) then
1601 Error_Msg_N
1602 ("initialization not allowed for `IN OUT` formals", N);
1603 end if;
1604 end if;
1606 end Analyze_Formal_Object_Declaration;
1608 ----------------------------------------------
1609 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
1610 ----------------------------------------------
1612 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
1613 (T : Entity_Id;
1614 Def : Node_Id)
1616 Loc : constant Source_Ptr := Sloc (Def);
1617 Base : constant Entity_Id :=
1618 New_Internal_Entity
1619 (E_Ordinary_Fixed_Point_Type, Current_Scope, Sloc (Def), 'G');
1620 begin
1621 -- The semantic attributes are set for completeness only, their
1622 -- values will never be used, because all properties of the type
1623 -- are non-static.
1625 Enter_Name (T);
1626 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
1627 Set_Etype (T, Base);
1628 Set_Size_Info (T, Standard_Integer);
1629 Set_RM_Size (T, RM_Size (Standard_Integer));
1630 Set_Small_Value (T, Ureal_1);
1631 Set_Delta_Value (T, Ureal_1);
1632 Set_Scalar_Range (T,
1633 Make_Range (Loc,
1634 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1635 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1636 Set_Is_Constrained (T);
1638 Set_Is_Generic_Type (Base);
1639 Set_Etype (Base, Base);
1640 Set_Size_Info (Base, Standard_Integer);
1641 Set_RM_Size (Base, RM_Size (Standard_Integer));
1642 Set_Small_Value (Base, Ureal_1);
1643 Set_Delta_Value (Base, Ureal_1);
1644 Set_Scalar_Range (Base, Scalar_Range (T));
1645 Set_Parent (Base, Parent (Def));
1647 Check_Restriction (No_Fixed_Point, Def);
1648 end Analyze_Formal_Ordinary_Fixed_Point_Type;
1650 ----------------------------
1651 -- Analyze_Formal_Package --
1652 ----------------------------
1654 procedure Analyze_Formal_Package (N : Node_Id) is
1655 Loc : constant Source_Ptr := Sloc (N);
1656 Pack_Id : constant Entity_Id := Defining_Identifier (N);
1657 Formal : Entity_Id;
1658 Gen_Id : constant Node_Id := Name (N);
1659 Gen_Decl : Node_Id;
1660 Gen_Unit : Entity_Id;
1661 New_N : Node_Id;
1662 Parent_Installed : Boolean := False;
1663 Renaming : Node_Id;
1664 Parent_Instance : Entity_Id;
1665 Renaming_In_Par : Entity_Id;
1667 begin
1668 Text_IO_Kludge (Gen_Id);
1670 Init_Env;
1671 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
1672 Gen_Unit := Entity (Gen_Id);
1674 -- Check for a formal package that is a package renaming
1676 if Present (Renamed_Object (Gen_Unit)) then
1677 Gen_Unit := Renamed_Object (Gen_Unit);
1678 end if;
1680 if Ekind (Gen_Unit) /= E_Generic_Package then
1681 Error_Msg_N ("expect generic package name", Gen_Id);
1682 Restore_Env;
1683 return;
1685 elsif Gen_Unit = Current_Scope then
1686 Error_Msg_N
1687 ("generic package cannot be used as a formal package of itself",
1688 Gen_Id);
1689 Restore_Env;
1690 return;
1692 elsif In_Open_Scopes (Gen_Unit) then
1693 if Is_Compilation_Unit (Gen_Unit)
1694 and then Is_Child_Unit (Current_Scope)
1695 then
1696 -- Special-case the error when the formal is a parent, and
1697 -- continue analysis to minimize cascaded errors.
1699 Error_Msg_N
1700 ("generic parent cannot be used as formal package "
1701 & "of a child unit",
1702 Gen_Id);
1704 else
1705 Error_Msg_N
1706 ("generic package cannot be used as a formal package "
1707 & "within itself",
1708 Gen_Id);
1709 Restore_Env;
1710 return;
1711 end if;
1712 end if;
1714 -- The formal package is treated like a regular instance, but only
1715 -- the specification needs to be instantiated, to make entities visible.
1717 if not Box_Present (N) then
1718 Hidden_Entities := New_Elmt_List;
1719 Analyze_Package_Instantiation (N);
1721 if Parent_Installed then
1722 Remove_Parent;
1723 end if;
1725 else
1726 -- If there are no generic associations, the generic parameters
1727 -- appear as local entities and are instantiated like them. We copy
1728 -- the generic package declaration as if it were an instantiation,
1729 -- and analyze it like a regular package, except that we treat the
1730 -- formals as additional visible components.
1732 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
1734 if In_Extended_Main_Source_Unit (N) then
1735 Set_Is_Instantiated (Gen_Unit);
1736 Generate_Reference (Gen_Unit, N);
1737 end if;
1739 Formal := New_Copy (Pack_Id);
1740 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
1742 New_N :=
1743 Copy_Generic_Node
1744 (Original_Node (Gen_Decl), Empty, Instantiating => True);
1745 Rewrite (N, New_N);
1746 Set_Defining_Unit_Name (Specification (New_N), Formal);
1747 Set_Generic_Parent (Specification (N), Gen_Unit);
1748 Set_Instance_Env (Gen_Unit, Formal);
1750 Enter_Name (Formal);
1751 Set_Ekind (Formal, E_Generic_Package);
1752 Set_Etype (Formal, Standard_Void_Type);
1753 Set_Inner_Instances (Formal, New_Elmt_List);
1754 New_Scope (Formal);
1756 -- Within the formal, the name of the generic package is a renaming
1757 -- of the formal (as for a regular instantiation).
1759 Renaming := Make_Package_Renaming_Declaration (Loc,
1760 Defining_Unit_Name =>
1761 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
1762 Name => New_Reference_To (Formal, Loc));
1764 if Present (Visible_Declarations (Specification (N))) then
1765 Prepend (Renaming, To => Visible_Declarations (Specification (N)));
1766 elsif Present (Private_Declarations (Specification (N))) then
1767 Prepend (Renaming, To => Private_Declarations (Specification (N)));
1768 end if;
1770 if Is_Child_Unit (Gen_Unit)
1771 and then Parent_Installed
1772 then
1773 -- Similarly, we have to make the name of the formal visible in
1774 -- the parent instance, to resolve properly fully qualified names
1775 -- that may appear in the generic unit. The parent instance has
1776 -- been placed on the scope stack ahead of the current scope.
1778 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
1780 Renaming_In_Par :=
1781 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
1782 Set_Ekind (Renaming_In_Par, E_Package);
1783 Set_Etype (Renaming_In_Par, Standard_Void_Type);
1784 Set_Scope (Renaming_In_Par, Parent_Instance);
1785 Set_Parent (Renaming_In_Par, Parent (Formal));
1786 Set_Renamed_Object (Renaming_In_Par, Formal);
1787 Append_Entity (Renaming_In_Par, Parent_Instance);
1788 end if;
1790 Analyze_Generic_Formal_Part (N);
1791 Analyze (Specification (N));
1792 End_Package_Scope (Formal);
1794 if Parent_Installed then
1795 Remove_Parent;
1796 end if;
1798 Restore_Env;
1800 -- Inside the generic unit, the formal package is a regular
1801 -- package, but no body is needed for it. Note that after
1802 -- instantiation, the defining_unit_name we need is in the
1803 -- new tree and not in the original. (see Package_Instantiation).
1804 -- A generic formal package is an instance, and can be used as
1805 -- an actual for an inner instance.
1807 Set_Ekind (Formal, E_Package);
1808 Set_Has_Completion (Formal, True);
1810 Set_Ekind (Pack_Id, E_Package);
1811 Set_Etype (Pack_Id, Standard_Void_Type);
1812 Set_Scope (Pack_Id, Scope (Formal));
1813 Set_Has_Completion (Pack_Id, True);
1814 end if;
1815 end Analyze_Formal_Package;
1817 ---------------------------------
1818 -- Analyze_Formal_Private_Type --
1819 ---------------------------------
1821 procedure Analyze_Formal_Private_Type
1822 (N : Node_Id;
1823 T : Entity_Id;
1824 Def : Node_Id)
1826 begin
1827 New_Private_Type (N, T, Def);
1829 -- Set the size to an arbitrary but legal value
1831 Set_Size_Info (T, Standard_Integer);
1832 Set_RM_Size (T, RM_Size (Standard_Integer));
1833 end Analyze_Formal_Private_Type;
1835 ----------------------------------------
1836 -- Analyze_Formal_Signed_Integer_Type --
1837 ----------------------------------------
1839 procedure Analyze_Formal_Signed_Integer_Type
1840 (T : Entity_Id;
1841 Def : Node_Id)
1843 Base : constant Entity_Id :=
1844 New_Internal_Entity
1845 (E_Signed_Integer_Type, Current_Scope, Sloc (Def), 'G');
1847 begin
1848 Enter_Name (T);
1850 Set_Ekind (T, E_Signed_Integer_Subtype);
1851 Set_Etype (T, Base);
1852 Set_Size_Info (T, Standard_Integer);
1853 Set_RM_Size (T, RM_Size (Standard_Integer));
1854 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
1855 Set_Is_Constrained (T);
1857 Set_Is_Generic_Type (Base);
1858 Set_Size_Info (Base, Standard_Integer);
1859 Set_RM_Size (Base, RM_Size (Standard_Integer));
1860 Set_Etype (Base, Base);
1861 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
1862 Set_Parent (Base, Parent (Def));
1863 end Analyze_Formal_Signed_Integer_Type;
1865 -------------------------------
1866 -- Analyze_Formal_Subprogram --
1867 -------------------------------
1869 procedure Analyze_Formal_Subprogram (N : Node_Id) is
1870 Spec : constant Node_Id := Specification (N);
1871 Def : constant Node_Id := Default_Name (N);
1872 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
1873 Subp : Entity_Id;
1875 begin
1876 if Nam = Error then
1877 return;
1878 end if;
1880 if Nkind (Nam) = N_Defining_Program_Unit_Name then
1881 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
1882 return;
1883 end if;
1885 Analyze_Subprogram_Declaration (N);
1886 Set_Is_Formal_Subprogram (Nam);
1887 Set_Has_Completion (Nam);
1889 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
1890 Set_Is_Abstract (Nam);
1891 Set_Is_Dispatching_Operation (Nam);
1893 declare
1894 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
1896 begin
1897 if not Present (Ctrl_Type) then
1898 Error_Msg_N
1899 ("abstract formal subprogram must have a controlling type",
1902 else
1903 Check_Controlling_Formals (Ctrl_Type, Nam);
1904 end if;
1905 end;
1906 end if;
1908 -- Default name is resolved at the point of instantiation
1910 if Box_Present (N) then
1911 null;
1913 -- Else default is bound at the point of generic declaration
1915 elsif Present (Def) then
1916 if Nkind (Def) = N_Operator_Symbol then
1917 Find_Direct_Name (Def);
1919 elsif Nkind (Def) /= N_Attribute_Reference then
1920 Analyze (Def);
1922 else
1923 -- For an attribute reference, analyze the prefix and verify
1924 -- that it has the proper profile for the subprogram.
1926 Analyze (Prefix (Def));
1927 Valid_Default_Attribute (Nam, Def);
1928 return;
1929 end if;
1931 -- Default name may be overloaded, in which case the interpretation
1932 -- with the correct profile must be selected, as for a renaming.
1934 if Etype (Def) = Any_Type then
1935 return;
1937 elsif Nkind (Def) = N_Selected_Component then
1938 Subp := Entity (Selector_Name (Def));
1940 if Ekind (Subp) /= E_Entry then
1941 Error_Msg_N ("expect valid subprogram name as default", Def);
1942 return;
1943 end if;
1945 elsif Nkind (Def) = N_Indexed_Component then
1947 if Nkind (Prefix (Def)) /= N_Selected_Component then
1948 Error_Msg_N ("expect valid subprogram name as default", Def);
1949 return;
1951 else
1952 Subp := Entity (Selector_Name (Prefix (Def)));
1954 if Ekind (Subp) /= E_Entry_Family then
1955 Error_Msg_N ("expect valid subprogram name as default", Def);
1956 return;
1957 end if;
1958 end if;
1960 elsif Nkind (Def) = N_Character_Literal then
1962 -- Needs some type checks: subprogram should be parameterless???
1964 Resolve (Def, (Etype (Nam)));
1966 elsif not Is_Entity_Name (Def)
1967 or else not Is_Overloadable (Entity (Def))
1968 then
1969 Error_Msg_N ("expect valid subprogram name as default", Def);
1970 return;
1972 elsif not Is_Overloaded (Def) then
1973 Subp := Entity (Def);
1975 if Subp = Nam then
1976 Error_Msg_N ("premature usage of formal subprogram", Def);
1978 elsif not Entity_Matches_Spec (Subp, Nam) then
1979 Error_Msg_N ("no visible entity matches specification", Def);
1980 end if;
1982 else
1983 declare
1984 I : Interp_Index;
1985 I1 : Interp_Index := 0;
1986 It : Interp;
1987 It1 : Interp;
1989 begin
1990 Subp := Any_Id;
1991 Get_First_Interp (Def, I, It);
1992 while Present (It.Nam) loop
1994 if Entity_Matches_Spec (It.Nam, Nam) then
1995 if Subp /= Any_Id then
1996 It1 := Disambiguate (Def, I1, I, Etype (Subp));
1998 if It1 = No_Interp then
1999 Error_Msg_N ("ambiguous default subprogram", Def);
2000 else
2001 Subp := It1.Nam;
2002 end if;
2004 exit;
2006 else
2007 I1 := I;
2008 Subp := It.Nam;
2009 end if;
2010 end if;
2012 Get_Next_Interp (I, It);
2013 end loop;
2014 end;
2016 if Subp /= Any_Id then
2017 Set_Entity (Def, Subp);
2019 if Subp = Nam then
2020 Error_Msg_N ("premature usage of formal subprogram", Def);
2022 elsif Ekind (Subp) /= E_Operator then
2023 Check_Mode_Conformant (Subp, Nam);
2024 end if;
2026 else
2027 Error_Msg_N ("no visible subprogram matches specification", N);
2028 end if;
2029 end if;
2030 end if;
2031 end Analyze_Formal_Subprogram;
2033 -------------------------------------
2034 -- Analyze_Formal_Type_Declaration --
2035 -------------------------------------
2037 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
2038 Def : constant Node_Id := Formal_Type_Definition (N);
2039 T : Entity_Id;
2041 begin
2042 T := Defining_Identifier (N);
2044 if Present (Discriminant_Specifications (N))
2045 and then Nkind (Def) /= N_Formal_Private_Type_Definition
2046 then
2047 Error_Msg_N
2048 ("discriminants not allowed for this formal type",
2049 Defining_Identifier (First (Discriminant_Specifications (N))));
2050 end if;
2052 -- Enter the new name, and branch to specific routine
2054 case Nkind (Def) is
2055 when N_Formal_Private_Type_Definition =>
2056 Analyze_Formal_Private_Type (N, T, Def);
2058 when N_Formal_Derived_Type_Definition =>
2059 Analyze_Formal_Derived_Type (N, T, Def);
2061 when N_Formal_Discrete_Type_Definition =>
2062 Analyze_Formal_Discrete_Type (T, Def);
2064 when N_Formal_Signed_Integer_Type_Definition =>
2065 Analyze_Formal_Signed_Integer_Type (T, Def);
2067 when N_Formal_Modular_Type_Definition =>
2068 Analyze_Formal_Modular_Type (T, Def);
2070 when N_Formal_Floating_Point_Definition =>
2071 Analyze_Formal_Floating_Type (T, Def);
2073 when N_Formal_Ordinary_Fixed_Point_Definition =>
2074 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
2076 when N_Formal_Decimal_Fixed_Point_Definition =>
2077 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
2079 when N_Array_Type_Definition =>
2080 Analyze_Formal_Array_Type (T, Def);
2082 when N_Access_To_Object_Definition |
2083 N_Access_Function_Definition |
2084 N_Access_Procedure_Definition =>
2085 Analyze_Generic_Access_Type (T, Def);
2087 -- Ada 2005: a interface declaration is encoded as an abstract
2088 -- record declaration or a abstract type derivation.
2090 when N_Record_Definition =>
2091 Analyze_Formal_Interface_Type (T, Def);
2093 when N_Derived_Type_Definition =>
2094 Analyze_Formal_Derived_Interface_Type (T, Def);
2096 when N_Error =>
2097 null;
2099 when others =>
2100 raise Program_Error;
2102 end case;
2104 Set_Is_Generic_Type (T);
2105 end Analyze_Formal_Type_Declaration;
2107 ------------------------------------
2108 -- Analyze_Function_Instantiation --
2109 ------------------------------------
2111 procedure Analyze_Function_Instantiation (N : Node_Id) is
2112 begin
2113 Analyze_Subprogram_Instantiation (N, E_Function);
2114 end Analyze_Function_Instantiation;
2116 ---------------------------------
2117 -- Analyze_Generic_Access_Type --
2118 ---------------------------------
2120 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
2121 begin
2122 Enter_Name (T);
2124 if Nkind (Def) = N_Access_To_Object_Definition then
2125 Access_Type_Declaration (T, Def);
2127 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
2128 and then No (Full_View (Designated_Type (T)))
2129 and then not Is_Generic_Type (Designated_Type (T))
2130 then
2131 Error_Msg_N ("premature usage of incomplete type", Def);
2133 elsif Is_Internal (Designated_Type (T)) then
2134 Error_Msg_N
2135 ("only a subtype mark is allowed in a formal", Def);
2136 end if;
2138 else
2139 Access_Subprogram_Declaration (T, Def);
2140 end if;
2141 end Analyze_Generic_Access_Type;
2143 ---------------------------------
2144 -- Analyze_Generic_Formal_Part --
2145 ---------------------------------
2147 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
2148 Gen_Parm_Decl : Node_Id;
2150 begin
2151 -- The generic formals are processed in the scope of the generic
2152 -- unit, where they are immediately visible. The scope is installed
2153 -- by the caller.
2155 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
2157 while Present (Gen_Parm_Decl) loop
2158 Analyze (Gen_Parm_Decl);
2159 Next (Gen_Parm_Decl);
2160 end loop;
2162 Generate_Reference_To_Generic_Formals (Current_Scope);
2163 end Analyze_Generic_Formal_Part;
2165 ------------------------------------------
2166 -- Analyze_Generic_Package_Declaration --
2167 ------------------------------------------
2169 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
2170 Loc : constant Source_Ptr := Sloc (N);
2171 Id : Entity_Id;
2172 New_N : Node_Id;
2173 Save_Parent : Node_Id;
2174 Renaming : Node_Id;
2175 Decls : constant List_Id :=
2176 Visible_Declarations (Specification (N));
2177 Decl : Node_Id;
2179 begin
2180 -- We introduce a renaming of the enclosing package, to have a usable
2181 -- entity as the prefix of an expanded name for a local entity of the
2182 -- form Par.P.Q, where P is the generic package. This is because a local
2183 -- entity named P may hide it, so that the usual visibility rules in
2184 -- the instance will not resolve properly.
2186 Renaming :=
2187 Make_Package_Renaming_Declaration (Loc,
2188 Defining_Unit_Name =>
2189 Make_Defining_Identifier (Loc,
2190 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
2191 Name => Make_Identifier (Loc, Chars (Defining_Entity (N))));
2193 if Present (Decls) then
2194 Decl := First (Decls);
2195 while Present (Decl)
2196 and then Nkind (Decl) = N_Pragma
2197 loop
2198 Next (Decl);
2199 end loop;
2201 if Present (Decl) then
2202 Insert_Before (Decl, Renaming);
2203 else
2204 Append (Renaming, Visible_Declarations (Specification (N)));
2205 end if;
2207 else
2208 Set_Visible_Declarations (Specification (N), New_List (Renaming));
2209 end if;
2211 -- Create copy of generic unit, and save for instantiation.
2212 -- If the unit is a child unit, do not copy the specifications
2213 -- for the parent, which are not part of the generic tree.
2215 Save_Parent := Parent_Spec (N);
2216 Set_Parent_Spec (N, Empty);
2218 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2219 Set_Parent_Spec (New_N, Save_Parent);
2220 Rewrite (N, New_N);
2221 Id := Defining_Entity (N);
2222 Generate_Definition (Id);
2224 -- Expansion is not applied to generic units
2226 Start_Generic;
2228 Enter_Name (Id);
2229 Set_Ekind (Id, E_Generic_Package);
2230 Set_Etype (Id, Standard_Void_Type);
2231 New_Scope (Id);
2232 Enter_Generic_Scope (Id);
2233 Set_Inner_Instances (Id, New_Elmt_List);
2235 Set_Categorization_From_Pragmas (N);
2236 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2238 -- Link the declaration of the generic homonym in the generic copy
2239 -- to the package it renames, so that it is always resolved properly.
2241 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
2242 Set_Entity (Associated_Node (Name (Renaming)), Id);
2244 -- For a library unit, we have reconstructed the entity for the
2245 -- unit, and must reset it in the library tables.
2247 if Nkind (Parent (N)) = N_Compilation_Unit then
2248 Set_Cunit_Entity (Current_Sem_Unit, Id);
2249 end if;
2251 Analyze_Generic_Formal_Part (N);
2253 -- After processing the generic formals, analysis proceeds
2254 -- as for a non-generic package.
2256 Analyze (Specification (N));
2258 Validate_Categorization_Dependency (N, Id);
2260 End_Generic;
2262 End_Package_Scope (Id);
2263 Exit_Generic_Scope (Id);
2265 if Nkind (Parent (N)) /= N_Compilation_Unit then
2266 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
2267 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
2268 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
2270 else
2271 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2272 Validate_RT_RAT_Component (N);
2274 -- If this is a spec without a body, check that generic parameters
2275 -- are referenced.
2277 if not Body_Required (Parent (N)) then
2278 Check_References (Id);
2279 end if;
2280 end if;
2281 end Analyze_Generic_Package_Declaration;
2283 --------------------------------------------
2284 -- Analyze_Generic_Subprogram_Declaration --
2285 --------------------------------------------
2287 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
2288 Spec : Node_Id;
2289 Id : Entity_Id;
2290 Formals : List_Id;
2291 New_N : Node_Id;
2292 Result_Type : Entity_Id;
2293 Save_Parent : Node_Id;
2295 begin
2296 -- Create copy of generic unit,and save for instantiation.
2297 -- If the unit is a child unit, do not copy the specifications
2298 -- for the parent, which are not part of the generic tree.
2300 Save_Parent := Parent_Spec (N);
2301 Set_Parent_Spec (N, Empty);
2303 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2304 Set_Parent_Spec (New_N, Save_Parent);
2305 Rewrite (N, New_N);
2307 Spec := Specification (N);
2308 Id := Defining_Entity (Spec);
2309 Generate_Definition (Id);
2311 if Nkind (Id) = N_Defining_Operator_Symbol then
2312 Error_Msg_N
2313 ("operator symbol not allowed for generic subprogram", Id);
2314 end if;
2316 Start_Generic;
2318 Enter_Name (Id);
2320 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
2321 New_Scope (Id);
2322 Enter_Generic_Scope (Id);
2323 Set_Inner_Instances (Id, New_Elmt_List);
2324 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2326 Analyze_Generic_Formal_Part (N);
2328 Formals := Parameter_Specifications (Spec);
2330 if Present (Formals) then
2331 Process_Formals (Formals, Spec);
2332 end if;
2334 if Nkind (Spec) = N_Function_Specification then
2335 Set_Ekind (Id, E_Generic_Function);
2337 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
2338 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
2339 Set_Etype (Id, Result_Type);
2340 else
2341 Find_Type (Result_Definition (Spec));
2342 Set_Etype (Id, Entity (Result_Definition (Spec)));
2343 end if;
2345 else
2346 Set_Ekind (Id, E_Generic_Procedure);
2347 Set_Etype (Id, Standard_Void_Type);
2348 end if;
2350 -- For a library unit, we have reconstructed the entity for the unit,
2351 -- and must reset it in the library tables. We also make sure that
2352 -- Body_Required is set properly in the original compilation unit node.
2354 if Nkind (Parent (N)) = N_Compilation_Unit then
2355 Set_Cunit_Entity (Current_Sem_Unit, Id);
2356 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2357 end if;
2359 Set_Categorization_From_Pragmas (N);
2360 Validate_Categorization_Dependency (N, Id);
2362 Save_Global_References (Original_Node (N));
2364 End_Generic;
2365 End_Scope;
2366 Exit_Generic_Scope (Id);
2367 Generate_Reference_To_Formals (Id);
2368 end Analyze_Generic_Subprogram_Declaration;
2370 -----------------------------------
2371 -- Analyze_Package_Instantiation --
2372 -----------------------------------
2374 -- Note: this procedure is also used for formal package declarations, in
2375 -- which case the argument N is an N_Formal_Package_Declaration node.
2376 -- This should really be noted in the spec! ???
2378 procedure Analyze_Package_Instantiation (N : Node_Id) is
2379 Loc : constant Source_Ptr := Sloc (N);
2380 Gen_Id : constant Node_Id := Name (N);
2382 Act_Decl : Node_Id;
2383 Act_Decl_Name : Node_Id;
2384 Act_Decl_Id : Entity_Id;
2385 Act_Spec : Node_Id;
2386 Act_Tree : Node_Id;
2388 Gen_Decl : Node_Id;
2389 Gen_Unit : Entity_Id;
2391 Is_Actual_Pack : constant Boolean :=
2392 Is_Internal (Defining_Entity (N));
2394 Env_Installed : Boolean := False;
2395 Parent_Installed : Boolean := False;
2396 Renaming_List : List_Id;
2397 Unit_Renaming : Node_Id;
2398 Needs_Body : Boolean;
2399 Inline_Now : Boolean := False;
2401 procedure Delay_Descriptors (E : Entity_Id);
2402 -- Delay generation of subprogram descriptors for given entity
2404 function Might_Inline_Subp return Boolean;
2405 -- If inlining is active and the generic contains inlined subprograms,
2406 -- we instantiate the body. This may cause superfluous instantiations,
2407 -- but it is simpler than detecting the need for the body at the point
2408 -- of inlining, when the context of the instance is not available.
2410 -----------------------
2411 -- Delay_Descriptors --
2412 -----------------------
2414 procedure Delay_Descriptors (E : Entity_Id) is
2415 begin
2416 if not Delay_Subprogram_Descriptors (E) then
2417 Set_Delay_Subprogram_Descriptors (E);
2418 Pending_Descriptor.Increment_Last;
2419 Pending_Descriptor.Table (Pending_Descriptor.Last) := E;
2420 end if;
2421 end Delay_Descriptors;
2423 -----------------------
2424 -- Might_Inline_Subp --
2425 -----------------------
2427 function Might_Inline_Subp return Boolean is
2428 E : Entity_Id;
2430 begin
2431 if not Inline_Processing_Required then
2432 return False;
2434 else
2435 E := First_Entity (Gen_Unit);
2436 while Present (E) loop
2437 if Is_Subprogram (E)
2438 and then Is_Inlined (E)
2439 then
2440 return True;
2441 end if;
2443 Next_Entity (E);
2444 end loop;
2445 end if;
2447 return False;
2448 end Might_Inline_Subp;
2450 -- Start of processing for Analyze_Package_Instantiation
2452 begin
2453 -- Very first thing: apply the special kludge for Text_IO processing
2454 -- in case we are instantiating one of the children of [Wide_]Text_IO.
2456 Text_IO_Kludge (Name (N));
2458 -- Make node global for error reporting
2460 Instantiation_Node := N;
2462 -- Case of instantiation of a generic package
2464 if Nkind (N) = N_Package_Instantiation then
2465 Act_Decl_Id := New_Copy (Defining_Entity (N));
2466 Set_Comes_From_Source (Act_Decl_Id, True);
2468 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
2469 Act_Decl_Name :=
2470 Make_Defining_Program_Unit_Name (Loc,
2471 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
2472 Defining_Identifier => Act_Decl_Id);
2473 else
2474 Act_Decl_Name := Act_Decl_Id;
2475 end if;
2477 -- Case of instantiation of a formal package
2479 else
2480 Act_Decl_Id := Defining_Identifier (N);
2481 Act_Decl_Name := Act_Decl_Id;
2482 end if;
2484 Generate_Definition (Act_Decl_Id);
2485 Pre_Analyze_Actuals (N);
2487 Init_Env;
2488 Env_Installed := True;
2489 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2490 Gen_Unit := Entity (Gen_Id);
2492 -- Verify that it is the name of a generic package
2494 if Etype (Gen_Unit) = Any_Type then
2495 Restore_Env;
2496 return;
2498 elsif Ekind (Gen_Unit) /= E_Generic_Package then
2500 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
2502 if From_With_Type (Gen_Unit) then
2503 Error_Msg_N
2504 ("cannot instantiate a limited withed package", Gen_Id);
2505 else
2506 Error_Msg_N
2507 ("expect name of generic package in instantiation", Gen_Id);
2508 end if;
2510 Restore_Env;
2511 return;
2512 end if;
2514 if In_Extended_Main_Source_Unit (N) then
2515 Set_Is_Instantiated (Gen_Unit);
2516 Generate_Reference (Gen_Unit, N);
2518 if Present (Renamed_Object (Gen_Unit)) then
2519 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
2520 Generate_Reference (Renamed_Object (Gen_Unit), N);
2521 end if;
2522 end if;
2524 if Nkind (Gen_Id) = N_Identifier
2525 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
2526 then
2527 Error_Msg_NE
2528 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2530 elsif Nkind (Gen_Id) = N_Expanded_Name
2531 and then Is_Child_Unit (Gen_Unit)
2532 and then Nkind (Prefix (Gen_Id)) = N_Identifier
2533 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
2534 then
2535 Error_Msg_N
2536 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
2537 end if;
2539 Set_Entity (Gen_Id, Gen_Unit);
2541 -- If generic is a renaming, get original generic unit
2543 if Present (Renamed_Object (Gen_Unit))
2544 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
2545 then
2546 Gen_Unit := Renamed_Object (Gen_Unit);
2547 end if;
2549 -- Verify that there are no circular instantiations
2551 if In_Open_Scopes (Gen_Unit) then
2552 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
2553 Restore_Env;
2554 return;
2556 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
2557 Error_Msg_Node_2 := Current_Scope;
2558 Error_Msg_NE
2559 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
2560 Circularity_Detected := True;
2561 Restore_Env;
2562 return;
2564 else
2565 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
2566 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2568 -- Initialize renamings map, for error checking, and the list
2569 -- that holds private entities whose views have changed between
2570 -- generic definition and instantiation. If this is the instance
2571 -- created to validate an actual package, the instantiation
2572 -- environment is that of the enclosing instance.
2574 Generic_Renamings.Set_Last (0);
2575 Generic_Renamings_HTable.Reset;
2577 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2579 -- Copy original generic tree, to produce text for instantiation
2581 Act_Tree :=
2582 Copy_Generic_Node
2583 (Original_Node (Gen_Decl), Empty, Instantiating => True);
2585 Act_Spec := Specification (Act_Tree);
2587 -- If this is the instance created to validate an actual package,
2588 -- only the formals matter, do not examine the package spec itself.
2590 if Is_Actual_Pack then
2591 Set_Visible_Declarations (Act_Spec, New_List);
2592 Set_Private_Declarations (Act_Spec, New_List);
2593 end if;
2595 Renaming_List :=
2596 Analyze_Associations
2598 Generic_Formal_Declarations (Act_Tree),
2599 Generic_Formal_Declarations (Gen_Decl));
2601 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
2602 Set_Is_Generic_Instance (Act_Decl_Id);
2604 Set_Generic_Parent (Act_Spec, Gen_Unit);
2606 -- References to the generic in its own declaration or its body
2607 -- are references to the instance. Add a renaming declaration for
2608 -- the generic unit itself. This declaration, as well as the renaming
2609 -- declarations for the generic formals, must remain private to the
2610 -- unit: the formals, because this is the language semantics, and
2611 -- the unit because its use is an artifact of the implementation.
2613 Unit_Renaming :=
2614 Make_Package_Renaming_Declaration (Loc,
2615 Defining_Unit_Name =>
2616 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2617 Name => New_Reference_To (Act_Decl_Id, Loc));
2619 Append (Unit_Renaming, Renaming_List);
2621 -- The renaming declarations are the first local declarations of
2622 -- the new unit.
2624 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
2625 Insert_List_Before
2626 (First (Visible_Declarations (Act_Spec)), Renaming_List);
2627 else
2628 Set_Visible_Declarations (Act_Spec, Renaming_List);
2629 end if;
2631 Act_Decl :=
2632 Make_Package_Declaration (Loc,
2633 Specification => Act_Spec);
2635 -- Save the instantiation node, for subsequent instantiation
2636 -- of the body, if there is one and we are generating code for
2637 -- the current unit. Mark the unit as having a body, to avoid
2638 -- a premature error message.
2640 -- We instantiate the body if we are generating code, if we are
2641 -- generating cross-reference information, or if we are building
2642 -- trees for ASIS use.
2644 declare
2645 Enclosing_Body_Present : Boolean := False;
2646 -- If the generic unit is not a compilation unit, then a body
2647 -- may be present in its parent even if none is required. We
2648 -- create a tentative pending instantiation for the body, which
2649 -- will be discarded if none is actually present.
2651 Scop : Entity_Id;
2653 begin
2654 if Scope (Gen_Unit) /= Standard_Standard
2655 and then not Is_Child_Unit (Gen_Unit)
2656 then
2657 Scop := Scope (Gen_Unit);
2659 while Present (Scop)
2660 and then Scop /= Standard_Standard
2661 loop
2662 if Unit_Requires_Body (Scop) then
2663 Enclosing_Body_Present := True;
2664 exit;
2666 elsif In_Open_Scopes (Scop)
2667 and then In_Package_Body (Scop)
2668 then
2669 Enclosing_Body_Present := True;
2670 exit;
2671 end if;
2673 exit when Is_Compilation_Unit (Scop);
2674 Scop := Scope (Scop);
2675 end loop;
2676 end if;
2678 -- If front-end inlining is enabled, and this is a unit for which
2679 -- code will be generated, we instantiate the body at once.
2680 -- This is done if the instance is not the main unit, and if the
2681 -- generic is not a child unit of another generic, to avoid scope
2682 -- problems and the reinstallation of parent instances.
2684 if Expander_Active
2685 and then (not Is_Child_Unit (Gen_Unit)
2686 or else not Is_Generic_Unit (Scope (Gen_Unit)))
2687 and then Might_Inline_Subp
2688 and then not Is_Actual_Pack
2689 then
2690 if Front_End_Inlining
2691 and then (Is_In_Main_Unit (N)
2692 or else In_Main_Context (Current_Scope))
2693 and then Nkind (Parent (N)) /= N_Compilation_Unit
2694 then
2695 Inline_Now := True;
2697 -- In configurable_run_time mode we force the inlining of
2698 -- predefined subprogram marked Inline_Always, to minimize
2699 -- the use of the run-time library.
2701 elsif Is_Predefined_File_Name
2702 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
2703 and then Configurable_Run_Time_Mode
2704 and then Nkind (Parent (N)) /= N_Compilation_Unit
2705 then
2706 Inline_Now := True;
2707 end if;
2709 -- If the current scope is itself an instance within a child
2710 -- unit, and that unit itself is not an instance, it is
2711 -- duplicated in the scope stack, and the unstacking mechanism
2712 -- in Inline_Instance_Body will fail. This loses some rare
2713 -- cases of optimization, and might be improved some day ????
2715 if Is_Generic_Instance (Current_Scope)
2716 and then Is_Child_Unit (Scope (Current_Scope))
2717 and then not Is_Generic_Instance (Scope (Current_Scope))
2718 then
2719 Inline_Now := False;
2720 end if;
2721 end if;
2723 Needs_Body :=
2724 (Unit_Requires_Body (Gen_Unit)
2725 or else Enclosing_Body_Present
2726 or else Present (Corresponding_Body (Gen_Decl)))
2727 and then (Is_In_Main_Unit (N)
2728 or else Might_Inline_Subp)
2729 and then not Is_Actual_Pack
2730 and then not Inline_Now
2731 and then (Operating_Mode = Generate_Code
2732 or else (Operating_Mode = Check_Semantics
2733 and then ASIS_Mode));
2735 -- If front_end_inlining is enabled, do not instantiate a
2736 -- body if within a generic context.
2738 if (Front_End_Inlining
2739 and then not Expander_Active)
2740 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
2741 then
2742 Needs_Body := False;
2743 end if;
2745 -- If the current context is generic, and the package being
2746 -- instantiated is declared within a formal package, there is no
2747 -- body to instantiate until the enclosing generic is instantiated
2748 -- and there is an actual for the formal package. If the formal
2749 -- package has parameters, we build regular package instance for
2750 -- it, that preceeds the original formal package declaration.
2752 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
2753 declare
2754 Decl : constant Node_Id :=
2755 Original_Node
2756 (Unit_Declaration_Node (Scope (Gen_Unit)));
2757 begin
2758 if Nkind (Decl) = N_Formal_Package_Declaration
2759 or else (Nkind (Decl) = N_Package_Declaration
2760 and then Is_List_Member (Decl)
2761 and then Present (Next (Decl))
2762 and then
2763 Nkind (Next (Decl)) = N_Formal_Package_Declaration)
2764 then
2765 Needs_Body := False;
2766 end if;
2767 end;
2768 end if;
2769 end;
2771 -- If we are generating the calling stubs from the instantiation of
2772 -- a generic RCI package, we will not use the body of the generic
2773 -- package.
2775 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
2776 and then Is_Compilation_Unit (Defining_Entity (N))
2777 then
2778 Needs_Body := False;
2779 end if;
2781 if Needs_Body then
2783 -- Here is a defence against a ludicrous number of instantiations
2784 -- caused by a circular set of instantiation attempts.
2786 if Pending_Instantiations.Last >
2787 Hostparm.Max_Instantiations
2788 then
2789 Error_Msg_N ("too many instantiations", N);
2790 raise Unrecoverable_Error;
2791 end if;
2793 -- Indicate that the enclosing scopes contain an instantiation,
2794 -- and that cleanup actions should be delayed until after the
2795 -- instance body is expanded.
2797 Check_Forward_Instantiation (Gen_Decl);
2798 if Nkind (N) = N_Package_Instantiation then
2799 declare
2800 Enclosing_Master : Entity_Id := Current_Scope;
2802 begin
2803 while Enclosing_Master /= Standard_Standard loop
2805 if Ekind (Enclosing_Master) = E_Package then
2806 if Is_Compilation_Unit (Enclosing_Master) then
2807 if In_Package_Body (Enclosing_Master) then
2808 Delay_Descriptors
2809 (Body_Entity (Enclosing_Master));
2810 else
2811 Delay_Descriptors
2812 (Enclosing_Master);
2813 end if;
2815 exit;
2817 else
2818 Enclosing_Master := Scope (Enclosing_Master);
2819 end if;
2821 elsif Ekind (Enclosing_Master) = E_Generic_Package then
2822 Enclosing_Master := Scope (Enclosing_Master);
2824 elsif Is_Generic_Subprogram (Enclosing_Master)
2825 or else Ekind (Enclosing_Master) = E_Void
2826 then
2827 -- Cleanup actions will eventually be performed on
2828 -- the enclosing instance, if any. enclosing scope
2829 -- is void in the formal part of a generic subp.
2831 exit;
2833 else
2834 if Ekind (Enclosing_Master) = E_Entry
2835 and then
2836 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
2837 then
2838 Enclosing_Master :=
2839 Protected_Body_Subprogram (Enclosing_Master);
2840 end if;
2842 Set_Delay_Cleanups (Enclosing_Master);
2844 while Ekind (Enclosing_Master) = E_Block loop
2845 Enclosing_Master := Scope (Enclosing_Master);
2846 end loop;
2848 if Is_Subprogram (Enclosing_Master) then
2849 Delay_Descriptors (Enclosing_Master);
2851 elsif Is_Task_Type (Enclosing_Master) then
2852 declare
2853 TBP : constant Node_Id :=
2854 Get_Task_Body_Procedure
2855 (Enclosing_Master);
2857 begin
2858 if Present (TBP) then
2859 Delay_Descriptors (TBP);
2860 Set_Delay_Cleanups (TBP);
2861 end if;
2862 end;
2863 end if;
2865 exit;
2866 end if;
2867 end loop;
2868 end;
2870 -- Make entry in table
2872 Pending_Instantiations.Increment_Last;
2873 Pending_Instantiations.Table (Pending_Instantiations.Last) :=
2874 (N, Act_Decl, Expander_Active, Current_Sem_Unit);
2875 end if;
2876 end if;
2878 Set_Categorization_From_Pragmas (Act_Decl);
2880 if Parent_Installed then
2881 Hide_Current_Scope;
2882 end if;
2884 Set_Instance_Spec (N, Act_Decl);
2886 -- If not a compilation unit, insert the package declaration
2887 -- before the original instantiation node.
2889 if Nkind (Parent (N)) /= N_Compilation_Unit then
2890 Mark_Rewrite_Insertion (Act_Decl);
2891 Insert_Before (N, Act_Decl);
2892 Analyze (Act_Decl);
2894 -- For an instantiation that is a compilation unit, place
2895 -- declaration on current node so context is complete
2896 -- for analysis (including nested instantiations). It this
2897 -- is the main unit, the declaration eventually replaces the
2898 -- instantiation node. If the instance body is later created, it
2899 -- replaces the instance node, and the declation is attached to
2900 -- it (see Build_Instance_Compilation_Unit_Nodes).
2902 else
2903 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
2905 -- The entity for the current unit is the newly created one,
2906 -- and all semantic information is attached to it.
2908 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
2910 -- If this is the main unit, replace the main entity as well
2912 if Current_Sem_Unit = Main_Unit then
2913 Main_Unit_Entity := Act_Decl_Id;
2914 end if;
2915 end if;
2917 -- There is a problem with inlining here
2918 -- More comments needed??? what problem
2920 Set_Unit (Parent (N), Act_Decl);
2921 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
2922 Set_Package_Instantiation (Act_Decl_Id, N);
2923 Analyze (Act_Decl);
2924 Set_Unit (Parent (N), N);
2925 Set_Body_Required (Parent (N), False);
2927 -- We never need elaboration checks on instantiations, since
2928 -- by definition, the body instantiation is elaborated at the
2929 -- same time as the spec instantiation.
2931 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
2932 Set_Kill_Elaboration_Checks (Act_Decl_Id);
2933 end if;
2935 Check_Elab_Instantiation (N);
2937 if ABE_Is_Certain (N) and then Needs_Body then
2938 Pending_Instantiations.Decrement_Last;
2939 end if;
2940 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
2942 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
2943 First_Private_Entity (Act_Decl_Id));
2945 -- If the instantiation will receive a body, the unit will
2946 -- be transformed into a package body, and receive its own
2947 -- elaboration entity. Otherwise, the nature of the unit is
2948 -- now a package declaration.
2950 if Nkind (Parent (N)) = N_Compilation_Unit
2951 and then not Needs_Body
2952 then
2953 Rewrite (N, Act_Decl);
2954 end if;
2956 if Present (Corresponding_Body (Gen_Decl))
2957 or else Unit_Requires_Body (Gen_Unit)
2958 then
2959 Set_Has_Completion (Act_Decl_Id);
2960 end if;
2962 Check_Formal_Packages (Act_Decl_Id);
2964 Restore_Private_Views (Act_Decl_Id);
2966 if not Generic_Separately_Compiled (Gen_Unit) then
2967 Inherit_Context (Gen_Decl, N);
2968 end if;
2970 if Parent_Installed then
2971 Remove_Parent;
2972 end if;
2974 Restore_Env;
2975 Env_Installed := False;
2976 end if;
2978 Validate_Categorization_Dependency (N, Act_Decl_Id);
2980 -- Check restriction, but skip this if something went wrong in
2981 -- the above analysis, indicated by Act_Decl_Id being void.
2983 if Ekind (Act_Decl_Id) /= E_Void
2984 and then not Is_Library_Level_Entity (Act_Decl_Id)
2985 then
2986 Check_Restriction (No_Local_Allocators, N);
2987 end if;
2989 if Inline_Now then
2990 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
2991 end if;
2993 -- The following is a tree patch for ASIS: ASIS needs separate nodes
2994 -- to be used as defining identifiers for a formal package and for the
2995 -- corresponding expanded package
2997 if Nkind (N) = N_Formal_Package_Declaration then
2998 Act_Decl_Id := New_Copy (Defining_Entity (N));
2999 Set_Comes_From_Source (Act_Decl_Id, True);
3000 Set_Is_Generic_Instance (Act_Decl_Id, False);
3001 Set_Defining_Identifier (N, Act_Decl_Id);
3002 end if;
3004 exception
3005 when Instantiation_Error =>
3006 if Parent_Installed then
3007 Remove_Parent;
3008 end if;
3010 if Env_Installed then
3011 Restore_Env;
3012 end if;
3013 end Analyze_Package_Instantiation;
3015 --------------------------
3016 -- Inline_Instance_Body --
3017 --------------------------
3019 procedure Inline_Instance_Body
3020 (N : Node_Id;
3021 Gen_Unit : Entity_Id;
3022 Act_Decl : Node_Id)
3024 Vis : Boolean;
3025 Gen_Comp : constant Entity_Id :=
3026 Cunit_Entity (Get_Source_Unit (Gen_Unit));
3027 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
3028 Curr_Scope : Entity_Id := Empty;
3029 Curr_Unit : constant Entity_Id :=
3030 Cunit_Entity (Current_Sem_Unit);
3031 Removed : Boolean := False;
3032 Num_Scopes : Int := 0;
3033 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
3034 Instances : array (1 .. Scope_Stack.Last) of Entity_Id;
3035 Inner_Scopes : array (1 .. Scope_Stack.Last) of Entity_Id;
3036 Num_Inner : Int := 0;
3037 N_Instances : Int := 0;
3038 S : Entity_Id;
3040 begin
3041 -- Case of generic unit defined in another unit. We must remove the
3042 -- complete context of the current unit to install that of the generic.
3044 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
3046 -- Add some comments for the following two loops ???
3048 S := Current_Scope;
3049 while Present (S) and then S /= Standard_Standard loop
3050 loop
3051 Num_Scopes := Num_Scopes + 1;
3053 Use_Clauses (Num_Scopes) :=
3054 (Scope_Stack.Table
3055 (Scope_Stack.Last - Num_Scopes + 1).
3056 First_Use_Clause);
3057 End_Use_Clauses (Use_Clauses (Num_Scopes));
3059 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
3060 or else Scope_Stack.Table
3061 (Scope_Stack.Last - Num_Scopes).Entity
3062 = Scope (S);
3063 end loop;
3065 exit when Is_Generic_Instance (S)
3066 and then (In_Package_Body (S)
3067 or else Ekind (S) = E_Procedure
3068 or else Ekind (S) = E_Function);
3069 S := Scope (S);
3070 end loop;
3072 Vis := Is_Immediately_Visible (Gen_Comp);
3074 -- Find and save all enclosing instances
3076 S := Current_Scope;
3078 while Present (S)
3079 and then S /= Standard_Standard
3080 loop
3081 if Is_Generic_Instance (S) then
3082 N_Instances := N_Instances + 1;
3083 Instances (N_Instances) := S;
3085 exit when In_Package_Body (S);
3086 end if;
3088 S := Scope (S);
3089 end loop;
3091 -- Remove context of current compilation unit, unless we are within a
3092 -- nested package instantiation, in which case the context has been
3093 -- removed previously.
3095 -- If current scope is the body of a child unit, remove context of
3096 -- spec as well.
3098 S := Current_Scope;
3100 while Present (S)
3101 and then S /= Standard_Standard
3102 loop
3103 exit when Is_Generic_Instance (S)
3104 and then (In_Package_Body (S)
3105 or else Ekind (S) = E_Procedure
3106 or else Ekind (S) = E_Function);
3108 if S = Curr_Unit
3109 or else (Ekind (Curr_Unit) = E_Package_Body
3110 and then S = Spec_Entity (Curr_Unit))
3111 or else (Ekind (Curr_Unit) = E_Subprogram_Body
3112 and then S =
3113 Corresponding_Spec
3114 (Unit_Declaration_Node (Curr_Unit)))
3115 then
3116 Removed := True;
3118 -- Remove entities in current scopes from visibility, so
3119 -- that instance body is compiled in a clean environment.
3121 Save_Scope_Stack (Handle_Use => False);
3123 if Is_Child_Unit (S) then
3125 -- Remove child unit from stack, as well as inner scopes.
3126 -- Removing the context of a child unit removes parent
3127 -- units as well.
3129 while Current_Scope /= S loop
3130 Num_Inner := Num_Inner + 1;
3131 Inner_Scopes (Num_Inner) := Current_Scope;
3132 Pop_Scope;
3133 end loop;
3135 Pop_Scope;
3136 Remove_Context (Curr_Comp);
3137 Curr_Scope := S;
3139 else
3140 Remove_Context (Curr_Comp);
3141 end if;
3143 if Ekind (Curr_Unit) = E_Package_Body then
3144 Remove_Context (Library_Unit (Curr_Comp));
3145 end if;
3146 end if;
3148 S := Scope (S);
3149 end loop;
3150 pragma Assert (Num_Inner < Num_Scopes);
3152 New_Scope (Standard_Standard);
3153 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
3154 Instantiate_Package_Body
3155 ((N, Act_Decl, Expander_Active, Current_Sem_Unit), True);
3156 Pop_Scope;
3158 -- Restore context
3160 Set_Is_Immediately_Visible (Gen_Comp, Vis);
3162 -- Reset Generic_Instance flag so that use clauses can be installed
3163 -- in the proper order. (See Use_One_Package for effect of enclosing
3164 -- instances on processing of use clauses).
3166 for J in 1 .. N_Instances loop
3167 Set_Is_Generic_Instance (Instances (J), False);
3168 end loop;
3170 if Removed then
3171 Install_Context (Curr_Comp);
3173 if Present (Curr_Scope)
3174 and then Is_Child_Unit (Curr_Scope)
3175 then
3176 New_Scope (Curr_Scope);
3177 Set_Is_Immediately_Visible (Curr_Scope);
3179 -- Finally, restore inner scopes as well
3181 for J in reverse 1 .. Num_Inner loop
3182 New_Scope (Inner_Scopes (J));
3183 end loop;
3184 end if;
3186 Restore_Scope_Stack (Handle_Use => False);
3188 if Present (Curr_Scope)
3189 and then
3190 (In_Private_Part (Curr_Scope)
3191 or else In_Package_Body (Curr_Scope))
3192 then
3193 -- Install private declaration of ancestor units, which
3194 -- are currently available. Restore_Scope_Stack and
3195 -- Install_Context only install the visible part of parents.
3197 declare
3198 Par : Entity_Id;
3199 begin
3200 Par := Scope (Curr_Scope);
3201 while (Present (Par))
3202 and then Par /= Standard_Standard
3203 loop
3204 Install_Private_Declarations (Par);
3205 Par := Scope (Par);
3206 end loop;
3207 end;
3208 end if;
3209 end if;
3211 -- Restore use clauses. For a child unit, use clauses in the parents
3212 -- are restored when installing the context, so only those in inner
3213 -- scopes (and those local to the child unit itself) need to be
3214 -- installed explicitly.
3216 if Is_Child_Unit (Curr_Unit)
3217 and then Removed
3218 then
3219 for J in reverse 1 .. Num_Inner + 1 loop
3220 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3221 Use_Clauses (J);
3222 Install_Use_Clauses (Use_Clauses (J));
3223 end loop;
3225 else
3226 for J in reverse 1 .. Num_Scopes loop
3227 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3228 Use_Clauses (J);
3229 Install_Use_Clauses (Use_Clauses (J));
3230 end loop;
3231 end if;
3233 for J in 1 .. N_Instances loop
3234 Set_Is_Generic_Instance (Instances (J), True);
3235 end loop;
3237 -- If generic unit is in current unit, current context is correct
3239 else
3240 Instantiate_Package_Body
3241 ((N, Act_Decl, Expander_Active, Current_Sem_Unit), True);
3242 end if;
3243 end Inline_Instance_Body;
3245 -------------------------------------
3246 -- Analyze_Procedure_Instantiation --
3247 -------------------------------------
3249 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
3250 begin
3251 Analyze_Subprogram_Instantiation (N, E_Procedure);
3252 end Analyze_Procedure_Instantiation;
3254 --------------------------------------
3255 -- Analyze_Subprogram_Instantiation --
3256 --------------------------------------
3258 procedure Analyze_Subprogram_Instantiation
3259 (N : Node_Id;
3260 K : Entity_Kind)
3262 Loc : constant Source_Ptr := Sloc (N);
3263 Gen_Id : constant Node_Id := Name (N);
3265 Anon_Id : constant Entity_Id :=
3266 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
3267 Chars => New_External_Name
3268 (Chars (Defining_Entity (N)), 'R'));
3270 Act_Decl_Id : Entity_Id;
3271 Act_Decl : Node_Id;
3272 Act_Spec : Node_Id;
3273 Act_Tree : Node_Id;
3275 Env_Installed : Boolean := False;
3276 Gen_Unit : Entity_Id;
3277 Gen_Decl : Node_Id;
3278 Pack_Id : Entity_Id;
3279 Parent_Installed : Boolean := False;
3280 Renaming_List : List_Id;
3282 procedure Analyze_Instance_And_Renamings;
3283 -- The instance must be analyzed in a context that includes the
3284 -- mappings of generic parameters into actuals. We create a package
3285 -- declaration for this purpose, and a subprogram with an internal
3286 -- name within the package. The subprogram instance is simply an
3287 -- alias for the internal subprogram, declared in the current scope.
3289 ------------------------------------
3290 -- Analyze_Instance_And_Renamings --
3291 ------------------------------------
3293 procedure Analyze_Instance_And_Renamings is
3294 Def_Ent : constant Entity_Id := Defining_Entity (N);
3295 Pack_Decl : Node_Id;
3297 begin
3298 if Nkind (Parent (N)) = N_Compilation_Unit then
3300 -- For the case of a compilation unit, the container package
3301 -- has the same name as the instantiation, to insure that the
3302 -- binder calls the elaboration procedure with the right name.
3303 -- Copy the entity of the instance, which may have compilation
3304 -- level flags (e.g. Is_Child_Unit) set.
3306 Pack_Id := New_Copy (Def_Ent);
3308 else
3309 -- Otherwise we use the name of the instantiation concatenated
3310 -- with its source position to ensure uniqueness if there are
3311 -- several instantiations with the same name.
3313 Pack_Id :=
3314 Make_Defining_Identifier (Loc,
3315 Chars => New_External_Name
3316 (Related_Id => Chars (Def_Ent),
3317 Suffix => "GP",
3318 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
3319 end if;
3321 Pack_Decl := Make_Package_Declaration (Loc,
3322 Specification => Make_Package_Specification (Loc,
3323 Defining_Unit_Name => Pack_Id,
3324 Visible_Declarations => Renaming_List,
3325 End_Label => Empty));
3327 Set_Instance_Spec (N, Pack_Decl);
3328 Set_Is_Generic_Instance (Pack_Id);
3329 Set_Needs_Debug_Info (Pack_Id);
3331 -- Case of not a compilation unit
3333 if Nkind (Parent (N)) /= N_Compilation_Unit then
3334 Mark_Rewrite_Insertion (Pack_Decl);
3335 Insert_Before (N, Pack_Decl);
3336 Set_Has_Completion (Pack_Id);
3338 -- Case of an instantiation that is a compilation unit
3340 -- Place declaration on current node so context is complete
3341 -- for analysis (including nested instantiations), and for
3342 -- use in a context_clause (see Analyze_With_Clause).
3344 else
3345 Set_Unit (Parent (N), Pack_Decl);
3346 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
3347 end if;
3349 Analyze (Pack_Decl);
3350 Check_Formal_Packages (Pack_Id);
3351 Set_Is_Generic_Instance (Pack_Id, False);
3353 -- Body of the enclosing package is supplied when instantiating
3354 -- the subprogram body, after semantic analysis is completed.
3356 if Nkind (Parent (N)) = N_Compilation_Unit then
3358 -- Remove package itself from visibility, so it does not
3359 -- conflict with subprogram.
3361 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
3363 -- Set name and scope of internal subprogram so that the
3364 -- proper external name will be generated. The proper scope
3365 -- is the scope of the wrapper package. We need to generate
3366 -- debugging information for the internal subprogram, so set
3367 -- flag accordingly.
3369 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
3370 Set_Scope (Anon_Id, Scope (Pack_Id));
3372 -- Mark wrapper package as referenced, to avoid spurious
3373 -- warnings if the instantiation appears in various with_
3374 -- clauses of subunits of the main unit.
3376 Set_Referenced (Pack_Id);
3377 end if;
3379 Set_Is_Generic_Instance (Anon_Id);
3380 Set_Needs_Debug_Info (Anon_Id);
3381 Act_Decl_Id := New_Copy (Anon_Id);
3383 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3384 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
3385 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
3386 Set_Comes_From_Source (Act_Decl_Id, True);
3388 -- The signature may involve types that are not frozen yet, but
3389 -- the subprogram will be frozen at the point the wrapper package
3390 -- is frozen, so it does not need its own freeze node. In fact, if
3391 -- one is created, it might conflict with the freezing actions from
3392 -- the wrapper package (see 7206-013).
3394 Set_Has_Delayed_Freeze (Anon_Id, False);
3396 -- If the instance is a child unit, mark the Id accordingly. Mark
3397 -- the anonymous entity as well, which is the real subprogram and
3398 -- which is used when the instance appears in a context clause.
3400 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
3401 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
3402 New_Overloaded_Entity (Act_Decl_Id);
3403 Check_Eliminated (Act_Decl_Id);
3405 -- In compilation unit case, kill elaboration checks on the
3406 -- instantiation, since they are never needed -- the body is
3407 -- instantiated at the same point as the spec.
3409 if Nkind (Parent (N)) = N_Compilation_Unit then
3410 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
3411 Set_Kill_Elaboration_Checks (Act_Decl_Id);
3412 Set_Is_Compilation_Unit (Anon_Id);
3414 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
3415 end if;
3417 -- The instance is not a freezing point for the new subprogram
3419 Set_Is_Frozen (Act_Decl_Id, False);
3421 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
3422 Valid_Operator_Definition (Act_Decl_Id);
3423 end if;
3425 Set_Alias (Act_Decl_Id, Anon_Id);
3426 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3427 Set_Has_Completion (Act_Decl_Id);
3428 Set_Related_Instance (Pack_Id, Act_Decl_Id);
3430 if Nkind (Parent (N)) = N_Compilation_Unit then
3431 Set_Body_Required (Parent (N), False);
3432 end if;
3434 end Analyze_Instance_And_Renamings;
3436 -- Start of processing for Analyze_Subprogram_Instantiation
3438 begin
3439 -- Very first thing: apply the special kludge for Text_IO processing
3440 -- in case we are instantiating one of the children of [Wide_]Text_IO.
3441 -- Of course such an instantiation is bogus (these are packages, not
3442 -- subprograms), but we get a better error message if we do this.
3444 Text_IO_Kludge (Gen_Id);
3446 -- Make node global for error reporting
3448 Instantiation_Node := N;
3449 Pre_Analyze_Actuals (N);
3451 Init_Env;
3452 Env_Installed := True;
3453 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3454 Gen_Unit := Entity (Gen_Id);
3456 Generate_Reference (Gen_Unit, Gen_Id);
3458 if Nkind (Gen_Id) = N_Identifier
3459 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3460 then
3461 Error_Msg_NE
3462 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3463 end if;
3465 if Etype (Gen_Unit) = Any_Type then
3466 Restore_Env;
3467 return;
3468 end if;
3470 -- Verify that it is a generic subprogram of the right kind, and that
3471 -- it does not lead to a circular instantiation.
3473 if Ekind (Gen_Unit) /= E_Generic_Procedure
3474 and then Ekind (Gen_Unit) /= E_Generic_Function
3475 then
3476 Error_Msg_N ("expect generic subprogram in instantiation", Gen_Id);
3478 elsif In_Open_Scopes (Gen_Unit) then
3479 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3481 elsif K = E_Procedure
3482 and then Ekind (Gen_Unit) /= E_Generic_Procedure
3483 then
3484 if Ekind (Gen_Unit) = E_Generic_Function then
3485 Error_Msg_N
3486 ("cannot instantiate generic function as procedure", Gen_Id);
3487 else
3488 Error_Msg_N
3489 ("expect name of generic procedure in instantiation", Gen_Id);
3490 end if;
3492 elsif K = E_Function
3493 and then Ekind (Gen_Unit) /= E_Generic_Function
3494 then
3495 if Ekind (Gen_Unit) = E_Generic_Procedure then
3496 Error_Msg_N
3497 ("cannot instantiate generic procedure as function", Gen_Id);
3498 else
3499 Error_Msg_N
3500 ("expect name of generic function in instantiation", Gen_Id);
3501 end if;
3503 else
3504 Set_Entity (Gen_Id, Gen_Unit);
3505 Set_Is_Instantiated (Gen_Unit);
3507 if In_Extended_Main_Source_Unit (N) then
3508 Generate_Reference (Gen_Unit, N);
3509 end if;
3511 -- If renaming, get original unit
3513 if Present (Renamed_Object (Gen_Unit))
3514 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
3515 or else
3516 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
3517 then
3518 Gen_Unit := Renamed_Object (Gen_Unit);
3519 Set_Is_Instantiated (Gen_Unit);
3520 Generate_Reference (Gen_Unit, N);
3521 end if;
3523 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3524 Error_Msg_Node_2 := Current_Scope;
3525 Error_Msg_NE
3526 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3527 Circularity_Detected := True;
3528 return;
3529 end if;
3531 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3533 -- The subprogram itself cannot contain a nested instance, so
3534 -- the current parent is left empty.
3536 Set_Instance_Env (Gen_Unit, Empty);
3538 -- Initialize renamings map, for error checking
3540 Generic_Renamings.Set_Last (0);
3541 Generic_Renamings_HTable.Reset;
3543 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3545 -- Copy original generic tree, to produce text for instantiation
3547 Act_Tree :=
3548 Copy_Generic_Node
3549 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3551 Act_Spec := Specification (Act_Tree);
3552 Renaming_List :=
3553 Analyze_Associations
3555 Generic_Formal_Declarations (Act_Tree),
3556 Generic_Formal_Declarations (Gen_Decl));
3558 -- Build the subprogram declaration, which does not appear
3559 -- in the generic template, and give it a sloc consistent
3560 -- with that of the template.
3562 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
3563 Set_Generic_Parent (Act_Spec, Gen_Unit);
3564 Act_Decl :=
3565 Make_Subprogram_Declaration (Sloc (Act_Spec),
3566 Specification => Act_Spec);
3568 Set_Categorization_From_Pragmas (Act_Decl);
3570 if Parent_Installed then
3571 Hide_Current_Scope;
3572 end if;
3574 Append (Act_Decl, Renaming_List);
3575 Analyze_Instance_And_Renamings;
3577 -- If the generic is marked Import (Intrinsic), then so is the
3578 -- instance. This indicates that there is no body to instantiate.
3579 -- If generic is marked inline, so it the instance, and the
3580 -- anonymous subprogram it renames. If inlined, or else if inlining
3581 -- is enabled for the compilation, we generate the instance body
3582 -- even if it is not within the main unit.
3584 -- Any other pragmas might also be inherited ???
3586 if Is_Intrinsic_Subprogram (Gen_Unit) then
3587 Set_Is_Intrinsic_Subprogram (Anon_Id);
3588 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
3590 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
3591 Validate_Unchecked_Conversion (N, Act_Decl_Id);
3592 end if;
3593 end if;
3595 Generate_Definition (Act_Decl_Id);
3597 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
3598 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
3600 if not Is_Intrinsic_Subprogram (Gen_Unit) then
3601 Check_Elab_Instantiation (N);
3602 end if;
3604 if Is_Dispatching_Operation (Act_Decl_Id)
3605 and then Ada_Version >= Ada_05
3606 then
3607 declare
3608 Formal : Entity_Id;
3610 begin
3611 Formal := First_Formal (Act_Decl_Id);
3612 while Present (Formal) loop
3613 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
3614 and then Is_Controlling_Formal (Formal)
3615 and then not Can_Never_Be_Null (Formal)
3616 then
3617 Error_Msg_NE ("access parameter& is controlling,",
3618 N, Formal);
3619 Error_Msg_NE ("\corresponding parameter of & must be"
3620 & " explicitly null-excluding", N, Gen_Id);
3621 end if;
3623 Next_Formal (Formal);
3624 end loop;
3625 end;
3626 end if;
3628 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
3630 -- Subject to change, pending on if other pragmas are inherited ???
3632 Validate_Categorization_Dependency (N, Act_Decl_Id);
3634 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
3635 if not Generic_Separately_Compiled (Gen_Unit) then
3636 Inherit_Context (Gen_Decl, N);
3637 end if;
3639 Restore_Private_Views (Pack_Id, False);
3641 -- If the context requires a full instantiation, mark node for
3642 -- subsequent construction of the body.
3644 if (Is_In_Main_Unit (N)
3645 or else Is_Inlined (Act_Decl_Id))
3646 and then (Operating_Mode = Generate_Code
3647 or else (Operating_Mode = Check_Semantics
3648 and then ASIS_Mode))
3649 and then (Expander_Active or else ASIS_Mode)
3650 and then not ABE_Is_Certain (N)
3651 and then not Is_Eliminated (Act_Decl_Id)
3652 then
3653 Pending_Instantiations.Increment_Last;
3654 Pending_Instantiations.Table (Pending_Instantiations.Last) :=
3655 (N, Act_Decl, Expander_Active, Current_Sem_Unit);
3656 Check_Forward_Instantiation (Gen_Decl);
3658 -- The wrapper package is always delayed, because it does
3659 -- not constitute a freeze point, but to insure that the
3660 -- freeze node is placed properly, it is created directly
3661 -- when instantiating the body (otherwise the freeze node
3662 -- might appear to early for nested instantiations).
3664 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3666 -- For ASIS purposes, indicate that the wrapper package has
3667 -- replaced the instantiation node.
3669 Rewrite (N, Unit (Parent (N)));
3670 Set_Unit (Parent (N), N);
3671 end if;
3673 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3675 -- Replace instance node for library-level instantiations
3676 -- of intrinsic subprograms, for ASIS use.
3678 Rewrite (N, Unit (Parent (N)));
3679 Set_Unit (Parent (N), N);
3680 end if;
3682 if Parent_Installed then
3683 Remove_Parent;
3684 end if;
3686 Restore_Env;
3687 Env_Installed := False;
3688 Generic_Renamings.Set_Last (0);
3689 Generic_Renamings_HTable.Reset;
3690 end if;
3692 exception
3693 when Instantiation_Error =>
3694 if Parent_Installed then
3695 Remove_Parent;
3696 end if;
3698 if Env_Installed then
3699 Restore_Env;
3700 end if;
3701 end Analyze_Subprogram_Instantiation;
3703 -------------------------
3704 -- Get_Associated_Node --
3705 -------------------------
3707 function Get_Associated_Node (N : Node_Id) return Node_Id is
3708 Assoc : Node_Id := Associated_Node (N);
3710 begin
3711 if Nkind (Assoc) /= Nkind (N) then
3712 return Assoc;
3714 elsif Nkind (Assoc) = N_Aggregate
3715 or else Nkind (Assoc) = N_Extension_Aggregate
3716 then
3717 return Assoc;
3719 else
3720 -- If the node is part of an inner generic, it may itself have been
3721 -- remapped into a further generic copy. Associated_Node is otherwise
3722 -- used for the entity of the node, and will be of a different node
3723 -- kind, or else N has been rewritten as a literal or function call.
3725 while Present (Associated_Node (Assoc))
3726 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
3727 loop
3728 Assoc := Associated_Node (Assoc);
3729 end loop;
3731 -- Follow and additional link in case the final node was rewritten.
3732 -- This can only happen with nested generic units.
3734 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
3735 and then Present (Associated_Node (Assoc))
3736 and then (Nkind (Associated_Node (Assoc)) = N_Function_Call
3737 or else
3738 Nkind (Associated_Node (Assoc)) = N_Explicit_Dereference
3739 or else
3740 Nkind (Associated_Node (Assoc)) = N_Integer_Literal
3741 or else
3742 Nkind (Associated_Node (Assoc)) = N_Real_Literal
3743 or else
3744 Nkind (Associated_Node (Assoc)) = N_String_Literal)
3745 then
3746 Assoc := Associated_Node (Assoc);
3747 end if;
3749 return Assoc;
3750 end if;
3751 end Get_Associated_Node;
3753 -------------------------------------------
3754 -- Build_Instance_Compilation_Unit_Nodes --
3755 -------------------------------------------
3757 procedure Build_Instance_Compilation_Unit_Nodes
3758 (N : Node_Id;
3759 Act_Body : Node_Id;
3760 Act_Decl : Node_Id)
3762 Decl_Cunit : Node_Id;
3763 Body_Cunit : Node_Id;
3764 Citem : Node_Id;
3765 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
3766 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
3768 begin
3769 -- A new compilation unit node is built for the instance declaration
3771 Decl_Cunit :=
3772 Make_Compilation_Unit (Sloc (N),
3773 Context_Items => Empty_List,
3774 Unit => Act_Decl,
3775 Aux_Decls_Node =>
3776 Make_Compilation_Unit_Aux (Sloc (N)));
3778 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
3779 Set_Body_Required (Decl_Cunit, True);
3781 -- We use the original instantiation compilation unit as the resulting
3782 -- compilation unit of the instance, since this is the main unit.
3784 Rewrite (N, Act_Body);
3785 Body_Cunit := Parent (N);
3787 -- The two compilation unit nodes are linked by the Library_Unit field
3789 Set_Library_Unit (Decl_Cunit, Body_Cunit);
3790 Set_Library_Unit (Body_Cunit, Decl_Cunit);
3792 -- Preserve the private nature of the package if needed
3794 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
3796 -- If the instance is not the main unit, its context, categorization,
3797 -- and elaboration entity are not relevant to the compilation.
3799 if Parent (N) /= Cunit (Main_Unit) then
3800 return;
3801 end if;
3803 -- The context clause items on the instantiation, which are now
3804 -- attached to the body compilation unit (since the body overwrote
3805 -- the original instantiation node), semantically belong on the spec,
3806 -- so copy them there. It's harmless to leave them on the body as well.
3807 -- In fact one could argue that they belong in both places.
3809 Citem := First (Context_Items (Body_Cunit));
3810 while Present (Citem) loop
3811 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
3812 Next (Citem);
3813 end loop;
3815 -- Propagate categorization flags on packages, so that they appear
3816 -- in ali file for the spec of the unit.
3818 if Ekind (New_Main) = E_Package then
3819 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
3820 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
3821 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
3822 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
3823 Set_Is_Remote_Call_Interface
3824 (Old_Main, Is_Remote_Call_Interface (New_Main));
3825 end if;
3827 -- Make entry in Units table, so that binder can generate call to
3828 -- elaboration procedure for body, if any.
3830 Make_Instance_Unit (Body_Cunit);
3831 Main_Unit_Entity := New_Main;
3832 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
3834 -- Build elaboration entity, since the instance may certainly
3835 -- generate elaboration code requiring a flag for protection.
3837 Build_Elaboration_Entity (Decl_Cunit, New_Main);
3838 end Build_Instance_Compilation_Unit_Nodes;
3840 -----------------------------------
3841 -- Check_Formal_Package_Instance --
3842 -----------------------------------
3844 -- If the formal has specific parameters, they must match those of the
3845 -- actual. Both of them are instances, and the renaming declarations
3846 -- for their formal parameters appear in the same order in both. The
3847 -- analyzed formal has been analyzed in the context of the current
3848 -- instance.
3850 procedure Check_Formal_Package_Instance
3851 (Formal_Pack : Entity_Id;
3852 Actual_Pack : Entity_Id)
3854 E1 : Entity_Id := First_Entity (Actual_Pack);
3855 E2 : Entity_Id := First_Entity (Formal_Pack);
3857 Expr1 : Node_Id;
3858 Expr2 : Node_Id;
3860 procedure Check_Mismatch (B : Boolean);
3861 -- Common error routine for mismatch between the parameters of
3862 -- the actual instance and those of the formal package.
3864 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
3865 -- The formal may come from a nested formal package, and the actual
3866 -- may have been constant-folded. To determine whether the two denote
3867 -- the same entity we may have to traverse several definitions to
3868 -- recover the ultimate entity that they refer to.
3870 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
3871 -- Similarly, if the formal comes from a nested formal package, the
3872 -- actual may designate the formal through multiple renamings, which
3873 -- have to be followed to determine the original variable in question.
3875 --------------------
3876 -- Check_Mismatch --
3877 --------------------
3879 procedure Check_Mismatch (B : Boolean) is
3880 begin
3881 if B then
3882 Error_Msg_NE
3883 ("actual for & in actual instance does not match formal",
3884 Parent (Actual_Pack), E1);
3885 end if;
3886 end Check_Mismatch;
3888 --------------------------------
3889 -- Same_Instantiated_Constant --
3890 --------------------------------
3892 function Same_Instantiated_Constant
3893 (E1, E2 : Entity_Id) return Boolean
3895 Ent : Entity_Id;
3897 begin
3898 Ent := E2;
3899 while Present (Ent) loop
3900 if E1 = Ent then
3901 return True;
3903 elsif Ekind (Ent) /= E_Constant then
3904 return False;
3906 elsif Is_Entity_Name (Constant_Value (Ent)) then
3907 if Entity (Constant_Value (Ent)) = E1 then
3908 return True;
3909 else
3910 Ent := Entity (Constant_Value (Ent));
3911 end if;
3913 -- The actual may be a constant that has been folded. Recover
3914 -- original name.
3916 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
3917 Ent := Entity (Original_Node (Constant_Value (Ent)));
3918 else
3919 return False;
3920 end if;
3921 end loop;
3923 return False;
3924 end Same_Instantiated_Constant;
3926 --------------------------------
3927 -- Same_Instantiated_Variable --
3928 --------------------------------
3930 function Same_Instantiated_Variable
3931 (E1, E2 : Entity_Id) return Boolean
3933 function Original_Entity (E : Entity_Id) return Entity_Id;
3934 -- Follow chain of renamings to the ultimate ancestor
3936 ---------------------
3937 -- Original_Entity --
3938 ---------------------
3940 function Original_Entity (E : Entity_Id) return Entity_Id is
3941 Orig : Entity_Id;
3943 begin
3944 Orig := E;
3945 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
3946 and then Present (Renamed_Object (Orig))
3947 and then Is_Entity_Name (Renamed_Object (Orig))
3948 loop
3949 Orig := Entity (Renamed_Object (Orig));
3950 end loop;
3952 return Orig;
3953 end Original_Entity;
3955 -- Start of processing for Same_Instantiated_Variable
3957 begin
3958 return Ekind (E1) = Ekind (E2)
3959 and then Original_Entity (E1) = Original_Entity (E2);
3960 end Same_Instantiated_Variable;
3962 -- Start of processing for Check_Formal_Package_Instance
3964 begin
3965 while Present (E1)
3966 and then Present (E2)
3967 loop
3968 exit when Ekind (E1) = E_Package
3969 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
3971 if Is_Type (E1) then
3973 -- Subtypes must statically match. E1 and E2 are the
3974 -- local entities that are subtypes of the actuals.
3975 -- Itypes generated for other parameters need not be checked,
3976 -- the check will be performed on the parameters themselves.
3978 if not Is_Itype (E1)
3979 and then not Is_Itype (E2)
3980 then
3981 Check_Mismatch
3982 (not Is_Type (E2)
3983 or else Etype (E1) /= Etype (E2)
3984 or else not Subtypes_Statically_Match (E1, E2));
3985 end if;
3987 elsif Ekind (E1) = E_Constant then
3989 -- IN parameters must denote the same static value, or
3990 -- the same constant, or the literal null.
3992 Expr1 := Expression (Parent (E1));
3994 if Ekind (E2) /= E_Constant then
3995 Check_Mismatch (True);
3996 goto Next_E;
3997 else
3998 Expr2 := Expression (Parent (E2));
3999 end if;
4001 if Is_Static_Expression (Expr1) then
4003 if not Is_Static_Expression (Expr2) then
4004 Check_Mismatch (True);
4006 elsif Is_Integer_Type (Etype (E1)) then
4008 declare
4009 V1 : constant Uint := Expr_Value (Expr1);
4010 V2 : constant Uint := Expr_Value (Expr2);
4011 begin
4012 Check_Mismatch (V1 /= V2);
4013 end;
4015 elsif Is_Real_Type (Etype (E1)) then
4016 declare
4017 V1 : constant Ureal := Expr_Value_R (Expr1);
4018 V2 : constant Ureal := Expr_Value_R (Expr2);
4019 begin
4020 Check_Mismatch (V1 /= V2);
4021 end;
4023 elsif Is_String_Type (Etype (E1))
4024 and then Nkind (Expr1) = N_String_Literal
4025 then
4027 if Nkind (Expr2) /= N_String_Literal then
4028 Check_Mismatch (True);
4029 else
4030 Check_Mismatch
4031 (not String_Equal (Strval (Expr1), Strval (Expr2)));
4032 end if;
4033 end if;
4035 elsif Is_Entity_Name (Expr1) then
4036 if Is_Entity_Name (Expr2) then
4037 if Entity (Expr1) = Entity (Expr2) then
4038 null;
4039 else
4040 Check_Mismatch
4041 (not Same_Instantiated_Constant
4042 (Entity (Expr1), Entity (Expr2)));
4043 end if;
4044 else
4045 Check_Mismatch (True);
4046 end if;
4048 elsif Is_Entity_Name (Original_Node (Expr1))
4049 and then Is_Entity_Name (Expr2)
4050 and then
4051 Same_Instantiated_Constant
4052 (Entity (Original_Node (Expr1)), Entity (Expr2))
4053 then
4054 null;
4056 elsif Nkind (Expr1) = N_Null then
4057 Check_Mismatch (Nkind (Expr1) /= N_Null);
4059 else
4060 Check_Mismatch (True);
4061 end if;
4063 elsif Ekind (E1) = E_Variable then
4064 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
4066 elsif Ekind (E1) = E_Package then
4067 Check_Mismatch
4068 (Ekind (E1) /= Ekind (E2)
4069 or else Renamed_Object (E1) /= Renamed_Object (E2));
4071 elsif Is_Overloadable (E1) then
4073 -- Verify that the names of the entities match.
4074 -- What if actual is an attribute ???
4076 Check_Mismatch
4077 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
4079 else
4080 raise Program_Error;
4081 end if;
4083 <<Next_E>>
4084 Next_Entity (E1);
4085 Next_Entity (E2);
4086 end loop;
4087 end Check_Formal_Package_Instance;
4089 ---------------------------
4090 -- Check_Formal_Packages --
4091 ---------------------------
4093 procedure Check_Formal_Packages (P_Id : Entity_Id) is
4094 E : Entity_Id;
4095 Formal_P : Entity_Id;
4097 begin
4098 -- Iterate through the declarations in the instance, looking for
4099 -- package renaming declarations that denote instances of formal
4100 -- packages. Stop when we find the renaming of the current package
4101 -- itself. The declaration for a formal package without a box is
4102 -- followed by an internal entity that repeats the instantiation.
4104 E := First_Entity (P_Id);
4105 while Present (E) loop
4106 if Ekind (E) = E_Package then
4107 if Renamed_Object (E) = P_Id then
4108 exit;
4110 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
4111 null;
4113 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
4114 Formal_P := Next_Entity (E);
4115 Check_Formal_Package_Instance (Formal_P, E);
4116 end if;
4117 end if;
4119 Next_Entity (E);
4120 end loop;
4121 end Check_Formal_Packages;
4123 ---------------------------------
4124 -- Check_Forward_Instantiation --
4125 ---------------------------------
4127 procedure Check_Forward_Instantiation (Decl : Node_Id) is
4128 S : Entity_Id;
4129 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
4131 begin
4132 -- The instantiation appears before the generic body if we are in the
4133 -- scope of the unit containing the generic, either in its spec or in
4134 -- the package body. and before the generic body.
4136 if Ekind (Gen_Comp) = E_Package_Body then
4137 Gen_Comp := Spec_Entity (Gen_Comp);
4138 end if;
4140 if In_Open_Scopes (Gen_Comp)
4141 and then No (Corresponding_Body (Decl))
4142 then
4143 S := Current_Scope;
4145 while Present (S)
4146 and then not Is_Compilation_Unit (S)
4147 and then not Is_Child_Unit (S)
4148 loop
4149 if Ekind (S) = E_Package then
4150 Set_Has_Forward_Instantiation (S);
4151 end if;
4153 S := Scope (S);
4154 end loop;
4155 end if;
4156 end Check_Forward_Instantiation;
4158 ---------------------------
4159 -- Check_Generic_Actuals --
4160 ---------------------------
4162 -- The visibility of the actuals may be different between the
4163 -- point of generic instantiation and the instantiation of the body.
4165 procedure Check_Generic_Actuals
4166 (Instance : Entity_Id;
4167 Is_Formal_Box : Boolean)
4169 E : Entity_Id;
4170 Astype : Entity_Id;
4172 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
4173 -- For a formal that is an array type, the component type is often
4174 -- a previous formal in the same unit. The privacy status of the
4175 -- component type will have been examined earlier in the traversal
4176 -- of the corresponding actuals, and this status should not be
4177 -- modified for the array type itself.
4178 -- To detect this case we have to rescan the list of formals, which
4179 -- is usually short enough to ignore the resulting inefficiency.
4181 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
4182 Prev : Entity_Id;
4183 begin
4184 Prev := First_Entity (Instance);
4185 while Present (Prev) loop
4186 if Is_Type (Prev)
4187 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
4188 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
4189 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
4190 then
4191 return True;
4192 elsif Prev = E then
4193 return False;
4194 else
4195 Next_Entity (Prev);
4196 end if;
4197 end loop;
4198 return False;
4199 end Denotes_Previous_Actual;
4201 -- Start of processing for Check_Generic_Actuals
4203 begin
4204 E := First_Entity (Instance);
4205 while Present (E) loop
4206 if Is_Type (E)
4207 and then Nkind (Parent (E)) = N_Subtype_Declaration
4208 and then Scope (Etype (E)) /= Instance
4209 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
4210 then
4211 if Is_Array_Type (E)
4212 and then Denotes_Previous_Actual (Component_Type (E))
4213 then
4214 null;
4215 else
4216 Check_Private_View (Subtype_Indication (Parent (E)));
4217 end if;
4218 Set_Is_Generic_Actual_Type (E, True);
4219 Set_Is_Hidden (E, False);
4220 Set_Is_Potentially_Use_Visible (E,
4221 In_Use (Instance));
4223 -- We constructed the generic actual type as a subtype of
4224 -- the supplied type. This means that it normally would not
4225 -- inherit subtype specific attributes of the actual, which
4226 -- is wrong for the generic case.
4228 Astype := Ancestor_Subtype (E);
4230 if No (Astype) then
4232 -- can happen when E is an itype that is the full view of
4233 -- a private type completed, e.g. with a constrained array.
4235 Astype := Base_Type (E);
4236 end if;
4238 Set_Size_Info (E, (Astype));
4239 Set_RM_Size (E, RM_Size (Astype));
4240 Set_First_Rep_Item (E, First_Rep_Item (Astype));
4242 if Is_Discrete_Or_Fixed_Point_Type (E) then
4243 Set_RM_Size (E, RM_Size (Astype));
4245 -- In nested instances, the base type of an access actual
4246 -- may itself be private, and need to be exchanged.
4248 elsif Is_Access_Type (E)
4249 and then Is_Private_Type (Etype (E))
4250 then
4251 Check_Private_View
4252 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
4253 end if;
4255 elsif Ekind (E) = E_Package then
4257 -- If this is the renaming for the current instance, we're done.
4258 -- Otherwise it is a formal package. If the corresponding formal
4259 -- was declared with a box, the (instantiations of the) generic
4260 -- formal part are also visible. Otherwise, ignore the entity
4261 -- created to validate the actuals.
4263 if Renamed_Object (E) = Instance then
4264 exit;
4266 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
4267 null;
4269 -- The visibility of a formal of an enclosing generic is already
4270 -- correct.
4272 elsif Denotes_Formal_Package (E) then
4273 null;
4275 elsif Present (Associated_Formal_Package (E)) then
4276 if Box_Present (Parent (Associated_Formal_Package (E))) then
4277 Check_Generic_Actuals (Renamed_Object (E), True);
4278 end if;
4280 Set_Is_Hidden (E, False);
4281 end if;
4283 -- If this is a subprogram instance (in a wrapper package) the
4284 -- actual is fully visible.
4286 elsif Is_Wrapper_Package (Instance) then
4287 Set_Is_Hidden (E, False);
4289 else
4290 Set_Is_Hidden (E, not Is_Formal_Box);
4291 end if;
4293 Next_Entity (E);
4294 end loop;
4295 end Check_Generic_Actuals;
4297 ------------------------------
4298 -- Check_Generic_Child_Unit --
4299 ------------------------------
4301 procedure Check_Generic_Child_Unit
4302 (Gen_Id : Node_Id;
4303 Parent_Installed : in out Boolean)
4305 Loc : constant Source_Ptr := Sloc (Gen_Id);
4306 Gen_Par : Entity_Id := Empty;
4307 Inst_Par : Entity_Id;
4308 E : Entity_Id;
4309 S : Node_Id;
4311 function Find_Generic_Child
4312 (Scop : Entity_Id;
4313 Id : Node_Id) return Entity_Id;
4314 -- Search generic parent for possible child unit with the given name
4316 function In_Enclosing_Instance return Boolean;
4317 -- Within an instance of the parent, the child unit may be denoted
4318 -- by a simple name, or an abbreviated expanded name. Examine enclosing
4319 -- scopes to locate a possible parent instantiation.
4321 ------------------------
4322 -- Find_Generic_Child --
4323 ------------------------
4325 function Find_Generic_Child
4326 (Scop : Entity_Id;
4327 Id : Node_Id) return Entity_Id
4329 E : Entity_Id;
4331 begin
4332 -- If entity of name is already set, instance has already been
4333 -- resolved, e.g. in an enclosing instantiation.
4335 if Present (Entity (Id)) then
4336 if Scope (Entity (Id)) = Scop then
4337 return Entity (Id);
4338 else
4339 return Empty;
4340 end if;
4342 else
4343 E := First_Entity (Scop);
4344 while Present (E) loop
4345 if Chars (E) = Chars (Id)
4346 and then Is_Child_Unit (E)
4347 then
4348 if Is_Child_Unit (E)
4349 and then not Is_Visible_Child_Unit (E)
4350 then
4351 Error_Msg_NE
4352 ("generic child unit& is not visible", Gen_Id, E);
4353 end if;
4355 Set_Entity (Id, E);
4356 return E;
4357 end if;
4359 Next_Entity (E);
4360 end loop;
4362 return Empty;
4363 end if;
4364 end Find_Generic_Child;
4366 ---------------------------
4367 -- In_Enclosing_Instance --
4368 ---------------------------
4370 function In_Enclosing_Instance return Boolean is
4371 Enclosing_Instance : Node_Id;
4372 Instance_Decl : Node_Id;
4374 begin
4375 -- We do not inline any call that contains instantiations, except
4376 -- for instantiations of Unchecked_Conversion, so if we are within
4377 -- an inlined body the current instance does not require parents.
4379 if In_Inlined_Body then
4380 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
4381 return False;
4382 end if;
4384 -- Loop to check enclosing scopes
4386 Enclosing_Instance := Current_Scope;
4387 while Present (Enclosing_Instance) loop
4388 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
4390 if Ekind (Enclosing_Instance) = E_Package
4391 and then Is_Generic_Instance (Enclosing_Instance)
4392 and then Present
4393 (Generic_Parent (Specification (Instance_Decl)))
4394 then
4395 -- Check whether the generic we are looking for is a child
4396 -- of this instance.
4398 E := Find_Generic_Child
4399 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
4400 exit when Present (E);
4402 else
4403 E := Empty;
4404 end if;
4406 Enclosing_Instance := Scope (Enclosing_Instance);
4407 end loop;
4409 if No (E) then
4411 -- Not a child unit
4413 Analyze (Gen_Id);
4414 return False;
4416 else
4417 Rewrite (Gen_Id,
4418 Make_Expanded_Name (Loc,
4419 Chars => Chars (E),
4420 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
4421 Selector_Name => New_Occurrence_Of (E, Loc)));
4423 Set_Entity (Gen_Id, E);
4424 Set_Etype (Gen_Id, Etype (E));
4425 Parent_Installed := False; -- Already in scope.
4426 return True;
4427 end if;
4428 end In_Enclosing_Instance;
4430 -- Start of processing for Check_Generic_Child_Unit
4432 begin
4433 -- If the name of the generic is given by a selected component, it
4434 -- may be the name of a generic child unit, and the prefix is the name
4435 -- of an instance of the parent, in which case the child unit must be
4436 -- visible. If this instance is not in scope, it must be placed there
4437 -- and removed after instantiation, because what is being instantiated
4438 -- is not the original child, but the corresponding child present in
4439 -- the instance of the parent.
4441 -- If the child is instantiated within the parent, it can be given by
4442 -- a simple name. In this case the instance is already in scope, but
4443 -- the child generic must be recovered from the generic parent as well.
4445 if Nkind (Gen_Id) = N_Selected_Component then
4446 S := Selector_Name (Gen_Id);
4447 Analyze (Prefix (Gen_Id));
4448 Inst_Par := Entity (Prefix (Gen_Id));
4450 if Ekind (Inst_Par) = E_Package
4451 and then Present (Renamed_Object (Inst_Par))
4452 then
4453 Inst_Par := Renamed_Object (Inst_Par);
4454 end if;
4456 if Ekind (Inst_Par) = E_Package then
4457 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
4458 Gen_Par := Generic_Parent (Parent (Inst_Par));
4460 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
4461 and then
4462 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
4463 then
4464 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
4465 end if;
4467 elsif Ekind (Inst_Par) = E_Generic_Package
4468 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
4469 then
4470 -- A formal package may be a real child package, and not the
4471 -- implicit instance within a parent. In this case the child is
4472 -- not visible and has to be retrieved explicitly as well.
4474 Gen_Par := Inst_Par;
4475 end if;
4477 if Present (Gen_Par) then
4479 -- The prefix denotes an instantiation. The entity itself
4480 -- may be a nested generic, or a child unit.
4482 E := Find_Generic_Child (Gen_Par, S);
4484 if Present (E) then
4485 Change_Selected_Component_To_Expanded_Name (Gen_Id);
4486 Set_Entity (Gen_Id, E);
4487 Set_Etype (Gen_Id, Etype (E));
4488 Set_Entity (S, E);
4489 Set_Etype (S, Etype (E));
4491 -- Indicate that this is a reference to the parent
4493 if In_Extended_Main_Source_Unit (Gen_Id) then
4494 Set_Is_Instantiated (Inst_Par);
4495 end if;
4497 -- A common mistake is to replicate the naming scheme of
4498 -- a hierarchy by instantiating a generic child directly,
4499 -- rather than the implicit child in a parent instance:
4501 -- generic .. package Gpar is ..
4502 -- generic .. package Gpar.Child is ..
4503 -- package Par is new Gpar ();
4505 -- with Gpar.Child;
4506 -- package Par.Child is new Gpar.Child ();
4507 -- rather than Par.Child
4509 -- In this case the instantiation is within Par, which is
4510 -- an instance, but Gpar does not denote Par because we are
4511 -- not IN the instance of Gpar, so this is illegal. The test
4512 -- below recognizes this particular case.
4514 if Is_Child_Unit (E)
4515 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
4516 and then (not In_Instance
4517 or else Nkind (Parent (Parent (Gen_Id))) =
4518 N_Compilation_Unit)
4519 then
4520 Error_Msg_N
4521 ("prefix of generic child unit must be instance of parent",
4522 Gen_Id);
4523 end if;
4525 if not In_Open_Scopes (Inst_Par)
4526 and then Nkind (Parent (Gen_Id)) not in
4527 N_Generic_Renaming_Declaration
4528 then
4529 Install_Parent (Inst_Par);
4530 Parent_Installed := True;
4531 end if;
4533 else
4534 -- If the generic parent does not contain an entity that
4535 -- corresponds to the selector, the instance doesn't either.
4536 -- Analyzing the node will yield the appropriate error message.
4537 -- If the entity is not a child unit, then it is an inner
4538 -- generic in the parent.
4540 Analyze (Gen_Id);
4541 end if;
4543 else
4544 Analyze (Gen_Id);
4546 if Is_Child_Unit (Entity (Gen_Id))
4547 and then
4548 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
4549 and then not In_Open_Scopes (Inst_Par)
4550 then
4551 Install_Parent (Inst_Par);
4552 Parent_Installed := True;
4553 end if;
4554 end if;
4556 elsif Nkind (Gen_Id) = N_Expanded_Name then
4558 -- Entity already present, analyze prefix, whose meaning may be
4559 -- an instance in the current context. If it is an instance of
4560 -- a relative within another, the proper parent may still have
4561 -- to be installed, if they are not of the same generation.
4563 Analyze (Prefix (Gen_Id));
4564 Inst_Par := Entity (Prefix (Gen_Id));
4566 if In_Enclosing_Instance then
4567 null;
4569 elsif Present (Entity (Gen_Id))
4570 and then Is_Child_Unit (Entity (Gen_Id))
4571 and then not In_Open_Scopes (Inst_Par)
4572 then
4573 Install_Parent (Inst_Par);
4574 Parent_Installed := True;
4575 end if;
4577 elsif In_Enclosing_Instance then
4579 -- The child unit is found in some enclosing scope
4581 null;
4583 else
4584 Analyze (Gen_Id);
4586 -- If this is the renaming of the implicit child in a parent
4587 -- instance, recover the parent name and install it.
4589 if Is_Entity_Name (Gen_Id) then
4590 E := Entity (Gen_Id);
4592 if Is_Generic_Unit (E)
4593 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
4594 and then Is_Child_Unit (Renamed_Object (E))
4595 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
4596 and then Nkind (Name (Parent (E))) = N_Expanded_Name
4597 then
4598 Rewrite (Gen_Id,
4599 New_Copy_Tree (Name (Parent (E))));
4600 Inst_Par := Entity (Prefix (Gen_Id));
4602 if not In_Open_Scopes (Inst_Par) then
4603 Install_Parent (Inst_Par);
4604 Parent_Installed := True;
4605 end if;
4607 -- If it is a child unit of a non-generic parent, it may be
4608 -- use-visible and given by a direct name. Install parent as
4609 -- for other cases.
4611 elsif Is_Generic_Unit (E)
4612 and then Is_Child_Unit (E)
4613 and then
4614 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
4615 and then not Is_Generic_Unit (Scope (E))
4616 then
4617 if not In_Open_Scopes (Scope (E)) then
4618 Install_Parent (Scope (E));
4619 Parent_Installed := True;
4620 end if;
4621 end if;
4622 end if;
4623 end if;
4624 end Check_Generic_Child_Unit;
4626 -----------------------------
4627 -- Check_Hidden_Child_Unit --
4628 -----------------------------
4630 procedure Check_Hidden_Child_Unit
4631 (N : Node_Id;
4632 Gen_Unit : Entity_Id;
4633 Act_Decl_Id : Entity_Id)
4635 Gen_Id : constant Node_Id := Name (N);
4637 begin
4638 if Is_Child_Unit (Gen_Unit)
4639 and then Is_Child_Unit (Act_Decl_Id)
4640 and then Nkind (Gen_Id) = N_Expanded_Name
4641 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
4642 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
4643 then
4644 Error_Msg_Node_2 := Scope (Act_Decl_Id);
4645 Error_Msg_NE
4646 ("generic unit & is implicitly declared in &",
4647 Defining_Unit_Name (N), Gen_Unit);
4648 Error_Msg_N ("\instance must have different name",
4649 Defining_Unit_Name (N));
4650 end if;
4651 end Check_Hidden_Child_Unit;
4653 ------------------------
4654 -- Check_Private_View --
4655 ------------------------
4657 procedure Check_Private_View (N : Node_Id) is
4658 T : constant Entity_Id := Etype (N);
4659 BT : Entity_Id;
4661 begin
4662 -- Exchange views if the type was not private in the generic but is
4663 -- private at the point of instantiation. Do not exchange views if
4664 -- the scope of the type is in scope. This can happen if both generic
4665 -- and instance are sibling units, or if type is defined in a parent.
4666 -- In this case the visibility of the type will be correct for all
4667 -- semantic checks.
4669 if Present (T) then
4670 BT := Base_Type (T);
4672 if Is_Private_Type (T)
4673 and then not Has_Private_View (N)
4674 and then Present (Full_View (T))
4675 and then not In_Open_Scopes (Scope (T))
4676 then
4677 -- In the generic, the full type was visible. Save the
4678 -- private entity, for subsequent exchange.
4680 Switch_View (T);
4682 elsif Has_Private_View (N)
4683 and then not Is_Private_Type (T)
4684 and then not Has_Been_Exchanged (T)
4685 and then Etype (Get_Associated_Node (N)) /= T
4686 then
4687 -- Only the private declaration was visible in the generic. If
4688 -- the type appears in a subtype declaration, the subtype in the
4689 -- instance must have a view compatible with that of its parent,
4690 -- which must be exchanged (see corresponding code in Restore_
4691 -- Private_Views). Otherwise, if the type is defined in a parent
4692 -- unit, leave full visibility within instance, which is safe.
4694 if In_Open_Scopes (Scope (Base_Type (T)))
4695 and then not Is_Private_Type (Base_Type (T))
4696 and then Comes_From_Source (Base_Type (T))
4697 then
4698 null;
4700 elsif Nkind (Parent (N)) = N_Subtype_Declaration
4701 or else not In_Private_Part (Scope (Base_Type (T)))
4702 then
4703 Prepend_Elmt (T, Exchanged_Views);
4704 Exchange_Declarations (Etype (Get_Associated_Node (N)));
4705 end if;
4707 -- For composite types with inconsistent representation
4708 -- exchange component types accordingly.
4710 elsif Is_Access_Type (T)
4711 and then Is_Private_Type (Designated_Type (T))
4712 and then not Has_Private_View (N)
4713 and then Present (Full_View (Designated_Type (T)))
4714 then
4715 Switch_View (Designated_Type (T));
4717 elsif Is_Array_Type (T)
4718 and then Is_Private_Type (Component_Type (T))
4719 and then not Has_Private_View (N)
4720 and then Present (Full_View (Component_Type (T)))
4721 then
4722 Switch_View (Component_Type (T));
4724 elsif Is_Private_Type (T)
4725 and then Present (Full_View (T))
4726 and then Is_Array_Type (Full_View (T))
4727 and then Is_Private_Type (Component_Type (Full_View (T)))
4728 then
4729 Switch_View (T);
4731 -- Finally, a non-private subtype may have a private base type,
4732 -- which must be exchanged for consistency. This can happen when
4733 -- instantiating a package body, when the scope stack is empty
4734 -- but in fact the subtype and the base type are declared in an
4735 -- enclosing scope.
4737 elsif not Is_Private_Type (T)
4738 and then not Has_Private_View (N)
4739 and then Is_Private_Type (Base_Type (T))
4740 and then Present (Full_View (BT))
4741 and then not Is_Generic_Type (BT)
4742 and then not In_Open_Scopes (BT)
4743 then
4744 Prepend_Elmt (Full_View (BT), Exchanged_Views);
4745 Exchange_Declarations (BT);
4746 end if;
4747 end if;
4748 end Check_Private_View;
4750 --------------------------
4751 -- Contains_Instance_Of --
4752 --------------------------
4754 function Contains_Instance_Of
4755 (Inner : Entity_Id;
4756 Outer : Entity_Id;
4757 N : Node_Id) return Boolean
4759 Elmt : Elmt_Id;
4760 Scop : Entity_Id;
4762 begin
4763 Scop := Outer;
4765 -- Verify that there are no circular instantiations. We check whether
4766 -- the unit contains an instance of the current scope or some enclosing
4767 -- scope (in case one of the instances appears in a subunit). Longer
4768 -- circularities involving subunits might seem too pathological to
4769 -- consider, but they were not too pathological for the authors of
4770 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
4771 -- enclosing generic scopes as containing an instance.
4773 loop
4774 -- Within a generic subprogram body, the scope is not generic, to
4775 -- allow for recursive subprograms. Use the declaration to determine
4776 -- whether this is a generic unit.
4778 if Ekind (Scop) = E_Generic_Package
4779 or else (Is_Subprogram (Scop)
4780 and then Nkind (Unit_Declaration_Node (Scop)) =
4781 N_Generic_Subprogram_Declaration)
4782 then
4783 Elmt := First_Elmt (Inner_Instances (Inner));
4785 while Present (Elmt) loop
4786 if Node (Elmt) = Scop then
4787 Error_Msg_Node_2 := Inner;
4788 Error_Msg_NE
4789 ("circular Instantiation: & instantiated within &!",
4790 N, Scop);
4791 return True;
4793 elsif Node (Elmt) = Inner then
4794 return True;
4796 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
4797 Error_Msg_Node_2 := Inner;
4798 Error_Msg_NE
4799 ("circular Instantiation: & instantiated within &!",
4800 N, Node (Elmt));
4801 return True;
4802 end if;
4804 Next_Elmt (Elmt);
4805 end loop;
4807 -- Indicate that Inner is being instantiated within Scop
4809 Append_Elmt (Inner, Inner_Instances (Scop));
4810 end if;
4812 if Scop = Standard_Standard then
4813 exit;
4814 else
4815 Scop := Scope (Scop);
4816 end if;
4817 end loop;
4819 return False;
4820 end Contains_Instance_Of;
4822 -----------------------
4823 -- Copy_Generic_Node --
4824 -----------------------
4826 function Copy_Generic_Node
4827 (N : Node_Id;
4828 Parent_Id : Node_Id;
4829 Instantiating : Boolean) return Node_Id
4831 Ent : Entity_Id;
4832 New_N : Node_Id;
4834 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
4835 -- Check the given value of one of the Fields referenced by the
4836 -- current node to determine whether to copy it recursively. The
4837 -- field may hold a Node_Id, a List_Id, or an Elist_Id, or a plain
4838 -- value (Sloc, Uint, Char) in which case it need not be copied.
4840 procedure Copy_Descendants;
4841 -- Common utility for various nodes
4843 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
4844 -- Make copy of element list
4846 function Copy_Generic_List
4847 (L : List_Id;
4848 Parent_Id : Node_Id) return List_Id;
4849 -- Apply Copy_Node recursively to the members of a node list
4851 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
4852 -- True if an identifier is part of the defining program unit name
4853 -- of a child unit. The entity of such an identifier must be kept
4854 -- (for ASIS use) even though as the name of an enclosing generic
4855 -- it would otherwise not be preserved in the generic tree.
4857 ----------------------
4858 -- Copy_Descendants --
4859 ----------------------
4861 procedure Copy_Descendants is
4863 use Atree.Unchecked_Access;
4864 -- This code section is part of the implementation of an untyped
4865 -- tree traversal, so it needs direct access to node fields.
4867 begin
4868 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
4869 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
4870 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
4871 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
4872 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
4873 end Copy_Descendants;
4875 -----------------------------
4876 -- Copy_Generic_Descendant --
4877 -----------------------------
4879 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
4880 begin
4881 if D = Union_Id (Empty) then
4882 return D;
4884 elsif D in Node_Range then
4885 return Union_Id
4886 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
4888 elsif D in List_Range then
4889 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
4891 elsif D in Elist_Range then
4892 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
4894 -- Nothing else is copyable (e.g. Uint values), return as is
4896 else
4897 return D;
4898 end if;
4899 end Copy_Generic_Descendant;
4901 ------------------------
4902 -- Copy_Generic_Elist --
4903 ------------------------
4905 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
4906 M : Elmt_Id;
4907 L : Elist_Id;
4909 begin
4910 if Present (E) then
4911 L := New_Elmt_List;
4912 M := First_Elmt (E);
4913 while Present (M) loop
4914 Append_Elmt
4915 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
4916 Next_Elmt (M);
4917 end loop;
4919 return L;
4921 else
4922 return No_Elist;
4923 end if;
4924 end Copy_Generic_Elist;
4926 -----------------------
4927 -- Copy_Generic_List --
4928 -----------------------
4930 function Copy_Generic_List
4931 (L : List_Id;
4932 Parent_Id : Node_Id) return List_Id
4934 N : Node_Id;
4935 New_L : List_Id;
4937 begin
4938 if Present (L) then
4939 New_L := New_List;
4940 Set_Parent (New_L, Parent_Id);
4942 N := First (L);
4943 while Present (N) loop
4944 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
4945 Next (N);
4946 end loop;
4948 return New_L;
4950 else
4951 return No_List;
4952 end if;
4953 end Copy_Generic_List;
4955 ---------------------------
4956 -- In_Defining_Unit_Name --
4957 ---------------------------
4959 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
4960 begin
4961 return Present (Parent (Nam))
4962 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
4963 or else
4964 (Nkind (Parent (Nam)) = N_Expanded_Name
4965 and then In_Defining_Unit_Name (Parent (Nam))));
4966 end In_Defining_Unit_Name;
4968 -- Start of processing for Copy_Generic_Node
4970 begin
4971 if N = Empty then
4972 return N;
4973 end if;
4975 New_N := New_Copy (N);
4977 if Instantiating then
4978 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
4979 end if;
4981 if not Is_List_Member (N) then
4982 Set_Parent (New_N, Parent_Id);
4983 end if;
4985 -- If defining identifier, then all fields have been copied already
4987 if Nkind (New_N) in N_Entity then
4988 null;
4990 -- Special casing for identifiers and other entity names and operators
4992 elsif Nkind (New_N) = N_Identifier
4993 or else Nkind (New_N) = N_Character_Literal
4994 or else Nkind (New_N) = N_Expanded_Name
4995 or else Nkind (New_N) = N_Operator_Symbol
4996 or else Nkind (New_N) in N_Op
4997 then
4998 if not Instantiating then
5000 -- Link both nodes in order to assign subsequently the
5001 -- entity of the copy to the original node, in case this
5002 -- is a global reference.
5004 Set_Associated_Node (N, New_N);
5006 -- If we are within an instantiation, this is a nested generic
5007 -- that has already been analyzed at the point of definition. We
5008 -- must preserve references that were global to the enclosing
5009 -- parent at that point. Other occurrences, whether global or
5010 -- local to the current generic, must be resolved anew, so we
5011 -- reset the entity in the generic copy. A global reference has
5012 -- a smaller depth than the parent, or else the same depth in
5013 -- case both are distinct compilation units.
5015 -- It is also possible for Current_Instantiated_Parent to be
5016 -- defined, and for this not to be a nested generic, namely
5017 -- if the unit is loaded through Rtsfind. In that case, the
5018 -- entity of New_N is only a link to the associated node, and
5019 -- not a defining occurrence.
5021 -- The entities for parent units in the defining_program_unit
5022 -- of a generic child unit are established when the context of
5023 -- the unit is first analyzed, before the generic copy is made.
5024 -- They are preserved in the copy for use in ASIS queries.
5026 Ent := Entity (New_N);
5028 if No (Current_Instantiated_Parent.Gen_Id) then
5029 if No (Ent)
5030 or else Nkind (Ent) /= N_Defining_Identifier
5031 or else not In_Defining_Unit_Name (N)
5032 then
5033 Set_Associated_Node (New_N, Empty);
5034 end if;
5036 elsif No (Ent)
5037 or else
5038 not (Nkind (Ent) = N_Defining_Identifier
5039 or else
5040 Nkind (Ent) = N_Defining_Character_Literal
5041 or else
5042 Nkind (Ent) = N_Defining_Operator_Symbol)
5043 or else No (Scope (Ent))
5044 or else Scope (Ent) = Current_Instantiated_Parent.Gen_Id
5045 or else (Scope_Depth (Scope (Ent)) >
5046 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
5047 and then
5048 Get_Source_Unit (Ent) =
5049 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
5050 then
5051 Set_Associated_Node (New_N, Empty);
5052 end if;
5054 -- Case of instantiating identifier or some other name or operator
5056 else
5057 -- If the associated node is still defined, the entity in
5058 -- it is global, and must be copied to the instance.
5059 -- If this copy is being made for a body to inline, it is
5060 -- applied to an instantiated tree, and the entity is already
5061 -- present and must be also preserved.
5063 declare
5064 Assoc : constant Node_Id := Get_Associated_Node (N);
5065 begin
5066 if Present (Assoc) then
5067 if Nkind (Assoc) = Nkind (N) then
5068 Set_Entity (New_N, Entity (Assoc));
5069 Check_Private_View (N);
5071 elsif Nkind (Assoc) = N_Function_Call then
5072 Set_Entity (New_N, Entity (Name (Assoc)));
5074 elsif (Nkind (Assoc) = N_Defining_Identifier
5075 or else Nkind (Assoc) = N_Defining_Character_Literal
5076 or else Nkind (Assoc) = N_Defining_Operator_Symbol)
5077 and then Expander_Active
5078 then
5079 -- Inlining case: we are copying a tree that contains
5080 -- global entities, which are preserved in the copy
5081 -- to be used for subsequent inlining.
5083 null;
5085 else
5086 Set_Entity (New_N, Empty);
5087 end if;
5088 end if;
5089 end;
5090 end if;
5092 -- For expanded name, we must copy the Prefix and Selector_Name
5094 if Nkind (N) = N_Expanded_Name then
5095 Set_Prefix
5096 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
5098 Set_Selector_Name (New_N,
5099 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
5101 -- For operators, we must copy the right operand
5103 elsif Nkind (N) in N_Op then
5104 Set_Right_Opnd (New_N,
5105 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
5107 -- And for binary operators, the left operand as well
5109 if Nkind (N) in N_Binary_Op then
5110 Set_Left_Opnd (New_N,
5111 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
5112 end if;
5113 end if;
5115 -- Special casing for stubs
5117 elsif Nkind (N) in N_Body_Stub then
5119 -- In any case, we must copy the specification or defining
5120 -- identifier as appropriate.
5122 if Nkind (N) = N_Subprogram_Body_Stub then
5123 Set_Specification (New_N,
5124 Copy_Generic_Node (Specification (N), New_N, Instantiating));
5126 else
5127 Set_Defining_Identifier (New_N,
5128 Copy_Generic_Node
5129 (Defining_Identifier (N), New_N, Instantiating));
5130 end if;
5132 -- If we are not instantiating, then this is where we load and
5133 -- analyze subunits, i.e. at the point where the stub occurs. A
5134 -- more permissivle system might defer this analysis to the point
5135 -- of instantiation, but this seems to complicated for now.
5137 if not Instantiating then
5138 declare
5139 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
5140 Subunit : Node_Id;
5141 Unum : Unit_Number_Type;
5142 New_Body : Node_Id;
5144 begin
5145 Unum :=
5146 Load_Unit
5147 (Load_Name => Subunit_Name,
5148 Required => False,
5149 Subunit => True,
5150 Error_Node => N);
5152 -- If the proper body is not found, a warning message will
5153 -- be emitted when analyzing the stub, or later at the the
5154 -- point of instantiation. Here we just leave the stub as is.
5156 if Unum = No_Unit then
5157 Subunits_Missing := True;
5158 goto Subunit_Not_Found;
5159 end if;
5161 Subunit := Cunit (Unum);
5163 if Nkind (Unit (Subunit)) /= N_Subunit then
5164 Error_Msg_Sloc := Sloc (N);
5165 Error_Msg_N
5166 ("expected SEPARATE subunit to complete stub at#,"
5167 & " found child unit", Subunit);
5168 goto Subunit_Not_Found;
5169 end if;
5171 -- We must create a generic copy of the subunit, in order
5172 -- to perform semantic analysis on it, and we must replace
5173 -- the stub in the original generic unit with the subunit,
5174 -- in order to preserve non-local references within.
5176 -- Only the proper body needs to be copied. Library_Unit and
5177 -- context clause are simply inherited by the generic copy.
5178 -- Note that the copy (which may be recursive if there are
5179 -- nested subunits) must be done first, before attaching it
5180 -- to the enclosing generic.
5182 New_Body :=
5183 Copy_Generic_Node
5184 (Proper_Body (Unit (Subunit)),
5185 Empty, Instantiating => False);
5187 -- Now place the original proper body in the original
5188 -- generic unit. This is a body, not a compilation unit.
5190 Rewrite (N, Proper_Body (Unit (Subunit)));
5191 Set_Is_Compilation_Unit (Defining_Entity (N), False);
5192 Set_Was_Originally_Stub (N);
5194 -- Finally replace the body of the subunit with its copy,
5195 -- and make this new subunit into the library unit of the
5196 -- generic copy, which does not have stubs any longer.
5198 Set_Proper_Body (Unit (Subunit), New_Body);
5199 Set_Library_Unit (New_N, Subunit);
5200 Inherit_Context (Unit (Subunit), N);
5201 end;
5203 -- If we are instantiating, this must be an error case, since
5204 -- otherwise we would have replaced the stub node by the proper
5205 -- body that corresponds. So just ignore it in the copy (i.e.
5206 -- we have copied it, and that is good enough).
5208 else
5209 null;
5210 end if;
5212 <<Subunit_Not_Found>> null;
5214 -- If the node is a compilation unit, it is the subunit of a stub,
5215 -- which has been loaded already (see code below). In this case,
5216 -- the library unit field of N points to the parent unit (which
5217 -- is a compilation unit) and need not (and cannot!) be copied.
5219 -- When the proper body of the stub is analyzed, thie library_unit
5220 -- link is used to establish the proper context (see sem_ch10).
5222 -- The other fields of a compilation unit are copied as usual
5224 elsif Nkind (N) = N_Compilation_Unit then
5226 -- This code can only be executed when not instantiating, because
5227 -- in the copy made for an instantiation, the compilation unit
5228 -- node has disappeared at the point that a stub is replaced by
5229 -- its proper body.
5231 pragma Assert (not Instantiating);
5233 Set_Context_Items (New_N,
5234 Copy_Generic_List (Context_Items (N), New_N));
5236 Set_Unit (New_N,
5237 Copy_Generic_Node (Unit (N), New_N, False));
5239 Set_First_Inlined_Subprogram (New_N,
5240 Copy_Generic_Node
5241 (First_Inlined_Subprogram (N), New_N, False));
5243 Set_Aux_Decls_Node (New_N,
5244 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
5246 -- For an assignment node, the assignment is known to be semantically
5247 -- legal if we are instantiating the template. This avoids incorrect
5248 -- diagnostics in generated code.
5250 elsif Nkind (N) = N_Assignment_Statement then
5252 -- Copy name and expression fields in usual manner
5254 Set_Name (New_N,
5255 Copy_Generic_Node (Name (N), New_N, Instantiating));
5257 Set_Expression (New_N,
5258 Copy_Generic_Node (Expression (N), New_N, Instantiating));
5260 if Instantiating then
5261 Set_Assignment_OK (Name (New_N), True);
5262 end if;
5264 elsif Nkind (N) = N_Aggregate
5265 or else Nkind (N) = N_Extension_Aggregate
5266 then
5268 if not Instantiating then
5269 Set_Associated_Node (N, New_N);
5271 else
5272 if Present (Get_Associated_Node (N))
5273 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
5274 then
5275 -- In the generic the aggregate has some composite type. If at
5276 -- the point of instantiation the type has a private view,
5277 -- install the full view (and that of its ancestors, if any).
5279 declare
5280 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
5281 Rt : Entity_Id;
5283 begin
5284 if Present (T)
5285 and then Is_Private_Type (T)
5286 then
5287 Switch_View (T);
5288 end if;
5290 if Present (T)
5291 and then Is_Tagged_Type (T)
5292 and then Is_Derived_Type (T)
5293 then
5294 Rt := Root_Type (T);
5296 loop
5297 T := Etype (T);
5299 if Is_Private_Type (T) then
5300 Switch_View (T);
5301 end if;
5303 exit when T = Rt;
5304 end loop;
5305 end if;
5306 end;
5307 end if;
5308 end if;
5310 -- Do not copy the associated node, which points to
5311 -- the generic copy of the aggregate.
5313 declare
5314 use Atree.Unchecked_Access;
5315 -- This code section is part of the implementation of an untyped
5316 -- tree traversal, so it needs direct access to node fields.
5318 begin
5319 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
5320 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
5321 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
5322 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
5323 end;
5325 -- Allocators do not have an identifier denoting the access type,
5326 -- so we must locate it through the expression to check whether
5327 -- the views are consistent.
5329 elsif Nkind (N) = N_Allocator
5330 and then Nkind (Expression (N)) = N_Qualified_Expression
5331 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
5332 and then Instantiating
5333 then
5334 declare
5335 T : constant Node_Id :=
5336 Get_Associated_Node (Subtype_Mark (Expression (N)));
5337 Acc_T : Entity_Id;
5339 begin
5340 if Present (T) then
5341 -- Retrieve the allocator node in the generic copy
5343 Acc_T := Etype (Parent (Parent (T)));
5344 if Present (Acc_T)
5345 and then Is_Private_Type (Acc_T)
5346 then
5347 Switch_View (Acc_T);
5348 end if;
5349 end if;
5351 Copy_Descendants;
5352 end;
5354 -- For a proper body, we must catch the case of a proper body that
5355 -- replaces a stub. This represents the point at which a separate
5356 -- compilation unit, and hence template file, may be referenced, so
5357 -- we must make a new source instantiation entry for the template
5358 -- of the subunit, and ensure that all nodes in the subunit are
5359 -- adjusted using this new source instantiation entry.
5361 elsif Nkind (N) in N_Proper_Body then
5362 declare
5363 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
5365 begin
5366 if Instantiating and then Was_Originally_Stub (N) then
5367 Create_Instantiation_Source
5368 (Instantiation_Node,
5369 Defining_Entity (N),
5370 False,
5371 S_Adjustment);
5372 end if;
5374 -- Now copy the fields of the proper body, using the new
5375 -- adjustment factor if one was needed as per test above.
5377 Copy_Descendants;
5379 -- Restore the original adjustment factor in case changed
5381 S_Adjustment := Save_Adjustment;
5382 end;
5384 -- Don't copy Ident or Comment pragmas, since the comment belongs
5385 -- to the generic unit, not to the instantiating unit.
5387 elsif Nkind (N) = N_Pragma
5388 and then Instantiating
5389 then
5390 declare
5391 Prag_Id : constant Pragma_Id := Get_Pragma_Id (Chars (N));
5393 begin
5394 if Prag_Id = Pragma_Ident
5395 or else Prag_Id = Pragma_Comment
5396 then
5397 New_N := Make_Null_Statement (Sloc (N));
5399 else
5400 Copy_Descendants;
5401 end if;
5402 end;
5404 elsif Nkind (N) = N_Integer_Literal
5405 or else Nkind (N) = N_Real_Literal
5406 then
5407 -- No descendant fields need traversing
5409 null;
5411 -- For the remaining nodes, copy recursively their descendants
5413 else
5414 Copy_Descendants;
5416 if Instantiating
5417 and then Nkind (N) = N_Subprogram_Body
5418 then
5419 Set_Generic_Parent (Specification (New_N), N);
5420 end if;
5421 end if;
5423 return New_N;
5424 end Copy_Generic_Node;
5426 ----------------------------
5427 -- Denotes_Formal_Package --
5428 ----------------------------
5430 function Denotes_Formal_Package
5431 (Pack : Entity_Id;
5432 On_Exit : Boolean := False) return Boolean
5434 Par : Entity_Id;
5435 Scop : constant Entity_Id := Scope (Pack);
5436 E : Entity_Id;
5438 begin
5439 if On_Exit then
5440 Par :=
5441 Instance_Envs.Table
5442 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
5443 else
5444 Par := Current_Instantiated_Parent.Act_Id;
5445 end if;
5447 if Ekind (Scop) = E_Generic_Package
5448 or else Nkind (Unit_Declaration_Node (Scop)) =
5449 N_Generic_Subprogram_Declaration
5450 then
5451 return True;
5453 elsif Nkind (Parent (Pack)) = N_Formal_Package_Declaration then
5454 return True;
5456 elsif No (Par) then
5457 return False;
5459 else
5460 -- Check whether this package is associated with a formal
5461 -- package of the enclosing instantiation. Iterate over the
5462 -- list of renamings.
5464 E := First_Entity (Par);
5465 while Present (E) loop
5466 if Ekind (E) /= E_Package
5467 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
5468 then
5469 null;
5470 elsif Renamed_Object (E) = Par then
5471 return False;
5473 elsif Renamed_Object (E) = Pack then
5474 return True;
5475 end if;
5477 Next_Entity (E);
5478 end loop;
5480 return False;
5481 end if;
5482 end Denotes_Formal_Package;
5484 -----------------
5485 -- End_Generic --
5486 -----------------
5488 procedure End_Generic is
5489 begin
5490 -- ??? More things could be factored out in this
5491 -- routine. Should probably be done at a later stage.
5493 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
5494 Generic_Flags.Decrement_Last;
5496 Expander_Mode_Restore;
5497 end End_Generic;
5499 ----------------------
5500 -- Find_Actual_Type --
5501 ----------------------
5503 function Find_Actual_Type
5504 (Typ : Entity_Id;
5505 Gen_Scope : Entity_Id) return Entity_Id
5507 T : Entity_Id;
5509 begin
5510 if not Is_Child_Unit (Gen_Scope) then
5511 return Get_Instance_Of (Typ);
5513 elsif not Is_Generic_Type (Typ)
5514 or else Scope (Typ) = Gen_Scope
5515 then
5516 return Get_Instance_Of (Typ);
5518 else
5519 T := Current_Entity (Typ);
5520 while Present (T) loop
5521 if In_Open_Scopes (Scope (T)) then
5522 return T;
5523 end if;
5525 T := Homonym (T);
5526 end loop;
5528 return Typ;
5529 end if;
5530 end Find_Actual_Type;
5532 ----------------------------
5533 -- Freeze_Subprogram_Body --
5534 ----------------------------
5536 procedure Freeze_Subprogram_Body
5537 (Inst_Node : Node_Id;
5538 Gen_Body : Node_Id;
5539 Pack_Id : Entity_Id)
5541 F_Node : Node_Id;
5542 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
5543 Par : constant Entity_Id := Scope (Gen_Unit);
5544 Enc_G : Entity_Id;
5545 Enc_I : Node_Id;
5546 E_G_Id : Entity_Id;
5548 function Earlier (N1, N2 : Node_Id) return Boolean;
5549 -- Yields True if N1 and N2 appear in the same compilation unit,
5550 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
5551 -- traversal of the tree for the unit.
5553 function Enclosing_Body (N : Node_Id) return Node_Id;
5554 -- Find innermost package body that encloses the given node, and which
5555 -- is not a compilation unit. Freeze nodes for the instance, or for its
5556 -- enclosing body, may be inserted after the enclosing_body of the
5557 -- generic unit.
5559 function Package_Freeze_Node (B : Node_Id) return Node_Id;
5560 -- Find entity for given package body, and locate or create a freeze
5561 -- node for it.
5563 function True_Parent (N : Node_Id) return Node_Id;
5564 -- For a subunit, return parent of corresponding stub
5566 -------------
5567 -- Earlier --
5568 -------------
5570 function Earlier (N1, N2 : Node_Id) return Boolean is
5571 D1 : Integer := 0;
5572 D2 : Integer := 0;
5573 P1 : Node_Id := N1;
5574 P2 : Node_Id := N2;
5576 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
5577 -- Find distance from given node to enclosing compilation unit
5579 ----------------
5580 -- Find_Depth --
5581 ----------------
5583 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
5584 begin
5585 while Present (P)
5586 and then Nkind (P) /= N_Compilation_Unit
5587 loop
5588 P := True_Parent (P);
5589 D := D + 1;
5590 end loop;
5591 end Find_Depth;
5593 -- Start of procesing for Earlier
5595 begin
5596 Find_Depth (P1, D1);
5597 Find_Depth (P2, D2);
5599 if P1 /= P2 then
5600 return False;
5601 else
5602 P1 := N1;
5603 P2 := N2;
5604 end if;
5606 while D1 > D2 loop
5607 P1 := True_Parent (P1);
5608 D1 := D1 - 1;
5609 end loop;
5611 while D2 > D1 loop
5612 P2 := True_Parent (P2);
5613 D2 := D2 - 1;
5614 end loop;
5616 -- At this point P1 and P2 are at the same distance from the root.
5617 -- We examine their parents until we find a common declarative
5618 -- list, at which point we can establish their relative placement
5619 -- by comparing their ultimate slocs. If we reach the root,
5620 -- N1 and N2 do not descend from the same declarative list (e.g.
5621 -- one is nested in the declarative part and the other is in a block
5622 -- in the statement part) and the earlier one is already frozen.
5624 while not Is_List_Member (P1)
5625 or else not Is_List_Member (P2)
5626 or else List_Containing (P1) /= List_Containing (P2)
5627 loop
5628 P1 := True_Parent (P1);
5629 P2 := True_Parent (P2);
5631 if Nkind (Parent (P1)) = N_Subunit then
5632 P1 := Corresponding_Stub (Parent (P1));
5633 end if;
5635 if Nkind (Parent (P2)) = N_Subunit then
5636 P2 := Corresponding_Stub (Parent (P2));
5637 end if;
5639 if P1 = P2 then
5640 return False;
5641 end if;
5642 end loop;
5644 return
5645 Top_Level_Location (Sloc (P1)) < Top_Level_Location (Sloc (P2));
5646 end Earlier;
5648 --------------------
5649 -- Enclosing_Body --
5650 --------------------
5652 function Enclosing_Body (N : Node_Id) return Node_Id is
5653 P : Node_Id := Parent (N);
5655 begin
5656 while Present (P)
5657 and then Nkind (Parent (P)) /= N_Compilation_Unit
5658 loop
5659 if Nkind (P) = N_Package_Body then
5661 if Nkind (Parent (P)) = N_Subunit then
5662 return Corresponding_Stub (Parent (P));
5663 else
5664 return P;
5665 end if;
5666 end if;
5668 P := True_Parent (P);
5669 end loop;
5671 return Empty;
5672 end Enclosing_Body;
5674 -------------------------
5675 -- Package_Freeze_Node --
5676 -------------------------
5678 function Package_Freeze_Node (B : Node_Id) return Node_Id is
5679 Id : Entity_Id;
5681 begin
5682 if Nkind (B) = N_Package_Body then
5683 Id := Corresponding_Spec (B);
5685 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
5686 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
5687 end if;
5689 Ensure_Freeze_Node (Id);
5690 return Freeze_Node (Id);
5691 end Package_Freeze_Node;
5693 -----------------
5694 -- True_Parent --
5695 -----------------
5697 function True_Parent (N : Node_Id) return Node_Id is
5698 begin
5699 if Nkind (Parent (N)) = N_Subunit then
5700 return Parent (Corresponding_Stub (Parent (N)));
5701 else
5702 return Parent (N);
5703 end if;
5704 end True_Parent;
5706 -- Start of processing of Freeze_Subprogram_Body
5708 begin
5709 -- If the instance and the generic body appear within the same
5710 -- unit, and the instance preceeds the generic, the freeze node for
5711 -- the instance must appear after that of the generic. If the generic
5712 -- is nested within another instance I2, then current instance must
5713 -- be frozen after I2. In both cases, the freeze nodes are those of
5714 -- enclosing packages. Otherwise, the freeze node is placed at the end
5715 -- of the current declarative part.
5717 Enc_G := Enclosing_Body (Gen_Body);
5718 Enc_I := Enclosing_Body (Inst_Node);
5719 Ensure_Freeze_Node (Pack_Id);
5720 F_Node := Freeze_Node (Pack_Id);
5722 if Is_Generic_Instance (Par)
5723 and then Present (Freeze_Node (Par))
5724 and then
5725 In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
5726 then
5727 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
5729 -- The parent was a premature instantiation. Insert freeze
5730 -- node at the end the current declarative part.
5732 Insert_After_Last_Decl (Inst_Node, F_Node);
5734 else
5735 Insert_After (Freeze_Node (Par), F_Node);
5736 end if;
5738 -- The body enclosing the instance should be frozen after the body
5739 -- that includes the generic, because the body of the instance may
5740 -- make references to entities therein. If the two are not in the
5741 -- same declarative part, or if the one enclosing the instance is
5742 -- frozen already, freeze the instance at the end of the current
5743 -- declarative part.
5745 elsif Is_Generic_Instance (Par)
5746 and then Present (Freeze_Node (Par))
5747 and then Present (Enc_I)
5748 then
5749 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
5750 or else
5751 (Nkind (Enc_I) = N_Package_Body
5752 and then
5753 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
5754 then
5755 -- The enclosing package may contain several instances. Rather
5756 -- than computing the earliest point at which to insert its
5757 -- freeze node, we place it at the end of the declarative part
5758 -- of the parent of the generic.
5760 Insert_After_Last_Decl
5761 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
5762 end if;
5764 Insert_After_Last_Decl (Inst_Node, F_Node);
5766 elsif Present (Enc_G)
5767 and then Present (Enc_I)
5768 and then Enc_G /= Enc_I
5769 and then Earlier (Inst_Node, Gen_Body)
5770 then
5771 if Nkind (Enc_G) = N_Package_Body then
5772 E_G_Id := Corresponding_Spec (Enc_G);
5773 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
5774 E_G_Id :=
5775 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
5776 end if;
5778 -- Freeze package that encloses instance, and place node after
5779 -- package that encloses generic. If enclosing package is already
5780 -- frozen we have to assume it is at the proper place. This may
5781 -- be a potential ABE that requires dynamic checking.
5783 Insert_After_Last_Decl (Enc_G, Package_Freeze_Node (Enc_I));
5785 -- Freeze enclosing subunit before instance
5787 Ensure_Freeze_Node (E_G_Id);
5789 if not Is_List_Member (Freeze_Node (E_G_Id)) then
5790 Insert_After (Enc_G, Freeze_Node (E_G_Id));
5791 end if;
5793 Insert_After_Last_Decl (Inst_Node, F_Node);
5795 else
5796 -- If none of the above, insert freeze node at the end of the
5797 -- current declarative part.
5799 Insert_After_Last_Decl (Inst_Node, F_Node);
5800 end if;
5801 end Freeze_Subprogram_Body;
5803 ----------------
5804 -- Get_Gen_Id --
5805 ----------------
5807 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
5808 begin
5809 return Generic_Renamings.Table (E).Gen_Id;
5810 end Get_Gen_Id;
5812 ---------------------
5813 -- Get_Instance_Of --
5814 ---------------------
5816 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
5817 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
5819 begin
5820 if Res /= Assoc_Null then
5821 return Generic_Renamings.Table (Res).Act_Id;
5822 else
5823 -- On exit, entity is not instantiated: not a generic parameter,
5824 -- or else parameter of an inner generic unit.
5826 return A;
5827 end if;
5828 end Get_Instance_Of;
5830 ------------------------------------
5831 -- Get_Package_Instantiation_Node --
5832 ------------------------------------
5834 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
5835 Decl : Node_Id := Unit_Declaration_Node (A);
5836 Inst : Node_Id;
5838 begin
5839 -- If the Package_Instantiation attribute has been set on the package
5840 -- entity, then use it directly when it (or its Original_Node) refers
5841 -- to an N_Package_Instantiation node. In principle it should be
5842 -- possible to have this field set in all cases, which should be
5843 -- investigated, and would allow this function to be significantly
5844 -- simplified. ???
5846 if Present (Package_Instantiation (A)) then
5847 if Nkind (Package_Instantiation (A)) = N_Package_Instantiation then
5848 return Package_Instantiation (A);
5850 elsif Nkind (Original_Node (Package_Instantiation (A)))
5851 = N_Package_Instantiation
5852 then
5853 return Original_Node (Package_Instantiation (A));
5854 end if;
5855 end if;
5857 -- If the instantiation is a compilation unit that does not need a
5858 -- body then the instantiation node has been rewritten as a package
5859 -- declaration for the instance, and we return the original node.
5861 -- If it is a compilation unit and the instance node has not been
5862 -- rewritten, then it is still the unit of the compilation. Finally,
5863 -- if a body is present, this is a parent of the main unit whose body
5864 -- has been compiled for inlining purposes, and the instantiation node
5865 -- has been rewritten with the instance body.
5867 -- Otherwise the instantiation node appears after the declaration.
5868 -- If the entity is a formal package, the declaration may have been
5869 -- rewritten as a generic declaration (in the case of a formal with a
5870 -- box) or left as a formal package declaration if it has actuals, and
5871 -- is found with a forward search.
5873 if Nkind (Parent (Decl)) = N_Compilation_Unit then
5874 if Nkind (Decl) = N_Package_Declaration
5875 and then Present (Corresponding_Body (Decl))
5876 then
5877 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
5878 end if;
5880 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
5881 return Original_Node (Decl);
5882 else
5883 return Unit (Parent (Decl));
5884 end if;
5886 elsif Nkind (Decl) = N_Generic_Package_Declaration
5887 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
5888 then
5889 return Original_Node (Decl);
5891 else
5892 Inst := Next (Decl);
5893 while Nkind (Inst) /= N_Package_Instantiation
5894 and then Nkind (Inst) /= N_Formal_Package_Declaration
5895 loop
5896 Next (Inst);
5897 end loop;
5899 return Inst;
5900 end if;
5901 end Get_Package_Instantiation_Node;
5903 ------------------------
5904 -- Has_Been_Exchanged --
5905 ------------------------
5907 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
5908 Next : Elmt_Id := First_Elmt (Exchanged_Views);
5910 begin
5911 while Present (Next) loop
5912 if Full_View (Node (Next)) = E then
5913 return True;
5914 end if;
5916 Next_Elmt (Next);
5917 end loop;
5919 return False;
5920 end Has_Been_Exchanged;
5922 ----------
5923 -- Hash --
5924 ----------
5926 function Hash (F : Entity_Id) return HTable_Range is
5927 begin
5928 return HTable_Range (F mod HTable_Size);
5929 end Hash;
5931 ------------------------
5932 -- Hide_Current_Scope --
5933 ------------------------
5935 procedure Hide_Current_Scope is
5936 C : constant Entity_Id := Current_Scope;
5937 E : Entity_Id;
5939 begin
5940 Set_Is_Hidden_Open_Scope (C);
5941 E := First_Entity (C);
5943 while Present (E) loop
5944 if Is_Immediately_Visible (E) then
5945 Set_Is_Immediately_Visible (E, False);
5946 Append_Elmt (E, Hidden_Entities);
5947 end if;
5949 Next_Entity (E);
5950 end loop;
5952 -- Make the scope name invisible as well. This is necessary, but
5953 -- might conflict with calls to Rtsfind later on, in case the scope
5954 -- is a predefined one. There is no clean solution to this problem, so
5955 -- for now we depend on the user not redefining Standard itself in one
5956 -- of the parent units.
5958 if Is_Immediately_Visible (C)
5959 and then C /= Standard_Standard
5960 then
5961 Set_Is_Immediately_Visible (C, False);
5962 Append_Elmt (C, Hidden_Entities);
5963 end if;
5965 end Hide_Current_Scope;
5967 --------------
5968 -- Init_Env --
5969 --------------
5971 procedure Init_Env is
5972 Saved : Instance_Env;
5974 begin
5975 Saved.Ada_Version := Ada_Version;
5976 Saved.Ada_Version_Explicit := Ada_Version_Explicit;
5977 Saved.Instantiated_Parent := Current_Instantiated_Parent;
5978 Saved.Exchanged_Views := Exchanged_Views;
5979 Saved.Hidden_Entities := Hidden_Entities;
5980 Saved.Current_Sem_Unit := Current_Sem_Unit;
5981 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
5982 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
5983 Instance_Envs.Increment_Last;
5984 Instance_Envs.Table (Instance_Envs.Last) := Saved;
5986 Exchanged_Views := New_Elmt_List;
5987 Hidden_Entities := New_Elmt_List;
5989 -- Make dummy entry for Instantiated parent. If generic unit is
5990 -- legal, this is set properly in Set_Instance_Env.
5992 Current_Instantiated_Parent :=
5993 (Current_Scope, Current_Scope, Assoc_Null);
5994 end Init_Env;
5996 ------------------------------
5997 -- In_Same_Declarative_Part --
5998 ------------------------------
6000 function In_Same_Declarative_Part
6001 (F_Node : Node_Id;
6002 Inst : Node_Id) return Boolean
6004 Decls : constant Node_Id := Parent (F_Node);
6005 Nod : Node_Id := Parent (Inst);
6007 begin
6008 while Present (Nod) loop
6009 if Nod = Decls then
6010 return True;
6012 elsif Nkind (Nod) = N_Subprogram_Body
6013 or else Nkind (Nod) = N_Package_Body
6014 or else Nkind (Nod) = N_Task_Body
6015 or else Nkind (Nod) = N_Protected_Body
6016 or else Nkind (Nod) = N_Block_Statement
6017 then
6018 return False;
6020 elsif Nkind (Nod) = N_Subunit then
6021 Nod := Corresponding_Stub (Nod);
6023 elsif Nkind (Nod) = N_Compilation_Unit then
6024 return False;
6025 else
6026 Nod := Parent (Nod);
6027 end if;
6028 end loop;
6030 return False;
6031 end In_Same_Declarative_Part;
6033 ---------------------
6034 -- In_Main_Context --
6035 ---------------------
6037 function In_Main_Context (E : Entity_Id) return Boolean is
6038 Context : List_Id;
6039 Clause : Node_Id;
6040 Nam : Node_Id;
6042 begin
6043 if not Is_Compilation_Unit (E)
6044 or else Ekind (E) /= E_Package
6045 or else In_Private_Part (E)
6046 then
6047 return False;
6048 end if;
6050 Context := Context_Items (Cunit (Main_Unit));
6052 Clause := First (Context);
6053 while Present (Clause) loop
6054 if Nkind (Clause) = N_With_Clause then
6055 Nam := Name (Clause);
6057 -- If the current scope is part of the context of the main unit,
6058 -- analysis of the corresponding with_clause is not complete, and
6059 -- the entity is not set. We use the Chars field directly, which
6060 -- might produce false positives in rare cases, but guarantees
6061 -- that we produce all the instance bodies we will need.
6063 if (Nkind (Nam) = N_Identifier
6064 and then Chars (Nam) = Chars (E))
6065 or else (Nkind (Nam) = N_Selected_Component
6066 and then Chars (Selector_Name (Nam)) = Chars (E))
6067 then
6068 return True;
6069 end if;
6070 end if;
6072 Next (Clause);
6073 end loop;
6075 return False;
6076 end In_Main_Context;
6078 ---------------------
6079 -- Inherit_Context --
6080 ---------------------
6082 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
6083 Current_Context : List_Id;
6084 Current_Unit : Node_Id;
6085 Item : Node_Id;
6086 New_I : Node_Id;
6088 begin
6089 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
6091 -- The inherited context is attached to the enclosing compilation
6092 -- unit. This is either the main unit, or the declaration for the
6093 -- main unit (in case the instantation appears within the package
6094 -- declaration and the main unit is its body).
6096 Current_Unit := Parent (Inst);
6097 while Present (Current_Unit)
6098 and then Nkind (Current_Unit) /= N_Compilation_Unit
6099 loop
6100 Current_Unit := Parent (Current_Unit);
6101 end loop;
6103 Current_Context := Context_Items (Current_Unit);
6105 Item := First (Context_Items (Parent (Gen_Decl)));
6106 while Present (Item) loop
6107 if Nkind (Item) = N_With_Clause then
6108 New_I := New_Copy (Item);
6109 Set_Implicit_With (New_I, True);
6110 Append (New_I, Current_Context);
6111 end if;
6113 Next (Item);
6114 end loop;
6115 end if;
6116 end Inherit_Context;
6118 ----------------
6119 -- Initialize --
6120 ----------------
6122 procedure Initialize is
6123 begin
6124 Generic_Renamings.Init;
6125 Instance_Envs.Init;
6126 Generic_Flags.Init;
6127 Generic_Renamings_HTable.Reset;
6128 Circularity_Detected := False;
6129 Exchanged_Views := No_Elist;
6130 Hidden_Entities := No_Elist;
6131 end Initialize;
6133 ----------------------------
6134 -- Insert_After_Last_Decl --
6135 ----------------------------
6137 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id) is
6138 L : List_Id := List_Containing (N);
6139 P : constant Node_Id := Parent (L);
6141 begin
6142 if not Is_List_Member (F_Node) then
6143 if Nkind (P) = N_Package_Specification
6144 and then L = Visible_Declarations (P)
6145 and then Present (Private_Declarations (P))
6146 and then not Is_Empty_List (Private_Declarations (P))
6147 then
6148 L := Private_Declarations (P);
6149 end if;
6151 Insert_After (Last (L), F_Node);
6152 end if;
6153 end Insert_After_Last_Decl;
6155 ------------------
6156 -- Install_Body --
6157 ------------------
6159 procedure Install_Body
6160 (Act_Body : Node_Id;
6161 N : Node_Id;
6162 Gen_Body : Node_Id;
6163 Gen_Decl : Node_Id)
6165 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
6166 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
6167 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
6168 Par : constant Entity_Id := Scope (Gen_Id);
6169 Gen_Unit : constant Node_Id :=
6170 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
6171 Orig_Body : Node_Id := Gen_Body;
6172 F_Node : Node_Id;
6173 Body_Unit : Node_Id;
6175 Must_Delay : Boolean;
6177 function Enclosing_Subp (Id : Entity_Id) return Entity_Id;
6178 -- Find subprogram (if any) that encloses instance and/or generic body
6180 function True_Sloc (N : Node_Id) return Source_Ptr;
6181 -- If the instance is nested inside a generic unit, the Sloc of the
6182 -- instance indicates the place of the original definition, not the
6183 -- point of the current enclosing instance. Pending a better usage of
6184 -- Slocs to indicate instantiation places, we determine the place of
6185 -- origin of a node by finding the maximum sloc of any ancestor node.
6186 -- Why is this not equivalent to Top_Level_Location ???
6188 --------------------
6189 -- Enclosing_Subp --
6190 --------------------
6192 function Enclosing_Subp (Id : Entity_Id) return Entity_Id is
6193 Scop : Entity_Id := Scope (Id);
6195 begin
6196 while Scop /= Standard_Standard
6197 and then not Is_Overloadable (Scop)
6198 loop
6199 Scop := Scope (Scop);
6200 end loop;
6202 return Scop;
6203 end Enclosing_Subp;
6205 ---------------
6206 -- True_Sloc --
6207 ---------------
6209 function True_Sloc (N : Node_Id) return Source_Ptr is
6210 Res : Source_Ptr;
6211 N1 : Node_Id;
6213 begin
6214 Res := Sloc (N);
6215 N1 := N;
6216 while Present (N1) and then N1 /= Act_Unit loop
6217 if Sloc (N1) > Res then
6218 Res := Sloc (N1);
6219 end if;
6221 N1 := Parent (N1);
6222 end loop;
6224 return Res;
6225 end True_Sloc;
6227 -- Start of processing for Install_Body
6229 begin
6230 -- If the body is a subunit, the freeze point is the corresponding
6231 -- stub in the current compilation, not the subunit itself.
6233 if Nkind (Parent (Gen_Body)) = N_Subunit then
6234 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
6235 else
6236 Orig_Body := Gen_Body;
6237 end if;
6239 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
6241 -- If the instantiation and the generic definition appear in the
6242 -- same package declaration, this is an early instantiation.
6243 -- If they appear in the same declarative part, it is an early
6244 -- instantiation only if the generic body appears textually later,
6245 -- and the generic body is also in the main unit.
6247 -- If instance is nested within a subprogram, and the generic body is
6248 -- not, the instance is delayed because the enclosing body is. If
6249 -- instance and body are within the same scope, or the same sub-
6250 -- program body, indicate explicitly that the instance is delayed.
6252 Must_Delay :=
6253 (Gen_Unit = Act_Unit
6254 and then ((Nkind (Gen_Unit) = N_Package_Declaration)
6255 or else Nkind (Gen_Unit) = N_Generic_Package_Declaration
6256 or else (Gen_Unit = Body_Unit
6257 and then True_Sloc (N) < Sloc (Orig_Body)))
6258 and then Is_In_Main_Unit (Gen_Unit)
6259 and then (Scope (Act_Id) = Scope (Gen_Id)
6260 or else
6261 Enclosing_Subp (Act_Id) = Enclosing_Subp (Gen_Id)));
6263 -- If this is an early instantiation, the freeze node is placed after
6264 -- the generic body. Otherwise, if the generic appears in an instance,
6265 -- we cannot freeze the current instance until the outer one is frozen.
6266 -- This is only relevant if the current instance is nested within some
6267 -- inner scope not itself within the outer instance. If this scope is
6268 -- a package body in the same declarative part as the outer instance,
6269 -- then that body needs to be frozen after the outer instance. Finally,
6270 -- if no delay is needed, we place the freeze node at the end of the
6271 -- current declarative part.
6273 if Expander_Active then
6274 Ensure_Freeze_Node (Act_Id);
6275 F_Node := Freeze_Node (Act_Id);
6277 if Must_Delay then
6278 Insert_After (Orig_Body, F_Node);
6280 elsif Is_Generic_Instance (Par)
6281 and then Present (Freeze_Node (Par))
6282 and then Scope (Act_Id) /= Par
6283 then
6284 -- Freeze instance of inner generic after instance of enclosing
6285 -- generic.
6287 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
6288 Insert_After (Freeze_Node (Par), F_Node);
6290 -- Freeze package enclosing instance of inner generic after
6291 -- instance of enclosing generic.
6293 elsif Nkind (Parent (N)) = N_Package_Body
6294 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
6295 then
6297 declare
6298 Enclosing : constant Entity_Id :=
6299 Corresponding_Spec (Parent (N));
6301 begin
6302 Insert_After_Last_Decl (N, F_Node);
6303 Ensure_Freeze_Node (Enclosing);
6305 if not Is_List_Member (Freeze_Node (Enclosing)) then
6306 Insert_After (Freeze_Node (Par), Freeze_Node (Enclosing));
6307 end if;
6308 end;
6310 else
6311 Insert_After_Last_Decl (N, F_Node);
6312 end if;
6314 else
6315 Insert_After_Last_Decl (N, F_Node);
6316 end if;
6317 end if;
6319 Set_Is_Frozen (Act_Id);
6320 Insert_Before (N, Act_Body);
6321 Mark_Rewrite_Insertion (Act_Body);
6322 end Install_Body;
6324 --------------------
6325 -- Install_Parent --
6326 --------------------
6328 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
6329 Ancestors : constant Elist_Id := New_Elmt_List;
6330 S : constant Entity_Id := Current_Scope;
6331 Inst_Par : Entity_Id;
6332 First_Par : Entity_Id;
6333 Inst_Node : Node_Id;
6334 Gen_Par : Entity_Id;
6335 First_Gen : Entity_Id;
6336 Elmt : Elmt_Id;
6338 procedure Install_Formal_Packages (Par : Entity_Id);
6339 -- If any of the formals of the parent are formal packages with box,
6340 -- their formal parts are visible in the parent and thus in the child
6341 -- unit as well. Analogous to what is done in Check_Generic_Actuals
6342 -- for the unit itself.
6344 procedure Install_Noninstance_Specs (Par : Entity_Id);
6345 -- Install the scopes of noninstance parent units ending with Par
6347 procedure Install_Spec (Par : Entity_Id);
6348 -- The child unit is within the declarative part of the parent, so
6349 -- the declarations within the parent are immediately visible.
6351 -----------------------------
6352 -- Install_Formal_Packages --
6353 -----------------------------
6355 procedure Install_Formal_Packages (Par : Entity_Id) is
6356 E : Entity_Id;
6358 begin
6359 E := First_Entity (Par);
6360 while Present (E) loop
6361 if Ekind (E) = E_Package
6362 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
6363 then
6364 -- If this is the renaming for the parent instance, done
6366 if Renamed_Object (E) = Par then
6367 exit;
6369 -- The visibility of a formal of an enclosing generic is
6370 -- already correct.
6372 elsif Denotes_Formal_Package (E) then
6373 null;
6375 elsif Present (Associated_Formal_Package (E))
6376 and then Box_Present (Parent (Associated_Formal_Package (E)))
6377 then
6378 Check_Generic_Actuals (Renamed_Object (E), True);
6379 Set_Is_Hidden (E, False);
6380 end if;
6381 end if;
6383 Next_Entity (E);
6384 end loop;
6385 end Install_Formal_Packages;
6387 -------------------------------
6388 -- Install_Noninstance_Specs --
6389 -------------------------------
6391 procedure Install_Noninstance_Specs (Par : Entity_Id) is
6392 begin
6393 if Present (Par)
6394 and then Par /= Standard_Standard
6395 and then not In_Open_Scopes (Par)
6396 then
6397 Install_Noninstance_Specs (Scope (Par));
6398 Install_Spec (Par);
6399 end if;
6400 end Install_Noninstance_Specs;
6402 ------------------
6403 -- Install_Spec --
6404 ------------------
6406 procedure Install_Spec (Par : Entity_Id) is
6407 Spec : constant Node_Id :=
6408 Specification (Unit_Declaration_Node (Par));
6410 begin
6411 -- If this parent of the child instance is a top-level unit,
6412 -- then record the unit and its visibility for later resetting
6413 -- in Remove_Parent. We exclude units that are generic instances,
6414 -- as we only want to record this information for the ultimate
6415 -- top-level noninstance parent (is that always correct???).
6417 if Scope (Par) = Standard_Standard
6418 and then not Is_Generic_Instance (Par)
6419 then
6420 Parent_Unit_Visible := Is_Immediately_Visible (Par);
6421 Instance_Parent_Unit := Par;
6422 end if;
6424 -- Open the parent scope and make it and its declarations visible.
6425 -- If this point is not within a body, then only the visible
6426 -- declarations should be made visible, and installation of the
6427 -- private declarations is deferred until the appropriate point
6428 -- within analysis of the spec being instantiated (see the handling
6429 -- of parent visibility in Analyze_Package_Specification). This is
6430 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
6431 -- private view problems that occur when compiling instantiations of
6432 -- a generic child of that package (Generic_Dispatching_Constructor).
6433 -- If the instance freezes a tagged type, inlinings of operations
6434 -- from Ada.Tags may need the full view of type Tag. If inlining
6435 -- took proper account of establishing visibility of inlined
6436 -- subprograms' parents then it should be possible to remove this
6437 -- special check. ???
6439 New_Scope (Par);
6440 Set_Is_Immediately_Visible (Par);
6441 Install_Visible_Declarations (Par);
6442 Set_Use (Visible_Declarations (Spec));
6444 if In_Body or else Is_RTU (Par, Ada_Tags) then
6445 Install_Private_Declarations (Par);
6446 Set_Use (Private_Declarations (Spec));
6447 end if;
6448 end Install_Spec;
6450 -- Start of processing for Install_Parent
6452 begin
6453 -- We need to install the parent instance to compile the instantiation
6454 -- of the child, but the child instance must appear in the current
6455 -- scope. Given that we cannot place the parent above the current
6456 -- scope in the scope stack, we duplicate the current scope and unstack
6457 -- both after the instantiation is complete.
6459 -- If the parent is itself the instantiation of a child unit, we must
6460 -- also stack the instantiation of its parent, and so on. Each such
6461 -- ancestor is the prefix of the name in a prior instantiation.
6463 -- If this is a nested instance, the parent unit itself resolves to
6464 -- a renaming of the parent instance, whose declaration we need.
6466 -- Finally, the parent may be a generic (not an instance) when the
6467 -- child unit appears as a formal package.
6469 Inst_Par := P;
6471 if Present (Renamed_Entity (Inst_Par)) then
6472 Inst_Par := Renamed_Entity (Inst_Par);
6473 end if;
6475 First_Par := Inst_Par;
6477 Gen_Par :=
6478 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
6480 First_Gen := Gen_Par;
6482 while Present (Gen_Par)
6483 and then Is_Child_Unit (Gen_Par)
6484 loop
6485 -- Load grandparent instance as well
6487 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
6489 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
6490 Inst_Par := Entity (Prefix (Name (Inst_Node)));
6492 if Present (Renamed_Entity (Inst_Par)) then
6493 Inst_Par := Renamed_Entity (Inst_Par);
6494 end if;
6496 Gen_Par :=
6497 Generic_Parent
6498 (Specification (Unit_Declaration_Node (Inst_Par)));
6500 if Present (Gen_Par) then
6501 Prepend_Elmt (Inst_Par, Ancestors);
6503 else
6504 -- Parent is not the name of an instantiation
6506 Install_Noninstance_Specs (Inst_Par);
6508 exit;
6509 end if;
6511 else
6512 -- Previous error
6514 exit;
6515 end if;
6516 end loop;
6518 if Present (First_Gen) then
6519 Append_Elmt (First_Par, Ancestors);
6521 else
6522 Install_Noninstance_Specs (First_Par);
6523 end if;
6525 if not Is_Empty_Elmt_List (Ancestors) then
6526 Elmt := First_Elmt (Ancestors);
6528 while Present (Elmt) loop
6529 Install_Spec (Node (Elmt));
6530 Install_Formal_Packages (Node (Elmt));
6532 Next_Elmt (Elmt);
6533 end loop;
6534 end if;
6536 if not In_Body then
6537 New_Scope (S);
6538 end if;
6539 end Install_Parent;
6541 --------------------------------
6542 -- Instantiate_Formal_Package --
6543 --------------------------------
6545 function Instantiate_Formal_Package
6546 (Formal : Node_Id;
6547 Actual : Node_Id;
6548 Analyzed_Formal : Node_Id) return List_Id
6550 Loc : constant Source_Ptr := Sloc (Actual);
6551 Actual_Pack : Entity_Id;
6552 Formal_Pack : Entity_Id;
6553 Gen_Parent : Entity_Id;
6554 Decls : List_Id;
6555 Nod : Node_Id;
6556 Parent_Spec : Node_Id;
6558 procedure Find_Matching_Actual
6559 (F : Node_Id;
6560 Act : in out Entity_Id);
6561 -- We need to associate each formal entity in the formal package
6562 -- with the corresponding entity in the actual package. The actual
6563 -- package has been analyzed and possibly expanded, and as a result
6564 -- there is no one-to-one correspondence between the two lists (for
6565 -- example, the actual may include subtypes, itypes, and inherited
6566 -- primitive operations, interspersed among the renaming declarations
6567 -- for the actuals) . We retrieve the corresponding actual by name
6568 -- because each actual has the same name as the formal, and they do
6569 -- appear in the same order.
6571 function Formal_Entity
6572 (F : Node_Id;
6573 Act_Ent : Entity_Id) return Entity_Id;
6574 -- Returns the entity associated with the given formal F. In the
6575 -- case where F is a formal package, this function will iterate
6576 -- through all of F's formals and enter map associations from the
6577 -- actuals occurring in the formal package's corresponding actual
6578 -- package (obtained via Act_Ent) to the formal package's formal
6579 -- parameters. This function is called recursively for arbitrary
6580 -- levels of formal packages.
6582 function Is_Instance_Of
6583 (Act_Spec : Entity_Id;
6584 Gen_Anc : Entity_Id) return Boolean;
6585 -- The actual can be an instantiation of a generic within another
6586 -- instance, in which case there is no direct link from it to the
6587 -- original generic ancestor. In that case, we recognize that the
6588 -- ultimate ancestor is the same by examining names and scopes.
6590 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id);
6591 -- Within the generic part, entities in the formal package are
6592 -- visible. To validate subsequent type declarations, indicate
6593 -- the correspondence betwen the entities in the analyzed formal,
6594 -- and the entities in the actual package. There are three packages
6595 -- involved in the instantiation of a formal package: the parent
6596 -- generic P1 which appears in the generic declaration, the fake
6597 -- instantiation P2 which appears in the analyzed generic, and whose
6598 -- visible entities may be used in subsequent formals, and the actual
6599 -- P3 in the instance. To validate subsequent formals, me indicate
6600 -- that the entities in P2 are mapped into those of P3. The mapping of
6601 -- entities has to be done recursively for nested packages.
6603 procedure Process_Nested_Formal (Formal : Entity_Id);
6604 -- If the current formal is declared with a box, its own formals are
6605 -- visible in the instance, as they were in the generic, and their
6606 -- Hidden flag must be reset. If some of these formals are themselves
6607 -- packages declared with a box, the processing must be recursive.
6609 --------------------------
6610 -- Find_Matching_Actual --
6611 --------------------------
6613 procedure Find_Matching_Actual
6614 (F : Node_Id;
6615 Act : in out Entity_Id)
6617 Formal_Ent : Entity_Id;
6619 begin
6620 case Nkind (Original_Node (F)) is
6621 when N_Formal_Object_Declaration |
6622 N_Formal_Type_Declaration =>
6623 Formal_Ent := Defining_Identifier (F);
6625 while Chars (Act) /= Chars (Formal_Ent) loop
6626 Next_Entity (Act);
6627 end loop;
6629 when N_Formal_Subprogram_Declaration |
6630 N_Formal_Package_Declaration |
6631 N_Package_Declaration |
6632 N_Generic_Package_Declaration =>
6633 Formal_Ent := Defining_Entity (F);
6635 while Chars (Act) /= Chars (Formal_Ent) loop
6636 Next_Entity (Act);
6637 end loop;
6639 when others =>
6640 raise Program_Error;
6641 end case;
6642 end Find_Matching_Actual;
6644 -------------------
6645 -- Formal_Entity --
6646 -------------------
6648 function Formal_Entity
6649 (F : Node_Id;
6650 Act_Ent : Entity_Id) return Entity_Id
6652 Orig_Node : Node_Id := F;
6653 Act_Pkg : Entity_Id;
6655 begin
6656 case Nkind (Original_Node (F)) is
6657 when N_Formal_Object_Declaration =>
6658 return Defining_Identifier (F);
6660 when N_Formal_Type_Declaration =>
6661 return Defining_Identifier (F);
6663 when N_Formal_Subprogram_Declaration =>
6664 return Defining_Unit_Name (Specification (F));
6666 when N_Package_Declaration =>
6667 return Defining_Unit_Name (Specification (F));
6669 when N_Formal_Package_Declaration |
6670 N_Generic_Package_Declaration =>
6672 if Nkind (F) = N_Generic_Package_Declaration then
6673 Orig_Node := Original_Node (F);
6674 end if;
6676 Act_Pkg := Act_Ent;
6678 -- Find matching actual package, skipping over itypes and
6679 -- other entities generated when analyzing the formal. We
6680 -- know that if the instantiation is legal then there is
6681 -- a matching package for the formal.
6683 while Ekind (Act_Pkg) /= E_Package loop
6684 Act_Pkg := Next_Entity (Act_Pkg);
6685 end loop;
6687 declare
6688 Actual_Ent : Entity_Id := First_Entity (Act_Pkg);
6689 Formal_Node : Node_Id;
6690 Formal_Ent : Entity_Id;
6692 Gen_Decl : Node_Id;
6693 Formals : List_Id;
6695 begin
6696 -- The actual may be a renamed generic package, in which
6697 -- case we want to retrieve the original generic in order
6698 -- to traverse its formal part.
6700 if Present (Renamed_Entity (Entity (Name (Orig_Node)))) then
6701 Gen_Decl :=
6702 Unit_Declaration_Node (
6703 Renamed_Entity (Entity (Name (Orig_Node))));
6704 else
6705 Gen_Decl :=
6706 Unit_Declaration_Node (Entity (Name (Orig_Node)));
6707 end if;
6709 Formals := Generic_Formal_Declarations (Gen_Decl);
6711 if Present (Formals) then
6712 Formal_Node := First_Non_Pragma (Formals);
6713 else
6714 Formal_Node := Empty;
6715 end if;
6717 while Present (Actual_Ent)
6718 and then Present (Formal_Node)
6719 and then Actual_Ent /= First_Private_Entity (Act_Pkg)
6720 loop
6721 -- ??? Are the following calls also needed here:
6723 -- Set_Is_Hidden (Actual_Ent, False);
6724 -- Set_Is_Potentially_Use_Visible
6725 -- (Actual_Ent, In_Use (Act_Ent));
6727 Formal_Ent := Formal_Entity (Formal_Node, Actual_Ent);
6728 if Present (Formal_Ent) then
6729 Set_Instance_Of (Formal_Ent, Actual_Ent);
6730 end if;
6731 Next_Non_Pragma (Formal_Node);
6733 Next_Entity (Actual_Ent);
6734 end loop;
6735 end;
6737 return Defining_Identifier (Orig_Node);
6739 when N_Use_Package_Clause =>
6740 return Empty;
6742 when N_Use_Type_Clause =>
6743 return Empty;
6745 -- We return Empty for all other encountered forms of
6746 -- declarations because there are some cases of nonformal
6747 -- sorts of declaration that can show up (e.g., when array
6748 -- formals are present). Since it's not clear what kinds
6749 -- can appear among the formals, we won't raise failure here.
6751 when others =>
6752 return Empty;
6754 end case;
6755 end Formal_Entity;
6757 --------------------
6758 -- Is_Instance_Of --
6759 --------------------
6761 function Is_Instance_Of
6762 (Act_Spec : Entity_Id;
6763 Gen_Anc : Entity_Id) return Boolean
6765 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
6767 begin
6768 if No (Gen_Par) then
6769 return False;
6771 -- Simplest case: the generic parent of the actual is the formal
6773 elsif Gen_Par = Gen_Anc then
6774 return True;
6776 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
6777 return False;
6779 -- The actual may be obtained through several instantiations. Its
6780 -- scope must itself be an instance of a generic declared in the
6781 -- same scope as the formal. Any other case is detected above.
6783 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
6784 return False;
6786 else
6787 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
6788 end if;
6789 end Is_Instance_Of;
6791 ------------------
6792 -- Map_Entities --
6793 ------------------
6795 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id) is
6796 E1 : Entity_Id;
6797 E2 : Entity_Id;
6799 begin
6800 Set_Instance_Of (Form, Act);
6802 -- Traverse formal and actual package to map the corresponding
6803 -- entities. We skip over internal entities that may be generated
6804 -- during semantic analysis, and find the matching entities by
6805 -- name, given that they must appear in the same order.
6807 E1 := First_Entity (Form);
6808 E2 := First_Entity (Act);
6809 while Present (E1)
6810 and then E1 /= First_Private_Entity (Form)
6811 loop
6812 -- Could this test be a single condition???
6813 -- Seems like it could, and isn't FPE (Form) a constant anyway???
6815 if not Is_Internal (E1)
6816 and then Present (Parent (E1))
6817 and then not Is_Class_Wide_Type (E1)
6818 and then not Is_Internal_Name (Chars (E1))
6819 then
6820 while Present (E2)
6821 and then Chars (E2) /= Chars (E1)
6822 loop
6823 Next_Entity (E2);
6824 end loop;
6826 if No (E2) then
6827 exit;
6828 else
6829 Set_Instance_Of (E1, E2);
6831 if Is_Type (E1)
6832 and then Is_Tagged_Type (E2)
6833 then
6834 Set_Instance_Of
6835 (Class_Wide_Type (E1), Class_Wide_Type (E2));
6836 end if;
6838 if Ekind (E1) = E_Package
6839 and then No (Renamed_Object (E1))
6840 then
6841 Map_Entities (E1, E2);
6842 end if;
6843 end if;
6844 end if;
6846 Next_Entity (E1);
6847 end loop;
6848 end Map_Entities;
6850 ---------------------------
6851 -- Process_Nested_Formal --
6852 ---------------------------
6854 procedure Process_Nested_Formal (Formal : Entity_Id) is
6855 Ent : Entity_Id;
6857 begin
6858 if Present (Associated_Formal_Package (Formal))
6859 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
6860 then
6861 Ent := First_Entity (Formal);
6862 while Present (Ent) loop
6863 Set_Is_Hidden (Ent, False);
6864 Set_Is_Potentially_Use_Visible
6865 (Ent, Is_Potentially_Use_Visible (Formal));
6867 if Ekind (Ent) = E_Package then
6868 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
6869 Process_Nested_Formal (Ent);
6870 end if;
6872 Next_Entity (Ent);
6873 end loop;
6874 end if;
6875 end Process_Nested_Formal;
6877 -- Start of processing for Instantiate_Formal_Package
6879 begin
6880 Analyze (Actual);
6882 if not Is_Entity_Name (Actual)
6883 or else Ekind (Entity (Actual)) /= E_Package
6884 then
6885 Error_Msg_N
6886 ("expect package instance to instantiate formal", Actual);
6887 Abandon_Instantiation (Actual);
6888 raise Program_Error;
6890 else
6891 Actual_Pack := Entity (Actual);
6892 Set_Is_Instantiated (Actual_Pack);
6894 -- The actual may be a renamed package, or an outer generic
6895 -- formal package whose instantiation is converted into a renaming.
6897 if Present (Renamed_Object (Actual_Pack)) then
6898 Actual_Pack := Renamed_Object (Actual_Pack);
6899 end if;
6901 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
6902 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
6903 Formal_Pack := Defining_Identifier (Analyzed_Formal);
6904 else
6905 Gen_Parent :=
6906 Generic_Parent (Specification (Analyzed_Formal));
6907 Formal_Pack :=
6908 Defining_Unit_Name (Specification (Analyzed_Formal));
6909 end if;
6911 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
6912 Parent_Spec := Specification (Unit_Declaration_Node (Actual_Pack));
6913 else
6914 Parent_Spec := Parent (Actual_Pack);
6915 end if;
6917 if Gen_Parent = Any_Id then
6918 Error_Msg_N
6919 ("previous error in declaration of formal package", Actual);
6920 Abandon_Instantiation (Actual);
6922 elsif
6923 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
6924 then
6925 null;
6927 else
6928 Error_Msg_NE
6929 ("actual parameter must be instance of&", Actual, Gen_Parent);
6930 Abandon_Instantiation (Actual);
6931 end if;
6933 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
6934 Map_Entities (Formal_Pack, Actual_Pack);
6936 Nod :=
6937 Make_Package_Renaming_Declaration (Loc,
6938 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
6939 Name => New_Reference_To (Actual_Pack, Loc));
6941 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
6942 Defining_Identifier (Formal));
6943 Decls := New_List (Nod);
6945 -- If the formal F has a box, then the generic declarations are
6946 -- visible in the generic G. In an instance of G, the corresponding
6947 -- entities in the actual for F (which are the actuals for the
6948 -- instantiation of the generic that F denotes) must also be made
6949 -- visible for analysis of the current instance. On exit from the
6950 -- current instance, those entities are made private again. If the
6951 -- actual is currently in use, these entities are also use-visible.
6953 -- The loop through the actual entities also steps through the
6954 -- formal entities and enters associations from formals to
6955 -- actuals into the renaming map. This is necessary to properly
6956 -- handle checking of actual parameter associations for later
6957 -- formals that depend on actuals declared in the formal package.
6959 if Box_Present (Formal) then
6960 declare
6961 Gen_Decl : constant Node_Id :=
6962 Unit_Declaration_Node (Gen_Parent);
6963 Formals : constant List_Id :=
6964 Generic_Formal_Declarations (Gen_Decl);
6965 Actual_Ent : Entity_Id;
6966 Formal_Node : Node_Id;
6967 Formal_Ent : Entity_Id;
6969 begin
6970 if Present (Formals) then
6971 Formal_Node := First_Non_Pragma (Formals);
6972 else
6973 Formal_Node := Empty;
6974 end if;
6976 Actual_Ent := First_Entity (Actual_Pack);
6978 while Present (Actual_Ent)
6979 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
6980 loop
6981 Set_Is_Hidden (Actual_Ent, False);
6982 Set_Is_Potentially_Use_Visible
6983 (Actual_Ent, In_Use (Actual_Pack));
6985 if Ekind (Actual_Ent) = E_Package then
6986 Process_Nested_Formal (Actual_Ent);
6987 end if;
6989 if Present (Formal_Node) then
6990 Formal_Ent := Formal_Entity (Formal_Node, Actual_Ent);
6992 if Present (Formal_Ent) then
6993 Find_Matching_Actual (Formal_Node, Actual_Ent);
6994 Set_Instance_Of (Formal_Ent, Actual_Ent);
6995 end if;
6997 Next_Non_Pragma (Formal_Node);
6999 else
7000 -- No further formals to match, but the generic
7001 -- part may contain inherited operation that are
7002 -- not hidden in the enclosing instance.
7004 Next_Entity (Actual_Ent);
7005 end if;
7007 end loop;
7008 end;
7010 -- If the formal is not declared with a box, reanalyze it as
7011 -- an instantiation, to verify the matching rules of 12.7. The
7012 -- actual checks are performed after the generic associations
7013 -- been analyzed.
7015 else
7016 declare
7017 I_Pack : constant Entity_Id :=
7018 Make_Defining_Identifier (Sloc (Actual),
7019 Chars => New_Internal_Name ('P'));
7021 begin
7022 Set_Is_Internal (I_Pack);
7024 Append_To (Decls,
7025 Make_Package_Instantiation (Sloc (Actual),
7026 Defining_Unit_Name => I_Pack,
7027 Name => New_Occurrence_Of (Gen_Parent, Sloc (Actual)),
7028 Generic_Associations =>
7029 Generic_Associations (Formal)));
7030 end;
7031 end if;
7033 return Decls;
7034 end if;
7035 end Instantiate_Formal_Package;
7037 -----------------------------------
7038 -- Instantiate_Formal_Subprogram --
7039 -----------------------------------
7041 function Instantiate_Formal_Subprogram
7042 (Formal : Node_Id;
7043 Actual : Node_Id;
7044 Analyzed_Formal : Node_Id) return Node_Id
7046 Loc : Source_Ptr := Sloc (Instantiation_Node);
7047 Formal_Sub : constant Entity_Id :=
7048 Defining_Unit_Name (Specification (Formal));
7049 Analyzed_S : constant Entity_Id :=
7050 Defining_Unit_Name (Specification (Analyzed_Formal));
7051 Decl_Node : Node_Id;
7052 Nam : Node_Id;
7053 New_Spec : Node_Id;
7055 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
7056 -- If the generic is a child unit, the parent has been installed on the
7057 -- scope stack, but a default subprogram cannot resolve to something on
7058 -- the parent because that parent is not really part of the visible
7059 -- context (it is there to resolve explicit local entities). If the
7060 -- default has resolved in this way, we remove the entity from
7061 -- immediate visibility and analyze the node again to emit an error
7062 -- message or find another visible candidate.
7064 procedure Valid_Actual_Subprogram (Act : Node_Id);
7065 -- Perform legality check and raise exception on failure
7067 -----------------------
7068 -- From_Parent_Scope --
7069 -----------------------
7071 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
7072 Gen_Scope : Node_Id := Scope (Analyzed_S);
7074 begin
7075 while Present (Gen_Scope)
7076 and then Is_Child_Unit (Gen_Scope)
7077 loop
7078 if Scope (Subp) = Scope (Gen_Scope) then
7079 return True;
7080 end if;
7082 Gen_Scope := Scope (Gen_Scope);
7083 end loop;
7085 return False;
7086 end From_Parent_Scope;
7088 -----------------------------
7089 -- Valid_Actual_Subprogram --
7090 -----------------------------
7092 procedure Valid_Actual_Subprogram (Act : Node_Id) is
7093 Act_E : Entity_Id := Empty;
7095 begin
7096 if Is_Entity_Name (Act) then
7097 Act_E := Entity (Act);
7098 elsif Nkind (Act) = N_Selected_Component
7099 and then Is_Entity_Name (Selector_Name (Act))
7100 then
7101 Act_E := Entity (Selector_Name (Act));
7102 end if;
7104 if (Present (Act_E) and then Is_Overloadable (Act_E))
7105 or else Nkind (Act) = N_Attribute_Reference
7106 or else Nkind (Act) = N_Indexed_Component
7107 or else Nkind (Act) = N_Character_Literal
7108 or else Nkind (Act) = N_Explicit_Dereference
7109 then
7110 return;
7111 end if;
7113 Error_Msg_NE
7114 ("expect subprogram or entry name in instantiation of&",
7115 Instantiation_Node, Formal_Sub);
7116 Abandon_Instantiation (Instantiation_Node);
7118 end Valid_Actual_Subprogram;
7120 -- Start of processing for Instantiate_Formal_Subprogram
7122 begin
7123 New_Spec := New_Copy_Tree (Specification (Formal));
7125 -- Create new entity for the actual (New_Copy_Tree does not)
7127 Set_Defining_Unit_Name
7128 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
7130 -- Find entity of actual. If the actual is an attribute reference, it
7131 -- cannot be resolved here (its formal is missing) but is handled
7132 -- instead in Attribute_Renaming. If the actual is overloaded, it is
7133 -- fully resolved subsequently, when the renaming declaration for the
7134 -- formal is analyzed. If it is an explicit dereference, resolve the
7135 -- prefix but not the actual itself, to prevent interpretation as a
7136 -- call.
7138 if Present (Actual) then
7139 Loc := Sloc (Actual);
7140 Set_Sloc (New_Spec, Loc);
7142 if Nkind (Actual) = N_Operator_Symbol then
7143 Find_Direct_Name (Actual);
7145 elsif Nkind (Actual) = N_Explicit_Dereference then
7146 Analyze (Prefix (Actual));
7148 elsif Nkind (Actual) /= N_Attribute_Reference then
7149 Analyze (Actual);
7150 end if;
7152 Valid_Actual_Subprogram (Actual);
7153 Nam := Actual;
7155 elsif Present (Default_Name (Formal)) then
7156 if Nkind (Default_Name (Formal)) /= N_Attribute_Reference
7157 and then Nkind (Default_Name (Formal)) /= N_Selected_Component
7158 and then Nkind (Default_Name (Formal)) /= N_Indexed_Component
7159 and then Nkind (Default_Name (Formal)) /= N_Character_Literal
7160 and then Present (Entity (Default_Name (Formal)))
7161 then
7162 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
7163 else
7164 Nam := New_Copy (Default_Name (Formal));
7165 Set_Sloc (Nam, Loc);
7166 end if;
7168 elsif Box_Present (Formal) then
7170 -- Actual is resolved at the point of instantiation. Create
7171 -- an identifier or operator with the same name as the formal.
7173 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
7174 Nam := Make_Operator_Symbol (Loc,
7175 Chars => Chars (Formal_Sub),
7176 Strval => No_String);
7177 else
7178 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
7179 end if;
7181 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
7182 and then Null_Present (Specification (Formal))
7183 then
7184 -- Generate null body for procedure, for use in the instance
7186 Decl_Node :=
7187 Make_Subprogram_Body (Loc,
7188 Specification => New_Spec,
7189 Declarations => New_List,
7190 Handled_Statement_Sequence =>
7191 Make_Handled_Sequence_Of_Statements (Loc,
7192 Statements => New_List (Make_Null_Statement (Loc))));
7194 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
7195 return Decl_Node;
7197 else
7198 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
7199 Error_Msg_NE
7200 ("missing actual&", Instantiation_Node, Formal_Sub);
7201 Error_Msg_NE
7202 ("\in instantiation of & declared#",
7203 Instantiation_Node, Scope (Analyzed_S));
7204 Abandon_Instantiation (Instantiation_Node);
7205 end if;
7207 Decl_Node :=
7208 Make_Subprogram_Renaming_Declaration (Loc,
7209 Specification => New_Spec,
7210 Name => Nam);
7212 -- If we do not have an actual and the formal specified <> then
7213 -- set to get proper default.
7215 if No (Actual) and then Box_Present (Formal) then
7216 Set_From_Default (Decl_Node);
7217 end if;
7219 -- Gather possible interpretations for the actual before analyzing the
7220 -- instance. If overloaded, it will be resolved when analyzing the
7221 -- renaming declaration.
7223 if Box_Present (Formal)
7224 and then No (Actual)
7225 then
7226 Analyze (Nam);
7228 if Is_Child_Unit (Scope (Analyzed_S))
7229 and then Present (Entity (Nam))
7230 then
7231 if not Is_Overloaded (Nam) then
7233 if From_Parent_Scope (Entity (Nam)) then
7234 Set_Is_Immediately_Visible (Entity (Nam), False);
7235 Set_Entity (Nam, Empty);
7236 Set_Etype (Nam, Empty);
7238 Analyze (Nam);
7240 Set_Is_Immediately_Visible (Entity (Nam));
7241 end if;
7243 else
7244 declare
7245 I : Interp_Index;
7246 It : Interp;
7248 begin
7249 Get_First_Interp (Nam, I, It);
7251 while Present (It.Nam) loop
7252 if From_Parent_Scope (It.Nam) then
7253 Remove_Interp (I);
7254 end if;
7256 Get_Next_Interp (I, It);
7257 end loop;
7258 end;
7259 end if;
7260 end if;
7261 end if;
7263 -- The generic instantiation freezes the actual. This can only be
7264 -- done once the actual is resolved, in the analysis of the renaming
7265 -- declaration. To make the formal subprogram entity available, we set
7266 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
7267 -- This is also needed in Analyze_Subprogram_Renaming for the processing
7268 -- of formal abstract subprograms.
7270 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
7272 -- We cannot analyze the renaming declaration, and thus find the
7273 -- actual, until the all the actuals are assembled in the instance.
7274 -- For subsequent checks of other actuals, indicate the node that
7275 -- will hold the instance of this formal.
7277 Set_Instance_Of (Analyzed_S, Nam);
7279 if Nkind (Actual) = N_Selected_Component
7280 and then Is_Task_Type (Etype (Prefix (Actual)))
7281 and then not Is_Frozen (Etype (Prefix (Actual)))
7282 then
7283 -- The renaming declaration will create a body, which must appear
7284 -- outside of the instantiation, We move the renaming declaration
7285 -- out of the instance, and create an additional renaming inside,
7286 -- to prevent freezing anomalies.
7288 declare
7289 Anon_Id : constant Entity_Id :=
7290 Make_Defining_Identifier
7291 (Loc, New_Internal_Name ('E'));
7292 begin
7293 Set_Defining_Unit_Name (New_Spec, Anon_Id);
7294 Insert_Before (Instantiation_Node, Decl_Node);
7295 Analyze (Decl_Node);
7297 -- Now create renaming within the instance
7299 Decl_Node :=
7300 Make_Subprogram_Renaming_Declaration (Loc,
7301 Specification => New_Copy_Tree (New_Spec),
7302 Name => New_Occurrence_Of (Anon_Id, Loc));
7304 Set_Defining_Unit_Name (Specification (Decl_Node),
7305 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
7306 end;
7307 end if;
7309 return Decl_Node;
7310 end Instantiate_Formal_Subprogram;
7312 ------------------------
7313 -- Instantiate_Object --
7314 ------------------------
7316 function Instantiate_Object
7317 (Formal : Node_Id;
7318 Actual : Node_Id;
7319 Analyzed_Formal : Node_Id) return List_Id
7321 Formal_Id : constant Entity_Id := Defining_Identifier (Formal);
7322 Type_Id : constant Node_Id := Subtype_Mark (Formal);
7323 Loc : constant Source_Ptr := Sloc (Actual);
7324 Act_Assoc : constant Node_Id := Parent (Actual);
7325 Orig_Ftyp : constant Entity_Id :=
7326 Etype (Defining_Identifier (Analyzed_Formal));
7327 List : constant List_Id := New_List;
7328 Ftyp : Entity_Id;
7329 Decl_Node : Node_Id;
7330 Subt_Decl : Node_Id := Empty;
7332 begin
7333 -- Sloc for error message on missing actual
7335 Error_Msg_Sloc := Sloc (Scope (Defining_Identifier (Analyzed_Formal)));
7337 if Get_Instance_Of (Formal_Id) /= Formal_Id then
7338 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
7339 end if;
7341 Set_Parent (List, Parent (Actual));
7343 -- OUT present
7345 if Out_Present (Formal) then
7347 -- An IN OUT generic actual must be a name. The instantiation is a
7348 -- renaming declaration. The actual is the name being renamed. We
7349 -- use the actual directly, rather than a copy, because it is not
7350 -- used further in the list of actuals, and because a copy or a use
7351 -- of relocate_node is incorrect if the instance is nested within a
7352 -- generic. In order to simplify ASIS searches, the Generic_Parent
7353 -- field links the declaration to the generic association.
7355 if No (Actual) then
7356 Error_Msg_NE
7357 ("missing actual&",
7358 Instantiation_Node, Formal_Id);
7359 Error_Msg_NE
7360 ("\in instantiation of & declared#",
7361 Instantiation_Node,
7362 Scope (Defining_Identifier (Analyzed_Formal)));
7363 Abandon_Instantiation (Instantiation_Node);
7364 end if;
7366 Decl_Node :=
7367 Make_Object_Renaming_Declaration (Loc,
7368 Defining_Identifier => New_Copy (Formal_Id),
7369 Subtype_Mark => New_Copy_Tree (Type_Id),
7370 Name => Actual);
7372 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
7374 -- The analysis of the actual may produce insert_action nodes, so
7375 -- the declaration must have a context in which to attach them.
7377 Append (Decl_Node, List);
7378 Analyze (Actual);
7380 -- Return if the analysis of the actual reported some error
7382 if Etype (Actual) = Any_Type then
7383 return List;
7384 end if;
7386 -- This check is performed here because Analyze_Object_Renaming
7387 -- will not check it when Comes_From_Source is False. Note
7388 -- though that the check for the actual being the name of an
7389 -- object will be performed in Analyze_Object_Renaming.
7391 if Is_Object_Reference (Actual)
7392 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
7393 then
7394 Error_Msg_N
7395 ("illegal discriminant-dependent component for in out parameter",
7396 Actual);
7397 end if;
7399 -- The actual has to be resolved in order to check that it is
7400 -- a variable (due to cases such as F(1), where F returns
7401 -- access to an array, and for overloaded prefixes).
7403 Ftyp :=
7404 Get_Instance_Of (Etype (Defining_Identifier (Analyzed_Formal)));
7406 if Is_Private_Type (Ftyp)
7407 and then not Is_Private_Type (Etype (Actual))
7408 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
7409 or else Base_Type (Etype (Actual)) = Ftyp)
7410 then
7411 -- If the actual has the type of the full view of the formal,
7412 -- or else a non-private subtype of the formal, then
7413 -- the visibility of the formal type has changed. Add to the
7414 -- actuals a subtype declaration that will force the exchange
7415 -- of views in the body of the instance as well.
7417 Subt_Decl :=
7418 Make_Subtype_Declaration (Loc,
7419 Defining_Identifier =>
7420 Make_Defining_Identifier (Loc, New_Internal_Name ('P')),
7421 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
7423 Prepend (Subt_Decl, List);
7425 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
7426 Exchange_Declarations (Ftyp);
7427 end if;
7429 Resolve (Actual, Ftyp);
7431 if not Is_Variable (Actual) or else Paren_Count (Actual) > 0 then
7432 Error_Msg_NE
7433 ("actual for& must be a variable", Actual, Formal_Id);
7435 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
7436 Error_Msg_NE (
7437 "type of actual does not match type of&", Actual, Formal_Id);
7439 end if;
7441 Note_Possible_Modification (Actual);
7443 -- Check for instantiation of atomic/volatile actual for
7444 -- non-atomic/volatile formal (RM C.6 (12)).
7446 if Is_Atomic_Object (Actual)
7447 and then not Is_Atomic (Orig_Ftyp)
7448 then
7449 Error_Msg_N
7450 ("cannot instantiate non-atomic formal object " &
7451 "with atomic actual", Actual);
7453 elsif Is_Volatile_Object (Actual)
7454 and then not Is_Volatile (Orig_Ftyp)
7455 then
7456 Error_Msg_N
7457 ("cannot instantiate non-volatile formal object " &
7458 "with volatile actual", Actual);
7459 end if;
7461 -- OUT not present
7463 else
7464 -- The instantiation of a generic formal in-parameter
7465 -- is a constant declaration. The actual is the expression for
7466 -- that declaration.
7468 if Present (Actual) then
7470 Decl_Node := Make_Object_Declaration (Loc,
7471 Defining_Identifier => New_Copy (Formal_Id),
7472 Constant_Present => True,
7473 Object_Definition => New_Copy_Tree (Type_Id),
7474 Expression => Actual);
7476 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
7478 -- A generic formal object of a tagged type is defined
7479 -- to be aliased so the new constant must also be treated
7480 -- as aliased.
7482 if Is_Tagged_Type
7483 (Etype (Defining_Identifier (Analyzed_Formal)))
7484 then
7485 Set_Aliased_Present (Decl_Node);
7486 end if;
7488 Append (Decl_Node, List);
7490 -- No need to repeat (pre-)analysis of some expression nodes
7491 -- already handled in Pre_Analyze_Actuals.
7493 if Nkind (Actual) /= N_Allocator then
7494 Analyze (Actual);
7496 -- Return if the analysis of the actual reported some error
7498 if Etype (Actual) = Any_Type then
7499 return List;
7500 end if;
7501 end if;
7503 declare
7504 Typ : constant Entity_Id :=
7505 Get_Instance_Of
7506 (Etype (Defining_Identifier (Analyzed_Formal)));
7508 begin
7509 Freeze_Before (Instantiation_Node, Typ);
7511 -- If the actual is an aggregate, perform name resolution on
7512 -- its components (the analysis of an aggregate does not do
7513 -- it) to capture local names that may be hidden if the
7514 -- generic is a child unit.
7516 if Nkind (Actual) = N_Aggregate then
7517 Pre_Analyze_And_Resolve (Actual, Typ);
7518 end if;
7519 end;
7521 elsif Present (Expression (Formal)) then
7523 -- Use default to construct declaration
7525 Decl_Node :=
7526 Make_Object_Declaration (Sloc (Formal),
7527 Defining_Identifier => New_Copy (Formal_Id),
7528 Constant_Present => True,
7529 Object_Definition => New_Copy (Type_Id),
7530 Expression => New_Copy_Tree (Expression (Formal)));
7532 Append (Decl_Node, List);
7533 Set_Analyzed (Expression (Decl_Node), False);
7535 else
7536 Error_Msg_NE
7537 ("missing actual&",
7538 Instantiation_Node, Formal_Id);
7539 Error_Msg_NE ("\in instantiation of & declared#",
7540 Instantiation_Node,
7541 Scope (Defining_Identifier (Analyzed_Formal)));
7543 if Is_Scalar_Type
7544 (Etype (Defining_Identifier (Analyzed_Formal)))
7545 then
7546 -- Create dummy constant declaration so that instance can
7547 -- be analyzed, to minimize cascaded visibility errors.
7549 Decl_Node :=
7550 Make_Object_Declaration (Loc,
7551 Defining_Identifier => New_Copy (Formal_Id),
7552 Constant_Present => True,
7553 Object_Definition => New_Copy (Type_Id),
7554 Expression =>
7555 Make_Attribute_Reference (Sloc (Formal_Id),
7556 Attribute_Name => Name_First,
7557 Prefix => New_Copy (Type_Id)));
7559 Append (Decl_Node, List);
7561 else
7562 Abandon_Instantiation (Instantiation_Node);
7563 end if;
7564 end if;
7566 end if;
7568 return List;
7569 end Instantiate_Object;
7571 ------------------------------
7572 -- Instantiate_Package_Body --
7573 ------------------------------
7575 procedure Instantiate_Package_Body
7576 (Body_Info : Pending_Body_Info;
7577 Inlined_Body : Boolean := False)
7579 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
7580 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
7581 Loc : constant Source_Ptr := Sloc (Inst_Node);
7583 Gen_Id : constant Node_Id := Name (Inst_Node);
7584 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7585 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
7586 Act_Spec : constant Node_Id := Specification (Act_Decl);
7587 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
7589 Act_Body_Name : Node_Id;
7590 Gen_Body : Node_Id;
7591 Gen_Body_Id : Node_Id;
7592 Act_Body : Node_Id;
7593 Act_Body_Id : Entity_Id;
7595 Parent_Installed : Boolean := False;
7596 Save_Style_Check : constant Boolean := Style_Check;
7598 begin
7599 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7601 -- The instance body may already have been processed, as the parent
7602 -- of another instance that is inlined. (Load_Parent_Of_Generic).
7604 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
7605 return;
7606 end if;
7608 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
7610 if No (Gen_Body_Id) then
7611 Load_Parent_Of_Generic (Inst_Node, Specification (Gen_Decl));
7612 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7613 end if;
7615 -- Establish global variable for sloc adjustment and for error
7616 -- recovery.
7618 Instantiation_Node := Inst_Node;
7620 if Present (Gen_Body_Id) then
7621 Save_Env (Gen_Unit, Act_Decl_Id);
7622 Style_Check := False;
7623 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
7625 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
7627 Create_Instantiation_Source
7628 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
7630 Act_Body :=
7631 Copy_Generic_Node
7632 (Original_Node (Gen_Body), Empty, Instantiating => True);
7634 -- Build new name (possibly qualified) for body declaration
7636 Act_Body_Id := New_Copy (Act_Decl_Id);
7638 -- Some attributes of the spec entity are not inherited by the
7639 -- body entity.
7641 Set_Handler_Records (Act_Body_Id, No_List);
7643 if Nkind (Defining_Unit_Name (Act_Spec)) =
7644 N_Defining_Program_Unit_Name
7645 then
7646 Act_Body_Name :=
7647 Make_Defining_Program_Unit_Name (Loc,
7648 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
7649 Defining_Identifier => Act_Body_Id);
7650 else
7651 Act_Body_Name := Act_Body_Id;
7652 end if;
7654 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
7656 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
7657 Check_Generic_Actuals (Act_Decl_Id, False);
7659 -- If it is a child unit, make the parent instance (which is an
7660 -- instance of the parent of the generic) visible. The parent
7661 -- instance is the prefix of the name of the generic unit.
7663 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
7664 and then Nkind (Gen_Id) = N_Expanded_Name
7665 then
7666 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
7667 Parent_Installed := True;
7669 elsif Is_Child_Unit (Gen_Unit) then
7670 Install_Parent (Scope (Gen_Unit), In_Body => True);
7671 Parent_Installed := True;
7672 end if;
7674 -- If the instantiation is a library unit, and this is the main
7675 -- unit, then build the resulting compilation unit nodes for the
7676 -- instance. If this is a compilation unit but it is not the main
7677 -- unit, then it is the body of a unit in the context, that is being
7678 -- compiled because it is encloses some inlined unit or another
7679 -- generic unit being instantiated. In that case, this body is not
7680 -- part of the current compilation, and is not attached to the tree,
7681 -- but its parent must be set for analysis.
7683 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
7685 -- Replace instance node with body of instance, and create
7686 -- new node for corresponding instance declaration.
7688 Build_Instance_Compilation_Unit_Nodes
7689 (Inst_Node, Act_Body, Act_Decl);
7690 Analyze (Inst_Node);
7692 if Parent (Inst_Node) = Cunit (Main_Unit) then
7694 -- If the instance is a child unit itself, then set the
7695 -- scope of the expanded body to be the parent of the
7696 -- instantiation (ensuring that the fully qualified name
7697 -- will be generated for the elaboration subprogram).
7699 if Nkind (Defining_Unit_Name (Act_Spec)) =
7700 N_Defining_Program_Unit_Name
7701 then
7702 Set_Scope
7703 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
7704 end if;
7705 end if;
7707 -- Case where instantiation is not a library unit
7709 else
7710 -- If this is an early instantiation, i.e. appears textually
7711 -- before the corresponding body and must be elaborated first,
7712 -- indicate that the body instance is to be delayed.
7714 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
7716 -- Now analyze the body. We turn off all checks if this is
7717 -- an internal unit, since there is no reason to have checks
7718 -- on for any predefined run-time library code. All such
7719 -- code is designed to be compiled with checks off.
7721 -- Note that we do NOT apply this criterion to children of
7722 -- GNAT (or on VMS, children of DEC). The latter units must
7723 -- suppress checks explicitly if this is needed.
7725 if Is_Predefined_File_Name
7726 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
7727 then
7728 Analyze (Act_Body, Suppress => All_Checks);
7729 else
7730 Analyze (Act_Body);
7731 end if;
7732 end if;
7734 if not Generic_Separately_Compiled (Gen_Unit) then
7735 Inherit_Context (Gen_Body, Inst_Node);
7736 end if;
7738 -- Remove the parent instances if they have been placed on the
7739 -- scope stack to compile the body.
7741 if Parent_Installed then
7742 Remove_Parent (In_Body => True);
7743 end if;
7745 Restore_Private_Views (Act_Decl_Id);
7747 -- Remove the current unit from visibility if this is an instance
7748 -- that is not elaborated on the fly for inlining purposes.
7750 if not Inlined_Body then
7751 Set_Is_Immediately_Visible (Act_Decl_Id, False);
7752 end if;
7754 Restore_Env;
7755 Style_Check := Save_Style_Check;
7757 -- If we have no body, and the unit requires a body, then complain.
7758 -- This complaint is suppressed if we have detected other errors
7759 -- (since a common reason for missing the body is that it had errors).
7761 elsif Unit_Requires_Body (Gen_Unit) then
7762 if Serious_Errors_Detected = 0 then
7763 Error_Msg_NE
7764 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
7766 -- Don't attempt to perform any cleanup actions if some other
7767 -- error was aready detected, since this can cause blowups.
7769 else
7770 return;
7771 end if;
7773 -- Case of package that does not need a body
7775 else
7776 -- If the instantiation of the declaration is a library unit,
7777 -- rewrite the original package instantiation as a package
7778 -- declaration in the compilation unit node.
7780 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
7781 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
7782 Rewrite (Inst_Node, Act_Decl);
7784 -- Generate elaboration entity, in case spec has elaboration
7785 -- code. This cannot be done when the instance is analyzed,
7786 -- because it is not known yet whether the body exists.
7788 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
7789 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
7791 -- If the instantiation is not a library unit, then append the
7792 -- declaration to the list of implicitly generated entities.
7793 -- unless it is already a list member which means that it was
7794 -- already processed
7796 elsif not Is_List_Member (Act_Decl) then
7797 Mark_Rewrite_Insertion (Act_Decl);
7798 Insert_Before (Inst_Node, Act_Decl);
7799 end if;
7800 end if;
7802 Expander_Mode_Restore;
7803 end Instantiate_Package_Body;
7805 ---------------------------------
7806 -- Instantiate_Subprogram_Body --
7807 ---------------------------------
7809 procedure Instantiate_Subprogram_Body
7810 (Body_Info : Pending_Body_Info)
7812 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
7813 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
7814 Loc : constant Source_Ptr := Sloc (Inst_Node);
7815 Gen_Id : constant Node_Id := Name (Inst_Node);
7816 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7817 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
7818 Anon_Id : constant Entity_Id :=
7819 Defining_Unit_Name (Specification (Act_Decl));
7820 Pack_Id : constant Entity_Id :=
7821 Defining_Unit_Name (Parent (Act_Decl));
7822 Decls : List_Id;
7823 Gen_Body : Node_Id;
7824 Gen_Body_Id : Node_Id;
7825 Act_Body : Node_Id;
7826 Act_Body_Id : Entity_Id;
7827 Pack_Body : Node_Id;
7828 Prev_Formal : Entity_Id;
7829 Ret_Expr : Node_Id;
7830 Unit_Renaming : Node_Id;
7832 Parent_Installed : Boolean := False;
7833 Save_Style_Check : constant Boolean := Style_Check;
7835 begin
7836 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7838 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
7840 if No (Gen_Body_Id) then
7841 Load_Parent_Of_Generic (Inst_Node, Specification (Gen_Decl));
7842 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7843 end if;
7845 Instantiation_Node := Inst_Node;
7847 if Present (Gen_Body_Id) then
7848 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
7850 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
7852 -- Either body is not present, or context is non-expanding, as
7853 -- when compiling a subunit. Mark the instance as completed, and
7854 -- diagnose a missing body when needed.
7856 if Expander_Active
7857 and then Operating_Mode = Generate_Code
7858 then
7859 Error_Msg_N
7860 ("missing proper body for instantiation", Gen_Body);
7861 end if;
7863 Set_Has_Completion (Anon_Id);
7864 return;
7865 end if;
7867 Save_Env (Gen_Unit, Anon_Id);
7868 Style_Check := False;
7869 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
7870 Create_Instantiation_Source
7871 (Inst_Node,
7872 Gen_Body_Id,
7873 False,
7874 S_Adjustment);
7876 Act_Body :=
7877 Copy_Generic_Node
7878 (Original_Node (Gen_Body), Empty, Instantiating => True);
7879 Act_Body_Id := Defining_Entity (Act_Body);
7880 Set_Chars (Act_Body_Id, Chars (Anon_Id));
7881 Set_Sloc (Act_Body_Id, Sloc (Defining_Entity (Inst_Node)));
7882 Set_Corresponding_Spec (Act_Body, Anon_Id);
7883 Set_Has_Completion (Anon_Id);
7884 Check_Generic_Actuals (Pack_Id, False);
7886 -- If it is a child unit, make the parent instance (which is an
7887 -- instance of the parent of the generic) visible. The parent
7888 -- instance is the prefix of the name of the generic unit.
7890 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
7891 and then Nkind (Gen_Id) = N_Expanded_Name
7892 then
7893 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
7894 Parent_Installed := True;
7896 elsif Is_Child_Unit (Gen_Unit) then
7897 Install_Parent (Scope (Gen_Unit), In_Body => True);
7898 Parent_Installed := True;
7899 end if;
7901 -- Inside its body, a reference to the generic unit is a reference
7902 -- to the instance. The corresponding renaming is the first
7903 -- declaration in the body.
7905 Unit_Renaming :=
7906 Make_Subprogram_Renaming_Declaration (Loc,
7907 Specification =>
7908 Copy_Generic_Node (
7909 Specification (Original_Node (Gen_Body)),
7910 Empty,
7911 Instantiating => True),
7912 Name => New_Occurrence_Of (Anon_Id, Loc));
7914 -- If there is a formal subprogram with the same name as the
7915 -- unit itself, do not add this renaming declaration. This is
7916 -- a temporary fix for one ACVC test. ???
7918 Prev_Formal := First_Entity (Pack_Id);
7919 while Present (Prev_Formal) loop
7920 if Chars (Prev_Formal) = Chars (Gen_Unit)
7921 and then Is_Overloadable (Prev_Formal)
7922 then
7923 exit;
7924 end if;
7926 Next_Entity (Prev_Formal);
7927 end loop;
7929 if Present (Prev_Formal) then
7930 Decls := New_List (Act_Body);
7931 else
7932 Decls := New_List (Unit_Renaming, Act_Body);
7933 end if;
7935 -- The subprogram body is placed in the body of a dummy package
7936 -- body, whose spec contains the subprogram declaration as well
7937 -- as the renaming declarations for the generic parameters.
7939 Pack_Body := Make_Package_Body (Loc,
7940 Defining_Unit_Name => New_Copy (Pack_Id),
7941 Declarations => Decls);
7943 Set_Corresponding_Spec (Pack_Body, Pack_Id);
7945 -- If the instantiation is a library unit, then build resulting
7946 -- compilation unit nodes for the instance. The declaration of
7947 -- the enclosing package is the grandparent of the subprogram
7948 -- declaration. First replace the instantiation node as the unit
7949 -- of the corresponding compilation.
7951 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
7952 if Parent (Inst_Node) = Cunit (Main_Unit) then
7953 Set_Unit (Parent (Inst_Node), Inst_Node);
7954 Build_Instance_Compilation_Unit_Nodes
7955 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
7956 Analyze (Inst_Node);
7957 else
7958 Set_Parent (Pack_Body, Parent (Inst_Node));
7959 Analyze (Pack_Body);
7960 end if;
7962 else
7963 Insert_Before (Inst_Node, Pack_Body);
7964 Mark_Rewrite_Insertion (Pack_Body);
7965 Analyze (Pack_Body);
7967 if Expander_Active then
7968 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
7969 end if;
7970 end if;
7972 if not Generic_Separately_Compiled (Gen_Unit) then
7973 Inherit_Context (Gen_Body, Inst_Node);
7974 end if;
7976 Restore_Private_Views (Pack_Id, False);
7978 if Parent_Installed then
7979 Remove_Parent (In_Body => True);
7980 end if;
7982 Restore_Env;
7983 Style_Check := Save_Style_Check;
7985 -- Body not found. Error was emitted already. If there were no
7986 -- previous errors, this may be an instance whose scope is a premature
7987 -- instance. In that case we must insure that the (legal) program does
7988 -- raise program error if executed. We generate a subprogram body for
7989 -- this purpose. See DEC ac30vso.
7991 elsif Serious_Errors_Detected = 0
7992 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
7993 then
7994 if Ekind (Anon_Id) = E_Procedure then
7995 Act_Body :=
7996 Make_Subprogram_Body (Loc,
7997 Specification =>
7998 Make_Procedure_Specification (Loc,
7999 Defining_Unit_Name =>
8000 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
8001 Parameter_Specifications =>
8002 New_Copy_List
8003 (Parameter_Specifications (Parent (Anon_Id)))),
8005 Declarations => Empty_List,
8006 Handled_Statement_Sequence =>
8007 Make_Handled_Sequence_Of_Statements (Loc,
8008 Statements =>
8009 New_List (
8010 Make_Raise_Program_Error (Loc,
8011 Reason =>
8012 PE_Access_Before_Elaboration))));
8014 else
8015 Ret_Expr :=
8016 Make_Raise_Program_Error (Loc,
8017 Reason => PE_Access_Before_Elaboration);
8019 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
8020 Set_Analyzed (Ret_Expr);
8022 Act_Body :=
8023 Make_Subprogram_Body (Loc,
8024 Specification =>
8025 Make_Function_Specification (Loc,
8026 Defining_Unit_Name =>
8027 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
8028 Parameter_Specifications =>
8029 New_Copy_List
8030 (Parameter_Specifications (Parent (Anon_Id))),
8031 Result_Definition =>
8032 New_Occurrence_Of (Etype (Anon_Id), Loc)),
8034 Declarations => Empty_List,
8035 Handled_Statement_Sequence =>
8036 Make_Handled_Sequence_Of_Statements (Loc,
8037 Statements =>
8038 New_List (Make_Return_Statement (Loc, Ret_Expr))));
8039 end if;
8041 Pack_Body := Make_Package_Body (Loc,
8042 Defining_Unit_Name => New_Copy (Pack_Id),
8043 Declarations => New_List (Act_Body));
8045 Insert_After (Inst_Node, Pack_Body);
8046 Set_Corresponding_Spec (Pack_Body, Pack_Id);
8047 Analyze (Pack_Body);
8048 end if;
8050 Expander_Mode_Restore;
8051 end Instantiate_Subprogram_Body;
8053 ----------------------
8054 -- Instantiate_Type --
8055 ----------------------
8057 function Instantiate_Type
8058 (Formal : Node_Id;
8059 Actual : Node_Id;
8060 Analyzed_Formal : Node_Id;
8061 Actual_Decls : List_Id) return Node_Id
8063 Loc : constant Source_Ptr := Sloc (Actual);
8064 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
8065 A_Gen_T : constant Entity_Id := Defining_Identifier (Analyzed_Formal);
8066 Ancestor : Entity_Id := Empty;
8067 Def : constant Node_Id := Formal_Type_Definition (Formal);
8068 Act_T : Entity_Id;
8069 Decl_Node : Node_Id;
8071 procedure Validate_Array_Type_Instance;
8072 procedure Validate_Access_Subprogram_Instance;
8073 procedure Validate_Access_Type_Instance;
8074 procedure Validate_Derived_Type_Instance;
8075 procedure Validate_Derived_Interface_Type_Instance;
8076 procedure Validate_Interface_Type_Instance;
8077 procedure Validate_Private_Type_Instance;
8078 -- These procedures perform validation tests for the named case
8080 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
8081 -- Check that base types are the same and that the subtypes match
8082 -- statically. Used in several of the above.
8084 --------------------
8085 -- Subtypes_Match --
8086 --------------------
8088 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
8089 T : constant Entity_Id := Get_Instance_Of (Gen_T);
8091 begin
8092 return (Base_Type (T) = Base_Type (Act_T)
8093 and then Subtypes_Statically_Match (T, Act_T))
8095 or else (Is_Class_Wide_Type (Gen_T)
8096 and then Is_Class_Wide_Type (Act_T)
8097 and then
8098 Subtypes_Match
8099 (Get_Instance_Of (Root_Type (Gen_T)),
8100 Root_Type (Act_T)))
8102 or else
8103 ((Ekind (Gen_T) = E_Anonymous_Access_Subprogram_Type
8104 or else Ekind (Gen_T) = E_Anonymous_Access_Type)
8105 and then Ekind (Act_T) = Ekind (Gen_T)
8106 and then
8107 Subtypes_Statically_Match
8108 (Designated_Type (Gen_T), Designated_Type (Act_T)));
8109 end Subtypes_Match;
8111 -----------------------------------------
8112 -- Validate_Access_Subprogram_Instance --
8113 -----------------------------------------
8115 procedure Validate_Access_Subprogram_Instance is
8116 begin
8117 if not Is_Access_Type (Act_T)
8118 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
8119 then
8120 Error_Msg_NE
8121 ("expect access type in instantiation of &", Actual, Gen_T);
8122 Abandon_Instantiation (Actual);
8123 end if;
8125 Check_Mode_Conformant
8126 (Designated_Type (Act_T),
8127 Designated_Type (A_Gen_T),
8128 Actual,
8129 Get_Inst => True);
8131 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
8132 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
8133 Error_Msg_NE
8134 ("protected access type not allowed for formal &",
8135 Actual, Gen_T);
8136 end if;
8138 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
8139 Error_Msg_NE
8140 ("expect protected access type for formal &",
8141 Actual, Gen_T);
8142 end if;
8143 end Validate_Access_Subprogram_Instance;
8145 -----------------------------------
8146 -- Validate_Access_Type_Instance --
8147 -----------------------------------
8149 procedure Validate_Access_Type_Instance is
8150 Desig_Type : constant Entity_Id :=
8151 Find_Actual_Type
8152 (Designated_Type (A_Gen_T), Scope (A_Gen_T));
8154 begin
8155 if not Is_Access_Type (Act_T) then
8156 Error_Msg_NE
8157 ("expect access type in instantiation of &", Actual, Gen_T);
8158 Abandon_Instantiation (Actual);
8159 end if;
8161 if Is_Access_Constant (A_Gen_T) then
8162 if not Is_Access_Constant (Act_T) then
8163 Error_Msg_N
8164 ("actual type must be access-to-constant type", Actual);
8165 Abandon_Instantiation (Actual);
8166 end if;
8167 else
8168 if Is_Access_Constant (Act_T) then
8169 Error_Msg_N
8170 ("actual type must be access-to-variable type", Actual);
8171 Abandon_Instantiation (Actual);
8173 elsif Ekind (A_Gen_T) = E_General_Access_Type
8174 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
8175 then
8176 Error_Msg_N ("actual must be general access type!", Actual);
8177 Error_Msg_NE ("add ALL to }!", Actual, Act_T);
8178 Abandon_Instantiation (Actual);
8179 end if;
8180 end if;
8182 -- The designated subtypes, that is to say the subtypes introduced
8183 -- by an access type declaration (and not by a subtype declaration)
8184 -- must match.
8186 if not Subtypes_Match
8187 (Desig_Type, Designated_Type (Base_Type (Act_T)))
8188 then
8189 Error_Msg_NE
8190 ("designated type of actual does not match that of formal &",
8191 Actual, Gen_T);
8192 Abandon_Instantiation (Actual);
8194 elsif Is_Access_Type (Designated_Type (Act_T))
8195 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
8197 Is_Constrained (Designated_Type (Desig_Type))
8198 then
8199 Error_Msg_NE
8200 ("designated type of actual does not match that of formal &",
8201 Actual, Gen_T);
8202 Abandon_Instantiation (Actual);
8203 end if;
8204 end Validate_Access_Type_Instance;
8206 ----------------------------------
8207 -- Validate_Array_Type_Instance --
8208 ----------------------------------
8210 procedure Validate_Array_Type_Instance is
8211 I1 : Node_Id;
8212 I2 : Node_Id;
8213 T2 : Entity_Id;
8215 function Formal_Dimensions return Int;
8216 -- Count number of dimensions in array type formal
8218 -----------------------
8219 -- Formal_Dimensions --
8220 -----------------------
8222 function Formal_Dimensions return Int is
8223 Num : Int := 0;
8224 Index : Node_Id;
8226 begin
8227 if Nkind (Def) = N_Constrained_Array_Definition then
8228 Index := First (Discrete_Subtype_Definitions (Def));
8229 else
8230 Index := First (Subtype_Marks (Def));
8231 end if;
8233 while Present (Index) loop
8234 Num := Num + 1;
8235 Next_Index (Index);
8236 end loop;
8238 return Num;
8239 end Formal_Dimensions;
8241 -- Start of processing for Validate_Array_Type_Instance
8243 begin
8244 if not Is_Array_Type (Act_T) then
8245 Error_Msg_NE
8246 ("expect array type in instantiation of &", Actual, Gen_T);
8247 Abandon_Instantiation (Actual);
8249 elsif Nkind (Def) = N_Constrained_Array_Definition then
8250 if not (Is_Constrained (Act_T)) then
8251 Error_Msg_NE
8252 ("expect constrained array in instantiation of &",
8253 Actual, Gen_T);
8254 Abandon_Instantiation (Actual);
8255 end if;
8257 else
8258 if Is_Constrained (Act_T) then
8259 Error_Msg_NE
8260 ("expect unconstrained array in instantiation of &",
8261 Actual, Gen_T);
8262 Abandon_Instantiation (Actual);
8263 end if;
8264 end if;
8266 if Formal_Dimensions /= Number_Dimensions (Act_T) then
8267 Error_Msg_NE
8268 ("dimensions of actual do not match formal &", Actual, Gen_T);
8269 Abandon_Instantiation (Actual);
8270 end if;
8272 I1 := First_Index (A_Gen_T);
8273 I2 := First_Index (Act_T);
8274 for J in 1 .. Formal_Dimensions loop
8276 -- If the indices of the actual were given by a subtype_mark,
8277 -- the index was transformed into a range attribute. Retrieve
8278 -- the original type mark for checking.
8280 if Is_Entity_Name (Original_Node (I2)) then
8281 T2 := Entity (Original_Node (I2));
8282 else
8283 T2 := Etype (I2);
8284 end if;
8286 if not Subtypes_Match
8287 (Find_Actual_Type (Etype (I1), Scope (A_Gen_T)), T2)
8288 then
8289 Error_Msg_NE
8290 ("index types of actual do not match those of formal &",
8291 Actual, Gen_T);
8292 Abandon_Instantiation (Actual);
8293 end if;
8295 Next_Index (I1);
8296 Next_Index (I2);
8297 end loop;
8299 if not Subtypes_Match (
8300 Find_Actual_Type (Component_Type (A_Gen_T), Scope (A_Gen_T)),
8301 Component_Type (Act_T))
8302 then
8303 Error_Msg_NE
8304 ("component subtype of actual does not match that of formal &",
8305 Actual, Gen_T);
8306 Abandon_Instantiation (Actual);
8307 end if;
8309 if Has_Aliased_Components (A_Gen_T)
8310 and then not Has_Aliased_Components (Act_T)
8311 then
8312 Error_Msg_NE
8313 ("actual must have aliased components to match formal type &",
8314 Actual, Gen_T);
8315 end if;
8317 end Validate_Array_Type_Instance;
8319 -----------------------------------------------
8320 -- Validate_Derived_Interface_Type_Instance --
8321 -----------------------------------------------
8323 procedure Validate_Derived_Interface_Type_Instance is
8324 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
8325 Elmt : Elmt_Id;
8327 begin
8328 -- First apply interface instance checks
8330 Validate_Interface_Type_Instance;
8332 -- Verify that immediate parent interface is an ancestor of
8333 -- the actual.
8335 if Present (Par)
8336 and then not Interface_Present_In_Ancestor (Act_T, Par)
8337 then
8338 Error_Msg_NE
8339 ("interface actual must include progenitor&", Actual, Par);
8340 end if;
8342 -- Now verify that the actual includes all other ancestors of
8343 -- the formal.
8345 Elmt := First_Elmt (Abstract_Interfaces (A_Gen_T));
8346 while Present (Elmt) loop
8347 if not Interface_Present_In_Ancestor (Act_T, Node (Elmt)) then
8348 Error_Msg_NE
8349 ("interface actual must include progenitor&",
8350 Actual, Node (Elmt));
8351 end if;
8353 Next_Elmt (Elmt);
8354 end loop;
8355 end Validate_Derived_Interface_Type_Instance;
8357 ------------------------------------
8358 -- Validate_Derived_Type_Instance --
8359 ------------------------------------
8361 procedure Validate_Derived_Type_Instance is
8362 Actual_Discr : Entity_Id;
8363 Ancestor_Discr : Entity_Id;
8365 begin
8366 -- If the parent type in the generic declaration is itself a previous
8367 -- formal type, then it is local to the generic and absent from the
8368 -- analyzed generic definition. In that case the ancestor is the
8369 -- instance of the formal (which must have been instantiated
8370 -- previously), unless the ancestor is itself a formal derived type.
8371 -- In this latter case (which is the subject of Corrigendum 8652/0038
8372 -- (AI-202) the ancestor of the formals is the ancestor of its
8373 -- parent. Otherwise, the analyzed generic carries the parent type.
8374 -- If the parent type is defined in a previous formal package, then
8375 -- the scope of that formal package is that of the generic type
8376 -- itself, and it has already been mapped into the corresponding type
8377 -- in the actual package.
8379 -- Common case: parent type defined outside of the generic
8381 if Is_Entity_Name (Subtype_Mark (Def))
8382 and then Present (Entity (Subtype_Mark (Def)))
8383 then
8384 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
8386 -- Check whether parent is defined in a previous formal package
8388 elsif
8389 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
8390 then
8391 Ancestor :=
8392 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
8394 -- The type may be a local derivation, or a type extension of
8395 -- a previous formal, or of a formal of a parent package.
8397 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
8398 or else
8399 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
8400 then
8401 -- Check whether the parent is another derived formal type
8402 -- in the same generic unit.
8404 if Etype (A_Gen_T) /= A_Gen_T
8405 and then Is_Generic_Type (Etype (A_Gen_T))
8406 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
8407 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
8408 then
8409 -- Locate ancestor of parent from the subtype declaration
8410 -- created for the actual.
8412 declare
8413 Decl : Node_Id;
8415 begin
8416 Decl := First (Actual_Decls);
8417 while Present (Decl) loop
8418 if Nkind (Decl) = N_Subtype_Declaration
8419 and then Chars (Defining_Identifier (Decl)) =
8420 Chars (Etype (A_Gen_T))
8421 then
8422 Ancestor := Generic_Parent_Type (Decl);
8423 exit;
8424 else
8425 Next (Decl);
8426 end if;
8427 end loop;
8428 end;
8430 pragma Assert (Present (Ancestor));
8432 else
8433 Ancestor :=
8434 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
8435 end if;
8437 else
8438 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
8439 end if;
8441 -- Ada 2005 (AI-251)
8443 if Ada_Version >= Ada_05
8444 and then Is_Interface (Ancestor)
8445 then
8446 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
8447 Error_Msg_NE
8448 ("(Ada 2005) expected type implementing & in instantiation",
8449 Actual, Ancestor);
8450 end if;
8452 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
8453 Error_Msg_NE
8454 ("expect type derived from & in instantiation",
8455 Actual, First_Subtype (Ancestor));
8456 Abandon_Instantiation (Actual);
8457 end if;
8459 -- Perform atomic/volatile checks (RM C.6(12))
8461 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
8462 Error_Msg_N
8463 ("cannot have atomic actual type for non-atomic formal type",
8464 Actual);
8466 elsif Is_Volatile (Act_T)
8467 and then not Is_Volatile (Ancestor)
8468 and then Is_By_Reference_Type (Ancestor)
8469 then
8470 Error_Msg_N
8471 ("cannot have volatile actual type for non-volatile formal type",
8472 Actual);
8473 end if;
8475 -- It should not be necessary to check for unknown discriminants
8476 -- on Formal, but for some reason Has_Unknown_Discriminants is
8477 -- false for A_Gen_T, so Is_Indefinite_Subtype incorrectly
8478 -- returns False. This needs fixing. ???
8480 if not Is_Indefinite_Subtype (A_Gen_T)
8481 and then not Unknown_Discriminants_Present (Formal)
8482 and then Is_Indefinite_Subtype (Act_T)
8483 then
8484 Error_Msg_N
8485 ("actual subtype must be constrained", Actual);
8486 Abandon_Instantiation (Actual);
8487 end if;
8489 if not Unknown_Discriminants_Present (Formal) then
8490 if Is_Constrained (Ancestor) then
8491 if not Is_Constrained (Act_T) then
8492 Error_Msg_N
8493 ("actual subtype must be constrained", Actual);
8494 Abandon_Instantiation (Actual);
8495 end if;
8497 -- Ancestor is unconstrained
8499 elsif Is_Constrained (Act_T) then
8500 if Ekind (Ancestor) = E_Access_Type
8501 or else Is_Composite_Type (Ancestor)
8502 then
8503 Error_Msg_N
8504 ("actual subtype must be unconstrained", Actual);
8505 Abandon_Instantiation (Actual);
8506 end if;
8508 -- A class-wide type is only allowed if the formal has
8509 -- unknown discriminants.
8511 elsif Is_Class_Wide_Type (Act_T)
8512 and then not Has_Unknown_Discriminants (Ancestor)
8513 then
8514 Error_Msg_NE
8515 ("actual for & cannot be a class-wide type", Actual, Gen_T);
8516 Abandon_Instantiation (Actual);
8518 -- Otherwise, the formal and actual shall have the same
8519 -- number of discriminants and each discriminant of the
8520 -- actual must correspond to a discriminant of the formal.
8522 elsif Has_Discriminants (Act_T)
8523 and then not Has_Unknown_Discriminants (Act_T)
8524 and then Has_Discriminants (Ancestor)
8525 then
8526 Actual_Discr := First_Discriminant (Act_T);
8527 Ancestor_Discr := First_Discriminant (Ancestor);
8528 while Present (Actual_Discr)
8529 and then Present (Ancestor_Discr)
8530 loop
8531 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
8532 not Present (Corresponding_Discriminant (Actual_Discr))
8533 then
8534 Error_Msg_NE
8535 ("discriminant & does not correspond " &
8536 "to ancestor discriminant", Actual, Actual_Discr);
8537 Abandon_Instantiation (Actual);
8538 end if;
8540 Next_Discriminant (Actual_Discr);
8541 Next_Discriminant (Ancestor_Discr);
8542 end loop;
8544 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
8545 Error_Msg_NE
8546 ("actual for & must have same number of discriminants",
8547 Actual, Gen_T);
8548 Abandon_Instantiation (Actual);
8549 end if;
8551 -- This case should be caught by the earlier check for
8552 -- for constrainedness, but the check here is added for
8553 -- completeness.
8555 elsif Has_Discriminants (Act_T)
8556 and then not Has_Unknown_Discriminants (Act_T)
8557 then
8558 Error_Msg_NE
8559 ("actual for & must not have discriminants", Actual, Gen_T);
8560 Abandon_Instantiation (Actual);
8562 elsif Has_Discriminants (Ancestor) then
8563 Error_Msg_NE
8564 ("actual for & must have known discriminants", Actual, Gen_T);
8565 Abandon_Instantiation (Actual);
8566 end if;
8568 if not Subtypes_Statically_Compatible (Act_T, Ancestor) then
8569 Error_Msg_N
8570 ("constraint on actual is incompatible with formal", Actual);
8571 Abandon_Instantiation (Actual);
8572 end if;
8573 end if;
8574 end Validate_Derived_Type_Instance;
8576 --------------------------------------
8577 -- Validate_Interface_Type_Instance --
8578 --------------------------------------
8580 procedure Validate_Interface_Type_Instance is
8581 begin
8582 if not Is_Interface (Act_T) then
8583 Error_Msg_NE
8584 ("actual for formal interface type must be an interface",
8585 Actual, Gen_T);
8587 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
8588 or else
8589 Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
8590 or else
8591 Is_Protected_Interface (A_Gen_T) /=
8592 Is_Protected_Interface (Act_T)
8593 or else
8594 Is_Synchronized_Interface (A_Gen_T) /=
8595 Is_Synchronized_Interface (Act_T)
8596 then
8597 Error_Msg_NE
8598 ("actual for interface& does not match ('R'M 12.5.5(5))",
8599 Actual, Gen_T);
8600 end if;
8601 end Validate_Interface_Type_Instance;
8603 ------------------------------------
8604 -- Validate_Private_Type_Instance --
8605 ------------------------------------
8607 procedure Validate_Private_Type_Instance is
8608 Formal_Discr : Entity_Id;
8609 Actual_Discr : Entity_Id;
8610 Formal_Subt : Entity_Id;
8612 begin
8613 if Is_Limited_Type (Act_T)
8614 and then not Is_Limited_Type (A_Gen_T)
8615 then
8616 Error_Msg_NE
8617 ("actual for non-limited & cannot be a limited type", Actual,
8618 Gen_T);
8619 Explain_Limited_Type (Act_T, Actual);
8620 Abandon_Instantiation (Actual);
8622 elsif Is_Indefinite_Subtype (Act_T)
8623 and then not Is_Indefinite_Subtype (A_Gen_T)
8624 and then Ada_Version >= Ada_95
8625 then
8626 Error_Msg_NE
8627 ("actual for & must be a definite subtype", Actual, Gen_T);
8629 elsif not Is_Tagged_Type (Act_T)
8630 and then Is_Tagged_Type (A_Gen_T)
8631 then
8632 Error_Msg_NE
8633 ("actual for & must be a tagged type", Actual, Gen_T);
8635 elsif Has_Discriminants (A_Gen_T) then
8636 if not Has_Discriminants (Act_T) then
8637 Error_Msg_NE
8638 ("actual for & must have discriminants", Actual, Gen_T);
8639 Abandon_Instantiation (Actual);
8641 elsif Is_Constrained (Act_T) then
8642 Error_Msg_NE
8643 ("actual for & must be unconstrained", Actual, Gen_T);
8644 Abandon_Instantiation (Actual);
8646 else
8647 Formal_Discr := First_Discriminant (A_Gen_T);
8648 Actual_Discr := First_Discriminant (Act_T);
8649 while Formal_Discr /= Empty loop
8650 if Actual_Discr = Empty then
8651 Error_Msg_NE
8652 ("discriminants on actual do not match formal",
8653 Actual, Gen_T);
8654 Abandon_Instantiation (Actual);
8655 end if;
8657 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
8659 -- Access discriminants match if designated types do
8661 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
8662 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
8663 E_Anonymous_Access_Type
8664 and then
8665 Get_Instance_Of
8666 (Designated_Type (Base_Type (Formal_Subt))) =
8667 Designated_Type (Base_Type (Etype (Actual_Discr)))
8668 then
8669 null;
8671 elsif Base_Type (Formal_Subt) /=
8672 Base_Type (Etype (Actual_Discr))
8673 then
8674 Error_Msg_NE
8675 ("types of actual discriminants must match formal",
8676 Actual, Gen_T);
8677 Abandon_Instantiation (Actual);
8679 elsif not Subtypes_Statically_Match
8680 (Formal_Subt, Etype (Actual_Discr))
8681 and then Ada_Version >= Ada_95
8682 then
8683 Error_Msg_NE
8684 ("subtypes of actual discriminants must match formal",
8685 Actual, Gen_T);
8686 Abandon_Instantiation (Actual);
8687 end if;
8689 Next_Discriminant (Formal_Discr);
8690 Next_Discriminant (Actual_Discr);
8691 end loop;
8693 if Actual_Discr /= Empty then
8694 Error_Msg_NE
8695 ("discriminants on actual do not match formal",
8696 Actual, Gen_T);
8697 Abandon_Instantiation (Actual);
8698 end if;
8699 end if;
8701 end if;
8703 Ancestor := Gen_T;
8704 end Validate_Private_Type_Instance;
8706 -- Start of processing for Instantiate_Type
8708 begin
8709 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
8710 Error_Msg_N ("duplicate instantiation of generic type", Actual);
8711 return Error;
8713 elsif not Is_Entity_Name (Actual)
8714 or else not Is_Type (Entity (Actual))
8715 then
8716 Error_Msg_NE
8717 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
8718 Abandon_Instantiation (Actual);
8720 else
8721 Act_T := Entity (Actual);
8723 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
8724 -- as a generic actual parameter if the corresponding formal type
8725 -- does not have a known_discriminant_part, or is a formal derived
8726 -- type that is an Unchecked_Union type.
8728 if Is_Unchecked_Union (Base_Type (Act_T)) then
8729 if not Has_Discriminants (A_Gen_T)
8730 or else
8731 (Is_Derived_Type (A_Gen_T)
8732 and then
8733 Is_Unchecked_Union (A_Gen_T))
8734 then
8735 null;
8736 else
8737 Error_Msg_N ("Unchecked_Union cannot be the actual for a" &
8738 " discriminated formal type", Act_T);
8740 end if;
8741 end if;
8743 -- Deal with fixed/floating restrictions
8745 if Is_Floating_Point_Type (Act_T) then
8746 Check_Restriction (No_Floating_Point, Actual);
8747 elsif Is_Fixed_Point_Type (Act_T) then
8748 Check_Restriction (No_Fixed_Point, Actual);
8749 end if;
8751 -- Deal with error of using incomplete type as generic actual
8753 if Ekind (Act_T) = E_Incomplete_Type then
8754 if No (Underlying_Type (Act_T)) then
8755 Error_Msg_N ("premature use of incomplete type", Actual);
8756 Abandon_Instantiation (Actual);
8757 else
8758 Act_T := Full_View (Act_T);
8759 Set_Entity (Actual, Act_T);
8761 if Has_Private_Component (Act_T) then
8762 Error_Msg_N
8763 ("premature use of type with private component", Actual);
8764 end if;
8765 end if;
8767 -- Deal with error of premature use of private type as generic actual
8769 elsif Is_Private_Type (Act_T)
8770 and then Is_Private_Type (Base_Type (Act_T))
8771 and then not Is_Generic_Type (Act_T)
8772 and then not Is_Derived_Type (Act_T)
8773 and then No (Full_View (Root_Type (Act_T)))
8774 then
8775 Error_Msg_N ("premature use of private type", Actual);
8777 elsif Has_Private_Component (Act_T) then
8778 Error_Msg_N
8779 ("premature use of type with private component", Actual);
8780 end if;
8782 Set_Instance_Of (A_Gen_T, Act_T);
8784 -- If the type is generic, the class-wide type may also be used
8786 if Is_Tagged_Type (A_Gen_T)
8787 and then Is_Tagged_Type (Act_T)
8788 and then not Is_Class_Wide_Type (A_Gen_T)
8789 then
8790 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
8791 Class_Wide_Type (Act_T));
8792 end if;
8794 if not Is_Abstract (A_Gen_T)
8795 and then Is_Abstract (Act_T)
8796 then
8797 Error_Msg_N
8798 ("actual of non-abstract formal cannot be abstract", Actual);
8799 end if;
8801 if Is_Scalar_Type (Gen_T) then
8802 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
8803 end if;
8804 end if;
8806 case Nkind (Def) is
8807 when N_Formal_Private_Type_Definition =>
8808 Validate_Private_Type_Instance;
8810 when N_Formal_Derived_Type_Definition =>
8811 Validate_Derived_Type_Instance;
8813 when N_Formal_Discrete_Type_Definition =>
8814 if not Is_Discrete_Type (Act_T) then
8815 Error_Msg_NE
8816 ("expect discrete type in instantiation of&", Actual, Gen_T);
8817 Abandon_Instantiation (Actual);
8818 end if;
8820 when N_Formal_Signed_Integer_Type_Definition =>
8821 if not Is_Signed_Integer_Type (Act_T) then
8822 Error_Msg_NE
8823 ("expect signed integer type in instantiation of&",
8824 Actual, Gen_T);
8825 Abandon_Instantiation (Actual);
8826 end if;
8828 when N_Formal_Modular_Type_Definition =>
8829 if not Is_Modular_Integer_Type (Act_T) then
8830 Error_Msg_NE
8831 ("expect modular type in instantiation of &", Actual, Gen_T);
8832 Abandon_Instantiation (Actual);
8833 end if;
8835 when N_Formal_Floating_Point_Definition =>
8836 if not Is_Floating_Point_Type (Act_T) then
8837 Error_Msg_NE
8838 ("expect float type in instantiation of &", Actual, Gen_T);
8839 Abandon_Instantiation (Actual);
8840 end if;
8842 when N_Formal_Ordinary_Fixed_Point_Definition =>
8843 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
8844 Error_Msg_NE
8845 ("expect ordinary fixed point type in instantiation of &",
8846 Actual, Gen_T);
8847 Abandon_Instantiation (Actual);
8848 end if;
8850 when N_Formal_Decimal_Fixed_Point_Definition =>
8851 if not Is_Decimal_Fixed_Point_Type (Act_T) then
8852 Error_Msg_NE
8853 ("expect decimal type in instantiation of &",
8854 Actual, Gen_T);
8855 Abandon_Instantiation (Actual);
8856 end if;
8858 when N_Array_Type_Definition =>
8859 Validate_Array_Type_Instance;
8861 when N_Access_To_Object_Definition =>
8862 Validate_Access_Type_Instance;
8864 when N_Access_Function_Definition |
8865 N_Access_Procedure_Definition =>
8866 Validate_Access_Subprogram_Instance;
8868 when N_Record_Definition =>
8869 Validate_Interface_Type_Instance;
8871 when N_Derived_Type_Definition =>
8872 Validate_Derived_Interface_Type_Instance;
8874 when others =>
8875 raise Program_Error;
8877 end case;
8879 Decl_Node :=
8880 Make_Subtype_Declaration (Loc,
8881 Defining_Identifier => New_Copy (Gen_T),
8882 Subtype_Indication => New_Reference_To (Act_T, Loc));
8884 if Is_Private_Type (Act_T) then
8885 Set_Has_Private_View (Subtype_Indication (Decl_Node));
8887 elsif Is_Access_Type (Act_T)
8888 and then Is_Private_Type (Designated_Type (Act_T))
8889 then
8890 Set_Has_Private_View (Subtype_Indication (Decl_Node));
8891 end if;
8893 -- Flag actual derived types so their elaboration produces the
8894 -- appropriate renamings for the primitive operations of the ancestor.
8895 -- Flag actual for formal private types as well, to determine whether
8896 -- operations in the private part may override inherited operations.
8898 if Nkind (Def) = N_Formal_Derived_Type_Definition
8899 or else Nkind (Def) = N_Formal_Private_Type_Definition
8900 then
8901 Set_Generic_Parent_Type (Decl_Node, Ancestor);
8902 end if;
8904 return Decl_Node;
8905 end Instantiate_Type;
8907 ---------------------
8908 -- Is_In_Main_Unit --
8909 ---------------------
8911 function Is_In_Main_Unit (N : Node_Id) return Boolean is
8912 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
8913 Current_Unit : Node_Id;
8915 begin
8916 if Unum = Main_Unit then
8917 return True;
8919 -- If the current unit is a subunit then it is either the main unit
8920 -- or is being compiled as part of the main unit.
8922 elsif Nkind (N) = N_Compilation_Unit then
8923 return Nkind (Unit (N)) = N_Subunit;
8924 end if;
8926 Current_Unit := Parent (N);
8927 while Present (Current_Unit)
8928 and then Nkind (Current_Unit) /= N_Compilation_Unit
8929 loop
8930 Current_Unit := Parent (Current_Unit);
8931 end loop;
8933 -- The instantiation node is in the main unit, or else the current
8934 -- node (perhaps as the result of nested instantiations) is in the
8935 -- main unit, or in the declaration of the main unit, which in this
8936 -- last case must be a body.
8938 return Unum = Main_Unit
8939 or else Current_Unit = Cunit (Main_Unit)
8940 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
8941 or else (Present (Library_Unit (Current_Unit))
8942 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
8943 end Is_In_Main_Unit;
8945 ----------------------------
8946 -- Load_Parent_Of_Generic --
8947 ----------------------------
8949 procedure Load_Parent_Of_Generic (N : Node_Id; Spec : Node_Id) is
8950 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
8951 Save_Style_Check : constant Boolean := Style_Check;
8952 True_Parent : Node_Id;
8953 Inst_Node : Node_Id;
8954 OK : Boolean;
8956 begin
8957 if not In_Same_Source_Unit (N, Spec)
8958 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
8959 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
8960 and then not Is_In_Main_Unit (Spec))
8961 then
8962 -- Find body of parent of spec, and analyze it. A special case
8963 -- arises when the parent is an instantiation, that is to say when
8964 -- we are currently instantiating a nested generic. In that case,
8965 -- there is no separate file for the body of the enclosing instance.
8966 -- Instead, the enclosing body must be instantiated as if it were
8967 -- a pending instantiation, in order to produce the body for the
8968 -- nested generic we require now. Note that in that case the
8969 -- generic may be defined in a package body, the instance defined
8970 -- in the same package body, and the original enclosing body may not
8971 -- be in the main unit.
8973 True_Parent := Parent (Spec);
8974 Inst_Node := Empty;
8976 while Present (True_Parent)
8977 and then Nkind (True_Parent) /= N_Compilation_Unit
8978 loop
8979 if Nkind (True_Parent) = N_Package_Declaration
8980 and then
8981 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
8982 then
8983 -- Parent is a compilation unit that is an instantiation.
8984 -- Instantiation node has been replaced with package decl.
8986 Inst_Node := Original_Node (True_Parent);
8987 exit;
8989 elsif Nkind (True_Parent) = N_Package_Declaration
8990 and then Present (Generic_Parent (Specification (True_Parent)))
8991 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
8992 then
8993 -- Parent is an instantiation within another specification.
8994 -- Declaration for instance has been inserted before original
8995 -- instantiation node. A direct link would be preferable?
8997 Inst_Node := Next (True_Parent);
8999 while Present (Inst_Node)
9000 and then Nkind (Inst_Node) /= N_Package_Instantiation
9001 loop
9002 Next (Inst_Node);
9003 end loop;
9005 -- If the instance appears within a generic, and the generic
9006 -- unit is defined within a formal package of the enclosing
9007 -- generic, there is no generic body available, and none
9008 -- needed. A more precise test should be used ???
9010 if No (Inst_Node) then
9011 return;
9012 end if;
9014 exit;
9015 else
9016 True_Parent := Parent (True_Parent);
9017 end if;
9018 end loop;
9020 -- Case where we are currently instantiating a nested generic
9022 if Present (Inst_Node) then
9023 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
9025 -- Instantiation node and declaration of instantiated package
9026 -- were exchanged when only the declaration was needed.
9027 -- Restore instantiation node before proceeding with body.
9029 Set_Unit (Parent (True_Parent), Inst_Node);
9030 end if;
9032 -- Now complete instantiation of enclosing body, if it appears
9033 -- in some other unit. If it appears in the current unit, the
9034 -- body will have been instantiated already.
9036 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
9038 -- We need to determine the expander mode to instantiate
9039 -- the enclosing body. Because the generic body we need
9040 -- may use global entities declared in the enclosing package
9041 -- (including aggregates) it is in general necessary to
9042 -- compile this body with expansion enabled. The exception
9043 -- is if we are within a generic package, in which case
9044 -- the usual generic rule applies.
9046 declare
9047 Exp_Status : Boolean := True;
9048 Scop : Entity_Id;
9050 begin
9051 -- Loop through scopes looking for generic package
9053 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
9054 while Present (Scop)
9055 and then Scop /= Standard_Standard
9056 loop
9057 if Ekind (Scop) = E_Generic_Package then
9058 Exp_Status := False;
9059 exit;
9060 end if;
9062 Scop := Scope (Scop);
9063 end loop;
9065 Instantiate_Package_Body
9066 (Pending_Body_Info'(
9067 Inst_Node, True_Parent, Exp_Status,
9068 Get_Code_Unit (Sloc (Inst_Node))));
9069 end;
9070 end if;
9072 -- Case where we are not instantiating a nested generic
9074 else
9075 Opt.Style_Check := False;
9076 Expander_Mode_Save_And_Set (True);
9077 Load_Needed_Body (Comp_Unit, OK);
9078 Opt.Style_Check := Save_Style_Check;
9079 Expander_Mode_Restore;
9081 if not OK
9082 and then Unit_Requires_Body (Defining_Entity (Spec))
9083 then
9084 declare
9085 Bname : constant Unit_Name_Type :=
9086 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
9088 begin
9089 Error_Msg_Unit_1 := Bname;
9090 Error_Msg_N ("this instantiation requires$!", N);
9091 Error_Msg_Name_1 :=
9092 Get_File_Name (Bname, Subunit => False);
9093 Error_Msg_N ("\but file{ was not found!", N);
9094 raise Unrecoverable_Error;
9095 end;
9096 end if;
9097 end if;
9098 end if;
9100 -- If loading the parent of the generic caused an instantiation
9101 -- circularity, we abandon compilation at this point, because
9102 -- otherwise in some cases we get into trouble with infinite
9103 -- recursions after this point.
9105 if Circularity_Detected then
9106 raise Unrecoverable_Error;
9107 end if;
9108 end Load_Parent_Of_Generic;
9110 -----------------------
9111 -- Move_Freeze_Nodes --
9112 -----------------------
9114 procedure Move_Freeze_Nodes
9115 (Out_Of : Entity_Id;
9116 After : Node_Id;
9117 L : List_Id)
9119 Decl : Node_Id;
9120 Next_Decl : Node_Id;
9121 Next_Node : Node_Id := After;
9122 Spec : Node_Id;
9124 function Is_Outer_Type (T : Entity_Id) return Boolean;
9125 -- Check whether entity is declared in a scope external to that
9126 -- of the generic unit.
9128 -------------------
9129 -- Is_Outer_Type --
9130 -------------------
9132 function Is_Outer_Type (T : Entity_Id) return Boolean is
9133 Scop : Entity_Id := Scope (T);
9135 begin
9136 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
9137 return True;
9139 else
9140 while Scop /= Standard_Standard loop
9142 if Scop = Out_Of then
9143 return False;
9144 else
9145 Scop := Scope (Scop);
9146 end if;
9147 end loop;
9149 return True;
9150 end if;
9151 end Is_Outer_Type;
9153 -- Start of processing for Move_Freeze_Nodes
9155 begin
9156 if No (L) then
9157 return;
9158 end if;
9160 -- First remove the freeze nodes that may appear before all other
9161 -- declarations.
9163 Decl := First (L);
9164 while Present (Decl)
9165 and then Nkind (Decl) = N_Freeze_Entity
9166 and then Is_Outer_Type (Entity (Decl))
9167 loop
9168 Decl := Remove_Head (L);
9169 Insert_After (Next_Node, Decl);
9170 Set_Analyzed (Decl, False);
9171 Next_Node := Decl;
9172 Decl := First (L);
9173 end loop;
9175 -- Next scan the list of declarations and remove each freeze node that
9176 -- appears ahead of the current node.
9178 while Present (Decl) loop
9179 while Present (Next (Decl))
9180 and then Nkind (Next (Decl)) = N_Freeze_Entity
9181 and then Is_Outer_Type (Entity (Next (Decl)))
9182 loop
9183 Next_Decl := Remove_Next (Decl);
9184 Insert_After (Next_Node, Next_Decl);
9185 Set_Analyzed (Next_Decl, False);
9186 Next_Node := Next_Decl;
9187 end loop;
9189 -- If the declaration is a nested package or concurrent type, then
9190 -- recurse. Nested generic packages will have been processed from the
9191 -- inside out.
9193 if Nkind (Decl) = N_Package_Declaration then
9194 Spec := Specification (Decl);
9196 elsif Nkind (Decl) = N_Task_Type_Declaration then
9197 Spec := Task_Definition (Decl);
9199 elsif Nkind (Decl) = N_Protected_Type_Declaration then
9200 Spec := Protected_Definition (Decl);
9202 else
9203 Spec := Empty;
9204 end if;
9206 if Present (Spec) then
9207 Move_Freeze_Nodes (Out_Of, Next_Node,
9208 Visible_Declarations (Spec));
9209 Move_Freeze_Nodes (Out_Of, Next_Node,
9210 Private_Declarations (Spec));
9211 end if;
9213 Next (Decl);
9214 end loop;
9215 end Move_Freeze_Nodes;
9217 ----------------
9218 -- Next_Assoc --
9219 ----------------
9221 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
9222 begin
9223 return Generic_Renamings.Table (E).Next_In_HTable;
9224 end Next_Assoc;
9226 ------------------------
9227 -- Preanalyze_Actuals --
9228 ------------------------
9230 procedure Pre_Analyze_Actuals (N : Node_Id) is
9231 Assoc : Node_Id;
9232 Act : Node_Id;
9233 Errs : constant Int := Serious_Errors_Detected;
9235 begin
9236 Assoc := First (Generic_Associations (N));
9238 while Present (Assoc) loop
9239 Act := Explicit_Generic_Actual_Parameter (Assoc);
9241 -- Within a nested instantiation, a defaulted actual is an
9242 -- empty association, so nothing to analyze. If the actual for
9243 -- a subprogram is an attribute, analyze prefix only, because
9244 -- actual is not a complete attribute reference.
9246 -- If actual is an allocator, analyze expression only. The full
9247 -- analysis can generate code, and if the instance is a compilation
9248 -- unit we have to wait until the package instance is installed to
9249 -- have a proper place to insert this code.
9251 -- String literals may be operators, but at this point we do not
9252 -- know whether the actual is a formal subprogram or a string.
9254 if No (Act) then
9255 null;
9257 elsif Nkind (Act) = N_Attribute_Reference then
9258 Analyze (Prefix (Act));
9260 elsif Nkind (Act) = N_Explicit_Dereference then
9261 Analyze (Prefix (Act));
9263 elsif Nkind (Act) = N_Allocator then
9264 declare
9265 Expr : constant Node_Id := Expression (Act);
9267 begin
9268 if Nkind (Expr) = N_Subtype_Indication then
9269 Analyze (Subtype_Mark (Expr));
9270 Analyze_List (Constraints (Constraint (Expr)));
9271 else
9272 Analyze (Expr);
9273 end if;
9274 end;
9276 elsif Nkind (Act) /= N_Operator_Symbol then
9277 Analyze (Act);
9278 end if;
9280 if Errs /= Serious_Errors_Detected then
9281 Abandon_Instantiation (Act);
9282 end if;
9284 Next (Assoc);
9285 end loop;
9286 end Pre_Analyze_Actuals;
9288 -------------------
9289 -- Remove_Parent --
9290 -------------------
9292 procedure Remove_Parent (In_Body : Boolean := False) is
9293 S : Entity_Id := Current_Scope;
9294 E : Entity_Id;
9295 P : Entity_Id;
9296 Hidden : Elmt_Id;
9298 begin
9299 -- After child instantiation is complete, remove from scope stack
9300 -- the extra copy of the current scope, and then remove parent
9301 -- instances.
9303 if not In_Body then
9304 Pop_Scope;
9306 while Current_Scope /= S loop
9307 P := Current_Scope;
9308 End_Package_Scope (Current_Scope);
9310 if In_Open_Scopes (P) then
9311 E := First_Entity (P);
9313 while Present (E) loop
9314 Set_Is_Immediately_Visible (E, True);
9315 Next_Entity (E);
9316 end loop;
9318 if Is_Generic_Instance (Current_Scope)
9319 and then P /= Current_Scope
9320 then
9321 -- We are within an instance of some sibling. Retain
9322 -- visibility of parent, for proper subsequent cleanup,
9323 -- and reinstall private declarations as well.
9325 Set_In_Private_Part (P);
9326 Install_Private_Declarations (P);
9327 end if;
9329 -- If the ultimate parent is a top-level unit recorded in
9330 -- Instance_Parent_Unit, then reset its visibility to what
9331 -- it was before instantiation. (It's not clear what the
9332 -- purpose is of testing whether Scope (P) is In_Open_Scopes,
9333 -- but that test was present before the ultimate parent test
9334 -- was added.???)
9336 elsif not In_Open_Scopes (Scope (P))
9337 or else (P = Instance_Parent_Unit
9338 and then not Parent_Unit_Visible)
9339 then
9340 Set_Is_Immediately_Visible (P, False);
9341 end if;
9342 end loop;
9344 -- Reset visibility of entities in the enclosing scope
9346 Set_Is_Hidden_Open_Scope (Current_Scope, False);
9347 Hidden := First_Elmt (Hidden_Entities);
9349 while Present (Hidden) loop
9350 Set_Is_Immediately_Visible (Node (Hidden), True);
9351 Next_Elmt (Hidden);
9352 end loop;
9354 else
9355 -- Each body is analyzed separately, and there is no context
9356 -- that needs preserving from one body instance to the next,
9357 -- so remove all parent scopes that have been installed.
9359 while Present (S) loop
9360 End_Package_Scope (S);
9361 Set_Is_Immediately_Visible (S, False);
9362 S := Current_Scope;
9363 exit when S = Standard_Standard;
9364 end loop;
9365 end if;
9367 end Remove_Parent;
9369 -----------------
9370 -- Restore_Env --
9371 -----------------
9373 procedure Restore_Env is
9374 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
9376 begin
9377 Ada_Version := Saved.Ada_Version;
9378 Ada_Version_Explicit := Saved.Ada_Version_Explicit;
9380 if No (Current_Instantiated_Parent.Act_Id) then
9382 -- Restore environment after subprogram inlining
9384 Restore_Private_Views (Empty);
9385 end if;
9387 Current_Instantiated_Parent := Saved.Instantiated_Parent;
9388 Exchanged_Views := Saved.Exchanged_Views;
9389 Hidden_Entities := Saved.Hidden_Entities;
9390 Current_Sem_Unit := Saved.Current_Sem_Unit;
9391 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
9392 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
9394 Instance_Envs.Decrement_Last;
9395 end Restore_Env;
9397 ---------------------------
9398 -- Restore_Private_Views --
9399 ---------------------------
9401 procedure Restore_Private_Views
9402 (Pack_Id : Entity_Id;
9403 Is_Package : Boolean := True)
9405 M : Elmt_Id;
9406 E : Entity_Id;
9407 Typ : Entity_Id;
9408 Dep_Elmt : Elmt_Id;
9409 Dep_Typ : Node_Id;
9411 procedure Restore_Nested_Formal (Formal : Entity_Id);
9412 -- Hide the generic formals of formal packages declared with box
9413 -- which were reachable in the current instantiation.
9415 procedure Restore_Nested_Formal (Formal : Entity_Id) is
9416 Ent : Entity_Id;
9417 begin
9418 if Present (Renamed_Object (Formal))
9419 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
9420 then
9421 return;
9423 elsif Present (Associated_Formal_Package (Formal))
9424 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9425 then
9426 Ent := First_Entity (Formal);
9428 while Present (Ent) loop
9429 exit when Ekind (Ent) = E_Package
9430 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
9432 Set_Is_Hidden (Ent);
9433 Set_Is_Potentially_Use_Visible (Ent, False);
9435 -- If package, then recurse
9437 if Ekind (Ent) = E_Package then
9438 Restore_Nested_Formal (Ent);
9439 end if;
9441 Next_Entity (Ent);
9442 end loop;
9443 end if;
9444 end Restore_Nested_Formal;
9446 begin
9447 M := First_Elmt (Exchanged_Views);
9448 while Present (M) loop
9449 Typ := Node (M);
9451 -- Subtypes of types whose views have been exchanged, and that
9452 -- are defined within the instance, were not on the list of
9453 -- Private_Dependents on entry to the instance, so they have to
9454 -- be exchanged explicitly now, in order to remain consistent with
9455 -- the view of the parent type.
9457 if Ekind (Typ) = E_Private_Type
9458 or else Ekind (Typ) = E_Limited_Private_Type
9459 or else Ekind (Typ) = E_Record_Type_With_Private
9460 then
9461 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
9463 while Present (Dep_Elmt) loop
9464 Dep_Typ := Node (Dep_Elmt);
9466 if Scope (Dep_Typ) = Pack_Id
9467 and then Present (Full_View (Dep_Typ))
9468 then
9469 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
9470 Exchange_Declarations (Dep_Typ);
9471 end if;
9473 Next_Elmt (Dep_Elmt);
9474 end loop;
9475 end if;
9477 Exchange_Declarations (Node (M));
9478 Next_Elmt (M);
9479 end loop;
9481 if No (Pack_Id) then
9482 return;
9483 end if;
9485 -- Make the generic formal parameters private, and make the formal
9486 -- types into subtypes of the actuals again.
9488 E := First_Entity (Pack_Id);
9490 while Present (E) loop
9491 Set_Is_Hidden (E, True);
9493 if Is_Type (E)
9494 and then Nkind (Parent (E)) = N_Subtype_Declaration
9495 then
9496 Set_Is_Generic_Actual_Type (E, False);
9498 -- An unusual case of aliasing: the actual may also be directly
9499 -- visible in the generic, and be private there, while it is
9500 -- fully visible in the context of the instance. The internal
9501 -- subtype is private in the instance, but has full visibility
9502 -- like its parent in the enclosing scope. This enforces the
9503 -- invariant that the privacy status of all private dependents of
9504 -- a type coincide with that of the parent type. This can only
9505 -- happen when a generic child unit is instantiated within a
9506 -- sibling.
9508 if Is_Private_Type (E)
9509 and then not Is_Private_Type (Etype (E))
9510 then
9511 Exchange_Declarations (E);
9512 end if;
9514 elsif Ekind (E) = E_Package then
9516 -- The end of the renaming list is the renaming of the generic
9517 -- package itself. If the instance is a subprogram, all entities
9518 -- in the corresponding package are renamings. If this entity is
9519 -- a formal package, make its own formals private as well. The
9520 -- actual in this case is itself the renaming of an instantation.
9521 -- If the entity is not a package renaming, it is the entity
9522 -- created to validate formal package actuals: ignore.
9524 -- If the actual is itself a formal package for the enclosing
9525 -- generic, or the actual for such a formal package, it remains
9526 -- visible on exit from the instance, and therefore nothing
9527 -- needs to be done either, except to keep it accessible.
9529 if Is_Package
9530 and then Renamed_Object (E) = Pack_Id
9531 then
9532 exit;
9534 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
9535 null;
9537 elsif Denotes_Formal_Package (Renamed_Object (E), True) then
9538 Set_Is_Hidden (E, False);
9540 else
9541 declare
9542 Act_P : constant Entity_Id := Renamed_Object (E);
9543 Id : Entity_Id;
9545 begin
9546 Id := First_Entity (Act_P);
9547 while Present (Id)
9548 and then Id /= First_Private_Entity (Act_P)
9549 loop
9550 exit when Ekind (Id) = E_Package
9551 and then Renamed_Object (Id) = Act_P;
9553 Set_Is_Hidden (Id, True);
9554 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
9556 if Ekind (Id) = E_Package then
9557 Restore_Nested_Formal (Id);
9558 end if;
9560 Next_Entity (Id);
9561 end loop;
9562 end;
9563 end if;
9564 end if;
9566 Next_Entity (E);
9567 end loop;
9568 end Restore_Private_Views;
9570 --------------
9571 -- Save_Env --
9572 --------------
9574 procedure Save_Env
9575 (Gen_Unit : Entity_Id;
9576 Act_Unit : Entity_Id)
9578 begin
9579 Init_Env;
9580 Set_Instance_Env (Gen_Unit, Act_Unit);
9581 end Save_Env;
9583 ----------------------------
9584 -- Save_Global_References --
9585 ----------------------------
9587 procedure Save_Global_References (N : Node_Id) is
9588 Gen_Scope : Entity_Id;
9589 E : Entity_Id;
9590 N2 : Node_Id;
9592 function Is_Global (E : Entity_Id) return Boolean;
9593 -- Check whether entity is defined outside of generic unit.
9594 -- Examine the scope of an entity, and the scope of the scope,
9595 -- etc, until we find either Standard, in which case the entity
9596 -- is global, or the generic unit itself, which indicates that
9597 -- the entity is local. If the entity is the generic unit itself,
9598 -- as in the case of a recursive call, or the enclosing generic unit,
9599 -- if different from the current scope, then it is local as well,
9600 -- because it will be replaced at the point of instantiation. On
9601 -- the other hand, if it is a reference to a child unit of a common
9602 -- ancestor, which appears in an instantiation, it is global because
9603 -- it is used to denote a specific compilation unit at the time the
9604 -- instantiations will be analyzed.
9606 procedure Reset_Entity (N : Node_Id);
9607 -- Save semantic information on global entity, so that it is not
9608 -- resolved again at instantiation time.
9610 procedure Save_Entity_Descendants (N : Node_Id);
9611 -- Apply Save_Global_References to the two syntactic descendants of
9612 -- non-terminal nodes that carry an Associated_Node and are processed
9613 -- through Reset_Entity. Once the global entity (if any) has been
9614 -- captured together with its type, only two syntactic descendants
9615 -- need to be traversed to complete the processing of the tree rooted
9616 -- at N. This applies to Selected_Components, Expanded_Names, and to
9617 -- Operator nodes. N can also be a character literal, identifier, or
9618 -- operator symbol node, but the call has no effect in these cases.
9620 procedure Save_Global_Defaults (N1, N2 : Node_Id);
9621 -- Default actuals in nested instances must be handled specially
9622 -- because there is no link to them from the original tree. When an
9623 -- actual subprogram is given by a default, we add an explicit generic
9624 -- association for it in the instantiation node. When we save the
9625 -- global references on the name of the instance, we recover the list
9626 -- of generic associations, and add an explicit one to the original
9627 -- generic tree, through which a global actual can be preserved.
9628 -- Similarly, if a child unit is instantiated within a sibling, in the
9629 -- context of the parent, we must preserve the identifier of the parent
9630 -- so that it can be properly resolved in a subsequent instantiation.
9632 procedure Save_Global_Descendant (D : Union_Id);
9633 -- Apply Save_Global_References recursively to the descendents of
9634 -- current node.
9636 procedure Save_References (N : Node_Id);
9637 -- This is the recursive procedure that does the work, once the
9638 -- enclosing generic scope has been established.
9640 ---------------
9641 -- Is_Global --
9642 ---------------
9644 function Is_Global (E : Entity_Id) return Boolean is
9645 Se : Entity_Id := Scope (E);
9647 function Is_Instance_Node (Decl : Node_Id) return Boolean;
9648 -- Determine whether the parent node of a reference to a child unit
9649 -- denotes an instantiation or a formal package, in which case the
9650 -- reference to the child unit is global, even if it appears within
9651 -- the current scope (e.g. when the instance appears within the body
9652 -- of an ancestor).
9654 ----------------------
9655 -- Is_Instance_Node --
9656 ----------------------
9658 function Is_Instance_Node (Decl : Node_Id) return Boolean is
9659 begin
9660 return (Nkind (Decl) in N_Generic_Instantiation
9661 or else
9662 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration);
9663 end Is_Instance_Node;
9665 -- Start of processing for Is_Global
9667 begin
9668 if E = Gen_Scope then
9669 return False;
9671 elsif E = Standard_Standard then
9672 return True;
9674 elsif Is_Child_Unit (E)
9675 and then (Is_Instance_Node (Parent (N2))
9676 or else (Nkind (Parent (N2)) = N_Expanded_Name
9677 and then N2 = Selector_Name (Parent (N2))
9678 and then Is_Instance_Node (Parent (Parent (N2)))))
9679 then
9680 return True;
9682 else
9683 while Se /= Gen_Scope loop
9684 if Se = Standard_Standard then
9685 return True;
9686 else
9687 Se := Scope (Se);
9688 end if;
9689 end loop;
9691 return False;
9692 end if;
9693 end Is_Global;
9695 ------------------
9696 -- Reset_Entity --
9697 ------------------
9699 procedure Reset_Entity (N : Node_Id) is
9701 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
9702 -- The type of N2 is global to the generic unit. Save the
9703 -- type in the generic node.
9705 function Top_Ancestor (E : Entity_Id) return Entity_Id;
9706 -- Find the ultimate ancestor of the current unit. If it is
9707 -- not a generic unit, then the name of the current unit
9708 -- in the prefix of an expanded name must be replaced with
9709 -- its generic homonym to ensure that it will be properly
9710 -- resolved in an instance.
9712 ---------------------
9713 -- Set_Global_Type --
9714 ---------------------
9716 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
9717 Typ : constant Entity_Id := Etype (N2);
9719 begin
9720 Set_Etype (N, Typ);
9722 if Entity (N) /= N2
9723 and then Has_Private_View (Entity (N))
9724 then
9725 -- If the entity of N is not the associated node, this is
9726 -- a nested generic and it has an associated node as well,
9727 -- whose type is already the full view (see below). Indicate
9728 -- that the original node has a private view.
9730 Set_Has_Private_View (N);
9731 end if;
9733 -- If not a private type, nothing else to do
9735 if not Is_Private_Type (Typ) then
9736 if Is_Array_Type (Typ)
9737 and then Is_Private_Type (Component_Type (Typ))
9738 then
9739 Set_Has_Private_View (N);
9740 end if;
9742 -- If it is a derivation of a private type in a context where
9743 -- no full view is needed, nothing to do either.
9745 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
9746 null;
9748 -- Otherwise mark the type for flipping and use the full_view
9749 -- when available.
9751 else
9752 Set_Has_Private_View (N);
9754 if Present (Full_View (Typ)) then
9755 Set_Etype (N2, Full_View (Typ));
9756 end if;
9757 end if;
9758 end Set_Global_Type;
9760 ------------------
9761 -- Top_Ancestor --
9762 ------------------
9764 function Top_Ancestor (E : Entity_Id) return Entity_Id is
9765 Par : Entity_Id := E;
9767 begin
9768 while Is_Child_Unit (Par) loop
9769 Par := Scope (Par);
9770 end loop;
9772 return Par;
9773 end Top_Ancestor;
9775 -- Start of processing for Reset_Entity
9777 begin
9778 N2 := Get_Associated_Node (N);
9779 E := Entity (N2);
9781 if Present (E) then
9782 if Is_Global (E) then
9783 Set_Global_Type (N, N2);
9785 elsif Nkind (N) = N_Op_Concat
9786 and then Is_Generic_Type (Etype (N2))
9787 and then
9788 (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
9789 or else Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
9790 and then Is_Intrinsic_Subprogram (E)
9791 then
9792 null;
9794 else
9795 -- Entity is local. Mark generic node as unresolved.
9796 -- Note that now it does not have an entity.
9798 Set_Associated_Node (N, Empty);
9799 Set_Etype (N, Empty);
9800 end if;
9802 if Nkind (Parent (N)) in N_Generic_Instantiation
9803 and then N = Name (Parent (N))
9804 then
9805 Save_Global_Defaults (Parent (N), Parent (N2));
9806 end if;
9808 elsif Nkind (Parent (N)) = N_Selected_Component
9809 and then Nkind (Parent (N2)) = N_Expanded_Name
9810 then
9811 if Is_Global (Entity (Parent (N2))) then
9812 Change_Selected_Component_To_Expanded_Name (Parent (N));
9813 Set_Associated_Node (Parent (N), Parent (N2));
9814 Set_Global_Type (Parent (N), Parent (N2));
9815 Save_Entity_Descendants (N);
9817 -- If this is a reference to the current generic entity,
9818 -- replace by the name of the generic homonym of the current
9819 -- package. This is because in an instantiation Par.P.Q will
9820 -- not resolve to the name of the instance, whose enclosing
9821 -- scope is not necessarily Par. We use the generic homonym
9822 -- rather that the name of the generic itself, because it may
9823 -- be hidden by a local declaration.
9825 elsif In_Open_Scopes (Entity (Parent (N2)))
9826 and then not
9827 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
9828 then
9829 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
9830 Rewrite (Parent (N),
9831 Make_Identifier (Sloc (N),
9832 Chars =>
9833 Chars (Generic_Homonym (Entity (Parent (N2))))));
9834 else
9835 Rewrite (Parent (N),
9836 Make_Identifier (Sloc (N),
9837 Chars => Chars (Selector_Name (Parent (N2)))));
9838 end if;
9839 end if;
9841 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
9842 and then Parent (N) = Name (Parent (Parent (N)))
9843 then
9844 Save_Global_Defaults
9845 (Parent (Parent (N)), Parent (Parent ((N2))));
9846 end if;
9848 -- A selected component may denote a static constant that has
9849 -- been folded. Make the same replacement in original tree.
9851 elsif Nkind (Parent (N)) = N_Selected_Component
9852 and then (Nkind (Parent (N2)) = N_Integer_Literal
9853 or else Nkind (Parent (N2)) = N_Real_Literal)
9854 then
9855 Rewrite (Parent (N),
9856 New_Copy (Parent (N2)));
9857 Set_Analyzed (Parent (N), False);
9859 -- A selected component may be transformed into a parameterless
9860 -- function call. If the called entity is global, rewrite the
9861 -- node appropriately, i.e. as an extended name for the global
9862 -- entity.
9864 elsif Nkind (Parent (N)) = N_Selected_Component
9865 and then Nkind (Parent (N2)) = N_Function_Call
9866 and then Is_Global (Entity (Name (Parent (N2))))
9867 then
9868 Change_Selected_Component_To_Expanded_Name (Parent (N));
9869 Set_Associated_Node (Parent (N), Name (Parent (N2)));
9870 Set_Global_Type (Parent (N), Name (Parent (N2)));
9871 Save_Entity_Descendants (N);
9873 else
9874 -- Entity is local. Reset in generic unit, so that node
9875 -- is resolved anew at the point of instantiation.
9877 Set_Associated_Node (N, Empty);
9878 Set_Etype (N, Empty);
9879 end if;
9880 end Reset_Entity;
9882 -----------------------------
9883 -- Save_Entity_Descendants --
9884 -----------------------------
9886 procedure Save_Entity_Descendants (N : Node_Id) is
9887 begin
9888 case Nkind (N) is
9889 when N_Binary_Op =>
9890 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
9891 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
9893 when N_Unary_Op =>
9894 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
9896 when N_Expanded_Name | N_Selected_Component =>
9897 Save_Global_Descendant (Union_Id (Prefix (N)));
9898 Save_Global_Descendant (Union_Id (Selector_Name (N)));
9900 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
9901 null;
9903 when others =>
9904 raise Program_Error;
9905 end case;
9906 end Save_Entity_Descendants;
9908 --------------------------
9909 -- Save_Global_Defaults --
9910 --------------------------
9912 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
9913 Loc : constant Source_Ptr := Sloc (N1);
9914 Assoc2 : constant List_Id := Generic_Associations (N2);
9915 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
9916 Assoc1 : List_Id;
9917 Act1 : Node_Id;
9918 Act2 : Node_Id;
9919 Def : Node_Id;
9920 Ndec : Node_Id;
9921 Subp : Entity_Id;
9922 Actual : Entity_Id;
9924 begin
9925 Assoc1 := Generic_Associations (N1);
9927 if Present (Assoc1) then
9928 Act1 := First (Assoc1);
9929 else
9930 Act1 := Empty;
9931 Set_Generic_Associations (N1, New_List);
9932 Assoc1 := Generic_Associations (N1);
9933 end if;
9935 if Present (Assoc2) then
9936 Act2 := First (Assoc2);
9937 else
9938 return;
9939 end if;
9941 while Present (Act1) and then Present (Act2) loop
9942 Next (Act1);
9943 Next (Act2);
9944 end loop;
9946 -- Find the associations added for default suprograms
9948 if Present (Act2) then
9949 while Nkind (Act2) /= N_Generic_Association
9950 or else No (Entity (Selector_Name (Act2)))
9951 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
9952 loop
9953 Next (Act2);
9954 end loop;
9956 -- Add a similar association if the default is global. The
9957 -- renaming declaration for the actual has been analyzed, and
9958 -- its alias is the program it renames. Link the actual in the
9959 -- original generic tree with the node in the analyzed tree.
9961 while Present (Act2) loop
9962 Subp := Entity (Selector_Name (Act2));
9963 Def := Explicit_Generic_Actual_Parameter (Act2);
9965 -- Following test is defence against rubbish errors
9967 if No (Alias (Subp)) then
9968 return;
9969 end if;
9971 -- Retrieve the resolved actual from the renaming declaration
9972 -- created for the instantiated formal.
9974 Actual := Entity (Name (Parent (Parent (Subp))));
9975 Set_Entity (Def, Actual);
9976 Set_Etype (Def, Etype (Actual));
9978 if Is_Global (Actual) then
9979 Ndec :=
9980 Make_Generic_Association (Loc,
9981 Selector_Name => New_Occurrence_Of (Subp, Loc),
9982 Explicit_Generic_Actual_Parameter =>
9983 New_Occurrence_Of (Actual, Loc));
9985 Set_Associated_Node
9986 (Explicit_Generic_Actual_Parameter (Ndec), Def);
9988 Append (Ndec, Assoc1);
9990 -- If there are other defaults, add a dummy association
9991 -- in case there are other defaulted formals with the same
9992 -- name.
9994 elsif Present (Next (Act2)) then
9995 Ndec :=
9996 Make_Generic_Association (Loc,
9997 Selector_Name => New_Occurrence_Of (Subp, Loc),
9998 Explicit_Generic_Actual_Parameter => Empty);
10000 Append (Ndec, Assoc1);
10001 end if;
10003 Next (Act2);
10004 end loop;
10005 end if;
10007 if Nkind (Name (N1)) = N_Identifier
10008 and then Is_Child_Unit (Gen_Id)
10009 and then Is_Global (Gen_Id)
10010 and then Is_Generic_Unit (Scope (Gen_Id))
10011 and then In_Open_Scopes (Scope (Gen_Id))
10012 then
10013 -- This is an instantiation of a child unit within a sibling,
10014 -- so that the generic parent is in scope. An eventual instance
10015 -- must occur within the scope of an instance of the parent.
10016 -- Make name in instance into an expanded name, to preserve the
10017 -- identifier of the parent, so it can be resolved subsequently.
10019 Rewrite (Name (N2),
10020 Make_Expanded_Name (Loc,
10021 Chars => Chars (Gen_Id),
10022 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
10023 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
10024 Set_Entity (Name (N2), Gen_Id);
10026 Rewrite (Name (N1),
10027 Make_Expanded_Name (Loc,
10028 Chars => Chars (Gen_Id),
10029 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
10030 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
10032 Set_Associated_Node (Name (N1), Name (N2));
10033 Set_Associated_Node (Prefix (Name (N1)), Empty);
10034 Set_Associated_Node
10035 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
10036 Set_Etype (Name (N1), Etype (Gen_Id));
10037 end if;
10039 end Save_Global_Defaults;
10041 ----------------------------
10042 -- Save_Global_Descendant --
10043 ----------------------------
10045 procedure Save_Global_Descendant (D : Union_Id) is
10046 N1 : Node_Id;
10048 begin
10049 if D in Node_Range then
10050 if D = Union_Id (Empty) then
10051 null;
10053 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
10054 Save_References (Node_Id (D));
10055 end if;
10057 elsif D in List_Range then
10058 if D = Union_Id (No_List)
10059 or else Is_Empty_List (List_Id (D))
10060 then
10061 null;
10063 else
10064 N1 := First (List_Id (D));
10065 while Present (N1) loop
10066 Save_References (N1);
10067 Next (N1);
10068 end loop;
10069 end if;
10071 -- Element list or other non-node field, nothing to do
10073 else
10074 null;
10075 end if;
10076 end Save_Global_Descendant;
10078 ---------------------
10079 -- Save_References --
10080 ---------------------
10082 -- This is the recursive procedure that does the work, once the
10083 -- enclosing generic scope has been established. We have to treat
10084 -- specially a number of node rewritings that are required by semantic
10085 -- processing and which change the kind of nodes in the generic copy:
10086 -- typically constant-folding, replacing an operator node by a string
10087 -- literal, or a selected component by an expanded name. In each of
10088 -- those cases, the transformation is propagated to the generic unit.
10090 procedure Save_References (N : Node_Id) is
10091 begin
10092 if N = Empty then
10093 null;
10095 elsif Nkind (N) = N_Character_Literal
10096 or else Nkind (N) = N_Operator_Symbol
10097 then
10098 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
10099 Reset_Entity (N);
10101 elsif Nkind (N) = N_Operator_Symbol
10102 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
10103 then
10104 Change_Operator_Symbol_To_String_Literal (N);
10105 end if;
10107 elsif Nkind (N) in N_Op then
10109 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
10111 if Nkind (N) = N_Op_Concat then
10112 Set_Is_Component_Left_Opnd (N,
10113 Is_Component_Left_Opnd (Get_Associated_Node (N)));
10115 Set_Is_Component_Right_Opnd (N,
10116 Is_Component_Right_Opnd (Get_Associated_Node (N)));
10117 end if;
10119 Reset_Entity (N);
10120 else
10121 -- Node may be transformed into call to a user-defined operator
10123 N2 := Get_Associated_Node (N);
10125 if Nkind (N2) = N_Function_Call then
10126 E := Entity (Name (N2));
10128 if Present (E)
10129 and then Is_Global (E)
10130 then
10131 Set_Etype (N, Etype (N2));
10132 else
10133 Set_Associated_Node (N, Empty);
10134 Set_Etype (N, Empty);
10135 end if;
10137 elsif Nkind (N2) = N_Integer_Literal
10138 or else Nkind (N2) = N_Real_Literal
10139 or else Nkind (N2) = N_String_Literal
10140 then
10141 -- Operation was constant-folded, perform the same
10142 -- replacement in generic.
10144 Rewrite (N, New_Copy (N2));
10145 Set_Analyzed (N, False);
10147 elsif Nkind (N2) = N_Identifier
10148 and then Ekind (Entity (N2)) = E_Enumeration_Literal
10149 then
10150 -- Same if call was folded into a literal, but in this
10151 -- case retain the entity to avoid spurious ambiguities
10152 -- if id is overloaded at the point of instantiation or
10153 -- inlining.
10155 Rewrite (N, New_Copy (N2));
10156 Set_Analyzed (N, False);
10157 end if;
10158 end if;
10160 -- Complete the check on operands, if node has not been
10161 -- constant-folded.
10163 if Nkind (N) in N_Op then
10164 Save_Entity_Descendants (N);
10165 end if;
10167 elsif Nkind (N) = N_Identifier then
10168 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
10170 -- If this is a discriminant reference, always save it.
10171 -- It is used in the instance to find the corresponding
10172 -- discriminant positionally rather than by name.
10174 Set_Original_Discriminant
10175 (N, Original_Discriminant (Get_Associated_Node (N)));
10176 Reset_Entity (N);
10178 else
10179 N2 := Get_Associated_Node (N);
10181 if Nkind (N2) = N_Function_Call then
10182 E := Entity (Name (N2));
10184 -- Name resolves to a call to parameterless function.
10185 -- If original entity is global, mark node as resolved.
10187 if Present (E)
10188 and then Is_Global (E)
10189 then
10190 Set_Etype (N, Etype (N2));
10191 else
10192 Set_Associated_Node (N, Empty);
10193 Set_Etype (N, Empty);
10194 end if;
10196 elsif
10197 Nkind (N2) = N_Integer_Literal or else
10198 Nkind (N2) = N_Real_Literal or else
10199 Nkind (N2) = N_String_Literal
10200 then
10201 -- Name resolves to named number that is constant-folded,
10202 -- or to string literal from concatenation.
10203 -- Perform the same replacement in generic.
10205 Rewrite (N, New_Copy (N2));
10206 Set_Analyzed (N, False);
10208 elsif Nkind (N2) = N_Explicit_Dereference then
10210 -- An identifier is rewritten as a dereference if it is
10211 -- the prefix in a selected component, and it denotes an
10212 -- access to a composite type, or a parameterless function
10213 -- call that returns an access type.
10215 -- Check whether corresponding entity in prefix is global
10217 if Is_Entity_Name (Prefix (N2))
10218 and then Present (Entity (Prefix (N2)))
10219 and then Is_Global (Entity (Prefix (N2)))
10220 then
10221 Rewrite (N,
10222 Make_Explicit_Dereference (Sloc (N),
10223 Prefix => Make_Identifier (Sloc (N),
10224 Chars => Chars (N))));
10225 Set_Associated_Node (Prefix (N), Prefix (N2));
10227 elsif Nkind (Prefix (N2)) = N_Function_Call
10228 and then Is_Global (Entity (Name (Prefix (N2))))
10229 then
10230 Rewrite (N,
10231 Make_Explicit_Dereference (Sloc (N),
10232 Prefix => Make_Function_Call (Sloc (N),
10233 Name =>
10234 Make_Identifier (Sloc (N),
10235 Chars => Chars (N)))));
10237 Set_Associated_Node
10238 (Name (Prefix (N)), Name (Prefix (N2)));
10240 else
10241 Set_Associated_Node (N, Empty);
10242 Set_Etype (N, Empty);
10243 end if;
10245 -- The subtype mark of a nominally unconstrained object
10246 -- is rewritten as a subtype indication using the bounds
10247 -- of the expression. Recover the original subtype mark.
10249 elsif Nkind (N2) = N_Subtype_Indication
10250 and then Is_Entity_Name (Original_Node (N2))
10251 then
10252 Set_Associated_Node (N, Original_Node (N2));
10253 Reset_Entity (N);
10255 else
10256 null;
10257 end if;
10258 end if;
10260 elsif Nkind (N) in N_Entity then
10261 null;
10263 else
10264 declare
10265 Loc : constant Source_Ptr := Sloc (N);
10266 Qual : Node_Id := Empty;
10267 Typ : Entity_Id := Empty;
10268 Nam : Node_Id;
10270 use Atree.Unchecked_Access;
10271 -- This code section is part of implementing an untyped tree
10272 -- traversal, so it needs direct access to node fields.
10274 begin
10275 if Nkind (N) = N_Aggregate
10276 or else
10277 Nkind (N) = N_Extension_Aggregate
10278 then
10279 N2 := Get_Associated_Node (N);
10281 if No (N2) then
10282 Typ := Empty;
10283 else
10284 Typ := Etype (N2);
10286 -- In an instance within a generic, use the name of
10287 -- the actual and not the original generic parameter.
10288 -- If the actual is global in the current generic it
10289 -- must be preserved for its instantiation.
10291 if Nkind (Parent (Typ)) = N_Subtype_Declaration
10292 and then
10293 Present (Generic_Parent_Type (Parent (Typ)))
10294 then
10295 Typ := Base_Type (Typ);
10296 Set_Etype (N2, Typ);
10297 end if;
10298 end if;
10300 if No (N2)
10301 or else No (Typ)
10302 or else not Is_Global (Typ)
10303 then
10304 Set_Associated_Node (N, Empty);
10306 -- If the aggregate is an actual in a call, it has been
10307 -- resolved in the current context, to some local type.
10308 -- The enclosing call may have been disambiguated by
10309 -- the aggregate, and this disambiguation might fail at
10310 -- instantiation time because the type to which the
10311 -- aggregate did resolve is not preserved. In order to
10312 -- preserve some of this information, we wrap the
10313 -- aggregate in a qualified expression, using the id of
10314 -- its type. For further disambiguation we qualify the
10315 -- type name with its scope (if visible) because both
10316 -- id's will have corresponding entities in an instance.
10317 -- This resolves most of the problems with missing type
10318 -- information on aggregates in instances.
10320 if Nkind (N2) = Nkind (N)
10321 and then
10322 (Nkind (Parent (N2)) = N_Procedure_Call_Statement
10323 or else Nkind (Parent (N2)) = N_Function_Call)
10324 and then Comes_From_Source (Typ)
10325 then
10326 if Is_Immediately_Visible (Scope (Typ)) then
10327 Nam := Make_Selected_Component (Loc,
10328 Prefix =>
10329 Make_Identifier (Loc, Chars (Scope (Typ))),
10330 Selector_Name =>
10331 Make_Identifier (Loc, Chars (Typ)));
10332 else
10333 Nam := Make_Identifier (Loc, Chars (Typ));
10334 end if;
10336 Qual :=
10337 Make_Qualified_Expression (Loc,
10338 Subtype_Mark => Nam,
10339 Expression => Relocate_Node (N));
10340 end if;
10341 end if;
10343 Save_Global_Descendant (Field1 (N));
10344 Save_Global_Descendant (Field2 (N));
10345 Save_Global_Descendant (Field3 (N));
10346 Save_Global_Descendant (Field5 (N));
10348 if Present (Qual) then
10349 Rewrite (N, Qual);
10350 end if;
10352 -- All other cases than aggregates
10354 else
10355 Save_Global_Descendant (Field1 (N));
10356 Save_Global_Descendant (Field2 (N));
10357 Save_Global_Descendant (Field3 (N));
10358 Save_Global_Descendant (Field4 (N));
10359 Save_Global_Descendant (Field5 (N));
10360 end if;
10361 end;
10362 end if;
10363 end Save_References;
10365 -- Start of processing for Save_Global_References
10367 begin
10368 Gen_Scope := Current_Scope;
10370 -- If the generic unit is a child unit, references to entities in
10371 -- the parent are treated as local, because they will be resolved
10372 -- anew in the context of the instance of the parent.
10374 while Is_Child_Unit (Gen_Scope)
10375 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
10376 loop
10377 Gen_Scope := Scope (Gen_Scope);
10378 end loop;
10380 Save_References (N);
10381 end Save_Global_References;
10383 --------------------------------------
10384 -- Set_Copied_Sloc_For_Inlined_Body --
10385 --------------------------------------
10387 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
10388 begin
10389 Create_Instantiation_Source (N, E, True, S_Adjustment);
10390 end Set_Copied_Sloc_For_Inlined_Body;
10392 ---------------------
10393 -- Set_Instance_Of --
10394 ---------------------
10396 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
10397 begin
10398 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
10399 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
10400 Generic_Renamings.Increment_Last;
10401 end Set_Instance_Of;
10403 --------------------
10404 -- Set_Next_Assoc --
10405 --------------------
10407 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
10408 begin
10409 Generic_Renamings.Table (E).Next_In_HTable := Next;
10410 end Set_Next_Assoc;
10412 -------------------
10413 -- Start_Generic --
10414 -------------------
10416 procedure Start_Generic is
10417 begin
10418 -- ??? I am sure more things could be factored out in this
10419 -- routine. Should probably be done at a later stage.
10421 Generic_Flags.Increment_Last;
10422 Generic_Flags.Table (Generic_Flags.Last) := Inside_A_Generic;
10423 Inside_A_Generic := True;
10425 Expander_Mode_Save_And_Set (False);
10426 end Start_Generic;
10428 ----------------------
10429 -- Set_Instance_Env --
10430 ----------------------
10432 procedure Set_Instance_Env
10433 (Gen_Unit : Entity_Id;
10434 Act_Unit : Entity_Id)
10436 begin
10437 -- Regardless of the current mode, predefined units are analyzed in
10438 -- the most current Ada mode, and earlier version Ada checks do not
10439 -- apply to predefined units.
10441 -- Why is this not using the routine Opt.Set_Opt_Config_Switches ???
10443 if Is_Internal_File_Name
10444 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
10445 Renamings_Included => True) then
10446 Ada_Version := Ada_Version_Type'Last;
10447 Ada_Version_Explicit := Ada_Version_Explicit_Config;
10448 end if;
10450 Current_Instantiated_Parent := (Gen_Unit, Act_Unit, Assoc_Null);
10451 end Set_Instance_Env;
10453 -----------------
10454 -- Switch_View --
10455 -----------------
10457 procedure Switch_View (T : Entity_Id) is
10458 BT : constant Entity_Id := Base_Type (T);
10459 Priv_Elmt : Elmt_Id := No_Elmt;
10460 Priv_Sub : Entity_Id;
10462 begin
10463 -- T may be private but its base type may have been exchanged through
10464 -- some other occurrence, in which case there is nothing to switch.
10466 if not Is_Private_Type (BT) then
10467 return;
10468 end if;
10470 Priv_Elmt := First_Elmt (Private_Dependents (BT));
10472 if Present (Full_View (BT)) then
10473 Prepend_Elmt (Full_View (BT), Exchanged_Views);
10474 Exchange_Declarations (BT);
10475 end if;
10477 while Present (Priv_Elmt) loop
10478 Priv_Sub := (Node (Priv_Elmt));
10480 -- We avoid flipping the subtype if the Etype of its full
10481 -- view is private because this would result in a malformed
10482 -- subtype. This occurs when the Etype of the subtype full
10483 -- view is the full view of the base type (and since the
10484 -- base types were just switched, the subtype is pointing
10485 -- to the wrong view). This is currently the case for
10486 -- tagged record types, access types (maybe more?) and
10487 -- needs to be resolved. ???
10489 if Present (Full_View (Priv_Sub))
10490 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
10491 then
10492 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
10493 Exchange_Declarations (Priv_Sub);
10494 end if;
10496 Next_Elmt (Priv_Elmt);
10497 end loop;
10498 end Switch_View;
10500 -----------------------------
10501 -- Valid_Default_Attribute --
10502 -----------------------------
10504 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
10505 Attr_Id : constant Attribute_Id :=
10506 Get_Attribute_Id (Attribute_Name (Def));
10507 T : constant Entity_Id := Entity (Prefix (Def));
10508 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
10509 F : Entity_Id;
10510 Num_F : Int;
10511 OK : Boolean;
10513 begin
10514 if No (T)
10515 or else T = Any_Id
10516 then
10517 return;
10518 end if;
10520 Num_F := 0;
10521 F := First_Formal (Nam);
10522 while Present (F) loop
10523 Num_F := Num_F + 1;
10524 Next_Formal (F);
10525 end loop;
10527 case Attr_Id is
10528 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
10529 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
10530 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
10531 Attribute_Unbiased_Rounding =>
10532 OK := Is_Fun
10533 and then Num_F = 1
10534 and then Is_Floating_Point_Type (T);
10536 when Attribute_Image | Attribute_Pred | Attribute_Succ |
10537 Attribute_Value | Attribute_Wide_Image |
10538 Attribute_Wide_Value =>
10539 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
10541 when Attribute_Max | Attribute_Min =>
10542 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
10544 when Attribute_Input =>
10545 OK := (Is_Fun and then Num_F = 1);
10547 when Attribute_Output | Attribute_Read | Attribute_Write =>
10548 OK := (not Is_Fun and then Num_F = 2);
10550 when others =>
10551 OK := False;
10552 end case;
10554 if not OK then
10555 Error_Msg_N ("attribute reference has wrong profile for subprogram",
10556 Def);
10557 end if;
10558 end Valid_Default_Attribute;
10560 end Sem_Ch12;