1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2018, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects
; use Aspects
;
27 with Atree
; use Atree
;
28 with Contracts
; use Contracts
;
29 with Einfo
; use Einfo
;
30 with Elists
; use Elists
;
31 with Errout
; use Errout
;
32 with Expander
; use Expander
;
33 with Exp_Disp
; use Exp_Disp
;
34 with Fname
; use Fname
;
35 with Fname
.UF
; use Fname
.UF
;
36 with Freeze
; use Freeze
;
37 with Ghost
; use Ghost
;
38 with Itypes
; use Itypes
;
40 with Lib
.Load
; use Lib
.Load
;
41 with Lib
.Xref
; use Lib
.Xref
;
42 with Nlists
; use Nlists
;
43 with Namet
; use Namet
;
44 with Nmake
; use Nmake
;
46 with Rident
; use Rident
;
47 with Restrict
; use Restrict
;
48 with Rtsfind
; use Rtsfind
;
50 with Sem_Aux
; use Sem_Aux
;
51 with Sem_Cat
; use Sem_Cat
;
52 with Sem_Ch3
; use Sem_Ch3
;
53 with Sem_Ch6
; use Sem_Ch6
;
54 with Sem_Ch7
; use Sem_Ch7
;
55 with Sem_Ch8
; use Sem_Ch8
;
56 with Sem_Ch10
; use Sem_Ch10
;
57 with Sem_Ch13
; use Sem_Ch13
;
58 with Sem_Dim
; use Sem_Dim
;
59 with Sem_Disp
; use Sem_Disp
;
60 with Sem_Elab
; use Sem_Elab
;
61 with Sem_Elim
; use Sem_Elim
;
62 with Sem_Eval
; use Sem_Eval
;
63 with Sem_Prag
; use Sem_Prag
;
64 with Sem_Res
; use Sem_Res
;
65 with Sem_Type
; use Sem_Type
;
66 with Sem_Util
; use Sem_Util
;
67 with Sem_Warn
; use Sem_Warn
;
68 with Stand
; use Stand
;
69 with Sinfo
; use Sinfo
;
70 with Sinfo
.CN
; use Sinfo
.CN
;
71 with Sinput
; use Sinput
;
72 with Sinput
.L
; use Sinput
.L
;
73 with Snames
; use Snames
;
74 with Stringt
; use Stringt
;
75 with Uname
; use Uname
;
77 with Tbuild
; use Tbuild
;
78 with Uintp
; use Uintp
;
79 with Urealp
; use Urealp
;
80 with Warnsw
; use Warnsw
;
84 package body Sem_Ch12
is
86 ----------------------------------------------------------
87 -- Implementation of Generic Analysis and Instantiation --
88 ----------------------------------------------------------
90 -- GNAT implements generics by macro expansion. No attempt is made to share
91 -- generic instantiations (for now). Analysis of a generic definition does
92 -- not perform any expansion action, but the expander must be called on the
93 -- tree for each instantiation, because the expansion may of course depend
94 -- on the generic actuals. All of this is best achieved as follows:
96 -- a) Semantic analysis of a generic unit is performed on a copy of the
97 -- tree for the generic unit. All tree modifications that follow analysis
98 -- do not affect the original tree. Links are kept between the original
99 -- tree and the copy, in order to recognize non-local references within
100 -- the generic, and propagate them to each instance (recall that name
101 -- resolution is done on the generic declaration: generics are not really
102 -- macros). This is summarized in the following diagram:
104 -- .-----------. .----------.
105 -- | semantic |<--------------| generic |
107 -- | |==============>| |
108 -- |___________| global |__________|
119 -- b) Each instantiation copies the original tree, and inserts into it a
120 -- series of declarations that describe the mapping between generic formals
121 -- and actuals. For example, a generic In OUT parameter is an object
122 -- renaming of the corresponding actual, etc. Generic IN parameters are
123 -- constant declarations.
125 -- c) In order to give the right visibility for these renamings, we use
126 -- a different scheme for package and subprogram instantiations. For
127 -- packages, the list of renamings is inserted into the package
128 -- specification, before the visible declarations of the package. The
129 -- renamings are analyzed before any of the text of the instance, and are
130 -- thus visible at the right place. Furthermore, outside of the instance,
131 -- the generic parameters are visible and denote their corresponding
134 -- For subprograms, we create a container package to hold the renamings
135 -- and the subprogram instance itself. Analysis of the package makes the
136 -- renaming declarations visible to the subprogram. After analyzing the
137 -- package, the defining entity for the subprogram is touched-up so that
138 -- it appears declared in the current scope, and not inside the container
141 -- If the instantiation is a compilation unit, the container package is
142 -- given the same name as the subprogram instance. This ensures that
143 -- the elaboration procedure called by the binder, using the compilation
144 -- unit name, calls in fact the elaboration procedure for the package.
146 -- Not surprisingly, private types complicate this approach. By saving in
147 -- the original generic object the non-local references, we guarantee that
148 -- the proper entities are referenced at the point of instantiation.
149 -- However, for private types, this by itself does not insure that the
150 -- proper VIEW of the entity is used (the full type may be visible at the
151 -- point of generic definition, but not at instantiation, or vice-versa).
152 -- In order to reference the proper view, we special-case any reference
153 -- to private types in the generic object, by saving both views, one in
154 -- the generic and one in the semantic copy. At time of instantiation, we
155 -- check whether the two views are consistent, and exchange declarations if
156 -- necessary, in order to restore the correct visibility. Similarly, if
157 -- the instance view is private when the generic view was not, we perform
158 -- the exchange. After completing the instantiation, we restore the
159 -- current visibility. The flag Has_Private_View marks identifiers in the
160 -- the generic unit that require checking.
162 -- Visibility within nested generic units requires special handling.
163 -- Consider the following scheme:
165 -- type Global is ... -- outside of generic unit.
169 -- type Semi_Global is ... -- global to inner.
172 -- procedure inner (X1 : Global; X2 : Semi_Global);
174 -- procedure in2 is new inner (...); -- 4
177 -- package New_Outer is new Outer (...); -- 2
178 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
180 -- The semantic analysis of Outer captures all occurrences of Global.
181 -- The semantic analysis of Inner (at 1) captures both occurrences of
182 -- Global and Semi_Global.
184 -- At point 2 (instantiation of Outer), we also produce a generic copy
185 -- of Inner, even though Inner is, at that point, not being instantiated.
186 -- (This is just part of the semantic analysis of New_Outer).
188 -- Critically, references to Global within Inner must be preserved, while
189 -- references to Semi_Global should not preserved, because they must now
190 -- resolve to an entity within New_Outer. To distinguish between these, we
191 -- use a global variable, Current_Instantiated_Parent, which is set when
192 -- performing a generic copy during instantiation (at 2). This variable is
193 -- used when performing a generic copy that is not an instantiation, but
194 -- that is nested within one, as the occurrence of 1 within 2. The analysis
195 -- of a nested generic only preserves references that are global to the
196 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
197 -- determine whether a reference is external to the given parent.
199 -- The instantiation at point 3 requires no special treatment. The method
200 -- works as well for further nestings of generic units, but of course the
201 -- variable Current_Instantiated_Parent must be stacked because nested
202 -- instantiations can occur, e.g. the occurrence of 4 within 2.
204 -- The instantiation of package and subprogram bodies is handled in a
205 -- similar manner, except that it is delayed until after semantic
206 -- analysis is complete. In this fashion complex cross-dependencies
207 -- between several package declarations and bodies containing generics
208 -- can be compiled which otherwise would diagnose spurious circularities.
210 -- For example, it is possible to compile two packages A and B that
211 -- have the following structure:
213 -- package A is package B is
214 -- generic ... generic ...
215 -- package G_A is package G_B is
218 -- package body A is package body B is
219 -- package N_B is new G_B (..) package N_A is new G_A (..)
221 -- The table Pending_Instantiations in package Inline is used to keep
222 -- track of body instantiations that are delayed in this manner. Inline
223 -- handles the actual calls to do the body instantiations. This activity
224 -- is part of Inline, since the processing occurs at the same point, and
225 -- for essentially the same reason, as the handling of inlined routines.
227 ----------------------------------------------
228 -- Detection of Instantiation Circularities --
229 ----------------------------------------------
231 -- If we have a chain of instantiations that is circular, this is static
232 -- error which must be detected at compile time. The detection of these
233 -- circularities is carried out at the point that we insert a generic
234 -- instance spec or body. If there is a circularity, then the analysis of
235 -- the offending spec or body will eventually result in trying to load the
236 -- same unit again, and we detect this problem as we analyze the package
237 -- instantiation for the second time.
239 -- At least in some cases after we have detected the circularity, we get
240 -- into trouble if we try to keep going. The following flag is set if a
241 -- circularity is detected, and used to abandon compilation after the
242 -- messages have been posted.
244 -----------------------------------------
245 -- Implementation of Generic Contracts --
246 -----------------------------------------
248 -- A "contract" is a collection of aspects and pragmas that either verify a
249 -- property of a construct at runtime or classify the data flow to and from
250 -- the construct in some fashion.
252 -- Generic packages, subprograms and their respective bodies may be subject
253 -- to the following contract-related aspects or pragmas collectively known
256 -- package subprogram [body]
257 -- Abstract_State Contract_Cases
258 -- Initial_Condition Depends
259 -- Initializes Extensions_Visible
262 -- Refined_State Post_Class
272 -- Most package contract annotations utilize forward references to classify
273 -- data declared within the package [body]. Subprogram annotations then use
274 -- the classifications to further refine them. These inter dependencies are
275 -- problematic with respect to the implementation of generics because their
276 -- analysis, capture of global references and instantiation does not mesh
277 -- well with the existing mechanism.
279 -- 1) Analysis of generic contracts is carried out the same way non-generic
280 -- contracts are analyzed:
282 -- 1.1) General rule - a contract is analyzed after all related aspects
283 -- and pragmas are analyzed. This is done by routines
285 -- Analyze_Package_Body_Contract
286 -- Analyze_Package_Contract
287 -- Analyze_Subprogram_Body_Contract
288 -- Analyze_Subprogram_Contract
290 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
293 -- 1.3) Compilation unit body - the contract is analyzed at the end of
294 -- the body declaration list.
296 -- 1.4) Package - the contract is analyzed at the end of the private or
297 -- visible declarations, prior to analyzing the contracts of any nested
298 -- packages or subprograms.
300 -- 1.5) Package body - the contract is analyzed at the end of the body
301 -- declaration list, prior to analyzing the contracts of any nested
302 -- packages or subprograms.
304 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
305 -- package or a subprogram, then its contract is analyzed at the end of
306 -- the enclosing declarations, otherwise the subprogram is a compilation
309 -- 1.7) Subprogram body - if the subprogram body is declared inside a
310 -- block, a package body or a subprogram body, then its contract is
311 -- analyzed at the end of the enclosing declarations, otherwise the
312 -- subprogram is a compilation unit 1.3).
314 -- 2) Capture of global references within contracts is done after capturing
315 -- global references within the generic template. There are two reasons for
316 -- this delay - pragma annotations are not part of the generic template in
317 -- the case of a generic subprogram declaration, and analysis of contracts
320 -- Contract-related source pragmas within generic templates are prepared
321 -- for delayed capture of global references by routine
323 -- Create_Generic_Contract
325 -- The routine associates these pragmas with the contract of the template.
326 -- In the case of a generic subprogram declaration, the routine creates
327 -- generic templates for the pragmas declared after the subprogram because
328 -- they are not part of the template.
330 -- generic -- template starts
331 -- procedure Gen_Proc (Input : Integer); -- template ends
332 -- pragma Precondition (Input > 0); -- requires own template
334 -- 2.1) The capture of global references with aspect specifications and
335 -- source pragmas that apply to a generic unit must be suppressed when
336 -- the generic template is being processed because the contracts have not
337 -- been analyzed yet. Any attempts to capture global references at that
338 -- point will destroy the Associated_Node linkages and leave the template
339 -- undecorated. This delay is controlled by routine
341 -- Requires_Delayed_Save
343 -- 2.2) The real capture of global references within a contract is done
344 -- after the contract has been analyzed, by routine
346 -- Save_Global_References_In_Contract
348 -- 3) The instantiation of a generic contract occurs as part of the
349 -- instantiation of the contract owner. Generic subprogram declarations
350 -- require additional processing when the contract is specified by pragmas
351 -- because the pragmas are not part of the generic template. This is done
354 -- Instantiate_Subprogram_Contract
356 Circularity_Detected
: Boolean := False;
357 -- This should really be reset on encountering a new main unit, but in
358 -- practice we are not using multiple main units so it is not critical.
360 --------------------------------------------------
361 -- Formal packages and partial parameterization --
362 --------------------------------------------------
364 -- When compiling a generic, a formal package is a local instantiation. If
365 -- declared with a box, its generic formals are visible in the enclosing
366 -- generic. If declared with a partial list of actuals, those actuals that
367 -- are defaulted (covered by an Others clause, or given an explicit box
368 -- initialization) are also visible in the enclosing generic, while those
369 -- that have a corresponding actual are not.
371 -- In our source model of instantiation, the same visibility must be
372 -- present in the spec and body of an instance: the names of the formals
373 -- that are defaulted must be made visible within the instance, and made
374 -- invisible (hidden) after the instantiation is complete, so that they
375 -- are not accessible outside of the instance.
377 -- In a generic, a formal package is treated like a special instantiation.
378 -- Our Ada 95 compiler handled formals with and without box in different
379 -- ways. With partial parameterization, we use a single model for both.
380 -- We create a package declaration that consists of the specification of
381 -- the generic package, and a set of declarations that map the actuals
382 -- into local renamings, just as we do for bona fide instantiations. For
383 -- defaulted parameters and formals with a box, we copy directly the
384 -- declarations of the formal into this local package. The result is a
385 -- a package whose visible declarations may include generic formals. This
386 -- package is only used for type checking and visibility analysis, and
387 -- never reaches the back-end, so it can freely violate the placement
388 -- rules for generic formal declarations.
390 -- The list of declarations (renamings and copies of formals) is built
391 -- by Analyze_Associations, just as for regular instantiations.
393 -- At the point of instantiation, conformance checking must be applied only
394 -- to those parameters that were specified in the formal. We perform this
395 -- checking by creating another internal instantiation, this one including
396 -- only the renamings and the formals (the rest of the package spec is not
397 -- relevant to conformance checking). We can then traverse two lists: the
398 -- list of actuals in the instance that corresponds to the formal package,
399 -- and the list of actuals produced for this bogus instantiation. We apply
400 -- the conformance rules to those actuals that are not defaulted (i.e.
401 -- which still appear as generic formals.
403 -- When we compile an instance body we must make the right parameters
404 -- visible again. The predicate Is_Generic_Formal indicates which of the
405 -- formals should have its Is_Hidden flag reset.
407 -----------------------
408 -- Local subprograms --
409 -----------------------
411 procedure Abandon_Instantiation
(N
: Node_Id
);
412 pragma No_Return
(Abandon_Instantiation
);
413 -- Posts an error message "instantiation abandoned" at the indicated node
414 -- and then raises the exception Instantiation_Error to do it.
416 procedure Analyze_Formal_Array_Type
417 (T
: in out Entity_Id
;
419 -- A formal array type is treated like an array type declaration, and
420 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
421 -- in-out, because in the case of an anonymous type the entity is
422 -- actually created in the procedure.
424 -- The following procedures treat other kinds of formal parameters
426 procedure Analyze_Formal_Derived_Interface_Type
431 procedure Analyze_Formal_Derived_Type
436 procedure Analyze_Formal_Interface_Type
441 -- The following subprograms create abbreviated declarations for formal
442 -- scalar types. We introduce an anonymous base of the proper class for
443 -- each of them, and define the formals as constrained first subtypes of
444 -- their bases. The bounds are expressions that are non-static in the
447 procedure Analyze_Formal_Decimal_Fixed_Point_Type
448 (T
: Entity_Id
; Def
: Node_Id
);
449 procedure Analyze_Formal_Discrete_Type
(T
: Entity_Id
; Def
: Node_Id
);
450 procedure Analyze_Formal_Floating_Type
(T
: Entity_Id
; Def
: Node_Id
);
451 procedure Analyze_Formal_Signed_Integer_Type
(T
: Entity_Id
; Def
: Node_Id
);
452 procedure Analyze_Formal_Modular_Type
(T
: Entity_Id
; Def
: Node_Id
);
453 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
454 (T
: Entity_Id
; Def
: Node_Id
);
456 procedure Analyze_Formal_Private_Type
460 -- Creates a new private type, which does not require completion
462 procedure Analyze_Formal_Incomplete_Type
(T
: Entity_Id
; Def
: Node_Id
);
463 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
465 procedure Analyze_Generic_Formal_Part
(N
: Node_Id
);
466 -- Analyze generic formal part
468 procedure Analyze_Generic_Access_Type
(T
: Entity_Id
; Def
: Node_Id
);
469 -- Create a new access type with the given designated type
471 function Analyze_Associations
474 F_Copy
: List_Id
) return List_Id
;
475 -- At instantiation time, build the list of associations between formals
476 -- and actuals. Each association becomes a renaming declaration for the
477 -- formal entity. F_Copy is the analyzed list of formals in the generic
478 -- copy. It is used to apply legality checks to the actuals. I_Node is the
479 -- instantiation node itself.
481 procedure Analyze_Subprogram_Instantiation
485 procedure Build_Instance_Compilation_Unit_Nodes
489 -- This procedure is used in the case where the generic instance of a
490 -- subprogram body or package body is a library unit. In this case, the
491 -- original library unit node for the generic instantiation must be
492 -- replaced by the resulting generic body, and a link made to a new
493 -- compilation unit node for the generic declaration. The argument N is
494 -- the original generic instantiation. Act_Body and Act_Decl are the body
495 -- and declaration of the instance (either package body and declaration
496 -- nodes or subprogram body and declaration nodes depending on the case).
497 -- On return, the node N has been rewritten with the actual body.
499 procedure Check_Access_Definition
(N
: Node_Id
);
500 -- Subsidiary routine to null exclusion processing. Perform an assertion
501 -- check on Ada version and the presence of an access definition in N.
503 procedure Check_Formal_Packages
(P_Id
: Entity_Id
);
504 -- Apply the following to all formal packages in generic associations
506 procedure Check_Formal_Package_Instance
507 (Formal_Pack
: Entity_Id
;
508 Actual_Pack
: Entity_Id
);
509 -- Verify that the actuals of the actual instance match the actuals of
510 -- the template for a formal package that is not declared with a box.
512 procedure Check_Forward_Instantiation
(Decl
: Node_Id
);
513 -- If the generic is a local entity and the corresponding body has not
514 -- been seen yet, flag enclosing packages to indicate that it will be
515 -- elaborated after the generic body. Subprograms declared in the same
516 -- package cannot be inlined by the front end because front-end inlining
517 -- requires a strict linear order of elaboration.
519 function Check_Hidden_Primitives
(Assoc_List
: List_Id
) return Elist_Id
;
520 -- Check if some association between formals and actuals requires to make
521 -- visible primitives of a tagged type, and make those primitives visible.
522 -- Return the list of primitives whose visibility is modified (to restore
523 -- their visibility later through Restore_Hidden_Primitives). If no
524 -- candidate is found then return No_Elist.
526 procedure Check_Hidden_Child_Unit
528 Gen_Unit
: Entity_Id
;
529 Act_Decl_Id
: Entity_Id
);
530 -- If the generic unit is an implicit child instance within a parent
531 -- instance, we need to make an explicit test that it is not hidden by
532 -- a child instance of the same name and parent.
534 procedure Check_Generic_Actuals
535 (Instance
: Entity_Id
;
536 Is_Formal_Box
: Boolean);
537 -- Similar to previous one. Check the actuals in the instantiation,
538 -- whose views can change between the point of instantiation and the point
539 -- of instantiation of the body. In addition, mark the generic renamings
540 -- as generic actuals, so that they are not compatible with other actuals.
541 -- Recurse on an actual that is a formal package whose declaration has
544 function Contains_Instance_Of
547 N
: Node_Id
) return Boolean;
548 -- Inner is instantiated within the generic Outer. Check whether Inner
549 -- directly or indirectly contains an instance of Outer or of one of its
550 -- parents, in the case of a subunit. Each generic unit holds a list of
551 -- the entities instantiated within (at any depth). This procedure
552 -- determines whether the set of such lists contains a cycle, i.e. an
553 -- illegal circular instantiation.
555 function Denotes_Formal_Package
557 On_Exit
: Boolean := False;
558 Instance
: Entity_Id
:= Empty
) return Boolean;
559 -- Returns True if E is a formal package of an enclosing generic, or
560 -- the actual for such a formal in an enclosing instantiation. If such
561 -- a package is used as a formal in an nested generic, or as an actual
562 -- in a nested instantiation, the visibility of ITS formals should not
563 -- be modified. When called from within Restore_Private_Views, the flag
564 -- On_Exit is true, to indicate that the search for a possible enclosing
565 -- instance should ignore the current one. In that case Instance denotes
566 -- the declaration for which this is an actual. This declaration may be
567 -- an instantiation in the source, or the internal instantiation that
568 -- corresponds to the actual for a formal package.
570 function Earlier
(N1
, N2
: Node_Id
) return Boolean;
571 -- Yields True if N1 and N2 appear in the same compilation unit,
572 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
573 -- traversal of the tree for the unit. Used to determine the placement
574 -- of freeze nodes for instance bodies that may depend on other instances.
576 function Find_Actual_Type
578 Gen_Type
: Entity_Id
) return Entity_Id
;
579 -- When validating the actual types of a child instance, check whether
580 -- the formal is a formal type of the parent unit, and retrieve the current
581 -- actual for it. Typ is the entity in the analyzed formal type declaration
582 -- (component or index type of an array type, or designated type of an
583 -- access formal) and Gen_Type is the enclosing analyzed formal array
584 -- or access type. The desired actual may be a formal of a parent, or may
585 -- be declared in a formal package of a parent. In both cases it is a
586 -- generic actual type because it appears within a visible instance.
587 -- Finally, it may be declared in a parent unit without being a formal
588 -- of that unit, in which case it must be retrieved by visibility.
589 -- Ambiguities may still arise if two homonyms are declared in two formal
590 -- packages, and the prefix of the formal type may be needed to resolve
591 -- the ambiguity in the instance ???
593 procedure Freeze_Subprogram_Body
594 (Inst_Node
: Node_Id
;
596 Pack_Id
: Entity_Id
);
597 -- The generic body may appear textually after the instance, including
598 -- in the proper body of a stub, or within a different package instance.
599 -- Given that the instance can only be elaborated after the generic, we
600 -- place freeze_nodes for the instance and/or for packages that may enclose
601 -- the instance and the generic, so that the back-end can establish the
602 -- proper order of elaboration.
604 function Get_Associated_Node
(N
: Node_Id
) return Node_Id
;
605 -- In order to propagate semantic information back from the analyzed copy
606 -- to the original generic, we maintain links between selected nodes in the
607 -- generic and their corresponding copies. At the end of generic analysis,
608 -- the routine Save_Global_References traverses the generic tree, examines
609 -- the semantic information, and preserves the links to those nodes that
610 -- contain global information. At instantiation, the information from the
611 -- associated node is placed on the new copy, so that name resolution is
614 -- Three kinds of source nodes have associated nodes:
616 -- a) those that can reference (denote) entities, that is identifiers,
617 -- character literals, expanded_names, operator symbols, operators,
618 -- and attribute reference nodes. These nodes have an Entity field
619 -- and are the set of nodes that are in N_Has_Entity.
621 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
623 -- c) selected components (N_Selected_Component)
625 -- For the first class, the associated node preserves the entity if it is
626 -- global. If the generic contains nested instantiations, the associated
627 -- node itself has been recopied, and a chain of them must be followed.
629 -- For aggregates, the associated node allows retrieval of the type, which
630 -- may otherwise not appear in the generic. The view of this type may be
631 -- different between generic and instantiation, and the full view can be
632 -- installed before the instantiation is analyzed. For aggregates of type
633 -- extensions, the same view exchange may have to be performed for some of
634 -- the ancestor types, if their view is private at the point of
637 -- Nodes that are selected components in the parse tree may be rewritten
638 -- as expanded names after resolution, and must be treated as potential
639 -- entity holders, which is why they also have an Associated_Node.
641 -- Nodes that do not come from source, such as freeze nodes, do not appear
642 -- in the generic tree, and need not have an associated node.
644 -- The associated node is stored in the Associated_Node field. Note that
645 -- this field overlaps Entity, which is fine, because the whole point is
646 -- that we don't need or want the normal Entity field in this situation.
648 function Has_Been_Exchanged
(E
: Entity_Id
) return Boolean;
649 -- Traverse the Exchanged_Views list to see if a type was private
650 -- and has already been flipped during this phase of instantiation.
652 procedure Hide_Current_Scope
;
653 -- When instantiating a generic child unit, the parent context must be
654 -- present, but the instance and all entities that may be generated
655 -- must be inserted in the current scope. We leave the current scope
656 -- on the stack, but make its entities invisible to avoid visibility
657 -- problems. This is reversed at the end of the instantiation. This is
658 -- not done for the instantiation of the bodies, which only require the
659 -- instances of the generic parents to be in scope.
661 function In_Same_Declarative_Part
663 Inst
: Node_Id
) return Boolean;
664 -- True if the instantiation Inst and the given freeze_node F_Node appear
665 -- within the same declarative part, ignoring subunits, but with no inter-
666 -- vening subprograms or concurrent units. Used to find the proper plave
667 -- for the freeze node of an instance, when the generic is declared in a
668 -- previous instance. If predicate is true, the freeze node of the instance
669 -- can be placed after the freeze node of the previous instance, Otherwise
670 -- it has to be placed at the end of the current declarative part.
672 function In_Main_Context
(E
: Entity_Id
) return Boolean;
673 -- Check whether an instantiation is in the context of the main unit.
674 -- Used to determine whether its body should be elaborated to allow
675 -- front-end inlining.
677 procedure Inherit_Context
(Gen_Decl
: Node_Id
; Inst
: Node_Id
);
678 -- Add the context clause of the unit containing a generic unit to a
679 -- compilation unit that is, or contains, an instantiation.
682 -- Establish environment for subsequent instantiation. Separated from
683 -- Save_Env because data-structures for visibility handling must be
684 -- initialized before call to Check_Generic_Child_Unit.
686 procedure Inline_Instance_Body
688 Gen_Unit
: Entity_Id
;
690 -- If front-end inlining is requested, instantiate the package body,
691 -- and preserve the visibility of its compilation unit, to insure
692 -- that successive instantiations succeed.
694 procedure Insert_Freeze_Node_For_Instance
697 -- N denotes a package or a subprogram instantiation and F_Node is the
698 -- associated freeze node. Insert the freeze node before the first source
699 -- body which follows immediately after N. If no such body is found, the
700 -- freeze node is inserted at the end of the declarative region which
703 procedure Install_Body
708 -- If the instantiation happens textually before the body of the generic,
709 -- the instantiation of the body must be analyzed after the generic body,
710 -- and not at the point of instantiation. Such early instantiations can
711 -- happen if the generic and the instance appear in a package declaration
712 -- because the generic body can only appear in the corresponding package
713 -- body. Early instantiations can also appear if generic, instance and
714 -- body are all in the declarative part of a subprogram or entry. Entities
715 -- of packages that are early instantiations are delayed, and their freeze
716 -- node appears after the generic body. This rather complex machinery is
717 -- needed when nested instantiations are present, because the source does
718 -- not carry any indication of where the corresponding instance bodies must
719 -- be installed and frozen.
721 procedure Install_Formal_Packages
(Par
: Entity_Id
);
722 -- Install the visible part of any formal of the parent that is a formal
723 -- package. Note that for the case of a formal package with a box, this
724 -- includes the formal part of the formal package (12.7(10/2)).
726 procedure Install_Hidden_Primitives
727 (Prims_List
: in out Elist_Id
;
730 -- Remove suffix 'P' from hidden primitives of Act_T to match the
731 -- visibility of primitives of Gen_T. The list of primitives to which
732 -- the suffix is removed is added to Prims_List to restore them later.
734 procedure Install_Parent
(P
: Entity_Id
; In_Body
: Boolean := False);
735 -- When compiling an instance of a child unit the parent (which is
736 -- itself an instance) is an enclosing scope that must be made
737 -- immediately visible. This procedure is also used to install the non-
738 -- generic parent of a generic child unit when compiling its body, so
739 -- that full views of types in the parent are made visible.
741 -- The functions Instantiate_XXX perform various legality checks and build
742 -- the declarations for instantiated generic parameters. In all of these
743 -- Formal is the entity in the generic unit, Actual is the entity of
744 -- expression in the generic associations, and Analyzed_Formal is the
745 -- formal in the generic copy, which contains the semantic information to
746 -- be used to validate the actual.
748 function Instantiate_Object
751 Analyzed_Formal
: Node_Id
) return List_Id
;
753 function Instantiate_Type
756 Analyzed_Formal
: Node_Id
;
757 Actual_Decls
: List_Id
) return List_Id
;
759 function Instantiate_Formal_Subprogram
762 Analyzed_Formal
: Node_Id
) return Node_Id
;
764 function Instantiate_Formal_Package
767 Analyzed_Formal
: Node_Id
) return List_Id
;
768 -- If the formal package is declared with a box, special visibility rules
769 -- apply to its formals: they are in the visible part of the package. This
770 -- is true in the declarative region of the formal package, that is to say
771 -- in the enclosing generic or instantiation. For an instantiation, the
772 -- parameters of the formal package are made visible in an explicit step.
773 -- Furthermore, if the actual has a visible USE clause, these formals must
774 -- be made potentially use-visible as well. On exit from the enclosing
775 -- instantiation, the reverse must be done.
777 -- For a formal package declared without a box, there are conformance rules
778 -- that apply to the actuals in the generic declaration and the actuals of
779 -- the actual package in the enclosing instantiation. The simplest way to
780 -- apply these rules is to repeat the instantiation of the formal package
781 -- in the context of the enclosing instance, and compare the generic
782 -- associations of this instantiation with those of the actual package.
783 -- This internal instantiation only needs to contain the renamings of the
784 -- formals: the visible and private declarations themselves need not be
787 -- In Ada 2005, the formal package may be only partially parameterized.
788 -- In that case the visibility step must make visible those actuals whose
789 -- corresponding formals were given with a box. A final complication
790 -- involves inherited operations from formal derived types, which must
791 -- be visible if the type is.
793 function Is_In_Main_Unit
(N
: Node_Id
) return Boolean;
794 -- Test if given node is in the main unit
796 procedure Load_Parent_Of_Generic
799 Body_Optional
: Boolean := False);
800 -- If the generic appears in a separate non-generic library unit, load the
801 -- corresponding body to retrieve the body of the generic. N is the node
802 -- for the generic instantiation, Spec is the generic package declaration.
804 -- Body_Optional is a flag that indicates that the body is being loaded to
805 -- ensure that temporaries are generated consistently when there are other
806 -- instances in the current declarative part that precede the one being
807 -- loaded. In that case a missing body is acceptable.
809 procedure Map_Formal_Package_Entities
(Form
: Entity_Id
; Act
: Entity_Id
);
810 -- Within the generic part, entities in the formal package are
811 -- visible. To validate subsequent type declarations, indicate
812 -- the correspondence between the entities in the analyzed formal,
813 -- and the entities in the actual package. There are three packages
814 -- involved in the instantiation of a formal package: the parent
815 -- generic P1 which appears in the generic declaration, the fake
816 -- instantiation P2 which appears in the analyzed generic, and whose
817 -- visible entities may be used in subsequent formals, and the actual
818 -- P3 in the instance. To validate subsequent formals, me indicate
819 -- that the entities in P2 are mapped into those of P3. The mapping of
820 -- entities has to be done recursively for nested packages.
822 procedure Move_Freeze_Nodes
826 -- Freeze nodes can be generated in the analysis of a generic unit, but
827 -- will not be seen by the back-end. It is necessary to move those nodes
828 -- to the enclosing scope if they freeze an outer entity. We place them
829 -- at the end of the enclosing generic package, which is semantically
832 procedure Preanalyze_Actuals
(N
: Node_Id
; Inst
: Entity_Id
:= Empty
);
833 -- Analyze actuals to perform name resolution. Full resolution is done
834 -- later, when the expected types are known, but names have to be captured
835 -- before installing parents of generics, that are not visible for the
836 -- actuals themselves.
838 -- If Inst is present, it is the entity of the package instance. This
839 -- entity is marked as having a limited_view actual when some actual is
840 -- a limited view. This is used to place the instance body properly.
842 procedure Provide_Completing_Bodies
(N
: Node_Id
);
843 -- Generate completing bodies for all subprograms found within package or
844 -- subprogram declaration N.
846 procedure Remove_Parent
(In_Body
: Boolean := False);
847 -- Reverse effect after instantiation of child is complete
849 procedure Restore_Hidden_Primitives
(Prims_List
: in out Elist_Id
);
850 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
853 procedure Set_Instance_Env
854 (Gen_Unit
: Entity_Id
;
855 Act_Unit
: Entity_Id
);
856 -- Save current instance on saved environment, to be used to determine
857 -- the global status of entities in nested instances. Part of Save_Env.
858 -- called after verifying that the generic unit is legal for the instance,
859 -- The procedure also examines whether the generic unit is a predefined
860 -- unit, in order to set configuration switches accordingly. As a result
861 -- the procedure must be called after analyzing and freezing the actuals.
863 procedure Set_Instance_Of
(A
: Entity_Id
; B
: Entity_Id
);
864 -- Associate analyzed generic parameter with corresponding instance. Used
865 -- for semantic checks at instantiation time.
867 function True_Parent
(N
: Node_Id
) return Node_Id
;
868 -- For a subunit, return parent of corresponding stub, else return
871 procedure Valid_Default_Attribute
(Nam
: Entity_Id
; Def
: Node_Id
);
872 -- Verify that an attribute that appears as the default for a formal
873 -- subprogram is a function or procedure with the correct profile.
875 -------------------------------------------
876 -- Data Structures for Generic Renamings --
877 -------------------------------------------
879 -- The map Generic_Renamings associates generic entities with their
880 -- corresponding actuals. Currently used to validate type instances. It
881 -- will eventually be used for all generic parameters to eliminate the
882 -- need for overload resolution in the instance.
884 type Assoc_Ptr
is new Int
;
886 Assoc_Null
: constant Assoc_Ptr
:= -1;
891 Next_In_HTable
: Assoc_Ptr
;
894 package Generic_Renamings
is new Table
.Table
895 (Table_Component_Type
=> Assoc
,
896 Table_Index_Type
=> Assoc_Ptr
,
897 Table_Low_Bound
=> 0,
899 Table_Increment
=> 100,
900 Table_Name
=> "Generic_Renamings");
902 -- Variable to hold enclosing instantiation. When the environment is
903 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
905 Current_Instantiated_Parent
: Assoc
:= (Empty
, Empty
, Assoc_Null
);
907 -- Hash table for associations
909 HTable_Size
: constant := 37;
910 type HTable_Range
is range 0 .. HTable_Size
- 1;
912 procedure Set_Next_Assoc
(E
: Assoc_Ptr
; Next
: Assoc_Ptr
);
913 function Next_Assoc
(E
: Assoc_Ptr
) return Assoc_Ptr
;
914 function Get_Gen_Id
(E
: Assoc_Ptr
) return Entity_Id
;
915 function Hash
(F
: Entity_Id
) return HTable_Range
;
917 package Generic_Renamings_HTable
is new GNAT
.HTable
.Static_HTable
(
918 Header_Num
=> HTable_Range
,
920 Elmt_Ptr
=> Assoc_Ptr
,
921 Null_Ptr
=> Assoc_Null
,
922 Set_Next
=> Set_Next_Assoc
,
925 Get_Key
=> Get_Gen_Id
,
929 Exchanged_Views
: Elist_Id
;
930 -- This list holds the private views that have been exchanged during
931 -- instantiation to restore the visibility of the generic declaration.
932 -- (see comments above). After instantiation, the current visibility is
933 -- reestablished by means of a traversal of this list.
935 Hidden_Entities
: Elist_Id
;
936 -- This list holds the entities of the current scope that are removed
937 -- from immediate visibility when instantiating a child unit. Their
938 -- visibility is restored in Remove_Parent.
940 -- Because instantiations can be recursive, the following must be saved
941 -- on entry and restored on exit from an instantiation (spec or body).
942 -- This is done by the two procedures Save_Env and Restore_Env. For
943 -- package and subprogram instantiations (but not for the body instances)
944 -- the action of Save_Env is done in two steps: Init_Env is called before
945 -- Check_Generic_Child_Unit, because setting the parent instances requires
946 -- that the visibility data structures be properly initialized. Once the
947 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
949 Parent_Unit_Visible
: Boolean := False;
950 -- Parent_Unit_Visible is used when the generic is a child unit, and
951 -- indicates whether the ultimate parent of the generic is visible in the
952 -- instantiation environment. It is used to reset the visibility of the
953 -- parent at the end of the instantiation (see Remove_Parent).
955 Instance_Parent_Unit
: Entity_Id
:= Empty
;
956 -- This records the ultimate parent unit of an instance of a generic
957 -- child unit and is used in conjunction with Parent_Unit_Visible to
958 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
960 type Instance_Env
is record
961 Instantiated_Parent
: Assoc
;
962 Exchanged_Views
: Elist_Id
;
963 Hidden_Entities
: Elist_Id
;
964 Current_Sem_Unit
: Unit_Number_Type
;
965 Parent_Unit_Visible
: Boolean := False;
966 Instance_Parent_Unit
: Entity_Id
:= Empty
;
967 Switches
: Config_Switches_Type
;
970 package Instance_Envs
is new Table
.Table
(
971 Table_Component_Type
=> Instance_Env
,
972 Table_Index_Type
=> Int
,
973 Table_Low_Bound
=> 0,
975 Table_Increment
=> 100,
976 Table_Name
=> "Instance_Envs");
978 procedure Restore_Private_Views
979 (Pack_Id
: Entity_Id
;
980 Is_Package
: Boolean := True);
981 -- Restore the private views of external types, and unmark the generic
982 -- renamings of actuals, so that they become compatible subtypes again.
983 -- For subprograms, Pack_Id is the package constructed to hold the
986 procedure Switch_View
(T
: Entity_Id
);
987 -- Switch the partial and full views of a type and its private
988 -- dependents (i.e. its subtypes and derived types).
990 ------------------------------------
991 -- Structures for Error Reporting --
992 ------------------------------------
994 Instantiation_Node
: Node_Id
;
995 -- Used by subprograms that validate instantiation of formal parameters
996 -- where there might be no actual on which to place the error message.
997 -- Also used to locate the instantiation node for generic subunits.
999 Instantiation_Error
: exception;
1000 -- When there is a semantic error in the generic parameter matching,
1001 -- there is no point in continuing the instantiation, because the
1002 -- number of cascaded errors is unpredictable. This exception aborts
1003 -- the instantiation process altogether.
1005 S_Adjustment
: Sloc_Adjustment
;
1006 -- Offset created for each node in an instantiation, in order to keep
1007 -- track of the source position of the instantiation in each of its nodes.
1008 -- A subsequent semantic error or warning on a construct of the instance
1009 -- points to both places: the original generic node, and the point of
1010 -- instantiation. See Sinput and Sinput.L for additional details.
1012 ------------------------------------------------------------
1013 -- Data structure for keeping track when inside a Generic --
1014 ------------------------------------------------------------
1016 -- The following table is used to save values of the Inside_A_Generic
1017 -- flag (see spec of Sem) when they are saved by Start_Generic.
1019 package Generic_Flags
is new Table
.Table
(
1020 Table_Component_Type
=> Boolean,
1021 Table_Index_Type
=> Int
,
1022 Table_Low_Bound
=> 0,
1023 Table_Initial
=> 32,
1024 Table_Increment
=> 200,
1025 Table_Name
=> "Generic_Flags");
1027 ---------------------------
1028 -- Abandon_Instantiation --
1029 ---------------------------
1031 procedure Abandon_Instantiation
(N
: Node_Id
) is
1033 Error_Msg_N
("\instantiation abandoned!", N
);
1034 raise Instantiation_Error
;
1035 end Abandon_Instantiation
;
1037 --------------------------------
1038 -- Add_Pending_Instantiation --
1039 --------------------------------
1041 procedure Add_Pending_Instantiation
(Inst
: Node_Id
; Act_Decl
: Node_Id
) is
1044 -- Add to the instantiation node and the corresponding unit declaration
1045 -- the current values of global flags to be used when analyzing the
1048 Pending_Instantiations
.Append
1049 ((Inst_Node
=> Inst
,
1050 Act_Decl
=> Act_Decl
,
1051 Expander_Status
=> Expander_Active
,
1052 Current_Sem_Unit
=> Current_Sem_Unit
,
1053 Scope_Suppress
=> Scope_Suppress
,
1054 Local_Suppress_Stack_Top
=> Local_Suppress_Stack_Top
,
1055 Version
=> Ada_Version
,
1056 Version_Pragma
=> Ada_Version_Pragma
,
1057 Warnings
=> Save_Warnings
,
1058 SPARK_Mode
=> SPARK_Mode
,
1059 SPARK_Mode_Pragma
=> SPARK_Mode_Pragma
));
1060 end Add_Pending_Instantiation
;
1062 ----------------------------------
1063 -- Adjust_Inherited_Pragma_Sloc --
1064 ----------------------------------
1066 procedure Adjust_Inherited_Pragma_Sloc
(N
: Node_Id
) is
1068 Adjust_Instantiation_Sloc
(N
, S_Adjustment
);
1069 end Adjust_Inherited_Pragma_Sloc
;
1071 --------------------------
1072 -- Analyze_Associations --
1073 --------------------------
1075 function Analyze_Associations
1078 F_Copy
: List_Id
) return List_Id
1080 Actuals_To_Freeze
: constant Elist_Id
:= New_Elmt_List
;
1081 Assoc_List
: constant List_Id
:= New_List
;
1082 Default_Actuals
: constant List_Id
:= New_List
;
1083 Gen_Unit
: constant Entity_Id
:=
1084 Defining_Entity
(Parent
(F_Copy
));
1088 Analyzed_Formal
: Node_Id
;
1089 First_Named
: Node_Id
:= Empty
;
1093 Saved_Formal
: Node_Id
;
1095 Default_Formals
: constant List_Id
:= New_List
;
1096 -- If an Others_Choice is present, some of the formals may be defaulted.
1097 -- To simplify the treatment of visibility in an instance, we introduce
1098 -- individual defaults for each such formal. These defaults are
1099 -- appended to the list of associations and replace the Others_Choice.
1101 Found_Assoc
: Node_Id
;
1102 -- Association for the current formal being match. Empty if there are
1103 -- no remaining actuals, or if there is no named association with the
1104 -- name of the formal.
1106 Is_Named_Assoc
: Boolean;
1107 Num_Matched
: Nat
:= 0;
1108 Num_Actuals
: Nat
:= 0;
1110 Others_Present
: Boolean := False;
1111 Others_Choice
: Node_Id
:= Empty
;
1112 -- In Ada 2005, indicates partial parameterization of a formal
1113 -- package. As usual an other association must be last in the list.
1115 procedure Check_Fixed_Point_Actual
(Actual
: Node_Id
);
1116 -- Warn if an actual fixed-point type has user-defined arithmetic
1117 -- operations, but there is no corresponding formal in the generic,
1118 -- in which case the predefined operations will be used. This merits
1119 -- a warning because of the special semantics of fixed point ops.
1121 procedure Check_Overloaded_Formal_Subprogram
(Formal
: Entity_Id
);
1122 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1123 -- cannot have a named association for it. AI05-0025 extends this rule
1124 -- to formals of formal packages by AI05-0025, and it also applies to
1125 -- box-initialized formals.
1127 function Has_Fully_Defined_Profile
(Subp
: Entity_Id
) return Boolean;
1128 -- Determine whether the parameter types and the return type of Subp
1129 -- are fully defined at the point of instantiation.
1131 function Matching_Actual
1133 A_F
: Entity_Id
) return Node_Id
;
1134 -- Find actual that corresponds to a given a formal parameter. If the
1135 -- actuals are positional, return the next one, if any. If the actuals
1136 -- are named, scan the parameter associations to find the right one.
1137 -- A_F is the corresponding entity in the analyzed generic, which is
1138 -- placed on the selector name for ASIS use.
1140 -- In Ada 2005, a named association may be given with a box, in which
1141 -- case Matching_Actual sets Found_Assoc to the generic association,
1142 -- but return Empty for the actual itself. In this case the code below
1143 -- creates a corresponding declaration for the formal.
1145 function Partial_Parameterization
return Boolean;
1146 -- Ada 2005: if no match is found for a given formal, check if the
1147 -- association for it includes a box, or whether the associations
1148 -- include an Others clause.
1150 procedure Process_Default
(F
: Entity_Id
);
1151 -- Add a copy of the declaration of generic formal F to the list of
1152 -- associations, and add an explicit box association for F if there
1153 -- is none yet, and the default comes from an Others_Choice.
1155 function Renames_Standard_Subprogram
(Subp
: Entity_Id
) return Boolean;
1156 -- Determine whether Subp renames one of the subprograms defined in the
1157 -- generated package Standard.
1159 procedure Set_Analyzed_Formal
;
1160 -- Find the node in the generic copy that corresponds to a given formal.
1161 -- The semantic information on this node is used to perform legality
1162 -- checks on the actuals. Because semantic analysis can introduce some
1163 -- anonymous entities or modify the declaration node itself, the
1164 -- correspondence between the two lists is not one-one. In addition to
1165 -- anonymous types, the presence a formal equality will introduce an
1166 -- implicit declaration for the corresponding inequality.
1168 ----------------------------------------
1169 -- Check_Overloaded_Formal_Subprogram --
1170 ----------------------------------------
1172 procedure Check_Overloaded_Formal_Subprogram
(Formal
: Entity_Id
) is
1173 Temp_Formal
: Entity_Id
;
1176 Temp_Formal
:= First
(Formals
);
1177 while Present
(Temp_Formal
) loop
1178 if Nkind
(Temp_Formal
) in N_Formal_Subprogram_Declaration
1179 and then Temp_Formal
/= Formal
1181 Chars
(Defining_Unit_Name
(Specification
(Formal
))) =
1182 Chars
(Defining_Unit_Name
(Specification
(Temp_Formal
)))
1184 if Present
(Found_Assoc
) then
1186 ("named association not allowed for overloaded formal",
1191 ("named association not allowed for overloaded formal",
1195 Abandon_Instantiation
(Instantiation_Node
);
1200 end Check_Overloaded_Formal_Subprogram
;
1202 -------------------------------
1203 -- Check_Fixed_Point_Actual --
1204 -------------------------------
1206 procedure Check_Fixed_Point_Actual
(Actual
: Node_Id
) is
1207 Typ
: constant Entity_Id
:= Entity
(Actual
);
1208 Prims
: constant Elist_Id
:= Collect_Primitive_Operations
(Typ
);
1214 -- Locate primitive operations of the type that are arithmetic
1217 Elem
:= First_Elmt
(Prims
);
1218 while Present
(Elem
) loop
1219 if Nkind
(Node
(Elem
)) = N_Defining_Operator_Symbol
then
1221 -- Check whether the generic unit has a formal subprogram of
1222 -- the same name. This does not check types but is good enough
1223 -- to justify a warning.
1225 Formal
:= First_Non_Pragma
(Formals
);
1226 Op
:= Alias
(Node
(Elem
));
1228 while Present
(Formal
) loop
1229 if Nkind
(Formal
) = N_Formal_Concrete_Subprogram_Declaration
1230 and then Chars
(Defining_Entity
(Formal
)) =
1235 elsif Nkind
(Formal
) = N_Formal_Package_Declaration
then
1241 -- Locate corresponding actual, and check whether it
1242 -- includes a fixed-point type.
1244 Assoc
:= First
(Assoc_List
);
1245 while Present
(Assoc
) loop
1247 Nkind
(Assoc
) = N_Package_Renaming_Declaration
1248 and then Chars
(Defining_Unit_Name
(Assoc
)) =
1249 Chars
(Defining_Identifier
(Formal
));
1254 if Present
(Assoc
) then
1256 -- If formal package declares a fixed-point type,
1257 -- and the user-defined operator is derived from
1258 -- a generic instance package, the fixed-point type
1259 -- does not use the corresponding predefined op.
1261 Ent
:= First_Entity
(Entity
(Name
(Assoc
)));
1262 while Present
(Ent
) loop
1263 if Is_Fixed_Point_Type
(Ent
)
1264 and then Present
(Op
)
1265 and then Is_Generic_Instance
(Scope
(Op
))
1280 Error_Msg_Sloc
:= Sloc
(Node
(Elem
));
1282 ("?instance uses predefined operation, not primitive "
1283 & "operation&#", Actual
, Node
(Elem
));
1289 end Check_Fixed_Point_Actual
;
1291 -------------------------------
1292 -- Has_Fully_Defined_Profile --
1293 -------------------------------
1295 function Has_Fully_Defined_Profile
(Subp
: Entity_Id
) return Boolean is
1296 function Is_Fully_Defined_Type
(Typ
: Entity_Id
) return Boolean;
1297 -- Determine whethet type Typ is fully defined
1299 ---------------------------
1300 -- Is_Fully_Defined_Type --
1301 ---------------------------
1303 function Is_Fully_Defined_Type
(Typ
: Entity_Id
) return Boolean is
1305 -- A private type without a full view is not fully defined
1307 if Is_Private_Type
(Typ
)
1308 and then No
(Full_View
(Typ
))
1312 -- An incomplete type is never fully defined
1314 elsif Is_Incomplete_Type
(Typ
) then
1317 -- All other types are fully defined
1322 end Is_Fully_Defined_Type
;
1324 -- Local declarations
1328 -- Start of processing for Has_Fully_Defined_Profile
1331 -- Check the parameters
1333 Param
:= First_Formal
(Subp
);
1334 while Present
(Param
) loop
1335 if not Is_Fully_Defined_Type
(Etype
(Param
)) then
1339 Next_Formal
(Param
);
1342 -- Check the return type
1344 return Is_Fully_Defined_Type
(Etype
(Subp
));
1345 end Has_Fully_Defined_Profile
;
1347 ---------------------
1348 -- Matching_Actual --
1349 ---------------------
1351 function Matching_Actual
1353 A_F
: Entity_Id
) return Node_Id
1359 Is_Named_Assoc
:= False;
1361 -- End of list of purely positional parameters
1363 if No
(Actual
) or else Nkind
(Actual
) = N_Others_Choice
then
1364 Found_Assoc
:= Empty
;
1367 -- Case of positional parameter corresponding to current formal
1369 elsif No
(Selector_Name
(Actual
)) then
1370 Found_Assoc
:= Actual
;
1371 Act
:= Explicit_Generic_Actual_Parameter
(Actual
);
1372 Num_Matched
:= Num_Matched
+ 1;
1375 -- Otherwise scan list of named actuals to find the one with the
1376 -- desired name. All remaining actuals have explicit names.
1379 Is_Named_Assoc
:= True;
1380 Found_Assoc
:= Empty
;
1384 while Present
(Actual
) loop
1385 if Nkind
(Actual
) = N_Others_Choice
then
1386 Found_Assoc
:= Empty
;
1389 elsif Chars
(Selector_Name
(Actual
)) = Chars
(F
) then
1390 Set_Entity
(Selector_Name
(Actual
), A_F
);
1391 Set_Etype
(Selector_Name
(Actual
), Etype
(A_F
));
1392 Generate_Reference
(A_F
, Selector_Name
(Actual
));
1394 Found_Assoc
:= Actual
;
1395 Act
:= Explicit_Generic_Actual_Parameter
(Actual
);
1396 Num_Matched
:= Num_Matched
+ 1;
1404 -- Reset for subsequent searches. In most cases the named
1405 -- associations are in order. If they are not, we reorder them
1406 -- to avoid scanning twice the same actual. This is not just a
1407 -- question of efficiency: there may be multiple defaults with
1408 -- boxes that have the same name. In a nested instantiation we
1409 -- insert actuals for those defaults, and cannot rely on their
1410 -- names to disambiguate them.
1412 if Actual
= First_Named
then
1415 elsif Present
(Actual
) then
1416 Insert_Before
(First_Named
, Remove_Next
(Prev
));
1419 Actual
:= First_Named
;
1422 if Is_Entity_Name
(Act
) and then Present
(Entity
(Act
)) then
1423 Set_Used_As_Generic_Actual
(Entity
(Act
));
1427 end Matching_Actual
;
1429 ------------------------------
1430 -- Partial_Parameterization --
1431 ------------------------------
1433 function Partial_Parameterization
return Boolean is
1435 return Others_Present
1436 or else (Present
(Found_Assoc
) and then Box_Present
(Found_Assoc
));
1437 end Partial_Parameterization
;
1439 ---------------------
1440 -- Process_Default --
1441 ---------------------
1443 procedure Process_Default
(F
: Entity_Id
) is
1444 Loc
: constant Source_Ptr
:= Sloc
(I_Node
);
1445 F_Id
: constant Entity_Id
:= Defining_Entity
(F
);
1451 -- Append copy of formal declaration to associations, and create new
1452 -- defining identifier for it.
1454 Decl
:= New_Copy_Tree
(F
);
1455 Id
:= Make_Defining_Identifier
(Sloc
(F_Id
), Chars
(F_Id
));
1457 if Nkind
(F
) in N_Formal_Subprogram_Declaration
then
1458 Set_Defining_Unit_Name
(Specification
(Decl
), Id
);
1461 Set_Defining_Identifier
(Decl
, Id
);
1464 Append
(Decl
, Assoc_List
);
1466 if No
(Found_Assoc
) then
1468 Make_Generic_Association
(Loc
,
1470 New_Occurrence_Of
(Id
, Loc
),
1471 Explicit_Generic_Actual_Parameter
=> Empty
);
1472 Set_Box_Present
(Default
);
1473 Append
(Default
, Default_Formals
);
1475 end Process_Default
;
1477 ---------------------------------
1478 -- Renames_Standard_Subprogram --
1479 ---------------------------------
1481 function Renames_Standard_Subprogram
(Subp
: Entity_Id
) return Boolean is
1486 while Present
(Id
) loop
1487 if Scope
(Id
) = Standard_Standard
then
1495 end Renames_Standard_Subprogram
;
1497 -------------------------
1498 -- Set_Analyzed_Formal --
1499 -------------------------
1501 procedure Set_Analyzed_Formal
is
1505 while Present
(Analyzed_Formal
) loop
1506 Kind
:= Nkind
(Analyzed_Formal
);
1508 case Nkind
(Formal
) is
1509 when N_Formal_Subprogram_Declaration
=>
1510 exit when Kind
in N_Formal_Subprogram_Declaration
1513 (Defining_Unit_Name
(Specification
(Formal
))) =
1515 (Defining_Unit_Name
(Specification
(Analyzed_Formal
)));
1517 when N_Formal_Package_Declaration
=>
1518 exit when Nkind_In
(Kind
, N_Formal_Package_Declaration
,
1519 N_Generic_Package_Declaration
,
1520 N_Package_Declaration
);
1522 when N_Use_Package_Clause
1529 -- Skip freeze nodes, and nodes inserted to replace
1530 -- unrecognized pragmas.
1533 Kind
not in N_Formal_Subprogram_Declaration
1534 and then not Nkind_In
(Kind
, N_Subprogram_Declaration
,
1538 and then Chars
(Defining_Identifier
(Formal
)) =
1539 Chars
(Defining_Identifier
(Analyzed_Formal
));
1542 Next
(Analyzed_Formal
);
1544 end Set_Analyzed_Formal
;
1546 -- Start of processing for Analyze_Associations
1549 Actuals
:= Generic_Associations
(I_Node
);
1551 if Present
(Actuals
) then
1553 -- Check for an Others choice, indicating a partial parameterization
1554 -- for a formal package.
1556 Actual
:= First
(Actuals
);
1557 while Present
(Actual
) loop
1558 if Nkind
(Actual
) = N_Others_Choice
then
1559 Others_Present
:= True;
1560 Others_Choice
:= Actual
;
1562 if Present
(Next
(Actual
)) then
1563 Error_Msg_N
("others must be last association", Actual
);
1566 -- This subprogram is used both for formal packages and for
1567 -- instantiations. For the latter, associations must all be
1570 if Nkind
(I_Node
) /= N_Formal_Package_Declaration
1571 and then Comes_From_Source
(I_Node
)
1574 ("others association not allowed in an instance",
1578 -- In any case, nothing to do after the others association
1582 elsif Box_Present
(Actual
)
1583 and then Comes_From_Source
(I_Node
)
1584 and then Nkind
(I_Node
) /= N_Formal_Package_Declaration
1587 ("box association not allowed in an instance", Actual
);
1593 -- If named associations are present, save first named association
1594 -- (it may of course be Empty) to facilitate subsequent name search.
1596 First_Named
:= First
(Actuals
);
1597 while Present
(First_Named
)
1598 and then Nkind
(First_Named
) /= N_Others_Choice
1599 and then No
(Selector_Name
(First_Named
))
1601 Num_Actuals
:= Num_Actuals
+ 1;
1606 Named
:= First_Named
;
1607 while Present
(Named
) loop
1608 if Nkind
(Named
) /= N_Others_Choice
1609 and then No
(Selector_Name
(Named
))
1611 Error_Msg_N
("invalid positional actual after named one", Named
);
1612 Abandon_Instantiation
(Named
);
1615 -- A named association may lack an actual parameter, if it was
1616 -- introduced for a default subprogram that turns out to be local
1617 -- to the outer instantiation. If it has a box association it must
1618 -- correspond to some formal in the generic.
1620 if Nkind
(Named
) /= N_Others_Choice
1621 and then (Present
(Explicit_Generic_Actual_Parameter
(Named
))
1622 or else Box_Present
(Named
))
1624 Num_Actuals
:= Num_Actuals
+ 1;
1630 if Present
(Formals
) then
1631 Formal
:= First_Non_Pragma
(Formals
);
1632 Analyzed_Formal
:= First_Non_Pragma
(F_Copy
);
1634 if Present
(Actuals
) then
1635 Actual
:= First
(Actuals
);
1637 -- All formals should have default values
1643 while Present
(Formal
) loop
1644 Set_Analyzed_Formal
;
1645 Saved_Formal
:= Next_Non_Pragma
(Formal
);
1647 case Nkind
(Formal
) is
1648 when N_Formal_Object_Declaration
=>
1651 (Defining_Identifier
(Formal
),
1652 Defining_Identifier
(Analyzed_Formal
));
1654 if No
(Match
) and then Partial_Parameterization
then
1655 Process_Default
(Formal
);
1659 (Instantiate_Object
(Formal
, Match
, Analyzed_Formal
),
1662 -- For a defaulted in_parameter, create an entry in the
1663 -- the list of defaulted actuals, for GNATProve use. Do
1664 -- not included these defaults for an instance nested
1665 -- within a generic, because the defaults are also used
1666 -- in the analysis of the enclosing generic, and only
1667 -- defaulted subprograms are relevant there.
1669 if No
(Match
) and then not Inside_A_Generic
then
1670 Append_To
(Default_Actuals
,
1671 Make_Generic_Association
(Sloc
(I_Node
),
1674 (Defining_Identifier
(Formal
), Sloc
(I_Node
)),
1675 Explicit_Generic_Actual_Parameter
=>
1676 New_Copy_Tree
(Default_Expression
(Formal
))));
1680 -- If the object is a call to an expression function, this
1681 -- is a freezing point for it.
1683 if Is_Entity_Name
(Match
)
1684 and then Present
(Entity
(Match
))
1686 (Original_Node
(Unit_Declaration_Node
(Entity
(Match
))))
1687 = N_Expression_Function
1689 Append_Elmt
(Entity
(Match
), Actuals_To_Freeze
);
1692 when N_Formal_Type_Declaration
=>
1695 (Defining_Identifier
(Formal
),
1696 Defining_Identifier
(Analyzed_Formal
));
1699 if Partial_Parameterization
then
1700 Process_Default
(Formal
);
1703 Error_Msg_Sloc
:= Sloc
(Gen_Unit
);
1706 Instantiation_Node
, Defining_Identifier
(Formal
));
1708 ("\in instantiation of & declared#",
1709 Instantiation_Node
, Gen_Unit
);
1710 Abandon_Instantiation
(Instantiation_Node
);
1717 (Formal
, Match
, Analyzed_Formal
, Assoc_List
),
1720 -- Warn when an actual is a fixed-point with user-
1721 -- defined promitives. The warning is superfluous
1722 -- if the fornal is private, because there can be
1723 -- no arithmetic operations in the generic so there
1724 -- no danger of confusion.
1726 if Is_Fixed_Point_Type
(Entity
(Match
))
1727 and then not Is_Private_Type
1728 (Defining_Identifier
(Analyzed_Formal
))
1730 Check_Fixed_Point_Actual
(Match
);
1733 -- An instantiation is a freeze point for the actuals,
1734 -- unless this is a rewritten formal package, or the
1735 -- formal is an Ada 2012 formal incomplete type.
1737 if Nkind
(I_Node
) = N_Formal_Package_Declaration
1739 (Ada_Version
>= Ada_2012
1741 Ekind
(Defining_Identifier
(Analyzed_Formal
)) =
1747 Append_Elmt
(Entity
(Match
), Actuals_To_Freeze
);
1751 -- A remote access-to-class-wide type is not a legal actual
1752 -- for a generic formal of an access type (E.2.2(17/2)).
1753 -- In GNAT an exception to this rule is introduced when
1754 -- the formal is marked as remote using implementation
1755 -- defined aspect/pragma Remote_Access_Type. In that case
1756 -- the actual must be remote as well.
1758 -- If the current instantiation is the construction of a
1759 -- local copy for a formal package the actuals may be
1760 -- defaulted, and there is no matching actual to check.
1762 if Nkind
(Analyzed_Formal
) = N_Formal_Type_Declaration
1764 Nkind
(Formal_Type_Definition
(Analyzed_Formal
)) =
1765 N_Access_To_Object_Definition
1766 and then Present
(Match
)
1769 Formal_Ent
: constant Entity_Id
:=
1770 Defining_Identifier
(Analyzed_Formal
);
1772 if Is_Remote_Access_To_Class_Wide_Type
(Entity
(Match
))
1773 = Is_Remote_Types
(Formal_Ent
)
1775 -- Remoteness of formal and actual match
1779 elsif Is_Remote_Types
(Formal_Ent
) then
1781 -- Remote formal, non-remote actual
1784 ("actual for& must be remote", Match
, Formal_Ent
);
1787 -- Non-remote formal, remote actual
1790 ("actual for& may not be remote",
1796 when N_Formal_Subprogram_Declaration
=>
1799 (Defining_Unit_Name
(Specification
(Formal
)),
1800 Defining_Unit_Name
(Specification
(Analyzed_Formal
)));
1802 -- If the formal subprogram has the same name as another
1803 -- formal subprogram of the generic, then a named
1804 -- association is illegal (12.3(9)). Exclude named
1805 -- associations that are generated for a nested instance.
1808 and then Is_Named_Assoc
1809 and then Comes_From_Source
(Found_Assoc
)
1811 Check_Overloaded_Formal_Subprogram
(Formal
);
1814 -- If there is no corresponding actual, this may be case
1815 -- of partial parameterization, or else the formal has a
1816 -- default or a box.
1818 if No
(Match
) and then Partial_Parameterization
then
1819 Process_Default
(Formal
);
1821 if Nkind
(I_Node
) = N_Formal_Package_Declaration
then
1822 Check_Overloaded_Formal_Subprogram
(Formal
);
1826 Append_To
(Assoc_List
,
1827 Instantiate_Formal_Subprogram
1828 (Formal
, Match
, Analyzed_Formal
));
1830 -- An instantiation is a freeze point for the actuals,
1831 -- unless this is a rewritten formal package.
1833 if Nkind
(I_Node
) /= N_Formal_Package_Declaration
1834 and then Nkind
(Match
) = N_Identifier
1835 and then Is_Subprogram
(Entity
(Match
))
1837 -- The actual subprogram may rename a routine defined
1838 -- in Standard. Avoid freezing such renamings because
1839 -- subprograms coming from Standard cannot be frozen.
1842 not Renames_Standard_Subprogram
(Entity
(Match
))
1844 -- If the actual subprogram comes from a different
1845 -- unit, it is already frozen, either by a body in
1846 -- that unit or by the end of the declarative part
1847 -- of the unit. This check avoids the freezing of
1848 -- subprograms defined in Standard which are used
1849 -- as generic actuals.
1851 and then In_Same_Code_Unit
(Entity
(Match
), I_Node
)
1852 and then Has_Fully_Defined_Profile
(Entity
(Match
))
1854 -- Mark the subprogram as having a delayed freeze
1855 -- since this may be an out-of-order action.
1857 Set_Has_Delayed_Freeze
(Entity
(Match
));
1858 Append_Elmt
(Entity
(Match
), Actuals_To_Freeze
);
1862 -- If this is a nested generic, preserve default for later
1863 -- instantiations. We do this as well for GNATProve use,
1864 -- so that the list of generic associations is complete.
1866 if No
(Match
) and then Box_Present
(Formal
) then
1868 Subp
: constant Entity_Id
:=
1870 (Specification
(Last
(Assoc_List
)));
1873 Append_To
(Default_Actuals
,
1874 Make_Generic_Association
(Sloc
(I_Node
),
1876 New_Occurrence_Of
(Subp
, Sloc
(I_Node
)),
1877 Explicit_Generic_Actual_Parameter
=>
1878 New_Occurrence_Of
(Subp
, Sloc
(I_Node
))));
1882 when N_Formal_Package_Declaration
=>
1885 (Defining_Identifier
(Formal
),
1886 Defining_Identifier
(Original_Node
(Analyzed_Formal
)));
1889 if Partial_Parameterization
then
1890 Process_Default
(Formal
);
1893 Error_Msg_Sloc
:= Sloc
(Gen_Unit
);
1896 Instantiation_Node
, Defining_Identifier
(Formal
));
1898 ("\in instantiation of & declared#",
1899 Instantiation_Node
, Gen_Unit
);
1901 Abandon_Instantiation
(Instantiation_Node
);
1907 (Instantiate_Formal_Package
1908 (Formal
, Match
, Analyzed_Formal
),
1911 -- Determine whether the actual package needs an explicit
1912 -- freeze node. This is only the case if the actual is
1913 -- declared in the same unit and has a body. Normally
1914 -- packages do not have explicit freeze nodes, and gigi
1915 -- only uses them to elaborate entities in a package
1918 Explicit_Freeze_Check
: declare
1919 Actual
: constant Entity_Id
:= Entity
(Match
);
1920 Gen_Par
: Entity_Id
;
1922 Needs_Freezing
: Boolean;
1925 procedure Check_Generic_Parent
;
1926 -- The actual may be an instantiation of a unit
1927 -- declared in a previous instantiation. If that
1928 -- one is also in the current compilation, it must
1929 -- itself be frozen before the actual. The actual
1930 -- may be an instantiation of a generic child unit,
1931 -- in which case the same applies to the instance
1932 -- of the parent which must be frozen before the
1934 -- Should this itself be recursive ???
1936 --------------------------
1937 -- Check_Generic_Parent --
1938 --------------------------
1940 procedure Check_Generic_Parent
is
1941 Inst
: constant Node_Id
:=
1942 Next
(Unit_Declaration_Node
(Actual
));
1948 if Nkind
(Parent
(Actual
)) = N_Package_Specification
1950 Par
:= Scope
(Generic_Parent
(Parent
(Actual
)));
1952 if Is_Generic_Instance
(Par
) then
1955 -- If the actual is a child generic unit, check
1956 -- whether the instantiation of the parent is
1957 -- also local and must also be frozen now. We
1958 -- must retrieve the instance node to locate the
1959 -- parent instance if any.
1961 elsif Ekind
(Par
) = E_Generic_Package
1962 and then Is_Child_Unit
(Gen_Par
)
1963 and then Ekind
(Scope
(Gen_Par
)) =
1966 if Nkind
(Inst
) = N_Package_Instantiation
1967 and then Nkind
(Name
(Inst
)) =
1970 -- Retrieve entity of parent instance
1972 Par
:= Entity
(Prefix
(Name
(Inst
)));
1981 and then Is_Generic_Instance
(Par
)
1982 and then Scope
(Par
) = Current_Scope
1984 (No
(Freeze_Node
(Par
))
1986 not Is_List_Member
(Freeze_Node
(Par
)))
1988 Set_Has_Delayed_Freeze
(Par
);
1989 Append_Elmt
(Par
, Actuals_To_Freeze
);
1991 end Check_Generic_Parent
;
1993 -- Start of processing for Explicit_Freeze_Check
1996 if Present
(Renamed_Entity
(Actual
)) then
1998 Generic_Parent
(Specification
1999 (Unit_Declaration_Node
2000 (Renamed_Entity
(Actual
))));
2003 Generic_Parent
(Specification
2004 (Unit_Declaration_Node
(Actual
)));
2007 if not Expander_Active
2008 or else not Has_Completion
(Actual
)
2009 or else not In_Same_Source_Unit
(I_Node
, Actual
)
2010 or else Is_Frozen
(Actual
)
2012 (Present
(Renamed_Entity
(Actual
))
2014 not In_Same_Source_Unit
2015 (I_Node
, (Renamed_Entity
(Actual
))))
2020 -- Finally we want to exclude such freeze nodes
2021 -- from statement sequences, which freeze
2022 -- everything before them.
2023 -- Is this strictly necessary ???
2025 Needs_Freezing
:= True;
2028 while Present
(S
) loop
2029 if Ekind_In
(S
, E_Block
,
2034 Needs_Freezing
:= False;
2041 if Needs_Freezing
then
2042 Check_Generic_Parent
;
2044 -- If the actual is a renaming of a proper
2045 -- instance of the formal package, indicate
2046 -- that it is the instance that must be frozen.
2048 if Nkind
(Parent
(Actual
)) =
2049 N_Package_Renaming_Declaration
2051 Set_Has_Delayed_Freeze
2052 (Renamed_Entity
(Actual
));
2054 (Renamed_Entity
(Actual
),
2057 Set_Has_Delayed_Freeze
(Actual
);
2058 Append_Elmt
(Actual
, Actuals_To_Freeze
);
2062 end Explicit_Freeze_Check
;
2065 -- For use type and use package appearing in the generic part,
2066 -- we have already copied them, so we can just move them where
2067 -- they belong (we mustn't recopy them since this would mess up
2068 -- the Sloc values).
2070 when N_Use_Package_Clause
2073 if Nkind
(Original_Node
(I_Node
)) =
2074 N_Formal_Package_Declaration
2076 Append
(New_Copy_Tree
(Formal
), Assoc_List
);
2079 Append
(Formal
, Assoc_List
);
2083 raise Program_Error
;
2086 Formal
:= Saved_Formal
;
2087 Next_Non_Pragma
(Analyzed_Formal
);
2090 if Num_Actuals
> Num_Matched
then
2091 Error_Msg_Sloc
:= Sloc
(Gen_Unit
);
2093 if Present
(Selector_Name
(Actual
)) then
2095 ("unmatched actual &", Actual
, Selector_Name
(Actual
));
2097 ("\in instantiation of & declared#", Actual
, Gen_Unit
);
2100 ("unmatched actual in instantiation of & declared#",
2105 elsif Present
(Actuals
) then
2107 ("too many actuals in generic instantiation", Instantiation_Node
);
2110 -- An instantiation freezes all generic actuals. The only exceptions
2111 -- to this are incomplete types and subprograms which are not fully
2112 -- defined at the point of instantiation.
2115 Elmt
: Elmt_Id
:= First_Elmt
(Actuals_To_Freeze
);
2117 while Present
(Elmt
) loop
2118 Freeze_Before
(I_Node
, Node
(Elmt
));
2123 -- If there are default subprograms, normalize the tree by adding
2124 -- explicit associations for them. This is required if the instance
2125 -- appears within a generic.
2127 if not Is_Empty_List
(Default_Actuals
) then
2132 Default
:= First
(Default_Actuals
);
2133 while Present
(Default
) loop
2134 Mark_Rewrite_Insertion
(Default
);
2138 if No
(Actuals
) then
2139 Set_Generic_Associations
(I_Node
, Default_Actuals
);
2141 Append_List_To
(Actuals
, Default_Actuals
);
2146 -- If this is a formal package, normalize the parameter list by adding
2147 -- explicit box associations for the formals that are covered by an
2150 if not Is_Empty_List
(Default_Formals
) then
2151 Append_List
(Default_Formals
, Formals
);
2155 end Analyze_Associations
;
2157 -------------------------------
2158 -- Analyze_Formal_Array_Type --
2159 -------------------------------
2161 procedure Analyze_Formal_Array_Type
2162 (T
: in out Entity_Id
;
2168 -- Treated like a non-generic array declaration, with additional
2173 if Nkind
(Def
) = N_Constrained_Array_Definition
then
2174 DSS
:= First
(Discrete_Subtype_Definitions
(Def
));
2175 while Present
(DSS
) loop
2176 if Nkind_In
(DSS
, N_Subtype_Indication
,
2178 N_Attribute_Reference
)
2180 Error_Msg_N
("only a subtype mark is allowed in a formal", DSS
);
2187 Array_Type_Declaration
(T
, Def
);
2188 Set_Is_Generic_Type
(Base_Type
(T
));
2190 if Ekind
(Component_Type
(T
)) = E_Incomplete_Type
2191 and then No
(Full_View
(Component_Type
(T
)))
2193 Error_Msg_N
("premature usage of incomplete type", Def
);
2195 -- Check that range constraint is not allowed on the component type
2196 -- of a generic formal array type (AARM 12.5.3(3))
2198 elsif Is_Internal
(Component_Type
(T
))
2199 and then Present
(Subtype_Indication
(Component_Definition
(Def
)))
2200 and then Nkind
(Original_Node
2201 (Subtype_Indication
(Component_Definition
(Def
)))) =
2202 N_Subtype_Indication
2205 ("in a formal, a subtype indication can only be "
2206 & "a subtype mark (RM 12.5.3(3))",
2207 Subtype_Indication
(Component_Definition
(Def
)));
2210 end Analyze_Formal_Array_Type
;
2212 ---------------------------------------------
2213 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2214 ---------------------------------------------
2216 -- As for other generic types, we create a valid type representation with
2217 -- legal but arbitrary attributes, whose values are never considered
2218 -- static. For all scalar types we introduce an anonymous base type, with
2219 -- the same attributes. We choose the corresponding integer type to be
2220 -- Standard_Integer.
2221 -- Here and in other similar routines, the Sloc of the generated internal
2222 -- type must be the same as the sloc of the defining identifier of the
2223 -- formal type declaration, to provide proper source navigation.
2225 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2229 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2231 Base
: constant Entity_Id
:=
2233 (E_Decimal_Fixed_Point_Type
,
2235 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
2237 Int_Base
: constant Entity_Id
:= Standard_Integer
;
2238 Delta_Val
: constant Ureal
:= Ureal_1
;
2239 Digs_Val
: constant Uint
:= Uint_6
;
2241 function Make_Dummy_Bound
return Node_Id
;
2242 -- Return a properly typed universal real literal to use as a bound
2244 ----------------------
2245 -- Make_Dummy_Bound --
2246 ----------------------
2248 function Make_Dummy_Bound
return Node_Id
is
2249 Bound
: constant Node_Id
:= Make_Real_Literal
(Loc
, Ureal_1
);
2251 Set_Etype
(Bound
, Universal_Real
);
2253 end Make_Dummy_Bound
;
2255 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2260 Set_Etype
(Base
, Base
);
2261 Set_Size_Info
(Base
, Int_Base
);
2262 Set_RM_Size
(Base
, RM_Size
(Int_Base
));
2263 Set_First_Rep_Item
(Base
, First_Rep_Item
(Int_Base
));
2264 Set_Digits_Value
(Base
, Digs_Val
);
2265 Set_Delta_Value
(Base
, Delta_Val
);
2266 Set_Small_Value
(Base
, Delta_Val
);
2267 Set_Scalar_Range
(Base
,
2269 Low_Bound
=> Make_Dummy_Bound
,
2270 High_Bound
=> Make_Dummy_Bound
));
2272 Set_Is_Generic_Type
(Base
);
2273 Set_Parent
(Base
, Parent
(Def
));
2275 Set_Ekind
(T
, E_Decimal_Fixed_Point_Subtype
);
2276 Set_Etype
(T
, Base
);
2277 Set_Size_Info
(T
, Int_Base
);
2278 Set_RM_Size
(T
, RM_Size
(Int_Base
));
2279 Set_First_Rep_Item
(T
, First_Rep_Item
(Int_Base
));
2280 Set_Digits_Value
(T
, Digs_Val
);
2281 Set_Delta_Value
(T
, Delta_Val
);
2282 Set_Small_Value
(T
, Delta_Val
);
2283 Set_Scalar_Range
(T
, Scalar_Range
(Base
));
2284 Set_Is_Constrained
(T
);
2286 Check_Restriction
(No_Fixed_Point
, Def
);
2287 end Analyze_Formal_Decimal_Fixed_Point_Type
;
2289 -------------------------------------------
2290 -- Analyze_Formal_Derived_Interface_Type --
2291 -------------------------------------------
2293 procedure Analyze_Formal_Derived_Interface_Type
2298 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2301 -- Rewrite as a type declaration of a derived type. This ensures that
2302 -- the interface list and primitive operations are properly captured.
2305 Make_Full_Type_Declaration
(Loc
,
2306 Defining_Identifier
=> T
,
2307 Type_Definition
=> Def
));
2309 Set_Is_Generic_Type
(T
);
2310 end Analyze_Formal_Derived_Interface_Type
;
2312 ---------------------------------
2313 -- Analyze_Formal_Derived_Type --
2314 ---------------------------------
2316 procedure Analyze_Formal_Derived_Type
2321 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2322 Unk_Disc
: constant Boolean := Unknown_Discriminants_Present
(N
);
2326 Set_Is_Generic_Type
(T
);
2328 if Private_Present
(Def
) then
2330 Make_Private_Extension_Declaration
(Loc
,
2331 Defining_Identifier
=> T
,
2332 Discriminant_Specifications
=> Discriminant_Specifications
(N
),
2333 Unknown_Discriminants_Present
=> Unk_Disc
,
2334 Subtype_Indication
=> Subtype_Mark
(Def
),
2335 Interface_List
=> Interface_List
(Def
));
2337 Set_Abstract_Present
(New_N
, Abstract_Present
(Def
));
2338 Set_Limited_Present
(New_N
, Limited_Present
(Def
));
2339 Set_Synchronized_Present
(New_N
, Synchronized_Present
(Def
));
2343 Make_Full_Type_Declaration
(Loc
,
2344 Defining_Identifier
=> T
,
2345 Discriminant_Specifications
=>
2346 Discriminant_Specifications
(Parent
(T
)),
2348 Make_Derived_Type_Definition
(Loc
,
2349 Subtype_Indication
=> Subtype_Mark
(Def
)));
2351 Set_Abstract_Present
2352 (Type_Definition
(New_N
), Abstract_Present
(Def
));
2354 (Type_Definition
(New_N
), Limited_Present
(Def
));
2361 if not Is_Composite_Type
(T
) then
2363 ("unknown discriminants not allowed for elementary types", N
);
2365 Set_Has_Unknown_Discriminants
(T
);
2366 Set_Is_Constrained
(T
, False);
2370 -- If the parent type has a known size, so does the formal, which makes
2371 -- legal representation clauses that involve the formal.
2373 Set_Size_Known_At_Compile_Time
2374 (T
, Size_Known_At_Compile_Time
(Entity
(Subtype_Mark
(Def
))));
2375 end Analyze_Formal_Derived_Type
;
2377 ----------------------------------
2378 -- Analyze_Formal_Discrete_Type --
2379 ----------------------------------
2381 -- The operations defined for a discrete types are those of an enumeration
2382 -- type. The size is set to an arbitrary value, for use in analyzing the
2385 procedure Analyze_Formal_Discrete_Type
(T
: Entity_Id
; Def
: Node_Id
) is
2386 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2390 Base
: constant Entity_Id
:=
2392 (E_Floating_Point_Type
, Current_Scope
,
2393 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
2397 Set_Ekind
(T
, E_Enumeration_Subtype
);
2398 Set_Etype
(T
, Base
);
2401 Set_Is_Generic_Type
(T
);
2402 Set_Is_Constrained
(T
);
2404 -- For semantic analysis, the bounds of the type must be set to some
2405 -- non-static value. The simplest is to create attribute nodes for those
2406 -- bounds, that refer to the type itself. These bounds are never
2407 -- analyzed but serve as place-holders.
2410 Make_Attribute_Reference
(Loc
,
2411 Attribute_Name
=> Name_First
,
2412 Prefix
=> New_Occurrence_Of
(T
, Loc
));
2416 Make_Attribute_Reference
(Loc
,
2417 Attribute_Name
=> Name_Last
,
2418 Prefix
=> New_Occurrence_Of
(T
, Loc
));
2421 Set_Scalar_Range
(T
,
2426 Set_Ekind
(Base
, E_Enumeration_Type
);
2427 Set_Etype
(Base
, Base
);
2428 Init_Size
(Base
, 8);
2429 Init_Alignment
(Base
);
2430 Set_Is_Generic_Type
(Base
);
2431 Set_Scalar_Range
(Base
, Scalar_Range
(T
));
2432 Set_Parent
(Base
, Parent
(Def
));
2433 end Analyze_Formal_Discrete_Type
;
2435 ----------------------------------
2436 -- Analyze_Formal_Floating_Type --
2437 ---------------------------------
2439 procedure Analyze_Formal_Floating_Type
(T
: Entity_Id
; Def
: Node_Id
) is
2440 Base
: constant Entity_Id
:=
2442 (E_Floating_Point_Type
, Current_Scope
,
2443 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
2446 -- The various semantic attributes are taken from the predefined type
2447 -- Float, just so that all of them are initialized. Their values are
2448 -- never used because no constant folding or expansion takes place in
2449 -- the generic itself.
2452 Set_Ekind
(T
, E_Floating_Point_Subtype
);
2453 Set_Etype
(T
, Base
);
2454 Set_Size_Info
(T
, (Standard_Float
));
2455 Set_RM_Size
(T
, RM_Size
(Standard_Float
));
2456 Set_Digits_Value
(T
, Digits_Value
(Standard_Float
));
2457 Set_Scalar_Range
(T
, Scalar_Range
(Standard_Float
));
2458 Set_Is_Constrained
(T
);
2460 Set_Is_Generic_Type
(Base
);
2461 Set_Etype
(Base
, Base
);
2462 Set_Size_Info
(Base
, (Standard_Float
));
2463 Set_RM_Size
(Base
, RM_Size
(Standard_Float
));
2464 Set_Digits_Value
(Base
, Digits_Value
(Standard_Float
));
2465 Set_Scalar_Range
(Base
, Scalar_Range
(Standard_Float
));
2466 Set_Parent
(Base
, Parent
(Def
));
2468 Check_Restriction
(No_Floating_Point
, Def
);
2469 end Analyze_Formal_Floating_Type
;
2471 -----------------------------------
2472 -- Analyze_Formal_Interface_Type;--
2473 -----------------------------------
2475 procedure Analyze_Formal_Interface_Type
2480 Loc
: constant Source_Ptr
:= Sloc
(N
);
2485 Make_Full_Type_Declaration
(Loc
,
2486 Defining_Identifier
=> T
,
2487 Type_Definition
=> Def
);
2491 Set_Is_Generic_Type
(T
);
2492 end Analyze_Formal_Interface_Type
;
2494 ---------------------------------
2495 -- Analyze_Formal_Modular_Type --
2496 ---------------------------------
2498 procedure Analyze_Formal_Modular_Type
(T
: Entity_Id
; Def
: Node_Id
) is
2500 -- Apart from their entity kind, generic modular types are treated like
2501 -- signed integer types, and have the same attributes.
2503 Analyze_Formal_Signed_Integer_Type
(T
, Def
);
2504 Set_Ekind
(T
, E_Modular_Integer_Subtype
);
2505 Set_Ekind
(Etype
(T
), E_Modular_Integer_Type
);
2507 end Analyze_Formal_Modular_Type
;
2509 ---------------------------------------
2510 -- Analyze_Formal_Object_Declaration --
2511 ---------------------------------------
2513 procedure Analyze_Formal_Object_Declaration
(N
: Node_Id
) is
2514 E
: constant Node_Id
:= Default_Expression
(N
);
2515 Id
: constant Node_Id
:= Defining_Identifier
(N
);
2522 -- Determine the mode of the formal object
2524 if Out_Present
(N
) then
2525 K
:= E_Generic_In_Out_Parameter
;
2527 if not In_Present
(N
) then
2528 Error_Msg_N
("formal generic objects cannot have mode OUT", N
);
2532 K
:= E_Generic_In_Parameter
;
2535 if Present
(Subtype_Mark
(N
)) then
2536 Find_Type
(Subtype_Mark
(N
));
2537 T
:= Entity
(Subtype_Mark
(N
));
2539 -- Verify that there is no redundant null exclusion
2541 if Null_Exclusion_Present
(N
) then
2542 if not Is_Access_Type
(T
) then
2544 ("null exclusion can only apply to an access type", N
);
2546 elsif Can_Never_Be_Null
(T
) then
2548 ("`NOT NULL` not allowed (& already excludes null)", N
, T
);
2552 -- Ada 2005 (AI-423): Formal object with an access definition
2555 Check_Access_Definition
(N
);
2556 T
:= Access_Definition
2558 N
=> Access_Definition
(N
));
2561 if Ekind
(T
) = E_Incomplete_Type
then
2563 Error_Node
: Node_Id
;
2566 if Present
(Subtype_Mark
(N
)) then
2567 Error_Node
:= Subtype_Mark
(N
);
2569 Check_Access_Definition
(N
);
2570 Error_Node
:= Access_Definition
(N
);
2573 Error_Msg_N
("premature usage of incomplete type", Error_Node
);
2577 if K
= E_Generic_In_Parameter
then
2579 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2581 if Ada_Version
< Ada_2005
and then Is_Limited_Type
(T
) then
2583 ("generic formal of mode IN must not be of limited type", N
);
2584 Explain_Limited_Type
(T
, N
);
2587 if Is_Abstract_Type
(T
) then
2589 ("generic formal of mode IN must not be of abstract type", N
);
2593 Preanalyze_Spec_Expression
(E
, T
);
2595 if Is_Limited_Type
(T
) and then not OK_For_Limited_Init
(T
, E
) then
2597 ("initialization not allowed for limited types", E
);
2598 Explain_Limited_Type
(T
, E
);
2605 -- Case of generic IN OUT parameter
2608 -- If the formal has an unconstrained type, construct its actual
2609 -- subtype, as is done for subprogram formals. In this fashion, all
2610 -- its uses can refer to specific bounds.
2615 if (Is_Array_Type
(T
) and then not Is_Constrained
(T
))
2616 or else (Ekind
(T
) = E_Record_Type
and then Has_Discriminants
(T
))
2619 Non_Freezing_Ref
: constant Node_Id
:=
2620 New_Occurrence_Of
(Id
, Sloc
(Id
));
2624 -- Make sure the actual subtype doesn't generate bogus freezing
2626 Set_Must_Not_Freeze
(Non_Freezing_Ref
);
2627 Decl
:= Build_Actual_Subtype
(T
, Non_Freezing_Ref
);
2628 Insert_Before_And_Analyze
(N
, Decl
);
2629 Set_Actual_Subtype
(Id
, Defining_Identifier
(Decl
));
2632 Set_Actual_Subtype
(Id
, T
);
2637 ("initialization not allowed for `IN OUT` formals", N
);
2641 if Has_Aspects
(N
) then
2642 Analyze_Aspect_Specifications
(N
, Id
);
2644 end Analyze_Formal_Object_Declaration
;
2646 ----------------------------------------------
2647 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2648 ----------------------------------------------
2650 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2654 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2655 Base
: constant Entity_Id
:=
2657 (E_Ordinary_Fixed_Point_Type
, Current_Scope
,
2658 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
2661 -- The semantic attributes are set for completeness only, their values
2662 -- will never be used, since all properties of the type are non-static.
2665 Set_Ekind
(T
, E_Ordinary_Fixed_Point_Subtype
);
2666 Set_Etype
(T
, Base
);
2667 Set_Size_Info
(T
, Standard_Integer
);
2668 Set_RM_Size
(T
, RM_Size
(Standard_Integer
));
2669 Set_Small_Value
(T
, Ureal_1
);
2670 Set_Delta_Value
(T
, Ureal_1
);
2671 Set_Scalar_Range
(T
,
2673 Low_Bound
=> Make_Real_Literal
(Loc
, Ureal_1
),
2674 High_Bound
=> Make_Real_Literal
(Loc
, Ureal_1
)));
2675 Set_Is_Constrained
(T
);
2677 Set_Is_Generic_Type
(Base
);
2678 Set_Etype
(Base
, Base
);
2679 Set_Size_Info
(Base
, Standard_Integer
);
2680 Set_RM_Size
(Base
, RM_Size
(Standard_Integer
));
2681 Set_Small_Value
(Base
, Ureal_1
);
2682 Set_Delta_Value
(Base
, Ureal_1
);
2683 Set_Scalar_Range
(Base
, Scalar_Range
(T
));
2684 Set_Parent
(Base
, Parent
(Def
));
2686 Check_Restriction
(No_Fixed_Point
, Def
);
2687 end Analyze_Formal_Ordinary_Fixed_Point_Type
;
2689 ----------------------------------------
2690 -- Analyze_Formal_Package_Declaration --
2691 ----------------------------------------
2693 procedure Analyze_Formal_Package_Declaration
(N
: Node_Id
) is
2694 Gen_Id
: constant Node_Id
:= Name
(N
);
2695 Loc
: constant Source_Ptr
:= Sloc
(N
);
2696 Pack_Id
: constant Entity_Id
:= Defining_Identifier
(N
);
2699 Gen_Unit
: Entity_Id
;
2702 Vis_Prims_List
: Elist_Id
:= No_Elist
;
2703 -- List of primitives made temporarily visible in the instantiation
2704 -- to match the visibility of the formal type.
2706 function Build_Local_Package
return Node_Id
;
2707 -- The formal package is rewritten so that its parameters are replaced
2708 -- with corresponding declarations. For parameters with bona fide
2709 -- associations these declarations are created by Analyze_Associations
2710 -- as for a regular instantiation. For boxed parameters, we preserve
2711 -- the formal declarations and analyze them, in order to introduce
2712 -- entities of the right kind in the environment of the formal.
2714 -------------------------
2715 -- Build_Local_Package --
2716 -------------------------
2718 function Build_Local_Package
return Node_Id
is
2720 Pack_Decl
: Node_Id
;
2723 -- Within the formal, the name of the generic package is a renaming
2724 -- of the formal (as for a regular instantiation).
2727 Make_Package_Declaration
(Loc
,
2730 (Specification
(Original_Node
(Gen_Decl
)),
2731 Empty
, Instantiating
=> True));
2734 Make_Package_Renaming_Declaration
(Loc
,
2735 Defining_Unit_Name
=>
2736 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
)),
2737 Name
=> New_Occurrence_Of
(Formal
, Loc
));
2739 if Nkind
(Gen_Id
) = N_Identifier
2740 and then Chars
(Gen_Id
) = Chars
(Pack_Id
)
2743 ("& is hidden within declaration of instance", Gen_Id
, Gen_Unit
);
2746 -- If the formal is declared with a box, or with an others choice,
2747 -- create corresponding declarations for all entities in the formal
2748 -- part, so that names with the proper types are available in the
2749 -- specification of the formal package.
2751 -- On the other hand, if there are no associations, then all the
2752 -- formals must have defaults, and this will be checked by the
2753 -- call to Analyze_Associations.
2756 or else Nkind
(First
(Generic_Associations
(N
))) = N_Others_Choice
2759 Formal_Decl
: Node_Id
;
2762 -- TBA : for a formal package, need to recurse ???
2767 (Generic_Formal_Declarations
(Original_Node
(Gen_Decl
)));
2768 while Present
(Formal_Decl
) loop
2772 (Formal_Decl
, Empty
, Instantiating
=> True));
2777 -- If generic associations are present, use Analyze_Associations to
2778 -- create the proper renaming declarations.
2782 Act_Tree
: constant Node_Id
:=
2784 (Original_Node
(Gen_Decl
), Empty
,
2785 Instantiating
=> True);
2788 Generic_Renamings
.Set_Last
(0);
2789 Generic_Renamings_HTable
.Reset
;
2790 Instantiation_Node
:= N
;
2793 Analyze_Associations
2794 (I_Node
=> Original_Node
(N
),
2795 Formals
=> Generic_Formal_Declarations
(Act_Tree
),
2796 F_Copy
=> Generic_Formal_Declarations
(Gen_Decl
));
2798 Vis_Prims_List
:= Check_Hidden_Primitives
(Decls
);
2802 Append
(Renaming
, To
=> Decls
);
2804 -- Add generated declarations ahead of local declarations in
2807 if No
(Visible_Declarations
(Specification
(Pack_Decl
))) then
2808 Set_Visible_Declarations
(Specification
(Pack_Decl
), Decls
);
2811 (First
(Visible_Declarations
(Specification
(Pack_Decl
))),
2816 end Build_Local_Package
;
2820 Save_ISMP
: constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance
;
2821 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2823 Associations
: Boolean := True;
2825 Parent_Installed
: Boolean := False;
2826 Parent_Instance
: Entity_Id
;
2827 Renaming_In_Par
: Entity_Id
;
2829 -- Start of processing for Analyze_Formal_Package_Declaration
2832 Check_Text_IO_Special_Unit
(Gen_Id
);
2835 Check_Generic_Child_Unit
(Gen_Id
, Parent_Installed
);
2836 Gen_Unit
:= Entity
(Gen_Id
);
2838 -- Check for a formal package that is a package renaming
2840 if Present
(Renamed_Object
(Gen_Unit
)) then
2842 -- Indicate that unit is used, before replacing it with renamed
2843 -- entity for use below.
2845 if In_Extended_Main_Source_Unit
(N
) then
2846 Set_Is_Instantiated
(Gen_Unit
);
2847 Generate_Reference
(Gen_Unit
, N
);
2850 Gen_Unit
:= Renamed_Object
(Gen_Unit
);
2853 if Ekind
(Gen_Unit
) /= E_Generic_Package
then
2854 Error_Msg_N
("expect generic package name", Gen_Id
);
2858 elsif Gen_Unit
= Current_Scope
then
2860 ("generic package cannot be used as a formal package of itself",
2865 elsif In_Open_Scopes
(Gen_Unit
) then
2866 if Is_Compilation_Unit
(Gen_Unit
)
2867 and then Is_Child_Unit
(Current_Scope
)
2869 -- Special-case the error when the formal is a parent, and
2870 -- continue analysis to minimize cascaded errors.
2873 ("generic parent cannot be used as formal package of a child "
2878 ("generic package cannot be used as a formal package within "
2879 & "itself", Gen_Id
);
2885 -- Check that name of formal package does not hide name of generic,
2886 -- or its leading prefix. This check must be done separately because
2887 -- the name of the generic has already been analyzed.
2890 Gen_Name
: Entity_Id
;
2894 while Nkind
(Gen_Name
) = N_Expanded_Name
loop
2895 Gen_Name
:= Prefix
(Gen_Name
);
2898 if Chars
(Gen_Name
) = Chars
(Pack_Id
) then
2900 ("& is hidden within declaration of formal package",
2906 or else No
(Generic_Associations
(N
))
2907 or else Nkind
(First
(Generic_Associations
(N
))) = N_Others_Choice
2909 Associations
:= False;
2912 -- If there are no generic associations, the generic parameters appear
2913 -- as local entities and are instantiated like them. We copy the generic
2914 -- package declaration as if it were an instantiation, and analyze it
2915 -- like a regular package, except that we treat the formals as
2916 -- additional visible components.
2918 Gen_Decl
:= Unit_Declaration_Node
(Gen_Unit
);
2920 if In_Extended_Main_Source_Unit
(N
) then
2921 Set_Is_Instantiated
(Gen_Unit
);
2922 Generate_Reference
(Gen_Unit
, N
);
2925 Formal
:= New_Copy
(Pack_Id
);
2926 Create_Instantiation_Source
(N
, Gen_Unit
, S_Adjustment
);
2928 -- Make local generic without formals. The formals will be replaced with
2929 -- internal declarations.
2932 New_N
:= Build_Local_Package
;
2934 -- If there are errors in the parameter list, Analyze_Associations
2935 -- raises Instantiation_Error. Patch the declaration to prevent further
2936 -- exception propagation.
2939 when Instantiation_Error
=>
2940 Enter_Name
(Formal
);
2941 Set_Ekind
(Formal
, E_Variable
);
2942 Set_Etype
(Formal
, Any_Type
);
2943 Restore_Hidden_Primitives
(Vis_Prims_List
);
2945 if Parent_Installed
then
2953 Set_Defining_Unit_Name
(Specification
(New_N
), Formal
);
2954 Set_Generic_Parent
(Specification
(N
), Gen_Unit
);
2955 Set_Instance_Env
(Gen_Unit
, Formal
);
2956 Set_Is_Generic_Instance
(Formal
);
2958 Enter_Name
(Formal
);
2959 Set_Ekind
(Formal
, E_Package
);
2960 Set_Etype
(Formal
, Standard_Void_Type
);
2961 Set_Inner_Instances
(Formal
, New_Elmt_List
);
2962 Push_Scope
(Formal
);
2964 -- Manually set the SPARK_Mode from the context because the package
2965 -- declaration is never analyzed.
2967 Set_SPARK_Pragma
(Formal
, SPARK_Mode_Pragma
);
2968 Set_SPARK_Aux_Pragma
(Formal
, SPARK_Mode_Pragma
);
2969 Set_SPARK_Pragma_Inherited
(Formal
);
2970 Set_SPARK_Aux_Pragma_Inherited
(Formal
);
2972 if Is_Child_Unit
(Gen_Unit
) and then Parent_Installed
then
2974 -- Similarly, we have to make the name of the formal visible in the
2975 -- parent instance, to resolve properly fully qualified names that
2976 -- may appear in the generic unit. The parent instance has been
2977 -- placed on the scope stack ahead of the current scope.
2979 Parent_Instance
:= Scope_Stack
.Table
(Scope_Stack
.Last
- 1).Entity
;
2982 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
));
2983 Set_Ekind
(Renaming_In_Par
, E_Package
);
2984 Set_Etype
(Renaming_In_Par
, Standard_Void_Type
);
2985 Set_Scope
(Renaming_In_Par
, Parent_Instance
);
2986 Set_Parent
(Renaming_In_Par
, Parent
(Formal
));
2987 Set_Renamed_Object
(Renaming_In_Par
, Formal
);
2988 Append_Entity
(Renaming_In_Par
, Parent_Instance
);
2991 -- A formal package declaration behaves as a package instantiation with
2992 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
2993 -- missing, set the global flag which signals Analyze_Pragma to ingnore
2994 -- all SPARK_Mode pragmas within the generic_package_name.
2996 if SPARK_Mode
/= On
then
2997 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
2999 -- Mark the formal spec in case the body is instantiated at a later
3000 -- pass. This preserves the original context in effect for the body.
3002 Set_Ignore_SPARK_Mode_Pragmas
(Formal
);
3005 Analyze
(Specification
(N
));
3007 -- The formals for which associations are provided are not visible
3008 -- outside of the formal package. The others are still declared by a
3009 -- formal parameter declaration.
3011 -- If there are no associations, the only local entity to hide is the
3012 -- generated package renaming itself.
3018 E
:= First_Entity
(Formal
);
3019 while Present
(E
) loop
3020 if Associations
and then not Is_Generic_Formal
(E
) then
3024 if Ekind
(E
) = E_Package
and then Renamed_Entity
(E
) = Formal
then
3033 End_Package_Scope
(Formal
);
3034 Restore_Hidden_Primitives
(Vis_Prims_List
);
3036 if Parent_Installed
then
3042 -- Inside the generic unit, the formal package is a regular package, but
3043 -- no body is needed for it. Note that after instantiation, the defining
3044 -- unit name we need is in the new tree and not in the original (see
3045 -- Package_Instantiation). A generic formal package is an instance, and
3046 -- can be used as an actual for an inner instance.
3048 Set_Has_Completion
(Formal
, True);
3050 -- Add semantic information to the original defining identifier for ASIS
3053 Set_Ekind
(Pack_Id
, E_Package
);
3054 Set_Etype
(Pack_Id
, Standard_Void_Type
);
3055 Set_Scope
(Pack_Id
, Scope
(Formal
));
3056 Set_Has_Completion
(Pack_Id
, True);
3059 if Has_Aspects
(N
) then
3060 Analyze_Aspect_Specifications
(N
, Pack_Id
);
3063 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Save_ISMP
;
3064 end Analyze_Formal_Package_Declaration
;
3066 ---------------------------------
3067 -- Analyze_Formal_Private_Type --
3068 ---------------------------------
3070 procedure Analyze_Formal_Private_Type
3076 New_Private_Type
(N
, T
, Def
);
3078 -- Set the size to an arbitrary but legal value
3080 Set_Size_Info
(T
, Standard_Integer
);
3081 Set_RM_Size
(T
, RM_Size
(Standard_Integer
));
3082 end Analyze_Formal_Private_Type
;
3084 ------------------------------------
3085 -- Analyze_Formal_Incomplete_Type --
3086 ------------------------------------
3088 procedure Analyze_Formal_Incomplete_Type
3094 Set_Ekind
(T
, E_Incomplete_Type
);
3096 Set_Private_Dependents
(T
, New_Elmt_List
);
3098 if Tagged_Present
(Def
) then
3099 Set_Is_Tagged_Type
(T
);
3100 Make_Class_Wide_Type
(T
);
3101 Set_Direct_Primitive_Operations
(T
, New_Elmt_List
);
3103 end Analyze_Formal_Incomplete_Type
;
3105 ----------------------------------------
3106 -- Analyze_Formal_Signed_Integer_Type --
3107 ----------------------------------------
3109 procedure Analyze_Formal_Signed_Integer_Type
3113 Base
: constant Entity_Id
:=
3115 (E_Signed_Integer_Type
,
3117 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
3122 Set_Ekind
(T
, E_Signed_Integer_Subtype
);
3123 Set_Etype
(T
, Base
);
3124 Set_Size_Info
(T
, Standard_Integer
);
3125 Set_RM_Size
(T
, RM_Size
(Standard_Integer
));
3126 Set_Scalar_Range
(T
, Scalar_Range
(Standard_Integer
));
3127 Set_Is_Constrained
(T
);
3129 Set_Is_Generic_Type
(Base
);
3130 Set_Size_Info
(Base
, Standard_Integer
);
3131 Set_RM_Size
(Base
, RM_Size
(Standard_Integer
));
3132 Set_Etype
(Base
, Base
);
3133 Set_Scalar_Range
(Base
, Scalar_Range
(Standard_Integer
));
3134 Set_Parent
(Base
, Parent
(Def
));
3135 end Analyze_Formal_Signed_Integer_Type
;
3137 -------------------------------------------
3138 -- Analyze_Formal_Subprogram_Declaration --
3139 -------------------------------------------
3141 procedure Analyze_Formal_Subprogram_Declaration
(N
: Node_Id
) is
3142 Spec
: constant Node_Id
:= Specification
(N
);
3143 Def
: constant Node_Id
:= Default_Name
(N
);
3144 Nam
: constant Entity_Id
:= Defining_Unit_Name
(Spec
);
3152 if Nkind
(Nam
) = N_Defining_Program_Unit_Name
then
3153 Error_Msg_N
("name of formal subprogram must be a direct name", Nam
);
3157 Analyze_Subprogram_Declaration
(N
);
3158 Set_Is_Formal_Subprogram
(Nam
);
3159 Set_Has_Completion
(Nam
);
3161 if Nkind
(N
) = N_Formal_Abstract_Subprogram_Declaration
then
3162 Set_Is_Abstract_Subprogram
(Nam
);
3164 Set_Is_Dispatching_Operation
(Nam
);
3166 -- A formal abstract procedure cannot have a null default
3167 -- (RM 12.6(4.1/2)).
3169 if Nkind
(Spec
) = N_Procedure_Specification
3170 and then Null_Present
(Spec
)
3173 ("a formal abstract subprogram cannot default to null", Spec
);
3177 Ctrl_Type
: constant Entity_Id
:= Find_Dispatching_Type
(Nam
);
3179 if No
(Ctrl_Type
) then
3181 ("abstract formal subprogram must have a controlling type",
3184 elsif Ada_Version
>= Ada_2012
3185 and then Is_Incomplete_Type
(Ctrl_Type
)
3188 ("controlling type of abstract formal subprogram cannot "
3189 & "be incomplete type", N
, Ctrl_Type
);
3192 Check_Controlling_Formals
(Ctrl_Type
, Nam
);
3197 -- Default name is resolved at the point of instantiation
3199 if Box_Present
(N
) then
3202 -- Else default is bound at the point of generic declaration
3204 elsif Present
(Def
) then
3205 if Nkind
(Def
) = N_Operator_Symbol
then
3206 Find_Direct_Name
(Def
);
3208 elsif Nkind
(Def
) /= N_Attribute_Reference
then
3212 -- For an attribute reference, analyze the prefix and verify
3213 -- that it has the proper profile for the subprogram.
3215 Analyze
(Prefix
(Def
));
3216 Valid_Default_Attribute
(Nam
, Def
);
3220 -- Default name may be overloaded, in which case the interpretation
3221 -- with the correct profile must be selected, as for a renaming.
3222 -- If the definition is an indexed component, it must denote a
3223 -- member of an entry family. If it is a selected component, it
3224 -- can be a protected operation.
3226 if Etype
(Def
) = Any_Type
then
3229 elsif Nkind
(Def
) = N_Selected_Component
then
3230 if not Is_Overloadable
(Entity
(Selector_Name
(Def
))) then
3231 Error_Msg_N
("expect valid subprogram name as default", Def
);
3234 elsif Nkind
(Def
) = N_Indexed_Component
then
3235 if Is_Entity_Name
(Prefix
(Def
)) then
3236 if Ekind
(Entity
(Prefix
(Def
))) /= E_Entry_Family
then
3237 Error_Msg_N
("expect valid subprogram name as default", Def
);
3240 elsif Nkind
(Prefix
(Def
)) = N_Selected_Component
then
3241 if Ekind
(Entity
(Selector_Name
(Prefix
(Def
)))) /=
3244 Error_Msg_N
("expect valid subprogram name as default", Def
);
3248 Error_Msg_N
("expect valid subprogram name as default", Def
);
3252 elsif Nkind
(Def
) = N_Character_Literal
then
3254 -- Needs some type checks: subprogram should be parameterless???
3256 Resolve
(Def
, (Etype
(Nam
)));
3258 elsif not Is_Entity_Name
(Def
)
3259 or else not Is_Overloadable
(Entity
(Def
))
3261 Error_Msg_N
("expect valid subprogram name as default", Def
);
3264 elsif not Is_Overloaded
(Def
) then
3265 Subp
:= Entity
(Def
);
3268 Error_Msg_N
("premature usage of formal subprogram", Def
);
3270 elsif not Entity_Matches_Spec
(Subp
, Nam
) then
3271 Error_Msg_N
("no visible entity matches specification", Def
);
3274 -- More than one interpretation, so disambiguate as for a renaming
3279 I1
: Interp_Index
:= 0;
3285 Get_First_Interp
(Def
, I
, It
);
3286 while Present
(It
.Nam
) loop
3287 if Entity_Matches_Spec
(It
.Nam
, Nam
) then
3288 if Subp
/= Any_Id
then
3289 It1
:= Disambiguate
(Def
, I1
, I
, Etype
(Subp
));
3291 if It1
= No_Interp
then
3292 Error_Msg_N
("ambiguous default subprogram", Def
);
3305 Get_Next_Interp
(I
, It
);
3309 if Subp
/= Any_Id
then
3311 -- Subprogram found, generate reference to it
3313 Set_Entity
(Def
, Subp
);
3314 Generate_Reference
(Subp
, Def
);
3317 Error_Msg_N
("premature usage of formal subprogram", Def
);
3319 elsif Ekind
(Subp
) /= E_Operator
then
3320 Check_Mode_Conformant
(Subp
, Nam
);
3324 Error_Msg_N
("no visible subprogram matches specification", N
);
3330 if Has_Aspects
(N
) then
3331 Analyze_Aspect_Specifications
(N
, Nam
);
3334 end Analyze_Formal_Subprogram_Declaration
;
3336 -------------------------------------
3337 -- Analyze_Formal_Type_Declaration --
3338 -------------------------------------
3340 procedure Analyze_Formal_Type_Declaration
(N
: Node_Id
) is
3341 Def
: constant Node_Id
:= Formal_Type_Definition
(N
);
3345 T
:= Defining_Identifier
(N
);
3347 if Present
(Discriminant_Specifications
(N
))
3348 and then Nkind
(Def
) /= N_Formal_Private_Type_Definition
3351 ("discriminants not allowed for this formal type", T
);
3354 -- Enter the new name, and branch to specific routine
3357 when N_Formal_Private_Type_Definition
=>
3358 Analyze_Formal_Private_Type
(N
, T
, Def
);
3360 when N_Formal_Derived_Type_Definition
=>
3361 Analyze_Formal_Derived_Type
(N
, T
, Def
);
3363 when N_Formal_Incomplete_Type_Definition
=>
3364 Analyze_Formal_Incomplete_Type
(T
, Def
);
3366 when N_Formal_Discrete_Type_Definition
=>
3367 Analyze_Formal_Discrete_Type
(T
, Def
);
3369 when N_Formal_Signed_Integer_Type_Definition
=>
3370 Analyze_Formal_Signed_Integer_Type
(T
, Def
);
3372 when N_Formal_Modular_Type_Definition
=>
3373 Analyze_Formal_Modular_Type
(T
, Def
);
3375 when N_Formal_Floating_Point_Definition
=>
3376 Analyze_Formal_Floating_Type
(T
, Def
);
3378 when N_Formal_Ordinary_Fixed_Point_Definition
=>
3379 Analyze_Formal_Ordinary_Fixed_Point_Type
(T
, Def
);
3381 when N_Formal_Decimal_Fixed_Point_Definition
=>
3382 Analyze_Formal_Decimal_Fixed_Point_Type
(T
, Def
);
3384 when N_Array_Type_Definition
=>
3385 Analyze_Formal_Array_Type
(T
, Def
);
3387 when N_Access_Function_Definition
3388 | N_Access_Procedure_Definition
3389 | N_Access_To_Object_Definition
3391 Analyze_Generic_Access_Type
(T
, Def
);
3393 -- Ada 2005: a interface declaration is encoded as an abstract
3394 -- record declaration or a abstract type derivation.
3396 when N_Record_Definition
=>
3397 Analyze_Formal_Interface_Type
(N
, T
, Def
);
3399 when N_Derived_Type_Definition
=>
3400 Analyze_Formal_Derived_Interface_Type
(N
, T
, Def
);
3406 raise Program_Error
;
3409 Set_Is_Generic_Type
(T
);
3411 if Has_Aspects
(N
) then
3412 Analyze_Aspect_Specifications
(N
, T
);
3414 end Analyze_Formal_Type_Declaration
;
3416 ------------------------------------
3417 -- Analyze_Function_Instantiation --
3418 ------------------------------------
3420 procedure Analyze_Function_Instantiation
(N
: Node_Id
) is
3422 Analyze_Subprogram_Instantiation
(N
, E_Function
);
3423 end Analyze_Function_Instantiation
;
3425 ---------------------------------
3426 -- Analyze_Generic_Access_Type --
3427 ---------------------------------
3429 procedure Analyze_Generic_Access_Type
(T
: Entity_Id
; Def
: Node_Id
) is
3433 if Nkind
(Def
) = N_Access_To_Object_Definition
then
3434 Access_Type_Declaration
(T
, Def
);
3436 if Is_Incomplete_Or_Private_Type
(Designated_Type
(T
))
3437 and then No
(Full_View
(Designated_Type
(T
)))
3438 and then not Is_Generic_Type
(Designated_Type
(T
))
3440 Error_Msg_N
("premature usage of incomplete type", Def
);
3442 elsif not Is_Entity_Name
(Subtype_Indication
(Def
)) then
3444 ("only a subtype mark is allowed in a formal", Def
);
3448 Access_Subprogram_Declaration
(T
, Def
);
3450 end Analyze_Generic_Access_Type
;
3452 ---------------------------------
3453 -- Analyze_Generic_Formal_Part --
3454 ---------------------------------
3456 procedure Analyze_Generic_Formal_Part
(N
: Node_Id
) is
3457 Gen_Parm_Decl
: Node_Id
;
3460 -- The generic formals are processed in the scope of the generic unit,
3461 -- where they are immediately visible. The scope is installed by the
3464 Gen_Parm_Decl
:= First
(Generic_Formal_Declarations
(N
));
3465 while Present
(Gen_Parm_Decl
) loop
3466 Analyze
(Gen_Parm_Decl
);
3467 Next
(Gen_Parm_Decl
);
3470 Generate_Reference_To_Generic_Formals
(Current_Scope
);
3471 end Analyze_Generic_Formal_Part
;
3473 ------------------------------------------
3474 -- Analyze_Generic_Package_Declaration --
3475 ------------------------------------------
3477 procedure Analyze_Generic_Package_Declaration
(N
: Node_Id
) is
3478 Decls
: constant List_Id
:= Visible_Declarations
(Specification
(N
));
3479 Loc
: constant Source_Ptr
:= Sloc
(N
);
3485 Save_Parent
: Node_Id
;
3488 Check_SPARK_05_Restriction
("generic is not allowed", N
);
3490 -- We introduce a renaming of the enclosing package, to have a usable
3491 -- entity as the prefix of an expanded name for a local entity of the
3492 -- form Par.P.Q, where P is the generic package. This is because a local
3493 -- entity named P may hide it, so that the usual visibility rules in
3494 -- the instance will not resolve properly.
3497 Make_Package_Renaming_Declaration
(Loc
,
3498 Defining_Unit_Name
=>
3499 Make_Defining_Identifier
(Loc
,
3500 Chars
=> New_External_Name
(Chars
(Defining_Entity
(N
)), "GH")),
3502 Make_Identifier
(Loc
, Chars
(Defining_Entity
(N
))));
3504 -- The declaration is inserted before other declarations, but before
3505 -- pragmas that may be library-unit pragmas and must appear before other
3506 -- declarations. The pragma Compile_Time_Error is not in this class, and
3507 -- may contain an expression that includes such a qualified name, so the
3508 -- renaming declaration must appear before it.
3510 -- Are there other pragmas that require this special handling ???
3512 if Present
(Decls
) then
3513 Decl
:= First
(Decls
);
3514 while Present
(Decl
)
3515 and then Nkind
(Decl
) = N_Pragma
3516 and then Get_Pragma_Id
(Decl
) /= Pragma_Compile_Time_Error
3521 if Present
(Decl
) then
3522 Insert_Before
(Decl
, Renaming
);
3524 Append
(Renaming
, Visible_Declarations
(Specification
(N
)));
3528 Set_Visible_Declarations
(Specification
(N
), New_List
(Renaming
));
3531 -- Create copy of generic unit, and save for instantiation. If the unit
3532 -- is a child unit, do not copy the specifications for the parent, which
3533 -- are not part of the generic tree.
3535 Save_Parent
:= Parent_Spec
(N
);
3536 Set_Parent_Spec
(N
, Empty
);
3538 New_N
:= Copy_Generic_Node
(N
, Empty
, Instantiating
=> False);
3539 Set_Parent_Spec
(New_N
, Save_Parent
);
3542 -- Once the contents of the generic copy and the template are swapped,
3543 -- do the same for their respective aspect specifications.
3545 Exchange_Aspects
(N
, New_N
);
3547 -- Collect all contract-related source pragmas found within the template
3548 -- and attach them to the contract of the package spec. This contract is
3549 -- used in the capture of global references within annotations.
3551 Create_Generic_Contract
(N
);
3553 Id
:= Defining_Entity
(N
);
3554 Generate_Definition
(Id
);
3556 -- Expansion is not applied to generic units
3561 Set_Ekind
(Id
, E_Generic_Package
);
3562 Set_Etype
(Id
, Standard_Void_Type
);
3564 -- Set SPARK_Mode from context
3566 Set_SPARK_Pragma
(Id
, SPARK_Mode_Pragma
);
3567 Set_SPARK_Aux_Pragma
(Id
, SPARK_Mode_Pragma
);
3568 Set_SPARK_Pragma_Inherited
(Id
);
3569 Set_SPARK_Aux_Pragma_Inherited
(Id
);
3571 -- Preserve relevant elaboration-related attributes of the context which
3572 -- are no longer available or very expensive to recompute once analysis,
3573 -- resolution, and expansion are over.
3575 Mark_Elaboration_Attributes
3579 -- Analyze aspects now, so that generated pragmas appear in the
3580 -- declarations before building and analyzing the generic copy.
3582 if Has_Aspects
(N
) then
3583 Analyze_Aspect_Specifications
(N
, Id
);
3587 Enter_Generic_Scope
(Id
);
3588 Set_Inner_Instances
(Id
, New_Elmt_List
);
3590 Set_Categorization_From_Pragmas
(N
);
3591 Set_Is_Pure
(Id
, Is_Pure
(Current_Scope
));
3593 -- Link the declaration of the generic homonym in the generic copy to
3594 -- the package it renames, so that it is always resolved properly.
3596 Set_Generic_Homonym
(Id
, Defining_Unit_Name
(Renaming
));
3597 Set_Entity
(Associated_Node
(Name
(Renaming
)), Id
);
3599 -- For a library unit, we have reconstructed the entity for the unit,
3600 -- and must reset it in the library tables.
3602 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
3603 Set_Cunit_Entity
(Current_Sem_Unit
, Id
);
3606 Analyze_Generic_Formal_Part
(N
);
3608 -- After processing the generic formals, analysis proceeds as for a
3609 -- non-generic package.
3611 Analyze
(Specification
(N
));
3613 Validate_Categorization_Dependency
(N
, Id
);
3617 End_Package_Scope
(Id
);
3618 Exit_Generic_Scope
(Id
);
3620 -- If the generic appears within a package unit, the body of that unit
3621 -- has to be present for instantiation and inlining.
3623 if Nkind
(Unit
(Cunit
(Current_Sem_Unit
))) = N_Package_Declaration
then
3624 Set_Body_Needed_For_Inlining
3625 (Defining_Entity
(Unit
(Cunit
(Current_Sem_Unit
))));
3628 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
3629 Move_Freeze_Nodes
(Id
, N
, Visible_Declarations
(Specification
(N
)));
3630 Move_Freeze_Nodes
(Id
, N
, Private_Declarations
(Specification
(N
)));
3631 Move_Freeze_Nodes
(Id
, N
, Generic_Formal_Declarations
(N
));
3634 Set_Body_Required
(Parent
(N
), Unit_Requires_Body
(Id
));
3635 Validate_RT_RAT_Component
(N
);
3637 -- If this is a spec without a body, check that generic parameters
3640 if not Body_Required
(Parent
(N
)) then
3641 Check_References
(Id
);
3645 -- If there is a specified storage pool in the context, create an
3646 -- aspect on the package declaration, so that it is used in any
3647 -- instance that does not override it.
3649 if Present
(Default_Pool
) then
3655 Make_Aspect_Specification
(Loc
,
3656 Identifier
=> Make_Identifier
(Loc
, Name_Default_Storage_Pool
),
3657 Expression
=> New_Copy
(Default_Pool
));
3659 if No
(Aspect_Specifications
(Specification
(N
))) then
3660 Set_Aspect_Specifications
(Specification
(N
), New_List
(ASN
));
3662 Append
(ASN
, Aspect_Specifications
(Specification
(N
)));
3666 end Analyze_Generic_Package_Declaration
;
3668 --------------------------------------------
3669 -- Analyze_Generic_Subprogram_Declaration --
3670 --------------------------------------------
3672 procedure Analyze_Generic_Subprogram_Declaration
(N
: Node_Id
) is
3676 Result_Type
: Entity_Id
;
3677 Save_Parent
: Node_Id
;
3682 Check_SPARK_05_Restriction
("generic is not allowed", N
);
3684 -- Create copy of generic unit, and save for instantiation. If the unit
3685 -- is a child unit, do not copy the specifications for the parent, which
3686 -- are not part of the generic tree.
3688 Save_Parent
:= Parent_Spec
(N
);
3689 Set_Parent_Spec
(N
, Empty
);
3691 New_N
:= Copy_Generic_Node
(N
, Empty
, Instantiating
=> False);
3692 Set_Parent_Spec
(New_N
, Save_Parent
);
3695 -- Once the contents of the generic copy and the template are swapped,
3696 -- do the same for their respective aspect specifications.
3698 Exchange_Aspects
(N
, New_N
);
3700 -- Collect all contract-related source pragmas found within the template
3701 -- and attach them to the contract of the subprogram spec. This contract
3702 -- is used in the capture of global references within annotations.
3704 Create_Generic_Contract
(N
);
3706 Spec
:= Specification
(N
);
3707 Id
:= Defining_Entity
(Spec
);
3708 Generate_Definition
(Id
);
3710 if Nkind
(Id
) = N_Defining_Operator_Symbol
then
3712 ("operator symbol not allowed for generic subprogram", Id
);
3718 Set_Scope_Depth_Value
(Id
, Scope_Depth
(Current_Scope
) + 1);
3720 -- Analyze the aspects of the generic copy to ensure that all generated
3721 -- pragmas (if any) perform their semantic effects.
3723 if Has_Aspects
(N
) then
3724 Analyze_Aspect_Specifications
(N
, Id
);
3728 Enter_Generic_Scope
(Id
);
3729 Set_Inner_Instances
(Id
, New_Elmt_List
);
3730 Set_Is_Pure
(Id
, Is_Pure
(Current_Scope
));
3732 Analyze_Generic_Formal_Part
(N
);
3734 if Nkind
(Spec
) = N_Function_Specification
then
3735 Set_Ekind
(Id
, E_Generic_Function
);
3737 Set_Ekind
(Id
, E_Generic_Procedure
);
3740 -- Set SPARK_Mode from context
3742 Set_SPARK_Pragma
(Id
, SPARK_Mode_Pragma
);
3743 Set_SPARK_Pragma_Inherited
(Id
);
3745 -- Preserve relevant elaboration-related attributes of the context which
3746 -- are no longer available or very expensive to recompute once analysis,
3747 -- resolution, and expansion are over.
3749 Mark_Elaboration_Attributes
3753 Formals
:= Parameter_Specifications
(Spec
);
3755 if Present
(Formals
) then
3756 Process_Formals
(Formals
, Spec
);
3759 if Nkind
(Spec
) = N_Function_Specification
then
3760 if Nkind
(Result_Definition
(Spec
)) = N_Access_Definition
then
3761 Result_Type
:= Access_Definition
(Spec
, Result_Definition
(Spec
));
3762 Set_Etype
(Id
, Result_Type
);
3764 -- Check restriction imposed by AI05-073: a generic function
3765 -- cannot return an abstract type or an access to such.
3767 -- This is a binding interpretation should it apply to earlier
3768 -- versions of Ada as well as Ada 2012???
3770 if Is_Abstract_Type
(Designated_Type
(Result_Type
))
3771 and then Ada_Version
>= Ada_2012
3774 ("generic function cannot have an access result "
3775 & "that designates an abstract type", Spec
);
3779 Find_Type
(Result_Definition
(Spec
));
3780 Typ
:= Entity
(Result_Definition
(Spec
));
3782 if Is_Abstract_Type
(Typ
)
3783 and then Ada_Version
>= Ada_2012
3786 ("generic function cannot have abstract result type", Spec
);
3789 -- If a null exclusion is imposed on the result type, then create
3790 -- a null-excluding itype (an access subtype) and use it as the
3791 -- function's Etype.
3793 if Is_Access_Type
(Typ
)
3794 and then Null_Exclusion_Present
(Spec
)
3797 Create_Null_Excluding_Itype
3799 Related_Nod
=> Spec
,
3800 Scope_Id
=> Defining_Unit_Name
(Spec
)));
3802 Set_Etype
(Id
, Typ
);
3807 Set_Etype
(Id
, Standard_Void_Type
);
3810 -- For a library unit, we have reconstructed the entity for the unit,
3811 -- and must reset it in the library tables. We also make sure that
3812 -- Body_Required is set properly in the original compilation unit node.
3814 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
3815 Set_Cunit_Entity
(Current_Sem_Unit
, Id
);
3816 Set_Body_Required
(Parent
(N
), Unit_Requires_Body
(Id
));
3819 -- If the generic appears within a package unit, the body of that unit
3820 -- has to be present for instantiation and inlining.
3822 if Nkind
(Unit
(Cunit
(Current_Sem_Unit
))) = N_Package_Declaration
3823 and then Unit_Requires_Body
(Id
)
3825 Set_Body_Needed_For_Inlining
3826 (Defining_Entity
(Unit
(Cunit
(Current_Sem_Unit
))));
3829 Set_Categorization_From_Pragmas
(N
);
3830 Validate_Categorization_Dependency
(N
, Id
);
3832 -- Capture all global references that occur within the profile of the
3833 -- generic subprogram. Aspects are not part of this processing because
3834 -- they must be delayed. If processed now, Save_Global_References will
3835 -- destroy the Associated_Node links and prevent the capture of global
3836 -- references when the contract of the generic subprogram is analyzed.
3838 Save_Global_References
(Original_Node
(N
));
3842 Exit_Generic_Scope
(Id
);
3843 Generate_Reference_To_Formals
(Id
);
3845 List_Inherited_Pre_Post_Aspects
(Id
);
3846 end Analyze_Generic_Subprogram_Declaration
;
3848 -----------------------------------
3849 -- Analyze_Package_Instantiation --
3850 -----------------------------------
3852 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
3853 -- must be replaced by gotos which jump to the end of the routine in order
3854 -- to restore the Ghost and SPARK modes.
3856 procedure Analyze_Package_Instantiation
(N
: Node_Id
) is
3857 Has_Inline_Always
: Boolean := False;
3859 procedure Delay_Descriptors
(E
: Entity_Id
);
3860 -- Delay generation of subprogram descriptors for given entity
3862 function Might_Inline_Subp
(Gen_Unit
: Entity_Id
) return Boolean;
3863 -- If inlining is active and the generic contains inlined subprograms,
3864 -- we instantiate the body. This may cause superfluous instantiations,
3865 -- but it is simpler than detecting the need for the body at the point
3866 -- of inlining, when the context of the instance is not available.
3868 -----------------------
3869 -- Delay_Descriptors --
3870 -----------------------
3872 procedure Delay_Descriptors
(E
: Entity_Id
) is
3874 if not Delay_Subprogram_Descriptors
(E
) then
3875 Set_Delay_Subprogram_Descriptors
(E
);
3876 Pending_Descriptor
.Append
(E
);
3878 end Delay_Descriptors
;
3880 -----------------------
3881 -- Might_Inline_Subp --
3882 -----------------------
3884 function Might_Inline_Subp
(Gen_Unit
: Entity_Id
) return Boolean is
3888 if not Inline_Processing_Required
then
3892 E
:= First_Entity
(Gen_Unit
);
3893 while Present
(E
) loop
3894 if Is_Subprogram
(E
) and then Is_Inlined
(E
) then
3895 -- Remember if there are any subprograms with Inline_Always
3897 if Has_Pragma_Inline_Always
(E
) then
3898 Has_Inline_Always
:= True;
3909 end Might_Inline_Subp
;
3911 -- Local declarations
3913 Gen_Id
: constant Node_Id
:= Name
(N
);
3914 Is_Actual_Pack
: constant Boolean :=
3915 Is_Internal
(Defining_Entity
(N
));
3916 Loc
: constant Source_Ptr
:= Sloc
(N
);
3918 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
3919 Saved_ISMP
: constant Boolean :=
3920 Ignore_SPARK_Mode_Pragmas_In_Instance
;
3921 Saved_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
3922 Saved_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
3923 -- Save the Ghost and SPARK mode-related data to restore on exit
3925 Saved_Style_Check
: constant Boolean := Style_Check
;
3926 -- Save style check mode for restore on exit
3929 Act_Decl_Name
: Node_Id
;
3930 Act_Decl_Id
: Entity_Id
;
3933 Env_Installed
: Boolean := False;
3936 Gen_Unit
: Entity_Id
;
3937 Inline_Now
: Boolean := False;
3938 Needs_Body
: Boolean;
3939 Parent_Installed
: Boolean := False;
3940 Renaming_List
: List_Id
;
3941 Unit_Renaming
: Node_Id
;
3943 Vis_Prims_List
: Elist_Id
:= No_Elist
;
3944 -- List of primitives made temporarily visible in the instantiation
3945 -- to match the visibility of the formal type
3947 -- Start of processing for Analyze_Package_Instantiation
3950 -- Preserve relevant elaboration-related attributes of the context which
3951 -- are no longer available or very expensive to recompute once analysis,
3952 -- resolution, and expansion are over.
3954 Mark_Elaboration_Attributes
3961 Check_SPARK_05_Restriction
("generic is not allowed", N
);
3963 -- Very first thing: check for Text_IO special unit in case we are
3964 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3966 Check_Text_IO_Special_Unit
(Name
(N
));
3968 -- Make node global for error reporting
3970 Instantiation_Node
:= N
;
3972 -- Case of instantiation of a generic package
3974 if Nkind
(N
) = N_Package_Instantiation
then
3975 Act_Decl_Id
:= New_Copy
(Defining_Entity
(N
));
3976 Set_Comes_From_Source
(Act_Decl_Id
, True);
3978 if Nkind
(Defining_Unit_Name
(N
)) = N_Defining_Program_Unit_Name
then
3980 Make_Defining_Program_Unit_Name
(Loc
,
3982 New_Copy_Tree
(Name
(Defining_Unit_Name
(N
))),
3983 Defining_Identifier
=> Act_Decl_Id
);
3985 Act_Decl_Name
:= Act_Decl_Id
;
3988 -- Case of instantiation of a formal package
3991 Act_Decl_Id
:= Defining_Identifier
(N
);
3992 Act_Decl_Name
:= Act_Decl_Id
;
3995 Generate_Definition
(Act_Decl_Id
);
3996 Set_Ekind
(Act_Decl_Id
, E_Package
);
3998 -- Initialize list of incomplete actuals before analysis
4000 Set_Incomplete_Actuals
(Act_Decl_Id
, New_Elmt_List
);
4002 Preanalyze_Actuals
(N
, Act_Decl_Id
);
4004 -- Turn off style checking in instances. If the check is enabled on the
4005 -- generic unit, a warning in an instance would just be noise. If not
4006 -- enabled on the generic, then a warning in an instance is just wrong.
4007 -- This must be done after analyzing the actuals, which do come from
4008 -- source and are subject to style checking.
4010 Style_Check
:= False;
4013 Env_Installed
:= True;
4015 -- Reset renaming map for formal types. The mapping is established
4016 -- when analyzing the generic associations, but some mappings are
4017 -- inherited from formal packages of parent units, and these are
4018 -- constructed when the parents are installed.
4020 Generic_Renamings
.Set_Last
(0);
4021 Generic_Renamings_HTable
.Reset
;
4023 Check_Generic_Child_Unit
(Gen_Id
, Parent_Installed
);
4024 Gen_Unit
:= Entity
(Gen_Id
);
4026 -- A package instantiation is Ghost when it is subject to pragma Ghost
4027 -- or the generic template is Ghost. Set the mode now to ensure that
4028 -- any nodes generated during analysis and expansion are marked as
4031 Mark_And_Set_Ghost_Instantiation
(N
, Gen_Unit
);
4033 -- Verify that it is the name of a generic package
4035 -- A visibility glitch: if the instance is a child unit and the generic
4036 -- is the generic unit of a parent instance (i.e. both the parent and
4037 -- the child units are instances of the same package) the name now
4038 -- denotes the renaming within the parent, not the intended generic
4039 -- unit. See if there is a homonym that is the desired generic. The
4040 -- renaming declaration must be visible inside the instance of the
4041 -- child, but not when analyzing the name in the instantiation itself.
4043 if Ekind
(Gen_Unit
) = E_Package
4044 and then Present
(Renamed_Entity
(Gen_Unit
))
4045 and then In_Open_Scopes
(Renamed_Entity
(Gen_Unit
))
4046 and then Is_Generic_Instance
(Renamed_Entity
(Gen_Unit
))
4047 and then Present
(Homonym
(Gen_Unit
))
4049 Gen_Unit
:= Homonym
(Gen_Unit
);
4052 if Etype
(Gen_Unit
) = Any_Type
then
4056 elsif Ekind
(Gen_Unit
) /= E_Generic_Package
then
4058 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4060 if From_Limited_With
(Gen_Unit
) then
4062 ("cannot instantiate a limited withed package", Gen_Id
);
4065 ("& is not the name of a generic package", Gen_Id
, Gen_Unit
);
4072 if In_Extended_Main_Source_Unit
(N
) then
4073 Set_Is_Instantiated
(Gen_Unit
);
4074 Generate_Reference
(Gen_Unit
, N
);
4076 if Present
(Renamed_Object
(Gen_Unit
)) then
4077 Set_Is_Instantiated
(Renamed_Object
(Gen_Unit
));
4078 Generate_Reference
(Renamed_Object
(Gen_Unit
), N
);
4082 if Nkind
(Gen_Id
) = N_Identifier
4083 and then Chars
(Gen_Unit
) = Chars
(Defining_Entity
(N
))
4086 ("& is hidden within declaration of instance", Gen_Id
, Gen_Unit
);
4088 elsif Nkind
(Gen_Id
) = N_Expanded_Name
4089 and then Is_Child_Unit
(Gen_Unit
)
4090 and then Nkind
(Prefix
(Gen_Id
)) = N_Identifier
4091 and then Chars
(Act_Decl_Id
) = Chars
(Prefix
(Gen_Id
))
4094 ("& is hidden within declaration of instance ", Prefix
(Gen_Id
));
4097 Set_Entity
(Gen_Id
, Gen_Unit
);
4099 -- If generic is a renaming, get original generic unit
4101 if Present
(Renamed_Object
(Gen_Unit
))
4102 and then Ekind
(Renamed_Object
(Gen_Unit
)) = E_Generic_Package
4104 Gen_Unit
:= Renamed_Object
(Gen_Unit
);
4107 -- Verify that there are no circular instantiations
4109 if In_Open_Scopes
(Gen_Unit
) then
4110 Error_Msg_NE
("instantiation of & within itself", N
, Gen_Unit
);
4114 elsif Contains_Instance_Of
(Gen_Unit
, Current_Scope
, Gen_Id
) then
4115 Error_Msg_Node_2
:= Current_Scope
;
4117 ("circular Instantiation: & instantiated in &!", N
, Gen_Unit
);
4118 Circularity_Detected
:= True;
4123 -- If the context of the instance is subject to SPARK_Mode "off" or
4124 -- the annotation is altogether missing, set the global flag which
4125 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4128 if SPARK_Mode
/= On
then
4129 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
4131 -- Mark the instance spec in case the body is instantiated at a
4132 -- later pass. This preserves the original context in effect for
4135 Set_Ignore_SPARK_Mode_Pragmas
(Act_Decl_Id
);
4138 Gen_Decl
:= Unit_Declaration_Node
(Gen_Unit
);
4139 Gen_Spec
:= Specification
(Gen_Decl
);
4141 -- Initialize renamings map, for error checking, and the list that
4142 -- holds private entities whose views have changed between generic
4143 -- definition and instantiation. If this is the instance created to
4144 -- validate an actual package, the instantiation environment is that
4145 -- of the enclosing instance.
4147 Create_Instantiation_Source
(N
, Gen_Unit
, S_Adjustment
);
4149 -- Copy original generic tree, to produce text for instantiation
4153 (Original_Node
(Gen_Decl
), Empty
, Instantiating
=> True);
4155 Act_Spec
:= Specification
(Act_Tree
);
4157 -- If this is the instance created to validate an actual package,
4158 -- only the formals matter, do not examine the package spec itself.
4160 if Is_Actual_Pack
then
4161 Set_Visible_Declarations
(Act_Spec
, New_List
);
4162 Set_Private_Declarations
(Act_Spec
, New_List
);
4166 Analyze_Associations
4168 Formals
=> Generic_Formal_Declarations
(Act_Tree
),
4169 F_Copy
=> Generic_Formal_Declarations
(Gen_Decl
));
4171 Vis_Prims_List
:= Check_Hidden_Primitives
(Renaming_List
);
4173 Set_Instance_Env
(Gen_Unit
, Act_Decl_Id
);
4174 Set_Defining_Unit_Name
(Act_Spec
, Act_Decl_Name
);
4175 Set_Is_Generic_Instance
(Act_Decl_Id
);
4176 Set_Generic_Parent
(Act_Spec
, Gen_Unit
);
4178 -- References to the generic in its own declaration or its body are
4179 -- references to the instance. Add a renaming declaration for the
4180 -- generic unit itself. This declaration, as well as the renaming
4181 -- declarations for the generic formals, must remain private to the
4182 -- unit: the formals, because this is the language semantics, and
4183 -- the unit because its use is an artifact of the implementation.
4186 Make_Package_Renaming_Declaration
(Loc
,
4187 Defining_Unit_Name
=>
4188 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
)),
4189 Name
=> New_Occurrence_Of
(Act_Decl_Id
, Loc
));
4191 Append
(Unit_Renaming
, Renaming_List
);
4193 -- The renaming declarations are the first local declarations of the
4196 if Is_Non_Empty_List
(Visible_Declarations
(Act_Spec
)) then
4198 (First
(Visible_Declarations
(Act_Spec
)), Renaming_List
);
4200 Set_Visible_Declarations
(Act_Spec
, Renaming_List
);
4203 Act_Decl
:= Make_Package_Declaration
(Loc
, Specification
=> Act_Spec
);
4205 -- Propagate the aspect specifications from the package declaration
4206 -- template to the instantiated version of the package declaration.
4208 if Has_Aspects
(Act_Tree
) then
4209 Set_Aspect_Specifications
(Act_Decl
,
4210 New_Copy_List_Tree
(Aspect_Specifications
(Act_Tree
)));
4213 -- The generic may have a generated Default_Storage_Pool aspect,
4214 -- set at the point of generic declaration. If the instance has
4215 -- that aspect, it overrides the one inherited from the generic.
4217 if Has_Aspects
(Gen_Spec
) then
4218 if No
(Aspect_Specifications
(N
)) then
4219 Set_Aspect_Specifications
(N
,
4221 (Aspect_Specifications
(Gen_Spec
))));
4225 ASN1
, ASN2
: Node_Id
;
4228 ASN1
:= First
(Aspect_Specifications
(N
));
4229 while Present
(ASN1
) loop
4230 if Chars
(Identifier
(ASN1
)) = Name_Default_Storage_Pool
4232 -- If generic carries a default storage pool, remove
4233 -- it in favor of the instance one.
4235 ASN2
:= First
(Aspect_Specifications
(Gen_Spec
));
4236 while Present
(ASN2
) loop
4237 if Chars
(Identifier
(ASN2
)) =
4238 Name_Default_Storage_Pool
4251 Prepend_List_To
(Aspect_Specifications
(N
),
4253 (Aspect_Specifications
(Gen_Spec
))));
4258 -- Save the instantiation node, for subsequent instantiation of the
4259 -- body, if there is one and we are generating code for the current
4260 -- unit. Mark unit as having a body (avoids premature error message).
4262 -- We instantiate the body if we are generating code, if we are
4263 -- generating cross-reference information, or if we are building
4264 -- trees for ASIS use or GNATprove use.
4267 Enclosing_Body_Present
: Boolean := False;
4268 -- If the generic unit is not a compilation unit, then a body may
4269 -- be present in its parent even if none is required. We create a
4270 -- tentative pending instantiation for the body, which will be
4271 -- discarded if none is actually present.
4276 if Scope
(Gen_Unit
) /= Standard_Standard
4277 and then not Is_Child_Unit
(Gen_Unit
)
4279 Scop
:= Scope
(Gen_Unit
);
4280 while Present
(Scop
) and then Scop
/= Standard_Standard
loop
4281 if Unit_Requires_Body
(Scop
) then
4282 Enclosing_Body_Present
:= True;
4285 elsif In_Open_Scopes
(Scop
)
4286 and then In_Package_Body
(Scop
)
4288 Enclosing_Body_Present
:= True;
4292 exit when Is_Compilation_Unit
(Scop
);
4293 Scop
:= Scope
(Scop
);
4297 -- If front-end inlining is enabled or there are any subprograms
4298 -- marked with Inline_Always, and this is a unit for which code
4299 -- will be generated, we instantiate the body at once.
4301 -- This is done if the instance is not the main unit, and if the
4302 -- generic is not a child unit of another generic, to avoid scope
4303 -- problems and the reinstallation of parent instances.
4306 and then (not Is_Child_Unit
(Gen_Unit
)
4307 or else not Is_Generic_Unit
(Scope
(Gen_Unit
)))
4308 and then Might_Inline_Subp
(Gen_Unit
)
4309 and then not Is_Actual_Pack
4311 if not Back_End_Inlining
4312 and then (Front_End_Inlining
or else Has_Inline_Always
)
4313 and then (Is_In_Main_Unit
(N
)
4314 or else In_Main_Context
(Current_Scope
))
4315 and then Nkind
(Parent
(N
)) /= N_Compilation_Unit
4319 -- In configurable_run_time mode we force the inlining of
4320 -- predefined subprograms marked Inline_Always, to minimize
4321 -- the use of the run-time library.
4323 elsif In_Predefined_Unit
(Gen_Decl
)
4324 and then Configurable_Run_Time_Mode
4325 and then Nkind
(Parent
(N
)) /= N_Compilation_Unit
4330 -- If the current scope is itself an instance within a child
4331 -- unit, there will be duplications in the scope stack, and the
4332 -- unstacking mechanism in Inline_Instance_Body will fail.
4333 -- This loses some rare cases of optimization, and might be
4334 -- improved some day, if we can find a proper abstraction for
4335 -- "the complete compilation context" that can be saved and
4338 if Is_Generic_Instance
(Current_Scope
) then
4340 Curr_Unit
: constant Entity_Id
:=
4341 Cunit_Entity
(Current_Sem_Unit
);
4343 if Curr_Unit
/= Current_Scope
4344 and then Is_Child_Unit
(Curr_Unit
)
4346 Inline_Now
:= False;
4353 (Unit_Requires_Body
(Gen_Unit
)
4354 or else Enclosing_Body_Present
4355 or else Present
(Corresponding_Body
(Gen_Decl
)))
4356 and then (Is_In_Main_Unit
(N
)
4357 or else Might_Inline_Subp
(Gen_Unit
))
4358 and then not Is_Actual_Pack
4359 and then not Inline_Now
4360 and then (Operating_Mode
= Generate_Code
4362 -- Need comment for this check ???
4364 or else (Operating_Mode
= Check_Semantics
4365 and then (ASIS_Mode
or GNATprove_Mode
)));
4367 -- If front-end inlining is enabled or there are any subprograms
4368 -- marked with Inline_Always, do not instantiate body when within
4369 -- a generic context.
4371 if ((Front_End_Inlining
or else Has_Inline_Always
)
4372 and then not Expander_Active
)
4373 or else Is_Generic_Unit
(Cunit_Entity
(Main_Unit
))
4375 Needs_Body
:= False;
4378 -- If the current context is generic, and the package being
4379 -- instantiated is declared within a formal package, there is no
4380 -- body to instantiate until the enclosing generic is instantiated
4381 -- and there is an actual for the formal package. If the formal
4382 -- package has parameters, we build a regular package instance for
4383 -- it, that precedes the original formal package declaration.
4385 if In_Open_Scopes
(Scope
(Scope
(Gen_Unit
))) then
4387 Decl
: constant Node_Id
:=
4389 (Unit_Declaration_Node
(Scope
(Gen_Unit
)));
4391 if Nkind
(Decl
) = N_Formal_Package_Declaration
4392 or else (Nkind
(Decl
) = N_Package_Declaration
4393 and then Is_List_Member
(Decl
)
4394 and then Present
(Next
(Decl
))
4396 Nkind
(Next
(Decl
)) =
4397 N_Formal_Package_Declaration
)
4399 Needs_Body
:= False;
4405 -- For RCI unit calling stubs, we omit the instance body if the
4406 -- instance is the RCI library unit itself.
4408 -- However there is a special case for nested instances: in this case
4409 -- we do generate the instance body, as it might be required, e.g.
4410 -- because it provides stream attributes for some type used in the
4411 -- profile of a remote subprogram. This is consistent with 12.3(12),
4412 -- which indicates that the instance body occurs at the place of the
4413 -- instantiation, and thus is part of the RCI declaration, which is
4414 -- present on all client partitions (this is E.2.3(18)).
4416 -- Note that AI12-0002 may make it illegal at some point to have
4417 -- stream attributes defined in an RCI unit, in which case this
4418 -- special case will become unnecessary. In the meantime, there
4419 -- is known application code in production that depends on this
4420 -- being possible, so we definitely cannot eliminate the body in
4421 -- the case of nested instances for the time being.
4423 -- When we generate a nested instance body, calling stubs for any
4424 -- relevant subprogram will be be inserted immediately after the
4425 -- subprogram declarations, and will take precedence over the
4426 -- subsequent (original) body. (The stub and original body will be
4427 -- complete homographs, but this is permitted in an instance).
4428 -- (Could we do better and remove the original body???)
4430 if Distribution_Stub_Mode
= Generate_Caller_Stub_Body
4431 and then Comes_From_Source
(N
)
4432 and then Nkind
(Parent
(N
)) = N_Compilation_Unit
4434 Needs_Body
:= False;
4439 -- Here is a defence against a ludicrous number of instantiations
4440 -- caused by a circular set of instantiation attempts.
4442 if Pending_Instantiations
.Last
> Maximum_Instantiations
then
4443 Error_Msg_Uint_1
:= UI_From_Int
(Maximum_Instantiations
);
4444 Error_Msg_N
("too many instantiations, exceeds max of^", N
);
4445 Error_Msg_N
("\limit can be changed using -gnateinn switch", N
);
4446 raise Unrecoverable_Error
;
4449 -- Indicate that the enclosing scopes contain an instantiation,
4450 -- and that cleanup actions should be delayed until after the
4451 -- instance body is expanded.
4453 Check_Forward_Instantiation
(Gen_Decl
);
4454 if Nkind
(N
) = N_Package_Instantiation
then
4456 Enclosing_Master
: Entity_Id
;
4459 -- Loop to search enclosing masters
4461 Enclosing_Master
:= Current_Scope
;
4462 Scope_Loop
: while Enclosing_Master
/= Standard_Standard
loop
4463 if Ekind
(Enclosing_Master
) = E_Package
then
4464 if Is_Compilation_Unit
(Enclosing_Master
) then
4465 if In_Package_Body
(Enclosing_Master
) then
4467 (Body_Entity
(Enclosing_Master
));
4476 Enclosing_Master
:= Scope
(Enclosing_Master
);
4479 elsif Is_Generic_Unit
(Enclosing_Master
)
4480 or else Ekind
(Enclosing_Master
) = E_Void
4482 -- Cleanup actions will eventually be performed on the
4483 -- enclosing subprogram or package instance, if any.
4484 -- Enclosing scope is void in the formal part of a
4485 -- generic subprogram.
4490 if Ekind
(Enclosing_Master
) = E_Entry
4492 Ekind
(Scope
(Enclosing_Master
)) = E_Protected_Type
4494 if not Expander_Active
then
4498 Protected_Body_Subprogram
(Enclosing_Master
);
4502 Set_Delay_Cleanups
(Enclosing_Master
);
4504 while Ekind
(Enclosing_Master
) = E_Block
loop
4505 Enclosing_Master
:= Scope
(Enclosing_Master
);
4508 if Is_Subprogram
(Enclosing_Master
) then
4509 Delay_Descriptors
(Enclosing_Master
);
4511 elsif Is_Task_Type
(Enclosing_Master
) then
4513 TBP
: constant Node_Id
:=
4514 Get_Task_Body_Procedure
4517 if Present
(TBP
) then
4518 Delay_Descriptors
(TBP
);
4519 Set_Delay_Cleanups
(TBP
);
4526 end loop Scope_Loop
;
4529 -- Make entry in table
4531 Add_Pending_Instantiation
(N
, Act_Decl
);
4535 Set_Categorization_From_Pragmas
(Act_Decl
);
4537 if Parent_Installed
then
4541 Set_Instance_Spec
(N
, Act_Decl
);
4543 -- If not a compilation unit, insert the package declaration before
4544 -- the original instantiation node.
4546 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
4547 Mark_Rewrite_Insertion
(Act_Decl
);
4548 Insert_Before
(N
, Act_Decl
);
4550 if Has_Aspects
(N
) then
4551 Analyze_Aspect_Specifications
(N
, Act_Decl_Id
);
4553 -- The pragma created for a Default_Storage_Pool aspect must
4554 -- appear ahead of the declarations in the instance spec.
4555 -- Analysis has placed it after the instance node, so remove
4556 -- it and reinsert it properly now.
4559 ASN
: constant Node_Id
:= First
(Aspect_Specifications
(N
));
4560 A_Name
: constant Name_Id
:= Chars
(Identifier
(ASN
));
4564 if A_Name
= Name_Default_Storage_Pool
then
4565 if No
(Visible_Declarations
(Act_Spec
)) then
4566 Set_Visible_Declarations
(Act_Spec
, New_List
);
4570 while Present
(Decl
) loop
4571 if Nkind
(Decl
) = N_Pragma
then
4573 Prepend
(Decl
, Visible_Declarations
(Act_Spec
));
4585 -- For an instantiation that is a compilation unit, place
4586 -- declaration on current node so context is complete for analysis
4587 -- (including nested instantiations). If this is the main unit,
4588 -- the declaration eventually replaces the instantiation node.
4589 -- If the instance body is created later, it replaces the
4590 -- instance node, and the declaration is attached to it
4591 -- (see Build_Instance_Compilation_Unit_Nodes).
4594 if Cunit_Entity
(Current_Sem_Unit
) = Defining_Entity
(N
) then
4596 -- The entity for the current unit is the newly created one,
4597 -- and all semantic information is attached to it.
4599 Set_Cunit_Entity
(Current_Sem_Unit
, Act_Decl_Id
);
4601 -- If this is the main unit, replace the main entity as well
4603 if Current_Sem_Unit
= Main_Unit
then
4604 Main_Unit_Entity
:= Act_Decl_Id
;
4608 Set_Unit
(Parent
(N
), Act_Decl
);
4609 Set_Parent_Spec
(Act_Decl
, Parent_Spec
(N
));
4610 Set_Package_Instantiation
(Act_Decl_Id
, N
);
4612 -- Process aspect specifications of the instance node, if any, to
4613 -- take into account categorization pragmas before analyzing the
4616 if Has_Aspects
(N
) then
4617 Analyze_Aspect_Specifications
(N
, Act_Decl_Id
);
4621 Set_Unit
(Parent
(N
), N
);
4622 Set_Body_Required
(Parent
(N
), False);
4624 -- We never need elaboration checks on instantiations, since by
4625 -- definition, the body instantiation is elaborated at the same
4626 -- time as the spec instantiation.
4628 if Legacy_Elaboration_Checks
then
4629 Set_Kill_Elaboration_Checks
(Act_Decl_Id
);
4630 Set_Suppress_Elaboration_Warnings
(Act_Decl_Id
);
4634 if Legacy_Elaboration_Checks
then
4635 Check_Elab_Instantiation
(N
);
4638 -- Save the scenario for later examination by the ABE Processing
4641 Record_Elaboration_Scenario
(N
);
4643 -- The instantiation results in a guaranteed ABE
4645 if Is_Known_Guaranteed_ABE
(N
) and then Needs_Body
then
4647 -- Do not instantiate the corresponding body because gigi cannot
4648 -- handle certain types of premature instantiations.
4650 Pending_Instantiations
.Decrement_Last
;
4652 -- Create completing bodies for all subprogram declarations since
4653 -- their real bodies will not be instantiated.
4655 Provide_Completing_Bodies
(Instance_Spec
(N
));
4658 Check_Hidden_Child_Unit
(N
, Gen_Unit
, Act_Decl_Id
);
4660 Set_First_Private_Entity
(Defining_Unit_Name
(Unit_Renaming
),
4661 First_Private_Entity
(Act_Decl_Id
));
4663 -- If the instantiation will receive a body, the unit will be
4664 -- transformed into a package body, and receive its own elaboration
4665 -- entity. Otherwise, the nature of the unit is now a package
4668 if Nkind
(Parent
(N
)) = N_Compilation_Unit
4669 and then not Needs_Body
4671 Rewrite
(N
, Act_Decl
);
4674 if Present
(Corresponding_Body
(Gen_Decl
))
4675 or else Unit_Requires_Body
(Gen_Unit
)
4677 Set_Has_Completion
(Act_Decl_Id
);
4680 Check_Formal_Packages
(Act_Decl_Id
);
4682 Restore_Hidden_Primitives
(Vis_Prims_List
);
4683 Restore_Private_Views
(Act_Decl_Id
);
4685 Inherit_Context
(Gen_Decl
, N
);
4687 if Parent_Installed
then
4692 Env_Installed
:= False;
4695 Validate_Categorization_Dependency
(N
, Act_Decl_Id
);
4697 -- There used to be a check here to prevent instantiations in local
4698 -- contexts if the No_Local_Allocators restriction was active. This
4699 -- check was removed by a binding interpretation in AI-95-00130/07,
4700 -- but we retain the code for documentation purposes.
4702 -- if Ekind (Act_Decl_Id) /= E_Void
4703 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4705 -- Check_Restriction (No_Local_Allocators, N);
4709 Inline_Instance_Body
(N
, Gen_Unit
, Act_Decl
);
4712 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4713 -- be used as defining identifiers for a formal package and for the
4714 -- corresponding expanded package.
4716 if Nkind
(N
) = N_Formal_Package_Declaration
then
4717 Act_Decl_Id
:= New_Copy
(Defining_Entity
(N
));
4718 Set_Comes_From_Source
(Act_Decl_Id
, True);
4719 Set_Is_Generic_Instance
(Act_Decl_Id
, False);
4720 Set_Defining_Identifier
(N
, Act_Decl_Id
);
4723 -- Check that if N is an instantiation of System.Dim_Float_IO or
4724 -- System.Dim_Integer_IO, the formal type has a dimension system.
4726 if Nkind
(N
) = N_Package_Instantiation
4727 and then Is_Dim_IO_Package_Instantiation
(N
)
4730 Assoc
: constant Node_Id
:= First
(Generic_Associations
(N
));
4732 if not Has_Dimension_System
4733 (Etype
(Explicit_Generic_Actual_Parameter
(Assoc
)))
4735 Error_Msg_N
("type with a dimension system expected", Assoc
);
4741 if Has_Aspects
(N
) and then Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
4742 Analyze_Aspect_Specifications
(N
, Act_Decl_Id
);
4745 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
4746 Restore_Ghost_Mode
(Saved_GM
);
4747 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
4748 Style_Check
:= Saved_Style_Check
;
4751 when Instantiation_Error
=>
4752 if Parent_Installed
then
4756 if Env_Installed
then
4760 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
4761 Restore_Ghost_Mode
(Saved_GM
);
4762 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
4763 Style_Check
:= Saved_Style_Check
;
4764 end Analyze_Package_Instantiation
;
4766 --------------------------
4767 -- Inline_Instance_Body --
4768 --------------------------
4770 -- WARNING: This routine manages SPARK regions. Return statements must be
4771 -- replaced by gotos which jump to the end of the routine and restore the
4774 procedure Inline_Instance_Body
4776 Gen_Unit
: Entity_Id
;
4779 Curr_Comp
: constant Node_Id
:= Cunit
(Current_Sem_Unit
);
4780 Curr_Unit
: constant Entity_Id
:= Cunit_Entity
(Current_Sem_Unit
);
4781 Gen_Comp
: constant Entity_Id
:=
4782 Cunit_Entity
(Get_Source_Unit
(Gen_Unit
));
4784 Saved_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
4785 Saved_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
4786 -- Save the SPARK mode-related data to restore on exit. Removing
4787 -- enclosing scopes to provide a clean environment for analysis of
4788 -- the inlined body will eliminate any previously set SPARK_Mode.
4790 Scope_Stack_Depth
: constant Pos
:=
4791 Scope_Stack
.Last
- Scope_Stack
.First
+ 1;
4793 Inner_Scopes
: array (1 .. Scope_Stack_Depth
) of Entity_Id
;
4794 Instances
: array (1 .. Scope_Stack_Depth
) of Entity_Id
;
4795 Use_Clauses
: array (1 .. Scope_Stack_Depth
) of Node_Id
;
4797 Curr_Scope
: Entity_Id
:= Empty
;
4798 List
: Elist_Id
:= No_Elist
; -- init to avoid warning
4799 N_Instances
: Nat
:= 0;
4800 Num_Inner
: Nat
:= 0;
4801 Num_Scopes
: Nat
:= 0;
4802 Removed
: Boolean := False;
4807 -- Case of generic unit defined in another unit. We must remove the
4808 -- complete context of the current unit to install that of the generic.
4810 if Gen_Comp
/= Cunit_Entity
(Current_Sem_Unit
) then
4812 -- Add some comments for the following two loops ???
4815 while Present
(S
) and then S
/= Standard_Standard
loop
4817 Num_Scopes
:= Num_Scopes
+ 1;
4819 Use_Clauses
(Num_Scopes
) :=
4821 (Scope_Stack
.Last
- Num_Scopes
+ 1).
4823 End_Use_Clauses
(Use_Clauses
(Num_Scopes
));
4825 exit when Scope_Stack
.Last
- Num_Scopes
+ 1 = Scope_Stack
.First
4826 or else Scope_Stack
.Table
4827 (Scope_Stack
.Last
- Num_Scopes
).Entity
= Scope
(S
);
4830 exit when Is_Generic_Instance
(S
)
4831 and then (In_Package_Body
(S
)
4832 or else Ekind
(S
) = E_Procedure
4833 or else Ekind
(S
) = E_Function
);
4837 Vis
:= Is_Immediately_Visible
(Gen_Comp
);
4839 -- Find and save all enclosing instances
4844 and then S
/= Standard_Standard
4846 if Is_Generic_Instance
(S
) then
4847 N_Instances
:= N_Instances
+ 1;
4848 Instances
(N_Instances
) := S
;
4850 exit when In_Package_Body
(S
);
4856 -- Remove context of current compilation unit, unless we are within a
4857 -- nested package instantiation, in which case the context has been
4858 -- removed previously.
4860 -- If current scope is the body of a child unit, remove context of
4861 -- spec as well. If an enclosing scope is an instance body, the
4862 -- context has already been removed, but the entities in the body
4863 -- must be made invisible as well.
4866 while Present
(S
) and then S
/= Standard_Standard
loop
4867 if Is_Generic_Instance
(S
)
4868 and then (In_Package_Body
(S
)
4869 or else Ekind_In
(S
, E_Procedure
, E_Function
))
4871 -- We still have to remove the entities of the enclosing
4872 -- instance from direct visibility.
4877 E
:= First_Entity
(S
);
4878 while Present
(E
) loop
4879 Set_Is_Immediately_Visible
(E
, False);
4888 or else (Ekind
(Curr_Unit
) = E_Package_Body
4889 and then S
= Spec_Entity
(Curr_Unit
))
4890 or else (Ekind
(Curr_Unit
) = E_Subprogram_Body
4891 and then S
= Corresponding_Spec
4892 (Unit_Declaration_Node
(Curr_Unit
)))
4896 -- Remove entities in current scopes from visibility, so that
4897 -- instance body is compiled in a clean environment.
4899 List
:= Save_Scope_Stack
(Handle_Use
=> False);
4901 if Is_Child_Unit
(S
) then
4903 -- Remove child unit from stack, as well as inner scopes.
4904 -- Removing the context of a child unit removes parent units
4907 while Current_Scope
/= S
loop
4908 Num_Inner
:= Num_Inner
+ 1;
4909 Inner_Scopes
(Num_Inner
) := Current_Scope
;
4914 Remove_Context
(Curr_Comp
);
4918 Remove_Context
(Curr_Comp
);
4921 if Ekind
(Curr_Unit
) = E_Package_Body
then
4922 Remove_Context
(Library_Unit
(Curr_Comp
));
4929 pragma Assert
(Num_Inner
< Num_Scopes
);
4931 -- The inlined package body must be analyzed with the SPARK_Mode of
4932 -- the enclosing context, otherwise the body may cause bogus errors
4933 -- if a configuration SPARK_Mode pragma in in effect.
4935 Push_Scope
(Standard_Standard
);
4936 Scope_Stack
.Table
(Scope_Stack
.Last
).Is_Active_Stack_Base
:= True;
4937 Instantiate_Package_Body
4940 Act_Decl
=> Act_Decl
,
4941 Expander_Status
=> Expander_Active
,
4942 Current_Sem_Unit
=> Current_Sem_Unit
,
4943 Scope_Suppress
=> Scope_Suppress
,
4944 Local_Suppress_Stack_Top
=> Local_Suppress_Stack_Top
,
4945 Version
=> Ada_Version
,
4946 Version_Pragma
=> Ada_Version_Pragma
,
4947 Warnings
=> Save_Warnings
,
4948 SPARK_Mode
=> Saved_SM
,
4949 SPARK_Mode_Pragma
=> Saved_SMP
)),
4950 Inlined_Body
=> True);
4956 Set_Is_Immediately_Visible
(Gen_Comp
, Vis
);
4958 -- Reset Generic_Instance flag so that use clauses can be installed
4959 -- in the proper order. (See Use_One_Package for effect of enclosing
4960 -- instances on processing of use clauses).
4962 for J
in 1 .. N_Instances
loop
4963 Set_Is_Generic_Instance
(Instances
(J
), False);
4967 Install_Context
(Curr_Comp
, Chain
=> False);
4969 if Present
(Curr_Scope
)
4970 and then Is_Child_Unit
(Curr_Scope
)
4972 Push_Scope
(Curr_Scope
);
4973 Set_Is_Immediately_Visible
(Curr_Scope
);
4975 -- Finally, restore inner scopes as well
4977 for J
in reverse 1 .. Num_Inner
loop
4978 Push_Scope
(Inner_Scopes
(J
));
4982 Restore_Scope_Stack
(List
, Handle_Use
=> False);
4984 if Present
(Curr_Scope
)
4986 (In_Private_Part
(Curr_Scope
)
4987 or else In_Package_Body
(Curr_Scope
))
4989 -- Install private declaration of ancestor units, which are
4990 -- currently available. Restore_Scope_Stack and Install_Context
4991 -- only install the visible part of parents.
4996 Par
:= Scope
(Curr_Scope
);
4997 while (Present
(Par
)) and then Par
/= Standard_Standard
loop
4998 Install_Private_Declarations
(Par
);
5005 -- Restore use clauses. For a child unit, use clauses in the parents
5006 -- are restored when installing the context, so only those in inner
5007 -- scopes (and those local to the child unit itself) need to be
5008 -- installed explicitly.
5010 if Is_Child_Unit
(Curr_Unit
) and then Removed
then
5011 for J
in reverse 1 .. Num_Inner
+ 1 loop
5012 Scope_Stack
.Table
(Scope_Stack
.Last
- J
+ 1).First_Use_Clause
:=
5014 Install_Use_Clauses
(Use_Clauses
(J
));
5018 for J
in reverse 1 .. Num_Scopes
loop
5019 Scope_Stack
.Table
(Scope_Stack
.Last
- J
+ 1).First_Use_Clause
:=
5021 Install_Use_Clauses
(Use_Clauses
(J
));
5025 -- Restore status of instances. If one of them is a body, make its
5026 -- local entities visible again.
5033 for J
in 1 .. N_Instances
loop
5034 Inst
:= Instances
(J
);
5035 Set_Is_Generic_Instance
(Inst
, True);
5037 if In_Package_Body
(Inst
)
5038 or else Ekind_In
(S
, E_Procedure
, E_Function
)
5040 E
:= First_Entity
(Instances
(J
));
5041 while Present
(E
) loop
5042 Set_Is_Immediately_Visible
(E
);
5049 -- If generic unit is in current unit, current context is correct. Note
5050 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5051 -- enclosing scopes were removed.
5054 Instantiate_Package_Body
5057 Act_Decl
=> Act_Decl
,
5058 Expander_Status
=> Expander_Active
,
5059 Current_Sem_Unit
=> Current_Sem_Unit
,
5060 Scope_Suppress
=> Scope_Suppress
,
5061 Local_Suppress_Stack_Top
=> Local_Suppress_Stack_Top
,
5062 Version
=> Ada_Version
,
5063 Version_Pragma
=> Ada_Version_Pragma
,
5064 Warnings
=> Save_Warnings
,
5065 SPARK_Mode
=> SPARK_Mode
,
5066 SPARK_Mode_Pragma
=> SPARK_Mode_Pragma
)),
5067 Inlined_Body
=> True);
5069 end Inline_Instance_Body
;
5071 -------------------------------------
5072 -- Analyze_Procedure_Instantiation --
5073 -------------------------------------
5075 procedure Analyze_Procedure_Instantiation
(N
: Node_Id
) is
5077 Analyze_Subprogram_Instantiation
(N
, E_Procedure
);
5078 end Analyze_Procedure_Instantiation
;
5080 -----------------------------------
5081 -- Need_Subprogram_Instance_Body --
5082 -----------------------------------
5084 function Need_Subprogram_Instance_Body
5086 Subp
: Entity_Id
) return Boolean
5088 function Is_Inlined_Or_Child_Of_Inlined
(E
: Entity_Id
) return Boolean;
5089 -- Return True if E is an inlined subprogram, an inlined renaming or a
5090 -- subprogram nested in an inlined subprogram. The inlining machinery
5091 -- totally disregards nested subprograms since it considers that they
5092 -- will always be compiled if the parent is (see Inline.Is_Nested).
5094 ------------------------------------
5095 -- Is_Inlined_Or_Child_Of_Inlined --
5096 ------------------------------------
5098 function Is_Inlined_Or_Child_Of_Inlined
(E
: Entity_Id
) return Boolean is
5102 if Is_Inlined
(E
) or else Is_Inlined
(Alias
(E
)) then
5107 while Scop
/= Standard_Standard
loop
5108 if Ekind
(Scop
) in Subprogram_Kind
and then Is_Inlined
(Scop
) then
5112 Scop
:= Scope
(Scop
);
5116 end Is_Inlined_Or_Child_Of_Inlined
;
5119 -- Must be in the main unit or inlined (or child of inlined)
5121 if (Is_In_Main_Unit
(N
) or else Is_Inlined_Or_Child_Of_Inlined
(Subp
))
5123 -- Must be generating code or analyzing code in ASIS/GNATprove mode
5125 and then (Operating_Mode
= Generate_Code
5126 or else (Operating_Mode
= Check_Semantics
5127 and then (ASIS_Mode
or GNATprove_Mode
)))
5129 -- The body is needed when generating code (full expansion), in ASIS
5130 -- mode for other tools, and in GNATprove mode (special expansion) for
5131 -- formal verification of the body itself.
5133 and then (Expander_Active
or ASIS_Mode
or GNATprove_Mode
)
5135 -- No point in inlining if ABE is inevitable
5137 and then not Is_Known_Guaranteed_ABE
(N
)
5139 -- Or if subprogram is eliminated
5141 and then not Is_Eliminated
(Subp
)
5143 Add_Pending_Instantiation
(N
, Unit_Declaration_Node
(Subp
));
5146 -- Here if not inlined, or we ignore the inlining
5151 end Need_Subprogram_Instance_Body
;
5153 --------------------------------------
5154 -- Analyze_Subprogram_Instantiation --
5155 --------------------------------------
5157 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5158 -- must be replaced by gotos which jump to the end of the routine in order
5159 -- to restore the Ghost and SPARK modes.
5161 procedure Analyze_Subprogram_Instantiation
5165 Loc
: constant Source_Ptr
:= Sloc
(N
);
5166 Gen_Id
: constant Node_Id
:= Name
(N
);
5167 Errs
: constant Nat
:= Serious_Errors_Detected
;
5169 Anon_Id
: constant Entity_Id
:=
5170 Make_Defining_Identifier
(Sloc
(Defining_Entity
(N
)),
5171 Chars
=> New_External_Name
5172 (Chars
(Defining_Entity
(N
)), 'R'));
5174 Act_Decl_Id
: Entity_Id
:= Empty
; -- init to avoid warning
5179 Env_Installed
: Boolean := False;
5180 Gen_Unit
: Entity_Id
;
5182 Pack_Id
: Entity_Id
;
5183 Parent_Installed
: Boolean := False;
5185 Renaming_List
: List_Id
;
5186 -- The list of declarations that link formals and actuals of the
5187 -- instance. These are subtype declarations for formal types, and
5188 -- renaming declarations for other formals. The subprogram declaration
5189 -- for the instance is then appended to the list, and the last item on
5190 -- the list is the renaming declaration for the instance.
5192 procedure Analyze_Instance_And_Renamings
;
5193 -- The instance must be analyzed in a context that includes the mappings
5194 -- of generic parameters into actuals. We create a package declaration
5195 -- for this purpose, and a subprogram with an internal name within the
5196 -- package. The subprogram instance is simply an alias for the internal
5197 -- subprogram, declared in the current scope.
5199 procedure Build_Subprogram_Renaming
;
5200 -- If the subprogram is recursive, there are occurrences of the name of
5201 -- the generic within the body, which must resolve to the current
5202 -- instance. We add a renaming declaration after the declaration, which
5203 -- is available in the instance body, as well as in the analysis of
5204 -- aspects that appear in the generic. This renaming declaration is
5205 -- inserted after the instance declaration which it renames.
5207 ------------------------------------
5208 -- Analyze_Instance_And_Renamings --
5209 ------------------------------------
5211 procedure Analyze_Instance_And_Renamings
is
5212 Def_Ent
: constant Entity_Id
:= Defining_Entity
(N
);
5213 Pack_Decl
: Node_Id
;
5216 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5218 -- For the case of a compilation unit, the container package has
5219 -- the same name as the instantiation, to insure that the binder
5220 -- calls the elaboration procedure with the right name. Copy the
5221 -- entity of the instance, which may have compilation level flags
5222 -- (e.g. Is_Child_Unit) set.
5224 Pack_Id
:= New_Copy
(Def_Ent
);
5227 -- Otherwise we use the name of the instantiation concatenated
5228 -- with its source position to ensure uniqueness if there are
5229 -- several instantiations with the same name.
5232 Make_Defining_Identifier
(Loc
,
5233 Chars
=> New_External_Name
5234 (Related_Id
=> Chars
(Def_Ent
),
5236 Suffix_Index
=> Source_Offset
(Sloc
(Def_Ent
))));
5240 Make_Package_Declaration
(Loc
,
5241 Specification
=> Make_Package_Specification
(Loc
,
5242 Defining_Unit_Name
=> Pack_Id
,
5243 Visible_Declarations
=> Renaming_List
,
5244 End_Label
=> Empty
));
5246 Set_Instance_Spec
(N
, Pack_Decl
);
5247 Set_Is_Generic_Instance
(Pack_Id
);
5248 Set_Debug_Info_Needed
(Pack_Id
);
5250 -- Case of not a compilation unit
5252 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
5253 Mark_Rewrite_Insertion
(Pack_Decl
);
5254 Insert_Before
(N
, Pack_Decl
);
5255 Set_Has_Completion
(Pack_Id
);
5257 -- Case of an instantiation that is a compilation unit
5259 -- Place declaration on current node so context is complete for
5260 -- analysis (including nested instantiations), and for use in a
5261 -- context_clause (see Analyze_With_Clause).
5264 Set_Unit
(Parent
(N
), Pack_Decl
);
5265 Set_Parent_Spec
(Pack_Decl
, Parent_Spec
(N
));
5268 Analyze
(Pack_Decl
);
5269 Check_Formal_Packages
(Pack_Id
);
5270 Set_Is_Generic_Instance
(Pack_Id
, False);
5272 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
5275 -- Body of the enclosing package is supplied when instantiating the
5276 -- subprogram body, after semantic analysis is completed.
5278 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5280 -- Remove package itself from visibility, so it does not
5281 -- conflict with subprogram.
5283 Set_Name_Entity_Id
(Chars
(Pack_Id
), Homonym
(Pack_Id
));
5285 -- Set name and scope of internal subprogram so that the proper
5286 -- external name will be generated. The proper scope is the scope
5287 -- of the wrapper package. We need to generate debugging info for
5288 -- the internal subprogram, so set flag accordingly.
5290 Set_Chars
(Anon_Id
, Chars
(Defining_Entity
(N
)));
5291 Set_Scope
(Anon_Id
, Scope
(Pack_Id
));
5293 -- Mark wrapper package as referenced, to avoid spurious warnings
5294 -- if the instantiation appears in various with_ clauses of
5295 -- subunits of the main unit.
5297 Set_Referenced
(Pack_Id
);
5300 Set_Is_Generic_Instance
(Anon_Id
);
5301 Set_Debug_Info_Needed
(Anon_Id
);
5302 Act_Decl_Id
:= New_Copy
(Anon_Id
);
5304 Set_Parent
(Act_Decl_Id
, Parent
(Anon_Id
));
5305 Set_Chars
(Act_Decl_Id
, Chars
(Defining_Entity
(N
)));
5306 Set_Sloc
(Act_Decl_Id
, Sloc
(Defining_Entity
(N
)));
5308 -- Subprogram instance comes from source only if generic does
5310 Set_Comes_From_Source
(Act_Decl_Id
, Comes_From_Source
(Gen_Unit
));
5312 -- If the instance is a child unit, mark the Id accordingly. Mark
5313 -- the anonymous entity as well, which is the real subprogram and
5314 -- which is used when the instance appears in a context clause.
5315 -- Similarly, propagate the Is_Eliminated flag to handle properly
5316 -- nested eliminated subprograms.
5318 Set_Is_Child_Unit
(Act_Decl_Id
, Is_Child_Unit
(Defining_Entity
(N
)));
5319 Set_Is_Child_Unit
(Anon_Id
, Is_Child_Unit
(Defining_Entity
(N
)));
5320 New_Overloaded_Entity
(Act_Decl_Id
);
5321 Check_Eliminated
(Act_Decl_Id
);
5322 Set_Is_Eliminated
(Anon_Id
, Is_Eliminated
(Act_Decl_Id
));
5324 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5326 -- In compilation unit case, kill elaboration checks on the
5327 -- instantiation, since they are never needed - the body is
5328 -- instantiated at the same point as the spec.
5330 if Legacy_Elaboration_Checks
then
5331 Set_Kill_Elaboration_Checks
(Act_Decl_Id
);
5332 Set_Suppress_Elaboration_Warnings
(Act_Decl_Id
);
5335 Set_Is_Compilation_Unit
(Anon_Id
);
5336 Set_Cunit_Entity
(Current_Sem_Unit
, Pack_Id
);
5339 -- The instance is not a freezing point for the new subprogram.
5340 -- The anonymous subprogram may have a freeze node, created for
5341 -- some delayed aspects. This freeze node must not be inherited
5342 -- by the visible subprogram entity.
5344 Set_Is_Frozen
(Act_Decl_Id
, False);
5345 Set_Freeze_Node
(Act_Decl_Id
, Empty
);
5347 if Nkind
(Defining_Entity
(N
)) = N_Defining_Operator_Symbol
then
5348 Valid_Operator_Definition
(Act_Decl_Id
);
5351 Set_Alias
(Act_Decl_Id
, Anon_Id
);
5352 Set_Has_Completion
(Act_Decl_Id
);
5353 Set_Related_Instance
(Pack_Id
, Act_Decl_Id
);
5355 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5356 Set_Body_Required
(Parent
(N
), False);
5358 end Analyze_Instance_And_Renamings
;
5360 -------------------------------
5361 -- Build_Subprogram_Renaming --
5362 -------------------------------
5364 procedure Build_Subprogram_Renaming
is
5365 Renaming_Decl
: Node_Id
;
5366 Unit_Renaming
: Node_Id
;
5370 Make_Subprogram_Renaming_Declaration
(Loc
,
5373 (Specification
(Original_Node
(Gen_Decl
)),
5375 Instantiating
=> True),
5376 Name
=> New_Occurrence_Of
(Anon_Id
, Loc
));
5378 -- The generic may be a a child unit. The renaming needs an
5379 -- identifier with the proper name.
5381 Set_Defining_Unit_Name
(Specification
(Unit_Renaming
),
5382 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
)));
5384 -- If there is a formal subprogram with the same name as the unit
5385 -- itself, do not add this renaming declaration, to prevent
5386 -- ambiguities when there is a call with that name in the body.
5387 -- This is a partial and ugly fix for one ACATS test. ???
5389 Renaming_Decl
:= First
(Renaming_List
);
5390 while Present
(Renaming_Decl
) loop
5391 if Nkind
(Renaming_Decl
) = N_Subprogram_Renaming_Declaration
5393 Chars
(Defining_Entity
(Renaming_Decl
)) = Chars
(Gen_Unit
)
5398 Next
(Renaming_Decl
);
5401 if No
(Renaming_Decl
) then
5402 Append
(Unit_Renaming
, Renaming_List
);
5404 end Build_Subprogram_Renaming
;
5408 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
5409 Saved_ISMP
: constant Boolean :=
5410 Ignore_SPARK_Mode_Pragmas_In_Instance
;
5411 Saved_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
5412 Saved_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
5413 -- Save the Ghost and SPARK mode-related data to restore on exit
5415 Vis_Prims_List
: Elist_Id
:= No_Elist
;
5416 -- List of primitives made temporarily visible in the instantiation
5417 -- to match the visibility of the formal type
5419 -- Start of processing for Analyze_Subprogram_Instantiation
5422 -- Preserve relevant elaboration-related attributes of the context which
5423 -- are no longer available or very expensive to recompute once analysis,
5424 -- resolution, and expansion are over.
5426 Mark_Elaboration_Attributes
5433 Check_SPARK_05_Restriction
("generic is not allowed", N
);
5435 -- Very first thing: check for special Text_IO unit in case we are
5436 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5437 -- such an instantiation is bogus (these are packages, not subprograms),
5438 -- but we get a better error message if we do this.
5440 Check_Text_IO_Special_Unit
(Gen_Id
);
5442 -- Make node global for error reporting
5444 Instantiation_Node
:= N
;
5446 -- For package instantiations we turn off style checks, because they
5447 -- will have been emitted in the generic. For subprogram instantiations
5448 -- we want to apply at least the check on overriding indicators so we
5449 -- do not modify the style check status.
5451 -- The renaming declarations for the actuals do not come from source and
5452 -- will not generate spurious warnings.
5454 Preanalyze_Actuals
(N
);
5457 Env_Installed
:= True;
5458 Check_Generic_Child_Unit
(Gen_Id
, Parent_Installed
);
5459 Gen_Unit
:= Entity
(Gen_Id
);
5461 -- A subprogram instantiation is Ghost when it is subject to pragma
5462 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5463 -- that any nodes generated during analysis and expansion are marked as
5466 Mark_And_Set_Ghost_Instantiation
(N
, Gen_Unit
);
5468 Generate_Reference
(Gen_Unit
, Gen_Id
);
5470 if Nkind
(Gen_Id
) = N_Identifier
5471 and then Chars
(Gen_Unit
) = Chars
(Defining_Entity
(N
))
5474 ("& is hidden within declaration of instance", Gen_Id
, Gen_Unit
);
5477 if Etype
(Gen_Unit
) = Any_Type
then
5482 -- Verify that it is a generic subprogram of the right kind, and that
5483 -- it does not lead to a circular instantiation.
5485 if K
= E_Procedure
and then Ekind
(Gen_Unit
) /= E_Generic_Procedure
then
5487 ("& is not the name of a generic procedure", Gen_Id
, Gen_Unit
);
5489 elsif K
= E_Function
and then Ekind
(Gen_Unit
) /= E_Generic_Function
then
5491 ("& is not the name of a generic function", Gen_Id
, Gen_Unit
);
5493 elsif In_Open_Scopes
(Gen_Unit
) then
5494 Error_Msg_NE
("instantiation of & within itself", N
, Gen_Unit
);
5497 Set_Entity
(Gen_Id
, Gen_Unit
);
5498 Set_Is_Instantiated
(Gen_Unit
);
5500 if In_Extended_Main_Source_Unit
(N
) then
5501 Generate_Reference
(Gen_Unit
, N
);
5504 -- If renaming, get original unit
5506 if Present
(Renamed_Object
(Gen_Unit
))
5507 and then Ekind_In
(Renamed_Object
(Gen_Unit
), E_Generic_Procedure
,
5510 Gen_Unit
:= Renamed_Object
(Gen_Unit
);
5511 Set_Is_Instantiated
(Gen_Unit
);
5512 Generate_Reference
(Gen_Unit
, N
);
5515 if Contains_Instance_Of
(Gen_Unit
, Current_Scope
, Gen_Id
) then
5516 Error_Msg_Node_2
:= Current_Scope
;
5518 ("circular Instantiation: & instantiated in &!", N
, Gen_Unit
);
5519 Circularity_Detected
:= True;
5520 Restore_Hidden_Primitives
(Vis_Prims_List
);
5524 Gen_Decl
:= Unit_Declaration_Node
(Gen_Unit
);
5526 -- Initialize renamings map, for error checking
5528 Generic_Renamings
.Set_Last
(0);
5529 Generic_Renamings_HTable
.Reset
;
5531 Create_Instantiation_Source
(N
, Gen_Unit
, S_Adjustment
);
5533 -- Copy original generic tree, to produce text for instantiation
5537 (Original_Node
(Gen_Decl
), Empty
, Instantiating
=> True);
5539 -- Inherit overriding indicator from instance node
5541 Act_Spec
:= Specification
(Act_Tree
);
5542 Set_Must_Override
(Act_Spec
, Must_Override
(N
));
5543 Set_Must_Not_Override
(Act_Spec
, Must_Not_Override
(N
));
5546 Analyze_Associations
5548 Formals
=> Generic_Formal_Declarations
(Act_Tree
),
5549 F_Copy
=> Generic_Formal_Declarations
(Gen_Decl
));
5551 Vis_Prims_List
:= Check_Hidden_Primitives
(Renaming_List
);
5553 -- The subprogram itself cannot contain a nested instance, so the
5554 -- current parent is left empty.
5556 Set_Instance_Env
(Gen_Unit
, Empty
);
5558 -- Build the subprogram declaration, which does not appear in the
5559 -- generic template, and give it a sloc consistent with that of the
5562 Set_Defining_Unit_Name
(Act_Spec
, Anon_Id
);
5563 Set_Generic_Parent
(Act_Spec
, Gen_Unit
);
5565 Make_Subprogram_Declaration
(Sloc
(Act_Spec
),
5566 Specification
=> Act_Spec
);
5568 -- The aspects have been copied previously, but they have to be
5569 -- linked explicitly to the new subprogram declaration. Explicit
5570 -- pre/postconditions on the instance are analyzed below, in a
5573 Move_Aspects
(Act_Tree
, To
=> Act_Decl
);
5574 Set_Categorization_From_Pragmas
(Act_Decl
);
5576 if Parent_Installed
then
5580 Append
(Act_Decl
, Renaming_List
);
5582 -- Contract-related source pragmas that follow a generic subprogram
5583 -- must be instantiated explicitly because they are not part of the
5584 -- subprogram template.
5586 Instantiate_Subprogram_Contract
5587 (Original_Node
(Gen_Decl
), Renaming_List
);
5589 Build_Subprogram_Renaming
;
5591 -- If the context of the instance is subject to SPARK_Mode "off" or
5592 -- the annotation is altogether missing, set the global flag which
5593 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5594 -- the instance. This should be done prior to analyzing the instance.
5596 if SPARK_Mode
/= On
then
5597 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
5600 -- If the context of an instance is not subject to SPARK_Mode "off",
5601 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5602 -- the latter should be the one applicable to the instance.
5604 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5605 and then Saved_SM
/= Off
5606 and then Present
(SPARK_Pragma
(Gen_Unit
))
5608 Set_SPARK_Mode
(Gen_Unit
);
5611 Analyze_Instance_And_Renamings
;
5613 -- Restore SPARK_Mode from the context after analysis of the package
5614 -- declaration, so that the SPARK_Mode on the generic spec does not
5615 -- apply to the pending instance for the instance body.
5617 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5618 and then Saved_SM
/= Off
5619 and then Present
(SPARK_Pragma
(Gen_Unit
))
5621 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
5624 -- If the generic is marked Import (Intrinsic), then so is the
5625 -- instance. This indicates that there is no body to instantiate. If
5626 -- generic is marked inline, so it the instance, and the anonymous
5627 -- subprogram it renames. If inlined, or else if inlining is enabled
5628 -- for the compilation, we generate the instance body even if it is
5629 -- not within the main unit.
5631 if Is_Intrinsic_Subprogram
(Gen_Unit
) then
5632 Set_Is_Intrinsic_Subprogram
(Anon_Id
);
5633 Set_Is_Intrinsic_Subprogram
(Act_Decl_Id
);
5635 if Chars
(Gen_Unit
) = Name_Unchecked_Conversion
then
5636 Validate_Unchecked_Conversion
(N
, Act_Decl_Id
);
5640 -- Inherit convention from generic unit. Intrinsic convention, as for
5641 -- an instance of unchecked conversion, is not inherited because an
5642 -- explicit Ada instance has been created.
5644 if Has_Convention_Pragma
(Gen_Unit
)
5645 and then Convention
(Gen_Unit
) /= Convention_Intrinsic
5647 Set_Convention
(Act_Decl_Id
, Convention
(Gen_Unit
));
5648 Set_Is_Exported
(Act_Decl_Id
, Is_Exported
(Gen_Unit
));
5651 Generate_Definition
(Act_Decl_Id
);
5653 -- Inherit all inlining-related flags which apply to the generic in
5654 -- the subprogram and its declaration.
5656 Set_Is_Inlined
(Act_Decl_Id
, Is_Inlined
(Gen_Unit
));
5657 Set_Is_Inlined
(Anon_Id
, Is_Inlined
(Gen_Unit
));
5659 Set_Has_Pragma_Inline
(Act_Decl_Id
, Has_Pragma_Inline
(Gen_Unit
));
5660 Set_Has_Pragma_Inline
(Anon_Id
, Has_Pragma_Inline
(Gen_Unit
));
5662 -- Propagate No_Return if pragma applied to generic unit. This must
5663 -- be done explicitly because pragma does not appear in generic
5664 -- declaration (unlike the aspect case).
5666 if No_Return
(Gen_Unit
) then
5667 Set_No_Return
(Act_Decl_Id
);
5668 Set_No_Return
(Anon_Id
);
5671 Set_Has_Pragma_Inline_Always
5672 (Act_Decl_Id
, Has_Pragma_Inline_Always
(Gen_Unit
));
5673 Set_Has_Pragma_Inline_Always
5674 (Anon_Id
, Has_Pragma_Inline_Always
(Gen_Unit
));
5676 -- Mark both the instance spec and the anonymous package in case the
5677 -- body is instantiated at a later pass. This preserves the original
5678 -- context in effect for the body.
5680 if SPARK_Mode
/= On
then
5681 Set_Ignore_SPARK_Mode_Pragmas
(Act_Decl_Id
);
5682 Set_Ignore_SPARK_Mode_Pragmas
(Anon_Id
);
5685 if Legacy_Elaboration_Checks
5686 and then not Is_Intrinsic_Subprogram
(Gen_Unit
)
5688 Check_Elab_Instantiation
(N
);
5691 -- Save the scenario for later examination by the ABE Processing
5694 Record_Elaboration_Scenario
(N
);
5696 -- The instantiation results in a guaranteed ABE. Create a completing
5697 -- body for the subprogram declaration because the real body will not
5700 if Is_Known_Guaranteed_ABE
(N
) then
5701 Provide_Completing_Bodies
(Instance_Spec
(N
));
5704 if Is_Dispatching_Operation
(Act_Decl_Id
)
5705 and then Ada_Version
>= Ada_2005
5711 Formal
:= First_Formal
(Act_Decl_Id
);
5712 while Present
(Formal
) loop
5713 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
5714 and then Is_Controlling_Formal
(Formal
)
5715 and then not Can_Never_Be_Null
(Formal
)
5718 ("access parameter& is controlling,", N
, Formal
);
5720 ("\corresponding parameter of & must be explicitly "
5721 & "null-excluding", N
, Gen_Id
);
5724 Next_Formal
(Formal
);
5729 Check_Hidden_Child_Unit
(N
, Gen_Unit
, Act_Decl_Id
);
5731 Validate_Categorization_Dependency
(N
, Act_Decl_Id
);
5733 if not Is_Intrinsic_Subprogram
(Act_Decl_Id
) then
5734 Inherit_Context
(Gen_Decl
, N
);
5736 Restore_Private_Views
(Pack_Id
, False);
5738 -- If the context requires a full instantiation, mark node for
5739 -- subsequent construction of the body.
5741 if Need_Subprogram_Instance_Body
(N
, Act_Decl_Id
) then
5742 Check_Forward_Instantiation
(Gen_Decl
);
5744 -- The wrapper package is always delayed, because it does not
5745 -- constitute a freeze point, but to insure that the freeze node
5746 -- is placed properly, it is created directly when instantiating
5747 -- the body (otherwise the freeze node might appear to early for
5748 -- nested instantiations). For ASIS purposes, indicate that the
5749 -- wrapper package has replaced the instantiation node.
5751 elsif Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5752 Rewrite
(N
, Unit
(Parent
(N
)));
5753 Set_Unit
(Parent
(N
), N
);
5756 -- Replace instance node for library-level instantiations of
5757 -- intrinsic subprograms, for ASIS use.
5759 elsif Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5760 Rewrite
(N
, Unit
(Parent
(N
)));
5761 Set_Unit
(Parent
(N
), N
);
5764 if Parent_Installed
then
5768 Restore_Hidden_Primitives
(Vis_Prims_List
);
5770 Env_Installed
:= False;
5771 Generic_Renamings
.Set_Last
(0);
5772 Generic_Renamings_HTable
.Reset
;
5776 -- Analyze aspects in declaration if no errors appear in the instance.
5778 if Has_Aspects
(N
) and then Serious_Errors_Detected
= Errs
then
5779 Analyze_Aspect_Specifications
(N
, Act_Decl_Id
);
5782 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
5783 Restore_Ghost_Mode
(Saved_GM
);
5784 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
5787 when Instantiation_Error
=>
5788 if Parent_Installed
then
5792 if Env_Installed
then
5796 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
5797 Restore_Ghost_Mode
(Saved_GM
);
5798 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
5799 end Analyze_Subprogram_Instantiation
;
5801 -------------------------
5802 -- Get_Associated_Node --
5803 -------------------------
5805 function Get_Associated_Node
(N
: Node_Id
) return Node_Id
is
5809 Assoc
:= Associated_Node
(N
);
5811 if Nkind
(Assoc
) /= Nkind
(N
) then
5814 elsif Nkind_In
(Assoc
, N_Aggregate
, N_Extension_Aggregate
) then
5818 -- If the node is part of an inner generic, it may itself have been
5819 -- remapped into a further generic copy. Associated_Node is otherwise
5820 -- used for the entity of the node, and will be of a different node
5821 -- kind, or else N has been rewritten as a literal or function call.
5823 while Present
(Associated_Node
(Assoc
))
5824 and then Nkind
(Associated_Node
(Assoc
)) = Nkind
(Assoc
)
5826 Assoc
:= Associated_Node
(Assoc
);
5829 -- Follow an additional link in case the final node was rewritten.
5830 -- This can only happen with nested generic units.
5832 if (Nkind
(Assoc
) = N_Identifier
or else Nkind
(Assoc
) in N_Op
)
5833 and then Present
(Associated_Node
(Assoc
))
5834 and then (Nkind_In
(Associated_Node
(Assoc
), N_Function_Call
,
5835 N_Explicit_Dereference
,
5840 Assoc
:= Associated_Node
(Assoc
);
5843 -- An additional special case: an unconstrained type in an object
5844 -- declaration may have been rewritten as a local subtype constrained
5845 -- by the expression in the declaration. We need to recover the
5846 -- original entity, which may be global.
5848 if Present
(Original_Node
(Assoc
))
5849 and then Nkind
(Parent
(N
)) = N_Object_Declaration
5851 Assoc
:= Original_Node
(Assoc
);
5856 end Get_Associated_Node
;
5858 ----------------------------
5859 -- Build_Function_Wrapper --
5860 ----------------------------
5862 function Build_Function_Wrapper
5863 (Formal_Subp
: Entity_Id
;
5864 Actual_Subp
: Entity_Id
) return Node_Id
5866 Loc
: constant Source_Ptr
:= Sloc
(Current_Scope
);
5867 Ret_Type
: constant Entity_Id
:= Get_Instance_Of
(Etype
(Formal_Subp
));
5870 Func_Name
: Node_Id
;
5872 Parm_Type
: Node_Id
;
5873 Profile
: List_Id
:= New_List
;
5880 Func_Name
:= New_Occurrence_Of
(Actual_Subp
, Loc
);
5882 Func
:= Make_Defining_Identifier
(Loc
, Chars
(Formal_Subp
));
5883 Set_Ekind
(Func
, E_Function
);
5884 Set_Is_Generic_Actual_Subprogram
(Func
);
5886 Actuals
:= New_List
;
5887 Profile
:= New_List
;
5889 Act_F
:= First_Formal
(Actual_Subp
);
5890 Form_F
:= First_Formal
(Formal_Subp
);
5891 while Present
(Form_F
) loop
5893 -- Create new formal for profile of wrapper, and add a reference
5894 -- to it in the list of actuals for the enclosing call. The name
5895 -- must be that of the formal in the formal subprogram, because
5896 -- calls to it in the generic body may use named associations.
5898 New_F
:= Make_Defining_Identifier
(Loc
, Chars
(Form_F
));
5901 New_Occurrence_Of
(Get_Instance_Of
(Etype
(Form_F
)), Loc
);
5904 Make_Parameter_Specification
(Loc
,
5905 Defining_Identifier
=> New_F
,
5906 Parameter_Type
=> Parm_Type
));
5908 Append_To
(Actuals
, New_Occurrence_Of
(New_F
, Loc
));
5909 Next_Formal
(Form_F
);
5911 if Present
(Act_F
) then
5912 Next_Formal
(Act_F
);
5917 Make_Function_Specification
(Loc
,
5918 Defining_Unit_Name
=> Func
,
5919 Parameter_Specifications
=> Profile
,
5920 Result_Definition
=> New_Occurrence_Of
(Ret_Type
, Loc
));
5923 Make_Expression_Function
(Loc
,
5924 Specification
=> Spec
,
5926 Make_Function_Call
(Loc
,
5928 Parameter_Associations
=> Actuals
));
5931 end Build_Function_Wrapper
;
5933 ----------------------------
5934 -- Build_Operator_Wrapper --
5935 ----------------------------
5937 function Build_Operator_Wrapper
5938 (Formal_Subp
: Entity_Id
;
5939 Actual_Subp
: Entity_Id
) return Node_Id
5941 Loc
: constant Source_Ptr
:= Sloc
(Current_Scope
);
5942 Ret_Type
: constant Entity_Id
:=
5943 Get_Instance_Of
(Etype
(Formal_Subp
));
5944 Op_Type
: constant Entity_Id
:=
5945 Get_Instance_Of
(Etype
(First_Formal
(Formal_Subp
)));
5946 Is_Binary
: constant Boolean :=
5947 Present
(Next_Formal
(First_Formal
(Formal_Subp
)));
5950 Expr
: Node_Id
:= Empty
;
5958 Op_Name
:= Chars
(Actual_Subp
);
5960 -- Create entities for wrapper function and its formals
5962 F1
:= Make_Temporary
(Loc
, 'A');
5963 F2
:= Make_Temporary
(Loc
, 'B');
5964 L
:= New_Occurrence_Of
(F1
, Loc
);
5965 R
:= New_Occurrence_Of
(F2
, Loc
);
5967 Func
:= Make_Defining_Identifier
(Loc
, Chars
(Formal_Subp
));
5968 Set_Ekind
(Func
, E_Function
);
5969 Set_Is_Generic_Actual_Subprogram
(Func
);
5972 Make_Function_Specification
(Loc
,
5973 Defining_Unit_Name
=> Func
,
5974 Parameter_Specifications
=> New_List
(
5975 Make_Parameter_Specification
(Loc
,
5976 Defining_Identifier
=> F1
,
5977 Parameter_Type
=> New_Occurrence_Of
(Op_Type
, Loc
))),
5978 Result_Definition
=> New_Occurrence_Of
(Ret_Type
, Loc
));
5981 Append_To
(Parameter_Specifications
(Spec
),
5982 Make_Parameter_Specification
(Loc
,
5983 Defining_Identifier
=> F2
,
5984 Parameter_Type
=> New_Occurrence_Of
(Op_Type
, Loc
)));
5987 -- Build expression as a function call, or as an operator node
5988 -- that corresponds to the name of the actual, starting with
5989 -- binary operators.
5991 if Op_Name
not in Any_Operator_Name
then
5993 Make_Function_Call
(Loc
,
5995 New_Occurrence_Of
(Actual_Subp
, Loc
),
5996 Parameter_Associations
=> New_List
(L
));
5999 Append_To
(Parameter_Associations
(Expr
), R
);
6004 elsif Is_Binary
then
6005 if Op_Name
= Name_Op_And
then
6006 Expr
:= Make_Op_And
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6007 elsif Op_Name
= Name_Op_Or
then
6008 Expr
:= Make_Op_Or
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6009 elsif Op_Name
= Name_Op_Xor
then
6010 Expr
:= Make_Op_Xor
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6011 elsif Op_Name
= Name_Op_Eq
then
6012 Expr
:= Make_Op_Eq
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6013 elsif Op_Name
= Name_Op_Ne
then
6014 Expr
:= Make_Op_Ne
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6015 elsif Op_Name
= Name_Op_Le
then
6016 Expr
:= Make_Op_Le
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6017 elsif Op_Name
= Name_Op_Gt
then
6018 Expr
:= Make_Op_Gt
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6019 elsif Op_Name
= Name_Op_Ge
then
6020 Expr
:= Make_Op_Ge
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6021 elsif Op_Name
= Name_Op_Lt
then
6022 Expr
:= Make_Op_Lt
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6023 elsif Op_Name
= Name_Op_Add
then
6024 Expr
:= Make_Op_Add
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6025 elsif Op_Name
= Name_Op_Subtract
then
6026 Expr
:= Make_Op_Subtract
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6027 elsif Op_Name
= Name_Op_Concat
then
6028 Expr
:= Make_Op_Concat
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6029 elsif Op_Name
= Name_Op_Multiply
then
6030 Expr
:= Make_Op_Multiply
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6031 elsif Op_Name
= Name_Op_Divide
then
6032 Expr
:= Make_Op_Divide
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6033 elsif Op_Name
= Name_Op_Mod
then
6034 Expr
:= Make_Op_Mod
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6035 elsif Op_Name
= Name_Op_Rem
then
6036 Expr
:= Make_Op_Rem
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6037 elsif Op_Name
= Name_Op_Expon
then
6038 Expr
:= Make_Op_Expon
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6044 if Op_Name
= Name_Op_Add
then
6045 Expr
:= Make_Op_Plus
(Loc
, Right_Opnd
=> L
);
6046 elsif Op_Name
= Name_Op_Subtract
then
6047 Expr
:= Make_Op_Minus
(Loc
, Right_Opnd
=> L
);
6048 elsif Op_Name
= Name_Op_Abs
then
6049 Expr
:= Make_Op_Abs
(Loc
, Right_Opnd
=> L
);
6050 elsif Op_Name
= Name_Op_Not
then
6051 Expr
:= Make_Op_Not
(Loc
, Right_Opnd
=> L
);
6056 Make_Expression_Function
(Loc
,
6057 Specification
=> Spec
,
6058 Expression
=> Expr
);
6061 end Build_Operator_Wrapper
;
6063 -------------------------------------------
6064 -- Build_Instance_Compilation_Unit_Nodes --
6065 -------------------------------------------
6067 procedure Build_Instance_Compilation_Unit_Nodes
6072 Decl_Cunit
: Node_Id
;
6073 Body_Cunit
: Node_Id
;
6075 New_Main
: constant Entity_Id
:= Defining_Entity
(Act_Decl
);
6076 Old_Main
: constant Entity_Id
:= Cunit_Entity
(Main_Unit
);
6079 -- A new compilation unit node is built for the instance declaration
6082 Make_Compilation_Unit
(Sloc
(N
),
6083 Context_Items
=> Empty_List
,
6085 Aux_Decls_Node
=> Make_Compilation_Unit_Aux
(Sloc
(N
)));
6087 Set_Parent_Spec
(Act_Decl
, Parent_Spec
(N
));
6089 -- The new compilation unit is linked to its body, but both share the
6090 -- same file, so we do not set Body_Required on the new unit so as not
6091 -- to create a spurious dependency on a non-existent body in the ali.
6092 -- This simplifies CodePeer unit traversal.
6094 -- We use the original instantiation compilation unit as the resulting
6095 -- compilation unit of the instance, since this is the main unit.
6097 Rewrite
(N
, Act_Body
);
6099 -- Propagate the aspect specifications from the package body template to
6100 -- the instantiated version of the package body.
6102 if Has_Aspects
(Act_Body
) then
6103 Set_Aspect_Specifications
6104 (N
, New_Copy_List_Tree
(Aspect_Specifications
(Act_Body
)));
6107 Body_Cunit
:= Parent
(N
);
6109 -- The two compilation unit nodes are linked by the Library_Unit field
6111 Set_Library_Unit
(Decl_Cunit
, Body_Cunit
);
6112 Set_Library_Unit
(Body_Cunit
, Decl_Cunit
);
6114 -- Preserve the private nature of the package if needed
6116 Set_Private_Present
(Decl_Cunit
, Private_Present
(Body_Cunit
));
6118 -- If the instance is not the main unit, its context, categorization
6119 -- and elaboration entity are not relevant to the compilation.
6121 if Body_Cunit
/= Cunit
(Main_Unit
) then
6122 Make_Instance_Unit
(Body_Cunit
, In_Main
=> False);
6126 -- The context clause items on the instantiation, which are now attached
6127 -- to the body compilation unit (since the body overwrote the original
6128 -- instantiation node), semantically belong on the spec, so copy them
6129 -- there. It's harmless to leave them on the body as well. In fact one
6130 -- could argue that they belong in both places.
6132 Citem
:= First
(Context_Items
(Body_Cunit
));
6133 while Present
(Citem
) loop
6134 Append
(New_Copy
(Citem
), Context_Items
(Decl_Cunit
));
6138 -- Propagate categorization flags on packages, so that they appear in
6139 -- the ali file for the spec of the unit.
6141 if Ekind
(New_Main
) = E_Package
then
6142 Set_Is_Pure
(Old_Main
, Is_Pure
(New_Main
));
6143 Set_Is_Preelaborated
(Old_Main
, Is_Preelaborated
(New_Main
));
6144 Set_Is_Remote_Types
(Old_Main
, Is_Remote_Types
(New_Main
));
6145 Set_Is_Shared_Passive
(Old_Main
, Is_Shared_Passive
(New_Main
));
6146 Set_Is_Remote_Call_Interface
6147 (Old_Main
, Is_Remote_Call_Interface
(New_Main
));
6150 -- Make entry in Units table, so that binder can generate call to
6151 -- elaboration procedure for body, if any.
6153 Make_Instance_Unit
(Body_Cunit
, In_Main
=> True);
6154 Main_Unit_Entity
:= New_Main
;
6155 Set_Cunit_Entity
(Main_Unit
, Main_Unit_Entity
);
6157 -- Build elaboration entity, since the instance may certainly generate
6158 -- elaboration code requiring a flag for protection.
6160 Build_Elaboration_Entity
(Decl_Cunit
, New_Main
);
6161 end Build_Instance_Compilation_Unit_Nodes
;
6163 -----------------------------
6164 -- Check_Access_Definition --
6165 -----------------------------
6167 procedure Check_Access_Definition
(N
: Node_Id
) is
6170 (Ada_Version
>= Ada_2005
and then Present
(Access_Definition
(N
)));
6172 end Check_Access_Definition
;
6174 -----------------------------------
6175 -- Check_Formal_Package_Instance --
6176 -----------------------------------
6178 -- If the formal has specific parameters, they must match those of the
6179 -- actual. Both of them are instances, and the renaming declarations for
6180 -- their formal parameters appear in the same order in both. The analyzed
6181 -- formal has been analyzed in the context of the current instance.
6183 procedure Check_Formal_Package_Instance
6184 (Formal_Pack
: Entity_Id
;
6185 Actual_Pack
: Entity_Id
)
6187 E1
: Entity_Id
:= First_Entity
(Actual_Pack
);
6188 E2
: Entity_Id
:= First_Entity
(Formal_Pack
);
6189 Prev_E1
: Entity_Id
;
6194 procedure Check_Mismatch
(B
: Boolean);
6195 -- Common error routine for mismatch between the parameters of the
6196 -- actual instance and those of the formal package.
6198 function Same_Instantiated_Constant
(E1
, E2
: Entity_Id
) return Boolean;
6199 -- The formal may come from a nested formal package, and the actual may
6200 -- have been constant-folded. To determine whether the two denote the
6201 -- same entity we may have to traverse several definitions to recover
6202 -- the ultimate entity that they refer to.
6204 function Same_Instantiated_Function
(E1
, E2
: Entity_Id
) return Boolean;
6205 -- The formal and the actual must be identical, but if both are
6206 -- given by attributes they end up renaming different generated bodies,
6207 -- and we must verify that the attributes themselves match.
6209 function Same_Instantiated_Variable
(E1
, E2
: Entity_Id
) return Boolean;
6210 -- Similarly, if the formal comes from a nested formal package, the
6211 -- actual may designate the formal through multiple renamings, which
6212 -- have to be followed to determine the original variable in question.
6214 --------------------
6215 -- Check_Mismatch --
6216 --------------------
6218 procedure Check_Mismatch
(B
: Boolean) is
6219 -- A Formal_Type_Declaration for a derived private type is rewritten
6220 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6221 -- which is why we examine the original node.
6223 Kind
: constant Node_Kind
:= Nkind
(Original_Node
(Parent
(E2
)));
6226 if Kind
= N_Formal_Type_Declaration
then
6229 elsif Nkind_In
(Kind
, N_Formal_Object_Declaration
,
6230 N_Formal_Package_Declaration
)
6231 or else Kind
in N_Formal_Subprogram_Declaration
6235 -- Ada 2012: If both formal and actual are incomplete types they
6238 elsif Is_Incomplete_Type
(E1
) and then Is_Incomplete_Type
(E2
) then
6243 ("actual for & in actual instance does not match formal",
6244 Parent
(Actual_Pack
), E1
);
6248 --------------------------------
6249 -- Same_Instantiated_Constant --
6250 --------------------------------
6252 function Same_Instantiated_Constant
6253 (E1
, E2
: Entity_Id
) return Boolean
6259 while Present
(Ent
) loop
6263 elsif Ekind
(Ent
) /= E_Constant
then
6266 elsif Is_Entity_Name
(Constant_Value
(Ent
)) then
6267 if Entity
(Constant_Value
(Ent
)) = E1
then
6270 Ent
:= Entity
(Constant_Value
(Ent
));
6273 -- The actual may be a constant that has been folded. Recover
6276 elsif Is_Entity_Name
(Original_Node
(Constant_Value
(Ent
))) then
6277 Ent
:= Entity
(Original_Node
(Constant_Value
(Ent
)));
6285 end Same_Instantiated_Constant
;
6287 --------------------------------
6288 -- Same_Instantiated_Function --
6289 --------------------------------
6291 function Same_Instantiated_Function
6292 (E1
, E2
: Entity_Id
) return Boolean
6296 if Alias
(E1
) = Alias
(E2
) then
6299 elsif Present
(Alias
(E2
)) then
6300 U1
:= Original_Node
(Unit_Declaration_Node
(E1
));
6301 U2
:= Original_Node
(Unit_Declaration_Node
(Alias
(E2
)));
6303 return Nkind
(U1
) = N_Subprogram_Renaming_Declaration
6304 and then Nkind
(Name
(U1
)) = N_Attribute_Reference
6306 and then Nkind
(U2
) = N_Subprogram_Renaming_Declaration
6307 and then Nkind
(Name
(U2
)) = N_Attribute_Reference
6310 Attribute_Name
(Name
(U1
)) = Attribute_Name
(Name
(U2
));
6314 end Same_Instantiated_Function
;
6316 --------------------------------
6317 -- Same_Instantiated_Variable --
6318 --------------------------------
6320 function Same_Instantiated_Variable
6321 (E1
, E2
: Entity_Id
) return Boolean
6323 function Original_Entity
(E
: Entity_Id
) return Entity_Id
;
6324 -- Follow chain of renamings to the ultimate ancestor
6326 ---------------------
6327 -- Original_Entity --
6328 ---------------------
6330 function Original_Entity
(E
: Entity_Id
) return Entity_Id
is
6335 while Nkind
(Parent
(Orig
)) = N_Object_Renaming_Declaration
6336 and then Present
(Renamed_Object
(Orig
))
6337 and then Is_Entity_Name
(Renamed_Object
(Orig
))
6339 Orig
:= Entity
(Renamed_Object
(Orig
));
6343 end Original_Entity
;
6345 -- Start of processing for Same_Instantiated_Variable
6348 return Ekind
(E1
) = Ekind
(E2
)
6349 and then Original_Entity
(E1
) = Original_Entity
(E2
);
6350 end Same_Instantiated_Variable
;
6352 -- Start of processing for Check_Formal_Package_Instance
6356 while Present
(E1
) and then Present
(E2
) loop
6357 exit when Ekind
(E1
) = E_Package
6358 and then Renamed_Entity
(E1
) = Renamed_Entity
(Actual_Pack
);
6360 -- If the formal is the renaming of the formal package, this
6361 -- is the end of its formal part, which may occur before the
6362 -- end of the formal part in the actual in the presence of
6363 -- defaulted parameters in the formal package.
6365 exit when Nkind
(Parent
(E2
)) = N_Package_Renaming_Declaration
6366 and then Renamed_Entity
(E2
) = Scope
(E2
);
6368 -- The analysis of the actual may generate additional internal
6369 -- entities. If the formal is defaulted, there is no corresponding
6370 -- analysis and the internal entities must be skipped, until we
6371 -- find corresponding entities again.
6373 if Comes_From_Source
(E2
)
6374 and then not Comes_From_Source
(E1
)
6375 and then Chars
(E1
) /= Chars
(E2
)
6377 while Present
(E1
) and then Chars
(E1
) /= Chars
(E2
) loop
6385 -- Entities may be declared without full declaration, such as
6386 -- itypes and predefined operators (concatenation for arrays, eg).
6387 -- Skip it and keep the formal entity to find a later match for it.
6389 elsif No
(Parent
(E2
)) and then Ekind
(E1
) /= Ekind
(E2
) then
6393 -- If the formal entity comes from a formal declaration, it was
6394 -- defaulted in the formal package, and no check is needed on it.
6396 elsif Nkind_In
(Original_Node
(Parent
(E2
)),
6397 N_Formal_Object_Declaration
,
6398 N_Formal_Type_Declaration
)
6400 -- If the formal is a tagged type the corresponding class-wide
6401 -- type has been generated as well, and it must be skipped.
6403 if Is_Type
(E2
) and then Is_Tagged_Type
(E2
) then
6409 -- Ditto for defaulted formal subprograms.
6411 elsif Is_Overloadable
(E1
)
6412 and then Nkind
(Unit_Declaration_Node
(E2
)) in
6413 N_Formal_Subprogram_Declaration
6417 elsif Is_Type
(E1
) then
6419 -- Subtypes must statically match. E1, E2 are the local entities
6420 -- that are subtypes of the actuals. Itypes generated for other
6421 -- parameters need not be checked, the check will be performed
6422 -- on the parameters themselves.
6424 -- If E2 is a formal type declaration, it is a defaulted parameter
6425 -- and needs no checking.
6427 if not Is_Itype
(E1
) and then not Is_Itype
(E2
) then
6430 or else Etype
(E1
) /= Etype
(E2
)
6431 or else not Subtypes_Statically_Match
(E1
, E2
));
6434 elsif Ekind
(E1
) = E_Constant
then
6436 -- IN parameters must denote the same static value, or the same
6437 -- constant, or the literal null.
6439 Expr1
:= Expression
(Parent
(E1
));
6441 if Ekind
(E2
) /= E_Constant
then
6442 Check_Mismatch
(True);
6445 Expr2
:= Expression
(Parent
(E2
));
6448 if Is_OK_Static_Expression
(Expr1
) then
6449 if not Is_OK_Static_Expression
(Expr2
) then
6450 Check_Mismatch
(True);
6452 elsif Is_Discrete_Type
(Etype
(E1
)) then
6454 V1
: constant Uint
:= Expr_Value
(Expr1
);
6455 V2
: constant Uint
:= Expr_Value
(Expr2
);
6457 Check_Mismatch
(V1
/= V2
);
6460 elsif Is_Real_Type
(Etype
(E1
)) then
6462 V1
: constant Ureal
:= Expr_Value_R
(Expr1
);
6463 V2
: constant Ureal
:= Expr_Value_R
(Expr2
);
6465 Check_Mismatch
(V1
/= V2
);
6468 elsif Is_String_Type
(Etype
(E1
))
6469 and then Nkind
(Expr1
) = N_String_Literal
6471 if Nkind
(Expr2
) /= N_String_Literal
then
6472 Check_Mismatch
(True);
6475 (not String_Equal
(Strval
(Expr1
), Strval
(Expr2
)));
6479 elsif Is_Entity_Name
(Expr1
) then
6480 if Is_Entity_Name
(Expr2
) then
6481 if Entity
(Expr1
) = Entity
(Expr2
) then
6485 (not Same_Instantiated_Constant
6486 (Entity
(Expr1
), Entity
(Expr2
)));
6490 Check_Mismatch
(True);
6493 elsif Is_Entity_Name
(Original_Node
(Expr1
))
6494 and then Is_Entity_Name
(Expr2
)
6495 and then Same_Instantiated_Constant
6496 (Entity
(Original_Node
(Expr1
)), Entity
(Expr2
))
6500 elsif Nkind
(Expr1
) = N_Null
then
6501 Check_Mismatch
(Nkind
(Expr1
) /= N_Null
);
6504 Check_Mismatch
(True);
6507 elsif Ekind
(E1
) = E_Variable
then
6508 Check_Mismatch
(not Same_Instantiated_Variable
(E1
, E2
));
6510 elsif Ekind
(E1
) = E_Package
then
6512 (Ekind
(E1
) /= Ekind
(E2
)
6513 or else (Present
(Renamed_Object
(E2
))
6514 and then Renamed_Object
(E1
) /=
6515 Renamed_Object
(E2
)));
6517 elsif Is_Overloadable
(E1
) then
6518 -- Verify that the actual subprograms match. Note that actuals
6519 -- that are attributes are rewritten as subprograms. If the
6520 -- subprogram in the formal package is defaulted, no check is
6521 -- needed. Note that this can only happen in Ada 2005 when the
6522 -- formal package can be partially parameterized.
6524 if Nkind
(Unit_Declaration_Node
(E1
)) =
6525 N_Subprogram_Renaming_Declaration
6526 and then From_Default
(Unit_Declaration_Node
(E1
))
6530 -- If the formal package has an "others" box association that
6531 -- covers this formal, there is no need for a check either.
6533 elsif Nkind
(Unit_Declaration_Node
(E2
)) in
6534 N_Formal_Subprogram_Declaration
6535 and then Box_Present
(Unit_Declaration_Node
(E2
))
6539 -- No check needed if subprogram is a defaulted null procedure
6541 elsif No
(Alias
(E2
))
6542 and then Ekind
(E2
) = E_Procedure
6544 Null_Present
(Specification
(Unit_Declaration_Node
(E2
)))
6548 -- Otherwise the actual in the formal and the actual in the
6549 -- instantiation of the formal must match, up to renamings.
6553 (Ekind
(E2
) /= Ekind
(E1
)
6554 or else not Same_Instantiated_Function
(E1
, E2
));
6558 raise Program_Error
;
6566 end Check_Formal_Package_Instance
;
6568 ---------------------------
6569 -- Check_Formal_Packages --
6570 ---------------------------
6572 procedure Check_Formal_Packages
(P_Id
: Entity_Id
) is
6574 Formal_P
: Entity_Id
;
6575 Formal_Decl
: Node_Id
;
6578 -- Iterate through the declarations in the instance, looking for package
6579 -- renaming declarations that denote instances of formal packages. Stop
6580 -- when we find the renaming of the current package itself. The
6581 -- declaration for a formal package without a box is followed by an
6582 -- internal entity that repeats the instantiation.
6584 E
:= First_Entity
(P_Id
);
6585 while Present
(E
) loop
6586 if Ekind
(E
) = E_Package
then
6587 if Renamed_Object
(E
) = P_Id
then
6590 elsif Nkind
(Parent
(E
)) /= N_Package_Renaming_Declaration
then
6594 Formal_Decl
:= Parent
(Associated_Formal_Package
(E
));
6596 -- Nothing to check if the formal has a box or an others_clause
6597 -- (necessarily with a box).
6599 if Box_Present
(Formal_Decl
) then
6602 elsif Nkind
(First
(Generic_Associations
(Formal_Decl
))) =
6605 -- The internal validating package was generated but formal
6606 -- and instance are known to be compatible.
6608 Formal_P
:= Next_Entity
(E
);
6609 Remove
(Unit_Declaration_Node
(Formal_P
));
6612 Formal_P
:= Next_Entity
(E
);
6614 -- If the instance is within an enclosing instance body
6615 -- there is no need to verify the legality of current formal
6616 -- packages because they were legal in the generic body.
6617 -- This optimization may be applicable elsewhere, and it
6618 -- also removes spurious errors that may arise with
6619 -- on-the-fly inlining and confusion between private and
6622 if not In_Instance_Body
then
6623 Check_Formal_Package_Instance
(Formal_P
, E
);
6626 -- After checking, remove the internal validating package.
6627 -- It is only needed for semantic checks, and as it may
6628 -- contain generic formal declarations it should not reach
6631 Remove
(Unit_Declaration_Node
(Formal_P
));
6638 end Check_Formal_Packages
;
6640 ---------------------------------
6641 -- Check_Forward_Instantiation --
6642 ---------------------------------
6644 procedure Check_Forward_Instantiation
(Decl
: Node_Id
) is
6646 Gen_Comp
: Entity_Id
:= Cunit_Entity
(Get_Source_Unit
(Decl
));
6649 -- The instantiation appears before the generic body if we are in the
6650 -- scope of the unit containing the generic, either in its spec or in
6651 -- the package body, and before the generic body.
6653 if Ekind
(Gen_Comp
) = E_Package_Body
then
6654 Gen_Comp
:= Spec_Entity
(Gen_Comp
);
6657 if In_Open_Scopes
(Gen_Comp
)
6658 and then No
(Corresponding_Body
(Decl
))
6663 and then not Is_Compilation_Unit
(S
)
6664 and then not Is_Child_Unit
(S
)
6666 if Ekind
(S
) = E_Package
then
6667 Set_Has_Forward_Instantiation
(S
);
6673 end Check_Forward_Instantiation
;
6675 ---------------------------
6676 -- Check_Generic_Actuals --
6677 ---------------------------
6679 -- The visibility of the actuals may be different between the point of
6680 -- generic instantiation and the instantiation of the body.
6682 procedure Check_Generic_Actuals
6683 (Instance
: Entity_Id
;
6684 Is_Formal_Box
: Boolean)
6689 function Denotes_Previous_Actual
(Typ
: Entity_Id
) return Boolean;
6690 -- For a formal that is an array type, the component type is often a
6691 -- previous formal in the same unit. The privacy status of the component
6692 -- type will have been examined earlier in the traversal of the
6693 -- corresponding actuals, and this status should not be modified for
6694 -- the array (sub)type itself. However, if the base type of the array
6695 -- (sub)type is private, its full view must be restored in the body to
6696 -- be consistent with subsequent index subtypes, etc.
6698 -- To detect this case we have to rescan the list of formals, which is
6699 -- usually short enough to ignore the resulting inefficiency.
6701 -----------------------------
6702 -- Denotes_Previous_Actual --
6703 -----------------------------
6705 function Denotes_Previous_Actual
(Typ
: Entity_Id
) return Boolean is
6709 Prev
:= First_Entity
(Instance
);
6710 while Present
(Prev
) loop
6712 and then Nkind
(Parent
(Prev
)) = N_Subtype_Declaration
6713 and then Is_Entity_Name
(Subtype_Indication
(Parent
(Prev
)))
6714 and then Entity
(Subtype_Indication
(Parent
(Prev
))) = Typ
6727 end Denotes_Previous_Actual
;
6729 -- Start of processing for Check_Generic_Actuals
6732 E
:= First_Entity
(Instance
);
6733 while Present
(E
) loop
6735 and then Nkind
(Parent
(E
)) = N_Subtype_Declaration
6736 and then Scope
(Etype
(E
)) /= Instance
6737 and then Is_Entity_Name
(Subtype_Indication
(Parent
(E
)))
6739 if Is_Array_Type
(E
)
6740 and then not Is_Private_Type
(Etype
(E
))
6741 and then Denotes_Previous_Actual
(Component_Type
(E
))
6745 Check_Private_View
(Subtype_Indication
(Parent
(E
)));
6748 Set_Is_Generic_Actual_Type
(E
, True);
6749 Set_Is_Hidden
(E
, False);
6750 Set_Is_Potentially_Use_Visible
(E
, In_Use
(Instance
));
6752 -- We constructed the generic actual type as a subtype of the
6753 -- supplied type. This means that it normally would not inherit
6754 -- subtype specific attributes of the actual, which is wrong for
6755 -- the generic case.
6757 Astype
:= Ancestor_Subtype
(E
);
6761 -- This can happen when E is an itype that is the full view of
6762 -- a private type completed, e.g. with a constrained array. In
6763 -- that case, use the first subtype, which will carry size
6764 -- information. The base type itself is unconstrained and will
6767 Astype
:= First_Subtype
(E
);
6770 Set_Size_Info
(E
, (Astype
));
6771 Set_RM_Size
(E
, RM_Size
(Astype
));
6772 Set_First_Rep_Item
(E
, First_Rep_Item
(Astype
));
6774 if Is_Discrete_Or_Fixed_Point_Type
(E
) then
6775 Set_RM_Size
(E
, RM_Size
(Astype
));
6777 -- In nested instances, the base type of an access actual may
6778 -- itself be private, and need to be exchanged.
6780 elsif Is_Access_Type
(E
)
6781 and then Is_Private_Type
(Etype
(E
))
6784 (New_Occurrence_Of
(Etype
(E
), Sloc
(Instance
)));
6787 elsif Ekind
(E
) = E_Package
then
6789 -- If this is the renaming for the current instance, we're done.
6790 -- Otherwise it is a formal package. If the corresponding formal
6791 -- was declared with a box, the (instantiations of the) generic
6792 -- formal part are also visible. Otherwise, ignore the entity
6793 -- created to validate the actuals.
6795 if Renamed_Object
(E
) = Instance
then
6798 elsif Nkind
(Parent
(E
)) /= N_Package_Renaming_Declaration
then
6801 -- The visibility of a formal of an enclosing generic is already
6804 elsif Denotes_Formal_Package
(E
) then
6807 elsif Present
(Associated_Formal_Package
(E
))
6808 and then not Is_Generic_Formal
(E
)
6810 if Box_Present
(Parent
(Associated_Formal_Package
(E
))) then
6811 Check_Generic_Actuals
(Renamed_Object
(E
), True);
6814 Check_Generic_Actuals
(Renamed_Object
(E
), False);
6817 Set_Is_Hidden
(E
, False);
6820 -- If this is a subprogram instance (in a wrapper package) the
6821 -- actual is fully visible.
6823 elsif Is_Wrapper_Package
(Instance
) then
6824 Set_Is_Hidden
(E
, False);
6826 -- If the formal package is declared with a box, or if the formal
6827 -- parameter is defaulted, it is visible in the body.
6829 elsif Is_Formal_Box
or else Is_Visible_Formal
(E
) then
6830 Set_Is_Hidden
(E
, False);
6833 if Ekind
(E
) = E_Constant
then
6835 -- If the type of the actual is a private type declared in the
6836 -- enclosing scope of the generic unit, the body of the generic
6837 -- sees the full view of the type (because it has to appear in
6838 -- the corresponding package body). If the type is private now,
6839 -- exchange views to restore the proper visiblity in the instance.
6842 Typ
: constant Entity_Id
:= Base_Type
(Etype
(E
));
6843 -- The type of the actual
6848 Parent_Scope
: Entity_Id
;
6849 -- The enclosing scope of the generic unit
6852 if Is_Wrapper_Package
(Instance
) then
6856 (Unit_Declaration_Node
6857 (Related_Instance
(Instance
))));
6860 Generic_Parent
(Package_Specification
(Instance
));
6863 Parent_Scope
:= Scope
(Gen_Id
);
6865 -- The exchange is only needed if the generic is defined
6866 -- within a package which is not a common ancestor of the
6867 -- scope of the instance, and is not already in scope.
6869 if Is_Private_Type
(Typ
)
6870 and then Scope
(Typ
) = Parent_Scope
6871 and then Scope
(Instance
) /= Parent_Scope
6872 and then Ekind
(Parent_Scope
) = E_Package
6873 and then not Is_Child_Unit
(Gen_Id
)
6877 -- If the type of the entity is a subtype, it may also have
6878 -- to be made visible, together with the base type of its
6879 -- full view, after exchange.
6881 if Is_Private_Type
(Etype
(E
)) then
6882 Switch_View
(Etype
(E
));
6883 Switch_View
(Base_Type
(Etype
(E
)));
6891 end Check_Generic_Actuals
;
6893 ------------------------------
6894 -- Check_Generic_Child_Unit --
6895 ------------------------------
6897 procedure Check_Generic_Child_Unit
6899 Parent_Installed
: in out Boolean)
6901 Loc
: constant Source_Ptr
:= Sloc
(Gen_Id
);
6902 Gen_Par
: Entity_Id
:= Empty
;
6904 Inst_Par
: Entity_Id
;
6907 function Find_Generic_Child
6909 Id
: Node_Id
) return Entity_Id
;
6910 -- Search generic parent for possible child unit with the given name
6912 function In_Enclosing_Instance
return Boolean;
6913 -- Within an instance of the parent, the child unit may be denoted by
6914 -- a simple name, or an abbreviated expanded name. Examine enclosing
6915 -- scopes to locate a possible parent instantiation.
6917 ------------------------
6918 -- Find_Generic_Child --
6919 ------------------------
6921 function Find_Generic_Child
6923 Id
: Node_Id
) return Entity_Id
6928 -- If entity of name is already set, instance has already been
6929 -- resolved, e.g. in an enclosing instantiation.
6931 if Present
(Entity
(Id
)) then
6932 if Scope
(Entity
(Id
)) = Scop
then
6939 E
:= First_Entity
(Scop
);
6940 while Present
(E
) loop
6941 if Chars
(E
) = Chars
(Id
)
6942 and then Is_Child_Unit
(E
)
6944 if Is_Child_Unit
(E
)
6945 and then not Is_Visible_Lib_Unit
(E
)
6948 ("generic child unit& is not visible", Gen_Id
, E
);
6960 end Find_Generic_Child
;
6962 ---------------------------
6963 -- In_Enclosing_Instance --
6964 ---------------------------
6966 function In_Enclosing_Instance
return Boolean is
6967 Enclosing_Instance
: Node_Id
;
6968 Instance_Decl
: Node_Id
;
6971 -- We do not inline any call that contains instantiations, except
6972 -- for instantiations of Unchecked_Conversion, so if we are within
6973 -- an inlined body the current instance does not require parents.
6975 if In_Inlined_Body
then
6976 pragma Assert
(Chars
(Gen_Id
) = Name_Unchecked_Conversion
);
6980 -- Loop to check enclosing scopes
6982 Enclosing_Instance
:= Current_Scope
;
6983 while Present
(Enclosing_Instance
) loop
6984 Instance_Decl
:= Unit_Declaration_Node
(Enclosing_Instance
);
6986 if Ekind
(Enclosing_Instance
) = E_Package
6987 and then Is_Generic_Instance
(Enclosing_Instance
)
6989 (Generic_Parent
(Specification
(Instance_Decl
)))
6991 -- Check whether the generic we are looking for is a child of
6994 E
:= Find_Generic_Child
6995 (Generic_Parent
(Specification
(Instance_Decl
)), Gen_Id
);
6996 exit when Present
(E
);
7002 Enclosing_Instance
:= Scope
(Enclosing_Instance
);
7014 Make_Expanded_Name
(Loc
,
7016 Prefix
=> New_Occurrence_Of
(Enclosing_Instance
, Loc
),
7017 Selector_Name
=> New_Occurrence_Of
(E
, Loc
)));
7019 Set_Entity
(Gen_Id
, E
);
7020 Set_Etype
(Gen_Id
, Etype
(E
));
7021 Parent_Installed
:= False; -- Already in scope.
7024 end In_Enclosing_Instance
;
7026 -- Start of processing for Check_Generic_Child_Unit
7029 -- If the name of the generic is given by a selected component, it may
7030 -- be the name of a generic child unit, and the prefix is the name of an
7031 -- instance of the parent, in which case the child unit must be visible.
7032 -- If this instance is not in scope, it must be placed there and removed
7033 -- after instantiation, because what is being instantiated is not the
7034 -- original child, but the corresponding child present in the instance
7037 -- If the child is instantiated within the parent, it can be given by
7038 -- a simple name. In this case the instance is already in scope, but
7039 -- the child generic must be recovered from the generic parent as well.
7041 if Nkind
(Gen_Id
) = N_Selected_Component
then
7042 S
:= Selector_Name
(Gen_Id
);
7043 Analyze
(Prefix
(Gen_Id
));
7044 Inst_Par
:= Entity
(Prefix
(Gen_Id
));
7046 if Ekind
(Inst_Par
) = E_Package
7047 and then Present
(Renamed_Object
(Inst_Par
))
7049 Inst_Par
:= Renamed_Object
(Inst_Par
);
7052 if Ekind
(Inst_Par
) = E_Package
then
7053 if Nkind
(Parent
(Inst_Par
)) = N_Package_Specification
then
7054 Gen_Par
:= Generic_Parent
(Parent
(Inst_Par
));
7056 elsif Nkind
(Parent
(Inst_Par
)) = N_Defining_Program_Unit_Name
7058 Nkind
(Parent
(Parent
(Inst_Par
))) = N_Package_Specification
7060 Gen_Par
:= Generic_Parent
(Parent
(Parent
(Inst_Par
)));
7063 elsif Ekind
(Inst_Par
) = E_Generic_Package
7064 and then Nkind
(Parent
(Gen_Id
)) = N_Formal_Package_Declaration
7066 -- A formal package may be a real child package, and not the
7067 -- implicit instance within a parent. In this case the child is
7068 -- not visible and has to be retrieved explicitly as well.
7070 Gen_Par
:= Inst_Par
;
7073 if Present
(Gen_Par
) then
7075 -- The prefix denotes an instantiation. The entity itself may be a
7076 -- nested generic, or a child unit.
7078 E
:= Find_Generic_Child
(Gen_Par
, S
);
7081 Change_Selected_Component_To_Expanded_Name
(Gen_Id
);
7082 Set_Entity
(Gen_Id
, E
);
7083 Set_Etype
(Gen_Id
, Etype
(E
));
7085 Set_Etype
(S
, Etype
(E
));
7087 -- Indicate that this is a reference to the parent
7089 if In_Extended_Main_Source_Unit
(Gen_Id
) then
7090 Set_Is_Instantiated
(Inst_Par
);
7093 -- A common mistake is to replicate the naming scheme of a
7094 -- hierarchy by instantiating a generic child directly, rather
7095 -- than the implicit child in a parent instance:
7097 -- generic .. package Gpar is ..
7098 -- generic .. package Gpar.Child is ..
7099 -- package Par is new Gpar ();
7102 -- package Par.Child is new Gpar.Child ();
7103 -- rather than Par.Child
7105 -- In this case the instantiation is within Par, which is an
7106 -- instance, but Gpar does not denote Par because we are not IN
7107 -- the instance of Gpar, so this is illegal. The test below
7108 -- recognizes this particular case.
7110 if Is_Child_Unit
(E
)
7111 and then not Comes_From_Source
(Entity
(Prefix
(Gen_Id
)))
7112 and then (not In_Instance
7113 or else Nkind
(Parent
(Parent
(Gen_Id
))) =
7117 ("prefix of generic child unit must be instance of parent",
7121 if not In_Open_Scopes
(Inst_Par
)
7122 and then Nkind
(Parent
(Gen_Id
)) not in
7123 N_Generic_Renaming_Declaration
7125 Install_Parent
(Inst_Par
);
7126 Parent_Installed
:= True;
7128 elsif In_Open_Scopes
(Inst_Par
) then
7130 -- If the parent is already installed, install the actuals
7131 -- for its formal packages. This is necessary when the child
7132 -- instance is a child of the parent instance: in this case,
7133 -- the parent is placed on the scope stack but the formal
7134 -- packages are not made visible.
7136 Install_Formal_Packages
(Inst_Par
);
7140 -- If the generic parent does not contain an entity that
7141 -- corresponds to the selector, the instance doesn't either.
7142 -- Analyzing the node will yield the appropriate error message.
7143 -- If the entity is not a child unit, then it is an inner
7144 -- generic in the parent.
7152 if Is_Child_Unit
(Entity
(Gen_Id
))
7154 Nkind
(Parent
(Gen_Id
)) not in N_Generic_Renaming_Declaration
7155 and then not In_Open_Scopes
(Inst_Par
)
7157 Install_Parent
(Inst_Par
);
7158 Parent_Installed
:= True;
7160 -- The generic unit may be the renaming of the implicit child
7161 -- present in an instance. In that case the parent instance is
7162 -- obtained from the name of the renamed entity.
7164 elsif Ekind
(Entity
(Gen_Id
)) = E_Generic_Package
7165 and then Present
(Renamed_Entity
(Entity
(Gen_Id
)))
7166 and then Is_Child_Unit
(Renamed_Entity
(Entity
(Gen_Id
)))
7169 Renamed_Package
: constant Node_Id
:=
7170 Name
(Parent
(Entity
(Gen_Id
)));
7172 if Nkind
(Renamed_Package
) = N_Expanded_Name
then
7173 Inst_Par
:= Entity
(Prefix
(Renamed_Package
));
7174 Install_Parent
(Inst_Par
);
7175 Parent_Installed
:= True;
7181 elsif Nkind
(Gen_Id
) = N_Expanded_Name
then
7183 -- Entity already present, analyze prefix, whose meaning may be an
7184 -- instance in the current context. If it is an instance of a
7185 -- relative within another, the proper parent may still have to be
7186 -- installed, if they are not of the same generation.
7188 Analyze
(Prefix
(Gen_Id
));
7190 -- Prevent cascaded errors
7192 if Etype
(Prefix
(Gen_Id
)) = Any_Type
then
7196 -- In the unlikely case that a local declaration hides the name of
7197 -- the parent package, locate it on the homonym chain. If the context
7198 -- is an instance of the parent, the renaming entity is flagged as
7201 Inst_Par
:= Entity
(Prefix
(Gen_Id
));
7202 while Present
(Inst_Par
)
7203 and then not Is_Package_Or_Generic_Package
(Inst_Par
)
7205 Inst_Par
:= Homonym
(Inst_Par
);
7208 pragma Assert
(Present
(Inst_Par
));
7209 Set_Entity
(Prefix
(Gen_Id
), Inst_Par
);
7211 if In_Enclosing_Instance
then
7214 elsif Present
(Entity
(Gen_Id
))
7215 and then Is_Child_Unit
(Entity
(Gen_Id
))
7216 and then not In_Open_Scopes
(Inst_Par
)
7218 Install_Parent
(Inst_Par
);
7219 Parent_Installed
:= True;
7222 elsif In_Enclosing_Instance
then
7224 -- The child unit is found in some enclosing scope
7231 -- If this is the renaming of the implicit child in a parent
7232 -- instance, recover the parent name and install it.
7234 if Is_Entity_Name
(Gen_Id
) then
7235 E
:= Entity
(Gen_Id
);
7237 if Is_Generic_Unit
(E
)
7238 and then Nkind
(Parent
(E
)) in N_Generic_Renaming_Declaration
7239 and then Is_Child_Unit
(Renamed_Object
(E
))
7240 and then Is_Generic_Unit
(Scope
(Renamed_Object
(E
)))
7241 and then Nkind
(Name
(Parent
(E
))) = N_Expanded_Name
7243 Rewrite
(Gen_Id
, New_Copy_Tree
(Name
(Parent
(E
))));
7244 Inst_Par
:= Entity
(Prefix
(Gen_Id
));
7246 if not In_Open_Scopes
(Inst_Par
) then
7247 Install_Parent
(Inst_Par
);
7248 Parent_Installed
:= True;
7251 -- If it is a child unit of a non-generic parent, it may be
7252 -- use-visible and given by a direct name. Install parent as
7255 elsif Is_Generic_Unit
(E
)
7256 and then Is_Child_Unit
(E
)
7258 Nkind
(Parent
(Gen_Id
)) not in N_Generic_Renaming_Declaration
7259 and then not Is_Generic_Unit
(Scope
(E
))
7261 if not In_Open_Scopes
(Scope
(E
)) then
7262 Install_Parent
(Scope
(E
));
7263 Parent_Installed
:= True;
7268 end Check_Generic_Child_Unit
;
7270 -----------------------------
7271 -- Check_Hidden_Child_Unit --
7272 -----------------------------
7274 procedure Check_Hidden_Child_Unit
7276 Gen_Unit
: Entity_Id
;
7277 Act_Decl_Id
: Entity_Id
)
7279 Gen_Id
: constant Node_Id
:= Name
(N
);
7282 if Is_Child_Unit
(Gen_Unit
)
7283 and then Is_Child_Unit
(Act_Decl_Id
)
7284 and then Nkind
(Gen_Id
) = N_Expanded_Name
7285 and then Entity
(Prefix
(Gen_Id
)) = Scope
(Act_Decl_Id
)
7286 and then Chars
(Gen_Unit
) = Chars
(Act_Decl_Id
)
7288 Error_Msg_Node_2
:= Scope
(Act_Decl_Id
);
7290 ("generic unit & is implicitly declared in &",
7291 Defining_Unit_Name
(N
), Gen_Unit
);
7292 Error_Msg_N
("\instance must have different name",
7293 Defining_Unit_Name
(N
));
7295 end Check_Hidden_Child_Unit
;
7297 ------------------------
7298 -- Check_Private_View --
7299 ------------------------
7301 procedure Check_Private_View
(N
: Node_Id
) is
7302 T
: constant Entity_Id
:= Etype
(N
);
7306 -- Exchange views if the type was not private in the generic but is
7307 -- private at the point of instantiation. Do not exchange views if
7308 -- the scope of the type is in scope. This can happen if both generic
7309 -- and instance are sibling units, or if type is defined in a parent.
7310 -- In this case the visibility of the type will be correct for all
7314 BT
:= Base_Type
(T
);
7316 if Is_Private_Type
(T
)
7317 and then not Has_Private_View
(N
)
7318 and then Present
(Full_View
(T
))
7319 and then not In_Open_Scopes
(Scope
(T
))
7321 -- In the generic, the full type was visible. Save the private
7322 -- entity, for subsequent exchange.
7326 elsif Has_Private_View
(N
)
7327 and then not Is_Private_Type
(T
)
7328 and then not Has_Been_Exchanged
(T
)
7329 and then Etype
(Get_Associated_Node
(N
)) /= T
7331 -- Only the private declaration was visible in the generic. If
7332 -- the type appears in a subtype declaration, the subtype in the
7333 -- instance must have a view compatible with that of its parent,
7334 -- which must be exchanged (see corresponding code in Restore_
7335 -- Private_Views). Otherwise, if the type is defined in a parent
7336 -- unit, leave full visibility within instance, which is safe.
7338 if In_Open_Scopes
(Scope
(Base_Type
(T
)))
7339 and then not Is_Private_Type
(Base_Type
(T
))
7340 and then Comes_From_Source
(Base_Type
(T
))
7344 elsif Nkind
(Parent
(N
)) = N_Subtype_Declaration
7345 or else not In_Private_Part
(Scope
(Base_Type
(T
)))
7347 Prepend_Elmt
(T
, Exchanged_Views
);
7348 Exchange_Declarations
(Etype
(Get_Associated_Node
(N
)));
7351 -- For composite types with inconsistent representation exchange
7352 -- component types accordingly.
7354 elsif Is_Access_Type
(T
)
7355 and then Is_Private_Type
(Designated_Type
(T
))
7356 and then not Has_Private_View
(N
)
7357 and then Present
(Full_View
(Designated_Type
(T
)))
7359 Switch_View
(Designated_Type
(T
));
7361 elsif Is_Array_Type
(T
) then
7362 if Is_Private_Type
(Component_Type
(T
))
7363 and then not Has_Private_View
(N
)
7364 and then Present
(Full_View
(Component_Type
(T
)))
7366 Switch_View
(Component_Type
(T
));
7369 -- The normal exchange mechanism relies on the setting of a
7370 -- flag on the reference in the generic. However, an additional
7371 -- mechanism is needed for types that are not explicitly
7372 -- mentioned in the generic, but may be needed in expanded code
7373 -- in the instance. This includes component types of arrays and
7374 -- designated types of access types. This processing must also
7375 -- include the index types of arrays which we take care of here.
7382 Indx
:= First_Index
(T
);
7383 while Present
(Indx
) loop
7384 Typ
:= Base_Type
(Etype
(Indx
));
7386 if Is_Private_Type
(Typ
)
7387 and then Present
(Full_View
(Typ
))
7396 elsif Is_Private_Type
(T
)
7397 and then Present
(Full_View
(T
))
7398 and then Is_Array_Type
(Full_View
(T
))
7399 and then Is_Private_Type
(Component_Type
(Full_View
(T
)))
7403 -- Finally, a non-private subtype may have a private base type, which
7404 -- must be exchanged for consistency. This can happen when a package
7405 -- body is instantiated, when the scope stack is empty but in fact
7406 -- the subtype and the base type are declared in an enclosing scope.
7408 -- Note that in this case we introduce an inconsistency in the view
7409 -- set, because we switch the base type BT, but there could be some
7410 -- private dependent subtypes of BT which remain unswitched. Such
7411 -- subtypes might need to be switched at a later point (see specific
7412 -- provision for that case in Switch_View).
7414 elsif not Is_Private_Type
(T
)
7415 and then not Has_Private_View
(N
)
7416 and then Is_Private_Type
(BT
)
7417 and then Present
(Full_View
(BT
))
7418 and then not Is_Generic_Type
(BT
)
7419 and then not In_Open_Scopes
(BT
)
7421 Prepend_Elmt
(Full_View
(BT
), Exchanged_Views
);
7422 Exchange_Declarations
(BT
);
7425 end Check_Private_View
;
7427 -----------------------------
7428 -- Check_Hidden_Primitives --
7429 -----------------------------
7431 function Check_Hidden_Primitives
(Assoc_List
: List_Id
) return Elist_Id
is
7434 Result
: Elist_Id
:= No_Elist
;
7437 if No
(Assoc_List
) then
7441 -- Traverse the list of associations between formals and actuals
7442 -- searching for renamings of tagged types
7444 Actual
:= First
(Assoc_List
);
7445 while Present
(Actual
) loop
7446 if Nkind
(Actual
) = N_Subtype_Declaration
then
7447 Gen_T
:= Generic_Parent_Type
(Actual
);
7449 if Present
(Gen_T
) and then Is_Tagged_Type
(Gen_T
) then
7451 -- Traverse the list of primitives of the actual types
7452 -- searching for hidden primitives that are visible in the
7453 -- corresponding generic formal; leave them visible and
7454 -- append them to Result to restore their decoration later.
7456 Install_Hidden_Primitives
7457 (Prims_List
=> Result
,
7459 Act_T
=> Entity
(Subtype_Indication
(Actual
)));
7467 end Check_Hidden_Primitives
;
7469 --------------------------
7470 -- Contains_Instance_Of --
7471 --------------------------
7473 function Contains_Instance_Of
7476 N
: Node_Id
) return Boolean
7484 -- Verify that there are no circular instantiations. We check whether
7485 -- the unit contains an instance of the current scope or some enclosing
7486 -- scope (in case one of the instances appears in a subunit). Longer
7487 -- circularities involving subunits might seem too pathological to
7488 -- consider, but they were not too pathological for the authors of
7489 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7490 -- enclosing generic scopes as containing an instance.
7493 -- Within a generic subprogram body, the scope is not generic, to
7494 -- allow for recursive subprograms. Use the declaration to determine
7495 -- whether this is a generic unit.
7497 if Ekind
(Scop
) = E_Generic_Package
7498 or else (Is_Subprogram
(Scop
)
7499 and then Nkind
(Unit_Declaration_Node
(Scop
)) =
7500 N_Generic_Subprogram_Declaration
)
7502 Elmt
:= First_Elmt
(Inner_Instances
(Inner
));
7504 while Present
(Elmt
) loop
7505 if Node
(Elmt
) = Scop
then
7506 Error_Msg_Node_2
:= Inner
;
7508 ("circular Instantiation: & instantiated within &!",
7512 elsif Node
(Elmt
) = Inner
then
7515 elsif Contains_Instance_Of
(Node
(Elmt
), Scop
, N
) then
7516 Error_Msg_Node_2
:= Inner
;
7518 ("circular Instantiation: & instantiated within &!",
7526 -- Indicate that Inner is being instantiated within Scop
7528 Append_Elmt
(Inner
, Inner_Instances
(Scop
));
7531 if Scop
= Standard_Standard
then
7534 Scop
:= Scope
(Scop
);
7539 end Contains_Instance_Of
;
7541 -----------------------
7542 -- Copy_Generic_Node --
7543 -----------------------
7545 function Copy_Generic_Node
7547 Parent_Id
: Node_Id
;
7548 Instantiating
: Boolean) return Node_Id
7553 function Copy_Generic_Descendant
(D
: Union_Id
) return Union_Id
;
7554 -- Check the given value of one of the Fields referenced by the current
7555 -- node to determine whether to copy it recursively. The field may hold
7556 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7557 -- Char) in which case it need not be copied.
7559 procedure Copy_Descendants
;
7560 -- Common utility for various nodes
7562 function Copy_Generic_Elist
(E
: Elist_Id
) return Elist_Id
;
7563 -- Make copy of element list
7565 function Copy_Generic_List
7567 Parent_Id
: Node_Id
) return List_Id
;
7568 -- Apply Copy_Node recursively to the members of a node list
7570 function In_Defining_Unit_Name
(Nam
: Node_Id
) return Boolean;
7571 -- True if an identifier is part of the defining program unit name of
7572 -- a child unit. The entity of such an identifier must be kept (for
7573 -- ASIS use) even though as the name of an enclosing generic it would
7574 -- otherwise not be preserved in the generic tree.
7576 ----------------------
7577 -- Copy_Descendants --
7578 ----------------------
7580 procedure Copy_Descendants
is
7581 use Atree
.Unchecked_Access
;
7582 -- This code section is part of the implementation of an untyped
7583 -- tree traversal, so it needs direct access to node fields.
7586 Set_Field1
(New_N
, Copy_Generic_Descendant
(Field1
(N
)));
7587 Set_Field2
(New_N
, Copy_Generic_Descendant
(Field2
(N
)));
7588 Set_Field3
(New_N
, Copy_Generic_Descendant
(Field3
(N
)));
7589 Set_Field4
(New_N
, Copy_Generic_Descendant
(Field4
(N
)));
7590 Set_Field5
(New_N
, Copy_Generic_Descendant
(Field5
(N
)));
7591 end Copy_Descendants
;
7593 -----------------------------
7594 -- Copy_Generic_Descendant --
7595 -----------------------------
7597 function Copy_Generic_Descendant
(D
: Union_Id
) return Union_Id
is
7599 if D
= Union_Id
(Empty
) then
7602 elsif D
in Node_Range
then
7604 (Copy_Generic_Node
(Node_Id
(D
), New_N
, Instantiating
));
7606 elsif D
in List_Range
then
7607 return Union_Id
(Copy_Generic_List
(List_Id
(D
), New_N
));
7609 elsif D
in Elist_Range
then
7610 return Union_Id
(Copy_Generic_Elist
(Elist_Id
(D
)));
7612 -- Nothing else is copyable (e.g. Uint values), return as is
7617 end Copy_Generic_Descendant
;
7619 ------------------------
7620 -- Copy_Generic_Elist --
7621 ------------------------
7623 function Copy_Generic_Elist
(E
: Elist_Id
) return Elist_Id
is
7630 M
:= First_Elmt
(E
);
7631 while Present
(M
) loop
7633 (Copy_Generic_Node
(Node
(M
), Empty
, Instantiating
), L
);
7642 end Copy_Generic_Elist
;
7644 -----------------------
7645 -- Copy_Generic_List --
7646 -----------------------
7648 function Copy_Generic_List
7650 Parent_Id
: Node_Id
) return List_Id
7658 Set_Parent
(New_L
, Parent_Id
);
7661 while Present
(N
) loop
7662 Append
(Copy_Generic_Node
(N
, Empty
, Instantiating
), New_L
);
7671 end Copy_Generic_List
;
7673 ---------------------------
7674 -- In_Defining_Unit_Name --
7675 ---------------------------
7677 function In_Defining_Unit_Name
(Nam
: Node_Id
) return Boolean is
7680 Present
(Parent
(Nam
))
7681 and then (Nkind
(Parent
(Nam
)) = N_Defining_Program_Unit_Name
7683 (Nkind
(Parent
(Nam
)) = N_Expanded_Name
7684 and then In_Defining_Unit_Name
(Parent
(Nam
))));
7685 end In_Defining_Unit_Name
;
7687 -- Start of processing for Copy_Generic_Node
7694 New_N
:= New_Copy
(N
);
7696 -- Copy aspects if present
7698 if Has_Aspects
(N
) then
7699 Set_Has_Aspects
(New_N
, False);
7700 Set_Aspect_Specifications
7701 (New_N
, Copy_Generic_List
(Aspect_Specifications
(N
), Parent_Id
));
7704 -- If we are instantiating, we want to adjust the sloc based on the
7705 -- current S_Adjustment. However, if this is the root node of a subunit,
7706 -- we need to defer that adjustment to below (see "elsif Instantiating
7707 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
7708 -- computed the adjustment.
7711 and then not (Nkind
(N
) in N_Proper_Body
7712 and then Was_Originally_Stub
(N
))
7714 Adjust_Instantiation_Sloc
(New_N
, S_Adjustment
);
7717 if not Is_List_Member
(N
) then
7718 Set_Parent
(New_N
, Parent_Id
);
7721 -- Special casing for identifiers and other entity names and operators
7723 if Nkind_In
(New_N
, N_Character_Literal
,
7727 or else Nkind
(New_N
) in N_Op
7729 if not Instantiating
then
7731 -- Link both nodes in order to assign subsequently the entity of
7732 -- the copy to the original node, in case this is a global
7735 Set_Associated_Node
(N
, New_N
);
7737 -- If we are within an instantiation, this is a nested generic
7738 -- that has already been analyzed at the point of definition.
7739 -- We must preserve references that were global to the enclosing
7740 -- parent at that point. Other occurrences, whether global or
7741 -- local to the current generic, must be resolved anew, so we
7742 -- reset the entity in the generic copy. A global reference has a
7743 -- smaller depth than the parent, or else the same depth in case
7744 -- both are distinct compilation units.
7746 -- A child unit is implicitly declared within the enclosing parent
7747 -- but is in fact global to it, and must be preserved.
7749 -- It is also possible for Current_Instantiated_Parent to be
7750 -- defined, and for this not to be a nested generic, namely if
7751 -- the unit is loaded through Rtsfind. In that case, the entity of
7752 -- New_N is only a link to the associated node, and not a defining
7755 -- The entities for parent units in the defining_program_unit of a
7756 -- generic child unit are established when the context of the unit
7757 -- is first analyzed, before the generic copy is made. They are
7758 -- preserved in the copy for use in ASIS queries.
7760 Ent
:= Entity
(New_N
);
7762 if No
(Current_Instantiated_Parent
.Gen_Id
) then
7764 or else Nkind
(Ent
) /= N_Defining_Identifier
7765 or else not In_Defining_Unit_Name
(N
)
7767 Set_Associated_Node
(New_N
, Empty
);
7772 not Nkind_In
(Ent
, N_Defining_Identifier
,
7773 N_Defining_Character_Literal
,
7774 N_Defining_Operator_Symbol
)
7775 or else No
(Scope
(Ent
))
7777 (Scope
(Ent
) = Current_Instantiated_Parent
.Gen_Id
7778 and then not Is_Child_Unit
(Ent
))
7780 (Scope_Depth
(Scope
(Ent
)) >
7781 Scope_Depth
(Current_Instantiated_Parent
.Gen_Id
)
7783 Get_Source_Unit
(Ent
) =
7784 Get_Source_Unit
(Current_Instantiated_Parent
.Gen_Id
))
7786 Set_Associated_Node
(New_N
, Empty
);
7789 -- Case of instantiating identifier or some other name or operator
7792 -- If the associated node is still defined, the entity in it
7793 -- is global, and must be copied to the instance. If this copy
7794 -- is being made for a body to inline, it is applied to an
7795 -- instantiated tree, and the entity is already present and
7796 -- must be also preserved.
7799 Assoc
: constant Node_Id
:= Get_Associated_Node
(N
);
7802 if Present
(Assoc
) then
7803 if Nkind
(Assoc
) = Nkind
(N
) then
7804 Set_Entity
(New_N
, Entity
(Assoc
));
7805 Check_Private_View
(N
);
7807 -- The node is a reference to a global type and acts as the
7808 -- subtype mark of a qualified expression created in order
7809 -- to aid resolution of accidental overloading in instances.
7810 -- Since N is a reference to a type, the Associated_Node of
7811 -- N denotes an entity rather than another identifier. See
7812 -- Qualify_Universal_Operands for details.
7814 elsif Nkind
(N
) = N_Identifier
7815 and then Nkind
(Parent
(N
)) = N_Qualified_Expression
7816 and then Subtype_Mark
(Parent
(N
)) = N
7817 and then Is_Qualified_Universal_Literal
(Parent
(N
))
7819 Set_Entity
(New_N
, Assoc
);
7821 -- The name in the call may be a selected component if the
7822 -- call has not been analyzed yet, as may be the case for
7823 -- pre/post conditions in a generic unit.
7825 elsif Nkind
(Assoc
) = N_Function_Call
7826 and then Is_Entity_Name
(Name
(Assoc
))
7828 Set_Entity
(New_N
, Entity
(Name
(Assoc
)));
7830 elsif Nkind_In
(Assoc
, N_Defining_Identifier
,
7831 N_Defining_Character_Literal
,
7832 N_Defining_Operator_Symbol
)
7833 and then Expander_Active
7835 -- Inlining case: we are copying a tree that contains
7836 -- global entities, which are preserved in the copy to be
7837 -- used for subsequent inlining.
7842 Set_Entity
(New_N
, Empty
);
7848 -- For expanded name, we must copy the Prefix and Selector_Name
7850 if Nkind
(N
) = N_Expanded_Name
then
7852 (New_N
, Copy_Generic_Node
(Prefix
(N
), New_N
, Instantiating
));
7854 Set_Selector_Name
(New_N
,
7855 Copy_Generic_Node
(Selector_Name
(N
), New_N
, Instantiating
));
7857 -- For operators, copy the operands
7859 elsif Nkind
(N
) in N_Op
then
7860 if Nkind
(N
) in N_Binary_Op
then
7861 Set_Left_Opnd
(New_N
,
7862 Copy_Generic_Node
(Left_Opnd
(N
), New_N
, Instantiating
));
7865 Set_Right_Opnd
(New_N
,
7866 Copy_Generic_Node
(Right_Opnd
(N
), New_N
, Instantiating
));
7869 -- Establish a link between an entity from the generic template and the
7870 -- corresponding entity in the generic copy to be analyzed.
7872 elsif Nkind
(N
) in N_Entity
then
7873 if not Instantiating
then
7874 Set_Associated_Entity
(N
, New_N
);
7877 -- Clear any existing link the copy may inherit from the replicated
7878 -- generic template entity.
7880 Set_Associated_Entity
(New_N
, Empty
);
7882 -- Special casing for stubs
7884 elsif Nkind
(N
) in N_Body_Stub
then
7886 -- In any case, we must copy the specification or defining
7887 -- identifier as appropriate.
7889 if Nkind
(N
) = N_Subprogram_Body_Stub
then
7890 Set_Specification
(New_N
,
7891 Copy_Generic_Node
(Specification
(N
), New_N
, Instantiating
));
7894 Set_Defining_Identifier
(New_N
,
7896 (Defining_Identifier
(N
), New_N
, Instantiating
));
7899 -- If we are not instantiating, then this is where we load and
7900 -- analyze subunits, i.e. at the point where the stub occurs. A
7901 -- more permissive system might defer this analysis to the point
7902 -- of instantiation, but this seems too complicated for now.
7904 if not Instantiating
then
7906 Subunit_Name
: constant Unit_Name_Type
:= Get_Unit_Name
(N
);
7908 Unum
: Unit_Number_Type
;
7912 -- Make sure that, if it is a subunit of the main unit that is
7913 -- preprocessed and if -gnateG is specified, the preprocessed
7914 -- file will be written.
7916 Lib
.Analysing_Subunit_Of_Main
:=
7917 Lib
.In_Extended_Main_Source_Unit
(N
);
7920 (Load_Name
=> Subunit_Name
,
7924 Lib
.Analysing_Subunit_Of_Main
:= False;
7926 -- If the proper body is not found, a warning message will be
7927 -- emitted when analyzing the stub, or later at the point of
7928 -- instantiation. Here we just leave the stub as is.
7930 if Unum
= No_Unit
then
7931 Subunits_Missing
:= True;
7932 goto Subunit_Not_Found
;
7935 Subunit
:= Cunit
(Unum
);
7937 if Nkind
(Unit
(Subunit
)) /= N_Subunit
then
7939 ("found child unit instead of expected SEPARATE subunit",
7941 Error_Msg_Sloc
:= Sloc
(N
);
7942 Error_Msg_N
("\to complete stub #", Subunit
);
7943 goto Subunit_Not_Found
;
7946 -- We must create a generic copy of the subunit, in order to
7947 -- perform semantic analysis on it, and we must replace the
7948 -- stub in the original generic unit with the subunit, in order
7949 -- to preserve non-local references within.
7951 -- Only the proper body needs to be copied. Library_Unit and
7952 -- context clause are simply inherited by the generic copy.
7953 -- Note that the copy (which may be recursive if there are
7954 -- nested subunits) must be done first, before attaching it to
7955 -- the enclosing generic.
7959 (Proper_Body
(Unit
(Subunit
)),
7960 Empty
, Instantiating
=> False);
7962 -- Now place the original proper body in the original generic
7963 -- unit. This is a body, not a compilation unit.
7965 Rewrite
(N
, Proper_Body
(Unit
(Subunit
)));
7966 Set_Is_Compilation_Unit
(Defining_Entity
(N
), False);
7967 Set_Was_Originally_Stub
(N
);
7969 -- Finally replace the body of the subunit with its copy, and
7970 -- make this new subunit into the library unit of the generic
7971 -- copy, which does not have stubs any longer.
7973 Set_Proper_Body
(Unit
(Subunit
), New_Body
);
7974 Set_Library_Unit
(New_N
, Subunit
);
7975 Inherit_Context
(Unit
(Subunit
), N
);
7978 -- If we are instantiating, this must be an error case, since
7979 -- otherwise we would have replaced the stub node by the proper body
7980 -- that corresponds. So just ignore it in the copy (i.e. we have
7981 -- copied it, and that is good enough).
7987 <<Subunit_Not_Found
>> null;
7989 -- If the node is a compilation unit, it is the subunit of a stub, which
7990 -- has been loaded already (see code below). In this case, the library
7991 -- unit field of N points to the parent unit (which is a compilation
7992 -- unit) and need not (and cannot) be copied.
7994 -- When the proper body of the stub is analyzed, the library_unit link
7995 -- is used to establish the proper context (see sem_ch10).
7997 -- The other fields of a compilation unit are copied as usual
7999 elsif Nkind
(N
) = N_Compilation_Unit
then
8001 -- This code can only be executed when not instantiating, because in
8002 -- the copy made for an instantiation, the compilation unit node has
8003 -- disappeared at the point that a stub is replaced by its proper
8006 pragma Assert
(not Instantiating
);
8008 Set_Context_Items
(New_N
,
8009 Copy_Generic_List
(Context_Items
(N
), New_N
));
8012 Copy_Generic_Node
(Unit
(N
), New_N
, Instantiating
=> False));
8014 Set_First_Inlined_Subprogram
(New_N
,
8016 (First_Inlined_Subprogram
(N
), New_N
, Instantiating
=> False));
8021 (Aux_Decls_Node
(N
), New_N
, Instantiating
=> False));
8023 -- For an assignment node, the assignment is known to be semantically
8024 -- legal if we are instantiating the template. This avoids incorrect
8025 -- diagnostics in generated code.
8027 elsif Nkind
(N
) = N_Assignment_Statement
then
8029 -- Copy name and expression fields in usual manner
8032 Copy_Generic_Node
(Name
(N
), New_N
, Instantiating
));
8034 Set_Expression
(New_N
,
8035 Copy_Generic_Node
(Expression
(N
), New_N
, Instantiating
));
8037 if Instantiating
then
8038 Set_Assignment_OK
(Name
(New_N
), True);
8041 elsif Nkind_In
(N
, N_Aggregate
, N_Extension_Aggregate
) then
8042 if not Instantiating
then
8043 Set_Associated_Node
(N
, New_N
);
8046 if Present
(Get_Associated_Node
(N
))
8047 and then Nkind
(Get_Associated_Node
(N
)) = Nkind
(N
)
8049 -- In the generic the aggregate has some composite type. If at
8050 -- the point of instantiation the type has a private view,
8051 -- install the full view (and that of its ancestors, if any).
8054 T
: Entity_Id
:= (Etype
(Get_Associated_Node
(New_N
)));
8058 if Present
(T
) and then Is_Private_Type
(T
) then
8063 and then Is_Tagged_Type
(T
)
8064 and then Is_Derived_Type
(T
)
8066 Rt
:= Root_Type
(T
);
8071 if Is_Private_Type
(T
) then
8082 -- Do not copy the associated node, which points to the generic copy
8083 -- of the aggregate.
8086 use Atree
.Unchecked_Access
;
8087 -- This code section is part of the implementation of an untyped
8088 -- tree traversal, so it needs direct access to node fields.
8091 Set_Field1
(New_N
, Copy_Generic_Descendant
(Field1
(N
)));
8092 Set_Field2
(New_N
, Copy_Generic_Descendant
(Field2
(N
)));
8093 Set_Field3
(New_N
, Copy_Generic_Descendant
(Field3
(N
)));
8094 Set_Field5
(New_N
, Copy_Generic_Descendant
(Field5
(N
)));
8097 -- Allocators do not have an identifier denoting the access type, so we
8098 -- must locate it through the expression to check whether the views are
8101 elsif Nkind
(N
) = N_Allocator
8102 and then Nkind
(Expression
(N
)) = N_Qualified_Expression
8103 and then Is_Entity_Name
(Subtype_Mark
(Expression
(N
)))
8104 and then Instantiating
8107 T
: constant Node_Id
:=
8108 Get_Associated_Node
(Subtype_Mark
(Expression
(N
)));
8114 -- Retrieve the allocator node in the generic copy
8116 Acc_T
:= Etype
(Parent
(Parent
(T
)));
8118 if Present
(Acc_T
) and then Is_Private_Type
(Acc_T
) then
8119 Switch_View
(Acc_T
);
8126 -- For a proper body, we must catch the case of a proper body that
8127 -- replaces a stub. This represents the point at which a separate
8128 -- compilation unit, and hence template file, may be referenced, so we
8129 -- must make a new source instantiation entry for the template of the
8130 -- subunit, and ensure that all nodes in the subunit are adjusted using
8131 -- this new source instantiation entry.
8133 elsif Nkind
(N
) in N_Proper_Body
then
8135 Save_Adjustment
: constant Sloc_Adjustment
:= S_Adjustment
;
8137 if Instantiating
and then Was_Originally_Stub
(N
) then
8138 Create_Instantiation_Source
8139 (Instantiation_Node
,
8140 Defining_Entity
(N
),
8143 Adjust_Instantiation_Sloc
(New_N
, S_Adjustment
);
8146 -- Now copy the fields of the proper body, using the new
8147 -- adjustment factor if one was needed as per test above.
8151 -- Restore the original adjustment factor
8153 S_Adjustment
:= Save_Adjustment
;
8156 elsif Nkind
(N
) = N_Pragma
and then Instantiating
then
8158 -- Do not copy Comment or Ident pragmas their content is relevant to
8159 -- the generic unit, not to the instantiating unit.
8161 if Nam_In
(Pragma_Name_Unmapped
(N
), Name_Comment
, Name_Ident
) then
8162 New_N
:= Make_Null_Statement
(Sloc
(N
));
8164 -- Do not copy pragmas generated from aspects because the pragmas do
8165 -- not carry any semantic information, plus they will be regenerated
8168 -- However, generating C we need to copy them since postconditions
8169 -- are inlined by the front end, and the front-end inlining machinery
8170 -- relies on this routine to perform inlining.
8172 elsif From_Aspect_Specification
(N
)
8173 and then not Modify_Tree_For_C
8175 New_N
:= Make_Null_Statement
(Sloc
(N
));
8181 elsif Nkind_In
(N
, N_Integer_Literal
, N_Real_Literal
) then
8183 -- No descendant fields need traversing
8187 elsif Nkind
(N
) = N_String_Literal
8188 and then Present
(Etype
(N
))
8189 and then Instantiating
8191 -- If the string is declared in an outer scope, the string_literal
8192 -- subtype created for it may have the wrong scope. Force reanalysis
8193 -- of the constant to generate a new itype in the proper context.
8195 Set_Etype
(New_N
, Empty
);
8196 Set_Analyzed
(New_N
, False);
8198 -- For the remaining nodes, copy their descendants recursively
8203 if Instantiating
and then Nkind
(N
) = N_Subprogram_Body
then
8204 Set_Generic_Parent
(Specification
(New_N
), N
);
8206 -- Should preserve Corresponding_Spec??? (12.3(14))
8210 -- Propagate dimensions if present, so that they are reflected in the
8213 if Nkind
(N
) in N_Has_Etype
8214 and then (Nkind
(N
) in N_Op
or else Is_Entity_Name
(N
))
8215 and then Present
(Etype
(N
))
8216 and then Is_Floating_Point_Type
(Etype
(N
))
8217 and then Has_Dimension_System
(Etype
(N
))
8219 Copy_Dimensions
(N
, New_N
);
8223 end Copy_Generic_Node
;
8225 ----------------------------
8226 -- Denotes_Formal_Package --
8227 ----------------------------
8229 function Denotes_Formal_Package
8231 On_Exit
: Boolean := False;
8232 Instance
: Entity_Id
:= Empty
) return Boolean
8235 Scop
: constant Entity_Id
:= Scope
(Pack
);
8238 function Is_Actual_Of_Previous_Formal
(P
: Entity_Id
) return Boolean;
8239 -- The package in question may be an actual for a previous formal
8240 -- package P of the current instance, so examine its actuals as well.
8241 -- This must be recursive over other formal packages.
8243 ----------------------------------
8244 -- Is_Actual_Of_Previous_Formal --
8245 ----------------------------------
8247 function Is_Actual_Of_Previous_Formal
(P
: Entity_Id
) return Boolean is
8251 E1
:= First_Entity
(P
);
8252 while Present
(E1
) and then E1
/= Instance
loop
8253 if Ekind
(E1
) = E_Package
8254 and then Nkind
(Parent
(E1
)) = N_Package_Renaming_Declaration
8256 if Renamed_Object
(E1
) = Pack
then
8259 elsif E1
= P
or else Renamed_Object
(E1
) = P
then
8262 elsif Is_Actual_Of_Previous_Formal
(E1
) then
8271 end Is_Actual_Of_Previous_Formal
;
8273 -- Start of processing for Denotes_Formal_Package
8279 (Instance_Envs
.Last
).Instantiated_Parent
.Act_Id
;
8281 Par
:= Current_Instantiated_Parent
.Act_Id
;
8284 if Ekind
(Scop
) = E_Generic_Package
8285 or else Nkind
(Unit_Declaration_Node
(Scop
)) =
8286 N_Generic_Subprogram_Declaration
8290 elsif Nkind
(Original_Node
(Unit_Declaration_Node
(Pack
))) =
8291 N_Formal_Package_Declaration
8299 -- Check whether this package is associated with a formal package of
8300 -- the enclosing instantiation. Iterate over the list of renamings.
8302 E
:= First_Entity
(Par
);
8303 while Present
(E
) loop
8304 if Ekind
(E
) /= E_Package
8305 or else Nkind
(Parent
(E
)) /= N_Package_Renaming_Declaration
8309 elsif Renamed_Object
(E
) = Par
then
8312 elsif Renamed_Object
(E
) = Pack
then
8315 elsif Is_Actual_Of_Previous_Formal
(E
) then
8325 end Denotes_Formal_Package
;
8331 procedure End_Generic
is
8333 -- ??? More things could be factored out in this routine. Should
8334 -- probably be done at a later stage.
8336 Inside_A_Generic
:= Generic_Flags
.Table
(Generic_Flags
.Last
);
8337 Generic_Flags
.Decrement_Last
;
8339 Expander_Mode_Restore
;
8346 function Earlier
(N1
, N2
: Node_Id
) return Boolean is
8347 procedure Find_Depth
(P
: in out Node_Id
; D
: in out Integer);
8348 -- Find distance from given node to enclosing compilation unit
8354 procedure Find_Depth
(P
: in out Node_Id
; D
: in out Integer) is
8357 and then Nkind
(P
) /= N_Compilation_Unit
8359 P
:= True_Parent
(P
);
8364 -- Local declarations
8373 -- Start of processing for Earlier
8376 Find_Depth
(P1
, D1
);
8377 Find_Depth
(P2
, D2
);
8387 P1
:= True_Parent
(P1
);
8392 P2
:= True_Parent
(P2
);
8396 -- At this point P1 and P2 are at the same distance from the root.
8397 -- We examine their parents until we find a common declarative list.
8398 -- If we reach the root, N1 and N2 do not descend from the same
8399 -- declarative list (e.g. one is nested in the declarative part and
8400 -- the other is in a block in the statement part) and the earlier
8401 -- one is already frozen.
8403 while not Is_List_Member
(P1
)
8404 or else not Is_List_Member
(P2
)
8405 or else List_Containing
(P1
) /= List_Containing
(P2
)
8407 P1
:= True_Parent
(P1
);
8408 P2
:= True_Parent
(P2
);
8410 if Nkind
(Parent
(P1
)) = N_Subunit
then
8411 P1
:= Corresponding_Stub
(Parent
(P1
));
8414 if Nkind
(Parent
(P2
)) = N_Subunit
then
8415 P2
:= Corresponding_Stub
(Parent
(P2
));
8423 -- Expanded code usually shares the source location of the original
8424 -- construct it was generated for. This however may not necessarily
8425 -- reflect the true location of the code within the tree.
8427 -- Before comparing the slocs of the two nodes, make sure that we are
8428 -- working with correct source locations. Assume that P1 is to the left
8429 -- of P2. If either one does not come from source, traverse the common
8430 -- list heading towards the other node and locate the first source
8434 -- ----+===+===+--------------+===+===+----
8435 -- expanded code expanded code
8437 if not Comes_From_Source
(P1
) then
8438 while Present
(P1
) loop
8440 -- Neither P2 nor a source statement were located during the
8441 -- search. If we reach the end of the list, then P1 does not
8442 -- occur earlier than P2.
8445 -- start --- P2 ----- P1 --- end
8447 if No
(Next
(P1
)) then
8450 -- We encounter P2 while going to the right of the list. This
8451 -- means that P1 does indeed appear earlier.
8454 -- start --- P1 ===== P2 --- end
8455 -- expanded code in between
8460 -- No need to look any further since we have located a source
8463 elsif Comes_From_Source
(P1
) then
8473 if not Comes_From_Source
(P2
) then
8474 while Present
(P2
) loop
8476 -- Neither P1 nor a source statement were located during the
8477 -- search. If we reach the start of the list, then P1 does not
8478 -- occur earlier than P2.
8481 -- start --- P2 --- P1 --- end
8483 if No
(Prev
(P2
)) then
8486 -- We encounter P1 while going to the left of the list. This
8487 -- means that P1 does indeed appear earlier.
8490 -- start --- P1 ===== P2 --- end
8491 -- expanded code in between
8496 -- No need to look any further since we have located a source
8499 elsif Comes_From_Source
(P2
) then
8509 -- At this point either both nodes came from source or we approximated
8510 -- their source locations through neighboring source statements.
8512 T1
:= Top_Level_Location
(Sloc
(P1
));
8513 T2
:= Top_Level_Location
(Sloc
(P2
));
8515 -- When two nodes come from the same instance, they have identical top
8516 -- level locations. To determine proper relation within the tree, check
8517 -- their locations within the template.
8520 return Sloc
(P1
) < Sloc
(P2
);
8522 -- The two nodes either come from unrelated instances or do not come
8523 -- from instantiated code at all.
8530 ----------------------
8531 -- Find_Actual_Type --
8532 ----------------------
8534 function Find_Actual_Type
8536 Gen_Type
: Entity_Id
) return Entity_Id
8538 Gen_Scope
: constant Entity_Id
:= Scope
(Gen_Type
);
8542 -- Special processing only applies to child units
8544 if not Is_Child_Unit
(Gen_Scope
) then
8545 return Get_Instance_Of
(Typ
);
8547 -- If designated or component type is itself a formal of the child unit,
8548 -- its instance is available.
8550 elsif Scope
(Typ
) = Gen_Scope
then
8551 return Get_Instance_Of
(Typ
);
8553 -- If the array or access type is not declared in the parent unit,
8554 -- no special processing needed.
8556 elsif not Is_Generic_Type
(Typ
)
8557 and then Scope
(Gen_Scope
) /= Scope
(Typ
)
8559 return Get_Instance_Of
(Typ
);
8561 -- Otherwise, retrieve designated or component type by visibility
8564 T
:= Current_Entity
(Typ
);
8565 while Present
(T
) loop
8566 if In_Open_Scopes
(Scope
(T
)) then
8568 elsif Is_Generic_Actual_Type
(T
) then
8577 end Find_Actual_Type
;
8579 ----------------------------
8580 -- Freeze_Subprogram_Body --
8581 ----------------------------
8583 procedure Freeze_Subprogram_Body
8584 (Inst_Node
: Node_Id
;
8586 Pack_Id
: Entity_Id
)
8588 Gen_Unit
: constant Entity_Id
:= Get_Generic_Entity
(Inst_Node
);
8589 Par
: constant Entity_Id
:= Scope
(Gen_Unit
);
8595 function Enclosing_Package_Body
(N
: Node_Id
) return Node_Id
;
8596 -- Find innermost package body that encloses the given node, and which
8597 -- is not a compilation unit. Freeze nodes for the instance, or for its
8598 -- enclosing body, may be inserted after the enclosing_body of the
8599 -- generic unit. Used to determine proper placement of freeze node for
8600 -- both package and subprogram instances.
8602 function Package_Freeze_Node
(B
: Node_Id
) return Node_Id
;
8603 -- Find entity for given package body, and locate or create a freeze
8606 ----------------------------
8607 -- Enclosing_Package_Body --
8608 ----------------------------
8610 function Enclosing_Package_Body
(N
: Node_Id
) return Node_Id
is
8616 and then Nkind
(Parent
(P
)) /= N_Compilation_Unit
8618 if Nkind
(P
) = N_Package_Body
then
8619 if Nkind
(Parent
(P
)) = N_Subunit
then
8620 return Corresponding_Stub
(Parent
(P
));
8626 P
:= True_Parent
(P
);
8630 end Enclosing_Package_Body
;
8632 -------------------------
8633 -- Package_Freeze_Node --
8634 -------------------------
8636 function Package_Freeze_Node
(B
: Node_Id
) return Node_Id
is
8640 if Nkind
(B
) = N_Package_Body
then
8641 Id
:= Corresponding_Spec
(B
);
8642 else pragma Assert
(Nkind
(B
) = N_Package_Body_Stub
);
8643 Id
:= Corresponding_Spec
(Proper_Body
(Unit
(Library_Unit
(B
))));
8646 Ensure_Freeze_Node
(Id
);
8647 return Freeze_Node
(Id
);
8648 end Package_Freeze_Node
;
8650 -- Start of processing for Freeze_Subprogram_Body
8653 -- If the instance and the generic body appear within the same unit, and
8654 -- the instance precedes the generic, the freeze node for the instance
8655 -- must appear after that of the generic. If the generic is nested
8656 -- within another instance I2, then current instance must be frozen
8657 -- after I2. In both cases, the freeze nodes are those of enclosing
8658 -- packages. Otherwise, the freeze node is placed at the end of the
8659 -- current declarative part.
8661 Enc_G
:= Enclosing_Package_Body
(Gen_Body
);
8662 Enc_I
:= Enclosing_Package_Body
(Inst_Node
);
8663 Ensure_Freeze_Node
(Pack_Id
);
8664 F_Node
:= Freeze_Node
(Pack_Id
);
8666 if Is_Generic_Instance
(Par
)
8667 and then Present
(Freeze_Node
(Par
))
8668 and then In_Same_Declarative_Part
(Freeze_Node
(Par
), Inst_Node
)
8670 -- The parent was a premature instantiation. Insert freeze node at
8671 -- the end the current declarative part.
8673 if Is_Known_Guaranteed_ABE
(Get_Unit_Instantiation_Node
(Par
)) then
8674 Insert_Freeze_Node_For_Instance
(Inst_Node
, F_Node
);
8676 -- Handle the following case:
8678 -- package Parent_Inst is new ...
8681 -- procedure P ... -- this body freezes Parent_Inst
8683 -- package Inst is new ...
8685 -- In this particular scenario, the freeze node for Inst must be
8686 -- inserted in the same manner as that of Parent_Inst - before the
8687 -- next source body or at the end of the declarative list (body not
8688 -- available). If body P did not exist and Parent_Inst was frozen
8689 -- after Inst, either by a body following Inst or at the end of the
8690 -- declarative region, the freeze node for Inst must be inserted
8691 -- after that of Parent_Inst. This relation is established by
8692 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8694 elsif List_Containing
(Get_Unit_Instantiation_Node
(Par
)) =
8695 List_Containing
(Inst_Node
)
8696 and then Sloc
(Freeze_Node
(Par
)) < Sloc
(Inst_Node
)
8698 Insert_Freeze_Node_For_Instance
(Inst_Node
, F_Node
);
8701 Insert_After
(Freeze_Node
(Par
), F_Node
);
8704 -- The body enclosing the instance should be frozen after the body that
8705 -- includes the generic, because the body of the instance may make
8706 -- references to entities therein. If the two are not in the same
8707 -- declarative part, or if the one enclosing the instance is frozen
8708 -- already, freeze the instance at the end of the current declarative
8711 elsif Is_Generic_Instance
(Par
)
8712 and then Present
(Freeze_Node
(Par
))
8713 and then Present
(Enc_I
)
8715 if In_Same_Declarative_Part
(Freeze_Node
(Par
), Enc_I
)
8717 (Nkind
(Enc_I
) = N_Package_Body
8719 In_Same_Declarative_Part
(Freeze_Node
(Par
), Parent
(Enc_I
)))
8721 -- The enclosing package may contain several instances. Rather
8722 -- than computing the earliest point at which to insert its freeze
8723 -- node, we place it at the end of the declarative part of the
8724 -- parent of the generic.
8726 Insert_Freeze_Node_For_Instance
8727 (Freeze_Node
(Par
), Package_Freeze_Node
(Enc_I
));
8730 Insert_Freeze_Node_For_Instance
(Inst_Node
, F_Node
);
8732 elsif Present
(Enc_G
)
8733 and then Present
(Enc_I
)
8734 and then Enc_G
/= Enc_I
8735 and then Earlier
(Inst_Node
, Gen_Body
)
8737 if Nkind
(Enc_G
) = N_Package_Body
then
8739 Corresponding_Spec
(Enc_G
);
8740 else pragma Assert
(Nkind
(Enc_G
) = N_Package_Body_Stub
);
8742 Corresponding_Spec
(Proper_Body
(Unit
(Library_Unit
(Enc_G
))));
8745 -- Freeze package that encloses instance, and place node after the
8746 -- package that encloses generic. If enclosing package is already
8747 -- frozen we have to assume it is at the proper place. This may be a
8748 -- potential ABE that requires dynamic checking. Do not add a freeze
8749 -- node if the package that encloses the generic is inside the body
8750 -- that encloses the instance, because the freeze node would be in
8751 -- the wrong scope. Additional contortions needed if the bodies are
8752 -- within a subunit.
8755 Enclosing_Body
: Node_Id
;
8758 if Nkind
(Enc_I
) = N_Package_Body_Stub
then
8759 Enclosing_Body
:= Proper_Body
(Unit
(Library_Unit
(Enc_I
)));
8761 Enclosing_Body
:= Enc_I
;
8764 if Parent
(List_Containing
(Enc_G
)) /= Enclosing_Body
then
8765 Insert_Freeze_Node_For_Instance
8766 (Enc_G
, Package_Freeze_Node
(Enc_I
));
8770 -- Freeze enclosing subunit before instance
8772 Ensure_Freeze_Node
(E_G_Id
);
8774 if not Is_List_Member
(Freeze_Node
(E_G_Id
)) then
8775 Insert_After
(Enc_G
, Freeze_Node
(E_G_Id
));
8778 Insert_Freeze_Node_For_Instance
(Inst_Node
, F_Node
);
8781 -- If none of the above, insert freeze node at the end of the current
8782 -- declarative part.
8784 Insert_Freeze_Node_For_Instance
(Inst_Node
, F_Node
);
8786 end Freeze_Subprogram_Body
;
8792 function Get_Gen_Id
(E
: Assoc_Ptr
) return Entity_Id
is
8794 return Generic_Renamings
.Table
(E
).Gen_Id
;
8797 ---------------------
8798 -- Get_Instance_Of --
8799 ---------------------
8801 function Get_Instance_Of
(A
: Entity_Id
) return Entity_Id
is
8802 Res
: constant Assoc_Ptr
:= Generic_Renamings_HTable
.Get
(A
);
8805 if Res
/= Assoc_Null
then
8806 return Generic_Renamings
.Table
(Res
).Act_Id
;
8809 -- On exit, entity is not instantiated: not a generic parameter, or
8810 -- else parameter of an inner generic unit.
8814 end Get_Instance_Of
;
8816 ---------------------------------
8817 -- Get_Unit_Instantiation_Node --
8818 ---------------------------------
8820 function Get_Unit_Instantiation_Node
(A
: Entity_Id
) return Node_Id
is
8821 Decl
: Node_Id
:= Unit_Declaration_Node
(A
);
8825 -- If the Package_Instantiation attribute has been set on the package
8826 -- entity, then use it directly when it (or its Original_Node) refers
8827 -- to an N_Package_Instantiation node. In principle it should be
8828 -- possible to have this field set in all cases, which should be
8829 -- investigated, and would allow this function to be significantly
8832 Inst
:= Package_Instantiation
(A
);
8834 if Present
(Inst
) then
8835 if Nkind
(Inst
) = N_Package_Instantiation
then
8838 elsif Nkind
(Original_Node
(Inst
)) = N_Package_Instantiation
then
8839 return Original_Node
(Inst
);
8843 -- If the instantiation is a compilation unit that does not need body
8844 -- then the instantiation node has been rewritten as a package
8845 -- declaration for the instance, and we return the original node.
8847 -- If it is a compilation unit and the instance node has not been
8848 -- rewritten, then it is still the unit of the compilation. Finally, if
8849 -- a body is present, this is a parent of the main unit whose body has
8850 -- been compiled for inlining purposes, and the instantiation node has
8851 -- been rewritten with the instance body.
8853 -- Otherwise the instantiation node appears after the declaration. If
8854 -- the entity is a formal package, the declaration may have been
8855 -- rewritten as a generic declaration (in the case of a formal with box)
8856 -- or left as a formal package declaration if it has actuals, and is
8857 -- found with a forward search.
8859 if Nkind
(Parent
(Decl
)) = N_Compilation_Unit
then
8860 if Nkind
(Decl
) = N_Package_Declaration
8861 and then Present
(Corresponding_Body
(Decl
))
8863 Decl
:= Unit_Declaration_Node
(Corresponding_Body
(Decl
));
8866 if Nkind_In
(Original_Node
(Decl
), N_Function_Instantiation
,
8867 N_Package_Instantiation
,
8868 N_Procedure_Instantiation
)
8870 return Original_Node
(Decl
);
8872 return Unit
(Parent
(Decl
));
8875 elsif Nkind
(Decl
) = N_Package_Declaration
8876 and then Nkind
(Original_Node
(Decl
)) = N_Formal_Package_Declaration
8878 return Original_Node
(Decl
);
8881 Inst
:= Next
(Decl
);
8882 while not Nkind_In
(Inst
, N_Formal_Package_Declaration
,
8883 N_Function_Instantiation
,
8884 N_Package_Instantiation
,
8885 N_Procedure_Instantiation
)
8892 end Get_Unit_Instantiation_Node
;
8894 ------------------------
8895 -- Has_Been_Exchanged --
8896 ------------------------
8898 function Has_Been_Exchanged
(E
: Entity_Id
) return Boolean is
8902 Next
:= First_Elmt
(Exchanged_Views
);
8903 while Present
(Next
) loop
8904 if Full_View
(Node
(Next
)) = E
then
8912 end Has_Been_Exchanged
;
8918 function Hash
(F
: Entity_Id
) return HTable_Range
is
8920 return HTable_Range
(F
mod HTable_Size
);
8923 ------------------------
8924 -- Hide_Current_Scope --
8925 ------------------------
8927 procedure Hide_Current_Scope
is
8928 C
: constant Entity_Id
:= Current_Scope
;
8932 Set_Is_Hidden_Open_Scope
(C
);
8934 E
:= First_Entity
(C
);
8935 while Present
(E
) loop
8936 if Is_Immediately_Visible
(E
) then
8937 Set_Is_Immediately_Visible
(E
, False);
8938 Append_Elmt
(E
, Hidden_Entities
);
8944 -- Make the scope name invisible as well. This is necessary, but might
8945 -- conflict with calls to Rtsfind later on, in case the scope is a
8946 -- predefined one. There is no clean solution to this problem, so for
8947 -- now we depend on the user not redefining Standard itself in one of
8948 -- the parent units.
8950 if Is_Immediately_Visible
(C
) and then C
/= Standard_Standard
then
8951 Set_Is_Immediately_Visible
(C
, False);
8952 Append_Elmt
(C
, Hidden_Entities
);
8955 end Hide_Current_Scope
;
8961 procedure Init_Env
is
8962 Saved
: Instance_Env
;
8965 Saved
.Instantiated_Parent
:= Current_Instantiated_Parent
;
8966 Saved
.Exchanged_Views
:= Exchanged_Views
;
8967 Saved
.Hidden_Entities
:= Hidden_Entities
;
8968 Saved
.Current_Sem_Unit
:= Current_Sem_Unit
;
8969 Saved
.Parent_Unit_Visible
:= Parent_Unit_Visible
;
8970 Saved
.Instance_Parent_Unit
:= Instance_Parent_Unit
;
8972 -- Save configuration switches. These may be reset if the unit is a
8973 -- predefined unit, and the current mode is not Ada 2005.
8975 Save_Opt_Config_Switches
(Saved
.Switches
);
8977 Instance_Envs
.Append
(Saved
);
8979 Exchanged_Views
:= New_Elmt_List
;
8980 Hidden_Entities
:= New_Elmt_List
;
8982 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8983 -- this is set properly in Set_Instance_Env.
8985 Current_Instantiated_Parent
:=
8986 (Current_Scope
, Current_Scope
, Assoc_Null
);
8989 ------------------------------
8990 -- In_Same_Declarative_Part --
8991 ------------------------------
8993 function In_Same_Declarative_Part
8995 Inst
: Node_Id
) return Boolean
8997 Decls
: constant Node_Id
:= Parent
(F_Node
);
9001 Nod
:= Parent
(Inst
);
9002 while Present
(Nod
) loop
9006 elsif Nkind_In
(Nod
, N_Subprogram_Body
,
9008 N_Package_Declaration
,
9015 elsif Nkind
(Nod
) = N_Subunit
then
9016 Nod
:= Corresponding_Stub
(Nod
);
9018 elsif Nkind
(Nod
) = N_Compilation_Unit
then
9022 Nod
:= Parent
(Nod
);
9027 end In_Same_Declarative_Part
;
9029 ---------------------
9030 -- In_Main_Context --
9031 ---------------------
9033 function In_Main_Context
(E
: Entity_Id
) return Boolean is
9039 if not Is_Compilation_Unit
(E
)
9040 or else Ekind
(E
) /= E_Package
9041 or else In_Private_Part
(E
)
9046 Context
:= Context_Items
(Cunit
(Main_Unit
));
9048 Clause
:= First
(Context
);
9049 while Present
(Clause
) loop
9050 if Nkind
(Clause
) = N_With_Clause
then
9051 Nam
:= Name
(Clause
);
9053 -- If the current scope is part of the context of the main unit,
9054 -- analysis of the corresponding with_clause is not complete, and
9055 -- the entity is not set. We use the Chars field directly, which
9056 -- might produce false positives in rare cases, but guarantees
9057 -- that we produce all the instance bodies we will need.
9059 if (Is_Entity_Name
(Nam
) and then Chars
(Nam
) = Chars
(E
))
9060 or else (Nkind
(Nam
) = N_Selected_Component
9061 and then Chars
(Selector_Name
(Nam
)) = Chars
(E
))
9071 end In_Main_Context
;
9073 ---------------------
9074 -- Inherit_Context --
9075 ---------------------
9077 procedure Inherit_Context
(Gen_Decl
: Node_Id
; Inst
: Node_Id
) is
9078 Current_Context
: List_Id
;
9079 Current_Unit
: Node_Id
;
9088 if Nkind
(Parent
(Gen_Decl
)) = N_Compilation_Unit
then
9090 -- The inherited context is attached to the enclosing compilation
9091 -- unit. This is either the main unit, or the declaration for the
9092 -- main unit (in case the instantiation appears within the package
9093 -- declaration and the main unit is its body).
9095 Current_Unit
:= Parent
(Inst
);
9096 while Present
(Current_Unit
)
9097 and then Nkind
(Current_Unit
) /= N_Compilation_Unit
9099 Current_Unit
:= Parent
(Current_Unit
);
9102 Current_Context
:= Context_Items
(Current_Unit
);
9104 Item
:= First
(Context_Items
(Parent
(Gen_Decl
)));
9105 while Present
(Item
) loop
9106 if Nkind
(Item
) = N_With_Clause
then
9107 Lib_Unit
:= Library_Unit
(Item
);
9109 -- Take care to prevent direct cyclic with's
9111 if Lib_Unit
/= Current_Unit
then
9113 -- Do not add a unit if it is already in the context
9115 Clause
:= First
(Current_Context
);
9117 while Present
(Clause
) loop
9118 if Nkind
(Clause
) = N_With_Clause
9119 and then Library_Unit
(Clause
) = Lib_Unit
9129 New_I
:= New_Copy
(Item
);
9130 Set_Implicit_With
(New_I
);
9132 Append
(New_I
, Current_Context
);
9140 end Inherit_Context
;
9146 procedure Initialize
is
9148 Generic_Renamings
.Init
;
9151 Generic_Renamings_HTable
.Reset
;
9152 Circularity_Detected
:= False;
9153 Exchanged_Views
:= No_Elist
;
9154 Hidden_Entities
:= No_Elist
;
9157 -------------------------------------
9158 -- Insert_Freeze_Node_For_Instance --
9159 -------------------------------------
9161 procedure Insert_Freeze_Node_For_Instance
9170 function Enclosing_Body
(N
: Node_Id
) return Node_Id
;
9171 -- Find enclosing package or subprogram body, if any. Freeze node may
9172 -- be placed at end of current declarative list if previous instance
9173 -- and current one have different enclosing bodies.
9175 function Previous_Instance
(Gen
: Entity_Id
) return Entity_Id
;
9176 -- Find the local instance, if any, that declares the generic that is
9177 -- being instantiated. If present, the freeze node for this instance
9178 -- must follow the freeze node for the previous instance.
9180 --------------------
9181 -- Enclosing_Body --
9182 --------------------
9184 function Enclosing_Body
(N
: Node_Id
) return Node_Id
is
9190 and then Nkind
(Parent
(P
)) /= N_Compilation_Unit
9192 if Nkind_In
(P
, N_Package_Body
, N_Subprogram_Body
) then
9193 if Nkind
(Parent
(P
)) = N_Subunit
then
9194 return Corresponding_Stub
(Parent
(P
));
9200 P
:= True_Parent
(P
);
9206 -----------------------
9207 -- Previous_Instance --
9208 -----------------------
9210 function Previous_Instance
(Gen
: Entity_Id
) return Entity_Id
is
9215 while Present
(S
) and then S
/= Standard_Standard
loop
9216 if Is_Generic_Instance
(S
)
9217 and then In_Same_Source_Unit
(S
, N
)
9226 end Previous_Instance
;
9228 -- Start of processing for Insert_Freeze_Node_For_Instance
9231 if not Is_List_Member
(F_Node
) then
9233 Decls
:= List_Containing
(N
);
9234 Inst
:= Entity
(F_Node
);
9235 Par_N
:= Parent
(Decls
);
9237 -- When processing a subprogram instantiation, utilize the actual
9238 -- subprogram instantiation rather than its package wrapper as it
9239 -- carries all the context information.
9241 if Is_Wrapper_Package
(Inst
) then
9242 Inst
:= Related_Instance
(Inst
);
9245 -- If this is a package instance, check whether the generic is
9246 -- declared in a previous instance and the current instance is
9247 -- not within the previous one.
9249 if Present
(Generic_Parent
(Parent
(Inst
)))
9250 and then Is_In_Main_Unit
(N
)
9253 Enclosing_N
: constant Node_Id
:= Enclosing_Body
(N
);
9254 Par_I
: constant Entity_Id
:=
9256 (Generic_Parent
(Parent
(Inst
)));
9261 and then Earlier
(N
, Freeze_Node
(Par_I
))
9263 Scop
:= Scope
(Inst
);
9265 -- If the current instance is within the one that contains
9266 -- the generic, the freeze node for the current one must
9267 -- appear in the current declarative part. Ditto, if the
9268 -- current instance is within another package instance or
9269 -- within a body that does not enclose the current instance.
9270 -- In these three cases the freeze node of the previous
9271 -- instance is not relevant.
9273 while Present
(Scop
) and then Scop
/= Standard_Standard
loop
9274 exit when Scop
= Par_I
9276 (Is_Generic_Instance
(Scop
)
9277 and then Scope_Depth
(Scop
) > Scope_Depth
(Par_I
));
9278 Scop
:= Scope
(Scop
);
9281 -- Previous instance encloses current instance
9283 if Scop
= Par_I
then
9286 -- If the next node is a source body we must freeze in
9287 -- the current scope as well.
9289 elsif Present
(Next
(N
))
9290 and then Nkind_In
(Next
(N
), N_Subprogram_Body
,
9292 and then Comes_From_Source
(Next
(N
))
9296 -- Current instance is within an unrelated instance
9298 elsif Is_Generic_Instance
(Scop
) then
9301 -- Current instance is within an unrelated body
9303 elsif Present
(Enclosing_N
)
9304 and then Enclosing_N
/= Enclosing_Body
(Par_I
)
9309 Insert_After
(Freeze_Node
(Par_I
), F_Node
);
9316 -- When the instantiation occurs in a package declaration, append the
9317 -- freeze node to the private declarations (if any).
9319 if Nkind
(Par_N
) = N_Package_Specification
9320 and then Decls
= Visible_Declarations
(Par_N
)
9321 and then Present
(Private_Declarations
(Par_N
))
9322 and then not Is_Empty_List
(Private_Declarations
(Par_N
))
9324 Decls
:= Private_Declarations
(Par_N
);
9325 Decl
:= First
(Decls
);
9328 -- Determine the proper freeze point of a package instantiation. We
9329 -- adhere to the general rule of a package or subprogram body causing
9330 -- freezing of anything before it in the same declarative region. In
9331 -- this case, the proper freeze point of a package instantiation is
9332 -- before the first source body which follows, or before a stub. This
9333 -- ensures that entities coming from the instance are already frozen
9334 -- and usable in source bodies.
9336 if Nkind
(Par_N
) /= N_Package_Declaration
9337 and then Ekind
(Inst
) = E_Package
9338 and then Is_Generic_Instance
(Inst
)
9340 not In_Same_Source_Unit
(Generic_Parent
(Parent
(Inst
)), Inst
)
9342 while Present
(Decl
) loop
9343 if (Nkind
(Decl
) in N_Unit_Body
9345 Nkind
(Decl
) in N_Body_Stub
)
9346 and then Comes_From_Source
(Decl
)
9348 Insert_Before
(Decl
, F_Node
);
9356 -- In a package declaration, or if no previous body, insert at end
9359 Set_Sloc
(F_Node
, Sloc
(Last
(Decls
)));
9360 Insert_After
(Last
(Decls
), F_Node
);
9362 end Insert_Freeze_Node_For_Instance
;
9368 procedure Install_Body
9369 (Act_Body
: Node_Id
;
9374 function In_Same_Scope
(Gen_Id
, Act_Id
: Node_Id
) return Boolean;
9375 -- Check if the generic definition and the instantiation come from
9376 -- a common scope, in which case the instance must be frozen after
9377 -- the generic body.
9379 function True_Sloc
(N
, Act_Unit
: Node_Id
) return Source_Ptr
;
9380 -- If the instance is nested inside a generic unit, the Sloc of the
9381 -- instance indicates the place of the original definition, not the
9382 -- point of the current enclosing instance. Pending a better usage of
9383 -- Slocs to indicate instantiation places, we determine the place of
9384 -- origin of a node by finding the maximum sloc of any ancestor node.
9385 -- Why is this not equivalent to Top_Level_Location ???
9391 function In_Same_Scope
(Gen_Id
, Act_Id
: Node_Id
) return Boolean is
9392 Act_Scop
: Entity_Id
:= Scope
(Act_Id
);
9393 Gen_Scop
: Entity_Id
:= Scope
(Gen_Id
);
9396 while Act_Scop
/= Standard_Standard
9397 and then Gen_Scop
/= Standard_Standard
9399 if Act_Scop
= Gen_Scop
then
9403 Act_Scop
:= Scope
(Act_Scop
);
9404 Gen_Scop
:= Scope
(Gen_Scop
);
9414 function True_Sloc
(N
, Act_Unit
: Node_Id
) return Source_Ptr
is
9421 while Present
(N1
) and then N1
/= Act_Unit
loop
9422 if Sloc
(N1
) > Res
then
9432 Act_Id
: constant Entity_Id
:= Corresponding_Spec
(Act_Body
);
9433 Act_Unit
: constant Node_Id
:= Unit
(Cunit
(Get_Source_Unit
(N
)));
9434 Gen_Id
: constant Entity_Id
:= Corresponding_Spec
(Gen_Body
);
9435 Par
: constant Entity_Id
:= Scope
(Gen_Id
);
9436 Gen_Unit
: constant Node_Id
:=
9437 Unit
(Cunit
(Get_Source_Unit
(Gen_Decl
)));
9439 Body_Unit
: Node_Id
;
9441 Must_Delay
: Boolean;
9442 Orig_Body
: Node_Id
:= Gen_Body
;
9444 -- Start of processing for Install_Body
9447 -- Handle first the case of an instance with incomplete actual types.
9448 -- The instance body cannot be placed after the declaration because
9449 -- full views have not been seen yet. Any use of the non-limited views
9450 -- in the instance body requires the presence of a regular with_clause
9451 -- in the enclosing unit, and will fail if this with_clause is missing.
9452 -- We place the instance body at the beginning of the enclosing body,
9453 -- which is the unit being compiled. The freeze node for the instance
9454 -- is then placed after the instance body.
9456 if not Is_Empty_Elmt_List
(Incomplete_Actuals
(Act_Id
))
9457 and then Expander_Active
9458 and then Ekind
(Scope
(Act_Id
)) = E_Package
9461 Scop
: constant Entity_Id
:= Scope
(Act_Id
);
9462 Body_Id
: constant Node_Id
:=
9463 Corresponding_Body
(Unit_Declaration_Node
(Scop
));
9466 Ensure_Freeze_Node
(Act_Id
);
9467 F_Node
:= Freeze_Node
(Act_Id
);
9468 if Present
(Body_Id
) then
9469 Set_Is_Frozen
(Act_Id
, False);
9470 Prepend
(Act_Body
, Declarations
(Parent
(Body_Id
)));
9471 if Is_List_Member
(F_Node
) then
9475 Insert_After
(Act_Body
, F_Node
);
9481 -- If the body is a subunit, the freeze point is the corresponding stub
9482 -- in the current compilation, not the subunit itself.
9484 if Nkind
(Parent
(Gen_Body
)) = N_Subunit
then
9485 Orig_Body
:= Corresponding_Stub
(Parent
(Gen_Body
));
9487 Orig_Body
:= Gen_Body
;
9490 Body_Unit
:= Unit
(Cunit
(Get_Source_Unit
(Orig_Body
)));
9492 -- If the instantiation and the generic definition appear in the same
9493 -- package declaration, this is an early instantiation. If they appear
9494 -- in the same declarative part, it is an early instantiation only if
9495 -- the generic body appears textually later, and the generic body is
9496 -- also in the main unit.
9498 -- If instance is nested within a subprogram, and the generic body
9499 -- is not, the instance is delayed because the enclosing body is. If
9500 -- instance and body are within the same scope, or the same subprogram
9501 -- body, indicate explicitly that the instance is delayed.
9504 (Gen_Unit
= Act_Unit
9505 and then (Nkind_In
(Gen_Unit
, N_Generic_Package_Declaration
,
9506 N_Package_Declaration
)
9507 or else (Gen_Unit
= Body_Unit
9508 and then True_Sloc
(N
, Act_Unit
) <
9510 and then Is_In_Main_Unit
(Original_Node
(Gen_Unit
))
9511 and then In_Same_Scope
(Gen_Id
, Act_Id
));
9513 -- If this is an early instantiation, the freeze node is placed after
9514 -- the generic body. Otherwise, if the generic appears in an instance,
9515 -- we cannot freeze the current instance until the outer one is frozen.
9516 -- This is only relevant if the current instance is nested within some
9517 -- inner scope not itself within the outer instance. If this scope is
9518 -- a package body in the same declarative part as the outer instance,
9519 -- then that body needs to be frozen after the outer instance. Finally,
9520 -- if no delay is needed, we place the freeze node at the end of the
9521 -- current declarative part.
9524 and then (No
(Freeze_Node
(Act_Id
))
9525 or else not Is_List_Member
(Freeze_Node
(Act_Id
)))
9527 Ensure_Freeze_Node
(Act_Id
);
9528 F_Node
:= Freeze_Node
(Act_Id
);
9531 Insert_After
(Orig_Body
, F_Node
);
9533 elsif Is_Generic_Instance
(Par
)
9534 and then Present
(Freeze_Node
(Par
))
9535 and then Scope
(Act_Id
) /= Par
9537 -- Freeze instance of inner generic after instance of enclosing
9540 if In_Same_Declarative_Part
(Freeze_Node
(Par
), N
) then
9542 -- Handle the following case:
9544 -- package Parent_Inst is new ...
9547 -- procedure P ... -- this body freezes Parent_Inst
9549 -- package Inst is new ...
9551 -- In this particular scenario, the freeze node for Inst must
9552 -- be inserted in the same manner as that of Parent_Inst,
9553 -- before the next source body or at the end of the declarative
9554 -- list (body not available). If body P did not exist and
9555 -- Parent_Inst was frozen after Inst, either by a body
9556 -- following Inst or at the end of the declarative region,
9557 -- the freeze node for Inst must be inserted after that of
9558 -- Parent_Inst. This relation is established by comparing
9559 -- the Slocs of Parent_Inst freeze node and Inst.
9561 if List_Containing
(Get_Unit_Instantiation_Node
(Par
)) =
9563 and then Sloc
(Freeze_Node
(Par
)) < Sloc
(N
)
9565 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9567 Insert_After
(Freeze_Node
(Par
), F_Node
);
9570 -- Freeze package enclosing instance of inner generic after
9571 -- instance of enclosing generic.
9573 elsif Nkind_In
(Parent
(N
), N_Package_Body
, N_Subprogram_Body
)
9574 and then In_Same_Declarative_Part
(Freeze_Node
(Par
), Parent
(N
))
9577 Enclosing
: Entity_Id
;
9580 Enclosing
:= Corresponding_Spec
(Parent
(N
));
9582 if No
(Enclosing
) then
9583 Enclosing
:= Defining_Entity
(Parent
(N
));
9586 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9587 Ensure_Freeze_Node
(Enclosing
);
9589 if not Is_List_Member
(Freeze_Node
(Enclosing
)) then
9591 -- The enclosing context is a subunit, insert the freeze
9592 -- node after the stub.
9594 if Nkind
(Parent
(Parent
(N
))) = N_Subunit
then
9595 Insert_Freeze_Node_For_Instance
9596 (Corresponding_Stub
(Parent
(Parent
(N
))),
9597 Freeze_Node
(Enclosing
));
9599 -- The enclosing context is a package with a stub body
9600 -- which has already been replaced by the real body.
9601 -- Insert the freeze node after the actual body.
9603 elsif Ekind
(Enclosing
) = E_Package
9604 and then Present
(Body_Entity
(Enclosing
))
9605 and then Was_Originally_Stub
9606 (Parent
(Body_Entity
(Enclosing
)))
9608 Insert_Freeze_Node_For_Instance
9609 (Parent
(Body_Entity
(Enclosing
)),
9610 Freeze_Node
(Enclosing
));
9612 -- The parent instance has been frozen before the body of
9613 -- the enclosing package, insert the freeze node after
9616 elsif List_Containing
(Freeze_Node
(Par
)) =
9617 List_Containing
(Parent
(N
))
9618 and then Sloc
(Freeze_Node
(Par
)) < Sloc
(Parent
(N
))
9620 Insert_Freeze_Node_For_Instance
9621 (Parent
(N
), Freeze_Node
(Enclosing
));
9625 (Freeze_Node
(Par
), Freeze_Node
(Enclosing
));
9631 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9635 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9639 Set_Is_Frozen
(Act_Id
);
9640 Insert_Before
(N
, Act_Body
);
9641 Mark_Rewrite_Insertion
(Act_Body
);
9644 -----------------------------
9645 -- Install_Formal_Packages --
9646 -----------------------------
9648 procedure Install_Formal_Packages
(Par
: Entity_Id
) is
9651 Gen_E
: Entity_Id
:= Empty
;
9654 E
:= First_Entity
(Par
);
9656 -- If we are installing an instance parent, locate the formal packages
9657 -- of its generic parent.
9659 if Is_Generic_Instance
(Par
) then
9660 Gen
:= Generic_Parent
(Package_Specification
(Par
));
9661 Gen_E
:= First_Entity
(Gen
);
9664 while Present
(E
) loop
9665 if Ekind
(E
) = E_Package
9666 and then Nkind
(Parent
(E
)) = N_Package_Renaming_Declaration
9668 -- If this is the renaming for the parent instance, done
9670 if Renamed_Object
(E
) = Par
then
9673 -- The visibility of a formal of an enclosing generic is already
9676 elsif Denotes_Formal_Package
(E
) then
9679 elsif Present
(Associated_Formal_Package
(E
)) then
9680 Check_Generic_Actuals
(Renamed_Object
(E
), True);
9681 Set_Is_Hidden
(E
, False);
9683 -- Find formal package in generic unit that corresponds to
9684 -- (instance of) formal package in instance.
9686 while Present
(Gen_E
) and then Chars
(Gen_E
) /= Chars
(E
) loop
9687 Next_Entity
(Gen_E
);
9690 if Present
(Gen_E
) then
9691 Map_Formal_Package_Entities
(Gen_E
, E
);
9698 if Present
(Gen_E
) then
9699 Next_Entity
(Gen_E
);
9702 end Install_Formal_Packages
;
9704 --------------------
9705 -- Install_Parent --
9706 --------------------
9708 procedure Install_Parent
(P
: Entity_Id
; In_Body
: Boolean := False) is
9709 Ancestors
: constant Elist_Id
:= New_Elmt_List
;
9710 S
: constant Entity_Id
:= Current_Scope
;
9711 Inst_Par
: Entity_Id
;
9712 First_Par
: Entity_Id
;
9713 Inst_Node
: Node_Id
;
9714 Gen_Par
: Entity_Id
;
9715 First_Gen
: Entity_Id
;
9718 procedure Install_Noninstance_Specs
(Par
: Entity_Id
);
9719 -- Install the scopes of noninstance parent units ending with Par
9721 procedure Install_Spec
(Par
: Entity_Id
);
9722 -- The child unit is within the declarative part of the parent, so the
9723 -- declarations within the parent are immediately visible.
9725 -------------------------------
9726 -- Install_Noninstance_Specs --
9727 -------------------------------
9729 procedure Install_Noninstance_Specs
(Par
: Entity_Id
) is
9732 and then Par
/= Standard_Standard
9733 and then not In_Open_Scopes
(Par
)
9735 Install_Noninstance_Specs
(Scope
(Par
));
9738 end Install_Noninstance_Specs
;
9744 procedure Install_Spec
(Par
: Entity_Id
) is
9745 Spec
: constant Node_Id
:= Package_Specification
(Par
);
9748 -- If this parent of the child instance is a top-level unit,
9749 -- then record the unit and its visibility for later resetting in
9750 -- Remove_Parent. We exclude units that are generic instances, as we
9751 -- only want to record this information for the ultimate top-level
9752 -- noninstance parent (is that always correct???).
9754 if Scope
(Par
) = Standard_Standard
9755 and then not Is_Generic_Instance
(Par
)
9757 Parent_Unit_Visible
:= Is_Immediately_Visible
(Par
);
9758 Instance_Parent_Unit
:= Par
;
9761 -- Open the parent scope and make it and its declarations visible.
9762 -- If this point is not within a body, then only the visible
9763 -- declarations should be made visible, and installation of the
9764 -- private declarations is deferred until the appropriate point
9765 -- within analysis of the spec being instantiated (see the handling
9766 -- of parent visibility in Analyze_Package_Specification). This is
9767 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9768 -- private view problems that occur when compiling instantiations of
9769 -- a generic child of that package (Generic_Dispatching_Constructor).
9770 -- If the instance freezes a tagged type, inlinings of operations
9771 -- from Ada.Tags may need the full view of type Tag. If inlining took
9772 -- proper account of establishing visibility of inlined subprograms'
9773 -- parents then it should be possible to remove this
9774 -- special check. ???
9777 Set_Is_Immediately_Visible
(Par
);
9778 Install_Visible_Declarations
(Par
);
9779 Set_Use
(Visible_Declarations
(Spec
));
9781 if In_Body
or else Is_RTU
(Par
, Ada_Tags
) then
9782 Install_Private_Declarations
(Par
);
9783 Set_Use
(Private_Declarations
(Spec
));
9787 -- Start of processing for Install_Parent
9790 -- We need to install the parent instance to compile the instantiation
9791 -- of the child, but the child instance must appear in the current
9792 -- scope. Given that we cannot place the parent above the current scope
9793 -- in the scope stack, we duplicate the current scope and unstack both
9794 -- after the instantiation is complete.
9796 -- If the parent is itself the instantiation of a child unit, we must
9797 -- also stack the instantiation of its parent, and so on. Each such
9798 -- ancestor is the prefix of the name in a prior instantiation.
9800 -- If this is a nested instance, the parent unit itself resolves to
9801 -- a renaming of the parent instance, whose declaration we need.
9803 -- Finally, the parent may be a generic (not an instance) when the
9804 -- child unit appears as a formal package.
9808 if Present
(Renamed_Entity
(Inst_Par
)) then
9809 Inst_Par
:= Renamed_Entity
(Inst_Par
);
9812 First_Par
:= Inst_Par
;
9814 Gen_Par
:= Generic_Parent
(Package_Specification
(Inst_Par
));
9816 First_Gen
:= Gen_Par
;
9818 while Present
(Gen_Par
) and then Is_Child_Unit
(Gen_Par
) loop
9820 -- Load grandparent instance as well
9822 Inst_Node
:= Get_Unit_Instantiation_Node
(Inst_Par
);
9824 if Nkind
(Name
(Inst_Node
)) = N_Expanded_Name
then
9825 Inst_Par
:= Entity
(Prefix
(Name
(Inst_Node
)));
9827 if Present
(Renamed_Entity
(Inst_Par
)) then
9828 Inst_Par
:= Renamed_Entity
(Inst_Par
);
9831 Gen_Par
:= Generic_Parent
(Package_Specification
(Inst_Par
));
9833 if Present
(Gen_Par
) then
9834 Prepend_Elmt
(Inst_Par
, Ancestors
);
9837 -- Parent is not the name of an instantiation
9839 Install_Noninstance_Specs
(Inst_Par
);
9850 if Present
(First_Gen
) then
9851 Append_Elmt
(First_Par
, Ancestors
);
9853 Install_Noninstance_Specs
(First_Par
);
9856 if not Is_Empty_Elmt_List
(Ancestors
) then
9857 Elmt
:= First_Elmt
(Ancestors
);
9858 while Present
(Elmt
) loop
9859 Install_Spec
(Node
(Elmt
));
9860 Install_Formal_Packages
(Node
(Elmt
));
9870 -------------------------------
9871 -- Install_Hidden_Primitives --
9872 -------------------------------
9874 procedure Install_Hidden_Primitives
9875 (Prims_List
: in out Elist_Id
;
9880 List
: Elist_Id
:= No_Elist
;
9881 Prim_G_Elmt
: Elmt_Id
;
9882 Prim_A_Elmt
: Elmt_Id
;
9887 -- No action needed in case of serious errors because we cannot trust
9888 -- in the order of primitives
9890 if Serious_Errors_Detected
> 0 then
9893 -- No action possible if we don't have available the list of primitive
9897 or else not Is_Record_Type
(Gen_T
)
9898 or else not Is_Tagged_Type
(Gen_T
)
9899 or else not Is_Record_Type
(Act_T
)
9900 or else not Is_Tagged_Type
(Act_T
)
9904 -- There is no need to handle interface types since their primitives
9907 elsif Is_Interface
(Gen_T
) then
9911 Prim_G_Elmt
:= First_Elmt
(Primitive_Operations
(Gen_T
));
9913 if not Is_Class_Wide_Type
(Act_T
) then
9914 Prim_A_Elmt
:= First_Elmt
(Primitive_Operations
(Act_T
));
9916 Prim_A_Elmt
:= First_Elmt
(Primitive_Operations
(Root_Type
(Act_T
)));
9920 -- Skip predefined primitives in the generic formal
9922 while Present
(Prim_G_Elmt
)
9923 and then Is_Predefined_Dispatching_Operation
(Node
(Prim_G_Elmt
))
9925 Next_Elmt
(Prim_G_Elmt
);
9928 -- Skip predefined primitives in the generic actual
9930 while Present
(Prim_A_Elmt
)
9931 and then Is_Predefined_Dispatching_Operation
(Node
(Prim_A_Elmt
))
9933 Next_Elmt
(Prim_A_Elmt
);
9936 exit when No
(Prim_G_Elmt
) or else No
(Prim_A_Elmt
);
9938 Prim_G
:= Node
(Prim_G_Elmt
);
9939 Prim_A
:= Node
(Prim_A_Elmt
);
9941 -- There is no need to handle interface primitives because their
9942 -- primitives are not hidden
9944 exit when Present
(Interface_Alias
(Prim_G
));
9946 -- Here we install one hidden primitive
9948 if Chars
(Prim_G
) /= Chars
(Prim_A
)
9949 and then Has_Suffix
(Prim_A
, 'P')
9950 and then Remove_Suffix
(Prim_A
, 'P') = Chars
(Prim_G
)
9952 Set_Chars
(Prim_A
, Chars
(Prim_G
));
9953 Append_New_Elmt
(Prim_A
, To
=> List
);
9956 Next_Elmt
(Prim_A_Elmt
);
9957 Next_Elmt
(Prim_G_Elmt
);
9960 -- Append the elements to the list of temporarily visible primitives
9961 -- avoiding duplicates.
9963 if Present
(List
) then
9964 if No
(Prims_List
) then
9965 Prims_List
:= New_Elmt_List
;
9968 Elmt
:= First_Elmt
(List
);
9969 while Present
(Elmt
) loop
9970 Append_Unique_Elmt
(Node
(Elmt
), Prims_List
);
9974 end Install_Hidden_Primitives
;
9976 -------------------------------
9977 -- Restore_Hidden_Primitives --
9978 -------------------------------
9980 procedure Restore_Hidden_Primitives
(Prims_List
: in out Elist_Id
) is
9981 Prim_Elmt
: Elmt_Id
;
9985 if Prims_List
/= No_Elist
then
9986 Prim_Elmt
:= First_Elmt
(Prims_List
);
9987 while Present
(Prim_Elmt
) loop
9988 Prim
:= Node
(Prim_Elmt
);
9989 Set_Chars
(Prim
, Add_Suffix
(Prim
, 'P'));
9990 Next_Elmt
(Prim_Elmt
);
9993 Prims_List
:= No_Elist
;
9995 end Restore_Hidden_Primitives
;
9997 --------------------------------
9998 -- Instantiate_Formal_Package --
9999 --------------------------------
10001 function Instantiate_Formal_Package
10004 Analyzed_Formal
: Node_Id
) return List_Id
10006 Loc
: constant Source_Ptr
:= Sloc
(Actual
);
10007 Actual_Pack
: Entity_Id
;
10008 Formal_Pack
: Entity_Id
;
10009 Gen_Parent
: Entity_Id
;
10012 Parent_Spec
: Node_Id
;
10014 procedure Find_Matching_Actual
10016 Act
: in out Entity_Id
);
10017 -- We need to associate each formal entity in the formal package with
10018 -- the corresponding entity in the actual package. The actual package
10019 -- has been analyzed and possibly expanded, and as a result there is
10020 -- no one-to-one correspondence between the two lists (for example,
10021 -- the actual may include subtypes, itypes, and inherited primitive
10022 -- operations, interspersed among the renaming declarations for the
10023 -- actuals). We retrieve the corresponding actual by name because each
10024 -- actual has the same name as the formal, and they do appear in the
10027 function Get_Formal_Entity
(N
: Node_Id
) return Entity_Id
;
10028 -- Retrieve entity of defining entity of generic formal parameter.
10029 -- Only the declarations of formals need to be considered when
10030 -- linking them to actuals, but the declarative list may include
10031 -- internal entities generated during analysis, and those are ignored.
10033 procedure Match_Formal_Entity
10034 (Formal_Node
: Node_Id
;
10035 Formal_Ent
: Entity_Id
;
10036 Actual_Ent
: Entity_Id
);
10037 -- Associates the formal entity with the actual. In the case where
10038 -- Formal_Ent is a formal package, this procedure iterates through all
10039 -- of its formals and enters associations between the actuals occurring
10040 -- in the formal package's corresponding actual package (given by
10041 -- Actual_Ent) and the formal package's formal parameters. This
10042 -- procedure recurses if any of the parameters is itself a package.
10044 function Is_Instance_Of
10045 (Act_Spec
: Entity_Id
;
10046 Gen_Anc
: Entity_Id
) return Boolean;
10047 -- The actual can be an instantiation of a generic within another
10048 -- instance, in which case there is no direct link from it to the
10049 -- original generic ancestor. In that case, we recognize that the
10050 -- ultimate ancestor is the same by examining names and scopes.
10052 procedure Process_Nested_Formal
(Formal
: Entity_Id
);
10053 -- If the current formal is declared with a box, its own formals are
10054 -- visible in the instance, as they were in the generic, and their
10055 -- Hidden flag must be reset. If some of these formals are themselves
10056 -- packages declared with a box, the processing must be recursive.
10058 --------------------------
10059 -- Find_Matching_Actual --
10060 --------------------------
10062 procedure Find_Matching_Actual
10064 Act
: in out Entity_Id
)
10066 Formal_Ent
: Entity_Id
;
10069 case Nkind
(Original_Node
(F
)) is
10070 when N_Formal_Object_Declaration
10071 | N_Formal_Type_Declaration
10073 Formal_Ent
:= Defining_Identifier
(F
);
10075 while Chars
(Act
) /= Chars
(Formal_Ent
) loop
10079 when N_Formal_Package_Declaration
10080 | N_Formal_Subprogram_Declaration
10081 | N_Generic_Package_Declaration
10082 | N_Package_Declaration
10084 Formal_Ent
:= Defining_Entity
(F
);
10086 while Chars
(Act
) /= Chars
(Formal_Ent
) loop
10091 raise Program_Error
;
10093 end Find_Matching_Actual
;
10095 -------------------------
10096 -- Match_Formal_Entity --
10097 -------------------------
10099 procedure Match_Formal_Entity
10100 (Formal_Node
: Node_Id
;
10101 Formal_Ent
: Entity_Id
;
10102 Actual_Ent
: Entity_Id
)
10104 Act_Pkg
: Entity_Id
;
10107 Set_Instance_Of
(Formal_Ent
, Actual_Ent
);
10109 if Ekind
(Actual_Ent
) = E_Package
then
10111 -- Record associations for each parameter
10113 Act_Pkg
:= Actual_Ent
;
10116 A_Ent
: Entity_Id
:= First_Entity
(Act_Pkg
);
10120 Gen_Decl
: Node_Id
;
10122 Actual
: Entity_Id
;
10125 -- Retrieve the actual given in the formal package declaration
10127 Actual
:= Entity
(Name
(Original_Node
(Formal_Node
)));
10129 -- The actual in the formal package declaration may be a
10130 -- renamed generic package, in which case we want to retrieve
10131 -- the original generic in order to traverse its formal part.
10133 if Present
(Renamed_Entity
(Actual
)) then
10134 Gen_Decl
:= Unit_Declaration_Node
(Renamed_Entity
(Actual
));
10136 Gen_Decl
:= Unit_Declaration_Node
(Actual
);
10139 Formals
:= Generic_Formal_Declarations
(Gen_Decl
);
10141 if Present
(Formals
) then
10142 F_Node
:= First_Non_Pragma
(Formals
);
10147 while Present
(A_Ent
)
10148 and then Present
(F_Node
)
10149 and then A_Ent
/= First_Private_Entity
(Act_Pkg
)
10151 F_Ent
:= Get_Formal_Entity
(F_Node
);
10153 if Present
(F_Ent
) then
10155 -- This is a formal of the original package. Record
10156 -- association and recurse.
10158 Find_Matching_Actual
(F_Node
, A_Ent
);
10159 Match_Formal_Entity
(F_Node
, F_Ent
, A_Ent
);
10160 Next_Entity
(A_Ent
);
10163 Next_Non_Pragma
(F_Node
);
10167 end Match_Formal_Entity
;
10169 -----------------------
10170 -- Get_Formal_Entity --
10171 -----------------------
10173 function Get_Formal_Entity
(N
: Node_Id
) return Entity_Id
is
10174 Kind
: constant Node_Kind
:= Nkind
(Original_Node
(N
));
10177 when N_Formal_Object_Declaration
=>
10178 return Defining_Identifier
(N
);
10180 when N_Formal_Type_Declaration
=>
10181 return Defining_Identifier
(N
);
10183 when N_Formal_Subprogram_Declaration
=>
10184 return Defining_Unit_Name
(Specification
(N
));
10186 when N_Formal_Package_Declaration
=>
10187 return Defining_Identifier
(Original_Node
(N
));
10189 when N_Generic_Package_Declaration
=>
10190 return Defining_Identifier
(Original_Node
(N
));
10192 -- All other declarations are introduced by semantic analysis and
10193 -- have no match in the actual.
10198 end Get_Formal_Entity
;
10200 --------------------
10201 -- Is_Instance_Of --
10202 --------------------
10204 function Is_Instance_Of
10205 (Act_Spec
: Entity_Id
;
10206 Gen_Anc
: Entity_Id
) return Boolean
10208 Gen_Par
: constant Entity_Id
:= Generic_Parent
(Act_Spec
);
10211 if No
(Gen_Par
) then
10214 -- Simplest case: the generic parent of the actual is the formal
10216 elsif Gen_Par
= Gen_Anc
then
10219 elsif Chars
(Gen_Par
) /= Chars
(Gen_Anc
) then
10222 -- The actual may be obtained through several instantiations. Its
10223 -- scope must itself be an instance of a generic declared in the
10224 -- same scope as the formal. Any other case is detected above.
10226 elsif not Is_Generic_Instance
(Scope
(Gen_Par
)) then
10230 return Generic_Parent
(Parent
(Scope
(Gen_Par
))) = Scope
(Gen_Anc
);
10232 end Is_Instance_Of
;
10234 ---------------------------
10235 -- Process_Nested_Formal --
10236 ---------------------------
10238 procedure Process_Nested_Formal
(Formal
: Entity_Id
) is
10242 if Present
(Associated_Formal_Package
(Formal
))
10243 and then Box_Present
(Parent
(Associated_Formal_Package
(Formal
)))
10245 Ent
:= First_Entity
(Formal
);
10246 while Present
(Ent
) loop
10247 Set_Is_Hidden
(Ent
, False);
10248 Set_Is_Visible_Formal
(Ent
);
10249 Set_Is_Potentially_Use_Visible
10250 (Ent
, Is_Potentially_Use_Visible
(Formal
));
10252 if Ekind
(Ent
) = E_Package
then
10253 exit when Renamed_Entity
(Ent
) = Renamed_Entity
(Formal
);
10254 Process_Nested_Formal
(Ent
);
10260 end Process_Nested_Formal
;
10262 -- Start of processing for Instantiate_Formal_Package
10267 if not Is_Entity_Name
(Actual
)
10268 or else Ekind
(Entity
(Actual
)) /= E_Package
10271 ("expect package instance to instantiate formal", Actual
);
10272 Abandon_Instantiation
(Actual
);
10273 raise Program_Error
;
10276 Actual_Pack
:= Entity
(Actual
);
10277 Set_Is_Instantiated
(Actual_Pack
);
10279 -- The actual may be a renamed package, or an outer generic formal
10280 -- package whose instantiation is converted into a renaming.
10282 if Present
(Renamed_Object
(Actual_Pack
)) then
10283 Actual_Pack
:= Renamed_Object
(Actual_Pack
);
10286 if Nkind
(Analyzed_Formal
) = N_Formal_Package_Declaration
then
10287 Gen_Parent
:= Get_Instance_Of
(Entity
(Name
(Analyzed_Formal
)));
10288 Formal_Pack
:= Defining_Identifier
(Analyzed_Formal
);
10291 Generic_Parent
(Specification
(Analyzed_Formal
));
10293 Defining_Unit_Name
(Specification
(Analyzed_Formal
));
10296 if Nkind
(Parent
(Actual_Pack
)) = N_Defining_Program_Unit_Name
then
10297 Parent_Spec
:= Package_Specification
(Actual_Pack
);
10299 Parent_Spec
:= Parent
(Actual_Pack
);
10302 if Gen_Parent
= Any_Id
then
10304 ("previous error in declaration of formal package", Actual
);
10305 Abandon_Instantiation
(Actual
);
10308 Is_Instance_Of
(Parent_Spec
, Get_Instance_Of
(Gen_Parent
))
10314 ("actual parameter must be instance of&", Actual
, Gen_Parent
);
10315 Abandon_Instantiation
(Actual
);
10318 Set_Instance_Of
(Defining_Identifier
(Formal
), Actual_Pack
);
10319 Map_Formal_Package_Entities
(Formal_Pack
, Actual_Pack
);
10322 Make_Package_Renaming_Declaration
(Loc
,
10323 Defining_Unit_Name
=> New_Copy
(Defining_Identifier
(Formal
)),
10324 Name
=> New_Occurrence_Of
(Actual_Pack
, Loc
));
10326 Set_Associated_Formal_Package
10327 (Defining_Unit_Name
(Nod
), Defining_Identifier
(Formal
));
10328 Decls
:= New_List
(Nod
);
10330 -- If the formal F has a box, then the generic declarations are
10331 -- visible in the generic G. In an instance of G, the corresponding
10332 -- entities in the actual for F (which are the actuals for the
10333 -- instantiation of the generic that F denotes) must also be made
10334 -- visible for analysis of the current instance. On exit from the
10335 -- current instance, those entities are made private again. If the
10336 -- actual is currently in use, these entities are also use-visible.
10338 -- The loop through the actual entities also steps through the formal
10339 -- entities and enters associations from formals to actuals into the
10340 -- renaming map. This is necessary to properly handle checking of
10341 -- actual parameter associations for later formals that depend on
10342 -- actuals declared in the formal package.
10344 -- In Ada 2005, partial parameterization requires that we make
10345 -- visible the actuals corresponding to formals that were defaulted
10346 -- in the formal package. There formals are identified because they
10347 -- remain formal generics within the formal package, rather than
10348 -- being renamings of the actuals supplied.
10351 Gen_Decl
: constant Node_Id
:=
10352 Unit_Declaration_Node
(Gen_Parent
);
10353 Formals
: constant List_Id
:=
10354 Generic_Formal_Declarations
(Gen_Decl
);
10356 Actual_Ent
: Entity_Id
;
10357 Actual_Of_Formal
: Node_Id
;
10358 Formal_Node
: Node_Id
;
10359 Formal_Ent
: Entity_Id
;
10362 if Present
(Formals
) then
10363 Formal_Node
:= First_Non_Pragma
(Formals
);
10365 Formal_Node
:= Empty
;
10368 Actual_Ent
:= First_Entity
(Actual_Pack
);
10369 Actual_Of_Formal
:=
10370 First
(Visible_Declarations
(Specification
(Analyzed_Formal
)));
10371 while Present
(Actual_Ent
)
10372 and then Actual_Ent
/= First_Private_Entity
(Actual_Pack
)
10374 if Present
(Formal_Node
) then
10375 Formal_Ent
:= Get_Formal_Entity
(Formal_Node
);
10377 if Present
(Formal_Ent
) then
10378 Find_Matching_Actual
(Formal_Node
, Actual_Ent
);
10379 Match_Formal_Entity
(Formal_Node
, Formal_Ent
, Actual_Ent
);
10381 -- We iterate at the same time over the actuals of the
10382 -- local package created for the formal, to determine
10383 -- which one of the formals of the original generic were
10384 -- defaulted in the formal. The corresponding actual
10385 -- entities are visible in the enclosing instance.
10387 if Box_Present
(Formal
)
10389 (Present
(Actual_Of_Formal
)
10392 (Get_Formal_Entity
(Actual_Of_Formal
)))
10394 Set_Is_Hidden
(Actual_Ent
, False);
10395 Set_Is_Visible_Formal
(Actual_Ent
);
10396 Set_Is_Potentially_Use_Visible
10397 (Actual_Ent
, In_Use
(Actual_Pack
));
10399 if Ekind
(Actual_Ent
) = E_Package
then
10400 Process_Nested_Formal
(Actual_Ent
);
10404 Set_Is_Hidden
(Actual_Ent
);
10405 Set_Is_Potentially_Use_Visible
(Actual_Ent
, False);
10409 Next_Non_Pragma
(Formal_Node
);
10410 Next
(Actual_Of_Formal
);
10413 -- No further formals to match, but the generic part may
10414 -- contain inherited operation that are not hidden in the
10415 -- enclosing instance.
10417 Next_Entity
(Actual_Ent
);
10421 -- Inherited subprograms generated by formal derived types are
10422 -- also visible if the types are.
10424 Actual_Ent
:= First_Entity
(Actual_Pack
);
10425 while Present
(Actual_Ent
)
10426 and then Actual_Ent
/= First_Private_Entity
(Actual_Pack
)
10428 if Is_Overloadable
(Actual_Ent
)
10430 Nkind
(Parent
(Actual_Ent
)) = N_Subtype_Declaration
10432 not Is_Hidden
(Defining_Identifier
(Parent
(Actual_Ent
)))
10434 Set_Is_Hidden
(Actual_Ent
, False);
10435 Set_Is_Potentially_Use_Visible
10436 (Actual_Ent
, In_Use
(Actual_Pack
));
10439 Next_Entity
(Actual_Ent
);
10443 -- If the formal is not declared with a box, reanalyze it as an
10444 -- abbreviated instantiation, to verify the matching rules of 12.7.
10445 -- The actual checks are performed after the generic associations
10446 -- have been analyzed, to guarantee the same visibility for this
10447 -- instantiation and for the actuals.
10449 -- In Ada 2005, the generic associations for the formal can include
10450 -- defaulted parameters. These are ignored during check. This
10451 -- internal instantiation is removed from the tree after conformance
10452 -- checking, because it contains formal declarations for those
10453 -- defaulted parameters, and those should not reach the back-end.
10455 if not Box_Present
(Formal
) then
10457 I_Pack
: constant Entity_Id
:=
10458 Make_Temporary
(Sloc
(Actual
), 'P');
10461 Set_Is_Internal
(I_Pack
);
10464 Make_Package_Instantiation
(Sloc
(Actual
),
10465 Defining_Unit_Name
=> I_Pack
,
10468 (Get_Instance_Of
(Gen_Parent
), Sloc
(Actual
)),
10469 Generic_Associations
=> Generic_Associations
(Formal
)));
10475 end Instantiate_Formal_Package
;
10477 -----------------------------------
10478 -- Instantiate_Formal_Subprogram --
10479 -----------------------------------
10481 function Instantiate_Formal_Subprogram
10484 Analyzed_Formal
: Node_Id
) return Node_Id
10486 Analyzed_S
: constant Entity_Id
:=
10487 Defining_Unit_Name
(Specification
(Analyzed_Formal
));
10488 Formal_Sub
: constant Entity_Id
:=
10489 Defining_Unit_Name
(Specification
(Formal
));
10491 function From_Parent_Scope
(Subp
: Entity_Id
) return Boolean;
10492 -- If the generic is a child unit, the parent has been installed on the
10493 -- scope stack, but a default subprogram cannot resolve to something
10494 -- on the parent because that parent is not really part of the visible
10495 -- context (it is there to resolve explicit local entities). If the
10496 -- default has resolved in this way, we remove the entity from immediate
10497 -- visibility and analyze the node again to emit an error message or
10498 -- find another visible candidate.
10500 procedure Valid_Actual_Subprogram
(Act
: Node_Id
);
10501 -- Perform legality check and raise exception on failure
10503 -----------------------
10504 -- From_Parent_Scope --
10505 -----------------------
10507 function From_Parent_Scope
(Subp
: Entity_Id
) return Boolean is
10508 Gen_Scope
: Node_Id
;
10511 Gen_Scope
:= Scope
(Analyzed_S
);
10512 while Present
(Gen_Scope
) and then Is_Child_Unit
(Gen_Scope
) loop
10513 if Scope
(Subp
) = Scope
(Gen_Scope
) then
10517 Gen_Scope
:= Scope
(Gen_Scope
);
10521 end From_Parent_Scope
;
10523 -----------------------------
10524 -- Valid_Actual_Subprogram --
10525 -----------------------------
10527 procedure Valid_Actual_Subprogram
(Act
: Node_Id
) is
10531 if Is_Entity_Name
(Act
) then
10532 Act_E
:= Entity
(Act
);
10534 elsif Nkind
(Act
) = N_Selected_Component
10535 and then Is_Entity_Name
(Selector_Name
(Act
))
10537 Act_E
:= Entity
(Selector_Name
(Act
));
10543 if (Present
(Act_E
) and then Is_Overloadable
(Act_E
))
10544 or else Nkind_In
(Act
, N_Attribute_Reference
,
10545 N_Indexed_Component
,
10546 N_Character_Literal
,
10547 N_Explicit_Dereference
)
10553 ("expect subprogram or entry name in instantiation of &",
10554 Instantiation_Node
, Formal_Sub
);
10555 Abandon_Instantiation
(Instantiation_Node
);
10556 end Valid_Actual_Subprogram
;
10560 Decl_Node
: Node_Id
;
10563 New_Spec
: Node_Id
;
10564 New_Subp
: Entity_Id
;
10566 -- Start of processing for Instantiate_Formal_Subprogram
10569 New_Spec
:= New_Copy_Tree
(Specification
(Formal
));
10571 -- The tree copy has created the proper instantiation sloc for the
10572 -- new specification. Use this location for all other constructed
10575 Loc
:= Sloc
(Defining_Unit_Name
(New_Spec
));
10577 -- Create new entity for the actual (New_Copy_Tree does not), and
10578 -- indicate that it is an actual.
10580 New_Subp
:= Make_Defining_Identifier
(Loc
, Chars
(Formal_Sub
));
10581 Set_Ekind
(New_Subp
, Ekind
(Analyzed_S
));
10582 Set_Is_Generic_Actual_Subprogram
(New_Subp
);
10583 Set_Defining_Unit_Name
(New_Spec
, New_Subp
);
10585 -- Create new entities for the each of the formals in the specification
10586 -- of the renaming declaration built for the actual.
10588 if Present
(Parameter_Specifications
(New_Spec
)) then
10594 F
:= First
(Parameter_Specifications
(New_Spec
));
10595 while Present
(F
) loop
10596 F_Id
:= Defining_Identifier
(F
);
10598 Set_Defining_Identifier
(F
,
10599 Make_Defining_Identifier
(Sloc
(F_Id
), Chars
(F_Id
)));
10605 -- Find entity of actual. If the actual is an attribute reference, it
10606 -- cannot be resolved here (its formal is missing) but is handled
10607 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10608 -- fully resolved subsequently, when the renaming declaration for the
10609 -- formal is analyzed. If it is an explicit dereference, resolve the
10610 -- prefix but not the actual itself, to prevent interpretation as call.
10612 if Present
(Actual
) then
10613 Loc
:= Sloc
(Actual
);
10614 Set_Sloc
(New_Spec
, Loc
);
10616 if Nkind
(Actual
) = N_Operator_Symbol
then
10617 Find_Direct_Name
(Actual
);
10619 elsif Nkind
(Actual
) = N_Explicit_Dereference
then
10620 Analyze
(Prefix
(Actual
));
10622 elsif Nkind
(Actual
) /= N_Attribute_Reference
then
10626 Valid_Actual_Subprogram
(Actual
);
10629 elsif Present
(Default_Name
(Formal
)) then
10630 if not Nkind_In
(Default_Name
(Formal
), N_Attribute_Reference
,
10631 N_Selected_Component
,
10632 N_Indexed_Component
,
10633 N_Character_Literal
)
10634 and then Present
(Entity
(Default_Name
(Formal
)))
10636 Nam
:= New_Occurrence_Of
(Entity
(Default_Name
(Formal
)), Loc
);
10638 Nam
:= New_Copy
(Default_Name
(Formal
));
10639 Set_Sloc
(Nam
, Loc
);
10642 elsif Box_Present
(Formal
) then
10644 -- Actual is resolved at the point of instantiation. Create an
10645 -- identifier or operator with the same name as the formal.
10647 if Nkind
(Formal_Sub
) = N_Defining_Operator_Symbol
then
10649 Make_Operator_Symbol
(Loc
,
10650 Chars
=> Chars
(Formal_Sub
),
10651 Strval
=> No_String
);
10653 Nam
:= Make_Identifier
(Loc
, Chars
(Formal_Sub
));
10656 elsif Nkind
(Specification
(Formal
)) = N_Procedure_Specification
10657 and then Null_Present
(Specification
(Formal
))
10659 -- Generate null body for procedure, for use in the instance
10662 Make_Subprogram_Body
(Loc
,
10663 Specification
=> New_Spec
,
10664 Declarations
=> New_List
,
10665 Handled_Statement_Sequence
=>
10666 Make_Handled_Sequence_Of_Statements
(Loc
,
10667 Statements
=> New_List
(Make_Null_Statement
(Loc
))));
10669 Set_Is_Intrinsic_Subprogram
(Defining_Unit_Name
(New_Spec
));
10673 Error_Msg_Sloc
:= Sloc
(Scope
(Analyzed_S
));
10675 ("missing actual&", Instantiation_Node
, Formal_Sub
);
10677 ("\in instantiation of & declared#",
10678 Instantiation_Node
, Scope
(Analyzed_S
));
10679 Abandon_Instantiation
(Instantiation_Node
);
10683 Make_Subprogram_Renaming_Declaration
(Loc
,
10684 Specification
=> New_Spec
,
10687 -- If we do not have an actual and the formal specified <> then set to
10688 -- get proper default.
10690 if No
(Actual
) and then Box_Present
(Formal
) then
10691 Set_From_Default
(Decl_Node
);
10694 -- Gather possible interpretations for the actual before analyzing the
10695 -- instance. If overloaded, it will be resolved when analyzing the
10696 -- renaming declaration.
10698 if Box_Present
(Formal
) and then No
(Actual
) then
10701 if Is_Child_Unit
(Scope
(Analyzed_S
))
10702 and then Present
(Entity
(Nam
))
10704 if not Is_Overloaded
(Nam
) then
10705 if From_Parent_Scope
(Entity
(Nam
)) then
10706 Set_Is_Immediately_Visible
(Entity
(Nam
), False);
10707 Set_Entity
(Nam
, Empty
);
10708 Set_Etype
(Nam
, Empty
);
10711 Set_Is_Immediately_Visible
(Entity
(Nam
));
10720 Get_First_Interp
(Nam
, I
, It
);
10721 while Present
(It
.Nam
) loop
10722 if From_Parent_Scope
(It
.Nam
) then
10726 Get_Next_Interp
(I
, It
);
10733 -- The generic instantiation freezes the actual. This can only be done
10734 -- once the actual is resolved, in the analysis of the renaming
10735 -- declaration. To make the formal subprogram entity available, we set
10736 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10737 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10738 -- of formal abstract subprograms.
10740 Set_Corresponding_Formal_Spec
(Decl_Node
, Analyzed_S
);
10742 -- We cannot analyze the renaming declaration, and thus find the actual,
10743 -- until all the actuals are assembled in the instance. For subsequent
10744 -- checks of other actuals, indicate the node that will hold the
10745 -- instance of this formal.
10747 Set_Instance_Of
(Analyzed_S
, Nam
);
10749 if Nkind
(Actual
) = N_Selected_Component
10750 and then Is_Task_Type
(Etype
(Prefix
(Actual
)))
10751 and then not Is_Frozen
(Etype
(Prefix
(Actual
)))
10753 -- The renaming declaration will create a body, which must appear
10754 -- outside of the instantiation, We move the renaming declaration
10755 -- out of the instance, and create an additional renaming inside,
10756 -- to prevent freezing anomalies.
10759 Anon_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'E');
10762 Set_Defining_Unit_Name
(New_Spec
, Anon_Id
);
10763 Insert_Before
(Instantiation_Node
, Decl_Node
);
10764 Analyze
(Decl_Node
);
10766 -- Now create renaming within the instance
10769 Make_Subprogram_Renaming_Declaration
(Loc
,
10770 Specification
=> New_Copy_Tree
(New_Spec
),
10771 Name
=> New_Occurrence_Of
(Anon_Id
, Loc
));
10773 Set_Defining_Unit_Name
(Specification
(Decl_Node
),
10774 Make_Defining_Identifier
(Loc
, Chars
(Formal_Sub
)));
10779 end Instantiate_Formal_Subprogram
;
10781 ------------------------
10782 -- Instantiate_Object --
10783 ------------------------
10785 function Instantiate_Object
10788 Analyzed_Formal
: Node_Id
) return List_Id
10790 Gen_Obj
: constant Entity_Id
:= Defining_Identifier
(Formal
);
10791 A_Gen_Obj
: constant Entity_Id
:=
10792 Defining_Identifier
(Analyzed_Formal
);
10793 Acc_Def
: Node_Id
:= Empty
;
10794 Act_Assoc
: constant Node_Id
:= Parent
(Actual
);
10795 Actual_Decl
: Node_Id
:= Empty
;
10796 Decl_Node
: Node_Id
;
10799 List
: constant List_Id
:= New_List
;
10800 Loc
: constant Source_Ptr
:= Sloc
(Actual
);
10801 Orig_Ftyp
: constant Entity_Id
:= Etype
(A_Gen_Obj
);
10802 Subt_Decl
: Node_Id
:= Empty
;
10803 Subt_Mark
: Node_Id
:= Empty
;
10805 function Copy_Access_Def
return Node_Id
;
10806 -- If formal is an anonymous access, copy access definition of formal
10807 -- for generated object declaration.
10809 ---------------------
10810 -- Copy_Access_Def --
10811 ---------------------
10813 function Copy_Access_Def
return Node_Id
is
10815 Def
:= New_Copy_Tree
(Acc_Def
);
10817 -- In addition, if formal is an access to subprogram we need to
10818 -- generate new formals for the signature of the default, so that
10819 -- the tree is properly formatted for ASIS use.
10821 if Present
(Access_To_Subprogram_Definition
(Acc_Def
)) then
10823 Par_Spec
: Node_Id
;
10826 First
(Parameter_Specifications
10827 (Access_To_Subprogram_Definition
(Def
)));
10828 while Present
(Par_Spec
) loop
10829 Set_Defining_Identifier
(Par_Spec
,
10830 Make_Defining_Identifier
(Sloc
(Acc_Def
),
10831 Chars
=> Chars
(Defining_Identifier
(Par_Spec
))));
10838 end Copy_Access_Def
;
10840 -- Start of processing for Instantiate_Object
10843 -- Formal may be an anonymous access
10845 if Present
(Subtype_Mark
(Formal
)) then
10846 Subt_Mark
:= Subtype_Mark
(Formal
);
10848 Check_Access_Definition
(Formal
);
10849 Acc_Def
:= Access_Definition
(Formal
);
10852 -- Sloc for error message on missing actual
10854 Error_Msg_Sloc
:= Sloc
(Scope
(A_Gen_Obj
));
10856 if Get_Instance_Of
(Gen_Obj
) /= Gen_Obj
then
10857 Error_Msg_N
("duplicate instantiation of generic parameter", Actual
);
10860 Set_Parent
(List
, Parent
(Actual
));
10864 if Out_Present
(Formal
) then
10866 -- An IN OUT generic actual must be a name. The instantiation is a
10867 -- renaming declaration. The actual is the name being renamed. We
10868 -- use the actual directly, rather than a copy, because it is not
10869 -- used further in the list of actuals, and because a copy or a use
10870 -- of relocate_node is incorrect if the instance is nested within a
10871 -- generic. In order to simplify ASIS searches, the Generic_Parent
10872 -- field links the declaration to the generic association.
10874 if No
(Actual
) then
10876 ("missing actual &",
10877 Instantiation_Node
, Gen_Obj
);
10879 ("\in instantiation of & declared#",
10880 Instantiation_Node
, Scope
(A_Gen_Obj
));
10881 Abandon_Instantiation
(Instantiation_Node
);
10884 if Present
(Subt_Mark
) then
10886 Make_Object_Renaming_Declaration
(Loc
,
10887 Defining_Identifier
=> New_Copy
(Gen_Obj
),
10888 Subtype_Mark
=> New_Copy_Tree
(Subt_Mark
),
10891 else pragma Assert
(Present
(Acc_Def
));
10893 Make_Object_Renaming_Declaration
(Loc
,
10894 Defining_Identifier
=> New_Copy
(Gen_Obj
),
10895 Access_Definition
=> New_Copy_Tree
(Acc_Def
),
10899 Set_Corresponding_Generic_Association
(Decl_Node
, Act_Assoc
);
10901 -- The analysis of the actual may produce Insert_Action nodes, so
10902 -- the declaration must have a context in which to attach them.
10904 Append
(Decl_Node
, List
);
10907 -- Return if the analysis of the actual reported some error
10909 if Etype
(Actual
) = Any_Type
then
10913 -- This check is performed here because Analyze_Object_Renaming will
10914 -- not check it when Comes_From_Source is False. Note though that the
10915 -- check for the actual being the name of an object will be performed
10916 -- in Analyze_Object_Renaming.
10918 if Is_Object_Reference
(Actual
)
10919 and then Is_Dependent_Component_Of_Mutable_Object
(Actual
)
10922 ("illegal discriminant-dependent component for in out parameter",
10926 -- The actual has to be resolved in order to check that it is a
10927 -- variable (due to cases such as F (1), where F returns access to
10928 -- an array, and for overloaded prefixes).
10930 Ftyp
:= Get_Instance_Of
(Etype
(A_Gen_Obj
));
10932 -- If the type of the formal is not itself a formal, and the current
10933 -- unit is a child unit, the formal type must be declared in a
10934 -- parent, and must be retrieved by visibility.
10936 if Ftyp
= Orig_Ftyp
10937 and then Is_Generic_Unit
(Scope
(Ftyp
))
10938 and then Is_Child_Unit
(Scope
(A_Gen_Obj
))
10941 Temp
: constant Node_Id
:=
10942 New_Copy_Tree
(Subtype_Mark
(Analyzed_Formal
));
10944 Set_Entity
(Temp
, Empty
);
10946 Ftyp
:= Entity
(Temp
);
10950 if Is_Private_Type
(Ftyp
)
10951 and then not Is_Private_Type
(Etype
(Actual
))
10952 and then (Base_Type
(Full_View
(Ftyp
)) = Base_Type
(Etype
(Actual
))
10953 or else Base_Type
(Etype
(Actual
)) = Ftyp
)
10955 -- If the actual has the type of the full view of the formal, or
10956 -- else a non-private subtype of the formal, then the visibility
10957 -- of the formal type has changed. Add to the actuals a subtype
10958 -- declaration that will force the exchange of views in the body
10959 -- of the instance as well.
10962 Make_Subtype_Declaration
(Loc
,
10963 Defining_Identifier
=> Make_Temporary
(Loc
, 'P'),
10964 Subtype_Indication
=> New_Occurrence_Of
(Ftyp
, Loc
));
10966 Prepend
(Subt_Decl
, List
);
10968 Prepend_Elmt
(Full_View
(Ftyp
), Exchanged_Views
);
10969 Exchange_Declarations
(Ftyp
);
10972 Resolve
(Actual
, Ftyp
);
10974 if not Denotes_Variable
(Actual
) then
10975 Error_Msg_NE
("actual for& must be a variable", Actual
, Gen_Obj
);
10977 elsif Base_Type
(Ftyp
) /= Base_Type
(Etype
(Actual
)) then
10979 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10980 -- the type of the actual shall resolve to a specific anonymous
10983 if Ada_Version
< Ada_2005
10984 or else Ekind
(Base_Type
(Ftyp
)) /=
10985 E_Anonymous_Access_Type
10986 or else Ekind
(Base_Type
(Etype
(Actual
))) /=
10987 E_Anonymous_Access_Type
10990 ("type of actual does not match type of&", Actual
, Gen_Obj
);
10994 Note_Possible_Modification
(Actual
, Sure
=> True);
10996 -- Check for instantiation of atomic/volatile actual for
10997 -- non-atomic/volatile formal (RM C.6 (12)).
10999 if Is_Atomic_Object
(Actual
) and then not Is_Atomic
(Orig_Ftyp
) then
11001 ("cannot instantiate non-atomic formal object "
11002 & "with atomic actual", Actual
);
11004 elsif Is_Volatile_Object
(Actual
) and then not Is_Volatile
(Orig_Ftyp
)
11007 ("cannot instantiate non-volatile formal object "
11008 & "with volatile actual", Actual
);
11011 -- Formal in-parameter
11014 -- The instantiation of a generic formal in-parameter is constant
11015 -- declaration. The actual is the expression for that declaration.
11016 -- Its type is a full copy of the type of the formal. This may be
11017 -- an access to subprogram, for which we need to generate entities
11018 -- for the formals in the new signature.
11020 if Present
(Actual
) then
11021 if Present
(Subt_Mark
) then
11022 Def
:= New_Copy_Tree
(Subt_Mark
);
11023 else pragma Assert
(Present
(Acc_Def
));
11024 Def
:= Copy_Access_Def
;
11028 Make_Object_Declaration
(Loc
,
11029 Defining_Identifier
=> New_Copy
(Gen_Obj
),
11030 Constant_Present
=> True,
11031 Null_Exclusion_Present
=> Null_Exclusion_Present
(Formal
),
11032 Object_Definition
=> Def
,
11033 Expression
=> Actual
);
11035 Set_Corresponding_Generic_Association
(Decl_Node
, Act_Assoc
);
11037 -- A generic formal object of a tagged type is defined to be
11038 -- aliased so the new constant must also be treated as aliased.
11040 if Is_Tagged_Type
(Etype
(A_Gen_Obj
)) then
11041 Set_Aliased_Present
(Decl_Node
);
11044 Append
(Decl_Node
, List
);
11046 -- No need to repeat (pre-)analysis of some expression nodes
11047 -- already handled in Preanalyze_Actuals.
11049 if Nkind
(Actual
) /= N_Allocator
then
11052 -- Return if the analysis of the actual reported some error
11054 if Etype
(Actual
) = Any_Type
then
11060 Formal_Type
: constant Entity_Id
:= Etype
(A_Gen_Obj
);
11064 Typ
:= Get_Instance_Of
(Formal_Type
);
11066 -- If the actual appears in the current or an enclosing scope,
11067 -- use its type directly. This is relevant if it has an actual
11068 -- subtype that is distinct from its nominal one. This cannot
11069 -- be done in general because the type of the actual may
11070 -- depend on other actuals, and only be fully determined when
11071 -- the enclosing instance is analyzed.
11073 if Present
(Etype
(Actual
))
11074 and then Is_Constr_Subt_For_U_Nominal
(Etype
(Actual
))
11076 Freeze_Before
(Instantiation_Node
, Etype
(Actual
));
11078 Freeze_Before
(Instantiation_Node
, Typ
);
11081 -- If the actual is an aggregate, perform name resolution on
11082 -- its components (the analysis of an aggregate does not do it)
11083 -- to capture local names that may be hidden if the generic is
11086 if Nkind
(Actual
) = N_Aggregate
then
11087 Preanalyze_And_Resolve
(Actual
, Typ
);
11090 if Is_Limited_Type
(Typ
)
11091 and then not OK_For_Limited_Init
(Typ
, Actual
)
11094 ("initialization not allowed for limited types", Actual
);
11095 Explain_Limited_Type
(Typ
, Actual
);
11099 elsif Present
(Default_Expression
(Formal
)) then
11101 -- Use default to construct declaration
11103 if Present
(Subt_Mark
) then
11104 Def
:= New_Copy
(Subt_Mark
);
11105 else pragma Assert
(Present
(Acc_Def
));
11106 Def
:= Copy_Access_Def
;
11110 Make_Object_Declaration
(Sloc
(Formal
),
11111 Defining_Identifier
=> New_Copy
(Gen_Obj
),
11112 Constant_Present
=> True,
11113 Null_Exclusion_Present
=> Null_Exclusion_Present
(Formal
),
11114 Object_Definition
=> Def
,
11115 Expression
=> New_Copy_Tree
11116 (Default_Expression
(Formal
)));
11118 Append
(Decl_Node
, List
);
11119 Set_Analyzed
(Expression
(Decl_Node
), False);
11122 Error_Msg_NE
("missing actual&", Instantiation_Node
, Gen_Obj
);
11123 Error_Msg_NE
("\in instantiation of & declared#",
11124 Instantiation_Node
, Scope
(A_Gen_Obj
));
11126 if Is_Scalar_Type
(Etype
(A_Gen_Obj
)) then
11128 -- Create dummy constant declaration so that instance can be
11129 -- analyzed, to minimize cascaded visibility errors.
11131 if Present
(Subt_Mark
) then
11133 else pragma Assert
(Present
(Acc_Def
));
11138 Make_Object_Declaration
(Loc
,
11139 Defining_Identifier
=> New_Copy
(Gen_Obj
),
11140 Constant_Present
=> True,
11141 Null_Exclusion_Present
=> Null_Exclusion_Present
(Formal
),
11142 Object_Definition
=> New_Copy
(Def
),
11144 Make_Attribute_Reference
(Sloc
(Gen_Obj
),
11145 Attribute_Name
=> Name_First
,
11146 Prefix
=> New_Copy
(Def
)));
11148 Append
(Decl_Node
, List
);
11151 Abandon_Instantiation
(Instantiation_Node
);
11156 if Nkind
(Actual
) in N_Has_Entity
then
11157 Actual_Decl
:= Parent
(Entity
(Actual
));
11160 -- Ada 2005 (AI-423): For a formal object declaration with a null
11161 -- exclusion or an access definition that has a null exclusion: If the
11162 -- actual matching the formal object declaration denotes a generic
11163 -- formal object of another generic unit G, and the instantiation
11164 -- containing the actual occurs within the body of G or within the body
11165 -- of a generic unit declared within the declarative region of G, then
11166 -- the declaration of the formal object of G must have a null exclusion.
11167 -- Otherwise, the subtype of the actual matching the formal object
11168 -- declaration shall exclude null.
11170 if Ada_Version
>= Ada_2005
11171 and then Present
(Actual_Decl
)
11172 and then Nkind_In
(Actual_Decl
, N_Formal_Object_Declaration
,
11173 N_Object_Declaration
)
11174 and then Nkind
(Analyzed_Formal
) = N_Formal_Object_Declaration
11175 and then not Has_Null_Exclusion
(Actual_Decl
)
11176 and then Has_Null_Exclusion
(Analyzed_Formal
)
11178 Error_Msg_Sloc
:= Sloc
(Analyzed_Formal
);
11180 ("actual must exclude null to match generic formal#", Actual
);
11183 -- An effectively volatile object cannot be used as an actual in a
11184 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
11185 -- relevant only when SPARK_Mode is on as it is not a standard Ada
11186 -- legality rule, and also verifies that the actual is an object.
11189 and then Present
(Actual
)
11190 and then Is_Object_Reference
(Actual
)
11191 and then Is_Effectively_Volatile_Object
(Actual
)
11194 ("volatile object cannot act as actual in generic instantiation",
11199 end Instantiate_Object
;
11201 ------------------------------
11202 -- Instantiate_Package_Body --
11203 ------------------------------
11205 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11206 -- must be replaced by gotos which jump to the end of the routine in order
11207 -- to restore the Ghost and SPARK modes.
11209 procedure Instantiate_Package_Body
11210 (Body_Info
: Pending_Body_Info
;
11211 Inlined_Body
: Boolean := False;
11212 Body_Optional
: Boolean := False)
11214 Act_Decl
: constant Node_Id
:= Body_Info
.Act_Decl
;
11215 Act_Decl_Id
: constant Entity_Id
:= Defining_Entity
(Act_Decl
);
11216 Act_Spec
: constant Node_Id
:= Specification
(Act_Decl
);
11217 Inst_Node
: constant Node_Id
:= Body_Info
.Inst_Node
;
11218 Gen_Id
: constant Node_Id
:= Name
(Inst_Node
);
11219 Gen_Unit
: constant Entity_Id
:= Get_Generic_Entity
(Inst_Node
);
11220 Gen_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Gen_Unit
);
11221 Loc
: constant Source_Ptr
:= Sloc
(Inst_Node
);
11223 Saved_ISMP
: constant Boolean :=
11224 Ignore_SPARK_Mode_Pragmas_In_Instance
;
11225 Saved_Style_Check
: constant Boolean := Style_Check
;
11227 procedure Check_Initialized_Types
;
11228 -- In a generic package body, an entity of a generic private type may
11229 -- appear uninitialized. This is suspicious, unless the actual is a
11230 -- fully initialized type.
11232 -----------------------------
11233 -- Check_Initialized_Types --
11234 -----------------------------
11236 procedure Check_Initialized_Types
is
11238 Formal
: Entity_Id
;
11239 Actual
: Entity_Id
;
11240 Uninit_Var
: Entity_Id
;
11243 Decl
:= First
(Generic_Formal_Declarations
(Gen_Decl
));
11244 while Present
(Decl
) loop
11245 Uninit_Var
:= Empty
;
11247 if Nkind
(Decl
) = N_Private_Extension_Declaration
then
11248 Uninit_Var
:= Uninitialized_Variable
(Decl
);
11250 elsif Nkind
(Decl
) = N_Formal_Type_Declaration
11251 and then Nkind
(Formal_Type_Definition
(Decl
)) =
11252 N_Formal_Private_Type_Definition
11255 Uninitialized_Variable
(Formal_Type_Definition
(Decl
));
11258 if Present
(Uninit_Var
) then
11259 Formal
:= Defining_Identifier
(Decl
);
11260 Actual
:= First_Entity
(Act_Decl_Id
);
11262 -- For each formal there is a subtype declaration that renames
11263 -- the actual and has the same name as the formal. Locate the
11264 -- formal for warning message about uninitialized variables
11265 -- in the generic, for which the actual type should be a fully
11266 -- initialized type.
11268 while Present
(Actual
) loop
11269 exit when Ekind
(Actual
) = E_Package
11270 and then Present
(Renamed_Object
(Actual
));
11272 if Chars
(Actual
) = Chars
(Formal
)
11273 and then not Is_Scalar_Type
(Actual
)
11274 and then not Is_Fully_Initialized_Type
(Actual
)
11275 and then Warn_On_No_Value_Assigned
11277 Error_Msg_Node_2
:= Formal
;
11279 ("generic unit has uninitialized variable& of "
11280 & "formal private type &?v?", Actual
, Uninit_Var
);
11282 ("actual type for& should be fully initialized type?v?",
11287 Next_Entity
(Actual
);
11293 end Check_Initialized_Types
;
11297 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
11298 Saved_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
11299 Saved_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
11300 -- Save the Ghost and SPARK mode-related data to restore on exit
11302 Act_Body
: Node_Id
;
11303 Act_Body_Id
: Entity_Id
;
11304 Act_Body_Name
: Node_Id
;
11305 Gen_Body
: Node_Id
;
11306 Gen_Body_Id
: Node_Id
;
11307 Par_Ent
: Entity_Id
:= Empty
;
11308 Par_Vis
: Boolean := False;
11309 Parent_Installed
: Boolean := False;
11311 Vis_Prims_List
: Elist_Id
:= No_Elist
;
11312 -- List of primitives made temporarily visible in the instantiation
11313 -- to match the visibility of the formal type.
11315 -- Start of processing for Instantiate_Package_Body
11318 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
11320 -- The instance body may already have been processed, as the parent of
11321 -- another instance that is inlined (Load_Parent_Of_Generic).
11323 if Present
(Corresponding_Body
(Instance_Spec
(Inst_Node
))) then
11327 -- The package being instantiated may be subject to pragma Ghost. Set
11328 -- the mode now to ensure that any nodes generated during instantiation
11329 -- are properly marked as Ghost.
11331 Set_Ghost_Mode
(Act_Decl_Id
);
11333 Expander_Mode_Save_And_Set
(Body_Info
.Expander_Status
);
11335 -- Re-establish the state of information on which checks are suppressed.
11336 -- This information was set in Body_Info at the point of instantiation,
11337 -- and now we restore it so that the instance is compiled using the
11338 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11340 Local_Suppress_Stack_Top
:= Body_Info
.Local_Suppress_Stack_Top
;
11341 Scope_Suppress
:= Body_Info
.Scope_Suppress
;
11342 Opt
.Ada_Version
:= Body_Info
.Version
;
11343 Opt
.Ada_Version_Pragma
:= Body_Info
.Version_Pragma
;
11344 Restore_Warnings
(Body_Info
.Warnings
);
11346 -- Install the SPARK mode which applies to the package body
11348 Install_SPARK_Mode
(Body_Info
.SPARK_Mode
, Body_Info
.SPARK_Mode_Pragma
);
11350 if No
(Gen_Body_Id
) then
11352 -- Do not look for parent of generic body if none is required.
11353 -- This may happen when the routine is called as part of the
11354 -- Pending_Instantiations processing, when nested instances
11355 -- may precede the one generated from the main unit.
11357 if not Unit_Requires_Body
(Defining_Entity
(Gen_Decl
))
11358 and then Body_Optional
11362 Load_Parent_Of_Generic
11363 (Inst_Node
, Specification
(Gen_Decl
), Body_Optional
);
11364 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
11368 -- Establish global variable for sloc adjustment and for error recovery
11369 -- In the case of an instance body for an instantiation with actuals
11370 -- from a limited view, the instance body is placed at the beginning
11371 -- of the enclosing package body: use the body entity as the source
11372 -- location for nodes of the instance body.
11374 if not Is_Empty_Elmt_List
(Incomplete_Actuals
(Act_Decl_Id
)) then
11376 Scop
: constant Entity_Id
:= Scope
(Act_Decl_Id
);
11377 Body_Id
: constant Node_Id
:=
11378 Corresponding_Body
(Unit_Declaration_Node
(Scop
));
11381 Instantiation_Node
:= Body_Id
;
11384 Instantiation_Node
:= Inst_Node
;
11387 if Present
(Gen_Body_Id
) then
11388 Save_Env
(Gen_Unit
, Act_Decl_Id
);
11389 Style_Check
:= False;
11391 -- If the context of the instance is subject to SPARK_Mode "off", the
11392 -- annotation is missing, or the body is instantiated at a later pass
11393 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11394 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11397 if SPARK_Mode
/= On
11398 or else Ignore_SPARK_Mode_Pragmas
(Act_Decl_Id
)
11400 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
11403 Current_Sem_Unit
:= Body_Info
.Current_Sem_Unit
;
11404 Gen_Body
:= Unit_Declaration_Node
(Gen_Body_Id
);
11406 Create_Instantiation_Source
11407 (Inst_Node
, Gen_Body_Id
, S_Adjustment
);
11411 (Original_Node
(Gen_Body
), Empty
, Instantiating
=> True);
11413 -- Create proper (possibly qualified) defining name for the body, to
11414 -- correspond to the one in the spec.
11417 Make_Defining_Identifier
(Sloc
(Act_Decl_Id
), Chars
(Act_Decl_Id
));
11418 Set_Comes_From_Source
(Act_Body_Id
, Comes_From_Source
(Act_Decl_Id
));
11420 -- Some attributes of spec entity are not inherited by body entity
11422 Set_Handler_Records
(Act_Body_Id
, No_List
);
11424 if Nkind
(Defining_Unit_Name
(Act_Spec
)) =
11425 N_Defining_Program_Unit_Name
11428 Make_Defining_Program_Unit_Name
(Loc
,
11430 New_Copy_Tree
(Name
(Defining_Unit_Name
(Act_Spec
))),
11431 Defining_Identifier
=> Act_Body_Id
);
11433 Act_Body_Name
:= Act_Body_Id
;
11436 Set_Defining_Unit_Name
(Act_Body
, Act_Body_Name
);
11438 Set_Corresponding_Spec
(Act_Body
, Act_Decl_Id
);
11439 Check_Generic_Actuals
(Act_Decl_Id
, False);
11440 Check_Initialized_Types
;
11442 -- Install primitives hidden at the point of the instantiation but
11443 -- visible when processing the generic formals
11449 E
:= First_Entity
(Act_Decl_Id
);
11450 while Present
(E
) loop
11452 and then not Is_Itype
(E
)
11453 and then Is_Generic_Actual_Type
(E
)
11454 and then Is_Tagged_Type
(E
)
11456 Install_Hidden_Primitives
11457 (Prims_List
=> Vis_Prims_List
,
11458 Gen_T
=> Generic_Parent_Type
(Parent
(E
)),
11466 -- If it is a child unit, make the parent instance (which is an
11467 -- instance of the parent of the generic) visible. The parent
11468 -- instance is the prefix of the name of the generic unit.
11470 if Ekind
(Scope
(Gen_Unit
)) = E_Generic_Package
11471 and then Nkind
(Gen_Id
) = N_Expanded_Name
11473 Par_Ent
:= Entity
(Prefix
(Gen_Id
));
11474 Par_Vis
:= Is_Immediately_Visible
(Par_Ent
);
11475 Install_Parent
(Par_Ent
, In_Body
=> True);
11476 Parent_Installed
:= True;
11478 elsif Is_Child_Unit
(Gen_Unit
) then
11479 Par_Ent
:= Scope
(Gen_Unit
);
11480 Par_Vis
:= Is_Immediately_Visible
(Par_Ent
);
11481 Install_Parent
(Par_Ent
, In_Body
=> True);
11482 Parent_Installed
:= True;
11485 -- If the instantiation is a library unit, and this is the main unit,
11486 -- then build the resulting compilation unit nodes for the instance.
11487 -- If this is a compilation unit but it is not the main unit, then it
11488 -- is the body of a unit in the context, that is being compiled
11489 -- because it is encloses some inlined unit or another generic unit
11490 -- being instantiated. In that case, this body is not part of the
11491 -- current compilation, and is not attached to the tree, but its
11492 -- parent must be set for analysis.
11494 if Nkind
(Parent
(Inst_Node
)) = N_Compilation_Unit
then
11496 -- Replace instance node with body of instance, and create new
11497 -- node for corresponding instance declaration.
11499 Build_Instance_Compilation_Unit_Nodes
11500 (Inst_Node
, Act_Body
, Act_Decl
);
11501 Analyze
(Inst_Node
);
11503 if Parent
(Inst_Node
) = Cunit
(Main_Unit
) then
11505 -- If the instance is a child unit itself, then set the scope
11506 -- of the expanded body to be the parent of the instantiation
11507 -- (ensuring that the fully qualified name will be generated
11508 -- for the elaboration subprogram).
11510 if Nkind
(Defining_Unit_Name
(Act_Spec
)) =
11511 N_Defining_Program_Unit_Name
11513 Set_Scope
(Defining_Entity
(Inst_Node
), Scope
(Act_Decl_Id
));
11517 -- Case where instantiation is not a library unit
11520 -- If this is an early instantiation, i.e. appears textually
11521 -- before the corresponding body and must be elaborated first,
11522 -- indicate that the body instance is to be delayed.
11524 Install_Body
(Act_Body
, Inst_Node
, Gen_Body
, Gen_Decl
);
11526 -- Now analyze the body. We turn off all checks if this is an
11527 -- internal unit, since there is no reason to have checks on for
11528 -- any predefined run-time library code. All such code is designed
11529 -- to be compiled with checks off.
11531 -- Note that we do NOT apply this criterion to children of GNAT
11532 -- The latter units must suppress checks explicitly if needed.
11534 -- We also do not suppress checks in CodePeer mode where we are
11535 -- interested in finding possible runtime errors.
11537 if not CodePeer_Mode
11538 and then In_Predefined_Unit
(Gen_Decl
)
11540 Analyze
(Act_Body
, Suppress
=> All_Checks
);
11542 Analyze
(Act_Body
);
11546 Inherit_Context
(Gen_Body
, Inst_Node
);
11548 -- Remove the parent instances if they have been placed on the scope
11549 -- stack to compile the body.
11551 if Parent_Installed
then
11552 Remove_Parent
(In_Body
=> True);
11554 -- Restore the previous visibility of the parent
11556 Set_Is_Immediately_Visible
(Par_Ent
, Par_Vis
);
11559 Restore_Hidden_Primitives
(Vis_Prims_List
);
11560 Restore_Private_Views
(Act_Decl_Id
);
11562 -- Remove the current unit from visibility if this is an instance
11563 -- that is not elaborated on the fly for inlining purposes.
11565 if not Inlined_Body
then
11566 Set_Is_Immediately_Visible
(Act_Decl_Id
, False);
11571 -- If we have no body, and the unit requires a body, then complain. This
11572 -- complaint is suppressed if we have detected other errors (since a
11573 -- common reason for missing the body is that it had errors).
11574 -- In CodePeer mode, a warning has been emitted already, no need for
11575 -- further messages.
11577 elsif Unit_Requires_Body
(Gen_Unit
)
11578 and then not Body_Optional
11580 if CodePeer_Mode
then
11583 elsif Serious_Errors_Detected
= 0 then
11585 ("cannot find body of generic package &", Inst_Node
, Gen_Unit
);
11587 -- Don't attempt to perform any cleanup actions if some other error
11588 -- was already detected, since this can cause blowups.
11594 -- Case of package that does not need a body
11597 -- If the instantiation of the declaration is a library unit, rewrite
11598 -- the original package instantiation as a package declaration in the
11599 -- compilation unit node.
11601 if Nkind
(Parent
(Inst_Node
)) = N_Compilation_Unit
then
11602 Set_Parent_Spec
(Act_Decl
, Parent_Spec
(Inst_Node
));
11603 Rewrite
(Inst_Node
, Act_Decl
);
11605 -- Generate elaboration entity, in case spec has elaboration code.
11606 -- This cannot be done when the instance is analyzed, because it
11607 -- is not known yet whether the body exists.
11609 Set_Elaboration_Entity_Required
(Act_Decl_Id
, False);
11610 Build_Elaboration_Entity
(Parent
(Inst_Node
), Act_Decl_Id
);
11612 -- If the instantiation is not a library unit, then append the
11613 -- declaration to the list of implicitly generated entities, unless
11614 -- it is already a list member which means that it was already
11617 elsif not Is_List_Member
(Act_Decl
) then
11618 Mark_Rewrite_Insertion
(Act_Decl
);
11619 Insert_Before
(Inst_Node
, Act_Decl
);
11623 Expander_Mode_Restore
;
11626 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
11627 Restore_Ghost_Mode
(Saved_GM
);
11628 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
11629 Style_Check
:= Saved_Style_Check
;
11630 end Instantiate_Package_Body
;
11632 ---------------------------------
11633 -- Instantiate_Subprogram_Body --
11634 ---------------------------------
11636 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11637 -- must be replaced by gotos which jump to the end of the routine in order
11638 -- to restore the Ghost and SPARK modes.
11640 procedure Instantiate_Subprogram_Body
11641 (Body_Info
: Pending_Body_Info
;
11642 Body_Optional
: Boolean := False)
11644 Act_Decl
: constant Node_Id
:= Body_Info
.Act_Decl
;
11645 Act_Decl_Id
: constant Entity_Id
:= Defining_Entity
(Act_Decl
);
11646 Inst_Node
: constant Node_Id
:= Body_Info
.Inst_Node
;
11647 Gen_Id
: constant Node_Id
:= Name
(Inst_Node
);
11648 Gen_Unit
: constant Entity_Id
:= Get_Generic_Entity
(Inst_Node
);
11649 Gen_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Gen_Unit
);
11650 Loc
: constant Source_Ptr
:= Sloc
(Inst_Node
);
11651 Pack_Id
: constant Entity_Id
:=
11652 Defining_Unit_Name
(Parent
(Act_Decl
));
11654 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
11655 Saved_ISMP
: constant Boolean :=
11656 Ignore_SPARK_Mode_Pragmas_In_Instance
;
11657 Saved_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
11658 Saved_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
11659 -- Save the Ghost and SPARK mode-related data to restore on exit
11661 Saved_Style_Check
: constant Boolean := Style_Check
;
11662 Saved_Warnings
: constant Warning_Record
:= Save_Warnings
;
11664 Act_Body
: Node_Id
;
11665 Act_Body_Id
: Entity_Id
;
11666 Gen_Body
: Node_Id
;
11667 Gen_Body_Id
: Node_Id
;
11668 Pack_Body
: Node_Id
;
11669 Par_Ent
: Entity_Id
:= Empty
;
11670 Par_Vis
: Boolean := False;
11671 Ret_Expr
: Node_Id
;
11673 Parent_Installed
: Boolean := False;
11676 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
11678 -- Subprogram body may have been created already because of an inline
11679 -- pragma, or because of multiple elaborations of the enclosing package
11680 -- when several instances of the subprogram appear in the main unit.
11682 if Present
(Corresponding_Body
(Act_Decl
)) then
11686 -- The subprogram being instantiated may be subject to pragma Ghost. Set
11687 -- the mode now to ensure that any nodes generated during instantiation
11688 -- are properly marked as Ghost.
11690 Set_Ghost_Mode
(Act_Decl_Id
);
11692 Expander_Mode_Save_And_Set
(Body_Info
.Expander_Status
);
11694 -- Re-establish the state of information on which checks are suppressed.
11695 -- This information was set in Body_Info at the point of instantiation,
11696 -- and now we restore it so that the instance is compiled using the
11697 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11699 Local_Suppress_Stack_Top
:= Body_Info
.Local_Suppress_Stack_Top
;
11700 Scope_Suppress
:= Body_Info
.Scope_Suppress
;
11701 Opt
.Ada_Version
:= Body_Info
.Version
;
11702 Opt
.Ada_Version_Pragma
:= Body_Info
.Version_Pragma
;
11703 Restore_Warnings
(Body_Info
.Warnings
);
11705 -- Install the SPARK mode which applies to the subprogram body from the
11706 -- instantiation context. This may be refined further if an explicit
11707 -- SPARK_Mode pragma applies to the generic body.
11709 Install_SPARK_Mode
(Body_Info
.SPARK_Mode
, Body_Info
.SPARK_Mode_Pragma
);
11711 if No
(Gen_Body_Id
) then
11713 -- For imported generic subprogram, no body to compile, complete
11714 -- the spec entity appropriately.
11716 if Is_Imported
(Gen_Unit
) then
11717 Set_Is_Imported
(Act_Decl_Id
);
11718 Set_First_Rep_Item
(Act_Decl_Id
, First_Rep_Item
(Gen_Unit
));
11719 Set_Interface_Name
(Act_Decl_Id
, Interface_Name
(Gen_Unit
));
11720 Set_Convention
(Act_Decl_Id
, Convention
(Gen_Unit
));
11721 Set_Has_Completion
(Act_Decl_Id
);
11724 -- For other cases, compile the body
11727 Load_Parent_Of_Generic
11728 (Inst_Node
, Specification
(Gen_Decl
), Body_Optional
);
11729 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
11733 Instantiation_Node
:= Inst_Node
;
11735 if Present
(Gen_Body_Id
) then
11736 Gen_Body
:= Unit_Declaration_Node
(Gen_Body_Id
);
11738 if Nkind
(Gen_Body
) = N_Subprogram_Body_Stub
then
11740 -- Either body is not present, or context is non-expanding, as
11741 -- when compiling a subunit. Mark the instance as completed, and
11742 -- diagnose a missing body when needed.
11745 and then Operating_Mode
= Generate_Code
11747 Error_Msg_N
("missing proper body for instantiation", Gen_Body
);
11750 Set_Has_Completion
(Act_Decl_Id
);
11754 Save_Env
(Gen_Unit
, Act_Decl_Id
);
11755 Style_Check
:= False;
11757 -- If the context of the instance is subject to SPARK_Mode "off", the
11758 -- annotation is missing, or the body is instantiated at a later pass
11759 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11760 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11763 if SPARK_Mode
/= On
11764 or else Ignore_SPARK_Mode_Pragmas
(Act_Decl_Id
)
11766 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
11769 -- If the context of an instance is not subject to SPARK_Mode "off",
11770 -- and the generic body is subject to an explicit SPARK_Mode pragma,
11771 -- the latter should be the one applicable to the instance.
11773 if not Ignore_SPARK_Mode_Pragmas_In_Instance
11774 and then SPARK_Mode
/= Off
11775 and then Present
(SPARK_Pragma
(Gen_Body_Id
))
11777 Set_SPARK_Mode
(Gen_Body_Id
);
11780 Current_Sem_Unit
:= Body_Info
.Current_Sem_Unit
;
11781 Create_Instantiation_Source
11788 (Original_Node
(Gen_Body
), Empty
, Instantiating
=> True);
11790 -- Create proper defining name for the body, to correspond to the one
11794 Make_Defining_Identifier
(Sloc
(Act_Decl_Id
), Chars
(Act_Decl_Id
));
11796 Set_Comes_From_Source
(Act_Body_Id
, Comes_From_Source
(Act_Decl_Id
));
11797 Set_Defining_Unit_Name
(Specification
(Act_Body
), Act_Body_Id
);
11799 Set_Corresponding_Spec
(Act_Body
, Act_Decl_Id
);
11800 Set_Has_Completion
(Act_Decl_Id
);
11801 Check_Generic_Actuals
(Pack_Id
, False);
11803 -- Generate a reference to link the visible subprogram instance to
11804 -- the generic body, which for navigation purposes is the only
11805 -- available source for the instance.
11808 (Related_Instance
(Pack_Id
),
11809 Gen_Body_Id
, 'b', Set_Ref
=> False, Force
=> True);
11811 -- If it is a child unit, make the parent instance (which is an
11812 -- instance of the parent of the generic) visible. The parent
11813 -- instance is the prefix of the name of the generic unit.
11815 if Ekind
(Scope
(Gen_Unit
)) = E_Generic_Package
11816 and then Nkind
(Gen_Id
) = N_Expanded_Name
11818 Par_Ent
:= Entity
(Prefix
(Gen_Id
));
11819 Par_Vis
:= Is_Immediately_Visible
(Par_Ent
);
11820 Install_Parent
(Par_Ent
, In_Body
=> True);
11821 Parent_Installed
:= True;
11823 elsif Is_Child_Unit
(Gen_Unit
) then
11824 Par_Ent
:= Scope
(Gen_Unit
);
11825 Par_Vis
:= Is_Immediately_Visible
(Par_Ent
);
11826 Install_Parent
(Par_Ent
, In_Body
=> True);
11827 Parent_Installed
:= True;
11830 -- Subprogram body is placed in the body of wrapper package,
11831 -- whose spec contains the subprogram declaration as well as
11832 -- the renaming declarations for the generic parameters.
11835 Make_Package_Body
(Loc
,
11836 Defining_Unit_Name
=> New_Copy
(Pack_Id
),
11837 Declarations
=> New_List
(Act_Body
));
11839 Set_Corresponding_Spec
(Pack_Body
, Pack_Id
);
11841 -- If the instantiation is a library unit, then build resulting
11842 -- compilation unit nodes for the instance. The declaration of
11843 -- the enclosing package is the grandparent of the subprogram
11844 -- declaration. First replace the instantiation node as the unit
11845 -- of the corresponding compilation.
11847 if Nkind
(Parent
(Inst_Node
)) = N_Compilation_Unit
then
11848 if Parent
(Inst_Node
) = Cunit
(Main_Unit
) then
11849 Set_Unit
(Parent
(Inst_Node
), Inst_Node
);
11850 Build_Instance_Compilation_Unit_Nodes
11851 (Inst_Node
, Pack_Body
, Parent
(Parent
(Act_Decl
)));
11852 Analyze
(Inst_Node
);
11854 Set_Parent
(Pack_Body
, Parent
(Inst_Node
));
11855 Analyze
(Pack_Body
);
11859 Insert_Before
(Inst_Node
, Pack_Body
);
11860 Mark_Rewrite_Insertion
(Pack_Body
);
11861 Analyze
(Pack_Body
);
11863 if Expander_Active
then
11864 Freeze_Subprogram_Body
(Inst_Node
, Gen_Body
, Pack_Id
);
11868 Inherit_Context
(Gen_Body
, Inst_Node
);
11870 Restore_Private_Views
(Pack_Id
, False);
11872 if Parent_Installed
then
11873 Remove_Parent
(In_Body
=> True);
11875 -- Restore the previous visibility of the parent
11877 Set_Is_Immediately_Visible
(Par_Ent
, Par_Vis
);
11881 Restore_Warnings
(Saved_Warnings
);
11883 -- Body not found. Error was emitted already. If there were no previous
11884 -- errors, this may be an instance whose scope is a premature instance.
11885 -- In that case we must insure that the (legal) program does raise
11886 -- program error if executed. We generate a subprogram body for this
11887 -- purpose. See DEC ac30vso.
11889 -- Should not reference proprietary DEC tests in comments ???
11891 elsif Serious_Errors_Detected
= 0
11892 and then Nkind
(Parent
(Inst_Node
)) /= N_Compilation_Unit
11894 if Body_Optional
then
11897 elsif Ekind
(Act_Decl_Id
) = E_Procedure
then
11899 Make_Subprogram_Body
(Loc
,
11901 Make_Procedure_Specification
(Loc
,
11902 Defining_Unit_Name
=>
11903 Make_Defining_Identifier
(Loc
, Chars
(Act_Decl_Id
)),
11904 Parameter_Specifications
=>
11906 (Parameter_Specifications
(Parent
(Act_Decl_Id
)))),
11908 Declarations
=> Empty_List
,
11909 Handled_Statement_Sequence
=>
11910 Make_Handled_Sequence_Of_Statements
(Loc
,
11911 Statements
=> New_List
(
11912 Make_Raise_Program_Error
(Loc
,
11913 Reason
=> PE_Access_Before_Elaboration
))));
11917 Make_Raise_Program_Error
(Loc
,
11918 Reason
=> PE_Access_Before_Elaboration
);
11920 Set_Etype
(Ret_Expr
, (Etype
(Act_Decl_Id
)));
11921 Set_Analyzed
(Ret_Expr
);
11924 Make_Subprogram_Body
(Loc
,
11926 Make_Function_Specification
(Loc
,
11927 Defining_Unit_Name
=>
11928 Make_Defining_Identifier
(Loc
, Chars
(Act_Decl_Id
)),
11929 Parameter_Specifications
=>
11931 (Parameter_Specifications
(Parent
(Act_Decl_Id
))),
11932 Result_Definition
=>
11933 New_Occurrence_Of
(Etype
(Act_Decl_Id
), Loc
)),
11935 Declarations
=> Empty_List
,
11936 Handled_Statement_Sequence
=>
11937 Make_Handled_Sequence_Of_Statements
(Loc
,
11938 Statements
=> New_List
(
11939 Make_Simple_Return_Statement
(Loc
, Ret_Expr
))));
11943 Make_Package_Body
(Loc
,
11944 Defining_Unit_Name
=> New_Copy
(Pack_Id
),
11945 Declarations
=> New_List
(Act_Body
));
11947 Insert_After
(Inst_Node
, Pack_Body
);
11948 Set_Corresponding_Spec
(Pack_Body
, Pack_Id
);
11949 Analyze
(Pack_Body
);
11952 Expander_Mode_Restore
;
11955 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
11956 Restore_Ghost_Mode
(Saved_GM
);
11957 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
11958 Style_Check
:= Saved_Style_Check
;
11959 end Instantiate_Subprogram_Body
;
11961 ----------------------
11962 -- Instantiate_Type --
11963 ----------------------
11965 function Instantiate_Type
11968 Analyzed_Formal
: Node_Id
;
11969 Actual_Decls
: List_Id
) return List_Id
11971 A_Gen_T
: constant Entity_Id
:=
11972 Defining_Identifier
(Analyzed_Formal
);
11973 Def
: constant Node_Id
:= Formal_Type_Definition
(Formal
);
11974 Gen_T
: constant Entity_Id
:= Defining_Identifier
(Formal
);
11976 Ancestor
: Entity_Id
:= Empty
;
11977 Decl_Node
: Node_Id
;
11978 Decl_Nodes
: List_Id
;
11982 procedure Diagnose_Predicated_Actual
;
11983 -- There are a number of constructs in which a discrete type with
11984 -- predicates is illegal, e.g. as an index in an array type declaration.
11985 -- If a generic type is used is such a construct in a generic package
11986 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11987 -- of the generic contract that the actual cannot have predicates.
11989 procedure Validate_Array_Type_Instance
;
11990 procedure Validate_Access_Subprogram_Instance
;
11991 procedure Validate_Access_Type_Instance
;
11992 procedure Validate_Derived_Type_Instance
;
11993 procedure Validate_Derived_Interface_Type_Instance
;
11994 procedure Validate_Discriminated_Formal_Type
;
11995 procedure Validate_Interface_Type_Instance
;
11996 procedure Validate_Private_Type_Instance
;
11997 procedure Validate_Incomplete_Type_Instance
;
11998 -- These procedures perform validation tests for the named case.
11999 -- Validate_Discriminated_Formal_Type is shared by formal private
12000 -- types and Ada 2012 formal incomplete types.
12002 function Subtypes_Match
(Gen_T
, Act_T
: Entity_Id
) return Boolean;
12003 -- Check that base types are the same and that the subtypes match
12004 -- statically. Used in several of the above.
12006 ---------------------------------
12007 -- Diagnose_Predicated_Actual --
12008 ---------------------------------
12010 procedure Diagnose_Predicated_Actual
is
12012 if No_Predicate_On_Actual
(A_Gen_T
)
12013 and then Has_Predicates
(Act_T
)
12016 ("actual for& cannot be a type with predicate",
12017 Instantiation_Node
, A_Gen_T
);
12019 elsif No_Dynamic_Predicate_On_Actual
(A_Gen_T
)
12020 and then Has_Predicates
(Act_T
)
12021 and then not Has_Static_Predicate_Aspect
(Act_T
)
12024 ("actual for& cannot be a type with a dynamic predicate",
12025 Instantiation_Node
, A_Gen_T
);
12027 end Diagnose_Predicated_Actual
;
12029 --------------------
12030 -- Subtypes_Match --
12031 --------------------
12033 function Subtypes_Match
(Gen_T
, Act_T
: Entity_Id
) return Boolean is
12034 T
: constant Entity_Id
:= Get_Instance_Of
(Gen_T
);
12037 -- Some detailed comments would be useful here ???
12039 return ((Base_Type
(T
) = Act_T
12040 or else Base_Type
(T
) = Base_Type
(Act_T
))
12041 and then Subtypes_Statically_Match
(T
, Act_T
))
12043 or else (Is_Class_Wide_Type
(Gen_T
)
12044 and then Is_Class_Wide_Type
(Act_T
)
12045 and then Subtypes_Match
12046 (Get_Instance_Of
(Root_Type
(Gen_T
)),
12047 Root_Type
(Act_T
)))
12050 (Ekind_In
(Gen_T
, E_Anonymous_Access_Subprogram_Type
,
12051 E_Anonymous_Access_Type
)
12052 and then Ekind
(Act_T
) = Ekind
(Gen_T
)
12053 and then Subtypes_Statically_Match
12054 (Designated_Type
(Gen_T
), Designated_Type
(Act_T
)));
12055 end Subtypes_Match
;
12057 -----------------------------------------
12058 -- Validate_Access_Subprogram_Instance --
12059 -----------------------------------------
12061 procedure Validate_Access_Subprogram_Instance
is
12063 if not Is_Access_Type
(Act_T
)
12064 or else Ekind
(Designated_Type
(Act_T
)) /= E_Subprogram_Type
12067 ("expect access type in instantiation of &", Actual
, Gen_T
);
12068 Abandon_Instantiation
(Actual
);
12071 -- According to AI05-288, actuals for access_to_subprograms must be
12072 -- subtype conformant with the generic formal. Previous to AI05-288
12073 -- only mode conformance was required.
12075 -- This is a binding interpretation that applies to previous versions
12076 -- of the language, no need to maintain previous weaker checks.
12078 Check_Subtype_Conformant
12079 (Designated_Type
(Act_T
),
12080 Designated_Type
(A_Gen_T
),
12084 if Ekind
(Base_Type
(Act_T
)) = E_Access_Protected_Subprogram_Type
then
12085 if Ekind
(A_Gen_T
) = E_Access_Subprogram_Type
then
12087 ("protected access type not allowed for formal &",
12091 elsif Ekind
(A_Gen_T
) = E_Access_Protected_Subprogram_Type
then
12093 ("expect protected access type for formal &",
12097 -- If the formal has a specified convention (which in most cases
12098 -- will be StdCall) verify that the actual has the same convention.
12100 if Has_Convention_Pragma
(A_Gen_T
)
12101 and then Convention
(A_Gen_T
) /= Convention
(Act_T
)
12103 Error_Msg_Name_1
:= Get_Convention_Name
(Convention
(A_Gen_T
));
12105 ("actual for formal & must have convention %", Actual
, Gen_T
);
12107 end Validate_Access_Subprogram_Instance
;
12109 -----------------------------------
12110 -- Validate_Access_Type_Instance --
12111 -----------------------------------
12113 procedure Validate_Access_Type_Instance
is
12114 Desig_Type
: constant Entity_Id
:=
12115 Find_Actual_Type
(Designated_Type
(A_Gen_T
), A_Gen_T
);
12116 Desig_Act
: Entity_Id
;
12119 if not Is_Access_Type
(Act_T
) then
12121 ("expect access type in instantiation of &", Actual
, Gen_T
);
12122 Abandon_Instantiation
(Actual
);
12125 if Is_Access_Constant
(A_Gen_T
) then
12126 if not Is_Access_Constant
(Act_T
) then
12128 ("actual type must be access-to-constant type", Actual
);
12129 Abandon_Instantiation
(Actual
);
12132 if Is_Access_Constant
(Act_T
) then
12134 ("actual type must be access-to-variable type", Actual
);
12135 Abandon_Instantiation
(Actual
);
12137 elsif Ekind
(A_Gen_T
) = E_General_Access_Type
12138 and then Ekind
(Base_Type
(Act_T
)) /= E_General_Access_Type
12140 Error_Msg_N
-- CODEFIX
12141 ("actual must be general access type!", Actual
);
12142 Error_Msg_NE
-- CODEFIX
12143 ("add ALL to }!", Actual
, Act_T
);
12144 Abandon_Instantiation
(Actual
);
12148 -- The designated subtypes, that is to say the subtypes introduced
12149 -- by an access type declaration (and not by a subtype declaration)
12152 Desig_Act
:= Designated_Type
(Base_Type
(Act_T
));
12154 -- The designated type may have been introduced through a limited_
12155 -- with clause, in which case retrieve the non-limited view. This
12156 -- applies to incomplete types as well as to class-wide types.
12158 if From_Limited_With
(Desig_Act
) then
12159 Desig_Act
:= Available_View
(Desig_Act
);
12162 if not Subtypes_Match
(Desig_Type
, Desig_Act
) then
12164 ("designated type of actual does not match that of formal &",
12167 if not Predicates_Match
(Desig_Type
, Desig_Act
) then
12168 Error_Msg_N
("\predicates do not match", Actual
);
12171 Abandon_Instantiation
(Actual
);
12173 elsif Is_Access_Type
(Designated_Type
(Act_T
))
12174 and then Is_Constrained
(Designated_Type
(Designated_Type
(Act_T
)))
12176 Is_Constrained
(Designated_Type
(Desig_Type
))
12179 ("designated type of actual does not match that of formal &",
12182 if not Predicates_Match
(Desig_Type
, Desig_Act
) then
12183 Error_Msg_N
("\predicates do not match", Actual
);
12186 Abandon_Instantiation
(Actual
);
12189 -- Ada 2005: null-exclusion indicators of the two types must agree
12191 if Can_Never_Be_Null
(A_Gen_T
) /= Can_Never_Be_Null
(Act_T
) then
12193 ("non null exclusion of actual and formal & do not match",
12196 end Validate_Access_Type_Instance
;
12198 ----------------------------------
12199 -- Validate_Array_Type_Instance --
12200 ----------------------------------
12202 procedure Validate_Array_Type_Instance
is
12207 function Formal_Dimensions
return Nat
;
12208 -- Count number of dimensions in array type formal
12210 -----------------------
12211 -- Formal_Dimensions --
12212 -----------------------
12214 function Formal_Dimensions
return Nat
is
12219 if Nkind
(Def
) = N_Constrained_Array_Definition
then
12220 Index
:= First
(Discrete_Subtype_Definitions
(Def
));
12222 Index
:= First
(Subtype_Marks
(Def
));
12225 while Present
(Index
) loop
12227 Next_Index
(Index
);
12231 end Formal_Dimensions
;
12233 -- Start of processing for Validate_Array_Type_Instance
12236 if not Is_Array_Type
(Act_T
) then
12238 ("expect array type in instantiation of &", Actual
, Gen_T
);
12239 Abandon_Instantiation
(Actual
);
12241 elsif Nkind
(Def
) = N_Constrained_Array_Definition
then
12242 if not (Is_Constrained
(Act_T
)) then
12244 ("expect constrained array in instantiation of &",
12246 Abandon_Instantiation
(Actual
);
12250 if Is_Constrained
(Act_T
) then
12252 ("expect unconstrained array in instantiation of &",
12254 Abandon_Instantiation
(Actual
);
12258 if Formal_Dimensions
/= Number_Dimensions
(Act_T
) then
12260 ("dimensions of actual do not match formal &", Actual
, Gen_T
);
12261 Abandon_Instantiation
(Actual
);
12264 I1
:= First_Index
(A_Gen_T
);
12265 I2
:= First_Index
(Act_T
);
12266 for J
in 1 .. Formal_Dimensions
loop
12268 -- If the indexes of the actual were given by a subtype_mark,
12269 -- the index was transformed into a range attribute. Retrieve
12270 -- the original type mark for checking.
12272 if Is_Entity_Name
(Original_Node
(I2
)) then
12273 T2
:= Entity
(Original_Node
(I2
));
12278 if not Subtypes_Match
12279 (Find_Actual_Type
(Etype
(I1
), A_Gen_T
), T2
)
12282 ("index types of actual do not match those of formal &",
12284 Abandon_Instantiation
(Actual
);
12291 -- Check matching subtypes. Note that there are complex visibility
12292 -- issues when the generic is a child unit and some aspect of the
12293 -- generic type is declared in a parent unit of the generic. We do
12294 -- the test to handle this special case only after a direct check
12295 -- for static matching has failed. The case where both the component
12296 -- type and the array type are separate formals, and the component
12297 -- type is a private view may also require special checking in
12298 -- Subtypes_Match. Finally, we assume that a child instance where
12299 -- the component type comes from a formal of a parent instance is
12300 -- correct because the generic was correct. A more precise check
12301 -- seems too complex to install???
12304 (Component_Type
(A_Gen_T
), Component_Type
(Act_T
))
12307 (Find_Actual_Type
(Component_Type
(A_Gen_T
), A_Gen_T
),
12308 Component_Type
(Act_T
))
12310 (not Inside_A_Generic
12311 and then Is_Child_Unit
(Scope
(Component_Type
(A_Gen_T
))))
12316 ("component subtype of actual does not match that of formal &",
12318 Abandon_Instantiation
(Actual
);
12321 if Has_Aliased_Components
(A_Gen_T
)
12322 and then not Has_Aliased_Components
(Act_T
)
12325 ("actual must have aliased components to match formal type &",
12328 end Validate_Array_Type_Instance
;
12330 -----------------------------------------------
12331 -- Validate_Derived_Interface_Type_Instance --
12332 -----------------------------------------------
12334 procedure Validate_Derived_Interface_Type_Instance
is
12335 Par
: constant Entity_Id
:= Entity
(Subtype_Indication
(Def
));
12339 -- First apply interface instance checks
12341 Validate_Interface_Type_Instance
;
12343 -- Verify that immediate parent interface is an ancestor of
12347 and then not Interface_Present_In_Ancestor
(Act_T
, Par
)
12350 ("interface actual must include progenitor&", Actual
, Par
);
12353 -- Now verify that the actual includes all other ancestors of
12356 Elmt
:= First_Elmt
(Interfaces
(A_Gen_T
));
12357 while Present
(Elmt
) loop
12358 if not Interface_Present_In_Ancestor
12359 (Act_T
, Get_Instance_Of
(Node
(Elmt
)))
12362 ("interface actual must include progenitor&",
12363 Actual
, Node
(Elmt
));
12368 end Validate_Derived_Interface_Type_Instance
;
12370 ------------------------------------
12371 -- Validate_Derived_Type_Instance --
12372 ------------------------------------
12374 procedure Validate_Derived_Type_Instance
is
12375 Actual_Discr
: Entity_Id
;
12376 Ancestor_Discr
: Entity_Id
;
12379 -- If the parent type in the generic declaration is itself a previous
12380 -- formal type, then it is local to the generic and absent from the
12381 -- analyzed generic definition. In that case the ancestor is the
12382 -- instance of the formal (which must have been instantiated
12383 -- previously), unless the ancestor is itself a formal derived type.
12384 -- In this latter case (which is the subject of Corrigendum 8652/0038
12385 -- (AI-202) the ancestor of the formals is the ancestor of its
12386 -- parent. Otherwise, the analyzed generic carries the parent type.
12387 -- If the parent type is defined in a previous formal package, then
12388 -- the scope of that formal package is that of the generic type
12389 -- itself, and it has already been mapped into the corresponding type
12390 -- in the actual package.
12392 -- Common case: parent type defined outside of the generic
12394 if Is_Entity_Name
(Subtype_Mark
(Def
))
12395 and then Present
(Entity
(Subtype_Mark
(Def
)))
12397 Ancestor
:= Get_Instance_Of
(Entity
(Subtype_Mark
(Def
)));
12399 -- Check whether parent is defined in a previous formal package
12402 Scope
(Scope
(Base_Type
(Etype
(A_Gen_T
)))) = Scope
(A_Gen_T
)
12405 Get_Instance_Of
(Base_Type
(Etype
(A_Gen_T
)));
12407 -- The type may be a local derivation, or a type extension of a
12408 -- previous formal, or of a formal of a parent package.
12410 elsif Is_Derived_Type
(Get_Instance_Of
(A_Gen_T
))
12412 Ekind
(Get_Instance_Of
(A_Gen_T
)) = E_Record_Type_With_Private
12414 -- Check whether the parent is another derived formal type in the
12415 -- same generic unit.
12417 if Etype
(A_Gen_T
) /= A_Gen_T
12418 and then Is_Generic_Type
(Etype
(A_Gen_T
))
12419 and then Scope
(Etype
(A_Gen_T
)) = Scope
(A_Gen_T
)
12420 and then Etype
(Etype
(A_Gen_T
)) /= Etype
(A_Gen_T
)
12422 -- Locate ancestor of parent from the subtype declaration
12423 -- created for the actual.
12429 Decl
:= First
(Actual_Decls
);
12430 while Present
(Decl
) loop
12431 if Nkind
(Decl
) = N_Subtype_Declaration
12432 and then Chars
(Defining_Identifier
(Decl
)) =
12433 Chars
(Etype
(A_Gen_T
))
12435 Ancestor
:= Generic_Parent_Type
(Decl
);
12443 pragma Assert
(Present
(Ancestor
));
12445 -- The ancestor itself may be a previous formal that has been
12448 Ancestor
:= Get_Instance_Of
(Ancestor
);
12452 Get_Instance_Of
(Base_Type
(Get_Instance_Of
(A_Gen_T
)));
12455 -- Check whether parent is a previous formal of the current generic
12457 elsif Is_Derived_Type
(A_Gen_T
)
12458 and then Is_Generic_Type
(Etype
(A_Gen_T
))
12459 and then Scope
(A_Gen_T
) = Scope
(Etype
(A_Gen_T
))
12461 Ancestor
:= Get_Instance_Of
(First_Subtype
(Etype
(A_Gen_T
)));
12463 -- An unusual case: the actual is a type declared in a parent unit,
12464 -- but is not a formal type so there is no instance_of for it.
12465 -- Retrieve it by analyzing the record extension.
12467 elsif Is_Child_Unit
(Scope
(A_Gen_T
))
12468 and then In_Open_Scopes
(Scope
(Act_T
))
12469 and then Is_Generic_Instance
(Scope
(Act_T
))
12471 Analyze
(Subtype_Mark
(Def
));
12472 Ancestor
:= Entity
(Subtype_Mark
(Def
));
12475 Ancestor
:= Get_Instance_Of
(Etype
(Base_Type
(A_Gen_T
)));
12478 -- If the formal derived type has pragma Preelaborable_Initialization
12479 -- then the actual type must have preelaborable initialization.
12481 if Known_To_Have_Preelab_Init
(A_Gen_T
)
12482 and then not Has_Preelaborable_Initialization
(Act_T
)
12485 ("actual for & must have preelaborable initialization",
12489 -- Ada 2005 (AI-251)
12491 if Ada_Version
>= Ada_2005
and then Is_Interface
(Ancestor
) then
12492 if not Interface_Present_In_Ancestor
(Act_T
, Ancestor
) then
12494 ("(Ada 2005) expected type implementing & in instantiation",
12498 -- Finally verify that the (instance of) the ancestor is an ancestor
12501 elsif not Is_Ancestor
(Base_Type
(Ancestor
), Act_T
) then
12503 ("expect type derived from & in instantiation",
12504 Actual
, First_Subtype
(Ancestor
));
12505 Abandon_Instantiation
(Actual
);
12508 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
12509 -- that the formal type declaration has been rewritten as a private
12512 if Ada_Version
>= Ada_2005
12513 and then Nkind
(Parent
(A_Gen_T
)) = N_Private_Extension_Declaration
12514 and then Synchronized_Present
(Parent
(A_Gen_T
))
12516 -- The actual must be a synchronized tagged type
12518 if not Is_Tagged_Type
(Act_T
) then
12520 ("actual of synchronized type must be tagged", Actual
);
12521 Abandon_Instantiation
(Actual
);
12523 elsif Nkind
(Parent
(Act_T
)) = N_Full_Type_Declaration
12524 and then Nkind
(Type_Definition
(Parent
(Act_T
))) =
12525 N_Derived_Type_Definition
12526 and then not Synchronized_Present
12527 (Type_Definition
(Parent
(Act_T
)))
12530 ("actual of synchronized type must be synchronized", Actual
);
12531 Abandon_Instantiation
(Actual
);
12535 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
12536 -- removes the second instance of the phrase "or allow pass by copy".
12538 if Is_Atomic
(Act_T
) and then not Is_Atomic
(Ancestor
) then
12540 ("cannot have atomic actual type for non-atomic formal type",
12543 elsif Is_Volatile
(Act_T
) and then not Is_Volatile
(Ancestor
) then
12545 ("cannot have volatile actual type for non-volatile formal type",
12549 -- It should not be necessary to check for unknown discriminants on
12550 -- Formal, but for some reason Has_Unknown_Discriminants is false for
12551 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
12552 -- needs fixing. ???
12554 if Is_Definite_Subtype
(A_Gen_T
)
12555 and then not Unknown_Discriminants_Present
(Formal
)
12556 and then not Is_Definite_Subtype
(Act_T
)
12558 Error_Msg_N
("actual subtype must be constrained", Actual
);
12559 Abandon_Instantiation
(Actual
);
12562 if not Unknown_Discriminants_Present
(Formal
) then
12563 if Is_Constrained
(Ancestor
) then
12564 if not Is_Constrained
(Act_T
) then
12565 Error_Msg_N
("actual subtype must be constrained", Actual
);
12566 Abandon_Instantiation
(Actual
);
12569 -- Ancestor is unconstrained, Check if generic formal and actual
12570 -- agree on constrainedness. The check only applies to array types
12571 -- and discriminated types.
12573 elsif Is_Constrained
(Act_T
) then
12574 if Ekind
(Ancestor
) = E_Access_Type
12575 or else (not Is_Constrained
(A_Gen_T
)
12576 and then Is_Composite_Type
(A_Gen_T
))
12578 Error_Msg_N
("actual subtype must be unconstrained", Actual
);
12579 Abandon_Instantiation
(Actual
);
12582 -- A class-wide type is only allowed if the formal has unknown
12585 elsif Is_Class_Wide_Type
(Act_T
)
12586 and then not Has_Unknown_Discriminants
(Ancestor
)
12589 ("actual for & cannot be a class-wide type", Actual
, Gen_T
);
12590 Abandon_Instantiation
(Actual
);
12592 -- Otherwise, the formal and actual must have the same number
12593 -- of discriminants and each discriminant of the actual must
12594 -- correspond to a discriminant of the formal.
12596 elsif Has_Discriminants
(Act_T
)
12597 and then not Has_Unknown_Discriminants
(Act_T
)
12598 and then Has_Discriminants
(Ancestor
)
12600 Actual_Discr
:= First_Discriminant
(Act_T
);
12601 Ancestor_Discr
:= First_Discriminant
(Ancestor
);
12602 while Present
(Actual_Discr
)
12603 and then Present
(Ancestor_Discr
)
12605 if Base_Type
(Act_T
) /= Base_Type
(Ancestor
) and then
12606 No
(Corresponding_Discriminant
(Actual_Discr
))
12609 ("discriminant & does not correspond "
12610 & "to ancestor discriminant", Actual
, Actual_Discr
);
12611 Abandon_Instantiation
(Actual
);
12614 Next_Discriminant
(Actual_Discr
);
12615 Next_Discriminant
(Ancestor_Discr
);
12618 if Present
(Actual_Discr
) or else Present
(Ancestor_Discr
) then
12620 ("actual for & must have same number of discriminants",
12622 Abandon_Instantiation
(Actual
);
12625 -- This case should be caught by the earlier check for
12626 -- constrainedness, but the check here is added for completeness.
12628 elsif Has_Discriminants
(Act_T
)
12629 and then not Has_Unknown_Discriminants
(Act_T
)
12632 ("actual for & must not have discriminants", Actual
, Gen_T
);
12633 Abandon_Instantiation
(Actual
);
12635 elsif Has_Discriminants
(Ancestor
) then
12637 ("actual for & must have known discriminants", Actual
, Gen_T
);
12638 Abandon_Instantiation
(Actual
);
12641 if not Subtypes_Statically_Compatible
12642 (Act_T
, Ancestor
, Formal_Derived_Matching
=> True)
12645 ("constraint on actual is incompatible with formal", Actual
);
12646 Abandon_Instantiation
(Actual
);
12650 -- If the formal and actual types are abstract, check that there
12651 -- are no abstract primitives of the actual type that correspond to
12652 -- nonabstract primitives of the formal type (second sentence of
12655 if Is_Abstract_Type
(A_Gen_T
) and then Is_Abstract_Type
(Act_T
) then
12656 Check_Abstract_Primitives
: declare
12657 Gen_Prims
: constant Elist_Id
:=
12658 Primitive_Operations
(A_Gen_T
);
12659 Gen_Elmt
: Elmt_Id
;
12660 Gen_Subp
: Entity_Id
;
12661 Anc_Subp
: Entity_Id
;
12662 Anc_Formal
: Entity_Id
;
12663 Anc_F_Type
: Entity_Id
;
12665 Act_Prims
: constant Elist_Id
:= Primitive_Operations
(Act_T
);
12666 Act_Elmt
: Elmt_Id
;
12667 Act_Subp
: Entity_Id
;
12668 Act_Formal
: Entity_Id
;
12669 Act_F_Type
: Entity_Id
;
12671 Subprograms_Correspond
: Boolean;
12673 function Is_Tagged_Ancestor
(T1
, T2
: Entity_Id
) return Boolean;
12674 -- Returns true if T2 is derived directly or indirectly from
12675 -- T1, including derivations from interfaces. T1 and T2 are
12676 -- required to be specific tagged base types.
12678 ------------------------
12679 -- Is_Tagged_Ancestor --
12680 ------------------------
12682 function Is_Tagged_Ancestor
(T1
, T2
: Entity_Id
) return Boolean
12684 Intfc_Elmt
: Elmt_Id
;
12687 -- The predicate is satisfied if the types are the same
12692 -- If we've reached the top of the derivation chain then
12693 -- we know that T1 is not an ancestor of T2.
12695 elsif Etype
(T2
) = T2
then
12698 -- Proceed to check T2's immediate parent
12700 elsif Is_Ancestor
(T1
, Base_Type
(Etype
(T2
))) then
12703 -- Finally, check to see if T1 is an ancestor of any of T2's
12707 Intfc_Elmt
:= First_Elmt
(Interfaces
(T2
));
12708 while Present
(Intfc_Elmt
) loop
12709 if Is_Ancestor
(T1
, Node
(Intfc_Elmt
)) then
12713 Next_Elmt
(Intfc_Elmt
);
12718 end Is_Tagged_Ancestor
;
12720 -- Start of processing for Check_Abstract_Primitives
12723 -- Loop over all of the formal derived type's primitives
12725 Gen_Elmt
:= First_Elmt
(Gen_Prims
);
12726 while Present
(Gen_Elmt
) loop
12727 Gen_Subp
:= Node
(Gen_Elmt
);
12729 -- If the primitive of the formal is not abstract, then
12730 -- determine whether there is a corresponding primitive of
12731 -- the actual type that's abstract.
12733 if not Is_Abstract_Subprogram
(Gen_Subp
) then
12734 Act_Elmt
:= First_Elmt
(Act_Prims
);
12735 while Present
(Act_Elmt
) loop
12736 Act_Subp
:= Node
(Act_Elmt
);
12738 -- If we find an abstract primitive of the actual,
12739 -- then we need to test whether it corresponds to the
12740 -- subprogram from which the generic formal primitive
12743 if Is_Abstract_Subprogram
(Act_Subp
) then
12744 Anc_Subp
:= Alias
(Gen_Subp
);
12746 -- Test whether we have a corresponding primitive
12747 -- by comparing names, kinds, formal types, and
12750 if Chars
(Anc_Subp
) = Chars
(Act_Subp
)
12751 and then Ekind
(Anc_Subp
) = Ekind
(Act_Subp
)
12753 Anc_Formal
:= First_Formal
(Anc_Subp
);
12754 Act_Formal
:= First_Formal
(Act_Subp
);
12755 while Present
(Anc_Formal
)
12756 and then Present
(Act_Formal
)
12758 Anc_F_Type
:= Etype
(Anc_Formal
);
12759 Act_F_Type
:= Etype
(Act_Formal
);
12761 if Ekind
(Anc_F_Type
) =
12762 E_Anonymous_Access_Type
12764 Anc_F_Type
:= Designated_Type
(Anc_F_Type
);
12766 if Ekind
(Act_F_Type
) =
12767 E_Anonymous_Access_Type
12770 Designated_Type
(Act_F_Type
);
12776 Ekind
(Act_F_Type
) = E_Anonymous_Access_Type
12781 Anc_F_Type
:= Base_Type
(Anc_F_Type
);
12782 Act_F_Type
:= Base_Type
(Act_F_Type
);
12784 -- If the formal is controlling, then the
12785 -- the type of the actual primitive's formal
12786 -- must be derived directly or indirectly
12787 -- from the type of the ancestor primitive's
12790 if Is_Controlling_Formal
(Anc_Formal
) then
12791 if not Is_Tagged_Ancestor
12792 (Anc_F_Type
, Act_F_Type
)
12797 -- Otherwise the types of the formals must
12800 elsif Anc_F_Type
/= Act_F_Type
then
12804 Next_Entity
(Anc_Formal
);
12805 Next_Entity
(Act_Formal
);
12808 -- If we traversed through all of the formals
12809 -- then so far the subprograms correspond, so
12810 -- now check that any result types correspond.
12812 if No
(Anc_Formal
) and then No
(Act_Formal
) then
12813 Subprograms_Correspond
:= True;
12815 if Ekind
(Act_Subp
) = E_Function
then
12816 Anc_F_Type
:= Etype
(Anc_Subp
);
12817 Act_F_Type
:= Etype
(Act_Subp
);
12819 if Ekind
(Anc_F_Type
) =
12820 E_Anonymous_Access_Type
12823 Designated_Type
(Anc_F_Type
);
12825 if Ekind
(Act_F_Type
) =
12826 E_Anonymous_Access_Type
12829 Designated_Type
(Act_F_Type
);
12831 Subprograms_Correspond
:= False;
12836 = E_Anonymous_Access_Type
12838 Subprograms_Correspond
:= False;
12841 Anc_F_Type
:= Base_Type
(Anc_F_Type
);
12842 Act_F_Type
:= Base_Type
(Act_F_Type
);
12844 -- Now either the result types must be
12845 -- the same or, if the result type is
12846 -- controlling, the result type of the
12847 -- actual primitive must descend from the
12848 -- result type of the ancestor primitive.
12850 if Subprograms_Correspond
12851 and then Anc_F_Type
/= Act_F_Type
12853 Has_Controlling_Result
(Anc_Subp
)
12854 and then not Is_Tagged_Ancestor
12855 (Anc_F_Type
, Act_F_Type
)
12857 Subprograms_Correspond
:= False;
12861 -- Found a matching subprogram belonging to
12862 -- formal ancestor type, so actual subprogram
12863 -- corresponds and this violates 3.9.3(9).
12865 if Subprograms_Correspond
then
12867 ("abstract subprogram & overrides "
12868 & "nonabstract subprogram of ancestor",
12875 Next_Elmt
(Act_Elmt
);
12879 Next_Elmt
(Gen_Elmt
);
12881 end Check_Abstract_Primitives
;
12884 -- Verify that limitedness matches. If parent is a limited
12885 -- interface then the generic formal is not unless declared
12886 -- explicitly so. If not declared limited, the actual cannot be
12887 -- limited (see AI05-0087).
12889 -- Even though this AI is a binding interpretation, we enable the
12890 -- check only in Ada 2012 mode, because this improper construct
12891 -- shows up in user code and in existing B-tests.
12893 if Is_Limited_Type
(Act_T
)
12894 and then not Is_Limited_Type
(A_Gen_T
)
12895 and then Ada_Version
>= Ada_2012
12897 if In_Instance
then
12901 ("actual for non-limited & cannot be a limited type",
12903 Explain_Limited_Type
(Act_T
, Actual
);
12904 Abandon_Instantiation
(Actual
);
12907 end Validate_Derived_Type_Instance
;
12909 ----------------------------------------
12910 -- Validate_Discriminated_Formal_Type --
12911 ----------------------------------------
12913 procedure Validate_Discriminated_Formal_Type
is
12914 Formal_Discr
: Entity_Id
;
12915 Actual_Discr
: Entity_Id
;
12916 Formal_Subt
: Entity_Id
;
12919 if Has_Discriminants
(A_Gen_T
) then
12920 if not Has_Discriminants
(Act_T
) then
12922 ("actual for & must have discriminants", Actual
, Gen_T
);
12923 Abandon_Instantiation
(Actual
);
12925 elsif Is_Constrained
(Act_T
) then
12927 ("actual for & must be unconstrained", Actual
, Gen_T
);
12928 Abandon_Instantiation
(Actual
);
12931 Formal_Discr
:= First_Discriminant
(A_Gen_T
);
12932 Actual_Discr
:= First_Discriminant
(Act_T
);
12933 while Formal_Discr
/= Empty
loop
12934 if Actual_Discr
= Empty
then
12936 ("discriminants on actual do not match formal",
12938 Abandon_Instantiation
(Actual
);
12941 Formal_Subt
:= Get_Instance_Of
(Etype
(Formal_Discr
));
12943 -- Access discriminants match if designated types do
12945 if Ekind
(Base_Type
(Formal_Subt
)) = E_Anonymous_Access_Type
12946 and then (Ekind
(Base_Type
(Etype
(Actual_Discr
)))) =
12947 E_Anonymous_Access_Type
12950 (Designated_Type
(Base_Type
(Formal_Subt
))) =
12951 Designated_Type
(Base_Type
(Etype
(Actual_Discr
)))
12955 elsif Base_Type
(Formal_Subt
) /=
12956 Base_Type
(Etype
(Actual_Discr
))
12959 ("types of actual discriminants must match formal",
12961 Abandon_Instantiation
(Actual
);
12963 elsif not Subtypes_Statically_Match
12964 (Formal_Subt
, Etype
(Actual_Discr
))
12965 and then Ada_Version
>= Ada_95
12968 ("subtypes of actual discriminants must match formal",
12970 Abandon_Instantiation
(Actual
);
12973 Next_Discriminant
(Formal_Discr
);
12974 Next_Discriminant
(Actual_Discr
);
12977 if Actual_Discr
/= Empty
then
12979 ("discriminants on actual do not match formal",
12981 Abandon_Instantiation
(Actual
);
12985 end Validate_Discriminated_Formal_Type
;
12987 ---------------------------------------
12988 -- Validate_Incomplete_Type_Instance --
12989 ---------------------------------------
12991 procedure Validate_Incomplete_Type_Instance
is
12993 if not Is_Tagged_Type
(Act_T
)
12994 and then Is_Tagged_Type
(A_Gen_T
)
12997 ("actual for & must be a tagged type", Actual
, Gen_T
);
13000 Validate_Discriminated_Formal_Type
;
13001 end Validate_Incomplete_Type_Instance
;
13003 --------------------------------------
13004 -- Validate_Interface_Type_Instance --
13005 --------------------------------------
13007 procedure Validate_Interface_Type_Instance
is
13009 if not Is_Interface
(Act_T
) then
13011 ("actual for formal interface type must be an interface",
13014 elsif Is_Limited_Type
(Act_T
) /= Is_Limited_Type
(A_Gen_T
)
13015 or else Is_Task_Interface
(A_Gen_T
) /= Is_Task_Interface
(Act_T
)
13016 or else Is_Protected_Interface
(A_Gen_T
) /=
13017 Is_Protected_Interface
(Act_T
)
13018 or else Is_Synchronized_Interface
(A_Gen_T
) /=
13019 Is_Synchronized_Interface
(Act_T
)
13022 ("actual for interface& does not match (RM 12.5.5(4))",
13025 end Validate_Interface_Type_Instance
;
13027 ------------------------------------
13028 -- Validate_Private_Type_Instance --
13029 ------------------------------------
13031 procedure Validate_Private_Type_Instance
is
13033 if Is_Limited_Type
(Act_T
)
13034 and then not Is_Limited_Type
(A_Gen_T
)
13036 if In_Instance
then
13040 ("actual for non-limited & cannot be a limited type", Actual
,
13042 Explain_Limited_Type
(Act_T
, Actual
);
13043 Abandon_Instantiation
(Actual
);
13046 elsif Known_To_Have_Preelab_Init
(A_Gen_T
)
13047 and then not Has_Preelaborable_Initialization
(Act_T
)
13050 ("actual for & must have preelaborable initialization", Actual
,
13053 elsif not Is_Definite_Subtype
(Act_T
)
13054 and then Is_Definite_Subtype
(A_Gen_T
)
13055 and then Ada_Version
>= Ada_95
13058 ("actual for & must be a definite subtype", Actual
, Gen_T
);
13060 elsif not Is_Tagged_Type
(Act_T
)
13061 and then Is_Tagged_Type
(A_Gen_T
)
13064 ("actual for & must be a tagged type", Actual
, Gen_T
);
13067 Validate_Discriminated_Formal_Type
;
13069 end Validate_Private_Type_Instance
;
13071 -- Start of processing for Instantiate_Type
13074 if Get_Instance_Of
(A_Gen_T
) /= A_Gen_T
then
13075 Error_Msg_N
("duplicate instantiation of generic type", Actual
);
13076 return New_List
(Error
);
13078 elsif not Is_Entity_Name
(Actual
)
13079 or else not Is_Type
(Entity
(Actual
))
13082 ("expect valid subtype mark to instantiate &", Actual
, Gen_T
);
13083 Abandon_Instantiation
(Actual
);
13086 Act_T
:= Entity
(Actual
);
13088 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
13089 -- as a generic actual parameter if the corresponding formal type
13090 -- does not have a known_discriminant_part, or is a formal derived
13091 -- type that is an Unchecked_Union type.
13093 if Is_Unchecked_Union
(Base_Type
(Act_T
)) then
13094 if not Has_Discriminants
(A_Gen_T
)
13095 or else (Is_Derived_Type
(A_Gen_T
)
13096 and then Is_Unchecked_Union
(A_Gen_T
))
13100 Error_Msg_N
("unchecked union cannot be the actual for a "
13101 & "discriminated formal type", Act_T
);
13106 -- Deal with fixed/floating restrictions
13108 if Is_Floating_Point_Type
(Act_T
) then
13109 Check_Restriction
(No_Floating_Point
, Actual
);
13110 elsif Is_Fixed_Point_Type
(Act_T
) then
13111 Check_Restriction
(No_Fixed_Point
, Actual
);
13114 -- Deal with error of using incomplete type as generic actual.
13115 -- This includes limited views of a type, even if the non-limited
13116 -- view may be available.
13118 if Ekind
(Act_T
) = E_Incomplete_Type
13119 or else (Is_Class_Wide_Type
(Act_T
)
13120 and then Ekind
(Root_Type
(Act_T
)) = E_Incomplete_Type
)
13122 -- If the formal is an incomplete type, the actual can be
13123 -- incomplete as well.
13125 if Ekind
(A_Gen_T
) = E_Incomplete_Type
then
13128 elsif Is_Class_Wide_Type
(Act_T
)
13129 or else No
(Full_View
(Act_T
))
13131 Error_Msg_N
("premature use of incomplete type", Actual
);
13132 Abandon_Instantiation
(Actual
);
13134 Act_T
:= Full_View
(Act_T
);
13135 Set_Entity
(Actual
, Act_T
);
13137 if Has_Private_Component
(Act_T
) then
13139 ("premature use of type with private component", Actual
);
13143 -- Deal with error of premature use of private type as generic actual
13145 elsif Is_Private_Type
(Act_T
)
13146 and then Is_Private_Type
(Base_Type
(Act_T
))
13147 and then not Is_Generic_Type
(Act_T
)
13148 and then not Is_Derived_Type
(Act_T
)
13149 and then No
(Full_View
(Root_Type
(Act_T
)))
13151 -- If the formal is an incomplete type, the actual can be
13152 -- private or incomplete as well.
13154 if Ekind
(A_Gen_T
) = E_Incomplete_Type
then
13157 Error_Msg_N
("premature use of private type", Actual
);
13160 elsif Has_Private_Component
(Act_T
) then
13162 ("premature use of type with private component", Actual
);
13165 Set_Instance_Of
(A_Gen_T
, Act_T
);
13167 -- If the type is generic, the class-wide type may also be used
13169 if Is_Tagged_Type
(A_Gen_T
)
13170 and then Is_Tagged_Type
(Act_T
)
13171 and then not Is_Class_Wide_Type
(A_Gen_T
)
13173 Set_Instance_Of
(Class_Wide_Type
(A_Gen_T
),
13174 Class_Wide_Type
(Act_T
));
13177 if not Is_Abstract_Type
(A_Gen_T
)
13178 and then Is_Abstract_Type
(Act_T
)
13181 ("actual of non-abstract formal cannot be abstract", Actual
);
13184 -- A generic scalar type is a first subtype for which we generate
13185 -- an anonymous base type. Indicate that the instance of this base
13186 -- is the base type of the actual.
13188 if Is_Scalar_Type
(A_Gen_T
) then
13189 Set_Instance_Of
(Etype
(A_Gen_T
), Etype
(Act_T
));
13193 if Error_Posted
(Act_T
) then
13196 case Nkind
(Def
) is
13197 when N_Formal_Private_Type_Definition
=>
13198 Validate_Private_Type_Instance
;
13200 when N_Formal_Incomplete_Type_Definition
=>
13201 Validate_Incomplete_Type_Instance
;
13203 when N_Formal_Derived_Type_Definition
=>
13204 Validate_Derived_Type_Instance
;
13206 when N_Formal_Discrete_Type_Definition
=>
13207 if not Is_Discrete_Type
(Act_T
) then
13209 ("expect discrete type in instantiation of&",
13211 Abandon_Instantiation
(Actual
);
13214 Diagnose_Predicated_Actual
;
13216 when N_Formal_Signed_Integer_Type_Definition
=>
13217 if not Is_Signed_Integer_Type
(Act_T
) then
13219 ("expect signed integer type in instantiation of&",
13221 Abandon_Instantiation
(Actual
);
13224 Diagnose_Predicated_Actual
;
13226 when N_Formal_Modular_Type_Definition
=>
13227 if not Is_Modular_Integer_Type
(Act_T
) then
13229 ("expect modular type in instantiation of &",
13231 Abandon_Instantiation
(Actual
);
13234 Diagnose_Predicated_Actual
;
13236 when N_Formal_Floating_Point_Definition
=>
13237 if not Is_Floating_Point_Type
(Act_T
) then
13239 ("expect float type in instantiation of &", Actual
, Gen_T
);
13240 Abandon_Instantiation
(Actual
);
13243 when N_Formal_Ordinary_Fixed_Point_Definition
=>
13244 if not Is_Ordinary_Fixed_Point_Type
(Act_T
) then
13246 ("expect ordinary fixed point type in instantiation of &",
13248 Abandon_Instantiation
(Actual
);
13251 when N_Formal_Decimal_Fixed_Point_Definition
=>
13252 if not Is_Decimal_Fixed_Point_Type
(Act_T
) then
13254 ("expect decimal type in instantiation of &",
13256 Abandon_Instantiation
(Actual
);
13259 when N_Array_Type_Definition
=>
13260 Validate_Array_Type_Instance
;
13262 when N_Access_To_Object_Definition
=>
13263 Validate_Access_Type_Instance
;
13265 when N_Access_Function_Definition
13266 | N_Access_Procedure_Definition
13268 Validate_Access_Subprogram_Instance
;
13270 when N_Record_Definition
=>
13271 Validate_Interface_Type_Instance
;
13273 when N_Derived_Type_Definition
=>
13274 Validate_Derived_Interface_Type_Instance
;
13277 raise Program_Error
;
13281 Subt
:= New_Copy
(Gen_T
);
13283 -- Use adjusted sloc of subtype name as the location for other nodes in
13284 -- the subtype declaration.
13286 Loc
:= Sloc
(Subt
);
13289 Make_Subtype_Declaration
(Loc
,
13290 Defining_Identifier
=> Subt
,
13291 Subtype_Indication
=> New_Occurrence_Of
(Act_T
, Loc
));
13293 if Is_Private_Type
(Act_T
) then
13294 Set_Has_Private_View
(Subtype_Indication
(Decl_Node
));
13296 elsif Is_Access_Type
(Act_T
)
13297 and then Is_Private_Type
(Designated_Type
(Act_T
))
13299 Set_Has_Private_View
(Subtype_Indication
(Decl_Node
));
13302 -- In Ada 2012 the actual may be a limited view. Indicate that
13303 -- the local subtype must be treated as such.
13305 if From_Limited_With
(Act_T
) then
13306 Set_Ekind
(Subt
, E_Incomplete_Subtype
);
13307 Set_From_Limited_With
(Subt
);
13310 Decl_Nodes
:= New_List
(Decl_Node
);
13312 -- Flag actual derived types so their elaboration produces the
13313 -- appropriate renamings for the primitive operations of the ancestor.
13314 -- Flag actual for formal private types as well, to determine whether
13315 -- operations in the private part may override inherited operations.
13316 -- If the formal has an interface list, the ancestor is not the
13317 -- parent, but the analyzed formal that includes the interface
13318 -- operations of all its progenitors.
13320 -- Same treatment for formal private types, so we can check whether the
13321 -- type is tagged limited when validating derivations in the private
13322 -- part. (See AI05-096).
13324 if Nkind
(Def
) = N_Formal_Derived_Type_Definition
then
13325 if Present
(Interface_List
(Def
)) then
13326 Set_Generic_Parent_Type
(Decl_Node
, A_Gen_T
);
13328 Set_Generic_Parent_Type
(Decl_Node
, Ancestor
);
13331 elsif Nkind_In
(Def
, N_Formal_Private_Type_Definition
,
13332 N_Formal_Incomplete_Type_Definition
)
13334 Set_Generic_Parent_Type
(Decl_Node
, A_Gen_T
);
13337 -- If the actual is a synchronized type that implements an interface,
13338 -- the primitive operations are attached to the corresponding record,
13339 -- and we have to treat it as an additional generic actual, so that its
13340 -- primitive operations become visible in the instance. The task or
13341 -- protected type itself does not carry primitive operations.
13343 if Is_Concurrent_Type
(Act_T
)
13344 and then Is_Tagged_Type
(Act_T
)
13345 and then Present
(Corresponding_Record_Type
(Act_T
))
13346 and then Present
(Ancestor
)
13347 and then Is_Interface
(Ancestor
)
13350 Corr_Rec
: constant Entity_Id
:=
13351 Corresponding_Record_Type
(Act_T
);
13352 New_Corr
: Entity_Id
;
13353 Corr_Decl
: Node_Id
;
13356 New_Corr
:= Make_Temporary
(Loc
, 'S');
13358 Make_Subtype_Declaration
(Loc
,
13359 Defining_Identifier
=> New_Corr
,
13360 Subtype_Indication
=>
13361 New_Occurrence_Of
(Corr_Rec
, Loc
));
13362 Append_To
(Decl_Nodes
, Corr_Decl
);
13364 if Ekind
(Act_T
) = E_Task_Type
then
13365 Set_Ekind
(Subt
, E_Task_Subtype
);
13367 Set_Ekind
(Subt
, E_Protected_Subtype
);
13370 Set_Corresponding_Record_Type
(Subt
, Corr_Rec
);
13371 Set_Generic_Parent_Type
(Corr_Decl
, Ancestor
);
13372 Set_Generic_Parent_Type
(Decl_Node
, Empty
);
13376 -- For a floating-point type, capture dimension info if any, because
13377 -- the generated subtype declaration does not come from source and
13378 -- will not process dimensions.
13380 if Is_Floating_Point_Type
(Act_T
) then
13381 Copy_Dimensions
(Act_T
, Subt
);
13385 end Instantiate_Type
;
13387 ---------------------
13388 -- Is_In_Main_Unit --
13389 ---------------------
13391 function Is_In_Main_Unit
(N
: Node_Id
) return Boolean is
13392 Unum
: constant Unit_Number_Type
:= Get_Source_Unit
(N
);
13393 Current_Unit
: Node_Id
;
13396 if Unum
= Main_Unit
then
13399 -- If the current unit is a subunit then it is either the main unit or
13400 -- is being compiled as part of the main unit.
13402 elsif Nkind
(N
) = N_Compilation_Unit
then
13403 return Nkind
(Unit
(N
)) = N_Subunit
;
13406 Current_Unit
:= Parent
(N
);
13407 while Present
(Current_Unit
)
13408 and then Nkind
(Current_Unit
) /= N_Compilation_Unit
13410 Current_Unit
:= Parent
(Current_Unit
);
13413 -- The instantiation node is in the main unit, or else the current node
13414 -- (perhaps as the result of nested instantiations) is in the main unit,
13415 -- or in the declaration of the main unit, which in this last case must
13419 Current_Unit
= Cunit
(Main_Unit
)
13420 or else Current_Unit
= Library_Unit
(Cunit
(Main_Unit
))
13421 or else (Present
(Current_Unit
)
13422 and then Present
(Library_Unit
(Current_Unit
))
13423 and then Is_In_Main_Unit
(Library_Unit
(Current_Unit
)));
13424 end Is_In_Main_Unit
;
13426 ----------------------------
13427 -- Load_Parent_Of_Generic --
13428 ----------------------------
13430 procedure Load_Parent_Of_Generic
13433 Body_Optional
: Boolean := False)
13435 Comp_Unit
: constant Node_Id
:= Cunit
(Get_Source_Unit
(Spec
));
13436 Saved_Style_Check
: constant Boolean := Style_Check
;
13437 Saved_Warnings
: constant Warning_Record
:= Save_Warnings
;
13438 True_Parent
: Node_Id
;
13439 Inst_Node
: Node_Id
;
13441 Previous_Instances
: constant Elist_Id
:= New_Elmt_List
;
13443 procedure Collect_Previous_Instances
(Decls
: List_Id
);
13444 -- Collect all instantiations in the given list of declarations, that
13445 -- precede the generic that we need to load. If the bodies of these
13446 -- instantiations are available, we must analyze them, to ensure that
13447 -- the public symbols generated are the same when the unit is compiled
13448 -- to generate code, and when it is compiled in the context of a unit
13449 -- that needs a particular nested instance. This process is applied to
13450 -- both package and subprogram instances.
13452 --------------------------------
13453 -- Collect_Previous_Instances --
13454 --------------------------------
13456 procedure Collect_Previous_Instances
(Decls
: List_Id
) is
13460 Decl
:= First
(Decls
);
13461 while Present
(Decl
) loop
13462 if Sloc
(Decl
) >= Sloc
(Inst_Node
) then
13465 -- If Decl is an instantiation, then record it as requiring
13466 -- instantiation of the corresponding body, except if it is an
13467 -- abbreviated instantiation generated internally for conformance
13468 -- checking purposes only for the case of a formal package
13469 -- declared without a box (see Instantiate_Formal_Package). Such
13470 -- an instantiation does not generate any code (the actual code
13471 -- comes from actual) and thus does not need to be analyzed here.
13472 -- If the instantiation appears with a generic package body it is
13473 -- not analyzed here either.
13475 elsif Nkind
(Decl
) = N_Package_Instantiation
13476 and then not Is_Internal
(Defining_Entity
(Decl
))
13478 Append_Elmt
(Decl
, Previous_Instances
);
13480 -- For a subprogram instantiation, omit instantiations intrinsic
13481 -- operations (Unchecked_Conversions, etc.) that have no bodies.
13483 elsif Nkind_In
(Decl
, N_Function_Instantiation
,
13484 N_Procedure_Instantiation
)
13485 and then not Is_Intrinsic_Subprogram
(Entity
(Name
(Decl
)))
13487 Append_Elmt
(Decl
, Previous_Instances
);
13489 elsif Nkind
(Decl
) = N_Package_Declaration
then
13490 Collect_Previous_Instances
13491 (Visible_Declarations
(Specification
(Decl
)));
13492 Collect_Previous_Instances
13493 (Private_Declarations
(Specification
(Decl
)));
13495 -- Previous non-generic bodies may contain instances as well
13497 elsif Nkind
(Decl
) = N_Package_Body
13498 and then Ekind
(Corresponding_Spec
(Decl
)) /= E_Generic_Package
13500 Collect_Previous_Instances
(Declarations
(Decl
));
13502 elsif Nkind
(Decl
) = N_Subprogram_Body
13503 and then not Acts_As_Spec
(Decl
)
13504 and then not Is_Generic_Subprogram
(Corresponding_Spec
(Decl
))
13506 Collect_Previous_Instances
(Declarations
(Decl
));
13511 end Collect_Previous_Instances
;
13513 -- Start of processing for Load_Parent_Of_Generic
13516 if not In_Same_Source_Unit
(N
, Spec
)
13517 or else Nkind
(Unit
(Comp_Unit
)) = N_Package_Declaration
13518 or else (Nkind
(Unit
(Comp_Unit
)) = N_Package_Body
13519 and then not Is_In_Main_Unit
(Spec
))
13521 -- Find body of parent of spec, and analyze it. A special case arises
13522 -- when the parent is an instantiation, that is to say when we are
13523 -- currently instantiating a nested generic. In that case, there is
13524 -- no separate file for the body of the enclosing instance. Instead,
13525 -- the enclosing body must be instantiated as if it were a pending
13526 -- instantiation, in order to produce the body for the nested generic
13527 -- we require now. Note that in that case the generic may be defined
13528 -- in a package body, the instance defined in the same package body,
13529 -- and the original enclosing body may not be in the main unit.
13531 Inst_Node
:= Empty
;
13533 True_Parent
:= Parent
(Spec
);
13534 while Present
(True_Parent
)
13535 and then Nkind
(True_Parent
) /= N_Compilation_Unit
13537 if Nkind
(True_Parent
) = N_Package_Declaration
13539 Nkind
(Original_Node
(True_Parent
)) = N_Package_Instantiation
13541 -- Parent is a compilation unit that is an instantiation.
13542 -- Instantiation node has been replaced with package decl.
13544 Inst_Node
:= Original_Node
(True_Parent
);
13547 elsif Nkind
(True_Parent
) = N_Package_Declaration
13548 and then Present
(Generic_Parent
(Specification
(True_Parent
)))
13549 and then Nkind
(Parent
(True_Parent
)) /= N_Compilation_Unit
13551 -- Parent is an instantiation within another specification.
13552 -- Declaration for instance has been inserted before original
13553 -- instantiation node. A direct link would be preferable?
13555 Inst_Node
:= Next
(True_Parent
);
13556 while Present
(Inst_Node
)
13557 and then Nkind
(Inst_Node
) /= N_Package_Instantiation
13562 -- If the instance appears within a generic, and the generic
13563 -- unit is defined within a formal package of the enclosing
13564 -- generic, there is no generic body available, and none
13565 -- needed. A more precise test should be used ???
13567 if No
(Inst_Node
) then
13574 True_Parent
:= Parent
(True_Parent
);
13578 -- Case where we are currently instantiating a nested generic
13580 if Present
(Inst_Node
) then
13581 if Nkind
(Parent
(True_Parent
)) = N_Compilation_Unit
then
13583 -- Instantiation node and declaration of instantiated package
13584 -- were exchanged when only the declaration was needed.
13585 -- Restore instantiation node before proceeding with body.
13587 Set_Unit
(Parent
(True_Parent
), Inst_Node
);
13590 -- Now complete instantiation of enclosing body, if it appears in
13591 -- some other unit. If it appears in the current unit, the body
13592 -- will have been instantiated already.
13594 if No
(Corresponding_Body
(Instance_Spec
(Inst_Node
))) then
13596 -- We need to determine the expander mode to instantiate the
13597 -- enclosing body. Because the generic body we need may use
13598 -- global entities declared in the enclosing package (including
13599 -- aggregates) it is in general necessary to compile this body
13600 -- with expansion enabled, except if we are within a generic
13601 -- package, in which case the usual generic rule applies.
13604 Exp_Status
: Boolean := True;
13608 -- Loop through scopes looking for generic package
13610 Scop
:= Scope
(Defining_Entity
(Instance_Spec
(Inst_Node
)));
13611 while Present
(Scop
)
13612 and then Scop
/= Standard_Standard
13614 if Ekind
(Scop
) = E_Generic_Package
then
13615 Exp_Status
:= False;
13619 Scop
:= Scope
(Scop
);
13622 -- Collect previous instantiations in the unit that contains
13623 -- the desired generic.
13625 if Nkind
(Parent
(True_Parent
)) /= N_Compilation_Unit
13626 and then not Body_Optional
13630 Info
: Pending_Body_Info
;
13634 Par
:= Parent
(Inst_Node
);
13635 while Present
(Par
) loop
13636 exit when Nkind
(Parent
(Par
)) = N_Compilation_Unit
;
13637 Par
:= Parent
(Par
);
13640 pragma Assert
(Present
(Par
));
13642 if Nkind
(Par
) = N_Package_Body
then
13643 Collect_Previous_Instances
(Declarations
(Par
));
13645 elsif Nkind
(Par
) = N_Package_Declaration
then
13646 Collect_Previous_Instances
13647 (Visible_Declarations
(Specification
(Par
)));
13648 Collect_Previous_Instances
13649 (Private_Declarations
(Specification
(Par
)));
13652 -- Enclosing unit is a subprogram body. In this
13653 -- case all instance bodies are processed in order
13654 -- and there is no need to collect them separately.
13659 Decl
:= First_Elmt
(Previous_Instances
);
13660 while Present
(Decl
) loop
13662 (Inst_Node
=> Node
(Decl
),
13664 Instance_Spec
(Node
(Decl
)),
13665 Expander_Status
=> Exp_Status
,
13666 Current_Sem_Unit
=>
13667 Get_Code_Unit
(Sloc
(Node
(Decl
))),
13668 Scope_Suppress
=> Scope_Suppress
,
13669 Local_Suppress_Stack_Top
=>
13670 Local_Suppress_Stack_Top
,
13671 Version
=> Ada_Version
,
13672 Version_Pragma
=> Ada_Version_Pragma
,
13673 Warnings
=> Save_Warnings
,
13674 SPARK_Mode
=> SPARK_Mode
,
13675 SPARK_Mode_Pragma
=> SPARK_Mode_Pragma
);
13677 -- Package instance
13679 if Nkind
(Node
(Decl
)) = N_Package_Instantiation
13681 Instantiate_Package_Body
13682 (Info
, Body_Optional
=> True);
13684 -- Subprogram instance
13687 -- The instance_spec is in the wrapper package,
13688 -- usually followed by its local renaming
13689 -- declaration. See Build_Subprogram_Renaming
13690 -- for details. If the instance carries aspects,
13691 -- these result in the corresponding pragmas,
13692 -- inserted after the subprogram declaration.
13693 -- They must be skipped as well when retrieving
13694 -- the desired spec. Some of them may have been
13695 -- rewritten as null statements.
13696 -- A direct link would be more robust ???
13700 (Last
(Visible_Declarations
13701 (Specification
(Info
.Act_Decl
))));
13703 while Nkind_In
(Decl
,
13706 N_Subprogram_Renaming_Declaration
)
13708 Decl
:= Prev
(Decl
);
13711 Info
.Act_Decl
:= Decl
;
13714 Instantiate_Subprogram_Body
13715 (Info
, Body_Optional
=> True);
13723 Instantiate_Package_Body
13725 ((Inst_Node
=> Inst_Node
,
13726 Act_Decl
=> True_Parent
,
13727 Expander_Status
=> Exp_Status
,
13728 Current_Sem_Unit
=> Get_Code_Unit
13729 (Sloc
(Inst_Node
)),
13730 Scope_Suppress
=> Scope_Suppress
,
13731 Local_Suppress_Stack_Top
=> Local_Suppress_Stack_Top
,
13732 Version
=> Ada_Version
,
13733 Version_Pragma
=> Ada_Version_Pragma
,
13734 Warnings
=> Save_Warnings
,
13735 SPARK_Mode
=> SPARK_Mode
,
13736 SPARK_Mode_Pragma
=> SPARK_Mode_Pragma
)),
13737 Body_Optional
=> Body_Optional
);
13741 -- Case where we are not instantiating a nested generic
13744 Opt
.Style_Check
:= False;
13745 Expander_Mode_Save_And_Set
(True);
13746 Load_Needed_Body
(Comp_Unit
, OK
);
13747 Opt
.Style_Check
:= Saved_Style_Check
;
13748 Restore_Warnings
(Saved_Warnings
);
13749 Expander_Mode_Restore
;
13752 and then Unit_Requires_Body
(Defining_Entity
(Spec
))
13753 and then not Body_Optional
13756 Bname
: constant Unit_Name_Type
:=
13757 Get_Body_Name
(Get_Unit_Name
(Unit
(Comp_Unit
)));
13760 -- In CodePeer mode, the missing body may make the analysis
13761 -- incomplete, but we do not treat it as fatal.
13763 if CodePeer_Mode
then
13767 Error_Msg_Unit_1
:= Bname
;
13768 Error_Msg_N
("this instantiation requires$!", N
);
13769 Error_Msg_File_1
:=
13770 Get_File_Name
(Bname
, Subunit
=> False);
13771 Error_Msg_N
("\but file{ was not found!", N
);
13772 raise Unrecoverable_Error
;
13779 -- If loading parent of the generic caused an instantiation circularity,
13780 -- we abandon compilation at this point, because otherwise in some cases
13781 -- we get into trouble with infinite recursions after this point.
13783 if Circularity_Detected
then
13784 raise Unrecoverable_Error
;
13786 end Load_Parent_Of_Generic
;
13788 ---------------------------------
13789 -- Map_Formal_Package_Entities --
13790 ---------------------------------
13792 procedure Map_Formal_Package_Entities
(Form
: Entity_Id
; Act
: Entity_Id
) is
13797 Set_Instance_Of
(Form
, Act
);
13799 -- Traverse formal and actual package to map the corresponding entities.
13800 -- We skip over internal entities that may be generated during semantic
13801 -- analysis, and find the matching entities by name, given that they
13802 -- must appear in the same order.
13804 E1
:= First_Entity
(Form
);
13805 E2
:= First_Entity
(Act
);
13806 while Present
(E1
) and then E1
/= First_Private_Entity
(Form
) loop
13807 -- Could this test be a single condition??? Seems like it could, and
13808 -- isn't FPE (Form) a constant anyway???
13810 if not Is_Internal
(E1
)
13811 and then Present
(Parent
(E1
))
13812 and then not Is_Class_Wide_Type
(E1
)
13813 and then not Is_Internal_Name
(Chars
(E1
))
13815 while Present
(E2
) and then Chars
(E2
) /= Chars
(E1
) loop
13822 Set_Instance_Of
(E1
, E2
);
13824 if Is_Type
(E1
) and then Is_Tagged_Type
(E2
) then
13825 Set_Instance_Of
(Class_Wide_Type
(E1
), Class_Wide_Type
(E2
));
13828 if Is_Constrained
(E1
) then
13829 Set_Instance_Of
(Base_Type
(E1
), Base_Type
(E2
));
13832 if Ekind
(E1
) = E_Package
and then No
(Renamed_Object
(E1
)) then
13833 Map_Formal_Package_Entities
(E1
, E2
);
13840 end Map_Formal_Package_Entities
;
13842 -----------------------
13843 -- Move_Freeze_Nodes --
13844 -----------------------
13846 procedure Move_Freeze_Nodes
13847 (Out_Of
: Entity_Id
;
13852 Next_Decl
: Node_Id
;
13853 Next_Node
: Node_Id
:= After
;
13856 function Is_Outer_Type
(T
: Entity_Id
) return Boolean;
13857 -- Check whether entity is declared in a scope external to that of the
13860 -------------------
13861 -- Is_Outer_Type --
13862 -------------------
13864 function Is_Outer_Type
(T
: Entity_Id
) return Boolean is
13865 Scop
: Entity_Id
:= Scope
(T
);
13868 if Scope_Depth
(Scop
) < Scope_Depth
(Out_Of
) then
13872 while Scop
/= Standard_Standard
loop
13873 if Scop
= Out_Of
then
13876 Scop
:= Scope
(Scop
);
13884 -- Start of processing for Move_Freeze_Nodes
13891 -- First remove the freeze nodes that may appear before all other
13895 while Present
(Decl
)
13896 and then Nkind
(Decl
) = N_Freeze_Entity
13897 and then Is_Outer_Type
(Entity
(Decl
))
13899 Decl
:= Remove_Head
(L
);
13900 Insert_After
(Next_Node
, Decl
);
13901 Set_Analyzed
(Decl
, False);
13906 -- Next scan the list of declarations and remove each freeze node that
13907 -- appears ahead of the current node.
13909 while Present
(Decl
) loop
13910 while Present
(Next
(Decl
))
13911 and then Nkind
(Next
(Decl
)) = N_Freeze_Entity
13912 and then Is_Outer_Type
(Entity
(Next
(Decl
)))
13914 Next_Decl
:= Remove_Next
(Decl
);
13915 Insert_After
(Next_Node
, Next_Decl
);
13916 Set_Analyzed
(Next_Decl
, False);
13917 Next_Node
:= Next_Decl
;
13920 -- If the declaration is a nested package or concurrent type, then
13921 -- recurse. Nested generic packages will have been processed from the
13924 case Nkind
(Decl
) is
13925 when N_Package_Declaration
=>
13926 Spec
:= Specification
(Decl
);
13928 when N_Task_Type_Declaration
=>
13929 Spec
:= Task_Definition
(Decl
);
13931 when N_Protected_Type_Declaration
=>
13932 Spec
:= Protected_Definition
(Decl
);
13938 if Present
(Spec
) then
13939 Move_Freeze_Nodes
(Out_Of
, Next_Node
, Visible_Declarations
(Spec
));
13940 Move_Freeze_Nodes
(Out_Of
, Next_Node
, Private_Declarations
(Spec
));
13945 end Move_Freeze_Nodes
;
13951 function Next_Assoc
(E
: Assoc_Ptr
) return Assoc_Ptr
is
13953 return Generic_Renamings
.Table
(E
).Next_In_HTable
;
13956 ------------------------
13957 -- Preanalyze_Actuals --
13958 ------------------------
13960 procedure Preanalyze_Actuals
(N
: Node_Id
; Inst
: Entity_Id
:= Empty
) is
13963 Errs
: constant Nat
:= Serious_Errors_Detected
;
13965 Cur
: Entity_Id
:= Empty
;
13966 -- Current homograph of the instance name
13968 Vis
: Boolean := False;
13969 -- Saved visibility status of the current homograph
13972 Assoc
:= First
(Generic_Associations
(N
));
13974 -- If the instance is a child unit, its name may hide an outer homonym,
13975 -- so make it invisible to perform name resolution on the actuals.
13977 if Nkind
(Defining_Unit_Name
(N
)) = N_Defining_Program_Unit_Name
13979 (Current_Entity
(Defining_Identifier
(Defining_Unit_Name
(N
))))
13981 Cur
:= Current_Entity
(Defining_Identifier
(Defining_Unit_Name
(N
)));
13983 if Is_Compilation_Unit
(Cur
) then
13984 Vis
:= Is_Immediately_Visible
(Cur
);
13985 Set_Is_Immediately_Visible
(Cur
, False);
13991 while Present
(Assoc
) loop
13992 if Nkind
(Assoc
) /= N_Others_Choice
then
13993 Act
:= Explicit_Generic_Actual_Parameter
(Assoc
);
13995 -- Within a nested instantiation, a defaulted actual is an empty
13996 -- association, so nothing to analyze. If the subprogram actual
13997 -- is an attribute, analyze prefix only, because actual is not a
13998 -- complete attribute reference.
14000 -- If actual is an allocator, analyze expression only. The full
14001 -- analysis can generate code, and if instance is a compilation
14002 -- unit we have to wait until the package instance is installed
14003 -- to have a proper place to insert this code.
14005 -- String literals may be operators, but at this point we do not
14006 -- know whether the actual is a formal subprogram or a string.
14011 elsif Nkind
(Act
) = N_Attribute_Reference
then
14012 Analyze
(Prefix
(Act
));
14014 elsif Nkind
(Act
) = N_Explicit_Dereference
then
14015 Analyze
(Prefix
(Act
));
14017 elsif Nkind
(Act
) = N_Allocator
then
14019 Expr
: constant Node_Id
:= Expression
(Act
);
14022 if Nkind
(Expr
) = N_Subtype_Indication
then
14023 Analyze
(Subtype_Mark
(Expr
));
14025 -- Analyze separately each discriminant constraint, when
14026 -- given with a named association.
14032 Constr
:= First
(Constraints
(Constraint
(Expr
)));
14033 while Present
(Constr
) loop
14034 if Nkind
(Constr
) = N_Discriminant_Association
then
14035 Analyze
(Expression
(Constr
));
14049 elsif Nkind
(Act
) /= N_Operator_Symbol
then
14052 -- Within a package instance, mark actuals that are limited
14053 -- views, so their use can be moved to the body of the
14056 if Is_Entity_Name
(Act
)
14057 and then Is_Type
(Entity
(Act
))
14058 and then From_Limited_With
(Entity
(Act
))
14059 and then Present
(Inst
)
14061 Append_Elmt
(Entity
(Act
), Incomplete_Actuals
(Inst
));
14065 if Errs
/= Serious_Errors_Detected
then
14067 -- Do a minimal analysis of the generic, to prevent spurious
14068 -- warnings complaining about the generic being unreferenced,
14069 -- before abandoning the instantiation.
14071 Analyze
(Name
(N
));
14073 if Is_Entity_Name
(Name
(N
))
14074 and then Etype
(Name
(N
)) /= Any_Type
14076 Generate_Reference
(Entity
(Name
(N
)), Name
(N
));
14077 Set_Is_Instantiated
(Entity
(Name
(N
)));
14080 if Present
(Cur
) then
14082 -- For the case of a child instance hiding an outer homonym,
14083 -- provide additional warning which might explain the error.
14085 Set_Is_Immediately_Visible
(Cur
, Vis
);
14087 ("& hides outer unit with the same name??",
14088 N
, Defining_Unit_Name
(N
));
14091 Abandon_Instantiation
(Act
);
14098 if Present
(Cur
) then
14099 Set_Is_Immediately_Visible
(Cur
, Vis
);
14101 end Preanalyze_Actuals
;
14103 -------------------------------
14104 -- Provide_Completing_Bodies --
14105 -------------------------------
14107 procedure Provide_Completing_Bodies
(N
: Node_Id
) is
14108 procedure Build_Completing_Body
(Subp_Decl
: Node_Id
);
14109 -- Generate the completing body for subprogram declaration Subp_Decl
14111 procedure Provide_Completing_Bodies_In
(Decls
: List_Id
);
14112 -- Generating completing bodies for all subprograms found in declarative
14115 ---------------------------
14116 -- Build_Completing_Body --
14117 ---------------------------
14119 procedure Build_Completing_Body
(Subp_Decl
: Node_Id
) is
14120 Loc
: constant Source_Ptr
:= Sloc
(Subp_Decl
);
14121 Subp_Id
: constant Entity_Id
:= Defining_Entity
(Subp_Decl
);
14125 -- Nothing to do if the subprogram already has a completing body
14127 if Present
(Corresponding_Body
(Subp_Decl
)) then
14130 -- Mark the function as having a valid return statement even though
14131 -- the body contains a single raise statement.
14133 elsif Ekind
(Subp_Id
) = E_Function
then
14134 Set_Return_Present
(Subp_Id
);
14137 -- Clone the specification to obtain new entities and reset the only
14140 Spec
:= Copy_Subprogram_Spec
(Specification
(Subp_Decl
));
14141 Set_Generic_Parent
(Spec
, Empty
);
14144 -- function Func ... return ... is
14146 -- procedure Proc ... is
14148 -- raise Program_Error with "access before elaboration";
14151 Insert_After_And_Analyze
(Subp_Decl
,
14152 Make_Subprogram_Body
(Loc
,
14153 Specification
=> Spec
,
14154 Declarations
=> New_List
,
14155 Handled_Statement_Sequence
=>
14156 Make_Handled_Sequence_Of_Statements
(Loc
,
14157 Statements
=> New_List
(
14158 Make_Raise_Program_Error
(Loc
,
14159 Reason
=> PE_Access_Before_Elaboration
)))));
14160 end Build_Completing_Body
;
14162 ----------------------------------
14163 -- Provide_Completing_Bodies_In --
14164 ----------------------------------
14166 procedure Provide_Completing_Bodies_In
(Decls
: List_Id
) is
14170 if Present
(Decls
) then
14171 Decl
:= First
(Decls
);
14172 while Present
(Decl
) loop
14173 Provide_Completing_Bodies
(Decl
);
14177 end Provide_Completing_Bodies_In
;
14183 -- Start of processing for Provide_Completing_Bodies
14186 if Nkind
(N
) = N_Package_Declaration
then
14187 Spec
:= Specification
(N
);
14189 Push_Scope
(Defining_Entity
(N
));
14190 Provide_Completing_Bodies_In
(Visible_Declarations
(Spec
));
14191 Provide_Completing_Bodies_In
(Private_Declarations
(Spec
));
14194 elsif Nkind
(N
) = N_Subprogram_Declaration
then
14195 Build_Completing_Body
(N
);
14197 end Provide_Completing_Bodies
;
14199 -------------------
14200 -- Remove_Parent --
14201 -------------------
14203 procedure Remove_Parent
(In_Body
: Boolean := False) is
14204 S
: Entity_Id
:= Current_Scope
;
14205 -- S is the scope containing the instantiation just completed. The scope
14206 -- stack contains the parent instances of the instantiation, followed by
14215 -- After child instantiation is complete, remove from scope stack the
14216 -- extra copy of the current scope, and then remove parent instances.
14218 if not In_Body
then
14221 while Current_Scope
/= S
loop
14222 P
:= Current_Scope
;
14223 End_Package_Scope
(Current_Scope
);
14225 if In_Open_Scopes
(P
) then
14226 E
:= First_Entity
(P
);
14227 while Present
(E
) loop
14228 Set_Is_Immediately_Visible
(E
, True);
14232 -- If instantiation is declared in a block, it is the enclosing
14233 -- scope that might be a parent instance. Note that only one
14234 -- block can be involved, because the parent instances have
14235 -- been installed within it.
14237 if Ekind
(P
) = E_Block
then
14238 Cur_P
:= Scope
(P
);
14243 if Is_Generic_Instance
(Cur_P
) and then P
/= Current_Scope
then
14244 -- We are within an instance of some sibling. Retain
14245 -- visibility of parent, for proper subsequent cleanup, and
14246 -- reinstall private declarations as well.
14248 Set_In_Private_Part
(P
);
14249 Install_Private_Declarations
(P
);
14252 -- If the ultimate parent is a top-level unit recorded in
14253 -- Instance_Parent_Unit, then reset its visibility to what it was
14254 -- before instantiation. (It's not clear what the purpose is of
14255 -- testing whether Scope (P) is In_Open_Scopes, but that test was
14256 -- present before the ultimate parent test was added.???)
14258 elsif not In_Open_Scopes
(Scope
(P
))
14259 or else (P
= Instance_Parent_Unit
14260 and then not Parent_Unit_Visible
)
14262 Set_Is_Immediately_Visible
(P
, False);
14264 -- If the current scope is itself an instantiation of a generic
14265 -- nested within P, and we are in the private part of body of this
14266 -- instantiation, restore the full views of P, that were removed
14267 -- in End_Package_Scope above. This obscure case can occur when a
14268 -- subunit of a generic contains an instance of a child unit of
14269 -- its generic parent unit.
14271 elsif S
= Current_Scope
and then Is_Generic_Instance
(S
) then
14273 Par
: constant Entity_Id
:=
14274 Generic_Parent
(Package_Specification
(S
));
14277 and then P
= Scope
(Par
)
14278 and then (In_Package_Body
(S
) or else In_Private_Part
(S
))
14280 Set_In_Private_Part
(P
);
14281 Install_Private_Declarations
(P
);
14287 -- Reset visibility of entities in the enclosing scope
14289 Set_Is_Hidden_Open_Scope
(Current_Scope
, False);
14291 Hidden
:= First_Elmt
(Hidden_Entities
);
14292 while Present
(Hidden
) loop
14293 Set_Is_Immediately_Visible
(Node
(Hidden
), True);
14294 Next_Elmt
(Hidden
);
14298 -- Each body is analyzed separately, and there is no context that
14299 -- needs preserving from one body instance to the next, so remove all
14300 -- parent scopes that have been installed.
14302 while Present
(S
) loop
14303 End_Package_Scope
(S
);
14304 Set_Is_Immediately_Visible
(S
, False);
14305 S
:= Current_Scope
;
14306 exit when S
= Standard_Standard
;
14315 procedure Restore_Env
is
14316 Saved
: Instance_Env
renames Instance_Envs
.Table
(Instance_Envs
.Last
);
14319 if No
(Current_Instantiated_Parent
.Act_Id
) then
14320 -- Restore environment after subprogram inlining
14322 Restore_Private_Views
(Empty
);
14325 Current_Instantiated_Parent
:= Saved
.Instantiated_Parent
;
14326 Exchanged_Views
:= Saved
.Exchanged_Views
;
14327 Hidden_Entities
:= Saved
.Hidden_Entities
;
14328 Current_Sem_Unit
:= Saved
.Current_Sem_Unit
;
14329 Parent_Unit_Visible
:= Saved
.Parent_Unit_Visible
;
14330 Instance_Parent_Unit
:= Saved
.Instance_Parent_Unit
;
14332 Restore_Opt_Config_Switches
(Saved
.Switches
);
14334 Instance_Envs
.Decrement_Last
;
14337 ---------------------------
14338 -- Restore_Private_Views --
14339 ---------------------------
14341 procedure Restore_Private_Views
14342 (Pack_Id
: Entity_Id
;
14343 Is_Package
: Boolean := True)
14348 Dep_Elmt
: Elmt_Id
;
14351 procedure Restore_Nested_Formal
(Formal
: Entity_Id
);
14352 -- Hide the generic formals of formal packages declared with box which
14353 -- were reachable in the current instantiation.
14355 ---------------------------
14356 -- Restore_Nested_Formal --
14357 ---------------------------
14359 procedure Restore_Nested_Formal
(Formal
: Entity_Id
) is
14363 if Present
(Renamed_Object
(Formal
))
14364 and then Denotes_Formal_Package
(Renamed_Object
(Formal
), True)
14368 elsif Present
(Associated_Formal_Package
(Formal
)) then
14369 Ent
:= First_Entity
(Formal
);
14370 while Present
(Ent
) loop
14371 exit when Ekind
(Ent
) = E_Package
14372 and then Renamed_Entity
(Ent
) = Renamed_Entity
(Formal
);
14374 Set_Is_Hidden
(Ent
);
14375 Set_Is_Potentially_Use_Visible
(Ent
, False);
14377 -- If package, then recurse
14379 if Ekind
(Ent
) = E_Package
then
14380 Restore_Nested_Formal
(Ent
);
14386 end Restore_Nested_Formal
;
14388 -- Start of processing for Restore_Private_Views
14391 M
:= First_Elmt
(Exchanged_Views
);
14392 while Present
(M
) loop
14395 -- Subtypes of types whose views have been exchanged, and that are
14396 -- defined within the instance, were not on the Private_Dependents
14397 -- list on entry to the instance, so they have to be exchanged
14398 -- explicitly now, in order to remain consistent with the view of the
14401 if Ekind_In
(Typ
, E_Private_Type
,
14402 E_Limited_Private_Type
,
14403 E_Record_Type_With_Private
)
14405 Dep_Elmt
:= First_Elmt
(Private_Dependents
(Typ
));
14406 while Present
(Dep_Elmt
) loop
14407 Dep_Typ
:= Node
(Dep_Elmt
);
14409 if Scope
(Dep_Typ
) = Pack_Id
14410 and then Present
(Full_View
(Dep_Typ
))
14412 Replace_Elmt
(Dep_Elmt
, Full_View
(Dep_Typ
));
14413 Exchange_Declarations
(Dep_Typ
);
14416 Next_Elmt
(Dep_Elmt
);
14420 Exchange_Declarations
(Node
(M
));
14424 if No
(Pack_Id
) then
14428 -- Make the generic formal parameters private, and make the formal types
14429 -- into subtypes of the actuals again.
14431 E
:= First_Entity
(Pack_Id
);
14432 while Present
(E
) loop
14433 Set_Is_Hidden
(E
, True);
14436 and then Nkind
(Parent
(E
)) = N_Subtype_Declaration
14438 -- If the actual for E is itself a generic actual type from
14439 -- an enclosing instance, E is still a generic actual type
14440 -- outside of the current instance. This matter when resolving
14441 -- an overloaded call that may be ambiguous in the enclosing
14442 -- instance, when two of its actuals coincide.
14444 if Is_Entity_Name
(Subtype_Indication
(Parent
(E
)))
14445 and then Is_Generic_Actual_Type
14446 (Entity
(Subtype_Indication
(Parent
(E
))))
14450 Set_Is_Generic_Actual_Type
(E
, False);
14453 -- An unusual case of aliasing: the actual may also be directly
14454 -- visible in the generic, and be private there, while it is fully
14455 -- visible in the context of the instance. The internal subtype
14456 -- is private in the instance but has full visibility like its
14457 -- parent in the enclosing scope. This enforces the invariant that
14458 -- the privacy status of all private dependents of a type coincide
14459 -- with that of the parent type. This can only happen when a
14460 -- generic child unit is instantiated within a sibling.
14462 if Is_Private_Type
(E
)
14463 and then not Is_Private_Type
(Etype
(E
))
14465 Exchange_Declarations
(E
);
14468 elsif Ekind
(E
) = E_Package
then
14470 -- The end of the renaming list is the renaming of the generic
14471 -- package itself. If the instance is a subprogram, all entities
14472 -- in the corresponding package are renamings. If this entity is
14473 -- a formal package, make its own formals private as well. The
14474 -- actual in this case is itself the renaming of an instantiation.
14475 -- If the entity is not a package renaming, it is the entity
14476 -- created to validate formal package actuals: ignore it.
14478 -- If the actual is itself a formal package for the enclosing
14479 -- generic, or the actual for such a formal package, it remains
14480 -- visible on exit from the instance, and therefore nothing needs
14481 -- to be done either, except to keep it accessible.
14483 if Is_Package
and then Renamed_Object
(E
) = Pack_Id
then
14486 elsif Nkind
(Parent
(E
)) /= N_Package_Renaming_Declaration
then
14490 Denotes_Formal_Package
(Renamed_Object
(E
), True, Pack_Id
)
14492 Set_Is_Hidden
(E
, False);
14496 Act_P
: constant Entity_Id
:= Renamed_Object
(E
);
14500 Id
:= First_Entity
(Act_P
);
14502 and then Id
/= First_Private_Entity
(Act_P
)
14504 exit when Ekind
(Id
) = E_Package
14505 and then Renamed_Object
(Id
) = Act_P
;
14507 Set_Is_Hidden
(Id
, True);
14508 Set_Is_Potentially_Use_Visible
(Id
, In_Use
(Act_P
));
14510 if Ekind
(Id
) = E_Package
then
14511 Restore_Nested_Formal
(Id
);
14522 end Restore_Private_Views
;
14529 (Gen_Unit
: Entity_Id
;
14530 Act_Unit
: Entity_Id
)
14534 Set_Instance_Env
(Gen_Unit
, Act_Unit
);
14537 ----------------------------
14538 -- Save_Global_References --
14539 ----------------------------
14541 procedure Save_Global_References
(Templ
: Node_Id
) is
14543 -- ??? it is horrible to use global variables in highly recursive code
14546 -- The entity of the current associated node
14548 Gen_Scope
: Entity_Id
;
14549 -- The scope of the generic for which references are being saved
14552 -- The current associated node
14554 function Is_Global
(E
: Entity_Id
) return Boolean;
14555 -- Check whether entity is defined outside of generic unit. Examine the
14556 -- scope of an entity, and the scope of the scope, etc, until we find
14557 -- either Standard, in which case the entity is global, or the generic
14558 -- unit itself, which indicates that the entity is local. If the entity
14559 -- is the generic unit itself, as in the case of a recursive call, or
14560 -- the enclosing generic unit, if different from the current scope, then
14561 -- it is local as well, because it will be replaced at the point of
14562 -- instantiation. On the other hand, if it is a reference to a child
14563 -- unit of a common ancestor, which appears in an instantiation, it is
14564 -- global because it is used to denote a specific compilation unit at
14565 -- the time the instantiations will be analyzed.
14567 procedure Qualify_Universal_Operands
14569 Func_Call
: Node_Id
);
14570 -- Op denotes a binary or unary operator in generic template Templ. Node
14571 -- Func_Call is the function call alternative of the operator within the
14572 -- the analyzed copy of the template. Change each operand which yields a
14573 -- universal type by wrapping it into a qualified expression
14575 -- Actual_Typ'(Operand)
14577 -- where Actual_Typ is the type of corresponding actual parameter of
14578 -- Operand in Func_Call.
14580 procedure Reset_Entity
(N
: Node_Id
);
14581 -- Save semantic information on global entity so that it is not resolved
14582 -- again at instantiation time.
14584 procedure Save_Entity_Descendants
(N
: Node_Id
);
14585 -- Apply Save_Global_References to the two syntactic descendants of
14586 -- non-terminal nodes that carry an Associated_Node and are processed
14587 -- through Reset_Entity. Once the global entity (if any) has been
14588 -- captured together with its type, only two syntactic descendants need
14589 -- to be traversed to complete the processing of the tree rooted at N.
14590 -- This applies to Selected_Components, Expanded_Names, and to Operator
14591 -- nodes. N can also be a character literal, identifier, or operator
14592 -- symbol node, but the call has no effect in these cases.
14594 procedure Save_Global_Defaults
(N1
: Node_Id
; N2
: Node_Id
);
14595 -- Default actuals in nested instances must be handled specially
14596 -- because there is no link to them from the original tree. When an
14597 -- actual subprogram is given by a default, we add an explicit generic
14598 -- association for it in the instantiation node. When we save the
14599 -- global references on the name of the instance, we recover the list
14600 -- of generic associations, and add an explicit one to the original
14601 -- generic tree, through which a global actual can be preserved.
14602 -- Similarly, if a child unit is instantiated within a sibling, in the
14603 -- context of the parent, we must preserve the identifier of the parent
14604 -- so that it can be properly resolved in a subsequent instantiation.
14606 procedure Save_Global_Descendant
(D
: Union_Id
);
14607 -- Apply Save_References recursively to the descendants of node D
14609 procedure Save_References
(N
: Node_Id
);
14610 -- This is the recursive procedure that does the work, once the
14611 -- enclosing generic scope has been established.
14617 function Is_Global
(E
: Entity_Id
) return Boolean is
14620 function Is_Instance_Node
(Decl
: Node_Id
) return Boolean;
14621 -- Determine whether the parent node of a reference to a child unit
14622 -- denotes an instantiation or a formal package, in which case the
14623 -- reference to the child unit is global, even if it appears within
14624 -- the current scope (e.g. when the instance appears within the body
14625 -- of an ancestor).
14627 ----------------------
14628 -- Is_Instance_Node --
14629 ----------------------
14631 function Is_Instance_Node
(Decl
: Node_Id
) return Boolean is
14633 return Nkind
(Decl
) in N_Generic_Instantiation
14635 Nkind
(Original_Node
(Decl
)) = N_Formal_Package_Declaration
;
14636 end Is_Instance_Node
;
14638 -- Start of processing for Is_Global
14641 if E
= Gen_Scope
then
14644 elsif E
= Standard_Standard
then
14647 elsif Is_Child_Unit
(E
)
14648 and then (Is_Instance_Node
(Parent
(N2
))
14649 or else (Nkind
(Parent
(N2
)) = N_Expanded_Name
14650 and then N2
= Selector_Name
(Parent
(N2
))
14652 Is_Instance_Node
(Parent
(Parent
(N2
)))))
14658 while Se
/= Gen_Scope
loop
14659 if Se
= Standard_Standard
then
14670 --------------------------------
14671 -- Qualify_Universal_Operands --
14672 --------------------------------
14674 procedure Qualify_Universal_Operands
14676 Func_Call
: Node_Id
)
14678 procedure Qualify_Operand
(Opnd
: Node_Id
; Actual
: Node_Id
);
14679 -- Rewrite operand Opnd as a qualified expression of the form
14681 -- Actual_Typ'(Opnd)
14683 -- where Actual is the corresponding actual parameter of Opnd in
14684 -- function call Func_Call.
14686 function Qualify_Type
14688 Typ
: Entity_Id
) return Node_Id
;
14689 -- Qualify type Typ by creating a selected component of the form
14691 -- Scope_Of_Typ.Typ
14693 ---------------------
14694 -- Qualify_Operand --
14695 ---------------------
14697 procedure Qualify_Operand
(Opnd
: Node_Id
; Actual
: Node_Id
) is
14698 Loc
: constant Source_Ptr
:= Sloc
(Opnd
);
14699 Typ
: constant Entity_Id
:= Etype
(Actual
);
14704 -- Qualify the operand when it is of a universal type. Note that
14705 -- the template is unanalyzed and it is not possible to directly
14706 -- query the type. This transformation is not done when the type
14707 -- of the actual is internally generated because the type will be
14708 -- regenerated in the instance.
14710 if Yields_Universal_Type
(Opnd
)
14711 and then Comes_From_Source
(Typ
)
14712 and then not Is_Hidden
(Typ
)
14714 -- The type of the actual may be a global reference. Save this
14715 -- information by creating a reference to it.
14717 if Is_Global
(Typ
) then
14718 Mark
:= New_Occurrence_Of
(Typ
, Loc
);
14720 -- Otherwise rely on resolution to find the proper type within
14724 Mark
:= Qualify_Type
(Loc
, Typ
);
14728 Make_Qualified_Expression
(Loc
,
14729 Subtype_Mark
=> Mark
,
14730 Expression
=> Relocate_Node
(Opnd
));
14732 -- Mark the qualification to distinguish it from other source
14733 -- constructs and signal the instantiation mechanism that this
14734 -- node requires special processing. See Copy_Generic_Node for
14737 Set_Is_Qualified_Universal_Literal
(Qual
);
14739 Rewrite
(Opnd
, Qual
);
14741 end Qualify_Operand
;
14747 function Qualify_Type
14749 Typ
: Entity_Id
) return Node_Id
14751 Scop
: constant Entity_Id
:= Scope
(Typ
);
14755 Result
:= Make_Identifier
(Loc
, Chars
(Typ
));
14757 if Present
(Scop
) and then not Is_Generic_Unit
(Scop
) then
14759 Make_Selected_Component
(Loc
,
14760 Prefix
=> Make_Identifier
(Loc
, Chars
(Scop
)),
14761 Selector_Name
=> Result
);
14769 Actuals
: constant List_Id
:= Parameter_Associations
(Func_Call
);
14771 -- Start of processing for Qualify_Universal_Operands
14774 if Nkind
(Op
) in N_Binary_Op
then
14775 Qualify_Operand
(Left_Opnd
(Op
), First
(Actuals
));
14776 Qualify_Operand
(Right_Opnd
(Op
), Next
(First
(Actuals
)));
14778 elsif Nkind
(Op
) in N_Unary_Op
then
14779 Qualify_Operand
(Right_Opnd
(Op
), First
(Actuals
));
14781 end Qualify_Universal_Operands
;
14787 procedure Reset_Entity
(N
: Node_Id
) is
14788 procedure Set_Global_Type
(N
: Node_Id
; N2
: Node_Id
);
14789 -- If the type of N2 is global to the generic unit, save the type in
14790 -- the generic node. Just as we perform name capture for explicit
14791 -- references within the generic, we must capture the global types
14792 -- of local entities because they may participate in resolution in
14795 function Top_Ancestor
(E
: Entity_Id
) return Entity_Id
;
14796 -- Find the ultimate ancestor of the current unit. If it is not a
14797 -- generic unit, then the name of the current unit in the prefix of
14798 -- an expanded name must be replaced with its generic homonym to
14799 -- ensure that it will be properly resolved in an instance.
14801 ---------------------
14802 -- Set_Global_Type --
14803 ---------------------
14805 procedure Set_Global_Type
(N
: Node_Id
; N2
: Node_Id
) is
14806 Typ
: constant Entity_Id
:= Etype
(N2
);
14809 Set_Etype
(N
, Typ
);
14811 -- If the entity of N is not the associated node, this is a
14812 -- nested generic and it has an associated node as well, whose
14813 -- type is already the full view (see below). Indicate that the
14814 -- original node has a private view.
14816 if Entity
(N
) /= N2
and then Has_Private_View
(Entity
(N
)) then
14817 Set_Has_Private_View
(N
);
14820 -- If not a private type, nothing else to do
14822 if not Is_Private_Type
(Typ
) then
14823 if Is_Array_Type
(Typ
)
14824 and then Is_Private_Type
(Component_Type
(Typ
))
14826 Set_Has_Private_View
(N
);
14829 -- If it is a derivation of a private type in a context where no
14830 -- full view is needed, nothing to do either.
14832 elsif No
(Full_View
(Typ
)) and then Typ
/= Etype
(Typ
) then
14835 -- Otherwise mark the type for flipping and use the full view when
14839 Set_Has_Private_View
(N
);
14841 if Present
(Full_View
(Typ
)) then
14842 Set_Etype
(N2
, Full_View
(Typ
));
14846 if Is_Floating_Point_Type
(Typ
)
14847 and then Has_Dimension_System
(Typ
)
14849 Copy_Dimensions
(N2
, N
);
14851 end Set_Global_Type
;
14857 function Top_Ancestor
(E
: Entity_Id
) return Entity_Id
is
14862 while Is_Child_Unit
(Par
) loop
14863 Par
:= Scope
(Par
);
14869 -- Start of processing for Reset_Entity
14872 N2
:= Get_Associated_Node
(N
);
14875 if Present
(E
) then
14877 -- If the node is an entry call to an entry in an enclosing task,
14878 -- it is rewritten as a selected component. No global entity to
14879 -- preserve in this case, since the expansion will be redone in
14882 if not Nkind_In
(E
, N_Defining_Character_Literal
,
14883 N_Defining_Identifier
,
14884 N_Defining_Operator_Symbol
)
14886 Set_Associated_Node
(N
, Empty
);
14887 Set_Etype
(N
, Empty
);
14891 -- If the entity is an itype created as a subtype of an access
14892 -- type with a null exclusion restore source entity for proper
14893 -- visibility. The itype will be created anew in the instance.
14896 and then Ekind
(E
) = E_Access_Subtype
14897 and then Is_Entity_Name
(N
)
14898 and then Chars
(Etype
(E
)) = Chars
(N
)
14901 Set_Entity
(N2
, E
);
14905 if Is_Global
(E
) then
14907 -- If the entity is a package renaming that is the prefix of
14908 -- an expanded name, it has been rewritten as the renamed
14909 -- package, which is necessary semantically but complicates
14910 -- ASIS tree traversal, so we recover the original entity to
14911 -- expose the renaming. Take into account that the context may
14912 -- be a nested generic, that the original node may itself have
14913 -- an associated node that had better be an entity, and that
14914 -- the current node is still a selected component.
14916 if Ekind
(E
) = E_Package
14917 and then Nkind
(N
) = N_Selected_Component
14918 and then Nkind
(Parent
(N
)) = N_Expanded_Name
14919 and then Present
(Original_Node
(N2
))
14920 and then Is_Entity_Name
(Original_Node
(N2
))
14921 and then Present
(Entity
(Original_Node
(N2
)))
14923 if Is_Global
(Entity
(Original_Node
(N2
))) then
14924 N2
:= Original_Node
(N2
);
14925 Set_Associated_Node
(N
, N2
);
14926 Set_Global_Type
(N
, N2
);
14928 -- Renaming is local, and will be resolved in instance
14931 Set_Associated_Node
(N
, Empty
);
14932 Set_Etype
(N
, Empty
);
14936 Set_Global_Type
(N
, N2
);
14939 elsif Nkind
(N
) = N_Op_Concat
14940 and then Is_Generic_Type
(Etype
(N2
))
14941 and then (Base_Type
(Etype
(Right_Opnd
(N2
))) = Etype
(N2
)
14943 Base_Type
(Etype
(Left_Opnd
(N2
))) = Etype
(N2
))
14944 and then Is_Intrinsic_Subprogram
(E
)
14948 -- Entity is local. Mark generic node as unresolved. Note that now
14949 -- it does not have an entity.
14952 Set_Associated_Node
(N
, Empty
);
14953 Set_Etype
(N
, Empty
);
14956 if Nkind
(Parent
(N
)) in N_Generic_Instantiation
14957 and then N
= Name
(Parent
(N
))
14959 Save_Global_Defaults
(Parent
(N
), Parent
(N2
));
14962 elsif Nkind
(Parent
(N
)) = N_Selected_Component
14963 and then Nkind
(Parent
(N2
)) = N_Expanded_Name
14965 if Is_Global
(Entity
(Parent
(N2
))) then
14966 Change_Selected_Component_To_Expanded_Name
(Parent
(N
));
14967 Set_Associated_Node
(Parent
(N
), Parent
(N2
));
14968 Set_Global_Type
(Parent
(N
), Parent
(N2
));
14969 Save_Entity_Descendants
(N
);
14971 -- If this is a reference to the current generic entity, replace
14972 -- by the name of the generic homonym of the current package. This
14973 -- is because in an instantiation Par.P.Q will not resolve to the
14974 -- name of the instance, whose enclosing scope is not necessarily
14975 -- Par. We use the generic homonym rather that the name of the
14976 -- generic itself because it may be hidden by a local declaration.
14978 elsif In_Open_Scopes
(Entity
(Parent
(N2
)))
14980 Is_Generic_Unit
(Top_Ancestor
(Entity
(Prefix
(Parent
(N2
)))))
14982 if Ekind
(Entity
(Parent
(N2
))) = E_Generic_Package
then
14983 Rewrite
(Parent
(N
),
14984 Make_Identifier
(Sloc
(N
),
14986 Chars
(Generic_Homonym
(Entity
(Parent
(N2
))))));
14988 Rewrite
(Parent
(N
),
14989 Make_Identifier
(Sloc
(N
),
14990 Chars
=> Chars
(Selector_Name
(Parent
(N2
)))));
14994 if Nkind
(Parent
(Parent
(N
))) in N_Generic_Instantiation
14995 and then Parent
(N
) = Name
(Parent
(Parent
(N
)))
14997 Save_Global_Defaults
14998 (Parent
(Parent
(N
)), Parent
(Parent
(N2
)));
15001 -- A selected component may denote a static constant that has been
15002 -- folded. If the static constant is global to the generic, capture
15003 -- its value. Otherwise the folding will happen in any instantiation.
15005 elsif Nkind
(Parent
(N
)) = N_Selected_Component
15006 and then Nkind_In
(Parent
(N2
), N_Integer_Literal
, N_Real_Literal
)
15008 if Present
(Entity
(Original_Node
(Parent
(N2
))))
15009 and then Is_Global
(Entity
(Original_Node
(Parent
(N2
))))
15011 Rewrite
(Parent
(N
), New_Copy
(Parent
(N2
)));
15012 Set_Analyzed
(Parent
(N
), False);
15015 -- A selected component may be transformed into a parameterless
15016 -- function call. If the called entity is global, rewrite the node
15017 -- appropriately, i.e. as an extended name for the global entity.
15019 elsif Nkind
(Parent
(N
)) = N_Selected_Component
15020 and then Nkind
(Parent
(N2
)) = N_Function_Call
15021 and then N
= Selector_Name
(Parent
(N
))
15023 if No
(Parameter_Associations
(Parent
(N2
))) then
15024 if Is_Global
(Entity
(Name
(Parent
(N2
)))) then
15025 Change_Selected_Component_To_Expanded_Name
(Parent
(N
));
15026 Set_Associated_Node
(Parent
(N
), Name
(Parent
(N2
)));
15027 Set_Global_Type
(Parent
(N
), Name
(Parent
(N2
)));
15028 Save_Entity_Descendants
(N
);
15031 Set_Is_Prefixed_Call
(Parent
(N
));
15032 Set_Associated_Node
(N
, Empty
);
15033 Set_Etype
(N
, Empty
);
15036 -- In Ada 2005, X.F may be a call to a primitive operation,
15037 -- rewritten as F (X). This rewriting will be done again in an
15038 -- instance, so keep the original node. Global entities will be
15039 -- captured as for other constructs. Indicate that this must
15040 -- resolve as a call, to prevent accidental overloading in the
15041 -- instance, if both a component and a primitive operation appear
15045 Set_Is_Prefixed_Call
(Parent
(N
));
15048 -- Entity is local. Reset in generic unit, so that node is resolved
15049 -- anew at the point of instantiation.
15052 Set_Associated_Node
(N
, Empty
);
15053 Set_Etype
(N
, Empty
);
15057 -----------------------------
15058 -- Save_Entity_Descendants --
15059 -----------------------------
15061 procedure Save_Entity_Descendants
(N
: Node_Id
) is
15064 when N_Binary_Op
=>
15065 Save_Global_Descendant
(Union_Id
(Left_Opnd
(N
)));
15066 Save_Global_Descendant
(Union_Id
(Right_Opnd
(N
)));
15069 Save_Global_Descendant
(Union_Id
(Right_Opnd
(N
)));
15071 when N_Expanded_Name
15072 | N_Selected_Component
15074 Save_Global_Descendant
(Union_Id
(Prefix
(N
)));
15075 Save_Global_Descendant
(Union_Id
(Selector_Name
(N
)));
15077 when N_Character_Literal
15079 | N_Operator_Symbol
15084 raise Program_Error
;
15086 end Save_Entity_Descendants
;
15088 --------------------------
15089 -- Save_Global_Defaults --
15090 --------------------------
15092 procedure Save_Global_Defaults
(N1
: Node_Id
; N2
: Node_Id
) is
15093 Loc
: constant Source_Ptr
:= Sloc
(N1
);
15094 Assoc2
: constant List_Id
:= Generic_Associations
(N2
);
15095 Gen_Id
: constant Entity_Id
:= Get_Generic_Entity
(N2
);
15102 Actual
: Entity_Id
;
15105 Assoc1
:= Generic_Associations
(N1
);
15107 if Present
(Assoc1
) then
15108 Act1
:= First
(Assoc1
);
15111 Set_Generic_Associations
(N1
, New_List
);
15112 Assoc1
:= Generic_Associations
(N1
);
15115 if Present
(Assoc2
) then
15116 Act2
:= First
(Assoc2
);
15121 while Present
(Act1
) and then Present
(Act2
) loop
15126 -- Find the associations added for default subprograms
15128 if Present
(Act2
) then
15129 while Nkind
(Act2
) /= N_Generic_Association
15130 or else No
(Entity
(Selector_Name
(Act2
)))
15131 or else not Is_Overloadable
(Entity
(Selector_Name
(Act2
)))
15136 -- Add a similar association if the default is global. The
15137 -- renaming declaration for the actual has been analyzed, and
15138 -- its alias is the program it renames. Link the actual in the
15139 -- original generic tree with the node in the analyzed tree.
15141 while Present
(Act2
) loop
15142 Subp
:= Entity
(Selector_Name
(Act2
));
15143 Def
:= Explicit_Generic_Actual_Parameter
(Act2
);
15145 -- Following test is defence against rubbish errors
15147 if No
(Alias
(Subp
)) then
15151 -- Retrieve the resolved actual from the renaming declaration
15152 -- created for the instantiated formal.
15154 Actual
:= Entity
(Name
(Parent
(Parent
(Subp
))));
15155 Set_Entity
(Def
, Actual
);
15156 Set_Etype
(Def
, Etype
(Actual
));
15158 if Is_Global
(Actual
) then
15160 Make_Generic_Association
(Loc
,
15162 New_Occurrence_Of
(Subp
, Loc
),
15163 Explicit_Generic_Actual_Parameter
=>
15164 New_Occurrence_Of
(Actual
, Loc
));
15166 Set_Associated_Node
15167 (Explicit_Generic_Actual_Parameter
(Ndec
), Def
);
15169 Append
(Ndec
, Assoc1
);
15171 -- If there are other defaults, add a dummy association in case
15172 -- there are other defaulted formals with the same name.
15174 elsif Present
(Next
(Act2
)) then
15176 Make_Generic_Association
(Loc
,
15178 New_Occurrence_Of
(Subp
, Loc
),
15179 Explicit_Generic_Actual_Parameter
=> Empty
);
15181 Append
(Ndec
, Assoc1
);
15188 if Nkind
(Name
(N1
)) = N_Identifier
15189 and then Is_Child_Unit
(Gen_Id
)
15190 and then Is_Global
(Gen_Id
)
15191 and then Is_Generic_Unit
(Scope
(Gen_Id
))
15192 and then In_Open_Scopes
(Scope
(Gen_Id
))
15194 -- This is an instantiation of a child unit within a sibling, so
15195 -- that the generic parent is in scope. An eventual instance must
15196 -- occur within the scope of an instance of the parent. Make name
15197 -- in instance into an expanded name, to preserve the identifier
15198 -- of the parent, so it can be resolved subsequently.
15200 Rewrite
(Name
(N2
),
15201 Make_Expanded_Name
(Loc
,
15202 Chars
=> Chars
(Gen_Id
),
15203 Prefix
=> New_Occurrence_Of
(Scope
(Gen_Id
), Loc
),
15204 Selector_Name
=> New_Occurrence_Of
(Gen_Id
, Loc
)));
15205 Set_Entity
(Name
(N2
), Gen_Id
);
15207 Rewrite
(Name
(N1
),
15208 Make_Expanded_Name
(Loc
,
15209 Chars
=> Chars
(Gen_Id
),
15210 Prefix
=> New_Occurrence_Of
(Scope
(Gen_Id
), Loc
),
15211 Selector_Name
=> New_Occurrence_Of
(Gen_Id
, Loc
)));
15213 Set_Associated_Node
(Name
(N1
), Name
(N2
));
15214 Set_Associated_Node
(Prefix
(Name
(N1
)), Empty
);
15215 Set_Associated_Node
15216 (Selector_Name
(Name
(N1
)), Selector_Name
(Name
(N2
)));
15217 Set_Etype
(Name
(N1
), Etype
(Gen_Id
));
15219 end Save_Global_Defaults
;
15221 ----------------------------
15222 -- Save_Global_Descendant --
15223 ----------------------------
15225 procedure Save_Global_Descendant
(D
: Union_Id
) is
15229 if D
in Node_Range
then
15230 if D
= Union_Id
(Empty
) then
15233 elsif Nkind
(Node_Id
(D
)) /= N_Compilation_Unit
then
15234 Save_References
(Node_Id
(D
));
15237 elsif D
in List_Range
then
15238 pragma Assert
(D
/= Union_Id
(No_List
));
15239 -- Because No_List = Empty, which is in Node_Range above
15241 if Is_Empty_List
(List_Id
(D
)) then
15245 N1
:= First
(List_Id
(D
));
15246 while Present
(N1
) loop
15247 Save_References
(N1
);
15252 -- Element list or other non-node field, nothing to do
15257 end Save_Global_Descendant
;
15259 ---------------------
15260 -- Save_References --
15261 ---------------------
15263 -- This is the recursive procedure that does the work once the enclosing
15264 -- generic scope has been established. We have to treat specially a
15265 -- number of node rewritings that are required by semantic processing
15266 -- and which change the kind of nodes in the generic copy: typically
15267 -- constant-folding, replacing an operator node by a string literal, or
15268 -- a selected component by an expanded name. In each of those cases, the
15269 -- transformation is propagated to the generic unit.
15271 procedure Save_References
(N
: Node_Id
) is
15272 Loc
: constant Source_Ptr
:= Sloc
(N
);
15274 function Requires_Delayed_Save
(Nod
: Node_Id
) return Boolean;
15275 -- Determine whether arbitrary node Nod requires delayed capture of
15276 -- global references within its aspect specifications.
15278 procedure Save_References_In_Aggregate
(N
: Node_Id
);
15279 -- Save all global references in [extension] aggregate node N
15281 procedure Save_References_In_Char_Lit_Or_Op_Symbol
(N
: Node_Id
);
15282 -- Save all global references in a character literal or operator
15283 -- symbol denoted by N.
15285 procedure Save_References_In_Descendants
(N
: Node_Id
);
15286 -- Save all global references in all descendants of node N
15288 procedure Save_References_In_Identifier
(N
: Node_Id
);
15289 -- Save all global references in identifier node N
15291 procedure Save_References_In_Operator
(N
: Node_Id
);
15292 -- Save all global references in operator node N
15294 procedure Save_References_In_Pragma
(Prag
: Node_Id
);
15295 -- Save all global references found within the expression of pragma
15298 ---------------------------
15299 -- Requires_Delayed_Save --
15300 ---------------------------
15302 function Requires_Delayed_Save
(Nod
: Node_Id
) return Boolean is
15304 -- Generic packages and subprograms require delayed capture of
15305 -- global references within their aspects due to the timing of
15306 -- annotation analysis.
15308 if Nkind_In
(Nod
, N_Generic_Package_Declaration
,
15309 N_Generic_Subprogram_Declaration
,
15311 N_Package_Body_Stub
,
15313 N_Subprogram_Body_Stub
)
15315 -- Since the capture of global references is done on the
15316 -- unanalyzed generic template, there is no information around
15317 -- to infer the context. Use the Associated_Entity linkages to
15318 -- peek into the analyzed generic copy and determine what the
15319 -- template corresponds to.
15321 if Nod
= Templ
then
15323 Is_Generic_Declaration_Or_Body
15324 (Unit_Declaration_Node
15325 (Associated_Entity
(Defining_Entity
(Nod
))));
15327 -- Otherwise the generic unit being processed is not the top
15328 -- level template. It is safe to capture of global references
15329 -- within the generic unit because at this point the top level
15330 -- copy is fully analyzed.
15336 -- Otherwise capture the global references without interference
15341 end Requires_Delayed_Save
;
15343 ----------------------------------
15344 -- Save_References_In_Aggregate --
15345 ----------------------------------
15347 procedure Save_References_In_Aggregate
(N
: Node_Id
) is
15349 Qual
: Node_Id
:= Empty
;
15350 Typ
: Entity_Id
:= Empty
;
15352 use Atree
.Unchecked_Access
;
15353 -- This code section is part of implementing an untyped tree
15354 -- traversal, so it needs direct access to node fields.
15357 N2
:= Get_Associated_Node
(N
);
15359 if Present
(N2
) then
15362 -- In an instance within a generic, use the name of the actual
15363 -- and not the original generic parameter. If the actual is
15364 -- global in the current generic it must be preserved for its
15367 if Nkind
(Parent
(Typ
)) = N_Subtype_Declaration
15368 and then Present
(Generic_Parent_Type
(Parent
(Typ
)))
15370 Typ
:= Base_Type
(Typ
);
15371 Set_Etype
(N2
, Typ
);
15375 if No
(N2
) or else No
(Typ
) or else not Is_Global
(Typ
) then
15376 Set_Associated_Node
(N
, Empty
);
15378 -- If the aggregate is an actual in a call, it has been
15379 -- resolved in the current context, to some local type. The
15380 -- enclosing call may have been disambiguated by the aggregate,
15381 -- and this disambiguation might fail at instantiation time
15382 -- because the type to which the aggregate did resolve is not
15383 -- preserved. In order to preserve some of this information,
15384 -- wrap the aggregate in a qualified expression, using the id
15385 -- of its type. For further disambiguation we qualify the type
15386 -- name with its scope (if visible and not hidden by a local
15387 -- homograph) because both id's will have corresponding
15388 -- entities in an instance. This resolves most of the problems
15389 -- with missing type information on aggregates in instances.
15392 and then Nkind
(N2
) = Nkind
(N
)
15393 and then Nkind
(Parent
(N2
)) in N_Subprogram_Call
15394 and then Present
(Typ
)
15395 and then Comes_From_Source
(Typ
)
15397 Nam
:= Make_Identifier
(Loc
, Chars
(Typ
));
15399 if Is_Immediately_Visible
(Scope
(Typ
))
15401 (not In_Open_Scopes
(Scope
(Typ
))
15402 or else Current_Entity
(Scope
(Typ
)) = Scope
(Typ
))
15405 Make_Selected_Component
(Loc
,
15407 Make_Identifier
(Loc
, Chars
(Scope
(Typ
))),
15408 Selector_Name
=> Nam
);
15412 Make_Qualified_Expression
(Loc
,
15413 Subtype_Mark
=> Nam
,
15414 Expression
=> Relocate_Node
(N
));
15418 Save_Global_Descendant
(Field1
(N
));
15419 Save_Global_Descendant
(Field2
(N
));
15420 Save_Global_Descendant
(Field3
(N
));
15421 Save_Global_Descendant
(Field5
(N
));
15423 if Present
(Qual
) then
15426 end Save_References_In_Aggregate
;
15428 ----------------------------------------------
15429 -- Save_References_In_Char_Lit_Or_Op_Symbol --
15430 ----------------------------------------------
15432 procedure Save_References_In_Char_Lit_Or_Op_Symbol
(N
: Node_Id
) is
15434 if Nkind
(N
) = Nkind
(Get_Associated_Node
(N
)) then
15437 elsif Nkind
(N
) = N_Operator_Symbol
15438 and then Nkind
(Get_Associated_Node
(N
)) = N_String_Literal
15440 Change_Operator_Symbol_To_String_Literal
(N
);
15442 end Save_References_In_Char_Lit_Or_Op_Symbol
;
15444 ------------------------------------
15445 -- Save_References_In_Descendants --
15446 ------------------------------------
15448 procedure Save_References_In_Descendants
(N
: Node_Id
) is
15449 use Atree
.Unchecked_Access
;
15450 -- This code section is part of implementing an untyped tree
15451 -- traversal, so it needs direct access to node fields.
15454 Save_Global_Descendant
(Field1
(N
));
15455 Save_Global_Descendant
(Field2
(N
));
15456 Save_Global_Descendant
(Field3
(N
));
15457 Save_Global_Descendant
(Field4
(N
));
15458 Save_Global_Descendant
(Field5
(N
));
15459 end Save_References_In_Descendants
;
15461 -----------------------------------
15462 -- Save_References_In_Identifier --
15463 -----------------------------------
15465 procedure Save_References_In_Identifier
(N
: Node_Id
) is
15467 -- The node did not undergo a transformation
15469 if Nkind
(N
) = Nkind
(Get_Associated_Node
(N
)) then
15471 Aux_N2
: constant Node_Id
:= Get_Associated_Node
(N
);
15472 Orig_N2_Parent
: constant Node_Id
:=
15473 Original_Node
(Parent
(Aux_N2
));
15475 -- The parent of this identifier is a selected component
15476 -- which denotes a named number that was constant folded.
15477 -- Preserve the original name for ASIS and link the parent
15478 -- with its expanded name. The constant folding will be
15479 -- repeated in the instance.
15481 if Nkind
(Parent
(N
)) = N_Selected_Component
15482 and then Nkind_In
(Parent
(Aux_N2
), N_Integer_Literal
,
15484 and then Is_Entity_Name
(Orig_N2_Parent
)
15485 and then Ekind
(Entity
(Orig_N2_Parent
)) in Named_Kind
15486 and then Is_Global
(Entity
(Orig_N2_Parent
))
15489 Set_Associated_Node
15490 (Parent
(N
), Original_Node
(Parent
(N2
)));
15495 -- If this is a discriminant reference, always save it.
15496 -- It is used in the instance to find the corresponding
15497 -- discriminant positionally rather than by name.
15499 Set_Original_Discriminant
15500 (N
, Original_Discriminant
(Get_Associated_Node
(N
)));
15506 -- The analysis of the generic copy transformed the identifier
15507 -- into another construct. Propagate the changes to the template.
15510 N2
:= Get_Associated_Node
(N
);
15512 -- The identifier denotes a call to a parameterless function.
15513 -- Mark the node as resolved when the function is external.
15515 if Nkind
(N2
) = N_Function_Call
then
15516 E
:= Entity
(Name
(N2
));
15518 if Present
(E
) and then Is_Global
(E
) then
15519 Set_Etype
(N
, Etype
(N2
));
15521 Set_Associated_Node
(N
, Empty
);
15522 Set_Etype
(N
, Empty
);
15525 -- The identifier denotes a named number that was constant
15526 -- folded. Preserve the original name for ASIS and undo the
15527 -- constant folding which will be repeated in the instance.
15529 elsif Nkind_In
(N2
, N_Integer_Literal
, N_Real_Literal
)
15530 and then Is_Entity_Name
(Original_Node
(N2
))
15532 Set_Associated_Node
(N
, Original_Node
(N2
));
15535 -- The identifier resolved to a string literal. Propagate this
15536 -- information to the generic template.
15538 elsif Nkind
(N2
) = N_String_Literal
then
15539 Rewrite
(N
, New_Copy
(N2
));
15541 -- The identifier is rewritten as a dereference if it is the
15542 -- prefix of an implicit dereference. Preserve the original
15543 -- tree as the analysis of the instance will expand the node
15544 -- again, but preserve the resolved entity if it is global.
15546 elsif Nkind
(N2
) = N_Explicit_Dereference
then
15547 if Is_Entity_Name
(Prefix
(N2
))
15548 and then Present
(Entity
(Prefix
(N2
)))
15549 and then Is_Global
(Entity
(Prefix
(N2
)))
15551 Set_Associated_Node
(N
, Prefix
(N2
));
15553 elsif Nkind
(Prefix
(N2
)) = N_Function_Call
15554 and then Present
(Entity
(Name
(Prefix
(N2
))))
15555 and then Is_Global
(Entity
(Name
(Prefix
(N2
))))
15558 Make_Explicit_Dereference
(Loc
,
15560 Make_Function_Call
(Loc
,
15563 (Entity
(Name
(Prefix
(N2
))), Loc
))));
15566 Set_Associated_Node
(N
, Empty
);
15567 Set_Etype
(N
, Empty
);
15570 -- The subtype mark of a nominally unconstrained object is
15571 -- rewritten as a subtype indication using the bounds of the
15572 -- expression. Recover the original subtype mark.
15574 elsif Nkind
(N2
) = N_Subtype_Indication
15575 and then Is_Entity_Name
(Original_Node
(N2
))
15577 Set_Associated_Node
(N
, Original_Node
(N2
));
15581 end Save_References_In_Identifier
;
15583 ---------------------------------
15584 -- Save_References_In_Operator --
15585 ---------------------------------
15587 procedure Save_References_In_Operator
(N
: Node_Id
) is
15589 -- The node did not undergo a transformation
15591 if Nkind
(N
) = Nkind
(Get_Associated_Node
(N
)) then
15592 if Nkind
(N
) = N_Op_Concat
then
15593 Set_Is_Component_Left_Opnd
(N
,
15594 Is_Component_Left_Opnd
(Get_Associated_Node
(N
)));
15596 Set_Is_Component_Right_Opnd
(N
,
15597 Is_Component_Right_Opnd
(Get_Associated_Node
(N
)));
15602 -- The analysis of the generic copy transformed the operator into
15603 -- some other construct. Propagate the changes to the template if
15607 N2
:= Get_Associated_Node
(N
);
15609 -- The operator resoved to a function call
15611 if Nkind
(N2
) = N_Function_Call
then
15613 -- Add explicit qualifications in the generic template for
15614 -- all operands of universal type. This aids resolution by
15615 -- preserving the actual type of a literal or an attribute
15616 -- that yields a universal result.
15618 Qualify_Universal_Operands
(N
, N2
);
15620 E
:= Entity
(Name
(N2
));
15622 if Present
(E
) and then Is_Global
(E
) then
15623 Set_Etype
(N
, Etype
(N2
));
15625 Set_Associated_Node
(N
, Empty
);
15626 Set_Etype
(N
, Empty
);
15629 -- The operator was folded into a literal
15631 elsif Nkind_In
(N2
, N_Integer_Literal
,
15635 if Present
(Original_Node
(N2
))
15636 and then Nkind
(Original_Node
(N2
)) = Nkind
(N
)
15638 -- Operation was constant-folded. Whenever possible,
15639 -- recover semantic information from unfolded node,
15642 Set_Associated_Node
(N
, Original_Node
(N2
));
15644 if Nkind
(N
) = N_Op_Concat
then
15645 Set_Is_Component_Left_Opnd
(N
,
15646 Is_Component_Left_Opnd
(Get_Associated_Node
(N
)));
15647 Set_Is_Component_Right_Opnd
(N
,
15648 Is_Component_Right_Opnd
(Get_Associated_Node
(N
)));
15653 -- Propagate the constant folding back to the template
15656 Rewrite
(N
, New_Copy
(N2
));
15657 Set_Analyzed
(N
, False);
15660 -- The operator was folded into an enumeration literal. Retain
15661 -- the entity to avoid spurious ambiguities if it is overloaded
15662 -- at the point of instantiation or inlining.
15664 elsif Nkind
(N2
) = N_Identifier
15665 and then Ekind
(Entity
(N2
)) = E_Enumeration_Literal
15667 Rewrite
(N
, New_Copy
(N2
));
15668 Set_Analyzed
(N
, False);
15672 -- Complete the operands check if node has not been constant
15675 if Nkind
(N
) in N_Op
then
15676 Save_Entity_Descendants
(N
);
15678 end Save_References_In_Operator
;
15680 -------------------------------
15681 -- Save_References_In_Pragma --
15682 -------------------------------
15684 procedure Save_References_In_Pragma
(Prag
: Node_Id
) is
15686 Do_Save
: Boolean := True;
15688 use Atree
.Unchecked_Access
;
15689 -- This code section is part of implementing an untyped tree
15690 -- traversal, so it needs direct access to node fields.
15693 -- Do not save global references in pragmas generated from aspects
15694 -- because the pragmas will be regenerated at instantiation time.
15696 if From_Aspect_Specification
(Prag
) then
15699 -- The capture of global references within contract-related source
15700 -- pragmas associated with generic packages, subprograms or their
15701 -- respective bodies must be delayed due to timing of annotation
15702 -- analysis. Global references are still captured in routine
15703 -- Save_Global_References_In_Contract.
15705 elsif Is_Generic_Contract_Pragma
(Prag
) and then Prag
/= Templ
then
15706 if Is_Package_Contract_Annotation
(Prag
) then
15707 Context
:= Find_Related_Package_Or_Body
(Prag
);
15709 pragma Assert
(Is_Subprogram_Contract_Annotation
(Prag
));
15710 Context
:= Find_Related_Declaration_Or_Body
(Prag
);
15713 -- The use of Original_Node accounts for the case when the
15714 -- related context is generic template.
15716 if Requires_Delayed_Save
(Original_Node
(Context
)) then
15721 -- For all other cases, save all global references within the
15722 -- descendants, but skip the following semantic fields:
15724 -- Field1 - Next_Pragma
15725 -- Field3 - Corresponding_Aspect
15726 -- Field5 - Next_Rep_Item
15729 Save_Global_Descendant
(Field2
(Prag
));
15730 Save_Global_Descendant
(Field4
(Prag
));
15732 end Save_References_In_Pragma
;
15734 -- Start of processing for Save_References
15742 elsif Nkind_In
(N
, N_Aggregate
, N_Extension_Aggregate
) then
15743 Save_References_In_Aggregate
(N
);
15745 -- Character literals, operator symbols
15747 elsif Nkind_In
(N
, N_Character_Literal
, N_Operator_Symbol
) then
15748 Save_References_In_Char_Lit_Or_Op_Symbol
(N
);
15750 -- Defining identifiers
15752 elsif Nkind
(N
) in N_Entity
then
15757 elsif Nkind
(N
) = N_Identifier
then
15758 Save_References_In_Identifier
(N
);
15762 elsif Nkind
(N
) in N_Op
then
15763 Save_References_In_Operator
(N
);
15767 elsif Nkind
(N
) = N_Pragma
then
15768 Save_References_In_Pragma
(N
);
15771 Save_References_In_Descendants
(N
);
15774 -- Save all global references found within the aspect specifications
15775 -- of the related node.
15777 if Permits_Aspect_Specifications
(N
) and then Has_Aspects
(N
) then
15779 -- The capture of global references within aspects associated with
15780 -- generic packages, subprograms or their bodies must be delayed
15781 -- due to timing of annotation analysis. Global references are
15782 -- still captured in routine Save_Global_References_In_Contract.
15784 if Requires_Delayed_Save
(N
) then
15787 -- Otherwise save all global references within the aspects
15790 Save_Global_References_In_Aspects
(N
);
15793 end Save_References
;
15795 -- Start of processing for Save_Global_References
15798 Gen_Scope
:= Current_Scope
;
15800 -- If the generic unit is a child unit, references to entities in the
15801 -- parent are treated as local, because they will be resolved anew in
15802 -- the context of the instance of the parent.
15804 while Is_Child_Unit
(Gen_Scope
)
15805 and then Ekind
(Scope
(Gen_Scope
)) = E_Generic_Package
15807 Gen_Scope
:= Scope
(Gen_Scope
);
15810 Save_References
(Templ
);
15811 end Save_Global_References
;
15813 ---------------------------------------
15814 -- Save_Global_References_In_Aspects --
15815 ---------------------------------------
15817 procedure Save_Global_References_In_Aspects
(N
: Node_Id
) is
15822 Asp
:= First
(Aspect_Specifications
(N
));
15823 while Present
(Asp
) loop
15824 Expr
:= Expression
(Asp
);
15826 if Present
(Expr
) then
15827 Save_Global_References
(Expr
);
15832 end Save_Global_References_In_Aspects
;
15834 ------------------------------------------
15835 -- Set_Copied_Sloc_For_Inherited_Pragma --
15836 ------------------------------------------
15838 procedure Set_Copied_Sloc_For_Inherited_Pragma
15843 Create_Instantiation_Source
(N
, E
,
15844 Inlined_Body
=> False,
15845 Inherited_Pragma
=> True,
15846 Factor
=> S_Adjustment
);
15847 end Set_Copied_Sloc_For_Inherited_Pragma
;
15849 --------------------------------------
15850 -- Set_Copied_Sloc_For_Inlined_Body --
15851 --------------------------------------
15853 procedure Set_Copied_Sloc_For_Inlined_Body
(N
: Node_Id
; E
: Entity_Id
) is
15855 Create_Instantiation_Source
(N
, E
,
15856 Inlined_Body
=> True,
15857 Inherited_Pragma
=> False,
15858 Factor
=> S_Adjustment
);
15859 end Set_Copied_Sloc_For_Inlined_Body
;
15861 ---------------------
15862 -- Set_Instance_Of --
15863 ---------------------
15865 procedure Set_Instance_Of
(A
: Entity_Id
; B
: Entity_Id
) is
15867 Generic_Renamings
.Table
(Generic_Renamings
.Last
) := (A
, B
, Assoc_Null
);
15868 Generic_Renamings_HTable
.Set
(Generic_Renamings
.Last
);
15869 Generic_Renamings
.Increment_Last
;
15870 end Set_Instance_Of
;
15872 --------------------
15873 -- Set_Next_Assoc --
15874 --------------------
15876 procedure Set_Next_Assoc
(E
: Assoc_Ptr
; Next
: Assoc_Ptr
) is
15878 Generic_Renamings
.Table
(E
).Next_In_HTable
:= Next
;
15879 end Set_Next_Assoc
;
15881 -------------------
15882 -- Start_Generic --
15883 -------------------
15885 procedure Start_Generic
is
15887 -- ??? More things could be factored out in this routine.
15888 -- Should probably be done at a later stage.
15890 Generic_Flags
.Append
(Inside_A_Generic
);
15891 Inside_A_Generic
:= True;
15893 Expander_Mode_Save_And_Set
(False);
15896 ----------------------
15897 -- Set_Instance_Env --
15898 ----------------------
15900 -- WARNING: This routine manages SPARK regions
15902 procedure Set_Instance_Env
15903 (Gen_Unit
: Entity_Id
;
15904 Act_Unit
: Entity_Id
)
15906 Saved_AE
: constant Boolean := Assertions_Enabled
;
15907 Saved_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
15908 Saved_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
15909 -- Save the SPARK mode-related data because utilizing the configuration
15910 -- values of pragmas and switches will eliminate any previously set
15914 -- Regardless of the current mode, predefined units are analyzed in the
15915 -- most current Ada mode, and earlier version Ada checks do not apply
15916 -- to predefined units. Nothing needs to be done for non-internal units.
15917 -- These are always analyzed in the current mode.
15919 if In_Internal_Unit
(Gen_Unit
) then
15920 Set_Opt_Config_Switches
(True, Current_Sem_Unit
= Main_Unit
);
15922 -- In Ada2012 we may want to enable assertions in an instance of a
15923 -- predefined unit, in which case we need to preserve the current
15924 -- setting for the Assertions_Enabled flag. This will become more
15925 -- critical when pre/postconditions are added to predefined units,
15926 -- as is already the case for some numeric libraries.
15928 if Ada_Version
>= Ada_2012
then
15929 Assertions_Enabled
:= Saved_AE
;
15932 -- Reinstall the SPARK_Mode which was in effect at the point of
15935 Install_SPARK_Mode
(Saved_SM
, Saved_SMP
);
15938 Current_Instantiated_Parent
:=
15939 (Gen_Id
=> Gen_Unit
,
15940 Act_Id
=> Act_Unit
,
15941 Next_In_HTable
=> Assoc_Null
);
15942 end Set_Instance_Env
;
15948 procedure Switch_View
(T
: Entity_Id
) is
15949 BT
: constant Entity_Id
:= Base_Type
(T
);
15950 Priv_Elmt
: Elmt_Id
:= No_Elmt
;
15951 Priv_Sub
: Entity_Id
;
15954 -- T may be private but its base type may have been exchanged through
15955 -- some other occurrence, in which case there is nothing to switch
15956 -- besides T itself. Note that a private dependent subtype of a private
15957 -- type might not have been switched even if the base type has been,
15958 -- because of the last branch of Check_Private_View (see comment there).
15960 if not Is_Private_Type
(BT
) then
15961 Prepend_Elmt
(Full_View
(T
), Exchanged_Views
);
15962 Exchange_Declarations
(T
);
15966 Priv_Elmt
:= First_Elmt
(Private_Dependents
(BT
));
15968 if Present
(Full_View
(BT
)) then
15969 Prepend_Elmt
(Full_View
(BT
), Exchanged_Views
);
15970 Exchange_Declarations
(BT
);
15973 while Present
(Priv_Elmt
) loop
15974 Priv_Sub
:= (Node
(Priv_Elmt
));
15976 -- We avoid flipping the subtype if the Etype of its full view is
15977 -- private because this would result in a malformed subtype. This
15978 -- occurs when the Etype of the subtype full view is the full view of
15979 -- the base type (and since the base types were just switched, the
15980 -- subtype is pointing to the wrong view). This is currently the case
15981 -- for tagged record types, access types (maybe more?) and needs to
15982 -- be resolved. ???
15984 if Present
(Full_View
(Priv_Sub
))
15985 and then not Is_Private_Type
(Etype
(Full_View
(Priv_Sub
)))
15987 Prepend_Elmt
(Full_View
(Priv_Sub
), Exchanged_Views
);
15988 Exchange_Declarations
(Priv_Sub
);
15991 Next_Elmt
(Priv_Elmt
);
15999 function True_Parent
(N
: Node_Id
) return Node_Id
is
16001 if Nkind
(Parent
(N
)) = N_Subunit
then
16002 return Parent
(Corresponding_Stub
(Parent
(N
)));
16008 -----------------------------
16009 -- Valid_Default_Attribute --
16010 -----------------------------
16012 procedure Valid_Default_Attribute
(Nam
: Entity_Id
; Def
: Node_Id
) is
16013 Attr_Id
: constant Attribute_Id
:=
16014 Get_Attribute_Id
(Attribute_Name
(Def
));
16015 T
: constant Entity_Id
:= Entity
(Prefix
(Def
));
16016 Is_Fun
: constant Boolean := (Ekind
(Nam
) = E_Function
);
16022 if No
(T
) or else T
= Any_Id
then
16027 F
:= First_Formal
(Nam
);
16028 while Present
(F
) loop
16029 Num_F
:= Num_F
+ 1;
16034 when Attribute_Adjacent
16035 | Attribute_Ceiling
16036 | Attribute_Copy_Sign
16038 | Attribute_Fraction
16039 | Attribute_Machine
16041 | Attribute_Remainder
16042 | Attribute_Rounding
16043 | Attribute_Unbiased_Rounding
16047 and then Is_Floating_Point_Type
(T
);
16049 when Attribute_Image
16053 | Attribute_Wide_Image
16054 | Attribute_Wide_Value
16056 OK
:= Is_Fun
and then Num_F
= 1 and then Is_Scalar_Type
(T
);
16061 OK
:= Is_Fun
and then Num_F
= 2 and then Is_Scalar_Type
(T
);
16063 when Attribute_Input
=>
16064 OK
:= (Is_Fun
and then Num_F
= 1);
16066 when Attribute_Output
16070 OK
:= not Is_Fun
and then Num_F
= 2;
16078 ("attribute reference has wrong profile for subprogram", Def
);
16080 end Valid_Default_Attribute
;