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