1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001, Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
29 with Atree; use Atree;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Expander; use Expander;
34 with Fname; use Fname;
35 with Fname.UF; use Fname.UF;
36 with Freeze; use Freeze;
38 with Inline; use Inline;
40 with Lib.Load; use Lib.Load;
41 with Lib.Xref; use Lib.Xref;
42 with Nlists; use Nlists;
43 with Nmake; use Nmake;
45 with Restrict; use Restrict;
46 with Rtsfind; use Rtsfind;
48 with Sem_Cat; use Sem_Cat;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Ch6; use Sem_Ch6;
51 with Sem_Ch7; use Sem_Ch7;
52 with Sem_Ch8; use Sem_Ch8;
53 with Sem_Ch10; use Sem_Ch10;
54 with Sem_Ch13; use Sem_Ch13;
55 with Sem_Elab; use Sem_Elab;
56 with Sem_Elim; use Sem_Elim;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Res; use Sem_Res;
59 with Sem_Type; use Sem_Type;
60 with Sem_Util; use Sem_Util;
61 with Stand; use Stand;
62 with Sinfo; use Sinfo;
63 with Sinfo.CN; use Sinfo.CN;
64 with Sinput; use Sinput;
65 with Sinput.L; use Sinput.L;
66 with Snames; use Snames;
67 with Stringt; use Stringt;
68 with Uname; use Uname;
70 with Tbuild; use Tbuild;
71 with Uintp; use Uintp;
72 with Urealp; use Urealp;
76 package body Sem_Ch12 is
78 ----------------------------------------------------------
79 -- Implementation of Generic Analysis and Instantiation --
80 -----------------------------------------------------------
82 -- GNAT implements generics by macro expansion. No attempt is made to
83 -- share generic instantiations (for now). Analysis of a generic definition
84 -- does not perform any expansion action, but the expander must be called
85 -- on the tree for each instantiation, because the expansion may of course
86 -- depend on the generic actuals. All of this is best achieved as follows:
88 -- a) Semantic analysis of a generic unit is performed on a copy of the
89 -- tree for the generic unit. All tree modifications that follow analysis
90 -- do not affect the original tree. Links are kept between the original
91 -- tree and the copy, in order to recognize non-local references within
92 -- the generic, and propagate them to each instance (recall that name
93 -- resolution is done on the generic declaration: generics are not really
94 -- macros!). This is summarized in the following diagram:
96 -- .-----------. .----------.
97 -- | semantic |<--------------| generic |
99 -- | |==============>| |
100 -- |___________| global |__________|
111 -- b) Each instantiation copies the original tree, and inserts into it a
112 -- series of declarations that describe the mapping between generic formals
113 -- and actuals. For example, a generic In OUT parameter is an object
114 -- renaming of the corresponing actual, etc. Generic IN parameters are
115 -- constant declarations.
117 -- c) In order to give the right visibility for these renamings, we use
118 -- a different scheme for package and subprogram instantiations. For
119 -- packages, the list of renamings is inserted into the package
120 -- specification, before the visible declarations of the package. The
121 -- renamings are analyzed before any of the text of the instance, and are
122 -- thus visible at the right place. Furthermore, outside of the instance,
123 -- the generic parameters are visible and denote their corresponding
126 -- For subprograms, we create a container package to hold the renamings
127 -- and the subprogram instance itself. Analysis of the package makes the
128 -- renaming declarations visible to the subprogram. After analyzing the
129 -- package, the defining entity for the subprogram is touched-up so that
130 -- it appears declared in the current scope, and not inside the container
133 -- If the instantiation is a compilation unit, the container package is
134 -- given the same name as the subprogram instance. This ensures that
135 -- the elaboration procedure called by the binder, using the compilation
136 -- unit name, calls in fact the elaboration procedure for the package.
138 -- Not surprisingly, private types complicate this approach. By saving in
139 -- the original generic object the non-local references, we guarantee that
140 -- the proper entities are referenced at the point of instantiation.
141 -- However, for private types, this by itself does not insure that the
142 -- proper VIEW of the entity is used (the full type may be visible at the
143 -- point of generic definition, but not at instantiation, or vice-versa).
144 -- In order to reference the proper view, we special-case any reference
145 -- to private types in the generic object, by saving both views, one in
146 -- the generic and one in the semantic copy. At time of instantiation, we
147 -- check whether the two views are consistent, and exchange declarations if
148 -- necessary, in order to restore the correct visibility. Similarly, if
149 -- the instance view is private when the generic view was not, we perform
150 -- the exchange. After completing the instantiation, we restore the
151 -- current visibility. The flag Has_Private_View marks identifiers in the
152 -- the generic unit that require checking.
154 -- Visibility within nested generic units requires special handling.
155 -- Consider the following scheme:
157 -- type Global is ... -- outside of generic unit.
161 -- type Semi_Global is ... -- global to inner.
164 -- procedure inner (X1 : Global; X2 : Semi_Global);
166 -- procedure in2 is new inner (...); -- 4
169 -- package New_Outer is new Outer (...); -- 2
170 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
172 -- The semantic analysis of Outer captures all occurrences of Global.
173 -- The semantic analysis of Inner (at 1) captures both occurrences of
174 -- Global and Semi_Global.
176 -- At point 2 (instantiation of Outer), we also produce a generic copy
177 -- of Inner, even though Inner is, at that point, not being instantiated.
178 -- (This is just part of the semantic analysis of New_Outer).
180 -- Critically, references to Global within Inner must be preserved, while
181 -- references to Semi_Global should not preserved, because they must now
182 -- resolve to an entity within New_Outer. To distinguish between these, we
183 -- use a global variable, Current_Instantiated_Parent, which is set when
184 -- performing a generic copy during instantiation (at 2). This variable is
185 -- used when performing a generic copy that is not an instantiation, but
186 -- that is nested within one, as the occurrence of 1 within 2. The analysis
187 -- of a nested generic only preserves references that are global to the
188 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
189 -- determine whether a reference is external to the given parent.
191 -- The instantiation at point 3 requires no special treatment. The method
192 -- works as well for further nestings of generic units, but of course the
193 -- variable Current_Instantiated_Parent must be stacked because nested
194 -- instantiations can occur, e.g. the occurrence of 4 within 2.
196 -- The instantiation of package and subprogram bodies is handled in a
197 -- similar manner, except that it is delayed until after semantic
198 -- analysis is complete. In this fashion complex cross-dependencies
199 -- between several package declarations and bodies containing generics
200 -- can be compiled which otherwise would diagnose spurious circularities.
202 -- For example, it is possible to compile two packages A and B that
203 -- have the following structure:
205 -- package A is package B is
206 -- generic ... generic ...
207 -- package G_A is package G_B is
210 -- package body A is package body B is
211 -- package N_B is new G_B (..) package N_A is new G_A (..)
213 -- The table Pending_Instantiations in package Inline is used to keep
214 -- track of body instantiations that are delayed in this manner. Inline
215 -- handles the actual calls to do the body instantiations. This activity
216 -- is part of Inline, since the processing occurs at the same point, and
217 -- for essentially the same reason, as the handling of inlined routines.
219 ----------------------------------------------
220 -- Detection of Instantiation Circularities --
221 ----------------------------------------------
223 -- If we have a chain of instantiations that is circular, this is a
224 -- static error which must be detected at compile time. The detection
225 -- of these circularities is carried out at the point that we insert
226 -- a generic instance spec or body. If there is a circularity, then
227 -- the analysis of the offending spec or body will eventually result
228 -- in trying to load the same unit again, and we detect this problem
229 -- as we analyze the package instantiation for the second time.
231 -- At least in some cases after we have detected the circularity, we
232 -- get into trouble if we try to keep going. The following flag is
233 -- set if a circularity is detected, and used to abandon compilation
234 -- after the messages have been posted.
236 Circularity_Detected : Boolean := False;
237 -- This should really be reset on encountering a new main unit, but in
238 -- practice we are not using multiple main units so it is not critical.
240 -----------------------
241 -- Local subprograms --
242 -----------------------
244 procedure Abandon_Instantiation (N : Node_Id);
245 pragma No_Return (Abandon_Instantiation);
246 -- Posts an error message "instantiation abandoned" at the indicated
247 -- node and then raises the exception Instantiation_Error to do it.
249 procedure Analyze_Formal_Array_Type
250 (T : in out Entity_Id;
252 -- A formal array type is treated like an array type declaration, and
253 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
254 -- in-out, because in the case of an anonymous type the entity is
255 -- actually created in the procedure.
257 -- The following procedures treat other kinds of formal parameters.
259 procedure Analyze_Formal_Derived_Type
264 -- All the following need comments???
266 procedure Analyze_Formal_Decimal_Fixed_Point_Type
267 (T : Entity_Id; Def : Node_Id);
268 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
269 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
270 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
271 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
272 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
273 (T : Entity_Id; Def : Node_Id);
275 procedure Analyze_Formal_Private_Type
279 -- This needs comments???
281 procedure Analyze_Generic_Formal_Part (N : Node_Id);
283 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
284 -- This needs comments ???
286 function Analyze_Associations
291 -- At instantiation time, build the list of associations between formals
292 -- and actuals. Each association becomes a renaming declaration for the
293 -- formal entity. F_Copy is the analyzed list of formals in the generic
294 -- copy. It is used to apply legality checks to the actuals. I_Node is the
295 -- instantiation node itself.
297 procedure Analyze_Subprogram_Instantiation
301 procedure Build_Instance_Compilation_Unit_Nodes
305 -- This procedure is used in the case where the generic instance of a
306 -- subprogram body or package body is a library unit. In this case, the
307 -- original library unit node for the generic instantiation must be
308 -- replaced by the resulting generic body, and a link made to a new
309 -- compilation unit node for the generic declaration. The argument N is
310 -- the original generic instantiation. Act_Body and Act_Decl are the body
311 -- and declaration of the instance (either package body and declaration
312 -- nodes or subprogram body and declaration nodes depending on the case).
313 -- On return, the node N has been rewritten with the actual body.
315 procedure Check_Formal_Packages (P_Id : Entity_Id);
316 -- Apply the following to all formal packages in generic associations.
318 procedure Check_Formal_Package_Instance
319 (Formal_Pack : Entity_Id;
320 Actual_Pack : Entity_Id);
321 -- Verify that the actuals of the actual instance match the actuals of
322 -- the template for a formal package that is not declared with a box.
324 procedure Check_Forward_Instantiation (N : Node_Id; Decl : Node_Id);
325 -- If the generic is a local entity and the corresponding body has not
326 -- been seen yet, flag enclosing packages to indicate that it will be
327 -- elaborated after the generic body. Subprograms declared in the same
328 -- package cannot be inlined by the front-end because front-end inlining
329 -- requires a strict linear order of elaboration.
331 procedure Check_Hidden_Child_Unit
333 Gen_Unit : Entity_Id;
334 Act_Decl_Id : Entity_Id);
335 -- If the generic unit is an implicit child instance within a parent
336 -- instance, we need to make an explicit test that it is not hidden by
337 -- a child instance of the same name and parent.
339 procedure Check_Private_View (N : Node_Id);
340 -- Check whether the type of a generic entity has a different view between
341 -- the point of generic analysis and the point of instantiation. If the
342 -- view has changed, then at the point of instantiation we restore the
343 -- correct view to perform semantic analysis of the instance, and reset
344 -- the current view after instantiation. The processing is driven by the
345 -- current private status of the type of the node, and Has_Private_View,
346 -- a flag that is set at the point of generic compilation. If view and
347 -- flag are inconsistent then the type is updated appropriately.
349 procedure Check_Generic_Actuals
350 (Instance : Entity_Id;
351 Is_Formal_Box : Boolean);
352 -- Similar to previous one. Check the actuals in the instantiation,
353 -- whose views can change between the point of instantiation and the point
354 -- of instantiation of the body. In addition, mark the generic renamings
355 -- as generic actuals, so that they are not compatible with other actuals.
356 -- Recurse on an actual that is a formal package whose declaration has
359 function Contains_Instance_Of
364 -- Inner is instantiated within the generic Outer. Check whether Inner
365 -- directly or indirectly contains an instance of Outer or of one of its
366 -- parents, in the case of a subunit. Each generic unit holds a list of
367 -- the entities instantiated within (at any depth). This procedure
368 -- determines whether the set of such lists contains a cycle, i.e. an
369 -- illegal circular instantiation.
371 function Denotes_Formal_Package (Pack : Entity_Id) return Boolean;
372 -- Returns True if E is a formal package of an enclosing generic, or
373 -- the actual for such a formal in an enclosing instantiation. Used in
374 -- Restore_Private_Views, to keep the formals of such a package visible
375 -- on exit from an inner instantiation.
377 function Find_Actual_Type
379 Gen_Scope : Entity_Id)
381 -- When validating the actual types of a child instance, check whether
382 -- the formal is a formal type of the parent unit, and retrieve the current
383 -- actual for it. Typ is the entity in the analyzed formal type declaration
384 -- (component or index type of an array type) and Gen_Scope is the scope of
385 -- the analyzed formal array type.
387 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id;
388 -- Given the entity of a unit that is an instantiation, retrieve the
389 -- original instance node. This is used when loading the instantiations
390 -- of the ancestors of a child generic that is being instantiated.
392 function In_Same_Declarative_Part
396 -- True if the instantiation Inst and the given freeze_node F_Node appear
397 -- within the same declarative part, ignoring subunits, but with no inter-
398 -- vening suprograms or concurrent units. If true, the freeze node
399 -- of the instance can be placed after the freeze node of the parent,
400 -- which it itself an instance.
402 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
403 -- Associate analyzed generic parameter with corresponding
404 -- instance. Used for semantic checks at instantiation time.
406 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
407 -- Traverse the Exchanged_Views list to see if a type was private
408 -- and has already been flipped during this phase of instantiation.
410 procedure Hide_Current_Scope;
411 -- When compiling a generic child unit, the parent context must be
412 -- present, but the instance and all entities that may be generated
413 -- must be inserted in the current scope. We leave the current scope
414 -- on the stack, but make its entities invisible to avoid visibility
415 -- problems. This is reversed at the end of instantiations. This is
416 -- not done for the instantiation of the bodies, which only require the
417 -- instances of the generic parents to be in scope.
419 procedure Install_Body
424 -- If the instantiation happens textually before the body of the generic,
425 -- the instantiation of the body must be analyzed after the generic body,
426 -- and not at the point of instantiation. Such early instantiations can
427 -- happen if the generic and the instance appear in a package declaration
428 -- because the generic body can only appear in the corresponding package
429 -- body. Early instantiations can also appear if generic, instance and
430 -- body are all in the declarative part of a subprogram or entry. Entities
431 -- of packages that are early instantiations are delayed, and their freeze
432 -- node appears after the generic body.
434 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id);
435 -- Insert freeze node at the end of the declarative part that includes the
436 -- instance node N. If N is in the visible part of an enclosing package
437 -- declaration, the freeze node has to be inserted at the end of the
438 -- private declarations, if any.
440 procedure Freeze_Subprogram_Body
441 (Inst_Node : Node_Id;
443 Pack_Id : Entity_Id);
444 -- The generic body may appear textually after the instance, including
445 -- in the proper body of a stub, or within a different package instance.
446 -- Given that the instance can only be elaborated after the generic, we
447 -- place freeze_nodes for the instance and/or for packages that may enclose
448 -- the instance and the generic, so that the back-end can establish the
449 -- proper order of elaboration.
451 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
452 -- When compiling an instance of a child unit the parent (which is
453 -- itself an instance) is an enclosing scope that must be made
454 -- immediately visible. This procedure is also used to install the non-
455 -- generic parent of a generic child unit when compiling its body, so that
456 -- full views of types in the parent are made visible.
458 procedure Remove_Parent (In_Body : Boolean := False);
459 -- Reverse effect after instantiation of child is complete.
461 procedure Inline_Instance_Body
463 Gen_Unit : Entity_Id;
465 -- If front-end inlining is requested, instantiate the package body,
466 -- and preserve the visibility of its compilation unit, to insure
467 -- that successive instantiations succeed.
469 -- The functions Instantiate_XXX perform various legality checks and build
470 -- the declarations for instantiated generic parameters.
471 -- Need to describe what the parameters are ???
473 function Instantiate_Object
476 Analyzed_Formal : Node_Id)
479 function Instantiate_Type
482 Analyzed_Formal : Node_Id)
485 function Instantiate_Formal_Subprogram
488 Analyzed_Formal : Node_Id)
491 function Instantiate_Formal_Package
494 Analyzed_Formal : Node_Id)
496 -- If the formal package is declared with a box, special visibility rules
497 -- apply to its formals: they are in the visible part of the package. This
498 -- is true in the declarative region of the formal package, that is to say
499 -- in the enclosing generic or instantiation. For an instantiation, the
500 -- parameters of the formal package are made visible in an explicit step.
501 -- Furthermore, if the actual is a visible use_clause, these formals must
502 -- be made potentially use_visible as well. On exit from the enclosing
503 -- instantiation, the reverse must be done.
505 -- For a formal package declared without a box, there are conformance rules
506 -- that apply to the actuals in the generic declaration and the actuals of
507 -- the actual package in the enclosing instantiation. The simplest way to
508 -- apply these rules is to repeat the instantiation of the formal package
509 -- in the context of the enclosing instance, and compare the generic
510 -- associations of this instantiation with those of the actual package.
512 function Is_In_Main_Unit (N : Node_Id) return Boolean;
513 -- Test if given node is in the main unit
515 procedure Load_Parent_Of_Generic (N : Node_Id; Spec : Node_Id);
516 -- If the generic appears in a separate non-generic library unit,
517 -- load the corresponding body to retrieve the body of the generic.
518 -- N is the node for the generic instantiation, Spec is the generic
519 -- package declaration.
521 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
522 -- Add the context clause of the unit containing a generic unit to
523 -- an instantiation that is a compilation unit.
525 function Get_Associated_Node (N : Node_Id) return Node_Id;
526 -- In order to propagate semantic information back from the analyzed
527 -- copy to the original generic, we maintain links between selected nodes
528 -- in the generic and their corresponding copies. At the end of generic
529 -- analysis, the routine Save_Global_References traverses the generic
530 -- tree, examines the semantic information, and preserves the links to
531 -- those nodes that contain global information. At instantiation, the
532 -- information from the associated node is placed on the new copy, so
533 -- that name resolution is not repeated.
535 -- Three kinds of nodes have associated nodes:
537 -- a) those that contain entities, that is to say identifiers,
538 -- expanded_names, and operators (N_Has_Entity)
540 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
542 -- c) selected components (N_Selected_Component)
544 -- For the first class, the associated node preserves the entity if it is
545 -- global. If the generic contains nested instantiations, the associated_
546 -- node itself has been recopied, and a chain of them must be followed.
548 -- For aggregates, the associated node allows retrieval of the type, which
549 -- may otherwise not appear in the generic. The view of this type may be
550 -- different between generic and instantiation, and the full view can be
551 -- installed before the instantiation is analyzed. For aggregates of
552 -- type extensions, the same view exchange may have to be performed for
553 -- some of the ancestor types, if their view is private at the point of
556 -- Query??? why selected components. What about N_Freeze_Nodes, I assume
557 -- that the answer is no, which means that the comment above for a) is
560 -- The associated node is stored in the Associated_Node field. Note that
561 -- this field overlaps Entity, which is fine, because the whole point is
562 -- that we don't need or want the normal Entity field in this situation.
564 procedure Move_Freeze_Nodes
568 -- Freeze nodes can be generated in the analysis of a generic unit, but
569 -- will not be seen by the back-end. It is necessary to move those nodes
570 -- to the enclosing scope if they freeze an outer entity. We place them
571 -- at the end of the enclosing generic package, which is semantically
574 procedure Pre_Analyze_Actuals (N : Node_Id);
575 -- Analyze actuals to perform name resolution. Full resolution is done
576 -- later, when the expected types are known, but names have to be captured
577 -- before installing parents of generics, that are not visible for the
578 -- actuals themselves.
580 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
581 -- Verify that an attribute that appears as the default for a formal
582 -- subprogram is a function or procedure with the correct profile.
584 -------------------------------------------
585 -- Data Structures for Generic Renamings --
586 -------------------------------------------
588 -- The map Generic_Renamings associates generic entities with their
589 -- corresponding actuals. Currently used to validate type instances.
590 -- It will eventually be used for all generic parameters to eliminate
591 -- the need for overload resolution in the instance.
593 type Assoc_Ptr is new Int;
595 Assoc_Null : constant Assoc_Ptr := -1;
600 Next_In_HTable : Assoc_Ptr;
603 package Generic_Renamings is new Table.Table
604 (Table_Component_Type => Assoc,
605 Table_Index_Type => Assoc_Ptr,
606 Table_Low_Bound => 0,
608 Table_Increment => 100,
609 Table_Name => "Generic_Renamings");
611 -- Variable to hold enclosing instantiation. When the environment is
612 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
614 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
616 -- Hash table for associations
618 HTable_Size : constant := 37;
619 type HTable_Range is range 0 .. HTable_Size - 1;
621 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
622 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
623 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
624 function Hash (F : Entity_Id) return HTable_Range;
626 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
627 Header_Num => HTable_Range,
629 Elmt_Ptr => Assoc_Ptr,
630 Null_Ptr => Assoc_Null,
631 Set_Next => Set_Next_Assoc,
634 Get_Key => Get_Gen_Id,
638 Exchanged_Views : Elist_Id;
639 -- This list holds the private views that have been exchanged during
640 -- instantiation to restore the visibility of the generic declaration.
641 -- (see comments above). After instantiation, the current visibility is
642 -- reestablished by means of a traversal of this list.
644 Hidden_Entities : Elist_Id;
645 -- This list holds the entities of the current scope that are removed
646 -- from immediate visibility when instantiating a child unit. Their
647 -- visibility is restored in Remove_Parent.
649 -- Because instantiations can be recursive, the following must be saved
650 -- on entry and restored on exit from an instantiation (spec or body).
651 -- This is done by the two procedures Save_Env and Restore_Env.
653 type Instance_Env is record
655 Instantiated_Parent : Assoc;
656 Exchanged_Views : Elist_Id;
657 Hidden_Entities : Elist_Id;
658 Current_Sem_Unit : Unit_Number_Type;
661 package Instance_Envs is new Table.Table (
662 Table_Component_Type => Instance_Env,
663 Table_Index_Type => Int,
664 Table_Low_Bound => 0,
666 Table_Increment => 100,
667 Table_Name => "Instance_Envs");
669 procedure Restore_Private_Views
670 (Pack_Id : Entity_Id;
671 Is_Package : Boolean := True);
672 -- Restore the private views of external types, and unmark the generic
673 -- renamings of actuals, so that they become comptible subtypes again.
674 -- For subprograms, Pack_Id is the package constructed to hold the
677 procedure Switch_View (T : Entity_Id);
678 -- Switch the partial and full views of a type and its private
679 -- dependents (i.e. its subtypes and derived types).
681 ------------------------------------
682 -- Structures for Error Reporting --
683 ------------------------------------
685 Instantiation_Node : Node_Id;
686 -- Used by subprograms that validate instantiation of formal parameters
687 -- where there might be no actual on which to place the error message.
688 -- Also used to locate the instantiation node for generic subunits.
690 Instantiation_Error : exception;
691 -- When there is a semantic error in the generic parameter matching,
692 -- there is no point in continuing the instantiation, because the
693 -- number of cascaded errors is unpredictable. This exception aborts
694 -- the instantiation process altogether.
696 S_Adjustment : Sloc_Adjustment;
697 -- Offset created for each node in an instantiation, in order to keep
698 -- track of the source position of the instantiation in each of its nodes.
699 -- A subsequent semantic error or warning on a construct of the instance
700 -- points to both places: the original generic node, and the point of
701 -- instantiation. See Sinput and Sinput.L for additional details.
703 ------------------------------------------------------------
704 -- Data structure for keeping track when inside a Generic --
705 ------------------------------------------------------------
707 -- The following table is used to save values of the Inside_A_Generic
708 -- flag (see spec of Sem) when they are saved by Start_Generic.
710 package Generic_Flags is new Table.Table (
711 Table_Component_Type => Boolean,
712 Table_Index_Type => Int,
713 Table_Low_Bound => 0,
715 Table_Increment => 200,
716 Table_Name => "Generic_Flags");
718 ---------------------------
719 -- Abandon_Instantiation --
720 ---------------------------
722 procedure Abandon_Instantiation (N : Node_Id) is
724 Error_Msg_N ("instantiation abandoned!", N);
725 raise Instantiation_Error;
726 end Abandon_Instantiation;
728 --------------------------
729 -- Analyze_Associations --
730 --------------------------
732 function Analyze_Associations
738 Actuals : List_Id := Generic_Associations (I_Node);
740 Actual_Types : Elist_Id := New_Elmt_List;
741 Assoc : List_Id := New_List;
743 Next_Formal : Node_Id;
744 Temp_Formal : Node_Id;
745 Analyzed_Formal : Node_Id;
746 Defaults : Elist_Id := New_Elmt_List;
749 First_Named : Node_Id := Empty;
750 Found_Assoc : Node_Id;
751 Is_Named_Assoc : Boolean;
752 Num_Matched : Int := 0;
753 Num_Actuals : Int := 0;
755 function Matching_Actual
759 -- Find actual that corresponds to a given a formal parameter. If the
760 -- actuals are positional, return the next one, if any. If the actuals
761 -- are named, scan the parameter associations to find the right one.
762 -- A_F is the corresponding entity in the analyzed generic,which is
763 -- placed on the selector name for ASIS use.
765 procedure Set_Analyzed_Formal;
766 -- Find the node in the generic copy that corresponds to a given formal.
767 -- The semantic information on this node is used to perform legality
768 -- checks on the actuals. Because semantic analysis can introduce some
769 -- anonymous entities or modify the declaration node itself, the
770 -- correspondence between the two lists is not one-one. In addition to
771 -- anonymous types, the presence a formal equality will introduce an
772 -- implicit declaration for the corresponding inequality.
774 ---------------------
775 -- Matching_Actual --
776 ---------------------
778 function Matching_Actual
787 Is_Named_Assoc := False;
789 -- End of list of purely positional parameters
794 -- Case of positional parameter corresponding to current formal
796 elsif No (Selector_Name (Actual)) then
797 Found := Explicit_Generic_Actual_Parameter (Actual);
798 Found_Assoc := Actual;
799 Num_Matched := Num_Matched + 1;
802 -- Otherwise scan list of named actuals to find the one with the
803 -- desired name. All remaining actuals have explicit names.
806 Is_Named_Assoc := True;
810 while Present (Actual) loop
811 if Chars (Selector_Name (Actual)) = Chars (F) then
812 Found := Explicit_Generic_Actual_Parameter (Actual);
813 Set_Entity (Selector_Name (Actual), A_F);
814 Set_Etype (Selector_Name (Actual), Etype (A_F));
815 Found_Assoc := Actual;
816 Num_Matched := Num_Matched + 1;
824 -- Reset for subsequent searches. In most cases the named
825 -- associations are in order. If they are not, we reorder them
826 -- to avoid scanning twice the same actual. This is not just a
827 -- question of efficiency: there may be multiple defaults with
828 -- boxes that have the same name. In a nested instantiation we
829 -- insert actuals for those defaults, and cannot rely on their
830 -- names to disambiguate them.
832 if Actual = First_Named then
835 elsif Present (Actual) then
836 Insert_Before (First_Named, Remove_Next (Prev));
839 Actual := First_Named;
845 -------------------------
846 -- Set_Analyzed_Formal --
847 -------------------------
849 procedure Set_Analyzed_Formal is
852 while Present (Analyzed_Formal) loop
853 Kind := Nkind (Analyzed_Formal);
855 case Nkind (Formal) is
857 when N_Formal_Subprogram_Declaration =>
858 exit when Kind = N_Formal_Subprogram_Declaration
861 (Defining_Unit_Name (Specification (Formal))) =
863 (Defining_Unit_Name (Specification (Analyzed_Formal)));
865 when N_Formal_Package_Declaration =>
867 Kind = N_Formal_Package_Declaration
869 Kind = N_Generic_Package_Declaration;
871 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
875 -- Skip freeze nodes, and nodes inserted to replace
876 -- unrecognized pragmas.
879 Kind /= N_Formal_Subprogram_Declaration
880 and then Kind /= N_Subprogram_Declaration
881 and then Kind /= N_Freeze_Entity
882 and then Kind /= N_Null_Statement
883 and then Kind /= N_Itype_Reference
884 and then Chars (Defining_Identifier (Formal)) =
885 Chars (Defining_Identifier (Analyzed_Formal));
888 Next (Analyzed_Formal);
891 end Set_Analyzed_Formal;
893 -- Start of processing for Analyze_Associations
896 -- If named associations are present, save the first named association
897 -- (it may of course be Empty) to facilitate subsequent name search.
899 if Present (Actuals) then
900 First_Named := First (Actuals);
902 while Present (First_Named)
903 and then No (Selector_Name (First_Named))
905 Num_Actuals := Num_Actuals + 1;
910 Named := First_Named;
911 while Present (Named) loop
912 if No (Selector_Name (Named)) then
913 Error_Msg_N ("invalid positional actual after named one", Named);
914 Abandon_Instantiation (Named);
917 Num_Actuals := Num_Actuals + 1;
921 if Present (Formals) then
922 Formal := First_Non_Pragma (Formals);
923 Analyzed_Formal := First_Non_Pragma (F_Copy);
925 if Present (Actuals) then
926 Actual := First (Actuals);
928 -- All formals should have default values
934 while Present (Formal) loop
936 Next_Formal := Next_Non_Pragma (Formal);
938 case Nkind (Formal) is
939 when N_Formal_Object_Declaration =>
942 Defining_Identifier (Formal),
943 Defining_Identifier (Analyzed_Formal));
946 (Instantiate_Object (Formal, Match, Analyzed_Formal),
949 when N_Formal_Type_Declaration =>
952 Defining_Identifier (Formal),
953 Defining_Identifier (Analyzed_Formal));
956 Error_Msg_NE ("missing actual for instantiation of &",
957 Instantiation_Node, Defining_Identifier (Formal));
958 Abandon_Instantiation (Instantiation_Node);
963 Instantiate_Type (Formal, Match, Analyzed_Formal));
965 -- an instantiation is a freeze point for the actuals,
966 -- unless this is a rewritten formal package.
968 if Nkind (I_Node) /= N_Formal_Package_Declaration then
969 Append_Elmt (Entity (Match), Actual_Types);
973 -- A remote access-to-class-wide type must not be an
974 -- actual parameter for a generic formal of an access
975 -- type (E.2.2 (17)).
977 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
979 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
980 N_Access_To_Object_Definition
982 Validate_Remote_Access_To_Class_Wide_Type (Match);
985 when N_Formal_Subprogram_Declaration =>
988 Defining_Unit_Name (Specification (Formal)),
989 Defining_Unit_Name (Specification (Analyzed_Formal)));
991 -- If the formal subprogram has the same name as
992 -- another formal subprogram of the generic, then
993 -- a named association is illegal (12.3(9)). Exclude
994 -- named associations that are generated for a nested
998 and then Is_Named_Assoc
999 and then Comes_From_Source (Found_Assoc)
1001 Temp_Formal := First (Formals);
1002 while Present (Temp_Formal) loop
1003 if Nkind (Temp_Formal) =
1004 N_Formal_Subprogram_Declaration
1005 and then Temp_Formal /= Formal
1007 Chars (Selector_Name (Found_Assoc)) =
1008 Chars (Defining_Unit_Name
1009 (Specification (Temp_Formal)))
1012 ("name not allowed for overloaded formal",
1014 Abandon_Instantiation (Instantiation_Node);
1022 Instantiate_Formal_Subprogram
1023 (Formal, Match, Analyzed_Formal));
1026 and then Box_Present (Formal)
1029 (Defining_Unit_Name (Specification (Last (Assoc))),
1033 when N_Formal_Package_Declaration =>
1036 Defining_Identifier (Formal),
1037 Defining_Identifier (Original_Node (Analyzed_Formal)));
1041 ("missing actual for instantiation of&",
1043 Defining_Identifier (Formal));
1045 Abandon_Instantiation (Instantiation_Node);
1050 (Instantiate_Formal_Package
1051 (Formal, Match, Analyzed_Formal),
1055 -- For use type and use package appearing in the context
1056 -- clause, we have already copied them, so we can just
1057 -- move them where they belong (we mustn't recopy them
1058 -- since this would mess up the Sloc values).
1060 when N_Use_Package_Clause |
1061 N_Use_Type_Clause =>
1063 Append (Formal, Assoc);
1066 raise Program_Error;
1070 Formal := Next_Formal;
1071 Next_Non_Pragma (Analyzed_Formal);
1074 if Num_Actuals > Num_Matched then
1076 ("unmatched actuals in instantiation", Instantiation_Node);
1079 elsif Present (Actuals) then
1081 ("too many actuals in generic instantiation", Instantiation_Node);
1085 Elmt : Elmt_Id := First_Elmt (Actual_Types);
1088 while Present (Elmt) loop
1089 Freeze_Before (I_Node, Node (Elmt));
1094 -- If there are default subprograms, normalize the tree by adding
1095 -- explicit associations for them. This is required if the instance
1096 -- appears within a generic.
1104 Elmt := First_Elmt (Defaults);
1105 while Present (Elmt) loop
1106 if No (Actuals) then
1107 Actuals := New_List;
1108 Set_Generic_Associations (I_Node, Actuals);
1111 Subp := Node (Elmt);
1113 Make_Generic_Association (Sloc (Subp),
1114 Selector_Name => New_Occurrence_Of (Subp, Sloc (Subp)),
1115 Explicit_Generic_Actual_Parameter =>
1116 New_Occurrence_Of (Subp, Sloc (Subp)));
1117 Mark_Rewrite_Insertion (New_D);
1118 Append_To (Actuals, New_D);
1124 end Analyze_Associations;
1126 -------------------------------
1127 -- Analyze_Formal_Array_Type --
1128 -------------------------------
1130 procedure Analyze_Formal_Array_Type
1131 (T : in out Entity_Id;
1137 -- Treated like a non-generic array declaration, with
1138 -- additional semantic checks.
1142 if Nkind (Def) = N_Constrained_Array_Definition then
1143 DSS := First (Discrete_Subtype_Definitions (Def));
1144 while Present (DSS) loop
1145 if Nkind (DSS) = N_Subtype_Indication
1146 or else Nkind (DSS) = N_Range
1147 or else Nkind (DSS) = N_Attribute_Reference
1149 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1156 Array_Type_Declaration (T, Def);
1157 Set_Is_Generic_Type (Base_Type (T));
1159 if Ekind (Component_Type (T)) = E_Incomplete_Type
1160 and then No (Full_View (Component_Type (T)))
1162 Error_Msg_N ("premature usage of incomplete type", Def);
1164 elsif Is_Internal (Component_Type (T))
1165 and then Nkind (Original_Node (Subtype_Indication (Def)))
1166 /= N_Attribute_Reference
1169 ("only a subtype mark is allowed in a formal",
1170 Subtype_Indication (Def));
1173 end Analyze_Formal_Array_Type;
1175 ---------------------------------------------
1176 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1177 ---------------------------------------------
1179 -- As for other generic types, we create a valid type representation
1180 -- with legal but arbitrary attributes, whose values are never considered
1181 -- static. For all scalar types we introduce an anonymous base type, with
1182 -- the same attributes. We choose the corresponding integer type to be
1183 -- Standard_Integer.
1185 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1189 Loc : constant Source_Ptr := Sloc (Def);
1190 Base : constant Entity_Id :=
1192 (E_Decimal_Fixed_Point_Type,
1193 Current_Scope, Sloc (Def), 'G');
1194 Int_Base : constant Entity_Id := Standard_Integer;
1195 Delta_Val : constant Ureal := Ureal_1;
1196 Digs_Val : constant Uint := Uint_6;
1201 Set_Etype (Base, Base);
1202 Set_Size_Info (Base, Int_Base);
1203 Set_RM_Size (Base, RM_Size (Int_Base));
1204 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1205 Set_Digits_Value (Base, Digs_Val);
1206 Set_Delta_Value (Base, Delta_Val);
1207 Set_Small_Value (Base, Delta_Val);
1208 Set_Scalar_Range (Base,
1210 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1211 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1213 Set_Is_Generic_Type (Base);
1214 Set_Parent (Base, Parent (Def));
1216 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1217 Set_Etype (T, Base);
1218 Set_Size_Info (T, Int_Base);
1219 Set_RM_Size (T, RM_Size (Int_Base));
1220 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1221 Set_Digits_Value (T, Digs_Val);
1222 Set_Delta_Value (T, Delta_Val);
1223 Set_Small_Value (T, Delta_Val);
1224 Set_Scalar_Range (T, Scalar_Range (Base));
1226 end Analyze_Formal_Decimal_Fixed_Point_Type;
1228 ---------------------------------
1229 -- Analyze_Formal_Derived_Type --
1230 ---------------------------------
1232 procedure Analyze_Formal_Derived_Type
1237 Loc : constant Source_Ptr := Sloc (Def);
1239 Unk_Disc : Boolean := Unknown_Discriminants_Present (N);
1242 Set_Is_Generic_Type (T);
1244 if Private_Present (Def) then
1246 Make_Private_Extension_Declaration (Loc,
1247 Defining_Identifier => T,
1248 Discriminant_Specifications => Discriminant_Specifications (N),
1249 Unknown_Discriminants_Present => Unk_Disc,
1250 Subtype_Indication => Subtype_Mark (Def));
1252 Set_Abstract_Present (New_N, Abstract_Present (Def));
1256 Make_Full_Type_Declaration (Loc,
1257 Defining_Identifier => T,
1258 Discriminant_Specifications =>
1259 Discriminant_Specifications (Parent (T)),
1261 Make_Derived_Type_Definition (Loc,
1262 Subtype_Indication => Subtype_Mark (Def)));
1264 Set_Abstract_Present
1265 (Type_Definition (New_N), Abstract_Present (Def));
1272 if not Is_Composite_Type (T) then
1274 ("unknown discriminants not allowed for elementary types", N);
1276 Set_Has_Unknown_Discriminants (T);
1277 Set_Is_Constrained (T, False);
1281 -- If the parent type has a known size, so does the formal, which
1282 -- makes legal representation clauses that involve the formal.
1284 Set_Size_Known_At_Compile_Time
1285 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
1287 end Analyze_Formal_Derived_Type;
1289 ----------------------------------
1290 -- Analyze_Formal_Discrete_Type --
1291 ----------------------------------
1293 -- The operations defined for a discrete types are those of an
1294 -- enumeration type. The size is set to an arbitrary value, for use
1295 -- in analyzing the generic unit.
1297 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
1298 Loc : constant Source_Ptr := Sloc (Def);
1304 Set_Ekind (T, E_Enumeration_Type);
1309 -- For semantic analysis, the bounds of the type must be set to some
1310 -- non-static value. The simplest is to create attribute nodes for
1311 -- those bounds, that refer to the type itself. These bounds are never
1312 -- analyzed but serve as place-holders.
1315 Make_Attribute_Reference (Loc,
1316 Attribute_Name => Name_First,
1317 Prefix => New_Reference_To (T, Loc));
1321 Make_Attribute_Reference (Loc,
1322 Attribute_Name => Name_Last,
1323 Prefix => New_Reference_To (T, Loc));
1326 Set_Scalar_Range (T,
1331 end Analyze_Formal_Discrete_Type;
1333 ----------------------------------
1334 -- Analyze_Formal_Floating_Type --
1335 ---------------------------------
1337 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
1338 Base : constant Entity_Id :=
1340 (E_Floating_Point_Type, Current_Scope, Sloc (Def), 'G');
1343 -- The various semantic attributes are taken from the predefined type
1344 -- Float, just so that all of them are initialized. Their values are
1345 -- never used because no constant folding or expansion takes place in
1346 -- the generic itself.
1349 Set_Ekind (T, E_Floating_Point_Subtype);
1350 Set_Etype (T, Base);
1351 Set_Size_Info (T, (Standard_Float));
1352 Set_RM_Size (T, RM_Size (Standard_Float));
1353 Set_Digits_Value (T, Digits_Value (Standard_Float));
1354 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
1356 Set_Is_Generic_Type (Base);
1357 Set_Etype (Base, Base);
1358 Set_Size_Info (Base, (Standard_Float));
1359 Set_RM_Size (Base, RM_Size (Standard_Float));
1360 Set_Digits_Value (Base, Digits_Value (Standard_Float));
1361 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
1362 Set_Parent (Base, Parent (Def));
1363 end Analyze_Formal_Floating_Type;
1365 ---------------------------------
1366 -- Analyze_Formal_Modular_Type --
1367 ---------------------------------
1369 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
1371 -- Apart from their entity kind, generic modular types are treated
1372 -- like signed integer types, and have the same attributes.
1374 Analyze_Formal_Signed_Integer_Type (T, Def);
1375 Set_Ekind (T, E_Modular_Integer_Subtype);
1376 Set_Ekind (Etype (T), E_Modular_Integer_Type);
1378 end Analyze_Formal_Modular_Type;
1380 ---------------------------------------
1381 -- Analyze_Formal_Object_Declaration --
1382 ---------------------------------------
1384 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
1385 E : constant Node_Id := Expression (N);
1386 Id : Node_Id := Defining_Identifier (N);
1393 -- Determine the mode of the formal object
1395 if Out_Present (N) then
1396 K := E_Generic_In_Out_Parameter;
1398 if not In_Present (N) then
1399 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
1403 K := E_Generic_In_Parameter;
1406 Find_Type (Subtype_Mark (N));
1407 T := Entity (Subtype_Mark (N));
1409 if Ekind (T) = E_Incomplete_Type then
1410 Error_Msg_N ("premature usage of incomplete type", Subtype_Mark (N));
1413 if K = E_Generic_In_Parameter then
1414 if Is_Limited_Type (T) then
1416 ("generic formal of mode IN must not be of limited type", N);
1419 if Is_Abstract (T) then
1421 ("generic formal of mode IN must not be of abstract type", N);
1425 Analyze_Default_Expression (E, T);
1431 -- Case of generic IN OUT parameter.
1434 -- If the formal has an unconstrained type, construct its
1435 -- actual subtype, as is done for subprogram formals. In this
1436 -- fashion, all its uses can refer to specific bounds.
1441 if (Is_Array_Type (T)
1442 and then not Is_Constrained (T))
1444 (Ekind (T) = E_Record_Type
1445 and then Has_Discriminants (T))
1448 Non_Freezing_Ref : constant Node_Id :=
1449 New_Reference_To (Id, Sloc (Id));
1453 -- Make sure that the actual subtype doesn't generate
1456 Set_Must_Not_Freeze (Non_Freezing_Ref);
1457 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
1458 Insert_Before_And_Analyze (N, Decl);
1459 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
1462 Set_Actual_Subtype (Id, T);
1467 ("initialization not allowed for `IN OUT` formals", N);
1471 end Analyze_Formal_Object_Declaration;
1473 ----------------------------------------------
1474 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
1475 ----------------------------------------------
1477 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
1481 Loc : constant Source_Ptr := Sloc (Def);
1482 Base : constant Entity_Id :=
1484 (E_Ordinary_Fixed_Point_Type, Current_Scope, Sloc (Def), 'G');
1486 -- The semantic attributes are set for completeness only, their
1487 -- values will never be used, because all properties of the type
1491 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
1492 Set_Etype (T, Base);
1493 Set_Size_Info (T, Standard_Integer);
1494 Set_RM_Size (T, RM_Size (Standard_Integer));
1495 Set_Small_Value (T, Ureal_1);
1496 Set_Delta_Value (T, Ureal_1);
1497 Set_Scalar_Range (T,
1499 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
1500 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
1502 Set_Is_Generic_Type (Base);
1503 Set_Etype (Base, Base);
1504 Set_Size_Info (Base, Standard_Integer);
1505 Set_RM_Size (Base, RM_Size (Standard_Integer));
1506 Set_Small_Value (Base, Ureal_1);
1507 Set_Delta_Value (Base, Ureal_1);
1508 Set_Scalar_Range (Base, Scalar_Range (T));
1509 Set_Parent (Base, Parent (Def));
1510 end Analyze_Formal_Ordinary_Fixed_Point_Type;
1512 ----------------------------
1513 -- Analyze_Formal_Package --
1514 ----------------------------
1516 procedure Analyze_Formal_Package (N : Node_Id) is
1517 Loc : constant Source_Ptr := Sloc (N);
1518 Formal : Entity_Id := Defining_Identifier (N);
1519 Gen_Id : constant Node_Id := Name (N);
1521 Gen_Unit : Entity_Id;
1523 Parent_Installed : Boolean := False;
1525 Parent_Instance : Entity_Id;
1526 Renaming_In_Par : Entity_Id;
1529 Text_IO_Kludge (Gen_Id);
1531 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
1532 Gen_Unit := Entity (Gen_Id);
1534 if Ekind (Gen_Unit) /= E_Generic_Package then
1535 Error_Msg_N ("expect generic package name", Gen_Id);
1538 elsif Gen_Unit = Current_Scope then
1540 ("generic package cannot be used as a formal package of itself",
1545 -- Check for a formal package that is a package renaming.
1547 if Present (Renamed_Object (Gen_Unit)) then
1548 Gen_Unit := Renamed_Object (Gen_Unit);
1551 -- The formal package is treated like a regular instance, but only
1552 -- the specification needs to be instantiated, to make entities visible.
1554 if not Box_Present (N) then
1555 Hidden_Entities := New_Elmt_List;
1556 Analyze_Package_Instantiation (N);
1558 if Parent_Installed then
1563 -- If there are no generic associations, the generic parameters
1564 -- appear as local entities and are instantiated like them. We copy
1565 -- the generic package declaration as if it were an instantiation,
1566 -- and analyze it like a regular package, except that we treat the
1567 -- formals as additional visible components.
1569 Save_Env (Gen_Unit, Formal);
1571 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
1573 if In_Extended_Main_Source_Unit (N) then
1574 Set_Is_Instantiated (Gen_Unit);
1575 Generate_Reference (Gen_Unit, N);
1580 (Original_Node (Gen_Decl), Empty, Instantiating => True);
1581 Set_Defining_Unit_Name (Specification (New_N), Formal);
1584 Enter_Name (Formal);
1585 Set_Ekind (Formal, E_Generic_Package);
1586 Set_Etype (Formal, Standard_Void_Type);
1587 Set_Inner_Instances (Formal, New_Elmt_List);
1590 -- Within the formal, the name of the generic package is a renaming
1591 -- of the formal (as for a regular instantiation).
1593 Renaming := Make_Package_Renaming_Declaration (Loc,
1594 Defining_Unit_Name =>
1595 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
1596 Name => New_Reference_To (Formal, Loc));
1598 if Present (Visible_Declarations (Specification (N))) then
1599 Prepend (Renaming, To => Visible_Declarations (Specification (N)));
1600 elsif Present (Private_Declarations (Specification (N))) then
1601 Prepend (Renaming, To => Private_Declarations (Specification (N)));
1604 if Is_Child_Unit (Gen_Unit)
1605 and then Parent_Installed
1607 -- Similarly, we have to make the name of the formal visible in
1608 -- the parent instance, to resolve properly fully qualified names
1609 -- that may appear in the generic unit. The parent instance has
1610 -- been placed on the scope stack ahead of the current scope.
1612 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
1615 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
1616 Set_Ekind (Renaming_In_Par, E_Package);
1617 Set_Etype (Renaming_In_Par, Standard_Void_Type);
1618 Set_Scope (Renaming_In_Par, Parent_Instance);
1619 Set_Parent (Renaming_In_Par, Parent (Formal));
1620 Set_Renamed_Object (Renaming_In_Par, Formal);
1621 Append_Entity (Renaming_In_Par, Parent_Instance);
1624 Analyze_Generic_Formal_Part (N);
1625 Analyze (Specification (N));
1626 End_Package_Scope (Formal);
1628 if Parent_Installed then
1634 -- Inside the generic unit, the formal package is a regular
1635 -- package, but no body is needed for it. Note that after
1636 -- instantiation, the defining_unit_name we need is in the
1637 -- new tree and not in the original. (see Package_Instantiation).
1638 -- A generic formal package is an instance, and can be used as
1639 -- an actual for an inner instance. Mark its generic parent.
1641 Set_Ekind (Formal, E_Package);
1642 Set_Generic_Parent (Specification (N), Gen_Unit);
1643 Set_Has_Completion (Formal, True);
1645 end Analyze_Formal_Package;
1647 ---------------------------------
1648 -- Analyze_Formal_Private_Type --
1649 ---------------------------------
1651 procedure Analyze_Formal_Private_Type
1657 New_Private_Type (N, T, Def);
1659 -- Set the size to an arbitrary but legal value.
1661 Set_Size_Info (T, Standard_Integer);
1662 Set_RM_Size (T, RM_Size (Standard_Integer));
1663 end Analyze_Formal_Private_Type;
1665 ----------------------------------------
1666 -- Analyze_Formal_Signed_Integer_Type --
1667 ----------------------------------------
1669 procedure Analyze_Formal_Signed_Integer_Type
1673 Base : constant Entity_Id :=
1675 (E_Signed_Integer_Type, Current_Scope, Sloc (Def), 'G');
1680 Set_Ekind (T, E_Signed_Integer_Subtype);
1681 Set_Etype (T, Base);
1682 Set_Size_Info (T, Standard_Integer);
1683 Set_RM_Size (T, RM_Size (Standard_Integer));
1684 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
1686 Set_Is_Generic_Type (Base);
1687 Set_Size_Info (Base, Standard_Integer);
1688 Set_RM_Size (Base, RM_Size (Standard_Integer));
1689 Set_Etype (Base, Base);
1690 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
1691 Set_Parent (Base, Parent (Def));
1692 end Analyze_Formal_Signed_Integer_Type;
1694 -------------------------------
1695 -- Analyze_Formal_Subprogram --
1696 -------------------------------
1698 procedure Analyze_Formal_Subprogram (N : Node_Id) is
1699 Spec : constant Node_Id := Specification (N);
1700 Def : constant Node_Id := Default_Name (N);
1701 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
1705 if Nkind (Nam) = N_Defining_Program_Unit_Name then
1706 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
1710 Analyze_Subprogram_Declaration (N);
1711 Set_Is_Formal_Subprogram (Nam);
1712 Set_Has_Completion (Nam);
1714 -- Default name is resolved at the point of instantiation
1716 if Box_Present (N) then
1719 -- Else default is bound at the point of generic declaration
1721 elsif Present (Def) then
1722 if Nkind (Def) = N_Operator_Symbol then
1723 Find_Direct_Name (Def);
1725 elsif Nkind (Def) /= N_Attribute_Reference then
1729 -- For an attribute reference, analyze the prefix and verify
1730 -- that it has the proper profile for the subprogram.
1732 Analyze (Prefix (Def));
1733 Valid_Default_Attribute (Nam, Def);
1737 -- Default name may be overloaded, in which case the interpretation
1738 -- with the correct profile must be selected, as for a renaming.
1740 if Etype (Def) = Any_Type then
1743 elsif Nkind (Def) = N_Selected_Component then
1744 Subp := Entity (Selector_Name (Def));
1746 if Ekind (Subp) /= E_Entry then
1747 Error_Msg_N ("expect valid subprogram name as default", Def);
1751 elsif Nkind (Def) = N_Indexed_Component then
1753 if Nkind (Prefix (Def)) /= N_Selected_Component then
1754 Error_Msg_N ("expect valid subprogram name as default", Def);
1758 Subp := Entity (Selector_Name (Prefix (Def)));
1760 if Ekind (Subp) /= E_Entry_Family then
1761 Error_Msg_N ("expect valid subprogram name as default", Def);
1766 elsif Nkind (Def) = N_Character_Literal then
1768 -- Needs some type checks: subprogram should be parameterless???
1770 Resolve (Def, (Etype (Nam)));
1772 elsif (not Is_Entity_Name (Def)
1773 or else not Is_Overloadable (Entity (Def)))
1775 Error_Msg_N ("expect valid subprogram name as default", Def);
1778 elsif not Is_Overloaded (Def) then
1779 Subp := Entity (Def);
1782 Error_Msg_N ("premature usage of formal subprogram", Def);
1784 elsif not Entity_Matches_Spec (Subp, Nam) then
1785 Error_Msg_N ("no visible entity matches specification", Def);
1791 I1 : Interp_Index := 0;
1797 Get_First_Interp (Def, I, It);
1798 while Present (It.Nam) loop
1800 if Entity_Matches_Spec (It.Nam, Nam) then
1801 if Subp /= Any_Id then
1802 It1 := Disambiguate (Def, I1, I, Etype (Subp));
1804 if It1 = No_Interp then
1805 Error_Msg_N ("ambiguous default subprogram", Def);
1818 Get_Next_Interp (I, It);
1822 if Subp /= Any_Id then
1823 Set_Entity (Def, Subp);
1826 Error_Msg_N ("premature usage of formal subprogram", Def);
1828 elsif Ekind (Subp) /= E_Operator then
1829 Check_Mode_Conformant (Subp, Nam);
1833 Error_Msg_N ("no visible subprogram matches specification", N);
1837 end Analyze_Formal_Subprogram;
1839 -------------------------------------
1840 -- Analyze_Formal_Type_Declaration --
1841 -------------------------------------
1843 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
1844 Def : constant Node_Id := Formal_Type_Definition (N);
1848 T := Defining_Identifier (N);
1850 if Present (Discriminant_Specifications (N))
1851 and then Nkind (Def) /= N_Formal_Private_Type_Definition
1854 ("discriminants not allowed for this formal type",
1855 Defining_Identifier (First (Discriminant_Specifications (N))));
1858 -- Enter the new name, and branch to specific routine.
1861 when N_Formal_Private_Type_Definition
1862 => Analyze_Formal_Private_Type (N, T, Def);
1864 when N_Formal_Derived_Type_Definition
1865 => Analyze_Formal_Derived_Type (N, T, Def);
1867 when N_Formal_Discrete_Type_Definition
1868 => Analyze_Formal_Discrete_Type (T, Def);
1870 when N_Formal_Signed_Integer_Type_Definition
1871 => Analyze_Formal_Signed_Integer_Type (T, Def);
1873 when N_Formal_Modular_Type_Definition
1874 => Analyze_Formal_Modular_Type (T, Def);
1876 when N_Formal_Floating_Point_Definition
1877 => Analyze_Formal_Floating_Type (T, Def);
1879 when N_Formal_Ordinary_Fixed_Point_Definition
1880 => Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
1882 when N_Formal_Decimal_Fixed_Point_Definition
1883 => Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
1885 when N_Array_Type_Definition
1886 => Analyze_Formal_Array_Type (T, Def);
1888 when N_Access_To_Object_Definition |
1889 N_Access_Function_Definition |
1890 N_Access_Procedure_Definition
1891 => Analyze_Generic_Access_Type (T, Def);
1894 raise Program_Error;
1898 Set_Is_Generic_Type (T);
1900 end Analyze_Formal_Type_Declaration;
1902 ------------------------------------
1903 -- Analyze_Function_Instantiation --
1904 ------------------------------------
1906 procedure Analyze_Function_Instantiation (N : Node_Id) is
1908 Analyze_Subprogram_Instantiation (N, E_Function);
1909 end Analyze_Function_Instantiation;
1911 ---------------------------------
1912 -- Analyze_Generic_Access_Type --
1913 ---------------------------------
1915 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
1919 if Nkind (Def) = N_Access_To_Object_Definition then
1920 Access_Type_Declaration (T, Def);
1922 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
1923 and then No (Full_View (Designated_Type (T)))
1924 and then not Is_Generic_Type (Designated_Type (T))
1926 Error_Msg_N ("premature usage of incomplete type", Def);
1928 elsif Is_Internal (Designated_Type (T)) then
1930 ("only a subtype mark is allowed in a formal", Def);
1934 Access_Subprogram_Declaration (T, Def);
1936 end Analyze_Generic_Access_Type;
1938 ---------------------------------
1939 -- Analyze_Generic_Formal_Part --
1940 ---------------------------------
1942 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
1943 Gen_Parm_Decl : Node_Id;
1946 -- The generic formals are processed in the scope of the generic
1947 -- unit, where they are immediately visible. The scope is installed
1950 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
1952 while Present (Gen_Parm_Decl) loop
1953 Analyze (Gen_Parm_Decl);
1954 Next (Gen_Parm_Decl);
1956 end Analyze_Generic_Formal_Part;
1958 ------------------------------------------
1959 -- Analyze_Generic_Package_Declaration --
1960 ------------------------------------------
1962 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
1965 Save_Parent : Node_Id;
1968 -- Create copy of generic unit, and save for instantiation.
1969 -- If the unit is a child unit, do not copy the specifications
1970 -- for the parent, which are not part of the generic tree.
1972 Save_Parent := Parent_Spec (N);
1973 Set_Parent_Spec (N, Empty);
1975 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1976 Set_Parent_Spec (New_N, Save_Parent);
1978 Id := Defining_Entity (N);
1979 Generate_Definition (Id);
1981 -- Expansion is not applied to generic units.
1986 Set_Ekind (Id, E_Generic_Package);
1987 Set_Etype (Id, Standard_Void_Type);
1989 Enter_Generic_Scope (Id);
1990 Set_Inner_Instances (Id, New_Elmt_List);
1992 Set_Categorization_From_Pragmas (N);
1993 Set_Is_Pure (Id, Is_Pure (Current_Scope));
1995 -- For a library unit, we have reconstructed the entity for the
1996 -- unit, and must reset it in the library tables.
1998 if Nkind (Parent (N)) = N_Compilation_Unit then
1999 Set_Cunit_Entity (Current_Sem_Unit, Id);
2002 Analyze_Generic_Formal_Part (N);
2004 -- After processing the generic formals, analysis proceeds
2005 -- as for a non-generic package.
2007 Analyze (Specification (N));
2009 Validate_Categorization_Dependency (N, Id);
2013 End_Package_Scope (Id);
2014 Exit_Generic_Scope (Id);
2016 if Nkind (Parent (N)) /= N_Compilation_Unit then
2017 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
2018 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
2019 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
2022 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2023 Validate_RT_RAT_Component (N);
2026 end Analyze_Generic_Package_Declaration;
2028 --------------------------------------------
2029 -- Analyze_Generic_Subprogram_Declaration --
2030 --------------------------------------------
2032 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
2037 Save_Parent : Node_Id;
2040 -- Create copy of generic unit,and save for instantiation.
2041 -- If the unit is a child unit, do not copy the specifications
2042 -- for the parent, which are not part of the generic tree.
2044 Save_Parent := Parent_Spec (N);
2045 Set_Parent_Spec (N, Empty);
2047 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
2048 Set_Parent_Spec (New_N, Save_Parent);
2051 Spec := Specification (N);
2052 Id := Defining_Entity (Spec);
2053 Generate_Definition (Id);
2055 if Nkind (Id) = N_Defining_Operator_Symbol then
2057 ("operator symbol not allowed for generic subprogram", Id);
2064 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
2066 Enter_Generic_Scope (Id);
2067 Set_Inner_Instances (Id, New_Elmt_List);
2068 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2070 Analyze_Generic_Formal_Part (N);
2072 Formals := Parameter_Specifications (Spec);
2074 if Present (Formals) then
2075 Process_Formals (Id, Formals, Spec);
2078 if Nkind (Spec) = N_Function_Specification then
2079 Set_Ekind (Id, E_Generic_Function);
2080 Find_Type (Subtype_Mark (Spec));
2081 Set_Etype (Id, Entity (Subtype_Mark (Spec)));
2083 Set_Ekind (Id, E_Generic_Procedure);
2084 Set_Etype (Id, Standard_Void_Type);
2087 -- For a library unit, we have reconstructed the entity for the
2088 -- unit, and must reset it in the library tables. We also need
2089 -- to make sure that Body_Required is set properly in the original
2090 -- compilation unit node.
2092 if Nkind (Parent (N)) = N_Compilation_Unit then
2093 Set_Cunit_Entity (Current_Sem_Unit, Id);
2094 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
2097 Set_Categorization_From_Pragmas (N);
2098 Validate_Categorization_Dependency (N, Id);
2100 Save_Global_References (Original_Node (N));
2104 Exit_Generic_Scope (Id);
2106 end Analyze_Generic_Subprogram_Declaration;
2108 -----------------------------------
2109 -- Analyze_Package_Instantiation --
2110 -----------------------------------
2112 -- Note: this procedure is also used for formal package declarations,
2113 -- in which case the argument N is an N_Formal_Package_Declaration
2114 -- node. This should really be noted in the spec! ???
2116 procedure Analyze_Package_Instantiation (N : Node_Id) is
2117 Loc : constant Source_Ptr := Sloc (N);
2118 Gen_Id : constant Node_Id := Name (N);
2121 Act_Decl_Name : Node_Id;
2122 Act_Decl_Id : Entity_Id;
2127 Gen_Unit : Entity_Id;
2129 Is_Actual_Pack : Boolean := Is_Internal (Defining_Entity (N));
2130 Parent_Installed : Boolean := False;
2131 Renaming_List : List_Id;
2132 Unit_Renaming : Node_Id;
2133 Needs_Body : Boolean;
2134 Inline_Now : Boolean := False;
2136 procedure Delay_Descriptors (E : Entity_Id);
2137 -- Delay generation of subprogram descriptors for given entity
2139 function Might_Inline_Subp return Boolean;
2140 -- If inlining is active and the generic contains inlined subprograms,
2141 -- we instantiate the body. This may cause superfluous instantiations,
2142 -- but it is simpler than detecting the need for the body at the point
2143 -- of inlining, when the context of the instance is not available.
2145 -----------------------
2146 -- Delay_Descriptors --
2147 -----------------------
2149 procedure Delay_Descriptors (E : Entity_Id) is
2151 if not Delay_Subprogram_Descriptors (E) then
2152 Set_Delay_Subprogram_Descriptors (E);
2153 Pending_Descriptor.Increment_Last;
2154 Pending_Descriptor.Table (Pending_Descriptor.Last) := E;
2156 end Delay_Descriptors;
2158 -----------------------
2159 -- Might_Inline_Subp --
2160 -----------------------
2162 function Might_Inline_Subp return Boolean is
2166 if not Inline_Processing_Required then
2170 E := First_Entity (Gen_Unit);
2172 while Present (E) loop
2174 if Is_Subprogram (E)
2175 and then Is_Inlined (E)
2185 end Might_Inline_Subp;
2187 -- Start of processing for Analyze_Package_Instantiation
2190 -- Very first thing: apply the special kludge for Text_IO processing
2191 -- in case we are instantiating one of the children of [Wide_]Text_IO.
2193 Text_IO_Kludge (Name (N));
2195 -- Make node global for error reporting.
2197 Instantiation_Node := N;
2199 -- Case of instantiation of a generic package
2201 if Nkind (N) = N_Package_Instantiation then
2202 Act_Decl_Id := New_Copy (Defining_Entity (N));
2203 Set_Comes_From_Source (Act_Decl_Id, True);
2205 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
2207 Make_Defining_Program_Unit_Name (Loc,
2208 Name => New_Copy_Tree (Name (Defining_Unit_Name (N))),
2209 Defining_Identifier => Act_Decl_Id);
2211 Act_Decl_Name := Act_Decl_Id;
2214 -- Case of instantiation of a formal package
2217 Act_Decl_Id := Defining_Identifier (N);
2218 Act_Decl_Name := Act_Decl_Id;
2221 Generate_Definition (Act_Decl_Id);
2222 Pre_Analyze_Actuals (N);
2224 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2225 Gen_Unit := Entity (Gen_Id);
2227 -- Verify that it is the name of a generic package
2229 if Etype (Gen_Unit) = Any_Type then
2232 elsif Ekind (Gen_Unit) /= E_Generic_Package then
2234 ("expect name of generic package in instantiation", Gen_Id);
2238 if In_Extended_Main_Source_Unit (N) then
2239 Set_Is_Instantiated (Gen_Unit);
2240 Generate_Reference (Gen_Unit, N);
2242 if Present (Renamed_Object (Gen_Unit)) then
2243 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
2244 Generate_Reference (Renamed_Object (Gen_Unit), N);
2248 if Nkind (Gen_Id) = N_Identifier
2249 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
2252 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2254 elsif Nkind (Gen_Id) = N_Expanded_Name
2255 and then Is_Child_Unit (Gen_Unit)
2256 and then Nkind (Prefix (Gen_Id)) = N_Identifier
2257 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
2260 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
2263 -- If renaming, indicate this is an instantiation of renamed unit.
2265 if Present (Renamed_Object (Gen_Unit))
2266 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
2268 Gen_Unit := Renamed_Object (Gen_Unit);
2269 Set_Entity (Gen_Id, Gen_Unit);
2272 -- Verify that there are no circular instantiations.
2274 if In_Open_Scopes (Gen_Unit) then
2275 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
2278 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
2279 Error_Msg_Node_2 := Current_Scope;
2281 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
2282 Circularity_Detected := True;
2286 Save_Env (Gen_Unit, Act_Decl_Id);
2287 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2289 -- Initialize renamings map, for error checking, and the list
2290 -- that holds private entities whose views have changed between
2291 -- generic definition and instantiation. If this is the instance
2292 -- created to validate an actual package, the instantiation
2293 -- environment is that of the enclosing instance.
2295 Generic_Renamings.Set_Last (0);
2296 Generic_Renamings_HTable.Reset;
2298 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
2300 -- Copy original generic tree, to produce text for instantiation.
2304 (Original_Node (Gen_Decl), Empty, Instantiating => True);
2306 Act_Spec := Specification (Act_Tree);
2308 -- If this is the instance created to validate an actual package,
2309 -- only the formals matter, do not examine the package spec itself.
2311 if Is_Actual_Pack then
2312 Set_Visible_Declarations (Act_Spec, New_List);
2313 Set_Private_Declarations (Act_Spec, New_List);
2317 Analyze_Associations
2319 Generic_Formal_Declarations (Act_Tree),
2320 Generic_Formal_Declarations (Gen_Decl));
2322 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
2323 Set_Is_Generic_Instance (Act_Decl_Id);
2325 Set_Generic_Parent (Act_Spec, Gen_Unit);
2327 -- References to the generic in its own declaration or its body
2328 -- are references to the instance. Add a renaming declaration for
2329 -- the generic unit itself. This declaration, as well as the renaming
2330 -- declarations for the generic formals, must remain private to the
2331 -- unit: the formals, because this is the language semantics, and
2332 -- the unit because its use is an artifact of the implementation.
2335 Make_Package_Renaming_Declaration (Loc,
2336 Defining_Unit_Name =>
2337 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2338 Name => New_Reference_To (Act_Decl_Id, Loc));
2340 Append (Unit_Renaming, Renaming_List);
2342 -- The renaming declarations are the first local declarations of
2345 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
2347 (First (Visible_Declarations (Act_Spec)), Renaming_List);
2349 Set_Visible_Declarations (Act_Spec, Renaming_List);
2353 Make_Package_Declaration (Loc,
2354 Specification => Act_Spec);
2356 -- Save the instantiation node, for subsequent instantiation
2357 -- of the body, if there is one and we are generating code for
2358 -- the current unit. Mark the unit as having a body, to avoid
2359 -- a premature error message.
2361 -- We instantiate the body if we are generating code, if we are
2362 -- generating cross-reference information, or if we are building
2363 -- trees for ASIS use.
2366 Enclosing_Body_Present : Boolean := False;
2370 if Scope (Gen_Unit) /= Standard_Standard
2371 and then not Is_Child_Unit (Gen_Unit)
2373 Scop := Scope (Gen_Unit);
2375 while Present (Scop)
2376 and then Scop /= Standard_Standard
2378 if Unit_Requires_Body (Scop) then
2379 Enclosing_Body_Present := True;
2383 Scop := Scope (Scop);
2387 -- If front-end inlining is enabled, and this is a unit for which
2388 -- code will be generated, we instantiate the body at once.
2389 -- This is done if the instance is not the main unit, and if the
2390 -- generic is not a child unit, to avoid scope problems.
2392 if Front_End_Inlining
2393 and then Expander_Active
2394 and then not Is_Child_Unit (Gen_Unit)
2395 and then Is_In_Main_Unit (N)
2396 and then Nkind (Parent (N)) /= N_Compilation_Unit
2397 and then Might_Inline_Subp
2403 (Unit_Requires_Body (Gen_Unit)
2404 or else Enclosing_Body_Present
2405 or else Present (Corresponding_Body (Gen_Decl)))
2406 and then (Is_In_Main_Unit (N)
2407 or else Might_Inline_Subp)
2408 and then not Is_Actual_Pack
2409 and then not Inline_Now
2411 and then (Operating_Mode = Generate_Code
2412 or else (Operating_Mode = Check_Semantics
2413 and then Tree_Output));
2415 -- If front_end_inlining is enabled, do not instantiate a
2416 -- body if within a generic context.
2418 if Front_End_Inlining
2419 and then not Expander_Active
2421 Needs_Body := False;
2426 -- If we are generating the calling stubs from the instantiation
2427 -- of a generic RCI package, we will not use the body of the
2430 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
2431 and then Is_Compilation_Unit (Defining_Entity (N))
2433 Needs_Body := False;
2438 -- Here is a defence against a ludicrous number of instantiations
2439 -- caused by a circular set of instantiation attempts.
2441 if Pending_Instantiations.Last >
2442 Hostparm.Max_Instantiations
2444 Error_Msg_N ("too many instantiations", N);
2445 raise Unrecoverable_Error;
2448 -- Indicate that the enclosing scopes contain an instantiation,
2449 -- and that cleanup actions should be delayed until after the
2450 -- instance body is expanded.
2452 Check_Forward_Instantiation (N, Gen_Decl);
2453 if Nkind (N) = N_Package_Instantiation then
2455 Enclosing_Master : Entity_Id := Current_Scope;
2458 while Enclosing_Master /= Standard_Standard loop
2460 if Ekind (Enclosing_Master) = E_Package then
2461 if Is_Compilation_Unit (Enclosing_Master) then
2462 if In_Package_Body (Enclosing_Master) then
2464 (Body_Entity (Enclosing_Master));
2473 Enclosing_Master := Scope (Enclosing_Master);
2476 elsif Ekind (Enclosing_Master) = E_Generic_Package then
2477 Enclosing_Master := Scope (Enclosing_Master);
2479 elsif Ekind (Enclosing_Master) = E_Generic_Function
2480 or else Ekind (Enclosing_Master) = E_Generic_Procedure
2481 or else Ekind (Enclosing_Master) = E_Void
2483 -- Cleanup actions will eventually be performed on
2484 -- the enclosing instance, if any. enclosing scope
2485 -- is void in the formal part of a generic subp.
2490 if Ekind (Enclosing_Master) = E_Entry
2492 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
2495 Protected_Body_Subprogram (Enclosing_Master);
2498 Set_Delay_Cleanups (Enclosing_Master);
2500 while Ekind (Enclosing_Master) = E_Block loop
2501 Enclosing_Master := Scope (Enclosing_Master);
2504 if Is_Subprogram (Enclosing_Master) then
2505 Delay_Descriptors (Enclosing_Master);
2507 elsif Is_Task_Type (Enclosing_Master) then
2509 TBP : constant Node_Id :=
2510 Get_Task_Body_Procedure
2514 if Present (TBP) then
2515 Delay_Descriptors (TBP);
2516 Set_Delay_Cleanups (TBP);
2526 -- Make entry in table
2528 Pending_Instantiations.Increment_Last;
2529 Pending_Instantiations.Table (Pending_Instantiations.Last) :=
2530 (N, Act_Decl, Expander_Active, Current_Sem_Unit);
2534 Set_Categorization_From_Pragmas (Act_Decl);
2536 if Parent_Installed then
2540 Set_Instance_Spec (N, Act_Decl);
2542 -- Case of not a compilation unit
2544 if Nkind (Parent (N)) /= N_Compilation_Unit then
2545 Mark_Rewrite_Insertion (Act_Decl);
2546 Insert_Before (N, Act_Decl);
2549 -- Case of compilation unit that is generic instantiation
2551 -- Place declaration on current node so context is complete
2552 -- for analysis (including nested instantiations).
2555 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
2557 -- The entity for the current unit is the newly created one,
2558 -- and all semantic information is attached to it.
2560 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
2562 -- If this is the main unit, replace the main entity as well.
2564 if Current_Sem_Unit = Main_Unit then
2565 Main_Unit_Entity := Act_Decl_Id;
2569 Set_Unit (Parent (N), Act_Decl);
2570 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
2572 Set_Unit (Parent (N), N);
2573 Set_Body_Required (Parent (N), False);
2575 -- We never need elaboration checks on instantiations, since
2576 -- by definition, the body instantiation is elaborated at the
2577 -- same time as the spec instantiation.
2579 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
2580 Set_Suppress_Elaboration_Checks (Act_Decl_Id);
2583 Check_Elab_Instantiation (N);
2585 if ABE_Is_Certain (N) and then Needs_Body then
2586 Pending_Instantiations.Decrement_Last;
2588 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
2590 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
2591 First_Private_Entity (Act_Decl_Id));
2593 if Nkind (Parent (N)) = N_Compilation_Unit
2594 and then not Needs_Body
2596 Rewrite (N, Act_Decl);
2599 if Present (Corresponding_Body (Gen_Decl))
2600 or else Unit_Requires_Body (Gen_Unit)
2602 Set_Has_Completion (Act_Decl_Id);
2605 Check_Formal_Packages (Act_Decl_Id);
2607 Restore_Private_Views (Act_Decl_Id);
2609 if not Generic_Separately_Compiled (Gen_Unit) then
2610 Inherit_Context (Gen_Decl, N);
2613 if Parent_Installed then
2620 Validate_Categorization_Dependency (N, Act_Decl_Id);
2622 -- Check restriction, but skip this if something went wrong in
2623 -- the above analysis, indicated by Act_Decl_Id being void.
2625 if Ekind (Act_Decl_Id) /= E_Void
2626 and then not Is_Library_Level_Entity (Act_Decl_Id)
2628 Check_Restriction (No_Local_Allocators, N);
2632 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
2636 when Instantiation_Error =>
2637 if Parent_Installed then
2641 end Analyze_Package_Instantiation;
2643 ---------------------------
2644 -- Inline_Instance_Body --
2645 ---------------------------
2647 procedure Inline_Instance_Body
2649 Gen_Unit : Entity_Id;
2653 Gen_Comp : constant Entity_Id :=
2654 Cunit_Entity (Get_Source_Unit (Gen_Unit));
2655 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
2656 Curr_Scope : Entity_Id := Empty;
2657 Curr_Unit : constant Entity_Id :=
2658 Cunit_Entity (Current_Sem_Unit);
2659 Removed : Boolean := False;
2660 Num_Scopes : Int := 0;
2661 Use_Clauses : array (1 .. Scope_Stack.Last) of Node_Id;
2662 Instances : array (1 .. Scope_Stack.Last) of Entity_Id;
2663 Inner_Scopes : array (1 .. Scope_Stack.Last) of Entity_Id;
2664 Num_Inner : Int := 0;
2665 N_Instances : Int := 0;
2669 -- Case of generic unit defined in another unit
2671 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
2672 Vis := Is_Immediately_Visible (Gen_Comp);
2677 and then S /= Standard_Standard
2679 Num_Scopes := Num_Scopes + 1;
2681 Use_Clauses (Num_Scopes) :=
2683 (Scope_Stack.Last - Num_Scopes + 1).
2685 End_Use_Clauses (Use_Clauses (Num_Scopes));
2687 exit when Is_Generic_Instance (S)
2688 and then (In_Package_Body (S)
2689 or else Ekind (S) = E_Procedure
2690 or else Ekind (S) = E_Function);
2694 -- Find and save all enclosing instances.
2699 and then S /= Standard_Standard
2701 if Is_Generic_Instance (S) then
2702 N_Instances := N_Instances + 1;
2703 Instances (N_Instances) := S;
2709 -- Remove context of current compilation unit, unless we
2710 -- are within a nested package instantiation, in which case
2711 -- the context has been removed previously.
2712 -- If current scope is the body of a child unit, remove context
2718 and then S /= Standard_Standard
2720 exit when Is_Generic_Instance (S)
2721 and then (In_Package_Body (S)
2722 or else Ekind (S) = E_Procedure
2723 or else Ekind (S) = E_Function);
2726 or else (Ekind (Curr_Unit) = E_Package_Body
2727 and then S = Spec_Entity (Curr_Unit))
2731 if Is_Child_Unit (S) then
2732 -- Remove child unit from stack, as well as inner scopes.
2733 -- Removing the context of a child unit removes parent
2736 while Current_Scope /= S loop
2737 Num_Inner := Num_Inner + 1;
2738 Inner_Scopes (Num_Inner) := Current_Scope;
2743 Remove_Context (Curr_Comp);
2747 Remove_Context (Curr_Comp);
2750 if Ekind (Curr_Unit) = E_Package_Body then
2751 Remove_Context (Library_Unit (Curr_Comp));
2758 Instantiate_Package_Body
2759 ((N, Act_Decl, Expander_Active, Current_Sem_Unit));
2763 Set_Is_Immediately_Visible (Gen_Comp, Vis);
2765 -- Reset Generic_Instance flag so that use clauses can be installed
2766 -- in the proper order. (See Use_One_Package for effect of enclosing
2767 -- instances on processing of use clauses).
2769 for J in 1 .. N_Instances loop
2770 Set_Is_Generic_Instance (Instances (J), False);
2774 -- Make local entities not visible, so that when the context of
2775 -- unit is restored, there are not spurious hidings of use-
2776 -- visible entities (which appear in the environment before the
2779 if Current_Scope /= Standard_Standard then
2780 S := First_Entity (Current_Scope);
2782 while Present (S) loop
2783 if Is_Overloadable (S) then
2784 Set_Is_Immediately_Visible (S, False);
2791 Install_Context (Curr_Comp);
2793 if Current_Scope /= Standard_Standard then
2794 S := First_Entity (Current_Scope);
2796 while Present (S) loop
2797 if Is_Overloadable (S) then
2798 Set_Is_Immediately_Visible (S);
2805 if Present (Curr_Scope)
2806 and then Is_Child_Unit (Curr_Scope)
2808 New_Scope (Curr_Scope);
2809 Set_Is_Immediately_Visible (Curr_Scope);
2811 -- Finally, restore inner scopes as well.
2813 for J in reverse 1 .. Num_Inner loop
2814 New_Scope (Inner_Scopes (J));
2819 for J in reverse 1 .. Num_Scopes loop
2820 Install_Use_Clauses (Use_Clauses (J));
2823 for J in 1 .. N_Instances loop
2824 Set_Is_Generic_Instance (Instances (J), True);
2827 -- If generic unit is in current unit, current context is correct.
2830 Instantiate_Package_Body
2831 ((N, Act_Decl, Expander_Active, Current_Sem_Unit));
2833 end Inline_Instance_Body;
2835 -------------------------------------
2836 -- Analyze_Procedure_Instantiation --
2837 -------------------------------------
2839 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
2841 Analyze_Subprogram_Instantiation (N, E_Procedure);
2842 end Analyze_Procedure_Instantiation;
2844 --------------------------------------
2845 -- Analyze_Subprogram_Instantiation --
2846 --------------------------------------
2848 procedure Analyze_Subprogram_Instantiation
2852 Loc : constant Source_Ptr := Sloc (N);
2853 Gen_Id : constant Node_Id := Name (N);
2855 Act_Decl_Id : Entity_Id;
2856 Anon_Id : Entity_Id :=
2857 Make_Defining_Identifier
2858 (Sloc (Defining_Entity (N)),
2860 (Chars (Defining_Entity (N)), 'R'));
2865 Gen_Unit : Entity_Id;
2867 Pack_Id : Entity_Id;
2868 Parent_Installed : Boolean := False;
2869 Renaming_List : List_Id;
2872 procedure Analyze_Instance_And_Renamings;
2873 -- The instance must be analyzed in a context that includes the
2874 -- mappings of generic parameters into actuals. We create a package
2875 -- declaration for this purpose, and a subprogram with an internal
2876 -- name within the package. The subprogram instance is simply an
2877 -- alias for the internal subprogram, declared in the current scope.
2879 ------------------------------------
2880 -- Analyze_Instance_And_Renamings --
2881 ------------------------------------
2883 procedure Analyze_Instance_And_Renamings is
2884 Def_Ent : constant Entity_Id := Defining_Entity (N);
2885 Pack_Decl : Node_Id;
2888 if Nkind (Parent (N)) = N_Compilation_Unit then
2890 -- For the case of a compilation unit, the container package
2891 -- has the same name as the instantiation, to insure that the
2892 -- binder calls the elaboration procedure with the right name.
2893 -- Copy the entity of the instance, which may have compilation
2894 -- level flags (eg. is_child_unit) set.
2896 Pack_Id := New_Copy (Def_Ent);
2899 -- Otherwise we use the name of the instantiation concatenated
2900 -- with its source position to ensure uniqueness if there are
2901 -- several instantiations with the same name.
2904 Make_Defining_Identifier (Loc,
2905 Chars => New_External_Name
2906 (Related_Id => Chars (Def_Ent),
2908 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
2911 Pack_Decl := Make_Package_Declaration (Loc,
2912 Specification => Make_Package_Specification (Loc,
2913 Defining_Unit_Name => Pack_Id,
2914 Visible_Declarations => Renaming_List,
2915 End_Label => Empty));
2917 Set_Instance_Spec (N, Pack_Decl);
2918 Set_Is_Generic_Instance (Pack_Id);
2920 -- Case of not a compilation unit
2922 if Nkind (Parent (N)) /= N_Compilation_Unit then
2923 Mark_Rewrite_Insertion (Pack_Decl);
2924 Insert_Before (N, Pack_Decl);
2925 Set_Has_Completion (Pack_Id);
2927 -- Case of an instantiation that is a compilation unit
2929 -- Place declaration on current node so context is complete
2930 -- for analysis (including nested instantiations), and for
2931 -- use in a context_clause (see Analyze_With_Clause).
2934 Set_Unit (Parent (N), Pack_Decl);
2935 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
2938 Analyze (Pack_Decl);
2939 Check_Formal_Packages (Pack_Id);
2940 Set_Is_Generic_Instance (Pack_Id, False);
2942 -- Body of the enclosing package is supplied when instantiating
2943 -- the subprogram body, after semantic analysis is completed.
2945 if Nkind (Parent (N)) = N_Compilation_Unit then
2947 -- Remove package itself from visibility, so it does not
2948 -- conflict with subprogram.
2950 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
2952 -- Set name and scope of internal subprogram so that the
2953 -- proper external name will be generated. The proper scope
2954 -- is the scope of the wrapper package.
2956 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
2957 Set_Scope (Anon_Id, Scope (Pack_Id));
2960 Set_Is_Generic_Instance (Anon_Id);
2961 Act_Decl_Id := New_Copy (Anon_Id);
2963 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
2964 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
2965 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
2966 Set_Comes_From_Source (Act_Decl_Id, True);
2968 -- The signature may involve types that are not frozen yet, but
2969 -- the subprogram will be frozen at the point the wrapper package
2970 -- is frozen, so it does not need its own freeze node. In fact, if
2971 -- one is created, it might conflict with the freezing actions from
2972 -- the wrapper package (see 7206-013).
2974 Set_Has_Delayed_Freeze (Anon_Id, False);
2976 -- If the instance is a child unit, mark the Id accordingly. Mark
2977 -- the anonymous entity as well, which is the real subprogram and
2978 -- which is used when the instance appears in a context clause.
2980 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
2981 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
2982 New_Overloaded_Entity (Act_Decl_Id);
2983 Check_Eliminated (Act_Decl_Id);
2985 -- In compilation unit case, kill elaboration checks on the
2986 -- instantiation, since they are never needed -- the body is
2987 -- instantiated at the same point as the spec.
2989 if Nkind (Parent (N)) = N_Compilation_Unit then
2990 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
2991 Set_Suppress_Elaboration_Checks (Act_Decl_Id);
2992 Set_Is_Compilation_Unit (Anon_Id);
2994 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
2997 -- The instance is not a freezing point for the new subprogram.
2999 Set_Is_Frozen (Act_Decl_Id, False);
3001 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
3002 Valid_Operator_Definition (Act_Decl_Id);
3005 Set_Alias (Act_Decl_Id, Anon_Id);
3006 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
3007 Set_Has_Completion (Act_Decl_Id);
3008 Set_Related_Instance (Pack_Id, Act_Decl_Id);
3010 if Nkind (Parent (N)) = N_Compilation_Unit then
3011 Set_Body_Required (Parent (N), False);
3014 end Analyze_Instance_And_Renamings;
3016 -- Start of processing for Analyze_Subprogram_Instantiation
3019 -- Very first thing: apply the special kludge for Text_IO processing
3020 -- in case we are instantiating one of the children of [Wide_]Text_IO.
3021 -- Of course such an instantiation is bogus (these are packages, not
3022 -- subprograms), but we get a better error message if we do this.
3024 Text_IO_Kludge (Gen_Id);
3026 -- Make node global for error reporting.
3028 Instantiation_Node := N;
3029 Pre_Analyze_Actuals (N);
3031 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3032 Gen_Unit := Entity (Gen_Id);
3034 Generate_Reference (Gen_Unit, Gen_Id);
3036 if Nkind (Gen_Id) = N_Identifier
3037 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3040 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3043 if Etype (Gen_Unit) = Any_Type then return; end if;
3045 -- Verify that it is a generic subprogram of the right kind, and that
3046 -- it does not lead to a circular instantiation.
3048 if Ekind (Gen_Unit) /= E_Generic_Procedure
3049 and then Ekind (Gen_Unit) /= E_Generic_Function
3051 Error_Msg_N ("expect generic subprogram in instantiation", Gen_Id);
3053 elsif In_Open_Scopes (Gen_Unit) then
3054 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3056 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3057 Error_Msg_Node_2 := Current_Scope;
3059 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3060 Circularity_Detected := True;
3062 elsif K = E_Procedure
3063 and then Ekind (Gen_Unit) /= E_Generic_Procedure
3065 if Ekind (Gen_Unit) = E_Generic_Function then
3067 ("cannot instantiate generic function as procedure", Gen_Id);
3070 ("expect name of generic procedure in instantiation", Gen_Id);
3073 elsif K = E_Function
3074 and then Ekind (Gen_Unit) /= E_Generic_Function
3076 if Ekind (Gen_Unit) = E_Generic_Procedure then
3078 ("cannot instantiate generic procedure as function", Gen_Id);
3081 ("expect name of generic function in instantiation", Gen_Id);
3085 -- If renaming, indicate that this is instantiation of renamed unit
3087 if Present (Renamed_Object (Gen_Unit))
3088 and then (Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Procedure
3090 Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Function)
3092 Gen_Unit := Renamed_Object (Gen_Unit);
3093 Set_Entity (Gen_Id, Gen_Unit);
3096 if In_Extended_Main_Source_Unit (N) then
3097 Set_Is_Instantiated (Gen_Unit);
3098 Generate_Reference (Gen_Unit, N);
3101 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3102 Spec := Specification (Gen_Decl);
3104 -- The subprogram itself cannot contain a nested instance, so
3105 -- the current parent is left empty.
3107 Save_Env (Gen_Unit, Empty);
3109 -- Initialize renamings map, for error checking.
3111 Generic_Renamings.Set_Last (0);
3112 Generic_Renamings_HTable.Reset;
3114 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3116 -- Copy original generic tree, to produce text for instantiation.
3120 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3122 Act_Spec := Specification (Act_Tree);
3124 Analyze_Associations
3126 Generic_Formal_Declarations (Act_Tree),
3127 Generic_Formal_Declarations (Gen_Decl));
3129 -- Build the subprogram declaration, which does not appear
3130 -- in the generic template, and give it a sloc consistent
3131 -- with that of the template.
3133 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
3134 Set_Generic_Parent (Act_Spec, Gen_Unit);
3136 Make_Subprogram_Declaration (Sloc (Act_Spec),
3137 Specification => Act_Spec);
3139 Set_Categorization_From_Pragmas (Act_Decl);
3141 if Parent_Installed then
3145 Append (Act_Decl, Renaming_List);
3146 Analyze_Instance_And_Renamings;
3148 -- If the generic is marked Import (Intrinsic), then so is the
3149 -- instance. This indicates that there is no body to instantiate.
3150 -- If generic is marked inline, so it the instance, and the
3151 -- anonymous subprogram it renames. If inlined, or else if inlining
3152 -- is enabled for the compilation, we generate the instance body
3153 -- even if it is not within the main unit.
3155 -- Any other pragmas might also be inherited ???
3157 if Is_Intrinsic_Subprogram (Gen_Unit) then
3158 Set_Is_Intrinsic_Subprogram (Anon_Id);
3159 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
3161 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
3162 Validate_Unchecked_Conversion (N, Act_Decl_Id);
3166 Generate_Definition (Act_Decl_Id);
3168 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
3169 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
3171 Check_Elab_Instantiation (N);
3172 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
3174 -- Subject to change, pending on if other pragmas are inherited ???
3176 Validate_Categorization_Dependency (N, Act_Decl_Id);
3178 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
3180 if not Generic_Separately_Compiled (Gen_Unit) then
3181 Inherit_Context (Gen_Decl, N);
3184 Restore_Private_Views (Pack_Id, False);
3186 -- If the context requires a full instantiation, mark node for
3187 -- subsequent construction of the body.
3189 if (Is_In_Main_Unit (N)
3190 or else Is_Inlined (Act_Decl_Id))
3191 and then (Operating_Mode = Generate_Code
3192 or else (Operating_Mode = Check_Semantics
3193 and then Tree_Output))
3194 and then (Expander_Active or else Tree_Output)
3195 and then not ABE_Is_Certain (N)
3196 and then not Is_Eliminated (Act_Decl_Id)
3198 Pending_Instantiations.Increment_Last;
3199 Pending_Instantiations.Table (Pending_Instantiations.Last) :=
3200 (N, Act_Decl, Expander_Active, Current_Sem_Unit);
3201 Check_Forward_Instantiation (N, Gen_Decl);
3203 -- The wrapper package is always delayed, because it does
3204 -- not constitute a freeze point, but to insure that the
3205 -- freeze node is placed properly, it is created directly
3206 -- when instantiating the body (otherwise the freeze node
3207 -- might appear to early for nested instantiations).
3209 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3211 -- For ASIS purposes, indicate that the wrapper package has
3212 -- replaced the instantiation node.
3214 Rewrite (N, Unit (Parent (N)));
3215 Set_Unit (Parent (N), N);
3218 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3220 -- Replace instance node for library-level instantiations
3221 -- of intrinsic subprograms, for ASIS use.
3223 Rewrite (N, Unit (Parent (N)));
3224 Set_Unit (Parent (N), N);
3227 if Parent_Installed then
3232 Generic_Renamings.Set_Last (0);
3233 Generic_Renamings_HTable.Reset;
3237 when Instantiation_Error =>
3238 if Parent_Installed then
3242 end Analyze_Subprogram_Instantiation;
3244 -------------------------
3245 -- Get_Associated_Node --
3246 -------------------------
3248 function Get_Associated_Node (N : Node_Id) return Node_Id is
3249 Assoc : Node_Id := Associated_Node (N);
3252 if Nkind (Assoc) /= Nkind (N) then
3255 elsif Nkind (Assoc) = N_Aggregate
3256 or else Nkind (Assoc) = N_Extension_Aggregate
3260 -- If the node is part of an inner generic, it may itself have been
3261 -- remapped into a further generic copy. Associated_Node is otherwise
3262 -- used for the entity of the node, and will be of a different node
3263 -- kind, or else N has been rewritten as a literal or function call.
3265 while Present (Associated_Node (Assoc))
3266 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
3268 Assoc := Associated_Node (Assoc);
3271 -- Follow and additional link in case the final node was rewritten.
3272 -- This can only happen with nested generic units.
3274 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
3275 and then Present (Associated_Node (Assoc))
3276 and then (Nkind (Associated_Node (Assoc)) = N_Function_Call
3278 Nkind (Associated_Node (Assoc)) = N_Explicit_Dereference
3280 Nkind (Associated_Node (Assoc)) = N_Integer_Literal
3282 Nkind (Associated_Node (Assoc)) = N_Real_Literal
3284 Nkind (Associated_Node (Assoc)) = N_String_Literal)
3286 Assoc := Associated_Node (Assoc);
3291 end Get_Associated_Node;
3293 -------------------------------------------
3294 -- Build_Instance_Compilation_Unit_Nodes --
3295 -------------------------------------------
3297 procedure Build_Instance_Compilation_Unit_Nodes
3302 Decl_Cunit : Node_Id;
3303 Body_Cunit : Node_Id;
3305 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
3306 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
3309 -- A new compilation unit node is built for the instance declaration
3312 Make_Compilation_Unit (Sloc (N),
3313 Context_Items => Empty_List,
3316 Make_Compilation_Unit_Aux (Sloc (N)));
3318 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
3319 Set_Body_Required (Decl_Cunit, True);
3321 -- We use the original instantiation compilation unit as the resulting
3322 -- compilation unit of the instance, since this is the main unit.
3324 Rewrite (N, Act_Body);
3325 Body_Cunit := Parent (N);
3327 -- The two compilation unit nodes are linked by the Library_Unit field
3329 Set_Library_Unit (Decl_Cunit, Body_Cunit);
3330 Set_Library_Unit (Body_Cunit, Decl_Cunit);
3332 -- The context clause items on the instantiation, which are now
3333 -- attached to the body compilation unit (since the body overwrote
3334 -- the original instantiation node), semantically belong on the spec,
3335 -- so copy them there. It's harmless to leave them on the body as well.
3336 -- In fact one could argue that they belong in both places.
3338 Citem := First (Context_Items (Body_Cunit));
3339 while Present (Citem) loop
3340 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
3344 -- Propagate categorization flags on packages, so that they appear
3345 -- in ali file for the spec of the unit.
3347 if Ekind (New_Main) = E_Package then
3348 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
3349 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
3350 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
3351 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
3352 Set_Is_Remote_Call_Interface
3353 (Old_Main, Is_Remote_Call_Interface (New_Main));
3356 -- Make entry in Units table, so that binder can generate call to
3357 -- elaboration procedure for body, if any.
3359 Make_Instance_Unit (Body_Cunit);
3360 Main_Unit_Entity := New_Main;
3361 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
3363 -- Build elaboration entity, since the instance may certainly
3364 -- generate elaboration code requiring a flag for protection.
3366 Build_Elaboration_Entity (Decl_Cunit, New_Main);
3367 end Build_Instance_Compilation_Unit_Nodes;
3369 -----------------------------------
3370 -- Check_Formal_Package_Instance --
3371 -----------------------------------
3373 -- If the formal has specific parameters, they must match those of the
3374 -- actual. Both of them are instances, and the renaming declarations
3375 -- for their formal parameters appear in the same order in both. The
3376 -- analyzed formal has been analyzed in the context of the current
3379 procedure Check_Formal_Package_Instance
3380 (Formal_Pack : Entity_Id;
3381 Actual_Pack : Entity_Id)
3383 E1 : Entity_Id := First_Entity (Actual_Pack);
3384 E2 : Entity_Id := First_Entity (Formal_Pack);
3389 procedure Check_Mismatch (B : Boolean);
3390 -- Common error routine for mismatch between the parameters of
3391 -- the actual instance and those of the formal package.
3393 procedure Check_Mismatch (B : Boolean) is
3397 ("actual for & in actual instance does not match formal",
3398 Parent (Actual_Pack), E1);
3402 -- Start of processing for Check_Formal_Package_Instance
3406 and then Present (E2)
3408 exit when Ekind (E1) = E_Package
3409 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
3411 if Is_Type (E1) then
3413 -- Subtypes must statically match. E1 and E2 are the
3414 -- local entities that are subtypes of the actuals.
3415 -- Itypes generated for other parameters need not be checked,
3416 -- the check will be performed on the parameters themselves.
3418 if not Is_Itype (E1)
3419 and then not Is_Itype (E2)
3423 or else Etype (E1) /= Etype (E2)
3424 or else not Subtypes_Statically_Match (E1, E2));
3427 elsif Ekind (E1) = E_Constant then
3429 -- IN parameters must denote the same static value, or
3430 -- the same constant, or the literal null.
3432 Expr1 := Expression (Parent (E1));
3434 if Ekind (E2) /= E_Constant then
3435 Check_Mismatch (True);
3438 Expr2 := Expression (Parent (E2));
3441 if Is_Static_Expression (Expr1) then
3443 if not Is_Static_Expression (Expr2) then
3444 Check_Mismatch (True);
3446 elsif Is_Integer_Type (Etype (E1)) then
3449 V1 : Uint := Expr_Value (Expr1);
3450 V2 : Uint := Expr_Value (Expr2);
3452 Check_Mismatch (V1 /= V2);
3455 elsif Is_Real_Type (Etype (E1)) then
3458 V1 : Ureal := Expr_Value_R (Expr1);
3459 V2 : Ureal := Expr_Value_R (Expr2);
3461 Check_Mismatch (V1 /= V2);
3464 elsif Is_String_Type (Etype (E1))
3465 and then Nkind (Expr1) = N_String_Literal
3468 if Nkind (Expr2) /= N_String_Literal then
3469 Check_Mismatch (True);
3472 (not String_Equal (Strval (Expr1), Strval (Expr2)));
3476 elsif Is_Entity_Name (Expr1) then
3477 if Is_Entity_Name (Expr2) then
3478 if Entity (Expr1) = Entity (Expr2) then
3481 elsif Ekind (Entity (Expr2)) = E_Constant
3482 and then Is_Entity_Name (Constant_Value (Entity (Expr2)))
3484 Entity (Constant_Value (Entity (Expr2))) = Entity (Expr1)
3488 Check_Mismatch (True);
3491 Check_Mismatch (True);
3494 elsif Nkind (Expr1) = N_Null then
3495 Check_Mismatch (Nkind (Expr1) /= N_Null);
3498 Check_Mismatch (True);
3501 elsif Ekind (E1) = E_Variable
3502 or else Ekind (E1) = E_Package
3505 (Ekind (E1) /= Ekind (E2)
3506 or else Renamed_Object (E1) /= Renamed_Object (E2));
3508 elsif Is_Overloadable (E1) then
3510 -- Verify that the names of the entities match.
3511 -- What if actual is an attribute ???
3514 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
3517 raise Program_Error;
3524 end Check_Formal_Package_Instance;
3526 ---------------------------
3527 -- Check_Formal_Packages --
3528 ---------------------------
3530 procedure Check_Formal_Packages (P_Id : Entity_Id) is
3532 Formal_P : Entity_Id;
3535 -- Iterate through the declarations in the instance, looking for
3536 -- package renaming declarations that denote instances of formal
3537 -- packages. Stop when we find the renaming of the current package
3538 -- itself. The declaration for a formal package without a box is
3539 -- followed by an internal entity that repeats the instantiation.
3541 E := First_Entity (P_Id);
3542 while Present (E) loop
3543 if Ekind (E) = E_Package then
3544 if Renamed_Object (E) = P_Id then
3547 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
3550 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
3551 Formal_P := Next_Entity (E);
3552 Check_Formal_Package_Instance (Formal_P, E);
3558 end Check_Formal_Packages;
3560 ---------------------------------
3561 -- Check_Forward_Instantiation --
3562 ---------------------------------
3564 procedure Check_Forward_Instantiation (N : Node_Id; Decl : Node_Id) is
3566 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
3569 -- The instantiation appears before the generic body if we are in the
3570 -- scope of the unit containing the generic, either in its spec or in
3571 -- the package body. and before the generic body.
3573 if Ekind (Gen_Comp) = E_Package_Body then
3574 Gen_Comp := Spec_Entity (Gen_Comp);
3577 if In_Open_Scopes (Gen_Comp)
3578 and then No (Corresponding_Body (Decl))
3583 and then not Is_Compilation_Unit (S)
3584 and then not Is_Child_Unit (S)
3586 if Ekind (S) = E_Package then
3587 Set_Has_Forward_Instantiation (S);
3593 end Check_Forward_Instantiation;
3595 ---------------------------
3596 -- Check_Generic_Actuals --
3597 ---------------------------
3599 -- The visibility of the actuals may be different between the
3600 -- point of generic instantiation and the instantiation of the body.
3602 procedure Check_Generic_Actuals
3603 (Instance : Entity_Id;
3604 Is_Formal_Box : Boolean)
3610 E := First_Entity (Instance);
3611 while Present (E) loop
3613 and then Nkind (Parent (E)) = N_Subtype_Declaration
3614 and then Scope (Etype (E)) /= Instance
3615 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
3617 Check_Private_View (Subtype_Indication (Parent (E)));
3618 Set_Is_Generic_Actual_Type (E, True);
3619 Set_Is_Hidden (E, False);
3621 -- We constructed the generic actual type as a subtype of
3622 -- the supplied type. This means that it normally would not
3623 -- inherit subtype specific attributes of the actual, which
3624 -- is wrong for the generic case.
3626 Astype := Ancestor_Subtype (E);
3630 -- can happen when E is an itype that is the full view of
3631 -- a private type completed, e.g. with a constrained array.
3633 Astype := Base_Type (E);
3636 Set_Size_Info (E, (Astype));
3637 Set_RM_Size (E, RM_Size (Astype));
3638 Set_First_Rep_Item (E, First_Rep_Item (Astype));
3640 if Is_Discrete_Or_Fixed_Point_Type (E) then
3641 Set_RM_Size (E, RM_Size (Astype));
3643 -- In nested instances, the base type of an access actual
3644 -- may itself be private, and need to be exchanged.
3646 elsif Is_Access_Type (E)
3647 and then Is_Private_Type (Etype (E))
3650 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
3653 elsif Ekind (E) = E_Package then
3655 -- If this is the renaming for the current instance, we're done.
3656 -- Otherwise it is a formal package. If the corresponding formal
3657 -- was declared with a box, the (instantiations of the) generic
3658 -- formal part are also visible. Otherwise, ignore the entity
3659 -- created to validate the actuals.
3661 if Renamed_Object (E) = Instance then
3664 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
3667 -- The visibility of a formal of an enclosing generic is already
3670 elsif Denotes_Formal_Package (E) then
3673 elsif Present (Associated_Formal_Package (E))
3674 and then Box_Present (Parent (Associated_Formal_Package (E)))
3676 Check_Generic_Actuals (Renamed_Object (E), True);
3677 Set_Is_Hidden (E, False);
3681 Set_Is_Hidden (E, not Is_Formal_Box);
3687 end Check_Generic_Actuals;
3689 ------------------------------
3690 -- Check_Generic_Child_Unit --
3691 ------------------------------
3693 procedure Check_Generic_Child_Unit
3695 Parent_Installed : in out Boolean)
3697 Loc : constant Source_Ptr := Sloc (Gen_Id);
3698 Gen_Par : Entity_Id := Empty;
3699 Inst_Par : Entity_Id;
3703 function Find_Generic_Child
3707 -- Search generic parent for possible child unit.
3709 function In_Enclosing_Instance return Boolean;
3710 -- Within an instance of the parent, the child unit may be denoted
3711 -- by a simple name. Examine enclosing scopes to locate a possible
3712 -- parent instantiation.
3714 function Find_Generic_Child
3722 -- If entity of name is already set, instance has already been
3723 -- resolved, e.g. in an enclosing instantiation.
3725 if Present (Entity (Id)) then
3726 if Scope (Entity (Id)) = Scop then
3733 E := First_Entity (Scop);
3734 while Present (E) loop
3735 if Chars (E) = Chars (Id)
3736 and then Is_Child_Unit (E)
3738 if Is_Child_Unit (E)
3739 and then not Is_Visible_Child_Unit (E)
3742 ("generic child unit& is not visible", Gen_Id, E);
3754 end Find_Generic_Child;
3756 function In_Enclosing_Instance return Boolean is
3757 Enclosing_Instance : Node_Id;
3760 Enclosing_Instance := Current_Scope;
3762 while Present (Enclosing_Instance) loop
3763 exit when Ekind (Enclosing_Instance) = E_Package
3764 and then Nkind (Parent (Enclosing_Instance)) =
3765 N_Package_Specification
3767 (Generic_Parent (Parent (Enclosing_Instance)));
3769 Enclosing_Instance := Scope (Enclosing_Instance);
3772 if Present (Enclosing_Instance) then
3773 E := Find_Generic_Child
3774 (Generic_Parent (Parent (Enclosing_Instance)), Gen_Id);
3781 Make_Expanded_Name (Loc,
3783 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
3784 Selector_Name => New_Occurrence_Of (E, Loc)));
3786 Set_Entity (Gen_Id, E);
3787 Set_Etype (Gen_Id, Etype (E));
3788 Parent_Installed := False; -- Already in scope.
3794 end In_Enclosing_Instance;
3796 -- Start of processing for Check_Generic_Child_Unit
3799 -- If the name of the generic is given by a selected component, it
3800 -- may be the name of a generic child unit, and the prefix is the name
3801 -- of an instance of the parent, in which case the child unit must be
3802 -- visible. If this instance is not in scope, it must be placed there
3803 -- and removed after instantiation, because what is being instantiated
3804 -- is not the original child, but the corresponding child present in
3805 -- the instance of the parent.
3807 -- If the child is instantiated within the parent, it can be given by
3808 -- a simple name. In this case the instance is already in scope, but
3809 -- the child generic must be recovered from the generic parent as well.
3811 if Nkind (Gen_Id) = N_Selected_Component then
3812 S := Selector_Name (Gen_Id);
3813 Analyze (Prefix (Gen_Id));
3814 Inst_Par := Entity (Prefix (Gen_Id));
3816 if Ekind (Inst_Par) = E_Package
3817 and then Present (Renamed_Object (Inst_Par))
3819 Inst_Par := Renamed_Object (Inst_Par);
3822 if Ekind (Inst_Par) = E_Package then
3823 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
3824 Gen_Par := Generic_Parent (Parent (Inst_Par));
3826 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
3828 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
3830 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
3833 elsif Ekind (Inst_Par) = E_Generic_Package
3834 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
3837 -- A formal package may be a real child package, and not the
3838 -- implicit instance within a parent. In this case the child is
3839 -- not visible and has to be retrieved explicitly as well.
3841 Gen_Par := Inst_Par;
3844 if Present (Gen_Par) then
3846 -- The prefix denotes an instantiation. The entity itself
3847 -- may be a nested generic, or a child unit.
3849 E := Find_Generic_Child (Gen_Par, S);
3852 Change_Selected_Component_To_Expanded_Name (Gen_Id);
3853 Set_Entity (Gen_Id, E);
3854 Set_Etype (Gen_Id, Etype (E));
3856 Set_Etype (S, Etype (E));
3858 -- Indicate that this is a reference to the parent.
3860 if In_Extended_Main_Source_Unit (Gen_Id) then
3861 Set_Is_Instantiated (Inst_Par);
3864 -- A common mistake is to replicate the naming scheme of
3865 -- a hierarchy by instantiating a generic child directly,
3866 -- rather than the implicit child in a parent instance:
3868 -- generic .. package Gpar is ..
3869 -- generic .. package Gpar.Child is ..
3870 -- package Par is new Gpar ();
3873 -- package Par.Child is new Gpar.Child ();
3874 -- rather than Par.Child
3876 -- In this case the instantiation is within Par, which is
3877 -- an instance, but Gpar does not denote Par because we are
3878 -- not IN the instance of Gpar, so this is illegal. The test
3879 -- below recognizes this particular case.
3881 if Is_Child_Unit (E)
3882 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
3883 and then (not In_Instance
3884 or else Nkind (Parent (Parent (Gen_Id))) =
3888 ("prefix of generic child unit must be instance of parent",
3892 if not In_Open_Scopes (Inst_Par)
3893 and then Nkind (Parent (Gen_Id))
3894 not in N_Generic_Renaming_Declaration
3896 Install_Parent (Inst_Par);
3897 Parent_Installed := True;
3901 -- If the generic parent does not contain an entity that
3902 -- corresponds to the selector, the instance doesn't either.
3903 -- Analyzing the node will yield the appropriate error message.
3904 -- If the entity is not a child unit, then it is an inner
3905 -- generic in the parent.
3913 if Is_Child_Unit (Entity (Gen_Id))
3914 and then Nkind (Parent (Gen_Id))
3915 not in N_Generic_Renaming_Declaration
3916 and then not In_Open_Scopes (Inst_Par)
3918 Install_Parent (Inst_Par);
3919 Parent_Installed := True;
3923 elsif Nkind (Gen_Id) = N_Expanded_Name then
3925 -- Entity already present, analyze prefix, whose meaning may be
3926 -- an instance in the current context. If it is an instance of
3927 -- a relative within another, the proper parent may still have
3928 -- to be installed, if they are not of the same generation.
3930 Analyze (Prefix (Gen_Id));
3931 Inst_Par := Entity (Prefix (Gen_Id));
3933 if In_Enclosing_Instance then
3936 elsif Present (Entity (Gen_Id))
3937 and then Is_Child_Unit (Entity (Gen_Id))
3938 and then not In_Open_Scopes (Inst_Par)
3940 Install_Parent (Inst_Par);
3941 Parent_Installed := True;
3944 elsif In_Enclosing_Instance then
3945 -- The child unit is found in some enclosing scope.
3951 -- If this is the renaming of the implicit child in a parent
3952 -- instance, recover the parent name and install it.
3954 if Is_Entity_Name (Gen_Id) then
3955 E := Entity (Gen_Id);
3957 if Is_Generic_Unit (E)
3958 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
3959 and then Is_Child_Unit (Renamed_Object (E))
3960 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
3961 and then Nkind (Name (Parent (E))) = N_Expanded_Name
3964 New_Copy_Tree (Name (Parent (E))));
3965 Inst_Par := Entity (Prefix (Gen_Id));
3967 if not In_Open_Scopes (Inst_Par) then
3968 Install_Parent (Inst_Par);
3969 Parent_Installed := True;
3972 -- If it is a child unit of a non-generic parent, it may be
3973 -- use-visible and given by a direct name. Install parent as
3976 elsif Is_Generic_Unit (E)
3977 and then Is_Child_Unit (E)
3979 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
3980 and then not Is_Generic_Unit (Scope (E))
3982 if not In_Open_Scopes (Scope (E)) then
3983 Install_Parent (Scope (E));
3984 Parent_Installed := True;
3989 end Check_Generic_Child_Unit;
3991 -----------------------------
3992 -- Check_Hidden_Child_Unit --
3993 -----------------------------
3995 procedure Check_Hidden_Child_Unit
3997 Gen_Unit : Entity_Id;
3998 Act_Decl_Id : Entity_Id)
4000 Gen_Id : Node_Id := Name (N);
4003 if Is_Child_Unit (Gen_Unit)
4004 and then Is_Child_Unit (Act_Decl_Id)
4005 and then Nkind (Gen_Id) = N_Expanded_Name
4006 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
4007 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
4009 Error_Msg_Node_2 := Scope (Act_Decl_Id);
4011 ("generic unit & is implicitly declared in &",
4012 Defining_Unit_Name (N), Gen_Unit);
4013 Error_Msg_N ("\instance must have different name",
4014 Defining_Unit_Name (N));
4016 end Check_Hidden_Child_Unit;
4018 ------------------------
4019 -- Check_Private_View --
4020 ------------------------
4022 procedure Check_Private_View (N : Node_Id) is
4023 T : constant Entity_Id := Etype (N);
4027 -- Exchange views if the type was not private in the generic but is
4028 -- private at the point of instantiation. Do not exchange views if
4029 -- the scope of the type is in scope. This can happen if both generic
4030 -- and instance are sibling units, or if type is defined in a parent.
4031 -- In this case the visibility of the type will be correct for all
4035 BT := Base_Type (T);
4037 if Is_Private_Type (T)
4038 and then not Has_Private_View (N)
4039 and then Present (Full_View (T))
4040 and then not In_Open_Scopes (Scope (T))
4042 -- In the generic, the full type was visible. Save the
4043 -- private entity, for subsequent exchange.
4047 elsif Has_Private_View (N)
4048 and then not Is_Private_Type (T)
4049 and then not Has_Been_Exchanged (T)
4050 and then Etype (Get_Associated_Node (N)) /= T
4052 -- Only the private declaration was visible in the generic. If
4053 -- the type appears in a subtype declaration, the subtype in the
4054 -- instance must have a view compatible with that of its parent,
4055 -- which must be exchanged (see corresponding code in Restore_
4056 -- Private_Views). Otherwise, if the type is defined in a parent
4057 -- unit, leave full visibility within instance, which is safe.
4059 if In_Open_Scopes (Scope (Base_Type (T)))
4060 and then not Is_Private_Type (Base_Type (T))
4061 and then Comes_From_Source (Base_Type (T))
4065 elsif Nkind (Parent (N)) = N_Subtype_Declaration
4066 or else not In_Private_Part (Scope (Base_Type (T)))
4068 Append_Elmt (T, Exchanged_Views);
4069 Exchange_Declarations (Etype (Get_Associated_Node (N)));
4072 -- For composite types with inconsistent representation
4073 -- exchange component types accordingly.
4075 elsif Is_Access_Type (T)
4076 and then Is_Private_Type (Designated_Type (T))
4077 and then Present (Full_View (Designated_Type (T)))
4079 Switch_View (Designated_Type (T));
4081 elsif Is_Array_Type (T)
4082 and then Is_Private_Type (Component_Type (T))
4083 and then not Has_Private_View (N)
4084 and then Present (Full_View (Component_Type (T)))
4086 Switch_View (Component_Type (T));
4088 elsif Is_Private_Type (T)
4089 and then Present (Full_View (T))
4090 and then Is_Array_Type (Full_View (T))
4091 and then Is_Private_Type (Component_Type (Full_View (T)))
4095 -- Finally, a non-private subtype may have a private base type,
4096 -- which must be exchanged for consistency. This can happen when
4097 -- instantiating a package body, when the scope stack is empty but
4098 -- in fact the subtype and the base type are declared in an enclosing
4101 elsif not Is_Private_Type (T)
4102 and then not Has_Private_View (N)
4103 and then Is_Private_Type (Base_Type (T))
4104 and then Present (Full_View (BT))
4105 and then not Is_Generic_Type (BT)
4106 and then not In_Open_Scopes (BT)
4108 Append_Elmt (Full_View (BT), Exchanged_Views);
4109 Exchange_Declarations (BT);
4112 end Check_Private_View;
4114 --------------------------
4115 -- Contains_Instance_Of --
4116 --------------------------
4118 function Contains_Instance_Of
4130 -- Verify that there are no circular instantiations. We check whether
4131 -- the unit contains an instance of the current scope or some enclosing
4132 -- scope (in case one of the instances appears in a subunit). Longer
4133 -- circularities involving subunits might seem too pathological to
4134 -- consider, but they were not too pathological for the authors of
4135 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
4136 -- enclosing generic scopes as containing an instance.
4139 -- Within a generic subprogram body, the scope is not generic, to
4140 -- allow for recursive subprograms. Use the declaration to determine
4141 -- whether this is a generic unit.
4143 if Ekind (Scop) = E_Generic_Package
4144 or else (Is_Subprogram (Scop)
4145 and then Nkind (Unit_Declaration_Node (Scop)) =
4146 N_Generic_Subprogram_Declaration)
4148 Elmt := First_Elmt (Inner_Instances (Inner));
4150 while Present (Elmt) loop
4151 if Node (Elmt) = Scop then
4152 Error_Msg_Node_2 := Inner;
4154 ("circular Instantiation: & instantiated within &!",
4158 elsif Node (Elmt) = Inner then
4161 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
4162 Error_Msg_Node_2 := Inner;
4164 ("circular Instantiation: & instantiated within &!",
4172 -- Indicate that Inner is being instantiated within Scop.
4174 Append_Elmt (Inner, Inner_Instances (Scop));
4177 if Scop = Standard_Standard then
4180 Scop := Scope (Scop);
4185 end Contains_Instance_Of;
4187 -----------------------
4188 -- Copy_Generic_Node --
4189 -----------------------
4191 function Copy_Generic_Node
4193 Parent_Id : Node_Id;
4194 Instantiating : Boolean)
4200 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
4201 -- Check the given value of one of the Fields referenced by the
4202 -- current node to determine whether to copy it recursively. The
4203 -- field may hold a Node_Id, a List_Id, or an Elist_Id, or a plain
4204 -- value (Sloc, Uint, Char) in which case it need not be copied.
4206 procedure Copy_Descendants;
4207 -- Common utility for various nodes.
4209 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
4210 -- Make copy of element list.
4212 function Copy_Generic_List
4214 Parent_Id : Node_Id)
4216 -- Apply Copy_Node recursively to the members of a node list.
4218 -----------------------
4219 -- Copy_Descendants --
4220 -----------------------
4222 procedure Copy_Descendants is
4224 use Atree.Unchecked_Access;
4225 -- This code section is part of the implementation of an untyped
4226 -- tree traversal, so it needs direct access to node fields.
4229 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
4230 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
4231 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
4232 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
4233 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
4234 end Copy_Descendants;
4236 -----------------------------
4237 -- Copy_Generic_Descendant --
4238 -----------------------------
4240 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
4242 if D = Union_Id (Empty) then
4245 elsif D in Node_Range then
4247 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
4249 elsif D in List_Range then
4250 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
4252 elsif D in Elist_Range then
4253 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
4255 -- Nothing else is copyable (e.g. Uint values), return as is
4260 end Copy_Generic_Descendant;
4262 ------------------------
4263 -- Copy_Generic_Elist --
4264 ------------------------
4266 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
4273 M := First_Elmt (E);
4274 while Present (M) loop
4276 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
4285 end Copy_Generic_Elist;
4287 -----------------------
4288 -- Copy_Generic_List --
4289 -----------------------
4291 function Copy_Generic_List
4293 Parent_Id : Node_Id)
4302 Set_Parent (New_L, Parent_Id);
4305 while Present (N) loop
4306 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
4315 end Copy_Generic_List;
4317 -- Start of processing for Copy_Generic_Node
4324 New_N := New_Copy (N);
4326 if Instantiating then
4327 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
4330 if not Is_List_Member (N) then
4331 Set_Parent (New_N, Parent_Id);
4334 -- If defining identifier, then all fields have been copied already
4336 if Nkind (New_N) in N_Entity then
4339 -- Special casing for identifiers and other entity names and operators
4341 elsif (Nkind (New_N) = N_Identifier
4342 or else Nkind (New_N) = N_Character_Literal
4343 or else Nkind (New_N) = N_Expanded_Name
4344 or else Nkind (New_N) = N_Operator_Symbol
4345 or else Nkind (New_N) in N_Op)
4347 if not Instantiating then
4349 -- Link both nodes in order to assign subsequently the
4350 -- entity of the copy to the original node, in case this
4351 -- is a global reference.
4353 Set_Associated_Node (N, New_N);
4355 -- If we are within an instantiation, this is a nested generic
4356 -- that has already been analyzed at the point of definition. We
4357 -- must preserve references that were global to the enclosing
4358 -- parent at that point. Other occurrences, whether global or
4359 -- local to the current generic, must be resolved anew, so we
4360 -- reset the entity in the generic copy. A global reference has
4361 -- a smaller depth than the parent, or else the same depth in
4362 -- case both are distinct compilation units.
4364 -- It is also possible for Current_Instantiated_Parent to be
4365 -- defined, and for this not to be a nested generic, namely
4366 -- if the unit is loaded through Rtsfind. In that case, the
4367 -- entity of New_N is only a link to the associated node, and
4368 -- not a defining occurrence.
4370 -- The entities for parent units in the defining_program_unit
4371 -- of a generic child unit are established when the context of
4372 -- the unit is first analyzed, before the generic copy is made.
4373 -- They are preserved in the copy for use in ASIS queries.
4375 Ent := Entity (New_N);
4377 if No (Current_Instantiated_Parent.Gen_Id) then
4379 or else Nkind (Ent) /= N_Defining_Identifier
4380 or else Nkind (Parent (N)) /= N_Defining_Program_Unit_Name
4382 Set_Associated_Node (New_N, Empty);
4387 not (Nkind (Ent) = N_Defining_Identifier
4389 Nkind (Ent) = N_Defining_Character_Literal
4391 Nkind (Ent) = N_Defining_Operator_Symbol)
4392 or else No (Scope (Ent))
4393 or else Scope (Ent) = Current_Instantiated_Parent.Gen_Id
4394 or else (Scope_Depth (Scope (Ent)) >
4395 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
4397 Get_Source_Unit (Ent) =
4398 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
4400 Set_Associated_Node (New_N, Empty);
4403 -- Case of instantiating identifier or some other name or operator
4406 -- If the associated node is still defined, the entity in
4407 -- it is global, and must be copied to the instance.
4409 if Present (Get_Associated_Node (N)) then
4410 if Nkind (Get_Associated_Node (N)) = Nkind (N) then
4411 Set_Entity (New_N, Entity (Get_Associated_Node (N)));
4412 Check_Private_View (N);
4414 elsif Nkind (Get_Associated_Node (N)) = N_Function_Call then
4415 Set_Entity (New_N, Entity (Name (Get_Associated_Node (N))));
4418 Set_Entity (New_N, Empty);
4423 -- For expanded name, we must copy the Prefix and Selector_Name
4425 if Nkind (N) = N_Expanded_Name then
4428 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
4430 Set_Selector_Name (New_N,
4431 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
4433 -- For operators, we must copy the right operand
4435 elsif Nkind (N) in N_Op then
4437 Set_Right_Opnd (New_N,
4438 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
4440 -- And for binary operators, the left operand as well
4442 if Nkind (N) in N_Binary_Op then
4443 Set_Left_Opnd (New_N,
4444 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
4448 -- Special casing for stubs
4450 elsif Nkind (N) in N_Body_Stub then
4452 -- In any case, we must copy the specification or defining
4453 -- identifier as appropriate.
4455 if Nkind (N) = N_Subprogram_Body_Stub then
4456 Set_Specification (New_N,
4457 Copy_Generic_Node (Specification (N), New_N, Instantiating));
4460 Set_Defining_Identifier (New_N,
4462 (Defining_Identifier (N), New_N, Instantiating));
4465 -- If we are not instantiating, then this is where we load and
4466 -- analyze subunits, i.e. at the point where the stub occurs. A
4467 -- more permissivle system might defer this analysis to the point
4468 -- of instantiation, but this seems to complicated for now.
4470 if not Instantiating then
4472 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
4474 Unum : Unit_Number_Type;
4480 (Load_Name => Subunit_Name,
4485 -- If the proper body is not found, a warning message will
4486 -- be emitted when analyzing the stub, or later at the the
4487 -- point of instantiation. Here we just leave the stub as is.
4489 if Unum = No_Unit then
4490 Subunits_Missing := True;
4491 goto Subunit_Not_Found;
4494 Subunit := Cunit (Unum);
4496 -- We must create a generic copy of the subunit, in order
4497 -- to perform semantic analysis on it, and we must replace
4498 -- the stub in the original generic unit with the subunit,
4499 -- in order to preserve non-local references within.
4501 -- Only the proper body needs to be copied. Library_Unit and
4502 -- context clause are simply inherited by the generic copy.
4503 -- Note that the copy (which may be recursive if there are
4504 -- nested subunits) must be done first, before attaching it
4505 -- to the enclosing generic.
4509 (Proper_Body (Unit (Subunit)),
4510 Empty, Instantiating => False);
4512 -- Now place the original proper body in the original
4513 -- generic unit. This is a body, not a compilation unit.
4515 Rewrite (N, Proper_Body (Unit (Subunit)));
4516 Set_Is_Compilation_Unit (Defining_Entity (N), False);
4517 Set_Was_Originally_Stub (N);
4519 -- Finally replace the body of the subunit with its copy,
4520 -- and make this new subunit into the library unit of the
4521 -- generic copy, which does not have stubs any longer.
4523 Set_Proper_Body (Unit (Subunit), New_Body);
4524 Set_Library_Unit (New_N, Subunit);
4525 Inherit_Context (Unit (Subunit), N);
4529 -- If we are instantiating, this must be an error case, since
4530 -- otherwise we would have replaced the stub node by the proper
4531 -- body that corresponds. So just ignore it in the copy (i.e.
4532 -- we have copied it, and that is good enough).
4538 <<Subunit_Not_Found>> null;
4540 -- If the node is a compilation unit, it is the subunit of a stub,
4541 -- which has been loaded already (see code below). In this case,
4542 -- the library unit field of N points to the parent unit (which
4543 -- is a compilation unit) and need not (and cannot!) be copied.
4545 -- When the proper body of the stub is analyzed, thie library_unit
4546 -- link is used to establish the proper context (see sem_ch10).
4548 -- The other fields of a compilation unit are copied as usual
4550 elsif Nkind (N) = N_Compilation_Unit then
4552 -- This code can only be executed when not instantiating, because
4553 -- in the copy made for an instantiation, the compilation unit
4554 -- node has disappeared at the point that a stub is replaced by
4557 pragma Assert (not Instantiating);
4559 Set_Context_Items (New_N,
4560 Copy_Generic_List (Context_Items (N), New_N));
4563 Copy_Generic_Node (Unit (N), New_N, False));
4565 Set_First_Inlined_Subprogram (New_N,
4567 (First_Inlined_Subprogram (N), New_N, False));
4569 Set_Aux_Decls_Node (New_N,
4570 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
4572 -- For an assignment node, the assignment is known to be semantically
4573 -- legal if we are instantiating the template. This avoids incorrect
4574 -- diagnostics in generated code.
4576 elsif Nkind (N) = N_Assignment_Statement then
4578 -- Copy name and expression fields in usual manner
4581 Copy_Generic_Node (Name (N), New_N, Instantiating));
4583 Set_Expression (New_N,
4584 Copy_Generic_Node (Expression (N), New_N, Instantiating));
4586 if Instantiating then
4587 Set_Assignment_OK (Name (New_N), True);
4590 elsif Nkind (N) = N_Aggregate
4591 or else Nkind (N) = N_Extension_Aggregate
4594 if not Instantiating then
4595 Set_Associated_Node (N, New_N);
4598 if Present (Get_Associated_Node (N))
4599 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
4601 -- In the generic the aggregate has some composite type.
4602 -- If at the point of instantiation the type has a private
4603 -- view, install the full view (and that of its ancestors,
4607 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
4612 and then Is_Private_Type (T)
4618 and then Is_Tagged_Type (T)
4619 and then Is_Derived_Type (T)
4621 Rt := Root_Type (T);
4626 if Is_Private_Type (T) then
4637 -- Do not copy the associated node, which points to
4638 -- the generic copy of the aggregate.
4641 use Atree.Unchecked_Access;
4642 -- This code section is part of the implementation of an untyped
4643 -- tree traversal, so it needs direct access to node fields.
4646 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
4647 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
4648 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
4649 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
4652 -- Allocators do not have an identifier denoting the access type,
4653 -- so we must locate it through the expression to check whether
4654 -- the views are consistent.
4656 elsif Nkind (N) = N_Allocator
4657 and then Nkind (Expression (N)) = N_Qualified_Expression
4658 and then Instantiating
4661 T : Node_Id := Get_Associated_Node (Subtype_Mark (Expression (N)));
4666 -- Retrieve the allocator node in the generic copy.
4668 Acc_T := Etype (Parent (Parent (T)));
4670 and then Is_Private_Type (Acc_T)
4672 Switch_View (Acc_T);
4679 -- For a proper body, we must catch the case of a proper body that
4680 -- replaces a stub. This represents the point at which a separate
4681 -- compilation unit, and hence template file, may be referenced, so
4682 -- we must make a new source instantiation entry for the template
4683 -- of the subunit, and ensure that all nodes in the subunit are
4684 -- adjusted using this new source instantiation entry.
4686 elsif Nkind (N) in N_Proper_Body then
4689 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
4692 if Instantiating and then Was_Originally_Stub (N) then
4693 Create_Instantiation_Source
4694 (Instantiation_Node, Defining_Entity (N), S_Adjustment);
4697 -- Now copy the fields of the proper body, using the new
4698 -- adjustment factor if one was needed as per test above.
4702 -- Restore the original adjustment factor in case changed
4704 S_Adjustment := Save_Adjustment;
4707 -- Don't copy Ident or Comment pragmas, since the comment belongs
4708 -- to the generic unit, not to the instantiating unit.
4710 elsif Nkind (N) = N_Pragma
4711 and then Instantiating
4714 Prag_Id : constant Pragma_Id := Get_Pragma_Id (Chars (N));
4717 if Prag_Id = Pragma_Ident
4718 or else Prag_Id = Pragma_Comment
4720 New_N := Make_Null_Statement (Sloc (N));
4727 -- For the remaining nodes, copy recursively their descendants.
4733 and then Nkind (N) = N_Subprogram_Body
4735 Set_Generic_Parent (Specification (New_N), N);
4740 end Copy_Generic_Node;
4742 ----------------------------
4743 -- Denotes_Formal_Package --
4744 ----------------------------
4746 function Denotes_Formal_Package (Pack : Entity_Id) return Boolean is
4747 Par : constant Entity_Id := Current_Instantiated_Parent.Act_Id;
4748 Scop : Entity_Id := Scope (Pack);
4752 if Ekind (Scop) = E_Generic_Package
4753 or else Nkind (Unit_Declaration_Node (Scop))
4754 = N_Generic_Subprogram_Declaration
4758 elsif Nkind (Parent (Pack)) = N_Formal_Package_Declaration then
4765 -- Check whether this package is associated with a formal
4766 -- package of the enclosing instantiation. Iterate over the
4767 -- list of renamings.
4769 E := First_Entity (Par);
4770 while Present (E) loop
4772 if Ekind (E) /= E_Package
4773 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
4776 elsif Renamed_Object (E) = Par then
4779 elsif Renamed_Object (E) = Pack then
4788 end Denotes_Formal_Package;
4794 procedure End_Generic is
4796 -- ??? More things could be factored out in this
4797 -- routine. Should probably be done at a later stage.
4799 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
4800 Generic_Flags.Decrement_Last;
4802 Expander_Mode_Restore;
4805 ----------------------
4806 -- Find_Actual_Type --
4807 ----------------------
4809 function Find_Actual_Type
4811 Gen_Scope : Entity_Id)
4817 if not Is_Child_Unit (Gen_Scope) then
4818 return Get_Instance_Of (Typ);
4820 elsif not Is_Generic_Type (Typ)
4821 or else Scope (Typ) = Gen_Scope
4823 return Get_Instance_Of (Typ);
4826 T := Current_Entity (Typ);
4827 while Present (T) loop
4828 if In_Open_Scopes (Scope (T)) then
4837 end Find_Actual_Type;
4839 ----------------------------
4840 -- Freeze_Subprogram_Body --
4841 ----------------------------
4843 procedure Freeze_Subprogram_Body
4844 (Inst_Node : Node_Id;
4846 Pack_Id : Entity_Id)
4849 Gen_Unit : constant Entity_Id := Entity (Name (Inst_Node));
4850 Par : constant Entity_Id := Scope (Gen_Unit);
4855 function Earlier (N1, N2 : Node_Id) return Boolean;
4856 -- Yields True if N1 and N2 appear in the same compilation unit,
4857 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
4858 -- traversal of the tree for the unit.
4860 function Enclosing_Body (N : Node_Id) return Node_Id;
4861 -- Find innermost package body that encloses the given node, and which
4862 -- is not a compilation unit. Freeze nodes for the instance, or for its
4863 -- enclosing body, may be inserted after the enclosing_body of the
4866 function Package_Freeze_Node (B : Node_Id) return Node_Id;
4867 -- Find entity for given package body, and locate or create a freeze
4870 function True_Parent (N : Node_Id) return Node_Id;
4871 -- For a subunit, return parent of corresponding stub.
4877 function Earlier (N1, N2 : Node_Id) return Boolean is
4883 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
4884 -- Find distance from given node to enclosing compilation unit.
4886 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
4889 and then Nkind (P) /= N_Compilation_Unit
4891 P := True_Parent (P);
4897 Find_Depth (P1, D1);
4898 Find_Depth (P2, D2);
4908 P1 := True_Parent (P1);
4913 P2 := True_Parent (P2);
4917 -- At this point P1 and P2 are at the same distance from the root.
4918 -- We examine their parents until we find a common declarative
4919 -- list, at which point we can establish their relative placement
4920 -- by comparing their ultimate slocs. If we reach the root,
4921 -- N1 and N2 do not descend from the same declarative list (e.g.
4922 -- one is nested in the declarative part and the other is in a block
4923 -- in the statement part) and the earlier one is already frozen.
4925 while not Is_List_Member (P1)
4926 or else not Is_List_Member (P2)
4927 or else List_Containing (P1) /= List_Containing (P2)
4929 P1 := True_Parent (P1);
4930 P2 := True_Parent (P2);
4932 if Nkind (Parent (P1)) = N_Subunit then
4933 P1 := Corresponding_Stub (Parent (P1));
4936 if Nkind (Parent (P2)) = N_Subunit then
4937 P2 := Corresponding_Stub (Parent (P2));
4946 Top_Level_Location (Sloc (P1)) < Top_Level_Location (Sloc (P2));
4949 --------------------
4950 -- Enclosing_Body --
4951 --------------------
4953 function Enclosing_Body (N : Node_Id) return Node_Id is
4954 P : Node_Id := Parent (N);
4958 and then Nkind (Parent (P)) /= N_Compilation_Unit
4960 if Nkind (P) = N_Package_Body then
4962 if Nkind (Parent (P)) = N_Subunit then
4963 return Corresponding_Stub (Parent (P));
4969 P := True_Parent (P);
4975 -------------------------
4976 -- Package_Freeze_Node --
4977 -------------------------
4979 function Package_Freeze_Node (B : Node_Id) return Node_Id is
4983 if Nkind (B) = N_Package_Body then
4984 Id := Corresponding_Spec (B);
4986 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
4987 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
4990 Ensure_Freeze_Node (Id);
4991 return Freeze_Node (Id);
4992 end Package_Freeze_Node;
4998 function True_Parent (N : Node_Id) return Node_Id is
5000 if Nkind (Parent (N)) = N_Subunit then
5001 return Parent (Corresponding_Stub (Parent (N)));
5007 -- Start of processing of Freeze_Subprogram_Body
5010 -- If the instance and the generic body appear within the same
5011 -- unit, and the instance preceeds the generic, the freeze node for
5012 -- the instance must appear after that of the generic. If the generic
5013 -- is nested within another instance I2, then current instance must
5014 -- be frozen after I2. In both cases, the freeze nodes are those of
5015 -- enclosing packages. Otherwise, the freeze node is placed at the end
5016 -- of the current declarative part.
5018 Enc_G := Enclosing_Body (Gen_Body);
5019 Enc_I := Enclosing_Body (Inst_Node);
5020 Ensure_Freeze_Node (Pack_Id);
5021 F_Node := Freeze_Node (Pack_Id);
5023 if Is_Generic_Instance (Par)
5024 and then Present (Freeze_Node (Par))
5026 In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
5028 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
5029 -- The parent was a premature instantiation. Insert freeze
5030 -- node at the end the current declarative part.
5032 Insert_After_Last_Decl (Inst_Node, F_Node);
5035 Insert_After (Freeze_Node (Par), F_Node);
5038 -- The body enclosing the instance should be frozen after the body
5039 -- that includes the generic, because the body of the instance may
5040 -- make references to entities therein. If the two are not in the
5041 -- same declarative part, or if the one enclosing the instance is
5042 -- frozen already, freeze the instance at the end of the current
5043 -- declarative part.
5045 elsif Is_Generic_Instance (Par)
5046 and then Present (Freeze_Node (Par))
5047 and then Present (Enc_I)
5049 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
5051 (Nkind (Enc_I) = N_Package_Body
5053 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
5056 -- The enclosing package may contain several instances. Rather
5057 -- than computing the earliest point at which to insert its
5058 -- freeze node, we place it at the end of the declarative part
5059 -- of the parent of the generic.
5061 Insert_After_Last_Decl
5062 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
5065 Insert_After_Last_Decl (Inst_Node, F_Node);
5067 elsif Present (Enc_G)
5068 and then Present (Enc_I)
5069 and then Enc_G /= Enc_I
5070 and then Earlier (Inst_Node, Gen_Body)
5072 if Nkind (Enc_G) = N_Package_Body then
5073 E_G_Id := Corresponding_Spec (Enc_G);
5074 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
5076 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
5079 -- Freeze package that encloses instance, and place node after
5080 -- package that encloses generic. If enclosing package is already
5081 -- frozen we have to assume it is at the proper place. This may
5082 -- be a potential ABE that requires dynamic checking.
5084 Insert_After_Last_Decl (Enc_G, Package_Freeze_Node (Enc_I));
5086 -- Freeze enclosing subunit before instance
5088 Ensure_Freeze_Node (E_G_Id);
5090 if not Is_List_Member (Freeze_Node (E_G_Id)) then
5091 Insert_After (Enc_G, Freeze_Node (E_G_Id));
5094 Insert_After_Last_Decl (Inst_Node, F_Node);
5098 -- If none of the above, insert freeze node at the end of the
5099 -- current declarative part.
5101 Insert_After_Last_Decl (Inst_Node, F_Node);
5103 end Freeze_Subprogram_Body;
5109 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
5111 return Generic_Renamings.Table (E).Gen_Id;
5114 ---------------------
5115 -- Get_Instance_Of --
5116 ---------------------
5118 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
5119 Res : Assoc_Ptr := Generic_Renamings_HTable.Get (A);
5121 if Res /= Assoc_Null then
5122 return Generic_Renamings.Table (Res).Act_Id;
5124 -- On exit, entity is not instantiated: not a generic parameter,
5125 -- or else parameter of an inner generic unit.
5129 end Get_Instance_Of;
5131 ------------------------------------
5132 -- Get_Package_Instantiation_Node --
5133 ------------------------------------
5135 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
5136 Decl : Node_Id := Unit_Declaration_Node (A);
5140 -- If the instantiation is a compilation unit that does not need a
5141 -- body then the instantiation node has been rewritten as a package
5142 -- declaration for the instance, and we return the original node.
5143 -- If it is a compilation unit and the instance node has not been
5144 -- rewritten, then it is still the unit of the compilation.
5145 -- Otherwise the instantiation node appears after the declaration.
5146 -- If the entity is a formal package, the declaration may have been
5147 -- rewritten as a generic declaration (in the case of a formal with a
5148 -- box) or left as a formal package declaration if it has actuals, and
5149 -- is found with a forward search.
5151 if Nkind (Parent (Decl)) = N_Compilation_Unit then
5152 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
5153 return Original_Node (Decl);
5155 return Unit (Parent (Decl));
5158 elsif Nkind (Decl) = N_Generic_Package_Declaration
5159 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
5161 return Original_Node (Decl);
5164 Inst := Next (Decl);
5165 while Nkind (Inst) /= N_Package_Instantiation
5166 and then Nkind (Inst) /= N_Formal_Package_Declaration
5173 end Get_Package_Instantiation_Node;
5175 ------------------------
5176 -- Has_Been_Exchanged --
5177 ------------------------
5179 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
5180 Next : Elmt_Id := First_Elmt (Exchanged_Views);
5183 while Present (Next) loop
5184 if Full_View (Node (Next)) = E then
5192 end Has_Been_Exchanged;
5198 function Hash (F : Entity_Id) return HTable_Range is
5200 return HTable_Range (F mod HTable_Size);
5203 ------------------------
5204 -- Hide_Current_Scope --
5205 ------------------------
5207 procedure Hide_Current_Scope is
5208 C : constant Entity_Id := Current_Scope;
5212 Set_Is_Hidden_Open_Scope (C);
5213 E := First_Entity (C);
5215 while Present (E) loop
5216 if Is_Immediately_Visible (E) then
5217 Set_Is_Immediately_Visible (E, False);
5218 Append_Elmt (E, Hidden_Entities);
5224 -- Make the scope name invisible as well. This is necessary, but
5225 -- might conflict with calls to Rtsfind later on, in case the scope
5226 -- is a predefined one. There is no clean solution to this problem, so
5227 -- for now we depend on the user not redefining Standard itself in one
5228 -- of the parent units.
5230 if Is_Immediately_Visible (C)
5231 and then C /= Standard_Standard
5233 Set_Is_Immediately_Visible (C, False);
5234 Append_Elmt (C, Hidden_Entities);
5237 end Hide_Current_Scope;
5239 ------------------------------
5240 -- In_Same_Declarative_Part --
5241 ------------------------------
5243 function In_Same_Declarative_Part
5248 Decls : Node_Id := Parent (F_Node);
5249 Nod : Node_Id := Parent (Inst);
5252 while Present (Nod) loop
5256 elsif Nkind (Nod) = N_Subprogram_Body
5257 or else Nkind (Nod) = N_Package_Body
5258 or else Nkind (Nod) = N_Task_Body
5259 or else Nkind (Nod) = N_Protected_Body
5260 or else Nkind (Nod) = N_Block_Statement
5264 elsif Nkind (Nod) = N_Subunit then
5265 Nod := Corresponding_Stub (Nod);
5267 elsif Nkind (Nod) = N_Compilation_Unit then
5270 Nod := Parent (Nod);
5275 end In_Same_Declarative_Part;
5277 ---------------------
5278 -- Inherit_Context --
5279 ---------------------
5281 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
5282 Current_Context : List_Id;
5283 Current_Unit : Node_Id;
5288 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
5290 -- The inherited context is attached to the enclosing compilation
5291 -- unit. This is either the main unit, or the declaration for the
5292 -- main unit (in case the instantation appears within the package
5293 -- declaration and the main unit is its body).
5295 Current_Unit := Parent (Inst);
5296 while Present (Current_Unit)
5297 and then Nkind (Current_Unit) /= N_Compilation_Unit
5299 Current_Unit := Parent (Current_Unit);
5302 Current_Context := Context_Items (Current_Unit);
5304 Item := First (Context_Items (Parent (Gen_Decl)));
5305 while Present (Item) loop
5306 if Nkind (Item) = N_With_Clause then
5307 New_I := New_Copy (Item);
5308 Set_Implicit_With (New_I, True);
5309 Append (New_I, Current_Context);
5315 end Inherit_Context;
5317 ----------------------------
5318 -- Insert_After_Last_Decl --
5319 ----------------------------
5321 procedure Insert_After_Last_Decl (N : Node_Id; F_Node : Node_Id) is
5322 L : List_Id := List_Containing (N);
5323 P : Node_Id := Parent (L);
5326 if not Is_List_Member (F_Node) then
5327 if Nkind (P) = N_Package_Specification
5328 and then L = Visible_Declarations (P)
5329 and then Present (Private_Declarations (P))
5330 and then not Is_Empty_List (Private_Declarations (P))
5332 L := Private_Declarations (P);
5335 Insert_After (Last (L), F_Node);
5337 end Insert_After_Last_Decl;
5343 procedure Install_Body
5344 (Act_Body : Node_Id;
5349 Act_Id : Entity_Id := Corresponding_Spec (Act_Body);
5350 Act_Unit : constant Node_Id :=
5351 Unit (Cunit (Get_Source_Unit (N)));
5353 Gen_Id : Entity_Id := Corresponding_Spec (Gen_Body);
5354 Gen_Unit : constant Node_Id :=
5355 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
5356 Orig_Body : Node_Id := Gen_Body;
5357 Par : constant Entity_Id := Scope (Gen_Id);
5358 Body_Unit : Node_Id;
5360 Must_Delay : Boolean;
5362 function Enclosing_Subp (Id : Entity_Id) return Entity_Id;
5363 -- Find subprogram (if any) that encloses instance and/or generic body.
5365 function True_Sloc (N : Node_Id) return Source_Ptr;
5366 -- If the instance is nested inside a generic unit, the Sloc of the
5367 -- instance indicates the place of the original definition, not the
5368 -- point of the current enclosing instance. Pending a better usage of
5369 -- Slocs to indicate instantiation places, we determine the place of
5370 -- origin of a node by finding the maximum sloc of any ancestor node.
5371 -- Why is this not equivalent fo Top_Level_Location ???
5373 function Enclosing_Subp (Id : Entity_Id) return Entity_Id is
5374 Scop : Entity_Id := Scope (Id);
5377 while Scop /= Standard_Standard
5378 and then not Is_Overloadable (Scop)
5380 Scop := Scope (Scop);
5386 function True_Sloc (N : Node_Id) return Source_Ptr is
5393 while Present (N1) and then N1 /= Act_Unit loop
5394 if Sloc (N1) > Res then
5404 -- Start of processing for Install_Body
5407 -- If the body is a subunit, the freeze point is the corresponding
5408 -- stub in the current compilation, not the subunit itself.
5410 if Nkind (Parent (Gen_Body)) = N_Subunit then
5411 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
5413 Orig_Body := Gen_Body;
5416 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
5418 -- If the instantiation and the generic definition appear in the
5419 -- same package declaration, this is an early instantiation.
5420 -- If they appear in the same declarative part, it is an early
5421 -- instantiation only if the generic body appears textually later,
5422 -- and the generic body is also in the main unit.
5424 -- If instance is nested within a subprogram, and the generic body is
5425 -- not, the instance is delayed because the enclosing body is. If
5426 -- instance and body are within the same scope, or the same sub-
5427 -- program body, indicate explicitly that the instance is delayed.
5430 (Gen_Unit = Act_Unit
5431 and then ((Nkind (Gen_Unit) = N_Package_Declaration)
5432 or else Nkind (Gen_Unit) = N_Generic_Package_Declaration
5433 or else (Gen_Unit = Body_Unit
5434 and then True_Sloc (N) < Sloc (Orig_Body)))
5435 and then Is_In_Main_Unit (Gen_Unit)
5436 and then (Scope (Act_Id) = Scope (Gen_Id)
5438 Enclosing_Subp (Act_Id) = Enclosing_Subp (Gen_Id)));
5440 -- If this is an early instantiation, the freeze node is placed after
5441 -- the generic body. Otherwise, if the generic appears in an instance,
5442 -- we cannot freeze the current instance until the outer one is frozen.
5443 -- This is only relevant if the current instance is nested within some
5444 -- inner scope not itself within the outer instance. If this scope is
5445 -- a package body in the same declarative part as the outer instance,
5446 -- then that body needs to be frozen after the outer instance. Finally,
5447 -- if no delay is needed, we place the freeze node at the end of the
5448 -- current declarative part.
5450 if Expander_Active then
5451 Ensure_Freeze_Node (Act_Id);
5452 F_Node := Freeze_Node (Act_Id);
5455 Insert_After (Orig_Body, F_Node);
5457 elsif Is_Generic_Instance (Par)
5458 and then Present (Freeze_Node (Par))
5459 and then Scope (Act_Id) /= Par
5461 -- Freeze instance of inner generic after instance of enclosing
5464 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
5465 Insert_After (Freeze_Node (Par), F_Node);
5467 -- Freeze package enclosing instance of inner generic after
5468 -- instance of enclosing generic.
5470 elsif Nkind (Parent (N)) = N_Package_Body
5471 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
5475 Enclosing : Entity_Id := Corresponding_Spec (Parent (N));
5478 Insert_After_Last_Decl (N, F_Node);
5479 Ensure_Freeze_Node (Enclosing);
5481 if not Is_List_Member (Freeze_Node (Enclosing)) then
5482 Insert_After (Freeze_Node (Par), Freeze_Node (Enclosing));
5487 Insert_After_Last_Decl (N, F_Node);
5491 Insert_After_Last_Decl (N, F_Node);
5495 Set_Is_Frozen (Act_Id);
5496 Insert_Before (N, Act_Body);
5497 Mark_Rewrite_Insertion (Act_Body);
5500 --------------------
5501 -- Install_Parent --
5502 --------------------
5504 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
5505 S : Entity_Id := Current_Scope;
5506 Inst_Par : Entity_Id;
5507 First_Par : Entity_Id;
5508 Inst_Node : Node_Id;
5509 Gen_Par : Entity_Id;
5510 First_Gen : Entity_Id;
5511 Ancestors : Elist_Id := New_Elmt_List;
5514 procedure Install_Formal_Packages (Par : Entity_Id);
5515 -- If any of the formals of the parent are formal packages with box,
5516 -- their formal parts are visible in the parent and thus in the child
5517 -- unit as well. Analogous to what is done in Check_Generic_Actuals
5518 -- for the unit itself.
5520 procedure Install_Noninstance_Specs (Par : Entity_Id);
5521 -- Install the scopes of noninstance parent units ending with Par.
5523 procedure Install_Spec (Par : Entity_Id);
5524 -- The child unit is within the declarative part of the parent, so
5525 -- the declarations within the parent are immediately visible.
5527 -----------------------------
5528 -- Install_Formal_Packages --
5529 -----------------------------
5531 procedure Install_Formal_Packages (Par : Entity_Id) is
5535 E := First_Entity (Par);
5537 while Present (E) loop
5539 if Ekind (E) = E_Package
5540 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
5542 -- If this is the renaming for the parent instance, done.
5544 if Renamed_Object (E) = Par then
5547 -- The visibility of a formal of an enclosing generic is
5550 elsif Denotes_Formal_Package (E) then
5553 elsif Present (Associated_Formal_Package (E))
5554 and then Box_Present (Parent (Associated_Formal_Package (E)))
5556 Check_Generic_Actuals (Renamed_Object (E), True);
5557 Set_Is_Hidden (E, False);
5563 end Install_Formal_Packages;
5565 -------------------------------
5566 -- Install_Noninstance_Specs --
5567 -------------------------------
5569 procedure Install_Noninstance_Specs (Par : Entity_Id) is
5572 and then Par /= Standard_Standard
5573 and then not In_Open_Scopes (Par)
5575 Install_Noninstance_Specs (Scope (Par));
5578 end Install_Noninstance_Specs;
5584 procedure Install_Spec (Par : Entity_Id) is
5585 Spec : constant Node_Id :=
5586 Specification (Unit_Declaration_Node (Par));
5590 Set_Is_Immediately_Visible (Par);
5591 Install_Visible_Declarations (Par);
5592 Install_Private_Declarations (Par);
5593 Set_Use (Visible_Declarations (Spec));
5594 Set_Use (Private_Declarations (Spec));
5597 -- Start of processing for Install_Parent
5600 -- We need to install the parent instance to compile the instantiation
5601 -- of the child, but the child instance must appear in the current
5602 -- scope. Given that we cannot place the parent above the current
5603 -- scope in the scope stack, we duplicate the current scope and unstack
5604 -- both after the instantiation is complete.
5606 -- If the parent is itself the instantiation of a child unit, we must
5607 -- also stack the instantiation of its parent, and so on. Each such
5608 -- ancestor is the prefix of the name in a prior instantiation.
5610 -- If this is a nested instance, the parent unit itself resolves to
5611 -- a renaming of the parent instance, whose declaration we need.
5613 -- Finally, the parent may be a generic (not an instance) when the
5614 -- child unit appears as a formal package.
5618 if Present (Renamed_Entity (Inst_Par)) then
5619 Inst_Par := Renamed_Entity (Inst_Par);
5622 First_Par := Inst_Par;
5625 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
5627 First_Gen := Gen_Par;
5629 while Present (Gen_Par)
5630 and then Is_Child_Unit (Gen_Par)
5632 -- Load grandparent instance as well.
5634 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
5636 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
5637 Inst_Par := Entity (Prefix (Name (Inst_Node)));
5639 if Present (Renamed_Entity (Inst_Par)) then
5640 Inst_Par := Renamed_Entity (Inst_Par);
5645 (Specification (Unit_Declaration_Node (Inst_Par)));
5647 if Present (Gen_Par) then
5648 Prepend_Elmt (Inst_Par, Ancestors);
5651 -- Parent is not the name of an instantiation.
5653 Install_Noninstance_Specs (Inst_Par);
5665 if Present (First_Gen) then
5666 Append_Elmt (First_Par, Ancestors);
5669 Install_Noninstance_Specs (First_Par);
5672 if not Is_Empty_Elmt_List (Ancestors) then
5673 Elmt := First_Elmt (Ancestors);
5675 while Present (Elmt) loop
5676 Install_Spec (Node (Elmt));
5677 Install_Formal_Packages (Node (Elmt));
5688 --------------------------------
5689 -- Instantiate_Formal_Package --
5690 --------------------------------
5692 function Instantiate_Formal_Package
5695 Analyzed_Formal : Node_Id)
5698 Loc : constant Source_Ptr := Sloc (Actual);
5699 Actual_Pack : Entity_Id;
5700 Formal_Pack : Entity_Id;
5701 Gen_Parent : Entity_Id;
5704 Parent_Spec : Node_Id;
5706 function Formal_Entity
5708 Act_Ent : Entity_Id)
5710 -- Returns the entity associated with the given formal F. In the
5711 -- case where F is a formal package, this function will iterate
5712 -- through all of F's formals and enter map associations from the
5713 -- actuals occurring in the formal package's corresponding actual
5714 -- package (obtained via Act_Ent) to the formal package's formal
5715 -- parameters. This function is called recursively for arbitrary
5716 -- levels of formal packages.
5718 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id);
5719 -- Within the generic part, entities in the formal package are
5720 -- visible. To validate subsequent type declarations, indicate
5721 -- the correspondence betwen the entities in the analyzed formal,
5722 -- and the entities in the actual package. There are three packages
5723 -- involved in the instantiation of a formal package: the parent
5724 -- generic P1 which appears in the generic declaration, the fake
5725 -- instantiation P2 which appears in the analyzed generic, and whose
5726 -- visible entities may be used in subsequent formals, and the actual
5727 -- P3 in the instance. To validate subsequent formals, me indicate
5728 -- that the entities in P2 are mapped into those of P3. The mapping of
5729 -- entities has to be done recursively for nested packages.
5735 function Formal_Entity
5737 Act_Ent : Entity_Id)
5740 Orig_Node : Node_Id := F;
5744 when N_Formal_Object_Declaration =>
5745 return Defining_Identifier (F);
5747 when N_Formal_Type_Declaration =>
5748 return Defining_Identifier (F);
5750 when N_Formal_Subprogram_Declaration =>
5751 return Defining_Unit_Name (Specification (F));
5753 when N_Formal_Package_Declaration |
5754 N_Generic_Package_Declaration =>
5756 if Nkind (F) = N_Generic_Package_Declaration then
5757 Orig_Node := Original_Node (F);
5761 Actual_Ent : Entity_Id := First_Entity (Act_Ent);
5762 Formal_Node : Node_Id;
5763 Formal_Ent : Entity_Id;
5765 Gen_Decl : Node_Id :=
5766 Unit_Declaration_Node
5767 (Entity (Name (Orig_Node)));
5768 Formals : List_Id :=
5769 Generic_Formal_Declarations (Gen_Decl);
5772 if Present (Formals) then
5773 Formal_Node := First_Non_Pragma (Formals);
5775 Formal_Node := Empty;
5778 -- As for the loop further below, this loop is making
5779 -- a probably invalid assumption about the correspondence
5780 -- between formals and actuals and eventually needs to
5781 -- corrected to account for cases where the formals are
5782 -- not synchronized and in one-to-one correspondence
5783 -- with actuals. ???
5785 -- What is certain is that for a legal program the
5786 -- presence of actual entities guarantees the existing
5789 while Present (Actual_Ent)
5790 and then Present (Formal_Node)
5791 and then Actual_Ent /= First_Private_Entity (Act_Ent)
5793 -- ??? Are the following calls also needed here:
5795 -- Set_Is_Hidden (Actual_Ent, False);
5796 -- Set_Is_Potentially_Use_Visible
5797 -- (Actual_Ent, In_Use (Act_Ent));
5799 Formal_Ent := Formal_Entity (Formal_Node, Actual_Ent);
5800 if Present (Formal_Ent) then
5801 Set_Instance_Of (Formal_Ent, Actual_Ent);
5803 Next_Non_Pragma (Formal_Node);
5805 Next_Entity (Actual_Ent);
5809 return Defining_Identifier (Orig_Node);
5811 when N_Use_Package_Clause =>
5814 when N_Use_Type_Clause =>
5817 -- We return Empty for all other encountered forms of
5818 -- declarations because there are some cases of nonformal
5819 -- sorts of declaration that can show up (e.g., when array
5820 -- formals are present). Since it's not clear what kinds
5821 -- can appear among the formals, we won't raise failure here.
5833 procedure Map_Entities (Form : Entity_Id; Act : Entity_Id) is
5838 Set_Instance_Of (Form, Act);
5840 E1 := First_Entity (Form);
5841 E2 := First_Entity (Act);
5843 and then E1 /= First_Private_Entity (Form)
5845 if not Is_Internal (E1)
5846 and then not Is_Class_Wide_Type (E1)
5850 and then Chars (E2) /= Chars (E1)
5858 Set_Instance_Of (E1, E2);
5861 and then Is_Tagged_Type (E2)
5864 (Class_Wide_Type (E1), Class_Wide_Type (E2));
5867 if Ekind (E1) = E_Package
5868 and then No (Renamed_Object (E1))
5870 Map_Entities (E1, E2);
5879 -- Start of processing for Instantiate_Formal_Package
5884 if not Is_Entity_Name (Actual)
5885 or else Ekind (Entity (Actual)) /= E_Package
5888 ("expect package instance to instantiate formal", Actual);
5889 Abandon_Instantiation (Actual);
5890 raise Program_Error;
5893 Actual_Pack := Entity (Actual);
5894 Set_Is_Instantiated (Actual_Pack);
5896 -- The actual may be a renamed package, or an outer generic
5897 -- formal package whose instantiation is converted into a renaming.
5899 if Present (Renamed_Object (Actual_Pack)) then
5900 Actual_Pack := Renamed_Object (Actual_Pack);
5903 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
5904 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
5905 Formal_Pack := Defining_Identifier (Analyzed_Formal);
5908 Generic_Parent (Specification (Analyzed_Formal));
5910 Defining_Unit_Name (Specification (Analyzed_Formal));
5913 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
5914 Parent_Spec := Specification (Unit_Declaration_Node (Actual_Pack));
5916 Parent_Spec := Parent (Actual_Pack);
5919 if Gen_Parent = Any_Id then
5921 ("previous error in declaration of formal package", Actual);
5922 Abandon_Instantiation (Actual);
5925 Generic_Parent (Parent_Spec) /= Get_Instance_Of (Gen_Parent)
5928 ("actual parameter must be instance of&", Actual, Gen_Parent);
5929 Abandon_Instantiation (Actual);
5932 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
5933 Map_Entities (Formal_Pack, Actual_Pack);
5936 Make_Package_Renaming_Declaration (Loc,
5937 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
5938 Name => New_Reference_To (Actual_Pack, Loc));
5940 Set_Associated_Formal_Package (Defining_Unit_Name (Nod),
5941 Defining_Identifier (Formal));
5942 Decls := New_List (Nod);
5944 -- If the formal F has a box, then the generic declarations are
5945 -- visible in the generic G. In an instance of G, the corresponding
5946 -- entities in the actual for F (which are the actuals for the
5947 -- instantiation of the generic that F denotes) must also be made
5948 -- visible for analysis of the current instance. On exit from the
5949 -- current instance, those entities are made private again. If the
5950 -- actual is currently in use, these entities are also use-visible.
5952 -- The loop through the actual entities also steps through the
5953 -- formal entities and enters associations from formals to
5954 -- actuals into the renaming map. This is necessary to properly
5955 -- handle checking of actual parameter associations for later
5956 -- formals that depend on actuals declared in the formal package.
5958 -- This processing needs to be reviewed at some point because
5959 -- it is probably not entirely correct as written. For example
5960 -- there may not be a strict one-to-one correspondence between
5961 -- actuals and formals and this loop is currently assuming that
5964 if Box_Present (Formal) then
5966 Actual_Ent : Entity_Id := First_Entity (Actual_Pack);
5967 Formal_Node : Node_Id := Empty;
5968 Formal_Ent : Entity_Id;
5969 Gen_Decl : Node_Id := Unit_Declaration_Node (Gen_Parent);
5970 Formals : List_Id := Generic_Formal_Declarations (Gen_Decl);
5973 if Present (Formals) then
5974 Formal_Node := First_Non_Pragma (Formals);
5977 while Present (Actual_Ent)
5978 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
5980 Set_Is_Hidden (Actual_Ent, False);
5981 Set_Is_Potentially_Use_Visible
5982 (Actual_Ent, In_Use (Actual_Pack));
5984 if Present (Formal_Node) then
5985 Formal_Ent := Formal_Entity (Formal_Node, Actual_Ent);
5987 if Present (Formal_Ent) then
5988 Set_Instance_Of (Formal_Ent, Actual_Ent);
5991 Next_Non_Pragma (Formal_Node);
5994 Next_Entity (Actual_Ent);
5998 -- If the formal is not declared with a box, reanalyze it as
5999 -- an instantiation, to verify the matching rules of 12.7. The
6000 -- actual checks are performed after the generic associations
6005 I_Pack : constant Entity_Id :=
6006 Make_Defining_Identifier (Sloc (Actual),
6007 Chars => New_Internal_Name ('P'));
6010 Set_Is_Internal (I_Pack);
6013 Make_Package_Instantiation (Sloc (Actual),
6014 Defining_Unit_Name => I_Pack,
6015 Name => New_Occurrence_Of (Gen_Parent, Sloc (Actual)),
6016 Generic_Associations =>
6017 Generic_Associations (Formal)));
6024 end Instantiate_Formal_Package;
6026 -----------------------------------
6027 -- Instantiate_Formal_Subprogram --
6028 -----------------------------------
6030 function Instantiate_Formal_Subprogram
6033 Analyzed_Formal : Node_Id)
6036 Loc : Source_Ptr := Sloc (Instantiation_Node);
6037 Formal_Sub : constant Entity_Id :=
6038 Defining_Unit_Name (Specification (Formal));
6039 Analyzed_S : constant Entity_Id :=
6040 Defining_Unit_Name (Specification (Analyzed_Formal));
6041 Decl_Node : Node_Id;
6045 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
6046 -- If the generic is a child unit, the parent has been installed
6047 -- on the scope stack, but a default subprogram cannot resolve to
6048 -- something on the parent because that parent is not really part
6049 -- of the visible context (it is there to resolve explicit local
6050 -- entities). If the default has resolved in this way, we remove
6051 -- the entity from immediate visibility and analyze the node again
6052 -- to emit an error message or find another visible candidate.
6054 procedure Valid_Actual_Subprogram (Act : Node_Id);
6055 -- Perform legality check and raise exception on failure.
6057 -----------------------
6058 -- From_Parent_Scope --
6059 -----------------------
6061 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
6062 Gen_Scope : Node_Id := Scope (Analyzed_S);
6065 while Present (Gen_Scope)
6066 and then Is_Child_Unit (Gen_Scope)
6068 if Scope (Subp) = Scope (Gen_Scope) then
6072 Gen_Scope := Scope (Gen_Scope);
6076 end From_Parent_Scope;
6078 -----------------------------
6079 -- Valid_Actual_Subprogram --
6080 -----------------------------
6082 procedure Valid_Actual_Subprogram (Act : Node_Id) is
6084 if not Is_Entity_Name (Act)
6085 and then Nkind (Act) /= N_Operator_Symbol
6086 and then Nkind (Act) /= N_Attribute_Reference
6087 and then Nkind (Act) /= N_Selected_Component
6088 and then Nkind (Act) /= N_Indexed_Component
6089 and then Nkind (Act) /= N_Character_Literal
6090 and then Nkind (Act) /= N_Explicit_Dereference
6092 if Etype (Act) /= Any_Type then
6094 ("Expect subprogram name to instantiate &",
6095 Instantiation_Node, Formal_Sub);
6098 -- In any case, instantiation cannot continue.
6100 Abandon_Instantiation (Instantiation_Node);
6102 end Valid_Actual_Subprogram;
6104 -- Start of processing for Instantiate_Formal_Subprogram
6107 New_Spec := New_Copy_Tree (Specification (Formal));
6109 -- Create new entity for the actual (New_Copy_Tree does not).
6111 Set_Defining_Unit_Name
6112 (New_Spec, Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
6114 -- Find entity of actual. If the actual is an attribute reference, it
6115 -- cannot be resolved here (its formal is missing) but is handled
6116 -- instead in Attribute_Renaming. If the actual is overloaded, it is
6117 -- fully resolved subsequently, when the renaming declaration for the
6118 -- formal is analyzed. If it is an explicit dereference, resolve the
6119 -- prefix but not the actual itself, to prevent interpretation as a
6122 if Present (Actual) then
6123 Loc := Sloc (Actual);
6124 Set_Sloc (New_Spec, Loc);
6126 if Nkind (Actual) = N_Operator_Symbol then
6127 Find_Direct_Name (Actual);
6129 elsif Nkind (Actual) = N_Explicit_Dereference then
6130 Analyze (Prefix (Actual));
6132 elsif Nkind (Actual) /= N_Attribute_Reference then
6136 Valid_Actual_Subprogram (Actual);
6139 elsif Present (Default_Name (Formal)) then
6141 if Nkind (Default_Name (Formal)) /= N_Attribute_Reference
6142 and then Nkind (Default_Name (Formal)) /= N_Selected_Component
6143 and then Nkind (Default_Name (Formal)) /= N_Indexed_Component
6144 and then Nkind (Default_Name (Formal)) /= N_Character_Literal
6145 and then Present (Entity (Default_Name (Formal)))
6147 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
6149 Nam := New_Copy (Default_Name (Formal));
6150 Set_Sloc (Nam, Loc);
6153 elsif Box_Present (Formal) then
6155 -- Actual is resolved at the point of instantiation. Create
6156 -- an identifier or operator with the same name as the formal.
6158 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
6159 Nam := Make_Operator_Symbol (Loc,
6160 Chars => Chars (Formal_Sub),
6161 Strval => No_String);
6163 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
6168 ("missing actual for instantiation of &",
6169 Instantiation_Node, Formal_Sub);
6170 Abandon_Instantiation (Instantiation_Node);
6174 Make_Subprogram_Renaming_Declaration (Loc,
6175 Specification => New_Spec,
6178 -- Gather possible interpretations for the actual before analyzing the
6179 -- instance. If overloaded, it will be resolved when analyzing the
6180 -- renaming declaration.
6182 if Box_Present (Formal)
6183 and then No (Actual)
6187 if Is_Child_Unit (Scope (Analyzed_S))
6188 and then Present (Entity (Nam))
6190 if not Is_Overloaded (Nam) then
6192 if From_Parent_Scope (Entity (Nam)) then
6193 Set_Is_Immediately_Visible (Entity (Nam), False);
6194 Set_Entity (Nam, Empty);
6195 Set_Etype (Nam, Empty);
6199 Set_Is_Immediately_Visible (Entity (Nam));
6208 Get_First_Interp (Nam, I, It);
6210 while Present (It.Nam) loop
6211 if From_Parent_Scope (It.Nam) then
6215 Get_Next_Interp (I, It);
6222 -- The generic instantiation freezes the actual. This can only be
6223 -- done once the actual is resolved, in the analysis of the renaming
6224 -- declaration. To indicate that must be done, we set the corresponding
6225 -- spec of the node to point to the formal subprogram declaration.
6227 Set_Corresponding_Spec (Decl_Node, Analyzed_Formal);
6229 -- We cannot analyze the renaming declaration, and thus find the
6230 -- actual, until the all the actuals are assembled in the instance.
6231 -- For subsequent checks of other actuals, indicate the node that
6232 -- will hold the instance of this formal.
6234 Set_Instance_Of (Analyzed_S, Nam);
6236 if Nkind (Actual) = N_Selected_Component
6237 and then Is_Task_Type (Etype (Prefix (Actual)))
6238 and then not Is_Frozen (Etype (Prefix (Actual)))
6240 -- The renaming declaration will create a body, which must appear
6241 -- outside of the instantiation, We move the renaming declaration
6242 -- out of the instance, and create an additional renaming inside,
6243 -- to prevent freezing anomalies.
6246 Anon_Id : constant Entity_Id :=
6247 Make_Defining_Identifier
6248 (Loc, New_Internal_Name ('E'));
6250 Set_Defining_Unit_Name (New_Spec, Anon_Id);
6251 Insert_Before (Instantiation_Node, Decl_Node);
6252 Analyze (Decl_Node);
6254 -- Now create renaming within the instance.
6257 Make_Subprogram_Renaming_Declaration (Loc,
6258 Specification => New_Copy_Tree (New_Spec),
6259 Name => New_Occurrence_Of (Anon_Id, Loc));
6261 Set_Defining_Unit_Name (Specification (Decl_Node),
6262 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
6267 end Instantiate_Formal_Subprogram;
6269 ------------------------
6270 -- Instantiate_Object --
6271 ------------------------
6273 function Instantiate_Object
6276 Analyzed_Formal : Node_Id)
6279 Formal_Id : constant Entity_Id := Defining_Identifier (Formal);
6280 Type_Id : constant Node_Id := Subtype_Mark (Formal);
6281 Loc : constant Source_Ptr := Sloc (Actual);
6282 Act_Assoc : constant Node_Id := Parent (Actual);
6283 Orig_Ftyp : constant Entity_Id :=
6284 Etype (Defining_Identifier (Analyzed_Formal));
6286 Decl_Node : Node_Id;
6287 Subt_Decl : Node_Id := Empty;
6288 List : List_Id := New_List;
6291 if Get_Instance_Of (Formal_Id) /= Formal_Id then
6292 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
6295 Set_Parent (List, Parent (Actual));
6299 if Out_Present (Formal) then
6301 -- An IN OUT generic actual must be a name. The instantiation is
6302 -- a renaming declaration. The actual is the name being renamed.
6303 -- We use the actual directly, rather than a copy, because it is not
6304 -- used further in the list of actuals, and because a copy or a use
6305 -- of relocate_node is incorrect if the instance is nested within
6306 -- a generic. In order to simplify ASIS searches, the Generic_Parent
6307 -- field links the declaration to the generic association.
6311 ("missing actual for instantiation of &",
6312 Instantiation_Node, Formal_Id);
6313 Abandon_Instantiation (Instantiation_Node);
6317 Make_Object_Renaming_Declaration (Loc,
6318 Defining_Identifier => New_Copy (Formal_Id),
6319 Subtype_Mark => New_Copy_Tree (Type_Id),
6322 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
6324 -- The analysis of the actual may produce insert_action nodes, so
6325 -- the declaration must have a context in which to attach them.
6327 Append (Decl_Node, List);
6330 -- This check is performed here because Analyze_Object_Renaming
6331 -- will not check it when Comes_From_Source is False. Note
6332 -- though that the check for the actual being the name of an
6333 -- object will be performed in Analyze_Object_Renaming.
6335 if Is_Object_Reference (Actual)
6336 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
6339 ("illegal discriminant-dependent component for in out parameter",
6343 -- The actual has to be resolved in order to check that it is
6344 -- a variable (due to cases such as F(1), where F returns
6345 -- access to an array, and for overloaded prefixes).
6348 Get_Instance_Of (Etype (Defining_Identifier (Analyzed_Formal)));
6350 if Is_Private_Type (Ftyp)
6351 and then not Is_Private_Type (Etype (Actual))
6352 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
6353 or else Base_Type (Etype (Actual)) = Ftyp)
6355 -- If the actual has the type of the full view of the formal,
6356 -- or else a non-private subtype of the formal, then
6357 -- the visibility of the formal type has changed. Add to the
6358 -- actuals a subtype declaration that will force the exchange
6359 -- of views in the body of the instance as well.
6362 Make_Subtype_Declaration (Loc,
6363 Defining_Identifier =>
6364 Make_Defining_Identifier (Loc, New_Internal_Name ('P')),
6365 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
6367 Prepend (Subt_Decl, List);
6369 Append_Elmt (Full_View (Ftyp), Exchanged_Views);
6370 Exchange_Declarations (Ftyp);
6373 Resolve (Actual, Ftyp);
6375 if not Is_Variable (Actual) or else Paren_Count (Actual) > 0 then
6377 ("actual for& must be a variable", Actual, Formal_Id);
6379 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
6381 "type of actual does not match type of&", Actual, Formal_Id);
6385 Note_Possible_Modification (Actual);
6387 -- Check for instantiation of atomic/volatile actual for
6388 -- non-atomic/volatile formal (RM C.6 (12)).
6390 if Is_Atomic_Object (Actual)
6391 and then not Is_Atomic (Orig_Ftyp)
6394 ("cannot instantiate non-atomic formal object " &
6395 "with atomic actual", Actual);
6397 elsif Is_Volatile_Object (Actual)
6398 and then not Is_Volatile (Orig_Ftyp)
6401 ("cannot instantiate non-volatile formal object " &
6402 "with volatile actual", Actual);
6408 -- The instantiation of a generic formal in-parameter
6409 -- is a constant declaration. The actual is the expression for
6410 -- that declaration.
6412 if Present (Actual) then
6414 Decl_Node := Make_Object_Declaration (Loc,
6415 Defining_Identifier => New_Copy (Formal_Id),
6416 Constant_Present => True,
6417 Object_Definition => New_Copy_Tree (Type_Id),
6418 Expression => Actual);
6420 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
6422 -- A generic formal object of a tagged type is defined
6423 -- to be aliased so the new constant must also be treated
6427 (Etype (Defining_Identifier (Analyzed_Formal)))
6429 Set_Aliased_Present (Decl_Node);
6432 Append (Decl_Node, List);
6438 (Etype (Defining_Identifier (Analyzed_Formal)));
6440 Freeze_Before (Instantiation_Node, Typ);
6442 -- If the actual is an aggregate, perform name resolution
6443 -- on its components (the analysis of an aggregate does not
6444 -- do it) to capture local names that may be hidden if the
6445 -- generic is a child unit.
6447 if Nkind (Actual) = N_Aggregate then
6448 Pre_Analyze_And_Resolve (Actual, Typ);
6452 elsif Present (Expression (Formal)) then
6454 -- Use default to construct declaration.
6457 Make_Object_Declaration (Sloc (Formal),
6458 Defining_Identifier => New_Copy (Formal_Id),
6459 Constant_Present => True,
6460 Object_Definition => New_Copy (Type_Id),
6461 Expression => New_Copy_Tree (Expression (Formal)));
6463 Append (Decl_Node, List);
6464 Set_Analyzed (Expression (Decl_Node), False);
6468 ("missing actual for instantiation of &",
6469 Instantiation_Node, Formal_Id);
6470 Abandon_Instantiation (Instantiation_Node);
6476 end Instantiate_Object;
6478 ------------------------------
6479 -- Instantiate_Package_Body --
6480 ------------------------------
6482 procedure Instantiate_Package_Body
6483 (Body_Info : Pending_Body_Info)
6485 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
6486 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
6487 Loc : constant Source_Ptr := Sloc (Inst_Node);
6489 Gen_Id : constant Node_Id := Name (Inst_Node);
6490 Gen_Unit : constant Entity_Id := Entity (Name (Inst_Node));
6491 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
6492 Act_Spec : constant Node_Id := Specification (Act_Decl);
6493 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
6495 Act_Body_Name : Node_Id;
6497 Gen_Body_Id : Node_Id;
6499 Act_Body_Id : Entity_Id;
6501 Parent_Installed : Boolean := False;
6502 Save_Style_Check : Boolean := Style_Check;
6505 Gen_Body_Id := Corresponding_Body (Gen_Decl);
6506 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
6508 if No (Gen_Body_Id) then
6509 Load_Parent_Of_Generic (Inst_Node, Specification (Gen_Decl));
6510 Gen_Body_Id := Corresponding_Body (Gen_Decl);
6513 -- Establish global variable for sloc adjustment and for error
6516 Instantiation_Node := Inst_Node;
6518 if Present (Gen_Body_Id) then
6519 Save_Env (Gen_Unit, Act_Decl_Id);
6520 Style_Check := False;
6521 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
6523 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
6525 Create_Instantiation_Source
6526 (Inst_Node, Gen_Body_Id, S_Adjustment);
6530 (Original_Node (Gen_Body), Empty, Instantiating => True);
6532 -- Build new name (possibly qualified) for body declaration.
6534 Act_Body_Id := New_Copy (Act_Decl_Id);
6536 -- Some attributes of the spec entity are not inherited by the
6539 Set_Handler_Records (Act_Body_Id, No_List);
6541 if Nkind (Defining_Unit_Name (Act_Spec)) =
6542 N_Defining_Program_Unit_Name
6545 Make_Defining_Program_Unit_Name (Loc,
6546 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
6547 Defining_Identifier => Act_Body_Id);
6549 Act_Body_Name := Act_Body_Id;
6552 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
6554 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
6555 Check_Generic_Actuals (Act_Decl_Id, False);
6557 -- If it is a child unit, make the parent instance (which is an
6558 -- instance of the parent of the generic) visible. The parent
6559 -- instance is the prefix of the name of the generic unit.
6561 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
6562 and then Nkind (Gen_Id) = N_Expanded_Name
6564 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
6565 Parent_Installed := True;
6567 elsif Is_Child_Unit (Gen_Unit) then
6568 Install_Parent (Scope (Gen_Unit), In_Body => True);
6569 Parent_Installed := True;
6572 -- If the instantiation is a library unit, and this is the main
6573 -- unit, then build the resulting compilation unit nodes for the
6574 -- instance. If this is a compilation unit but it is not the main
6575 -- unit, then it is the body of a unit in the context, that is being
6576 -- compiled because it is encloses some inlined unit or another
6577 -- generic unit being instantiated. In that case, this body is not
6578 -- part of the current compilation, and is not attached to the tree,
6579 -- but its parent must be set for analysis.
6581 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
6583 if Parent (Inst_Node) = Cunit (Main_Unit) then
6584 Build_Instance_Compilation_Unit_Nodes
6585 (Inst_Node, Act_Body, Act_Decl);
6586 Analyze (Inst_Node);
6588 -- If the instance is a child unit itself, then set the
6589 -- scope of the expanded body to be the parent of the
6590 -- instantiation (ensuring that the fully qualified name
6591 -- will be generated for the elaboration subprogram).
6593 if Nkind (Defining_Unit_Name (Act_Spec)) =
6594 N_Defining_Program_Unit_Name
6597 (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
6601 Set_Parent (Act_Body, Parent (Inst_Node));
6605 -- Case where instantiation is not a library unit
6608 -- If this is an early instantiation, i.e. appears textually
6609 -- before the corresponding body and must be elaborated first,
6610 -- indicate that the body instance is to be delayed.
6612 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
6614 -- Now analyze the body. We turn off all checks if this is
6615 -- an internal unit, since there is no reason to have checks
6616 -- on for any predefined run-time library code. All such
6617 -- code is designed to be compiled with checks off.
6619 -- Note that we do NOT apply this criterion to children of
6620 -- GNAT (or on VMS, children of DEC). The latter units must
6621 -- suppress checks explicitly if this is needed.
6623 if Is_Predefined_File_Name
6624 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
6626 Analyze (Act_Body, Suppress => All_Checks);
6632 if not Generic_Separately_Compiled (Gen_Unit) then
6633 Inherit_Context (Gen_Body, Inst_Node);
6636 Restore_Private_Views (Act_Decl_Id);
6638 Style_Check := Save_Style_Check;
6640 -- If we have no body, and the unit requires a body, then complain.
6641 -- This complaint is suppressed if we have detected other errors
6642 -- (since a common reason for missing the body is that it had errors).
6644 elsif Unit_Requires_Body (Gen_Unit) then
6645 if Errors_Detected = 0 then
6647 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
6649 -- Don't attempt to perform any cleanup actions if some other
6650 -- error was aready detected, since this can cause blowups.
6656 -- Case of package that does not need a body
6659 -- If the instantiation of the declaration is a library unit,
6660 -- rewrite the original package instantiation as a package
6661 -- declaration in the compilation unit node.
6663 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
6664 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
6665 Rewrite (Inst_Node, Act_Decl);
6667 -- If the instantiation is not a library unit, then append the
6668 -- declaration to the list of implicitly generated entities.
6669 -- unless it is already a list member which means that it was
6670 -- already processed
6672 elsif not Is_List_Member (Act_Decl) then
6673 Mark_Rewrite_Insertion (Act_Decl);
6674 Insert_Before (Inst_Node, Act_Decl);
6678 Expander_Mode_Restore;
6680 -- Remove the parent instances if they have been placed on the
6681 -- scope stack to compile the body.
6683 if Parent_Installed then
6684 Remove_Parent (In_Body => True);
6686 end Instantiate_Package_Body;
6688 ---------------------------------
6689 -- Instantiate_Subprogram_Body --
6690 ---------------------------------
6692 procedure Instantiate_Subprogram_Body
6693 (Body_Info : Pending_Body_Info)
6695 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
6696 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
6697 Loc : constant Source_Ptr := Sloc (Inst_Node);
6700 Gen_Id : constant Node_Id := Name (Inst_Node);
6701 Gen_Unit : constant Entity_Id := Entity (Name (Inst_Node));
6702 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
6703 Anon_Id : constant Entity_Id :=
6704 Defining_Unit_Name (Specification (Act_Decl));
6706 Gen_Body_Id : Node_Id;
6708 Act_Body_Id : Entity_Id;
6709 Pack_Id : Entity_Id := Defining_Unit_Name (Parent (Act_Decl));
6710 Pack_Body : Node_Id;
6711 Prev_Formal : Entity_Id;
6712 Unit_Renaming : Node_Id;
6714 Parent_Installed : Boolean := False;
6715 Save_Style_Check : Boolean := Style_Check;
6718 Gen_Body_Id := Corresponding_Body (Gen_Decl);
6720 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
6722 if No (Gen_Body_Id) then
6723 Load_Parent_Of_Generic (Inst_Node, Specification (Gen_Decl));
6724 Gen_Body_Id := Corresponding_Body (Gen_Decl);
6727 Instantiation_Node := Inst_Node;
6729 if Present (Gen_Body_Id) then
6730 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
6732 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
6734 -- Either body is not present, or context is non-expanding, as
6735 -- when compiling a subunit. Mark the instance as completed.
6737 Set_Has_Completion (Anon_Id);
6741 Save_Env (Gen_Unit, Anon_Id);
6742 Style_Check := False;
6743 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
6744 Create_Instantiation_Source (Inst_Node, Gen_Body_Id, S_Adjustment);
6748 (Original_Node (Gen_Body), Empty, Instantiating => True);
6749 Act_Body_Id := Defining_Entity (Act_Body);
6750 Set_Chars (Act_Body_Id, Chars (Anon_Id));
6751 Set_Sloc (Act_Body_Id, Sloc (Defining_Entity (Inst_Node)));
6752 Set_Corresponding_Spec (Act_Body, Anon_Id);
6753 Set_Has_Completion (Anon_Id);
6754 Check_Generic_Actuals (Pack_Id, False);
6756 -- If it is a child unit, make the parent instance (which is an
6757 -- instance of the parent of the generic) visible. The parent
6758 -- instance is the prefix of the name of the generic unit.
6760 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
6761 and then Nkind (Gen_Id) = N_Expanded_Name
6763 Install_Parent (Entity (Prefix (Gen_Id)), In_Body => True);
6764 Parent_Installed := True;
6766 elsif Is_Child_Unit (Gen_Unit) then
6767 Install_Parent (Scope (Gen_Unit), In_Body => True);
6768 Parent_Installed := True;
6771 -- Inside its body, a reference to the generic unit is a reference
6772 -- to the instance. The corresponding renaming is the first
6773 -- declaration in the body.
6776 Make_Subprogram_Renaming_Declaration (Loc,
6779 Specification (Original_Node (Gen_Body)),
6781 Instantiating => True),
6782 Name => New_Occurrence_Of (Anon_Id, Loc));
6784 -- If there is a formal subprogram with the same name as the
6785 -- unit itself, do not add this renaming declaration. This is
6786 -- a temporary fix for one ACVC test. ???
6788 Prev_Formal := First_Entity (Pack_Id);
6789 while Present (Prev_Formal) loop
6790 if Chars (Prev_Formal) = Chars (Gen_Unit)
6791 and then Is_Overloadable (Prev_Formal)
6796 Next_Entity (Prev_Formal);
6799 if Present (Prev_Formal) then
6800 Decls := New_List (Act_Body);
6802 Decls := New_List (Unit_Renaming, Act_Body);
6805 -- The subprogram body is placed in the body of a dummy package
6806 -- body, whose spec contains the subprogram declaration as well
6807 -- as the renaming declarations for the generic parameters.
6809 Pack_Body := Make_Package_Body (Loc,
6810 Defining_Unit_Name => New_Copy (Pack_Id),
6811 Declarations => Decls);
6813 Set_Corresponding_Spec (Pack_Body, Pack_Id);
6815 -- If the instantiation is a library unit, then build resulting
6816 -- compilation unit nodes for the instance. The declaration of
6817 -- the enclosing package is the grandparent of the subprogram
6818 -- declaration. First replace the instantiation node as the unit
6819 -- of the corresponding compilation.
6821 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
6823 if Parent (Inst_Node) = Cunit (Main_Unit) then
6824 Set_Unit (Parent (Inst_Node), Inst_Node);
6825 Build_Instance_Compilation_Unit_Nodes
6826 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
6827 Analyze (Inst_Node);
6829 Set_Parent (Pack_Body, Parent (Inst_Node));
6830 Analyze (Pack_Body);
6834 Insert_Before (Inst_Node, Pack_Body);
6835 Mark_Rewrite_Insertion (Pack_Body);
6836 Analyze (Pack_Body);
6838 if Expander_Active then
6839 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
6843 if not Generic_Separately_Compiled (Gen_Unit) then
6844 Inherit_Context (Gen_Body, Inst_Node);
6847 Restore_Private_Views (Pack_Id, False);
6849 if Parent_Installed then
6850 Remove_Parent (In_Body => True);
6854 Style_Check := Save_Style_Check;
6856 -- Body not found. Error was emitted already. If there were no
6857 -- previous errors, this may be an instance whose scope is a premature
6858 -- instance. In that case we must insure that the (legal) program does
6859 -- raise program error if executed. We generate a subprogram body for
6860 -- this purpose. See DEC ac30vso.
6862 elsif Errors_Detected = 0
6863 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
6865 if Ekind (Anon_Id) = E_Procedure then
6867 Make_Subprogram_Body (Loc,
6869 Make_Procedure_Specification (Loc,
6870 Defining_Unit_Name => New_Copy (Anon_Id),
6871 Parameter_Specifications =>
6873 (Parameter_Specifications (Parent (Anon_Id)))),
6875 Declarations => Empty_List,
6876 Handled_Statement_Sequence =>
6877 Make_Handled_Sequence_Of_Statements (Loc,
6879 New_List (Make_Raise_Program_Error (Loc))));
6882 Make_Subprogram_Body (Loc,
6884 Make_Function_Specification (Loc,
6885 Defining_Unit_Name => New_Copy (Anon_Id),
6886 Parameter_Specifications =>
6888 (Parameter_Specifications (Parent (Anon_Id))),
6890 New_Occurrence_Of (Etype (Anon_Id), Loc)),
6892 Declarations => Empty_List,
6893 Handled_Statement_Sequence =>
6894 Make_Handled_Sequence_Of_Statements (Loc,
6895 Statements => New_List (
6896 Make_Return_Statement (Loc,
6897 Expression => Make_Raise_Program_Error (Loc)))));
6900 Pack_Body := Make_Package_Body (Loc,
6901 Defining_Unit_Name => New_Copy (Pack_Id),
6902 Declarations => New_List (Act_Body));
6904 Insert_After (Inst_Node, Pack_Body);
6905 Set_Corresponding_Spec (Pack_Body, Pack_Id);
6906 Analyze (Pack_Body);
6909 Expander_Mode_Restore;
6910 end Instantiate_Subprogram_Body;
6912 ----------------------
6913 -- Instantiate_Type --
6914 ----------------------
6916 function Instantiate_Type
6919 Analyzed_Formal : Node_Id)
6922 Loc : constant Source_Ptr := Sloc (Actual);
6923 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
6924 A_Gen_T : constant Entity_Id := Defining_Identifier (Analyzed_Formal);
6925 Ancestor : Entity_Id;
6926 Def : constant Node_Id := Formal_Type_Definition (Formal);
6928 Decl_Node : Node_Id;
6930 procedure Validate_Array_Type_Instance;
6931 procedure Validate_Access_Subprogram_Instance;
6932 procedure Validate_Access_Type_Instance;
6933 procedure Validate_Derived_Type_Instance;
6934 procedure Validate_Private_Type_Instance;
6935 -- These procedures perform validation tests for the named case
6937 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
6938 -- Check that base types are the same and that the subtypes match
6939 -- statically. Used in several of the above.
6941 --------------------
6942 -- Subtypes_Match --
6943 --------------------
6945 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
6946 T : constant Entity_Id := Get_Instance_Of (Gen_T);
6949 return (Base_Type (T) = Base_Type (Act_T)
6950 -- why is the and then commented out here???
6951 -- and then Is_Constrained (T) = Is_Constrained (Act_T)
6952 and then Subtypes_Statically_Match (T, Act_T))
6954 or else (Is_Class_Wide_Type (Gen_T)
6955 and then Is_Class_Wide_Type (Act_T)
6958 Get_Instance_Of (Root_Type (Gen_T)),
6959 Root_Type (Act_T)));
6962 -----------------------------------------
6963 -- Validate_Access_Subprogram_Instance --
6964 -----------------------------------------
6966 procedure Validate_Access_Subprogram_Instance is
6968 if not Is_Access_Type (Act_T)
6969 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
6972 ("expect access type in instantiation of &", Actual, Gen_T);
6973 Abandon_Instantiation (Actual);
6976 Check_Mode_Conformant
6977 (Designated_Type (Act_T),
6978 Designated_Type (A_Gen_T),
6982 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
6983 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
6985 ("protected access type not allowed for formal &",
6989 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
6991 ("expect protected access type for formal &",
6994 end Validate_Access_Subprogram_Instance;
6996 -----------------------------------
6997 -- Validate_Access_Type_Instance --
6998 -----------------------------------
7000 procedure Validate_Access_Type_Instance is
7001 Desig_Type : Entity_Id :=
7002 Find_Actual_Type (Designated_Type (A_Gen_T), Scope (A_Gen_T));
7005 if not Is_Access_Type (Act_T) then
7007 ("expect access type in instantiation of &", Actual, Gen_T);
7008 Abandon_Instantiation (Actual);
7011 if Is_Access_Constant (A_Gen_T) then
7012 if not Is_Access_Constant (Act_T) then
7014 ("actual type must be access-to-constant type", Actual);
7015 Abandon_Instantiation (Actual);
7018 if Is_Access_Constant (Act_T) then
7020 ("actual type must be access-to-variable type", Actual);
7021 Abandon_Instantiation (Actual);
7023 elsif Ekind (A_Gen_T) = E_General_Access_Type
7024 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
7026 Error_Msg_N ("actual must be general access type!", Actual);
7027 Error_Msg_NE ("add ALL to }!", Actual, Act_T);
7028 Abandon_Instantiation (Actual);
7032 -- The designated subtypes, that is to say the subtypes introduced
7033 -- by an access type declaration (and not by a subtype declaration)
7036 if not Subtypes_Match
7037 (Desig_Type, Designated_Type (Base_Type (Act_T)))
7040 ("designated type of actual does not match that of formal &",
7042 Abandon_Instantiation (Actual);
7044 elsif Is_Access_Type (Designated_Type (Act_T))
7045 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
7047 Is_Constrained (Designated_Type (Desig_Type))
7050 ("designated type of actual does not match that of formal &",
7052 Abandon_Instantiation (Actual);
7054 end Validate_Access_Type_Instance;
7056 ----------------------------------
7057 -- Validate_Array_Type_Instance --
7058 ----------------------------------
7060 procedure Validate_Array_Type_Instance is
7065 function Formal_Dimensions return Int;
7066 -- Count number of dimensions in array type formal
7068 function Formal_Dimensions return Int is
7073 if Nkind (Def) = N_Constrained_Array_Definition then
7074 Index := First (Discrete_Subtype_Definitions (Def));
7076 Index := First (Subtype_Marks (Def));
7079 while Present (Index) loop
7085 end Formal_Dimensions;
7087 -- Start of processing for Validate_Array_Type_Instance
7090 if not Is_Array_Type (Act_T) then
7092 ("expect array type in instantiation of &", Actual, Gen_T);
7093 Abandon_Instantiation (Actual);
7095 elsif Nkind (Def) = N_Constrained_Array_Definition then
7096 if not (Is_Constrained (Act_T)) then
7098 ("expect constrained array in instantiation of &",
7100 Abandon_Instantiation (Actual);
7104 if Is_Constrained (Act_T) then
7106 ("expect unconstrained array in instantiation of &",
7108 Abandon_Instantiation (Actual);
7112 if Formal_Dimensions /= Number_Dimensions (Act_T) then
7114 ("dimensions of actual do not match formal &", Actual, Gen_T);
7115 Abandon_Instantiation (Actual);
7118 I1 := First_Index (A_Gen_T);
7119 I2 := First_Index (Act_T);
7120 for J in 1 .. Formal_Dimensions loop
7122 -- If the indices of the actual were given by a subtype_mark,
7123 -- the index was transformed into a range attribute. Retrieve
7124 -- the original type mark for checking.
7126 if Is_Entity_Name (Original_Node (I2)) then
7127 T2 := Entity (Original_Node (I2));
7132 if not Subtypes_Match
7133 (Find_Actual_Type (Etype (I1), Scope (A_Gen_T)), T2)
7136 ("index types of actual do not match those of formal &",
7138 Abandon_Instantiation (Actual);
7145 if not Subtypes_Match (
7146 Find_Actual_Type (Component_Type (A_Gen_T), Scope (A_Gen_T)),
7147 Component_Type (Act_T))
7150 ("component subtype of actual does not match that of formal &",
7152 Abandon_Instantiation (Actual);
7155 if Has_Aliased_Components (A_Gen_T)
7156 and then not Has_Aliased_Components (Act_T)
7159 ("actual must have aliased components to match formal type &",
7163 end Validate_Array_Type_Instance;
7165 ------------------------------------
7166 -- Validate_Derived_Type_Instance --
7167 ------------------------------------
7169 procedure Validate_Derived_Type_Instance is
7170 Actual_Discr : Entity_Id;
7171 Ancestor_Discr : Entity_Id;
7174 -- If the parent type in the generic declaration is itself
7175 -- a previous formal type, then it is local to the generic
7176 -- and absent from the analyzed generic definition. In that
7177 -- case the ancestor is the instance of the formal (which must
7178 -- have been instantiated previously). Otherwise, the analyzed
7179 -- generic carries the parent type. If the parent type is defined
7180 -- in a previous formal package, then the scope of that formal
7181 -- package is that of the generic type itself, and it has already
7182 -- been mapped into the corresponding type in the actual package.
7184 -- Common case: parent type defined outside of the generic.
7186 if Is_Entity_Name (Subtype_Mark (Def))
7187 and then Present (Entity (Subtype_Mark (Def)))
7189 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
7191 -- Check whether parent is defined in a previous formal package.
7194 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
7197 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
7199 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T)) then
7201 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
7204 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
7207 if not Is_Ancestor (Base_Type (Ancestor), Act_T) then
7209 ("expect type derived from & in instantiation",
7210 Actual, First_Subtype (Ancestor));
7211 Abandon_Instantiation (Actual);
7214 -- Perform atomic/volatile checks (RM C.6(12))
7216 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
7218 ("cannot have atomic actual type for non-atomic formal type",
7221 elsif Is_Volatile (Act_T)
7222 and then not Is_Volatile (Ancestor)
7223 and then Is_By_Reference_Type (Ancestor)
7226 ("cannot have volatile actual type for non-volatile formal type",
7230 -- It should not be necessary to check for unknown discriminants
7231 -- on Formal, but for some reason Has_Unknown_Discriminants is
7232 -- false for A_Gen_T, so Is_Indefinite_Subtype incorrectly
7233 -- returns False. This needs fixing. ???
7235 if not Is_Indefinite_Subtype (A_Gen_T)
7236 and then not Unknown_Discriminants_Present (Formal)
7237 and then Is_Indefinite_Subtype (Act_T)
7240 ("actual subtype must be constrained", Actual);
7241 Abandon_Instantiation (Actual);
7244 if not Unknown_Discriminants_Present (Formal) then
7245 if Is_Constrained (Ancestor) then
7246 if not Is_Constrained (Act_T) then
7248 ("actual subtype must be constrained", Actual);
7249 Abandon_Instantiation (Actual);
7252 -- Ancestor is unconstrained
7254 elsif Is_Constrained (Act_T) then
7255 if Ekind (Ancestor) = E_Access_Type
7256 or else Is_Composite_Type (Ancestor)
7259 ("actual subtype must be unconstrained", Actual);
7260 Abandon_Instantiation (Actual);
7263 -- A class-wide type is only allowed if the formal has
7264 -- unknown discriminants.
7266 elsif Is_Class_Wide_Type (Act_T)
7267 and then not Has_Unknown_Discriminants (Ancestor)
7270 ("actual for & cannot be a class-wide type", Actual, Gen_T);
7271 Abandon_Instantiation (Actual);
7273 -- Otherwise, the formal and actual shall have the same
7274 -- number of discriminants and each discriminant of the
7275 -- actual must correspond to a discriminant of the formal.
7277 elsif Has_Discriminants (Act_T)
7278 and then Has_Discriminants (Ancestor)
7280 Actual_Discr := First_Discriminant (Act_T);
7281 Ancestor_Discr := First_Discriminant (Ancestor);
7282 while Present (Actual_Discr)
7283 and then Present (Ancestor_Discr)
7285 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
7286 not Present (Corresponding_Discriminant (Actual_Discr))
7289 ("discriminant & does not correspond " &
7290 "to ancestor discriminant", Actual, Actual_Discr);
7291 Abandon_Instantiation (Actual);
7294 Next_Discriminant (Actual_Discr);
7295 Next_Discriminant (Ancestor_Discr);
7298 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
7300 ("actual for & must have same number of discriminants",
7302 Abandon_Instantiation (Actual);
7305 -- This case should be caught by the earlier check for
7306 -- for constrainedness, but the check here is added for
7309 elsif Has_Discriminants (Act_T) then
7311 ("actual for & must not have discriminants", Actual, Gen_T);
7312 Abandon_Instantiation (Actual);
7314 elsif Has_Discriminants (Ancestor) then
7316 ("actual for & must have known discriminants", Actual, Gen_T);
7317 Abandon_Instantiation (Actual);
7320 if not Subtypes_Statically_Compatible (Act_T, Ancestor) then
7322 ("constraint on actual is incompatible with formal", Actual);
7323 Abandon_Instantiation (Actual);
7327 end Validate_Derived_Type_Instance;
7329 ------------------------------------
7330 -- Validate_Private_Type_Instance --
7331 ------------------------------------
7333 procedure Validate_Private_Type_Instance is
7334 Formal_Discr : Entity_Id;
7335 Actual_Discr : Entity_Id;
7336 Formal_Subt : Entity_Id;
7339 if (Is_Limited_Type (Act_T)
7340 or else Is_Limited_Composite (Act_T))
7341 and then not Is_Limited_Type (A_Gen_T)
7344 ("actual for non-limited & cannot be a limited type", Actual,
7346 Abandon_Instantiation (Actual);
7348 elsif Is_Indefinite_Subtype (Act_T)
7349 and then not Is_Indefinite_Subtype (A_Gen_T)
7353 ("actual for & must be a definite subtype", Actual, Gen_T);
7355 elsif not Is_Tagged_Type (Act_T)
7356 and then Is_Tagged_Type (A_Gen_T)
7359 ("actual for & must be a tagged type", Actual, Gen_T);
7361 elsif Has_Discriminants (A_Gen_T) then
7362 if not Has_Discriminants (Act_T) then
7364 ("actual for & must have discriminants", Actual, Gen_T);
7365 Abandon_Instantiation (Actual);
7367 elsif Is_Constrained (Act_T) then
7369 ("actual for & must be unconstrained", Actual, Gen_T);
7370 Abandon_Instantiation (Actual);
7373 Formal_Discr := First_Discriminant (A_Gen_T);
7374 Actual_Discr := First_Discriminant (Act_T);
7375 while Formal_Discr /= Empty loop
7376 if Actual_Discr = Empty then
7378 ("discriminants on actual do not match formal",
7380 Abandon_Instantiation (Actual);
7383 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
7385 -- access discriminants match if designated types do.
7387 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
7388 and then (Ekind (Base_Type (Etype (Actual_Discr))))
7389 = E_Anonymous_Access_Type
7390 and then Get_Instance_Of (
7391 Designated_Type (Base_Type (Formal_Subt)))
7392 = Designated_Type (Base_Type (Etype (Actual_Discr)))
7396 elsif Base_Type (Formal_Subt) /=
7397 Base_Type (Etype (Actual_Discr))
7400 ("types of actual discriminants must match formal",
7402 Abandon_Instantiation (Actual);
7404 elsif not Subtypes_Statically_Match
7405 (Formal_Subt, Etype (Actual_Discr))
7409 ("subtypes of actual discriminants must match formal",
7411 Abandon_Instantiation (Actual);
7414 Next_Discriminant (Formal_Discr);
7415 Next_Discriminant (Actual_Discr);
7418 if Actual_Discr /= Empty then
7420 ("discriminants on actual do not match formal",
7422 Abandon_Instantiation (Actual);
7429 end Validate_Private_Type_Instance;
7431 -- Start of processing for Instantiate_Type
7434 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
7435 Error_Msg_N ("duplicate instantiation of generic type", Actual);
7438 elsif not Is_Entity_Name (Actual)
7439 or else not Is_Type (Entity (Actual))
7442 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
7443 Abandon_Instantiation (Actual);
7446 Act_T := Entity (Actual);
7448 if Ekind (Act_T) = E_Incomplete_Type then
7449 if No (Underlying_Type (Act_T)) then
7450 Error_Msg_N ("premature use of incomplete type", Actual);
7451 Abandon_Instantiation (Actual);
7453 Act_T := Full_View (Act_T);
7454 Set_Entity (Actual, Act_T);
7456 if Has_Private_Component (Act_T) then
7458 ("premature use of type with private component", Actual);
7462 elsif Is_Private_Type (Act_T)
7463 and then Is_Private_Type (Base_Type (Act_T))
7464 and then not Is_Generic_Type (Act_T)
7465 and then not Is_Derived_Type (Act_T)
7466 and then No (Full_View (Root_Type (Act_T)))
7468 Error_Msg_N ("premature use of private type", Actual);
7470 elsif Has_Private_Component (Act_T) then
7472 ("premature use of type with private component", Actual);
7475 Set_Instance_Of (A_Gen_T, Act_T);
7477 -- If the type is generic, the class-wide type may also be used
7479 if Is_Tagged_Type (A_Gen_T)
7480 and then Is_Tagged_Type (Act_T)
7481 and then not Is_Class_Wide_Type (A_Gen_T)
7483 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
7484 Class_Wide_Type (Act_T));
7487 if not Is_Abstract (A_Gen_T)
7488 and then Is_Abstract (Act_T)
7491 ("actual of non-abstract formal cannot be abstract", Actual);
7494 if Is_Scalar_Type (Gen_T) then
7495 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
7500 when N_Formal_Private_Type_Definition =>
7501 Validate_Private_Type_Instance;
7503 when N_Formal_Derived_Type_Definition =>
7504 Validate_Derived_Type_Instance;
7506 when N_Formal_Discrete_Type_Definition =>
7507 if not Is_Discrete_Type (Act_T) then
7509 ("expect discrete type in instantiation of&", Actual, Gen_T);
7510 Abandon_Instantiation (Actual);
7513 when N_Formal_Signed_Integer_Type_Definition =>
7514 if not Is_Signed_Integer_Type (Act_T) then
7516 ("expect signed integer type in instantiation of&",
7518 Abandon_Instantiation (Actual);
7521 when N_Formal_Modular_Type_Definition =>
7522 if not Is_Modular_Integer_Type (Act_T) then
7524 ("expect modular type in instantiation of &", Actual, Gen_T);
7525 Abandon_Instantiation (Actual);
7528 when N_Formal_Floating_Point_Definition =>
7529 if not Is_Floating_Point_Type (Act_T) then
7531 ("expect float type in instantiation of &", Actual, Gen_T);
7532 Abandon_Instantiation (Actual);
7535 when N_Formal_Ordinary_Fixed_Point_Definition =>
7536 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
7538 ("expect ordinary fixed point type in instantiation of &",
7540 Abandon_Instantiation (Actual);
7543 when N_Formal_Decimal_Fixed_Point_Definition =>
7544 if not Is_Decimal_Fixed_Point_Type (Act_T) then
7546 ("expect decimal type in instantiation of &",
7548 Abandon_Instantiation (Actual);
7551 when N_Array_Type_Definition =>
7552 Validate_Array_Type_Instance;
7554 when N_Access_To_Object_Definition =>
7555 Validate_Access_Type_Instance;
7557 when N_Access_Function_Definition |
7558 N_Access_Procedure_Definition =>
7559 Validate_Access_Subprogram_Instance;
7562 raise Program_Error;
7567 Make_Subtype_Declaration (Loc,
7568 Defining_Identifier => New_Copy (Gen_T),
7569 Subtype_Indication => New_Reference_To (Act_T, Loc));
7571 if Is_Private_Type (Act_T) then
7572 Set_Has_Private_View (Subtype_Indication (Decl_Node));
7575 -- Flag actual derived types so their elaboration produces the
7576 -- appropriate renamings for the primitive operations of the ancestor.
7577 -- Flag actual for formal private types as well, to determine whether
7578 -- operations in the private part may override inherited operations.
7580 if Nkind (Def) = N_Formal_Derived_Type_Definition
7581 or else Nkind (Def) = N_Formal_Private_Type_Definition
7583 Set_Generic_Parent_Type (Decl_Node, Ancestor);
7587 end Instantiate_Type;
7589 ---------------------
7590 -- Is_In_Main_Unit --
7591 ---------------------
7593 function Is_In_Main_Unit (N : Node_Id) return Boolean is
7594 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
7596 Current_Unit : Node_Id;
7599 if Unum = Main_Unit then
7602 -- If the current unit is a subunit then it is either the main unit
7603 -- or is being compiled as part of the main unit.
7605 elsif Nkind (N) = N_Compilation_Unit then
7606 return Nkind (Unit (N)) = N_Subunit;
7609 Current_Unit := Parent (N);
7610 while Present (Current_Unit)
7611 and then Nkind (Current_Unit) /= N_Compilation_Unit
7613 Current_Unit := Parent (Current_Unit);
7616 -- The instantiation node is in the main unit, or else the current
7617 -- node (perhaps as the result of nested instantiations) is in the
7618 -- main unit, or in the declaration of the main unit, which in this
7619 -- last case must be a body.
7621 return Unum = Main_Unit
7622 or else Current_Unit = Cunit (Main_Unit)
7623 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
7624 or else (Present (Library_Unit (Current_Unit))
7625 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
7626 end Is_In_Main_Unit;
7628 ----------------------------
7629 -- Load_Parent_Of_Generic --
7630 ----------------------------
7632 procedure Load_Parent_Of_Generic (N : Node_Id; Spec : Node_Id) is
7633 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
7634 True_Parent : Node_Id;
7635 Inst_Node : Node_Id;
7637 Save_Style_Check : Boolean := Style_Check;
7640 if not In_Same_Source_Unit (N, Spec)
7641 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
7642 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
7643 and then not Is_In_Main_Unit (Spec))
7645 -- Find body of parent of spec, and analyze it. A special case
7646 -- arises when the parent is an instantiation, that is to say when
7647 -- we are currently instantiating a nested generic. In that case,
7648 -- there is no separate file for the body of the enclosing instance.
7649 -- Instead, the enclosing body must be instantiated as if it were
7650 -- a pending instantiation, in order to produce the body for the
7651 -- nested generic we require now. Note that in that case the
7652 -- generic may be defined in a package body, the instance defined
7653 -- in the same package body, and the original enclosing body may not
7654 -- be in the main unit.
7656 True_Parent := Parent (Spec);
7659 while Present (True_Parent)
7660 and then Nkind (True_Parent) /= N_Compilation_Unit
7662 if Nkind (True_Parent) = N_Package_Declaration
7664 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
7666 -- Parent is a compilation unit that is an instantiation.
7667 -- Instantiation node has been replaced with package decl.
7669 Inst_Node := Original_Node (True_Parent);
7672 elsif Nkind (True_Parent) = N_Package_Declaration
7673 and then Present (Generic_Parent (Specification (True_Parent)))
7675 -- Parent is an instantiation within another specification.
7676 -- Declaration for instance has been inserted before original
7677 -- instantiation node. A direct link would be preferable?
7679 Inst_Node := Next (True_Parent);
7681 while Present (Inst_Node)
7682 and then Nkind (Inst_Node) /= N_Package_Instantiation
7687 -- If the instance appears within a generic, and the generic
7688 -- unit is defined within a formal package of the enclosing
7689 -- generic, there is no generic body available, and none
7690 -- needed. A more precise test should be used ???
7692 if No (Inst_Node) then
7698 True_Parent := Parent (True_Parent);
7702 if Present (Inst_Node) then
7704 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
7706 -- Instantiation node and declaration of instantiated package
7707 -- were exchanged when only the declaration was needed.
7708 -- Restore instantiation node before proceeding with body.
7710 Set_Unit (Parent (True_Parent), Inst_Node);
7713 -- Now complete instantiation of enclosing body, if it appears
7714 -- in some other unit. If it appears in the current unit, the
7715 -- body will have been instantiated already.
7717 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
7718 Instantiate_Package_Body
7719 (Pending_Body_Info'(
7720 Inst_Node, True_Parent, Expander_Active,
7721 Get_Code_Unit (Sloc (Inst_Node))));
7725 Opt.Style_Check := False;
7726 Load_Needed_Body (Comp_Unit, OK);
7727 Opt.Style_Check := Save_Style_Check;
7730 and then Unit_Requires_Body (Defining_Entity (Spec))
7733 Bname : constant Unit_Name_Type :=
7734 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
7737 Error_Msg_Unit_1 := Bname;
7738 Error_Msg_N ("this instantiation requires$!", N);
7740 Get_File_Name (Bname, Subunit => False);
7741 Error_Msg_N ("\but file{ was not found!", N);
7742 raise Unrecoverable_Error;
7748 -- If loading the parent of the generic caused an instantiation
7749 -- circularity, we abandon compilation at this point, because
7750 -- otherwise in some cases we get into trouble with infinite
7751 -- recursions after this point.
7753 if Circularity_Detected then
7754 raise Unrecoverable_Error;
7757 end Load_Parent_Of_Generic;
7759 -----------------------
7760 -- Move_Freeze_Nodes --
7761 -----------------------
7763 procedure Move_Freeze_Nodes
7764 (Out_Of : Entity_Id;
7769 Next_Decl : Node_Id;
7770 Next_Node : Node_Id := After;
7773 function Is_Outer_Type (T : Entity_Id) return Boolean;
7774 -- Check whether entity is declared in a scope external to that
7775 -- of the generic unit.
7781 function Is_Outer_Type (T : Entity_Id) return Boolean is
7782 Scop : Entity_Id := Scope (T);
7785 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
7789 while Scop /= Standard_Standard loop
7791 if Scop = Out_Of then
7794 Scop := Scope (Scop);
7802 -- Start of processing for Move_Freeze_Nodes
7809 -- First remove the freeze nodes that may appear before all other
7813 while Present (Decl)
7814 and then Nkind (Decl) = N_Freeze_Entity
7815 and then Is_Outer_Type (Entity (Decl))
7817 Decl := Remove_Head (L);
7818 Insert_After (Next_Node, Decl);
7819 Set_Analyzed (Decl, False);
7824 -- Next scan the list of declarations and remove each freeze node that
7825 -- appears ahead of the current node.
7827 while Present (Decl) loop
7828 while Present (Next (Decl))
7829 and then Nkind (Next (Decl)) = N_Freeze_Entity
7830 and then Is_Outer_Type (Entity (Next (Decl)))
7832 Next_Decl := Remove_Next (Decl);
7833 Insert_After (Next_Node, Next_Decl);
7834 Set_Analyzed (Next_Decl, False);
7835 Next_Node := Next_Decl;
7838 -- If the declaration is a nested package or concurrent type, then
7839 -- recurse. Nested generic packages will have been processed from the
7842 if Nkind (Decl) = N_Package_Declaration then
7843 Spec := Specification (Decl);
7845 elsif Nkind (Decl) = N_Task_Type_Declaration then
7846 Spec := Task_Definition (Decl);
7848 elsif Nkind (Decl) = N_Protected_Type_Declaration then
7849 Spec := Protected_Definition (Decl);
7855 if Present (Spec) then
7856 Move_Freeze_Nodes (Out_Of, Next_Node,
7857 Visible_Declarations (Spec));
7858 Move_Freeze_Nodes (Out_Of, Next_Node,
7859 Private_Declarations (Spec));
7864 end Move_Freeze_Nodes;
7870 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
7872 return Generic_Renamings.Table (E).Next_In_HTable;
7875 ------------------------
7876 -- Preanalyze_Actuals --
7877 ------------------------
7879 procedure Pre_Analyze_Actuals (N : Node_Id) is
7882 Errs : Int := Errors_Detected;
7885 Assoc := First (Generic_Associations (N));
7887 while Present (Assoc) loop
7888 Act := Explicit_Generic_Actual_Parameter (Assoc);
7890 -- Within a nested instantiation, a defaulted actual is an
7891 -- empty association, so nothing to analyze. If the actual for
7892 -- a subprogram is an attribute, analyze prefix only, because
7893 -- actual is not a complete attribute reference.
7894 -- String literals may be operators, but at this point we do not
7895 -- know whether the actual is a formal subprogram or a string.
7900 elsif Nkind (Act) = N_Attribute_Reference then
7901 Analyze (Prefix (Act));
7903 elsif Nkind (Act) = N_Explicit_Dereference then
7904 Analyze (Prefix (Act));
7906 elsif Nkind (Act) /= N_Operator_Symbol then
7910 if Errs /= Errors_Detected then
7911 Abandon_Instantiation (Act);
7916 end Pre_Analyze_Actuals;
7922 procedure Remove_Parent (In_Body : Boolean := False) is
7923 S : Entity_Id := Current_Scope;
7929 -- After child instantiation is complete, remove from scope stack
7930 -- the extra copy of the current scope, and then remove parent
7936 while Current_Scope /= S loop
7938 End_Package_Scope (Current_Scope);
7940 if In_Open_Scopes (P) then
7941 E := First_Entity (P);
7943 while Present (E) loop
7944 Set_Is_Immediately_Visible (E, True);
7948 elsif not In_Open_Scopes (Scope (P)) then
7949 Set_Is_Immediately_Visible (P, False);
7953 -- Reset visibility of entities in the enclosing scope.
7955 Set_Is_Hidden_Open_Scope (Current_Scope, False);
7956 Hidden := First_Elmt (Hidden_Entities);
7958 while Present (Hidden) loop
7959 Set_Is_Immediately_Visible (Node (Hidden), True);
7964 -- Each body is analyzed separately, and there is no context
7965 -- that needs preserving from one body instance to the next,
7966 -- so remove all parent scopes that have been installed.
7968 while Present (S) loop
7969 End_Package_Scope (S);
7971 exit when S = Standard_Standard;
7981 procedure Restore_Env is
7982 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
7985 Ada_83 := Saved.Ada_83;
7987 if No (Current_Instantiated_Parent.Act_Id) then
7989 -- Restore environment after subprogram inlining
7991 Restore_Private_Views (Empty);
7994 Current_Instantiated_Parent := Saved.Instantiated_Parent;
7995 Exchanged_Views := Saved.Exchanged_Views;
7996 Hidden_Entities := Saved.Hidden_Entities;
7997 Current_Sem_Unit := Saved.Current_Sem_Unit;
7999 Instance_Envs.Decrement_Last;
8002 ---------------------------
8003 -- Restore_Private_Views --
8004 ---------------------------
8006 procedure Restore_Private_Views
8007 (Pack_Id : Entity_Id;
8008 Is_Package : Boolean := True)
8017 M := First_Elmt (Exchanged_Views);
8018 while Present (M) loop
8021 -- Subtypes of types whose views have been exchanged, and that
8022 -- are defined within the instance, were not on the list of
8023 -- Private_Dependents on entry to the instance, so they have to
8024 -- be exchanged explicitly now, in order to remain consistent with
8025 -- the view of the parent type.
8027 if Ekind (Typ) = E_Private_Type
8028 or else Ekind (Typ) = E_Limited_Private_Type
8029 or else Ekind (Typ) = E_Record_Type_With_Private
8031 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
8033 while Present (Dep_Elmt) loop
8034 Dep_Typ := Node (Dep_Elmt);
8036 if Scope (Dep_Typ) = Pack_Id
8037 and then Present (Full_View (Dep_Typ))
8039 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
8040 Exchange_Declarations (Dep_Typ);
8043 Next_Elmt (Dep_Elmt);
8047 Exchange_Declarations (Node (M));
8051 if No (Pack_Id) then
8055 -- Make the generic formal parameters private, and make the formal
8056 -- types into subtypes of the actuals again.
8058 E := First_Entity (Pack_Id);
8060 while Present (E) loop
8061 Set_Is_Hidden (E, True);
8064 and then Nkind (Parent (E)) = N_Subtype_Declaration
8066 Set_Is_Generic_Actual_Type (E, False);
8068 -- An unusual case of aliasing: the actual may also be directly
8069 -- visible in the generic, and be private there, while it is
8070 -- fully visible in the context of the instance. The internal
8071 -- subtype is private in the instance, but has full visibility
8072 -- like its parent in the enclosing scope. This enforces the
8073 -- invariant that the privacy status of all private dependents of
8074 -- a type coincide with that of the parent type. This can only
8075 -- happen when a generic child unit is instantiated within a
8078 if Is_Private_Type (E)
8079 and then not Is_Private_Type (Etype (E))
8081 Exchange_Declarations (E);
8084 elsif Ekind (E) = E_Package then
8086 -- The end of the renaming list is the renaming of the generic
8087 -- package itself. If the instance is a subprogram, all entities
8088 -- in the corresponding package are renamings. If this entity is
8089 -- a formal package, make its own formals private as well. The
8090 -- actual in this case is itself the renaming of an instantation.
8091 -- If the entity is not a package renaming, it is the entity
8092 -- created to validate formal package actuals: ignore.
8094 -- If the actual is itself a formal package for the enclosing
8095 -- generic, or the actual for such a formal package, it remains
8096 -- visible after the current instance, and therefore nothing
8097 -- needs to be done either, except to keep it accessible.
8100 and then Renamed_Object (E) = Pack_Id
8104 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
8107 elsif Denotes_Formal_Package (Renamed_Object (E)) then
8108 Set_Is_Hidden (E, False);
8112 Act_P : Entity_Id := Renamed_Object (E);
8113 Id : Entity_Id := First_Entity (Act_P);
8117 and then Id /= First_Private_Entity (Act_P)
8119 Set_Is_Hidden (Id, True);
8120 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
8121 exit when Ekind (Id) = E_Package
8122 and then Renamed_Object (Id) = Act_P;
8133 end Restore_Private_Views;
8140 (Gen_Unit : Entity_Id;
8141 Act_Unit : Entity_Id)
8143 Saved : Instance_Env;
8146 Saved.Ada_83 := Ada_83;
8147 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8148 Saved.Exchanged_Views := Exchanged_Views;
8149 Saved.Hidden_Entities := Hidden_Entities;
8150 Saved.Current_Sem_Unit := Current_Sem_Unit;
8151 Instance_Envs.Increment_Last;
8152 Instance_Envs.Table (Instance_Envs.Last) := Saved;
8154 -- Regardless of the current mode, predefined units are analyzed in
8155 -- Ada95 mode, and Ada83 checks don't apply.
8157 if Is_Internal_File_Name
8158 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
8159 Renamings_Included => True) then
8163 Current_Instantiated_Parent := (Gen_Unit, Act_Unit, Assoc_Null);
8164 Exchanged_Views := New_Elmt_List;
8165 Hidden_Entities := New_Elmt_List;
8168 ----------------------------
8169 -- Save_Global_References --
8170 ----------------------------
8172 procedure Save_Global_References (N : Node_Id) is
8173 Gen_Scope : Entity_Id;
8177 function Is_Global (E : Entity_Id) return Boolean;
8178 -- Check whether entity is defined outside of generic unit.
8179 -- Examine the scope of an entity, and the scope of the scope,
8180 -- etc, until we find either Standard, in which case the entity
8181 -- is global, or the generic unit itself, which indicates that
8182 -- the entity is local. If the entity is the generic unit itself,
8183 -- as in the case of a recursive call, or the enclosing generic unit,
8184 -- if different from the current scope, then it is local as well,
8185 -- because it will be replaced at the point of instantiation. On
8186 -- the other hand, if it is a reference to a child unit of a common
8187 -- ancestor, which appears in an instantiation, it is global because
8188 -- it is used to denote a specific compilation unit at the time the
8189 -- instantiations will be analyzed.
8191 procedure Reset_Entity (N : Node_Id);
8192 -- Save semantic information on global entity, so that it is not
8193 -- resolved again at instantiation time.
8195 procedure Save_Global_Defaults (N1, N2 : Node_Id);
8196 -- Default actuals in nested instances must be handled specially
8197 -- because there is no link to them from the original tree. When an
8198 -- actual subprogram is given by a default, we add an explicit generic
8199 -- association for it in the instantiation node. When we save the
8200 -- global references on the name of the instance, we recover the list
8201 -- of generic associations, and add an explicit one to the original
8202 -- generic tree, through which a global actual can be preserved.
8203 -- Similarly, if a child unit is instantiated within a sibling, in the
8204 -- context of the parent, we must preserve the identifier of the parent
8205 -- so that it can be properly resolved in a subsequent instantiation.
8207 procedure Save_Global_Operand_Descendants (N : Node_Id);
8208 -- Apply Save_Global_Descendant to the possible operand fields
8209 -- of the node N (Field2 = Left_Opnd, Field3 = Right_Opnd).
8211 -- It is uncomfortable for Sem_Ch12 to have this knowledge ???
8213 procedure Save_Global_Descendant (D : Union_Id);
8214 -- Apply Save_Global_References recursively to the descendents of
8217 procedure Save_References (N : Node_Id);
8218 -- This is the recursive procedure that does the work, once the
8219 -- enclosing generic scope has been established.
8225 function Is_Global (E : Entity_Id) return Boolean is
8226 Se : Entity_Id := Scope (E);
8228 function Is_Instance_Node (Decl : Node_Id) return Boolean;
8229 -- Determine whether the parent node of a reference to a child unit
8230 -- denotes an instantiation or a formal package, in which case the
8231 -- reference to the child unit is global, even if it appears within
8232 -- the current scope (e.g. when the instance appears within the body
8235 function Is_Instance_Node (Decl : Node_Id) return Boolean is
8237 return (Nkind (Decl) in N_Generic_Instantiation
8239 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration);
8240 end Is_Instance_Node;
8242 -- Start of processing for Is_Global
8245 if E = Gen_Scope then
8248 elsif E = Standard_Standard then
8251 elsif Is_Child_Unit (E)
8252 and then (Is_Instance_Node (Parent (N2))
8253 or else (Nkind (Parent (N2)) = N_Expanded_Name
8254 and then N2 = Selector_Name (Parent (N2))
8255 and then Is_Instance_Node (Parent (Parent (N2)))))
8260 while Se /= Gen_Scope loop
8261 if Se = Standard_Standard then
8276 procedure Reset_Entity (N : Node_Id) is
8278 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
8279 -- The type of N2 is global to the generic unit. Save the
8280 -- type in the generic node.
8282 ---------------------
8283 -- Set_Global_Type --
8284 ---------------------
8286 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
8287 Typ : constant Entity_Id := Etype (N2);
8293 and then Has_Private_View (Entity (N))
8295 -- If the entity of N is not the associated node, this is
8296 -- a nested generic and it has an associated node as well,
8297 -- whose type is already the full view (see below). Indicate
8298 -- that the original node has a private view.
8300 Set_Has_Private_View (N);
8303 -- If not a private type, nothing else to do
8305 if not Is_Private_Type (Typ) then
8306 if Is_Array_Type (Typ)
8307 and then Is_Private_Type (Component_Type (Typ))
8309 Set_Has_Private_View (N);
8312 -- If it is a derivation of a private type in a context where
8313 -- no full view is needed, nothing to do either.
8315 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
8318 -- Otherwise mark the type for flipping and use the full_view
8322 Set_Has_Private_View (N);
8324 if Present (Full_View (Typ)) then
8325 Set_Etype (N2, Full_View (Typ));
8328 end Set_Global_Type;
8330 -- Start of processing for Reset_Entity
8333 N2 := Get_Associated_Node (N);
8337 if Is_Global (E) then
8338 Set_Global_Type (N, N2);
8340 elsif Nkind (N) = N_Op_Concat
8341 and then Is_Generic_Type (Etype (N2))
8343 (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
8344 or else Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
8345 and then Is_Intrinsic_Subprogram (E)
8350 -- Entity is local. Mark generic node as unresolved.
8351 -- Note that now it does not have an entity.
8353 Set_Associated_Node (N, Empty);
8354 Set_Etype (N, Empty);
8357 if (Nkind (Parent (N)) = N_Package_Instantiation
8358 or else Nkind (Parent (N)) = N_Function_Instantiation
8359 or else Nkind (Parent (N)) = N_Procedure_Instantiation)
8360 and then N = Name (Parent (N))
8362 Save_Global_Defaults (Parent (N), Parent (N2));
8365 elsif Nkind (Parent (N)) = N_Selected_Component
8366 and then Nkind (Parent (N2)) = N_Expanded_Name
8369 if Is_Global (Entity (Parent (N2))) then
8370 Change_Selected_Component_To_Expanded_Name (Parent (N));
8371 Set_Associated_Node (Parent (N), Parent (N2));
8372 Set_Global_Type (Parent (N), Parent (N2));
8373 Save_Global_Operand_Descendants (N);
8375 -- If this is a reference to the current generic entity,
8376 -- replace it with a simple name. This is to avoid anomalies
8377 -- when the enclosing scope is also a generic unit, in which
8378 -- case the selected component will not resolve to the current
8379 -- unit within an instance of the outer one. Ditto if the
8380 -- entity is an enclosing scope, e.g. a parent unit.
8382 elsif In_Open_Scopes (Entity (Parent (N2)))
8383 and then not Is_Generic_Unit (Entity (Prefix (Parent (N2))))
8385 Rewrite (Parent (N),
8386 Make_Identifier (Sloc (N),
8387 Chars => Chars (Selector_Name (Parent (N2)))));
8390 if (Nkind (Parent (Parent (N))) = N_Package_Instantiation
8391 or else Nkind (Parent (Parent (N)))
8392 = N_Function_Instantiation
8393 or else Nkind (Parent (Parent (N)))
8394 = N_Procedure_Instantiation)
8395 and then Parent (N) = Name (Parent (Parent (N)))
8397 Save_Global_Defaults
8398 (Parent (Parent (N)), Parent (Parent ((N2))));
8401 -- A selected component may denote a static constant that has
8402 -- been folded. Make the same replacement in original tree.
8404 elsif Nkind (Parent (N)) = N_Selected_Component
8405 and then (Nkind (Parent (N2)) = N_Integer_Literal
8406 or else Nkind (Parent (N2)) = N_Real_Literal)
8408 Rewrite (Parent (N),
8409 New_Copy (Parent (N2)));
8410 Set_Analyzed (Parent (N), False);
8412 -- A selected component may be transformed into a parameterless
8413 -- function call. If the called entity is global, rewrite the
8414 -- node appropriately, i.e. as an extended name for the global
8417 elsif Nkind (Parent (N)) = N_Selected_Component
8418 and then Nkind (Parent (N2)) = N_Function_Call
8419 and then Is_Global (Entity (Name (Parent (N2))))
8421 Change_Selected_Component_To_Expanded_Name (Parent (N));
8422 Set_Associated_Node (Parent (N), Name (Parent (N2)));
8423 Set_Global_Type (Parent (N), Name (Parent (N2)));
8424 Save_Global_Operand_Descendants (N);
8427 -- Entity is local. Reset in generic unit, so that node
8428 -- is resolved anew at the point of instantiation.
8430 Set_Associated_Node (N, Empty);
8431 Set_Etype (N, Empty);
8435 --------------------------
8436 -- Save_Global_Defaults --
8437 --------------------------
8439 procedure Save_Global_Defaults (N1, N2 : Node_Id) is
8440 Loc : constant Source_Ptr := Sloc (N1);
8441 Assoc1 : List_Id := Generic_Associations (N1);
8442 Assoc2 : List_Id := Generic_Associations (N2);
8446 Gen_Id : Entity_Id := Entity (Name (N2));
8452 if Present (Assoc1) then
8453 Act1 := First (Assoc1);
8456 Set_Generic_Associations (N1, New_List);
8457 Assoc1 := Generic_Associations (N1);
8460 if Present (Assoc2) then
8461 Act2 := First (Assoc2);
8466 while Present (Act1) and then Present (Act2) loop
8471 -- Find the associations added for default suprograms.
8473 if Present (Act2) then
8474 while Nkind (Act2) /= N_Generic_Association
8475 or else No (Entity (Selector_Name (Act2)))
8476 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
8481 -- Add a similar association if the default is global. The
8482 -- renaming declaration for the actual has been analyzed, and
8483 -- its alias is the program it renames. Link the actual in the
8484 -- original generic tree with the node in the analyzed tree.
8486 while Present (Act2) loop
8487 Subp := Entity (Selector_Name (Act2));
8488 Def := Explicit_Generic_Actual_Parameter (Act2);
8490 -- Following test is defence against rubbish errors
8492 if No (Alias (Subp)) then
8496 -- Retrieve the resolved actual from the renaming declaration
8497 -- created for the instantiated formal.
8499 Actual := Entity (Name (Parent (Parent (Subp))));
8500 Set_Entity (Def, Actual);
8501 Set_Etype (Def, Etype (Actual));
8503 if Is_Global (Actual) then
8505 Make_Generic_Association (Loc,
8506 Selector_Name => New_Occurrence_Of (Subp, Loc),
8507 Explicit_Generic_Actual_Parameter =>
8508 New_Occurrence_Of (Actual, Loc));
8511 (Explicit_Generic_Actual_Parameter (Ndec), Def);
8513 Append (Ndec, Assoc1);
8515 -- If there are other defaults, add a dummy association
8516 -- in case there are other defaulted formals with the same
8519 elsif Present (Next (Act2)) then
8521 Make_Generic_Association (Loc,
8522 Selector_Name => New_Occurrence_Of (Subp, Loc),
8523 Explicit_Generic_Actual_Parameter => Empty);
8525 Append (Ndec, Assoc1);
8532 if Nkind (Name (N1)) = N_Identifier
8533 and then Is_Child_Unit (Gen_Id)
8534 and then Is_Global (Gen_Id)
8535 and then Is_Generic_Unit (Scope (Gen_Id))
8536 and then In_Open_Scopes (Scope (Gen_Id))
8538 -- This is an instantiation of a child unit within a sibling,
8539 -- so that the generic parent is in scope. An eventual instance
8540 -- must occur within the scope of an instance of the parent.
8541 -- Make name in instance into an expanded name, to preserve the
8542 -- identifier of the parent, so it can be resolved subsequently.
8545 Make_Expanded_Name (Loc,
8546 Chars => Chars (Gen_Id),
8547 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
8548 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
8549 Set_Entity (Name (N2), Gen_Id);
8552 Make_Expanded_Name (Loc,
8553 Chars => Chars (Gen_Id),
8554 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
8555 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
8557 Set_Associated_Node (Name (N1), Name (N2));
8558 Set_Associated_Node (Prefix (Name (N1)), Empty);
8560 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
8561 Set_Etype (Name (N1), Etype (Gen_Id));
8564 end Save_Global_Defaults;
8566 ----------------------------
8567 -- Save_Global_Descendant --
8568 ----------------------------
8570 procedure Save_Global_Descendant (D : Union_Id) is
8574 if D in Node_Range then
8575 if D = Union_Id (Empty) then
8578 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
8579 Save_References (Node_Id (D));
8582 elsif D in List_Range then
8583 if D = Union_Id (No_List)
8584 or else Is_Empty_List (List_Id (D))
8589 N1 := First (List_Id (D));
8590 while Present (N1) loop
8591 Save_References (N1);
8596 -- Element list or other non-node field, nothing to do
8601 end Save_Global_Descendant;
8603 -------------------------------------
8604 -- Save_Global_Operand_Descendants --
8605 -------------------------------------
8607 procedure Save_Global_Operand_Descendants (N : Node_Id) is
8609 use Atree.Unchecked_Access;
8610 -- This code section is part of the implementation of an untyped
8611 -- tree traversal, so it needs direct access to node fields.
8614 Save_Global_Descendant (Field2 (N));
8615 Save_Global_Descendant (Field3 (N));
8616 end Save_Global_Operand_Descendants;
8618 ---------------------
8619 -- Save_References --
8620 ---------------------
8622 -- This is the recursive procedure that does the work, once the
8623 -- enclosing generic scope has been established. We have to treat
8624 -- specially a number of node rewritings that are required by semantic
8625 -- processing and which change the kind of nodes in the generic copy:
8626 -- typically constant-folding, replacing an operator node by a string
8627 -- literal, or a selected component by an expanded name. In each of
8628 -- those cases, the transformation is propagated to the generic unit.
8630 procedure Save_References (N : Node_Id) is
8635 elsif (Nkind (N) = N_Character_Literal
8636 or else Nkind (N) = N_Operator_Symbol)
8638 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
8641 elsif Nkind (N) = N_Operator_Symbol
8642 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
8644 Change_Operator_Symbol_To_String_Literal (N);
8647 elsif Nkind (N) in N_Op then
8649 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
8651 if Nkind (N) = N_Op_Concat then
8652 Set_Is_Component_Left_Opnd (N,
8653 Is_Component_Left_Opnd (Get_Associated_Node (N)));
8655 Set_Is_Component_Right_Opnd (N,
8656 Is_Component_Right_Opnd (Get_Associated_Node (N)));
8661 -- Node may be transformed into call to a user-defined operator
8663 N2 := Get_Associated_Node (N);
8665 if Nkind (N2) = N_Function_Call then
8666 E := Entity (Name (N2));
8669 and then Is_Global (E)
8671 Set_Etype (N, Etype (N2));
8673 Set_Associated_Node (N, Empty);
8674 Set_Etype (N, Empty);
8677 elsif Nkind (N2) = N_Integer_Literal
8678 or else Nkind (N2) = N_Real_Literal
8679 or else Nkind (N2) = N_String_Literal
8680 or else (Nkind (N2) = N_Identifier
8682 Ekind (Entity (N2)) = E_Enumeration_Literal)
8684 -- Operation was constant-folded, perform the same
8685 -- replacement in generic.
8687 -- Note: we do a Replace here rather than a Rewrite,
8688 -- which is a definite violation of the standard rules
8689 -- with regard to retrievability of the original tree,
8690 -- and likely ASIS bugs or at least irregularities are
8691 -- caused by this choice.
8693 -- The reason we do this is that the appropriate original
8694 -- nodes are never constructed (we don't go applying the
8695 -- generic instantiation to rewritten nodes in general).
8696 -- We could try to create an appropriate copy but it would
8697 -- be hard work and does not seem worth while, because
8698 -- the original expression is accessible in the generic,
8699 -- and ASIS rules for traversing instances are fuzzy.
8701 Replace (N, New_Copy (N2));
8702 Set_Analyzed (N, False);
8706 -- Complete the check on operands
8708 Save_Global_Operand_Descendants (N);
8710 elsif Nkind (N) = N_Identifier then
8711 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
8713 -- If this is a discriminant reference, always save it.
8714 -- It is used in the instance to find the corresponding
8715 -- discriminant positionally rather than by name.
8717 Set_Original_Discriminant
8718 (N, Original_Discriminant (Get_Associated_Node (N)));
8722 N2 := Get_Associated_Node (N);
8724 if Nkind (N2) = N_Function_Call then
8725 E := Entity (Name (N2));
8727 -- Name resolves to a call to parameterless function.
8728 -- If original entity is global, mark node as resolved.
8731 and then Is_Global (E)
8733 Set_Etype (N, Etype (N2));
8735 Set_Associated_Node (N, Empty);
8736 Set_Etype (N, Empty);
8740 Nkind (N2) = N_Integer_Literal or else
8741 Nkind (N2) = N_Real_Literal or else
8742 Nkind (N2) = N_String_Literal
8744 -- Name resolves to named number that is constant-folded,
8745 -- or to string literal from concatenation.
8746 -- Perform the same replacement in generic.
8748 Rewrite (N, New_Copy (N2));
8749 Set_Analyzed (N, False);
8751 elsif Nkind (N2) = N_Explicit_Dereference then
8753 -- An identifier is rewritten as a dereference if it is
8754 -- the prefix in a selected component, and it denotes an
8755 -- access to a composite type, or a parameterless function
8756 -- call that returns an access type.
8758 -- Check whether corresponding entity in prefix is global.
8760 if Is_Entity_Name (Prefix (N2))
8761 and then Present (Entity (Prefix (N2)))
8762 and then Is_Global (Entity (Prefix (N2)))
8765 Make_Explicit_Dereference (Sloc (N),
8766 Prefix => Make_Identifier (Sloc (N),
8767 Chars => Chars (N))));
8768 Set_Associated_Node (Prefix (N), Prefix (N2));
8770 elsif Nkind (Prefix (N2)) = N_Function_Call
8771 and then Is_Global (Entity (Name (Prefix (N2))))
8774 Make_Explicit_Dereference (Sloc (N),
8775 Prefix => Make_Function_Call (Sloc (N),
8777 Make_Identifier (Sloc (N),
8778 Chars => Chars (N)))));
8781 (Name (Prefix (N)), Name (Prefix (N2)));
8784 Set_Associated_Node (N, Empty);
8785 Set_Etype (N, Empty);
8788 -- The subtype mark of a nominally unconstrained object
8789 -- is rewritten as a subtype indication using the bounds
8790 -- of the expression. Recover the original subtype mark.
8792 elsif Nkind (N2) = N_Subtype_Indication
8793 and then Is_Entity_Name (Original_Node (N2))
8795 Set_Associated_Node (N, Original_Node (N2));
8803 elsif Nkind (N) in N_Entity then
8808 use Atree.Unchecked_Access;
8809 -- This code section is part of implementing an untyped tree
8810 -- traversal, so it needs direct access to node fields.
8813 if Nkind (N) = N_Aggregate
8815 Nkind (N) = N_Extension_Aggregate
8817 N2 := Get_Associated_Node (N);
8820 or else No (Etype (N2))
8821 or else not Is_Global (Etype (N2))
8823 Set_Associated_Node (N, Empty);
8826 Save_Global_Descendant (Field1 (N));
8827 Save_Global_Descendant (Field2 (N));
8828 Save_Global_Descendant (Field3 (N));
8829 Save_Global_Descendant (Field5 (N));
8831 -- All other cases than aggregates
8834 Save_Global_Descendant (Field1 (N));
8835 Save_Global_Descendant (Field2 (N));
8836 Save_Global_Descendant (Field3 (N));
8837 Save_Global_Descendant (Field4 (N));
8838 Save_Global_Descendant (Field5 (N));
8842 end Save_References;
8844 -- Start of processing for Save_Global_References
8847 Gen_Scope := Current_Scope;
8849 -- If the generic unit is a child unit, references to entities in
8850 -- the parent are treated as local, because they will be resolved
8851 -- anew in the context of the instance of the parent.
8853 while Is_Child_Unit (Gen_Scope)
8854 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
8856 Gen_Scope := Scope (Gen_Scope);
8859 Save_References (N);
8860 end Save_Global_References;
8862 ---------------------
8863 -- Set_Copied_Sloc --
8864 ---------------------
8866 procedure Set_Copied_Sloc (N : Node_Id; E : Entity_Id) is
8868 Create_Instantiation_Source (N, E, S_Adjustment);
8869 end Set_Copied_Sloc;
8871 ---------------------
8872 -- Set_Instance_Of --
8873 ---------------------
8875 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
8877 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
8878 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
8879 Generic_Renamings.Increment_Last;
8880 end Set_Instance_Of;
8882 --------------------
8883 -- Set_Next_Assoc --
8884 --------------------
8886 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
8888 Generic_Renamings.Table (E).Next_In_HTable := Next;
8895 procedure Start_Generic is
8897 -- ??? I am sure more things could be factored out in this
8898 -- routine. Should probably be done at a later stage.
8900 Generic_Flags.Increment_Last;
8901 Generic_Flags.Table (Generic_Flags.Last) := Inside_A_Generic;
8902 Inside_A_Generic := True;
8904 Expander_Mode_Save_And_Set (False);
8911 procedure Switch_View (T : Entity_Id) is
8912 Priv_Elmt : Elmt_Id := No_Elmt;
8913 Priv_Sub : Entity_Id;
8914 BT : Entity_Id := Base_Type (T);
8917 -- T may be private but its base type may have been exchanged through
8918 -- some other occurrence, in which case there is nothing to switch.
8920 if not Is_Private_Type (BT) then
8924 Priv_Elmt := First_Elmt (Private_Dependents (BT));
8926 if Present (Full_View (BT)) then
8927 Append_Elmt (Full_View (BT), Exchanged_Views);
8928 Exchange_Declarations (BT);
8931 while Present (Priv_Elmt) loop
8932 Priv_Sub := (Node (Priv_Elmt));
8934 -- We avoid flipping the subtype if the Etype of its full
8935 -- view is private because this would result in a malformed
8936 -- subtype. This occurs when the Etype of the subtype full
8937 -- view is the full view of the base type (and since the
8938 -- base types were just switched, the subtype is pointing
8939 -- to the wrong view). This is currently the case for
8940 -- tagged record types, access types (maybe more?) and
8941 -- needs to be resolved. ???
8943 if Present (Full_View (Priv_Sub))
8944 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
8946 Append_Elmt (Full_View (Priv_Sub), Exchanged_Views);
8947 Exchange_Declarations (Priv_Sub);
8950 Next_Elmt (Priv_Elmt);
8954 -----------------------------
8955 -- Valid_Default_Attribute --
8956 -----------------------------
8958 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
8959 Attr_Id : constant Attribute_Id :=
8960 Get_Attribute_Id (Attribute_Name (Def));
8963 T : Entity_Id := Entity (Prefix (Def));
8965 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
8975 F := First_Formal (Nam);
8976 while Present (F) loop
8982 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
8983 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
8984 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
8985 Attribute_Unbiased_Rounding =>
8986 OK := (Is_Fun and then Num_F = 1 and then Is_Floating_Point_Type (T));
8988 when Attribute_Image | Attribute_Pred | Attribute_Succ |
8989 Attribute_Value | Attribute_Wide_Image |
8990 Attribute_Wide_Value =>
8991 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
8993 when Attribute_Max | Attribute_Min =>
8994 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
8996 when Attribute_Input =>
8997 OK := (Is_Fun and then Num_F = 1);
8999 when Attribute_Output | Attribute_Read | Attribute_Write =>
9000 OK := (not Is_Fun and then Num_F = 2);
9002 when others => OK := False;
9006 Error_Msg_N ("attribute reference has wrong profile for subprogram",
9009 end Valid_Default_Attribute;