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