* config/arm/arm.md (addsi3_cbranch_scratch): Correct constraints.
[official-gcc.git] / gcc / ada / sem_ch12.adb
blob59e3bec5dfa56f168619fbb7621e85b2b4b5cd2b
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-2004, 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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 Inline; use Inline;
37 with Lib; use Lib;
38 with Lib.Load; use Lib.Load;
39 with Lib.Xref; use Lib.Xref;
40 with Nlists; use Nlists;
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_Elab; use Sem_Elab;
55 with Sem_Elim; use Sem_Elim;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Res; use Sem_Res;
58 with Sem_Type; use Sem_Type;
59 with Sem_Util; use Sem_Util;
60 with Sem_Warn; use Sem_Warn;
61 with Stand; use Stand;
62 with Sinfo; use Sinfo;
63 with Sinfo.CN; use Sinfo.CN;
64 with Sinput; use Sinput;
65 with Sinput.L; use Sinput.L;
66 with Snames; use Snames;
67 with Stringt; use Stringt;
68 with Uname; use Uname;
69 with Table;
70 with Tbuild; use Tbuild;
71 with Uintp; use Uintp;
72 with Urealp; use Urealp;
74 with GNAT.HTable;
76 package body Sem_Ch12 is
78 ----------------------------------------------------------
79 -- Implementation of Generic Analysis and Instantiation --
80 -----------------------------------------------------------
82 -- GNAT implements generics by macro expansion. No attempt is made to
83 -- share generic instantiations (for now). Analysis of a generic definition
84 -- does not perform any expansion action, but the expander must be called
85 -- on the tree for each instantiation, because the expansion may of course
86 -- depend on the generic actuals. All of this is best achieved as follows:
88 -- a) Semantic analysis of a generic unit is performed on a copy of the
89 -- tree for the generic unit. All tree modifications that follow analysis
90 -- do not affect the original tree. Links are kept between the original
91 -- tree and the copy, in order to recognize non-local references within
92 -- the generic, and propagate them to each instance (recall that name
93 -- resolution is done on the generic declaration: generics are not really
94 -- macros!). This is summarized in the following diagram:
96 -- .-----------. .----------.
97 -- | semantic |<--------------| generic |
98 -- | copy | | unit |
99 -- | |==============>| |
100 -- |___________| global |__________|
101 -- references | | |
102 -- | | |
103 -- .-----|--|.
104 -- | .-----|---.
105 -- | | .----------.
106 -- | | | generic |
107 -- |__| | |
108 -- |__| instance |
109 -- |__________|
111 -- b) Each instantiation copies the original tree, and inserts into it a
112 -- series of declarations that describe the mapping between generic formals
113 -- and actuals. For example, a generic In OUT parameter is an object
114 -- renaming of the corresponing actual, etc. Generic IN parameters are
115 -- constant declarations.
117 -- c) In order to give the right visibility for these renamings, we use
118 -- a different scheme for package and subprogram instantiations. For
119 -- packages, the list of renamings is inserted into the package
120 -- specification, before the visible declarations of the package. The
121 -- renamings are analyzed before any of the text of the instance, and are
122 -- thus visible at the right place. Furthermore, outside of the instance,
123 -- the generic parameters are visible and denote their corresponding
124 -- actuals.
126 -- For subprograms, we create a container package to hold the renamings
127 -- and the subprogram instance itself. Analysis of the package makes the
128 -- renaming declarations visible to the subprogram. After analyzing the
129 -- package, the defining entity for the subprogram is touched-up so that
130 -- it appears declared in the current scope, and not inside the container
131 -- package.
133 -- If the instantiation is a compilation unit, the container package is
134 -- given the same name as the subprogram instance. This ensures that
135 -- the elaboration procedure called by the binder, using the compilation
136 -- unit name, calls in fact the elaboration procedure for the package.
138 -- Not surprisingly, private types complicate this approach. By saving in
139 -- the original generic object the non-local references, we guarantee that
140 -- the proper entities are referenced at the point of instantiation.
141 -- However, for private types, this by itself does not insure that the
142 -- proper VIEW of the entity is used (the full type may be visible at the
143 -- point of generic definition, but not at instantiation, or vice-versa).
144 -- In order to reference the proper view, we special-case any reference
145 -- to private types in the generic object, by saving both views, one in
146 -- the generic and one in the semantic copy. At time of instantiation, we
147 -- check whether the two views are consistent, and exchange declarations if
148 -- necessary, in order to restore the correct visibility. Similarly, if
149 -- the instance view is private when the generic view was not, we perform
150 -- the exchange. After completing the instantiation, we restore the
151 -- current visibility. The flag Has_Private_View marks identifiers in the
152 -- the generic unit that require checking.
154 -- Visibility within nested generic units requires special handling.
155 -- Consider the following scheme:
157 -- type Global is ... -- outside of generic unit.
158 -- generic ...
159 -- package Outer is
160 -- ...
161 -- type Semi_Global is ... -- global to inner.
163 -- generic ... -- 1
164 -- procedure inner (X1 : Global; X2 : Semi_Global);
166 -- procedure in2 is new inner (...); -- 4
167 -- end Outer;
169 -- package New_Outer is new Outer (...); -- 2
170 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
172 -- The semantic analysis of Outer captures all occurrences of Global.
173 -- The semantic analysis of Inner (at 1) captures both occurrences of
174 -- Global and Semi_Global.
176 -- At point 2 (instantiation of Outer), we also produce a generic copy
177 -- of Inner, even though Inner is, at that point, not being instantiated.
178 -- (This is just part of the semantic analysis of New_Outer).
180 -- Critically, references to Global within Inner must be preserved, while
181 -- references to Semi_Global should not preserved, because they must now
182 -- resolve to an entity within New_Outer. To distinguish between these, we
183 -- use a global variable, Current_Instantiated_Parent, which is set when
184 -- performing a generic copy during instantiation (at 2). This variable is
185 -- used when performing a generic copy that is not an instantiation, but
186 -- that is nested within one, as the occurrence of 1 within 2. The analysis
187 -- of a nested generic only preserves references that are global to the
188 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
189 -- determine whether a reference is external to the given parent.
191 -- The instantiation at point 3 requires no special treatment. The method
192 -- works as well for further nestings of generic units, but of course the
193 -- variable Current_Instantiated_Parent must be stacked because nested
194 -- instantiations can occur, e.g. the occurrence of 4 within 2.
196 -- The instantiation of package and subprogram bodies is handled in a
197 -- similar manner, except that it is delayed until after semantic
198 -- analysis is complete. In this fashion complex cross-dependencies
199 -- between several package declarations and bodies containing generics
200 -- can be compiled which otherwise would diagnose spurious circularities.
202 -- For example, it is possible to compile two packages A and B that
203 -- have the following structure:
205 -- package A is package B is
206 -- generic ... generic ...
207 -- package G_A is package G_B is
209 -- with B; with A;
210 -- package body A is package body B is
211 -- package N_B is new G_B (..) package N_A is new G_A (..)
213 -- The table Pending_Instantiations in package Inline is used to keep
214 -- track of body instantiations that are delayed in this manner. Inline
215 -- handles the actual calls to do the body instantiations. This activity
216 -- is part of Inline, since the processing occurs at the same point, and
217 -- for essentially the same reason, as the handling of inlined routines.
219 ----------------------------------------------
220 -- Detection of Instantiation Circularities --
221 ----------------------------------------------
223 -- If we have a chain of instantiations that is circular, this is a
224 -- static error which must be detected at compile time. The detection
225 -- of these circularities is carried out at the point that we insert
226 -- a generic instance spec or body. If there is a circularity, then
227 -- the analysis of the offending spec or body will eventually result
228 -- in trying to load the same unit again, and we detect this problem
229 -- as we analyze the package instantiation for the second time.
231 -- At least in some cases after we have detected the circularity, we
232 -- get into trouble if we try to keep going. The following flag is
233 -- set if a circularity is detected, and used to abandon compilation
234 -- after the messages have been posted.
236 Circularity_Detected : Boolean := False;
237 -- This should really be reset on encountering a new main unit, but in
238 -- practice we are not using multiple main units so it is not critical.
240 -----------------------
241 -- Local subprograms --
242 -----------------------
244 procedure Abandon_Instantiation (N : Node_Id);
245 pragma No_Return (Abandon_Instantiation);
246 -- Posts an error message "instantiation abandoned" at the indicated
247 -- node and then raises the exception Instantiation_Error to do it.
249 procedure Analyze_Formal_Array_Type
250 (T : in out Entity_Id;
251 Def : Node_Id);
252 -- A formal array type is treated like an array type declaration, and
253 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
254 -- in-out, because in the case of an anonymous type the entity is
255 -- actually created in the procedure.
257 -- The following procedures treat other kinds of formal parameters.
259 procedure Analyze_Formal_Derived_Type
260 (N : Node_Id;
261 T : Entity_Id;
262 Def : Node_Id);
264 -- All the following need comments???
266 procedure Analyze_Formal_Decimal_Fixed_Point_Type
267 (T : Entity_Id; Def : Node_Id);
268 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
269 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
270 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
271 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
272 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
273 (T : Entity_Id; Def : Node_Id);
275 procedure Analyze_Formal_Private_Type
276 (N : Node_Id;
277 T : Entity_Id;
278 Def : Node_Id);
279 -- This needs comments???
281 procedure Analyze_Generic_Formal_Part (N : Node_Id);
283 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
284 -- This needs comments ???
286 function Analyze_Associations
287 (I_Node : Node_Id;
288 Formals : List_Id;
289 F_Copy : List_Id) return List_Id;
290 -- At instantiation time, build the list of associations between formals
291 -- and actuals. Each association becomes a renaming declaration for the
292 -- formal entity. F_Copy is the analyzed list of formals in the generic
293 -- copy. It is used to apply legality checks to the actuals. I_Node is the
294 -- instantiation node itself.
296 procedure Analyze_Subprogram_Instantiation
297 (N : Node_Id;
298 K : Entity_Kind);
300 procedure Build_Instance_Compilation_Unit_Nodes
301 (N : Node_Id;
302 Act_Body : Node_Id;
303 Act_Decl : Node_Id);
304 -- This procedure is used in the case where the generic instance of a
305 -- subprogram body or package body is a library unit. In this case, the
306 -- original library unit node for the generic instantiation must be
307 -- replaced by the resulting generic body, and a link made to a new
308 -- compilation unit node for the generic declaration. The argument N is
309 -- the original generic instantiation. Act_Body and Act_Decl are the body
310 -- and declaration of the instance (either package body and declaration
311 -- nodes or subprogram body and declaration nodes depending on the case).
312 -- On return, the node N has been rewritten with the actual body.
314 procedure Check_Formal_Packages (P_Id : Entity_Id);
315 -- Apply the following to all formal packages in generic associations.
317 procedure Check_Formal_Package_Instance
318 (Formal_Pack : Entity_Id;
319 Actual_Pack : Entity_Id);
320 -- Verify that the actuals of the actual instance match the actuals of
321 -- the template for a formal package that is not declared with a box.
323 procedure Check_Forward_Instantiation (Decl : Node_Id);
324 -- If the generic is a local entity and the corresponding body has not
325 -- been seen yet, flag enclosing packages to indicate that it will be
326 -- elaborated after the generic body. Subprograms declared in the same
327 -- package cannot be inlined by the front-end because front-end inlining
328 -- requires a strict linear order of elaboration.
330 procedure Check_Hidden_Child_Unit
331 (N : Node_Id;
332 Gen_Unit : Entity_Id;
333 Act_Decl_Id : Entity_Id);
334 -- If the generic unit is an implicit child instance within a parent
335 -- instance, we need to make an explicit test that it is not hidden by
336 -- a child instance of the same name and parent.
338 procedure Check_Private_View (N : Node_Id);
339 -- Check whether the type of a generic entity has a different view between
340 -- the point of generic analysis and the point of instantiation. If the
341 -- view has changed, then at the point of instantiation we restore the
342 -- correct view to perform semantic analysis of the instance, and reset
343 -- the current view after instantiation. The processing is driven by the
344 -- current private status of the type of the node, and Has_Private_View,
345 -- a flag that is set at the point of generic compilation. If view and
346 -- flag are inconsistent then the type is updated appropriately.
348 procedure Check_Generic_Actuals
349 (Instance : Entity_Id;
350 Is_Formal_Box : Boolean);
351 -- Similar to previous one. Check the actuals in the instantiation,
352 -- whose views can change between the point of instantiation and the point
353 -- of instantiation of the body. In addition, mark the generic renamings
354 -- as generic actuals, so that they are not compatible with other actuals.
355 -- Recurse on an actual that is a formal package whose declaration has
356 -- a box.
358 function Contains_Instance_Of
359 (Inner : Entity_Id;
360 Outer : Entity_Id;
361 N : Node_Id) return Boolean;
362 -- Inner is instantiated within the generic Outer. Check whether Inner
363 -- directly or indirectly contains an instance of Outer or of one of its
364 -- parents, in the case of a subunit. Each generic unit holds a list of
365 -- the entities instantiated within (at any depth). This procedure
366 -- determines whether the set of such lists contains a cycle, i.e. an
367 -- illegal circular instantiation.
369 function Denotes_Formal_Package
370 (Pack : Entity_Id;
371 On_Exit : Boolean := False) return Boolean;
372 -- Returns True if E is a formal package of an enclosing generic, or
373 -- the actual for such a formal in an enclosing instantiation. If such
374 -- a package is used as a formal in an nested generic, or as an actual
375 -- in a nested instantiation, the visibility of ITS formals should not
376 -- be modified. When called from within Restore_Private_Views, the flag
377 -- On_Exit is true, to indicate that the search for a possible enclosing
378 -- instance should ignore the current one.
380 function Find_Actual_Type
381 (Typ : Entity_Id;
382 Gen_Scope : Entity_Id) return Entity_Id;
383 -- When validating the actual types of a child instance, check whether
384 -- the formal is a formal type of the parent unit, and retrieve the current
385 -- actual for it. Typ is the entity in the analyzed formal type declaration
386 -- (component or index type of an array type) and Gen_Scope is the scope of
387 -- the analyzed formal array type.
389 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id;
390 -- Given the entity of a unit that is an instantiation, retrieve the
391 -- original instance node. This is used when loading the instantiations
392 -- of the ancestors of a child generic that is being instantiated.
394 function In_Same_Declarative_Part
395 (F_Node : Node_Id;
396 Inst : Node_Id) return Boolean;
397 -- True if the instantiation Inst and the given freeze_node F_Node appear
398 -- within the same declarative part, ignoring subunits, but with no inter-
399 -- vening suprograms or concurrent units. If true, the freeze node
400 -- of the instance can be placed after the freeze node of the parent,
401 -- which it itself an instance.
403 procedure Set_Instance_Env
404 (Gen_Unit : Entity_Id;
405 Act_Unit : Entity_Id);
406 -- Save current instance on saved environment, to be used to determine
407 -- the global status of entities in nested instances. Part of Save_Env.
408 -- called after verifying that the generic unit is legal for the instance.
410 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
411 -- Associate analyzed generic parameter with corresponding
412 -- instance. Used for semantic checks at instantiation time.
414 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
415 -- Traverse the Exchanged_Views list to see if a type was private
416 -- and has already been flipped during this phase of instantiation.
418 procedure Hide_Current_Scope;
419 -- When compiling a generic child unit, the parent context must be
420 -- present, but the instance and all entities that may be generated
421 -- must be inserted in the current scope. We leave the current scope
422 -- on the stack, but make its entities invisible to avoid visibility
423 -- problems. This is reversed at the end of instantiations. This is
424 -- not done for the instantiation of the bodies, which only require the
425 -- instances of the generic parents to be in scope.
427 procedure Install_Body
428 (Act_Body : Node_Id;
429 N : Node_Id;
430 Gen_Body : Node_Id;
431 Gen_Decl : Node_Id);
432 -- If the instantiation happens textually before the body of the generic,
433 -- the instantiation of the body must be analyzed after the generic body,
434 -- and not at the point of instantiation. Such early instantiations can
435 -- happen if the generic and the instance appear in a package declaration
436 -- because the generic body can only appear in the corresponding package
437 -- body. Early instantiations can also appear if generic, instance and
438 -- body are all in the declarative part of a subprogram or entry. Entities
439 -- of packages that are early instantiations are delayed, and their freeze
440 -- node appears after the generic body.
442 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id);
443 -- Insert freeze node at the end of the declarative part that includes the
444 -- instance node N. If N is in the visible part of an enclosing package
445 -- declaration, the freeze node has to be inserted at the end of the
446 -- private declarations, if any.
448 procedure Freeze_Subprogram_Body
449 (Inst_Node : Node_Id;
450 Gen_Body : Node_Id;
451 Pack_Id : Entity_Id);
452 -- The generic body may appear textually after the instance, including
453 -- in the proper body of a stub, or within a different package instance.
454 -- Given that the instance can only be elaborated after the generic, we
455 -- place freeze_nodes for the instance and/or for packages that may enclose
456 -- the instance and the generic, so that the back-end can establish the
457 -- proper order of elaboration.
459 procedure Init_Env;
460 -- Establish environment for subsequent instantiation. Separated from
461 -- Save_Env because data-structures for visibility handling must be
462 -- initialized before call to Check_Generic_Child_Unit.
464 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
465 -- When compiling an instance of a child unit the parent (which is
466 -- itself an instance) is an enclosing scope that must be made
467 -- immediately visible. This procedure is also used to install the non-
468 -- generic parent of a generic child unit when compiling its body, so that
469 -- full views of types in the parent are made visible.
471 procedure Remove_Parent (In_Body : Boolean := False);
472 -- Reverse effect after instantiation of child is complete.
474 procedure Inline_Instance_Body
475 (N : Node_Id;
476 Gen_Unit : Entity_Id;
477 Act_Decl : Node_Id);
478 -- If front-end inlining is requested, instantiate the package body,
479 -- and preserve the visibility of its compilation unit, to insure
480 -- that successive instantiations succeed.
482 -- The functions Instantiate_XXX perform various legality checks and build
483 -- the declarations for instantiated generic parameters.
484 -- Need to describe what the parameters are ???
486 function Instantiate_Object
487 (Formal : Node_Id;
488 Actual : Node_Id;
489 Analyzed_Formal : Node_Id) return List_Id;
491 function Instantiate_Type
492 (Formal : Node_Id;
493 Actual : Node_Id;
494 Analyzed_Formal : Node_Id;
495 Actual_Decls : List_Id) return Node_Id;
497 function Instantiate_Formal_Subprogram
498 (Formal : Node_Id;
499 Actual : Node_Id;
500 Analyzed_Formal : Node_Id) return Node_Id;
502 function Instantiate_Formal_Package
503 (Formal : Node_Id;
504 Actual : Node_Id;
505 Analyzed_Formal : Node_Id) return List_Id;
506 -- If the formal package is declared with a box, special visibility rules
507 -- apply to its formals: they are in the visible part of the package. This
508 -- is true in the declarative region of the formal package, that is to say
509 -- in the enclosing generic or instantiation. For an instantiation, the
510 -- parameters of the formal package are made visible in an explicit step.
511 -- Furthermore, if the actual is a visible use_clause, these formals must
512 -- be made potentially use_visible as well. On exit from the enclosing
513 -- instantiation, the reverse must be done.
515 -- For a formal package declared without a box, there are conformance rules
516 -- that apply to the actuals in the generic declaration and the actuals of
517 -- the actual package in the enclosing instantiation. The simplest way to
518 -- apply these rules is to repeat the instantiation of the formal package
519 -- in the context of the enclosing instance, and compare the generic
520 -- associations of this instantiation with those of the actual package.
522 function Is_In_Main_Unit (N : Node_Id) return Boolean;
523 -- Test if given node is in the main unit
525 procedure Load_Parent_Of_Generic (N : Node_Id; Spec : Node_Id);
526 -- If the generic appears in a separate non-generic library unit,
527 -- load the corresponding body to retrieve the body of the generic.
528 -- N is the node for the generic instantiation, Spec is the generic
529 -- package declaration.
531 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
532 -- Add the context clause of the unit containing a generic unit to
533 -- an instantiation that is a compilation unit.
535 function Get_Associated_Node (N : Node_Id) return Node_Id;
536 -- In order to propagate semantic information back from the analyzed
537 -- copy to the original generic, we maintain links between selected nodes
538 -- in the generic and their corresponding copies. At the end of generic
539 -- analysis, the routine Save_Global_References traverses the generic
540 -- tree, examines the semantic information, and preserves the links to
541 -- those nodes that contain global information. At instantiation, the
542 -- information from the associated node is placed on the new copy, so
543 -- that name resolution is not repeated.
545 -- Three kinds of source nodes have associated nodes:
547 -- a) those that can reference (denote) entities, that is identifiers,
548 -- character literals, expanded_names, operator symbols, operators,
549 -- and attribute reference nodes. These nodes have an Entity field
550 -- and are the set of nodes that are in N_Has_Entity.
552 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
554 -- c) selected components (N_Selected_Component)
556 -- For the first class, the associated node preserves the entity if it is
557 -- global. If the generic contains nested instantiations, the associated
558 -- node itself has been recopied, and a chain of them must be followed.
560 -- For aggregates, the associated node allows retrieval of the type, which
561 -- may otherwise not appear in the generic. The view of this type may be
562 -- different between generic and instantiation, and the full view can be
563 -- installed before the instantiation is analyzed. For aggregates of
564 -- type extensions, the same view exchange may have to be performed for
565 -- some of the ancestor types, if their view is private at the point of
566 -- instantiation.
568 -- Nodes that are selected components in the parse tree may be rewritten
569 -- as expanded names after resolution, and must be treated as potential
570 -- entity holders. which is why they also have an Associated_Node.
572 -- Nodes that do not come from source, such as freeze nodes, do not appear
573 -- in the generic tree, and need not have an associated node.
575 -- The associated node is stored in the Associated_Node field. Note that
576 -- this field overlaps Entity, which is fine, because the whole point is
577 -- that we don't need or want the normal Entity field in this situation.
579 procedure Move_Freeze_Nodes
580 (Out_Of : Entity_Id;
581 After : Node_Id;
582 L : List_Id);
583 -- Freeze nodes can be generated in the analysis of a generic unit, but
584 -- will not be seen by the back-end. It is necessary to move those nodes
585 -- to the enclosing scope if they freeze an outer entity. We place them
586 -- at the end of the enclosing generic package, which is semantically
587 -- neutral.
589 procedure Pre_Analyze_Actuals (N : Node_Id);
590 -- Analyze actuals to perform name resolution. Full resolution is done
591 -- later, when the expected types are known, but names have to be captured
592 -- before installing parents of generics, that are not visible for the
593 -- actuals themselves.
595 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
596 -- Verify that an attribute that appears as the default for a formal
597 -- subprogram is a function or procedure with the correct profile.
599 -------------------------------------------
600 -- Data Structures for Generic Renamings --
601 -------------------------------------------
603 -- The map Generic_Renamings associates generic entities with their
604 -- corresponding actuals. Currently used to validate type instances.
605 -- It will eventually be used for all generic parameters to eliminate
606 -- the need for overload resolution in the instance.
608 type Assoc_Ptr is new Int;
610 Assoc_Null : constant Assoc_Ptr := -1;
612 type Assoc is record
613 Gen_Id : Entity_Id;
614 Act_Id : Entity_Id;
615 Next_In_HTable : Assoc_Ptr;
616 end record;
618 package Generic_Renamings is new Table.Table
619 (Table_Component_Type => Assoc,
620 Table_Index_Type => Assoc_Ptr,
621 Table_Low_Bound => 0,
622 Table_Initial => 10,
623 Table_Increment => 100,
624 Table_Name => "Generic_Renamings");
626 -- Variable to hold enclosing instantiation. When the environment is
627 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
629 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
631 -- Hash table for associations
633 HTable_Size : constant := 37;
634 type HTable_Range is range 0 .. HTable_Size - 1;
636 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
637 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
638 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
639 function Hash (F : Entity_Id) return HTable_Range;
641 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
642 Header_Num => HTable_Range,
643 Element => Assoc,
644 Elmt_Ptr => Assoc_Ptr,
645 Null_Ptr => Assoc_Null,
646 Set_Next => Set_Next_Assoc,
647 Next => Next_Assoc,
648 Key => Entity_Id,
649 Get_Key => Get_Gen_Id,
650 Hash => Hash,
651 Equal => "=");
653 Exchanged_Views : Elist_Id;
654 -- This list holds the private views that have been exchanged during
655 -- instantiation to restore the visibility of the generic declaration.
656 -- (see comments above). After instantiation, the current visibility is
657 -- reestablished by means of a traversal of this list.
659 Hidden_Entities : Elist_Id;
660 -- This list holds the entities of the current scope that are removed
661 -- from immediate visibility when instantiating a child unit. Their
662 -- visibility is restored in Remove_Parent.
664 -- Because instantiations can be recursive, the following must be saved
665 -- on entry and restored on exit from an instantiation (spec or body).
666 -- This is done by the two procedures Save_Env and Restore_Env. For
667 -- package and subprogram instantiations (but not for the body instances)
668 -- the action of Save_Env is done in two steps: Init_Env is called before
669 -- Check_Generic_Child_Unit, because setting the parent instances requires
670 -- that the visibility data structures be properly initialized. Once the
671 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
673 type Instance_Env is record
674 Ada_Version : Ada_Version_Type;
675 Instantiated_Parent : Assoc;
676 Exchanged_Views : Elist_Id;
677 Hidden_Entities : Elist_Id;
678 Current_Sem_Unit : Unit_Number_Type;
679 end record;
681 package Instance_Envs is new Table.Table (
682 Table_Component_Type => Instance_Env,
683 Table_Index_Type => Int,
684 Table_Low_Bound => 0,
685 Table_Initial => 32,
686 Table_Increment => 100,
687 Table_Name => "Instance_Envs");
689 procedure Restore_Private_Views
690 (Pack_Id : Entity_Id;
691 Is_Package : Boolean := True);
692 -- Restore the private views of external types, and unmark the generic
693 -- renamings of actuals, so that they become comptible subtypes again.
694 -- For subprograms, Pack_Id is the package constructed to hold the
695 -- renamings.
697 procedure Switch_View (T : Entity_Id);
698 -- Switch the partial and full views of a type and its private
699 -- dependents (i.e. its subtypes and derived types).
701 ------------------------------------
702 -- Structures for Error Reporting --
703 ------------------------------------
705 Instantiation_Node : Node_Id;
706 -- Used by subprograms that validate instantiation of formal parameters
707 -- where there might be no actual on which to place the error message.
708 -- Also used to locate the instantiation node for generic subunits.
710 Instantiation_Error : exception;
711 -- When there is a semantic error in the generic parameter matching,
712 -- there is no point in continuing the instantiation, because the
713 -- number of cascaded errors is unpredictable. This exception aborts
714 -- the instantiation process altogether.
716 S_Adjustment : Sloc_Adjustment;
717 -- Offset created for each node in an instantiation, in order to keep
718 -- track of the source position of the instantiation in each of its nodes.
719 -- A subsequent semantic error or warning on a construct of the instance
720 -- points to both places: the original generic node, and the point of
721 -- instantiation. See Sinput and Sinput.L for additional details.
723 ------------------------------------------------------------
724 -- Data structure for keeping track when inside a Generic --
725 ------------------------------------------------------------
727 -- The following table is used to save values of the Inside_A_Generic
728 -- flag (see spec of Sem) when they are saved by Start_Generic.
730 package Generic_Flags is new Table.Table (
731 Table_Component_Type => Boolean,
732 Table_Index_Type => Int,
733 Table_Low_Bound => 0,
734 Table_Initial => 32,
735 Table_Increment => 200,
736 Table_Name => "Generic_Flags");
738 ---------------------------
739 -- Abandon_Instantiation --
740 ---------------------------
742 procedure Abandon_Instantiation (N : Node_Id) is
743 begin
744 Error_Msg_N ("instantiation abandoned!", N);
745 raise Instantiation_Error;
746 end Abandon_Instantiation;
748 --------------------------
749 -- Analyze_Associations --
750 --------------------------
752 function Analyze_Associations
753 (I_Node : Node_Id;
754 Formals : List_Id;
755 F_Copy : List_Id) return List_Id
757 Actual_Types : constant Elist_Id := New_Elmt_List;
758 Assoc : constant List_Id := New_List;
759 Defaults : constant Elist_Id := New_Elmt_List;
760 Gen_Unit : constant Entity_Id := Defining_Entity (Parent (F_Copy));
761 Actuals : List_Id;
762 Actual : Node_Id;
763 Formal : Node_Id;
764 Next_Formal : Node_Id;
765 Temp_Formal : Node_Id;
766 Analyzed_Formal : Node_Id;
767 Match : Node_Id;
768 Named : Node_Id;
769 First_Named : Node_Id := Empty;
770 Found_Assoc : Node_Id;
771 Is_Named_Assoc : Boolean;
772 Num_Matched : Int := 0;
773 Num_Actuals : Int := 0;
775 function Matching_Actual
776 (F : Entity_Id;
777 A_F : Entity_Id) return Node_Id;
778 -- Find actual that corresponds to a given a formal parameter. If the
779 -- actuals are positional, return the next one, if any. If the actuals
780 -- are named, scan the parameter associations to find the right one.
781 -- A_F is the corresponding entity in the analyzed generic,which is
782 -- placed on the selector name for ASIS use.
784 procedure Set_Analyzed_Formal;
785 -- Find the node in the generic copy that corresponds to a given formal.
786 -- The semantic information on this node is used to perform legality
787 -- checks on the actuals. Because semantic analysis can introduce some
788 -- anonymous entities or modify the declaration node itself, the
789 -- correspondence between the two lists is not one-one. In addition to
790 -- anonymous types, the presence a formal equality will introduce an
791 -- implicit declaration for the corresponding inequality.
793 ---------------------
794 -- Matching_Actual --
795 ---------------------
797 function Matching_Actual
798 (F : Entity_Id;
799 A_F : Entity_Id) return Node_Id
801 Found : Node_Id;
802 Prev : Node_Id;
804 begin
805 Is_Named_Assoc := False;
807 -- End of list of purely positional parameters
809 if No (Actual) then
810 Found := Empty;
812 -- Case of positional parameter corresponding to current formal
814 elsif No (Selector_Name (Actual)) then
815 Found := Explicit_Generic_Actual_Parameter (Actual);
816 Found_Assoc := Actual;
817 Num_Matched := Num_Matched + 1;
818 Next (Actual);
820 -- Otherwise scan list of named actuals to find the one with the
821 -- desired name. All remaining actuals have explicit names.
823 else
824 Is_Named_Assoc := True;
825 Found := Empty;
826 Prev := Empty;
828 while Present (Actual) loop
829 if Chars (Selector_Name (Actual)) = Chars (F) then
830 Found := Explicit_Generic_Actual_Parameter (Actual);
831 Set_Entity (Selector_Name (Actual), A_F);
832 Set_Etype (Selector_Name (Actual), Etype (A_F));
833 Generate_Reference (A_F, Selector_Name (Actual));
834 Found_Assoc := Actual;
835 Num_Matched := Num_Matched + 1;
836 exit;
837 end if;
839 Prev := Actual;
840 Next (Actual);
841 end loop;
843 -- Reset for subsequent searches. In most cases the named
844 -- associations are in order. If they are not, we reorder them
845 -- to avoid scanning twice the same actual. This is not just a
846 -- question of efficiency: there may be multiple defaults with
847 -- boxes that have the same name. In a nested instantiation we
848 -- insert actuals for those defaults, and cannot rely on their
849 -- names to disambiguate them.
851 if Actual = First_Named then
852 Next (First_Named);
854 elsif Present (Actual) then
855 Insert_Before (First_Named, Remove_Next (Prev));
856 end if;
858 Actual := First_Named;
859 end if;
861 return Found;
862 end Matching_Actual;
864 -------------------------
865 -- Set_Analyzed_Formal --
866 -------------------------
868 procedure Set_Analyzed_Formal is
869 Kind : Node_Kind;
870 begin
871 while Present (Analyzed_Formal) loop
872 Kind := Nkind (Analyzed_Formal);
874 case Nkind (Formal) is
876 when N_Formal_Subprogram_Declaration =>
877 exit when Kind = N_Formal_Subprogram_Declaration
878 and then
879 Chars
880 (Defining_Unit_Name (Specification (Formal))) =
881 Chars
882 (Defining_Unit_Name (Specification (Analyzed_Formal)));
884 when N_Formal_Package_Declaration =>
885 exit when
886 Kind = N_Formal_Package_Declaration
887 or else
888 Kind = N_Generic_Package_Declaration;
890 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
892 when others =>
894 -- Skip freeze nodes, and nodes inserted to replace
895 -- unrecognized pragmas.
897 exit when
898 Kind /= N_Formal_Subprogram_Declaration
899 and then Kind /= N_Subprogram_Declaration
900 and then Kind /= N_Freeze_Entity
901 and then Kind /= N_Null_Statement
902 and then Kind /= N_Itype_Reference
903 and then Chars (Defining_Identifier (Formal)) =
904 Chars (Defining_Identifier (Analyzed_Formal));
905 end case;
907 Next (Analyzed_Formal);
908 end loop;
910 end Set_Analyzed_Formal;
912 -- Start of processing for Analyze_Associations
914 begin
915 -- If named associations are present, save the first named association
916 -- (it may of course be Empty) to facilitate subsequent name search.
918 Actuals := Generic_Associations (I_Node);
920 if Present (Actuals) then
921 First_Named := First (Actuals);
923 while Present (First_Named)
924 and then No (Selector_Name (First_Named))
925 loop
926 Num_Actuals := Num_Actuals + 1;
927 Next (First_Named);
928 end loop;
929 end if;
931 Named := First_Named;
932 while Present (Named) loop
933 if No (Selector_Name (Named)) then
934 Error_Msg_N ("invalid positional actual after named one", Named);
935 Abandon_Instantiation (Named);
936 end if;
938 -- A named association may lack an actual parameter, if it was
939 -- introduced for a default subprogram that turns out to be local
940 -- to the outer instantiation.
942 if Present (Explicit_Generic_Actual_Parameter (Named)) then
943 Num_Actuals := Num_Actuals + 1;
944 end if;
946 Next (Named);
947 end loop;
949 if Present (Formals) then
950 Formal := First_Non_Pragma (Formals);
951 Analyzed_Formal := First_Non_Pragma (F_Copy);
953 if Present (Actuals) then
954 Actual := First (Actuals);
956 -- All formals should have default values
958 else
959 Actual := Empty;
960 end if;
962 while Present (Formal) loop
963 Set_Analyzed_Formal;
964 Next_Formal := Next_Non_Pragma (Formal);
966 case Nkind (Formal) is
967 when N_Formal_Object_Declaration =>
968 Match :=
969 Matching_Actual (
970 Defining_Identifier (Formal),
971 Defining_Identifier (Analyzed_Formal));
973 Append_List
974 (Instantiate_Object (Formal, Match, Analyzed_Formal),
975 Assoc);
977 when N_Formal_Type_Declaration =>
978 Match :=
979 Matching_Actual (
980 Defining_Identifier (Formal),
981 Defining_Identifier (Analyzed_Formal));
983 if No (Match) then
984 Error_Msg_Sloc := Sloc (Gen_Unit);
985 Error_Msg_NE
986 ("missing actual&",
987 Instantiation_Node, Defining_Identifier (Formal));
988 Error_Msg_NE ("\in instantiation of & declared#",
989 Instantiation_Node, Gen_Unit);
990 Abandon_Instantiation (Instantiation_Node);
992 else
993 Analyze (Match);
994 Append_To (Assoc,
995 Instantiate_Type
996 (Formal, Match, Analyzed_Formal, Assoc));
998 -- an instantiation is a freeze point for the actuals,
999 -- unless this is a rewritten formal package.
1001 if Nkind (I_Node) /= N_Formal_Package_Declaration then
1002 Append_Elmt (Entity (Match), Actual_Types);
1003 end if;
1004 end if;
1006 -- A remote access-to-class-wide type must not be an
1007 -- actual parameter for a generic formal of an access
1008 -- type (E.2.2 (17)).
1010 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1011 and then
1012 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1013 N_Access_To_Object_Definition
1014 then
1015 Validate_Remote_Access_To_Class_Wide_Type (Match);
1016 end if;
1018 when N_Formal_Subprogram_Declaration =>
1019 Match :=
1020 Matching_Actual (
1021 Defining_Unit_Name (Specification (Formal)),
1022 Defining_Unit_Name (Specification (Analyzed_Formal)));
1024 -- If the formal subprogram has the same name as
1025 -- another formal subprogram of the generic, then
1026 -- a named association is illegal (12.3(9)). Exclude
1027 -- named associations that are generated for a nested
1028 -- instance.
1030 if Present (Match)
1031 and then Is_Named_Assoc
1032 and then Comes_From_Source (Found_Assoc)
1033 then
1034 Temp_Formal := First (Formals);
1035 while Present (Temp_Formal) loop
1036 if Nkind (Temp_Formal) =
1037 N_Formal_Subprogram_Declaration
1038 and then Temp_Formal /= Formal
1039 and then
1040 Chars (Selector_Name (Found_Assoc)) =
1041 Chars (Defining_Unit_Name
1042 (Specification (Temp_Formal)))
1043 then
1044 Error_Msg_N
1045 ("name not allowed for overloaded formal",
1046 Found_Assoc);
1047 Abandon_Instantiation (Instantiation_Node);
1048 end if;
1050 Next (Temp_Formal);
1051 end loop;
1052 end if;
1054 Append_To (Assoc,
1055 Instantiate_Formal_Subprogram
1056 (Formal, Match, Analyzed_Formal));
1058 if No (Match)
1059 and then Box_Present (Formal)
1060 then
1061 Append_Elmt
1062 (Defining_Unit_Name (Specification (Last (Assoc))),
1063 Defaults);
1064 end if;
1066 when N_Formal_Package_Declaration =>
1067 Match :=
1068 Matching_Actual (
1069 Defining_Identifier (Formal),
1070 Defining_Identifier (Original_Node (Analyzed_Formal)));
1072 if No (Match) then
1073 Error_Msg_Sloc := Sloc (Gen_Unit);
1074 Error_Msg_NE
1075 ("missing actual&",
1076 Instantiation_Node, Defining_Identifier (Formal));
1077 Error_Msg_NE ("\in instantiation of & declared#",
1078 Instantiation_Node, Gen_Unit);
1080 Abandon_Instantiation (Instantiation_Node);
1082 else
1083 Analyze (Match);
1084 Append_List
1085 (Instantiate_Formal_Package
1086 (Formal, Match, Analyzed_Formal),
1087 Assoc);
1088 end if;
1090 -- For use type and use package appearing in the context
1091 -- clause, we have already copied them, so we can just
1092 -- move them where they belong (we mustn't recopy them
1093 -- since this would mess up the Sloc values).
1095 when N_Use_Package_Clause |
1096 N_Use_Type_Clause =>
1097 Remove (Formal);
1098 Append (Formal, Assoc);
1100 when others =>
1101 raise Program_Error;
1103 end case;
1105 Formal := Next_Formal;
1106 Next_Non_Pragma (Analyzed_Formal);
1107 end loop;
1109 if Num_Actuals > Num_Matched then
1110 Error_Msg_Sloc := Sloc (Gen_Unit);
1112 if Present (Selector_Name (Actual)) then
1113 Error_Msg_NE
1114 ("unmatched actual&",
1115 Actual, Selector_Name (Actual));
1116 Error_Msg_NE ("\in instantiation of& declared#",
1117 Actual, Gen_Unit);
1118 else
1119 Error_Msg_NE
1120 ("unmatched actual in instantiation of& declared#",
1121 Actual, Gen_Unit);
1122 end if;
1123 end if;
1125 elsif Present (Actuals) then
1126 Error_Msg_N
1127 ("too many actuals in generic instantiation", Instantiation_Node);
1128 end if;
1130 declare
1131 Elmt : Elmt_Id := First_Elmt (Actual_Types);
1133 begin
1134 while Present (Elmt) loop
1135 Freeze_Before (I_Node, Node (Elmt));
1136 Next_Elmt (Elmt);
1137 end loop;
1138 end;
1140 -- If there are default subprograms, normalize the tree by adding
1141 -- explicit associations for them. This is required if the instance
1142 -- appears within a generic.
1144 declare
1145 Elmt : Elmt_Id;
1146 Subp : Entity_Id;
1147 New_D : Node_Id;
1149 begin
1150 Elmt := First_Elmt (Defaults);
1151 while Present (Elmt) loop
1152 if No (Actuals) then
1153 Actuals := New_List;
1154 Set_Generic_Associations (I_Node, Actuals);
1155 end if;
1157 Subp := Node (Elmt);
1158 New_D :=
1159 Make_Generic_Association (Sloc (Subp),
1160 Selector_Name => New_Occurrence_Of (Subp, Sloc (Subp)),
1161 Explicit_Generic_Actual_Parameter =>
1162 New_Occurrence_Of (Subp, Sloc (Subp)));
1163 Mark_Rewrite_Insertion (New_D);
1164 Append_To (Actuals, New_D);
1165 Next_Elmt (Elmt);
1166 end loop;
1167 end;
1169 return Assoc;
1170 end Analyze_Associations;
1172 -------------------------------
1173 -- Analyze_Formal_Array_Type --
1174 -------------------------------
1176 procedure Analyze_Formal_Array_Type
1177 (T : in out Entity_Id;
1178 Def : Node_Id)
1180 DSS : Node_Id;
1182 begin
1183 -- Treated like a non-generic array declaration, with
1184 -- additional semantic checks.
1186 Enter_Name (T);
1188 if Nkind (Def) = N_Constrained_Array_Definition then
1189 DSS := First (Discrete_Subtype_Definitions (Def));
1190 while Present (DSS) loop
1191 if Nkind (DSS) = N_Subtype_Indication
1192 or else Nkind (DSS) = N_Range
1193 or else Nkind (DSS) = N_Attribute_Reference
1194 then
1195 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1196 end if;
1198 Next (DSS);
1199 end loop;
1200 end if;
1202 Array_Type_Declaration (T, Def);
1203 Set_Is_Generic_Type (Base_Type (T));
1205 if Ekind (Component_Type (T)) = E_Incomplete_Type
1206 and then No (Full_View (Component_Type (T)))
1207 then
1208 Error_Msg_N ("premature usage of incomplete type", Def);
1210 elsif Is_Internal (Component_Type (T))
1211 and then Nkind (Original_Node
1212 (Subtype_Indication (Component_Definition (Def))))
1213 /= N_Attribute_Reference
1214 then
1215 Error_Msg_N
1216 ("only a subtype mark is allowed in a formal",
1217 Subtype_Indication (Component_Definition (Def)));
1218 end if;
1220 end Analyze_Formal_Array_Type;
1222 ---------------------------------------------
1223 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1224 ---------------------------------------------
1226 -- As for other generic types, we create a valid type representation
1227 -- with legal but arbitrary attributes, whose values are never considered
1228 -- static. For all scalar types we introduce an anonymous base type, with
1229 -- the same attributes. We choose the corresponding integer type to be
1230 -- Standard_Integer.
1232 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1233 (T : Entity_Id;
1234 Def : Node_Id)
1236 Loc : constant Source_Ptr := Sloc (Def);
1237 Base : constant Entity_Id :=
1238 New_Internal_Entity
1239 (E_Decimal_Fixed_Point_Type,
1240 Current_Scope, Sloc (Def), 'G');
1241 Int_Base : constant Entity_Id := Standard_Integer;
1242 Delta_Val : constant Ureal := Ureal_1;
1243 Digs_Val : constant Uint := Uint_6;
1245 begin
1246 Enter_Name (T);
1248 Set_Etype (Base, Base);
1249 Set_Size_Info (Base, Int_Base);
1250 Set_RM_Size (Base, RM_Size (Int_Base));
1251 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1252 Set_Digits_Value (Base, Digs_Val);
1253 Set_Delta_Value (Base, Delta_Val);
1254 Set_Small_Value (Base, Delta_Val);
1255 Set_Scalar_Range (Base,
1256 Make_Range (Loc,
1257 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1258 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1260 Set_Is_Generic_Type (Base);
1261 Set_Parent (Base, Parent (Def));
1263 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1264 Set_Etype (T, Base);
1265 Set_Size_Info (T, Int_Base);
1266 Set_RM_Size (T, RM_Size (Int_Base));
1267 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1268 Set_Digits_Value (T, Digs_Val);
1269 Set_Delta_Value (T, Delta_Val);
1270 Set_Small_Value (T, Delta_Val);
1271 Set_Scalar_Range (T, Scalar_Range (Base));
1273 Check_Restriction (No_Fixed_Point, Def);
1274 end Analyze_Formal_Decimal_Fixed_Point_Type;
1276 ---------------------------------
1277 -- Analyze_Formal_Derived_Type --
1278 ---------------------------------
1280 procedure Analyze_Formal_Derived_Type
1281 (N : Node_Id;
1282 T : Entity_Id;
1283 Def : Node_Id)
1285 Loc : constant Source_Ptr := Sloc (Def);
1286 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
1287 New_N : Node_Id;
1289 begin
1290 Set_Is_Generic_Type (T);
1292 if Private_Present (Def) then
1293 New_N :=
1294 Make_Private_Extension_Declaration (Loc,
1295 Defining_Identifier => T,
1296 Discriminant_Specifications => Discriminant_Specifications (N),
1297 Unknown_Discriminants_Present => Unk_Disc,
1298 Subtype_Indication => Subtype_Mark (Def));
1300 Set_Abstract_Present (New_N, Abstract_Present (Def));
1302 else
1303 New_N :=
1304 Make_Full_Type_Declaration (Loc,
1305 Defining_Identifier => T,
1306 Discriminant_Specifications =>
1307 Discriminant_Specifications (Parent (T)),
1308 Type_Definition =>
1309 Make_Derived_Type_Definition (Loc,
1310 Subtype_Indication => Subtype_Mark (Def)));
1312 Set_Abstract_Present
1313 (Type_Definition (New_N), Abstract_Present (Def));
1314 end if;
1316 Rewrite (N, New_N);
1317 Analyze (N);
1319 if Unk_Disc then
1320 if not Is_Composite_Type (T) then
1321 Error_Msg_N
1322 ("unknown discriminants not allowed for elementary types", N);
1323 else
1324 Set_Has_Unknown_Discriminants (T);
1325 Set_Is_Constrained (T, False);
1326 end if;
1327 end if;
1329 -- If the parent type has a known size, so does the formal, which
1330 -- makes legal representation clauses that involve the formal.
1332 Set_Size_Known_At_Compile_Time
1333 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
1335 end Analyze_Formal_Derived_Type;
1337 ----------------------------------
1338 -- Analyze_Formal_Discrete_Type --
1339 ----------------------------------
1341 -- The operations defined for a discrete types are those of an
1342 -- enumeration type. The size is set to an arbitrary value, for use
1343 -- in analyzing the generic unit.
1345 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
1346 Loc : constant Source_Ptr := Sloc (Def);
1347 Lo : Node_Id;
1348 Hi : Node_Id;
1350 begin
1351 Enter_Name (T);
1352 Set_Ekind (T, E_Enumeration_Type);
1353 Set_Etype (T, T);
1354 Init_Size (T, 8);
1355 Init_Alignment (T);
1357 -- For semantic analysis, the bounds of the type must be set to some
1358 -- non-static value. The simplest is to create attribute nodes for
1359 -- those bounds, that refer to the type itself. These bounds are never
1360 -- analyzed but serve as place-holders.
1362 Lo :=
1363 Make_Attribute_Reference (Loc,
1364 Attribute_Name => Name_First,
1365 Prefix => New_Reference_To (T, Loc));
1366 Set_Etype (Lo, T);
1368 Hi :=
1369 Make_Attribute_Reference (Loc,
1370 Attribute_Name => Name_Last,
1371 Prefix => New_Reference_To (T, Loc));
1372 Set_Etype (Hi, T);
1374 Set_Scalar_Range (T,
1375 Make_Range (Loc,
1376 Low_Bound => Lo,
1377 High_Bound => Hi));
1379 end Analyze_Formal_Discrete_Type;
1381 ----------------------------------
1382 -- Analyze_Formal_Floating_Type --
1383 ---------------------------------
1385 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
1386 Base : constant Entity_Id :=
1387 New_Internal_Entity
1388 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1390 begin
1391 -- The various semantic attributes are taken from the predefined type
1392 -- Float, just so that all of them are initialized. Their values are
1393 -- never used because no constant folding or expansion takes place in
1394 -- the generic itself.
1396 Enter_Name (T);
1397 Set_Ekind (T, E_Floating_Point_Subtype);
1398 Set_Etype (T, Base);
1399 Set_Size_Info (T, (Standard_Float));
1400 Set_RM_Size (T, RM_Size (Standard_Float));
1401 Set_Digits_Value (T, Digits_Value (Standard_Float));
1402 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
1404 Set_Is_Generic_Type (Base);
1405 Set_Etype (Base, Base);
1406 Set_Size_Info (Base, (Standard_Float));
1407 Set_RM_Size (Base, RM_Size (Standard_Float));
1408 Set_Digits_Value (Base, Digits_Value (Standard_Float));
1409 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
1410 Set_Parent (Base, Parent (Def));
1412 Check_Restriction (No_Floating_Point, Def);
1413 end Analyze_Formal_Floating_Type;
1415 ---------------------------------
1416 -- Analyze_Formal_Modular_Type --
1417 ---------------------------------
1419 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
1420 begin
1421 -- Apart from their entity kind, generic modular types are treated
1422 -- like signed integer types, and have the same attributes.
1424 Analyze_Formal_Signed_Integer_Type (T, Def);
1425 Set_Ekind (T, E_Modular_Integer_Subtype);
1426 Set_Ekind (Etype (T), E_Modular_Integer_Type);
1428 end Analyze_Formal_Modular_Type;
1430 ---------------------------------------
1431 -- Analyze_Formal_Object_Declaration --
1432 ---------------------------------------
1434 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
1435 E : constant Node_Id := Expression (N);
1436 Id : constant Node_Id := Defining_Identifier (N);
1437 K : Entity_Kind;
1438 T : Node_Id;
1440 begin
1441 Enter_Name (Id);
1443 -- Determine the mode of the formal object
1445 if Out_Present (N) then
1446 K := E_Generic_In_Out_Parameter;
1448 if not In_Present (N) then
1449 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
1450 end if;
1452 else
1453 K := E_Generic_In_Parameter;
1454 end if;
1456 Find_Type (Subtype_Mark (N));
1457 T := Entity (Subtype_Mark (N));
1459 if Ekind (T) = E_Incomplete_Type then
1460 Error_Msg_N ("premature usage of incomplete type", Subtype_Mark (N));
1461 end if;
1463 if K = E_Generic_In_Parameter then
1465 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
1467 if Ada_Version < Ada_05 and then Is_Limited_Type (T) then
1468 Error_Msg_N
1469 ("generic formal of mode IN must not be of limited type", N);
1470 Explain_Limited_Type (T, N);
1471 end if;
1473 if Is_Abstract (T) then
1474 Error_Msg_N
1475 ("generic formal of mode IN must not be of abstract type", N);
1476 end if;
1478 if Present (E) then
1479 Analyze_Per_Use_Expression (E, T);
1480 end if;
1482 Set_Ekind (Id, K);
1483 Set_Etype (Id, T);
1485 -- Case of generic IN OUT parameter.
1487 else
1488 -- If the formal has an unconstrained type, construct its
1489 -- actual subtype, as is done for subprogram formals. In this
1490 -- fashion, all its uses can refer to specific bounds.
1492 Set_Ekind (Id, K);
1493 Set_Etype (Id, T);
1495 if (Is_Array_Type (T)
1496 and then not Is_Constrained (T))
1497 or else
1498 (Ekind (T) = E_Record_Type
1499 and then Has_Discriminants (T))
1500 then
1501 declare
1502 Non_Freezing_Ref : constant Node_Id :=
1503 New_Reference_To (Id, Sloc (Id));
1504 Decl : Node_Id;
1506 begin
1507 -- Make sure that the actual subtype doesn't generate
1508 -- bogus freezing.
1510 Set_Must_Not_Freeze (Non_Freezing_Ref);
1511 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
1512 Insert_Before_And_Analyze (N, Decl);
1513 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
1514 end;
1515 else
1516 Set_Actual_Subtype (Id, T);
1517 end if;
1519 if Present (E) then
1520 Error_Msg_N
1521 ("initialization not allowed for `IN OUT` formals", N);
1522 end if;
1523 end if;
1525 end Analyze_Formal_Object_Declaration;
1527 ----------------------------------------------
1528 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
1529 ----------------------------------------------
1531 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
1532 (T : Entity_Id;
1533 Def : Node_Id)
1535 Loc : constant Source_Ptr := Sloc (Def);
1536 Base : constant Entity_Id :=
1537 New_Internal_Entity
1538 (E_Ordinary_Fixed_Point_Type, Current_Scope, Sloc (Def), 'G');
1539 begin
1540 -- The semantic attributes are set for completeness only, their
1541 -- values will never be used, because all properties of the type
1542 -- are non-static.
1544 Enter_Name (T);
1545 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
1546 Set_Etype (T, Base);
1547 Set_Size_Info (T, Standard_Integer);
1548 Set_RM_Size (T, RM_Size (Standard_Integer));
1549 Set_Small_Value (T, Ureal_1);
1550 Set_Delta_Value (T, Ureal_1);
1551 Set_Scalar_Range (T,
1552 Make_Range (Loc,
1553 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1554 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1556 Set_Is_Generic_Type (Base);
1557 Set_Etype (Base, Base);
1558 Set_Size_Info (Base, Standard_Integer);
1559 Set_RM_Size (Base, RM_Size (Standard_Integer));
1560 Set_Small_Value (Base, Ureal_1);
1561 Set_Delta_Value (Base, Ureal_1);
1562 Set_Scalar_Range (Base, Scalar_Range (T));
1563 Set_Parent (Base, Parent (Def));
1565 Check_Restriction (No_Fixed_Point, Def);
1566 end Analyze_Formal_Ordinary_Fixed_Point_Type;
1568 ----------------------------
1569 -- Analyze_Formal_Package --
1570 ----------------------------
1572 procedure Analyze_Formal_Package (N : Node_Id) is
1573 Loc : constant Source_Ptr := Sloc (N);
1574 Pack_Id : constant Entity_Id := Defining_Identifier (N);
1575 Formal : Entity_Id;
1576 Gen_Id : constant Node_Id := Name (N);
1577 Gen_Decl : Node_Id;
1578 Gen_Unit : Entity_Id;
1579 New_N : Node_Id;
1580 Parent_Installed : Boolean := False;
1581 Renaming : Node_Id;
1582 Parent_Instance : Entity_Id;
1583 Renaming_In_Par : Entity_Id;
1585 begin
1586 Text_IO_Kludge (Gen_Id);
1588 Init_Env;
1589 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
1590 Gen_Unit := Entity (Gen_Id);
1592 if Ekind (Gen_Unit) /= E_Generic_Package then
1593 Error_Msg_N ("expect generic package name", Gen_Id);
1594 Restore_Env;
1595 return;
1597 elsif Gen_Unit = Current_Scope then
1598 Error_Msg_N
1599 ("generic package cannot be used as a formal package of itself",
1600 Gen_Id);
1601 Restore_Env;
1602 return;
1604 elsif In_Open_Scopes (Gen_Unit) then
1605 if Is_Compilation_Unit (Gen_Unit)
1606 and then Is_Child_Unit (Current_Scope)
1607 then
1608 -- Special-case the error when the formal is a parent, and
1609 -- continue analysis to minimize cascaded errors.
1611 Error_Msg_N
1612 ("generic parent cannot be used as formal package "
1613 & "of a child unit",
1614 Gen_Id);
1616 else
1617 Error_Msg_N
1618 ("generic package cannot be used as a formal package "
1619 & "within itself",
1620 Gen_Id);
1621 Restore_Env;
1622 return;
1623 end if;
1624 end if;
1626 -- Check for a formal package that is a package renaming.
1628 if Present (Renamed_Object (Gen_Unit)) then
1629 Gen_Unit := Renamed_Object (Gen_Unit);
1630 end if;
1632 -- The formal package is treated like a regular instance, but only
1633 -- the specification needs to be instantiated, to make entities visible.
1635 if not Box_Present (N) then
1636 Hidden_Entities := New_Elmt_List;
1637 Analyze_Package_Instantiation (N);
1639 if Parent_Installed then
1640 Remove_Parent;
1641 end if;
1643 else
1644 -- If there are no generic associations, the generic parameters
1645 -- appear as local entities and are instantiated like them. We copy
1646 -- the generic package declaration as if it were an instantiation,
1647 -- and analyze it like a regular package, except that we treat the
1648 -- formals as additional visible components.
1650 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
1652 if In_Extended_Main_Source_Unit (N) then
1653 Set_Is_Instantiated (Gen_Unit);
1654 Generate_Reference (Gen_Unit, N);
1655 end if;
1657 Formal := New_Copy (Pack_Id);
1658 New_N :=
1659 Copy_Generic_Node
1660 (Original_Node (Gen_Decl), Empty, Instantiating => True);
1661 Rewrite (N, New_N);
1662 Set_Defining_Unit_Name (Specification (New_N), Formal);
1663 Set_Instance_Env (Gen_Unit, Formal);
1665 Enter_Name (Formal);
1666 Set_Ekind (Formal, E_Generic_Package);
1667 Set_Etype (Formal, Standard_Void_Type);
1668 Set_Inner_Instances (Formal, New_Elmt_List);
1669 New_Scope (Formal);
1671 -- Within the formal, the name of the generic package is a renaming
1672 -- of the formal (as for a regular instantiation).
1674 Renaming := Make_Package_Renaming_Declaration (Loc,
1675 Defining_Unit_Name =>
1676 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
1677 Name => New_Reference_To (Formal, Loc));
1679 if Present (Visible_Declarations (Specification (N))) then
1680 Prepend (Renaming, To => Visible_Declarations (Specification (N)));
1681 elsif Present (Private_Declarations (Specification (N))) then
1682 Prepend (Renaming, To => Private_Declarations (Specification (N)));
1683 end if;
1685 if Is_Child_Unit (Gen_Unit)
1686 and then Parent_Installed
1687 then
1688 -- Similarly, we have to make the name of the formal visible in
1689 -- the parent instance, to resolve properly fully qualified names
1690 -- that may appear in the generic unit. The parent instance has
1691 -- been placed on the scope stack ahead of the current scope.
1693 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
1695 Renaming_In_Par :=
1696 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
1697 Set_Ekind (Renaming_In_Par, E_Package);
1698 Set_Etype (Renaming_In_Par, Standard_Void_Type);
1699 Set_Scope (Renaming_In_Par, Parent_Instance);
1700 Set_Parent (Renaming_In_Par, Parent (Formal));
1701 Set_Renamed_Object (Renaming_In_Par, Formal);
1702 Append_Entity (Renaming_In_Par, Parent_Instance);
1703 end if;
1705 Analyze_Generic_Formal_Part (N);
1706 Analyze (Specification (N));
1707 End_Package_Scope (Formal);
1709 if Parent_Installed then
1710 Remove_Parent;
1711 end if;
1713 Restore_Env;
1715 -- Inside the generic unit, the formal package is a regular
1716 -- package, but no body is needed for it. Note that after
1717 -- instantiation, the defining_unit_name we need is in the
1718 -- new tree and not in the original. (see Package_Instantiation).
1719 -- A generic formal package is an instance, and can be used as
1720 -- an actual for an inner instance. Mark its generic parent.
1722 Set_Ekind (Formal, E_Package);
1723 Set_Generic_Parent (Specification (N), Gen_Unit);
1724 Set_Has_Completion (Formal, True);
1726 Set_Ekind (Pack_Id, E_Package);
1727 Set_Etype (Pack_Id, Standard_Void_Type);
1728 Set_Scope (Pack_Id, Scope (Formal));
1729 Set_Has_Completion (Pack_Id, True);
1730 end if;
1731 end Analyze_Formal_Package;
1733 ---------------------------------
1734 -- Analyze_Formal_Private_Type --
1735 ---------------------------------
1737 procedure Analyze_Formal_Private_Type
1738 (N : Node_Id;
1739 T : Entity_Id;
1740 Def : Node_Id)
1742 begin
1743 New_Private_Type (N, T, Def);
1745 -- Set the size to an arbitrary but legal value.
1747 Set_Size_Info (T, Standard_Integer);
1748 Set_RM_Size (T, RM_Size (Standard_Integer));
1749 end Analyze_Formal_Private_Type;
1751 ----------------------------------------
1752 -- Analyze_Formal_Signed_Integer_Type --
1753 ----------------------------------------
1755 procedure Analyze_Formal_Signed_Integer_Type
1756 (T : Entity_Id;
1757 Def : Node_Id)
1759 Base : constant Entity_Id :=
1760 New_Internal_Entity
1761 (E_Signed_Integer_Type, Current_Scope, Sloc (Def), 'G');
1763 begin
1764 Enter_Name (T);
1766 Set_Ekind (T, E_Signed_Integer_Subtype);
1767 Set_Etype (T, Base);
1768 Set_Size_Info (T, Standard_Integer);
1769 Set_RM_Size (T, RM_Size (Standard_Integer));
1770 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
1772 Set_Is_Generic_Type (Base);
1773 Set_Size_Info (Base, Standard_Integer);
1774 Set_RM_Size (Base, RM_Size (Standard_Integer));
1775 Set_Etype (Base, Base);
1776 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
1777 Set_Parent (Base, Parent (Def));
1778 end Analyze_Formal_Signed_Integer_Type;
1780 -------------------------------
1781 -- Analyze_Formal_Subprogram --
1782 -------------------------------
1784 procedure Analyze_Formal_Subprogram (N : Node_Id) is
1785 Spec : constant Node_Id := Specification (N);
1786 Def : constant Node_Id := Default_Name (N);
1787 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
1788 Subp : Entity_Id;
1790 begin
1791 if Nam = Error then
1792 return;
1793 end if;
1795 if Nkind (Nam) = N_Defining_Program_Unit_Name then
1796 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
1797 return;
1798 end if;
1800 Analyze_Subprogram_Declaration (N);
1801 Set_Is_Formal_Subprogram (Nam);
1802 Set_Has_Completion (Nam);
1804 -- Default name is resolved at the point of instantiation
1806 if Box_Present (N) then
1807 null;
1809 -- Else default is bound at the point of generic declaration
1811 elsif Present (Def) then
1812 if Nkind (Def) = N_Operator_Symbol then
1813 Find_Direct_Name (Def);
1815 elsif Nkind (Def) /= N_Attribute_Reference then
1816 Analyze (Def);
1818 else
1819 -- For an attribute reference, analyze the prefix and verify
1820 -- that it has the proper profile for the subprogram.
1822 Analyze (Prefix (Def));
1823 Valid_Default_Attribute (Nam, Def);
1824 return;
1825 end if;
1827 -- Default name may be overloaded, in which case the interpretation
1828 -- with the correct profile must be selected, as for a renaming.
1830 if Etype (Def) = Any_Type then
1831 return;
1833 elsif Nkind (Def) = N_Selected_Component then
1834 Subp := Entity (Selector_Name (Def));
1836 if Ekind (Subp) /= E_Entry then
1837 Error_Msg_N ("expect valid subprogram name as default", Def);
1838 return;
1839 end if;
1841 elsif Nkind (Def) = N_Indexed_Component then
1843 if Nkind (Prefix (Def)) /= N_Selected_Component then
1844 Error_Msg_N ("expect valid subprogram name as default", Def);
1845 return;
1847 else
1848 Subp := Entity (Selector_Name (Prefix (Def)));
1850 if Ekind (Subp) /= E_Entry_Family then
1851 Error_Msg_N ("expect valid subprogram name as default", Def);
1852 return;
1853 end if;
1854 end if;
1856 elsif Nkind (Def) = N_Character_Literal then
1858 -- Needs some type checks: subprogram should be parameterless???
1860 Resolve (Def, (Etype (Nam)));
1862 elsif not Is_Entity_Name (Def)
1863 or else not Is_Overloadable (Entity (Def))
1864 then
1865 Error_Msg_N ("expect valid subprogram name as default", Def);
1866 return;
1868 elsif not Is_Overloaded (Def) then
1869 Subp := Entity (Def);
1871 if Subp = Nam then
1872 Error_Msg_N ("premature usage of formal subprogram", Def);
1874 elsif not Entity_Matches_Spec (Subp, Nam) then
1875 Error_Msg_N ("no visible entity matches specification", Def);
1876 end if;
1878 else
1879 declare
1880 I : Interp_Index;
1881 I1 : Interp_Index := 0;
1882 It : Interp;
1883 It1 : Interp;
1885 begin
1886 Subp := Any_Id;
1887 Get_First_Interp (Def, I, It);
1888 while Present (It.Nam) loop
1890 if Entity_Matches_Spec (It.Nam, Nam) then
1891 if Subp /= Any_Id then
1892 It1 := Disambiguate (Def, I1, I, Etype (Subp));
1894 if It1 = No_Interp then
1895 Error_Msg_N ("ambiguous default subprogram", Def);
1896 else
1897 Subp := It1.Nam;
1898 end if;
1900 exit;
1902 else
1903 I1 := I;
1904 Subp := It.Nam;
1905 end if;
1906 end if;
1908 Get_Next_Interp (I, It);
1909 end loop;
1910 end;
1912 if Subp /= Any_Id then
1913 Set_Entity (Def, Subp);
1915 if Subp = Nam then
1916 Error_Msg_N ("premature usage of formal subprogram", Def);
1918 elsif Ekind (Subp) /= E_Operator then
1919 Check_Mode_Conformant (Subp, Nam);
1920 end if;
1922 else
1923 Error_Msg_N ("no visible subprogram matches specification", N);
1924 end if;
1925 end if;
1926 end if;
1927 end Analyze_Formal_Subprogram;
1929 -------------------------------------
1930 -- Analyze_Formal_Type_Declaration --
1931 -------------------------------------
1933 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
1934 Def : constant Node_Id := Formal_Type_Definition (N);
1935 T : Entity_Id;
1937 begin
1938 T := Defining_Identifier (N);
1940 if Present (Discriminant_Specifications (N))
1941 and then Nkind (Def) /= N_Formal_Private_Type_Definition
1942 then
1943 Error_Msg_N
1944 ("discriminants not allowed for this formal type",
1945 Defining_Identifier (First (Discriminant_Specifications (N))));
1946 end if;
1948 -- Enter the new name, and branch to specific routine.
1950 case Nkind (Def) is
1951 when N_Formal_Private_Type_Definition =>
1952 Analyze_Formal_Private_Type (N, T, Def);
1954 when N_Formal_Derived_Type_Definition =>
1955 Analyze_Formal_Derived_Type (N, T, Def);
1957 when N_Formal_Discrete_Type_Definition =>
1958 Analyze_Formal_Discrete_Type (T, Def);
1960 when N_Formal_Signed_Integer_Type_Definition =>
1961 Analyze_Formal_Signed_Integer_Type (T, Def);
1963 when N_Formal_Modular_Type_Definition =>
1964 Analyze_Formal_Modular_Type (T, Def);
1966 when N_Formal_Floating_Point_Definition =>
1967 Analyze_Formal_Floating_Type (T, Def);
1969 when N_Formal_Ordinary_Fixed_Point_Definition =>
1970 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
1972 when N_Formal_Decimal_Fixed_Point_Definition =>
1973 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
1975 when N_Array_Type_Definition =>
1976 Analyze_Formal_Array_Type (T, Def);
1978 when N_Access_To_Object_Definition |
1979 N_Access_Function_Definition |
1980 N_Access_Procedure_Definition =>
1981 Analyze_Generic_Access_Type (T, Def);
1983 when N_Error =>
1984 null;
1986 when others =>
1987 raise Program_Error;
1989 end case;
1991 Set_Is_Generic_Type (T);
1992 end Analyze_Formal_Type_Declaration;
1994 ------------------------------------
1995 -- Analyze_Function_Instantiation --
1996 ------------------------------------
1998 procedure Analyze_Function_Instantiation (N : Node_Id) is
1999 begin
2000 Analyze_Subprogram_Instantiation (N, E_Function);
2001 end Analyze_Function_Instantiation;
2003 ---------------------------------
2004 -- Analyze_Generic_Access_Type --
2005 ---------------------------------
2007 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
2008 begin
2009 Enter_Name (T);
2011 if Nkind (Def) = N_Access_To_Object_Definition then
2012 Access_Type_Declaration (T, Def);
2014 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
2015 and then No (Full_View (Designated_Type (T)))
2016 and then not Is_Generic_Type (Designated_Type (T))
2017 then
2018 Error_Msg_N ("premature usage of incomplete type", Def);
2020 elsif Is_Internal (Designated_Type (T)) then
2021 Error_Msg_N
2022 ("only a subtype mark is allowed in a formal", Def);
2023 end if;
2025 else
2026 Access_Subprogram_Declaration (T, Def);
2027 end if;
2028 end Analyze_Generic_Access_Type;
2030 ---------------------------------
2031 -- Analyze_Generic_Formal_Part --
2032 ---------------------------------
2034 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
2035 Gen_Parm_Decl : Node_Id;
2037 begin
2038 -- The generic formals are processed in the scope of the generic
2039 -- unit, where they are immediately visible. The scope is installed
2040 -- by the caller.
2042 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
2044 while Present (Gen_Parm_Decl) loop
2045 Analyze (Gen_Parm_Decl);
2046 Next (Gen_Parm_Decl);
2047 end loop;
2049 Generate_Reference_To_Generic_Formals (Current_Scope);
2050 end Analyze_Generic_Formal_Part;
2052 ------------------------------------------
2053 -- Analyze_Generic_Package_Declaration --
2054 ------------------------------------------
2056 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
2057 Loc : constant Source_Ptr := Sloc (N);
2058 Id : Entity_Id;
2059 New_N : Node_Id;
2060 Save_Parent : Node_Id;
2061 Renaming : Node_Id;
2062 Decls : constant List_Id :=
2063 Visible_Declarations (Specification (N));
2064 Decl : Node_Id;
2066 begin
2067 -- We introduce a renaming of the enclosing package, to have a usable
2068 -- entity as the prefix of an expanded name for a local entity of the
2069 -- form Par.P.Q, where P is the generic package. This is because a local
2070 -- entity named P may hide it, so that the usual visibility rules in
2071 -- the instance will not resolve properly.
2073 Renaming :=
2074 Make_Package_Renaming_Declaration (Loc,
2075 Defining_Unit_Name =>
2076 Make_Defining_Identifier (Loc,
2077 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
2078 Name => Make_Identifier (Loc, Chars (Defining_Entity (N))));
2080 if Present (Decls) then
2081 Decl := First (Decls);
2082 while Present (Decl)
2083 and then Nkind (Decl) = N_Pragma
2084 loop
2085 Next (Decl);
2086 end loop;
2088 if Present (Decl) then
2089 Insert_Before (Decl, Renaming);
2090 else
2091 Append (Renaming, Visible_Declarations (Specification (N)));
2092 end if;
2094 else
2095 Set_Visible_Declarations (Specification (N), New_List (Renaming));
2096 end if;
2098 -- Create copy of generic unit, and save for instantiation.
2099 -- If the unit is a child unit, do not copy the specifications
2100 -- for the parent, which are not part of the generic tree.
2102 Save_Parent := Parent_Spec (N);
2103 Set_Parent_Spec (N, Empty);
2105 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2106 Set_Parent_Spec (New_N, Save_Parent);
2107 Rewrite (N, New_N);
2108 Id := Defining_Entity (N);
2109 Generate_Definition (Id);
2111 -- Expansion is not applied to generic units.
2113 Start_Generic;
2115 Enter_Name (Id);
2116 Set_Ekind (Id, E_Generic_Package);
2117 Set_Etype (Id, Standard_Void_Type);
2118 New_Scope (Id);
2119 Enter_Generic_Scope (Id);
2120 Set_Inner_Instances (Id, New_Elmt_List);
2122 Set_Categorization_From_Pragmas (N);
2123 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2125 -- Link the declaration of the generic homonym in the generic copy
2126 -- to the package it renames, so that it is always resolved properly.
2128 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
2129 Set_Entity (Associated_Node (Name (Renaming)), Id);
2131 -- For a library unit, we have reconstructed the entity for the
2132 -- unit, and must reset it in the library tables.
2134 if Nkind (Parent (N)) = N_Compilation_Unit then
2135 Set_Cunit_Entity (Current_Sem_Unit, Id);
2136 end if;
2138 Analyze_Generic_Formal_Part (N);
2140 -- After processing the generic formals, analysis proceeds
2141 -- as for a non-generic package.
2143 Analyze (Specification (N));
2145 Validate_Categorization_Dependency (N, Id);
2147 End_Generic;
2149 End_Package_Scope (Id);
2150 Exit_Generic_Scope (Id);
2152 if Nkind (Parent (N)) /= N_Compilation_Unit then
2153 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
2154 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
2155 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
2157 else
2158 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2159 Validate_RT_RAT_Component (N);
2161 -- If this is a spec without a body, check that generic parameters
2162 -- are referenced.
2164 if not Body_Required (Parent (N)) then
2165 Check_References (Id);
2166 end if;
2167 end if;
2168 end Analyze_Generic_Package_Declaration;
2170 --------------------------------------------
2171 -- Analyze_Generic_Subprogram_Declaration --
2172 --------------------------------------------
2174 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
2175 Spec : Node_Id;
2176 Id : Entity_Id;
2177 Formals : List_Id;
2178 New_N : Node_Id;
2179 Save_Parent : Node_Id;
2181 begin
2182 -- Create copy of generic unit,and save for instantiation.
2183 -- If the unit is a child unit, do not copy the specifications
2184 -- for the parent, which are not part of the generic tree.
2186 Save_Parent := Parent_Spec (N);
2187 Set_Parent_Spec (N, Empty);
2189 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2190 Set_Parent_Spec (New_N, Save_Parent);
2191 Rewrite (N, New_N);
2193 Spec := Specification (N);
2194 Id := Defining_Entity (Spec);
2195 Generate_Definition (Id);
2197 if Nkind (Id) = N_Defining_Operator_Symbol then
2198 Error_Msg_N
2199 ("operator symbol not allowed for generic subprogram", Id);
2200 end if;
2202 Start_Generic;
2204 Enter_Name (Id);
2206 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
2207 New_Scope (Id);
2208 Enter_Generic_Scope (Id);
2209 Set_Inner_Instances (Id, New_Elmt_List);
2210 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2212 Analyze_Generic_Formal_Part (N);
2214 Formals := Parameter_Specifications (Spec);
2216 if Present (Formals) then
2217 Process_Formals (Formals, Spec);
2218 end if;
2220 if Nkind (Spec) = N_Function_Specification then
2221 Set_Ekind (Id, E_Generic_Function);
2222 Find_Type (Subtype_Mark (Spec));
2223 Set_Etype (Id, Entity (Subtype_Mark (Spec)));
2224 else
2225 Set_Ekind (Id, E_Generic_Procedure);
2226 Set_Etype (Id, Standard_Void_Type);
2227 end if;
2229 -- For a library unit, we have reconstructed the entity for the
2230 -- unit, and must reset it in the library tables. We also need
2231 -- to make sure that Body_Required is set properly in the original
2232 -- compilation unit node.
2234 if Nkind (Parent (N)) = N_Compilation_Unit then
2235 Set_Cunit_Entity (Current_Sem_Unit, Id);
2236 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2237 end if;
2239 Set_Categorization_From_Pragmas (N);
2240 Validate_Categorization_Dependency (N, Id);
2242 Save_Global_References (Original_Node (N));
2244 End_Generic;
2245 End_Scope;
2246 Exit_Generic_Scope (Id);
2247 Generate_Reference_To_Formals (Id);
2248 end Analyze_Generic_Subprogram_Declaration;
2250 -----------------------------------
2251 -- Analyze_Package_Instantiation --
2252 -----------------------------------
2254 -- Note: this procedure is also used for formal package declarations,
2255 -- in which case the argument N is an N_Formal_Package_Declaration
2256 -- node. This should really be noted in the spec! ???
2258 procedure Analyze_Package_Instantiation (N : Node_Id) is
2259 Loc : constant Source_Ptr := Sloc (N);
2260 Gen_Id : constant Node_Id := Name (N);
2262 Act_Decl : Node_Id;
2263 Act_Decl_Name : Node_Id;
2264 Act_Decl_Id : Entity_Id;
2265 Act_Spec : Node_Id;
2266 Act_Tree : Node_Id;
2268 Gen_Decl : Node_Id;
2269 Gen_Unit : Entity_Id;
2271 Is_Actual_Pack : constant Boolean :=
2272 Is_Internal (Defining_Entity (N));
2274 Parent_Installed : Boolean := False;
2275 Renaming_List : List_Id;
2276 Unit_Renaming : Node_Id;
2277 Needs_Body : Boolean;
2278 Inline_Now : Boolean := False;
2280 procedure Delay_Descriptors (E : Entity_Id);
2281 -- Delay generation of subprogram descriptors for given entity
2283 function Might_Inline_Subp return Boolean;
2284 -- If inlining is active and the generic contains inlined subprograms,
2285 -- we instantiate the body. This may cause superfluous instantiations,
2286 -- but it is simpler than detecting the need for the body at the point
2287 -- of inlining, when the context of the instance is not available.
2289 -----------------------
2290 -- Delay_Descriptors --
2291 -----------------------
2293 procedure Delay_Descriptors (E : Entity_Id) is
2294 begin
2295 if not Delay_Subprogram_Descriptors (E) then
2296 Set_Delay_Subprogram_Descriptors (E);
2297 Pending_Descriptor.Increment_Last;
2298 Pending_Descriptor.Table (Pending_Descriptor.Last) := E;
2299 end if;
2300 end Delay_Descriptors;
2302 -----------------------
2303 -- Might_Inline_Subp --
2304 -----------------------
2306 function Might_Inline_Subp return Boolean is
2307 E : Entity_Id;
2309 begin
2310 if not Inline_Processing_Required then
2311 return False;
2313 else
2314 E := First_Entity (Gen_Unit);
2315 while Present (E) loop
2316 if Is_Subprogram (E)
2317 and then Is_Inlined (E)
2318 then
2319 return True;
2320 end if;
2322 Next_Entity (E);
2323 end loop;
2324 end if;
2326 return False;
2327 end Might_Inline_Subp;
2329 -- Start of processing for Analyze_Package_Instantiation
2331 begin
2332 -- Very first thing: apply the special kludge for Text_IO processing
2333 -- in case we are instantiating one of the children of [Wide_]Text_IO.
2335 Text_IO_Kludge (Name (N));
2337 -- Make node global for error reporting.
2339 Instantiation_Node := N;
2341 -- Case of instantiation of a generic package
2343 if Nkind (N) = N_Package_Instantiation then
2344 Act_Decl_Id := New_Copy (Defining_Entity (N));
2345 Set_Comes_From_Source (Act_Decl_Id, True);
2347 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
2348 Act_Decl_Name :=
2349 Make_Defining_Program_Unit_Name (Loc,
2350 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
2351 Defining_Identifier => Act_Decl_Id);
2352 else
2353 Act_Decl_Name := Act_Decl_Id;
2354 end if;
2356 -- Case of instantiation of a formal package
2358 else
2359 Act_Decl_Id := Defining_Identifier (N);
2360 Act_Decl_Name := Act_Decl_Id;
2361 end if;
2363 Generate_Definition (Act_Decl_Id);
2364 Pre_Analyze_Actuals (N);
2366 Init_Env;
2367 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2368 Gen_Unit := Entity (Gen_Id);
2370 -- Verify that it is the name of a generic package
2372 if Etype (Gen_Unit) = Any_Type then
2373 Restore_Env;
2374 return;
2376 elsif Ekind (Gen_Unit) /= E_Generic_Package then
2378 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
2380 if From_With_Type (Gen_Unit) then
2381 Error_Msg_N
2382 ("cannot instantiate a limited withed package", Gen_Id);
2383 else
2384 Error_Msg_N
2385 ("expect name of generic package in instantiation", Gen_Id);
2386 end if;
2388 Restore_Env;
2389 return;
2390 end if;
2392 if In_Extended_Main_Source_Unit (N) then
2393 Set_Is_Instantiated (Gen_Unit);
2394 Generate_Reference (Gen_Unit, N);
2396 if Present (Renamed_Object (Gen_Unit)) then
2397 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
2398 Generate_Reference (Renamed_Object (Gen_Unit), N);
2399 end if;
2400 end if;
2402 if Nkind (Gen_Id) = N_Identifier
2403 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
2404 then
2405 Error_Msg_NE
2406 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2408 elsif Nkind (Gen_Id) = N_Expanded_Name
2409 and then Is_Child_Unit (Gen_Unit)
2410 and then Nkind (Prefix (Gen_Id)) = N_Identifier
2411 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
2412 then
2413 Error_Msg_N
2414 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
2415 end if;
2417 Set_Entity (Gen_Id, Gen_Unit);
2419 -- If generic is a renaming, get original generic unit.
2421 if Present (Renamed_Object (Gen_Unit))
2422 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
2423 then
2424 Gen_Unit := Renamed_Object (Gen_Unit);
2425 end if;
2427 -- Verify that there are no circular instantiations.
2429 if In_Open_Scopes (Gen_Unit) then
2430 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
2431 Restore_Env;
2432 return;
2434 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
2435 Error_Msg_Node_2 := Current_Scope;
2436 Error_Msg_NE
2437 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
2438 Circularity_Detected := True;
2439 Restore_Env;
2440 return;
2442 else
2443 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
2444 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2446 -- Initialize renamings map, for error checking, and the list
2447 -- that holds private entities whose views have changed between
2448 -- generic definition and instantiation. If this is the instance
2449 -- created to validate an actual package, the instantiation
2450 -- environment is that of the enclosing instance.
2452 Generic_Renamings.Set_Last (0);
2453 Generic_Renamings_HTable.Reset;
2455 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2457 -- Copy original generic tree, to produce text for instantiation.
2459 Act_Tree :=
2460 Copy_Generic_Node
2461 (Original_Node (Gen_Decl), Empty, Instantiating => True);
2463 Act_Spec := Specification (Act_Tree);
2465 -- If this is the instance created to validate an actual package,
2466 -- only the formals matter, do not examine the package spec itself.
2468 if Is_Actual_Pack then
2469 Set_Visible_Declarations (Act_Spec, New_List);
2470 Set_Private_Declarations (Act_Spec, New_List);
2471 end if;
2473 Renaming_List :=
2474 Analyze_Associations
2476 Generic_Formal_Declarations (Act_Tree),
2477 Generic_Formal_Declarations (Gen_Decl));
2479 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
2480 Set_Is_Generic_Instance (Act_Decl_Id);
2482 Set_Generic_Parent (Act_Spec, Gen_Unit);
2484 -- References to the generic in its own declaration or its body
2485 -- are references to the instance. Add a renaming declaration for
2486 -- the generic unit itself. This declaration, as well as the renaming
2487 -- declarations for the generic formals, must remain private to the
2488 -- unit: the formals, because this is the language semantics, and
2489 -- the unit because its use is an artifact of the implementation.
2491 Unit_Renaming :=
2492 Make_Package_Renaming_Declaration (Loc,
2493 Defining_Unit_Name =>
2494 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2495 Name => New_Reference_To (Act_Decl_Id, Loc));
2497 Append (Unit_Renaming, Renaming_List);
2499 -- The renaming declarations are the first local declarations of
2500 -- the new unit.
2502 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
2503 Insert_List_Before
2504 (First (Visible_Declarations (Act_Spec)), Renaming_List);
2505 else
2506 Set_Visible_Declarations (Act_Spec, Renaming_List);
2507 end if;
2509 Act_Decl :=
2510 Make_Package_Declaration (Loc,
2511 Specification => Act_Spec);
2513 -- Save the instantiation node, for subsequent instantiation
2514 -- of the body, if there is one and we are generating code for
2515 -- the current unit. Mark the unit as having a body, to avoid
2516 -- a premature error message.
2518 -- We instantiate the body if we are generating code, if we are
2519 -- generating cross-reference information, or if we are building
2520 -- trees for ASIS use.
2522 declare
2523 Enclosing_Body_Present : Boolean := False;
2524 -- If the generic unit is not a compilation unit, then a body
2525 -- may be present in its parent even if none is required. We
2526 -- create a tentative pending instantiation for the body, which
2527 -- will be discarded if none is actually present.
2529 Scop : Entity_Id;
2531 begin
2532 if Scope (Gen_Unit) /= Standard_Standard
2533 and then not Is_Child_Unit (Gen_Unit)
2534 then
2535 Scop := Scope (Gen_Unit);
2537 while Present (Scop)
2538 and then Scop /= Standard_Standard
2539 loop
2540 if Unit_Requires_Body (Scop) then
2541 Enclosing_Body_Present := True;
2542 exit;
2544 elsif In_Open_Scopes (Scop)
2545 and then In_Package_Body (Scop)
2546 then
2547 Enclosing_Body_Present := True;
2548 exit;
2549 end if;
2551 exit when Is_Compilation_Unit (Scop);
2552 Scop := Scope (Scop);
2553 end loop;
2554 end if;
2556 -- If front-end inlining is enabled, and this is a unit for which
2557 -- code will be generated, we instantiate the body at once.
2558 -- This is done if the instance is not the main unit, and if the
2559 -- generic is not a child unit of another generic, to avoid scope
2560 -- problems and the reinstallation of parent instances.
2562 if Front_End_Inlining
2563 and then Expander_Active
2564 and then (not Is_Child_Unit (Gen_Unit)
2565 or else not Is_Generic_Unit (Scope (Gen_Unit)))
2566 and then Is_In_Main_Unit (N)
2567 and then Nkind (Parent (N)) /= N_Compilation_Unit
2568 and then Might_Inline_Subp
2569 and then not Is_Actual_Pack
2570 then
2571 Inline_Now := True;
2572 end if;
2574 Needs_Body :=
2575 (Unit_Requires_Body (Gen_Unit)
2576 or else Enclosing_Body_Present
2577 or else Present (Corresponding_Body (Gen_Decl)))
2578 and then (Is_In_Main_Unit (N)
2579 or else Might_Inline_Subp)
2580 and then not Is_Actual_Pack
2581 and then not Inline_Now
2583 and then (Operating_Mode = Generate_Code
2584 or else (Operating_Mode = Check_Semantics
2585 and then ASIS_Mode));
2587 -- If front_end_inlining is enabled, do not instantiate a
2588 -- body if within a generic context.
2590 if (Front_End_Inlining
2591 and then not Expander_Active)
2592 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
2593 then
2594 Needs_Body := False;
2595 end if;
2597 -- If the current context is generic, and the package being
2598 -- instantiated is declared within a formal package, there
2599 -- is no body to instantiate until the enclosing generic is
2600 -- instantiated, and there is an actual for the formal
2601 -- package. If the formal package has parameters, we build a
2602 -- regular package instance for it, that preceeds the original
2603 -- formal package declaration.
2605 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
2606 declare
2607 Decl : constant Node_Id :=
2608 Original_Node
2609 (Unit_Declaration_Node (Scope (Gen_Unit)));
2610 begin
2611 if Nkind (Decl) = N_Formal_Package_Declaration
2612 or else (Nkind (Decl) = N_Package_Declaration
2613 and then Is_List_Member (Decl)
2614 and then Present (Next (Decl))
2615 and then
2616 Nkind (Next (Decl)) = N_Formal_Package_Declaration)
2617 then
2618 Needs_Body := False;
2619 end if;
2620 end;
2621 end if;
2622 end;
2624 -- If we are generating the calling stubs from the instantiation
2625 -- of a generic RCI package, we will not use the body of the
2626 -- generic package.
2628 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
2629 and then Is_Compilation_Unit (Defining_Entity (N))
2630 then
2631 Needs_Body := False;
2632 end if;
2634 if Needs_Body then
2636 -- Here is a defence against a ludicrous number of instantiations
2637 -- caused by a circular set of instantiation attempts.
2639 if Pending_Instantiations.Last >
2640 Hostparm.Max_Instantiations
2641 then
2642 Error_Msg_N ("too many instantiations", N);
2643 raise Unrecoverable_Error;
2644 end if;
2646 -- Indicate that the enclosing scopes contain an instantiation,
2647 -- and that cleanup actions should be delayed until after the
2648 -- instance body is expanded.
2650 Check_Forward_Instantiation (Gen_Decl);
2651 if Nkind (N) = N_Package_Instantiation then
2652 declare
2653 Enclosing_Master : Entity_Id := Current_Scope;
2655 begin
2656 while Enclosing_Master /= Standard_Standard loop
2658 if Ekind (Enclosing_Master) = E_Package then
2659 if Is_Compilation_Unit (Enclosing_Master) then
2660 if In_Package_Body (Enclosing_Master) then
2661 Delay_Descriptors
2662 (Body_Entity (Enclosing_Master));
2663 else
2664 Delay_Descriptors
2665 (Enclosing_Master);
2666 end if;
2668 exit;
2670 else
2671 Enclosing_Master := Scope (Enclosing_Master);
2672 end if;
2674 elsif Ekind (Enclosing_Master) = E_Generic_Package then
2675 Enclosing_Master := Scope (Enclosing_Master);
2677 elsif Is_Generic_Subprogram (Enclosing_Master)
2678 or else Ekind (Enclosing_Master) = E_Void
2679 then
2680 -- Cleanup actions will eventually be performed on
2681 -- the enclosing instance, if any. enclosing scope
2682 -- is void in the formal part of a generic subp.
2684 exit;
2686 else
2687 if Ekind (Enclosing_Master) = E_Entry
2688 and then
2689 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
2690 then
2691 Enclosing_Master :=
2692 Protected_Body_Subprogram (Enclosing_Master);
2693 end if;
2695 Set_Delay_Cleanups (Enclosing_Master);
2697 while Ekind (Enclosing_Master) = E_Block loop
2698 Enclosing_Master := Scope (Enclosing_Master);
2699 end loop;
2701 if Is_Subprogram (Enclosing_Master) then
2702 Delay_Descriptors (Enclosing_Master);
2704 elsif Is_Task_Type (Enclosing_Master) then
2705 declare
2706 TBP : constant Node_Id :=
2707 Get_Task_Body_Procedure
2708 (Enclosing_Master);
2710 begin
2711 if Present (TBP) then
2712 Delay_Descriptors (TBP);
2713 Set_Delay_Cleanups (TBP);
2714 end if;
2715 end;
2716 end if;
2718 exit;
2719 end if;
2720 end loop;
2721 end;
2723 -- Make entry in table
2725 Pending_Instantiations.Increment_Last;
2726 Pending_Instantiations.Table (Pending_Instantiations.Last) :=
2727 (N, Act_Decl, Expander_Active, Current_Sem_Unit);
2728 end if;
2729 end if;
2731 Set_Categorization_From_Pragmas (Act_Decl);
2733 if Parent_Installed then
2734 Hide_Current_Scope;
2735 end if;
2737 Set_Instance_Spec (N, Act_Decl);
2739 -- If not a compilation unit, insert the package declaration
2740 -- before the original instantiation node.
2742 if Nkind (Parent (N)) /= N_Compilation_Unit then
2743 Mark_Rewrite_Insertion (Act_Decl);
2744 Insert_Before (N, Act_Decl);
2745 Analyze (Act_Decl);
2747 -- For an instantiation that is a compilation unit, place
2748 -- declaration on current node so context is complete
2749 -- for analysis (including nested instantiations). It this
2750 -- is the main unit, the declaration eventually replaces the
2751 -- instantiation node. If the instance body is later created, it
2752 -- replaces the instance node, and the declation is attached to
2753 -- it (see Build_Instance_Compilation_Unit_Nodes).
2755 else
2756 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
2758 -- The entity for the current unit is the newly created one,
2759 -- and all semantic information is attached to it.
2761 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
2763 -- If this is the main unit, replace the main entity as well.
2765 if Current_Sem_Unit = Main_Unit then
2766 Main_Unit_Entity := Act_Decl_Id;
2767 end if;
2768 end if;
2770 Set_Unit (Parent (N), Act_Decl);
2771 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
2772 Analyze (Act_Decl);
2773 Set_Unit (Parent (N), N);
2774 Set_Body_Required (Parent (N), False);
2776 -- We never need elaboration checks on instantiations, since
2777 -- by definition, the body instantiation is elaborated at the
2778 -- same time as the spec instantiation.
2780 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
2781 Set_Kill_Elaboration_Checks (Act_Decl_Id);
2782 end if;
2784 Check_Elab_Instantiation (N);
2786 if ABE_Is_Certain (N) and then Needs_Body then
2787 Pending_Instantiations.Decrement_Last;
2788 end if;
2789 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
2791 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
2792 First_Private_Entity (Act_Decl_Id));
2794 -- If the instantiation will receive a body, the unit will
2795 -- be transformed into a package body, and receive its own
2796 -- elaboration entity. Otherwise, the nature of the unit is
2797 -- now a package declaration.
2799 if Nkind (Parent (N)) = N_Compilation_Unit
2800 and then not Needs_Body
2801 then
2802 Rewrite (N, Act_Decl);
2803 end if;
2805 if Present (Corresponding_Body (Gen_Decl))
2806 or else Unit_Requires_Body (Gen_Unit)
2807 then
2808 Set_Has_Completion (Act_Decl_Id);
2809 end if;
2811 Check_Formal_Packages (Act_Decl_Id);
2813 Restore_Private_Views (Act_Decl_Id);
2815 if not Generic_Separately_Compiled (Gen_Unit) then
2816 Inherit_Context (Gen_Decl, N);
2817 end if;
2819 if Parent_Installed then
2820 Remove_Parent;
2821 end if;
2823 Restore_Env;
2824 end if;
2826 Validate_Categorization_Dependency (N, Act_Decl_Id);
2828 -- Check restriction, but skip this if something went wrong in
2829 -- the above analysis, indicated by Act_Decl_Id being void.
2831 if Ekind (Act_Decl_Id) /= E_Void
2832 and then not Is_Library_Level_Entity (Act_Decl_Id)
2833 then
2834 Check_Restriction (No_Local_Allocators, N);
2835 end if;
2837 if Inline_Now then
2838 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
2839 end if;
2841 exception
2842 when Instantiation_Error =>
2843 if Parent_Installed then
2844 Remove_Parent;
2845 end if;
2846 end Analyze_Package_Instantiation;
2848 --------------------------
2849 -- Inline_Instance_Body --
2850 --------------------------
2852 procedure Inline_Instance_Body
2853 (N : Node_Id;
2854 Gen_Unit : Entity_Id;
2855 Act_Decl : Node_Id)
2857 Vis : Boolean;
2858 Gen_Comp : constant Entity_Id :=
2859 Cunit_Entity (Get_Source_Unit (Gen_Unit));
2860 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
2861 Curr_Scope : Entity_Id := Empty;
2862 Curr_Unit : constant Entity_Id :=
2863 Cunit_Entity (Current_Sem_Unit);
2864 Removed : Boolean := False;
2865 Num_Scopes : Int := 0;
2866 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
2867 Instances : array (1 .. Scope_Stack.Last) of Entity_Id;
2868 Inner_Scopes : array (1 .. Scope_Stack.Last) of Entity_Id;
2869 Num_Inner : Int := 0;
2870 N_Instances : Int := 0;
2871 S : Entity_Id;
2873 begin
2874 -- Case of generic unit defined in another unit. We must remove
2875 -- the complete context of the current unit to install that of
2876 -- the generic.
2878 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
2879 S := Current_Scope;
2881 while Present (S)
2882 and then S /= Standard_Standard
2883 loop
2884 Num_Scopes := Num_Scopes + 1;
2886 Use_Clauses (Num_Scopes) :=
2887 (Scope_Stack.Table
2888 (Scope_Stack.Last - Num_Scopes + 1).
2889 First_Use_Clause);
2890 End_Use_Clauses (Use_Clauses (Num_Scopes));
2892 exit when Is_Generic_Instance (S)
2893 and then (In_Package_Body (S)
2894 or else Ekind (S) = E_Procedure
2895 or else Ekind (S) = E_Function);
2896 S := Scope (S);
2897 end loop;
2899 Vis := Is_Immediately_Visible (Gen_Comp);
2901 -- Find and save all enclosing instances
2903 S := Current_Scope;
2905 while Present (S)
2906 and then S /= Standard_Standard
2907 loop
2908 if Is_Generic_Instance (S) then
2909 N_Instances := N_Instances + 1;
2910 Instances (N_Instances) := S;
2912 exit when In_Package_Body (S);
2913 end if;
2915 S := Scope (S);
2916 end loop;
2918 -- Remove context of current compilation unit, unless we
2919 -- are within a nested package instantiation, in which case
2920 -- the context has been removed previously.
2922 -- If current scope is the body of a child unit, remove context
2923 -- of spec as well.
2925 S := Current_Scope;
2927 while Present (S)
2928 and then S /= Standard_Standard
2929 loop
2930 exit when Is_Generic_Instance (S)
2931 and then (In_Package_Body (S)
2932 or else Ekind (S) = E_Procedure
2933 or else Ekind (S) = E_Function);
2935 if S = Curr_Unit
2936 or else (Ekind (Curr_Unit) = E_Package_Body
2937 and then S = Spec_Entity (Curr_Unit))
2938 or else (Ekind (Curr_Unit) = E_Subprogram_Body
2939 and then S =
2940 Corresponding_Spec
2941 (Unit_Declaration_Node (Curr_Unit)))
2942 then
2943 Removed := True;
2945 -- Remove entities in current scopes from visibility, so
2946 -- than instance body is compiled in a clean environment.
2948 Save_Scope_Stack (Handle_Use => False);
2950 if Is_Child_Unit (S) then
2952 -- Remove child unit from stack, as well as inner scopes.
2953 -- Removing the context of a child unit removes parent
2954 -- units as well.
2956 while Current_Scope /= S loop
2957 Num_Inner := Num_Inner + 1;
2958 Inner_Scopes (Num_Inner) := Current_Scope;
2959 Pop_Scope;
2960 end loop;
2962 Pop_Scope;
2963 Remove_Context (Curr_Comp);
2964 Curr_Scope := S;
2966 else
2967 Remove_Context (Curr_Comp);
2968 end if;
2970 if Ekind (Curr_Unit) = E_Package_Body then
2971 Remove_Context (Library_Unit (Curr_Comp));
2972 end if;
2973 end if;
2975 S := Scope (S);
2976 end loop;
2978 New_Scope (Standard_Standard);
2979 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
2980 Instantiate_Package_Body
2981 ((N, Act_Decl, Expander_Active, Current_Sem_Unit), True);
2982 Pop_Scope;
2984 -- Restore context
2986 Set_Is_Immediately_Visible (Gen_Comp, Vis);
2988 -- Reset Generic_Instance flag so that use clauses can be installed
2989 -- in the proper order. (See Use_One_Package for effect of enclosing
2990 -- instances on processing of use clauses).
2992 for J in 1 .. N_Instances loop
2993 Set_Is_Generic_Instance (Instances (J), False);
2994 end loop;
2996 if Removed then
2997 Install_Context (Curr_Comp);
2999 if Present (Curr_Scope)
3000 and then Is_Child_Unit (Curr_Scope)
3001 then
3002 New_Scope (Curr_Scope);
3003 Set_Is_Immediately_Visible (Curr_Scope);
3005 -- Finally, restore inner scopes as well.
3007 for J in reverse 1 .. Num_Inner loop
3008 New_Scope (Inner_Scopes (J));
3009 end loop;
3010 end if;
3012 Restore_Scope_Stack (Handle_Use => False);
3013 end if;
3015 -- Restore use clauses. For a child unit, use clauses in the
3016 -- parents are restored when installing the context, so only
3017 -- those in inner scopes (and those local to the child unit itself)
3018 -- need to be installed explicitly.
3020 if Is_Child_Unit (Curr_Unit)
3021 and then Removed
3022 then
3023 for J in reverse 1 .. Num_Inner + 1 loop
3024 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3025 Use_Clauses (J);
3026 Install_Use_Clauses (Use_Clauses (J));
3027 end loop;
3029 else
3030 for J in reverse 1 .. Num_Scopes loop
3031 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
3032 Use_Clauses (J);
3033 Install_Use_Clauses (Use_Clauses (J));
3034 end loop;
3035 end if;
3037 for J in 1 .. N_Instances loop
3038 Set_Is_Generic_Instance (Instances (J), True);
3039 end loop;
3041 -- If generic unit is in current unit, current context is correct.
3043 else
3044 Instantiate_Package_Body
3045 ((N, Act_Decl, Expander_Active, Current_Sem_Unit), True);
3046 end if;
3047 end Inline_Instance_Body;
3049 -------------------------------------
3050 -- Analyze_Procedure_Instantiation --
3051 -------------------------------------
3053 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
3054 begin
3055 Analyze_Subprogram_Instantiation (N, E_Procedure);
3056 end Analyze_Procedure_Instantiation;
3058 --------------------------------------
3059 -- Analyze_Subprogram_Instantiation --
3060 --------------------------------------
3062 procedure Analyze_Subprogram_Instantiation
3063 (N : Node_Id;
3064 K : Entity_Kind)
3066 Loc : constant Source_Ptr := Sloc (N);
3067 Gen_Id : constant Node_Id := Name (N);
3069 Anon_Id : constant Entity_Id :=
3070 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
3071 Chars => New_External_Name
3072 (Chars (Defining_Entity (N)), 'R'));
3074 Act_Decl_Id : Entity_Id;
3075 Act_Decl : Node_Id;
3076 Act_Spec : Node_Id;
3077 Act_Tree : Node_Id;
3079 Gen_Unit : Entity_Id;
3080 Gen_Decl : Node_Id;
3081 Pack_Id : Entity_Id;
3082 Parent_Installed : Boolean := False;
3083 Renaming_List : List_Id;
3085 procedure Analyze_Instance_And_Renamings;
3086 -- The instance must be analyzed in a context that includes the
3087 -- mappings of generic parameters into actuals. We create a package
3088 -- declaration for this purpose, and a subprogram with an internal
3089 -- name within the package. The subprogram instance is simply an
3090 -- alias for the internal subprogram, declared in the current scope.
3092 ------------------------------------
3093 -- Analyze_Instance_And_Renamings --
3094 ------------------------------------
3096 procedure Analyze_Instance_And_Renamings is
3097 Def_Ent : constant Entity_Id := Defining_Entity (N);
3098 Pack_Decl : Node_Id;
3100 begin
3101 if Nkind (Parent (N)) = N_Compilation_Unit then
3103 -- For the case of a compilation unit, the container package
3104 -- has the same name as the instantiation, to insure that the
3105 -- binder calls the elaboration procedure with the right name.
3106 -- Copy the entity of the instance, which may have compilation
3107 -- level flags (e.g. Is_Child_Unit) set.
3109 Pack_Id := New_Copy (Def_Ent);
3111 else
3112 -- Otherwise we use the name of the instantiation concatenated
3113 -- with its source position to ensure uniqueness if there are
3114 -- several instantiations with the same name.
3116 Pack_Id :=
3117 Make_Defining_Identifier (Loc,
3118 Chars => New_External_Name
3119 (Related_Id => Chars (Def_Ent),
3120 Suffix => "GP",
3121 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
3122 end if;
3124 Pack_Decl := Make_Package_Declaration (Loc,
3125 Specification => Make_Package_Specification (Loc,
3126 Defining_Unit_Name => Pack_Id,
3127 Visible_Declarations => Renaming_List,
3128 End_Label => Empty));
3130 Set_Instance_Spec (N, Pack_Decl);
3131 Set_Is_Generic_Instance (Pack_Id);
3132 Set_Needs_Debug_Info (Pack_Id);
3134 -- Case of not a compilation unit
3136 if Nkind (Parent (N)) /= N_Compilation_Unit then
3137 Mark_Rewrite_Insertion (Pack_Decl);
3138 Insert_Before (N, Pack_Decl);
3139 Set_Has_Completion (Pack_Id);
3141 -- Case of an instantiation that is a compilation unit
3143 -- Place declaration on current node so context is complete
3144 -- for analysis (including nested instantiations), and for
3145 -- use in a context_clause (see Analyze_With_Clause).
3147 else
3148 Set_Unit (Parent (N), Pack_Decl);
3149 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
3150 end if;
3152 Analyze (Pack_Decl);
3153 Check_Formal_Packages (Pack_Id);
3154 Set_Is_Generic_Instance (Pack_Id, False);
3156 -- Body of the enclosing package is supplied when instantiating
3157 -- the subprogram body, after semantic analysis is completed.
3159 if Nkind (Parent (N)) = N_Compilation_Unit then
3161 -- Remove package itself from visibility, so it does not
3162 -- conflict with subprogram.
3164 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
3166 -- Set name and scope of internal subprogram so that the
3167 -- proper external name will be generated. The proper scope
3168 -- is the scope of the wrapper package. We need to generate
3169 -- debugging information for the internal subprogram, so set
3170 -- flag accordingly.
3172 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
3173 Set_Scope (Anon_Id, Scope (Pack_Id));
3175 -- Mark wrapper package as referenced, to avoid spurious
3176 -- warnings if the instantiation appears in various with_
3177 -- clauses of subunits of the main unit.
3179 Set_Referenced (Pack_Id);
3180 end if;
3182 Set_Is_Generic_Instance (Anon_Id);
3183 Set_Needs_Debug_Info (Anon_Id);
3184 Act_Decl_Id := New_Copy (Anon_Id);
3186 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3187 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
3188 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
3189 Set_Comes_From_Source (Act_Decl_Id, True);
3191 -- The signature may involve types that are not frozen yet, but
3192 -- the subprogram will be frozen at the point the wrapper package
3193 -- is frozen, so it does not need its own freeze node. In fact, if
3194 -- one is created, it might conflict with the freezing actions from
3195 -- the wrapper package (see 7206-013).
3197 Set_Has_Delayed_Freeze (Anon_Id, False);
3199 -- If the instance is a child unit, mark the Id accordingly. Mark
3200 -- the anonymous entity as well, which is the real subprogram and
3201 -- which is used when the instance appears in a context clause.
3203 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
3204 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
3205 New_Overloaded_Entity (Act_Decl_Id);
3206 Check_Eliminated (Act_Decl_Id);
3208 -- In compilation unit case, kill elaboration checks on the
3209 -- instantiation, since they are never needed -- the body is
3210 -- instantiated at the same point as the spec.
3212 if Nkind (Parent (N)) = N_Compilation_Unit then
3213 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
3214 Set_Kill_Elaboration_Checks (Act_Decl_Id);
3215 Set_Is_Compilation_Unit (Anon_Id);
3217 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
3218 end if;
3220 -- The instance is not a freezing point for the new subprogram.
3222 Set_Is_Frozen (Act_Decl_Id, False);
3224 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
3225 Valid_Operator_Definition (Act_Decl_Id);
3226 end if;
3228 Set_Alias (Act_Decl_Id, Anon_Id);
3229 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3230 Set_Has_Completion (Act_Decl_Id);
3231 Set_Related_Instance (Pack_Id, Act_Decl_Id);
3233 if Nkind (Parent (N)) = N_Compilation_Unit then
3234 Set_Body_Required (Parent (N), False);
3235 end if;
3237 end Analyze_Instance_And_Renamings;
3239 -- Start of processing for Analyze_Subprogram_Instantiation
3241 begin
3242 -- Very first thing: apply the special kludge for Text_IO processing
3243 -- in case we are instantiating one of the children of [Wide_]Text_IO.
3244 -- Of course such an instantiation is bogus (these are packages, not
3245 -- subprograms), but we get a better error message if we do this.
3247 Text_IO_Kludge (Gen_Id);
3249 -- Make node global for error reporting.
3251 Instantiation_Node := N;
3252 Pre_Analyze_Actuals (N);
3254 Init_Env;
3255 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3256 Gen_Unit := Entity (Gen_Id);
3258 Generate_Reference (Gen_Unit, Gen_Id);
3260 if Nkind (Gen_Id) = N_Identifier
3261 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3262 then
3263 Error_Msg_NE
3264 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3265 end if;
3267 if Etype (Gen_Unit) = Any_Type then
3268 Restore_Env;
3269 return;
3270 end if;
3272 -- Verify that it is a generic subprogram of the right kind, and that
3273 -- it does not lead to a circular instantiation.
3275 if Ekind (Gen_Unit) /= E_Generic_Procedure
3276 and then Ekind (Gen_Unit) /= E_Generic_Function
3277 then
3278 Error_Msg_N ("expect generic subprogram in instantiation", Gen_Id);
3280 elsif In_Open_Scopes (Gen_Unit) then
3281 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3283 elsif K = E_Procedure
3284 and then Ekind (Gen_Unit) /= E_Generic_Procedure
3285 then
3286 if Ekind (Gen_Unit) = E_Generic_Function then
3287 Error_Msg_N
3288 ("cannot instantiate generic function as procedure", Gen_Id);
3289 else
3290 Error_Msg_N
3291 ("expect name of generic procedure in instantiation", Gen_Id);
3292 end if;
3294 elsif K = E_Function
3295 and then Ekind (Gen_Unit) /= E_Generic_Function
3296 then
3297 if Ekind (Gen_Unit) = E_Generic_Procedure then
3298 Error_Msg_N
3299 ("cannot instantiate generic procedure as function", Gen_Id);
3300 else
3301 Error_Msg_N
3302 ("expect name of generic function in instantiation", Gen_Id);
3303 end if;
3305 else
3306 Set_Entity (Gen_Id, Gen_Unit);
3307 Set_Is_Instantiated (Gen_Unit);
3309 if In_Extended_Main_Source_Unit (N) then
3310 Generate_Reference (Gen_Unit, N);
3311 end if;
3313 -- If renaming, get original unit
3315 if Present (Renamed_Object (Gen_Unit))
3316 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
3317 or else
3318 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
3319 then
3320 Gen_Unit := Renamed_Object (Gen_Unit);
3321 Set_Is_Instantiated (Gen_Unit);
3322 Generate_Reference (Gen_Unit, N);
3323 end if;
3325 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3326 Error_Msg_Node_2 := Current_Scope;
3327 Error_Msg_NE
3328 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3329 Circularity_Detected := True;
3330 return;
3331 end if;
3333 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3335 -- The subprogram itself cannot contain a nested instance, so
3336 -- the current parent is left empty.
3338 Set_Instance_Env (Gen_Unit, Empty);
3340 -- Initialize renamings map, for error checking.
3342 Generic_Renamings.Set_Last (0);
3343 Generic_Renamings_HTable.Reset;
3345 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3347 -- Copy original generic tree, to produce text for instantiation.
3349 Act_Tree :=
3350 Copy_Generic_Node
3351 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3353 Act_Spec := Specification (Act_Tree);
3354 Renaming_List :=
3355 Analyze_Associations
3357 Generic_Formal_Declarations (Act_Tree),
3358 Generic_Formal_Declarations (Gen_Decl));
3360 -- Build the subprogram declaration, which does not appear
3361 -- in the generic template, and give it a sloc consistent
3362 -- with that of the template.
3364 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
3365 Set_Generic_Parent (Act_Spec, Gen_Unit);
3366 Act_Decl :=
3367 Make_Subprogram_Declaration (Sloc (Act_Spec),
3368 Specification => Act_Spec);
3370 Set_Categorization_From_Pragmas (Act_Decl);
3372 if Parent_Installed then
3373 Hide_Current_Scope;
3374 end if;
3376 Append (Act_Decl, Renaming_List);
3377 Analyze_Instance_And_Renamings;
3379 -- If the generic is marked Import (Intrinsic), then so is the
3380 -- instance. This indicates that there is no body to instantiate.
3381 -- If generic is marked inline, so it the instance, and the
3382 -- anonymous subprogram it renames. If inlined, or else if inlining
3383 -- is enabled for the compilation, we generate the instance body
3384 -- even if it is not within the main unit.
3386 -- Any other pragmas might also be inherited ???
3388 if Is_Intrinsic_Subprogram (Gen_Unit) then
3389 Set_Is_Intrinsic_Subprogram (Anon_Id);
3390 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
3392 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
3393 Validate_Unchecked_Conversion (N, Act_Decl_Id);
3394 end if;
3395 end if;
3397 Generate_Definition (Act_Decl_Id);
3399 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
3400 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
3402 if not Is_Intrinsic_Subprogram (Gen_Unit) then
3403 Check_Elab_Instantiation (N);
3404 end if;
3406 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
3408 -- Subject to change, pending on if other pragmas are inherited ???
3410 Validate_Categorization_Dependency (N, Act_Decl_Id);
3412 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
3414 if not Generic_Separately_Compiled (Gen_Unit) then
3415 Inherit_Context (Gen_Decl, N);
3416 end if;
3418 Restore_Private_Views (Pack_Id, False);
3420 -- If the context requires a full instantiation, mark node for
3421 -- subsequent construction of the body.
3423 if (Is_In_Main_Unit (N)
3424 or else Is_Inlined (Act_Decl_Id))
3425 and then (Operating_Mode = Generate_Code
3426 or else (Operating_Mode = Check_Semantics
3427 and then ASIS_Mode))
3428 and then (Expander_Active or else ASIS_Mode)
3429 and then not ABE_Is_Certain (N)
3430 and then not Is_Eliminated (Act_Decl_Id)
3431 then
3432 Pending_Instantiations.Increment_Last;
3433 Pending_Instantiations.Table (Pending_Instantiations.Last) :=
3434 (N, Act_Decl, Expander_Active, Current_Sem_Unit);
3435 Check_Forward_Instantiation (Gen_Decl);
3437 -- The wrapper package is always delayed, because it does
3438 -- not constitute a freeze point, but to insure that the
3439 -- freeze node is placed properly, it is created directly
3440 -- when instantiating the body (otherwise the freeze node
3441 -- might appear to early for nested instantiations).
3443 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3445 -- For ASIS purposes, indicate that the wrapper package has
3446 -- replaced the instantiation node.
3448 Rewrite (N, Unit (Parent (N)));
3449 Set_Unit (Parent (N), N);
3450 end if;
3452 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3454 -- Replace instance node for library-level instantiations
3455 -- of intrinsic subprograms, for ASIS use.
3457 Rewrite (N, Unit (Parent (N)));
3458 Set_Unit (Parent (N), N);
3459 end if;
3461 if Parent_Installed then
3462 Remove_Parent;
3463 end if;
3465 Restore_Env;
3466 Generic_Renamings.Set_Last (0);
3467 Generic_Renamings_HTable.Reset;
3468 end if;
3470 exception
3471 when Instantiation_Error =>
3472 if Parent_Installed then
3473 Remove_Parent;
3474 end if;
3475 end Analyze_Subprogram_Instantiation;
3477 -------------------------
3478 -- Get_Associated_Node --
3479 -------------------------
3481 function Get_Associated_Node (N : Node_Id) return Node_Id is
3482 Assoc : Node_Id := Associated_Node (N);
3484 begin
3485 if Nkind (Assoc) /= Nkind (N) then
3486 return Assoc;
3488 elsif Nkind (Assoc) = N_Aggregate
3489 or else Nkind (Assoc) = N_Extension_Aggregate
3490 then
3491 return Assoc;
3493 else
3494 -- If the node is part of an inner generic, it may itself have been
3495 -- remapped into a further generic copy. Associated_Node is otherwise
3496 -- used for the entity of the node, and will be of a different node
3497 -- kind, or else N has been rewritten as a literal or function call.
3499 while Present (Associated_Node (Assoc))
3500 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
3501 loop
3502 Assoc := Associated_Node (Assoc);
3503 end loop;
3505 -- Follow and additional link in case the final node was rewritten.
3506 -- This can only happen with nested generic units.
3508 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
3509 and then Present (Associated_Node (Assoc))
3510 and then (Nkind (Associated_Node (Assoc)) = N_Function_Call
3511 or else
3512 Nkind (Associated_Node (Assoc)) = N_Explicit_Dereference
3513 or else
3514 Nkind (Associated_Node (Assoc)) = N_Integer_Literal
3515 or else
3516 Nkind (Associated_Node (Assoc)) = N_Real_Literal
3517 or else
3518 Nkind (Associated_Node (Assoc)) = N_String_Literal)
3519 then
3520 Assoc := Associated_Node (Assoc);
3521 end if;
3523 return Assoc;
3524 end if;
3525 end Get_Associated_Node;
3527 -------------------------------------------
3528 -- Build_Instance_Compilation_Unit_Nodes --
3529 -------------------------------------------
3531 procedure Build_Instance_Compilation_Unit_Nodes
3532 (N : Node_Id;
3533 Act_Body : Node_Id;
3534 Act_Decl : Node_Id)
3536 Decl_Cunit : Node_Id;
3537 Body_Cunit : Node_Id;
3538 Citem : Node_Id;
3539 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
3540 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
3542 begin
3543 -- A new compilation unit node is built for the instance declaration
3545 Decl_Cunit :=
3546 Make_Compilation_Unit (Sloc (N),
3547 Context_Items => Empty_List,
3548 Unit => Act_Decl,
3549 Aux_Decls_Node =>
3550 Make_Compilation_Unit_Aux (Sloc (N)));
3552 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
3553 Set_Body_Required (Decl_Cunit, True);
3555 -- We use the original instantiation compilation unit as the resulting
3556 -- compilation unit of the instance, since this is the main unit.
3558 Rewrite (N, Act_Body);
3559 Body_Cunit := Parent (N);
3561 -- The two compilation unit nodes are linked by the Library_Unit field
3563 Set_Library_Unit (Decl_Cunit, Body_Cunit);
3564 Set_Library_Unit (Body_Cunit, Decl_Cunit);
3566 -- Preserve the private nature of the package if needed.
3568 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
3570 -- If the instance is not the main unit, its context, categorization,
3571 -- and elaboration entity are not relevant to the compilation.
3573 if Parent (N) /= Cunit (Main_Unit) then
3574 return;
3575 end if;
3577 -- The context clause items on the instantiation, which are now
3578 -- attached to the body compilation unit (since the body overwrote
3579 -- the original instantiation node), semantically belong on the spec,
3580 -- so copy them there. It's harmless to leave them on the body as well.
3581 -- In fact one could argue that they belong in both places.
3583 Citem := First (Context_Items (Body_Cunit));
3584 while Present (Citem) loop
3585 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
3586 Next (Citem);
3587 end loop;
3589 -- Propagate categorization flags on packages, so that they appear
3590 -- in ali file for the spec of the unit.
3592 if Ekind (New_Main) = E_Package then
3593 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
3594 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
3595 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
3596 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
3597 Set_Is_Remote_Call_Interface
3598 (Old_Main, Is_Remote_Call_Interface (New_Main));
3599 end if;
3601 -- Make entry in Units table, so that binder can generate call to
3602 -- elaboration procedure for body, if any.
3604 Make_Instance_Unit (Body_Cunit);
3605 Main_Unit_Entity := New_Main;
3606 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
3608 -- Build elaboration entity, since the instance may certainly
3609 -- generate elaboration code requiring a flag for protection.
3611 Build_Elaboration_Entity (Decl_Cunit, New_Main);
3612 end Build_Instance_Compilation_Unit_Nodes;
3614 -----------------------------------
3615 -- Check_Formal_Package_Instance --
3616 -----------------------------------
3618 -- If the formal has specific parameters, they must match those of the
3619 -- actual. Both of them are instances, and the renaming declarations
3620 -- for their formal parameters appear in the same order in both. The
3621 -- analyzed formal has been analyzed in the context of the current
3622 -- instance.
3624 procedure Check_Formal_Package_Instance
3625 (Formal_Pack : Entity_Id;
3626 Actual_Pack : Entity_Id)
3628 E1 : Entity_Id := First_Entity (Actual_Pack);
3629 E2 : Entity_Id := First_Entity (Formal_Pack);
3631 Expr1 : Node_Id;
3632 Expr2 : Node_Id;
3634 procedure Check_Mismatch (B : Boolean);
3635 -- Common error routine for mismatch between the parameters of
3636 -- the actual instance and those of the formal package.
3638 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
3639 -- The formal may come from a nested formal package, and the actual
3640 -- may have been constant-folded. To determine whether the two denote
3641 -- the same entity we may have to traverse several definitions to
3642 -- recover the ultimate entity that they refer to.
3644 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
3645 -- Similarly, if the formal comes from a nested formal package, the
3646 -- actual may designate the formal through multiple renamings, which
3647 -- have to be followed to determine the original variable in question.
3649 --------------------
3650 -- Check_Mismatch --
3651 --------------------
3653 procedure Check_Mismatch (B : Boolean) is
3654 begin
3655 if B then
3656 Error_Msg_NE
3657 ("actual for & in actual instance does not match formal",
3658 Parent (Actual_Pack), E1);
3659 end if;
3660 end Check_Mismatch;
3662 --------------------------------
3663 -- Same_Instantiated_Constant --
3664 --------------------------------
3666 function Same_Instantiated_Constant
3667 (E1, E2 : Entity_Id) return Boolean
3669 Ent : Entity_Id;
3670 begin
3671 Ent := E2;
3672 while Present (Ent) loop
3673 if E1 = Ent then
3674 return True;
3676 elsif Ekind (Ent) /= E_Constant then
3677 return False;
3679 elsif Is_Entity_Name (Constant_Value (Ent)) then
3680 if Entity (Constant_Value (Ent)) = E1 then
3681 return True;
3682 else
3683 Ent := Entity (Constant_Value (Ent));
3684 end if;
3686 -- The actual may be a constant that has been folded. Recover
3687 -- original name.
3689 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
3690 Ent := Entity (Original_Node (Constant_Value (Ent)));
3691 else
3692 return False;
3693 end if;
3694 end loop;
3696 return False;
3697 end Same_Instantiated_Constant;
3699 --------------------------------
3700 -- Same_Instantiated_Variable --
3701 --------------------------------
3703 function Same_Instantiated_Variable
3704 (E1, E2 : Entity_Id) return Boolean
3706 function Original_Entity (E : Entity_Id) return Entity_Id;
3707 -- Follow chain of renamings to the ultimate ancestor.
3709 ---------------------
3710 -- Original_Entity --
3711 ---------------------
3713 function Original_Entity (E : Entity_Id) return Entity_Id is
3714 Orig : Entity_Id;
3716 begin
3717 Orig := E;
3718 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
3719 and then Present (Renamed_Object (Orig))
3720 and then Is_Entity_Name (Renamed_Object (Orig))
3721 loop
3722 Orig := Entity (Renamed_Object (Orig));
3723 end loop;
3725 return Orig;
3726 end Original_Entity;
3728 -- Start of processing for Same_Instantiated_Variable
3730 begin
3731 return Ekind (E1) = Ekind (E2)
3732 and then Original_Entity (E1) = Original_Entity (E2);
3733 end Same_Instantiated_Variable;
3735 -- Start of processing for Check_Formal_Package_Instance
3737 begin
3738 while Present (E1)
3739 and then Present (E2)
3740 loop
3741 exit when Ekind (E1) = E_Package
3742 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
3744 if Is_Type (E1) then
3746 -- Subtypes must statically match. E1 and E2 are the
3747 -- local entities that are subtypes of the actuals.
3748 -- Itypes generated for other parameters need not be checked,
3749 -- the check will be performed on the parameters themselves.
3751 if not Is_Itype (E1)
3752 and then not Is_Itype (E2)
3753 then
3754 Check_Mismatch
3755 (not Is_Type (E2)
3756 or else Etype (E1) /= Etype (E2)
3757 or else not Subtypes_Statically_Match (E1, E2));
3758 end if;
3760 elsif Ekind (E1) = E_Constant then
3762 -- IN parameters must denote the same static value, or
3763 -- the same constant, or the literal null.
3765 Expr1 := Expression (Parent (E1));
3767 if Ekind (E2) /= E_Constant then
3768 Check_Mismatch (True);
3769 goto Next_E;
3770 else
3771 Expr2 := Expression (Parent (E2));
3772 end if;
3774 if Is_Static_Expression (Expr1) then
3776 if not Is_Static_Expression (Expr2) then
3777 Check_Mismatch (True);
3779 elsif Is_Integer_Type (Etype (E1)) then
3781 declare
3782 V1 : constant Uint := Expr_Value (Expr1);
3783 V2 : constant Uint := Expr_Value (Expr2);
3784 begin
3785 Check_Mismatch (V1 /= V2);
3786 end;
3788 elsif Is_Real_Type (Etype (E1)) then
3789 declare
3790 V1 : constant Ureal := Expr_Value_R (Expr1);
3791 V2 : constant Ureal := Expr_Value_R (Expr2);
3792 begin
3793 Check_Mismatch (V1 /= V2);
3794 end;
3796 elsif Is_String_Type (Etype (E1))
3797 and then Nkind (Expr1) = N_String_Literal
3798 then
3800 if Nkind (Expr2) /= N_String_Literal then
3801 Check_Mismatch (True);
3802 else
3803 Check_Mismatch
3804 (not String_Equal (Strval (Expr1), Strval (Expr2)));
3805 end if;
3806 end if;
3808 elsif Is_Entity_Name (Expr1) then
3809 if Is_Entity_Name (Expr2) then
3810 if Entity (Expr1) = Entity (Expr2) then
3811 null;
3812 else
3813 Check_Mismatch
3814 (not Same_Instantiated_Constant
3815 (Entity (Expr1), Entity (Expr2)));
3816 end if;
3817 else
3818 Check_Mismatch (True);
3819 end if;
3821 elsif Is_Entity_Name (Original_Node (Expr1))
3822 and then Is_Entity_Name (Expr2)
3823 and then
3824 Same_Instantiated_Constant
3825 (Entity (Original_Node (Expr1)), Entity (Expr2))
3826 then
3827 null;
3829 elsif Nkind (Expr1) = N_Null then
3830 Check_Mismatch (Nkind (Expr1) /= N_Null);
3832 else
3833 Check_Mismatch (True);
3834 end if;
3836 elsif Ekind (E1) = E_Variable then
3837 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
3839 elsif Ekind (E1) = E_Package then
3840 Check_Mismatch
3841 (Ekind (E1) /= Ekind (E2)
3842 or else Renamed_Object (E1) /= Renamed_Object (E2));
3844 elsif Is_Overloadable (E1) then
3846 -- Verify that the names of the entities match.
3847 -- What if actual is an attribute ???
3849 Check_Mismatch
3850 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
3852 else
3853 raise Program_Error;
3854 end if;
3856 <<Next_E>>
3857 Next_Entity (E1);
3858 Next_Entity (E2);
3859 end loop;
3860 end Check_Formal_Package_Instance;
3862 ---------------------------
3863 -- Check_Formal_Packages --
3864 ---------------------------
3866 procedure Check_Formal_Packages (P_Id : Entity_Id) is
3867 E : Entity_Id;
3868 Formal_P : Entity_Id;
3870 begin
3871 -- Iterate through the declarations in the instance, looking for
3872 -- package renaming declarations that denote instances of formal
3873 -- packages. Stop when we find the renaming of the current package
3874 -- itself. The declaration for a formal package without a box is
3875 -- followed by an internal entity that repeats the instantiation.
3877 E := First_Entity (P_Id);
3878 while Present (E) loop
3879 if Ekind (E) = E_Package then
3880 if Renamed_Object (E) = P_Id then
3881 exit;
3883 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
3884 null;
3886 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
3887 Formal_P := Next_Entity (E);
3888 Check_Formal_Package_Instance (Formal_P, E);
3889 end if;
3890 end if;
3892 Next_Entity (E);
3893 end loop;
3894 end Check_Formal_Packages;
3896 ---------------------------------
3897 -- Check_Forward_Instantiation --
3898 ---------------------------------
3900 procedure Check_Forward_Instantiation (Decl : Node_Id) is
3901 S : Entity_Id;
3902 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
3904 begin
3905 -- The instantiation appears before the generic body if we are in the
3906 -- scope of the unit containing the generic, either in its spec or in
3907 -- the package body. and before the generic body.
3909 if Ekind (Gen_Comp) = E_Package_Body then
3910 Gen_Comp := Spec_Entity (Gen_Comp);
3911 end if;
3913 if In_Open_Scopes (Gen_Comp)
3914 and then No (Corresponding_Body (Decl))
3915 then
3916 S := Current_Scope;
3918 while Present (S)
3919 and then not Is_Compilation_Unit (S)
3920 and then not Is_Child_Unit (S)
3921 loop
3922 if Ekind (S) = E_Package then
3923 Set_Has_Forward_Instantiation (S);
3924 end if;
3926 S := Scope (S);
3927 end loop;
3928 end if;
3929 end Check_Forward_Instantiation;
3931 ---------------------------
3932 -- Check_Generic_Actuals --
3933 ---------------------------
3935 -- The visibility of the actuals may be different between the
3936 -- point of generic instantiation and the instantiation of the body.
3938 procedure Check_Generic_Actuals
3939 (Instance : Entity_Id;
3940 Is_Formal_Box : Boolean)
3942 E : Entity_Id;
3943 Astype : Entity_Id;
3945 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
3946 -- For a formal that is an array type, the component type is often
3947 -- a previous formal in the same unit. The privacy status of the
3948 -- component type will have been examined earlier in the traversal
3949 -- of the corresponding actuals, and this status should not be
3950 -- modified for the array type itself.
3951 -- To detect this case we have to rescan the list of formals, which
3952 -- is usually short enough to ignore the resulting inefficiency.
3954 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
3955 Prev : Entity_Id;
3956 begin
3957 Prev := First_Entity (Instance);
3958 while Present (Prev) loop
3959 if Is_Type (Prev)
3960 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
3961 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
3962 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
3963 then
3964 return True;
3965 elsif Prev = E then
3966 return False;
3967 else
3968 Next_Entity (Prev);
3969 end if;
3970 end loop;
3971 return False;
3972 end Denotes_Previous_Actual;
3974 -- Start of processing for Check_Generic_Actuals
3976 begin
3977 E := First_Entity (Instance);
3978 while Present (E) loop
3979 if Is_Type (E)
3980 and then Nkind (Parent (E)) = N_Subtype_Declaration
3981 and then Scope (Etype (E)) /= Instance
3982 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
3983 then
3984 if Is_Array_Type (E)
3985 and then Denotes_Previous_Actual (Component_Type (E))
3986 then
3987 null;
3988 else
3989 Check_Private_View (Subtype_Indication (Parent (E)));
3990 end if;
3991 Set_Is_Generic_Actual_Type (E, True);
3992 Set_Is_Hidden (E, False);
3993 Set_Is_Potentially_Use_Visible (E,
3994 In_Use (Instance));
3996 -- We constructed the generic actual type as a subtype of
3997 -- the supplied type. This means that it normally would not
3998 -- inherit subtype specific attributes of the actual, which
3999 -- is wrong for the generic case.
4001 Astype := Ancestor_Subtype (E);
4003 if No (Astype) then
4005 -- can happen when E is an itype that is the full view of
4006 -- a private type completed, e.g. with a constrained array.
4008 Astype := Base_Type (E);
4009 end if;
4011 Set_Size_Info (E, (Astype));
4012 Set_RM_Size (E, RM_Size (Astype));
4013 Set_First_Rep_Item (E, First_Rep_Item (Astype));
4015 if Is_Discrete_Or_Fixed_Point_Type (E) then
4016 Set_RM_Size (E, RM_Size (Astype));
4018 -- In nested instances, the base type of an access actual
4019 -- may itself be private, and need to be exchanged.
4021 elsif Is_Access_Type (E)
4022 and then Is_Private_Type (Etype (E))
4023 then
4024 Check_Private_View
4025 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
4026 end if;
4028 elsif Ekind (E) = E_Package then
4030 -- If this is the renaming for the current instance, we're done.
4031 -- Otherwise it is a formal package. If the corresponding formal
4032 -- was declared with a box, the (instantiations of the) generic
4033 -- formal part are also visible. Otherwise, ignore the entity
4034 -- created to validate the actuals.
4036 if Renamed_Object (E) = Instance then
4037 exit;
4039 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
4040 null;
4042 -- The visibility of a formal of an enclosing generic is already
4043 -- correct.
4045 elsif Denotes_Formal_Package (E) then
4046 null;
4048 elsif Present (Associated_Formal_Package (E)) then
4049 if Box_Present (Parent (Associated_Formal_Package (E))) then
4050 Check_Generic_Actuals (Renamed_Object (E), True);
4051 end if;
4053 Set_Is_Hidden (E, False);
4054 end if;
4056 -- If this is a subprogram instance (in a wrapper package) the
4057 -- actual is fully visible.
4059 elsif Is_Wrapper_Package (Instance) then
4060 Set_Is_Hidden (E, False);
4062 else
4063 Set_Is_Hidden (E, not Is_Formal_Box);
4064 end if;
4066 Next_Entity (E);
4067 end loop;
4068 end Check_Generic_Actuals;
4070 ------------------------------
4071 -- Check_Generic_Child_Unit --
4072 ------------------------------
4074 procedure Check_Generic_Child_Unit
4075 (Gen_Id : Node_Id;
4076 Parent_Installed : in out Boolean)
4078 Loc : constant Source_Ptr := Sloc (Gen_Id);
4079 Gen_Par : Entity_Id := Empty;
4080 Inst_Par : Entity_Id;
4081 E : Entity_Id;
4082 S : Node_Id;
4084 function Find_Generic_Child
4085 (Scop : Entity_Id;
4086 Id : Node_Id) return Entity_Id;
4087 -- Search generic parent for possible child unit with the given name.
4089 function In_Enclosing_Instance return Boolean;
4090 -- Within an instance of the parent, the child unit may be denoted
4091 -- by a simple name, or an abbreviated expanded name. Examine enclosing
4092 -- scopes to locate a possible parent instantiation.
4094 ------------------------
4095 -- Find_Generic_Child --
4096 ------------------------
4098 function Find_Generic_Child
4099 (Scop : Entity_Id;
4100 Id : Node_Id) return Entity_Id
4102 E : Entity_Id;
4104 begin
4105 -- If entity of name is already set, instance has already been
4106 -- resolved, e.g. in an enclosing instantiation.
4108 if Present (Entity (Id)) then
4109 if Scope (Entity (Id)) = Scop then
4110 return Entity (Id);
4111 else
4112 return Empty;
4113 end if;
4115 else
4116 E := First_Entity (Scop);
4117 while Present (E) loop
4118 if Chars (E) = Chars (Id)
4119 and then Is_Child_Unit (E)
4120 then
4121 if Is_Child_Unit (E)
4122 and then not Is_Visible_Child_Unit (E)
4123 then
4124 Error_Msg_NE
4125 ("generic child unit& is not visible", Gen_Id, E);
4126 end if;
4128 Set_Entity (Id, E);
4129 return E;
4130 end if;
4132 Next_Entity (E);
4133 end loop;
4135 return Empty;
4136 end if;
4137 end Find_Generic_Child;
4139 ---------------------------
4140 -- In_Enclosing_Instance --
4141 ---------------------------
4143 function In_Enclosing_Instance return Boolean is
4144 Enclosing_Instance : Node_Id;
4145 Instance_Decl : Node_Id;
4147 begin
4148 Enclosing_Instance := Current_Scope;
4150 while Present (Enclosing_Instance) loop
4151 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
4153 if Ekind (Enclosing_Instance) = E_Package
4154 and then Is_Generic_Instance (Enclosing_Instance)
4155 and then Present
4156 (Generic_Parent (Specification (Instance_Decl)))
4157 then
4158 -- Check whether the generic we are looking for is a child
4159 -- of this instance.
4161 E := Find_Generic_Child
4162 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
4163 exit when Present (E);
4165 else
4166 E := Empty;
4167 end if;
4169 Enclosing_Instance := Scope (Enclosing_Instance);
4170 end loop;
4172 if No (E) then
4174 -- Not a child unit
4176 Analyze (Gen_Id);
4177 return False;
4179 else
4180 Rewrite (Gen_Id,
4181 Make_Expanded_Name (Loc,
4182 Chars => Chars (E),
4183 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
4184 Selector_Name => New_Occurrence_Of (E, Loc)));
4186 Set_Entity (Gen_Id, E);
4187 Set_Etype (Gen_Id, Etype (E));
4188 Parent_Installed := False; -- Already in scope.
4189 return True;
4190 end if;
4191 end In_Enclosing_Instance;
4193 -- Start of processing for Check_Generic_Child_Unit
4195 begin
4196 -- If the name of the generic is given by a selected component, it
4197 -- may be the name of a generic child unit, and the prefix is the name
4198 -- of an instance of the parent, in which case the child unit must be
4199 -- visible. If this instance is not in scope, it must be placed there
4200 -- and removed after instantiation, because what is being instantiated
4201 -- is not the original child, but the corresponding child present in
4202 -- the instance of the parent.
4204 -- If the child is instantiated within the parent, it can be given by
4205 -- a simple name. In this case the instance is already in scope, but
4206 -- the child generic must be recovered from the generic parent as well.
4208 if Nkind (Gen_Id) = N_Selected_Component then
4209 S := Selector_Name (Gen_Id);
4210 Analyze (Prefix (Gen_Id));
4211 Inst_Par := Entity (Prefix (Gen_Id));
4213 if Ekind (Inst_Par) = E_Package
4214 and then Present (Renamed_Object (Inst_Par))
4215 then
4216 Inst_Par := Renamed_Object (Inst_Par);
4217 end if;
4219 if Ekind (Inst_Par) = E_Package then
4220 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
4221 Gen_Par := Generic_Parent (Parent (Inst_Par));
4223 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
4224 and then
4225 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
4226 then
4227 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
4228 end if;
4230 elsif Ekind (Inst_Par) = E_Generic_Package
4231 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
4232 then
4233 -- A formal package may be a real child package, and not the
4234 -- implicit instance within a parent. In this case the child is
4235 -- not visible and has to be retrieved explicitly as well.
4237 Gen_Par := Inst_Par;
4238 end if;
4240 if Present (Gen_Par) then
4242 -- The prefix denotes an instantiation. The entity itself
4243 -- may be a nested generic, or a child unit.
4245 E := Find_Generic_Child (Gen_Par, S);
4247 if Present (E) then
4248 Change_Selected_Component_To_Expanded_Name (Gen_Id);
4249 Set_Entity (Gen_Id, E);
4250 Set_Etype (Gen_Id, Etype (E));
4251 Set_Entity (S, E);
4252 Set_Etype (S, Etype (E));
4254 -- Indicate that this is a reference to the parent.
4256 if In_Extended_Main_Source_Unit (Gen_Id) then
4257 Set_Is_Instantiated (Inst_Par);
4258 end if;
4260 -- A common mistake is to replicate the naming scheme of
4261 -- a hierarchy by instantiating a generic child directly,
4262 -- rather than the implicit child in a parent instance:
4264 -- generic .. package Gpar is ..
4265 -- generic .. package Gpar.Child is ..
4266 -- package Par is new Gpar ();
4268 -- with Gpar.Child;
4269 -- package Par.Child is new Gpar.Child ();
4270 -- rather than Par.Child
4272 -- In this case the instantiation is within Par, which is
4273 -- an instance, but Gpar does not denote Par because we are
4274 -- not IN the instance of Gpar, so this is illegal. The test
4275 -- below recognizes this particular case.
4277 if Is_Child_Unit (E)
4278 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
4279 and then (not In_Instance
4280 or else Nkind (Parent (Parent (Gen_Id))) =
4281 N_Compilation_Unit)
4282 then
4283 Error_Msg_N
4284 ("prefix of generic child unit must be instance of parent",
4285 Gen_Id);
4286 end if;
4288 if not In_Open_Scopes (Inst_Par)
4289 and then Nkind (Parent (Gen_Id)) not in
4290 N_Generic_Renaming_Declaration
4291 then
4292 Install_Parent (Inst_Par);
4293 Parent_Installed := True;
4294 end if;
4296 else
4297 -- If the generic parent does not contain an entity that
4298 -- corresponds to the selector, the instance doesn't either.
4299 -- Analyzing the node will yield the appropriate error message.
4300 -- If the entity is not a child unit, then it is an inner
4301 -- generic in the parent.
4303 Analyze (Gen_Id);
4304 end if;
4306 else
4307 Analyze (Gen_Id);
4309 if Is_Child_Unit (Entity (Gen_Id))
4310 and then
4311 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
4312 and then not In_Open_Scopes (Inst_Par)
4313 then
4314 Install_Parent (Inst_Par);
4315 Parent_Installed := True;
4316 end if;
4317 end if;
4319 elsif Nkind (Gen_Id) = N_Expanded_Name then
4321 -- Entity already present, analyze prefix, whose meaning may be
4322 -- an instance in the current context. If it is an instance of
4323 -- a relative within another, the proper parent may still have
4324 -- to be installed, if they are not of the same generation.
4326 Analyze (Prefix (Gen_Id));
4327 Inst_Par := Entity (Prefix (Gen_Id));
4329 if In_Enclosing_Instance then
4330 null;
4332 elsif Present (Entity (Gen_Id))
4333 and then Is_Child_Unit (Entity (Gen_Id))
4334 and then not In_Open_Scopes (Inst_Par)
4335 then
4336 Install_Parent (Inst_Par);
4337 Parent_Installed := True;
4338 end if;
4340 elsif In_Enclosing_Instance then
4342 -- The child unit is found in some enclosing scope
4344 null;
4346 else
4347 Analyze (Gen_Id);
4349 -- If this is the renaming of the implicit child in a parent
4350 -- instance, recover the parent name and install it.
4352 if Is_Entity_Name (Gen_Id) then
4353 E := Entity (Gen_Id);
4355 if Is_Generic_Unit (E)
4356 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
4357 and then Is_Child_Unit (Renamed_Object (E))
4358 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
4359 and then Nkind (Name (Parent (E))) = N_Expanded_Name
4360 then
4361 Rewrite (Gen_Id,
4362 New_Copy_Tree (Name (Parent (E))));
4363 Inst_Par := Entity (Prefix (Gen_Id));
4365 if not In_Open_Scopes (Inst_Par) then
4366 Install_Parent (Inst_Par);
4367 Parent_Installed := True;
4368 end if;
4370 -- If it is a child unit of a non-generic parent, it may be
4371 -- use-visible and given by a direct name. Install parent as
4372 -- for other cases.
4374 elsif Is_Generic_Unit (E)
4375 and then Is_Child_Unit (E)
4376 and then
4377 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
4378 and then not Is_Generic_Unit (Scope (E))
4379 then
4380 if not In_Open_Scopes (Scope (E)) then
4381 Install_Parent (Scope (E));
4382 Parent_Installed := True;
4383 end if;
4384 end if;
4385 end if;
4386 end if;
4387 end Check_Generic_Child_Unit;
4389 -----------------------------
4390 -- Check_Hidden_Child_Unit --
4391 -----------------------------
4393 procedure Check_Hidden_Child_Unit
4394 (N : Node_Id;
4395 Gen_Unit : Entity_Id;
4396 Act_Decl_Id : Entity_Id)
4398 Gen_Id : constant Node_Id := Name (N);
4400 begin
4401 if Is_Child_Unit (Gen_Unit)
4402 and then Is_Child_Unit (Act_Decl_Id)
4403 and then Nkind (Gen_Id) = N_Expanded_Name
4404 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
4405 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
4406 then
4407 Error_Msg_Node_2 := Scope (Act_Decl_Id);
4408 Error_Msg_NE
4409 ("generic unit & is implicitly declared in &",
4410 Defining_Unit_Name (N), Gen_Unit);
4411 Error_Msg_N ("\instance must have different name",
4412 Defining_Unit_Name (N));
4413 end if;
4414 end Check_Hidden_Child_Unit;
4416 ------------------------
4417 -- Check_Private_View --
4418 ------------------------
4420 procedure Check_Private_View (N : Node_Id) is
4421 T : constant Entity_Id := Etype (N);
4422 BT : Entity_Id;
4424 begin
4425 -- Exchange views if the type was not private in the generic but is
4426 -- private at the point of instantiation. Do not exchange views if
4427 -- the scope of the type is in scope. This can happen if both generic
4428 -- and instance are sibling units, or if type is defined in a parent.
4429 -- In this case the visibility of the type will be correct for all
4430 -- semantic checks.
4432 if Present (T) then
4433 BT := Base_Type (T);
4435 if Is_Private_Type (T)
4436 and then not Has_Private_View (N)
4437 and then Present (Full_View (T))
4438 and then not In_Open_Scopes (Scope (T))
4439 then
4440 -- In the generic, the full type was visible. Save the
4441 -- private entity, for subsequent exchange.
4443 Switch_View (T);
4445 elsif Has_Private_View (N)
4446 and then not Is_Private_Type (T)
4447 and then not Has_Been_Exchanged (T)
4448 and then Etype (Get_Associated_Node (N)) /= T
4449 then
4450 -- Only the private declaration was visible in the generic. If
4451 -- the type appears in a subtype declaration, the subtype in the
4452 -- instance must have a view compatible with that of its parent,
4453 -- which must be exchanged (see corresponding code in Restore_
4454 -- Private_Views). Otherwise, if the type is defined in a parent
4455 -- unit, leave full visibility within instance, which is safe.
4457 if In_Open_Scopes (Scope (Base_Type (T)))
4458 and then not Is_Private_Type (Base_Type (T))
4459 and then Comes_From_Source (Base_Type (T))
4460 then
4461 null;
4463 elsif Nkind (Parent (N)) = N_Subtype_Declaration
4464 or else not In_Private_Part (Scope (Base_Type (T)))
4465 then
4466 Append_Elmt (T, Exchanged_Views);
4467 Exchange_Declarations (Etype (Get_Associated_Node (N)));
4468 end if;
4470 -- For composite types with inconsistent representation
4471 -- exchange component types accordingly.
4473 elsif Is_Access_Type (T)
4474 and then Is_Private_Type (Designated_Type (T))
4475 and then not Has_Private_View (N)
4476 and then Present (Full_View (Designated_Type (T)))
4477 then
4478 Switch_View (Designated_Type (T));
4480 elsif Is_Array_Type (T)
4481 and then Is_Private_Type (Component_Type (T))
4482 and then not Has_Private_View (N)
4483 and then Present (Full_View (Component_Type (T)))
4484 then
4485 Switch_View (Component_Type (T));
4487 elsif Is_Private_Type (T)
4488 and then Present (Full_View (T))
4489 and then Is_Array_Type (Full_View (T))
4490 and then Is_Private_Type (Component_Type (Full_View (T)))
4491 then
4492 Switch_View (T);
4494 -- Finally, a non-private subtype may have a private base type,
4495 -- which must be exchanged for consistency. This can happen when
4496 -- instantiating a package body, when the scope stack is empty
4497 -- but in fact the subtype and the base type are declared in an
4498 -- enclosing scope.
4500 elsif not Is_Private_Type (T)
4501 and then not Has_Private_View (N)
4502 and then Is_Private_Type (Base_Type (T))
4503 and then Present (Full_View (BT))
4504 and then not Is_Generic_Type (BT)
4505 and then not In_Open_Scopes (BT)
4506 then
4507 Append_Elmt (Full_View (BT), Exchanged_Views);
4508 Exchange_Declarations (BT);
4509 end if;
4510 end if;
4511 end Check_Private_View;
4513 --------------------------
4514 -- Contains_Instance_Of --
4515 --------------------------
4517 function Contains_Instance_Of
4518 (Inner : Entity_Id;
4519 Outer : Entity_Id;
4520 N : Node_Id) return Boolean
4522 Elmt : Elmt_Id;
4523 Scop : Entity_Id;
4525 begin
4526 Scop := Outer;
4528 -- Verify that there are no circular instantiations. We check whether
4529 -- the unit contains an instance of the current scope or some enclosing
4530 -- scope (in case one of the instances appears in a subunit). Longer
4531 -- circularities involving subunits might seem too pathological to
4532 -- consider, but they were not too pathological for the authors of
4533 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
4534 -- enclosing generic scopes as containing an instance.
4536 loop
4537 -- Within a generic subprogram body, the scope is not generic, to
4538 -- allow for recursive subprograms. Use the declaration to determine
4539 -- whether this is a generic unit.
4541 if Ekind (Scop) = E_Generic_Package
4542 or else (Is_Subprogram (Scop)
4543 and then Nkind (Unit_Declaration_Node (Scop)) =
4544 N_Generic_Subprogram_Declaration)
4545 then
4546 Elmt := First_Elmt (Inner_Instances (Inner));
4548 while Present (Elmt) loop
4549 if Node (Elmt) = Scop then
4550 Error_Msg_Node_2 := Inner;
4551 Error_Msg_NE
4552 ("circular Instantiation: & instantiated within &!",
4553 N, Scop);
4554 return True;
4556 elsif Node (Elmt) = Inner then
4557 return True;
4559 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
4560 Error_Msg_Node_2 := Inner;
4561 Error_Msg_NE
4562 ("circular Instantiation: & instantiated within &!",
4563 N, Node (Elmt));
4564 return True;
4565 end if;
4567 Next_Elmt (Elmt);
4568 end loop;
4570 -- Indicate that Inner is being instantiated within Scop.
4572 Append_Elmt (Inner, Inner_Instances (Scop));
4573 end if;
4575 if Scop = Standard_Standard then
4576 exit;
4577 else
4578 Scop := Scope (Scop);
4579 end if;
4580 end loop;
4582 return False;
4583 end Contains_Instance_Of;
4585 -----------------------
4586 -- Copy_Generic_Node --
4587 -----------------------
4589 function Copy_Generic_Node
4590 (N : Node_Id;
4591 Parent_Id : Node_Id;
4592 Instantiating : Boolean) return Node_Id
4594 Ent : Entity_Id;
4595 New_N : Node_Id;
4597 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
4598 -- Check the given value of one of the Fields referenced by the
4599 -- current node to determine whether to copy it recursively. The
4600 -- field may hold a Node_Id, a List_Id, or an Elist_Id, or a plain
4601 -- value (Sloc, Uint, Char) in which case it need not be copied.
4603 procedure Copy_Descendants;
4604 -- Common utility for various nodes.
4606 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
4607 -- Make copy of element list.
4609 function Copy_Generic_List
4610 (L : List_Id;
4611 Parent_Id : Node_Id) return List_Id;
4612 -- Apply Copy_Node recursively to the members of a node list.
4614 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
4615 -- True if an identifier is part of the defining program unit name
4616 -- of a child unit. The entity of such an identifier must be kept
4617 -- (for ASIS use) even though as the name of an enclosing generic
4618 -- it would otherwise not be preserved in the generic tree.
4620 ----------------------
4621 -- Copy_Descendants --
4622 ----------------------
4624 procedure Copy_Descendants is
4626 use Atree.Unchecked_Access;
4627 -- This code section is part of the implementation of an untyped
4628 -- tree traversal, so it needs direct access to node fields.
4630 begin
4631 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
4632 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
4633 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
4634 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
4635 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
4636 end Copy_Descendants;
4638 -----------------------------
4639 -- Copy_Generic_Descendant --
4640 -----------------------------
4642 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
4643 begin
4644 if D = Union_Id (Empty) then
4645 return D;
4647 elsif D in Node_Range then
4648 return Union_Id
4649 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
4651 elsif D in List_Range then
4652 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
4654 elsif D in Elist_Range then
4655 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
4657 -- Nothing else is copyable (e.g. Uint values), return as is
4659 else
4660 return D;
4661 end if;
4662 end Copy_Generic_Descendant;
4664 ------------------------
4665 -- Copy_Generic_Elist --
4666 ------------------------
4668 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
4669 M : Elmt_Id;
4670 L : Elist_Id;
4672 begin
4673 if Present (E) then
4674 L := New_Elmt_List;
4675 M := First_Elmt (E);
4676 while Present (M) loop
4677 Append_Elmt
4678 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
4679 Next_Elmt (M);
4680 end loop;
4682 return L;
4684 else
4685 return No_Elist;
4686 end if;
4687 end Copy_Generic_Elist;
4689 -----------------------
4690 -- Copy_Generic_List --
4691 -----------------------
4693 function Copy_Generic_List
4694 (L : List_Id;
4695 Parent_Id : Node_Id) return List_Id
4697 N : Node_Id;
4698 New_L : List_Id;
4700 begin
4701 if Present (L) then
4702 New_L := New_List;
4703 Set_Parent (New_L, Parent_Id);
4705 N := First (L);
4706 while Present (N) loop
4707 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
4708 Next (N);
4709 end loop;
4711 return New_L;
4713 else
4714 return No_List;
4715 end if;
4716 end Copy_Generic_List;
4718 ---------------------------
4719 -- In_Defining_Unit_Name --
4720 ---------------------------
4722 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
4723 begin
4724 return Present (Parent (Nam))
4725 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
4726 or else
4727 (Nkind (Parent (Nam)) = N_Expanded_Name
4728 and then In_Defining_Unit_Name (Parent (Nam))));
4729 end In_Defining_Unit_Name;
4731 -- Start of processing for Copy_Generic_Node
4733 begin
4734 if N = Empty then
4735 return N;
4736 end if;
4738 New_N := New_Copy (N);
4740 if Instantiating then
4741 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
4742 end if;
4744 if not Is_List_Member (N) then
4745 Set_Parent (New_N, Parent_Id);
4746 end if;
4748 -- If defining identifier, then all fields have been copied already
4750 if Nkind (New_N) in N_Entity then
4751 null;
4753 -- Special casing for identifiers and other entity names and operators
4755 elsif Nkind (New_N) = N_Identifier
4756 or else Nkind (New_N) = N_Character_Literal
4757 or else Nkind (New_N) = N_Expanded_Name
4758 or else Nkind (New_N) = N_Operator_Symbol
4759 or else Nkind (New_N) in N_Op
4760 then
4761 if not Instantiating then
4763 -- Link both nodes in order to assign subsequently the
4764 -- entity of the copy to the original node, in case this
4765 -- is a global reference.
4767 Set_Associated_Node (N, New_N);
4769 -- If we are within an instantiation, this is a nested generic
4770 -- that has already been analyzed at the point of definition. We
4771 -- must preserve references that were global to the enclosing
4772 -- parent at that point. Other occurrences, whether global or
4773 -- local to the current generic, must be resolved anew, so we
4774 -- reset the entity in the generic copy. A global reference has
4775 -- a smaller depth than the parent, or else the same depth in
4776 -- case both are distinct compilation units.
4778 -- It is also possible for Current_Instantiated_Parent to be
4779 -- defined, and for this not to be a nested generic, namely
4780 -- if the unit is loaded through Rtsfind. In that case, the
4781 -- entity of New_N is only a link to the associated node, and
4782 -- not a defining occurrence.
4784 -- The entities for parent units in the defining_program_unit
4785 -- of a generic child unit are established when the context of
4786 -- the unit is first analyzed, before the generic copy is made.
4787 -- They are preserved in the copy for use in ASIS queries.
4789 Ent := Entity (New_N);
4791 if No (Current_Instantiated_Parent.Gen_Id) then
4792 if No (Ent)
4793 or else Nkind (Ent) /= N_Defining_Identifier
4794 or else not In_Defining_Unit_Name (N)
4795 then
4796 Set_Associated_Node (New_N, Empty);
4797 end if;
4799 elsif No (Ent)
4800 or else
4801 not (Nkind (Ent) = N_Defining_Identifier
4802 or else
4803 Nkind (Ent) = N_Defining_Character_Literal
4804 or else
4805 Nkind (Ent) = N_Defining_Operator_Symbol)
4806 or else No (Scope (Ent))
4807 or else Scope (Ent) = Current_Instantiated_Parent.Gen_Id
4808 or else (Scope_Depth (Scope (Ent)) >
4809 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
4810 and then
4811 Get_Source_Unit (Ent) =
4812 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
4813 then
4814 Set_Associated_Node (New_N, Empty);
4815 end if;
4817 -- Case of instantiating identifier or some other name or operator
4819 else
4820 -- If the associated node is still defined, the entity in
4821 -- it is global, and must be copied to the instance.
4822 -- If this copy is being made for a body to inline, it is
4823 -- applied to an instantiated tree, and the entity is already
4824 -- present and must be also preserved.
4826 declare
4827 Assoc : constant Node_Id := Get_Associated_Node (N);
4828 begin
4829 if Present (Assoc) then
4830 if Nkind (Assoc) = Nkind (N) then
4831 Set_Entity (New_N, Entity (Assoc));
4832 Check_Private_View (N);
4834 elsif Nkind (Assoc) = N_Function_Call then
4835 Set_Entity (New_N, Entity (Name (Assoc)));
4837 elsif (Nkind (Assoc) = N_Defining_Identifier
4838 or else Nkind (Assoc) = N_Defining_Character_Literal
4839 or else Nkind (Assoc) = N_Defining_Operator_Symbol)
4840 and then Expander_Active
4841 then
4842 -- Inlining case: we are copying a tree that contains
4843 -- global entities, which are preserved in the copy
4844 -- to be used for subsequent inlining.
4846 null;
4848 else
4849 Set_Entity (New_N, Empty);
4850 end if;
4851 end if;
4852 end;
4853 end if;
4855 -- For expanded name, we must copy the Prefix and Selector_Name
4857 if Nkind (N) = N_Expanded_Name then
4858 Set_Prefix
4859 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
4861 Set_Selector_Name (New_N,
4862 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
4864 -- For operators, we must copy the right operand
4866 elsif Nkind (N) in N_Op then
4867 Set_Right_Opnd (New_N,
4868 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
4870 -- And for binary operators, the left operand as well
4872 if Nkind (N) in N_Binary_Op then
4873 Set_Left_Opnd (New_N,
4874 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
4875 end if;
4876 end if;
4878 -- Special casing for stubs
4880 elsif Nkind (N) in N_Body_Stub then
4882 -- In any case, we must copy the specification or defining
4883 -- identifier as appropriate.
4885 if Nkind (N) = N_Subprogram_Body_Stub then
4886 Set_Specification (New_N,
4887 Copy_Generic_Node (Specification (N), New_N, Instantiating));
4889 else
4890 Set_Defining_Identifier (New_N,
4891 Copy_Generic_Node
4892 (Defining_Identifier (N), New_N, Instantiating));
4893 end if;
4895 -- If we are not instantiating, then this is where we load and
4896 -- analyze subunits, i.e. at the point where the stub occurs. A
4897 -- more permissivle system might defer this analysis to the point
4898 -- of instantiation, but this seems to complicated for now.
4900 if not Instantiating then
4901 declare
4902 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
4903 Subunit : Node_Id;
4904 Unum : Unit_Number_Type;
4905 New_Body : Node_Id;
4907 begin
4908 Unum :=
4909 Load_Unit
4910 (Load_Name => Subunit_Name,
4911 Required => False,
4912 Subunit => True,
4913 Error_Node => N);
4915 -- If the proper body is not found, a warning message will
4916 -- be emitted when analyzing the stub, or later at the the
4917 -- point of instantiation. Here we just leave the stub as is.
4919 if Unum = No_Unit then
4920 Subunits_Missing := True;
4921 goto Subunit_Not_Found;
4922 end if;
4924 Subunit := Cunit (Unum);
4926 if Nkind (Unit (Subunit)) /= N_Subunit then
4927 Error_Msg_Sloc := Sloc (N);
4928 Error_Msg_N
4929 ("expected SEPARATE subunit to complete stub at#,"
4930 & " found child unit", Subunit);
4931 goto Subunit_Not_Found;
4932 end if;
4934 -- We must create a generic copy of the subunit, in order
4935 -- to perform semantic analysis on it, and we must replace
4936 -- the stub in the original generic unit with the subunit,
4937 -- in order to preserve non-local references within.
4939 -- Only the proper body needs to be copied. Library_Unit and
4940 -- context clause are simply inherited by the generic copy.
4941 -- Note that the copy (which may be recursive if there are
4942 -- nested subunits) must be done first, before attaching it
4943 -- to the enclosing generic.
4945 New_Body :=
4946 Copy_Generic_Node
4947 (Proper_Body (Unit (Subunit)),
4948 Empty, Instantiating => False);
4950 -- Now place the original proper body in the original
4951 -- generic unit. This is a body, not a compilation unit.
4953 Rewrite (N, Proper_Body (Unit (Subunit)));
4954 Set_Is_Compilation_Unit (Defining_Entity (N), False);
4955 Set_Was_Originally_Stub (N);
4957 -- Finally replace the body of the subunit with its copy,
4958 -- and make this new subunit into the library unit of the
4959 -- generic copy, which does not have stubs any longer.
4961 Set_Proper_Body (Unit (Subunit), New_Body);
4962 Set_Library_Unit (New_N, Subunit);
4963 Inherit_Context (Unit (Subunit), N);
4964 end;
4966 -- If we are instantiating, this must be an error case, since
4967 -- otherwise we would have replaced the stub node by the proper
4968 -- body that corresponds. So just ignore it in the copy (i.e.
4969 -- we have copied it, and that is good enough).
4971 else
4972 null;
4973 end if;
4975 <<Subunit_Not_Found>> null;
4977 -- If the node is a compilation unit, it is the subunit of a stub,
4978 -- which has been loaded already (see code below). In this case,
4979 -- the library unit field of N points to the parent unit (which
4980 -- is a compilation unit) and need not (and cannot!) be copied.
4982 -- When the proper body of the stub is analyzed, thie library_unit
4983 -- link is used to establish the proper context (see sem_ch10).
4985 -- The other fields of a compilation unit are copied as usual
4987 elsif Nkind (N) = N_Compilation_Unit then
4989 -- This code can only be executed when not instantiating, because
4990 -- in the copy made for an instantiation, the compilation unit
4991 -- node has disappeared at the point that a stub is replaced by
4992 -- its proper body.
4994 pragma Assert (not Instantiating);
4996 Set_Context_Items (New_N,
4997 Copy_Generic_List (Context_Items (N), New_N));
4999 Set_Unit (New_N,
5000 Copy_Generic_Node (Unit (N), New_N, False));
5002 Set_First_Inlined_Subprogram (New_N,
5003 Copy_Generic_Node
5004 (First_Inlined_Subprogram (N), New_N, False));
5006 Set_Aux_Decls_Node (New_N,
5007 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
5009 -- For an assignment node, the assignment is known to be semantically
5010 -- legal if we are instantiating the template. This avoids incorrect
5011 -- diagnostics in generated code.
5013 elsif Nkind (N) = N_Assignment_Statement then
5015 -- Copy name and expression fields in usual manner
5017 Set_Name (New_N,
5018 Copy_Generic_Node (Name (N), New_N, Instantiating));
5020 Set_Expression (New_N,
5021 Copy_Generic_Node (Expression (N), New_N, Instantiating));
5023 if Instantiating then
5024 Set_Assignment_OK (Name (New_N), True);
5025 end if;
5027 elsif Nkind (N) = N_Aggregate
5028 or else Nkind (N) = N_Extension_Aggregate
5029 then
5031 if not Instantiating then
5032 Set_Associated_Node (N, New_N);
5034 else
5035 if Present (Get_Associated_Node (N))
5036 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
5037 then
5038 -- In the generic the aggregate has some composite type. If at
5039 -- the point of instantiation the type has a private view,
5040 -- install the full view (and that of its ancestors, if any).
5042 declare
5043 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
5044 Rt : Entity_Id;
5046 begin
5047 if Present (T)
5048 and then Is_Private_Type (T)
5049 then
5050 Switch_View (T);
5051 end if;
5053 if Present (T)
5054 and then Is_Tagged_Type (T)
5055 and then Is_Derived_Type (T)
5056 then
5057 Rt := Root_Type (T);
5059 loop
5060 T := Etype (T);
5062 if Is_Private_Type (T) then
5063 Switch_View (T);
5064 end if;
5066 exit when T = Rt;
5067 end loop;
5068 end if;
5069 end;
5070 end if;
5071 end if;
5073 -- Do not copy the associated node, which points to
5074 -- the generic copy of the aggregate.
5076 declare
5077 use Atree.Unchecked_Access;
5078 -- This code section is part of the implementation of an untyped
5079 -- tree traversal, so it needs direct access to node fields.
5081 begin
5082 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
5083 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
5084 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
5085 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
5086 end;
5088 -- Allocators do not have an identifier denoting the access type,
5089 -- so we must locate it through the expression to check whether
5090 -- the views are consistent.
5092 elsif Nkind (N) = N_Allocator
5093 and then Nkind (Expression (N)) = N_Qualified_Expression
5094 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
5095 and then Instantiating
5096 then
5097 declare
5098 T : constant Node_Id :=
5099 Get_Associated_Node (Subtype_Mark (Expression (N)));
5100 Acc_T : Entity_Id;
5102 begin
5103 if Present (T) then
5104 -- Retrieve the allocator node in the generic copy.
5106 Acc_T := Etype (Parent (Parent (T)));
5107 if Present (Acc_T)
5108 and then Is_Private_Type (Acc_T)
5109 then
5110 Switch_View (Acc_T);
5111 end if;
5112 end if;
5114 Copy_Descendants;
5115 end;
5117 -- For a proper body, we must catch the case of a proper body that
5118 -- replaces a stub. This represents the point at which a separate
5119 -- compilation unit, and hence template file, may be referenced, so
5120 -- we must make a new source instantiation entry for the template
5121 -- of the subunit, and ensure that all nodes in the subunit are
5122 -- adjusted using this new source instantiation entry.
5124 elsif Nkind (N) in N_Proper_Body then
5125 declare
5126 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
5128 begin
5129 if Instantiating and then Was_Originally_Stub (N) then
5130 Create_Instantiation_Source
5131 (Instantiation_Node,
5132 Defining_Entity (N),
5133 False,
5134 S_Adjustment);
5135 end if;
5137 -- Now copy the fields of the proper body, using the new
5138 -- adjustment factor if one was needed as per test above.
5140 Copy_Descendants;
5142 -- Restore the original adjustment factor in case changed
5144 S_Adjustment := Save_Adjustment;
5145 end;
5147 -- Don't copy Ident or Comment pragmas, since the comment belongs
5148 -- to the generic unit, not to the instantiating unit.
5150 elsif Nkind (N) = N_Pragma
5151 and then Instantiating
5152 then
5153 declare
5154 Prag_Id : constant Pragma_Id := Get_Pragma_Id (Chars (N));
5156 begin
5157 if Prag_Id = Pragma_Ident
5158 or else Prag_Id = Pragma_Comment
5159 then
5160 New_N := Make_Null_Statement (Sloc (N));
5162 else
5163 Copy_Descendants;
5164 end if;
5165 end;
5167 elsif Nkind (N) = N_Integer_Literal
5168 or else Nkind (N) = N_Real_Literal
5169 then
5170 -- No descendant fields need traversing
5172 null;
5174 -- For the remaining nodes, copy recursively their descendants
5176 else
5177 Copy_Descendants;
5179 if Instantiating
5180 and then Nkind (N) = N_Subprogram_Body
5181 then
5182 Set_Generic_Parent (Specification (New_N), N);
5183 end if;
5184 end if;
5186 return New_N;
5187 end Copy_Generic_Node;
5189 ----------------------------
5190 -- Denotes_Formal_Package --
5191 ----------------------------
5193 function Denotes_Formal_Package
5194 (Pack : Entity_Id;
5195 On_Exit : Boolean := False) return Boolean
5197 Par : Entity_Id;
5198 Scop : constant Entity_Id := Scope (Pack);
5199 E : Entity_Id;
5201 begin
5202 if On_Exit then
5203 Par :=
5204 Instance_Envs.Table
5205 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
5206 else
5207 Par := Current_Instantiated_Parent.Act_Id;
5208 end if;
5210 if Ekind (Scop) = E_Generic_Package
5211 or else Nkind (Unit_Declaration_Node (Scop)) =
5212 N_Generic_Subprogram_Declaration
5213 then
5214 return True;
5216 elsif Nkind (Parent (Pack)) = N_Formal_Package_Declaration then
5217 return True;
5219 elsif No (Par) then
5220 return False;
5222 else
5223 -- Check whether this package is associated with a formal
5224 -- package of the enclosing instantiation. Iterate over the
5225 -- list of renamings.
5227 E := First_Entity (Par);
5228 while Present (E) loop
5229 if Ekind (E) /= E_Package
5230 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
5231 then
5232 null;
5233 elsif Renamed_Object (E) = Par then
5234 return False;
5236 elsif Renamed_Object (E) = Pack then
5237 return True;
5238 end if;
5240 Next_Entity (E);
5241 end loop;
5243 return False;
5244 end if;
5245 end Denotes_Formal_Package;
5247 -----------------
5248 -- End_Generic --
5249 -----------------
5251 procedure End_Generic is
5252 begin
5253 -- ??? More things could be factored out in this
5254 -- routine. Should probably be done at a later stage.
5256 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
5257 Generic_Flags.Decrement_Last;
5259 Expander_Mode_Restore;
5260 end End_Generic;
5262 ----------------------
5263 -- Find_Actual_Type --
5264 ----------------------
5266 function Find_Actual_Type
5267 (Typ : Entity_Id;
5268 Gen_Scope : Entity_Id) return Entity_Id
5270 T : Entity_Id;
5272 begin
5273 if not Is_Child_Unit (Gen_Scope) then
5274 return Get_Instance_Of (Typ);
5276 elsif not Is_Generic_Type (Typ)
5277 or else Scope (Typ) = Gen_Scope
5278 then
5279 return Get_Instance_Of (Typ);
5281 else
5282 T := Current_Entity (Typ);
5283 while Present (T) loop
5284 if In_Open_Scopes (Scope (T)) then
5285 return T;
5286 end if;
5288 T := Homonym (T);
5289 end loop;
5291 return Typ;
5292 end if;
5293 end Find_Actual_Type;
5295 ----------------------------
5296 -- Freeze_Subprogram_Body --
5297 ----------------------------
5299 procedure Freeze_Subprogram_Body
5300 (Inst_Node : Node_Id;
5301 Gen_Body : Node_Id;
5302 Pack_Id : Entity_Id)
5304 F_Node : Node_Id;
5305 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
5306 Par : constant Entity_Id := Scope (Gen_Unit);
5307 Enc_G : Entity_Id;
5308 Enc_I : Node_Id;
5309 E_G_Id : Entity_Id;
5311 function Earlier (N1, N2 : Node_Id) return Boolean;
5312 -- Yields True if N1 and N2 appear in the same compilation unit,
5313 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
5314 -- traversal of the tree for the unit.
5316 function Enclosing_Body (N : Node_Id) return Node_Id;
5317 -- Find innermost package body that encloses the given node, and which
5318 -- is not a compilation unit. Freeze nodes for the instance, or for its
5319 -- enclosing body, may be inserted after the enclosing_body of the
5320 -- generic unit.
5322 function Package_Freeze_Node (B : Node_Id) return Node_Id;
5323 -- Find entity for given package body, and locate or create a freeze
5324 -- node for it.
5326 function True_Parent (N : Node_Id) return Node_Id;
5327 -- For a subunit, return parent of corresponding stub.
5329 -------------
5330 -- Earlier --
5331 -------------
5333 function Earlier (N1, N2 : Node_Id) return Boolean is
5334 D1 : Integer := 0;
5335 D2 : Integer := 0;
5336 P1 : Node_Id := N1;
5337 P2 : Node_Id := N2;
5339 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
5340 -- Find distance from given node to enclosing compilation unit.
5342 ----------------
5343 -- Find_Depth --
5344 ----------------
5346 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
5347 begin
5348 while Present (P)
5349 and then Nkind (P) /= N_Compilation_Unit
5350 loop
5351 P := True_Parent (P);
5352 D := D + 1;
5353 end loop;
5354 end Find_Depth;
5356 -- Start of procesing for Earlier
5358 begin
5359 Find_Depth (P1, D1);
5360 Find_Depth (P2, D2);
5362 if P1 /= P2 then
5363 return False;
5364 else
5365 P1 := N1;
5366 P2 := N2;
5367 end if;
5369 while D1 > D2 loop
5370 P1 := True_Parent (P1);
5371 D1 := D1 - 1;
5372 end loop;
5374 while D2 > D1 loop
5375 P2 := True_Parent (P2);
5376 D2 := D2 - 1;
5377 end loop;
5379 -- At this point P1 and P2 are at the same distance from the root.
5380 -- We examine their parents until we find a common declarative
5381 -- list, at which point we can establish their relative placement
5382 -- by comparing their ultimate slocs. If we reach the root,
5383 -- N1 and N2 do not descend from the same declarative list (e.g.
5384 -- one is nested in the declarative part and the other is in a block
5385 -- in the statement part) and the earlier one is already frozen.
5387 while not Is_List_Member (P1)
5388 or else not Is_List_Member (P2)
5389 or else List_Containing (P1) /= List_Containing (P2)
5390 loop
5391 P1 := True_Parent (P1);
5392 P2 := True_Parent (P2);
5394 if Nkind (Parent (P1)) = N_Subunit then
5395 P1 := Corresponding_Stub (Parent (P1));
5396 end if;
5398 if Nkind (Parent (P2)) = N_Subunit then
5399 P2 := Corresponding_Stub (Parent (P2));
5400 end if;
5402 if P1 = P2 then
5403 return False;
5404 end if;
5405 end loop;
5407 return
5408 Top_Level_Location (Sloc (P1)) < Top_Level_Location (Sloc (P2));
5409 end Earlier;
5411 --------------------
5412 -- Enclosing_Body --
5413 --------------------
5415 function Enclosing_Body (N : Node_Id) return Node_Id is
5416 P : Node_Id := Parent (N);
5418 begin
5419 while Present (P)
5420 and then Nkind (Parent (P)) /= N_Compilation_Unit
5421 loop
5422 if Nkind (P) = N_Package_Body then
5424 if Nkind (Parent (P)) = N_Subunit then
5425 return Corresponding_Stub (Parent (P));
5426 else
5427 return P;
5428 end if;
5429 end if;
5431 P := True_Parent (P);
5432 end loop;
5434 return Empty;
5435 end Enclosing_Body;
5437 -------------------------
5438 -- Package_Freeze_Node --
5439 -------------------------
5441 function Package_Freeze_Node (B : Node_Id) return Node_Id is
5442 Id : Entity_Id;
5444 begin
5445 if Nkind (B) = N_Package_Body then
5446 Id := Corresponding_Spec (B);
5448 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
5449 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
5450 end if;
5452 Ensure_Freeze_Node (Id);
5453 return Freeze_Node (Id);
5454 end Package_Freeze_Node;
5456 -----------------
5457 -- True_Parent --
5458 -----------------
5460 function True_Parent (N : Node_Id) return Node_Id is
5461 begin
5462 if Nkind (Parent (N)) = N_Subunit then
5463 return Parent (Corresponding_Stub (Parent (N)));
5464 else
5465 return Parent (N);
5466 end if;
5467 end True_Parent;
5469 -- Start of processing of Freeze_Subprogram_Body
5471 begin
5472 -- If the instance and the generic body appear within the same
5473 -- unit, and the instance preceeds the generic, the freeze node for
5474 -- the instance must appear after that of the generic. If the generic
5475 -- is nested within another instance I2, then current instance must
5476 -- be frozen after I2. In both cases, the freeze nodes are those of
5477 -- enclosing packages. Otherwise, the freeze node is placed at the end
5478 -- of the current declarative part.
5480 Enc_G := Enclosing_Body (Gen_Body);
5481 Enc_I := Enclosing_Body (Inst_Node);
5482 Ensure_Freeze_Node (Pack_Id);
5483 F_Node := Freeze_Node (Pack_Id);
5485 if Is_Generic_Instance (Par)
5486 and then Present (Freeze_Node (Par))
5487 and then
5488 In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
5489 then
5490 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
5492 -- The parent was a premature instantiation. Insert freeze
5493 -- node at the end the current declarative part.
5495 Insert_After_Last_Decl (Inst_Node, F_Node);
5497 else
5498 Insert_After (Freeze_Node (Par), F_Node);
5499 end if;
5501 -- The body enclosing the instance should be frozen after the body
5502 -- that includes the generic, because the body of the instance may
5503 -- make references to entities therein. If the two are not in the
5504 -- same declarative part, or if the one enclosing the instance is
5505 -- frozen already, freeze the instance at the end of the current
5506 -- declarative part.
5508 elsif Is_Generic_Instance (Par)
5509 and then Present (Freeze_Node (Par))
5510 and then Present (Enc_I)
5511 then
5512 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
5513 or else
5514 (Nkind (Enc_I) = N_Package_Body
5515 and then
5516 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
5517 then
5518 -- The enclosing package may contain several instances. Rather
5519 -- than computing the earliest point at which to insert its
5520 -- freeze node, we place it at the end of the declarative part
5521 -- of the parent of the generic.
5523 Insert_After_Last_Decl
5524 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
5525 end if;
5527 Insert_After_Last_Decl (Inst_Node, F_Node);
5529 elsif Present (Enc_G)
5530 and then Present (Enc_I)
5531 and then Enc_G /= Enc_I
5532 and then Earlier (Inst_Node, Gen_Body)
5533 then
5534 if Nkind (Enc_G) = N_Package_Body then
5535 E_G_Id := Corresponding_Spec (Enc_G);
5536 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
5537 E_G_Id :=
5538 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
5539 end if;
5541 -- Freeze package that encloses instance, and place node after
5542 -- package that encloses generic. If enclosing package is already
5543 -- frozen we have to assume it is at the proper place. This may
5544 -- be a potential ABE that requires dynamic checking.
5546 Insert_After_Last_Decl (Enc_G, Package_Freeze_Node (Enc_I));
5548 -- Freeze enclosing subunit before instance
5550 Ensure_Freeze_Node (E_G_Id);
5552 if not Is_List_Member (Freeze_Node (E_G_Id)) then
5553 Insert_After (Enc_G, Freeze_Node (E_G_Id));
5554 end if;
5556 Insert_After_Last_Decl (Inst_Node, F_Node);
5558 else
5559 -- If none of the above, insert freeze node at the end of the
5560 -- current declarative part.
5562 Insert_After_Last_Decl (Inst_Node, F_Node);
5563 end if;
5564 end Freeze_Subprogram_Body;
5566 ----------------
5567 -- Get_Gen_Id --
5568 ----------------
5570 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
5571 begin
5572 return Generic_Renamings.Table (E).Gen_Id;
5573 end Get_Gen_Id;
5575 ---------------------
5576 -- Get_Instance_Of --
5577 ---------------------
5579 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
5580 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
5582 begin
5583 if Res /= Assoc_Null then
5584 return Generic_Renamings.Table (Res).Act_Id;
5585 else
5586 -- On exit, entity is not instantiated: not a generic parameter,
5587 -- or else parameter of an inner generic unit.
5589 return A;
5590 end if;
5591 end Get_Instance_Of;
5593 ------------------------------------
5594 -- Get_Package_Instantiation_Node --
5595 ------------------------------------
5597 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
5598 Decl : Node_Id := Unit_Declaration_Node (A);
5599 Inst : Node_Id;
5601 begin
5602 -- If the instantiation is a compilation unit that does not need a
5603 -- body then the instantiation node has been rewritten as a package
5604 -- declaration for the instance, and we return the original node.
5606 -- If it is a compilation unit and the instance node has not been
5607 -- rewritten, then it is still the unit of the compilation. Finally,
5608 -- if a body is present, this is a parent of the main unit whose body
5609 -- has been compiled for inlining purposes, and the instantiation node
5610 -- has been rewritten with the instance body.
5612 -- Otherwise the instantiation node appears after the declaration.
5613 -- If the entity is a formal package, the declaration may have been
5614 -- rewritten as a generic declaration (in the case of a formal with a
5615 -- box) or left as a formal package declaration if it has actuals, and
5616 -- is found with a forward search.
5618 if Nkind (Parent (Decl)) = N_Compilation_Unit then
5619 if Nkind (Decl) = N_Package_Declaration
5620 and then Present (Corresponding_Body (Decl))
5621 then
5622 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
5623 end if;
5625 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
5626 return Original_Node (Decl);
5627 else
5628 return Unit (Parent (Decl));
5629 end if;
5631 elsif Nkind (Decl) = N_Generic_Package_Declaration
5632 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
5633 then
5634 return Original_Node (Decl);
5636 else
5637 Inst := Next (Decl);
5638 while Nkind (Inst) /= N_Package_Instantiation
5639 and then Nkind (Inst) /= N_Formal_Package_Declaration
5640 loop
5641 Next (Inst);
5642 end loop;
5644 return Inst;
5645 end if;
5646 end Get_Package_Instantiation_Node;
5648 ------------------------
5649 -- Has_Been_Exchanged --
5650 ------------------------
5652 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
5653 Next : Elmt_Id := First_Elmt (Exchanged_Views);
5655 begin
5656 while Present (Next) loop
5657 if Full_View (Node (Next)) = E then
5658 return True;
5659 end if;
5661 Next_Elmt (Next);
5662 end loop;
5664 return False;
5665 end Has_Been_Exchanged;
5667 ----------
5668 -- Hash --
5669 ----------
5671 function Hash (F : Entity_Id) return HTable_Range is
5672 begin
5673 return HTable_Range (F mod HTable_Size);
5674 end Hash;
5676 ------------------------
5677 -- Hide_Current_Scope --
5678 ------------------------
5680 procedure Hide_Current_Scope is
5681 C : constant Entity_Id := Current_Scope;
5682 E : Entity_Id;
5684 begin
5685 Set_Is_Hidden_Open_Scope (C);
5686 E := First_Entity (C);
5688 while Present (E) loop
5689 if Is_Immediately_Visible (E) then
5690 Set_Is_Immediately_Visible (E, False);
5691 Append_Elmt (E, Hidden_Entities);
5692 end if;
5694 Next_Entity (E);
5695 end loop;
5697 -- Make the scope name invisible as well. This is necessary, but
5698 -- might conflict with calls to Rtsfind later on, in case the scope
5699 -- is a predefined one. There is no clean solution to this problem, so
5700 -- for now we depend on the user not redefining Standard itself in one
5701 -- of the parent units.
5703 if Is_Immediately_Visible (C)
5704 and then C /= Standard_Standard
5705 then
5706 Set_Is_Immediately_Visible (C, False);
5707 Append_Elmt (C, Hidden_Entities);
5708 end if;
5710 end Hide_Current_Scope;
5712 --------------
5713 -- Init_Env --
5714 --------------
5716 procedure Init_Env is
5717 Saved : Instance_Env;
5719 begin
5720 Saved.Ada_Version := Ada_Version;
5721 Saved.Instantiated_Parent := Current_Instantiated_Parent;
5722 Saved.Exchanged_Views := Exchanged_Views;
5723 Saved.Hidden_Entities := Hidden_Entities;
5724 Saved.Current_Sem_Unit := Current_Sem_Unit;
5725 Instance_Envs.Increment_Last;
5726 Instance_Envs.Table (Instance_Envs.Last) := Saved;
5728 Exchanged_Views := New_Elmt_List;
5729 Hidden_Entities := New_Elmt_List;
5731 -- Make dummy entry for Instantiated parent. If generic unit is
5732 -- legal, this is set properly in Set_Instance_Env.
5734 Current_Instantiated_Parent :=
5735 (Current_Scope, Current_Scope, Assoc_Null);
5736 end Init_Env;
5738 ------------------------------
5739 -- In_Same_Declarative_Part --
5740 ------------------------------
5742 function In_Same_Declarative_Part
5743 (F_Node : Node_Id;
5744 Inst : Node_Id) return Boolean
5746 Decls : constant Node_Id := Parent (F_Node);
5747 Nod : Node_Id := Parent (Inst);
5749 begin
5750 while Present (Nod) loop
5751 if Nod = Decls then
5752 return True;
5754 elsif Nkind (Nod) = N_Subprogram_Body
5755 or else Nkind (Nod) = N_Package_Body
5756 or else Nkind (Nod) = N_Task_Body
5757 or else Nkind (Nod) = N_Protected_Body
5758 or else Nkind (Nod) = N_Block_Statement
5759 then
5760 return False;
5762 elsif Nkind (Nod) = N_Subunit then
5763 Nod := Corresponding_Stub (Nod);
5765 elsif Nkind (Nod) = N_Compilation_Unit then
5766 return False;
5767 else
5768 Nod := Parent (Nod);
5769 end if;
5770 end loop;
5772 return False;
5773 end In_Same_Declarative_Part;
5775 ---------------------
5776 -- Inherit_Context --
5777 ---------------------
5779 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
5780 Current_Context : List_Id;
5781 Current_Unit : Node_Id;
5782 Item : Node_Id;
5783 New_I : Node_Id;
5785 begin
5786 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
5788 -- The inherited context is attached to the enclosing compilation
5789 -- unit. This is either the main unit, or the declaration for the
5790 -- main unit (in case the instantation appears within the package
5791 -- declaration and the main unit is its body).
5793 Current_Unit := Parent (Inst);
5794 while Present (Current_Unit)
5795 and then Nkind (Current_Unit) /= N_Compilation_Unit
5796 loop
5797 Current_Unit := Parent (Current_Unit);
5798 end loop;
5800 Current_Context := Context_Items (Current_Unit);
5802 Item := First (Context_Items (Parent (Gen_Decl)));
5803 while Present (Item) loop
5804 if Nkind (Item) = N_With_Clause then
5805 New_I := New_Copy (Item);
5806 Set_Implicit_With (New_I, True);
5807 Append (New_I, Current_Context);
5808 end if;
5810 Next (Item);
5811 end loop;
5812 end if;
5813 end Inherit_Context;
5815 ----------------
5816 -- Initialize --
5817 ----------------
5819 procedure Initialize is
5820 begin
5821 Generic_Renamings.Init;
5822 Instance_Envs.Init;
5823 Generic_Flags.Init;
5824 Generic_Renamings_HTable.Reset;
5825 Circularity_Detected := False;
5826 Exchanged_Views := No_Elist;
5827 Hidden_Entities := No_Elist;
5828 end Initialize;
5830 ----------------------------
5831 -- Insert_After_Last_Decl --
5832 ----------------------------
5834 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id) is
5835 L : List_Id := List_Containing (N);
5836 P : constant Node_Id := Parent (L);
5838 begin
5839 if not Is_List_Member (F_Node) then
5840 if Nkind (P) = N_Package_Specification
5841 and then L = Visible_Declarations (P)
5842 and then Present (Private_Declarations (P))
5843 and then not Is_Empty_List (Private_Declarations (P))
5844 then
5845 L := Private_Declarations (P);
5846 end if;
5848 Insert_After (Last (L), F_Node);
5849 end if;
5850 end Insert_After_Last_Decl;
5852 ------------------
5853 -- Install_Body --
5854 ------------------
5856 procedure Install_Body
5857 (Act_Body : Node_Id;
5858 N : Node_Id;
5859 Gen_Body : Node_Id;
5860 Gen_Decl : Node_Id)
5862 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
5863 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
5864 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
5865 Par : constant Entity_Id := Scope (Gen_Id);
5866 Gen_Unit : constant Node_Id :=
5867 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
5868 Orig_Body : Node_Id := Gen_Body;
5869 F_Node : Node_Id;
5870 Body_Unit : Node_Id;
5872 Must_Delay : Boolean;
5874 function Enclosing_Subp (Id : Entity_Id) return Entity_Id;
5875 -- Find subprogram (if any) that encloses instance and/or generic body.
5877 function True_Sloc (N : Node_Id) return Source_Ptr;
5878 -- If the instance is nested inside a generic unit, the Sloc of the
5879 -- instance indicates the place of the original definition, not the
5880 -- point of the current enclosing instance. Pending a better usage of
5881 -- Slocs to indicate instantiation places, we determine the place of
5882 -- origin of a node by finding the maximum sloc of any ancestor node.
5883 -- Why is this not equivalent fo Top_Level_Location ???
5885 --------------------
5886 -- Enclosing_Subp --
5887 --------------------
5889 function Enclosing_Subp (Id : Entity_Id) return Entity_Id is
5890 Scop : Entity_Id := Scope (Id);
5892 begin
5893 while Scop /= Standard_Standard
5894 and then not Is_Overloadable (Scop)
5895 loop
5896 Scop := Scope (Scop);
5897 end loop;
5899 return Scop;
5900 end Enclosing_Subp;
5902 ---------------
5903 -- True_Sloc --
5904 ---------------
5906 function True_Sloc (N : Node_Id) return Source_Ptr is
5907 Res : Source_Ptr;
5908 N1 : Node_Id;
5910 begin
5911 Res := Sloc (N);
5912 N1 := N;
5913 while Present (N1) and then N1 /= Act_Unit loop
5914 if Sloc (N1) > Res then
5915 Res := Sloc (N1);
5916 end if;
5918 N1 := Parent (N1);
5919 end loop;
5921 return Res;
5922 end True_Sloc;
5924 -- Start of processing for Install_Body
5926 begin
5927 -- If the body is a subunit, the freeze point is the corresponding
5928 -- stub in the current compilation, not the subunit itself.
5930 if Nkind (Parent (Gen_Body)) = N_Subunit then
5931 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
5932 else
5933 Orig_Body := Gen_Body;
5934 end if;
5936 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
5938 -- If the instantiation and the generic definition appear in the
5939 -- same package declaration, this is an early instantiation.
5940 -- If they appear in the same declarative part, it is an early
5941 -- instantiation only if the generic body appears textually later,
5942 -- and the generic body is also in the main unit.
5944 -- If instance is nested within a subprogram, and the generic body is
5945 -- not, the instance is delayed because the enclosing body is. If
5946 -- instance and body are within the same scope, or the same sub-
5947 -- program body, indicate explicitly that the instance is delayed.
5949 Must_Delay :=
5950 (Gen_Unit = Act_Unit
5951 and then ((Nkind (Gen_Unit) = N_Package_Declaration)
5952 or else Nkind (Gen_Unit) = N_Generic_Package_Declaration
5953 or else (Gen_Unit = Body_Unit
5954 and then True_Sloc (N) < Sloc (Orig_Body)))
5955 and then Is_In_Main_Unit (Gen_Unit)
5956 and then (Scope (Act_Id) = Scope (Gen_Id)
5957 or else
5958 Enclosing_Subp (Act_Id) = Enclosing_Subp (Gen_Id)));
5960 -- If this is an early instantiation, the freeze node is placed after
5961 -- the generic body. Otherwise, if the generic appears in an instance,
5962 -- we cannot freeze the current instance until the outer one is frozen.
5963 -- This is only relevant if the current instance is nested within some
5964 -- inner scope not itself within the outer instance. If this scope is
5965 -- a package body in the same declarative part as the outer instance,
5966 -- then that body needs to be frozen after the outer instance. Finally,
5967 -- if no delay is needed, we place the freeze node at the end of the
5968 -- current declarative part.
5970 if Expander_Active then
5971 Ensure_Freeze_Node (Act_Id);
5972 F_Node := Freeze_Node (Act_Id);
5974 if Must_Delay then
5975 Insert_After (Orig_Body, F_Node);
5977 elsif Is_Generic_Instance (Par)
5978 and then Present (Freeze_Node (Par))
5979 and then Scope (Act_Id) /= Par
5980 then
5981 -- Freeze instance of inner generic after instance of enclosing
5982 -- generic.
5984 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
5985 Insert_After (Freeze_Node (Par), F_Node);
5987 -- Freeze package enclosing instance of inner generic after
5988 -- instance of enclosing generic.
5990 elsif Nkind (Parent (N)) = N_Package_Body
5991 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
5992 then
5994 declare
5995 Enclosing : constant Entity_Id :=
5996 Corresponding_Spec (Parent (N));
5998 begin
5999 Insert_After_Last_Decl (N, F_Node);
6000 Ensure_Freeze_Node (Enclosing);
6002 if not Is_List_Member (Freeze_Node (Enclosing)) then
6003 Insert_After (Freeze_Node (Par), Freeze_Node (Enclosing));
6004 end if;
6005 end;
6007 else
6008 Insert_After_Last_Decl (N, F_Node);
6009 end if;
6011 else
6012 Insert_After_Last_Decl (N, F_Node);
6013 end if;
6014 end if;
6016 Set_Is_Frozen (Act_Id);
6017 Insert_Before (N, Act_Body);
6018 Mark_Rewrite_Insertion (Act_Body);
6019 end Install_Body;
6021 --------------------
6022 -- Install_Parent --
6023 --------------------
6025 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
6026 Ancestors : constant Elist_Id := New_Elmt_List;
6027 S : constant Entity_Id := Current_Scope;
6028 Inst_Par : Entity_Id;
6029 First_Par : Entity_Id;
6030 Inst_Node : Node_Id;
6031 Gen_Par : Entity_Id;
6032 First_Gen : Entity_Id;
6033 Elmt : Elmt_Id;
6035 procedure Install_Formal_Packages (Par : Entity_Id);
6036 -- If any of the formals of the parent are formal packages with box,
6037 -- their formal parts are visible in the parent and thus in the child
6038 -- unit as well. Analogous to what is done in Check_Generic_Actuals
6039 -- for the unit itself.
6041 procedure Install_Noninstance_Specs (Par : Entity_Id);
6042 -- Install the scopes of noninstance parent units ending with Par.
6044 procedure Install_Spec (Par : Entity_Id);
6045 -- The child unit is within the declarative part of the parent, so
6046 -- the declarations within the parent are immediately visible.
6048 -----------------------------
6049 -- Install_Formal_Packages --
6050 -----------------------------
6052 procedure Install_Formal_Packages (Par : Entity_Id) is
6053 E : Entity_Id;
6055 begin
6056 E := First_Entity (Par);
6058 while Present (E) loop
6060 if Ekind (E) = E_Package
6061 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
6062 then
6063 -- If this is the renaming for the parent instance, done.
6065 if Renamed_Object (E) = Par then
6066 exit;
6068 -- The visibility of a formal of an enclosing generic is
6069 -- already correct.
6071 elsif Denotes_Formal_Package (E) then
6072 null;
6074 elsif Present (Associated_Formal_Package (E))
6075 and then Box_Present (Parent (Associated_Formal_Package (E)))
6076 then
6077 Check_Generic_Actuals (Renamed_Object (E), True);
6078 Set_Is_Hidden (E, False);
6079 end if;
6080 end if;
6082 Next_Entity (E);
6083 end loop;
6084 end Install_Formal_Packages;
6086 -------------------------------
6087 -- Install_Noninstance_Specs --
6088 -------------------------------
6090 procedure Install_Noninstance_Specs (Par : Entity_Id) is
6091 begin
6092 if Present (Par)
6093 and then Par /= Standard_Standard
6094 and then not In_Open_Scopes (Par)
6095 then
6096 Install_Noninstance_Specs (Scope (Par));
6097 Install_Spec (Par);
6098 end if;
6099 end Install_Noninstance_Specs;
6101 ------------------
6102 -- Install_Spec --
6103 ------------------
6105 procedure Install_Spec (Par : Entity_Id) is
6106 Spec : constant Node_Id :=
6107 Specification (Unit_Declaration_Node (Par));
6109 begin
6110 New_Scope (Par);
6111 Set_Is_Immediately_Visible (Par);
6112 Install_Visible_Declarations (Par);
6113 Install_Private_Declarations (Par);
6114 Set_Use (Visible_Declarations (Spec));
6115 Set_Use (Private_Declarations (Spec));
6116 end Install_Spec;
6118 -- Start of processing for Install_Parent
6120 begin
6121 -- We need to install the parent instance to compile the instantiation
6122 -- of the child, but the child instance must appear in the current
6123 -- scope. Given that we cannot place the parent above the current
6124 -- scope in the scope stack, we duplicate the current scope and unstack
6125 -- both after the instantiation is complete.
6127 -- If the parent is itself the instantiation of a child unit, we must
6128 -- also stack the instantiation of its parent, and so on. Each such
6129 -- ancestor is the prefix of the name in a prior instantiation.
6131 -- If this is a nested instance, the parent unit itself resolves to
6132 -- a renaming of the parent instance, whose declaration we need.
6134 -- Finally, the parent may be a generic (not an instance) when the
6135 -- child unit appears as a formal package.
6137 Inst_Par := P;
6139 if Present (Renamed_Entity (Inst_Par)) then
6140 Inst_Par := Renamed_Entity (Inst_Par);
6141 end if;
6143 First_Par := Inst_Par;
6145 Gen_Par :=
6146 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
6148 First_Gen := Gen_Par;
6150 while Present (Gen_Par)
6151 and then Is_Child_Unit (Gen_Par)
6152 loop
6153 -- Load grandparent instance as well
6155 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
6157 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
6158 Inst_Par := Entity (Prefix (Name (Inst_Node)));
6160 if Present (Renamed_Entity (Inst_Par)) then
6161 Inst_Par := Renamed_Entity (Inst_Par);
6162 end if;
6164 Gen_Par :=
6165 Generic_Parent
6166 (Specification (Unit_Declaration_Node (Inst_Par)));
6168 if Present (Gen_Par) then
6169 Prepend_Elmt (Inst_Par, Ancestors);
6171 else
6172 -- Parent is not the name of an instantiation
6174 Install_Noninstance_Specs (Inst_Par);
6176 exit;
6177 end if;
6179 else
6180 -- Previous error
6182 exit;
6183 end if;
6184 end loop;
6186 if Present (First_Gen) then
6187 Append_Elmt (First_Par, Ancestors);
6189 else
6190 Install_Noninstance_Specs (First_Par);
6191 end if;
6193 if not Is_Empty_Elmt_List (Ancestors) then
6194 Elmt := First_Elmt (Ancestors);
6196 while Present (Elmt) loop
6197 Install_Spec (Node (Elmt));
6198 Install_Formal_Packages (Node (Elmt));
6200 Next_Elmt (Elmt);
6201 end loop;
6202 end if;
6204 if not In_Body then
6205 New_Scope (S);
6206 end if;
6207 end Install_Parent;
6209 --------------------------------
6210 -- Instantiate_Formal_Package --
6211 --------------------------------
6213 function Instantiate_Formal_Package
6214 (Formal : Node_Id;
6215 Actual : Node_Id;
6216 Analyzed_Formal : Node_Id) return List_Id
6218 Loc : constant Source_Ptr := Sloc (Actual);
6219 Actual_Pack : Entity_Id;
6220 Formal_Pack : Entity_Id;
6221 Gen_Parent : Entity_Id;
6222 Decls : List_Id;
6223 Nod : Node_Id;
6224 Parent_Spec : Node_Id;
6226 procedure Find_Matching_Actual
6227 (F : Node_Id;
6228 Act : in out Entity_Id);
6229 -- We need to associate each formal entity in the formal package
6230 -- with the corresponding entity in the actual package. The actual
6231 -- package has been analyzed and possibly expanded, and as a result
6232 -- there is no one-to-one correspondence between the two lists (for
6233 -- example, the actual may include subtypes, itypes, and inherited
6234 -- primitive operations, interspersed among the renaming declarations
6235 -- for the actuals) . We retrieve the corresponding actual by name
6236 -- because each actual has the same name as the formal, and they do
6237 -- appear in the same order.
6239 function Formal_Entity
6240 (F : Node_Id;
6241 Act_Ent : Entity_Id) return Entity_Id;
6242 -- Returns the entity associated with the given formal F. In the
6243 -- case where F is a formal package, this function will iterate
6244 -- through all of F's formals and enter map associations from the
6245 -- actuals occurring in the formal package's corresponding actual
6246 -- package (obtained via Act_Ent) to the formal package's formal
6247 -- parameters. This function is called recursively for arbitrary
6248 -- levels of formal packages.
6250 function Is_Instance_Of
6251 (Act_Spec : Entity_Id;
6252 Gen_Anc : Entity_Id) return Boolean;
6253 -- The actual can be an instantiation of a generic within another
6254 -- instance, in which case there is no direct link from it to the
6255 -- original generic ancestor. In that case, we recognize that the
6256 -- ultimate ancestor is the same by examining names and scopes.
6258 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id);
6259 -- Within the generic part, entities in the formal package are
6260 -- visible. To validate subsequent type declarations, indicate
6261 -- the correspondence betwen the entities in the analyzed formal,
6262 -- and the entities in the actual package. There are three packages
6263 -- involved in the instantiation of a formal package: the parent
6264 -- generic P1 which appears in the generic declaration, the fake
6265 -- instantiation P2 which appears in the analyzed generic, and whose
6266 -- visible entities may be used in subsequent formals, and the actual
6267 -- P3 in the instance. To validate subsequent formals, me indicate
6268 -- that the entities in P2 are mapped into those of P3. The mapping of
6269 -- entities has to be done recursively for nested packages.
6271 procedure Process_Nested_Formal (Formal : Entity_Id);
6272 -- If the current formal is declared with a box, its own formals are
6273 -- visible in the instance, as they were in the generic, and their
6274 -- Hidden flag must be reset. If some of these formals are themselves
6275 -- packages declared with a box, the processing must be recursive.
6277 --------------------------
6278 -- Find_Matching_Actual --
6279 --------------------------
6281 procedure Find_Matching_Actual
6282 (F : Node_Id;
6283 Act : in out Entity_Id)
6285 Formal_Ent : Entity_Id;
6287 begin
6288 case Nkind (Original_Node (F)) is
6289 when N_Formal_Object_Declaration |
6290 N_Formal_Type_Declaration =>
6291 Formal_Ent := Defining_Identifier (F);
6293 while Chars (Act) /= Chars (Formal_Ent) loop
6294 Next_Entity (Act);
6295 end loop;
6297 when N_Formal_Subprogram_Declaration |
6298 N_Formal_Package_Declaration |
6299 N_Package_Declaration |
6300 N_Generic_Package_Declaration =>
6301 Formal_Ent := Defining_Entity (F);
6303 while Chars (Act) /= Chars (Formal_Ent) loop
6304 Next_Entity (Act);
6305 end loop;
6307 when others =>
6308 raise Program_Error;
6309 end case;
6310 end Find_Matching_Actual;
6312 -------------------
6313 -- Formal_Entity --
6314 -------------------
6316 function Formal_Entity
6317 (F : Node_Id;
6318 Act_Ent : Entity_Id) return Entity_Id
6320 Orig_Node : Node_Id := F;
6321 Act_Pkg : Entity_Id;
6323 begin
6324 case Nkind (Original_Node (F)) is
6325 when N_Formal_Object_Declaration =>
6326 return Defining_Identifier (F);
6328 when N_Formal_Type_Declaration =>
6329 return Defining_Identifier (F);
6331 when N_Formal_Subprogram_Declaration =>
6332 return Defining_Unit_Name (Specification (F));
6334 when N_Package_Declaration =>
6335 return Defining_Unit_Name (Specification (F));
6337 when N_Formal_Package_Declaration |
6338 N_Generic_Package_Declaration =>
6340 if Nkind (F) = N_Generic_Package_Declaration then
6341 Orig_Node := Original_Node (F);
6342 end if;
6344 Act_Pkg := Act_Ent;
6346 -- Find matching actual package, skipping over itypes and
6347 -- other entities generated when analyzing the formal. We
6348 -- know that if the instantiation is legal then there is
6349 -- a matching package for the formal.
6351 while Ekind (Act_Pkg) /= E_Package loop
6352 Act_Pkg := Next_Entity (Act_Pkg);
6353 end loop;
6355 declare
6356 Actual_Ent : Entity_Id := First_Entity (Act_Pkg);
6357 Formal_Node : Node_Id;
6358 Formal_Ent : Entity_Id;
6360 Gen_Decl : constant Node_Id :=
6361 Unit_Declaration_Node
6362 (Entity (Name (Orig_Node)));
6364 Formals : constant List_Id :=
6365 Generic_Formal_Declarations (Gen_Decl);
6367 begin
6368 if Present (Formals) then
6369 Formal_Node := First_Non_Pragma (Formals);
6370 else
6371 Formal_Node := Empty;
6372 end if;
6374 while Present (Actual_Ent)
6375 and then Present (Formal_Node)
6376 and then Actual_Ent /= First_Private_Entity (Act_Ent)
6377 loop
6378 -- ??? Are the following calls also needed here:
6380 -- Set_Is_Hidden (Actual_Ent, False);
6381 -- Set_Is_Potentially_Use_Visible
6382 -- (Actual_Ent, In_Use (Act_Ent));
6384 Formal_Ent := Formal_Entity (Formal_Node, Actual_Ent);
6385 if Present (Formal_Ent) then
6386 Set_Instance_Of (Formal_Ent, Actual_Ent);
6387 end if;
6388 Next_Non_Pragma (Formal_Node);
6390 Next_Entity (Actual_Ent);
6391 end loop;
6392 end;
6394 return Defining_Identifier (Orig_Node);
6396 when N_Use_Package_Clause =>
6397 return Empty;
6399 when N_Use_Type_Clause =>
6400 return Empty;
6402 -- We return Empty for all other encountered forms of
6403 -- declarations because there are some cases of nonformal
6404 -- sorts of declaration that can show up (e.g., when array
6405 -- formals are present). Since it's not clear what kinds
6406 -- can appear among the formals, we won't raise failure here.
6408 when others =>
6409 return Empty;
6411 end case;
6412 end Formal_Entity;
6414 --------------------
6415 -- Is_Instance_Of --
6416 --------------------
6418 function Is_Instance_Of
6419 (Act_Spec : Entity_Id;
6420 Gen_Anc : Entity_Id) return Boolean
6422 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
6424 begin
6425 if No (Gen_Par) then
6426 return False;
6428 -- Simplest case: the generic parent of the actual is the formal.
6430 elsif Gen_Par = Gen_Anc then
6431 return True;
6433 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
6434 return False;
6436 -- The actual may be obtained through several instantiations. Its
6437 -- scope must itself be an instance of a generic declared in the
6438 -- same scope as the formal. Any other case is detected above.
6440 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
6441 return False;
6443 else
6444 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
6445 end if;
6446 end Is_Instance_Of;
6448 ------------------
6449 -- Map_Entities --
6450 ------------------
6452 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id) is
6453 E1 : Entity_Id;
6454 E2 : Entity_Id;
6456 begin
6457 Set_Instance_Of (Form, Act);
6459 -- Traverse formal and actual package to map the corresponding
6460 -- entities. We skip over internal entities that may be generated
6461 -- during semantic analysis, and find the matching entities by
6462 -- name, given that they must appear in the same order.
6464 E1 := First_Entity (Form);
6465 E2 := First_Entity (Act);
6466 while Present (E1)
6467 and then E1 /= First_Private_Entity (Form)
6468 loop
6469 if not Is_Internal (E1)
6470 and then not Is_Class_Wide_Type (E1)
6471 and then Present (Parent (E1))
6472 then
6473 while Present (E2)
6474 and then Chars (E2) /= Chars (E1)
6475 loop
6476 Next_Entity (E2);
6477 end loop;
6479 if No (E2) then
6480 exit;
6481 else
6482 Set_Instance_Of (E1, E2);
6484 if Is_Type (E1)
6485 and then Is_Tagged_Type (E2)
6486 then
6487 Set_Instance_Of
6488 (Class_Wide_Type (E1), Class_Wide_Type (E2));
6489 end if;
6491 if Ekind (E1) = E_Package
6492 and then No (Renamed_Object (E1))
6493 then
6494 Map_Entities (E1, E2);
6495 end if;
6496 end if;
6497 end if;
6499 Next_Entity (E1);
6500 end loop;
6501 end Map_Entities;
6503 ---------------------------
6504 -- Process_Nested_Formal --
6505 ---------------------------
6507 procedure Process_Nested_Formal (Formal : Entity_Id) is
6508 Ent : Entity_Id;
6510 begin
6511 if Present (Associated_Formal_Package (Formal))
6512 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
6513 then
6514 Ent := First_Entity (Formal);
6515 while Present (Ent) loop
6516 Set_Is_Hidden (Ent, False);
6517 Set_Is_Potentially_Use_Visible
6518 (Ent, Is_Potentially_Use_Visible (Formal));
6520 if Ekind (Ent) = E_Package then
6521 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
6522 Process_Nested_Formal (Ent);
6523 end if;
6525 Next_Entity (Ent);
6526 end loop;
6527 end if;
6528 end Process_Nested_Formal;
6530 -- Start of processing for Instantiate_Formal_Package
6532 begin
6533 Analyze (Actual);
6535 if not Is_Entity_Name (Actual)
6536 or else Ekind (Entity (Actual)) /= E_Package
6537 then
6538 Error_Msg_N
6539 ("expect package instance to instantiate formal", Actual);
6540 Abandon_Instantiation (Actual);
6541 raise Program_Error;
6543 else
6544 Actual_Pack := Entity (Actual);
6545 Set_Is_Instantiated (Actual_Pack);
6547 -- The actual may be a renamed package, or an outer generic
6548 -- formal package whose instantiation is converted into a renaming.
6550 if Present (Renamed_Object (Actual_Pack)) then
6551 Actual_Pack := Renamed_Object (Actual_Pack);
6552 end if;
6554 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
6555 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
6556 Formal_Pack := Defining_Identifier (Analyzed_Formal);
6557 else
6558 Gen_Parent :=
6559 Generic_Parent (Specification (Analyzed_Formal));
6560 Formal_Pack :=
6561 Defining_Unit_Name (Specification (Analyzed_Formal));
6562 end if;
6564 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
6565 Parent_Spec := Specification (Unit_Declaration_Node (Actual_Pack));
6566 else
6567 Parent_Spec := Parent (Actual_Pack);
6568 end if;
6570 if Gen_Parent = Any_Id then
6571 Error_Msg_N
6572 ("previous error in declaration of formal package", Actual);
6573 Abandon_Instantiation (Actual);
6575 elsif
6576 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
6577 then
6578 null;
6580 else
6581 Error_Msg_NE
6582 ("actual parameter must be instance of&", Actual, Gen_Parent);
6583 Abandon_Instantiation (Actual);
6584 end if;
6586 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
6587 Map_Entities (Formal_Pack, Actual_Pack);
6589 Nod :=
6590 Make_Package_Renaming_Declaration (Loc,
6591 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
6592 Name => New_Reference_To (Actual_Pack, Loc));
6594 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
6595 Defining_Identifier (Formal));
6596 Decls := New_List (Nod);
6598 -- If the formal F has a box, then the generic declarations are
6599 -- visible in the generic G. In an instance of G, the corresponding
6600 -- entities in the actual for F (which are the actuals for the
6601 -- instantiation of the generic that F denotes) must also be made
6602 -- visible for analysis of the current instance. On exit from the
6603 -- current instance, those entities are made private again. If the
6604 -- actual is currently in use, these entities are also use-visible.
6606 -- The loop through the actual entities also steps through the
6607 -- formal entities and enters associations from formals to
6608 -- actuals into the renaming map. This is necessary to properly
6609 -- handle checking of actual parameter associations for later
6610 -- formals that depend on actuals declared in the formal package.
6612 if Box_Present (Formal) then
6613 declare
6614 Gen_Decl : constant Node_Id :=
6615 Unit_Declaration_Node (Gen_Parent);
6616 Formals : constant List_Id :=
6617 Generic_Formal_Declarations (Gen_Decl);
6618 Actual_Ent : Entity_Id;
6619 Formal_Node : Node_Id;
6620 Formal_Ent : Entity_Id;
6622 begin
6623 if Present (Formals) then
6624 Formal_Node := First_Non_Pragma (Formals);
6625 else
6626 Formal_Node := Empty;
6627 end if;
6629 Actual_Ent := First_Entity (Actual_Pack);
6631 while Present (Actual_Ent)
6632 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
6633 loop
6634 Set_Is_Hidden (Actual_Ent, False);
6635 Set_Is_Potentially_Use_Visible
6636 (Actual_Ent, In_Use (Actual_Pack));
6638 if Ekind (Actual_Ent) = E_Package then
6639 Process_Nested_Formal (Actual_Ent);
6640 end if;
6642 if Present (Formal_Node) then
6643 Formal_Ent := Formal_Entity (Formal_Node, Actual_Ent);
6645 if Present (Formal_Ent) then
6646 Find_Matching_Actual (Formal_Node, Actual_Ent);
6647 Set_Instance_Of (Formal_Ent, Actual_Ent);
6648 end if;
6650 Next_Non_Pragma (Formal_Node);
6652 else
6653 -- No further formals to match, but the generic
6654 -- part may contain inherited operation that are
6655 -- not hidden in the enclosing instance.
6657 Next_Entity (Actual_Ent);
6658 end if;
6660 end loop;
6661 end;
6663 -- If the formal is not declared with a box, reanalyze it as
6664 -- an instantiation, to verify the matching rules of 12.7. The
6665 -- actual checks are performed after the generic associations
6666 -- been analyzed.
6668 else
6669 declare
6670 I_Pack : constant Entity_Id :=
6671 Make_Defining_Identifier (Sloc (Actual),
6672 Chars => New_Internal_Name ('P'));
6674 begin
6675 Set_Is_Internal (I_Pack);
6677 Append_To (Decls,
6678 Make_Package_Instantiation (Sloc (Actual),
6679 Defining_Unit_Name => I_Pack,
6680 Name => New_Occurrence_Of (Gen_Parent, Sloc (Actual)),
6681 Generic_Associations =>
6682 Generic_Associations (Formal)));
6683 end;
6684 end if;
6686 return Decls;
6687 end if;
6688 end Instantiate_Formal_Package;
6690 -----------------------------------
6691 -- Instantiate_Formal_Subprogram --
6692 -----------------------------------
6694 function Instantiate_Formal_Subprogram
6695 (Formal : Node_Id;
6696 Actual : Node_Id;
6697 Analyzed_Formal : Node_Id) return Node_Id
6699 Loc : Source_Ptr := Sloc (Instantiation_Node);
6700 Formal_Sub : constant Entity_Id :=
6701 Defining_Unit_Name (Specification (Formal));
6702 Analyzed_S : constant Entity_Id :=
6703 Defining_Unit_Name (Specification (Analyzed_Formal));
6704 Decl_Node : Node_Id;
6705 Nam : Node_Id;
6706 New_Spec : Node_Id;
6708 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
6709 -- If the generic is a child unit, the parent has been installed
6710 -- on the scope stack, but a default subprogram cannot resolve to
6711 -- something on the parent because that parent is not really part
6712 -- of the visible context (it is there to resolve explicit local
6713 -- entities). If the default has resolved in this way, we remove
6714 -- the entity from immediate visibility and analyze the node again
6715 -- to emit an error message or find another visible candidate.
6717 procedure Valid_Actual_Subprogram (Act : Node_Id);
6718 -- Perform legality check and raise exception on failure.
6720 -----------------------
6721 -- From_Parent_Scope --
6722 -----------------------
6724 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
6725 Gen_Scope : Node_Id := Scope (Analyzed_S);
6727 begin
6728 while Present (Gen_Scope)
6729 and then Is_Child_Unit (Gen_Scope)
6730 loop
6731 if Scope (Subp) = Scope (Gen_Scope) then
6732 return True;
6733 end if;
6735 Gen_Scope := Scope (Gen_Scope);
6736 end loop;
6738 return False;
6739 end From_Parent_Scope;
6741 -----------------------------
6742 -- Valid_Actual_Subprogram --
6743 -----------------------------
6745 procedure Valid_Actual_Subprogram (Act : Node_Id) is
6746 Act_E : Entity_Id := Empty;
6748 begin
6749 if Is_Entity_Name (Act) then
6750 Act_E := Entity (Act);
6751 elsif Nkind (Act) = N_Selected_Component
6752 and then Is_Entity_Name (Selector_Name (Act))
6753 then
6754 Act_E := Entity (Selector_Name (Act));
6755 end if;
6757 if (Present (Act_E) and then Is_Overloadable (Act_E))
6758 or else Nkind (Act) = N_Attribute_Reference
6759 or else Nkind (Act) = N_Indexed_Component
6760 or else Nkind (Act) = N_Character_Literal
6761 or else Nkind (Act) = N_Explicit_Dereference
6762 then
6763 return;
6764 end if;
6766 Error_Msg_NE
6767 ("expect subprogram or entry name in instantiation of&",
6768 Instantiation_Node, Formal_Sub);
6769 Abandon_Instantiation (Instantiation_Node);
6771 end Valid_Actual_Subprogram;
6773 -- Start of processing for Instantiate_Formal_Subprogram
6775 begin
6776 New_Spec := New_Copy_Tree (Specification (Formal));
6778 -- Create new entity for the actual (New_Copy_Tree does not).
6780 Set_Defining_Unit_Name
6781 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
6783 -- Find entity of actual. If the actual is an attribute reference, it
6784 -- cannot be resolved here (its formal is missing) but is handled
6785 -- instead in Attribute_Renaming. If the actual is overloaded, it is
6786 -- fully resolved subsequently, when the renaming declaration for the
6787 -- formal is analyzed. If it is an explicit dereference, resolve the
6788 -- prefix but not the actual itself, to prevent interpretation as a
6789 -- call.
6791 if Present (Actual) then
6792 Loc := Sloc (Actual);
6793 Set_Sloc (New_Spec, Loc);
6795 if Nkind (Actual) = N_Operator_Symbol then
6796 Find_Direct_Name (Actual);
6798 elsif Nkind (Actual) = N_Explicit_Dereference then
6799 Analyze (Prefix (Actual));
6801 elsif Nkind (Actual) /= N_Attribute_Reference then
6802 Analyze (Actual);
6803 end if;
6805 Valid_Actual_Subprogram (Actual);
6806 Nam := Actual;
6808 elsif Present (Default_Name (Formal)) then
6809 if Nkind (Default_Name (Formal)) /= N_Attribute_Reference
6810 and then Nkind (Default_Name (Formal)) /= N_Selected_Component
6811 and then Nkind (Default_Name (Formal)) /= N_Indexed_Component
6812 and then Nkind (Default_Name (Formal)) /= N_Character_Literal
6813 and then Present (Entity (Default_Name (Formal)))
6814 then
6815 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
6816 else
6817 Nam := New_Copy (Default_Name (Formal));
6818 Set_Sloc (Nam, Loc);
6819 end if;
6821 elsif Box_Present (Formal) then
6823 -- Actual is resolved at the point of instantiation. Create
6824 -- an identifier or operator with the same name as the formal.
6826 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
6827 Nam := Make_Operator_Symbol (Loc,
6828 Chars => Chars (Formal_Sub),
6829 Strval => No_String);
6830 else
6831 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
6832 end if;
6834 else
6835 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
6836 Error_Msg_NE
6837 ("missing actual&", Instantiation_Node, Formal_Sub);
6838 Error_Msg_NE
6839 ("\in instantiation of & declared#",
6840 Instantiation_Node, Scope (Analyzed_S));
6841 Abandon_Instantiation (Instantiation_Node);
6842 end if;
6844 Decl_Node :=
6845 Make_Subprogram_Renaming_Declaration (Loc,
6846 Specification => New_Spec,
6847 Name => Nam);
6849 -- If we do not have an actual and the formal specified <> then
6850 -- set to get proper default.
6852 if No (Actual) and then Box_Present (Formal) then
6853 Set_From_Default (Decl_Node);
6854 end if;
6856 -- Gather possible interpretations for the actual before analyzing the
6857 -- instance. If overloaded, it will be resolved when analyzing the
6858 -- renaming declaration.
6860 if Box_Present (Formal)
6861 and then No (Actual)
6862 then
6863 Analyze (Nam);
6865 if Is_Child_Unit (Scope (Analyzed_S))
6866 and then Present (Entity (Nam))
6867 then
6868 if not Is_Overloaded (Nam) then
6870 if From_Parent_Scope (Entity (Nam)) then
6871 Set_Is_Immediately_Visible (Entity (Nam), False);
6872 Set_Entity (Nam, Empty);
6873 Set_Etype (Nam, Empty);
6875 Analyze (Nam);
6877 Set_Is_Immediately_Visible (Entity (Nam));
6878 end if;
6880 else
6881 declare
6882 I : Interp_Index;
6883 It : Interp;
6885 begin
6886 Get_First_Interp (Nam, I, It);
6888 while Present (It.Nam) loop
6889 if From_Parent_Scope (It.Nam) then
6890 Remove_Interp (I);
6891 end if;
6893 Get_Next_Interp (I, It);
6894 end loop;
6895 end;
6896 end if;
6897 end if;
6898 end if;
6900 -- The generic instantiation freezes the actual. This can only be
6901 -- done once the actual is resolved, in the analysis of the renaming
6902 -- declaration. To indicate that must be done, we set the corresponding
6903 -- spec of the node to point to the formal subprogram entity.
6905 Set_Corresponding_Spec (Decl_Node, Analyzed_S);
6907 -- We cannot analyze the renaming declaration, and thus find the
6908 -- actual, until the all the actuals are assembled in the instance.
6909 -- For subsequent checks of other actuals, indicate the node that
6910 -- will hold the instance of this formal.
6912 Set_Instance_Of (Analyzed_S, Nam);
6914 if Nkind (Actual) = N_Selected_Component
6915 and then Is_Task_Type (Etype (Prefix (Actual)))
6916 and then not Is_Frozen (Etype (Prefix (Actual)))
6917 then
6918 -- The renaming declaration will create a body, which must appear
6919 -- outside of the instantiation, We move the renaming declaration
6920 -- out of the instance, and create an additional renaming inside,
6921 -- to prevent freezing anomalies.
6923 declare
6924 Anon_Id : constant Entity_Id :=
6925 Make_Defining_Identifier
6926 (Loc, New_Internal_Name ('E'));
6927 begin
6928 Set_Defining_Unit_Name (New_Spec, Anon_Id);
6929 Insert_Before (Instantiation_Node, Decl_Node);
6930 Analyze (Decl_Node);
6932 -- Now create renaming within the instance
6934 Decl_Node :=
6935 Make_Subprogram_Renaming_Declaration (Loc,
6936 Specification => New_Copy_Tree (New_Spec),
6937 Name => New_Occurrence_Of (Anon_Id, Loc));
6939 Set_Defining_Unit_Name (Specification (Decl_Node),
6940 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
6941 end;
6942 end if;
6944 return Decl_Node;
6945 end Instantiate_Formal_Subprogram;
6947 ------------------------
6948 -- Instantiate_Object --
6949 ------------------------
6951 function Instantiate_Object
6952 (Formal : Node_Id;
6953 Actual : Node_Id;
6954 Analyzed_Formal : Node_Id) return List_Id
6956 Formal_Id : constant Entity_Id := Defining_Identifier (Formal);
6957 Type_Id : constant Node_Id := Subtype_Mark (Formal);
6958 Loc : constant Source_Ptr := Sloc (Actual);
6959 Act_Assoc : constant Node_Id := Parent (Actual);
6960 Orig_Ftyp : constant Entity_Id :=
6961 Etype (Defining_Identifier (Analyzed_Formal));
6962 List : constant List_Id := New_List;
6963 Ftyp : Entity_Id;
6964 Decl_Node : Node_Id;
6965 Subt_Decl : Node_Id := Empty;
6967 begin
6968 -- Sloc for error message on missing actual.
6969 Error_Msg_Sloc := Sloc (Scope (Defining_Identifier (Analyzed_Formal)));
6971 if Get_Instance_Of (Formal_Id) /= Formal_Id then
6972 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
6973 end if;
6975 Set_Parent (List, Parent (Actual));
6977 -- OUT present
6979 if Out_Present (Formal) then
6981 -- An IN OUT generic actual must be a name. The instantiation is
6982 -- a renaming declaration. The actual is the name being renamed.
6983 -- We use the actual directly, rather than a copy, because it is not
6984 -- used further in the list of actuals, and because a copy or a use
6985 -- of relocate_node is incorrect if the instance is nested within
6986 -- a generic. In order to simplify ASIS searches, the Generic_Parent
6987 -- field links the declaration to the generic association.
6989 if No (Actual) then
6990 Error_Msg_NE
6991 ("missing actual&",
6992 Instantiation_Node, Formal_Id);
6993 Error_Msg_NE
6994 ("\in instantiation of & declared#",
6995 Instantiation_Node,
6996 Scope (Defining_Identifier (Analyzed_Formal)));
6997 Abandon_Instantiation (Instantiation_Node);
6998 end if;
7000 Decl_Node :=
7001 Make_Object_Renaming_Declaration (Loc,
7002 Defining_Identifier => New_Copy (Formal_Id),
7003 Subtype_Mark => New_Copy_Tree (Type_Id),
7004 Name => Actual);
7006 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
7008 -- The analysis of the actual may produce insert_action nodes, so
7009 -- the declaration must have a context in which to attach them.
7011 Append (Decl_Node, List);
7012 Analyze (Actual);
7014 -- This check is performed here because Analyze_Object_Renaming
7015 -- will not check it when Comes_From_Source is False. Note
7016 -- though that the check for the actual being the name of an
7017 -- object will be performed in Analyze_Object_Renaming.
7019 if Is_Object_Reference (Actual)
7020 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
7021 then
7022 Error_Msg_N
7023 ("illegal discriminant-dependent component for in out parameter",
7024 Actual);
7025 end if;
7027 -- The actual has to be resolved in order to check that it is
7028 -- a variable (due to cases such as F(1), where F returns
7029 -- access to an array, and for overloaded prefixes).
7031 Ftyp :=
7032 Get_Instance_Of (Etype (Defining_Identifier (Analyzed_Formal)));
7034 if Is_Private_Type (Ftyp)
7035 and then not Is_Private_Type (Etype (Actual))
7036 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
7037 or else Base_Type (Etype (Actual)) = Ftyp)
7038 then
7039 -- If the actual has the type of the full view of the formal,
7040 -- or else a non-private subtype of the formal, then
7041 -- the visibility of the formal type has changed. Add to the
7042 -- actuals a subtype declaration that will force the exchange
7043 -- of views in the body of the instance as well.
7045 Subt_Decl :=
7046 Make_Subtype_Declaration (Loc,
7047 Defining_Identifier =>
7048 Make_Defining_Identifier (Loc, New_Internal_Name ('P')),
7049 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
7051 Prepend (Subt_Decl, List);
7053 Append_Elmt (Full_View (Ftyp), Exchanged_Views);
7054 Exchange_Declarations (Ftyp);
7055 end if;
7057 Resolve (Actual, Ftyp);
7059 if not Is_Variable (Actual) or else Paren_Count (Actual) > 0 then
7060 Error_Msg_NE
7061 ("actual for& must be a variable", Actual, Formal_Id);
7063 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
7064 Error_Msg_NE (
7065 "type of actual does not match type of&", Actual, Formal_Id);
7067 end if;
7069 Note_Possible_Modification (Actual);
7071 -- Check for instantiation of atomic/volatile actual for
7072 -- non-atomic/volatile formal (RM C.6 (12)).
7074 if Is_Atomic_Object (Actual)
7075 and then not Is_Atomic (Orig_Ftyp)
7076 then
7077 Error_Msg_N
7078 ("cannot instantiate non-atomic formal object " &
7079 "with atomic actual", Actual);
7081 elsif Is_Volatile_Object (Actual)
7082 and then not Is_Volatile (Orig_Ftyp)
7083 then
7084 Error_Msg_N
7085 ("cannot instantiate non-volatile formal object " &
7086 "with volatile actual", Actual);
7087 end if;
7089 -- OUT not present
7091 else
7092 -- The instantiation of a generic formal in-parameter
7093 -- is a constant declaration. The actual is the expression for
7094 -- that declaration.
7096 if Present (Actual) then
7098 Decl_Node := Make_Object_Declaration (Loc,
7099 Defining_Identifier => New_Copy (Formal_Id),
7100 Constant_Present => True,
7101 Object_Definition => New_Copy_Tree (Type_Id),
7102 Expression => Actual);
7104 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
7106 -- A generic formal object of a tagged type is defined
7107 -- to be aliased so the new constant must also be treated
7108 -- as aliased.
7110 if Is_Tagged_Type
7111 (Etype (Defining_Identifier (Analyzed_Formal)))
7112 then
7113 Set_Aliased_Present (Decl_Node);
7114 end if;
7116 Append (Decl_Node, List);
7118 -- No need to repeat (pre-)analysis of some expression nodes
7119 -- already handled in Pre_Analyze_Actuals.
7121 if Nkind (Actual) /= N_Allocator then
7122 Analyze (Actual);
7123 end if;
7125 declare
7126 Typ : constant Entity_Id :=
7127 Get_Instance_Of
7128 (Etype (Defining_Identifier (Analyzed_Formal)));
7130 begin
7131 Freeze_Before (Instantiation_Node, Typ);
7133 -- If the actual is an aggregate, perform name resolution
7134 -- on its components (the analysis of an aggregate does not
7135 -- do it) to capture local names that may be hidden if the
7136 -- generic is a child unit.
7138 if Nkind (Actual) = N_Aggregate then
7139 Pre_Analyze_And_Resolve (Actual, Typ);
7140 end if;
7141 end;
7143 elsif Present (Expression (Formal)) then
7145 -- Use default to construct declaration.
7147 Decl_Node :=
7148 Make_Object_Declaration (Sloc (Formal),
7149 Defining_Identifier => New_Copy (Formal_Id),
7150 Constant_Present => True,
7151 Object_Definition => New_Copy (Type_Id),
7152 Expression => New_Copy_Tree (Expression (Formal)));
7154 Append (Decl_Node, List);
7155 Set_Analyzed (Expression (Decl_Node), False);
7157 else
7158 Error_Msg_NE
7159 ("missing actual&",
7160 Instantiation_Node, Formal_Id);
7161 Error_Msg_NE ("\in instantiation of & declared#",
7162 Instantiation_Node,
7163 Scope (Defining_Identifier (Analyzed_Formal)));
7165 if Is_Scalar_Type
7166 (Etype (Defining_Identifier (Analyzed_Formal)))
7167 then
7168 -- Create dummy constant declaration so that instance can
7169 -- be analyzed, to minimize cascaded visibility errors.
7171 Decl_Node :=
7172 Make_Object_Declaration (Loc,
7173 Defining_Identifier => New_Copy (Formal_Id),
7174 Constant_Present => True,
7175 Object_Definition => New_Copy (Type_Id),
7176 Expression =>
7177 Make_Attribute_Reference (Sloc (Formal_Id),
7178 Attribute_Name => Name_First,
7179 Prefix => New_Copy (Type_Id)));
7181 Append (Decl_Node, List);
7183 else
7184 Abandon_Instantiation (Instantiation_Node);
7185 end if;
7186 end if;
7188 end if;
7190 return List;
7191 end Instantiate_Object;
7193 ------------------------------
7194 -- Instantiate_Package_Body --
7195 ------------------------------
7197 procedure Instantiate_Package_Body
7198 (Body_Info : Pending_Body_Info;
7199 Inlined_Body : Boolean := False)
7201 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
7202 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
7203 Loc : constant Source_Ptr := Sloc (Inst_Node);
7205 Gen_Id : constant Node_Id := Name (Inst_Node);
7206 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7207 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
7208 Act_Spec : constant Node_Id := Specification (Act_Decl);
7209 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
7211 Act_Body_Name : Node_Id;
7212 Gen_Body : Node_Id;
7213 Gen_Body_Id : Node_Id;
7214 Act_Body : Node_Id;
7215 Act_Body_Id : Entity_Id;
7217 Parent_Installed : Boolean := False;
7218 Save_Style_Check : constant Boolean := Style_Check;
7220 begin
7221 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7223 -- The instance body may already have been processed, as the parent
7224 -- of another instance that is inlined. (Load_Parent_Of_Generic).
7226 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
7227 return;
7228 end if;
7230 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
7232 if No (Gen_Body_Id) then
7233 Load_Parent_Of_Generic (Inst_Node, Specification (Gen_Decl));
7234 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7235 end if;
7237 -- Establish global variable for sloc adjustment and for error
7238 -- recovery.
7240 Instantiation_Node := Inst_Node;
7242 if Present (Gen_Body_Id) then
7243 Save_Env (Gen_Unit, Act_Decl_Id);
7244 Style_Check := False;
7245 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
7247 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
7249 Create_Instantiation_Source
7250 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
7252 Act_Body :=
7253 Copy_Generic_Node
7254 (Original_Node (Gen_Body), Empty, Instantiating => True);
7256 -- Build new name (possibly qualified) for body declaration
7258 Act_Body_Id := New_Copy (Act_Decl_Id);
7260 -- Some attributes of the spec entity are not inherited by the
7261 -- body entity.
7263 Set_Handler_Records (Act_Body_Id, No_List);
7265 if Nkind (Defining_Unit_Name (Act_Spec)) =
7266 N_Defining_Program_Unit_Name
7267 then
7268 Act_Body_Name :=
7269 Make_Defining_Program_Unit_Name (Loc,
7270 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
7271 Defining_Identifier => Act_Body_Id);
7272 else
7273 Act_Body_Name := Act_Body_Id;
7274 end if;
7276 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
7278 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
7279 Check_Generic_Actuals (Act_Decl_Id, False);
7281 -- If it is a child unit, make the parent instance (which is an
7282 -- instance of the parent of the generic) visible. The parent
7283 -- instance is the prefix of the name of the generic unit.
7285 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
7286 and then Nkind (Gen_Id) = N_Expanded_Name
7287 then
7288 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
7289 Parent_Installed := True;
7291 elsif Is_Child_Unit (Gen_Unit) then
7292 Install_Parent (Scope (Gen_Unit), In_Body => True);
7293 Parent_Installed := True;
7294 end if;
7296 -- If the instantiation is a library unit, and this is the main
7297 -- unit, then build the resulting compilation unit nodes for the
7298 -- instance. If this is a compilation unit but it is not the main
7299 -- unit, then it is the body of a unit in the context, that is being
7300 -- compiled because it is encloses some inlined unit or another
7301 -- generic unit being instantiated. In that case, this body is not
7302 -- part of the current compilation, and is not attached to the tree,
7303 -- but its parent must be set for analysis.
7305 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
7307 -- Replace instance node with body of instance, and create
7308 -- new node for corresponding instance declaration.
7310 Build_Instance_Compilation_Unit_Nodes
7311 (Inst_Node, Act_Body, Act_Decl);
7312 Analyze (Inst_Node);
7314 if Parent (Inst_Node) = Cunit (Main_Unit) then
7316 -- If the instance is a child unit itself, then set the
7317 -- scope of the expanded body to be the parent of the
7318 -- instantiation (ensuring that the fully qualified name
7319 -- will be generated for the elaboration subprogram).
7321 if Nkind (Defining_Unit_Name (Act_Spec)) =
7322 N_Defining_Program_Unit_Name
7323 then
7324 Set_Scope
7325 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
7326 end if;
7327 end if;
7329 -- Case where instantiation is not a library unit
7331 else
7332 -- If this is an early instantiation, i.e. appears textually
7333 -- before the corresponding body and must be elaborated first,
7334 -- indicate that the body instance is to be delayed.
7336 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
7338 -- Now analyze the body. We turn off all checks if this is
7339 -- an internal unit, since there is no reason to have checks
7340 -- on for any predefined run-time library code. All such
7341 -- code is designed to be compiled with checks off.
7343 -- Note that we do NOT apply this criterion to children of
7344 -- GNAT (or on VMS, children of DEC). The latter units must
7345 -- suppress checks explicitly if this is needed.
7347 if Is_Predefined_File_Name
7348 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
7349 then
7350 Analyze (Act_Body, Suppress => All_Checks);
7351 else
7352 Analyze (Act_Body);
7353 end if;
7354 end if;
7356 if not Generic_Separately_Compiled (Gen_Unit) then
7357 Inherit_Context (Gen_Body, Inst_Node);
7358 end if;
7360 -- Remove the parent instances if they have been placed on the
7361 -- scope stack to compile the body.
7363 if Parent_Installed then
7364 Remove_Parent (In_Body => True);
7365 end if;
7367 Restore_Private_Views (Act_Decl_Id);
7369 -- Remove the current unit from visibility if this is an instance
7370 -- that is not elaborated on the fly for inlining purposes.
7372 if not Inlined_Body then
7373 Set_Is_Immediately_Visible (Act_Decl_Id, False);
7374 end if;
7376 Restore_Env;
7377 Style_Check := Save_Style_Check;
7379 -- If we have no body, and the unit requires a body, then complain.
7380 -- This complaint is suppressed if we have detected other errors
7381 -- (since a common reason for missing the body is that it had errors).
7383 elsif Unit_Requires_Body (Gen_Unit) then
7384 if Serious_Errors_Detected = 0 then
7385 Error_Msg_NE
7386 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
7388 -- Don't attempt to perform any cleanup actions if some other
7389 -- error was aready detected, since this can cause blowups.
7391 else
7392 return;
7393 end if;
7395 -- Case of package that does not need a body
7397 else
7398 -- If the instantiation of the declaration is a library unit,
7399 -- rewrite the original package instantiation as a package
7400 -- declaration in the compilation unit node.
7402 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
7403 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
7404 Rewrite (Inst_Node, Act_Decl);
7406 -- Generate elaboration entity, in case spec has elaboration
7407 -- code. This cannot be done when the instance is analyzed,
7408 -- because it is not known yet whether the body exists.
7410 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
7411 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
7413 -- If the instantiation is not a library unit, then append the
7414 -- declaration to the list of implicitly generated entities.
7415 -- unless it is already a list member which means that it was
7416 -- already processed
7418 elsif not Is_List_Member (Act_Decl) then
7419 Mark_Rewrite_Insertion (Act_Decl);
7420 Insert_Before (Inst_Node, Act_Decl);
7421 end if;
7422 end if;
7424 Expander_Mode_Restore;
7425 end Instantiate_Package_Body;
7427 ---------------------------------
7428 -- Instantiate_Subprogram_Body --
7429 ---------------------------------
7431 procedure Instantiate_Subprogram_Body
7432 (Body_Info : Pending_Body_Info)
7434 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
7435 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
7436 Loc : constant Source_Ptr := Sloc (Inst_Node);
7437 Gen_Id : constant Node_Id := Name (Inst_Node);
7438 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7439 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
7440 Anon_Id : constant Entity_Id :=
7441 Defining_Unit_Name (Specification (Act_Decl));
7442 Pack_Id : constant Entity_Id :=
7443 Defining_Unit_Name (Parent (Act_Decl));
7444 Decls : List_Id;
7445 Gen_Body : Node_Id;
7446 Gen_Body_Id : Node_Id;
7447 Act_Body : Node_Id;
7448 Act_Body_Id : Entity_Id;
7449 Pack_Body : Node_Id;
7450 Prev_Formal : Entity_Id;
7451 Ret_Expr : Node_Id;
7452 Unit_Renaming : Node_Id;
7454 Parent_Installed : Boolean := False;
7455 Save_Style_Check : constant Boolean := Style_Check;
7457 begin
7458 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7460 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
7462 if No (Gen_Body_Id) then
7463 Load_Parent_Of_Generic (Inst_Node, Specification (Gen_Decl));
7464 Gen_Body_Id := Corresponding_Body (Gen_Decl);
7465 end if;
7467 Instantiation_Node := Inst_Node;
7469 if Present (Gen_Body_Id) then
7470 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
7472 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
7474 -- Either body is not present, or context is non-expanding, as
7475 -- when compiling a subunit. Mark the instance as completed, and
7476 -- diagnose a missing body when needed.
7478 if Expander_Active
7479 and then Operating_Mode = Generate_Code
7480 then
7481 Error_Msg_N
7482 ("missing proper body for instantiation", Gen_Body);
7483 end if;
7485 Set_Has_Completion (Anon_Id);
7486 return;
7487 end if;
7489 Save_Env (Gen_Unit, Anon_Id);
7490 Style_Check := False;
7491 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
7492 Create_Instantiation_Source
7493 (Inst_Node,
7494 Gen_Body_Id,
7495 False,
7496 S_Adjustment);
7498 Act_Body :=
7499 Copy_Generic_Node
7500 (Original_Node (Gen_Body), Empty, Instantiating => True);
7501 Act_Body_Id := Defining_Entity (Act_Body);
7502 Set_Chars (Act_Body_Id, Chars (Anon_Id));
7503 Set_Sloc (Act_Body_Id, Sloc (Defining_Entity (Inst_Node)));
7504 Set_Corresponding_Spec (Act_Body, Anon_Id);
7505 Set_Has_Completion (Anon_Id);
7506 Check_Generic_Actuals (Pack_Id, False);
7508 -- If it is a child unit, make the parent instance (which is an
7509 -- instance of the parent of the generic) visible. The parent
7510 -- instance is the prefix of the name of the generic unit.
7512 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
7513 and then Nkind (Gen_Id) = N_Expanded_Name
7514 then
7515 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
7516 Parent_Installed := True;
7518 elsif Is_Child_Unit (Gen_Unit) then
7519 Install_Parent (Scope (Gen_Unit), In_Body => True);
7520 Parent_Installed := True;
7521 end if;
7523 -- Inside its body, a reference to the generic unit is a reference
7524 -- to the instance. The corresponding renaming is the first
7525 -- declaration in the body.
7527 Unit_Renaming :=
7528 Make_Subprogram_Renaming_Declaration (Loc,
7529 Specification =>
7530 Copy_Generic_Node (
7531 Specification (Original_Node (Gen_Body)),
7532 Empty,
7533 Instantiating => True),
7534 Name => New_Occurrence_Of (Anon_Id, Loc));
7536 -- If there is a formal subprogram with the same name as the
7537 -- unit itself, do not add this renaming declaration. This is
7538 -- a temporary fix for one ACVC test. ???
7540 Prev_Formal := First_Entity (Pack_Id);
7541 while Present (Prev_Formal) loop
7542 if Chars (Prev_Formal) = Chars (Gen_Unit)
7543 and then Is_Overloadable (Prev_Formal)
7544 then
7545 exit;
7546 end if;
7548 Next_Entity (Prev_Formal);
7549 end loop;
7551 if Present (Prev_Formal) then
7552 Decls := New_List (Act_Body);
7553 else
7554 Decls := New_List (Unit_Renaming, Act_Body);
7555 end if;
7557 -- The subprogram body is placed in the body of a dummy package
7558 -- body, whose spec contains the subprogram declaration as well
7559 -- as the renaming declarations for the generic parameters.
7561 Pack_Body := Make_Package_Body (Loc,
7562 Defining_Unit_Name => New_Copy (Pack_Id),
7563 Declarations => Decls);
7565 Set_Corresponding_Spec (Pack_Body, Pack_Id);
7567 -- If the instantiation is a library unit, then build resulting
7568 -- compilation unit nodes for the instance. The declaration of
7569 -- the enclosing package is the grandparent of the subprogram
7570 -- declaration. First replace the instantiation node as the unit
7571 -- of the corresponding compilation.
7573 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
7574 if Parent (Inst_Node) = Cunit (Main_Unit) then
7575 Set_Unit (Parent (Inst_Node), Inst_Node);
7576 Build_Instance_Compilation_Unit_Nodes
7577 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
7578 Analyze (Inst_Node);
7579 else
7580 Set_Parent (Pack_Body, Parent (Inst_Node));
7581 Analyze (Pack_Body);
7582 end if;
7584 else
7585 Insert_Before (Inst_Node, Pack_Body);
7586 Mark_Rewrite_Insertion (Pack_Body);
7587 Analyze (Pack_Body);
7589 if Expander_Active then
7590 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
7591 end if;
7592 end if;
7594 if not Generic_Separately_Compiled (Gen_Unit) then
7595 Inherit_Context (Gen_Body, Inst_Node);
7596 end if;
7598 Restore_Private_Views (Pack_Id, False);
7600 if Parent_Installed then
7601 Remove_Parent (In_Body => True);
7602 end if;
7604 Restore_Env;
7605 Style_Check := Save_Style_Check;
7607 -- Body not found. Error was emitted already. If there were no
7608 -- previous errors, this may be an instance whose scope is a premature
7609 -- instance. In that case we must insure that the (legal) program does
7610 -- raise program error if executed. We generate a subprogram body for
7611 -- this purpose. See DEC ac30vso.
7613 elsif Serious_Errors_Detected = 0
7614 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
7615 then
7616 if Ekind (Anon_Id) = E_Procedure then
7617 Act_Body :=
7618 Make_Subprogram_Body (Loc,
7619 Specification =>
7620 Make_Procedure_Specification (Loc,
7621 Defining_Unit_Name => New_Copy (Anon_Id),
7622 Parameter_Specifications =>
7623 New_Copy_List
7624 (Parameter_Specifications (Parent (Anon_Id)))),
7626 Declarations => Empty_List,
7627 Handled_Statement_Sequence =>
7628 Make_Handled_Sequence_Of_Statements (Loc,
7629 Statements =>
7630 New_List (
7631 Make_Raise_Program_Error (Loc,
7632 Reason =>
7633 PE_Access_Before_Elaboration))));
7635 else
7636 Ret_Expr :=
7637 Make_Raise_Program_Error (Loc,
7638 Reason => PE_Access_Before_Elaboration);
7640 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
7641 Set_Analyzed (Ret_Expr);
7643 Act_Body :=
7644 Make_Subprogram_Body (Loc,
7645 Specification =>
7646 Make_Function_Specification (Loc,
7647 Defining_Unit_Name => New_Copy (Anon_Id),
7648 Parameter_Specifications =>
7649 New_Copy_List
7650 (Parameter_Specifications (Parent (Anon_Id))),
7651 Subtype_Mark =>
7652 New_Occurrence_Of (Etype (Anon_Id), Loc)),
7654 Declarations => Empty_List,
7655 Handled_Statement_Sequence =>
7656 Make_Handled_Sequence_Of_Statements (Loc,
7657 Statements =>
7658 New_List (Make_Return_Statement (Loc, Ret_Expr))));
7659 end if;
7661 Pack_Body := Make_Package_Body (Loc,
7662 Defining_Unit_Name => New_Copy (Pack_Id),
7663 Declarations => New_List (Act_Body));
7665 Insert_After (Inst_Node, Pack_Body);
7666 Set_Corresponding_Spec (Pack_Body, Pack_Id);
7667 Analyze (Pack_Body);
7668 end if;
7670 Expander_Mode_Restore;
7671 end Instantiate_Subprogram_Body;
7673 ----------------------
7674 -- Instantiate_Type --
7675 ----------------------
7677 function Instantiate_Type
7678 (Formal : Node_Id;
7679 Actual : Node_Id;
7680 Analyzed_Formal : Node_Id;
7681 Actual_Decls : List_Id) return Node_Id
7683 Loc : constant Source_Ptr := Sloc (Actual);
7684 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
7685 A_Gen_T : constant Entity_Id := Defining_Identifier (Analyzed_Formal);
7686 Ancestor : Entity_Id := Empty;
7687 Def : constant Node_Id := Formal_Type_Definition (Formal);
7688 Act_T : Entity_Id;
7689 Decl_Node : Node_Id;
7691 procedure Validate_Array_Type_Instance;
7692 procedure Validate_Access_Subprogram_Instance;
7693 procedure Validate_Access_Type_Instance;
7694 procedure Validate_Derived_Type_Instance;
7695 procedure Validate_Private_Type_Instance;
7696 -- These procedures perform validation tests for the named case
7698 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
7699 -- Check that base types are the same and that the subtypes match
7700 -- statically. Used in several of the above.
7702 --------------------
7703 -- Subtypes_Match --
7704 --------------------
7706 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
7707 T : constant Entity_Id := Get_Instance_Of (Gen_T);
7709 begin
7710 return (Base_Type (T) = Base_Type (Act_T)
7711 -- why is the and then commented out here???
7712 -- and then Is_Constrained (T) = Is_Constrained (Act_T)
7713 and then Subtypes_Statically_Match (T, Act_T))
7715 or else (Is_Class_Wide_Type (Gen_T)
7716 and then Is_Class_Wide_Type (Act_T)
7717 and then
7718 Subtypes_Match (
7719 Get_Instance_Of (Root_Type (Gen_T)),
7720 Root_Type (Act_T)));
7721 end Subtypes_Match;
7723 -----------------------------------------
7724 -- Validate_Access_Subprogram_Instance --
7725 -----------------------------------------
7727 procedure Validate_Access_Subprogram_Instance is
7728 begin
7729 if not Is_Access_Type (Act_T)
7730 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
7731 then
7732 Error_Msg_NE
7733 ("expect access type in instantiation of &", Actual, Gen_T);
7734 Abandon_Instantiation (Actual);
7735 end if;
7737 Check_Mode_Conformant
7738 (Designated_Type (Act_T),
7739 Designated_Type (A_Gen_T),
7740 Actual,
7741 Get_Inst => True);
7743 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
7744 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
7745 Error_Msg_NE
7746 ("protected access type not allowed for formal &",
7747 Actual, Gen_T);
7748 end if;
7750 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
7751 Error_Msg_NE
7752 ("expect protected access type for formal &",
7753 Actual, Gen_T);
7754 end if;
7755 end Validate_Access_Subprogram_Instance;
7757 -----------------------------------
7758 -- Validate_Access_Type_Instance --
7759 -----------------------------------
7761 procedure Validate_Access_Type_Instance is
7762 Desig_Type : constant Entity_Id :=
7763 Find_Actual_Type
7764 (Designated_Type (A_Gen_T), Scope (A_Gen_T));
7766 begin
7767 if not Is_Access_Type (Act_T) then
7768 Error_Msg_NE
7769 ("expect access type in instantiation of &", Actual, Gen_T);
7770 Abandon_Instantiation (Actual);
7771 end if;
7773 if Is_Access_Constant (A_Gen_T) then
7774 if not Is_Access_Constant (Act_T) then
7775 Error_Msg_N
7776 ("actual type must be access-to-constant type", Actual);
7777 Abandon_Instantiation (Actual);
7778 end if;
7779 else
7780 if Is_Access_Constant (Act_T) then
7781 Error_Msg_N
7782 ("actual type must be access-to-variable type", Actual);
7783 Abandon_Instantiation (Actual);
7785 elsif Ekind (A_Gen_T) = E_General_Access_Type
7786 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
7787 then
7788 Error_Msg_N ("actual must be general access type!", Actual);
7789 Error_Msg_NE ("add ALL to }!", Actual, Act_T);
7790 Abandon_Instantiation (Actual);
7791 end if;
7792 end if;
7794 -- The designated subtypes, that is to say the subtypes introduced
7795 -- by an access type declaration (and not by a subtype declaration)
7796 -- must match.
7798 if not Subtypes_Match
7799 (Desig_Type, Designated_Type (Base_Type (Act_T)))
7800 then
7801 Error_Msg_NE
7802 ("designated type of actual does not match that of formal &",
7803 Actual, Gen_T);
7804 Abandon_Instantiation (Actual);
7806 elsif Is_Access_Type (Designated_Type (Act_T))
7807 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
7809 Is_Constrained (Designated_Type (Desig_Type))
7810 then
7811 Error_Msg_NE
7812 ("designated type of actual does not match that of formal &",
7813 Actual, Gen_T);
7814 Abandon_Instantiation (Actual);
7815 end if;
7816 end Validate_Access_Type_Instance;
7818 ----------------------------------
7819 -- Validate_Array_Type_Instance --
7820 ----------------------------------
7822 procedure Validate_Array_Type_Instance is
7823 I1 : Node_Id;
7824 I2 : Node_Id;
7825 T2 : Entity_Id;
7827 function Formal_Dimensions return Int;
7828 -- Count number of dimensions in array type formal
7830 -----------------------
7831 -- Formal_Dimensions --
7832 -----------------------
7834 function Formal_Dimensions return Int is
7835 Num : Int := 0;
7836 Index : Node_Id;
7838 begin
7839 if Nkind (Def) = N_Constrained_Array_Definition then
7840 Index := First (Discrete_Subtype_Definitions (Def));
7841 else
7842 Index := First (Subtype_Marks (Def));
7843 end if;
7845 while Present (Index) loop
7846 Num := Num + 1;
7847 Next_Index (Index);
7848 end loop;
7850 return Num;
7851 end Formal_Dimensions;
7853 -- Start of processing for Validate_Array_Type_Instance
7855 begin
7856 if not Is_Array_Type (Act_T) then
7857 Error_Msg_NE
7858 ("expect array type in instantiation of &", Actual, Gen_T);
7859 Abandon_Instantiation (Actual);
7861 elsif Nkind (Def) = N_Constrained_Array_Definition then
7862 if not (Is_Constrained (Act_T)) then
7863 Error_Msg_NE
7864 ("expect constrained array in instantiation of &",
7865 Actual, Gen_T);
7866 Abandon_Instantiation (Actual);
7867 end if;
7869 else
7870 if Is_Constrained (Act_T) then
7871 Error_Msg_NE
7872 ("expect unconstrained array in instantiation of &",
7873 Actual, Gen_T);
7874 Abandon_Instantiation (Actual);
7875 end if;
7876 end if;
7878 if Formal_Dimensions /= Number_Dimensions (Act_T) then
7879 Error_Msg_NE
7880 ("dimensions of actual do not match formal &", Actual, Gen_T);
7881 Abandon_Instantiation (Actual);
7882 end if;
7884 I1 := First_Index (A_Gen_T);
7885 I2 := First_Index (Act_T);
7886 for J in 1 .. Formal_Dimensions loop
7888 -- If the indices of the actual were given by a subtype_mark,
7889 -- the index was transformed into a range attribute. Retrieve
7890 -- the original type mark for checking.
7892 if Is_Entity_Name (Original_Node (I2)) then
7893 T2 := Entity (Original_Node (I2));
7894 else
7895 T2 := Etype (I2);
7896 end if;
7898 if not Subtypes_Match
7899 (Find_Actual_Type (Etype (I1), Scope (A_Gen_T)), T2)
7900 then
7901 Error_Msg_NE
7902 ("index types of actual do not match those of formal &",
7903 Actual, Gen_T);
7904 Abandon_Instantiation (Actual);
7905 end if;
7907 Next_Index (I1);
7908 Next_Index (I2);
7909 end loop;
7911 if not Subtypes_Match (
7912 Find_Actual_Type (Component_Type (A_Gen_T), Scope (A_Gen_T)),
7913 Component_Type (Act_T))
7914 then
7915 Error_Msg_NE
7916 ("component subtype of actual does not match that of formal &",
7917 Actual, Gen_T);
7918 Abandon_Instantiation (Actual);
7919 end if;
7921 if Has_Aliased_Components (A_Gen_T)
7922 and then not Has_Aliased_Components (Act_T)
7923 then
7924 Error_Msg_NE
7925 ("actual must have aliased components to match formal type &",
7926 Actual, Gen_T);
7927 end if;
7929 end Validate_Array_Type_Instance;
7931 ------------------------------------
7932 -- Validate_Derived_Type_Instance --
7933 ------------------------------------
7935 procedure Validate_Derived_Type_Instance is
7936 Actual_Discr : Entity_Id;
7937 Ancestor_Discr : Entity_Id;
7939 begin
7940 -- If the parent type in the generic declaration is itself
7941 -- a previous formal type, then it is local to the generic
7942 -- and absent from the analyzed generic definition. In that
7943 -- case the ancestor is the instance of the formal (which must
7944 -- have been instantiated previously), unless the ancestor is
7945 -- itself a formal derived type. In this latter case (which is the
7946 -- subject of Corrigendum 8652/0038 (AI-202) the ancestor of the
7947 -- formals is the ancestor of its parent. Otherwise, the analyzed
7948 -- generic carries the parent type. If the parent type is defined
7949 -- in a previous formal package, then the scope of that formal
7950 -- package is that of the generic type itself, and it has already
7951 -- been mapped into the corresponding type in the actual package.
7953 -- Common case: parent type defined outside of the generic
7955 if Is_Entity_Name (Subtype_Mark (Def))
7956 and then Present (Entity (Subtype_Mark (Def)))
7957 then
7958 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
7960 -- Check whether parent is defined in a previous formal package
7962 elsif
7963 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
7964 then
7965 Ancestor :=
7966 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
7968 -- The type may be a local derivation, or a type extension of
7969 -- a previous formal, or of a formal of a parent package.
7971 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
7972 or else
7973 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
7974 then
7975 -- Check whether the parent is another derived formal type
7976 -- in the same generic unit.
7978 if Etype (A_Gen_T) /= A_Gen_T
7979 and then Is_Generic_Type (Etype (A_Gen_T))
7980 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
7981 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
7982 then
7983 -- Locate ancestor of parent from the subtype declaration
7984 -- created for the actual.
7986 declare
7987 Decl : Node_Id;
7989 begin
7990 Decl := First (Actual_Decls);
7991 while Present (Decl) loop
7992 if Nkind (Decl) = N_Subtype_Declaration
7993 and then Chars (Defining_Identifier (Decl)) =
7994 Chars (Etype (A_Gen_T))
7995 then
7996 Ancestor := Generic_Parent_Type (Decl);
7997 exit;
7998 else
7999 Next (Decl);
8000 end if;
8001 end loop;
8002 end;
8004 pragma Assert (Present (Ancestor));
8006 else
8007 Ancestor :=
8008 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
8009 end if;
8011 else
8012 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
8013 end if;
8015 if not Is_Ancestor (Base_Type (Ancestor), Act_T) then
8016 Error_Msg_NE
8017 ("expect type derived from & in instantiation",
8018 Actual, First_Subtype (Ancestor));
8019 Abandon_Instantiation (Actual);
8020 end if;
8022 -- Perform atomic/volatile checks (RM C.6(12))
8024 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
8025 Error_Msg_N
8026 ("cannot have atomic actual type for non-atomic formal type",
8027 Actual);
8029 elsif Is_Volatile (Act_T)
8030 and then not Is_Volatile (Ancestor)
8031 and then Is_By_Reference_Type (Ancestor)
8032 then
8033 Error_Msg_N
8034 ("cannot have volatile actual type for non-volatile formal type",
8035 Actual);
8036 end if;
8038 -- It should not be necessary to check for unknown discriminants
8039 -- on Formal, but for some reason Has_Unknown_Discriminants is
8040 -- false for A_Gen_T, so Is_Indefinite_Subtype incorrectly
8041 -- returns False. This needs fixing. ???
8043 if not Is_Indefinite_Subtype (A_Gen_T)
8044 and then not Unknown_Discriminants_Present (Formal)
8045 and then Is_Indefinite_Subtype (Act_T)
8046 then
8047 Error_Msg_N
8048 ("actual subtype must be constrained", Actual);
8049 Abandon_Instantiation (Actual);
8050 end if;
8052 if not Unknown_Discriminants_Present (Formal) then
8053 if Is_Constrained (Ancestor) then
8054 if not Is_Constrained (Act_T) then
8055 Error_Msg_N
8056 ("actual subtype must be constrained", Actual);
8057 Abandon_Instantiation (Actual);
8058 end if;
8060 -- Ancestor is unconstrained
8062 elsif Is_Constrained (Act_T) then
8063 if Ekind (Ancestor) = E_Access_Type
8064 or else Is_Composite_Type (Ancestor)
8065 then
8066 Error_Msg_N
8067 ("actual subtype must be unconstrained", Actual);
8068 Abandon_Instantiation (Actual);
8069 end if;
8071 -- A class-wide type is only allowed if the formal has
8072 -- unknown discriminants.
8074 elsif Is_Class_Wide_Type (Act_T)
8075 and then not Has_Unknown_Discriminants (Ancestor)
8076 then
8077 Error_Msg_NE
8078 ("actual for & cannot be a class-wide type", Actual, Gen_T);
8079 Abandon_Instantiation (Actual);
8081 -- Otherwise, the formal and actual shall have the same
8082 -- number of discriminants and each discriminant of the
8083 -- actual must correspond to a discriminant of the formal.
8085 elsif Has_Discriminants (Act_T)
8086 and then not Has_Unknown_Discriminants (Act_T)
8087 and then Has_Discriminants (Ancestor)
8088 then
8089 Actual_Discr := First_Discriminant (Act_T);
8090 Ancestor_Discr := First_Discriminant (Ancestor);
8091 while Present (Actual_Discr)
8092 and then Present (Ancestor_Discr)
8093 loop
8094 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
8095 not Present (Corresponding_Discriminant (Actual_Discr))
8096 then
8097 Error_Msg_NE
8098 ("discriminant & does not correspond " &
8099 "to ancestor discriminant", Actual, Actual_Discr);
8100 Abandon_Instantiation (Actual);
8101 end if;
8103 Next_Discriminant (Actual_Discr);
8104 Next_Discriminant (Ancestor_Discr);
8105 end loop;
8107 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
8108 Error_Msg_NE
8109 ("actual for & must have same number of discriminants",
8110 Actual, Gen_T);
8111 Abandon_Instantiation (Actual);
8112 end if;
8114 -- This case should be caught by the earlier check for
8115 -- for constrainedness, but the check here is added for
8116 -- completeness.
8118 elsif Has_Discriminants (Act_T)
8119 and then not Has_Unknown_Discriminants (Act_T)
8120 then
8121 Error_Msg_NE
8122 ("actual for & must not have discriminants", Actual, Gen_T);
8123 Abandon_Instantiation (Actual);
8125 elsif Has_Discriminants (Ancestor) then
8126 Error_Msg_NE
8127 ("actual for & must have known discriminants", Actual, Gen_T);
8128 Abandon_Instantiation (Actual);
8129 end if;
8131 if not Subtypes_Statically_Compatible (Act_T, Ancestor) then
8132 Error_Msg_N
8133 ("constraint on actual is incompatible with formal", Actual);
8134 Abandon_Instantiation (Actual);
8135 end if;
8136 end if;
8137 end Validate_Derived_Type_Instance;
8139 ------------------------------------
8140 -- Validate_Private_Type_Instance --
8141 ------------------------------------
8143 procedure Validate_Private_Type_Instance is
8144 Formal_Discr : Entity_Id;
8145 Actual_Discr : Entity_Id;
8146 Formal_Subt : Entity_Id;
8148 begin
8149 if Is_Limited_Type (Act_T)
8150 and then not Is_Limited_Type (A_Gen_T)
8151 then
8152 Error_Msg_NE
8153 ("actual for non-limited & cannot be a limited type", Actual,
8154 Gen_T);
8155 Explain_Limited_Type (Act_T, Actual);
8156 Abandon_Instantiation (Actual);
8158 elsif Is_Indefinite_Subtype (Act_T)
8159 and then not Is_Indefinite_Subtype (A_Gen_T)
8160 and then Ada_Version >= Ada_95
8161 then
8162 Error_Msg_NE
8163 ("actual for & must be a definite subtype", Actual, Gen_T);
8165 elsif not Is_Tagged_Type (Act_T)
8166 and then Is_Tagged_Type (A_Gen_T)
8167 then
8168 Error_Msg_NE
8169 ("actual for & must be a tagged type", Actual, Gen_T);
8171 elsif Has_Discriminants (A_Gen_T) then
8172 if not Has_Discriminants (Act_T) then
8173 Error_Msg_NE
8174 ("actual for & must have discriminants", Actual, Gen_T);
8175 Abandon_Instantiation (Actual);
8177 elsif Is_Constrained (Act_T) then
8178 Error_Msg_NE
8179 ("actual for & must be unconstrained", Actual, Gen_T);
8180 Abandon_Instantiation (Actual);
8182 else
8183 Formal_Discr := First_Discriminant (A_Gen_T);
8184 Actual_Discr := First_Discriminant (Act_T);
8185 while Formal_Discr /= Empty loop
8186 if Actual_Discr = Empty then
8187 Error_Msg_NE
8188 ("discriminants on actual do not match formal",
8189 Actual, Gen_T);
8190 Abandon_Instantiation (Actual);
8191 end if;
8193 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
8195 -- access discriminants match if designated types do.
8197 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
8198 and then (Ekind (Base_Type (Etype (Actual_Discr))))
8199 = E_Anonymous_Access_Type
8200 and then Get_Instance_Of (
8201 Designated_Type (Base_Type (Formal_Subt)))
8202 = Designated_Type (Base_Type (Etype (Actual_Discr)))
8203 then
8204 null;
8206 elsif Base_Type (Formal_Subt) /=
8207 Base_Type (Etype (Actual_Discr))
8208 then
8209 Error_Msg_NE
8210 ("types of actual discriminants must match formal",
8211 Actual, Gen_T);
8212 Abandon_Instantiation (Actual);
8214 elsif not Subtypes_Statically_Match
8215 (Formal_Subt, Etype (Actual_Discr))
8216 and then Ada_Version >= Ada_95
8217 then
8218 Error_Msg_NE
8219 ("subtypes of actual discriminants must match formal",
8220 Actual, Gen_T);
8221 Abandon_Instantiation (Actual);
8222 end if;
8224 Next_Discriminant (Formal_Discr);
8225 Next_Discriminant (Actual_Discr);
8226 end loop;
8228 if Actual_Discr /= Empty then
8229 Error_Msg_NE
8230 ("discriminants on actual do not match formal",
8231 Actual, Gen_T);
8232 Abandon_Instantiation (Actual);
8233 end if;
8234 end if;
8236 end if;
8238 Ancestor := Gen_T;
8239 end Validate_Private_Type_Instance;
8241 -- Start of processing for Instantiate_Type
8243 begin
8244 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
8245 Error_Msg_N ("duplicate instantiation of generic type", Actual);
8246 return Error;
8248 elsif not Is_Entity_Name (Actual)
8249 or else not Is_Type (Entity (Actual))
8250 then
8251 Error_Msg_NE
8252 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
8253 Abandon_Instantiation (Actual);
8255 else
8256 Act_T := Entity (Actual);
8258 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
8259 -- as a generic actual parameter if the corresponding formal type
8260 -- does not have a known_discriminant_part, or is a formal derived
8261 -- type that is an Unchecked_Union type.
8263 if Is_Unchecked_Union (Base_Type (Act_T)) then
8264 if not Has_Discriminants (A_Gen_T)
8265 or else
8266 (Is_Derived_Type (A_Gen_T)
8267 and then
8268 Is_Unchecked_Union (A_Gen_T))
8269 then
8270 null;
8271 else
8272 Error_Msg_N ("Unchecked_Union cannot be the actual for a" &
8273 " discriminated formal type", Act_T);
8275 end if;
8276 end if;
8278 -- Deal with fixed/floating restrictions
8280 if Is_Floating_Point_Type (Act_T) then
8281 Check_Restriction (No_Floating_Point, Actual);
8282 elsif Is_Fixed_Point_Type (Act_T) then
8283 Check_Restriction (No_Fixed_Point, Actual);
8284 end if;
8286 -- Deal with error of using incomplete type as generic actual
8288 if Ekind (Act_T) = E_Incomplete_Type then
8289 if No (Underlying_Type (Act_T)) then
8290 Error_Msg_N ("premature use of incomplete type", Actual);
8291 Abandon_Instantiation (Actual);
8292 else
8293 Act_T := Full_View (Act_T);
8294 Set_Entity (Actual, Act_T);
8296 if Has_Private_Component (Act_T) then
8297 Error_Msg_N
8298 ("premature use of type with private component", Actual);
8299 end if;
8300 end if;
8302 -- Deal with error of premature use of private type as generic actual
8304 elsif Is_Private_Type (Act_T)
8305 and then Is_Private_Type (Base_Type (Act_T))
8306 and then not Is_Generic_Type (Act_T)
8307 and then not Is_Derived_Type (Act_T)
8308 and then No (Full_View (Root_Type (Act_T)))
8309 then
8310 Error_Msg_N ("premature use of private type", Actual);
8312 elsif Has_Private_Component (Act_T) then
8313 Error_Msg_N
8314 ("premature use of type with private component", Actual);
8315 end if;
8317 Set_Instance_Of (A_Gen_T, Act_T);
8319 -- If the type is generic, the class-wide type may also be used
8321 if Is_Tagged_Type (A_Gen_T)
8322 and then Is_Tagged_Type (Act_T)
8323 and then not Is_Class_Wide_Type (A_Gen_T)
8324 then
8325 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
8326 Class_Wide_Type (Act_T));
8327 end if;
8329 if not Is_Abstract (A_Gen_T)
8330 and then Is_Abstract (Act_T)
8331 then
8332 Error_Msg_N
8333 ("actual of non-abstract formal cannot be abstract", Actual);
8334 end if;
8336 if Is_Scalar_Type (Gen_T) then
8337 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
8338 end if;
8339 end if;
8341 case Nkind (Def) is
8342 when N_Formal_Private_Type_Definition =>
8343 Validate_Private_Type_Instance;
8345 when N_Formal_Derived_Type_Definition =>
8346 Validate_Derived_Type_Instance;
8348 when N_Formal_Discrete_Type_Definition =>
8349 if not Is_Discrete_Type (Act_T) then
8350 Error_Msg_NE
8351 ("expect discrete type in instantiation of&", Actual, Gen_T);
8352 Abandon_Instantiation (Actual);
8353 end if;
8355 when N_Formal_Signed_Integer_Type_Definition =>
8356 if not Is_Signed_Integer_Type (Act_T) then
8357 Error_Msg_NE
8358 ("expect signed integer type in instantiation of&",
8359 Actual, Gen_T);
8360 Abandon_Instantiation (Actual);
8361 end if;
8363 when N_Formal_Modular_Type_Definition =>
8364 if not Is_Modular_Integer_Type (Act_T) then
8365 Error_Msg_NE
8366 ("expect modular type in instantiation of &", Actual, Gen_T);
8367 Abandon_Instantiation (Actual);
8368 end if;
8370 when N_Formal_Floating_Point_Definition =>
8371 if not Is_Floating_Point_Type (Act_T) then
8372 Error_Msg_NE
8373 ("expect float type in instantiation of &", Actual, Gen_T);
8374 Abandon_Instantiation (Actual);
8375 end if;
8377 when N_Formal_Ordinary_Fixed_Point_Definition =>
8378 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
8379 Error_Msg_NE
8380 ("expect ordinary fixed point type in instantiation of &",
8381 Actual, Gen_T);
8382 Abandon_Instantiation (Actual);
8383 end if;
8385 when N_Formal_Decimal_Fixed_Point_Definition =>
8386 if not Is_Decimal_Fixed_Point_Type (Act_T) then
8387 Error_Msg_NE
8388 ("expect decimal type in instantiation of &",
8389 Actual, Gen_T);
8390 Abandon_Instantiation (Actual);
8391 end if;
8393 when N_Array_Type_Definition =>
8394 Validate_Array_Type_Instance;
8396 when N_Access_To_Object_Definition =>
8397 Validate_Access_Type_Instance;
8399 when N_Access_Function_Definition |
8400 N_Access_Procedure_Definition =>
8401 Validate_Access_Subprogram_Instance;
8403 when others =>
8404 raise Program_Error;
8406 end case;
8408 Decl_Node :=
8409 Make_Subtype_Declaration (Loc,
8410 Defining_Identifier => New_Copy (Gen_T),
8411 Subtype_Indication => New_Reference_To (Act_T, Loc));
8413 if Is_Private_Type (Act_T) then
8414 Set_Has_Private_View (Subtype_Indication (Decl_Node));
8416 elsif Is_Access_Type (Act_T)
8417 and then Is_Private_Type (Designated_Type (Act_T))
8418 then
8419 Set_Has_Private_View (Subtype_Indication (Decl_Node));
8420 end if;
8422 -- Flag actual derived types so their elaboration produces the
8423 -- appropriate renamings for the primitive operations of the ancestor.
8424 -- Flag actual for formal private types as well, to determine whether
8425 -- operations in the private part may override inherited operations.
8427 if Nkind (Def) = N_Formal_Derived_Type_Definition
8428 or else Nkind (Def) = N_Formal_Private_Type_Definition
8429 then
8430 Set_Generic_Parent_Type (Decl_Node, Ancestor);
8431 end if;
8433 return Decl_Node;
8434 end Instantiate_Type;
8436 ---------------------
8437 -- Is_In_Main_Unit --
8438 ---------------------
8440 function Is_In_Main_Unit (N : Node_Id) return Boolean is
8441 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
8442 Current_Unit : Node_Id;
8444 begin
8445 if Unum = Main_Unit then
8446 return True;
8448 -- If the current unit is a subunit then it is either the main unit
8449 -- or is being compiled as part of the main unit.
8451 elsif Nkind (N) = N_Compilation_Unit then
8452 return Nkind (Unit (N)) = N_Subunit;
8453 end if;
8455 Current_Unit := Parent (N);
8456 while Present (Current_Unit)
8457 and then Nkind (Current_Unit) /= N_Compilation_Unit
8458 loop
8459 Current_Unit := Parent (Current_Unit);
8460 end loop;
8462 -- The instantiation node is in the main unit, or else the current
8463 -- node (perhaps as the result of nested instantiations) is in the
8464 -- main unit, or in the declaration of the main unit, which in this
8465 -- last case must be a body.
8467 return Unum = Main_Unit
8468 or else Current_Unit = Cunit (Main_Unit)
8469 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
8470 or else (Present (Library_Unit (Current_Unit))
8471 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
8472 end Is_In_Main_Unit;
8474 ----------------------------
8475 -- Load_Parent_Of_Generic --
8476 ----------------------------
8478 procedure Load_Parent_Of_Generic (N : Node_Id; Spec : Node_Id) is
8479 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
8480 Save_Style_Check : constant Boolean := Style_Check;
8481 True_Parent : Node_Id;
8482 Inst_Node : Node_Id;
8483 OK : Boolean;
8485 begin
8486 if not In_Same_Source_Unit (N, Spec)
8487 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
8488 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
8489 and then not Is_In_Main_Unit (Spec))
8490 then
8491 -- Find body of parent of spec, and analyze it. A special case
8492 -- arises when the parent is an instantiation, that is to say when
8493 -- we are currently instantiating a nested generic. In that case,
8494 -- there is no separate file for the body of the enclosing instance.
8495 -- Instead, the enclosing body must be instantiated as if it were
8496 -- a pending instantiation, in order to produce the body for the
8497 -- nested generic we require now. Note that in that case the
8498 -- generic may be defined in a package body, the instance defined
8499 -- in the same package body, and the original enclosing body may not
8500 -- be in the main unit.
8502 True_Parent := Parent (Spec);
8503 Inst_Node := Empty;
8505 while Present (True_Parent)
8506 and then Nkind (True_Parent) /= N_Compilation_Unit
8507 loop
8508 if Nkind (True_Parent) = N_Package_Declaration
8509 and then
8510 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
8511 then
8512 -- Parent is a compilation unit that is an instantiation.
8513 -- Instantiation node has been replaced with package decl.
8515 Inst_Node := Original_Node (True_Parent);
8516 exit;
8518 elsif Nkind (True_Parent) = N_Package_Declaration
8519 and then Present (Generic_Parent (Specification (True_Parent)))
8520 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
8521 then
8522 -- Parent is an instantiation within another specification.
8523 -- Declaration for instance has been inserted before original
8524 -- instantiation node. A direct link would be preferable?
8526 Inst_Node := Next (True_Parent);
8528 while Present (Inst_Node)
8529 and then Nkind (Inst_Node) /= N_Package_Instantiation
8530 loop
8531 Next (Inst_Node);
8532 end loop;
8534 -- If the instance appears within a generic, and the generic
8535 -- unit is defined within a formal package of the enclosing
8536 -- generic, there is no generic body available, and none
8537 -- needed. A more precise test should be used ???
8539 if No (Inst_Node) then
8540 return;
8541 end if;
8543 exit;
8544 else
8545 True_Parent := Parent (True_Parent);
8546 end if;
8547 end loop;
8549 -- Case where we are currently instantiating a nested generic
8551 if Present (Inst_Node) then
8552 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
8554 -- Instantiation node and declaration of instantiated package
8555 -- were exchanged when only the declaration was needed.
8556 -- Restore instantiation node before proceeding with body.
8558 Set_Unit (Parent (True_Parent), Inst_Node);
8559 end if;
8561 -- Now complete instantiation of enclosing body, if it appears
8562 -- in some other unit. If it appears in the current unit, the
8563 -- body will have been instantiated already.
8565 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
8567 -- We need to determine the expander mode to instantiate
8568 -- the enclosing body. Because the generic body we need
8569 -- may use global entities declared in the enclosing package
8570 -- (including aggregates) it is in general necessary to
8571 -- compile this body with expansion enabled. The exception
8572 -- is if we are within a generic package, in which case
8573 -- the usual generic rule applies.
8575 declare
8576 Exp_Status : Boolean := True;
8577 Scop : Entity_Id;
8579 begin
8580 -- Loop through scopes looking for generic package
8582 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
8583 while Present (Scop)
8584 and then Scop /= Standard_Standard
8585 loop
8586 if Ekind (Scop) = E_Generic_Package then
8587 Exp_Status := False;
8588 exit;
8589 end if;
8591 Scop := Scope (Scop);
8592 end loop;
8594 Instantiate_Package_Body
8595 (Pending_Body_Info'(
8596 Inst_Node, True_Parent, Exp_Status,
8597 Get_Code_Unit (Sloc (Inst_Node))));
8598 end;
8599 end if;
8601 -- Case where we are not instantiating a nested generic
8603 else
8604 Opt.Style_Check := False;
8605 Expander_Mode_Save_And_Set (True);
8606 Load_Needed_Body (Comp_Unit, OK);
8607 Opt.Style_Check := Save_Style_Check;
8608 Expander_Mode_Restore;
8610 if not OK
8611 and then Unit_Requires_Body (Defining_Entity (Spec))
8612 then
8613 declare
8614 Bname : constant Unit_Name_Type :=
8615 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
8617 begin
8618 Error_Msg_Unit_1 := Bname;
8619 Error_Msg_N ("this instantiation requires$!", N);
8620 Error_Msg_Name_1 :=
8621 Get_File_Name (Bname, Subunit => False);
8622 Error_Msg_N ("\but file{ was not found!", N);
8623 raise Unrecoverable_Error;
8624 end;
8625 end if;
8626 end if;
8627 end if;
8629 -- If loading the parent of the generic caused an instantiation
8630 -- circularity, we abandon compilation at this point, because
8631 -- otherwise in some cases we get into trouble with infinite
8632 -- recursions after this point.
8634 if Circularity_Detected then
8635 raise Unrecoverable_Error;
8636 end if;
8637 end Load_Parent_Of_Generic;
8639 -----------------------
8640 -- Move_Freeze_Nodes --
8641 -----------------------
8643 procedure Move_Freeze_Nodes
8644 (Out_Of : Entity_Id;
8645 After : Node_Id;
8646 L : List_Id)
8648 Decl : Node_Id;
8649 Next_Decl : Node_Id;
8650 Next_Node : Node_Id := After;
8651 Spec : Node_Id;
8653 function Is_Outer_Type (T : Entity_Id) return Boolean;
8654 -- Check whether entity is declared in a scope external to that
8655 -- of the generic unit.
8657 -------------------
8658 -- Is_Outer_Type --
8659 -------------------
8661 function Is_Outer_Type (T : Entity_Id) return Boolean is
8662 Scop : Entity_Id := Scope (T);
8664 begin
8665 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
8666 return True;
8668 else
8669 while Scop /= Standard_Standard loop
8671 if Scop = Out_Of then
8672 return False;
8673 else
8674 Scop := Scope (Scop);
8675 end if;
8676 end loop;
8678 return True;
8679 end if;
8680 end Is_Outer_Type;
8682 -- Start of processing for Move_Freeze_Nodes
8684 begin
8685 if No (L) then
8686 return;
8687 end if;
8689 -- First remove the freeze nodes that may appear before all other
8690 -- declarations.
8692 Decl := First (L);
8693 while Present (Decl)
8694 and then Nkind (Decl) = N_Freeze_Entity
8695 and then Is_Outer_Type (Entity (Decl))
8696 loop
8697 Decl := Remove_Head (L);
8698 Insert_After (Next_Node, Decl);
8699 Set_Analyzed (Decl, False);
8700 Next_Node := Decl;
8701 Decl := First (L);
8702 end loop;
8704 -- Next scan the list of declarations and remove each freeze node that
8705 -- appears ahead of the current node.
8707 while Present (Decl) loop
8708 while Present (Next (Decl))
8709 and then Nkind (Next (Decl)) = N_Freeze_Entity
8710 and then Is_Outer_Type (Entity (Next (Decl)))
8711 loop
8712 Next_Decl := Remove_Next (Decl);
8713 Insert_After (Next_Node, Next_Decl);
8714 Set_Analyzed (Next_Decl, False);
8715 Next_Node := Next_Decl;
8716 end loop;
8718 -- If the declaration is a nested package or concurrent type, then
8719 -- recurse. Nested generic packages will have been processed from the
8720 -- inside out.
8722 if Nkind (Decl) = N_Package_Declaration then
8723 Spec := Specification (Decl);
8725 elsif Nkind (Decl) = N_Task_Type_Declaration then
8726 Spec := Task_Definition (Decl);
8728 elsif Nkind (Decl) = N_Protected_Type_Declaration then
8729 Spec := Protected_Definition (Decl);
8731 else
8732 Spec := Empty;
8733 end if;
8735 if Present (Spec) then
8736 Move_Freeze_Nodes (Out_Of, Next_Node,
8737 Visible_Declarations (Spec));
8738 Move_Freeze_Nodes (Out_Of, Next_Node,
8739 Private_Declarations (Spec));
8740 end if;
8742 Next (Decl);
8743 end loop;
8744 end Move_Freeze_Nodes;
8746 ----------------
8747 -- Next_Assoc --
8748 ----------------
8750 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
8751 begin
8752 return Generic_Renamings.Table (E).Next_In_HTable;
8753 end Next_Assoc;
8755 ------------------------
8756 -- Preanalyze_Actuals --
8757 ------------------------
8759 procedure Pre_Analyze_Actuals (N : Node_Id) is
8760 Assoc : Node_Id;
8761 Act : Node_Id;
8762 Errs : constant Int := Serious_Errors_Detected;
8764 begin
8765 Assoc := First (Generic_Associations (N));
8767 while Present (Assoc) loop
8768 Act := Explicit_Generic_Actual_Parameter (Assoc);
8770 -- Within a nested instantiation, a defaulted actual is an
8771 -- empty association, so nothing to analyze. If the actual for
8772 -- a subprogram is an attribute, analyze prefix only, because
8773 -- actual is not a complete attribute reference.
8775 -- If actual is an allocator, analyze expression only. The full
8776 -- analysis can generate code, and if the instance is a compilation
8777 -- unit we have to wait until the package instance is installed to
8778 -- have a proper place to insert this code.
8780 -- String literals may be operators, but at this point we do not
8781 -- know whether the actual is a formal subprogram or a string.
8783 if No (Act) then
8784 null;
8786 elsif Nkind (Act) = N_Attribute_Reference then
8787 Analyze (Prefix (Act));
8789 elsif Nkind (Act) = N_Explicit_Dereference then
8790 Analyze (Prefix (Act));
8792 elsif Nkind (Act) = N_Allocator then
8793 declare
8794 Expr : constant Node_Id := Expression (Act);
8796 begin
8797 if Nkind (Expr) = N_Subtype_Indication then
8798 Analyze (Subtype_Mark (Expr));
8799 Analyze_List (Constraints (Constraint (Expr)));
8800 else
8801 Analyze (Expr);
8802 end if;
8803 end;
8805 elsif Nkind (Act) /= N_Operator_Symbol then
8806 Analyze (Act);
8807 end if;
8809 if Errs /= Serious_Errors_Detected then
8810 Abandon_Instantiation (Act);
8811 end if;
8813 Next (Assoc);
8814 end loop;
8815 end Pre_Analyze_Actuals;
8817 -------------------
8818 -- Remove_Parent --
8819 -------------------
8821 procedure Remove_Parent (In_Body : Boolean := False) is
8822 S : Entity_Id := Current_Scope;
8823 E : Entity_Id;
8824 P : Entity_Id;
8825 Hidden : Elmt_Id;
8827 begin
8828 -- After child instantiation is complete, remove from scope stack
8829 -- the extra copy of the current scope, and then remove parent
8830 -- instances.
8832 if not In_Body then
8833 Pop_Scope;
8835 while Current_Scope /= S loop
8836 P := Current_Scope;
8837 End_Package_Scope (Current_Scope);
8839 if In_Open_Scopes (P) then
8840 E := First_Entity (P);
8842 while Present (E) loop
8843 Set_Is_Immediately_Visible (E, True);
8844 Next_Entity (E);
8845 end loop;
8847 if Is_Generic_Instance (Current_Scope)
8848 and then P /= Current_Scope
8849 then
8850 -- We are within an instance of some sibling. Retain
8851 -- visibility of parent, for proper subsequent cleanup.
8853 Set_In_Private_Part (P);
8854 end if;
8856 -- This looks incomplete: what about compilation units that
8857 -- were made visible by Install_Parent but should not remain
8858 -- visible??? Standard is on the scope stack.
8860 elsif not In_Open_Scopes (Scope (P)) then
8861 Set_Is_Immediately_Visible (P, False);
8862 end if;
8863 end loop;
8865 -- Reset visibility of entities in the enclosing scope.
8867 Set_Is_Hidden_Open_Scope (Current_Scope, False);
8868 Hidden := First_Elmt (Hidden_Entities);
8870 while Present (Hidden) loop
8871 Set_Is_Immediately_Visible (Node (Hidden), True);
8872 Next_Elmt (Hidden);
8873 end loop;
8875 else
8876 -- Each body is analyzed separately, and there is no context
8877 -- that needs preserving from one body instance to the next,
8878 -- so remove all parent scopes that have been installed.
8880 while Present (S) loop
8881 End_Package_Scope (S);
8882 Set_Is_Immediately_Visible (S, False);
8883 S := Current_Scope;
8884 exit when S = Standard_Standard;
8885 end loop;
8886 end if;
8888 end Remove_Parent;
8890 -----------------
8891 -- Restore_Env --
8892 -----------------
8894 procedure Restore_Env is
8895 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
8897 begin
8898 Ada_Version := Saved.Ada_Version;
8900 if No (Current_Instantiated_Parent.Act_Id) then
8902 -- Restore environment after subprogram inlining
8904 Restore_Private_Views (Empty);
8905 end if;
8907 Current_Instantiated_Parent := Saved.Instantiated_Parent;
8908 Exchanged_Views := Saved.Exchanged_Views;
8909 Hidden_Entities := Saved.Hidden_Entities;
8910 Current_Sem_Unit := Saved.Current_Sem_Unit;
8912 Instance_Envs.Decrement_Last;
8913 end Restore_Env;
8915 ---------------------------
8916 -- Restore_Private_Views --
8917 ---------------------------
8919 procedure Restore_Private_Views
8920 (Pack_Id : Entity_Id;
8921 Is_Package : Boolean := True)
8923 M : Elmt_Id;
8924 E : Entity_Id;
8925 Typ : Entity_Id;
8926 Dep_Elmt : Elmt_Id;
8927 Dep_Typ : Node_Id;
8929 procedure Restore_Nested_Formal (Formal : Entity_Id);
8930 -- Hide the generic formals of formal packages declared with box
8931 -- which were reachable in the current instantiation.
8933 procedure Restore_Nested_Formal (Formal : Entity_Id) is
8934 Ent : Entity_Id;
8935 begin
8936 if Present (Renamed_Object (Formal))
8937 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
8938 then
8939 return;
8941 elsif Present (Associated_Formal_Package (Formal))
8942 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
8943 then
8944 Ent := First_Entity (Formal);
8946 while Present (Ent) loop
8947 exit when Ekind (Ent) = E_Package
8948 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
8950 Set_Is_Hidden (Ent);
8951 Set_Is_Potentially_Use_Visible (Ent, False);
8953 if Ekind (Ent) = E_Package then
8954 -- Recurse.
8955 Restore_Nested_Formal (Ent);
8956 end if;
8958 Next_Entity (Ent);
8959 end loop;
8960 end if;
8961 end Restore_Nested_Formal;
8963 begin
8964 M := First_Elmt (Exchanged_Views);
8965 while Present (M) loop
8966 Typ := Node (M);
8968 -- Subtypes of types whose views have been exchanged, and that
8969 -- are defined within the instance, were not on the list of
8970 -- Private_Dependents on entry to the instance, so they have to
8971 -- be exchanged explicitly now, in order to remain consistent with
8972 -- the view of the parent type.
8974 if Ekind (Typ) = E_Private_Type
8975 or else Ekind (Typ) = E_Limited_Private_Type
8976 or else Ekind (Typ) = E_Record_Type_With_Private
8977 then
8978 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
8980 while Present (Dep_Elmt) loop
8981 Dep_Typ := Node (Dep_Elmt);
8983 if Scope (Dep_Typ) = Pack_Id
8984 and then Present (Full_View (Dep_Typ))
8985 then
8986 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
8987 Exchange_Declarations (Dep_Typ);
8988 end if;
8990 Next_Elmt (Dep_Elmt);
8991 end loop;
8992 end if;
8994 Exchange_Declarations (Node (M));
8995 Next_Elmt (M);
8996 end loop;
8998 if No (Pack_Id) then
8999 return;
9000 end if;
9002 -- Make the generic formal parameters private, and make the formal
9003 -- types into subtypes of the actuals again.
9005 E := First_Entity (Pack_Id);
9007 while Present (E) loop
9008 Set_Is_Hidden (E, True);
9010 if Is_Type (E)
9011 and then Nkind (Parent (E)) = N_Subtype_Declaration
9012 then
9013 Set_Is_Generic_Actual_Type (E, False);
9015 -- An unusual case of aliasing: the actual may also be directly
9016 -- visible in the generic, and be private there, while it is
9017 -- fully visible in the context of the instance. The internal
9018 -- subtype is private in the instance, but has full visibility
9019 -- like its parent in the enclosing scope. This enforces the
9020 -- invariant that the privacy status of all private dependents of
9021 -- a type coincide with that of the parent type. This can only
9022 -- happen when a generic child unit is instantiated within a
9023 -- sibling.
9025 if Is_Private_Type (E)
9026 and then not Is_Private_Type (Etype (E))
9027 then
9028 Exchange_Declarations (E);
9029 end if;
9031 elsif Ekind (E) = E_Package then
9033 -- The end of the renaming list is the renaming of the generic
9034 -- package itself. If the instance is a subprogram, all entities
9035 -- in the corresponding package are renamings. If this entity is
9036 -- a formal package, make its own formals private as well. The
9037 -- actual in this case is itself the renaming of an instantation.
9038 -- If the entity is not a package renaming, it is the entity
9039 -- created to validate formal package actuals: ignore.
9041 -- If the actual is itself a formal package for the enclosing
9042 -- generic, or the actual for such a formal package, it remains
9043 -- visible on exit from the instance, and therefore nothing
9044 -- needs to be done either, except to keep it accessible.
9046 if Is_Package
9047 and then Renamed_Object (E) = Pack_Id
9048 then
9049 exit;
9051 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
9052 null;
9054 elsif Denotes_Formal_Package (Renamed_Object (E), True) then
9055 Set_Is_Hidden (E, False);
9057 else
9058 declare
9059 Act_P : constant Entity_Id := Renamed_Object (E);
9060 Id : Entity_Id;
9062 begin
9063 Id := First_Entity (Act_P);
9064 while Present (Id)
9065 and then Id /= First_Private_Entity (Act_P)
9066 loop
9067 exit when Ekind (Id) = E_Package
9068 and then Renamed_Object (Id) = Act_P;
9070 Set_Is_Hidden (Id, True);
9071 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
9073 if Ekind (Id) = E_Package then
9074 Restore_Nested_Formal (Id);
9075 end if;
9077 Next_Entity (Id);
9078 end loop;
9079 end;
9080 end if;
9081 end if;
9083 Next_Entity (E);
9084 end loop;
9085 end Restore_Private_Views;
9087 --------------
9088 -- Save_Env --
9089 --------------
9091 procedure Save_Env
9092 (Gen_Unit : Entity_Id;
9093 Act_Unit : Entity_Id)
9095 begin
9096 Init_Env;
9097 Set_Instance_Env (Gen_Unit, Act_Unit);
9098 end Save_Env;
9100 ----------------------------
9101 -- Save_Global_References --
9102 ----------------------------
9104 procedure Save_Global_References (N : Node_Id) is
9105 Gen_Scope : Entity_Id;
9106 E : Entity_Id;
9107 N2 : Node_Id;
9109 function Is_Global (E : Entity_Id) return Boolean;
9110 -- Check whether entity is defined outside of generic unit.
9111 -- Examine the scope of an entity, and the scope of the scope,
9112 -- etc, until we find either Standard, in which case the entity
9113 -- is global, or the generic unit itself, which indicates that
9114 -- the entity is local. If the entity is the generic unit itself,
9115 -- as in the case of a recursive call, or the enclosing generic unit,
9116 -- if different from the current scope, then it is local as well,
9117 -- because it will be replaced at the point of instantiation. On
9118 -- the other hand, if it is a reference to a child unit of a common
9119 -- ancestor, which appears in an instantiation, it is global because
9120 -- it is used to denote a specific compilation unit at the time the
9121 -- instantiations will be analyzed.
9123 procedure Reset_Entity (N : Node_Id);
9124 -- Save semantic information on global entity, so that it is not
9125 -- resolved again at instantiation time.
9127 procedure Save_Entity_Descendants (N : Node_Id);
9128 -- Apply Save_Global_References to the two syntactic descendants of
9129 -- non-terminal nodes that carry an Associated_Node and are processed
9130 -- through Reset_Entity. Once the global entity (if any) has been
9131 -- captured together with its type, only two syntactic descendants
9132 -- need to be traversed to complete the processing of the tree rooted
9133 -- at N. This applies to Selected_Components, Expanded_Names, and to
9134 -- Operator nodes. N can also be a character literal, identifier, or
9135 -- operator symbol node, but the call has no effect in these cases.
9137 procedure Save_Global_Defaults (N1, N2 : Node_Id);
9138 -- Default actuals in nested instances must be handled specially
9139 -- because there is no link to them from the original tree. When an
9140 -- actual subprogram is given by a default, we add an explicit generic
9141 -- association for it in the instantiation node. When we save the
9142 -- global references on the name of the instance, we recover the list
9143 -- of generic associations, and add an explicit one to the original
9144 -- generic tree, through which a global actual can be preserved.
9145 -- Similarly, if a child unit is instantiated within a sibling, in the
9146 -- context of the parent, we must preserve the identifier of the parent
9147 -- so that it can be properly resolved in a subsequent instantiation.
9149 procedure Save_Global_Descendant (D : Union_Id);
9150 -- Apply Save_Global_References recursively to the descendents of
9151 -- current node.
9153 procedure Save_References (N : Node_Id);
9154 -- This is the recursive procedure that does the work, once the
9155 -- enclosing generic scope has been established.
9157 ---------------
9158 -- Is_Global --
9159 ---------------
9161 function Is_Global (E : Entity_Id) return Boolean is
9162 Se : Entity_Id := Scope (E);
9164 function Is_Instance_Node (Decl : Node_Id) return Boolean;
9165 -- Determine whether the parent node of a reference to a child unit
9166 -- denotes an instantiation or a formal package, in which case the
9167 -- reference to the child unit is global, even if it appears within
9168 -- the current scope (e.g. when the instance appears within the body
9169 -- of an ancestor).
9171 ----------------------
9172 -- Is_Instance_Node --
9173 ----------------------
9175 function Is_Instance_Node (Decl : Node_Id) return Boolean is
9176 begin
9177 return (Nkind (Decl) in N_Generic_Instantiation
9178 or else
9179 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration);
9180 end Is_Instance_Node;
9182 -- Start of processing for Is_Global
9184 begin
9185 if E = Gen_Scope then
9186 return False;
9188 elsif E = Standard_Standard then
9189 return True;
9191 elsif Is_Child_Unit (E)
9192 and then (Is_Instance_Node (Parent (N2))
9193 or else (Nkind (Parent (N2)) = N_Expanded_Name
9194 and then N2 = Selector_Name (Parent (N2))
9195 and then Is_Instance_Node (Parent (Parent (N2)))))
9196 then
9197 return True;
9199 else
9200 while Se /= Gen_Scope loop
9201 if Se = Standard_Standard then
9202 return True;
9203 else
9204 Se := Scope (Se);
9205 end if;
9206 end loop;
9208 return False;
9209 end if;
9210 end Is_Global;
9212 ------------------
9213 -- Reset_Entity --
9214 ------------------
9216 procedure Reset_Entity (N : Node_Id) is
9218 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
9219 -- The type of N2 is global to the generic unit. Save the
9220 -- type in the generic node.
9222 function Top_Ancestor (E : Entity_Id) return Entity_Id;
9223 -- Find the ultimate ancestor of the current unit. If it is
9224 -- not a generic unit, then the name of the current unit
9225 -- in the prefix of an expanded name must be replaced with
9226 -- its generic homonym to ensure that it will be properly
9227 -- resolved in an instance.
9229 ---------------------
9230 -- Set_Global_Type --
9231 ---------------------
9233 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
9234 Typ : constant Entity_Id := Etype (N2);
9236 begin
9237 Set_Etype (N, Typ);
9239 if Entity (N) /= N2
9240 and then Has_Private_View (Entity (N))
9241 then
9242 -- If the entity of N is not the associated node, this is
9243 -- a nested generic and it has an associated node as well,
9244 -- whose type is already the full view (see below). Indicate
9245 -- that the original node has a private view.
9247 Set_Has_Private_View (N);
9248 end if;
9250 -- If not a private type, nothing else to do
9252 if not Is_Private_Type (Typ) then
9253 if Is_Array_Type (Typ)
9254 and then Is_Private_Type (Component_Type (Typ))
9255 then
9256 Set_Has_Private_View (N);
9257 end if;
9259 -- If it is a derivation of a private type in a context where
9260 -- no full view is needed, nothing to do either.
9262 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
9263 null;
9265 -- Otherwise mark the type for flipping and use the full_view
9266 -- when available.
9268 else
9269 Set_Has_Private_View (N);
9271 if Present (Full_View (Typ)) then
9272 Set_Etype (N2, Full_View (Typ));
9273 end if;
9274 end if;
9275 end Set_Global_Type;
9277 ------------------
9278 -- Top_Ancestor --
9279 ------------------
9281 function Top_Ancestor (E : Entity_Id) return Entity_Id is
9282 Par : Entity_Id := E;
9284 begin
9285 while Is_Child_Unit (Par) loop
9286 Par := Scope (Par);
9287 end loop;
9289 return Par;
9290 end Top_Ancestor;
9292 -- Start of processing for Reset_Entity
9294 begin
9295 N2 := Get_Associated_Node (N);
9296 E := Entity (N2);
9298 if Present (E) then
9299 if Is_Global (E) then
9300 Set_Global_Type (N, N2);
9302 elsif Nkind (N) = N_Op_Concat
9303 and then Is_Generic_Type (Etype (N2))
9304 and then
9305 (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
9306 or else Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
9307 and then Is_Intrinsic_Subprogram (E)
9308 then
9309 null;
9311 else
9312 -- Entity is local. Mark generic node as unresolved.
9313 -- Note that now it does not have an entity.
9315 Set_Associated_Node (N, Empty);
9316 Set_Etype (N, Empty);
9317 end if;
9319 if (Nkind (Parent (N)) = N_Package_Instantiation
9320 or else Nkind (Parent (N)) = N_Function_Instantiation
9321 or else Nkind (Parent (N)) = N_Procedure_Instantiation)
9322 and then N = Name (Parent (N))
9323 then
9324 Save_Global_Defaults (Parent (N), Parent (N2));
9325 end if;
9327 elsif Nkind (Parent (N)) = N_Selected_Component
9328 and then Nkind (Parent (N2)) = N_Expanded_Name
9329 then
9331 if Is_Global (Entity (Parent (N2))) then
9332 Change_Selected_Component_To_Expanded_Name (Parent (N));
9333 Set_Associated_Node (Parent (N), Parent (N2));
9334 Set_Global_Type (Parent (N), Parent (N2));
9335 Save_Entity_Descendants (N);
9337 -- If this is a reference to the current generic entity,
9338 -- replace by the name of the generic homonym of the current
9339 -- package. This is because in an instantiation Par.P.Q will
9340 -- not resolve to the name of the instance, whose enclosing
9341 -- scope is not necessarily Par. We use the generic homonym
9342 -- rather that the name of the generic itself, because it may
9343 -- be hidden by a local declaration.
9345 elsif In_Open_Scopes (Entity (Parent (N2)))
9346 and then not
9347 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
9348 then
9349 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
9350 Rewrite (Parent (N),
9351 Make_Identifier (Sloc (N),
9352 Chars =>
9353 Chars (Generic_Homonym (Entity (Parent (N2))))));
9354 else
9355 Rewrite (Parent (N),
9356 Make_Identifier (Sloc (N),
9357 Chars => Chars (Selector_Name (Parent (N2)))));
9358 end if;
9359 end if;
9361 if (Nkind (Parent (Parent (N))) = N_Package_Instantiation
9362 or else Nkind (Parent (Parent (N)))
9363 = N_Function_Instantiation
9364 or else Nkind (Parent (Parent (N)))
9365 = N_Procedure_Instantiation)
9366 and then Parent (N) = Name (Parent (Parent (N)))
9367 then
9368 Save_Global_Defaults
9369 (Parent (Parent (N)), Parent (Parent ((N2))));
9370 end if;
9372 -- A selected component may denote a static constant that has
9373 -- been folded. Make the same replacement in original tree.
9375 elsif Nkind (Parent (N)) = N_Selected_Component
9376 and then (Nkind (Parent (N2)) = N_Integer_Literal
9377 or else Nkind (Parent (N2)) = N_Real_Literal)
9378 then
9379 Rewrite (Parent (N),
9380 New_Copy (Parent (N2)));
9381 Set_Analyzed (Parent (N), False);
9383 -- A selected component may be transformed into a parameterless
9384 -- function call. If the called entity is global, rewrite the
9385 -- node appropriately, i.e. as an extended name for the global
9386 -- entity.
9388 elsif Nkind (Parent (N)) = N_Selected_Component
9389 and then Nkind (Parent (N2)) = N_Function_Call
9390 and then Is_Global (Entity (Name (Parent (N2))))
9391 then
9392 Change_Selected_Component_To_Expanded_Name (Parent (N));
9393 Set_Associated_Node (Parent (N), Name (Parent (N2)));
9394 Set_Global_Type (Parent (N), Name (Parent (N2)));
9395 Save_Entity_Descendants (N);
9397 else
9398 -- Entity is local. Reset in generic unit, so that node
9399 -- is resolved anew at the point of instantiation.
9401 Set_Associated_Node (N, Empty);
9402 Set_Etype (N, Empty);
9403 end if;
9404 end Reset_Entity;
9406 -----------------------------
9407 -- Save_Entity_Descendants --
9408 -----------------------------
9410 procedure Save_Entity_Descendants (N : Node_Id) is
9411 begin
9412 case Nkind (N) is
9413 when N_Binary_Op =>
9414 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
9415 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
9417 when N_Unary_Op =>
9418 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
9420 when N_Expanded_Name | N_Selected_Component =>
9421 Save_Global_Descendant (Union_Id (Prefix (N)));
9422 Save_Global_Descendant (Union_Id (Selector_Name (N)));
9424 when N_Identifier | N_Character_Literal | N_Operator_Symbol =>
9425 null;
9427 when others =>
9428 raise Program_Error;
9429 end case;
9430 end Save_Entity_Descendants;
9432 --------------------------
9433 -- Save_Global_Defaults --
9434 --------------------------
9436 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
9437 Loc : constant Source_Ptr := Sloc (N1);
9438 Assoc2 : constant List_Id := Generic_Associations (N2);
9439 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
9440 Assoc1 : List_Id;
9441 Act1 : Node_Id;
9442 Act2 : Node_Id;
9443 Def : Node_Id;
9444 Ndec : Node_Id;
9445 Subp : Entity_Id;
9446 Actual : Entity_Id;
9448 begin
9449 Assoc1 := Generic_Associations (N1);
9451 if Present (Assoc1) then
9452 Act1 := First (Assoc1);
9453 else
9454 Act1 := Empty;
9455 Set_Generic_Associations (N1, New_List);
9456 Assoc1 := Generic_Associations (N1);
9457 end if;
9459 if Present (Assoc2) then
9460 Act2 := First (Assoc2);
9461 else
9462 return;
9463 end if;
9465 while Present (Act1) and then Present (Act2) loop
9466 Next (Act1);
9467 Next (Act2);
9468 end loop;
9470 -- Find the associations added for default suprograms.
9472 if Present (Act2) then
9473 while Nkind (Act2) /= N_Generic_Association
9474 or else No (Entity (Selector_Name (Act2)))
9475 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
9476 loop
9477 Next (Act2);
9478 end loop;
9480 -- Add a similar association if the default is global. The
9481 -- renaming declaration for the actual has been analyzed, and
9482 -- its alias is the program it renames. Link the actual in the
9483 -- original generic tree with the node in the analyzed tree.
9485 while Present (Act2) loop
9486 Subp := Entity (Selector_Name (Act2));
9487 Def := Explicit_Generic_Actual_Parameter (Act2);
9489 -- Following test is defence against rubbish errors
9491 if No (Alias (Subp)) then
9492 return;
9493 end if;
9495 -- Retrieve the resolved actual from the renaming declaration
9496 -- created for the instantiated formal.
9498 Actual := Entity (Name (Parent (Parent (Subp))));
9499 Set_Entity (Def, Actual);
9500 Set_Etype (Def, Etype (Actual));
9502 if Is_Global (Actual) then
9503 Ndec :=
9504 Make_Generic_Association (Loc,
9505 Selector_Name => New_Occurrence_Of (Subp, Loc),
9506 Explicit_Generic_Actual_Parameter =>
9507 New_Occurrence_Of (Actual, Loc));
9509 Set_Associated_Node
9510 (Explicit_Generic_Actual_Parameter (Ndec), Def);
9512 Append (Ndec, Assoc1);
9514 -- If there are other defaults, add a dummy association
9515 -- in case there are other defaulted formals with the same
9516 -- name.
9518 elsif Present (Next (Act2)) then
9519 Ndec :=
9520 Make_Generic_Association (Loc,
9521 Selector_Name => New_Occurrence_Of (Subp, Loc),
9522 Explicit_Generic_Actual_Parameter => Empty);
9524 Append (Ndec, Assoc1);
9525 end if;
9527 Next (Act2);
9528 end loop;
9529 end if;
9531 if Nkind (Name (N1)) = N_Identifier
9532 and then Is_Child_Unit (Gen_Id)
9533 and then Is_Global (Gen_Id)
9534 and then Is_Generic_Unit (Scope (Gen_Id))
9535 and then In_Open_Scopes (Scope (Gen_Id))
9536 then
9537 -- This is an instantiation of a child unit within a sibling,
9538 -- so that the generic parent is in scope. An eventual instance
9539 -- must occur within the scope of an instance of the parent.
9540 -- Make name in instance into an expanded name, to preserve the
9541 -- identifier of the parent, so it can be resolved subsequently.
9543 Rewrite (Name (N2),
9544 Make_Expanded_Name (Loc,
9545 Chars => Chars (Gen_Id),
9546 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
9547 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
9548 Set_Entity (Name (N2), Gen_Id);
9550 Rewrite (Name (N1),
9551 Make_Expanded_Name (Loc,
9552 Chars => Chars (Gen_Id),
9553 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
9554 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
9556 Set_Associated_Node (Name (N1), Name (N2));
9557 Set_Associated_Node (Prefix (Name (N1)), Empty);
9558 Set_Associated_Node
9559 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
9560 Set_Etype (Name (N1), Etype (Gen_Id));
9561 end if;
9563 end Save_Global_Defaults;
9565 ----------------------------
9566 -- Save_Global_Descendant --
9567 ----------------------------
9569 procedure Save_Global_Descendant (D : Union_Id) is
9570 N1 : Node_Id;
9572 begin
9573 if D in Node_Range then
9574 if D = Union_Id (Empty) then
9575 null;
9577 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
9578 Save_References (Node_Id (D));
9579 end if;
9581 elsif D in List_Range then
9582 if D = Union_Id (No_List)
9583 or else Is_Empty_List (List_Id (D))
9584 then
9585 null;
9587 else
9588 N1 := First (List_Id (D));
9589 while Present (N1) loop
9590 Save_References (N1);
9591 Next (N1);
9592 end loop;
9593 end if;
9595 -- Element list or other non-node field, nothing to do
9597 else
9598 null;
9599 end if;
9600 end Save_Global_Descendant;
9602 ---------------------
9603 -- Save_References --
9604 ---------------------
9606 -- This is the recursive procedure that does the work, once the
9607 -- enclosing generic scope has been established. We have to treat
9608 -- specially a number of node rewritings that are required by semantic
9609 -- processing and which change the kind of nodes in the generic copy:
9610 -- typically constant-folding, replacing an operator node by a string
9611 -- literal, or a selected component by an expanded name. In each of
9612 -- those cases, the transformation is propagated to the generic unit.
9614 procedure Save_References (N : Node_Id) is
9615 begin
9616 if N = Empty then
9617 null;
9619 elsif Nkind (N) = N_Character_Literal
9620 or else Nkind (N) = N_Operator_Symbol
9621 then
9622 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
9623 Reset_Entity (N);
9625 elsif Nkind (N) = N_Operator_Symbol
9626 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
9627 then
9628 Change_Operator_Symbol_To_String_Literal (N);
9629 end if;
9631 elsif Nkind (N) in N_Op then
9633 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
9635 if Nkind (N) = N_Op_Concat then
9636 Set_Is_Component_Left_Opnd (N,
9637 Is_Component_Left_Opnd (Get_Associated_Node (N)));
9639 Set_Is_Component_Right_Opnd (N,
9640 Is_Component_Right_Opnd (Get_Associated_Node (N)));
9641 end if;
9643 Reset_Entity (N);
9644 else
9645 -- Node may be transformed into call to a user-defined operator
9647 N2 := Get_Associated_Node (N);
9649 if Nkind (N2) = N_Function_Call then
9650 E := Entity (Name (N2));
9652 if Present (E)
9653 and then Is_Global (E)
9654 then
9655 Set_Etype (N, Etype (N2));
9656 else
9657 Set_Associated_Node (N, Empty);
9658 Set_Etype (N, Empty);
9659 end if;
9661 elsif Nkind (N2) = N_Integer_Literal
9662 or else Nkind (N2) = N_Real_Literal
9663 or else Nkind (N2) = N_String_Literal
9664 then
9665 -- Operation was constant-folded, perform the same
9666 -- replacement in generic.
9668 Rewrite (N, New_Copy (N2));
9669 Set_Analyzed (N, False);
9671 elsif Nkind (N2) = N_Identifier
9672 and then Ekind (Entity (N2)) = E_Enumeration_Literal
9673 then
9674 -- Same if call was folded into a literal, but in this
9675 -- case retain the entity to avoid spurious ambiguities
9676 -- if id is overloaded at the point of instantiation or
9677 -- inlining.
9679 Rewrite (N, New_Copy (N2));
9680 Set_Analyzed (N, False);
9681 end if;
9682 end if;
9684 -- Complete the check on operands, if node has not been
9685 -- constant-folded.
9687 if Nkind (N) in N_Op then
9688 Save_Entity_Descendants (N);
9689 end if;
9691 elsif Nkind (N) = N_Identifier then
9692 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
9694 -- If this is a discriminant reference, always save it.
9695 -- It is used in the instance to find the corresponding
9696 -- discriminant positionally rather than by name.
9698 Set_Original_Discriminant
9699 (N, Original_Discriminant (Get_Associated_Node (N)));
9700 Reset_Entity (N);
9702 else
9703 N2 := Get_Associated_Node (N);
9705 if Nkind (N2) = N_Function_Call then
9706 E := Entity (Name (N2));
9708 -- Name resolves to a call to parameterless function.
9709 -- If original entity is global, mark node as resolved.
9711 if Present (E)
9712 and then Is_Global (E)
9713 then
9714 Set_Etype (N, Etype (N2));
9715 else
9716 Set_Associated_Node (N, Empty);
9717 Set_Etype (N, Empty);
9718 end if;
9720 elsif
9721 Nkind (N2) = N_Integer_Literal or else
9722 Nkind (N2) = N_Real_Literal or else
9723 Nkind (N2) = N_String_Literal
9724 then
9725 -- Name resolves to named number that is constant-folded,
9726 -- or to string literal from concatenation.
9727 -- Perform the same replacement in generic.
9729 Rewrite (N, New_Copy (N2));
9730 Set_Analyzed (N, False);
9732 elsif Nkind (N2) = N_Explicit_Dereference then
9734 -- An identifier is rewritten as a dereference if it is
9735 -- the prefix in a selected component, and it denotes an
9736 -- access to a composite type, or a parameterless function
9737 -- call that returns an access type.
9739 -- Check whether corresponding entity in prefix is global.
9741 if Is_Entity_Name (Prefix (N2))
9742 and then Present (Entity (Prefix (N2)))
9743 and then Is_Global (Entity (Prefix (N2)))
9744 then
9745 Rewrite (N,
9746 Make_Explicit_Dereference (Sloc (N),
9747 Prefix => Make_Identifier (Sloc (N),
9748 Chars => Chars (N))));
9749 Set_Associated_Node (Prefix (N), Prefix (N2));
9751 elsif Nkind (Prefix (N2)) = N_Function_Call
9752 and then Is_Global (Entity (Name (Prefix (N2))))
9753 then
9754 Rewrite (N,
9755 Make_Explicit_Dereference (Sloc (N),
9756 Prefix => Make_Function_Call (Sloc (N),
9757 Name =>
9758 Make_Identifier (Sloc (N),
9759 Chars => Chars (N)))));
9761 Set_Associated_Node
9762 (Name (Prefix (N)), Name (Prefix (N2)));
9764 else
9765 Set_Associated_Node (N, Empty);
9766 Set_Etype (N, Empty);
9767 end if;
9769 -- The subtype mark of a nominally unconstrained object
9770 -- is rewritten as a subtype indication using the bounds
9771 -- of the expression. Recover the original subtype mark.
9773 elsif Nkind (N2) = N_Subtype_Indication
9774 and then Is_Entity_Name (Original_Node (N2))
9775 then
9776 Set_Associated_Node (N, Original_Node (N2));
9777 Reset_Entity (N);
9779 else
9780 null;
9781 end if;
9782 end if;
9784 elsif Nkind (N) in N_Entity then
9785 null;
9787 else
9788 declare
9789 use Atree.Unchecked_Access;
9790 -- This code section is part of implementing an untyped tree
9791 -- traversal, so it needs direct access to node fields.
9793 begin
9794 if Nkind (N) = N_Aggregate
9795 or else
9796 Nkind (N) = N_Extension_Aggregate
9797 then
9798 N2 := Get_Associated_Node (N);
9800 if No (N2)
9801 or else No (Etype (N2))
9802 or else not Is_Global (Etype (N2))
9803 then
9804 Set_Associated_Node (N, Empty);
9805 end if;
9807 Save_Global_Descendant (Field1 (N));
9808 Save_Global_Descendant (Field2 (N));
9809 Save_Global_Descendant (Field3 (N));
9810 Save_Global_Descendant (Field5 (N));
9812 -- All other cases than aggregates
9814 else
9815 Save_Global_Descendant (Field1 (N));
9816 Save_Global_Descendant (Field2 (N));
9817 Save_Global_Descendant (Field3 (N));
9818 Save_Global_Descendant (Field4 (N));
9819 Save_Global_Descendant (Field5 (N));
9820 end if;
9821 end;
9822 end if;
9823 end Save_References;
9825 -- Start of processing for Save_Global_References
9827 begin
9828 Gen_Scope := Current_Scope;
9830 -- If the generic unit is a child unit, references to entities in
9831 -- the parent are treated as local, because they will be resolved
9832 -- anew in the context of the instance of the parent.
9834 while Is_Child_Unit (Gen_Scope)
9835 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
9836 loop
9837 Gen_Scope := Scope (Gen_Scope);
9838 end loop;
9840 Save_References (N);
9841 end Save_Global_References;
9843 --------------------------------------
9844 -- Set_Copied_Sloc_For_Inlined_Body --
9845 --------------------------------------
9847 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
9848 begin
9849 Create_Instantiation_Source (N, E, True, S_Adjustment);
9850 end Set_Copied_Sloc_For_Inlined_Body;
9852 ---------------------
9853 -- Set_Instance_Of --
9854 ---------------------
9856 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
9857 begin
9858 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
9859 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
9860 Generic_Renamings.Increment_Last;
9861 end Set_Instance_Of;
9863 --------------------
9864 -- Set_Next_Assoc --
9865 --------------------
9867 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
9868 begin
9869 Generic_Renamings.Table (E).Next_In_HTable := Next;
9870 end Set_Next_Assoc;
9872 -------------------
9873 -- Start_Generic --
9874 -------------------
9876 procedure Start_Generic is
9877 begin
9878 -- ??? I am sure more things could be factored out in this
9879 -- routine. Should probably be done at a later stage.
9881 Generic_Flags.Increment_Last;
9882 Generic_Flags.Table (Generic_Flags.Last) := Inside_A_Generic;
9883 Inside_A_Generic := True;
9885 Expander_Mode_Save_And_Set (False);
9886 end Start_Generic;
9888 ----------------------
9889 -- Set_Instance_Env --
9890 ----------------------
9892 procedure Set_Instance_Env
9893 (Gen_Unit : Entity_Id;
9894 Act_Unit : Entity_Id)
9897 begin
9898 -- Regardless of the current mode, predefined units are analyzed in
9899 -- the most current Ada mode, and earlier version Ada checks do not
9900 -- apply to predefined units.
9902 if Is_Internal_File_Name
9903 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
9904 Renamings_Included => True) then
9905 Ada_Version := Ada_Version_Type'Last;
9906 end if;
9908 Current_Instantiated_Parent := (Gen_Unit, Act_Unit, Assoc_Null);
9909 end Set_Instance_Env;
9911 -----------------
9912 -- Switch_View --
9913 -----------------
9915 procedure Switch_View (T : Entity_Id) is
9916 BT : constant Entity_Id := Base_Type (T);
9917 Priv_Elmt : Elmt_Id := No_Elmt;
9918 Priv_Sub : Entity_Id;
9920 begin
9921 -- T may be private but its base type may have been exchanged through
9922 -- some other occurrence, in which case there is nothing to switch.
9924 if not Is_Private_Type (BT) then
9925 return;
9926 end if;
9928 Priv_Elmt := First_Elmt (Private_Dependents (BT));
9930 if Present (Full_View (BT)) then
9931 Append_Elmt (Full_View (BT), Exchanged_Views);
9932 Exchange_Declarations (BT);
9933 end if;
9935 while Present (Priv_Elmt) loop
9936 Priv_Sub := (Node (Priv_Elmt));
9938 -- We avoid flipping the subtype if the Etype of its full
9939 -- view is private because this would result in a malformed
9940 -- subtype. This occurs when the Etype of the subtype full
9941 -- view is the full view of the base type (and since the
9942 -- base types were just switched, the subtype is pointing
9943 -- to the wrong view). This is currently the case for
9944 -- tagged record types, access types (maybe more?) and
9945 -- needs to be resolved. ???
9947 if Present (Full_View (Priv_Sub))
9948 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
9949 then
9950 Append_Elmt (Full_View (Priv_Sub), Exchanged_Views);
9951 Exchange_Declarations (Priv_Sub);
9952 end if;
9954 Next_Elmt (Priv_Elmt);
9955 end loop;
9956 end Switch_View;
9958 -----------------------------
9959 -- Valid_Default_Attribute --
9960 -----------------------------
9962 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
9963 Attr_Id : constant Attribute_Id :=
9964 Get_Attribute_Id (Attribute_Name (Def));
9965 T : constant Entity_Id := Entity (Prefix (Def));
9966 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
9967 F : Entity_Id;
9968 Num_F : Int;
9969 OK : Boolean;
9971 begin
9972 if No (T)
9973 or else T = Any_Id
9974 then
9975 return;
9976 end if;
9978 Num_F := 0;
9979 F := First_Formal (Nam);
9980 while Present (F) loop
9981 Num_F := Num_F + 1;
9982 Next_Formal (F);
9983 end loop;
9985 case Attr_Id is
9986 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
9987 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
9988 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
9989 Attribute_Unbiased_Rounding =>
9990 OK := Is_Fun
9991 and then Num_F = 1
9992 and then Is_Floating_Point_Type (T);
9994 when Attribute_Image | Attribute_Pred | Attribute_Succ |
9995 Attribute_Value | Attribute_Wide_Image |
9996 Attribute_Wide_Value =>
9997 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
9999 when Attribute_Max | Attribute_Min =>
10000 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
10002 when Attribute_Input =>
10003 OK := (Is_Fun and then Num_F = 1);
10005 when Attribute_Output | Attribute_Read | Attribute_Write =>
10006 OK := (not Is_Fun and then Num_F = 2);
10008 when others =>
10009 OK := False;
10010 end case;
10012 if not OK then
10013 Error_Msg_N ("attribute reference has wrong profile for subprogram",
10014 Def);
10015 end if;
10016 end Valid_Default_Attribute;
10018 end Sem_Ch12;