* config/mips/mips.c (function_arg): Where one part of a
[official-gcc.git] / gcc / ada / sem_ch12.adb
blob9b9313cacfcb8b1c6aa1e518aa45818d4e6da946
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 1 2 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2006, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Expander; use Expander;
32 with Fname; use Fname;
33 with Fname.UF; use Fname.UF;
34 with Freeze; use Freeze;
35 with Hostparm;
36 with Lib; use Lib;
37 with Lib.Load; use Lib.Load;
38 with Lib.Xref; use Lib.Xref;
39 with Nlists; use Nlists;
40 with Namet; use Namet;
41 with Nmake; use Nmake;
42 with Opt; use Opt;
43 with Rident; use Rident;
44 with Restrict; use Restrict;
45 with Rtsfind; use Rtsfind;
46 with Sem; use Sem;
47 with Sem_Cat; use Sem_Cat;
48 with Sem_Ch3; use Sem_Ch3;
49 with Sem_Ch6; use Sem_Ch6;
50 with Sem_Ch7; use Sem_Ch7;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Ch10; use Sem_Ch10;
53 with Sem_Ch13; use Sem_Ch13;
54 with Sem_Disp; use Sem_Disp;
55 with Sem_Elab; use Sem_Elab;
56 with Sem_Elim; use Sem_Elim;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Res; use Sem_Res;
59 with Sem_Type; use Sem_Type;
60 with Sem_Util; use Sem_Util;
61 with Sem_Warn; use Sem_Warn;
62 with Stand; use Stand;
63 with Sinfo; use Sinfo;
64 with Sinfo.CN; use Sinfo.CN;
65 with Sinput; use Sinput;
66 with Sinput.L; use Sinput.L;
67 with Snames; use Snames;
68 with Stringt; use Stringt;
69 with Uname; use Uname;
70 with Table;
71 with Tbuild; use Tbuild;
72 with Uintp; use Uintp;
73 with Urealp; use Urealp;
75 with GNAT.HTable;
77 package body Sem_Ch12 is
79 ----------------------------------------------------------
80 -- Implementation of Generic Analysis and Instantiation --
81 -----------------------------------------------------------
83 -- GNAT implements generics by macro expansion. No attempt is made to
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));
1354 Set_Limited_Present (New_N, Limited_Present (Def));
1356 else
1357 New_N :=
1358 Make_Full_Type_Declaration (Loc,
1359 Defining_Identifier => T,
1360 Discriminant_Specifications =>
1361 Discriminant_Specifications (Parent (T)),
1362 Type_Definition =>
1363 Make_Derived_Type_Definition (Loc,
1364 Subtype_Indication => Subtype_Mark (Def)));
1366 Set_Abstract_Present
1367 (Type_Definition (New_N), Abstract_Present (Def));
1368 Set_Limited_Present
1369 (Type_Definition (New_N), Limited_Present (Def));
1370 end if;
1372 Rewrite (N, New_N);
1373 Analyze (N);
1375 if Unk_Disc then
1376 if not Is_Composite_Type (T) then
1377 Error_Msg_N
1378 ("unknown discriminants not allowed for elementary types", N);
1379 else
1380 Set_Has_Unknown_Discriminants (T);
1381 Set_Is_Constrained (T, False);
1382 end if;
1383 end if;
1385 -- If the parent type has a known size, so does the formal, which
1386 -- makes legal representation clauses that involve the formal.
1388 Set_Size_Known_At_Compile_Time
1389 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
1391 end Analyze_Formal_Derived_Type;
1393 ----------------------------------
1394 -- Analyze_Formal_Discrete_Type --
1395 ----------------------------------
1397 -- The operations defined for a discrete types are those of an
1398 -- enumeration type. The size is set to an arbitrary value, for use
1399 -- in analyzing the generic unit.
1401 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
1402 Loc : constant Source_Ptr := Sloc (Def);
1403 Lo : Node_Id;
1404 Hi : Node_Id;
1406 Base : constant Entity_Id :=
1407 New_Internal_Entity
1408 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1409 begin
1410 Enter_Name (T);
1411 Set_Ekind (T, E_Enumeration_Subtype);
1412 Set_Etype (T, Base);
1413 Init_Size (T, 8);
1414 Init_Alignment (T);
1415 Set_Is_Generic_Type (T);
1416 Set_Is_Constrained (T);
1418 -- For semantic analysis, the bounds of the type must be set to some
1419 -- non-static value. The simplest is to create attribute nodes for
1420 -- those bounds, that refer to the type itself. These bounds are never
1421 -- analyzed but serve as place-holders.
1423 Lo :=
1424 Make_Attribute_Reference (Loc,
1425 Attribute_Name => Name_First,
1426 Prefix => New_Reference_To (T, Loc));
1427 Set_Etype (Lo, T);
1429 Hi :=
1430 Make_Attribute_Reference (Loc,
1431 Attribute_Name => Name_Last,
1432 Prefix => New_Reference_To (T, Loc));
1433 Set_Etype (Hi, T);
1435 Set_Scalar_Range (T,
1436 Make_Range (Loc,
1437 Low_Bound => Lo,
1438 High_Bound => Hi));
1440 Set_Ekind (Base, E_Enumeration_Type);
1441 Set_Etype (Base, Base);
1442 Init_Size (Base, 8);
1443 Init_Alignment (Base);
1444 Set_Is_Generic_Type (Base);
1445 Set_Scalar_Range (Base, Scalar_Range (T));
1446 Set_Parent (Base, Parent (Def));
1448 end Analyze_Formal_Discrete_Type;
1450 ----------------------------------
1451 -- Analyze_Formal_Floating_Type --
1452 ---------------------------------
1454 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
1455 Base : constant Entity_Id :=
1456 New_Internal_Entity
1457 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1459 begin
1460 -- The various semantic attributes are taken from the predefined type
1461 -- Float, just so that all of them are initialized. Their values are
1462 -- never used because no constant folding or expansion takes place in
1463 -- the generic itself.
1465 Enter_Name (T);
1466 Set_Ekind (T, E_Floating_Point_Subtype);
1467 Set_Etype (T, Base);
1468 Set_Size_Info (T, (Standard_Float));
1469 Set_RM_Size (T, RM_Size (Standard_Float));
1470 Set_Digits_Value (T, Digits_Value (Standard_Float));
1471 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
1472 Set_Is_Constrained (T);
1474 Set_Is_Generic_Type (Base);
1475 Set_Etype (Base, Base);
1476 Set_Size_Info (Base, (Standard_Float));
1477 Set_RM_Size (Base, RM_Size (Standard_Float));
1478 Set_Digits_Value (Base, Digits_Value (Standard_Float));
1479 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
1480 Set_Parent (Base, Parent (Def));
1482 Check_Restriction (No_Floating_Point, Def);
1483 end Analyze_Formal_Floating_Type;
1485 -----------------------------------
1486 -- Analyze_Formal_Interface_Type;--
1487 -----------------------------------
1489 procedure Analyze_Formal_Interface_Type (T : Entity_Id; Def : Node_Id) is
1490 begin
1491 Enter_Name (T);
1492 Set_Ekind (T, E_Record_Type);
1493 Set_Etype (T, T);
1494 Analyze_Interface_Declaration (T, Def);
1495 Make_Class_Wide_Type (T);
1496 Set_Primitive_Operations (T, New_Elmt_List);
1497 end Analyze_Formal_Interface_Type;
1499 ---------------------------------
1500 -- Analyze_Formal_Modular_Type --
1501 ---------------------------------
1503 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
1504 begin
1505 -- Apart from their entity kind, generic modular types are treated
1506 -- like signed integer types, and have the same attributes.
1508 Analyze_Formal_Signed_Integer_Type (T, Def);
1509 Set_Ekind (T, E_Modular_Integer_Subtype);
1510 Set_Ekind (Etype (T), E_Modular_Integer_Type);
1512 end Analyze_Formal_Modular_Type;
1514 ---------------------------------------
1515 -- Analyze_Formal_Object_Declaration --
1516 ---------------------------------------
1518 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
1519 E : constant Node_Id := Expression (N);
1520 Id : constant Node_Id := Defining_Identifier (N);
1521 K : Entity_Kind;
1522 T : Node_Id;
1524 begin
1525 Enter_Name (Id);
1527 -- Determine the mode of the formal object
1529 if Out_Present (N) then
1530 K := E_Generic_In_Out_Parameter;
1532 if not In_Present (N) then
1533 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
1534 end if;
1536 else
1537 K := E_Generic_In_Parameter;
1538 end if;
1540 Find_Type (Subtype_Mark (N));
1541 T := Entity (Subtype_Mark (N));
1543 if Ekind (T) = E_Incomplete_Type then
1544 Error_Msg_N ("premature usage of incomplete type", Subtype_Mark (N));
1545 end if;
1547 if K = E_Generic_In_Parameter then
1549 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
1551 if Ada_Version < Ada_05 and then Is_Limited_Type (T) then
1552 Error_Msg_N
1553 ("generic formal of mode IN must not be of limited type", N);
1554 Explain_Limited_Type (T, N);
1555 end if;
1557 if Is_Abstract (T) then
1558 Error_Msg_N
1559 ("generic formal of mode IN must not be of abstract type", N);
1560 end if;
1562 if Present (E) then
1563 Analyze_Per_Use_Expression (E, T);
1564 end if;
1566 Set_Ekind (Id, K);
1567 Set_Etype (Id, T);
1569 -- Case of generic IN OUT parameter
1571 else
1572 -- If the formal has an unconstrained type, construct its
1573 -- actual subtype, as is done for subprogram formals. In this
1574 -- fashion, all its uses can refer to specific bounds.
1576 Set_Ekind (Id, K);
1577 Set_Etype (Id, T);
1579 if (Is_Array_Type (T)
1580 and then not Is_Constrained (T))
1581 or else
1582 (Ekind (T) = E_Record_Type
1583 and then Has_Discriminants (T))
1584 then
1585 declare
1586 Non_Freezing_Ref : constant Node_Id :=
1587 New_Reference_To (Id, Sloc (Id));
1588 Decl : Node_Id;
1590 begin
1591 -- Make sure that the actual subtype doesn't generate
1592 -- bogus freezing.
1594 Set_Must_Not_Freeze (Non_Freezing_Ref);
1595 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
1596 Insert_Before_And_Analyze (N, Decl);
1597 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
1598 end;
1599 else
1600 Set_Actual_Subtype (Id, T);
1601 end if;
1603 if Present (E) then
1604 Error_Msg_N
1605 ("initialization not allowed for `IN OUT` formals", N);
1606 end if;
1607 end if;
1609 end Analyze_Formal_Object_Declaration;
1611 ----------------------------------------------
1612 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
1613 ----------------------------------------------
1615 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
1616 (T : Entity_Id;
1617 Def : Node_Id)
1619 Loc : constant Source_Ptr := Sloc (Def);
1620 Base : constant Entity_Id :=
1621 New_Internal_Entity
1622 (E_Ordinary_Fixed_Point_Type, Current_Scope, Sloc (Def), 'G');
1623 begin
1624 -- The semantic attributes are set for completeness only, their
1625 -- values will never be used, because all properties of the type
1626 -- are non-static.
1628 Enter_Name (T);
1629 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
1630 Set_Etype (T, Base);
1631 Set_Size_Info (T, Standard_Integer);
1632 Set_RM_Size (T, RM_Size (Standard_Integer));
1633 Set_Small_Value (T, Ureal_1);
1634 Set_Delta_Value (T, Ureal_1);
1635 Set_Scalar_Range (T,
1636 Make_Range (Loc,
1637 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1638 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1639 Set_Is_Constrained (T);
1641 Set_Is_Generic_Type (Base);
1642 Set_Etype (Base, Base);
1643 Set_Size_Info (Base, Standard_Integer);
1644 Set_RM_Size (Base, RM_Size (Standard_Integer));
1645 Set_Small_Value (Base, Ureal_1);
1646 Set_Delta_Value (Base, Ureal_1);
1647 Set_Scalar_Range (Base, Scalar_Range (T));
1648 Set_Parent (Base, Parent (Def));
1650 Check_Restriction (No_Fixed_Point, Def);
1651 end Analyze_Formal_Ordinary_Fixed_Point_Type;
1653 ----------------------------
1654 -- Analyze_Formal_Package --
1655 ----------------------------
1657 procedure Analyze_Formal_Package (N : Node_Id) is
1658 Loc : constant Source_Ptr := Sloc (N);
1659 Pack_Id : constant Entity_Id := Defining_Identifier (N);
1660 Formal : Entity_Id;
1661 Gen_Id : constant Node_Id := Name (N);
1662 Gen_Decl : Node_Id;
1663 Gen_Unit : Entity_Id;
1664 New_N : Node_Id;
1665 Parent_Installed : Boolean := False;
1666 Renaming : Node_Id;
1667 Parent_Instance : Entity_Id;
1668 Renaming_In_Par : Entity_Id;
1670 begin
1671 Text_IO_Kludge (Gen_Id);
1673 Init_Env;
1674 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
1675 Gen_Unit := Entity (Gen_Id);
1677 -- Check for a formal package that is a package renaming
1679 if Present (Renamed_Object (Gen_Unit)) then
1680 Gen_Unit := Renamed_Object (Gen_Unit);
1681 end if;
1683 if Ekind (Gen_Unit) /= E_Generic_Package then
1684 Error_Msg_N ("expect generic package name", Gen_Id);
1685 Restore_Env;
1686 return;
1688 elsif Gen_Unit = Current_Scope then
1689 Error_Msg_N
1690 ("generic package cannot be used as a formal package of itself",
1691 Gen_Id);
1692 Restore_Env;
1693 return;
1695 elsif In_Open_Scopes (Gen_Unit) then
1696 if Is_Compilation_Unit (Gen_Unit)
1697 and then Is_Child_Unit (Current_Scope)
1698 then
1699 -- Special-case the error when the formal is a parent, and
1700 -- continue analysis to minimize cascaded errors.
1702 Error_Msg_N
1703 ("generic parent cannot be used as formal package "
1704 & "of a child unit",
1705 Gen_Id);
1707 else
1708 Error_Msg_N
1709 ("generic package cannot be used as a formal package "
1710 & "within itself",
1711 Gen_Id);
1712 Restore_Env;
1713 return;
1714 end if;
1715 end if;
1717 -- The formal package is treated like a regular instance, but only
1718 -- the specification needs to be instantiated, to make entities visible.
1720 if not Box_Present (N) then
1721 Hidden_Entities := New_Elmt_List;
1722 Analyze_Package_Instantiation (N);
1724 if Parent_Installed then
1725 Remove_Parent;
1726 end if;
1728 else
1729 -- If there are no generic associations, the generic parameters
1730 -- appear as local entities and are instantiated like them. We copy
1731 -- the generic package declaration as if it were an instantiation,
1732 -- and analyze it like a regular package, except that we treat the
1733 -- formals as additional visible components.
1735 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
1737 if In_Extended_Main_Source_Unit (N) then
1738 Set_Is_Instantiated (Gen_Unit);
1739 Generate_Reference (Gen_Unit, N);
1740 end if;
1742 Formal := New_Copy (Pack_Id);
1743 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
1745 New_N :=
1746 Copy_Generic_Node
1747 (Original_Node (Gen_Decl), Empty, Instantiating => True);
1748 Rewrite (N, New_N);
1749 Set_Defining_Unit_Name (Specification (New_N), Formal);
1750 Set_Generic_Parent (Specification (N), Gen_Unit);
1751 Set_Instance_Env (Gen_Unit, Formal);
1753 Enter_Name (Formal);
1754 Set_Ekind (Formal, E_Generic_Package);
1755 Set_Etype (Formal, Standard_Void_Type);
1756 Set_Inner_Instances (Formal, New_Elmt_List);
1757 New_Scope (Formal);
1759 -- Within the formal, the name of the generic package is a renaming
1760 -- of the formal (as for a regular instantiation).
1762 Renaming := Make_Package_Renaming_Declaration (Loc,
1763 Defining_Unit_Name =>
1764 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
1765 Name => New_Reference_To (Formal, Loc));
1767 if Present (Visible_Declarations (Specification (N))) then
1768 Prepend (Renaming, To => Visible_Declarations (Specification (N)));
1769 elsif Present (Private_Declarations (Specification (N))) then
1770 Prepend (Renaming, To => Private_Declarations (Specification (N)));
1771 end if;
1773 if Is_Child_Unit (Gen_Unit)
1774 and then Parent_Installed
1775 then
1776 -- Similarly, we have to make the name of the formal visible in
1777 -- the parent instance, to resolve properly fully qualified names
1778 -- that may appear in the generic unit. The parent instance has
1779 -- been placed on the scope stack ahead of the current scope.
1781 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
1783 Renaming_In_Par :=
1784 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
1785 Set_Ekind (Renaming_In_Par, E_Package);
1786 Set_Etype (Renaming_In_Par, Standard_Void_Type);
1787 Set_Scope (Renaming_In_Par, Parent_Instance);
1788 Set_Parent (Renaming_In_Par, Parent (Formal));
1789 Set_Renamed_Object (Renaming_In_Par, Formal);
1790 Append_Entity (Renaming_In_Par, Parent_Instance);
1791 end if;
1793 Analyze_Generic_Formal_Part (N);
1794 Analyze (Specification (N));
1795 End_Package_Scope (Formal);
1797 if Parent_Installed then
1798 Remove_Parent;
1799 end if;
1801 Restore_Env;
1803 -- Inside the generic unit, the formal package is a regular
1804 -- package, but no body is needed for it. Note that after
1805 -- instantiation, the defining_unit_name we need is in the
1806 -- new tree and not in the original. (see Package_Instantiation).
1807 -- A generic formal package is an instance, and can be used as
1808 -- an actual for an inner instance.
1810 Set_Ekind (Formal, E_Package);
1811 Set_Has_Completion (Formal, True);
1813 Set_Ekind (Pack_Id, E_Package);
1814 Set_Etype (Pack_Id, Standard_Void_Type);
1815 Set_Scope (Pack_Id, Scope (Formal));
1816 Set_Has_Completion (Pack_Id, True);
1817 end if;
1818 end Analyze_Formal_Package;
1820 ---------------------------------
1821 -- Analyze_Formal_Private_Type --
1822 ---------------------------------
1824 procedure Analyze_Formal_Private_Type
1825 (N : Node_Id;
1826 T : Entity_Id;
1827 Def : Node_Id)
1829 begin
1830 New_Private_Type (N, T, Def);
1832 -- Set the size to an arbitrary but legal value
1834 Set_Size_Info (T, Standard_Integer);
1835 Set_RM_Size (T, RM_Size (Standard_Integer));
1836 end Analyze_Formal_Private_Type;
1838 ----------------------------------------
1839 -- Analyze_Formal_Signed_Integer_Type --
1840 ----------------------------------------
1842 procedure Analyze_Formal_Signed_Integer_Type
1843 (T : Entity_Id;
1844 Def : Node_Id)
1846 Base : constant Entity_Id :=
1847 New_Internal_Entity
1848 (E_Signed_Integer_Type, Current_Scope, Sloc (Def), 'G');
1850 begin
1851 Enter_Name (T);
1853 Set_Ekind (T, E_Signed_Integer_Subtype);
1854 Set_Etype (T, Base);
1855 Set_Size_Info (T, Standard_Integer);
1856 Set_RM_Size (T, RM_Size (Standard_Integer));
1857 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
1858 Set_Is_Constrained (T);
1860 Set_Is_Generic_Type (Base);
1861 Set_Size_Info (Base, Standard_Integer);
1862 Set_RM_Size (Base, RM_Size (Standard_Integer));
1863 Set_Etype (Base, Base);
1864 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
1865 Set_Parent (Base, Parent (Def));
1866 end Analyze_Formal_Signed_Integer_Type;
1868 -------------------------------
1869 -- Analyze_Formal_Subprogram --
1870 -------------------------------
1872 procedure Analyze_Formal_Subprogram (N : Node_Id) is
1873 Spec : constant Node_Id := Specification (N);
1874 Def : constant Node_Id := Default_Name (N);
1875 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
1876 Subp : Entity_Id;
1878 begin
1879 if Nam = Error then
1880 return;
1881 end if;
1883 if Nkind (Nam) = N_Defining_Program_Unit_Name then
1884 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
1885 return;
1886 end if;
1888 Analyze_Subprogram_Declaration (N);
1889 Set_Is_Formal_Subprogram (Nam);
1890 Set_Has_Completion (Nam);
1892 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
1893 Set_Is_Abstract (Nam);
1894 Set_Is_Dispatching_Operation (Nam);
1896 declare
1897 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
1899 begin
1900 if No (Ctrl_Type) then
1901 Error_Msg_N
1902 ("abstract formal subprogram must have a controlling type",
1905 else
1906 Check_Controlling_Formals (Ctrl_Type, Nam);
1907 end if;
1908 end;
1909 end if;
1911 -- Default name is resolved at the point of instantiation
1913 if Box_Present (N) then
1914 null;
1916 -- Else default is bound at the point of generic declaration
1918 elsif Present (Def) then
1919 if Nkind (Def) = N_Operator_Symbol then
1920 Find_Direct_Name (Def);
1922 elsif Nkind (Def) /= N_Attribute_Reference then
1923 Analyze (Def);
1925 else
1926 -- For an attribute reference, analyze the prefix and verify
1927 -- that it has the proper profile for the subprogram.
1929 Analyze (Prefix (Def));
1930 Valid_Default_Attribute (Nam, Def);
1931 return;
1932 end if;
1934 -- Default name may be overloaded, in which case the interpretation
1935 -- with the correct profile must be selected, as for a renaming.
1937 if Etype (Def) = Any_Type then
1938 return;
1940 elsif Nkind (Def) = N_Selected_Component then
1941 Subp := Entity (Selector_Name (Def));
1943 if Ekind (Subp) /= E_Entry then
1944 Error_Msg_N ("expect valid subprogram name as default", Def);
1945 return;
1946 end if;
1948 elsif Nkind (Def) = N_Indexed_Component then
1950 if Nkind (Prefix (Def)) /= N_Selected_Component then
1951 Error_Msg_N ("expect valid subprogram name as default", Def);
1952 return;
1954 else
1955 Subp := Entity (Selector_Name (Prefix (Def)));
1957 if Ekind (Subp) /= E_Entry_Family then
1958 Error_Msg_N ("expect valid subprogram name as default", Def);
1959 return;
1960 end if;
1961 end if;
1963 elsif Nkind (Def) = N_Character_Literal then
1965 -- Needs some type checks: subprogram should be parameterless???
1967 Resolve (Def, (Etype (Nam)));
1969 elsif not Is_Entity_Name (Def)
1970 or else not Is_Overloadable (Entity (Def))
1971 then
1972 Error_Msg_N ("expect valid subprogram name as default", Def);
1973 return;
1975 elsif not Is_Overloaded (Def) then
1976 Subp := Entity (Def);
1978 if Subp = Nam then
1979 Error_Msg_N ("premature usage of formal subprogram", Def);
1981 elsif not Entity_Matches_Spec (Subp, Nam) then
1982 Error_Msg_N ("no visible entity matches specification", Def);
1983 end if;
1985 else
1986 declare
1987 I : Interp_Index;
1988 I1 : Interp_Index := 0;
1989 It : Interp;
1990 It1 : Interp;
1992 begin
1993 Subp := Any_Id;
1994 Get_First_Interp (Def, I, It);
1995 while Present (It.Nam) loop
1997 if Entity_Matches_Spec (It.Nam, Nam) then
1998 if Subp /= Any_Id then
1999 It1 := Disambiguate (Def, I1, I, Etype (Subp));
2001 if It1 = No_Interp then
2002 Error_Msg_N ("ambiguous default subprogram", Def);
2003 else
2004 Subp := It1.Nam;
2005 end if;
2007 exit;
2009 else
2010 I1 := I;
2011 Subp := It.Nam;
2012 end if;
2013 end if;
2015 Get_Next_Interp (I, It);
2016 end loop;
2017 end;
2019 if Subp /= Any_Id then
2020 Set_Entity (Def, Subp);
2022 if Subp = Nam then
2023 Error_Msg_N ("premature usage of formal subprogram", Def);
2025 elsif Ekind (Subp) /= E_Operator then
2026 Check_Mode_Conformant (Subp, Nam);
2027 end if;
2029 else
2030 Error_Msg_N ("no visible subprogram matches specification", N);
2031 end if;
2032 end if;
2033 end if;
2034 end Analyze_Formal_Subprogram;
2036 -------------------------------------
2037 -- Analyze_Formal_Type_Declaration --
2038 -------------------------------------
2040 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
2041 Def : constant Node_Id := Formal_Type_Definition (N);
2042 T : Entity_Id;
2044 begin
2045 T := Defining_Identifier (N);
2047 if Present (Discriminant_Specifications (N))
2048 and then Nkind (Def) /= N_Formal_Private_Type_Definition
2049 then
2050 Error_Msg_N
2051 ("discriminants not allowed for this formal type",
2052 Defining_Identifier (First (Discriminant_Specifications (N))));
2053 end if;
2055 -- Enter the new name, and branch to specific routine
2057 case Nkind (Def) is
2058 when N_Formal_Private_Type_Definition =>
2059 Analyze_Formal_Private_Type (N, T, Def);
2061 when N_Formal_Derived_Type_Definition =>
2062 Analyze_Formal_Derived_Type (N, T, Def);
2064 when N_Formal_Discrete_Type_Definition =>
2065 Analyze_Formal_Discrete_Type (T, Def);
2067 when N_Formal_Signed_Integer_Type_Definition =>
2068 Analyze_Formal_Signed_Integer_Type (T, Def);
2070 when N_Formal_Modular_Type_Definition =>
2071 Analyze_Formal_Modular_Type (T, Def);
2073 when N_Formal_Floating_Point_Definition =>
2074 Analyze_Formal_Floating_Type (T, Def);
2076 when N_Formal_Ordinary_Fixed_Point_Definition =>
2077 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
2079 when N_Formal_Decimal_Fixed_Point_Definition =>
2080 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
2082 when N_Array_Type_Definition =>
2083 Analyze_Formal_Array_Type (T, Def);
2085 when N_Access_To_Object_Definition |
2086 N_Access_Function_Definition |
2087 N_Access_Procedure_Definition =>
2088 Analyze_Generic_Access_Type (T, Def);
2090 -- Ada 2005: a interface declaration is encoded as an abstract
2091 -- record declaration or a abstract type derivation.
2093 when N_Record_Definition =>
2094 Analyze_Formal_Interface_Type (T, Def);
2096 when N_Derived_Type_Definition =>
2097 Analyze_Formal_Derived_Interface_Type (T, Def);
2099 when N_Error =>
2100 null;
2102 when others =>
2103 raise Program_Error;
2105 end case;
2107 Set_Is_Generic_Type (T);
2108 end Analyze_Formal_Type_Declaration;
2110 ------------------------------------
2111 -- Analyze_Function_Instantiation --
2112 ------------------------------------
2114 procedure Analyze_Function_Instantiation (N : Node_Id) is
2115 begin
2116 Analyze_Subprogram_Instantiation (N, E_Function);
2117 end Analyze_Function_Instantiation;
2119 ---------------------------------
2120 -- Analyze_Generic_Access_Type --
2121 ---------------------------------
2123 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
2124 begin
2125 Enter_Name (T);
2127 if Nkind (Def) = N_Access_To_Object_Definition then
2128 Access_Type_Declaration (T, Def);
2130 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
2131 and then No (Full_View (Designated_Type (T)))
2132 and then not Is_Generic_Type (Designated_Type (T))
2133 then
2134 Error_Msg_N ("premature usage of incomplete type", Def);
2136 elsif Is_Internal (Designated_Type (T)) then
2137 Error_Msg_N
2138 ("only a subtype mark is allowed in a formal", Def);
2139 end if;
2141 else
2142 Access_Subprogram_Declaration (T, Def);
2143 end if;
2144 end Analyze_Generic_Access_Type;
2146 ---------------------------------
2147 -- Analyze_Generic_Formal_Part --
2148 ---------------------------------
2150 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
2151 Gen_Parm_Decl : Node_Id;
2153 begin
2154 -- The generic formals are processed in the scope of the generic
2155 -- unit, where they are immediately visible. The scope is installed
2156 -- by the caller.
2158 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
2160 while Present (Gen_Parm_Decl) loop
2161 Analyze (Gen_Parm_Decl);
2162 Next (Gen_Parm_Decl);
2163 end loop;
2165 Generate_Reference_To_Generic_Formals (Current_Scope);
2166 end Analyze_Generic_Formal_Part;
2168 ------------------------------------------
2169 -- Analyze_Generic_Package_Declaration --
2170 ------------------------------------------
2172 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
2173 Loc : constant Source_Ptr := Sloc (N);
2174 Id : Entity_Id;
2175 New_N : Node_Id;
2176 Save_Parent : Node_Id;
2177 Renaming : Node_Id;
2178 Decls : constant List_Id :=
2179 Visible_Declarations (Specification (N));
2180 Decl : Node_Id;
2182 begin
2183 -- We introduce a renaming of the enclosing package, to have a usable
2184 -- entity as the prefix of an expanded name for a local entity of the
2185 -- form Par.P.Q, where P is the generic package. This is because a local
2186 -- entity named P may hide it, so that the usual visibility rules in
2187 -- the instance will not resolve properly.
2189 Renaming :=
2190 Make_Package_Renaming_Declaration (Loc,
2191 Defining_Unit_Name =>
2192 Make_Defining_Identifier (Loc,
2193 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
2194 Name => Make_Identifier (Loc, Chars (Defining_Entity (N))));
2196 if Present (Decls) then
2197 Decl := First (Decls);
2198 while Present (Decl)
2199 and then Nkind (Decl) = N_Pragma
2200 loop
2201 Next (Decl);
2202 end loop;
2204 if Present (Decl) then
2205 Insert_Before (Decl, Renaming);
2206 else
2207 Append (Renaming, Visible_Declarations (Specification (N)));
2208 end if;
2210 else
2211 Set_Visible_Declarations (Specification (N), New_List (Renaming));
2212 end if;
2214 -- Create copy of generic unit, and save for instantiation.
2215 -- If the unit is a child unit, do not copy the specifications
2216 -- for the parent, which are not part of the generic tree.
2218 Save_Parent := Parent_Spec (N);
2219 Set_Parent_Spec (N, Empty);
2221 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2222 Set_Parent_Spec (New_N, Save_Parent);
2223 Rewrite (N, New_N);
2224 Id := Defining_Entity (N);
2225 Generate_Definition (Id);
2227 -- Expansion is not applied to generic units
2229 Start_Generic;
2231 Enter_Name (Id);
2232 Set_Ekind (Id, E_Generic_Package);
2233 Set_Etype (Id, Standard_Void_Type);
2234 New_Scope (Id);
2235 Enter_Generic_Scope (Id);
2236 Set_Inner_Instances (Id, New_Elmt_List);
2238 Set_Categorization_From_Pragmas (N);
2239 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2241 -- Link the declaration of the generic homonym in the generic copy
2242 -- to the package it renames, so that it is always resolved properly.
2244 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
2245 Set_Entity (Associated_Node (Name (Renaming)), Id);
2247 -- For a library unit, we have reconstructed the entity for the
2248 -- unit, and must reset it in the library tables.
2250 if Nkind (Parent (N)) = N_Compilation_Unit then
2251 Set_Cunit_Entity (Current_Sem_Unit, Id);
2252 end if;
2254 Analyze_Generic_Formal_Part (N);
2256 -- After processing the generic formals, analysis proceeds
2257 -- as for a non-generic package.
2259 Analyze (Specification (N));
2261 Validate_Categorization_Dependency (N, Id);
2263 End_Generic;
2265 End_Package_Scope (Id);
2266 Exit_Generic_Scope (Id);
2268 if Nkind (Parent (N)) /= N_Compilation_Unit then
2269 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
2270 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
2271 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
2273 else
2274 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2275 Validate_RT_RAT_Component (N);
2277 -- If this is a spec without a body, check that generic parameters
2278 -- are referenced.
2280 if not Body_Required (Parent (N)) then
2281 Check_References (Id);
2282 end if;
2283 end if;
2284 end Analyze_Generic_Package_Declaration;
2286 --------------------------------------------
2287 -- Analyze_Generic_Subprogram_Declaration --
2288 --------------------------------------------
2290 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
2291 Spec : Node_Id;
2292 Id : Entity_Id;
2293 Formals : List_Id;
2294 New_N : Node_Id;
2295 Result_Type : Entity_Id;
2296 Save_Parent : Node_Id;
2298 begin
2299 -- Create copy of generic unit,and save for instantiation.
2300 -- If the unit is a child unit, do not copy the specifications
2301 -- for the parent, which are not part of the generic tree.
2303 Save_Parent := Parent_Spec (N);
2304 Set_Parent_Spec (N, Empty);
2306 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2307 Set_Parent_Spec (New_N, Save_Parent);
2308 Rewrite (N, New_N);
2310 Spec := Specification (N);
2311 Id := Defining_Entity (Spec);
2312 Generate_Definition (Id);
2314 if Nkind (Id) = N_Defining_Operator_Symbol then
2315 Error_Msg_N
2316 ("operator symbol not allowed for generic subprogram", Id);
2317 end if;
2319 Start_Generic;
2321 Enter_Name (Id);
2323 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
2324 New_Scope (Id);
2325 Enter_Generic_Scope (Id);
2326 Set_Inner_Instances (Id, New_Elmt_List);
2327 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2329 Analyze_Generic_Formal_Part (N);
2331 Formals := Parameter_Specifications (Spec);
2333 if Present (Formals) then
2334 Process_Formals (Formals, Spec);
2335 end if;
2337 if Nkind (Spec) = N_Function_Specification then
2338 Set_Ekind (Id, E_Generic_Function);
2340 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
2341 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
2342 Set_Etype (Id, Result_Type);
2343 else
2344 Find_Type (Result_Definition (Spec));
2345 Set_Etype (Id, Entity (Result_Definition (Spec)));
2346 end if;
2348 else
2349 Set_Ekind (Id, E_Generic_Procedure);
2350 Set_Etype (Id, Standard_Void_Type);
2351 end if;
2353 -- For a library unit, we have reconstructed the entity for the unit,
2354 -- and must reset it in the library tables. We also make sure that
2355 -- Body_Required is set properly in the original compilation unit node.
2357 if Nkind (Parent (N)) = N_Compilation_Unit then
2358 Set_Cunit_Entity (Current_Sem_Unit, Id);
2359 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2360 end if;
2362 Set_Categorization_From_Pragmas (N);
2363 Validate_Categorization_Dependency (N, Id);
2365 Save_Global_References (Original_Node (N));
2367 End_Generic;
2368 End_Scope;
2369 Exit_Generic_Scope (Id);
2370 Generate_Reference_To_Formals (Id);
2371 end Analyze_Generic_Subprogram_Declaration;
2373 -----------------------------------
2374 -- Analyze_Package_Instantiation --
2375 -----------------------------------
2377 -- Note: this procedure is also used for formal package declarations, in
2378 -- which case the argument N is an N_Formal_Package_Declaration node.
2379 -- This should really be noted in the spec! ???
2381 procedure Analyze_Package_Instantiation (N : Node_Id) is
2382 Loc : constant Source_Ptr := Sloc (N);
2383 Gen_Id : constant Node_Id := Name (N);
2385 Act_Decl : Node_Id;
2386 Act_Decl_Name : Node_Id;
2387 Act_Decl_Id : Entity_Id;
2388 Act_Spec : Node_Id;
2389 Act_Tree : Node_Id;
2391 Gen_Decl : Node_Id;
2392 Gen_Unit : Entity_Id;
2394 Is_Actual_Pack : constant Boolean :=
2395 Is_Internal (Defining_Entity (N));
2397 Env_Installed : Boolean := False;
2398 Parent_Installed : Boolean := False;
2399 Renaming_List : List_Id;
2400 Unit_Renaming : Node_Id;
2401 Needs_Body : Boolean;
2402 Inline_Now : Boolean := False;
2404 procedure Delay_Descriptors (E : Entity_Id);
2405 -- Delay generation of subprogram descriptors for given entity
2407 function Might_Inline_Subp return Boolean;
2408 -- If inlining is active and the generic contains inlined subprograms,
2409 -- we instantiate the body. This may cause superfluous instantiations,
2410 -- but it is simpler than detecting the need for the body at the point
2411 -- of inlining, when the context of the instance is not available.
2413 -----------------------
2414 -- Delay_Descriptors --
2415 -----------------------
2417 procedure Delay_Descriptors (E : Entity_Id) is
2418 begin
2419 if not Delay_Subprogram_Descriptors (E) then
2420 Set_Delay_Subprogram_Descriptors (E);
2421 Pending_Descriptor.Increment_Last;
2422 Pending_Descriptor.Table (Pending_Descriptor.Last) := E;
2423 end if;
2424 end Delay_Descriptors;
2426 -----------------------
2427 -- Might_Inline_Subp --
2428 -----------------------
2430 function Might_Inline_Subp return Boolean is
2431 E : Entity_Id;
2433 begin
2434 if not Inline_Processing_Required then
2435 return False;
2437 else
2438 E := First_Entity (Gen_Unit);
2439 while Present (E) loop
2440 if Is_Subprogram (E)
2441 and then Is_Inlined (E)
2442 then
2443 return True;
2444 end if;
2446 Next_Entity (E);
2447 end loop;
2448 end if;
2450 return False;
2451 end Might_Inline_Subp;
2453 -- Start of processing for Analyze_Package_Instantiation
2455 begin
2456 -- Very first thing: apply the special kludge for Text_IO processing
2457 -- in case we are instantiating one of the children of [Wide_]Text_IO.
2459 Text_IO_Kludge (Name (N));
2461 -- Make node global for error reporting
2463 Instantiation_Node := N;
2465 -- Case of instantiation of a generic package
2467 if Nkind (N) = N_Package_Instantiation then
2468 Act_Decl_Id := New_Copy (Defining_Entity (N));
2469 Set_Comes_From_Source (Act_Decl_Id, True);
2471 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
2472 Act_Decl_Name :=
2473 Make_Defining_Program_Unit_Name (Loc,
2474 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
2475 Defining_Identifier => Act_Decl_Id);
2476 else
2477 Act_Decl_Name := Act_Decl_Id;
2478 end if;
2480 -- Case of instantiation of a formal package
2482 else
2483 Act_Decl_Id := Defining_Identifier (N);
2484 Act_Decl_Name := Act_Decl_Id;
2485 end if;
2487 Generate_Definition (Act_Decl_Id);
2488 Pre_Analyze_Actuals (N);
2490 Init_Env;
2491 Env_Installed := True;
2492 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2493 Gen_Unit := Entity (Gen_Id);
2495 -- Verify that it is the name of a generic package
2497 if Etype (Gen_Unit) = Any_Type then
2498 Restore_Env;
2499 return;
2501 elsif Ekind (Gen_Unit) /= E_Generic_Package then
2503 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
2505 if From_With_Type (Gen_Unit) then
2506 Error_Msg_N
2507 ("cannot instantiate a limited withed package", Gen_Id);
2508 else
2509 Error_Msg_N
2510 ("expect name of generic package in instantiation", Gen_Id);
2511 end if;
2513 Restore_Env;
2514 return;
2515 end if;
2517 if In_Extended_Main_Source_Unit (N) then
2518 Set_Is_Instantiated (Gen_Unit);
2519 Generate_Reference (Gen_Unit, N);
2521 if Present (Renamed_Object (Gen_Unit)) then
2522 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
2523 Generate_Reference (Renamed_Object (Gen_Unit), N);
2524 end if;
2525 end if;
2527 if Nkind (Gen_Id) = N_Identifier
2528 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
2529 then
2530 Error_Msg_NE
2531 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2533 elsif Nkind (Gen_Id) = N_Expanded_Name
2534 and then Is_Child_Unit (Gen_Unit)
2535 and then Nkind (Prefix (Gen_Id)) = N_Identifier
2536 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
2537 then
2538 Error_Msg_N
2539 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
2540 end if;
2542 Set_Entity (Gen_Id, Gen_Unit);
2544 -- If generic is a renaming, get original generic unit
2546 if Present (Renamed_Object (Gen_Unit))
2547 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
2548 then
2549 Gen_Unit := Renamed_Object (Gen_Unit);
2550 end if;
2552 -- Verify that there are no circular instantiations
2554 if In_Open_Scopes (Gen_Unit) then
2555 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
2556 Restore_Env;
2557 return;
2559 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
2560 Error_Msg_Node_2 := Current_Scope;
2561 Error_Msg_NE
2562 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
2563 Circularity_Detected := True;
2564 Restore_Env;
2565 return;
2567 else
2568 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
2569 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2571 -- Initialize renamings map, for error checking, and the list
2572 -- that holds private entities whose views have changed between
2573 -- generic definition and instantiation. If this is the instance
2574 -- created to validate an actual package, the instantiation
2575 -- environment is that of the enclosing instance.
2577 Generic_Renamings.Set_Last (0);
2578 Generic_Renamings_HTable.Reset;
2580 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2582 -- Copy original generic tree, to produce text for instantiation
2584 Act_Tree :=
2585 Copy_Generic_Node
2586 (Original_Node (Gen_Decl), Empty, Instantiating => True);
2588 Act_Spec := Specification (Act_Tree);
2590 -- If this is the instance created to validate an actual package,
2591 -- only the formals matter, do not examine the package spec itself.
2593 if Is_Actual_Pack then
2594 Set_Visible_Declarations (Act_Spec, New_List);
2595 Set_Private_Declarations (Act_Spec, New_List);
2596 end if;
2598 Renaming_List :=
2599 Analyze_Associations
2601 Generic_Formal_Declarations (Act_Tree),
2602 Generic_Formal_Declarations (Gen_Decl));
2604 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
2605 Set_Is_Generic_Instance (Act_Decl_Id);
2607 Set_Generic_Parent (Act_Spec, Gen_Unit);
2609 -- References to the generic in its own declaration or its body
2610 -- are references to the instance. Add a renaming declaration for
2611 -- the generic unit itself. This declaration, as well as the renaming
2612 -- declarations for the generic formals, must remain private to the
2613 -- unit: the formals, because this is the language semantics, and
2614 -- the unit because its use is an artifact of the implementation.
2616 Unit_Renaming :=
2617 Make_Package_Renaming_Declaration (Loc,
2618 Defining_Unit_Name =>
2619 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2620 Name => New_Reference_To (Act_Decl_Id, Loc));
2622 Append (Unit_Renaming, Renaming_List);
2624 -- The renaming declarations are the first local declarations of
2625 -- the new unit.
2627 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
2628 Insert_List_Before
2629 (First (Visible_Declarations (Act_Spec)), Renaming_List);
2630 else
2631 Set_Visible_Declarations (Act_Spec, Renaming_List);
2632 end if;
2634 Act_Decl :=
2635 Make_Package_Declaration (Loc,
2636 Specification => Act_Spec);
2638 -- Save the instantiation node, for subsequent instantiation
2639 -- of the body, if there is one and we are generating code for
2640 -- the current unit. Mark the unit as having a body, to avoid
2641 -- a premature error message.
2643 -- We instantiate the body if we are generating code, if we are
2644 -- generating cross-reference information, or if we are building
2645 -- trees for ASIS use.
2647 declare
2648 Enclosing_Body_Present : Boolean := False;
2649 -- If the generic unit is not a compilation unit, then a body
2650 -- may be present in its parent even if none is required. We
2651 -- create a tentative pending instantiation for the body, which
2652 -- will be discarded if none is actually present.
2654 Scop : Entity_Id;
2656 begin
2657 if Scope (Gen_Unit) /= Standard_Standard
2658 and then not Is_Child_Unit (Gen_Unit)
2659 then
2660 Scop := Scope (Gen_Unit);
2662 while Present (Scop)
2663 and then Scop /= Standard_Standard
2664 loop
2665 if Unit_Requires_Body (Scop) then
2666 Enclosing_Body_Present := True;
2667 exit;
2669 elsif In_Open_Scopes (Scop)
2670 and then In_Package_Body (Scop)
2671 then
2672 Enclosing_Body_Present := True;
2673 exit;
2674 end if;
2676 exit when Is_Compilation_Unit (Scop);
2677 Scop := Scope (Scop);
2678 end loop;
2679 end if;
2681 -- If front-end inlining is enabled, and this is a unit for which
2682 -- code will be generated, we instantiate the body at once.
2683 -- This is done if the instance is not the main unit, and if the
2684 -- generic is not a child unit of another generic, to avoid scope
2685 -- problems and the reinstallation of parent instances.
2687 if Expander_Active
2688 and then (not Is_Child_Unit (Gen_Unit)
2689 or else not Is_Generic_Unit (Scope (Gen_Unit)))
2690 and then Might_Inline_Subp
2691 and then not Is_Actual_Pack
2692 then
2693 if Front_End_Inlining
2694 and then (Is_In_Main_Unit (N)
2695 or else In_Main_Context (Current_Scope))
2696 and then Nkind (Parent (N)) /= N_Compilation_Unit
2697 then
2698 Inline_Now := True;
2700 -- In configurable_run_time mode we force the inlining of
2701 -- predefined subprogram marked Inline_Always, to minimize
2702 -- the use of the run-time library.
2704 elsif Is_Predefined_File_Name
2705 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
2706 and then Configurable_Run_Time_Mode
2707 and then Nkind (Parent (N)) /= N_Compilation_Unit
2708 then
2709 Inline_Now := True;
2710 end if;
2712 -- If the current scope is itself an instance within a child
2713 -- unit,there will be duplications in the scope stack, and the
2714 -- unstacking mechanism in Inline_Instance_Body will fail.
2715 -- This loses some rare cases of optimization, and might be
2716 -- improved some day, if we can find a proper abstraction for
2717 -- "the complete compilation context" that can be saved and
2718 -- restored ???
2720 if Is_Generic_Instance (Current_Scope) then
2721 declare
2722 Curr_Unit : constant Entity_Id :=
2723 Cunit_Entity (Current_Sem_Unit);
2724 begin
2725 if Curr_Unit /= Current_Scope
2726 and then Is_Child_Unit (Curr_Unit)
2727 then
2728 Inline_Now := False;
2729 end if;
2730 end;
2731 end if;
2732 end if;
2734 Needs_Body :=
2735 (Unit_Requires_Body (Gen_Unit)
2736 or else Enclosing_Body_Present
2737 or else Present (Corresponding_Body (Gen_Decl)))
2738 and then (Is_In_Main_Unit (N)
2739 or else Might_Inline_Subp)
2740 and then not Is_Actual_Pack
2741 and then not Inline_Now
2742 and then (Operating_Mode = Generate_Code
2743 or else (Operating_Mode = Check_Semantics
2744 and then ASIS_Mode));
2746 -- If front_end_inlining is enabled, do not instantiate a
2747 -- body if within a generic context.
2749 if (Front_End_Inlining
2750 and then not Expander_Active)
2751 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
2752 then
2753 Needs_Body := False;
2754 end if;
2756 -- If the current context is generic, and the package being
2757 -- instantiated is declared within a formal package, there is no
2758 -- body to instantiate until the enclosing generic is instantiated
2759 -- and there is an actual for the formal package. If the formal
2760 -- package has parameters, we build regular package instance for
2761 -- it, that preceeds the original formal package declaration.
2763 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
2764 declare
2765 Decl : constant Node_Id :=
2766 Original_Node
2767 (Unit_Declaration_Node (Scope (Gen_Unit)));
2768 begin
2769 if Nkind (Decl) = N_Formal_Package_Declaration
2770 or else (Nkind (Decl) = N_Package_Declaration
2771 and then Is_List_Member (Decl)
2772 and then Present (Next (Decl))
2773 and then
2774 Nkind (Next (Decl)) = N_Formal_Package_Declaration)
2775 then
2776 Needs_Body := False;
2777 end if;
2778 end;
2779 end if;
2780 end;
2782 -- If we are generating the calling stubs from the instantiation of
2783 -- a generic RCI package, we will not use the body of the generic
2784 -- package.
2786 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
2787 and then Is_Compilation_Unit (Defining_Entity (N))
2788 then
2789 Needs_Body := False;
2790 end if;
2792 if Needs_Body then
2794 -- Here is a defence against a ludicrous number of instantiations
2795 -- caused by a circular set of instantiation attempts.
2797 if Pending_Instantiations.Last >
2798 Hostparm.Max_Instantiations
2799 then
2800 Error_Msg_N ("too many instantiations", N);
2801 raise Unrecoverable_Error;
2802 end if;
2804 -- Indicate that the enclosing scopes contain an instantiation,
2805 -- and that cleanup actions should be delayed until after the
2806 -- instance body is expanded.
2808 Check_Forward_Instantiation (Gen_Decl);
2809 if Nkind (N) = N_Package_Instantiation then
2810 declare
2811 Enclosing_Master : Entity_Id := Current_Scope;
2813 begin
2814 while Enclosing_Master /= Standard_Standard loop
2816 if Ekind (Enclosing_Master) = E_Package then
2817 if Is_Compilation_Unit (Enclosing_Master) then
2818 if In_Package_Body (Enclosing_Master) then
2819 Delay_Descriptors
2820 (Body_Entity (Enclosing_Master));
2821 else
2822 Delay_Descriptors
2823 (Enclosing_Master);
2824 end if;
2826 exit;
2828 else
2829 Enclosing_Master := Scope (Enclosing_Master);
2830 end if;
2832 elsif Ekind (Enclosing_Master) = E_Generic_Package then
2833 Enclosing_Master := Scope (Enclosing_Master);
2835 elsif Is_Generic_Subprogram (Enclosing_Master)
2836 or else Ekind (Enclosing_Master) = E_Void
2837 then
2838 -- Cleanup actions will eventually be performed on
2839 -- the enclosing instance, if any. enclosing scope
2840 -- is void in the formal part of a generic subp.
2842 exit;
2844 else
2845 if Ekind (Enclosing_Master) = E_Entry
2846 and then
2847 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
2848 then
2849 Enclosing_Master :=
2850 Protected_Body_Subprogram (Enclosing_Master);
2851 end if;
2853 Set_Delay_Cleanups (Enclosing_Master);
2855 while Ekind (Enclosing_Master) = E_Block loop
2856 Enclosing_Master := Scope (Enclosing_Master);
2857 end loop;
2859 if Is_Subprogram (Enclosing_Master) then
2860 Delay_Descriptors (Enclosing_Master);
2862 elsif Is_Task_Type (Enclosing_Master) then
2863 declare
2864 TBP : constant Node_Id :=
2865 Get_Task_Body_Procedure
2866 (Enclosing_Master);
2868 begin
2869 if Present (TBP) then
2870 Delay_Descriptors (TBP);
2871 Set_Delay_Cleanups (TBP);
2872 end if;
2873 end;
2874 end if;
2876 exit;
2877 end if;
2878 end loop;
2879 end;
2881 -- Make entry in table
2883 Pending_Instantiations.Increment_Last;
2884 Pending_Instantiations.Table (Pending_Instantiations.Last) :=
2885 (N, Act_Decl, Expander_Active, Current_Sem_Unit);
2886 end if;
2887 end if;
2889 Set_Categorization_From_Pragmas (Act_Decl);
2891 if Parent_Installed then
2892 Hide_Current_Scope;
2893 end if;
2895 Set_Instance_Spec (N, Act_Decl);
2897 -- If not a compilation unit, insert the package declaration
2898 -- before the original instantiation node.
2900 if Nkind (Parent (N)) /= N_Compilation_Unit then
2901 Mark_Rewrite_Insertion (Act_Decl);
2902 Insert_Before (N, Act_Decl);
2903 Analyze (Act_Decl);
2905 -- For an instantiation that is a compilation unit, place
2906 -- declaration on current node so context is complete
2907 -- for analysis (including nested instantiations). It this
2908 -- is the main unit, the declaration eventually replaces the
2909 -- instantiation node. If the instance body is later created, it
2910 -- replaces the instance node, and the declation is attached to
2911 -- it (see Build_Instance_Compilation_Unit_Nodes).
2913 else
2914 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
2916 -- The entity for the current unit is the newly created one,
2917 -- and all semantic information is attached to it.
2919 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
2921 -- If this is the main unit, replace the main entity as well
2923 if Current_Sem_Unit = Main_Unit then
2924 Main_Unit_Entity := Act_Decl_Id;
2925 end if;
2926 end if;
2928 -- There is a problem with inlining here
2929 -- More comments needed??? what problem
2931 Set_Unit (Parent (N), Act_Decl);
2932 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
2933 Set_Package_Instantiation (Act_Decl_Id, N);
2934 Analyze (Act_Decl);
2935 Set_Unit (Parent (N), N);
2936 Set_Body_Required (Parent (N), False);
2938 -- We never need elaboration checks on instantiations, since
2939 -- by definition, the body instantiation is elaborated at the
2940 -- same time as the spec instantiation.
2942 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
2943 Set_Kill_Elaboration_Checks (Act_Decl_Id);
2944 end if;
2946 Check_Elab_Instantiation (N);
2948 if ABE_Is_Certain (N) and then Needs_Body then
2949 Pending_Instantiations.Decrement_Last;
2950 end if;
2951 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
2953 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
2954 First_Private_Entity (Act_Decl_Id));
2956 -- If the instantiation will receive a body, the unit will
2957 -- be transformed into a package body, and receive its own
2958 -- elaboration entity. Otherwise, the nature of the unit is
2959 -- now a package declaration.
2961 if Nkind (Parent (N)) = N_Compilation_Unit
2962 and then not Needs_Body
2963 then
2964 Rewrite (N, Act_Decl);
2965 end if;
2967 if Present (Corresponding_Body (Gen_Decl))
2968 or else Unit_Requires_Body (Gen_Unit)
2969 then
2970 Set_Has_Completion (Act_Decl_Id);
2971 end if;
2973 Check_Formal_Packages (Act_Decl_Id);
2975 Restore_Private_Views (Act_Decl_Id);
2977 if not Generic_Separately_Compiled (Gen_Unit) then
2978 Inherit_Context (Gen_Decl, N);
2979 end if;
2981 if Parent_Installed then
2982 Remove_Parent;
2983 end if;
2985 Restore_Env;
2986 Env_Installed := False;
2987 end if;
2989 Validate_Categorization_Dependency (N, Act_Decl_Id);
2991 -- Check restriction, but skip this if something went wrong in
2992 -- the above analysis, indicated by Act_Decl_Id being void.
2994 if Ekind (Act_Decl_Id) /= E_Void
2995 and then not Is_Library_Level_Entity (Act_Decl_Id)
2996 then
2997 Check_Restriction (No_Local_Allocators, N);
2998 end if;
3000 if Inline_Now then
3001 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
3002 end if;
3004 -- The following is a tree patch for ASIS: ASIS needs separate nodes
3005 -- to be used as defining identifiers for a formal package and for the
3006 -- corresponding expanded package
3008 if Nkind (N) = N_Formal_Package_Declaration then
3009 Act_Decl_Id := New_Copy (Defining_Entity (N));
3010 Set_Comes_From_Source (Act_Decl_Id, True);
3011 Set_Is_Generic_Instance (Act_Decl_Id, False);
3012 Set_Defining_Identifier (N, Act_Decl_Id);
3013 end if;
3015 exception
3016 when Instantiation_Error =>
3017 if Parent_Installed then
3018 Remove_Parent;
3019 end if;
3021 if Env_Installed then
3022 Restore_Env;
3023 end if;
3024 end Analyze_Package_Instantiation;
3026 --------------------------
3027 -- Inline_Instance_Body --
3028 --------------------------
3030 procedure Inline_Instance_Body
3031 (N : Node_Id;
3032 Gen_Unit : Entity_Id;
3033 Act_Decl : Node_Id)
3035 Vis : Boolean;
3036 Gen_Comp : constant Entity_Id :=
3037 Cunit_Entity (Get_Source_Unit (Gen_Unit));
3038 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
3039 Curr_Scope : Entity_Id := Empty;
3040 Curr_Unit : constant Entity_Id :=
3041 Cunit_Entity (Current_Sem_Unit);
3042 Removed : Boolean := False;
3043 Num_Scopes : Int := 0;
3045 Scope_Stack_Depth : constant Int :=
3046 Scope_Stack.Last - Scope_Stack.First + 1;
3048 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
3049 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
3050 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
3051 Num_Inner : Int := 0;
3052 N_Instances : Int := 0;
3053 S : Entity_Id;
3055 begin
3056 -- Case of generic unit defined in another unit. We must remove the
3057 -- complete context of the current unit to install that of the generic.
3059 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
3061 -- Add some comments for the following two loops ???
3063 S := Current_Scope;
3064 while Present (S) and then S /= Standard_Standard loop
3065 loop
3066 Num_Scopes := Num_Scopes + 1;
3068 Use_Clauses (Num_Scopes) :=
3069 (Scope_Stack.Table
3070 (Scope_Stack.Last - Num_Scopes + 1).
3071 First_Use_Clause);
3072 End_Use_Clauses (Use_Clauses (Num_Scopes));
3074 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
3075 or else Scope_Stack.Table
3076 (Scope_Stack.Last - Num_Scopes).Entity
3077 = Scope (S);
3078 end loop;
3080 exit when Is_Generic_Instance (S)
3081 and then (In_Package_Body (S)
3082 or else Ekind (S) = E_Procedure
3083 or else Ekind (S) = E_Function);
3084 S := Scope (S);
3085 end loop;
3087 Vis := Is_Immediately_Visible (Gen_Comp);
3089 -- Find and save all enclosing instances
3091 S := Current_Scope;
3093 while Present (S)
3094 and then S /= Standard_Standard
3095 loop
3096 if Is_Generic_Instance (S) then
3097 N_Instances := N_Instances + 1;
3098 Instances (N_Instances) := S;
3100 exit when In_Package_Body (S);
3101 end if;
3103 S := Scope (S);
3104 end loop;
3106 -- Remove context of current compilation unit, unless we are within a
3107 -- nested package instantiation, in which case the context has been
3108 -- removed previously.
3110 -- If current scope is the body of a child unit, remove context of
3111 -- spec as well.
3113 S := Current_Scope;
3115 while Present (S)
3116 and then S /= Standard_Standard
3117 loop
3118 exit when Is_Generic_Instance (S)
3119 and then (In_Package_Body (S)
3120 or else Ekind (S) = E_Procedure
3121 or else Ekind (S) = E_Function);
3123 if S = Curr_Unit
3124 or else (Ekind (Curr_Unit) = E_Package_Body
3125 and then S = Spec_Entity (Curr_Unit))
3126 or else (Ekind (Curr_Unit) = E_Subprogram_Body
3127 and then S =
3128 Corresponding_Spec
3129 (Unit_Declaration_Node (Curr_Unit)))
3130 then
3131 Removed := True;
3133 -- Remove entities in current scopes from visibility, so
3134 -- that instance body is compiled in a clean environment.
3136 Save_Scope_Stack (Handle_Use => False);
3138 if Is_Child_Unit (S) then
3140 -- Remove child unit from stack, as well as inner scopes.
3141 -- Removing the context of a child unit removes parent
3142 -- units as well.
3144 while Current_Scope /= S loop
3145 Num_Inner := Num_Inner + 1;
3146 Inner_Scopes (Num_Inner) := Current_Scope;
3147 Pop_Scope;
3148 end loop;
3150 Pop_Scope;
3151 Remove_Context (Curr_Comp);
3152 Curr_Scope := S;
3154 else
3155 Remove_Context (Curr_Comp);
3156 end if;
3158 if Ekind (Curr_Unit) = E_Package_Body then
3159 Remove_Context (Library_Unit (Curr_Comp));
3160 end if;
3161 end if;
3163 S := Scope (S);
3164 end loop;
3165 pragma Assert (Num_Inner < Num_Scopes);
3167 New_Scope (Standard_Standard);
3168 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
3169 Instantiate_Package_Body
3170 ((N, Act_Decl, Expander_Active, Current_Sem_Unit), True);
3171 Pop_Scope;
3173 -- Restore context
3175 Set_Is_Immediately_Visible (Gen_Comp, Vis);
3177 -- Reset Generic_Instance flag so that use clauses can be installed
3178 -- in the proper order. (See Use_One_Package for effect of enclosing
3179 -- instances on processing of use clauses).
3181 for J in 1 .. N_Instances loop
3182 Set_Is_Generic_Instance (Instances (J), False);
3183 end loop;
3185 if Removed then
3186 Install_Context (Curr_Comp);
3188 if Present (Curr_Scope)
3189 and then Is_Child_Unit (Curr_Scope)
3190 then
3191 New_Scope (Curr_Scope);
3192 Set_Is_Immediately_Visible (Curr_Scope);
3194 -- Finally, restore inner scopes as well
3196 for J in reverse 1 .. Num_Inner loop
3197 New_Scope (Inner_Scopes (J));
3198 end loop;
3199 end if;
3201 Restore_Scope_Stack (Handle_Use => False);
3203 if Present (Curr_Scope)
3204 and then
3205 (In_Private_Part (Curr_Scope)
3206 or else In_Package_Body (Curr_Scope))
3207 then
3208 -- Install private declaration of ancestor units, which
3209 -- are currently available. Restore_Scope_Stack and
3210 -- Install_Context only install the visible part of parents.
3212 declare
3213 Par : Entity_Id;
3214 begin
3215 Par := Scope (Curr_Scope);
3216 while (Present (Par))
3217 and then Par /= Standard_Standard
3218 loop
3219 Install_Private_Declarations (Par);
3220 Par := Scope (Par);
3221 end loop;
3222 end;
3223 end if;
3224 end if;
3226 -- Restore use clauses. For a child unit, use clauses in the parents
3227 -- are restored when installing the context, so only those in inner
3228 -- scopes (and those local to the child unit itself) need to be
3229 -- installed explicitly.
3231 if Is_Child_Unit (Curr_Unit)
3232 and then Removed
3233 then
3234 for J in reverse 1 .. Num_Inner + 1 loop
3235 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3236 Use_Clauses (J);
3237 Install_Use_Clauses (Use_Clauses (J));
3238 end loop;
3240 else
3241 for J in reverse 1 .. Num_Scopes loop
3242 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3243 Use_Clauses (J);
3244 Install_Use_Clauses (Use_Clauses (J));
3245 end loop;
3246 end if;
3248 for J in 1 .. N_Instances loop
3249 Set_Is_Generic_Instance (Instances (J), True);
3250 end loop;
3252 -- If generic unit is in current unit, current context is correct
3254 else
3255 Instantiate_Package_Body
3256 ((N, Act_Decl, Expander_Active, Current_Sem_Unit), True);
3257 end if;
3258 end Inline_Instance_Body;
3260 -------------------------------------
3261 -- Analyze_Procedure_Instantiation --
3262 -------------------------------------
3264 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
3265 begin
3266 Analyze_Subprogram_Instantiation (N, E_Procedure);
3267 end Analyze_Procedure_Instantiation;
3269 --------------------------------------
3270 -- Analyze_Subprogram_Instantiation --
3271 --------------------------------------
3273 procedure Analyze_Subprogram_Instantiation
3274 (N : Node_Id;
3275 K : Entity_Kind)
3277 Loc : constant Source_Ptr := Sloc (N);
3278 Gen_Id : constant Node_Id := Name (N);
3280 Anon_Id : constant Entity_Id :=
3281 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
3282 Chars => New_External_Name
3283 (Chars (Defining_Entity (N)), 'R'));
3285 Act_Decl_Id : Entity_Id;
3286 Act_Decl : Node_Id;
3287 Act_Spec : Node_Id;
3288 Act_Tree : Node_Id;
3290 Env_Installed : Boolean := False;
3291 Gen_Unit : Entity_Id;
3292 Gen_Decl : Node_Id;
3293 Pack_Id : Entity_Id;
3294 Parent_Installed : Boolean := False;
3295 Renaming_List : List_Id;
3297 procedure Analyze_Instance_And_Renamings;
3298 -- The instance must be analyzed in a context that includes the
3299 -- mappings of generic parameters into actuals. We create a package
3300 -- declaration for this purpose, and a subprogram with an internal
3301 -- name within the package. The subprogram instance is simply an
3302 -- alias for the internal subprogram, declared in the current scope.
3304 ------------------------------------
3305 -- Analyze_Instance_And_Renamings --
3306 ------------------------------------
3308 procedure Analyze_Instance_And_Renamings is
3309 Def_Ent : constant Entity_Id := Defining_Entity (N);
3310 Pack_Decl : Node_Id;
3312 begin
3313 if Nkind (Parent (N)) = N_Compilation_Unit then
3315 -- For the case of a compilation unit, the container package
3316 -- has the same name as the instantiation, to insure that the
3317 -- binder calls the elaboration procedure with the right name.
3318 -- Copy the entity of the instance, which may have compilation
3319 -- level flags (e.g. Is_Child_Unit) set.
3321 Pack_Id := New_Copy (Def_Ent);
3323 else
3324 -- Otherwise we use the name of the instantiation concatenated
3325 -- with its source position to ensure uniqueness if there are
3326 -- several instantiations with the same name.
3328 Pack_Id :=
3329 Make_Defining_Identifier (Loc,
3330 Chars => New_External_Name
3331 (Related_Id => Chars (Def_Ent),
3332 Suffix => "GP",
3333 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
3334 end if;
3336 Pack_Decl := Make_Package_Declaration (Loc,
3337 Specification => Make_Package_Specification (Loc,
3338 Defining_Unit_Name => Pack_Id,
3339 Visible_Declarations => Renaming_List,
3340 End_Label => Empty));
3342 Set_Instance_Spec (N, Pack_Decl);
3343 Set_Is_Generic_Instance (Pack_Id);
3344 Set_Needs_Debug_Info (Pack_Id);
3346 -- Case of not a compilation unit
3348 if Nkind (Parent (N)) /= N_Compilation_Unit then
3349 Mark_Rewrite_Insertion (Pack_Decl);
3350 Insert_Before (N, Pack_Decl);
3351 Set_Has_Completion (Pack_Id);
3353 -- Case of an instantiation that is a compilation unit
3355 -- Place declaration on current node so context is complete
3356 -- for analysis (including nested instantiations), and for
3357 -- use in a context_clause (see Analyze_With_Clause).
3359 else
3360 Set_Unit (Parent (N), Pack_Decl);
3361 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
3362 end if;
3364 Analyze (Pack_Decl);
3365 Check_Formal_Packages (Pack_Id);
3366 Set_Is_Generic_Instance (Pack_Id, False);
3368 -- Body of the enclosing package is supplied when instantiating
3369 -- the subprogram body, after semantic analysis is completed.
3371 if Nkind (Parent (N)) = N_Compilation_Unit then
3373 -- Remove package itself from visibility, so it does not
3374 -- conflict with subprogram.
3376 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
3378 -- Set name and scope of internal subprogram so that the
3379 -- proper external name will be generated. The proper scope
3380 -- is the scope of the wrapper package. We need to generate
3381 -- debugging information for the internal subprogram, so set
3382 -- flag accordingly.
3384 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
3385 Set_Scope (Anon_Id, Scope (Pack_Id));
3387 -- Mark wrapper package as referenced, to avoid spurious
3388 -- warnings if the instantiation appears in various with_
3389 -- clauses of subunits of the main unit.
3391 Set_Referenced (Pack_Id);
3392 end if;
3394 Set_Is_Generic_Instance (Anon_Id);
3395 Set_Needs_Debug_Info (Anon_Id);
3396 Act_Decl_Id := New_Copy (Anon_Id);
3398 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3399 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
3400 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
3401 Set_Comes_From_Source (Act_Decl_Id, True);
3403 -- The signature may involve types that are not frozen yet, but
3404 -- the subprogram will be frozen at the point the wrapper package
3405 -- is frozen, so it does not need its own freeze node. In fact, if
3406 -- one is created, it might conflict with the freezing actions from
3407 -- the wrapper package (see 7206-013).
3409 Set_Has_Delayed_Freeze (Anon_Id, False);
3411 -- If the instance is a child unit, mark the Id accordingly. Mark
3412 -- the anonymous entity as well, which is the real subprogram and
3413 -- which is used when the instance appears in a context clause.
3415 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
3416 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
3417 New_Overloaded_Entity (Act_Decl_Id);
3418 Check_Eliminated (Act_Decl_Id);
3420 -- In compilation unit case, kill elaboration checks on the
3421 -- instantiation, since they are never needed -- the body is
3422 -- instantiated at the same point as the spec.
3424 if Nkind (Parent (N)) = N_Compilation_Unit then
3425 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
3426 Set_Kill_Elaboration_Checks (Act_Decl_Id);
3427 Set_Is_Compilation_Unit (Anon_Id);
3429 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
3430 end if;
3432 -- The instance is not a freezing point for the new subprogram
3434 Set_Is_Frozen (Act_Decl_Id, False);
3436 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
3437 Valid_Operator_Definition (Act_Decl_Id);
3438 end if;
3440 Set_Alias (Act_Decl_Id, Anon_Id);
3441 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3442 Set_Has_Completion (Act_Decl_Id);
3443 Set_Related_Instance (Pack_Id, Act_Decl_Id);
3445 if Nkind (Parent (N)) = N_Compilation_Unit then
3446 Set_Body_Required (Parent (N), False);
3447 end if;
3449 end Analyze_Instance_And_Renamings;
3451 -- Start of processing for Analyze_Subprogram_Instantiation
3453 begin
3454 -- Very first thing: apply the special kludge for Text_IO processing
3455 -- in case we are instantiating one of the children of [Wide_]Text_IO.
3456 -- Of course such an instantiation is bogus (these are packages, not
3457 -- subprograms), but we get a better error message if we do this.
3459 Text_IO_Kludge (Gen_Id);
3461 -- Make node global for error reporting
3463 Instantiation_Node := N;
3464 Pre_Analyze_Actuals (N);
3466 Init_Env;
3467 Env_Installed := True;
3468 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3469 Gen_Unit := Entity (Gen_Id);
3471 Generate_Reference (Gen_Unit, Gen_Id);
3473 if Nkind (Gen_Id) = N_Identifier
3474 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3475 then
3476 Error_Msg_NE
3477 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3478 end if;
3480 if Etype (Gen_Unit) = Any_Type then
3481 Restore_Env;
3482 return;
3483 end if;
3485 -- Verify that it is a generic subprogram of the right kind, and that
3486 -- it does not lead to a circular instantiation.
3488 if Ekind (Gen_Unit) /= E_Generic_Procedure
3489 and then Ekind (Gen_Unit) /= E_Generic_Function
3490 then
3491 Error_Msg_N ("expect generic subprogram in instantiation", Gen_Id);
3493 elsif In_Open_Scopes (Gen_Unit) then
3494 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3496 elsif K = E_Procedure
3497 and then Ekind (Gen_Unit) /= E_Generic_Procedure
3498 then
3499 if Ekind (Gen_Unit) = E_Generic_Function then
3500 Error_Msg_N
3501 ("cannot instantiate generic function as procedure", Gen_Id);
3502 else
3503 Error_Msg_N
3504 ("expect name of generic procedure in instantiation", Gen_Id);
3505 end if;
3507 elsif K = E_Function
3508 and then Ekind (Gen_Unit) /= E_Generic_Function
3509 then
3510 if Ekind (Gen_Unit) = E_Generic_Procedure then
3511 Error_Msg_N
3512 ("cannot instantiate generic procedure as function", Gen_Id);
3513 else
3514 Error_Msg_N
3515 ("expect name of generic function in instantiation", Gen_Id);
3516 end if;
3518 else
3519 Set_Entity (Gen_Id, Gen_Unit);
3520 Set_Is_Instantiated (Gen_Unit);
3522 if In_Extended_Main_Source_Unit (N) then
3523 Generate_Reference (Gen_Unit, N);
3524 end if;
3526 -- If renaming, get original unit
3528 if Present (Renamed_Object (Gen_Unit))
3529 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
3530 or else
3531 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
3532 then
3533 Gen_Unit := Renamed_Object (Gen_Unit);
3534 Set_Is_Instantiated (Gen_Unit);
3535 Generate_Reference (Gen_Unit, N);
3536 end if;
3538 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3539 Error_Msg_Node_2 := Current_Scope;
3540 Error_Msg_NE
3541 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3542 Circularity_Detected := True;
3543 return;
3544 end if;
3546 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3548 -- The subprogram itself cannot contain a nested instance, so
3549 -- the current parent is left empty.
3551 Set_Instance_Env (Gen_Unit, Empty);
3553 -- Initialize renamings map, for error checking
3555 Generic_Renamings.Set_Last (0);
3556 Generic_Renamings_HTable.Reset;
3558 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3560 -- Copy original generic tree, to produce text for instantiation
3562 Act_Tree :=
3563 Copy_Generic_Node
3564 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3566 Act_Spec := Specification (Act_Tree);
3567 Renaming_List :=
3568 Analyze_Associations
3570 Generic_Formal_Declarations (Act_Tree),
3571 Generic_Formal_Declarations (Gen_Decl));
3573 -- Build the subprogram declaration, which does not appear
3574 -- in the generic template, and give it a sloc consistent
3575 -- with that of the template.
3577 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
3578 Set_Generic_Parent (Act_Spec, Gen_Unit);
3579 Act_Decl :=
3580 Make_Subprogram_Declaration (Sloc (Act_Spec),
3581 Specification => Act_Spec);
3583 Set_Categorization_From_Pragmas (Act_Decl);
3585 if Parent_Installed then
3586 Hide_Current_Scope;
3587 end if;
3589 Append (Act_Decl, Renaming_List);
3590 Analyze_Instance_And_Renamings;
3592 -- If the generic is marked Import (Intrinsic), then so is the
3593 -- instance. This indicates that there is no body to instantiate.
3594 -- If generic is marked inline, so it the instance, and the
3595 -- anonymous subprogram it renames. If inlined, or else if inlining
3596 -- is enabled for the compilation, we generate the instance body
3597 -- even if it is not within the main unit.
3599 -- Any other pragmas might also be inherited ???
3601 if Is_Intrinsic_Subprogram (Gen_Unit) then
3602 Set_Is_Intrinsic_Subprogram (Anon_Id);
3603 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
3605 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
3606 Validate_Unchecked_Conversion (N, Act_Decl_Id);
3607 end if;
3608 end if;
3610 Generate_Definition (Act_Decl_Id);
3612 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
3613 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
3615 if not Is_Intrinsic_Subprogram (Gen_Unit) then
3616 Check_Elab_Instantiation (N);
3617 end if;
3619 if Is_Dispatching_Operation (Act_Decl_Id)
3620 and then Ada_Version >= Ada_05
3621 then
3622 declare
3623 Formal : Entity_Id;
3625 begin
3626 Formal := First_Formal (Act_Decl_Id);
3627 while Present (Formal) loop
3628 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
3629 and then Is_Controlling_Formal (Formal)
3630 and then not Can_Never_Be_Null (Formal)
3631 then
3632 Error_Msg_NE ("access parameter& is controlling,",
3633 N, Formal);
3634 Error_Msg_NE ("\corresponding parameter of & must be"
3635 & " explicitly null-excluding", N, Gen_Id);
3636 end if;
3638 Next_Formal (Formal);
3639 end loop;
3640 end;
3641 end if;
3643 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
3645 -- Subject to change, pending on if other pragmas are inherited ???
3647 Validate_Categorization_Dependency (N, Act_Decl_Id);
3649 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
3650 if not Generic_Separately_Compiled (Gen_Unit) then
3651 Inherit_Context (Gen_Decl, N);
3652 end if;
3654 Restore_Private_Views (Pack_Id, False);
3656 -- If the context requires a full instantiation, mark node for
3657 -- subsequent construction of the body.
3659 if (Is_In_Main_Unit (N)
3660 or else Is_Inlined (Act_Decl_Id))
3661 and then (Operating_Mode = Generate_Code
3662 or else (Operating_Mode = Check_Semantics
3663 and then ASIS_Mode))
3664 and then (Expander_Active or else ASIS_Mode)
3665 and then not ABE_Is_Certain (N)
3666 and then not Is_Eliminated (Act_Decl_Id)
3667 then
3668 Pending_Instantiations.Increment_Last;
3669 Pending_Instantiations.Table (Pending_Instantiations.Last) :=
3670 (N, Act_Decl, Expander_Active, Current_Sem_Unit);
3671 Check_Forward_Instantiation (Gen_Decl);
3673 -- The wrapper package is always delayed, because it does
3674 -- not constitute a freeze point, but to insure that the
3675 -- freeze node is placed properly, it is created directly
3676 -- when instantiating the body (otherwise the freeze node
3677 -- might appear to early for nested instantiations).
3679 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3681 -- For ASIS purposes, indicate that the wrapper package has
3682 -- replaced the instantiation node.
3684 Rewrite (N, Unit (Parent (N)));
3685 Set_Unit (Parent (N), N);
3686 end if;
3688 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3690 -- Replace instance node for library-level instantiations
3691 -- of intrinsic subprograms, for ASIS use.
3693 Rewrite (N, Unit (Parent (N)));
3694 Set_Unit (Parent (N), N);
3695 end if;
3697 if Parent_Installed then
3698 Remove_Parent;
3699 end if;
3701 Restore_Env;
3702 Env_Installed := False;
3703 Generic_Renamings.Set_Last (0);
3704 Generic_Renamings_HTable.Reset;
3705 end if;
3707 exception
3708 when Instantiation_Error =>
3709 if Parent_Installed then
3710 Remove_Parent;
3711 end if;
3713 if Env_Installed then
3714 Restore_Env;
3715 end if;
3716 end Analyze_Subprogram_Instantiation;
3718 -------------------------
3719 -- Get_Associated_Node --
3720 -------------------------
3722 function Get_Associated_Node (N : Node_Id) return Node_Id is
3723 Assoc : Node_Id := Associated_Node (N);
3725 begin
3726 if Nkind (Assoc) /= Nkind (N) then
3727 return Assoc;
3729 elsif Nkind (Assoc) = N_Aggregate
3730 or else Nkind (Assoc) = N_Extension_Aggregate
3731 then
3732 return Assoc;
3734 else
3735 -- If the node is part of an inner generic, it may itself have been
3736 -- remapped into a further generic copy. Associated_Node is otherwise
3737 -- used for the entity of the node, and will be of a different node
3738 -- kind, or else N has been rewritten as a literal or function call.
3740 while Present (Associated_Node (Assoc))
3741 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
3742 loop
3743 Assoc := Associated_Node (Assoc);
3744 end loop;
3746 -- Follow and additional link in case the final node was rewritten.
3747 -- This can only happen with nested generic units.
3749 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
3750 and then Present (Associated_Node (Assoc))
3751 and then (Nkind (Associated_Node (Assoc)) = N_Function_Call
3752 or else
3753 Nkind (Associated_Node (Assoc)) = N_Explicit_Dereference
3754 or else
3755 Nkind (Associated_Node (Assoc)) = N_Integer_Literal
3756 or else
3757 Nkind (Associated_Node (Assoc)) = N_Real_Literal
3758 or else
3759 Nkind (Associated_Node (Assoc)) = N_String_Literal)
3760 then
3761 Assoc := Associated_Node (Assoc);
3762 end if;
3764 return Assoc;
3765 end if;
3766 end Get_Associated_Node;
3768 -------------------------------------------
3769 -- Build_Instance_Compilation_Unit_Nodes --
3770 -------------------------------------------
3772 procedure Build_Instance_Compilation_Unit_Nodes
3773 (N : Node_Id;
3774 Act_Body : Node_Id;
3775 Act_Decl : Node_Id)
3777 Decl_Cunit : Node_Id;
3778 Body_Cunit : Node_Id;
3779 Citem : Node_Id;
3780 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
3781 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
3783 begin
3784 -- A new compilation unit node is built for the instance declaration
3786 Decl_Cunit :=
3787 Make_Compilation_Unit (Sloc (N),
3788 Context_Items => Empty_List,
3789 Unit => Act_Decl,
3790 Aux_Decls_Node =>
3791 Make_Compilation_Unit_Aux (Sloc (N)));
3793 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
3794 Set_Body_Required (Decl_Cunit, True);
3796 -- We use the original instantiation compilation unit as the resulting
3797 -- compilation unit of the instance, since this is the main unit.
3799 Rewrite (N, Act_Body);
3800 Body_Cunit := Parent (N);
3802 -- The two compilation unit nodes are linked by the Library_Unit field
3804 Set_Library_Unit (Decl_Cunit, Body_Cunit);
3805 Set_Library_Unit (Body_Cunit, Decl_Cunit);
3807 -- Preserve the private nature of the package if needed
3809 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
3811 -- If the instance is not the main unit, its context, categorization,
3812 -- and elaboration entity are not relevant to the compilation.
3814 if Parent (N) /= Cunit (Main_Unit) then
3815 return;
3816 end if;
3818 -- The context clause items on the instantiation, which are now
3819 -- attached to the body compilation unit (since the body overwrote
3820 -- the original instantiation node), semantically belong on the spec,
3821 -- so copy them there. It's harmless to leave them on the body as well.
3822 -- In fact one could argue that they belong in both places.
3824 Citem := First (Context_Items (Body_Cunit));
3825 while Present (Citem) loop
3826 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
3827 Next (Citem);
3828 end loop;
3830 -- Propagate categorization flags on packages, so that they appear
3831 -- in ali file for the spec of the unit.
3833 if Ekind (New_Main) = E_Package then
3834 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
3835 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
3836 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
3837 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
3838 Set_Is_Remote_Call_Interface
3839 (Old_Main, Is_Remote_Call_Interface (New_Main));
3840 end if;
3842 -- Make entry in Units table, so that binder can generate call to
3843 -- elaboration procedure for body, if any.
3845 Make_Instance_Unit (Body_Cunit);
3846 Main_Unit_Entity := New_Main;
3847 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
3849 -- Build elaboration entity, since the instance may certainly
3850 -- generate elaboration code requiring a flag for protection.
3852 Build_Elaboration_Entity (Decl_Cunit, New_Main);
3853 end Build_Instance_Compilation_Unit_Nodes;
3855 -----------------------------------
3856 -- Check_Formal_Package_Instance --
3857 -----------------------------------
3859 -- If the formal has specific parameters, they must match those of the
3860 -- actual. Both of them are instances, and the renaming declarations
3861 -- for their formal parameters appear in the same order in both. The
3862 -- analyzed formal has been analyzed in the context of the current
3863 -- instance.
3865 procedure Check_Formal_Package_Instance
3866 (Formal_Pack : Entity_Id;
3867 Actual_Pack : Entity_Id)
3869 E1 : Entity_Id := First_Entity (Actual_Pack);
3870 E2 : Entity_Id := First_Entity (Formal_Pack);
3872 Expr1 : Node_Id;
3873 Expr2 : Node_Id;
3875 procedure Check_Mismatch (B : Boolean);
3876 -- Common error routine for mismatch between the parameters of
3877 -- the actual instance and those of the formal package.
3879 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
3880 -- The formal may come from a nested formal package, and the actual
3881 -- may have been constant-folded. To determine whether the two denote
3882 -- the same entity we may have to traverse several definitions to
3883 -- recover the ultimate entity that they refer to.
3885 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
3886 -- Similarly, if the formal comes from a nested formal package, the
3887 -- actual may designate the formal through multiple renamings, which
3888 -- have to be followed to determine the original variable in question.
3890 --------------------
3891 -- Check_Mismatch --
3892 --------------------
3894 procedure Check_Mismatch (B : Boolean) is
3895 begin
3896 if B then
3897 Error_Msg_NE
3898 ("actual for & in actual instance does not match formal",
3899 Parent (Actual_Pack), E1);
3900 end if;
3901 end Check_Mismatch;
3903 --------------------------------
3904 -- Same_Instantiated_Constant --
3905 --------------------------------
3907 function Same_Instantiated_Constant
3908 (E1, E2 : Entity_Id) return Boolean
3910 Ent : Entity_Id;
3912 begin
3913 Ent := E2;
3914 while Present (Ent) loop
3915 if E1 = Ent then
3916 return True;
3918 elsif Ekind (Ent) /= E_Constant then
3919 return False;
3921 elsif Is_Entity_Name (Constant_Value (Ent)) then
3922 if Entity (Constant_Value (Ent)) = E1 then
3923 return True;
3924 else
3925 Ent := Entity (Constant_Value (Ent));
3926 end if;
3928 -- The actual may be a constant that has been folded. Recover
3929 -- original name.
3931 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
3932 Ent := Entity (Original_Node (Constant_Value (Ent)));
3933 else
3934 return False;
3935 end if;
3936 end loop;
3938 return False;
3939 end Same_Instantiated_Constant;
3941 --------------------------------
3942 -- Same_Instantiated_Variable --
3943 --------------------------------
3945 function Same_Instantiated_Variable
3946 (E1, E2 : Entity_Id) return Boolean
3948 function Original_Entity (E : Entity_Id) return Entity_Id;
3949 -- Follow chain of renamings to the ultimate ancestor
3951 ---------------------
3952 -- Original_Entity --
3953 ---------------------
3955 function Original_Entity (E : Entity_Id) return Entity_Id is
3956 Orig : Entity_Id;
3958 begin
3959 Orig := E;
3960 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
3961 and then Present (Renamed_Object (Orig))
3962 and then Is_Entity_Name (Renamed_Object (Orig))
3963 loop
3964 Orig := Entity (Renamed_Object (Orig));
3965 end loop;
3967 return Orig;
3968 end Original_Entity;
3970 -- Start of processing for Same_Instantiated_Variable
3972 begin
3973 return Ekind (E1) = Ekind (E2)
3974 and then Original_Entity (E1) = Original_Entity (E2);
3975 end Same_Instantiated_Variable;
3977 -- Start of processing for Check_Formal_Package_Instance
3979 begin
3980 while Present (E1)
3981 and then Present (E2)
3982 loop
3983 exit when Ekind (E1) = E_Package
3984 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
3986 if Is_Type (E1) then
3988 -- Subtypes must statically match. E1 and E2 are the
3989 -- local entities that are subtypes of the actuals.
3990 -- Itypes generated for other parameters need not be checked,
3991 -- the check will be performed on the parameters themselves.
3993 if not Is_Itype (E1)
3994 and then not Is_Itype (E2)
3995 then
3996 Check_Mismatch
3997 (not Is_Type (E2)
3998 or else Etype (E1) /= Etype (E2)
3999 or else not Subtypes_Statically_Match (E1, E2));
4000 end if;
4002 elsif Ekind (E1) = E_Constant then
4004 -- IN parameters must denote the same static value, or
4005 -- the same constant, or the literal null.
4007 Expr1 := Expression (Parent (E1));
4009 if Ekind (E2) /= E_Constant then
4010 Check_Mismatch (True);
4011 goto Next_E;
4012 else
4013 Expr2 := Expression (Parent (E2));
4014 end if;
4016 if Is_Static_Expression (Expr1) then
4018 if not Is_Static_Expression (Expr2) then
4019 Check_Mismatch (True);
4021 elsif Is_Integer_Type (Etype (E1)) then
4023 declare
4024 V1 : constant Uint := Expr_Value (Expr1);
4025 V2 : constant Uint := Expr_Value (Expr2);
4026 begin
4027 Check_Mismatch (V1 /= V2);
4028 end;
4030 elsif Is_Real_Type (Etype (E1)) then
4031 declare
4032 V1 : constant Ureal := Expr_Value_R (Expr1);
4033 V2 : constant Ureal := Expr_Value_R (Expr2);
4034 begin
4035 Check_Mismatch (V1 /= V2);
4036 end;
4038 elsif Is_String_Type (Etype (E1))
4039 and then Nkind (Expr1) = N_String_Literal
4040 then
4042 if Nkind (Expr2) /= N_String_Literal then
4043 Check_Mismatch (True);
4044 else
4045 Check_Mismatch
4046 (not String_Equal (Strval (Expr1), Strval (Expr2)));
4047 end if;
4048 end if;
4050 elsif Is_Entity_Name (Expr1) then
4051 if Is_Entity_Name (Expr2) then
4052 if Entity (Expr1) = Entity (Expr2) then
4053 null;
4054 else
4055 Check_Mismatch
4056 (not Same_Instantiated_Constant
4057 (Entity (Expr1), Entity (Expr2)));
4058 end if;
4059 else
4060 Check_Mismatch (True);
4061 end if;
4063 elsif Is_Entity_Name (Original_Node (Expr1))
4064 and then Is_Entity_Name (Expr2)
4065 and then
4066 Same_Instantiated_Constant
4067 (Entity (Original_Node (Expr1)), Entity (Expr2))
4068 then
4069 null;
4071 elsif Nkind (Expr1) = N_Null then
4072 Check_Mismatch (Nkind (Expr1) /= N_Null);
4074 else
4075 Check_Mismatch (True);
4076 end if;
4078 elsif Ekind (E1) = E_Variable then
4079 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
4081 elsif Ekind (E1) = E_Package then
4082 Check_Mismatch
4083 (Ekind (E1) /= Ekind (E2)
4084 or else Renamed_Object (E1) /= Renamed_Object (E2));
4086 elsif Is_Overloadable (E1) then
4088 -- Verify that the names of the entities match.
4089 -- What if actual is an attribute ???
4091 Check_Mismatch
4092 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
4094 else
4095 raise Program_Error;
4096 end if;
4098 <<Next_E>>
4099 Next_Entity (E1);
4100 Next_Entity (E2);
4101 end loop;
4102 end Check_Formal_Package_Instance;
4104 ---------------------------
4105 -- Check_Formal_Packages --
4106 ---------------------------
4108 procedure Check_Formal_Packages (P_Id : Entity_Id) is
4109 E : Entity_Id;
4110 Formal_P : Entity_Id;
4112 begin
4113 -- Iterate through the declarations in the instance, looking for
4114 -- package renaming declarations that denote instances of formal
4115 -- packages. Stop when we find the renaming of the current package
4116 -- itself. The declaration for a formal package without a box is
4117 -- followed by an internal entity that repeats the instantiation.
4119 E := First_Entity (P_Id);
4120 while Present (E) loop
4121 if Ekind (E) = E_Package then
4122 if Renamed_Object (E) = P_Id then
4123 exit;
4125 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
4126 null;
4128 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
4129 Formal_P := Next_Entity (E);
4130 Check_Formal_Package_Instance (Formal_P, E);
4131 end if;
4132 end if;
4134 Next_Entity (E);
4135 end loop;
4136 end Check_Formal_Packages;
4138 ---------------------------------
4139 -- Check_Forward_Instantiation --
4140 ---------------------------------
4142 procedure Check_Forward_Instantiation (Decl : Node_Id) is
4143 S : Entity_Id;
4144 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
4146 begin
4147 -- The instantiation appears before the generic body if we are in the
4148 -- scope of the unit containing the generic, either in its spec or in
4149 -- the package body. and before the generic body.
4151 if Ekind (Gen_Comp) = E_Package_Body then
4152 Gen_Comp := Spec_Entity (Gen_Comp);
4153 end if;
4155 if In_Open_Scopes (Gen_Comp)
4156 and then No (Corresponding_Body (Decl))
4157 then
4158 S := Current_Scope;
4160 while Present (S)
4161 and then not Is_Compilation_Unit (S)
4162 and then not Is_Child_Unit (S)
4163 loop
4164 if Ekind (S) = E_Package then
4165 Set_Has_Forward_Instantiation (S);
4166 end if;
4168 S := Scope (S);
4169 end loop;
4170 end if;
4171 end Check_Forward_Instantiation;
4173 ---------------------------
4174 -- Check_Generic_Actuals --
4175 ---------------------------
4177 -- The visibility of the actuals may be different between the
4178 -- point of generic instantiation and the instantiation of the body.
4180 procedure Check_Generic_Actuals
4181 (Instance : Entity_Id;
4182 Is_Formal_Box : Boolean)
4184 E : Entity_Id;
4185 Astype : Entity_Id;
4187 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
4188 -- For a formal that is an array type, the component type is often
4189 -- a previous formal in the same unit. The privacy status of the
4190 -- component type will have been examined earlier in the traversal
4191 -- of the corresponding actuals, and this status should not be
4192 -- modified for the array type itself.
4193 -- To detect this case we have to rescan the list of formals, which
4194 -- is usually short enough to ignore the resulting inefficiency.
4196 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
4197 Prev : Entity_Id;
4198 begin
4199 Prev := First_Entity (Instance);
4200 while Present (Prev) loop
4201 if Is_Type (Prev)
4202 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
4203 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
4204 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
4205 then
4206 return True;
4207 elsif Prev = E then
4208 return False;
4209 else
4210 Next_Entity (Prev);
4211 end if;
4212 end loop;
4213 return False;
4214 end Denotes_Previous_Actual;
4216 -- Start of processing for Check_Generic_Actuals
4218 begin
4219 E := First_Entity (Instance);
4220 while Present (E) loop
4221 if Is_Type (E)
4222 and then Nkind (Parent (E)) = N_Subtype_Declaration
4223 and then Scope (Etype (E)) /= Instance
4224 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
4225 then
4226 if Is_Array_Type (E)
4227 and then Denotes_Previous_Actual (Component_Type (E))
4228 then
4229 null;
4230 else
4231 Check_Private_View (Subtype_Indication (Parent (E)));
4232 end if;
4233 Set_Is_Generic_Actual_Type (E, True);
4234 Set_Is_Hidden (E, False);
4235 Set_Is_Potentially_Use_Visible (E,
4236 In_Use (Instance));
4238 -- We constructed the generic actual type as a subtype of
4239 -- the supplied type. This means that it normally would not
4240 -- inherit subtype specific attributes of the actual, which
4241 -- is wrong for the generic case.
4243 Astype := Ancestor_Subtype (E);
4245 if No (Astype) then
4247 -- can happen when E is an itype that is the full view of
4248 -- a private type completed, e.g. with a constrained array.
4250 Astype := Base_Type (E);
4251 end if;
4253 Set_Size_Info (E, (Astype));
4254 Set_RM_Size (E, RM_Size (Astype));
4255 Set_First_Rep_Item (E, First_Rep_Item (Astype));
4257 if Is_Discrete_Or_Fixed_Point_Type (E) then
4258 Set_RM_Size (E, RM_Size (Astype));
4260 -- In nested instances, the base type of an access actual
4261 -- may itself be private, and need to be exchanged.
4263 elsif Is_Access_Type (E)
4264 and then Is_Private_Type (Etype (E))
4265 then
4266 Check_Private_View
4267 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
4268 end if;
4270 elsif Ekind (E) = E_Package then
4272 -- If this is the renaming for the current instance, we're done.
4273 -- Otherwise it is a formal package. If the corresponding formal
4274 -- was declared with a box, the (instantiations of the) generic
4275 -- formal part are also visible. Otherwise, ignore the entity
4276 -- created to validate the actuals.
4278 if Renamed_Object (E) = Instance then
4279 exit;
4281 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
4282 null;
4284 -- The visibility of a formal of an enclosing generic is already
4285 -- correct.
4287 elsif Denotes_Formal_Package (E) then
4288 null;
4290 elsif Present (Associated_Formal_Package (E)) then
4291 if Box_Present (Parent (Associated_Formal_Package (E))) then
4292 Check_Generic_Actuals (Renamed_Object (E), True);
4293 end if;
4295 Set_Is_Hidden (E, False);
4296 end if;
4298 -- If this is a subprogram instance (in a wrapper package) the
4299 -- actual is fully visible.
4301 elsif Is_Wrapper_Package (Instance) then
4302 Set_Is_Hidden (E, False);
4304 else
4305 Set_Is_Hidden (E, not Is_Formal_Box);
4306 end if;
4308 Next_Entity (E);
4309 end loop;
4310 end Check_Generic_Actuals;
4312 ------------------------------
4313 -- Check_Generic_Child_Unit --
4314 ------------------------------
4316 procedure Check_Generic_Child_Unit
4317 (Gen_Id : Node_Id;
4318 Parent_Installed : in out Boolean)
4320 Loc : constant Source_Ptr := Sloc (Gen_Id);
4321 Gen_Par : Entity_Id := Empty;
4322 Inst_Par : Entity_Id;
4323 E : Entity_Id;
4324 S : Node_Id;
4326 function Find_Generic_Child
4327 (Scop : Entity_Id;
4328 Id : Node_Id) return Entity_Id;
4329 -- Search generic parent for possible child unit with the given name
4331 function In_Enclosing_Instance return Boolean;
4332 -- Within an instance of the parent, the child unit may be denoted
4333 -- by a simple name, or an abbreviated expanded name. Examine enclosing
4334 -- scopes to locate a possible parent instantiation.
4336 ------------------------
4337 -- Find_Generic_Child --
4338 ------------------------
4340 function Find_Generic_Child
4341 (Scop : Entity_Id;
4342 Id : Node_Id) return Entity_Id
4344 E : Entity_Id;
4346 begin
4347 -- If entity of name is already set, instance has already been
4348 -- resolved, e.g. in an enclosing instantiation.
4350 if Present (Entity (Id)) then
4351 if Scope (Entity (Id)) = Scop then
4352 return Entity (Id);
4353 else
4354 return Empty;
4355 end if;
4357 else
4358 E := First_Entity (Scop);
4359 while Present (E) loop
4360 if Chars (E) = Chars (Id)
4361 and then Is_Child_Unit (E)
4362 then
4363 if Is_Child_Unit (E)
4364 and then not Is_Visible_Child_Unit (E)
4365 then
4366 Error_Msg_NE
4367 ("generic child unit& is not visible", Gen_Id, E);
4368 end if;
4370 Set_Entity (Id, E);
4371 return E;
4372 end if;
4374 Next_Entity (E);
4375 end loop;
4377 return Empty;
4378 end if;
4379 end Find_Generic_Child;
4381 ---------------------------
4382 -- In_Enclosing_Instance --
4383 ---------------------------
4385 function In_Enclosing_Instance return Boolean is
4386 Enclosing_Instance : Node_Id;
4387 Instance_Decl : Node_Id;
4389 begin
4390 -- We do not inline any call that contains instantiations, except
4391 -- for instantiations of Unchecked_Conversion, so if we are within
4392 -- an inlined body the current instance does not require parents.
4394 if In_Inlined_Body then
4395 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
4396 return False;
4397 end if;
4399 -- Loop to check enclosing scopes
4401 Enclosing_Instance := Current_Scope;
4402 while Present (Enclosing_Instance) loop
4403 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
4405 if Ekind (Enclosing_Instance) = E_Package
4406 and then Is_Generic_Instance (Enclosing_Instance)
4407 and then Present
4408 (Generic_Parent (Specification (Instance_Decl)))
4409 then
4410 -- Check whether the generic we are looking for is a child
4411 -- of this instance.
4413 E := Find_Generic_Child
4414 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
4415 exit when Present (E);
4417 else
4418 E := Empty;
4419 end if;
4421 Enclosing_Instance := Scope (Enclosing_Instance);
4422 end loop;
4424 if No (E) then
4426 -- Not a child unit
4428 Analyze (Gen_Id);
4429 return False;
4431 else
4432 Rewrite (Gen_Id,
4433 Make_Expanded_Name (Loc,
4434 Chars => Chars (E),
4435 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
4436 Selector_Name => New_Occurrence_Of (E, Loc)));
4438 Set_Entity (Gen_Id, E);
4439 Set_Etype (Gen_Id, Etype (E));
4440 Parent_Installed := False; -- Already in scope.
4441 return True;
4442 end if;
4443 end In_Enclosing_Instance;
4445 -- Start of processing for Check_Generic_Child_Unit
4447 begin
4448 -- If the name of the generic is given by a selected component, it
4449 -- may be the name of a generic child unit, and the prefix is the name
4450 -- of an instance of the parent, in which case the child unit must be
4451 -- visible. If this instance is not in scope, it must be placed there
4452 -- and removed after instantiation, because what is being instantiated
4453 -- is not the original child, but the corresponding child present in
4454 -- the instance of the parent.
4456 -- If the child is instantiated within the parent, it can be given by
4457 -- a simple name. In this case the instance is already in scope, but
4458 -- the child generic must be recovered from the generic parent as well.
4460 if Nkind (Gen_Id) = N_Selected_Component then
4461 S := Selector_Name (Gen_Id);
4462 Analyze (Prefix (Gen_Id));
4463 Inst_Par := Entity (Prefix (Gen_Id));
4465 if Ekind (Inst_Par) = E_Package
4466 and then Present (Renamed_Object (Inst_Par))
4467 then
4468 Inst_Par := Renamed_Object (Inst_Par);
4469 end if;
4471 if Ekind (Inst_Par) = E_Package then
4472 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
4473 Gen_Par := Generic_Parent (Parent (Inst_Par));
4475 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
4476 and then
4477 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
4478 then
4479 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
4480 end if;
4482 elsif Ekind (Inst_Par) = E_Generic_Package
4483 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
4484 then
4485 -- A formal package may be a real child package, and not the
4486 -- implicit instance within a parent. In this case the child is
4487 -- not visible and has to be retrieved explicitly as well.
4489 Gen_Par := Inst_Par;
4490 end if;
4492 if Present (Gen_Par) then
4494 -- The prefix denotes an instantiation. The entity itself
4495 -- may be a nested generic, or a child unit.
4497 E := Find_Generic_Child (Gen_Par, S);
4499 if Present (E) then
4500 Change_Selected_Component_To_Expanded_Name (Gen_Id);
4501 Set_Entity (Gen_Id, E);
4502 Set_Etype (Gen_Id, Etype (E));
4503 Set_Entity (S, E);
4504 Set_Etype (S, Etype (E));
4506 -- Indicate that this is a reference to the parent
4508 if In_Extended_Main_Source_Unit (Gen_Id) then
4509 Set_Is_Instantiated (Inst_Par);
4510 end if;
4512 -- A common mistake is to replicate the naming scheme of
4513 -- a hierarchy by instantiating a generic child directly,
4514 -- rather than the implicit child in a parent instance:
4516 -- generic .. package Gpar is ..
4517 -- generic .. package Gpar.Child is ..
4518 -- package Par is new Gpar ();
4520 -- with Gpar.Child;
4521 -- package Par.Child is new Gpar.Child ();
4522 -- rather than Par.Child
4524 -- In this case the instantiation is within Par, which is
4525 -- an instance, but Gpar does not denote Par because we are
4526 -- not IN the instance of Gpar, so this is illegal. The test
4527 -- below recognizes this particular case.
4529 if Is_Child_Unit (E)
4530 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
4531 and then (not In_Instance
4532 or else Nkind (Parent (Parent (Gen_Id))) =
4533 N_Compilation_Unit)
4534 then
4535 Error_Msg_N
4536 ("prefix of generic child unit must be instance of parent",
4537 Gen_Id);
4538 end if;
4540 if not In_Open_Scopes (Inst_Par)
4541 and then Nkind (Parent (Gen_Id)) not in
4542 N_Generic_Renaming_Declaration
4543 then
4544 Install_Parent (Inst_Par);
4545 Parent_Installed := True;
4546 end if;
4548 else
4549 -- If the generic parent does not contain an entity that
4550 -- corresponds to the selector, the instance doesn't either.
4551 -- Analyzing the node will yield the appropriate error message.
4552 -- If the entity is not a child unit, then it is an inner
4553 -- generic in the parent.
4555 Analyze (Gen_Id);
4556 end if;
4558 else
4559 Analyze (Gen_Id);
4561 if Is_Child_Unit (Entity (Gen_Id))
4562 and then
4563 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
4564 and then not In_Open_Scopes (Inst_Par)
4565 then
4566 Install_Parent (Inst_Par);
4567 Parent_Installed := True;
4568 end if;
4569 end if;
4571 elsif Nkind (Gen_Id) = N_Expanded_Name then
4573 -- Entity already present, analyze prefix, whose meaning may be
4574 -- an instance in the current context. If it is an instance of
4575 -- a relative within another, the proper parent may still have
4576 -- to be installed, if they are not of the same generation.
4578 Analyze (Prefix (Gen_Id));
4579 Inst_Par := Entity (Prefix (Gen_Id));
4581 if In_Enclosing_Instance then
4582 null;
4584 elsif Present (Entity (Gen_Id))
4585 and then Is_Child_Unit (Entity (Gen_Id))
4586 and then not In_Open_Scopes (Inst_Par)
4587 then
4588 Install_Parent (Inst_Par);
4589 Parent_Installed := True;
4590 end if;
4592 elsif In_Enclosing_Instance then
4594 -- The child unit is found in some enclosing scope
4596 null;
4598 else
4599 Analyze (Gen_Id);
4601 -- If this is the renaming of the implicit child in a parent
4602 -- instance, recover the parent name and install it.
4604 if Is_Entity_Name (Gen_Id) then
4605 E := Entity (Gen_Id);
4607 if Is_Generic_Unit (E)
4608 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
4609 and then Is_Child_Unit (Renamed_Object (E))
4610 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
4611 and then Nkind (Name (Parent (E))) = N_Expanded_Name
4612 then
4613 Rewrite (Gen_Id,
4614 New_Copy_Tree (Name (Parent (E))));
4615 Inst_Par := Entity (Prefix (Gen_Id));
4617 if not In_Open_Scopes (Inst_Par) then
4618 Install_Parent (Inst_Par);
4619 Parent_Installed := True;
4620 end if;
4622 -- If it is a child unit of a non-generic parent, it may be
4623 -- use-visible and given by a direct name. Install parent as
4624 -- for other cases.
4626 elsif Is_Generic_Unit (E)
4627 and then Is_Child_Unit (E)
4628 and then
4629 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
4630 and then not Is_Generic_Unit (Scope (E))
4631 then
4632 if not In_Open_Scopes (Scope (E)) then
4633 Install_Parent (Scope (E));
4634 Parent_Installed := True;
4635 end if;
4636 end if;
4637 end if;
4638 end if;
4639 end Check_Generic_Child_Unit;
4641 -----------------------------
4642 -- Check_Hidden_Child_Unit --
4643 -----------------------------
4645 procedure Check_Hidden_Child_Unit
4646 (N : Node_Id;
4647 Gen_Unit : Entity_Id;
4648 Act_Decl_Id : Entity_Id)
4650 Gen_Id : constant Node_Id := Name (N);
4652 begin
4653 if Is_Child_Unit (Gen_Unit)
4654 and then Is_Child_Unit (Act_Decl_Id)
4655 and then Nkind (Gen_Id) = N_Expanded_Name
4656 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
4657 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
4658 then
4659 Error_Msg_Node_2 := Scope (Act_Decl_Id);
4660 Error_Msg_NE
4661 ("generic unit & is implicitly declared in &",
4662 Defining_Unit_Name (N), Gen_Unit);
4663 Error_Msg_N ("\instance must have different name",
4664 Defining_Unit_Name (N));
4665 end if;
4666 end Check_Hidden_Child_Unit;
4668 ------------------------
4669 -- Check_Private_View --
4670 ------------------------
4672 procedure Check_Private_View (N : Node_Id) is
4673 T : constant Entity_Id := Etype (N);
4674 BT : Entity_Id;
4676 begin
4677 -- Exchange views if the type was not private in the generic but is
4678 -- private at the point of instantiation. Do not exchange views if
4679 -- the scope of the type is in scope. This can happen if both generic
4680 -- and instance are sibling units, or if type is defined in a parent.
4681 -- In this case the visibility of the type will be correct for all
4682 -- semantic checks.
4684 if Present (T) then
4685 BT := Base_Type (T);
4687 if Is_Private_Type (T)
4688 and then not Has_Private_View (N)
4689 and then Present (Full_View (T))
4690 and then not In_Open_Scopes (Scope (T))
4691 then
4692 -- In the generic, the full type was visible. Save the
4693 -- private entity, for subsequent exchange.
4695 Switch_View (T);
4697 elsif Has_Private_View (N)
4698 and then not Is_Private_Type (T)
4699 and then not Has_Been_Exchanged (T)
4700 and then Etype (Get_Associated_Node (N)) /= T
4701 then
4702 -- Only the private declaration was visible in the generic. If
4703 -- the type appears in a subtype declaration, the subtype in the
4704 -- instance must have a view compatible with that of its parent,
4705 -- which must be exchanged (see corresponding code in Restore_
4706 -- Private_Views). Otherwise, if the type is defined in a parent
4707 -- unit, leave full visibility within instance, which is safe.
4709 if In_Open_Scopes (Scope (Base_Type (T)))
4710 and then not Is_Private_Type (Base_Type (T))
4711 and then Comes_From_Source (Base_Type (T))
4712 then
4713 null;
4715 elsif Nkind (Parent (N)) = N_Subtype_Declaration
4716 or else not In_Private_Part (Scope (Base_Type (T)))
4717 then
4718 Prepend_Elmt (T, Exchanged_Views);
4719 Exchange_Declarations (Etype (Get_Associated_Node (N)));
4720 end if;
4722 -- For composite types with inconsistent representation
4723 -- exchange component types accordingly.
4725 elsif Is_Access_Type (T)
4726 and then Is_Private_Type (Designated_Type (T))
4727 and then not Has_Private_View (N)
4728 and then Present (Full_View (Designated_Type (T)))
4729 then
4730 Switch_View (Designated_Type (T));
4732 elsif Is_Array_Type (T)
4733 and then Is_Private_Type (Component_Type (T))
4734 and then not Has_Private_View (N)
4735 and then Present (Full_View (Component_Type (T)))
4736 then
4737 Switch_View (Component_Type (T));
4739 elsif Is_Private_Type (T)
4740 and then Present (Full_View (T))
4741 and then Is_Array_Type (Full_View (T))
4742 and then Is_Private_Type (Component_Type (Full_View (T)))
4743 then
4744 Switch_View (T);
4746 -- Finally, a non-private subtype may have a private base type,
4747 -- which must be exchanged for consistency. This can happen when
4748 -- instantiating a package body, when the scope stack is empty
4749 -- but in fact the subtype and the base type are declared in an
4750 -- enclosing scope.
4752 elsif not Is_Private_Type (T)
4753 and then not Has_Private_View (N)
4754 and then Is_Private_Type (Base_Type (T))
4755 and then Present (Full_View (BT))
4756 and then not Is_Generic_Type (BT)
4757 and then not In_Open_Scopes (BT)
4758 then
4759 Prepend_Elmt (Full_View (BT), Exchanged_Views);
4760 Exchange_Declarations (BT);
4761 end if;
4762 end if;
4763 end Check_Private_View;
4765 --------------------------
4766 -- Contains_Instance_Of --
4767 --------------------------
4769 function Contains_Instance_Of
4770 (Inner : Entity_Id;
4771 Outer : Entity_Id;
4772 N : Node_Id) return Boolean
4774 Elmt : Elmt_Id;
4775 Scop : Entity_Id;
4777 begin
4778 Scop := Outer;
4780 -- Verify that there are no circular instantiations. We check whether
4781 -- the unit contains an instance of the current scope or some enclosing
4782 -- scope (in case one of the instances appears in a subunit). Longer
4783 -- circularities involving subunits might seem too pathological to
4784 -- consider, but they were not too pathological for the authors of
4785 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
4786 -- enclosing generic scopes as containing an instance.
4788 loop
4789 -- Within a generic subprogram body, the scope is not generic, to
4790 -- allow for recursive subprograms. Use the declaration to determine
4791 -- whether this is a generic unit.
4793 if Ekind (Scop) = E_Generic_Package
4794 or else (Is_Subprogram (Scop)
4795 and then Nkind (Unit_Declaration_Node (Scop)) =
4796 N_Generic_Subprogram_Declaration)
4797 then
4798 Elmt := First_Elmt (Inner_Instances (Inner));
4800 while Present (Elmt) loop
4801 if Node (Elmt) = Scop then
4802 Error_Msg_Node_2 := Inner;
4803 Error_Msg_NE
4804 ("circular Instantiation: & instantiated within &!",
4805 N, Scop);
4806 return True;
4808 elsif Node (Elmt) = Inner then
4809 return True;
4811 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
4812 Error_Msg_Node_2 := Inner;
4813 Error_Msg_NE
4814 ("circular Instantiation: & instantiated within &!",
4815 N, Node (Elmt));
4816 return True;
4817 end if;
4819 Next_Elmt (Elmt);
4820 end loop;
4822 -- Indicate that Inner is being instantiated within Scop
4824 Append_Elmt (Inner, Inner_Instances (Scop));
4825 end if;
4827 if Scop = Standard_Standard then
4828 exit;
4829 else
4830 Scop := Scope (Scop);
4831 end if;
4832 end loop;
4834 return False;
4835 end Contains_Instance_Of;
4837 -----------------------
4838 -- Copy_Generic_Node --
4839 -----------------------
4841 function Copy_Generic_Node
4842 (N : Node_Id;
4843 Parent_Id : Node_Id;
4844 Instantiating : Boolean) return Node_Id
4846 Ent : Entity_Id;
4847 New_N : Node_Id;
4849 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
4850 -- Check the given value of one of the Fields referenced by the
4851 -- current node to determine whether to copy it recursively. The
4852 -- field may hold a Node_Id, a List_Id, or an Elist_Id, or a plain
4853 -- value (Sloc, Uint, Char) in which case it need not be copied.
4855 procedure Copy_Descendants;
4856 -- Common utility for various nodes
4858 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
4859 -- Make copy of element list
4861 function Copy_Generic_List
4862 (L : List_Id;
4863 Parent_Id : Node_Id) return List_Id;
4864 -- Apply Copy_Node recursively to the members of a node list
4866 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
4867 -- True if an identifier is part of the defining program unit name
4868 -- of a child unit. The entity of such an identifier must be kept
4869 -- (for ASIS use) even though as the name of an enclosing generic
4870 -- it would otherwise not be preserved in the generic tree.
4872 ----------------------
4873 -- Copy_Descendants --
4874 ----------------------
4876 procedure Copy_Descendants is
4878 use Atree.Unchecked_Access;
4879 -- This code section is part of the implementation of an untyped
4880 -- tree traversal, so it needs direct access to node fields.
4882 begin
4883 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
4884 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
4885 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
4886 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
4887 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
4888 end Copy_Descendants;
4890 -----------------------------
4891 -- Copy_Generic_Descendant --
4892 -----------------------------
4894 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
4895 begin
4896 if D = Union_Id (Empty) then
4897 return D;
4899 elsif D in Node_Range then
4900 return Union_Id
4901 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
4903 elsif D in List_Range then
4904 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
4906 elsif D in Elist_Range then
4907 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
4909 -- Nothing else is copyable (e.g. Uint values), return as is
4911 else
4912 return D;
4913 end if;
4914 end Copy_Generic_Descendant;
4916 ------------------------
4917 -- Copy_Generic_Elist --
4918 ------------------------
4920 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
4921 M : Elmt_Id;
4922 L : Elist_Id;
4924 begin
4925 if Present (E) then
4926 L := New_Elmt_List;
4927 M := First_Elmt (E);
4928 while Present (M) loop
4929 Append_Elmt
4930 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
4931 Next_Elmt (M);
4932 end loop;
4934 return L;
4936 else
4937 return No_Elist;
4938 end if;
4939 end Copy_Generic_Elist;
4941 -----------------------
4942 -- Copy_Generic_List --
4943 -----------------------
4945 function Copy_Generic_List
4946 (L : List_Id;
4947 Parent_Id : Node_Id) return List_Id
4949 N : Node_Id;
4950 New_L : List_Id;
4952 begin
4953 if Present (L) then
4954 New_L := New_List;
4955 Set_Parent (New_L, Parent_Id);
4957 N := First (L);
4958 while Present (N) loop
4959 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
4960 Next (N);
4961 end loop;
4963 return New_L;
4965 else
4966 return No_List;
4967 end if;
4968 end Copy_Generic_List;
4970 ---------------------------
4971 -- In_Defining_Unit_Name --
4972 ---------------------------
4974 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
4975 begin
4976 return Present (Parent (Nam))
4977 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
4978 or else
4979 (Nkind (Parent (Nam)) = N_Expanded_Name
4980 and then In_Defining_Unit_Name (Parent (Nam))));
4981 end In_Defining_Unit_Name;
4983 -- Start of processing for Copy_Generic_Node
4985 begin
4986 if N = Empty then
4987 return N;
4988 end if;
4990 New_N := New_Copy (N);
4992 if Instantiating then
4993 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
4994 end if;
4996 if not Is_List_Member (N) then
4997 Set_Parent (New_N, Parent_Id);
4998 end if;
5000 -- If defining identifier, then all fields have been copied already
5002 if Nkind (New_N) in N_Entity then
5003 null;
5005 -- Special casing for identifiers and other entity names and operators
5007 elsif Nkind (New_N) = N_Identifier
5008 or else Nkind (New_N) = N_Character_Literal
5009 or else Nkind (New_N) = N_Expanded_Name
5010 or else Nkind (New_N) = N_Operator_Symbol
5011 or else Nkind (New_N) in N_Op
5012 then
5013 if not Instantiating then
5015 -- Link both nodes in order to assign subsequently the
5016 -- entity of the copy to the original node, in case this
5017 -- is a global reference.
5019 Set_Associated_Node (N, New_N);
5021 -- If we are within an instantiation, this is a nested generic
5022 -- that has already been analyzed at the point of definition. We
5023 -- must preserve references that were global to the enclosing
5024 -- parent at that point. Other occurrences, whether global or
5025 -- local to the current generic, must be resolved anew, so we
5026 -- reset the entity in the generic copy. A global reference has
5027 -- a smaller depth than the parent, or else the same depth in
5028 -- case both are distinct compilation units.
5030 -- It is also possible for Current_Instantiated_Parent to be
5031 -- defined, and for this not to be a nested generic, namely
5032 -- if the unit is loaded through Rtsfind. In that case, the
5033 -- entity of New_N is only a link to the associated node, and
5034 -- not a defining occurrence.
5036 -- The entities for parent units in the defining_program_unit
5037 -- of a generic child unit are established when the context of
5038 -- the unit is first analyzed, before the generic copy is made.
5039 -- They are preserved in the copy for use in ASIS queries.
5041 Ent := Entity (New_N);
5043 if No (Current_Instantiated_Parent.Gen_Id) then
5044 if No (Ent)
5045 or else Nkind (Ent) /= N_Defining_Identifier
5046 or else not In_Defining_Unit_Name (N)
5047 then
5048 Set_Associated_Node (New_N, Empty);
5049 end if;
5051 elsif No (Ent)
5052 or else
5053 not (Nkind (Ent) = N_Defining_Identifier
5054 or else
5055 Nkind (Ent) = N_Defining_Character_Literal
5056 or else
5057 Nkind (Ent) = N_Defining_Operator_Symbol)
5058 or else No (Scope (Ent))
5059 or else Scope (Ent) = Current_Instantiated_Parent.Gen_Id
5060 or else (Scope_Depth (Scope (Ent)) >
5061 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
5062 and then
5063 Get_Source_Unit (Ent) =
5064 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
5065 then
5066 Set_Associated_Node (New_N, Empty);
5067 end if;
5069 -- Case of instantiating identifier or some other name or operator
5071 else
5072 -- If the associated node is still defined, the entity in
5073 -- it is global, and must be copied to the instance.
5074 -- If this copy is being made for a body to inline, it is
5075 -- applied to an instantiated tree, and the entity is already
5076 -- present and must be also preserved.
5078 declare
5079 Assoc : constant Node_Id := Get_Associated_Node (N);
5080 begin
5081 if Present (Assoc) then
5082 if Nkind (Assoc) = Nkind (N) then
5083 Set_Entity (New_N, Entity (Assoc));
5084 Check_Private_View (N);
5086 elsif Nkind (Assoc) = N_Function_Call then
5087 Set_Entity (New_N, Entity (Name (Assoc)));
5089 elsif (Nkind (Assoc) = N_Defining_Identifier
5090 or else Nkind (Assoc) = N_Defining_Character_Literal
5091 or else Nkind (Assoc) = N_Defining_Operator_Symbol)
5092 and then Expander_Active
5093 then
5094 -- Inlining case: we are copying a tree that contains
5095 -- global entities, which are preserved in the copy
5096 -- to be used for subsequent inlining.
5098 null;
5100 else
5101 Set_Entity (New_N, Empty);
5102 end if;
5103 end if;
5104 end;
5105 end if;
5107 -- For expanded name, we must copy the Prefix and Selector_Name
5109 if Nkind (N) = N_Expanded_Name then
5110 Set_Prefix
5111 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
5113 Set_Selector_Name (New_N,
5114 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
5116 -- For operators, we must copy the right operand
5118 elsif Nkind (N) in N_Op then
5119 Set_Right_Opnd (New_N,
5120 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
5122 -- And for binary operators, the left operand as well
5124 if Nkind (N) in N_Binary_Op then
5125 Set_Left_Opnd (New_N,
5126 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
5127 end if;
5128 end if;
5130 -- Special casing for stubs
5132 elsif Nkind (N) in N_Body_Stub then
5134 -- In any case, we must copy the specification or defining
5135 -- identifier as appropriate.
5137 if Nkind (N) = N_Subprogram_Body_Stub then
5138 Set_Specification (New_N,
5139 Copy_Generic_Node (Specification (N), New_N, Instantiating));
5141 else
5142 Set_Defining_Identifier (New_N,
5143 Copy_Generic_Node
5144 (Defining_Identifier (N), New_N, Instantiating));
5145 end if;
5147 -- If we are not instantiating, then this is where we load and
5148 -- analyze subunits, i.e. at the point where the stub occurs. A
5149 -- more permissivle system might defer this analysis to the point
5150 -- of instantiation, but this seems to complicated for now.
5152 if not Instantiating then
5153 declare
5154 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
5155 Subunit : Node_Id;
5156 Unum : Unit_Number_Type;
5157 New_Body : Node_Id;
5159 begin
5160 Unum :=
5161 Load_Unit
5162 (Load_Name => Subunit_Name,
5163 Required => False,
5164 Subunit => True,
5165 Error_Node => N);
5167 -- If the proper body is not found, a warning message will
5168 -- be emitted when analyzing the stub, or later at the the
5169 -- point of instantiation. Here we just leave the stub as is.
5171 if Unum = No_Unit then
5172 Subunits_Missing := True;
5173 goto Subunit_Not_Found;
5174 end if;
5176 Subunit := Cunit (Unum);
5178 if Nkind (Unit (Subunit)) /= N_Subunit then
5179 Error_Msg_Sloc := Sloc (N);
5180 Error_Msg_N
5181 ("expected SEPARATE subunit to complete stub at#,"
5182 & " found child unit", Subunit);
5183 goto Subunit_Not_Found;
5184 end if;
5186 -- We must create a generic copy of the subunit, in order
5187 -- to perform semantic analysis on it, and we must replace
5188 -- the stub in the original generic unit with the subunit,
5189 -- in order to preserve non-local references within.
5191 -- Only the proper body needs to be copied. Library_Unit and
5192 -- context clause are simply inherited by the generic copy.
5193 -- Note that the copy (which may be recursive if there are
5194 -- nested subunits) must be done first, before attaching it
5195 -- to the enclosing generic.
5197 New_Body :=
5198 Copy_Generic_Node
5199 (Proper_Body (Unit (Subunit)),
5200 Empty, Instantiating => False);
5202 -- Now place the original proper body in the original
5203 -- generic unit. This is a body, not a compilation unit.
5205 Rewrite (N, Proper_Body (Unit (Subunit)));
5206 Set_Is_Compilation_Unit (Defining_Entity (N), False);
5207 Set_Was_Originally_Stub (N);
5209 -- Finally replace the body of the subunit with its copy,
5210 -- and make this new subunit into the library unit of the
5211 -- generic copy, which does not have stubs any longer.
5213 Set_Proper_Body (Unit (Subunit), New_Body);
5214 Set_Library_Unit (New_N, Subunit);
5215 Inherit_Context (Unit (Subunit), N);
5216 end;
5218 -- If we are instantiating, this must be an error case, since
5219 -- otherwise we would have replaced the stub node by the proper
5220 -- body that corresponds. So just ignore it in the copy (i.e.
5221 -- we have copied it, and that is good enough).
5223 else
5224 null;
5225 end if;
5227 <<Subunit_Not_Found>> null;
5229 -- If the node is a compilation unit, it is the subunit of a stub,
5230 -- which has been loaded already (see code below). In this case,
5231 -- the library unit field of N points to the parent unit (which
5232 -- is a compilation unit) and need not (and cannot!) be copied.
5234 -- When the proper body of the stub is analyzed, thie library_unit
5235 -- link is used to establish the proper context (see sem_ch10).
5237 -- The other fields of a compilation unit are copied as usual
5239 elsif Nkind (N) = N_Compilation_Unit then
5241 -- This code can only be executed when not instantiating, because
5242 -- in the copy made for an instantiation, the compilation unit
5243 -- node has disappeared at the point that a stub is replaced by
5244 -- its proper body.
5246 pragma Assert (not Instantiating);
5248 Set_Context_Items (New_N,
5249 Copy_Generic_List (Context_Items (N), New_N));
5251 Set_Unit (New_N,
5252 Copy_Generic_Node (Unit (N), New_N, False));
5254 Set_First_Inlined_Subprogram (New_N,
5255 Copy_Generic_Node
5256 (First_Inlined_Subprogram (N), New_N, False));
5258 Set_Aux_Decls_Node (New_N,
5259 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
5261 -- For an assignment node, the assignment is known to be semantically
5262 -- legal if we are instantiating the template. This avoids incorrect
5263 -- diagnostics in generated code.
5265 elsif Nkind (N) = N_Assignment_Statement then
5267 -- Copy name and expression fields in usual manner
5269 Set_Name (New_N,
5270 Copy_Generic_Node (Name (N), New_N, Instantiating));
5272 Set_Expression (New_N,
5273 Copy_Generic_Node (Expression (N), New_N, Instantiating));
5275 if Instantiating then
5276 Set_Assignment_OK (Name (New_N), True);
5277 end if;
5279 elsif Nkind (N) = N_Aggregate
5280 or else Nkind (N) = N_Extension_Aggregate
5281 then
5283 if not Instantiating then
5284 Set_Associated_Node (N, New_N);
5286 else
5287 if Present (Get_Associated_Node (N))
5288 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
5289 then
5290 -- In the generic the aggregate has some composite type. If at
5291 -- the point of instantiation the type has a private view,
5292 -- install the full view (and that of its ancestors, if any).
5294 declare
5295 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
5296 Rt : Entity_Id;
5298 begin
5299 if Present (T)
5300 and then Is_Private_Type (T)
5301 then
5302 Switch_View (T);
5303 end if;
5305 if Present (T)
5306 and then Is_Tagged_Type (T)
5307 and then Is_Derived_Type (T)
5308 then
5309 Rt := Root_Type (T);
5311 loop
5312 T := Etype (T);
5314 if Is_Private_Type (T) then
5315 Switch_View (T);
5316 end if;
5318 exit when T = Rt;
5319 end loop;
5320 end if;
5321 end;
5322 end if;
5323 end if;
5325 -- Do not copy the associated node, which points to
5326 -- the generic copy of the aggregate.
5328 declare
5329 use Atree.Unchecked_Access;
5330 -- This code section is part of the implementation of an untyped
5331 -- tree traversal, so it needs direct access to node fields.
5333 begin
5334 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
5335 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
5336 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
5337 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
5338 end;
5340 -- Allocators do not have an identifier denoting the access type,
5341 -- so we must locate it through the expression to check whether
5342 -- the views are consistent.
5344 elsif Nkind (N) = N_Allocator
5345 and then Nkind (Expression (N)) = N_Qualified_Expression
5346 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
5347 and then Instantiating
5348 then
5349 declare
5350 T : constant Node_Id :=
5351 Get_Associated_Node (Subtype_Mark (Expression (N)));
5352 Acc_T : Entity_Id;
5354 begin
5355 if Present (T) then
5356 -- Retrieve the allocator node in the generic copy
5358 Acc_T := Etype (Parent (Parent (T)));
5359 if Present (Acc_T)
5360 and then Is_Private_Type (Acc_T)
5361 then
5362 Switch_View (Acc_T);
5363 end if;
5364 end if;
5366 Copy_Descendants;
5367 end;
5369 -- For a proper body, we must catch the case of a proper body that
5370 -- replaces a stub. This represents the point at which a separate
5371 -- compilation unit, and hence template file, may be referenced, so
5372 -- we must make a new source instantiation entry for the template
5373 -- of the subunit, and ensure that all nodes in the subunit are
5374 -- adjusted using this new source instantiation entry.
5376 elsif Nkind (N) in N_Proper_Body then
5377 declare
5378 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
5380 begin
5381 if Instantiating and then Was_Originally_Stub (N) then
5382 Create_Instantiation_Source
5383 (Instantiation_Node,
5384 Defining_Entity (N),
5385 False,
5386 S_Adjustment);
5387 end if;
5389 -- Now copy the fields of the proper body, using the new
5390 -- adjustment factor if one was needed as per test above.
5392 Copy_Descendants;
5394 -- Restore the original adjustment factor in case changed
5396 S_Adjustment := Save_Adjustment;
5397 end;
5399 -- Don't copy Ident or Comment pragmas, since the comment belongs
5400 -- to the generic unit, not to the instantiating unit.
5402 elsif Nkind (N) = N_Pragma
5403 and then Instantiating
5404 then
5405 declare
5406 Prag_Id : constant Pragma_Id := Get_Pragma_Id (Chars (N));
5408 begin
5409 if Prag_Id = Pragma_Ident
5410 or else Prag_Id = Pragma_Comment
5411 then
5412 New_N := Make_Null_Statement (Sloc (N));
5414 else
5415 Copy_Descendants;
5416 end if;
5417 end;
5419 elsif Nkind (N) = N_Integer_Literal
5420 or else Nkind (N) = N_Real_Literal
5421 then
5422 -- No descendant fields need traversing
5424 null;
5426 -- For the remaining nodes, copy recursively their descendants
5428 else
5429 Copy_Descendants;
5431 if Instantiating
5432 and then Nkind (N) = N_Subprogram_Body
5433 then
5434 Set_Generic_Parent (Specification (New_N), N);
5435 end if;
5436 end if;
5438 return New_N;
5439 end Copy_Generic_Node;
5441 ----------------------------
5442 -- Denotes_Formal_Package --
5443 ----------------------------
5445 function Denotes_Formal_Package
5446 (Pack : Entity_Id;
5447 On_Exit : Boolean := False) return Boolean
5449 Par : Entity_Id;
5450 Scop : constant Entity_Id := Scope (Pack);
5451 E : Entity_Id;
5453 begin
5454 if On_Exit then
5455 Par :=
5456 Instance_Envs.Table
5457 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
5458 else
5459 Par := Current_Instantiated_Parent.Act_Id;
5460 end if;
5462 if Ekind (Scop) = E_Generic_Package
5463 or else Nkind (Unit_Declaration_Node (Scop)) =
5464 N_Generic_Subprogram_Declaration
5465 then
5466 return True;
5468 elsif Nkind (Parent (Pack)) = N_Formal_Package_Declaration then
5469 return True;
5471 elsif No (Par) then
5472 return False;
5474 else
5475 -- Check whether this package is associated with a formal
5476 -- package of the enclosing instantiation. Iterate over the
5477 -- list of renamings.
5479 E := First_Entity (Par);
5480 while Present (E) loop
5481 if Ekind (E) /= E_Package
5482 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
5483 then
5484 null;
5485 elsif Renamed_Object (E) = Par then
5486 return False;
5488 elsif Renamed_Object (E) = Pack then
5489 return True;
5490 end if;
5492 Next_Entity (E);
5493 end loop;
5495 return False;
5496 end if;
5497 end Denotes_Formal_Package;
5499 -----------------
5500 -- End_Generic --
5501 -----------------
5503 procedure End_Generic is
5504 begin
5505 -- ??? More things could be factored out in this
5506 -- routine. Should probably be done at a later stage.
5508 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
5509 Generic_Flags.Decrement_Last;
5511 Expander_Mode_Restore;
5512 end End_Generic;
5514 ----------------------
5515 -- Find_Actual_Type --
5516 ----------------------
5518 function Find_Actual_Type
5519 (Typ : Entity_Id;
5520 Gen_Scope : Entity_Id) return Entity_Id
5522 T : Entity_Id;
5524 begin
5525 if not Is_Child_Unit (Gen_Scope) then
5526 return Get_Instance_Of (Typ);
5528 elsif not Is_Generic_Type (Typ)
5529 or else Scope (Typ) = Gen_Scope
5530 then
5531 return Get_Instance_Of (Typ);
5533 else
5534 T := Current_Entity (Typ);
5535 while Present (T) loop
5536 if In_Open_Scopes (Scope (T)) then
5537 return T;
5538 end if;
5540 T := Homonym (T);
5541 end loop;
5543 return Typ;
5544 end if;
5545 end Find_Actual_Type;
5547 ----------------------------
5548 -- Freeze_Subprogram_Body --
5549 ----------------------------
5551 procedure Freeze_Subprogram_Body
5552 (Inst_Node : Node_Id;
5553 Gen_Body : Node_Id;
5554 Pack_Id : Entity_Id)
5556 F_Node : Node_Id;
5557 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
5558 Par : constant Entity_Id := Scope (Gen_Unit);
5559 Enc_G : Entity_Id;
5560 Enc_I : Node_Id;
5561 E_G_Id : Entity_Id;
5563 function Earlier (N1, N2 : Node_Id) return Boolean;
5564 -- Yields True if N1 and N2 appear in the same compilation unit,
5565 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
5566 -- traversal of the tree for the unit.
5568 function Enclosing_Body (N : Node_Id) return Node_Id;
5569 -- Find innermost package body that encloses the given node, and which
5570 -- is not a compilation unit. Freeze nodes for the instance, or for its
5571 -- enclosing body, may be inserted after the enclosing_body of the
5572 -- generic unit.
5574 function Package_Freeze_Node (B : Node_Id) return Node_Id;
5575 -- Find entity for given package body, and locate or create a freeze
5576 -- node for it.
5578 function True_Parent (N : Node_Id) return Node_Id;
5579 -- For a subunit, return parent of corresponding stub
5581 -------------
5582 -- Earlier --
5583 -------------
5585 function Earlier (N1, N2 : Node_Id) return Boolean is
5586 D1 : Integer := 0;
5587 D2 : Integer := 0;
5588 P1 : Node_Id := N1;
5589 P2 : Node_Id := N2;
5591 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
5592 -- Find distance from given node to enclosing compilation unit
5594 ----------------
5595 -- Find_Depth --
5596 ----------------
5598 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
5599 begin
5600 while Present (P)
5601 and then Nkind (P) /= N_Compilation_Unit
5602 loop
5603 P := True_Parent (P);
5604 D := D + 1;
5605 end loop;
5606 end Find_Depth;
5608 -- Start of procesing for Earlier
5610 begin
5611 Find_Depth (P1, D1);
5612 Find_Depth (P2, D2);
5614 if P1 /= P2 then
5615 return False;
5616 else
5617 P1 := N1;
5618 P2 := N2;
5619 end if;
5621 while D1 > D2 loop
5622 P1 := True_Parent (P1);
5623 D1 := D1 - 1;
5624 end loop;
5626 while D2 > D1 loop
5627 P2 := True_Parent (P2);
5628 D2 := D2 - 1;
5629 end loop;
5631 -- At this point P1 and P2 are at the same distance from the root.
5632 -- We examine their parents until we find a common declarative
5633 -- list, at which point we can establish their relative placement
5634 -- by comparing their ultimate slocs. If we reach the root,
5635 -- N1 and N2 do not descend from the same declarative list (e.g.
5636 -- one is nested in the declarative part and the other is in a block
5637 -- in the statement part) and the earlier one is already frozen.
5639 while not Is_List_Member (P1)
5640 or else not Is_List_Member (P2)
5641 or else List_Containing (P1) /= List_Containing (P2)
5642 loop
5643 P1 := True_Parent (P1);
5644 P2 := True_Parent (P2);
5646 if Nkind (Parent (P1)) = N_Subunit then
5647 P1 := Corresponding_Stub (Parent (P1));
5648 end if;
5650 if Nkind (Parent (P2)) = N_Subunit then
5651 P2 := Corresponding_Stub (Parent (P2));
5652 end if;
5654 if P1 = P2 then
5655 return False;
5656 end if;
5657 end loop;
5659 return
5660 Top_Level_Location (Sloc (P1)) < Top_Level_Location (Sloc (P2));
5661 end Earlier;
5663 --------------------
5664 -- Enclosing_Body --
5665 --------------------
5667 function Enclosing_Body (N : Node_Id) return Node_Id is
5668 P : Node_Id := Parent (N);
5670 begin
5671 while Present (P)
5672 and then Nkind (Parent (P)) /= N_Compilation_Unit
5673 loop
5674 if Nkind (P) = N_Package_Body then
5676 if Nkind (Parent (P)) = N_Subunit then
5677 return Corresponding_Stub (Parent (P));
5678 else
5679 return P;
5680 end if;
5681 end if;
5683 P := True_Parent (P);
5684 end loop;
5686 return Empty;
5687 end Enclosing_Body;
5689 -------------------------
5690 -- Package_Freeze_Node --
5691 -------------------------
5693 function Package_Freeze_Node (B : Node_Id) return Node_Id is
5694 Id : Entity_Id;
5696 begin
5697 if Nkind (B) = N_Package_Body then
5698 Id := Corresponding_Spec (B);
5700 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
5701 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
5702 end if;
5704 Ensure_Freeze_Node (Id);
5705 return Freeze_Node (Id);
5706 end Package_Freeze_Node;
5708 -----------------
5709 -- True_Parent --
5710 -----------------
5712 function True_Parent (N : Node_Id) return Node_Id is
5713 begin
5714 if Nkind (Parent (N)) = N_Subunit then
5715 return Parent (Corresponding_Stub (Parent (N)));
5716 else
5717 return Parent (N);
5718 end if;
5719 end True_Parent;
5721 -- Start of processing of Freeze_Subprogram_Body
5723 begin
5724 -- If the instance and the generic body appear within the same
5725 -- unit, and the instance preceeds the generic, the freeze node for
5726 -- the instance must appear after that of the generic. If the generic
5727 -- is nested within another instance I2, then current instance must
5728 -- be frozen after I2. In both cases, the freeze nodes are those of
5729 -- enclosing packages. Otherwise, the freeze node is placed at the end
5730 -- of the current declarative part.
5732 Enc_G := Enclosing_Body (Gen_Body);
5733 Enc_I := Enclosing_Body (Inst_Node);
5734 Ensure_Freeze_Node (Pack_Id);
5735 F_Node := Freeze_Node (Pack_Id);
5737 if Is_Generic_Instance (Par)
5738 and then Present (Freeze_Node (Par))
5739 and then
5740 In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
5741 then
5742 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
5744 -- The parent was a premature instantiation. Insert freeze
5745 -- node at the end the current declarative part.
5747 Insert_After_Last_Decl (Inst_Node, F_Node);
5749 else
5750 Insert_After (Freeze_Node (Par), F_Node);
5751 end if;
5753 -- The body enclosing the instance should be frozen after the body
5754 -- that includes the generic, because the body of the instance may
5755 -- make references to entities therein. If the two are not in the
5756 -- same declarative part, or if the one enclosing the instance is
5757 -- frozen already, freeze the instance at the end of the current
5758 -- declarative part.
5760 elsif Is_Generic_Instance (Par)
5761 and then Present (Freeze_Node (Par))
5762 and then Present (Enc_I)
5763 then
5764 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
5765 or else
5766 (Nkind (Enc_I) = N_Package_Body
5767 and then
5768 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
5769 then
5770 -- The enclosing package may contain several instances. Rather
5771 -- than computing the earliest point at which to insert its
5772 -- freeze node, we place it at the end of the declarative part
5773 -- of the parent of the generic.
5775 Insert_After_Last_Decl
5776 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
5777 end if;
5779 Insert_After_Last_Decl (Inst_Node, F_Node);
5781 elsif Present (Enc_G)
5782 and then Present (Enc_I)
5783 and then Enc_G /= Enc_I
5784 and then Earlier (Inst_Node, Gen_Body)
5785 then
5786 if Nkind (Enc_G) = N_Package_Body then
5787 E_G_Id := Corresponding_Spec (Enc_G);
5788 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
5789 E_G_Id :=
5790 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
5791 end if;
5793 -- Freeze package that encloses instance, and place node after
5794 -- package that encloses generic. If enclosing package is already
5795 -- frozen we have to assume it is at the proper place. This may
5796 -- be a potential ABE that requires dynamic checking.
5798 Insert_After_Last_Decl (Enc_G, Package_Freeze_Node (Enc_I));
5800 -- Freeze enclosing subunit before instance
5802 Ensure_Freeze_Node (E_G_Id);
5804 if not Is_List_Member (Freeze_Node (E_G_Id)) then
5805 Insert_After (Enc_G, Freeze_Node (E_G_Id));
5806 end if;
5808 Insert_After_Last_Decl (Inst_Node, F_Node);
5810 else
5811 -- If none of the above, insert freeze node at the end of the
5812 -- current declarative part.
5814 Insert_After_Last_Decl (Inst_Node, F_Node);
5815 end if;
5816 end Freeze_Subprogram_Body;
5818 ----------------
5819 -- Get_Gen_Id --
5820 ----------------
5822 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
5823 begin
5824 return Generic_Renamings.Table (E).Gen_Id;
5825 end Get_Gen_Id;
5827 ---------------------
5828 -- Get_Instance_Of --
5829 ---------------------
5831 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
5832 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
5834 begin
5835 if Res /= Assoc_Null then
5836 return Generic_Renamings.Table (Res).Act_Id;
5837 else
5838 -- On exit, entity is not instantiated: not a generic parameter,
5839 -- or else parameter of an inner generic unit.
5841 return A;
5842 end if;
5843 end Get_Instance_Of;
5845 ------------------------------------
5846 -- Get_Package_Instantiation_Node --
5847 ------------------------------------
5849 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
5850 Decl : Node_Id := Unit_Declaration_Node (A);
5851 Inst : Node_Id;
5853 begin
5854 -- If the Package_Instantiation attribute has been set on the package
5855 -- entity, then use it directly when it (or its Original_Node) refers
5856 -- to an N_Package_Instantiation node. In principle it should be
5857 -- possible to have this field set in all cases, which should be
5858 -- investigated, and would allow this function to be significantly
5859 -- simplified. ???
5861 if Present (Package_Instantiation (A)) then
5862 if Nkind (Package_Instantiation (A)) = N_Package_Instantiation then
5863 return Package_Instantiation (A);
5865 elsif Nkind (Original_Node (Package_Instantiation (A)))
5866 = N_Package_Instantiation
5867 then
5868 return Original_Node (Package_Instantiation (A));
5869 end if;
5870 end if;
5872 -- If the instantiation is a compilation unit that does not need a
5873 -- body then the instantiation node has been rewritten as a package
5874 -- declaration for the instance, and we return the original node.
5876 -- If it is a compilation unit and the instance node has not been
5877 -- rewritten, then it is still the unit of the compilation. Finally,
5878 -- if a body is present, this is a parent of the main unit whose body
5879 -- has been compiled for inlining purposes, and the instantiation node
5880 -- has been rewritten with the instance body.
5882 -- Otherwise the instantiation node appears after the declaration.
5883 -- If the entity is a formal package, the declaration may have been
5884 -- rewritten as a generic declaration (in the case of a formal with a
5885 -- box) or left as a formal package declaration if it has actuals, and
5886 -- is found with a forward search.
5888 if Nkind (Parent (Decl)) = N_Compilation_Unit then
5889 if Nkind (Decl) = N_Package_Declaration
5890 and then Present (Corresponding_Body (Decl))
5891 then
5892 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
5893 end if;
5895 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
5896 return Original_Node (Decl);
5897 else
5898 return Unit (Parent (Decl));
5899 end if;
5901 elsif Nkind (Decl) = N_Generic_Package_Declaration
5902 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
5903 then
5904 return Original_Node (Decl);
5906 else
5907 Inst := Next (Decl);
5908 while Nkind (Inst) /= N_Package_Instantiation
5909 and then Nkind (Inst) /= N_Formal_Package_Declaration
5910 loop
5911 Next (Inst);
5912 end loop;
5914 return Inst;
5915 end if;
5916 end Get_Package_Instantiation_Node;
5918 ------------------------
5919 -- Has_Been_Exchanged --
5920 ------------------------
5922 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
5923 Next : Elmt_Id := First_Elmt (Exchanged_Views);
5925 begin
5926 while Present (Next) loop
5927 if Full_View (Node (Next)) = E then
5928 return True;
5929 end if;
5931 Next_Elmt (Next);
5932 end loop;
5934 return False;
5935 end Has_Been_Exchanged;
5937 ----------
5938 -- Hash --
5939 ----------
5941 function Hash (F : Entity_Id) return HTable_Range is
5942 begin
5943 return HTable_Range (F mod HTable_Size);
5944 end Hash;
5946 ------------------------
5947 -- Hide_Current_Scope --
5948 ------------------------
5950 procedure Hide_Current_Scope is
5951 C : constant Entity_Id := Current_Scope;
5952 E : Entity_Id;
5954 begin
5955 Set_Is_Hidden_Open_Scope (C);
5956 E := First_Entity (C);
5958 while Present (E) loop
5959 if Is_Immediately_Visible (E) then
5960 Set_Is_Immediately_Visible (E, False);
5961 Append_Elmt (E, Hidden_Entities);
5962 end if;
5964 Next_Entity (E);
5965 end loop;
5967 -- Make the scope name invisible as well. This is necessary, but
5968 -- might conflict with calls to Rtsfind later on, in case the scope
5969 -- is a predefined one. There is no clean solution to this problem, so
5970 -- for now we depend on the user not redefining Standard itself in one
5971 -- of the parent units.
5973 if Is_Immediately_Visible (C)
5974 and then C /= Standard_Standard
5975 then
5976 Set_Is_Immediately_Visible (C, False);
5977 Append_Elmt (C, Hidden_Entities);
5978 end if;
5980 end Hide_Current_Scope;
5982 --------------
5983 -- Init_Env --
5984 --------------
5986 procedure Init_Env is
5987 Saved : Instance_Env;
5989 begin
5990 Saved.Ada_Version := Ada_Version;
5991 Saved.Ada_Version_Explicit := Ada_Version_Explicit;
5992 Saved.Instantiated_Parent := Current_Instantiated_Parent;
5993 Saved.Exchanged_Views := Exchanged_Views;
5994 Saved.Hidden_Entities := Hidden_Entities;
5995 Saved.Current_Sem_Unit := Current_Sem_Unit;
5996 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
5997 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
5998 Instance_Envs.Increment_Last;
5999 Instance_Envs.Table (Instance_Envs.Last) := Saved;
6001 Exchanged_Views := New_Elmt_List;
6002 Hidden_Entities := New_Elmt_List;
6004 -- Make dummy entry for Instantiated parent. If generic unit is
6005 -- legal, this is set properly in Set_Instance_Env.
6007 Current_Instantiated_Parent :=
6008 (Current_Scope, Current_Scope, Assoc_Null);
6009 end Init_Env;
6011 ------------------------------
6012 -- In_Same_Declarative_Part --
6013 ------------------------------
6015 function In_Same_Declarative_Part
6016 (F_Node : Node_Id;
6017 Inst : Node_Id) return Boolean
6019 Decls : constant Node_Id := Parent (F_Node);
6020 Nod : Node_Id := Parent (Inst);
6022 begin
6023 while Present (Nod) loop
6024 if Nod = Decls then
6025 return True;
6027 elsif Nkind (Nod) = N_Subprogram_Body
6028 or else Nkind (Nod) = N_Package_Body
6029 or else Nkind (Nod) = N_Task_Body
6030 or else Nkind (Nod) = N_Protected_Body
6031 or else Nkind (Nod) = N_Block_Statement
6032 then
6033 return False;
6035 elsif Nkind (Nod) = N_Subunit then
6036 Nod := Corresponding_Stub (Nod);
6038 elsif Nkind (Nod) = N_Compilation_Unit then
6039 return False;
6040 else
6041 Nod := Parent (Nod);
6042 end if;
6043 end loop;
6045 return False;
6046 end In_Same_Declarative_Part;
6048 ---------------------
6049 -- In_Main_Context --
6050 ---------------------
6052 function In_Main_Context (E : Entity_Id) return Boolean is
6053 Context : List_Id;
6054 Clause : Node_Id;
6055 Nam : Node_Id;
6057 begin
6058 if not Is_Compilation_Unit (E)
6059 or else Ekind (E) /= E_Package
6060 or else In_Private_Part (E)
6061 then
6062 return False;
6063 end if;
6065 Context := Context_Items (Cunit (Main_Unit));
6067 Clause := First (Context);
6068 while Present (Clause) loop
6069 if Nkind (Clause) = N_With_Clause then
6070 Nam := Name (Clause);
6072 -- If the current scope is part of the context of the main unit,
6073 -- analysis of the corresponding with_clause is not complete, and
6074 -- the entity is not set. We use the Chars field directly, which
6075 -- might produce false positives in rare cases, but guarantees
6076 -- that we produce all the instance bodies we will need.
6078 if (Nkind (Nam) = N_Identifier
6079 and then Chars (Nam) = Chars (E))
6080 or else (Nkind (Nam) = N_Selected_Component
6081 and then Chars (Selector_Name (Nam)) = Chars (E))
6082 then
6083 return True;
6084 end if;
6085 end if;
6087 Next (Clause);
6088 end loop;
6090 return False;
6091 end In_Main_Context;
6093 ---------------------
6094 -- Inherit_Context --
6095 ---------------------
6097 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
6098 Current_Context : List_Id;
6099 Current_Unit : Node_Id;
6100 Item : Node_Id;
6101 New_I : Node_Id;
6103 begin
6104 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
6106 -- The inherited context is attached to the enclosing compilation
6107 -- unit. This is either the main unit, or the declaration for the
6108 -- main unit (in case the instantation appears within the package
6109 -- declaration and the main unit is its body).
6111 Current_Unit := Parent (Inst);
6112 while Present (Current_Unit)
6113 and then Nkind (Current_Unit) /= N_Compilation_Unit
6114 loop
6115 Current_Unit := Parent (Current_Unit);
6116 end loop;
6118 Current_Context := Context_Items (Current_Unit);
6120 Item := First (Context_Items (Parent (Gen_Decl)));
6121 while Present (Item) loop
6122 if Nkind (Item) = N_With_Clause then
6123 New_I := New_Copy (Item);
6124 Set_Implicit_With (New_I, True);
6125 Append (New_I, Current_Context);
6126 end if;
6128 Next (Item);
6129 end loop;
6130 end if;
6131 end Inherit_Context;
6133 ----------------
6134 -- Initialize --
6135 ----------------
6137 procedure Initialize is
6138 begin
6139 Generic_Renamings.Init;
6140 Instance_Envs.Init;
6141 Generic_Flags.Init;
6142 Generic_Renamings_HTable.Reset;
6143 Circularity_Detected := False;
6144 Exchanged_Views := No_Elist;
6145 Hidden_Entities := No_Elist;
6146 end Initialize;
6148 ----------------------------
6149 -- Insert_After_Last_Decl --
6150 ----------------------------
6152 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id) is
6153 L : List_Id := List_Containing (N);
6154 P : constant Node_Id := Parent (L);
6156 begin
6157 if not Is_List_Member (F_Node) then
6158 if Nkind (P) = N_Package_Specification
6159 and then L = Visible_Declarations (P)
6160 and then Present (Private_Declarations (P))
6161 and then not Is_Empty_List (Private_Declarations (P))
6162 then
6163 L := Private_Declarations (P);
6164 end if;
6166 Insert_After (Last (L), F_Node);
6167 end if;
6168 end Insert_After_Last_Decl;
6170 ------------------
6171 -- Install_Body --
6172 ------------------
6174 procedure Install_Body
6175 (Act_Body : Node_Id;
6176 N : Node_Id;
6177 Gen_Body : Node_Id;
6178 Gen_Decl : Node_Id)
6180 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
6181 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
6182 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
6183 Par : constant Entity_Id := Scope (Gen_Id);
6184 Gen_Unit : constant Node_Id :=
6185 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
6186 Orig_Body : Node_Id := Gen_Body;
6187 F_Node : Node_Id;
6188 Body_Unit : Node_Id;
6190 Must_Delay : Boolean;
6192 function Enclosing_Subp (Id : Entity_Id) return Entity_Id;
6193 -- Find subprogram (if any) that encloses instance and/or generic body
6195 function True_Sloc (N : Node_Id) return Source_Ptr;
6196 -- If the instance is nested inside a generic unit, the Sloc of the
6197 -- instance indicates the place of the original definition, not the
6198 -- point of the current enclosing instance. Pending a better usage of
6199 -- Slocs to indicate instantiation places, we determine the place of
6200 -- origin of a node by finding the maximum sloc of any ancestor node.
6201 -- Why is this not equivalent to Top_Level_Location ???
6203 --------------------
6204 -- Enclosing_Subp --
6205 --------------------
6207 function Enclosing_Subp (Id : Entity_Id) return Entity_Id is
6208 Scop : Entity_Id := Scope (Id);
6210 begin
6211 while Scop /= Standard_Standard
6212 and then not Is_Overloadable (Scop)
6213 loop
6214 Scop := Scope (Scop);
6215 end loop;
6217 return Scop;
6218 end Enclosing_Subp;
6220 ---------------
6221 -- True_Sloc --
6222 ---------------
6224 function True_Sloc (N : Node_Id) return Source_Ptr is
6225 Res : Source_Ptr;
6226 N1 : Node_Id;
6228 begin
6229 Res := Sloc (N);
6230 N1 := N;
6231 while Present (N1) and then N1 /= Act_Unit loop
6232 if Sloc (N1) > Res then
6233 Res := Sloc (N1);
6234 end if;
6236 N1 := Parent (N1);
6237 end loop;
6239 return Res;
6240 end True_Sloc;
6242 -- Start of processing for Install_Body
6244 begin
6245 -- If the body is a subunit, the freeze point is the corresponding
6246 -- stub in the current compilation, not the subunit itself.
6248 if Nkind (Parent (Gen_Body)) = N_Subunit then
6249 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
6250 else
6251 Orig_Body := Gen_Body;
6252 end if;
6254 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
6256 -- If the instantiation and the generic definition appear in the
6257 -- same package declaration, this is an early instantiation.
6258 -- If they appear in the same declarative part, it is an early
6259 -- instantiation only if the generic body appears textually later,
6260 -- and the generic body is also in the main unit.
6262 -- If instance is nested within a subprogram, and the generic body is
6263 -- not, the instance is delayed because the enclosing body is. If
6264 -- instance and body are within the same scope, or the same sub-
6265 -- program body, indicate explicitly that the instance is delayed.
6267 Must_Delay :=
6268 (Gen_Unit = Act_Unit
6269 and then ((Nkind (Gen_Unit) = N_Package_Declaration)
6270 or else Nkind (Gen_Unit) = N_Generic_Package_Declaration
6271 or else (Gen_Unit = Body_Unit
6272 and then True_Sloc (N) < Sloc (Orig_Body)))
6273 and then Is_In_Main_Unit (Gen_Unit)
6274 and then (Scope (Act_Id) = Scope (Gen_Id)
6275 or else
6276 Enclosing_Subp (Act_Id) = Enclosing_Subp (Gen_Id)));
6278 -- If this is an early instantiation, the freeze node is placed after
6279 -- the generic body. Otherwise, if the generic appears in an instance,
6280 -- we cannot freeze the current instance until the outer one is frozen.
6281 -- This is only relevant if the current instance is nested within some
6282 -- inner scope not itself within the outer instance. If this scope is
6283 -- a package body in the same declarative part as the outer instance,
6284 -- then that body needs to be frozen after the outer instance. Finally,
6285 -- if no delay is needed, we place the freeze node at the end of the
6286 -- current declarative part.
6288 if Expander_Active then
6289 Ensure_Freeze_Node (Act_Id);
6290 F_Node := Freeze_Node (Act_Id);
6292 if Must_Delay then
6293 Insert_After (Orig_Body, F_Node);
6295 elsif Is_Generic_Instance (Par)
6296 and then Present (Freeze_Node (Par))
6297 and then Scope (Act_Id) /= Par
6298 then
6299 -- Freeze instance of inner generic after instance of enclosing
6300 -- generic.
6302 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
6303 Insert_After (Freeze_Node (Par), F_Node);
6305 -- Freeze package enclosing instance of inner generic after
6306 -- instance of enclosing generic.
6308 elsif Nkind (Parent (N)) = N_Package_Body
6309 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
6310 then
6312 declare
6313 Enclosing : constant Entity_Id :=
6314 Corresponding_Spec (Parent (N));
6316 begin
6317 Insert_After_Last_Decl (N, F_Node);
6318 Ensure_Freeze_Node (Enclosing);
6320 if not Is_List_Member (Freeze_Node (Enclosing)) then
6321 Insert_After (Freeze_Node (Par), Freeze_Node (Enclosing));
6322 end if;
6323 end;
6325 else
6326 Insert_After_Last_Decl (N, F_Node);
6327 end if;
6329 else
6330 Insert_After_Last_Decl (N, F_Node);
6331 end if;
6332 end if;
6334 Set_Is_Frozen (Act_Id);
6335 Insert_Before (N, Act_Body);
6336 Mark_Rewrite_Insertion (Act_Body);
6337 end Install_Body;
6339 --------------------
6340 -- Install_Parent --
6341 --------------------
6343 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
6344 Ancestors : constant Elist_Id := New_Elmt_List;
6345 S : constant Entity_Id := Current_Scope;
6346 Inst_Par : Entity_Id;
6347 First_Par : Entity_Id;
6348 Inst_Node : Node_Id;
6349 Gen_Par : Entity_Id;
6350 First_Gen : Entity_Id;
6351 Elmt : Elmt_Id;
6353 procedure Install_Formal_Packages (Par : Entity_Id);
6354 -- If any of the formals of the parent are formal packages with box,
6355 -- their formal parts are visible in the parent and thus in the child
6356 -- unit as well. Analogous to what is done in Check_Generic_Actuals
6357 -- for the unit itself.
6359 procedure Install_Noninstance_Specs (Par : Entity_Id);
6360 -- Install the scopes of noninstance parent units ending with Par
6362 procedure Install_Spec (Par : Entity_Id);
6363 -- The child unit is within the declarative part of the parent, so
6364 -- the declarations within the parent are immediately visible.
6366 -----------------------------
6367 -- Install_Formal_Packages --
6368 -----------------------------
6370 procedure Install_Formal_Packages (Par : Entity_Id) is
6371 E : Entity_Id;
6373 begin
6374 E := First_Entity (Par);
6375 while Present (E) loop
6376 if Ekind (E) = E_Package
6377 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
6378 then
6379 -- If this is the renaming for the parent instance, done
6381 if Renamed_Object (E) = Par then
6382 exit;
6384 -- The visibility of a formal of an enclosing generic is
6385 -- already correct.
6387 elsif Denotes_Formal_Package (E) then
6388 null;
6390 elsif Present (Associated_Formal_Package (E))
6391 and then Box_Present (Parent (Associated_Formal_Package (E)))
6392 then
6393 Check_Generic_Actuals (Renamed_Object (E), True);
6394 Set_Is_Hidden (E, False);
6395 end if;
6396 end if;
6398 Next_Entity (E);
6399 end loop;
6400 end Install_Formal_Packages;
6402 -------------------------------
6403 -- Install_Noninstance_Specs --
6404 -------------------------------
6406 procedure Install_Noninstance_Specs (Par : Entity_Id) is
6407 begin
6408 if Present (Par)
6409 and then Par /= Standard_Standard
6410 and then not In_Open_Scopes (Par)
6411 then
6412 Install_Noninstance_Specs (Scope (Par));
6413 Install_Spec (Par);
6414 end if;
6415 end Install_Noninstance_Specs;
6417 ------------------
6418 -- Install_Spec --
6419 ------------------
6421 procedure Install_Spec (Par : Entity_Id) is
6422 Spec : constant Node_Id :=
6423 Specification (Unit_Declaration_Node (Par));
6425 begin
6426 -- If this parent of the child instance is a top-level unit,
6427 -- then record the unit and its visibility for later resetting
6428 -- in Remove_Parent. We exclude units that are generic instances,
6429 -- as we only want to record this information for the ultimate
6430 -- top-level noninstance parent (is that always correct???).
6432 if Scope (Par) = Standard_Standard
6433 and then not Is_Generic_Instance (Par)
6434 then
6435 Parent_Unit_Visible := Is_Immediately_Visible (Par);
6436 Instance_Parent_Unit := Par;
6437 end if;
6439 -- Open the parent scope and make it and its declarations visible.
6440 -- If this point is not within a body, then only the visible
6441 -- declarations should be made visible, and installation of the
6442 -- private declarations is deferred until the appropriate point
6443 -- within analysis of the spec being instantiated (see the handling
6444 -- of parent visibility in Analyze_Package_Specification). This is
6445 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
6446 -- private view problems that occur when compiling instantiations of
6447 -- a generic child of that package (Generic_Dispatching_Constructor).
6448 -- If the instance freezes a tagged type, inlinings of operations
6449 -- from Ada.Tags may need the full view of type Tag. If inlining
6450 -- took proper account of establishing visibility of inlined
6451 -- subprograms' parents then it should be possible to remove this
6452 -- special check. ???
6454 New_Scope (Par);
6455 Set_Is_Immediately_Visible (Par);
6456 Install_Visible_Declarations (Par);
6457 Set_Use (Visible_Declarations (Spec));
6459 if In_Body or else Is_RTU (Par, Ada_Tags) then
6460 Install_Private_Declarations (Par);
6461 Set_Use (Private_Declarations (Spec));
6462 end if;
6463 end Install_Spec;
6465 -- Start of processing for Install_Parent
6467 begin
6468 -- We need to install the parent instance to compile the instantiation
6469 -- of the child, but the child instance must appear in the current
6470 -- scope. Given that we cannot place the parent above the current
6471 -- scope in the scope stack, we duplicate the current scope and unstack
6472 -- both after the instantiation is complete.
6474 -- If the parent is itself the instantiation of a child unit, we must
6475 -- also stack the instantiation of its parent, and so on. Each such
6476 -- ancestor is the prefix of the name in a prior instantiation.
6478 -- If this is a nested instance, the parent unit itself resolves to
6479 -- a renaming of the parent instance, whose declaration we need.
6481 -- Finally, the parent may be a generic (not an instance) when the
6482 -- child unit appears as a formal package.
6484 Inst_Par := P;
6486 if Present (Renamed_Entity (Inst_Par)) then
6487 Inst_Par := Renamed_Entity (Inst_Par);
6488 end if;
6490 First_Par := Inst_Par;
6492 Gen_Par :=
6493 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
6495 First_Gen := Gen_Par;
6497 while Present (Gen_Par)
6498 and then Is_Child_Unit (Gen_Par)
6499 loop
6500 -- Load grandparent instance as well
6502 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
6504 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
6505 Inst_Par := Entity (Prefix (Name (Inst_Node)));
6507 if Present (Renamed_Entity (Inst_Par)) then
6508 Inst_Par := Renamed_Entity (Inst_Par);
6509 end if;
6511 Gen_Par :=
6512 Generic_Parent
6513 (Specification (Unit_Declaration_Node (Inst_Par)));
6515 if Present (Gen_Par) then
6516 Prepend_Elmt (Inst_Par, Ancestors);
6518 else
6519 -- Parent is not the name of an instantiation
6521 Install_Noninstance_Specs (Inst_Par);
6523 exit;
6524 end if;
6526 else
6527 -- Previous error
6529 exit;
6530 end if;
6531 end loop;
6533 if Present (First_Gen) then
6534 Append_Elmt (First_Par, Ancestors);
6536 else
6537 Install_Noninstance_Specs (First_Par);
6538 end if;
6540 if not Is_Empty_Elmt_List (Ancestors) then
6541 Elmt := First_Elmt (Ancestors);
6543 while Present (Elmt) loop
6544 Install_Spec (Node (Elmt));
6545 Install_Formal_Packages (Node (Elmt));
6547 Next_Elmt (Elmt);
6548 end loop;
6549 end if;
6551 if not In_Body then
6552 New_Scope (S);
6553 end if;
6554 end Install_Parent;
6556 --------------------------------
6557 -- Instantiate_Formal_Package --
6558 --------------------------------
6560 function Instantiate_Formal_Package
6561 (Formal : Node_Id;
6562 Actual : Node_Id;
6563 Analyzed_Formal : Node_Id) return List_Id
6565 Loc : constant Source_Ptr := Sloc (Actual);
6566 Actual_Pack : Entity_Id;
6567 Formal_Pack : Entity_Id;
6568 Gen_Parent : Entity_Id;
6569 Decls : List_Id;
6570 Nod : Node_Id;
6571 Parent_Spec : Node_Id;
6573 procedure Find_Matching_Actual
6574 (F : Node_Id;
6575 Act : in out Entity_Id);
6576 -- We need to associate each formal entity in the formal package
6577 -- with the corresponding entity in the actual package. The actual
6578 -- package has been analyzed and possibly expanded, and as a result
6579 -- there is no one-to-one correspondence between the two lists (for
6580 -- example, the actual may include subtypes, itypes, and inherited
6581 -- primitive operations, interspersed among the renaming declarations
6582 -- for the actuals) . We retrieve the corresponding actual by name
6583 -- because each actual has the same name as the formal, and they do
6584 -- appear in the same order.
6586 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
6587 -- Retrieve entity of defining entity of generic formal parameter.
6588 -- Only the declarations of formals need to be considered when
6589 -- linking them to actuals, but the declarative list may include
6590 -- internal entities generated during analysis, and those are ignored.
6592 procedure Match_Formal_Entity
6593 (Formal_Node : Node_Id;
6594 Formal_Ent : Entity_Id;
6595 Actual_Ent : Entity_Id);
6596 -- Associates the formal entity with the actual. In the case
6597 -- where Formal_Ent is a formal package, this procedure iterates
6598 -- through all of its formals and enters associations betwen the
6599 -- actuals occurring in the formal package's corresponding actual
6600 -- package (given by Actual_Ent) and the formal package's formal
6601 -- parameters. This procedure recurses if any of the parameters is
6602 -- itself a package.
6604 function Is_Instance_Of
6605 (Act_Spec : Entity_Id;
6606 Gen_Anc : Entity_Id) return Boolean;
6607 -- The actual can be an instantiation of a generic within another
6608 -- instance, in which case there is no direct link from it to the
6609 -- original generic ancestor. In that case, we recognize that the
6610 -- ultimate ancestor is the same by examining names and scopes.
6612 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id);
6613 -- Within the generic part, entities in the formal package are
6614 -- visible. To validate subsequent type declarations, indicate
6615 -- the correspondence betwen the entities in the analyzed formal,
6616 -- and the entities in the actual package. There are three packages
6617 -- involved in the instantiation of a formal package: the parent
6618 -- generic P1 which appears in the generic declaration, the fake
6619 -- instantiation P2 which appears in the analyzed generic, and whose
6620 -- visible entities may be used in subsequent formals, and the actual
6621 -- P3 in the instance. To validate subsequent formals, me indicate
6622 -- that the entities in P2 are mapped into those of P3. The mapping of
6623 -- entities has to be done recursively for nested packages.
6625 procedure Process_Nested_Formal (Formal : Entity_Id);
6626 -- If the current formal is declared with a box, its own formals are
6627 -- visible in the instance, as they were in the generic, and their
6628 -- Hidden flag must be reset. If some of these formals are themselves
6629 -- packages declared with a box, the processing must be recursive.
6631 --------------------------
6632 -- Find_Matching_Actual --
6633 --------------------------
6635 procedure Find_Matching_Actual
6636 (F : Node_Id;
6637 Act : in out Entity_Id)
6639 Formal_Ent : Entity_Id;
6641 begin
6642 case Nkind (Original_Node (F)) is
6643 when N_Formal_Object_Declaration |
6644 N_Formal_Type_Declaration =>
6645 Formal_Ent := Defining_Identifier (F);
6647 while Chars (Act) /= Chars (Formal_Ent) loop
6648 Next_Entity (Act);
6649 end loop;
6651 when N_Formal_Subprogram_Declaration |
6652 N_Formal_Package_Declaration |
6653 N_Package_Declaration |
6654 N_Generic_Package_Declaration =>
6655 Formal_Ent := Defining_Entity (F);
6657 while Chars (Act) /= Chars (Formal_Ent) loop
6658 Next_Entity (Act);
6659 end loop;
6661 when others =>
6662 raise Program_Error;
6663 end case;
6664 end Find_Matching_Actual;
6666 -------------------------
6667 -- Match_Formal_Entity --
6668 -------------------------
6670 procedure Match_Formal_Entity
6671 (Formal_Node : Node_Id;
6672 Formal_Ent : Entity_Id;
6673 Actual_Ent : Entity_Id)
6675 Act_Pkg : Entity_Id;
6677 begin
6678 Set_Instance_Of (Formal_Ent, Actual_Ent);
6680 if Ekind (Actual_Ent) = E_Package then
6681 -- Record associations for each parameter
6683 Act_Pkg := Actual_Ent;
6685 declare
6686 A_Ent : Entity_Id := First_Entity (Act_Pkg);
6687 F_Ent : Entity_Id;
6688 F_Node : Node_Id;
6690 Gen_Decl : Node_Id;
6691 Formals : List_Id;
6692 Actual : Entity_Id;
6694 begin
6695 -- Retrieve the actual given in the formal package declaration
6697 Actual := Entity (Name (Original_Node (Formal_Node)));
6699 -- The actual in the formal package declaration may be a
6700 -- renamed generic package, in which case we want to retrieve
6701 -- the original generic in order to traverse its formal part.
6703 if Present (Renamed_Entity (Actual)) then
6704 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
6705 else
6706 Gen_Decl := Unit_Declaration_Node (Actual);
6707 end if;
6709 Formals := Generic_Formal_Declarations (Gen_Decl);
6711 if Present (Formals) then
6712 F_Node := First_Non_Pragma (Formals);
6713 else
6714 F_Node := Empty;
6715 end if;
6717 while Present (A_Ent)
6718 and then Present (F_Node)
6719 and then A_Ent /= First_Private_Entity (Act_Pkg)
6720 loop
6721 F_Ent := Get_Formal_Entity (F_Node);
6723 if Present (F_Ent) then
6725 -- This is a formal of the original package. Record
6726 -- association and recurse.
6728 Find_Matching_Actual (F_Node, A_Ent);
6729 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
6730 Next_Entity (A_Ent);
6731 end if;
6733 Next_Non_Pragma (F_Node);
6734 end loop;
6735 end;
6736 end if;
6737 end Match_Formal_Entity;
6739 -----------------------
6740 -- Get_Formal_Entity --
6741 -----------------------
6743 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
6744 Kind : constant Node_Kind := Nkind (Original_Node (N));
6745 begin
6746 case Kind is
6747 when N_Formal_Object_Declaration =>
6748 return Defining_Identifier (N);
6750 when N_Formal_Type_Declaration =>
6751 return Defining_Identifier (N);
6753 when N_Formal_Subprogram_Declaration =>
6754 return Defining_Unit_Name (Specification (N));
6756 when N_Formal_Package_Declaration =>
6757 return Defining_Identifier (Original_Node (N));
6759 when N_Generic_Package_Declaration =>
6760 return Defining_Identifier (Original_Node (N));
6762 -- All other declarations are introduced by semantic analysis
6763 -- and have no match in the actual.
6765 when others =>
6766 return Empty;
6767 end case;
6768 end Get_Formal_Entity;
6770 --------------------
6771 -- Is_Instance_Of --
6772 --------------------
6774 function Is_Instance_Of
6775 (Act_Spec : Entity_Id;
6776 Gen_Anc : Entity_Id) return Boolean
6778 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
6780 begin
6781 if No (Gen_Par) then
6782 return False;
6784 -- Simplest case: the generic parent of the actual is the formal
6786 elsif Gen_Par = Gen_Anc then
6787 return True;
6789 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
6790 return False;
6792 -- The actual may be obtained through several instantiations. Its
6793 -- scope must itself be an instance of a generic declared in the
6794 -- same scope as the formal. Any other case is detected above.
6796 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
6797 return False;
6799 else
6800 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
6801 end if;
6802 end Is_Instance_Of;
6804 ------------------
6805 -- Map_Entities --
6806 ------------------
6808 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id) is
6809 E1 : Entity_Id;
6810 E2 : Entity_Id;
6812 begin
6813 Set_Instance_Of (Form, Act);
6815 -- Traverse formal and actual package to map the corresponding
6816 -- entities. We skip over internal entities that may be generated
6817 -- during semantic analysis, and find the matching entities by
6818 -- name, given that they must appear in the same order.
6820 E1 := First_Entity (Form);
6821 E2 := First_Entity (Act);
6822 while Present (E1)
6823 and then E1 /= First_Private_Entity (Form)
6824 loop
6825 -- Could this test be a single condition???
6826 -- Seems like it could, and isn't FPE (Form) a constant anyway???
6828 if not Is_Internal (E1)
6829 and then Present (Parent (E1))
6830 and then not Is_Class_Wide_Type (E1)
6831 and then not Is_Internal_Name (Chars (E1))
6832 then
6833 while Present (E2)
6834 and then Chars (E2) /= Chars (E1)
6835 loop
6836 Next_Entity (E2);
6837 end loop;
6839 if No (E2) then
6840 exit;
6841 else
6842 Set_Instance_Of (E1, E2);
6844 if Is_Type (E1)
6845 and then Is_Tagged_Type (E2)
6846 then
6847 Set_Instance_Of
6848 (Class_Wide_Type (E1), Class_Wide_Type (E2));
6849 end if;
6851 if Ekind (E1) = E_Package
6852 and then No (Renamed_Object (E1))
6853 then
6854 Map_Entities (E1, E2);
6855 end if;
6856 end if;
6857 end if;
6859 Next_Entity (E1);
6860 end loop;
6861 end Map_Entities;
6863 ---------------------------
6864 -- Process_Nested_Formal --
6865 ---------------------------
6867 procedure Process_Nested_Formal (Formal : Entity_Id) is
6868 Ent : Entity_Id;
6870 begin
6871 if Present (Associated_Formal_Package (Formal))
6872 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
6873 then
6874 Ent := First_Entity (Formal);
6875 while Present (Ent) loop
6876 Set_Is_Hidden (Ent, False);
6877 Set_Is_Potentially_Use_Visible
6878 (Ent, Is_Potentially_Use_Visible (Formal));
6880 if Ekind (Ent) = E_Package then
6881 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
6882 Process_Nested_Formal (Ent);
6883 end if;
6885 Next_Entity (Ent);
6886 end loop;
6887 end if;
6888 end Process_Nested_Formal;
6890 -- Start of processing for Instantiate_Formal_Package
6892 begin
6893 Analyze (Actual);
6895 if not Is_Entity_Name (Actual)
6896 or else Ekind (Entity (Actual)) /= E_Package
6897 then
6898 Error_Msg_N
6899 ("expect package instance to instantiate formal", Actual);
6900 Abandon_Instantiation (Actual);
6901 raise Program_Error;
6903 else
6904 Actual_Pack := Entity (Actual);
6905 Set_Is_Instantiated (Actual_Pack);
6907 -- The actual may be a renamed package, or an outer generic
6908 -- formal package whose instantiation is converted into a renaming.
6910 if Present (Renamed_Object (Actual_Pack)) then
6911 Actual_Pack := Renamed_Object (Actual_Pack);
6912 end if;
6914 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
6915 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
6916 Formal_Pack := Defining_Identifier (Analyzed_Formal);
6917 else
6918 Gen_Parent :=
6919 Generic_Parent (Specification (Analyzed_Formal));
6920 Formal_Pack :=
6921 Defining_Unit_Name (Specification (Analyzed_Formal));
6922 end if;
6924 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
6925 Parent_Spec := Specification (Unit_Declaration_Node (Actual_Pack));
6926 else
6927 Parent_Spec := Parent (Actual_Pack);
6928 end if;
6930 if Gen_Parent = Any_Id then
6931 Error_Msg_N
6932 ("previous error in declaration of formal package", Actual);
6933 Abandon_Instantiation (Actual);
6935 elsif
6936 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
6937 then
6938 null;
6940 else
6941 Error_Msg_NE
6942 ("actual parameter must be instance of&", Actual, Gen_Parent);
6943 Abandon_Instantiation (Actual);
6944 end if;
6946 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
6947 Map_Entities (Formal_Pack, Actual_Pack);
6949 Nod :=
6950 Make_Package_Renaming_Declaration (Loc,
6951 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
6952 Name => New_Reference_To (Actual_Pack, Loc));
6954 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
6955 Defining_Identifier (Formal));
6956 Decls := New_List (Nod);
6958 -- If the formal F has a box, then the generic declarations are
6959 -- visible in the generic G. In an instance of G, the corresponding
6960 -- entities in the actual for F (which are the actuals for the
6961 -- instantiation of the generic that F denotes) must also be made
6962 -- visible for analysis of the current instance. On exit from the
6963 -- current instance, those entities are made private again. If the
6964 -- actual is currently in use, these entities are also use-visible.
6966 -- The loop through the actual entities also steps through the
6967 -- formal entities and enters associations from formals to
6968 -- actuals into the renaming map. This is necessary to properly
6969 -- handle checking of actual parameter associations for later
6970 -- formals that depend on actuals declared in the formal package.
6972 if Box_Present (Formal) then
6973 declare
6974 Gen_Decl : constant Node_Id :=
6975 Unit_Declaration_Node (Gen_Parent);
6976 Formals : constant List_Id :=
6977 Generic_Formal_Declarations (Gen_Decl);
6978 Actual_Ent : Entity_Id;
6979 Formal_Node : Node_Id;
6980 Formal_Ent : Entity_Id;
6982 begin
6983 if Present (Formals) then
6984 Formal_Node := First_Non_Pragma (Formals);
6985 else
6986 Formal_Node := Empty;
6987 end if;
6989 Actual_Ent := First_Entity (Actual_Pack);
6991 while Present (Actual_Ent)
6992 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
6993 loop
6994 Set_Is_Hidden (Actual_Ent, False);
6995 Set_Is_Potentially_Use_Visible
6996 (Actual_Ent, In_Use (Actual_Pack));
6998 if Ekind (Actual_Ent) = E_Package then
6999 Process_Nested_Formal (Actual_Ent);
7000 end if;
7002 if Present (Formal_Node) then
7003 Formal_Ent := Get_Formal_Entity (Formal_Node);
7005 if Present (Formal_Ent) then
7006 Find_Matching_Actual (Formal_Node, Actual_Ent);
7007 Match_Formal_Entity
7008 (Formal_Node, Formal_Ent, Actual_Ent);
7009 end if;
7011 Next_Non_Pragma (Formal_Node);
7013 else
7014 -- No further formals to match, but the generic
7015 -- part may contain inherited operation that are
7016 -- not hidden in the enclosing instance.
7018 Next_Entity (Actual_Ent);
7019 end if;
7021 end loop;
7022 end;
7024 -- If the formal is not declared with a box, reanalyze it as
7025 -- an instantiation, to verify the matching rules of 12.7. The
7026 -- actual checks are performed after the generic associations
7027 -- been analyzed.
7029 else
7030 declare
7031 I_Pack : constant Entity_Id :=
7032 Make_Defining_Identifier (Sloc (Actual),
7033 Chars => New_Internal_Name ('P'));
7035 begin
7036 Set_Is_Internal (I_Pack);
7038 Append_To (Decls,
7039 Make_Package_Instantiation (Sloc (Actual),
7040 Defining_Unit_Name => I_Pack,
7041 Name => New_Occurrence_Of (Gen_Parent, Sloc (Actual)),
7042 Generic_Associations =>
7043 Generic_Associations (Formal)));
7044 end;
7045 end if;
7047 return Decls;
7048 end if;
7049 end Instantiate_Formal_Package;
7051 -----------------------------------
7052 -- Instantiate_Formal_Subprogram --
7053 -----------------------------------
7055 function Instantiate_Formal_Subprogram
7056 (Formal : Node_Id;
7057 Actual : Node_Id;
7058 Analyzed_Formal : Node_Id) return Node_Id
7060 Loc : Source_Ptr := Sloc (Instantiation_Node);
7061 Formal_Sub : constant Entity_Id :=
7062 Defining_Unit_Name (Specification (Formal));
7063 Analyzed_S : constant Entity_Id :=
7064 Defining_Unit_Name (Specification (Analyzed_Formal));
7065 Decl_Node : Node_Id;
7066 Nam : Node_Id;
7067 New_Spec : Node_Id;
7069 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
7070 -- If the generic is a child unit, the parent has been installed on the
7071 -- scope stack, but a default subprogram cannot resolve to something on
7072 -- the parent because that parent is not really part of the visible
7073 -- context (it is there to resolve explicit local entities). If the
7074 -- default has resolved in this way, we remove the entity from
7075 -- immediate visibility and analyze the node again to emit an error
7076 -- message or find another visible candidate.
7078 procedure Valid_Actual_Subprogram (Act : Node_Id);
7079 -- Perform legality check and raise exception on failure
7081 -----------------------
7082 -- From_Parent_Scope --
7083 -----------------------
7085 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
7086 Gen_Scope : Node_Id := Scope (Analyzed_S);
7088 begin
7089 while Present (Gen_Scope)
7090 and then Is_Child_Unit (Gen_Scope)
7091 loop
7092 if Scope (Subp) = Scope (Gen_Scope) then
7093 return True;
7094 end if;
7096 Gen_Scope := Scope (Gen_Scope);
7097 end loop;
7099 return False;
7100 end From_Parent_Scope;
7102 -----------------------------
7103 -- Valid_Actual_Subprogram --
7104 -----------------------------
7106 procedure Valid_Actual_Subprogram (Act : Node_Id) is
7107 Act_E : Entity_Id := Empty;
7109 begin
7110 if Is_Entity_Name (Act) then
7111 Act_E := Entity (Act);
7112 elsif Nkind (Act) = N_Selected_Component
7113 and then Is_Entity_Name (Selector_Name (Act))
7114 then
7115 Act_E := Entity (Selector_Name (Act));
7116 end if;
7118 if (Present (Act_E) and then Is_Overloadable (Act_E))
7119 or else Nkind (Act) = N_Attribute_Reference
7120 or else Nkind (Act) = N_Indexed_Component
7121 or else Nkind (Act) = N_Character_Literal
7122 or else Nkind (Act) = N_Explicit_Dereference
7123 then
7124 return;
7125 end if;
7127 Error_Msg_NE
7128 ("expect subprogram or entry name in instantiation of&",
7129 Instantiation_Node, Formal_Sub);
7130 Abandon_Instantiation (Instantiation_Node);
7132 end Valid_Actual_Subprogram;
7134 -- Start of processing for Instantiate_Formal_Subprogram
7136 begin
7137 New_Spec := New_Copy_Tree (Specification (Formal));
7139 -- Create new entity for the actual (New_Copy_Tree does not)
7141 Set_Defining_Unit_Name
7142 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
7144 -- Find entity of actual. If the actual is an attribute reference, it
7145 -- cannot be resolved here (its formal is missing) but is handled
7146 -- instead in Attribute_Renaming. If the actual is overloaded, it is
7147 -- fully resolved subsequently, when the renaming declaration for the
7148 -- formal is analyzed. If it is an explicit dereference, resolve the
7149 -- prefix but not the actual itself, to prevent interpretation as a
7150 -- call.
7152 if Present (Actual) then
7153 Loc := Sloc (Actual);
7154 Set_Sloc (New_Spec, Loc);
7156 if Nkind (Actual) = N_Operator_Symbol then
7157 Find_Direct_Name (Actual);
7159 elsif Nkind (Actual) = N_Explicit_Dereference then
7160 Analyze (Prefix (Actual));
7162 elsif Nkind (Actual) /= N_Attribute_Reference then
7163 Analyze (Actual);
7164 end if;
7166 Valid_Actual_Subprogram (Actual);
7167 Nam := Actual;
7169 elsif Present (Default_Name (Formal)) then
7170 if Nkind (Default_Name (Formal)) /= N_Attribute_Reference
7171 and then Nkind (Default_Name (Formal)) /= N_Selected_Component
7172 and then Nkind (Default_Name (Formal)) /= N_Indexed_Component
7173 and then Nkind (Default_Name (Formal)) /= N_Character_Literal
7174 and then Present (Entity (Default_Name (Formal)))
7175 then
7176 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
7177 else
7178 Nam := New_Copy (Default_Name (Formal));
7179 Set_Sloc (Nam, Loc);
7180 end if;
7182 elsif Box_Present (Formal) then
7184 -- Actual is resolved at the point of instantiation. Create
7185 -- an identifier or operator with the same name as the formal.
7187 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
7188 Nam := Make_Operator_Symbol (Loc,
7189 Chars => Chars (Formal_Sub),
7190 Strval => No_String);
7191 else
7192 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
7193 end if;
7195 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
7196 and then Null_Present (Specification (Formal))
7197 then
7198 -- Generate null body for procedure, for use in the instance
7200 Decl_Node :=
7201 Make_Subprogram_Body (Loc,
7202 Specification => New_Spec,
7203 Declarations => New_List,
7204 Handled_Statement_Sequence =>
7205 Make_Handled_Sequence_Of_Statements (Loc,
7206 Statements => New_List (Make_Null_Statement (Loc))));
7208 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
7209 return Decl_Node;
7211 else
7212 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
7213 Error_Msg_NE
7214 ("missing actual&", Instantiation_Node, Formal_Sub);
7215 Error_Msg_NE
7216 ("\in instantiation of & declared#",
7217 Instantiation_Node, Scope (Analyzed_S));
7218 Abandon_Instantiation (Instantiation_Node);
7219 end if;
7221 Decl_Node :=
7222 Make_Subprogram_Renaming_Declaration (Loc,
7223 Specification => New_Spec,
7224 Name => Nam);
7226 -- If we do not have an actual and the formal specified <> then
7227 -- set to get proper default.
7229 if No (Actual) and then Box_Present (Formal) then
7230 Set_From_Default (Decl_Node);
7231 end if;
7233 -- Gather possible interpretations for the actual before analyzing the
7234 -- instance. If overloaded, it will be resolved when analyzing the
7235 -- renaming declaration.
7237 if Box_Present (Formal)
7238 and then No (Actual)
7239 then
7240 Analyze (Nam);
7242 if Is_Child_Unit (Scope (Analyzed_S))
7243 and then Present (Entity (Nam))
7244 then
7245 if not Is_Overloaded (Nam) then
7247 if From_Parent_Scope (Entity (Nam)) then
7248 Set_Is_Immediately_Visible (Entity (Nam), False);
7249 Set_Entity (Nam, Empty);
7250 Set_Etype (Nam, Empty);
7252 Analyze (Nam);
7254 Set_Is_Immediately_Visible (Entity (Nam));
7255 end if;
7257 else
7258 declare
7259 I : Interp_Index;
7260 It : Interp;
7262 begin
7263 Get_First_Interp (Nam, I, It);
7265 while Present (It.Nam) loop
7266 if From_Parent_Scope (It.Nam) then
7267 Remove_Interp (I);
7268 end if;
7270 Get_Next_Interp (I, It);
7271 end loop;
7272 end;
7273 end if;
7274 end if;
7275 end if;
7277 -- The generic instantiation freezes the actual. This can only be
7278 -- done once the actual is resolved, in the analysis of the renaming
7279 -- declaration. To make the formal subprogram entity available, we set
7280 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
7281 -- This is also needed in Analyze_Subprogram_Renaming for the processing
7282 -- of formal abstract subprograms.
7284 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
7286 -- We cannot analyze the renaming declaration, and thus find the
7287 -- actual, until the all the actuals are assembled in the instance.
7288 -- For subsequent checks of other actuals, indicate the node that
7289 -- will hold the instance of this formal.
7291 Set_Instance_Of (Analyzed_S, Nam);
7293 if Nkind (Actual) = N_Selected_Component
7294 and then Is_Task_Type (Etype (Prefix (Actual)))
7295 and then not Is_Frozen (Etype (Prefix (Actual)))
7296 then
7297 -- The renaming declaration will create a body, which must appear
7298 -- outside of the instantiation, We move the renaming declaration
7299 -- out of the instance, and create an additional renaming inside,
7300 -- to prevent freezing anomalies.
7302 declare
7303 Anon_Id : constant Entity_Id :=
7304 Make_Defining_Identifier
7305 (Loc, New_Internal_Name ('E'));
7306 begin
7307 Set_Defining_Unit_Name (New_Spec, Anon_Id);
7308 Insert_Before (Instantiation_Node, Decl_Node);
7309 Analyze (Decl_Node);
7311 -- Now create renaming within the instance
7313 Decl_Node :=
7314 Make_Subprogram_Renaming_Declaration (Loc,
7315 Specification => New_Copy_Tree (New_Spec),
7316 Name => New_Occurrence_Of (Anon_Id, Loc));
7318 Set_Defining_Unit_Name (Specification (Decl_Node),
7319 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
7320 end;
7321 end if;
7323 return Decl_Node;
7324 end Instantiate_Formal_Subprogram;
7326 ------------------------
7327 -- Instantiate_Object --
7328 ------------------------
7330 function Instantiate_Object
7331 (Formal : Node_Id;
7332 Actual : Node_Id;
7333 Analyzed_Formal : Node_Id) return List_Id
7335 Formal_Id : constant Entity_Id := Defining_Identifier (Formal);
7336 Type_Id : constant Node_Id := Subtype_Mark (Formal);
7337 Loc : constant Source_Ptr := Sloc (Actual);
7338 Act_Assoc : constant Node_Id := Parent (Actual);
7339 Orig_Ftyp : constant Entity_Id :=
7340 Etype (Defining_Identifier (Analyzed_Formal));
7341 List : constant List_Id := New_List;
7342 Ftyp : Entity_Id;
7343 Decl_Node : Node_Id;
7344 Subt_Decl : Node_Id := Empty;
7346 begin
7347 -- Sloc for error message on missing actual
7349 Error_Msg_Sloc := Sloc (Scope (Defining_Identifier (Analyzed_Formal)));
7351 if Get_Instance_Of (Formal_Id) /= Formal_Id then
7352 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
7353 end if;
7355 Set_Parent (List, Parent (Actual));
7357 -- OUT present
7359 if Out_Present (Formal) then
7361 -- An IN OUT generic actual must be a name. The instantiation is a
7362 -- renaming declaration. The actual is the name being renamed. We
7363 -- use the actual directly, rather than a copy, because it is not
7364 -- used further in the list of actuals, and because a copy or a use
7365 -- of relocate_node is incorrect if the instance is nested within a
7366 -- generic. In order to simplify ASIS searches, the Generic_Parent
7367 -- field links the declaration to the generic association.
7369 if No (Actual) then
7370 Error_Msg_NE
7371 ("missing actual&",
7372 Instantiation_Node, Formal_Id);
7373 Error_Msg_NE
7374 ("\in instantiation of & declared#",
7375 Instantiation_Node,
7376 Scope (Defining_Identifier (Analyzed_Formal)));
7377 Abandon_Instantiation (Instantiation_Node);
7378 end if;
7380 Decl_Node :=
7381 Make_Object_Renaming_Declaration (Loc,
7382 Defining_Identifier => New_Copy (Formal_Id),
7383 Subtype_Mark => New_Copy_Tree (Type_Id),
7384 Name => Actual);
7386 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
7388 -- The analysis of the actual may produce insert_action nodes, so
7389 -- the declaration must have a context in which to attach them.
7391 Append (Decl_Node, List);
7392 Analyze (Actual);
7394 -- Return if the analysis of the actual reported some error
7396 if Etype (Actual) = Any_Type then
7397 return List;
7398 end if;
7400 -- This check is performed here because Analyze_Object_Renaming
7401 -- will not check it when Comes_From_Source is False. Note
7402 -- though that the check for the actual being the name of an
7403 -- object will be performed in Analyze_Object_Renaming.
7405 if Is_Object_Reference (Actual)
7406 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
7407 then
7408 Error_Msg_N
7409 ("illegal discriminant-dependent component for in out parameter",
7410 Actual);
7411 end if;
7413 -- The actual has to be resolved in order to check that it is
7414 -- a variable (due to cases such as F(1), where F returns
7415 -- access to an array, and for overloaded prefixes).
7417 Ftyp :=
7418 Get_Instance_Of (Etype (Defining_Identifier (Analyzed_Formal)));
7420 if Is_Private_Type (Ftyp)
7421 and then not Is_Private_Type (Etype (Actual))
7422 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
7423 or else Base_Type (Etype (Actual)) = Ftyp)
7424 then
7425 -- If the actual has the type of the full view of the formal,
7426 -- or else a non-private subtype of the formal, then
7427 -- the visibility of the formal type has changed. Add to the
7428 -- actuals a subtype declaration that will force the exchange
7429 -- of views in the body of the instance as well.
7431 Subt_Decl :=
7432 Make_Subtype_Declaration (Loc,
7433 Defining_Identifier =>
7434 Make_Defining_Identifier (Loc, New_Internal_Name ('P')),
7435 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
7437 Prepend (Subt_Decl, List);
7439 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
7440 Exchange_Declarations (Ftyp);
7441 end if;
7443 Resolve (Actual, Ftyp);
7445 if not Is_Variable (Actual) or else Paren_Count (Actual) > 0 then
7446 Error_Msg_NE
7447 ("actual for& must be a variable", Actual, Formal_Id);
7449 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
7450 Error_Msg_NE (
7451 "type of actual does not match type of&", Actual, Formal_Id);
7453 end if;
7455 Note_Possible_Modification (Actual);
7457 -- Check for instantiation of atomic/volatile actual for
7458 -- non-atomic/volatile formal (RM C.6 (12)).
7460 if Is_Atomic_Object (Actual)
7461 and then not Is_Atomic (Orig_Ftyp)
7462 then
7463 Error_Msg_N
7464 ("cannot instantiate non-atomic formal object " &
7465 "with atomic actual", Actual);
7467 elsif Is_Volatile_Object (Actual)
7468 and then not Is_Volatile (Orig_Ftyp)
7469 then
7470 Error_Msg_N
7471 ("cannot instantiate non-volatile formal object " &
7472 "with volatile actual", Actual);
7473 end if;
7475 -- OUT not present
7477 else
7478 -- The instantiation of a generic formal in-parameter
7479 -- is a constant declaration. The actual is the expression for
7480 -- that declaration.
7482 if Present (Actual) then
7484 Decl_Node := Make_Object_Declaration (Loc,
7485 Defining_Identifier => New_Copy (Formal_Id),
7486 Constant_Present => True,
7487 Object_Definition => New_Copy_Tree (Type_Id),
7488 Expression => Actual);
7490 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
7492 -- A generic formal object of a tagged type is defined
7493 -- to be aliased so the new constant must also be treated
7494 -- as aliased.
7496 if Is_Tagged_Type
7497 (Etype (Defining_Identifier (Analyzed_Formal)))
7498 then
7499 Set_Aliased_Present (Decl_Node);
7500 end if;
7502 Append (Decl_Node, List);
7504 -- No need to repeat (pre-)analysis of some expression nodes
7505 -- already handled in Pre_Analyze_Actuals.
7507 if Nkind (Actual) /= N_Allocator then
7508 Analyze (Actual);
7510 -- Return if the analysis of the actual reported some error
7512 if Etype (Actual) = Any_Type then
7513 return List;
7514 end if;
7515 end if;
7517 declare
7518 Typ : constant Entity_Id :=
7519 Get_Instance_Of
7520 (Etype (Defining_Identifier (Analyzed_Formal)));
7522 begin
7523 Freeze_Before (Instantiation_Node, Typ);
7525 -- If the actual is an aggregate, perform name resolution on
7526 -- its components (the analysis of an aggregate does not do
7527 -- it) to capture local names that may be hidden if the
7528 -- generic is a child unit.
7530 if Nkind (Actual) = N_Aggregate then
7531 Pre_Analyze_And_Resolve (Actual, Typ);
7532 end if;
7533 end;
7535 elsif Present (Expression (Formal)) then
7537 -- Use default to construct declaration
7539 Decl_Node :=
7540 Make_Object_Declaration (Sloc (Formal),
7541 Defining_Identifier => New_Copy (Formal_Id),
7542 Constant_Present => True,
7543 Object_Definition => New_Copy (Type_Id),
7544 Expression => New_Copy_Tree (Expression (Formal)));
7546 Append (Decl_Node, List);
7547 Set_Analyzed (Expression (Decl_Node), False);
7549 else
7550 Error_Msg_NE
7551 ("missing actual&",
7552 Instantiation_Node, Formal_Id);
7553 Error_Msg_NE ("\in instantiation of & declared#",
7554 Instantiation_Node,
7555 Scope (Defining_Identifier (Analyzed_Formal)));
7557 if Is_Scalar_Type
7558 (Etype (Defining_Identifier (Analyzed_Formal)))
7559 then
7560 -- Create dummy constant declaration so that instance can
7561 -- be analyzed, to minimize cascaded visibility errors.
7563 Decl_Node :=
7564 Make_Object_Declaration (Loc,
7565 Defining_Identifier => New_Copy (Formal_Id),
7566 Constant_Present => True,
7567 Object_Definition => New_Copy (Type_Id),
7568 Expression =>
7569 Make_Attribute_Reference (Sloc (Formal_Id),
7570 Attribute_Name => Name_First,
7571 Prefix => New_Copy (Type_Id)));
7573 Append (Decl_Node, List);
7575 else
7576 Abandon_Instantiation (Instantiation_Node);
7577 end if;
7578 end if;
7580 end if;
7582 return List;
7583 end Instantiate_Object;
7585 ------------------------------
7586 -- Instantiate_Package_Body --
7587 ------------------------------
7589 procedure Instantiate_Package_Body
7590 (Body_Info : Pending_Body_Info;
7591 Inlined_Body : Boolean := False)
7593 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
7594 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
7595 Loc : constant Source_Ptr := Sloc (Inst_Node);
7597 Gen_Id : constant Node_Id := Name (Inst_Node);
7598 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7599 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
7600 Act_Spec : constant Node_Id := Specification (Act_Decl);
7601 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
7603 Act_Body_Name : Node_Id;
7604 Gen_Body : Node_Id;
7605 Gen_Body_Id : Node_Id;
7606 Act_Body : Node_Id;
7607 Act_Body_Id : Entity_Id;
7609 Parent_Installed : Boolean := False;
7610 Save_Style_Check : constant Boolean := Style_Check;
7612 begin
7613 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7615 -- The instance body may already have been processed, as the parent
7616 -- of another instance that is inlined. (Load_Parent_Of_Generic).
7618 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
7619 return;
7620 end if;
7622 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
7624 if No (Gen_Body_Id) then
7625 Load_Parent_Of_Generic (Inst_Node, Specification (Gen_Decl));
7626 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7627 end if;
7629 -- Establish global variable for sloc adjustment and for error
7630 -- recovery.
7632 Instantiation_Node := Inst_Node;
7634 if Present (Gen_Body_Id) then
7635 Save_Env (Gen_Unit, Act_Decl_Id);
7636 Style_Check := False;
7637 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
7639 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
7641 Create_Instantiation_Source
7642 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
7644 Act_Body :=
7645 Copy_Generic_Node
7646 (Original_Node (Gen_Body), Empty, Instantiating => True);
7648 -- Build new name (possibly qualified) for body declaration
7650 Act_Body_Id := New_Copy (Act_Decl_Id);
7652 -- Some attributes of the spec entity are not inherited by the
7653 -- body entity.
7655 Set_Handler_Records (Act_Body_Id, No_List);
7657 if Nkind (Defining_Unit_Name (Act_Spec)) =
7658 N_Defining_Program_Unit_Name
7659 then
7660 Act_Body_Name :=
7661 Make_Defining_Program_Unit_Name (Loc,
7662 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
7663 Defining_Identifier => Act_Body_Id);
7664 else
7665 Act_Body_Name := Act_Body_Id;
7666 end if;
7668 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
7670 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
7671 Check_Generic_Actuals (Act_Decl_Id, False);
7673 -- If it is a child unit, make the parent instance (which is an
7674 -- instance of the parent of the generic) visible. The parent
7675 -- instance is the prefix of the name of the generic unit.
7677 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
7678 and then Nkind (Gen_Id) = N_Expanded_Name
7679 then
7680 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
7681 Parent_Installed := True;
7683 elsif Is_Child_Unit (Gen_Unit) then
7684 Install_Parent (Scope (Gen_Unit), In_Body => True);
7685 Parent_Installed := True;
7686 end if;
7688 -- If the instantiation is a library unit, and this is the main
7689 -- unit, then build the resulting compilation unit nodes for the
7690 -- instance. If this is a compilation unit but it is not the main
7691 -- unit, then it is the body of a unit in the context, that is being
7692 -- compiled because it is encloses some inlined unit or another
7693 -- generic unit being instantiated. In that case, this body is not
7694 -- part of the current compilation, and is not attached to the tree,
7695 -- but its parent must be set for analysis.
7697 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
7699 -- Replace instance node with body of instance, and create
7700 -- new node for corresponding instance declaration.
7702 Build_Instance_Compilation_Unit_Nodes
7703 (Inst_Node, Act_Body, Act_Decl);
7704 Analyze (Inst_Node);
7706 if Parent (Inst_Node) = Cunit (Main_Unit) then
7708 -- If the instance is a child unit itself, then set the
7709 -- scope of the expanded body to be the parent of the
7710 -- instantiation (ensuring that the fully qualified name
7711 -- will be generated for the elaboration subprogram).
7713 if Nkind (Defining_Unit_Name (Act_Spec)) =
7714 N_Defining_Program_Unit_Name
7715 then
7716 Set_Scope
7717 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
7718 end if;
7719 end if;
7721 -- Case where instantiation is not a library unit
7723 else
7724 -- If this is an early instantiation, i.e. appears textually
7725 -- before the corresponding body and must be elaborated first,
7726 -- indicate that the body instance is to be delayed.
7728 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
7730 -- Now analyze the body. We turn off all checks if this is
7731 -- an internal unit, since there is no reason to have checks
7732 -- on for any predefined run-time library code. All such
7733 -- code is designed to be compiled with checks off.
7735 -- Note that we do NOT apply this criterion to children of
7736 -- GNAT (or on VMS, children of DEC). The latter units must
7737 -- suppress checks explicitly if this is needed.
7739 if Is_Predefined_File_Name
7740 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
7741 then
7742 Analyze (Act_Body, Suppress => All_Checks);
7743 else
7744 Analyze (Act_Body);
7745 end if;
7746 end if;
7748 if not Generic_Separately_Compiled (Gen_Unit) then
7749 Inherit_Context (Gen_Body, Inst_Node);
7750 end if;
7752 -- Remove the parent instances if they have been placed on the
7753 -- scope stack to compile the body.
7755 if Parent_Installed then
7756 Remove_Parent (In_Body => True);
7757 end if;
7759 Restore_Private_Views (Act_Decl_Id);
7761 -- Remove the current unit from visibility if this is an instance
7762 -- that is not elaborated on the fly for inlining purposes.
7764 if not Inlined_Body then
7765 Set_Is_Immediately_Visible (Act_Decl_Id, False);
7766 end if;
7768 Restore_Env;
7769 Style_Check := Save_Style_Check;
7771 -- If we have no body, and the unit requires a body, then complain.
7772 -- This complaint is suppressed if we have detected other errors
7773 -- (since a common reason for missing the body is that it had errors).
7775 elsif Unit_Requires_Body (Gen_Unit) then
7776 if Serious_Errors_Detected = 0 then
7777 Error_Msg_NE
7778 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
7780 -- Don't attempt to perform any cleanup actions if some other
7781 -- error was aready detected, since this can cause blowups.
7783 else
7784 return;
7785 end if;
7787 -- Case of package that does not need a body
7789 else
7790 -- If the instantiation of the declaration is a library unit,
7791 -- rewrite the original package instantiation as a package
7792 -- declaration in the compilation unit node.
7794 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
7795 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
7796 Rewrite (Inst_Node, Act_Decl);
7798 -- Generate elaboration entity, in case spec has elaboration
7799 -- code. This cannot be done when the instance is analyzed,
7800 -- because it is not known yet whether the body exists.
7802 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
7803 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
7805 -- If the instantiation is not a library unit, then append the
7806 -- declaration to the list of implicitly generated entities.
7807 -- unless it is already a list member which means that it was
7808 -- already processed
7810 elsif not Is_List_Member (Act_Decl) then
7811 Mark_Rewrite_Insertion (Act_Decl);
7812 Insert_Before (Inst_Node, Act_Decl);
7813 end if;
7814 end if;
7816 Expander_Mode_Restore;
7817 end Instantiate_Package_Body;
7819 ---------------------------------
7820 -- Instantiate_Subprogram_Body --
7821 ---------------------------------
7823 procedure Instantiate_Subprogram_Body
7824 (Body_Info : Pending_Body_Info)
7826 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
7827 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
7828 Loc : constant Source_Ptr := Sloc (Inst_Node);
7829 Gen_Id : constant Node_Id := Name (Inst_Node);
7830 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7831 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
7832 Anon_Id : constant Entity_Id :=
7833 Defining_Unit_Name (Specification (Act_Decl));
7834 Pack_Id : constant Entity_Id :=
7835 Defining_Unit_Name (Parent (Act_Decl));
7836 Decls : List_Id;
7837 Gen_Body : Node_Id;
7838 Gen_Body_Id : Node_Id;
7839 Act_Body : Node_Id;
7840 Act_Body_Id : Entity_Id;
7841 Pack_Body : Node_Id;
7842 Prev_Formal : Entity_Id;
7843 Ret_Expr : Node_Id;
7844 Unit_Renaming : Node_Id;
7846 Parent_Installed : Boolean := False;
7847 Save_Style_Check : constant Boolean := Style_Check;
7849 begin
7850 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7852 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
7854 if No (Gen_Body_Id) then
7855 Load_Parent_Of_Generic (Inst_Node, Specification (Gen_Decl));
7856 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7857 end if;
7859 Instantiation_Node := Inst_Node;
7861 if Present (Gen_Body_Id) then
7862 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
7864 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
7866 -- Either body is not present, or context is non-expanding, as
7867 -- when compiling a subunit. Mark the instance as completed, and
7868 -- diagnose a missing body when needed.
7870 if Expander_Active
7871 and then Operating_Mode = Generate_Code
7872 then
7873 Error_Msg_N
7874 ("missing proper body for instantiation", Gen_Body);
7875 end if;
7877 Set_Has_Completion (Anon_Id);
7878 return;
7879 end if;
7881 Save_Env (Gen_Unit, Anon_Id);
7882 Style_Check := False;
7883 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
7884 Create_Instantiation_Source
7885 (Inst_Node,
7886 Gen_Body_Id,
7887 False,
7888 S_Adjustment);
7890 Act_Body :=
7891 Copy_Generic_Node
7892 (Original_Node (Gen_Body), Empty, Instantiating => True);
7893 Act_Body_Id := Defining_Entity (Act_Body);
7894 Set_Chars (Act_Body_Id, Chars (Anon_Id));
7895 Set_Sloc (Act_Body_Id, Sloc (Defining_Entity (Inst_Node)));
7896 Set_Corresponding_Spec (Act_Body, Anon_Id);
7897 Set_Has_Completion (Anon_Id);
7898 Check_Generic_Actuals (Pack_Id, False);
7900 -- If it is a child unit, make the parent instance (which is an
7901 -- instance of the parent of the generic) visible. The parent
7902 -- instance is the prefix of the name of the generic unit.
7904 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
7905 and then Nkind (Gen_Id) = N_Expanded_Name
7906 then
7907 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
7908 Parent_Installed := True;
7910 elsif Is_Child_Unit (Gen_Unit) then
7911 Install_Parent (Scope (Gen_Unit), In_Body => True);
7912 Parent_Installed := True;
7913 end if;
7915 -- Inside its body, a reference to the generic unit is a reference
7916 -- to the instance. The corresponding renaming is the first
7917 -- declaration in the body.
7919 Unit_Renaming :=
7920 Make_Subprogram_Renaming_Declaration (Loc,
7921 Specification =>
7922 Copy_Generic_Node (
7923 Specification (Original_Node (Gen_Body)),
7924 Empty,
7925 Instantiating => True),
7926 Name => New_Occurrence_Of (Anon_Id, Loc));
7928 -- If there is a formal subprogram with the same name as the
7929 -- unit itself, do not add this renaming declaration. This is
7930 -- a temporary fix for one ACVC test. ???
7932 Prev_Formal := First_Entity (Pack_Id);
7933 while Present (Prev_Formal) loop
7934 if Chars (Prev_Formal) = Chars (Gen_Unit)
7935 and then Is_Overloadable (Prev_Formal)
7936 then
7937 exit;
7938 end if;
7940 Next_Entity (Prev_Formal);
7941 end loop;
7943 if Present (Prev_Formal) then
7944 Decls := New_List (Act_Body);
7945 else
7946 Decls := New_List (Unit_Renaming, Act_Body);
7947 end if;
7949 -- The subprogram body is placed in the body of a dummy package
7950 -- body, whose spec contains the subprogram declaration as well
7951 -- as the renaming declarations for the generic parameters.
7953 Pack_Body := Make_Package_Body (Loc,
7954 Defining_Unit_Name => New_Copy (Pack_Id),
7955 Declarations => Decls);
7957 Set_Corresponding_Spec (Pack_Body, Pack_Id);
7959 -- If the instantiation is a library unit, then build resulting
7960 -- compilation unit nodes for the instance. The declaration of
7961 -- the enclosing package is the grandparent of the subprogram
7962 -- declaration. First replace the instantiation node as the unit
7963 -- of the corresponding compilation.
7965 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
7966 if Parent (Inst_Node) = Cunit (Main_Unit) then
7967 Set_Unit (Parent (Inst_Node), Inst_Node);
7968 Build_Instance_Compilation_Unit_Nodes
7969 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
7970 Analyze (Inst_Node);
7971 else
7972 Set_Parent (Pack_Body, Parent (Inst_Node));
7973 Analyze (Pack_Body);
7974 end if;
7976 else
7977 Insert_Before (Inst_Node, Pack_Body);
7978 Mark_Rewrite_Insertion (Pack_Body);
7979 Analyze (Pack_Body);
7981 if Expander_Active then
7982 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
7983 end if;
7984 end if;
7986 if not Generic_Separately_Compiled (Gen_Unit) then
7987 Inherit_Context (Gen_Body, Inst_Node);
7988 end if;
7990 Restore_Private_Views (Pack_Id, False);
7992 if Parent_Installed then
7993 Remove_Parent (In_Body => True);
7994 end if;
7996 Restore_Env;
7997 Style_Check := Save_Style_Check;
7999 -- Body not found. Error was emitted already. If there were no
8000 -- previous errors, this may be an instance whose scope is a premature
8001 -- instance. In that case we must insure that the (legal) program does
8002 -- raise program error if executed. We generate a subprogram body for
8003 -- this purpose. See DEC ac30vso.
8005 elsif Serious_Errors_Detected = 0
8006 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
8007 then
8008 if Ekind (Anon_Id) = E_Procedure then
8009 Act_Body :=
8010 Make_Subprogram_Body (Loc,
8011 Specification =>
8012 Make_Procedure_Specification (Loc,
8013 Defining_Unit_Name =>
8014 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
8015 Parameter_Specifications =>
8016 New_Copy_List
8017 (Parameter_Specifications (Parent (Anon_Id)))),
8019 Declarations => Empty_List,
8020 Handled_Statement_Sequence =>
8021 Make_Handled_Sequence_Of_Statements (Loc,
8022 Statements =>
8023 New_List (
8024 Make_Raise_Program_Error (Loc,
8025 Reason =>
8026 PE_Access_Before_Elaboration))));
8028 else
8029 Ret_Expr :=
8030 Make_Raise_Program_Error (Loc,
8031 Reason => PE_Access_Before_Elaboration);
8033 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
8034 Set_Analyzed (Ret_Expr);
8036 Act_Body :=
8037 Make_Subprogram_Body (Loc,
8038 Specification =>
8039 Make_Function_Specification (Loc,
8040 Defining_Unit_Name =>
8041 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
8042 Parameter_Specifications =>
8043 New_Copy_List
8044 (Parameter_Specifications (Parent (Anon_Id))),
8045 Result_Definition =>
8046 New_Occurrence_Of (Etype (Anon_Id), Loc)),
8048 Declarations => Empty_List,
8049 Handled_Statement_Sequence =>
8050 Make_Handled_Sequence_Of_Statements (Loc,
8051 Statements =>
8052 New_List (Make_Return_Statement (Loc, Ret_Expr))));
8053 end if;
8055 Pack_Body := Make_Package_Body (Loc,
8056 Defining_Unit_Name => New_Copy (Pack_Id),
8057 Declarations => New_List (Act_Body));
8059 Insert_After (Inst_Node, Pack_Body);
8060 Set_Corresponding_Spec (Pack_Body, Pack_Id);
8061 Analyze (Pack_Body);
8062 end if;
8064 Expander_Mode_Restore;
8065 end Instantiate_Subprogram_Body;
8067 ----------------------
8068 -- Instantiate_Type --
8069 ----------------------
8071 function Instantiate_Type
8072 (Formal : Node_Id;
8073 Actual : Node_Id;
8074 Analyzed_Formal : Node_Id;
8075 Actual_Decls : List_Id) return Node_Id
8077 Loc : constant Source_Ptr := Sloc (Actual);
8078 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
8079 A_Gen_T : constant Entity_Id := Defining_Identifier (Analyzed_Formal);
8080 Ancestor : Entity_Id := Empty;
8081 Def : constant Node_Id := Formal_Type_Definition (Formal);
8082 Act_T : Entity_Id;
8083 Decl_Node : Node_Id;
8085 procedure Validate_Array_Type_Instance;
8086 procedure Validate_Access_Subprogram_Instance;
8087 procedure Validate_Access_Type_Instance;
8088 procedure Validate_Derived_Type_Instance;
8089 procedure Validate_Derived_Interface_Type_Instance;
8090 procedure Validate_Interface_Type_Instance;
8091 procedure Validate_Private_Type_Instance;
8092 -- These procedures perform validation tests for the named case
8094 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
8095 -- Check that base types are the same and that the subtypes match
8096 -- statically. Used in several of the above.
8098 --------------------
8099 -- Subtypes_Match --
8100 --------------------
8102 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
8103 T : constant Entity_Id := Get_Instance_Of (Gen_T);
8105 begin
8106 return (Base_Type (T) = Base_Type (Act_T)
8107 and then Subtypes_Statically_Match (T, Act_T))
8109 or else (Is_Class_Wide_Type (Gen_T)
8110 and then Is_Class_Wide_Type (Act_T)
8111 and then
8112 Subtypes_Match
8113 (Get_Instance_Of (Root_Type (Gen_T)),
8114 Root_Type (Act_T)))
8116 or else
8117 ((Ekind (Gen_T) = E_Anonymous_Access_Subprogram_Type
8118 or else Ekind (Gen_T) = E_Anonymous_Access_Type)
8119 and then Ekind (Act_T) = Ekind (Gen_T)
8120 and then
8121 Subtypes_Statically_Match
8122 (Designated_Type (Gen_T), Designated_Type (Act_T)));
8123 end Subtypes_Match;
8125 -----------------------------------------
8126 -- Validate_Access_Subprogram_Instance --
8127 -----------------------------------------
8129 procedure Validate_Access_Subprogram_Instance is
8130 begin
8131 if not Is_Access_Type (Act_T)
8132 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
8133 then
8134 Error_Msg_NE
8135 ("expect access type in instantiation of &", Actual, Gen_T);
8136 Abandon_Instantiation (Actual);
8137 end if;
8139 Check_Mode_Conformant
8140 (Designated_Type (Act_T),
8141 Designated_Type (A_Gen_T),
8142 Actual,
8143 Get_Inst => True);
8145 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
8146 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
8147 Error_Msg_NE
8148 ("protected access type not allowed for formal &",
8149 Actual, Gen_T);
8150 end if;
8152 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
8153 Error_Msg_NE
8154 ("expect protected access type for formal &",
8155 Actual, Gen_T);
8156 end if;
8157 end Validate_Access_Subprogram_Instance;
8159 -----------------------------------
8160 -- Validate_Access_Type_Instance --
8161 -----------------------------------
8163 procedure Validate_Access_Type_Instance is
8164 Desig_Type : constant Entity_Id :=
8165 Find_Actual_Type
8166 (Designated_Type (A_Gen_T), Scope (A_Gen_T));
8168 begin
8169 if not Is_Access_Type (Act_T) then
8170 Error_Msg_NE
8171 ("expect access type in instantiation of &", Actual, Gen_T);
8172 Abandon_Instantiation (Actual);
8173 end if;
8175 if Is_Access_Constant (A_Gen_T) then
8176 if not Is_Access_Constant (Act_T) then
8177 Error_Msg_N
8178 ("actual type must be access-to-constant type", Actual);
8179 Abandon_Instantiation (Actual);
8180 end if;
8181 else
8182 if Is_Access_Constant (Act_T) then
8183 Error_Msg_N
8184 ("actual type must be access-to-variable type", Actual);
8185 Abandon_Instantiation (Actual);
8187 elsif Ekind (A_Gen_T) = E_General_Access_Type
8188 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
8189 then
8190 Error_Msg_N ("actual must be general access type!", Actual);
8191 Error_Msg_NE ("add ALL to }!", Actual, Act_T);
8192 Abandon_Instantiation (Actual);
8193 end if;
8194 end if;
8196 -- The designated subtypes, that is to say the subtypes introduced
8197 -- by an access type declaration (and not by a subtype declaration)
8198 -- must match.
8200 if not Subtypes_Match
8201 (Desig_Type, Designated_Type (Base_Type (Act_T)))
8202 then
8203 Error_Msg_NE
8204 ("designated type of actual does not match that of formal &",
8205 Actual, Gen_T);
8206 Abandon_Instantiation (Actual);
8208 elsif Is_Access_Type (Designated_Type (Act_T))
8209 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
8211 Is_Constrained (Designated_Type (Desig_Type))
8212 then
8213 Error_Msg_NE
8214 ("designated type of actual does not match that of formal &",
8215 Actual, Gen_T);
8216 Abandon_Instantiation (Actual);
8217 end if;
8218 end Validate_Access_Type_Instance;
8220 ----------------------------------
8221 -- Validate_Array_Type_Instance --
8222 ----------------------------------
8224 procedure Validate_Array_Type_Instance is
8225 I1 : Node_Id;
8226 I2 : Node_Id;
8227 T2 : Entity_Id;
8229 function Formal_Dimensions return Int;
8230 -- Count number of dimensions in array type formal
8232 -----------------------
8233 -- Formal_Dimensions --
8234 -----------------------
8236 function Formal_Dimensions return Int is
8237 Num : Int := 0;
8238 Index : Node_Id;
8240 begin
8241 if Nkind (Def) = N_Constrained_Array_Definition then
8242 Index := First (Discrete_Subtype_Definitions (Def));
8243 else
8244 Index := First (Subtype_Marks (Def));
8245 end if;
8247 while Present (Index) loop
8248 Num := Num + 1;
8249 Next_Index (Index);
8250 end loop;
8252 return Num;
8253 end Formal_Dimensions;
8255 -- Start of processing for Validate_Array_Type_Instance
8257 begin
8258 if not Is_Array_Type (Act_T) then
8259 Error_Msg_NE
8260 ("expect array type in instantiation of &", Actual, Gen_T);
8261 Abandon_Instantiation (Actual);
8263 elsif Nkind (Def) = N_Constrained_Array_Definition then
8264 if not (Is_Constrained (Act_T)) then
8265 Error_Msg_NE
8266 ("expect constrained array in instantiation of &",
8267 Actual, Gen_T);
8268 Abandon_Instantiation (Actual);
8269 end if;
8271 else
8272 if Is_Constrained (Act_T) then
8273 Error_Msg_NE
8274 ("expect unconstrained array in instantiation of &",
8275 Actual, Gen_T);
8276 Abandon_Instantiation (Actual);
8277 end if;
8278 end if;
8280 if Formal_Dimensions /= Number_Dimensions (Act_T) then
8281 Error_Msg_NE
8282 ("dimensions of actual do not match formal &", Actual, Gen_T);
8283 Abandon_Instantiation (Actual);
8284 end if;
8286 I1 := First_Index (A_Gen_T);
8287 I2 := First_Index (Act_T);
8288 for J in 1 .. Formal_Dimensions loop
8290 -- If the indices of the actual were given by a subtype_mark,
8291 -- the index was transformed into a range attribute. Retrieve
8292 -- the original type mark for checking.
8294 if Is_Entity_Name (Original_Node (I2)) then
8295 T2 := Entity (Original_Node (I2));
8296 else
8297 T2 := Etype (I2);
8298 end if;
8300 if not Subtypes_Match
8301 (Find_Actual_Type (Etype (I1), Scope (A_Gen_T)), T2)
8302 then
8303 Error_Msg_NE
8304 ("index types of actual do not match those of formal &",
8305 Actual, Gen_T);
8306 Abandon_Instantiation (Actual);
8307 end if;
8309 Next_Index (I1);
8310 Next_Index (I2);
8311 end loop;
8313 if not Subtypes_Match (
8314 Find_Actual_Type (Component_Type (A_Gen_T), Scope (A_Gen_T)),
8315 Component_Type (Act_T))
8316 then
8317 Error_Msg_NE
8318 ("component subtype of actual does not match that of formal &",
8319 Actual, Gen_T);
8320 Abandon_Instantiation (Actual);
8321 end if;
8323 if Has_Aliased_Components (A_Gen_T)
8324 and then not Has_Aliased_Components (Act_T)
8325 then
8326 Error_Msg_NE
8327 ("actual must have aliased components to match formal type &",
8328 Actual, Gen_T);
8329 end if;
8331 end Validate_Array_Type_Instance;
8333 -----------------------------------------------
8334 -- Validate_Derived_Interface_Type_Instance --
8335 -----------------------------------------------
8337 procedure Validate_Derived_Interface_Type_Instance is
8338 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
8339 Elmt : Elmt_Id;
8341 begin
8342 -- First apply interface instance checks
8344 Validate_Interface_Type_Instance;
8346 -- Verify that immediate parent interface is an ancestor of
8347 -- the actual.
8349 if Present (Par)
8350 and then not Interface_Present_In_Ancestor (Act_T, Par)
8351 then
8352 Error_Msg_NE
8353 ("interface actual must include progenitor&", Actual, Par);
8354 end if;
8356 -- Now verify that the actual includes all other ancestors of
8357 -- the formal.
8359 Elmt := First_Elmt (Abstract_Interfaces (A_Gen_T));
8360 while Present (Elmt) loop
8361 if not Interface_Present_In_Ancestor (Act_T, Node (Elmt)) then
8362 Error_Msg_NE
8363 ("interface actual must include progenitor&",
8364 Actual, Node (Elmt));
8365 end if;
8367 Next_Elmt (Elmt);
8368 end loop;
8369 end Validate_Derived_Interface_Type_Instance;
8371 ------------------------------------
8372 -- Validate_Derived_Type_Instance --
8373 ------------------------------------
8375 procedure Validate_Derived_Type_Instance is
8376 Actual_Discr : Entity_Id;
8377 Ancestor_Discr : Entity_Id;
8379 begin
8380 -- If the parent type in the generic declaration is itself a previous
8381 -- formal type, then it is local to the generic and absent from the
8382 -- analyzed generic definition. In that case the ancestor is the
8383 -- instance of the formal (which must have been instantiated
8384 -- previously), unless the ancestor is itself a formal derived type.
8385 -- In this latter case (which is the subject of Corrigendum 8652/0038
8386 -- (AI-202) the ancestor of the formals is the ancestor of its
8387 -- parent. Otherwise, the analyzed generic carries the parent type.
8388 -- If the parent type is defined in a previous formal package, then
8389 -- the scope of that formal package is that of the generic type
8390 -- itself, and it has already been mapped into the corresponding type
8391 -- in the actual package.
8393 -- Common case: parent type defined outside of the generic
8395 if Is_Entity_Name (Subtype_Mark (Def))
8396 and then Present (Entity (Subtype_Mark (Def)))
8397 then
8398 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
8400 -- Check whether parent is defined in a previous formal package
8402 elsif
8403 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
8404 then
8405 Ancestor :=
8406 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
8408 -- The type may be a local derivation, or a type extension of
8409 -- a previous formal, or of a formal of a parent package.
8411 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
8412 or else
8413 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
8414 then
8415 -- Check whether the parent is another derived formal type
8416 -- in the same generic unit.
8418 if Etype (A_Gen_T) /= A_Gen_T
8419 and then Is_Generic_Type (Etype (A_Gen_T))
8420 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
8421 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
8422 then
8423 -- Locate ancestor of parent from the subtype declaration
8424 -- created for the actual.
8426 declare
8427 Decl : Node_Id;
8429 begin
8430 Decl := First (Actual_Decls);
8431 while Present (Decl) loop
8432 if Nkind (Decl) = N_Subtype_Declaration
8433 and then Chars (Defining_Identifier (Decl)) =
8434 Chars (Etype (A_Gen_T))
8435 then
8436 Ancestor := Generic_Parent_Type (Decl);
8437 exit;
8438 else
8439 Next (Decl);
8440 end if;
8441 end loop;
8442 end;
8444 pragma Assert (Present (Ancestor));
8446 else
8447 Ancestor :=
8448 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
8449 end if;
8451 else
8452 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
8453 end if;
8455 -- Ada 2005 (AI-251)
8457 if Ada_Version >= Ada_05
8458 and then Is_Interface (Ancestor)
8459 then
8460 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
8461 Error_Msg_NE
8462 ("(Ada 2005) expected type implementing & in instantiation",
8463 Actual, Ancestor);
8464 end if;
8466 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
8467 Error_Msg_NE
8468 ("expect type derived from & in instantiation",
8469 Actual, First_Subtype (Ancestor));
8470 Abandon_Instantiation (Actual);
8471 end if;
8473 -- Perform atomic/volatile checks (RM C.6(12))
8475 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
8476 Error_Msg_N
8477 ("cannot have atomic actual type for non-atomic formal type",
8478 Actual);
8480 elsif Is_Volatile (Act_T)
8481 and then not Is_Volatile (Ancestor)
8482 and then Is_By_Reference_Type (Ancestor)
8483 then
8484 Error_Msg_N
8485 ("cannot have volatile actual type for non-volatile formal type",
8486 Actual);
8487 end if;
8489 -- It should not be necessary to check for unknown discriminants
8490 -- on Formal, but for some reason Has_Unknown_Discriminants is
8491 -- false for A_Gen_T, so Is_Indefinite_Subtype incorrectly
8492 -- returns False. This needs fixing. ???
8494 if not Is_Indefinite_Subtype (A_Gen_T)
8495 and then not Unknown_Discriminants_Present (Formal)
8496 and then Is_Indefinite_Subtype (Act_T)
8497 then
8498 Error_Msg_N
8499 ("actual subtype must be constrained", Actual);
8500 Abandon_Instantiation (Actual);
8501 end if;
8503 if not Unknown_Discriminants_Present (Formal) then
8504 if Is_Constrained (Ancestor) then
8505 if not Is_Constrained (Act_T) then
8506 Error_Msg_N
8507 ("actual subtype must be constrained", Actual);
8508 Abandon_Instantiation (Actual);
8509 end if;
8511 -- Ancestor is unconstrained
8513 elsif Is_Constrained (Act_T) then
8514 if Ekind (Ancestor) = E_Access_Type
8515 or else Is_Composite_Type (Ancestor)
8516 then
8517 Error_Msg_N
8518 ("actual subtype must be unconstrained", Actual);
8519 Abandon_Instantiation (Actual);
8520 end if;
8522 -- A class-wide type is only allowed if the formal has
8523 -- unknown discriminants.
8525 elsif Is_Class_Wide_Type (Act_T)
8526 and then not Has_Unknown_Discriminants (Ancestor)
8527 then
8528 Error_Msg_NE
8529 ("actual for & cannot be a class-wide type", Actual, Gen_T);
8530 Abandon_Instantiation (Actual);
8532 -- Otherwise, the formal and actual shall have the same
8533 -- number of discriminants and each discriminant of the
8534 -- actual must correspond to a discriminant of the formal.
8536 elsif Has_Discriminants (Act_T)
8537 and then not Has_Unknown_Discriminants (Act_T)
8538 and then Has_Discriminants (Ancestor)
8539 then
8540 Actual_Discr := First_Discriminant (Act_T);
8541 Ancestor_Discr := First_Discriminant (Ancestor);
8542 while Present (Actual_Discr)
8543 and then Present (Ancestor_Discr)
8544 loop
8545 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
8546 No (Corresponding_Discriminant (Actual_Discr))
8547 then
8548 Error_Msg_NE
8549 ("discriminant & does not correspond " &
8550 "to ancestor discriminant", Actual, Actual_Discr);
8551 Abandon_Instantiation (Actual);
8552 end if;
8554 Next_Discriminant (Actual_Discr);
8555 Next_Discriminant (Ancestor_Discr);
8556 end loop;
8558 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
8559 Error_Msg_NE
8560 ("actual for & must have same number of discriminants",
8561 Actual, Gen_T);
8562 Abandon_Instantiation (Actual);
8563 end if;
8565 -- This case should be caught by the earlier check for
8566 -- for constrainedness, but the check here is added for
8567 -- completeness.
8569 elsif Has_Discriminants (Act_T)
8570 and then not Has_Unknown_Discriminants (Act_T)
8571 then
8572 Error_Msg_NE
8573 ("actual for & must not have discriminants", Actual, Gen_T);
8574 Abandon_Instantiation (Actual);
8576 elsif Has_Discriminants (Ancestor) then
8577 Error_Msg_NE
8578 ("actual for & must have known discriminants", Actual, Gen_T);
8579 Abandon_Instantiation (Actual);
8580 end if;
8582 if not Subtypes_Statically_Compatible (Act_T, Ancestor) then
8583 Error_Msg_N
8584 ("constraint on actual is incompatible with formal", Actual);
8585 Abandon_Instantiation (Actual);
8586 end if;
8587 end if;
8588 end Validate_Derived_Type_Instance;
8590 --------------------------------------
8591 -- Validate_Interface_Type_Instance --
8592 --------------------------------------
8594 procedure Validate_Interface_Type_Instance is
8595 begin
8596 if not Is_Interface (Act_T) then
8597 Error_Msg_NE
8598 ("actual for formal interface type must be an interface",
8599 Actual, Gen_T);
8601 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
8602 or else
8603 Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
8604 or else
8605 Is_Protected_Interface (A_Gen_T) /=
8606 Is_Protected_Interface (Act_T)
8607 or else
8608 Is_Synchronized_Interface (A_Gen_T) /=
8609 Is_Synchronized_Interface (Act_T)
8610 then
8611 Error_Msg_NE
8612 ("actual for interface& does not match ('R'M 12.5.5(5))",
8613 Actual, Gen_T);
8614 end if;
8615 end Validate_Interface_Type_Instance;
8617 ------------------------------------
8618 -- Validate_Private_Type_Instance --
8619 ------------------------------------
8621 procedure Validate_Private_Type_Instance is
8622 Formal_Discr : Entity_Id;
8623 Actual_Discr : Entity_Id;
8624 Formal_Subt : Entity_Id;
8626 begin
8627 if Is_Limited_Type (Act_T)
8628 and then not Is_Limited_Type (A_Gen_T)
8629 then
8630 Error_Msg_NE
8631 ("actual for non-limited & cannot be a limited type", Actual,
8632 Gen_T);
8633 Explain_Limited_Type (Act_T, Actual);
8634 Abandon_Instantiation (Actual);
8636 elsif Is_Indefinite_Subtype (Act_T)
8637 and then not Is_Indefinite_Subtype (A_Gen_T)
8638 and then Ada_Version >= Ada_95
8639 then
8640 Error_Msg_NE
8641 ("actual for & must be a definite subtype", Actual, Gen_T);
8643 elsif not Is_Tagged_Type (Act_T)
8644 and then Is_Tagged_Type (A_Gen_T)
8645 then
8646 Error_Msg_NE
8647 ("actual for & must be a tagged type", Actual, Gen_T);
8649 elsif Has_Discriminants (A_Gen_T) then
8650 if not Has_Discriminants (Act_T) then
8651 Error_Msg_NE
8652 ("actual for & must have discriminants", Actual, Gen_T);
8653 Abandon_Instantiation (Actual);
8655 elsif Is_Constrained (Act_T) then
8656 Error_Msg_NE
8657 ("actual for & must be unconstrained", Actual, Gen_T);
8658 Abandon_Instantiation (Actual);
8660 else
8661 Formal_Discr := First_Discriminant (A_Gen_T);
8662 Actual_Discr := First_Discriminant (Act_T);
8663 while Formal_Discr /= Empty loop
8664 if Actual_Discr = Empty then
8665 Error_Msg_NE
8666 ("discriminants on actual do not match formal",
8667 Actual, Gen_T);
8668 Abandon_Instantiation (Actual);
8669 end if;
8671 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
8673 -- Access discriminants match if designated types do
8675 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
8676 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
8677 E_Anonymous_Access_Type
8678 and then
8679 Get_Instance_Of
8680 (Designated_Type (Base_Type (Formal_Subt))) =
8681 Designated_Type (Base_Type (Etype (Actual_Discr)))
8682 then
8683 null;
8685 elsif Base_Type (Formal_Subt) /=
8686 Base_Type (Etype (Actual_Discr))
8687 then
8688 Error_Msg_NE
8689 ("types of actual discriminants must match formal",
8690 Actual, Gen_T);
8691 Abandon_Instantiation (Actual);
8693 elsif not Subtypes_Statically_Match
8694 (Formal_Subt, Etype (Actual_Discr))
8695 and then Ada_Version >= Ada_95
8696 then
8697 Error_Msg_NE
8698 ("subtypes of actual discriminants must match formal",
8699 Actual, Gen_T);
8700 Abandon_Instantiation (Actual);
8701 end if;
8703 Next_Discriminant (Formal_Discr);
8704 Next_Discriminant (Actual_Discr);
8705 end loop;
8707 if Actual_Discr /= Empty then
8708 Error_Msg_NE
8709 ("discriminants on actual do not match formal",
8710 Actual, Gen_T);
8711 Abandon_Instantiation (Actual);
8712 end if;
8713 end if;
8715 end if;
8717 Ancestor := Gen_T;
8718 end Validate_Private_Type_Instance;
8720 -- Start of processing for Instantiate_Type
8722 begin
8723 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
8724 Error_Msg_N ("duplicate instantiation of generic type", Actual);
8725 return Error;
8727 elsif not Is_Entity_Name (Actual)
8728 or else not Is_Type (Entity (Actual))
8729 then
8730 Error_Msg_NE
8731 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
8732 Abandon_Instantiation (Actual);
8734 else
8735 Act_T := Entity (Actual);
8737 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
8738 -- as a generic actual parameter if the corresponding formal type
8739 -- does not have a known_discriminant_part, or is a formal derived
8740 -- type that is an Unchecked_Union type.
8742 if Is_Unchecked_Union (Base_Type (Act_T)) then
8743 if not Has_Discriminants (A_Gen_T)
8744 or else
8745 (Is_Derived_Type (A_Gen_T)
8746 and then
8747 Is_Unchecked_Union (A_Gen_T))
8748 then
8749 null;
8750 else
8751 Error_Msg_N ("Unchecked_Union cannot be the actual for a" &
8752 " discriminated formal type", Act_T);
8754 end if;
8755 end if;
8757 -- Deal with fixed/floating restrictions
8759 if Is_Floating_Point_Type (Act_T) then
8760 Check_Restriction (No_Floating_Point, Actual);
8761 elsif Is_Fixed_Point_Type (Act_T) then
8762 Check_Restriction (No_Fixed_Point, Actual);
8763 end if;
8765 -- Deal with error of using incomplete type as generic actual
8767 if Ekind (Act_T) = E_Incomplete_Type then
8768 if No (Underlying_Type (Act_T)) then
8769 Error_Msg_N ("premature use of incomplete type", Actual);
8770 Abandon_Instantiation (Actual);
8771 else
8772 Act_T := Full_View (Act_T);
8773 Set_Entity (Actual, Act_T);
8775 if Has_Private_Component (Act_T) then
8776 Error_Msg_N
8777 ("premature use of type with private component", Actual);
8778 end if;
8779 end if;
8781 -- Deal with error of premature use of private type as generic actual
8783 elsif Is_Private_Type (Act_T)
8784 and then Is_Private_Type (Base_Type (Act_T))
8785 and then not Is_Generic_Type (Act_T)
8786 and then not Is_Derived_Type (Act_T)
8787 and then No (Full_View (Root_Type (Act_T)))
8788 then
8789 Error_Msg_N ("premature use of private type", Actual);
8791 elsif Has_Private_Component (Act_T) then
8792 Error_Msg_N
8793 ("premature use of type with private component", Actual);
8794 end if;
8796 Set_Instance_Of (A_Gen_T, Act_T);
8798 -- If the type is generic, the class-wide type may also be used
8800 if Is_Tagged_Type (A_Gen_T)
8801 and then Is_Tagged_Type (Act_T)
8802 and then not Is_Class_Wide_Type (A_Gen_T)
8803 then
8804 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
8805 Class_Wide_Type (Act_T));
8806 end if;
8808 if not Is_Abstract (A_Gen_T)
8809 and then Is_Abstract (Act_T)
8810 then
8811 Error_Msg_N
8812 ("actual of non-abstract formal cannot be abstract", Actual);
8813 end if;
8815 if Is_Scalar_Type (Gen_T) then
8816 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
8817 end if;
8818 end if;
8820 case Nkind (Def) is
8821 when N_Formal_Private_Type_Definition =>
8822 Validate_Private_Type_Instance;
8824 when N_Formal_Derived_Type_Definition =>
8825 Validate_Derived_Type_Instance;
8827 when N_Formal_Discrete_Type_Definition =>
8828 if not Is_Discrete_Type (Act_T) then
8829 Error_Msg_NE
8830 ("expect discrete type in instantiation of&", Actual, Gen_T);
8831 Abandon_Instantiation (Actual);
8832 end if;
8834 when N_Formal_Signed_Integer_Type_Definition =>
8835 if not Is_Signed_Integer_Type (Act_T) then
8836 Error_Msg_NE
8837 ("expect signed integer type in instantiation of&",
8838 Actual, Gen_T);
8839 Abandon_Instantiation (Actual);
8840 end if;
8842 when N_Formal_Modular_Type_Definition =>
8843 if not Is_Modular_Integer_Type (Act_T) then
8844 Error_Msg_NE
8845 ("expect modular type in instantiation of &", Actual, Gen_T);
8846 Abandon_Instantiation (Actual);
8847 end if;
8849 when N_Formal_Floating_Point_Definition =>
8850 if not Is_Floating_Point_Type (Act_T) then
8851 Error_Msg_NE
8852 ("expect float type in instantiation of &", Actual, Gen_T);
8853 Abandon_Instantiation (Actual);
8854 end if;
8856 when N_Formal_Ordinary_Fixed_Point_Definition =>
8857 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
8858 Error_Msg_NE
8859 ("expect ordinary fixed point type in instantiation of &",
8860 Actual, Gen_T);
8861 Abandon_Instantiation (Actual);
8862 end if;
8864 when N_Formal_Decimal_Fixed_Point_Definition =>
8865 if not Is_Decimal_Fixed_Point_Type (Act_T) then
8866 Error_Msg_NE
8867 ("expect decimal type in instantiation of &",
8868 Actual, Gen_T);
8869 Abandon_Instantiation (Actual);
8870 end if;
8872 when N_Array_Type_Definition =>
8873 Validate_Array_Type_Instance;
8875 when N_Access_To_Object_Definition =>
8876 Validate_Access_Type_Instance;
8878 when N_Access_Function_Definition |
8879 N_Access_Procedure_Definition =>
8880 Validate_Access_Subprogram_Instance;
8882 when N_Record_Definition =>
8883 Validate_Interface_Type_Instance;
8885 when N_Derived_Type_Definition =>
8886 Validate_Derived_Interface_Type_Instance;
8888 when others =>
8889 raise Program_Error;
8891 end case;
8893 Decl_Node :=
8894 Make_Subtype_Declaration (Loc,
8895 Defining_Identifier => New_Copy (Gen_T),
8896 Subtype_Indication => New_Reference_To (Act_T, Loc));
8898 if Is_Private_Type (Act_T) then
8899 Set_Has_Private_View (Subtype_Indication (Decl_Node));
8901 elsif Is_Access_Type (Act_T)
8902 and then Is_Private_Type (Designated_Type (Act_T))
8903 then
8904 Set_Has_Private_View (Subtype_Indication (Decl_Node));
8905 end if;
8907 -- Flag actual derived types so their elaboration produces the
8908 -- appropriate renamings for the primitive operations of the ancestor.
8909 -- Flag actual for formal private types as well, to determine whether
8910 -- operations in the private part may override inherited operations.
8912 if Nkind (Def) = N_Formal_Derived_Type_Definition
8913 or else Nkind (Def) = N_Formal_Private_Type_Definition
8914 then
8915 Set_Generic_Parent_Type (Decl_Node, Ancestor);
8916 end if;
8918 return Decl_Node;
8919 end Instantiate_Type;
8921 ---------------------
8922 -- Is_In_Main_Unit --
8923 ---------------------
8925 function Is_In_Main_Unit (N : Node_Id) return Boolean is
8926 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
8927 Current_Unit : Node_Id;
8929 begin
8930 if Unum = Main_Unit then
8931 return True;
8933 -- If the current unit is a subunit then it is either the main unit
8934 -- or is being compiled as part of the main unit.
8936 elsif Nkind (N) = N_Compilation_Unit then
8937 return Nkind (Unit (N)) = N_Subunit;
8938 end if;
8940 Current_Unit := Parent (N);
8941 while Present (Current_Unit)
8942 and then Nkind (Current_Unit) /= N_Compilation_Unit
8943 loop
8944 Current_Unit := Parent (Current_Unit);
8945 end loop;
8947 -- The instantiation node is in the main unit, or else the current
8948 -- node (perhaps as the result of nested instantiations) is in the
8949 -- main unit, or in the declaration of the main unit, which in this
8950 -- last case must be a body.
8952 return Unum = Main_Unit
8953 or else Current_Unit = Cunit (Main_Unit)
8954 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
8955 or else (Present (Library_Unit (Current_Unit))
8956 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
8957 end Is_In_Main_Unit;
8959 ----------------------------
8960 -- Load_Parent_Of_Generic --
8961 ----------------------------
8963 procedure Load_Parent_Of_Generic (N : Node_Id; Spec : Node_Id) is
8964 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
8965 Save_Style_Check : constant Boolean := Style_Check;
8966 True_Parent : Node_Id;
8967 Inst_Node : Node_Id;
8968 OK : Boolean;
8970 begin
8971 if not In_Same_Source_Unit (N, Spec)
8972 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
8973 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
8974 and then not Is_In_Main_Unit (Spec))
8975 then
8976 -- Find body of parent of spec, and analyze it. A special case
8977 -- arises when the parent is an instantiation, that is to say when
8978 -- we are currently instantiating a nested generic. In that case,
8979 -- there is no separate file for the body of the enclosing instance.
8980 -- Instead, the enclosing body must be instantiated as if it were
8981 -- a pending instantiation, in order to produce the body for the
8982 -- nested generic we require now. Note that in that case the
8983 -- generic may be defined in a package body, the instance defined
8984 -- in the same package body, and the original enclosing body may not
8985 -- be in the main unit.
8987 True_Parent := Parent (Spec);
8988 Inst_Node := Empty;
8990 while Present (True_Parent)
8991 and then Nkind (True_Parent) /= N_Compilation_Unit
8992 loop
8993 if Nkind (True_Parent) = N_Package_Declaration
8994 and then
8995 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
8996 then
8997 -- Parent is a compilation unit that is an instantiation.
8998 -- Instantiation node has been replaced with package decl.
9000 Inst_Node := Original_Node (True_Parent);
9001 exit;
9003 elsif Nkind (True_Parent) = N_Package_Declaration
9004 and then Present (Generic_Parent (Specification (True_Parent)))
9005 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
9006 then
9007 -- Parent is an instantiation within another specification.
9008 -- Declaration for instance has been inserted before original
9009 -- instantiation node. A direct link would be preferable?
9011 Inst_Node := Next (True_Parent);
9013 while Present (Inst_Node)
9014 and then Nkind (Inst_Node) /= N_Package_Instantiation
9015 loop
9016 Next (Inst_Node);
9017 end loop;
9019 -- If the instance appears within a generic, and the generic
9020 -- unit is defined within a formal package of the enclosing
9021 -- generic, there is no generic body available, and none
9022 -- needed. A more precise test should be used ???
9024 if No (Inst_Node) then
9025 return;
9026 end if;
9028 exit;
9029 else
9030 True_Parent := Parent (True_Parent);
9031 end if;
9032 end loop;
9034 -- Case where we are currently instantiating a nested generic
9036 if Present (Inst_Node) then
9037 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
9039 -- Instantiation node and declaration of instantiated package
9040 -- were exchanged when only the declaration was needed.
9041 -- Restore instantiation node before proceeding with body.
9043 Set_Unit (Parent (True_Parent), Inst_Node);
9044 end if;
9046 -- Now complete instantiation of enclosing body, if it appears
9047 -- in some other unit. If it appears in the current unit, the
9048 -- body will have been instantiated already.
9050 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
9052 -- We need to determine the expander mode to instantiate
9053 -- the enclosing body. Because the generic body we need
9054 -- may use global entities declared in the enclosing package
9055 -- (including aggregates) it is in general necessary to
9056 -- compile this body with expansion enabled. The exception
9057 -- is if we are within a generic package, in which case
9058 -- the usual generic rule applies.
9060 declare
9061 Exp_Status : Boolean := True;
9062 Scop : Entity_Id;
9064 begin
9065 -- Loop through scopes looking for generic package
9067 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
9068 while Present (Scop)
9069 and then Scop /= Standard_Standard
9070 loop
9071 if Ekind (Scop) = E_Generic_Package then
9072 Exp_Status := False;
9073 exit;
9074 end if;
9076 Scop := Scope (Scop);
9077 end loop;
9079 Instantiate_Package_Body
9080 (Pending_Body_Info'(
9081 Inst_Node, True_Parent, Exp_Status,
9082 Get_Code_Unit (Sloc (Inst_Node))));
9083 end;
9084 end if;
9086 -- Case where we are not instantiating a nested generic
9088 else
9089 Opt.Style_Check := False;
9090 Expander_Mode_Save_And_Set (True);
9091 Load_Needed_Body (Comp_Unit, OK);
9092 Opt.Style_Check := Save_Style_Check;
9093 Expander_Mode_Restore;
9095 if not OK
9096 and then Unit_Requires_Body (Defining_Entity (Spec))
9097 then
9098 declare
9099 Bname : constant Unit_Name_Type :=
9100 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
9102 begin
9103 Error_Msg_Unit_1 := Bname;
9104 Error_Msg_N ("this instantiation requires$!", N);
9105 Error_Msg_Name_1 :=
9106 Get_File_Name (Bname, Subunit => False);
9107 Error_Msg_N ("\but file{ was not found!", N);
9108 raise Unrecoverable_Error;
9109 end;
9110 end if;
9111 end if;
9112 end if;
9114 -- If loading the parent of the generic caused an instantiation
9115 -- circularity, we abandon compilation at this point, because
9116 -- otherwise in some cases we get into trouble with infinite
9117 -- recursions after this point.
9119 if Circularity_Detected then
9120 raise Unrecoverable_Error;
9121 end if;
9122 end Load_Parent_Of_Generic;
9124 -----------------------
9125 -- Move_Freeze_Nodes --
9126 -----------------------
9128 procedure Move_Freeze_Nodes
9129 (Out_Of : Entity_Id;
9130 After : Node_Id;
9131 L : List_Id)
9133 Decl : Node_Id;
9134 Next_Decl : Node_Id;
9135 Next_Node : Node_Id := After;
9136 Spec : Node_Id;
9138 function Is_Outer_Type (T : Entity_Id) return Boolean;
9139 -- Check whether entity is declared in a scope external to that
9140 -- of the generic unit.
9142 -------------------
9143 -- Is_Outer_Type --
9144 -------------------
9146 function Is_Outer_Type (T : Entity_Id) return Boolean is
9147 Scop : Entity_Id := Scope (T);
9149 begin
9150 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
9151 return True;
9153 else
9154 while Scop /= Standard_Standard loop
9156 if Scop = Out_Of then
9157 return False;
9158 else
9159 Scop := Scope (Scop);
9160 end if;
9161 end loop;
9163 return True;
9164 end if;
9165 end Is_Outer_Type;
9167 -- Start of processing for Move_Freeze_Nodes
9169 begin
9170 if No (L) then
9171 return;
9172 end if;
9174 -- First remove the freeze nodes that may appear before all other
9175 -- declarations.
9177 Decl := First (L);
9178 while Present (Decl)
9179 and then Nkind (Decl) = N_Freeze_Entity
9180 and then Is_Outer_Type (Entity (Decl))
9181 loop
9182 Decl := Remove_Head (L);
9183 Insert_After (Next_Node, Decl);
9184 Set_Analyzed (Decl, False);
9185 Next_Node := Decl;
9186 Decl := First (L);
9187 end loop;
9189 -- Next scan the list of declarations and remove each freeze node that
9190 -- appears ahead of the current node.
9192 while Present (Decl) loop
9193 while Present (Next (Decl))
9194 and then Nkind (Next (Decl)) = N_Freeze_Entity
9195 and then Is_Outer_Type (Entity (Next (Decl)))
9196 loop
9197 Next_Decl := Remove_Next (Decl);
9198 Insert_After (Next_Node, Next_Decl);
9199 Set_Analyzed (Next_Decl, False);
9200 Next_Node := Next_Decl;
9201 end loop;
9203 -- If the declaration is a nested package or concurrent type, then
9204 -- recurse. Nested generic packages will have been processed from the
9205 -- inside out.
9207 if Nkind (Decl) = N_Package_Declaration then
9208 Spec := Specification (Decl);
9210 elsif Nkind (Decl) = N_Task_Type_Declaration then
9211 Spec := Task_Definition (Decl);
9213 elsif Nkind (Decl) = N_Protected_Type_Declaration then
9214 Spec := Protected_Definition (Decl);
9216 else
9217 Spec := Empty;
9218 end if;
9220 if Present (Spec) then
9221 Move_Freeze_Nodes (Out_Of, Next_Node,
9222 Visible_Declarations (Spec));
9223 Move_Freeze_Nodes (Out_Of, Next_Node,
9224 Private_Declarations (Spec));
9225 end if;
9227 Next (Decl);
9228 end loop;
9229 end Move_Freeze_Nodes;
9231 ----------------
9232 -- Next_Assoc --
9233 ----------------
9235 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
9236 begin
9237 return Generic_Renamings.Table (E).Next_In_HTable;
9238 end Next_Assoc;
9240 ------------------------
9241 -- Preanalyze_Actuals --
9242 ------------------------
9244 procedure Pre_Analyze_Actuals (N : Node_Id) is
9245 Assoc : Node_Id;
9246 Act : Node_Id;
9247 Errs : constant Int := Serious_Errors_Detected;
9249 begin
9250 Assoc := First (Generic_Associations (N));
9252 while Present (Assoc) loop
9253 Act := Explicit_Generic_Actual_Parameter (Assoc);
9255 -- Within a nested instantiation, a defaulted actual is an
9256 -- empty association, so nothing to analyze. If the actual for
9257 -- a subprogram is an attribute, analyze prefix only, because
9258 -- actual is not a complete attribute reference.
9260 -- If actual is an allocator, analyze expression only. The full
9261 -- analysis can generate code, and if the instance is a compilation
9262 -- unit we have to wait until the package instance is installed to
9263 -- have a proper place to insert this code.
9265 -- String literals may be operators, but at this point we do not
9266 -- know whether the actual is a formal subprogram or a string.
9268 if No (Act) then
9269 null;
9271 elsif Nkind (Act) = N_Attribute_Reference then
9272 Analyze (Prefix (Act));
9274 elsif Nkind (Act) = N_Explicit_Dereference then
9275 Analyze (Prefix (Act));
9277 elsif Nkind (Act) = N_Allocator then
9278 declare
9279 Expr : constant Node_Id := Expression (Act);
9281 begin
9282 if Nkind (Expr) = N_Subtype_Indication then
9283 Analyze (Subtype_Mark (Expr));
9284 Analyze_List (Constraints (Constraint (Expr)));
9285 else
9286 Analyze (Expr);
9287 end if;
9288 end;
9290 elsif Nkind (Act) /= N_Operator_Symbol then
9291 Analyze (Act);
9292 end if;
9294 if Errs /= Serious_Errors_Detected then
9295 Abandon_Instantiation (Act);
9296 end if;
9298 Next (Assoc);
9299 end loop;
9300 end Pre_Analyze_Actuals;
9302 -------------------
9303 -- Remove_Parent --
9304 -------------------
9306 procedure Remove_Parent (In_Body : Boolean := False) is
9307 S : Entity_Id := Current_Scope;
9308 E : Entity_Id;
9309 P : Entity_Id;
9310 Hidden : Elmt_Id;
9312 begin
9313 -- After child instantiation is complete, remove from scope stack
9314 -- the extra copy of the current scope, and then remove parent
9315 -- instances.
9317 if not In_Body then
9318 Pop_Scope;
9320 while Current_Scope /= S loop
9321 P := Current_Scope;
9322 End_Package_Scope (Current_Scope);
9324 if In_Open_Scopes (P) then
9325 E := First_Entity (P);
9327 while Present (E) loop
9328 Set_Is_Immediately_Visible (E, True);
9329 Next_Entity (E);
9330 end loop;
9332 if Is_Generic_Instance (Current_Scope)
9333 and then P /= Current_Scope
9334 then
9335 -- We are within an instance of some sibling. Retain
9336 -- visibility of parent, for proper subsequent cleanup,
9337 -- and reinstall private declarations as well.
9339 Set_In_Private_Part (P);
9340 Install_Private_Declarations (P);
9341 end if;
9343 -- If the ultimate parent is a top-level unit recorded in
9344 -- Instance_Parent_Unit, then reset its visibility to what
9345 -- it was before instantiation. (It's not clear what the
9346 -- purpose is of testing whether Scope (P) is In_Open_Scopes,
9347 -- but that test was present before the ultimate parent test
9348 -- was added.???)
9350 elsif not In_Open_Scopes (Scope (P))
9351 or else (P = Instance_Parent_Unit
9352 and then not Parent_Unit_Visible)
9353 then
9354 Set_Is_Immediately_Visible (P, False);
9355 end if;
9356 end loop;
9358 -- Reset visibility of entities in the enclosing scope
9360 Set_Is_Hidden_Open_Scope (Current_Scope, False);
9361 Hidden := First_Elmt (Hidden_Entities);
9363 while Present (Hidden) loop
9364 Set_Is_Immediately_Visible (Node (Hidden), True);
9365 Next_Elmt (Hidden);
9366 end loop;
9368 else
9369 -- Each body is analyzed separately, and there is no context
9370 -- that needs preserving from one body instance to the next,
9371 -- so remove all parent scopes that have been installed.
9373 while Present (S) loop
9374 End_Package_Scope (S);
9375 Set_Is_Immediately_Visible (S, False);
9376 S := Current_Scope;
9377 exit when S = Standard_Standard;
9378 end loop;
9379 end if;
9381 end Remove_Parent;
9383 -----------------
9384 -- Restore_Env --
9385 -----------------
9387 procedure Restore_Env is
9388 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
9390 begin
9391 Ada_Version := Saved.Ada_Version;
9392 Ada_Version_Explicit := Saved.Ada_Version_Explicit;
9394 if No (Current_Instantiated_Parent.Act_Id) then
9396 -- Restore environment after subprogram inlining
9398 Restore_Private_Views (Empty);
9399 end if;
9401 Current_Instantiated_Parent := Saved.Instantiated_Parent;
9402 Exchanged_Views := Saved.Exchanged_Views;
9403 Hidden_Entities := Saved.Hidden_Entities;
9404 Current_Sem_Unit := Saved.Current_Sem_Unit;
9405 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
9406 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
9408 Instance_Envs.Decrement_Last;
9409 end Restore_Env;
9411 ---------------------------
9412 -- Restore_Private_Views --
9413 ---------------------------
9415 procedure Restore_Private_Views
9416 (Pack_Id : Entity_Id;
9417 Is_Package : Boolean := True)
9419 M : Elmt_Id;
9420 E : Entity_Id;
9421 Typ : Entity_Id;
9422 Dep_Elmt : Elmt_Id;
9423 Dep_Typ : Node_Id;
9425 procedure Restore_Nested_Formal (Formal : Entity_Id);
9426 -- Hide the generic formals of formal packages declared with box
9427 -- which were reachable in the current instantiation.
9429 procedure Restore_Nested_Formal (Formal : Entity_Id) is
9430 Ent : Entity_Id;
9431 begin
9432 if Present (Renamed_Object (Formal))
9433 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
9434 then
9435 return;
9437 elsif Present (Associated_Formal_Package (Formal))
9438 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9439 then
9440 Ent := First_Entity (Formal);
9442 while Present (Ent) loop
9443 exit when Ekind (Ent) = E_Package
9444 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
9446 Set_Is_Hidden (Ent);
9447 Set_Is_Potentially_Use_Visible (Ent, False);
9449 -- If package, then recurse
9451 if Ekind (Ent) = E_Package then
9452 Restore_Nested_Formal (Ent);
9453 end if;
9455 Next_Entity (Ent);
9456 end loop;
9457 end if;
9458 end Restore_Nested_Formal;
9460 begin
9461 M := First_Elmt (Exchanged_Views);
9462 while Present (M) loop
9463 Typ := Node (M);
9465 -- Subtypes of types whose views have been exchanged, and that
9466 -- are defined within the instance, were not on the list of
9467 -- Private_Dependents on entry to the instance, so they have to
9468 -- be exchanged explicitly now, in order to remain consistent with
9469 -- the view of the parent type.
9471 if Ekind (Typ) = E_Private_Type
9472 or else Ekind (Typ) = E_Limited_Private_Type
9473 or else Ekind (Typ) = E_Record_Type_With_Private
9474 then
9475 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
9477 while Present (Dep_Elmt) loop
9478 Dep_Typ := Node (Dep_Elmt);
9480 if Scope (Dep_Typ) = Pack_Id
9481 and then Present (Full_View (Dep_Typ))
9482 then
9483 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
9484 Exchange_Declarations (Dep_Typ);
9485 end if;
9487 Next_Elmt (Dep_Elmt);
9488 end loop;
9489 end if;
9491 Exchange_Declarations (Node (M));
9492 Next_Elmt (M);
9493 end loop;
9495 if No (Pack_Id) then
9496 return;
9497 end if;
9499 -- Make the generic formal parameters private, and make the formal
9500 -- types into subtypes of the actuals again.
9502 E := First_Entity (Pack_Id);
9504 while Present (E) loop
9505 Set_Is_Hidden (E, True);
9507 if Is_Type (E)
9508 and then Nkind (Parent (E)) = N_Subtype_Declaration
9509 then
9510 Set_Is_Generic_Actual_Type (E, False);
9512 -- An unusual case of aliasing: the actual may also be directly
9513 -- visible in the generic, and be private there, while it is
9514 -- fully visible in the context of the instance. The internal
9515 -- subtype is private in the instance, but has full visibility
9516 -- like its parent in the enclosing scope. This enforces the
9517 -- invariant that the privacy status of all private dependents of
9518 -- a type coincide with that of the parent type. This can only
9519 -- happen when a generic child unit is instantiated within a
9520 -- sibling.
9522 if Is_Private_Type (E)
9523 and then not Is_Private_Type (Etype (E))
9524 then
9525 Exchange_Declarations (E);
9526 end if;
9528 elsif Ekind (E) = E_Package then
9530 -- The end of the renaming list is the renaming of the generic
9531 -- package itself. If the instance is a subprogram, all entities
9532 -- in the corresponding package are renamings. If this entity is
9533 -- a formal package, make its own formals private as well. The
9534 -- actual in this case is itself the renaming of an instantation.
9535 -- If the entity is not a package renaming, it is the entity
9536 -- created to validate formal package actuals: ignore.
9538 -- If the actual is itself a formal package for the enclosing
9539 -- generic, or the actual for such a formal package, it remains
9540 -- visible on exit from the instance, and therefore nothing
9541 -- needs to be done either, except to keep it accessible.
9543 if Is_Package
9544 and then Renamed_Object (E) = Pack_Id
9545 then
9546 exit;
9548 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
9549 null;
9551 elsif Denotes_Formal_Package (Renamed_Object (E), True) then
9552 Set_Is_Hidden (E, False);
9554 else
9555 declare
9556 Act_P : constant Entity_Id := Renamed_Object (E);
9557 Id : Entity_Id;
9559 begin
9560 Id := First_Entity (Act_P);
9561 while Present (Id)
9562 and then Id /= First_Private_Entity (Act_P)
9563 loop
9564 exit when Ekind (Id) = E_Package
9565 and then Renamed_Object (Id) = Act_P;
9567 Set_Is_Hidden (Id, True);
9568 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
9570 if Ekind (Id) = E_Package then
9571 Restore_Nested_Formal (Id);
9572 end if;
9574 Next_Entity (Id);
9575 end loop;
9576 end;
9577 end if;
9578 end if;
9580 Next_Entity (E);
9581 end loop;
9582 end Restore_Private_Views;
9584 --------------
9585 -- Save_Env --
9586 --------------
9588 procedure Save_Env
9589 (Gen_Unit : Entity_Id;
9590 Act_Unit : Entity_Id)
9592 begin
9593 Init_Env;
9594 Set_Instance_Env (Gen_Unit, Act_Unit);
9595 end Save_Env;
9597 ----------------------------
9598 -- Save_Global_References --
9599 ----------------------------
9601 procedure Save_Global_References (N : Node_Id) is
9602 Gen_Scope : Entity_Id;
9603 E : Entity_Id;
9604 N2 : Node_Id;
9606 function Is_Global (E : Entity_Id) return Boolean;
9607 -- Check whether entity is defined outside of generic unit.
9608 -- Examine the scope of an entity, and the scope of the scope,
9609 -- etc, until we find either Standard, in which case the entity
9610 -- is global, or the generic unit itself, which indicates that
9611 -- the entity is local. If the entity is the generic unit itself,
9612 -- as in the case of a recursive call, or the enclosing generic unit,
9613 -- if different from the current scope, then it is local as well,
9614 -- because it will be replaced at the point of instantiation. On
9615 -- the other hand, if it is a reference to a child unit of a common
9616 -- ancestor, which appears in an instantiation, it is global because
9617 -- it is used to denote a specific compilation unit at the time the
9618 -- instantiations will be analyzed.
9620 procedure Reset_Entity (N : Node_Id);
9621 -- Save semantic information on global entity, so that it is not
9622 -- resolved again at instantiation time.
9624 procedure Save_Entity_Descendants (N : Node_Id);
9625 -- Apply Save_Global_References to the two syntactic descendants of
9626 -- non-terminal nodes that carry an Associated_Node and are processed
9627 -- through Reset_Entity. Once the global entity (if any) has been
9628 -- captured together with its type, only two syntactic descendants
9629 -- need to be traversed to complete the processing of the tree rooted
9630 -- at N. This applies to Selected_Components, Expanded_Names, and to
9631 -- Operator nodes. N can also be a character literal, identifier, or
9632 -- operator symbol node, but the call has no effect in these cases.
9634 procedure Save_Global_Defaults (N1, N2 : Node_Id);
9635 -- Default actuals in nested instances must be handled specially
9636 -- because there is no link to them from the original tree. When an
9637 -- actual subprogram is given by a default, we add an explicit generic
9638 -- association for it in the instantiation node. When we save the
9639 -- global references on the name of the instance, we recover the list
9640 -- of generic associations, and add an explicit one to the original
9641 -- generic tree, through which a global actual can be preserved.
9642 -- Similarly, if a child unit is instantiated within a sibling, in the
9643 -- context of the parent, we must preserve the identifier of the parent
9644 -- so that it can be properly resolved in a subsequent instantiation.
9646 procedure Save_Global_Descendant (D : Union_Id);
9647 -- Apply Save_Global_References recursively to the descendents of
9648 -- current node.
9650 procedure Save_References (N : Node_Id);
9651 -- This is the recursive procedure that does the work, once the
9652 -- enclosing generic scope has been established.
9654 ---------------
9655 -- Is_Global --
9656 ---------------
9658 function Is_Global (E : Entity_Id) return Boolean is
9659 Se : Entity_Id := Scope (E);
9661 function Is_Instance_Node (Decl : Node_Id) return Boolean;
9662 -- Determine whether the parent node of a reference to a child unit
9663 -- denotes an instantiation or a formal package, in which case the
9664 -- reference to the child unit is global, even if it appears within
9665 -- the current scope (e.g. when the instance appears within the body
9666 -- of an ancestor).
9668 ----------------------
9669 -- Is_Instance_Node --
9670 ----------------------
9672 function Is_Instance_Node (Decl : Node_Id) return Boolean is
9673 begin
9674 return (Nkind (Decl) in N_Generic_Instantiation
9675 or else
9676 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration);
9677 end Is_Instance_Node;
9679 -- Start of processing for Is_Global
9681 begin
9682 if E = Gen_Scope then
9683 return False;
9685 elsif E = Standard_Standard then
9686 return True;
9688 elsif Is_Child_Unit (E)
9689 and then (Is_Instance_Node (Parent (N2))
9690 or else (Nkind (Parent (N2)) = N_Expanded_Name
9691 and then N2 = Selector_Name (Parent (N2))
9692 and then Is_Instance_Node (Parent (Parent (N2)))))
9693 then
9694 return True;
9696 else
9697 while Se /= Gen_Scope loop
9698 if Se = Standard_Standard then
9699 return True;
9700 else
9701 Se := Scope (Se);
9702 end if;
9703 end loop;
9705 return False;
9706 end if;
9707 end Is_Global;
9709 ------------------
9710 -- Reset_Entity --
9711 ------------------
9713 procedure Reset_Entity (N : Node_Id) is
9715 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
9716 -- The type of N2 is global to the generic unit. Save the
9717 -- type in the generic node.
9719 function Top_Ancestor (E : Entity_Id) return Entity_Id;
9720 -- Find the ultimate ancestor of the current unit. If it is
9721 -- not a generic unit, then the name of the current unit
9722 -- in the prefix of an expanded name must be replaced with
9723 -- its generic homonym to ensure that it will be properly
9724 -- resolved in an instance.
9726 ---------------------
9727 -- Set_Global_Type --
9728 ---------------------
9730 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
9731 Typ : constant Entity_Id := Etype (N2);
9733 begin
9734 Set_Etype (N, Typ);
9736 if Entity (N) /= N2
9737 and then Has_Private_View (Entity (N))
9738 then
9739 -- If the entity of N is not the associated node, this is
9740 -- a nested generic and it has an associated node as well,
9741 -- whose type is already the full view (see below). Indicate
9742 -- that the original node has a private view.
9744 Set_Has_Private_View (N);
9745 end if;
9747 -- If not a private type, nothing else to do
9749 if not Is_Private_Type (Typ) then
9750 if Is_Array_Type (Typ)
9751 and then Is_Private_Type (Component_Type (Typ))
9752 then
9753 Set_Has_Private_View (N);
9754 end if;
9756 -- If it is a derivation of a private type in a context where
9757 -- no full view is needed, nothing to do either.
9759 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
9760 null;
9762 -- Otherwise mark the type for flipping and use the full_view
9763 -- when available.
9765 else
9766 Set_Has_Private_View (N);
9768 if Present (Full_View (Typ)) then
9769 Set_Etype (N2, Full_View (Typ));
9770 end if;
9771 end if;
9772 end Set_Global_Type;
9774 ------------------
9775 -- Top_Ancestor --
9776 ------------------
9778 function Top_Ancestor (E : Entity_Id) return Entity_Id is
9779 Par : Entity_Id := E;
9781 begin
9782 while Is_Child_Unit (Par) loop
9783 Par := Scope (Par);
9784 end loop;
9786 return Par;
9787 end Top_Ancestor;
9789 -- Start of processing for Reset_Entity
9791 begin
9792 N2 := Get_Associated_Node (N);
9793 E := Entity (N2);
9795 if Present (E) then
9796 if Is_Global (E) then
9797 Set_Global_Type (N, N2);
9799 elsif Nkind (N) = N_Op_Concat
9800 and then Is_Generic_Type (Etype (N2))
9801 and then
9802 (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
9803 or else Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
9804 and then Is_Intrinsic_Subprogram (E)
9805 then
9806 null;
9808 else
9809 -- Entity is local. Mark generic node as unresolved.
9810 -- Note that now it does not have an entity.
9812 Set_Associated_Node (N, Empty);
9813 Set_Etype (N, Empty);
9814 end if;
9816 if Nkind (Parent (N)) in N_Generic_Instantiation
9817 and then N = Name (Parent (N))
9818 then
9819 Save_Global_Defaults (Parent (N), Parent (N2));
9820 end if;
9822 elsif Nkind (Parent (N)) = N_Selected_Component
9823 and then Nkind (Parent (N2)) = N_Expanded_Name
9824 then
9825 if Is_Global (Entity (Parent (N2))) then
9826 Change_Selected_Component_To_Expanded_Name (Parent (N));
9827 Set_Associated_Node (Parent (N), Parent (N2));
9828 Set_Global_Type (Parent (N), Parent (N2));
9829 Save_Entity_Descendants (N);
9831 -- If this is a reference to the current generic entity,
9832 -- replace by the name of the generic homonym of the current
9833 -- package. This is because in an instantiation Par.P.Q will
9834 -- not resolve to the name of the instance, whose enclosing
9835 -- scope is not necessarily Par. We use the generic homonym
9836 -- rather that the name of the generic itself, because it may
9837 -- be hidden by a local declaration.
9839 elsif In_Open_Scopes (Entity (Parent (N2)))
9840 and then not
9841 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
9842 then
9843 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
9844 Rewrite (Parent (N),
9845 Make_Identifier (Sloc (N),
9846 Chars =>
9847 Chars (Generic_Homonym (Entity (Parent (N2))))));
9848 else
9849 Rewrite (Parent (N),
9850 Make_Identifier (Sloc (N),
9851 Chars => Chars (Selector_Name (Parent (N2)))));
9852 end if;
9853 end if;
9855 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
9856 and then Parent (N) = Name (Parent (Parent (N)))
9857 then
9858 Save_Global_Defaults
9859 (Parent (Parent (N)), Parent (Parent ((N2))));
9860 end if;
9862 -- A selected component may denote a static constant that has
9863 -- been folded. Make the same replacement in original tree.
9865 elsif Nkind (Parent (N)) = N_Selected_Component
9866 and then (Nkind (Parent (N2)) = N_Integer_Literal
9867 or else Nkind (Parent (N2)) = N_Real_Literal)
9868 then
9869 Rewrite (Parent (N),
9870 New_Copy (Parent (N2)));
9871 Set_Analyzed (Parent (N), False);
9873 -- A selected component may be transformed into a parameterless
9874 -- function call. If the called entity is global, rewrite the
9875 -- node appropriately, i.e. as an extended name for the global
9876 -- entity.
9878 elsif Nkind (Parent (N)) = N_Selected_Component
9879 and then Nkind (Parent (N2)) = N_Function_Call
9880 and then Is_Global (Entity (Name (Parent (N2))))
9881 then
9882 Change_Selected_Component_To_Expanded_Name (Parent (N));
9883 Set_Associated_Node (Parent (N), Name (Parent (N2)));
9884 Set_Global_Type (Parent (N), Name (Parent (N2)));
9885 Save_Entity_Descendants (N);
9887 else
9888 -- Entity is local. Reset in generic unit, so that node
9889 -- is resolved anew at the point of instantiation.
9891 Set_Associated_Node (N, Empty);
9892 Set_Etype (N, Empty);
9893 end if;
9894 end Reset_Entity;
9896 -----------------------------
9897 -- Save_Entity_Descendants --
9898 -----------------------------
9900 procedure Save_Entity_Descendants (N : Node_Id) is
9901 begin
9902 case Nkind (N) is
9903 when N_Binary_Op =>
9904 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
9905 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
9907 when N_Unary_Op =>
9908 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
9910 when N_Expanded_Name | N_Selected_Component =>
9911 Save_Global_Descendant (Union_Id (Prefix (N)));
9912 Save_Global_Descendant (Union_Id (Selector_Name (N)));
9914 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
9915 null;
9917 when others =>
9918 raise Program_Error;
9919 end case;
9920 end Save_Entity_Descendants;
9922 --------------------------
9923 -- Save_Global_Defaults --
9924 --------------------------
9926 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
9927 Loc : constant Source_Ptr := Sloc (N1);
9928 Assoc2 : constant List_Id := Generic_Associations (N2);
9929 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
9930 Assoc1 : List_Id;
9931 Act1 : Node_Id;
9932 Act2 : Node_Id;
9933 Def : Node_Id;
9934 Ndec : Node_Id;
9935 Subp : Entity_Id;
9936 Actual : Entity_Id;
9938 begin
9939 Assoc1 := Generic_Associations (N1);
9941 if Present (Assoc1) then
9942 Act1 := First (Assoc1);
9943 else
9944 Act1 := Empty;
9945 Set_Generic_Associations (N1, New_List);
9946 Assoc1 := Generic_Associations (N1);
9947 end if;
9949 if Present (Assoc2) then
9950 Act2 := First (Assoc2);
9951 else
9952 return;
9953 end if;
9955 while Present (Act1) and then Present (Act2) loop
9956 Next (Act1);
9957 Next (Act2);
9958 end loop;
9960 -- Find the associations added for default suprograms
9962 if Present (Act2) then
9963 while Nkind (Act2) /= N_Generic_Association
9964 or else No (Entity (Selector_Name (Act2)))
9965 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
9966 loop
9967 Next (Act2);
9968 end loop;
9970 -- Add a similar association if the default is global. The
9971 -- renaming declaration for the actual has been analyzed, and
9972 -- its alias is the program it renames. Link the actual in the
9973 -- original generic tree with the node in the analyzed tree.
9975 while Present (Act2) loop
9976 Subp := Entity (Selector_Name (Act2));
9977 Def := Explicit_Generic_Actual_Parameter (Act2);
9979 -- Following test is defence against rubbish errors
9981 if No (Alias (Subp)) then
9982 return;
9983 end if;
9985 -- Retrieve the resolved actual from the renaming declaration
9986 -- created for the instantiated formal.
9988 Actual := Entity (Name (Parent (Parent (Subp))));
9989 Set_Entity (Def, Actual);
9990 Set_Etype (Def, Etype (Actual));
9992 if Is_Global (Actual) then
9993 Ndec :=
9994 Make_Generic_Association (Loc,
9995 Selector_Name => New_Occurrence_Of (Subp, Loc),
9996 Explicit_Generic_Actual_Parameter =>
9997 New_Occurrence_Of (Actual, Loc));
9999 Set_Associated_Node
10000 (Explicit_Generic_Actual_Parameter (Ndec), Def);
10002 Append (Ndec, Assoc1);
10004 -- If there are other defaults, add a dummy association
10005 -- in case there are other defaulted formals with the same
10006 -- name.
10008 elsif Present (Next (Act2)) then
10009 Ndec :=
10010 Make_Generic_Association (Loc,
10011 Selector_Name => New_Occurrence_Of (Subp, Loc),
10012 Explicit_Generic_Actual_Parameter => Empty);
10014 Append (Ndec, Assoc1);
10015 end if;
10017 Next (Act2);
10018 end loop;
10019 end if;
10021 if Nkind (Name (N1)) = N_Identifier
10022 and then Is_Child_Unit (Gen_Id)
10023 and then Is_Global (Gen_Id)
10024 and then Is_Generic_Unit (Scope (Gen_Id))
10025 and then In_Open_Scopes (Scope (Gen_Id))
10026 then
10027 -- This is an instantiation of a child unit within a sibling,
10028 -- so that the generic parent is in scope. An eventual instance
10029 -- must occur within the scope of an instance of the parent.
10030 -- Make name in instance into an expanded name, to preserve the
10031 -- identifier of the parent, so it can be resolved subsequently.
10033 Rewrite (Name (N2),
10034 Make_Expanded_Name (Loc,
10035 Chars => Chars (Gen_Id),
10036 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
10037 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
10038 Set_Entity (Name (N2), Gen_Id);
10040 Rewrite (Name (N1),
10041 Make_Expanded_Name (Loc,
10042 Chars => Chars (Gen_Id),
10043 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
10044 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
10046 Set_Associated_Node (Name (N1), Name (N2));
10047 Set_Associated_Node (Prefix (Name (N1)), Empty);
10048 Set_Associated_Node
10049 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
10050 Set_Etype (Name (N1), Etype (Gen_Id));
10051 end if;
10053 end Save_Global_Defaults;
10055 ----------------------------
10056 -- Save_Global_Descendant --
10057 ----------------------------
10059 procedure Save_Global_Descendant (D : Union_Id) is
10060 N1 : Node_Id;
10062 begin
10063 if D in Node_Range then
10064 if D = Union_Id (Empty) then
10065 null;
10067 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
10068 Save_References (Node_Id (D));
10069 end if;
10071 elsif D in List_Range then
10072 if D = Union_Id (No_List)
10073 or else Is_Empty_List (List_Id (D))
10074 then
10075 null;
10077 else
10078 N1 := First (List_Id (D));
10079 while Present (N1) loop
10080 Save_References (N1);
10081 Next (N1);
10082 end loop;
10083 end if;
10085 -- Element list or other non-node field, nothing to do
10087 else
10088 null;
10089 end if;
10090 end Save_Global_Descendant;
10092 ---------------------
10093 -- Save_References --
10094 ---------------------
10096 -- This is the recursive procedure that does the work, once the
10097 -- enclosing generic scope has been established. We have to treat
10098 -- specially a number of node rewritings that are required by semantic
10099 -- processing and which change the kind of nodes in the generic copy:
10100 -- typically constant-folding, replacing an operator node by a string
10101 -- literal, or a selected component by an expanded name. In each of
10102 -- those cases, the transformation is propagated to the generic unit.
10104 procedure Save_References (N : Node_Id) is
10105 begin
10106 if N = Empty then
10107 null;
10109 elsif Nkind (N) = N_Character_Literal
10110 or else Nkind (N) = N_Operator_Symbol
10111 then
10112 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
10113 Reset_Entity (N);
10115 elsif Nkind (N) = N_Operator_Symbol
10116 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
10117 then
10118 Change_Operator_Symbol_To_String_Literal (N);
10119 end if;
10121 elsif Nkind (N) in N_Op then
10123 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
10125 if Nkind (N) = N_Op_Concat then
10126 Set_Is_Component_Left_Opnd (N,
10127 Is_Component_Left_Opnd (Get_Associated_Node (N)));
10129 Set_Is_Component_Right_Opnd (N,
10130 Is_Component_Right_Opnd (Get_Associated_Node (N)));
10131 end if;
10133 Reset_Entity (N);
10134 else
10135 -- Node may be transformed into call to a user-defined operator
10137 N2 := Get_Associated_Node (N);
10139 if Nkind (N2) = N_Function_Call then
10140 E := Entity (Name (N2));
10142 if Present (E)
10143 and then Is_Global (E)
10144 then
10145 Set_Etype (N, Etype (N2));
10146 else
10147 Set_Associated_Node (N, Empty);
10148 Set_Etype (N, Empty);
10149 end if;
10151 elsif Nkind (N2) = N_Integer_Literal
10152 or else Nkind (N2) = N_Real_Literal
10153 or else Nkind (N2) = N_String_Literal
10154 then
10155 -- Operation was constant-folded, perform the same
10156 -- replacement in generic.
10158 Rewrite (N, New_Copy (N2));
10159 Set_Analyzed (N, False);
10161 elsif Nkind (N2) = N_Identifier
10162 and then Ekind (Entity (N2)) = E_Enumeration_Literal
10163 then
10164 -- Same if call was folded into a literal, but in this
10165 -- case retain the entity to avoid spurious ambiguities
10166 -- if id is overloaded at the point of instantiation or
10167 -- inlining.
10169 Rewrite (N, New_Copy (N2));
10170 Set_Analyzed (N, False);
10171 end if;
10172 end if;
10174 -- Complete the check on operands, if node has not been
10175 -- constant-folded.
10177 if Nkind (N) in N_Op then
10178 Save_Entity_Descendants (N);
10179 end if;
10181 elsif Nkind (N) = N_Identifier then
10182 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
10184 -- If this is a discriminant reference, always save it.
10185 -- It is used in the instance to find the corresponding
10186 -- discriminant positionally rather than by name.
10188 Set_Original_Discriminant
10189 (N, Original_Discriminant (Get_Associated_Node (N)));
10190 Reset_Entity (N);
10192 else
10193 N2 := Get_Associated_Node (N);
10195 if Nkind (N2) = N_Function_Call then
10196 E := Entity (Name (N2));
10198 -- Name resolves to a call to parameterless function.
10199 -- If original entity is global, mark node as resolved.
10201 if Present (E)
10202 and then Is_Global (E)
10203 then
10204 Set_Etype (N, Etype (N2));
10205 else
10206 Set_Associated_Node (N, Empty);
10207 Set_Etype (N, Empty);
10208 end if;
10210 elsif
10211 Nkind (N2) = N_Integer_Literal or else
10212 Nkind (N2) = N_Real_Literal or else
10213 Nkind (N2) = N_String_Literal
10214 then
10215 -- Name resolves to named number that is constant-folded,
10216 -- or to string literal from concatenation.
10217 -- Perform the same replacement in generic.
10219 Rewrite (N, New_Copy (N2));
10220 Set_Analyzed (N, False);
10222 elsif Nkind (N2) = N_Explicit_Dereference then
10224 -- An identifier is rewritten as a dereference if it is
10225 -- the prefix in a selected component, and it denotes an
10226 -- access to a composite type, or a parameterless function
10227 -- call that returns an access type.
10229 -- Check whether corresponding entity in prefix is global
10231 if Is_Entity_Name (Prefix (N2))
10232 and then Present (Entity (Prefix (N2)))
10233 and then Is_Global (Entity (Prefix (N2)))
10234 then
10235 Rewrite (N,
10236 Make_Explicit_Dereference (Sloc (N),
10237 Prefix => Make_Identifier (Sloc (N),
10238 Chars => Chars (N))));
10239 Set_Associated_Node (Prefix (N), Prefix (N2));
10241 elsif Nkind (Prefix (N2)) = N_Function_Call
10242 and then Is_Global (Entity (Name (Prefix (N2))))
10243 then
10244 Rewrite (N,
10245 Make_Explicit_Dereference (Sloc (N),
10246 Prefix => Make_Function_Call (Sloc (N),
10247 Name =>
10248 Make_Identifier (Sloc (N),
10249 Chars => Chars (N)))));
10251 Set_Associated_Node
10252 (Name (Prefix (N)), Name (Prefix (N2)));
10254 else
10255 Set_Associated_Node (N, Empty);
10256 Set_Etype (N, Empty);
10257 end if;
10259 -- The subtype mark of a nominally unconstrained object
10260 -- is rewritten as a subtype indication using the bounds
10261 -- of the expression. Recover the original subtype mark.
10263 elsif Nkind (N2) = N_Subtype_Indication
10264 and then Is_Entity_Name (Original_Node (N2))
10265 then
10266 Set_Associated_Node (N, Original_Node (N2));
10267 Reset_Entity (N);
10269 else
10270 null;
10271 end if;
10272 end if;
10274 elsif Nkind (N) in N_Entity then
10275 null;
10277 else
10278 declare
10279 Loc : constant Source_Ptr := Sloc (N);
10280 Qual : Node_Id := Empty;
10281 Typ : Entity_Id := Empty;
10282 Nam : Node_Id;
10284 use Atree.Unchecked_Access;
10285 -- This code section is part of implementing an untyped tree
10286 -- traversal, so it needs direct access to node fields.
10288 begin
10289 if Nkind (N) = N_Aggregate
10290 or else
10291 Nkind (N) = N_Extension_Aggregate
10292 then
10293 N2 := Get_Associated_Node (N);
10295 if No (N2) then
10296 Typ := Empty;
10297 else
10298 Typ := Etype (N2);
10300 -- In an instance within a generic, use the name of
10301 -- the actual and not the original generic parameter.
10302 -- If the actual is global in the current generic it
10303 -- must be preserved for its instantiation.
10305 if Nkind (Parent (Typ)) = N_Subtype_Declaration
10306 and then
10307 Present (Generic_Parent_Type (Parent (Typ)))
10308 then
10309 Typ := Base_Type (Typ);
10310 Set_Etype (N2, Typ);
10311 end if;
10312 end if;
10314 if No (N2)
10315 or else No (Typ)
10316 or else not Is_Global (Typ)
10317 then
10318 Set_Associated_Node (N, Empty);
10320 -- If the aggregate is an actual in a call, it has been
10321 -- resolved in the current context, to some local type.
10322 -- The enclosing call may have been disambiguated by
10323 -- the aggregate, and this disambiguation might fail at
10324 -- instantiation time because the type to which the
10325 -- aggregate did resolve is not preserved. In order to
10326 -- preserve some of this information, we wrap the
10327 -- aggregate in a qualified expression, using the id of
10328 -- its type. For further disambiguation we qualify the
10329 -- type name with its scope (if visible) because both
10330 -- id's will have corresponding entities in an instance.
10331 -- This resolves most of the problems with missing type
10332 -- information on aggregates in instances.
10334 if Nkind (N2) = Nkind (N)
10335 and then
10336 (Nkind (Parent (N2)) = N_Procedure_Call_Statement
10337 or else Nkind (Parent (N2)) = N_Function_Call)
10338 and then Comes_From_Source (Typ)
10339 then
10340 if Is_Immediately_Visible (Scope (Typ)) then
10341 Nam := Make_Selected_Component (Loc,
10342 Prefix =>
10343 Make_Identifier (Loc, Chars (Scope (Typ))),
10344 Selector_Name =>
10345 Make_Identifier (Loc, Chars (Typ)));
10346 else
10347 Nam := Make_Identifier (Loc, Chars (Typ));
10348 end if;
10350 Qual :=
10351 Make_Qualified_Expression (Loc,
10352 Subtype_Mark => Nam,
10353 Expression => Relocate_Node (N));
10354 end if;
10355 end if;
10357 Save_Global_Descendant (Field1 (N));
10358 Save_Global_Descendant (Field2 (N));
10359 Save_Global_Descendant (Field3 (N));
10360 Save_Global_Descendant (Field5 (N));
10362 if Present (Qual) then
10363 Rewrite (N, Qual);
10364 end if;
10366 -- All other cases than aggregates
10368 else
10369 Save_Global_Descendant (Field1 (N));
10370 Save_Global_Descendant (Field2 (N));
10371 Save_Global_Descendant (Field3 (N));
10372 Save_Global_Descendant (Field4 (N));
10373 Save_Global_Descendant (Field5 (N));
10374 end if;
10375 end;
10376 end if;
10377 end Save_References;
10379 -- Start of processing for Save_Global_References
10381 begin
10382 Gen_Scope := Current_Scope;
10384 -- If the generic unit is a child unit, references to entities in
10385 -- the parent are treated as local, because they will be resolved
10386 -- anew in the context of the instance of the parent.
10388 while Is_Child_Unit (Gen_Scope)
10389 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
10390 loop
10391 Gen_Scope := Scope (Gen_Scope);
10392 end loop;
10394 Save_References (N);
10395 end Save_Global_References;
10397 --------------------------------------
10398 -- Set_Copied_Sloc_For_Inlined_Body --
10399 --------------------------------------
10401 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
10402 begin
10403 Create_Instantiation_Source (N, E, True, S_Adjustment);
10404 end Set_Copied_Sloc_For_Inlined_Body;
10406 ---------------------
10407 -- Set_Instance_Of --
10408 ---------------------
10410 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
10411 begin
10412 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
10413 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
10414 Generic_Renamings.Increment_Last;
10415 end Set_Instance_Of;
10417 --------------------
10418 -- Set_Next_Assoc --
10419 --------------------
10421 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
10422 begin
10423 Generic_Renamings.Table (E).Next_In_HTable := Next;
10424 end Set_Next_Assoc;
10426 -------------------
10427 -- Start_Generic --
10428 -------------------
10430 procedure Start_Generic is
10431 begin
10432 -- ??? I am sure more things could be factored out in this
10433 -- routine. Should probably be done at a later stage.
10435 Generic_Flags.Increment_Last;
10436 Generic_Flags.Table (Generic_Flags.Last) := Inside_A_Generic;
10437 Inside_A_Generic := True;
10439 Expander_Mode_Save_And_Set (False);
10440 end Start_Generic;
10442 ----------------------
10443 -- Set_Instance_Env --
10444 ----------------------
10446 procedure Set_Instance_Env
10447 (Gen_Unit : Entity_Id;
10448 Act_Unit : Entity_Id)
10450 begin
10451 -- Regardless of the current mode, predefined units are analyzed in
10452 -- the most current Ada mode, and earlier version Ada checks do not
10453 -- apply to predefined units.
10455 -- Why is this not using the routine Opt.Set_Opt_Config_Switches ???
10457 if Is_Internal_File_Name
10458 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
10459 Renamings_Included => True) then
10460 Ada_Version := Ada_Version_Type'Last;
10461 end if;
10463 Current_Instantiated_Parent := (Gen_Unit, Act_Unit, Assoc_Null);
10464 end Set_Instance_Env;
10466 -----------------
10467 -- Switch_View --
10468 -----------------
10470 procedure Switch_View (T : Entity_Id) is
10471 BT : constant Entity_Id := Base_Type (T);
10472 Priv_Elmt : Elmt_Id := No_Elmt;
10473 Priv_Sub : Entity_Id;
10475 begin
10476 -- T may be private but its base type may have been exchanged through
10477 -- some other occurrence, in which case there is nothing to switch.
10479 if not Is_Private_Type (BT) then
10480 return;
10481 end if;
10483 Priv_Elmt := First_Elmt (Private_Dependents (BT));
10485 if Present (Full_View (BT)) then
10486 Prepend_Elmt (Full_View (BT), Exchanged_Views);
10487 Exchange_Declarations (BT);
10488 end if;
10490 while Present (Priv_Elmt) loop
10491 Priv_Sub := (Node (Priv_Elmt));
10493 -- We avoid flipping the subtype if the Etype of its full
10494 -- view is private because this would result in a malformed
10495 -- subtype. This occurs when the Etype of the subtype full
10496 -- view is the full view of the base type (and since the
10497 -- base types were just switched, the subtype is pointing
10498 -- to the wrong view). This is currently the case for
10499 -- tagged record types, access types (maybe more?) and
10500 -- needs to be resolved. ???
10502 if Present (Full_View (Priv_Sub))
10503 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
10504 then
10505 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
10506 Exchange_Declarations (Priv_Sub);
10507 end if;
10509 Next_Elmt (Priv_Elmt);
10510 end loop;
10511 end Switch_View;
10513 -----------------------------
10514 -- Valid_Default_Attribute --
10515 -----------------------------
10517 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
10518 Attr_Id : constant Attribute_Id :=
10519 Get_Attribute_Id (Attribute_Name (Def));
10520 T : constant Entity_Id := Entity (Prefix (Def));
10521 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
10522 F : Entity_Id;
10523 Num_F : Int;
10524 OK : Boolean;
10526 begin
10527 if No (T)
10528 or else T = Any_Id
10529 then
10530 return;
10531 end if;
10533 Num_F := 0;
10534 F := First_Formal (Nam);
10535 while Present (F) loop
10536 Num_F := Num_F + 1;
10537 Next_Formal (F);
10538 end loop;
10540 case Attr_Id is
10541 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
10542 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
10543 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
10544 Attribute_Unbiased_Rounding =>
10545 OK := Is_Fun
10546 and then Num_F = 1
10547 and then Is_Floating_Point_Type (T);
10549 when Attribute_Image | Attribute_Pred | Attribute_Succ |
10550 Attribute_Value | Attribute_Wide_Image |
10551 Attribute_Wide_Value =>
10552 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
10554 when Attribute_Max | Attribute_Min =>
10555 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
10557 when Attribute_Input =>
10558 OK := (Is_Fun and then Num_F = 1);
10560 when Attribute_Output | Attribute_Read | Attribute_Write =>
10561 OK := (not Is_Fun and then Num_F = 2);
10563 when others =>
10564 OK := False;
10565 end case;
10567 if not OK then
10568 Error_Msg_N ("attribute reference has wrong profile for subprogram",
10569 Def);
10570 end if;
10571 end Valid_Default_Attribute;
10573 end Sem_Ch12;