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