1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2016, 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 Remove_Parent
(In_Body
: Boolean := False);
843 -- Reverse effect after instantiation of child is complete
845 procedure Restore_Hidden_Primitives
(Prims_List
: in out Elist_Id
);
846 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
849 procedure Set_Instance_Env
850 (Gen_Unit
: Entity_Id
;
851 Act_Unit
: Entity_Id
);
852 -- Save current instance on saved environment, to be used to determine
853 -- the global status of entities in nested instances. Part of Save_Env.
854 -- called after verifying that the generic unit is legal for the instance,
855 -- The procedure also examines whether the generic unit is a predefined
856 -- unit, in order to set configuration switches accordingly. As a result
857 -- the procedure must be called after analyzing and freezing the actuals.
859 procedure Set_Instance_Of
(A
: Entity_Id
; B
: Entity_Id
);
860 -- Associate analyzed generic parameter with corresponding instance. Used
861 -- for semantic checks at instantiation time.
863 function True_Parent
(N
: Node_Id
) return Node_Id
;
864 -- For a subunit, return parent of corresponding stub, else return
867 procedure Valid_Default_Attribute
(Nam
: Entity_Id
; Def
: Node_Id
);
868 -- Verify that an attribute that appears as the default for a formal
869 -- subprogram is a function or procedure with the correct profile.
871 -------------------------------------------
872 -- Data Structures for Generic Renamings --
873 -------------------------------------------
875 -- The map Generic_Renamings associates generic entities with their
876 -- corresponding actuals. Currently used to validate type instances. It
877 -- will eventually be used for all generic parameters to eliminate the
878 -- need for overload resolution in the instance.
880 type Assoc_Ptr
is new Int
;
882 Assoc_Null
: constant Assoc_Ptr
:= -1;
887 Next_In_HTable
: Assoc_Ptr
;
890 package Generic_Renamings
is new Table
.Table
891 (Table_Component_Type
=> Assoc
,
892 Table_Index_Type
=> Assoc_Ptr
,
893 Table_Low_Bound
=> 0,
895 Table_Increment
=> 100,
896 Table_Name
=> "Generic_Renamings");
898 -- Variable to hold enclosing instantiation. When the environment is
899 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
901 Current_Instantiated_Parent
: Assoc
:= (Empty
, Empty
, Assoc_Null
);
903 -- Hash table for associations
905 HTable_Size
: constant := 37;
906 type HTable_Range
is range 0 .. HTable_Size
- 1;
908 procedure Set_Next_Assoc
(E
: Assoc_Ptr
; Next
: Assoc_Ptr
);
909 function Next_Assoc
(E
: Assoc_Ptr
) return Assoc_Ptr
;
910 function Get_Gen_Id
(E
: Assoc_Ptr
) return Entity_Id
;
911 function Hash
(F
: Entity_Id
) return HTable_Range
;
913 package Generic_Renamings_HTable
is new GNAT
.HTable
.Static_HTable
(
914 Header_Num
=> HTable_Range
,
916 Elmt_Ptr
=> Assoc_Ptr
,
917 Null_Ptr
=> Assoc_Null
,
918 Set_Next
=> Set_Next_Assoc
,
921 Get_Key
=> Get_Gen_Id
,
925 Exchanged_Views
: Elist_Id
;
926 -- This list holds the private views that have been exchanged during
927 -- instantiation to restore the visibility of the generic declaration.
928 -- (see comments above). After instantiation, the current visibility is
929 -- reestablished by means of a traversal of this list.
931 Hidden_Entities
: Elist_Id
;
932 -- This list holds the entities of the current scope that are removed
933 -- from immediate visibility when instantiating a child unit. Their
934 -- visibility is restored in Remove_Parent.
936 -- Because instantiations can be recursive, the following must be saved
937 -- on entry and restored on exit from an instantiation (spec or body).
938 -- This is done by the two procedures Save_Env and Restore_Env. For
939 -- package and subprogram instantiations (but not for the body instances)
940 -- the action of Save_Env is done in two steps: Init_Env is called before
941 -- Check_Generic_Child_Unit, because setting the parent instances requires
942 -- that the visibility data structures be properly initialized. Once the
943 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
945 Parent_Unit_Visible
: Boolean := False;
946 -- Parent_Unit_Visible is used when the generic is a child unit, and
947 -- indicates whether the ultimate parent of the generic is visible in the
948 -- instantiation environment. It is used to reset the visibility of the
949 -- parent at the end of the instantiation (see Remove_Parent).
951 Instance_Parent_Unit
: Entity_Id
:= Empty
;
952 -- This records the ultimate parent unit of an instance of a generic
953 -- child unit and is used in conjunction with Parent_Unit_Visible to
954 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
956 type Instance_Env
is record
957 Instantiated_Parent
: Assoc
;
958 Exchanged_Views
: Elist_Id
;
959 Hidden_Entities
: Elist_Id
;
960 Current_Sem_Unit
: Unit_Number_Type
;
961 Parent_Unit_Visible
: Boolean := False;
962 Instance_Parent_Unit
: Entity_Id
:= Empty
;
963 Switches
: Config_Switches_Type
;
966 package Instance_Envs
is new Table
.Table
(
967 Table_Component_Type
=> Instance_Env
,
968 Table_Index_Type
=> Int
,
969 Table_Low_Bound
=> 0,
971 Table_Increment
=> 100,
972 Table_Name
=> "Instance_Envs");
974 procedure Restore_Private_Views
975 (Pack_Id
: Entity_Id
;
976 Is_Package
: Boolean := True);
977 -- Restore the private views of external types, and unmark the generic
978 -- renamings of actuals, so that they become compatible subtypes again.
979 -- For subprograms, Pack_Id is the package constructed to hold the
982 procedure Switch_View
(T
: Entity_Id
);
983 -- Switch the partial and full views of a type and its private
984 -- dependents (i.e. its subtypes and derived types).
986 ------------------------------------
987 -- Structures for Error Reporting --
988 ------------------------------------
990 Instantiation_Node
: Node_Id
;
991 -- Used by subprograms that validate instantiation of formal parameters
992 -- where there might be no actual on which to place the error message.
993 -- Also used to locate the instantiation node for generic subunits.
995 Instantiation_Error
: exception;
996 -- When there is a semantic error in the generic parameter matching,
997 -- there is no point in continuing the instantiation, because the
998 -- number of cascaded errors is unpredictable. This exception aborts
999 -- the instantiation process altogether.
1001 S_Adjustment
: Sloc_Adjustment
;
1002 -- Offset created for each node in an instantiation, in order to keep
1003 -- track of the source position of the instantiation in each of its nodes.
1004 -- A subsequent semantic error or warning on a construct of the instance
1005 -- points to both places: the original generic node, and the point of
1006 -- instantiation. See Sinput and Sinput.L for additional details.
1008 ------------------------------------------------------------
1009 -- Data structure for keeping track when inside a Generic --
1010 ------------------------------------------------------------
1012 -- The following table is used to save values of the Inside_A_Generic
1013 -- flag (see spec of Sem) when they are saved by Start_Generic.
1015 package Generic_Flags
is new Table
.Table
(
1016 Table_Component_Type
=> Boolean,
1017 Table_Index_Type
=> Int
,
1018 Table_Low_Bound
=> 0,
1019 Table_Initial
=> 32,
1020 Table_Increment
=> 200,
1021 Table_Name
=> "Generic_Flags");
1023 ---------------------------
1024 -- Abandon_Instantiation --
1025 ---------------------------
1027 procedure Abandon_Instantiation
(N
: Node_Id
) is
1029 Error_Msg_N
("\instantiation abandoned!", N
);
1030 raise Instantiation_Error
;
1031 end Abandon_Instantiation
;
1033 --------------------------------
1034 -- Add_Pending_Instantiation --
1035 --------------------------------
1037 procedure Add_Pending_Instantiation
(Inst
: Node_Id
; Act_Decl
: Node_Id
) is
1040 -- Add to the instantiation node and the corresponding unit declaration
1041 -- the current values of global flags to be used when analyzing the
1044 Pending_Instantiations
.Append
1045 ((Inst_Node
=> Inst
,
1046 Act_Decl
=> Act_Decl
,
1047 Expander_Status
=> Expander_Active
,
1048 Current_Sem_Unit
=> Current_Sem_Unit
,
1049 Scope_Suppress
=> Scope_Suppress
,
1050 Local_Suppress_Stack_Top
=> Local_Suppress_Stack_Top
,
1051 Version
=> Ada_Version
,
1052 Version_Pragma
=> Ada_Version_Pragma
,
1053 Warnings
=> Save_Warnings
,
1054 SPARK_Mode
=> SPARK_Mode
,
1055 SPARK_Mode_Pragma
=> SPARK_Mode_Pragma
));
1056 end Add_Pending_Instantiation
;
1058 ----------------------------------
1059 -- Adjust_Inherited_Pragma_Sloc --
1060 ----------------------------------
1062 procedure Adjust_Inherited_Pragma_Sloc
(N
: Node_Id
) is
1064 Adjust_Instantiation_Sloc
(N
, S_Adjustment
);
1065 end Adjust_Inherited_Pragma_Sloc
;
1067 --------------------------
1068 -- Analyze_Associations --
1069 --------------------------
1071 function Analyze_Associations
1074 F_Copy
: List_Id
) return List_Id
1076 Actuals_To_Freeze
: constant Elist_Id
:= New_Elmt_List
;
1077 Assoc
: constant List_Id
:= New_List
;
1078 Default_Actuals
: constant List_Id
:= New_List
;
1079 Gen_Unit
: constant Entity_Id
:=
1080 Defining_Entity
(Parent
(F_Copy
));
1084 Analyzed_Formal
: Node_Id
;
1085 First_Named
: Node_Id
:= Empty
;
1089 Saved_Formal
: Node_Id
;
1091 Default_Formals
: constant List_Id
:= New_List
;
1092 -- If an Others_Choice is present, some of the formals may be defaulted.
1093 -- To simplify the treatment of visibility in an instance, we introduce
1094 -- individual defaults for each such formal. These defaults are
1095 -- appended to the list of associations and replace the Others_Choice.
1097 Found_Assoc
: Node_Id
;
1098 -- Association for the current formal being match. Empty if there are
1099 -- no remaining actuals, or if there is no named association with the
1100 -- name of the formal.
1102 Is_Named_Assoc
: Boolean;
1103 Num_Matched
: Nat
:= 0;
1104 Num_Actuals
: Nat
:= 0;
1106 Others_Present
: Boolean := False;
1107 Others_Choice
: Node_Id
:= Empty
;
1108 -- In Ada 2005, indicates partial parameterization of a formal
1109 -- package. As usual an other association must be last in the list.
1111 procedure Check_Fixed_Point_Actual
(Actual
: Node_Id
);
1112 -- Warn if an actual fixed-point type has user-defined arithmetic
1113 -- operations, but there is no corresponding formal in the generic,
1114 -- in which case the predefined operations will be used. This merits
1115 -- a warning because of the special semantics of fixed point ops.
1117 procedure Check_Overloaded_Formal_Subprogram
(Formal
: Entity_Id
);
1118 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1119 -- cannot have a named association for it. AI05-0025 extends this rule
1120 -- to formals of formal packages by AI05-0025, and it also applies to
1121 -- box-initialized formals.
1123 function Has_Fully_Defined_Profile
(Subp
: Entity_Id
) return Boolean;
1124 -- Determine whether the parameter types and the return type of Subp
1125 -- are fully defined at the point of instantiation.
1127 function Matching_Actual
1129 A_F
: Entity_Id
) return Node_Id
;
1130 -- Find actual that corresponds to a given a formal parameter. If the
1131 -- actuals are positional, return the next one, if any. If the actuals
1132 -- are named, scan the parameter associations to find the right one.
1133 -- A_F is the corresponding entity in the analyzed generic, which is
1134 -- placed on the selector name for ASIS use.
1136 -- In Ada 2005, a named association may be given with a box, in which
1137 -- case Matching_Actual sets Found_Assoc to the generic association,
1138 -- but return Empty for the actual itself. In this case the code below
1139 -- creates a corresponding declaration for the formal.
1141 function Partial_Parameterization
return Boolean;
1142 -- Ada 2005: if no match is found for a given formal, check if the
1143 -- association for it includes a box, or whether the associations
1144 -- include an Others clause.
1146 procedure Process_Default
(F
: Entity_Id
);
1147 -- Add a copy of the declaration of generic formal F to the list of
1148 -- associations, and add an explicit box association for F if there
1149 -- is none yet, and the default comes from an Others_Choice.
1151 function Renames_Standard_Subprogram
(Subp
: Entity_Id
) return Boolean;
1152 -- Determine whether Subp renames one of the subprograms defined in the
1153 -- generated package Standard.
1155 procedure Set_Analyzed_Formal
;
1156 -- Find the node in the generic copy that corresponds to a given formal.
1157 -- The semantic information on this node is used to perform legality
1158 -- checks on the actuals. Because semantic analysis can introduce some
1159 -- anonymous entities or modify the declaration node itself, the
1160 -- correspondence between the two lists is not one-one. In addition to
1161 -- anonymous types, the presence a formal equality will introduce an
1162 -- implicit declaration for the corresponding inequality.
1164 ----------------------------------------
1165 -- Check_Overloaded_Formal_Subprogram --
1166 ----------------------------------------
1168 procedure Check_Overloaded_Formal_Subprogram
(Formal
: Entity_Id
) is
1169 Temp_Formal
: Entity_Id
;
1172 Temp_Formal
:= First
(Formals
);
1173 while Present
(Temp_Formal
) loop
1174 if Nkind
(Temp_Formal
) in N_Formal_Subprogram_Declaration
1175 and then Temp_Formal
/= Formal
1177 Chars
(Defining_Unit_Name
(Specification
(Formal
))) =
1178 Chars
(Defining_Unit_Name
(Specification
(Temp_Formal
)))
1180 if Present
(Found_Assoc
) then
1182 ("named association not allowed for overloaded formal",
1187 ("named association not allowed for overloaded formal",
1191 Abandon_Instantiation
(Instantiation_Node
);
1196 end Check_Overloaded_Formal_Subprogram
;
1198 -------------------------------
1199 -- Check_Fixed_Point_Actual --
1200 -------------------------------
1202 procedure Check_Fixed_Point_Actual
(Actual
: Node_Id
) is
1203 Typ
: constant Entity_Id
:= Entity
(Actual
);
1204 Prims
: constant Elist_Id
:= Collect_Primitive_Operations
(Typ
);
1209 -- Locate primitive operations of the type that are arithmetic
1212 Elem
:= First_Elmt
(Prims
);
1213 while Present
(Elem
) loop
1214 if Nkind
(Node
(Elem
)) = N_Defining_Operator_Symbol
then
1216 -- Check whether the generic unit has a formal subprogram of
1217 -- the same name. This does not check types but is good enough
1218 -- to justify a warning.
1220 Formal
:= First_Non_Pragma
(Formals
);
1221 while Present
(Formal
) loop
1222 if Nkind
(Formal
) = N_Formal_Concrete_Subprogram_Declaration
1223 and then Chars
(Defining_Entity
(Formal
)) =
1233 Error_Msg_Sloc
:= Sloc
(Node
(Elem
));
1235 ("?instance does not use primitive operation&#",
1236 Actual
, Node
(Elem
));
1242 end Check_Fixed_Point_Actual
;
1244 -------------------------------
1245 -- Has_Fully_Defined_Profile --
1246 -------------------------------
1248 function Has_Fully_Defined_Profile
(Subp
: Entity_Id
) return Boolean is
1249 function Is_Fully_Defined_Type
(Typ
: Entity_Id
) return Boolean;
1250 -- Determine whethet type Typ is fully defined
1252 ---------------------------
1253 -- Is_Fully_Defined_Type --
1254 ---------------------------
1256 function Is_Fully_Defined_Type
(Typ
: Entity_Id
) return Boolean is
1258 -- A private type without a full view is not fully defined
1260 if Is_Private_Type
(Typ
)
1261 and then No
(Full_View
(Typ
))
1265 -- An incomplete type is never fully defined
1267 elsif Is_Incomplete_Type
(Typ
) then
1270 -- All other types are fully defined
1275 end Is_Fully_Defined_Type
;
1277 -- Local declarations
1281 -- Start of processing for Has_Fully_Defined_Profile
1284 -- Check the parameters
1286 Param
:= First_Formal
(Subp
);
1287 while Present
(Param
) loop
1288 if not Is_Fully_Defined_Type
(Etype
(Param
)) then
1292 Next_Formal
(Param
);
1295 -- Check the return type
1297 return Is_Fully_Defined_Type
(Etype
(Subp
));
1298 end Has_Fully_Defined_Profile
;
1300 ---------------------
1301 -- Matching_Actual --
1302 ---------------------
1304 function Matching_Actual
1306 A_F
: Entity_Id
) return Node_Id
1312 Is_Named_Assoc
:= False;
1314 -- End of list of purely positional parameters
1316 if No
(Actual
) or else Nkind
(Actual
) = N_Others_Choice
then
1317 Found_Assoc
:= Empty
;
1320 -- Case of positional parameter corresponding to current formal
1322 elsif No
(Selector_Name
(Actual
)) then
1323 Found_Assoc
:= Actual
;
1324 Act
:= Explicit_Generic_Actual_Parameter
(Actual
);
1325 Num_Matched
:= Num_Matched
+ 1;
1328 -- Otherwise scan list of named actuals to find the one with the
1329 -- desired name. All remaining actuals have explicit names.
1332 Is_Named_Assoc
:= True;
1333 Found_Assoc
:= Empty
;
1337 while Present
(Actual
) loop
1338 if Nkind
(Actual
) = N_Others_Choice
then
1339 Found_Assoc
:= Empty
;
1342 elsif Chars
(Selector_Name
(Actual
)) = Chars
(F
) then
1343 Set_Entity
(Selector_Name
(Actual
), A_F
);
1344 Set_Etype
(Selector_Name
(Actual
), Etype
(A_F
));
1345 Generate_Reference
(A_F
, Selector_Name
(Actual
));
1347 Found_Assoc
:= Actual
;
1348 Act
:= Explicit_Generic_Actual_Parameter
(Actual
);
1349 Num_Matched
:= Num_Matched
+ 1;
1357 -- Reset for subsequent searches. In most cases the named
1358 -- associations are in order. If they are not, we reorder them
1359 -- to avoid scanning twice the same actual. This is not just a
1360 -- question of efficiency: there may be multiple defaults with
1361 -- boxes that have the same name. In a nested instantiation we
1362 -- insert actuals for those defaults, and cannot rely on their
1363 -- names to disambiguate them.
1365 if Actual
= First_Named
then
1368 elsif Present
(Actual
) then
1369 Insert_Before
(First_Named
, Remove_Next
(Prev
));
1372 Actual
:= First_Named
;
1375 if Is_Entity_Name
(Act
) and then Present
(Entity
(Act
)) then
1376 Set_Used_As_Generic_Actual
(Entity
(Act
));
1380 end Matching_Actual
;
1382 ------------------------------
1383 -- Partial_Parameterization --
1384 ------------------------------
1386 function Partial_Parameterization
return Boolean is
1388 return Others_Present
1389 or else (Present
(Found_Assoc
) and then Box_Present
(Found_Assoc
));
1390 end Partial_Parameterization
;
1392 ---------------------
1393 -- Process_Default --
1394 ---------------------
1396 procedure Process_Default
(F
: Entity_Id
) is
1397 Loc
: constant Source_Ptr
:= Sloc
(I_Node
);
1398 F_Id
: constant Entity_Id
:= Defining_Entity
(F
);
1404 -- Append copy of formal declaration to associations, and create new
1405 -- defining identifier for it.
1407 Decl
:= New_Copy_Tree
(F
);
1408 Id
:= Make_Defining_Identifier
(Sloc
(F_Id
), Chars
(F_Id
));
1410 if Nkind
(F
) in N_Formal_Subprogram_Declaration
then
1411 Set_Defining_Unit_Name
(Specification
(Decl
), Id
);
1414 Set_Defining_Identifier
(Decl
, Id
);
1417 Append
(Decl
, Assoc
);
1419 if No
(Found_Assoc
) then
1421 Make_Generic_Association
(Loc
,
1423 New_Occurrence_Of
(Id
, Loc
),
1424 Explicit_Generic_Actual_Parameter
=> Empty
);
1425 Set_Box_Present
(Default
);
1426 Append
(Default
, Default_Formals
);
1428 end Process_Default
;
1430 ---------------------------------
1431 -- Renames_Standard_Subprogram --
1432 ---------------------------------
1434 function Renames_Standard_Subprogram
(Subp
: Entity_Id
) return Boolean is
1439 while Present
(Id
) loop
1440 if Scope
(Id
) = Standard_Standard
then
1448 end Renames_Standard_Subprogram
;
1450 -------------------------
1451 -- Set_Analyzed_Formal --
1452 -------------------------
1454 procedure Set_Analyzed_Formal
is
1458 while Present
(Analyzed_Formal
) loop
1459 Kind
:= Nkind
(Analyzed_Formal
);
1461 case Nkind
(Formal
) is
1463 when N_Formal_Subprogram_Declaration
=>
1464 exit when Kind
in N_Formal_Subprogram_Declaration
1467 (Defining_Unit_Name
(Specification
(Formal
))) =
1469 (Defining_Unit_Name
(Specification
(Analyzed_Formal
)));
1471 when N_Formal_Package_Declaration
=>
1472 exit when Nkind_In
(Kind
, N_Formal_Package_Declaration
,
1473 N_Generic_Package_Declaration
,
1474 N_Package_Declaration
);
1476 when N_Use_Package_Clause | N_Use_Type_Clause
=> exit;
1480 -- Skip freeze nodes, and nodes inserted to replace
1481 -- unrecognized pragmas.
1484 Kind
not in N_Formal_Subprogram_Declaration
1485 and then not Nkind_In
(Kind
, N_Subprogram_Declaration
,
1489 and then Chars
(Defining_Identifier
(Formal
)) =
1490 Chars
(Defining_Identifier
(Analyzed_Formal
));
1493 Next
(Analyzed_Formal
);
1495 end Set_Analyzed_Formal
;
1497 -- Start of processing for Analyze_Associations
1500 Actuals
:= Generic_Associations
(I_Node
);
1502 if Present
(Actuals
) then
1504 -- Check for an Others choice, indicating a partial parameterization
1505 -- for a formal package.
1507 Actual
:= First
(Actuals
);
1508 while Present
(Actual
) loop
1509 if Nkind
(Actual
) = N_Others_Choice
then
1510 Others_Present
:= True;
1511 Others_Choice
:= Actual
;
1513 if Present
(Next
(Actual
)) then
1514 Error_Msg_N
("others must be last association", Actual
);
1517 -- This subprogram is used both for formal packages and for
1518 -- instantiations. For the latter, associations must all be
1521 if Nkind
(I_Node
) /= N_Formal_Package_Declaration
1522 and then Comes_From_Source
(I_Node
)
1525 ("others association not allowed in an instance",
1529 -- In any case, nothing to do after the others association
1533 elsif Box_Present
(Actual
)
1534 and then Comes_From_Source
(I_Node
)
1535 and then Nkind
(I_Node
) /= N_Formal_Package_Declaration
1538 ("box association not allowed in an instance", Actual
);
1544 -- If named associations are present, save first named association
1545 -- (it may of course be Empty) to facilitate subsequent name search.
1547 First_Named
:= First
(Actuals
);
1548 while Present
(First_Named
)
1549 and then Nkind
(First_Named
) /= N_Others_Choice
1550 and then No
(Selector_Name
(First_Named
))
1552 Num_Actuals
:= Num_Actuals
+ 1;
1557 Named
:= First_Named
;
1558 while Present
(Named
) loop
1559 if Nkind
(Named
) /= N_Others_Choice
1560 and then No
(Selector_Name
(Named
))
1562 Error_Msg_N
("invalid positional actual after named one", Named
);
1563 Abandon_Instantiation
(Named
);
1566 -- A named association may lack an actual parameter, if it was
1567 -- introduced for a default subprogram that turns out to be local
1568 -- to the outer instantiation. If it has a box association it must
1569 -- correspond to some formal in the generic.
1571 if Nkind
(Named
) /= N_Others_Choice
1572 and then (Present
(Explicit_Generic_Actual_Parameter
(Named
))
1573 or else Box_Present
(Named
))
1575 Num_Actuals
:= Num_Actuals
+ 1;
1581 if Present
(Formals
) then
1582 Formal
:= First_Non_Pragma
(Formals
);
1583 Analyzed_Formal
:= First_Non_Pragma
(F_Copy
);
1585 if Present
(Actuals
) then
1586 Actual
:= First
(Actuals
);
1588 -- All formals should have default values
1594 while Present
(Formal
) loop
1595 Set_Analyzed_Formal
;
1596 Saved_Formal
:= Next_Non_Pragma
(Formal
);
1598 case Nkind
(Formal
) is
1599 when N_Formal_Object_Declaration
=>
1602 (Defining_Identifier
(Formal
),
1603 Defining_Identifier
(Analyzed_Formal
));
1605 if No
(Match
) and then Partial_Parameterization
then
1606 Process_Default
(Formal
);
1610 (Instantiate_Object
(Formal
, Match
, Analyzed_Formal
),
1613 -- For a defaulted in_parameter, create an entry in the
1614 -- the list of defaulted actuals, for GNATProve use. Do
1615 -- not included these defaults for an instance nested
1616 -- within a generic, because the defaults are also used
1617 -- in the analysis of the enclosing generic, and only
1618 -- defaulted subprograms are relevant there.
1620 if No
(Match
) and then not Inside_A_Generic
then
1621 Append_To
(Default_Actuals
,
1622 Make_Generic_Association
(Sloc
(I_Node
),
1625 (Defining_Identifier
(Formal
), Sloc
(I_Node
)),
1626 Explicit_Generic_Actual_Parameter
=>
1627 New_Copy_Tree
(Default_Expression
(Formal
))));
1631 -- If the object is a call to an expression function, this
1632 -- is a freezing point for it.
1634 if Is_Entity_Name
(Match
)
1635 and then Present
(Entity
(Match
))
1637 (Original_Node
(Unit_Declaration_Node
(Entity
(Match
))))
1638 = N_Expression_Function
1640 Append_Elmt
(Entity
(Match
), Actuals_To_Freeze
);
1643 when N_Formal_Type_Declaration
=>
1646 (Defining_Identifier
(Formal
),
1647 Defining_Identifier
(Analyzed_Formal
));
1650 if Partial_Parameterization
then
1651 Process_Default
(Formal
);
1654 Error_Msg_Sloc
:= Sloc
(Gen_Unit
);
1657 Instantiation_Node
, Defining_Identifier
(Formal
));
1659 ("\in instantiation of & declared#",
1660 Instantiation_Node
, Gen_Unit
);
1661 Abandon_Instantiation
(Instantiation_Node
);
1668 (Formal
, Match
, Analyzed_Formal
, Assoc
),
1671 if Is_Fixed_Point_Type
(Entity
(Match
)) then
1672 Check_Fixed_Point_Actual
(Match
);
1675 -- An instantiation is a freeze point for the actuals,
1676 -- unless this is a rewritten formal package, or the
1677 -- formal is an Ada 2012 formal incomplete type.
1679 if Nkind
(I_Node
) = N_Formal_Package_Declaration
1681 (Ada_Version
>= Ada_2012
1683 Ekind
(Defining_Identifier
(Analyzed_Formal
)) =
1689 Append_Elmt
(Entity
(Match
), Actuals_To_Freeze
);
1693 -- A remote access-to-class-wide type is not a legal actual
1694 -- for a generic formal of an access type (E.2.2(17/2)).
1695 -- In GNAT an exception to this rule is introduced when
1696 -- the formal is marked as remote using implementation
1697 -- defined aspect/pragma Remote_Access_Type. In that case
1698 -- the actual must be remote as well.
1700 -- If the current instantiation is the construction of a
1701 -- local copy for a formal package the actuals may be
1702 -- defaulted, and there is no matching actual to check.
1704 if Nkind
(Analyzed_Formal
) = N_Formal_Type_Declaration
1706 Nkind
(Formal_Type_Definition
(Analyzed_Formal
)) =
1707 N_Access_To_Object_Definition
1708 and then Present
(Match
)
1711 Formal_Ent
: constant Entity_Id
:=
1712 Defining_Identifier
(Analyzed_Formal
);
1714 if Is_Remote_Access_To_Class_Wide_Type
(Entity
(Match
))
1715 = Is_Remote_Types
(Formal_Ent
)
1717 -- Remoteness of formal and actual match
1721 elsif Is_Remote_Types
(Formal_Ent
) then
1723 -- Remote formal, non-remote actual
1726 ("actual for& must be remote", Match
, Formal_Ent
);
1729 -- Non-remote formal, remote actual
1732 ("actual for& may not be remote",
1738 when N_Formal_Subprogram_Declaration
=>
1741 (Defining_Unit_Name
(Specification
(Formal
)),
1742 Defining_Unit_Name
(Specification
(Analyzed_Formal
)));
1744 -- If the formal subprogram has the same name as another
1745 -- formal subprogram of the generic, then a named
1746 -- association is illegal (12.3(9)). Exclude named
1747 -- associations that are generated for a nested instance.
1750 and then Is_Named_Assoc
1751 and then Comes_From_Source
(Found_Assoc
)
1753 Check_Overloaded_Formal_Subprogram
(Formal
);
1756 -- If there is no corresponding actual, this may be case
1757 -- of partial parameterization, or else the formal has a
1758 -- default or a box.
1760 if No
(Match
) and then Partial_Parameterization
then
1761 Process_Default
(Formal
);
1763 if Nkind
(I_Node
) = N_Formal_Package_Declaration
then
1764 Check_Overloaded_Formal_Subprogram
(Formal
);
1769 Instantiate_Formal_Subprogram
1770 (Formal
, Match
, Analyzed_Formal
));
1772 -- An instantiation is a freeze point for the actuals,
1773 -- unless this is a rewritten formal package.
1775 if Nkind
(I_Node
) /= N_Formal_Package_Declaration
1776 and then Nkind
(Match
) = N_Identifier
1777 and then Is_Subprogram
(Entity
(Match
))
1779 -- The actual subprogram may rename a routine defined
1780 -- in Standard. Avoid freezing such renamings because
1781 -- subprograms coming from Standard cannot be frozen.
1784 not Renames_Standard_Subprogram
(Entity
(Match
))
1786 -- If the actual subprogram comes from a different
1787 -- unit, it is already frozen, either by a body in
1788 -- that unit or by the end of the declarative part
1789 -- of the unit. This check avoids the freezing of
1790 -- subprograms defined in Standard which are used
1791 -- as generic actuals.
1793 and then In_Same_Code_Unit
(Entity
(Match
), I_Node
)
1794 and then Has_Fully_Defined_Profile
(Entity
(Match
))
1796 -- Mark the subprogram as having a delayed freeze
1797 -- since this may be an out-of-order action.
1799 Set_Has_Delayed_Freeze
(Entity
(Match
));
1800 Append_Elmt
(Entity
(Match
), Actuals_To_Freeze
);
1804 -- If this is a nested generic, preserve default for later
1805 -- instantiations. We do this as well for GNATProve use,
1806 -- so that the list of generic associations is complete.
1808 if No
(Match
) and then Box_Present
(Formal
) then
1810 Subp
: constant Entity_Id
:=
1811 Defining_Unit_Name
(Specification
(Last
(Assoc
)));
1814 Append_To
(Default_Actuals
,
1815 Make_Generic_Association
(Sloc
(I_Node
),
1817 New_Occurrence_Of
(Subp
, Sloc
(I_Node
)),
1818 Explicit_Generic_Actual_Parameter
=>
1819 New_Occurrence_Of
(Subp
, Sloc
(I_Node
))));
1823 when N_Formal_Package_Declaration
=>
1826 (Defining_Identifier
(Formal
),
1827 Defining_Identifier
(Original_Node
(Analyzed_Formal
)));
1830 if Partial_Parameterization
then
1831 Process_Default
(Formal
);
1834 Error_Msg_Sloc
:= Sloc
(Gen_Unit
);
1837 Instantiation_Node
, Defining_Identifier
(Formal
));
1839 ("\in instantiation of & declared#",
1840 Instantiation_Node
, Gen_Unit
);
1842 Abandon_Instantiation
(Instantiation_Node
);
1848 (Instantiate_Formal_Package
1849 (Formal
, Match
, Analyzed_Formal
),
1853 -- For use type and use package appearing in the generic part,
1854 -- we have already copied them, so we can just move them where
1855 -- they belong (we mustn't recopy them since this would mess up
1856 -- the Sloc values).
1858 when N_Use_Package_Clause |
1859 N_Use_Type_Clause
=>
1860 if Nkind
(Original_Node
(I_Node
)) =
1861 N_Formal_Package_Declaration
1863 Append
(New_Copy_Tree
(Formal
), Assoc
);
1866 Append
(Formal
, Assoc
);
1870 raise Program_Error
;
1874 Formal
:= Saved_Formal
;
1875 Next_Non_Pragma
(Analyzed_Formal
);
1878 if Num_Actuals
> Num_Matched
then
1879 Error_Msg_Sloc
:= Sloc
(Gen_Unit
);
1881 if Present
(Selector_Name
(Actual
)) then
1883 ("unmatched actual &", Actual
, Selector_Name
(Actual
));
1885 ("\in instantiation of & declared#", Actual
, Gen_Unit
);
1888 ("unmatched actual in instantiation of & declared#",
1893 elsif Present
(Actuals
) then
1895 ("too many actuals in generic instantiation", Instantiation_Node
);
1898 -- An instantiation freezes all generic actuals. The only exceptions
1899 -- to this are incomplete types and subprograms which are not fully
1900 -- defined at the point of instantiation.
1903 Elmt
: Elmt_Id
:= First_Elmt
(Actuals_To_Freeze
);
1905 while Present
(Elmt
) loop
1906 Freeze_Before
(I_Node
, Node
(Elmt
));
1911 -- If there are default subprograms, normalize the tree by adding
1912 -- explicit associations for them. This is required if the instance
1913 -- appears within a generic.
1915 if not Is_Empty_List
(Default_Actuals
) then
1920 Default
:= First
(Default_Actuals
);
1921 while Present
(Default
) loop
1922 Mark_Rewrite_Insertion
(Default
);
1926 if No
(Actuals
) then
1927 Set_Generic_Associations
(I_Node
, Default_Actuals
);
1929 Append_List_To
(Actuals
, Default_Actuals
);
1934 -- If this is a formal package, normalize the parameter list by adding
1935 -- explicit box associations for the formals that are covered by an
1938 if not Is_Empty_List
(Default_Formals
) then
1939 Append_List
(Default_Formals
, Formals
);
1943 end Analyze_Associations
;
1945 -------------------------------
1946 -- Analyze_Formal_Array_Type --
1947 -------------------------------
1949 procedure Analyze_Formal_Array_Type
1950 (T
: in out Entity_Id
;
1956 -- Treated like a non-generic array declaration, with additional
1961 if Nkind
(Def
) = N_Constrained_Array_Definition
then
1962 DSS
:= First
(Discrete_Subtype_Definitions
(Def
));
1963 while Present
(DSS
) loop
1964 if Nkind_In
(DSS
, N_Subtype_Indication
,
1966 N_Attribute_Reference
)
1968 Error_Msg_N
("only a subtype mark is allowed in a formal", DSS
);
1975 Array_Type_Declaration
(T
, Def
);
1976 Set_Is_Generic_Type
(Base_Type
(T
));
1978 if Ekind
(Component_Type
(T
)) = E_Incomplete_Type
1979 and then No
(Full_View
(Component_Type
(T
)))
1981 Error_Msg_N
("premature usage of incomplete type", Def
);
1983 -- Check that range constraint is not allowed on the component type
1984 -- of a generic formal array type (AARM 12.5.3(3))
1986 elsif Is_Internal
(Component_Type
(T
))
1987 and then Present
(Subtype_Indication
(Component_Definition
(Def
)))
1988 and then Nkind
(Original_Node
1989 (Subtype_Indication
(Component_Definition
(Def
)))) =
1990 N_Subtype_Indication
1993 ("in a formal, a subtype indication can only be "
1994 & "a subtype mark (RM 12.5.3(3))",
1995 Subtype_Indication
(Component_Definition
(Def
)));
1998 end Analyze_Formal_Array_Type
;
2000 ---------------------------------------------
2001 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2002 ---------------------------------------------
2004 -- As for other generic types, we create a valid type representation with
2005 -- legal but arbitrary attributes, whose values are never considered
2006 -- static. For all scalar types we introduce an anonymous base type, with
2007 -- the same attributes. We choose the corresponding integer type to be
2008 -- Standard_Integer.
2009 -- Here and in other similar routines, the Sloc of the generated internal
2010 -- type must be the same as the sloc of the defining identifier of the
2011 -- formal type declaration, to provide proper source navigation.
2013 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2017 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2019 Base
: constant Entity_Id
:=
2021 (E_Decimal_Fixed_Point_Type
,
2023 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
2025 Int_Base
: constant Entity_Id
:= Standard_Integer
;
2026 Delta_Val
: constant Ureal
:= Ureal_1
;
2027 Digs_Val
: constant Uint
:= Uint_6
;
2029 function Make_Dummy_Bound
return Node_Id
;
2030 -- Return a properly typed universal real literal to use as a bound
2032 ----------------------
2033 -- Make_Dummy_Bound --
2034 ----------------------
2036 function Make_Dummy_Bound
return Node_Id
is
2037 Bound
: constant Node_Id
:= Make_Real_Literal
(Loc
, Ureal_1
);
2039 Set_Etype
(Bound
, Universal_Real
);
2041 end Make_Dummy_Bound
;
2043 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2048 Set_Etype
(Base
, Base
);
2049 Set_Size_Info
(Base
, Int_Base
);
2050 Set_RM_Size
(Base
, RM_Size
(Int_Base
));
2051 Set_First_Rep_Item
(Base
, First_Rep_Item
(Int_Base
));
2052 Set_Digits_Value
(Base
, Digs_Val
);
2053 Set_Delta_Value
(Base
, Delta_Val
);
2054 Set_Small_Value
(Base
, Delta_Val
);
2055 Set_Scalar_Range
(Base
,
2057 Low_Bound
=> Make_Dummy_Bound
,
2058 High_Bound
=> Make_Dummy_Bound
));
2060 Set_Is_Generic_Type
(Base
);
2061 Set_Parent
(Base
, Parent
(Def
));
2063 Set_Ekind
(T
, E_Decimal_Fixed_Point_Subtype
);
2064 Set_Etype
(T
, Base
);
2065 Set_Size_Info
(T
, Int_Base
);
2066 Set_RM_Size
(T
, RM_Size
(Int_Base
));
2067 Set_First_Rep_Item
(T
, First_Rep_Item
(Int_Base
));
2068 Set_Digits_Value
(T
, Digs_Val
);
2069 Set_Delta_Value
(T
, Delta_Val
);
2070 Set_Small_Value
(T
, Delta_Val
);
2071 Set_Scalar_Range
(T
, Scalar_Range
(Base
));
2072 Set_Is_Constrained
(T
);
2074 Check_Restriction
(No_Fixed_Point
, Def
);
2075 end Analyze_Formal_Decimal_Fixed_Point_Type
;
2077 -------------------------------------------
2078 -- Analyze_Formal_Derived_Interface_Type --
2079 -------------------------------------------
2081 procedure Analyze_Formal_Derived_Interface_Type
2086 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2089 -- Rewrite as a type declaration of a derived type. This ensures that
2090 -- the interface list and primitive operations are properly captured.
2093 Make_Full_Type_Declaration
(Loc
,
2094 Defining_Identifier
=> T
,
2095 Type_Definition
=> Def
));
2097 Set_Is_Generic_Type
(T
);
2098 end Analyze_Formal_Derived_Interface_Type
;
2100 ---------------------------------
2101 -- Analyze_Formal_Derived_Type --
2102 ---------------------------------
2104 procedure Analyze_Formal_Derived_Type
2109 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2110 Unk_Disc
: constant Boolean := Unknown_Discriminants_Present
(N
);
2114 Set_Is_Generic_Type
(T
);
2116 if Private_Present
(Def
) then
2118 Make_Private_Extension_Declaration
(Loc
,
2119 Defining_Identifier
=> T
,
2120 Discriminant_Specifications
=> Discriminant_Specifications
(N
),
2121 Unknown_Discriminants_Present
=> Unk_Disc
,
2122 Subtype_Indication
=> Subtype_Mark
(Def
),
2123 Interface_List
=> Interface_List
(Def
));
2125 Set_Abstract_Present
(New_N
, Abstract_Present
(Def
));
2126 Set_Limited_Present
(New_N
, Limited_Present
(Def
));
2127 Set_Synchronized_Present
(New_N
, Synchronized_Present
(Def
));
2131 Make_Full_Type_Declaration
(Loc
,
2132 Defining_Identifier
=> T
,
2133 Discriminant_Specifications
=>
2134 Discriminant_Specifications
(Parent
(T
)),
2136 Make_Derived_Type_Definition
(Loc
,
2137 Subtype_Indication
=> Subtype_Mark
(Def
)));
2139 Set_Abstract_Present
2140 (Type_Definition
(New_N
), Abstract_Present
(Def
));
2142 (Type_Definition
(New_N
), Limited_Present
(Def
));
2149 if not Is_Composite_Type
(T
) then
2151 ("unknown discriminants not allowed for elementary types", N
);
2153 Set_Has_Unknown_Discriminants
(T
);
2154 Set_Is_Constrained
(T
, False);
2158 -- If the parent type has a known size, so does the formal, which makes
2159 -- legal representation clauses that involve the formal.
2161 Set_Size_Known_At_Compile_Time
2162 (T
, Size_Known_At_Compile_Time
(Entity
(Subtype_Mark
(Def
))));
2163 end Analyze_Formal_Derived_Type
;
2165 ----------------------------------
2166 -- Analyze_Formal_Discrete_Type --
2167 ----------------------------------
2169 -- The operations defined for a discrete types are those of an enumeration
2170 -- type. The size is set to an arbitrary value, for use in analyzing the
2173 procedure Analyze_Formal_Discrete_Type
(T
: Entity_Id
; Def
: Node_Id
) is
2174 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2178 Base
: constant Entity_Id
:=
2180 (E_Floating_Point_Type
, Current_Scope
,
2181 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
2185 Set_Ekind
(T
, E_Enumeration_Subtype
);
2186 Set_Etype
(T
, Base
);
2189 Set_Is_Generic_Type
(T
);
2190 Set_Is_Constrained
(T
);
2192 -- For semantic analysis, the bounds of the type must be set to some
2193 -- non-static value. The simplest is to create attribute nodes for those
2194 -- bounds, that refer to the type itself. These bounds are never
2195 -- analyzed but serve as place-holders.
2198 Make_Attribute_Reference
(Loc
,
2199 Attribute_Name
=> Name_First
,
2200 Prefix
=> New_Occurrence_Of
(T
, Loc
));
2204 Make_Attribute_Reference
(Loc
,
2205 Attribute_Name
=> Name_Last
,
2206 Prefix
=> New_Occurrence_Of
(T
, Loc
));
2209 Set_Scalar_Range
(T
,
2214 Set_Ekind
(Base
, E_Enumeration_Type
);
2215 Set_Etype
(Base
, Base
);
2216 Init_Size
(Base
, 8);
2217 Init_Alignment
(Base
);
2218 Set_Is_Generic_Type
(Base
);
2219 Set_Scalar_Range
(Base
, Scalar_Range
(T
));
2220 Set_Parent
(Base
, Parent
(Def
));
2221 end Analyze_Formal_Discrete_Type
;
2223 ----------------------------------
2224 -- Analyze_Formal_Floating_Type --
2225 ---------------------------------
2227 procedure Analyze_Formal_Floating_Type
(T
: Entity_Id
; Def
: Node_Id
) is
2228 Base
: constant Entity_Id
:=
2230 (E_Floating_Point_Type
, Current_Scope
,
2231 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
2234 -- The various semantic attributes are taken from the predefined type
2235 -- Float, just so that all of them are initialized. Their values are
2236 -- never used because no constant folding or expansion takes place in
2237 -- the generic itself.
2240 Set_Ekind
(T
, E_Floating_Point_Subtype
);
2241 Set_Etype
(T
, Base
);
2242 Set_Size_Info
(T
, (Standard_Float
));
2243 Set_RM_Size
(T
, RM_Size
(Standard_Float
));
2244 Set_Digits_Value
(T
, Digits_Value
(Standard_Float
));
2245 Set_Scalar_Range
(T
, Scalar_Range
(Standard_Float
));
2246 Set_Is_Constrained
(T
);
2248 Set_Is_Generic_Type
(Base
);
2249 Set_Etype
(Base
, Base
);
2250 Set_Size_Info
(Base
, (Standard_Float
));
2251 Set_RM_Size
(Base
, RM_Size
(Standard_Float
));
2252 Set_Digits_Value
(Base
, Digits_Value
(Standard_Float
));
2253 Set_Scalar_Range
(Base
, Scalar_Range
(Standard_Float
));
2254 Set_Parent
(Base
, Parent
(Def
));
2256 Check_Restriction
(No_Floating_Point
, Def
);
2257 end Analyze_Formal_Floating_Type
;
2259 -----------------------------------
2260 -- Analyze_Formal_Interface_Type;--
2261 -----------------------------------
2263 procedure Analyze_Formal_Interface_Type
2268 Loc
: constant Source_Ptr
:= Sloc
(N
);
2273 Make_Full_Type_Declaration
(Loc
,
2274 Defining_Identifier
=> T
,
2275 Type_Definition
=> Def
);
2279 Set_Is_Generic_Type
(T
);
2280 end Analyze_Formal_Interface_Type
;
2282 ---------------------------------
2283 -- Analyze_Formal_Modular_Type --
2284 ---------------------------------
2286 procedure Analyze_Formal_Modular_Type
(T
: Entity_Id
; Def
: Node_Id
) is
2288 -- Apart from their entity kind, generic modular types are treated like
2289 -- signed integer types, and have the same attributes.
2291 Analyze_Formal_Signed_Integer_Type
(T
, Def
);
2292 Set_Ekind
(T
, E_Modular_Integer_Subtype
);
2293 Set_Ekind
(Etype
(T
), E_Modular_Integer_Type
);
2295 end Analyze_Formal_Modular_Type
;
2297 ---------------------------------------
2298 -- Analyze_Formal_Object_Declaration --
2299 ---------------------------------------
2301 procedure Analyze_Formal_Object_Declaration
(N
: Node_Id
) is
2302 E
: constant Node_Id
:= Default_Expression
(N
);
2303 Id
: constant Node_Id
:= Defining_Identifier
(N
);
2310 -- Determine the mode of the formal object
2312 if Out_Present
(N
) then
2313 K
:= E_Generic_In_Out_Parameter
;
2315 if not In_Present
(N
) then
2316 Error_Msg_N
("formal generic objects cannot have mode OUT", N
);
2320 K
:= E_Generic_In_Parameter
;
2323 if Present
(Subtype_Mark
(N
)) then
2324 Find_Type
(Subtype_Mark
(N
));
2325 T
:= Entity
(Subtype_Mark
(N
));
2327 -- Verify that there is no redundant null exclusion
2329 if Null_Exclusion_Present
(N
) then
2330 if not Is_Access_Type
(T
) then
2332 ("null exclusion can only apply to an access type", N
);
2334 elsif Can_Never_Be_Null
(T
) then
2336 ("`NOT NULL` not allowed (& already excludes null)", N
, T
);
2340 -- Ada 2005 (AI-423): Formal object with an access definition
2343 Check_Access_Definition
(N
);
2344 T
:= Access_Definition
2346 N
=> Access_Definition
(N
));
2349 if Ekind
(T
) = E_Incomplete_Type
then
2351 Error_Node
: Node_Id
;
2354 if Present
(Subtype_Mark
(N
)) then
2355 Error_Node
:= Subtype_Mark
(N
);
2357 Check_Access_Definition
(N
);
2358 Error_Node
:= Access_Definition
(N
);
2361 Error_Msg_N
("premature usage of incomplete type", Error_Node
);
2365 if K
= E_Generic_In_Parameter
then
2367 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2369 if Ada_Version
< Ada_2005
and then Is_Limited_Type
(T
) then
2371 ("generic formal of mode IN must not be of limited type", N
);
2372 Explain_Limited_Type
(T
, N
);
2375 if Is_Abstract_Type
(T
) then
2377 ("generic formal of mode IN must not be of abstract type", N
);
2381 Preanalyze_Spec_Expression
(E
, T
);
2383 if Is_Limited_Type
(T
) and then not OK_For_Limited_Init
(T
, E
) then
2385 ("initialization not allowed for limited types", E
);
2386 Explain_Limited_Type
(T
, E
);
2393 -- Case of generic IN OUT parameter
2396 -- If the formal has an unconstrained type, construct its actual
2397 -- subtype, as is done for subprogram formals. In this fashion, all
2398 -- its uses can refer to specific bounds.
2403 if (Is_Array_Type
(T
) and then not Is_Constrained
(T
))
2404 or else (Ekind
(T
) = E_Record_Type
and then Has_Discriminants
(T
))
2407 Non_Freezing_Ref
: constant Node_Id
:=
2408 New_Occurrence_Of
(Id
, Sloc
(Id
));
2412 -- Make sure the actual subtype doesn't generate bogus freezing
2414 Set_Must_Not_Freeze
(Non_Freezing_Ref
);
2415 Decl
:= Build_Actual_Subtype
(T
, Non_Freezing_Ref
);
2416 Insert_Before_And_Analyze
(N
, Decl
);
2417 Set_Actual_Subtype
(Id
, Defining_Identifier
(Decl
));
2420 Set_Actual_Subtype
(Id
, T
);
2425 ("initialization not allowed for `IN OUT` formals", N
);
2429 if Has_Aspects
(N
) then
2430 Analyze_Aspect_Specifications
(N
, Id
);
2432 end Analyze_Formal_Object_Declaration
;
2434 ----------------------------------------------
2435 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2436 ----------------------------------------------
2438 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2442 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2443 Base
: constant Entity_Id
:=
2445 (E_Ordinary_Fixed_Point_Type
, Current_Scope
,
2446 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
2449 -- The semantic attributes are set for completeness only, their values
2450 -- will never be used, since all properties of the type are non-static.
2453 Set_Ekind
(T
, E_Ordinary_Fixed_Point_Subtype
);
2454 Set_Etype
(T
, Base
);
2455 Set_Size_Info
(T
, Standard_Integer
);
2456 Set_RM_Size
(T
, RM_Size
(Standard_Integer
));
2457 Set_Small_Value
(T
, Ureal_1
);
2458 Set_Delta_Value
(T
, Ureal_1
);
2459 Set_Scalar_Range
(T
,
2461 Low_Bound
=> Make_Real_Literal
(Loc
, Ureal_1
),
2462 High_Bound
=> Make_Real_Literal
(Loc
, Ureal_1
)));
2463 Set_Is_Constrained
(T
);
2465 Set_Is_Generic_Type
(Base
);
2466 Set_Etype
(Base
, Base
);
2467 Set_Size_Info
(Base
, Standard_Integer
);
2468 Set_RM_Size
(Base
, RM_Size
(Standard_Integer
));
2469 Set_Small_Value
(Base
, Ureal_1
);
2470 Set_Delta_Value
(Base
, Ureal_1
);
2471 Set_Scalar_Range
(Base
, Scalar_Range
(T
));
2472 Set_Parent
(Base
, Parent
(Def
));
2474 Check_Restriction
(No_Fixed_Point
, Def
);
2475 end Analyze_Formal_Ordinary_Fixed_Point_Type
;
2477 ----------------------------------------
2478 -- Analyze_Formal_Package_Declaration --
2479 ----------------------------------------
2481 procedure Analyze_Formal_Package_Declaration
(N
: Node_Id
) is
2482 Gen_Id
: constant Node_Id
:= Name
(N
);
2483 Loc
: constant Source_Ptr
:= Sloc
(N
);
2484 Pack_Id
: constant Entity_Id
:= Defining_Identifier
(N
);
2487 Gen_Unit
: Entity_Id
;
2490 Vis_Prims_List
: Elist_Id
:= No_Elist
;
2491 -- List of primitives made temporarily visible in the instantiation
2492 -- to match the visibility of the formal type.
2494 function Build_Local_Package
return Node_Id
;
2495 -- The formal package is rewritten so that its parameters are replaced
2496 -- with corresponding declarations. For parameters with bona fide
2497 -- associations these declarations are created by Analyze_Associations
2498 -- as for a regular instantiation. For boxed parameters, we preserve
2499 -- the formal declarations and analyze them, in order to introduce
2500 -- entities of the right kind in the environment of the formal.
2502 -------------------------
2503 -- Build_Local_Package --
2504 -------------------------
2506 function Build_Local_Package
return Node_Id
is
2508 Pack_Decl
: Node_Id
;
2511 -- Within the formal, the name of the generic package is a renaming
2512 -- of the formal (as for a regular instantiation).
2515 Make_Package_Declaration
(Loc
,
2518 (Specification
(Original_Node
(Gen_Decl
)),
2519 Empty
, Instantiating
=> True));
2522 Make_Package_Renaming_Declaration
(Loc
,
2523 Defining_Unit_Name
=>
2524 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
)),
2525 Name
=> New_Occurrence_Of
(Formal
, Loc
));
2527 if Nkind
(Gen_Id
) = N_Identifier
2528 and then Chars
(Gen_Id
) = Chars
(Pack_Id
)
2531 ("& is hidden within declaration of instance", Gen_Id
, Gen_Unit
);
2534 -- If the formal is declared with a box, or with an others choice,
2535 -- create corresponding declarations for all entities in the formal
2536 -- part, so that names with the proper types are available in the
2537 -- specification of the formal package.
2539 -- On the other hand, if there are no associations, then all the
2540 -- formals must have defaults, and this will be checked by the
2541 -- call to Analyze_Associations.
2544 or else Nkind
(First
(Generic_Associations
(N
))) = N_Others_Choice
2547 Formal_Decl
: Node_Id
;
2550 -- TBA : for a formal package, need to recurse ???
2555 (Generic_Formal_Declarations
(Original_Node
(Gen_Decl
)));
2556 while Present
(Formal_Decl
) loop
2558 (Decls
, Copy_Generic_Node
(Formal_Decl
, Empty
, True));
2563 -- If generic associations are present, use Analyze_Associations to
2564 -- create the proper renaming declarations.
2568 Act_Tree
: constant Node_Id
:=
2570 (Original_Node
(Gen_Decl
), Empty
,
2571 Instantiating
=> True);
2574 Generic_Renamings
.Set_Last
(0);
2575 Generic_Renamings_HTable
.Reset
;
2576 Instantiation_Node
:= N
;
2579 Analyze_Associations
2580 (I_Node
=> Original_Node
(N
),
2581 Formals
=> Generic_Formal_Declarations
(Act_Tree
),
2582 F_Copy
=> Generic_Formal_Declarations
(Gen_Decl
));
2584 Vis_Prims_List
:= Check_Hidden_Primitives
(Decls
);
2588 Append
(Renaming
, To
=> Decls
);
2590 -- Add generated declarations ahead of local declarations in
2593 if No
(Visible_Declarations
(Specification
(Pack_Decl
))) then
2594 Set_Visible_Declarations
(Specification
(Pack_Decl
), Decls
);
2597 (First
(Visible_Declarations
(Specification
(Pack_Decl
))),
2602 end Build_Local_Package
;
2606 Save_IPSM
: constant Boolean := Ignore_Pragma_SPARK_Mode
;
2607 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
2609 Associations
: Boolean := True;
2611 Parent_Installed
: Boolean := False;
2612 Parent_Instance
: Entity_Id
;
2613 Renaming_In_Par
: Entity_Id
;
2615 -- Start of processing for Analyze_Formal_Package_Declaration
2618 Check_Text_IO_Special_Unit
(Gen_Id
);
2621 Check_Generic_Child_Unit
(Gen_Id
, Parent_Installed
);
2622 Gen_Unit
:= Entity
(Gen_Id
);
2624 -- Check for a formal package that is a package renaming
2626 if Present
(Renamed_Object
(Gen_Unit
)) then
2628 -- Indicate that unit is used, before replacing it with renamed
2629 -- entity for use below.
2631 if In_Extended_Main_Source_Unit
(N
) then
2632 Set_Is_Instantiated
(Gen_Unit
);
2633 Generate_Reference
(Gen_Unit
, N
);
2636 Gen_Unit
:= Renamed_Object
(Gen_Unit
);
2639 if Ekind
(Gen_Unit
) /= E_Generic_Package
then
2640 Error_Msg_N
("expect generic package name", Gen_Id
);
2644 elsif Gen_Unit
= Current_Scope
then
2646 ("generic package cannot be used as a formal package of itself",
2651 elsif In_Open_Scopes
(Gen_Unit
) then
2652 if Is_Compilation_Unit
(Gen_Unit
)
2653 and then Is_Child_Unit
(Current_Scope
)
2655 -- Special-case the error when the formal is a parent, and
2656 -- continue analysis to minimize cascaded errors.
2659 ("generic parent cannot be used as formal package "
2660 & "of a child unit", Gen_Id
);
2664 ("generic package cannot be used as a formal package "
2665 & "within itself", Gen_Id
);
2671 -- Check that name of formal package does not hide name of generic,
2672 -- or its leading prefix. This check must be done separately because
2673 -- the name of the generic has already been analyzed.
2676 Gen_Name
: Entity_Id
;
2680 while Nkind
(Gen_Name
) = N_Expanded_Name
loop
2681 Gen_Name
:= Prefix
(Gen_Name
);
2684 if Chars
(Gen_Name
) = Chars
(Pack_Id
) then
2686 ("& is hidden within declaration of formal package",
2692 or else No
(Generic_Associations
(N
))
2693 or else Nkind
(First
(Generic_Associations
(N
))) = N_Others_Choice
2695 Associations
:= False;
2698 -- If there are no generic associations, the generic parameters appear
2699 -- as local entities and are instantiated like them. We copy the generic
2700 -- package declaration as if it were an instantiation, and analyze it
2701 -- like a regular package, except that we treat the formals as
2702 -- additional visible components.
2704 Gen_Decl
:= Unit_Declaration_Node
(Gen_Unit
);
2706 if In_Extended_Main_Source_Unit
(N
) then
2707 Set_Is_Instantiated
(Gen_Unit
);
2708 Generate_Reference
(Gen_Unit
, N
);
2711 Formal
:= New_Copy
(Pack_Id
);
2712 Create_Instantiation_Source
(N
, Gen_Unit
, S_Adjustment
);
2714 -- Make local generic without formals. The formals will be replaced with
2715 -- internal declarations.
2718 New_N
:= Build_Local_Package
;
2720 -- If there are errors in the parameter list, Analyze_Associations
2721 -- raises Instantiation_Error. Patch the declaration to prevent further
2722 -- exception propagation.
2725 when Instantiation_Error
=>
2726 Enter_Name
(Formal
);
2727 Set_Ekind
(Formal
, E_Variable
);
2728 Set_Etype
(Formal
, Any_Type
);
2729 Restore_Hidden_Primitives
(Vis_Prims_List
);
2731 if Parent_Installed
then
2739 Set_Defining_Unit_Name
(Specification
(New_N
), Formal
);
2740 Set_Generic_Parent
(Specification
(N
), Gen_Unit
);
2741 Set_Instance_Env
(Gen_Unit
, Formal
);
2742 Set_Is_Generic_Instance
(Formal
);
2744 Enter_Name
(Formal
);
2745 Set_Ekind
(Formal
, E_Package
);
2746 Set_Etype
(Formal
, Standard_Void_Type
);
2747 Set_Inner_Instances
(Formal
, New_Elmt_List
);
2748 Push_Scope
(Formal
);
2750 -- Manually set the SPARK_Mode from the context because the package
2751 -- declaration is never analyzed.
2753 Set_SPARK_Pragma
(Formal
, SPARK_Mode_Pragma
);
2754 Set_SPARK_Aux_Pragma
(Formal
, SPARK_Mode_Pragma
);
2755 Set_SPARK_Pragma_Inherited
(Formal
);
2756 Set_SPARK_Aux_Pragma_Inherited
(Formal
);
2758 if Is_Child_Unit
(Gen_Unit
) and then Parent_Installed
then
2760 -- Similarly, we have to make the name of the formal visible in the
2761 -- parent instance, to resolve properly fully qualified names that
2762 -- may appear in the generic unit. The parent instance has been
2763 -- placed on the scope stack ahead of the current scope.
2765 Parent_Instance
:= Scope_Stack
.Table
(Scope_Stack
.Last
- 1).Entity
;
2768 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
));
2769 Set_Ekind
(Renaming_In_Par
, E_Package
);
2770 Set_Etype
(Renaming_In_Par
, Standard_Void_Type
);
2771 Set_Scope
(Renaming_In_Par
, Parent_Instance
);
2772 Set_Parent
(Renaming_In_Par
, Parent
(Formal
));
2773 Set_Renamed_Object
(Renaming_In_Par
, Formal
);
2774 Append_Entity
(Renaming_In_Par
, Parent_Instance
);
2777 -- A formal package declaration behaves as a package instantiation with
2778 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
2779 -- missing, set the global flag which signals Analyze_Pragma to ingnore
2780 -- all SPARK_Mode pragmas within the generic_package_name.
2782 if SPARK_Mode
/= On
then
2783 Ignore_Pragma_SPARK_Mode
:= True;
2786 Analyze
(Specification
(N
));
2788 -- The formals for which associations are provided are not visible
2789 -- outside of the formal package. The others are still declared by a
2790 -- formal parameter declaration.
2792 -- If there are no associations, the only local entity to hide is the
2793 -- generated package renaming itself.
2799 E
:= First_Entity
(Formal
);
2800 while Present
(E
) loop
2801 if Associations
and then not Is_Generic_Formal
(E
) then
2805 if Ekind
(E
) = E_Package
and then Renamed_Entity
(E
) = Formal
then
2814 End_Package_Scope
(Formal
);
2815 Restore_Hidden_Primitives
(Vis_Prims_List
);
2817 if Parent_Installed
then
2823 -- Inside the generic unit, the formal package is a regular package, but
2824 -- no body is needed for it. Note that after instantiation, the defining
2825 -- unit name we need is in the new tree and not in the original (see
2826 -- Package_Instantiation). A generic formal package is an instance, and
2827 -- can be used as an actual for an inner instance.
2829 Set_Has_Completion
(Formal
, True);
2831 -- Add semantic information to the original defining identifier for ASIS
2834 Set_Ekind
(Pack_Id
, E_Package
);
2835 Set_Etype
(Pack_Id
, Standard_Void_Type
);
2836 Set_Scope
(Pack_Id
, Scope
(Formal
));
2837 Set_Has_Completion
(Pack_Id
, True);
2840 if Has_Aspects
(N
) then
2841 Analyze_Aspect_Specifications
(N
, Pack_Id
);
2844 Ignore_Pragma_SPARK_Mode
:= Save_IPSM
;
2845 end Analyze_Formal_Package_Declaration
;
2847 ---------------------------------
2848 -- Analyze_Formal_Private_Type --
2849 ---------------------------------
2851 procedure Analyze_Formal_Private_Type
2857 New_Private_Type
(N
, T
, Def
);
2859 -- Set the size to an arbitrary but legal value
2861 Set_Size_Info
(T
, Standard_Integer
);
2862 Set_RM_Size
(T
, RM_Size
(Standard_Integer
));
2863 end Analyze_Formal_Private_Type
;
2865 ------------------------------------
2866 -- Analyze_Formal_Incomplete_Type --
2867 ------------------------------------
2869 procedure Analyze_Formal_Incomplete_Type
2875 Set_Ekind
(T
, E_Incomplete_Type
);
2877 Set_Private_Dependents
(T
, New_Elmt_List
);
2879 if Tagged_Present
(Def
) then
2880 Set_Is_Tagged_Type
(T
);
2881 Make_Class_Wide_Type
(T
);
2882 Set_Direct_Primitive_Operations
(T
, New_Elmt_List
);
2884 end Analyze_Formal_Incomplete_Type
;
2886 ----------------------------------------
2887 -- Analyze_Formal_Signed_Integer_Type --
2888 ----------------------------------------
2890 procedure Analyze_Formal_Signed_Integer_Type
2894 Base
: constant Entity_Id
:=
2896 (E_Signed_Integer_Type
,
2898 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
2903 Set_Ekind
(T
, E_Signed_Integer_Subtype
);
2904 Set_Etype
(T
, Base
);
2905 Set_Size_Info
(T
, Standard_Integer
);
2906 Set_RM_Size
(T
, RM_Size
(Standard_Integer
));
2907 Set_Scalar_Range
(T
, Scalar_Range
(Standard_Integer
));
2908 Set_Is_Constrained
(T
);
2910 Set_Is_Generic_Type
(Base
);
2911 Set_Size_Info
(Base
, Standard_Integer
);
2912 Set_RM_Size
(Base
, RM_Size
(Standard_Integer
));
2913 Set_Etype
(Base
, Base
);
2914 Set_Scalar_Range
(Base
, Scalar_Range
(Standard_Integer
));
2915 Set_Parent
(Base
, Parent
(Def
));
2916 end Analyze_Formal_Signed_Integer_Type
;
2918 -------------------------------------------
2919 -- Analyze_Formal_Subprogram_Declaration --
2920 -------------------------------------------
2922 procedure Analyze_Formal_Subprogram_Declaration
(N
: Node_Id
) is
2923 Spec
: constant Node_Id
:= Specification
(N
);
2924 Def
: constant Node_Id
:= Default_Name
(N
);
2925 Nam
: constant Entity_Id
:= Defining_Unit_Name
(Spec
);
2933 if Nkind
(Nam
) = N_Defining_Program_Unit_Name
then
2934 Error_Msg_N
("name of formal subprogram must be a direct name", Nam
);
2938 Analyze_Subprogram_Declaration
(N
);
2939 Set_Is_Formal_Subprogram
(Nam
);
2940 Set_Has_Completion
(Nam
);
2942 if Nkind
(N
) = N_Formal_Abstract_Subprogram_Declaration
then
2943 Set_Is_Abstract_Subprogram
(Nam
);
2945 Set_Is_Dispatching_Operation
(Nam
);
2947 -- A formal abstract procedure cannot have a null default
2948 -- (RM 12.6(4.1/2)).
2950 if Nkind
(Spec
) = N_Procedure_Specification
2951 and then Null_Present
(Spec
)
2954 ("a formal abstract subprogram cannot default to null", Spec
);
2958 Ctrl_Type
: constant Entity_Id
:= Find_Dispatching_Type
(Nam
);
2960 if No
(Ctrl_Type
) then
2962 ("abstract formal subprogram must have a controlling type",
2965 elsif Ada_Version
>= Ada_2012
2966 and then Is_Incomplete_Type
(Ctrl_Type
)
2969 ("controlling type of abstract formal subprogram cannot "
2970 & "be incomplete type", N
, Ctrl_Type
);
2973 Check_Controlling_Formals
(Ctrl_Type
, Nam
);
2978 -- Default name is resolved at the point of instantiation
2980 if Box_Present
(N
) then
2983 -- Else default is bound at the point of generic declaration
2985 elsif Present
(Def
) then
2986 if Nkind
(Def
) = N_Operator_Symbol
then
2987 Find_Direct_Name
(Def
);
2989 elsif Nkind
(Def
) /= N_Attribute_Reference
then
2993 -- For an attribute reference, analyze the prefix and verify
2994 -- that it has the proper profile for the subprogram.
2996 Analyze
(Prefix
(Def
));
2997 Valid_Default_Attribute
(Nam
, Def
);
3001 -- Default name may be overloaded, in which case the interpretation
3002 -- with the correct profile must be selected, as for a renaming.
3003 -- If the definition is an indexed component, it must denote a
3004 -- member of an entry family. If it is a selected component, it
3005 -- can be a protected operation.
3007 if Etype
(Def
) = Any_Type
then
3010 elsif Nkind
(Def
) = N_Selected_Component
then
3011 if not Is_Overloadable
(Entity
(Selector_Name
(Def
))) then
3012 Error_Msg_N
("expect valid subprogram name as default", Def
);
3015 elsif Nkind
(Def
) = N_Indexed_Component
then
3016 if Is_Entity_Name
(Prefix
(Def
)) then
3017 if Ekind
(Entity
(Prefix
(Def
))) /= E_Entry_Family
then
3018 Error_Msg_N
("expect valid subprogram name as default", Def
);
3021 elsif Nkind
(Prefix
(Def
)) = N_Selected_Component
then
3022 if Ekind
(Entity
(Selector_Name
(Prefix
(Def
)))) /=
3025 Error_Msg_N
("expect valid subprogram name as default", Def
);
3029 Error_Msg_N
("expect valid subprogram name as default", Def
);
3033 elsif Nkind
(Def
) = N_Character_Literal
then
3035 -- Needs some type checks: subprogram should be parameterless???
3037 Resolve
(Def
, (Etype
(Nam
)));
3039 elsif not Is_Entity_Name
(Def
)
3040 or else not Is_Overloadable
(Entity
(Def
))
3042 Error_Msg_N
("expect valid subprogram name as default", Def
);
3045 elsif not Is_Overloaded
(Def
) then
3046 Subp
:= Entity
(Def
);
3049 Error_Msg_N
("premature usage of formal subprogram", Def
);
3051 elsif not Entity_Matches_Spec
(Subp
, Nam
) then
3052 Error_Msg_N
("no visible entity matches specification", Def
);
3055 -- More than one interpretation, so disambiguate as for a renaming
3060 I1
: Interp_Index
:= 0;
3066 Get_First_Interp
(Def
, I
, It
);
3067 while Present
(It
.Nam
) loop
3068 if Entity_Matches_Spec
(It
.Nam
, Nam
) then
3069 if Subp
/= Any_Id
then
3070 It1
:= Disambiguate
(Def
, I1
, I
, Etype
(Subp
));
3072 if It1
= No_Interp
then
3073 Error_Msg_N
("ambiguous default subprogram", Def
);
3086 Get_Next_Interp
(I
, It
);
3090 if Subp
/= Any_Id
then
3092 -- Subprogram found, generate reference to it
3094 Set_Entity
(Def
, Subp
);
3095 Generate_Reference
(Subp
, Def
);
3098 Error_Msg_N
("premature usage of formal subprogram", Def
);
3100 elsif Ekind
(Subp
) /= E_Operator
then
3101 Check_Mode_Conformant
(Subp
, Nam
);
3105 Error_Msg_N
("no visible subprogram matches specification", N
);
3111 if Has_Aspects
(N
) then
3112 Analyze_Aspect_Specifications
(N
, Nam
);
3115 end Analyze_Formal_Subprogram_Declaration
;
3117 -------------------------------------
3118 -- Analyze_Formal_Type_Declaration --
3119 -------------------------------------
3121 procedure Analyze_Formal_Type_Declaration
(N
: Node_Id
) is
3122 Def
: constant Node_Id
:= Formal_Type_Definition
(N
);
3126 T
:= Defining_Identifier
(N
);
3128 if Present
(Discriminant_Specifications
(N
))
3129 and then Nkind
(Def
) /= N_Formal_Private_Type_Definition
3132 ("discriminants not allowed for this formal type", T
);
3135 -- Enter the new name, and branch to specific routine
3138 when N_Formal_Private_Type_Definition
=>
3139 Analyze_Formal_Private_Type
(N
, T
, Def
);
3141 when N_Formal_Derived_Type_Definition
=>
3142 Analyze_Formal_Derived_Type
(N
, T
, Def
);
3144 when N_Formal_Incomplete_Type_Definition
=>
3145 Analyze_Formal_Incomplete_Type
(T
, Def
);
3147 when N_Formal_Discrete_Type_Definition
=>
3148 Analyze_Formal_Discrete_Type
(T
, Def
);
3150 when N_Formal_Signed_Integer_Type_Definition
=>
3151 Analyze_Formal_Signed_Integer_Type
(T
, Def
);
3153 when N_Formal_Modular_Type_Definition
=>
3154 Analyze_Formal_Modular_Type
(T
, Def
);
3156 when N_Formal_Floating_Point_Definition
=>
3157 Analyze_Formal_Floating_Type
(T
, Def
);
3159 when N_Formal_Ordinary_Fixed_Point_Definition
=>
3160 Analyze_Formal_Ordinary_Fixed_Point_Type
(T
, Def
);
3162 when N_Formal_Decimal_Fixed_Point_Definition
=>
3163 Analyze_Formal_Decimal_Fixed_Point_Type
(T
, Def
);
3165 when N_Array_Type_Definition
=>
3166 Analyze_Formal_Array_Type
(T
, Def
);
3168 when N_Access_To_Object_Definition |
3169 N_Access_Function_Definition |
3170 N_Access_Procedure_Definition
=>
3171 Analyze_Generic_Access_Type
(T
, Def
);
3173 -- Ada 2005: a interface declaration is encoded as an abstract
3174 -- record declaration or a abstract type derivation.
3176 when N_Record_Definition
=>
3177 Analyze_Formal_Interface_Type
(N
, T
, Def
);
3179 when N_Derived_Type_Definition
=>
3180 Analyze_Formal_Derived_Interface_Type
(N
, T
, Def
);
3186 raise Program_Error
;
3190 Set_Is_Generic_Type
(T
);
3192 if Has_Aspects
(N
) then
3193 Analyze_Aspect_Specifications
(N
, T
);
3195 end Analyze_Formal_Type_Declaration
;
3197 ------------------------------------
3198 -- Analyze_Function_Instantiation --
3199 ------------------------------------
3201 procedure Analyze_Function_Instantiation
(N
: Node_Id
) is
3203 Analyze_Subprogram_Instantiation
(N
, E_Function
);
3204 end Analyze_Function_Instantiation
;
3206 ---------------------------------
3207 -- Analyze_Generic_Access_Type --
3208 ---------------------------------
3210 procedure Analyze_Generic_Access_Type
(T
: Entity_Id
; Def
: Node_Id
) is
3214 if Nkind
(Def
) = N_Access_To_Object_Definition
then
3215 Access_Type_Declaration
(T
, Def
);
3217 if Is_Incomplete_Or_Private_Type
(Designated_Type
(T
))
3218 and then No
(Full_View
(Designated_Type
(T
)))
3219 and then not Is_Generic_Type
(Designated_Type
(T
))
3221 Error_Msg_N
("premature usage of incomplete type", Def
);
3223 elsif not Is_Entity_Name
(Subtype_Indication
(Def
)) then
3225 ("only a subtype mark is allowed in a formal", Def
);
3229 Access_Subprogram_Declaration
(T
, Def
);
3231 end Analyze_Generic_Access_Type
;
3233 ---------------------------------
3234 -- Analyze_Generic_Formal_Part --
3235 ---------------------------------
3237 procedure Analyze_Generic_Formal_Part
(N
: Node_Id
) is
3238 Gen_Parm_Decl
: Node_Id
;
3241 -- The generic formals are processed in the scope of the generic unit,
3242 -- where they are immediately visible. The scope is installed by the
3245 Gen_Parm_Decl
:= First
(Generic_Formal_Declarations
(N
));
3246 while Present
(Gen_Parm_Decl
) loop
3247 Analyze
(Gen_Parm_Decl
);
3248 Next
(Gen_Parm_Decl
);
3251 Generate_Reference_To_Generic_Formals
(Current_Scope
);
3252 end Analyze_Generic_Formal_Part
;
3254 ------------------------------------------
3255 -- Analyze_Generic_Package_Declaration --
3256 ------------------------------------------
3258 procedure Analyze_Generic_Package_Declaration
(N
: Node_Id
) is
3259 Loc
: constant Source_Ptr
:= Sloc
(N
);
3260 Decls
: constant List_Id
:=
3261 Visible_Declarations
(Specification
(N
));
3266 Save_Parent
: Node_Id
;
3269 Check_SPARK_05_Restriction
("generic is not allowed", N
);
3271 -- We introduce a renaming of the enclosing package, to have a usable
3272 -- entity as the prefix of an expanded name for a local entity of the
3273 -- form Par.P.Q, where P is the generic package. This is because a local
3274 -- entity named P may hide it, so that the usual visibility rules in
3275 -- the instance will not resolve properly.
3278 Make_Package_Renaming_Declaration
(Loc
,
3279 Defining_Unit_Name
=>
3280 Make_Defining_Identifier
(Loc
,
3281 Chars
=> New_External_Name
(Chars
(Defining_Entity
(N
)), "GH")),
3283 Make_Identifier
(Loc
, Chars
(Defining_Entity
(N
))));
3285 if Present
(Decls
) then
3286 Decl
:= First
(Decls
);
3287 while Present
(Decl
) and then Nkind
(Decl
) = N_Pragma
loop
3291 if Present
(Decl
) then
3292 Insert_Before
(Decl
, Renaming
);
3294 Append
(Renaming
, Visible_Declarations
(Specification
(N
)));
3298 Set_Visible_Declarations
(Specification
(N
), New_List
(Renaming
));
3301 -- Create copy of generic unit, and save for instantiation. If the unit
3302 -- is a child unit, do not copy the specifications for the parent, which
3303 -- are not part of the generic tree.
3305 Save_Parent
:= Parent_Spec
(N
);
3306 Set_Parent_Spec
(N
, Empty
);
3308 New_N
:= Copy_Generic_Node
(N
, Empty
, Instantiating
=> False);
3309 Set_Parent_Spec
(New_N
, Save_Parent
);
3312 -- Once the contents of the generic copy and the template are swapped,
3313 -- do the same for their respective aspect specifications.
3315 Exchange_Aspects
(N
, New_N
);
3317 -- Collect all contract-related source pragmas found within the template
3318 -- and attach them to the contract of the package spec. This contract is
3319 -- used in the capture of global references within annotations.
3321 Create_Generic_Contract
(N
);
3323 Id
:= Defining_Entity
(N
);
3324 Generate_Definition
(Id
);
3326 -- Expansion is not applied to generic units
3331 Set_Ekind
(Id
, E_Generic_Package
);
3332 Set_Etype
(Id
, Standard_Void_Type
);
3334 -- A generic package declared within a Ghost region is rendered Ghost
3335 -- (SPARK RM 6.9(2)).
3337 if Ghost_Mode
> None
then
3338 Set_Is_Ghost_Entity
(Id
);
3341 -- Analyze aspects now, so that generated pragmas appear in the
3342 -- declarations before building and analyzing the generic copy.
3344 if Has_Aspects
(N
) then
3345 Analyze_Aspect_Specifications
(N
, Id
);
3349 Enter_Generic_Scope
(Id
);
3350 Set_Inner_Instances
(Id
, New_Elmt_List
);
3352 Set_Categorization_From_Pragmas
(N
);
3353 Set_Is_Pure
(Id
, Is_Pure
(Current_Scope
));
3355 -- Link the declaration of the generic homonym in the generic copy to
3356 -- the package it renames, so that it is always resolved properly.
3358 Set_Generic_Homonym
(Id
, Defining_Unit_Name
(Renaming
));
3359 Set_Entity
(Associated_Node
(Name
(Renaming
)), Id
);
3361 -- For a library unit, we have reconstructed the entity for the unit,
3362 -- and must reset it in the library tables.
3364 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
3365 Set_Cunit_Entity
(Current_Sem_Unit
, Id
);
3368 Analyze_Generic_Formal_Part
(N
);
3370 -- After processing the generic formals, analysis proceeds as for a
3371 -- non-generic package.
3373 Analyze
(Specification
(N
));
3375 Validate_Categorization_Dependency
(N
, Id
);
3379 End_Package_Scope
(Id
);
3380 Exit_Generic_Scope
(Id
);
3382 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
3383 Move_Freeze_Nodes
(Id
, N
, Visible_Declarations
(Specification
(N
)));
3384 Move_Freeze_Nodes
(Id
, N
, Private_Declarations
(Specification
(N
)));
3385 Move_Freeze_Nodes
(Id
, N
, Generic_Formal_Declarations
(N
));
3388 Set_Body_Required
(Parent
(N
), Unit_Requires_Body
(Id
));
3389 Validate_RT_RAT_Component
(N
);
3391 -- If this is a spec without a body, check that generic parameters
3394 if not Body_Required
(Parent
(N
)) then
3395 Check_References
(Id
);
3399 -- If there is a specified storage pool in the context, create an
3400 -- aspect on the package declaration, so that it is used in any
3401 -- instance that does not override it.
3403 if Present
(Default_Pool
) then
3409 Make_Aspect_Specification
(Loc
,
3410 Identifier
=> Make_Identifier
(Loc
, Name_Default_Storage_Pool
),
3411 Expression
=> New_Copy
(Default_Pool
));
3413 if No
(Aspect_Specifications
(Specification
(N
))) then
3414 Set_Aspect_Specifications
(Specification
(N
), New_List
(ASN
));
3416 Append
(ASN
, Aspect_Specifications
(Specification
(N
)));
3420 end Analyze_Generic_Package_Declaration
;
3422 --------------------------------------------
3423 -- Analyze_Generic_Subprogram_Declaration --
3424 --------------------------------------------
3426 procedure Analyze_Generic_Subprogram_Declaration
(N
: Node_Id
) is
3430 Result_Type
: Entity_Id
;
3431 Save_Parent
: Node_Id
;
3436 Check_SPARK_05_Restriction
("generic is not allowed", N
);
3438 -- Create copy of generic unit, and save for instantiation. If the unit
3439 -- is a child unit, do not copy the specifications for the parent, which
3440 -- are not part of the generic tree.
3442 Save_Parent
:= Parent_Spec
(N
);
3443 Set_Parent_Spec
(N
, Empty
);
3445 New_N
:= Copy_Generic_Node
(N
, Empty
, Instantiating
=> False);
3446 Set_Parent_Spec
(New_N
, Save_Parent
);
3449 -- Once the contents of the generic copy and the template are swapped,
3450 -- do the same for their respective aspect specifications.
3452 Exchange_Aspects
(N
, New_N
);
3454 -- Collect all contract-related source pragmas found within the template
3455 -- and attach them to the contract of the subprogram spec. This contract
3456 -- is used in the capture of global references within annotations.
3458 Create_Generic_Contract
(N
);
3460 Spec
:= Specification
(N
);
3461 Id
:= Defining_Entity
(Spec
);
3462 Generate_Definition
(Id
);
3464 if Nkind
(Id
) = N_Defining_Operator_Symbol
then
3466 ("operator symbol not allowed for generic subprogram", Id
);
3472 Set_Scope_Depth_Value
(Id
, Scope_Depth
(Current_Scope
) + 1);
3474 -- Analyze the aspects of the generic copy to ensure that all generated
3475 -- pragmas (if any) perform their semantic effects.
3477 if Has_Aspects
(N
) then
3478 Analyze_Aspect_Specifications
(N
, Id
);
3482 Enter_Generic_Scope
(Id
);
3483 Set_Inner_Instances
(Id
, New_Elmt_List
);
3484 Set_Is_Pure
(Id
, Is_Pure
(Current_Scope
));
3486 Analyze_Generic_Formal_Part
(N
);
3488 Formals
:= Parameter_Specifications
(Spec
);
3490 if Nkind
(Spec
) = N_Function_Specification
then
3491 Set_Ekind
(Id
, E_Generic_Function
);
3493 Set_Ekind
(Id
, E_Generic_Procedure
);
3496 if Present
(Formals
) then
3497 Process_Formals
(Formals
, Spec
);
3500 if Nkind
(Spec
) = N_Function_Specification
then
3501 if Nkind
(Result_Definition
(Spec
)) = N_Access_Definition
then
3502 Result_Type
:= Access_Definition
(Spec
, Result_Definition
(Spec
));
3503 Set_Etype
(Id
, Result_Type
);
3505 -- Check restriction imposed by AI05-073: a generic function
3506 -- cannot return an abstract type or an access to such.
3508 -- This is a binding interpretation should it apply to earlier
3509 -- versions of Ada as well as Ada 2012???
3511 if Is_Abstract_Type
(Designated_Type
(Result_Type
))
3512 and then Ada_Version
>= Ada_2012
3515 ("generic function cannot have an access result "
3516 & "that designates an abstract type", Spec
);
3520 Find_Type
(Result_Definition
(Spec
));
3521 Typ
:= Entity
(Result_Definition
(Spec
));
3523 if Is_Abstract_Type
(Typ
)
3524 and then Ada_Version
>= Ada_2012
3527 ("generic function cannot have abstract result type", Spec
);
3530 -- If a null exclusion is imposed on the result type, then create
3531 -- a null-excluding itype (an access subtype) and use it as the
3532 -- function's Etype.
3534 if Is_Access_Type
(Typ
)
3535 and then Null_Exclusion_Present
(Spec
)
3538 Create_Null_Excluding_Itype
3540 Related_Nod
=> Spec
,
3541 Scope_Id
=> Defining_Unit_Name
(Spec
)));
3543 Set_Etype
(Id
, Typ
);
3548 Set_Etype
(Id
, Standard_Void_Type
);
3551 -- A generic subprogram declared within a Ghost region is rendered Ghost
3552 -- (SPARK RM 6.9(2)).
3554 if Ghost_Mode
> None
then
3555 Set_Is_Ghost_Entity
(Id
);
3558 -- For a library unit, we have reconstructed the entity for the unit,
3559 -- and must reset it in the library tables. We also make sure that
3560 -- Body_Required is set properly in the original compilation unit node.
3562 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
3563 Set_Cunit_Entity
(Current_Sem_Unit
, Id
);
3564 Set_Body_Required
(Parent
(N
), Unit_Requires_Body
(Id
));
3567 Set_Categorization_From_Pragmas
(N
);
3568 Validate_Categorization_Dependency
(N
, Id
);
3570 -- Capture all global references that occur within the profile of the
3571 -- generic subprogram. Aspects are not part of this processing because
3572 -- they must be delayed. If processed now, Save_Global_References will
3573 -- destroy the Associated_Node links and prevent the capture of global
3574 -- references when the contract of the generic subprogram is analyzed.
3576 Save_Global_References
(Original_Node
(N
));
3580 Exit_Generic_Scope
(Id
);
3581 Generate_Reference_To_Formals
(Id
);
3583 List_Inherited_Pre_Post_Aspects
(Id
);
3584 end Analyze_Generic_Subprogram_Declaration
;
3586 -----------------------------------
3587 -- Analyze_Package_Instantiation --
3588 -----------------------------------
3590 procedure Analyze_Package_Instantiation
(N
: Node_Id
) is
3591 Loc
: constant Source_Ptr
:= Sloc
(N
);
3592 Gen_Id
: constant Node_Id
:= Name
(N
);
3595 Act_Decl_Name
: Node_Id
;
3596 Act_Decl_Id
: Entity_Id
;
3602 Gen_Unit
: Entity_Id
;
3604 Is_Actual_Pack
: constant Boolean :=
3605 Is_Internal
(Defining_Entity
(N
));
3607 Env_Installed
: Boolean := False;
3608 Parent_Installed
: Boolean := False;
3609 Renaming_List
: List_Id
;
3610 Unit_Renaming
: Node_Id
;
3611 Needs_Body
: Boolean;
3612 Inline_Now
: Boolean := False;
3613 Has_Inline_Always
: Boolean := False;
3615 Save_IPSM
: constant Boolean := Ignore_Pragma_SPARK_Mode
;
3616 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
3618 Save_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
3619 Save_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
3620 -- Save the SPARK_Mode-related data for restore on exit
3622 Save_Style_Check
: constant Boolean := Style_Check
;
3623 -- Save style check mode for restore on exit
3625 procedure Delay_Descriptors
(E
: Entity_Id
);
3626 -- Delay generation of subprogram descriptors for given entity
3628 function Might_Inline_Subp
return Boolean;
3629 -- If inlining is active and the generic contains inlined subprograms,
3630 -- we instantiate the body. This may cause superfluous instantiations,
3631 -- but it is simpler than detecting the need for the body at the point
3632 -- of inlining, when the context of the instance is not available.
3634 -----------------------
3635 -- Delay_Descriptors --
3636 -----------------------
3638 procedure Delay_Descriptors
(E
: Entity_Id
) is
3640 if not Delay_Subprogram_Descriptors
(E
) then
3641 Set_Delay_Subprogram_Descriptors
(E
);
3642 Pending_Descriptor
.Append
(E
);
3644 end Delay_Descriptors
;
3646 -----------------------
3647 -- Might_Inline_Subp --
3648 -----------------------
3650 function Might_Inline_Subp
return Boolean is
3654 if not Inline_Processing_Required
then
3658 E
:= First_Entity
(Gen_Unit
);
3659 while Present
(E
) loop
3660 if Is_Subprogram
(E
) and then Is_Inlined
(E
) then
3661 -- Remember if there are any subprograms with Inline_Always
3663 if Has_Pragma_Inline_Always
(E
) then
3664 Has_Inline_Always
:= True;
3675 end Might_Inline_Subp
;
3677 -- Local declarations
3679 Vis_Prims_List
: Elist_Id
:= No_Elist
;
3680 -- List of primitives made temporarily visible in the instantiation
3681 -- to match the visibility of the formal type
3683 -- Start of processing for Analyze_Package_Instantiation
3686 Check_SPARK_05_Restriction
("generic is not allowed", N
);
3688 -- Very first thing: check for Text_IO special unit in case we are
3689 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3691 Check_Text_IO_Special_Unit
(Name
(N
));
3693 -- Make node global for error reporting
3695 Instantiation_Node
:= N
;
3697 -- Turn off style checking in instances. If the check is enabled on the
3698 -- generic unit, a warning in an instance would just be noise. If not
3699 -- enabled on the generic, then a warning in an instance is just wrong.
3701 Style_Check
:= False;
3703 -- Case of instantiation of a generic package
3705 if Nkind
(N
) = N_Package_Instantiation
then
3706 Act_Decl_Id
:= New_Copy
(Defining_Entity
(N
));
3707 Set_Comes_From_Source
(Act_Decl_Id
, True);
3709 if Nkind
(Defining_Unit_Name
(N
)) = N_Defining_Program_Unit_Name
then
3711 Make_Defining_Program_Unit_Name
(Loc
,
3713 New_Copy_Tree
(Name
(Defining_Unit_Name
(N
))),
3714 Defining_Identifier
=> Act_Decl_Id
);
3716 Act_Decl_Name
:= Act_Decl_Id
;
3719 -- Case of instantiation of a formal package
3722 Act_Decl_Id
:= Defining_Identifier
(N
);
3723 Act_Decl_Name
:= Act_Decl_Id
;
3726 Generate_Definition
(Act_Decl_Id
);
3727 Set_Ekind
(Act_Decl_Id
, E_Package
);
3729 -- Initialize list of incomplete actuals before analysis
3731 Set_Incomplete_Actuals
(Act_Decl_Id
, New_Elmt_List
);
3733 Preanalyze_Actuals
(N
, Act_Decl_Id
);
3736 Env_Installed
:= True;
3738 -- Reset renaming map for formal types. The mapping is established
3739 -- when analyzing the generic associations, but some mappings are
3740 -- inherited from formal packages of parent units, and these are
3741 -- constructed when the parents are installed.
3743 Generic_Renamings
.Set_Last
(0);
3744 Generic_Renamings_HTable
.Reset
;
3746 Check_Generic_Child_Unit
(Gen_Id
, Parent_Installed
);
3747 Gen_Unit
:= Entity
(Gen_Id
);
3749 -- Verify that it is the name of a generic package
3751 -- A visibility glitch: if the instance is a child unit and the generic
3752 -- is the generic unit of a parent instance (i.e. both the parent and
3753 -- the child units are instances of the same package) the name now
3754 -- denotes the renaming within the parent, not the intended generic
3755 -- unit. See if there is a homonym that is the desired generic. The
3756 -- renaming declaration must be visible inside the instance of the
3757 -- child, but not when analyzing the name in the instantiation itself.
3759 if Ekind
(Gen_Unit
) = E_Package
3760 and then Present
(Renamed_Entity
(Gen_Unit
))
3761 and then In_Open_Scopes
(Renamed_Entity
(Gen_Unit
))
3762 and then Is_Generic_Instance
(Renamed_Entity
(Gen_Unit
))
3763 and then Present
(Homonym
(Gen_Unit
))
3765 Gen_Unit
:= Homonym
(Gen_Unit
);
3768 if Etype
(Gen_Unit
) = Any_Type
then
3772 elsif Ekind
(Gen_Unit
) /= E_Generic_Package
then
3774 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3776 if From_Limited_With
(Gen_Unit
) then
3778 ("cannot instantiate a limited withed package", Gen_Id
);
3781 ("& is not the name of a generic package", Gen_Id
, Gen_Unit
);
3788 if In_Extended_Main_Source_Unit
(N
) then
3789 Set_Is_Instantiated
(Gen_Unit
);
3790 Generate_Reference
(Gen_Unit
, N
);
3792 if Present
(Renamed_Object
(Gen_Unit
)) then
3793 Set_Is_Instantiated
(Renamed_Object
(Gen_Unit
));
3794 Generate_Reference
(Renamed_Object
(Gen_Unit
), N
);
3798 if Nkind
(Gen_Id
) = N_Identifier
3799 and then Chars
(Gen_Unit
) = Chars
(Defining_Entity
(N
))
3802 ("& is hidden within declaration of instance", Gen_Id
, Gen_Unit
);
3804 elsif Nkind
(Gen_Id
) = N_Expanded_Name
3805 and then Is_Child_Unit
(Gen_Unit
)
3806 and then Nkind
(Prefix
(Gen_Id
)) = N_Identifier
3807 and then Chars
(Act_Decl_Id
) = Chars
(Prefix
(Gen_Id
))
3810 ("& is hidden within declaration of instance ", Prefix
(Gen_Id
));
3813 Set_Entity
(Gen_Id
, Gen_Unit
);
3815 -- If generic is a renaming, get original generic unit
3817 if Present
(Renamed_Object
(Gen_Unit
))
3818 and then Ekind
(Renamed_Object
(Gen_Unit
)) = E_Generic_Package
3820 Gen_Unit
:= Renamed_Object
(Gen_Unit
);
3823 -- Verify that there are no circular instantiations
3825 if In_Open_Scopes
(Gen_Unit
) then
3826 Error_Msg_NE
("instantiation of & within itself", N
, Gen_Unit
);
3830 elsif Contains_Instance_Of
(Gen_Unit
, Current_Scope
, Gen_Id
) then
3831 Error_Msg_Node_2
:= Current_Scope
;
3833 ("circular Instantiation: & instantiated in &!", N
, Gen_Unit
);
3834 Circularity_Detected
:= True;
3839 -- If the context of the instance is subject to SPARK_Mode "off" or
3840 -- the annotation is altogether missing, set the global flag which
3841 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
3844 if SPARK_Mode
/= On
then
3845 Ignore_Pragma_SPARK_Mode
:= True;
3848 Gen_Decl
:= Unit_Declaration_Node
(Gen_Unit
);
3849 Gen_Spec
:= Specification
(Gen_Decl
);
3851 -- Initialize renamings map, for error checking, and the list that
3852 -- holds private entities whose views have changed between generic
3853 -- definition and instantiation. If this is the instance created to
3854 -- validate an actual package, the instantiation environment is that
3855 -- of the enclosing instance.
3857 Create_Instantiation_Source
(N
, Gen_Unit
, S_Adjustment
);
3859 -- Copy original generic tree, to produce text for instantiation
3863 (Original_Node
(Gen_Decl
), Empty
, Instantiating
=> True);
3865 Act_Spec
:= Specification
(Act_Tree
);
3867 -- If this is the instance created to validate an actual package,
3868 -- only the formals matter, do not examine the package spec itself.
3870 if Is_Actual_Pack
then
3871 Set_Visible_Declarations
(Act_Spec
, New_List
);
3872 Set_Private_Declarations
(Act_Spec
, New_List
);
3876 Analyze_Associations
3878 Formals
=> Generic_Formal_Declarations
(Act_Tree
),
3879 F_Copy
=> Generic_Formal_Declarations
(Gen_Decl
));
3881 Vis_Prims_List
:= Check_Hidden_Primitives
(Renaming_List
);
3883 Set_Instance_Env
(Gen_Unit
, Act_Decl_Id
);
3884 Set_Defining_Unit_Name
(Act_Spec
, Act_Decl_Name
);
3885 Set_Is_Generic_Instance
(Act_Decl_Id
);
3886 Set_Generic_Parent
(Act_Spec
, Gen_Unit
);
3888 -- References to the generic in its own declaration or its body are
3889 -- references to the instance. Add a renaming declaration for the
3890 -- generic unit itself. This declaration, as well as the renaming
3891 -- declarations for the generic formals, must remain private to the
3892 -- unit: the formals, because this is the language semantics, and
3893 -- the unit because its use is an artifact of the implementation.
3896 Make_Package_Renaming_Declaration
(Loc
,
3897 Defining_Unit_Name
=>
3898 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
)),
3899 Name
=> New_Occurrence_Of
(Act_Decl_Id
, Loc
));
3901 Append
(Unit_Renaming
, Renaming_List
);
3903 -- The renaming declarations are the first local declarations of the
3906 if Is_Non_Empty_List
(Visible_Declarations
(Act_Spec
)) then
3908 (First
(Visible_Declarations
(Act_Spec
)), Renaming_List
);
3910 Set_Visible_Declarations
(Act_Spec
, Renaming_List
);
3913 Act_Decl
:= Make_Package_Declaration
(Loc
, Specification
=> Act_Spec
);
3915 -- Propagate the aspect specifications from the package declaration
3916 -- template to the instantiated version of the package declaration.
3918 if Has_Aspects
(Act_Tree
) then
3919 Set_Aspect_Specifications
(Act_Decl
,
3920 New_Copy_List_Tree
(Aspect_Specifications
(Act_Tree
)));
3923 -- The generic may have a generated Default_Storage_Pool aspect,
3924 -- set at the point of generic declaration. If the instance has
3925 -- that aspect, it overrides the one inherited from the generic.
3927 if Has_Aspects
(Gen_Spec
) then
3928 if No
(Aspect_Specifications
(N
)) then
3929 Set_Aspect_Specifications
(N
,
3931 (Aspect_Specifications
(Gen_Spec
))));
3935 ASN1
, ASN2
: Node_Id
;
3938 ASN1
:= First
(Aspect_Specifications
(N
));
3939 while Present
(ASN1
) loop
3940 if Chars
(Identifier
(ASN1
)) = Name_Default_Storage_Pool
3942 -- If generic carries a default storage pool, remove
3943 -- it in favor of the instance one.
3945 ASN2
:= First
(Aspect_Specifications
(Gen_Spec
));
3946 while Present
(ASN2
) loop
3947 if Chars
(Identifier
(ASN2
)) =
3948 Name_Default_Storage_Pool
3961 Prepend_List_To
(Aspect_Specifications
(N
),
3963 (Aspect_Specifications
(Gen_Spec
))));
3968 -- Save the instantiation node, for subsequent instantiation of the
3969 -- body, if there is one and we are generating code for the current
3970 -- unit. Mark unit as having a body (avoids premature error message).
3972 -- We instantiate the body if we are generating code, if we are
3973 -- generating cross-reference information, or if we are building
3974 -- trees for ASIS use or GNATprove use.
3977 Enclosing_Body_Present
: Boolean := False;
3978 -- If the generic unit is not a compilation unit, then a body may
3979 -- be present in its parent even if none is required. We create a
3980 -- tentative pending instantiation for the body, which will be
3981 -- discarded if none is actually present.
3986 if Scope
(Gen_Unit
) /= Standard_Standard
3987 and then not Is_Child_Unit
(Gen_Unit
)
3989 Scop
:= Scope
(Gen_Unit
);
3990 while Present
(Scop
) and then Scop
/= Standard_Standard
loop
3991 if Unit_Requires_Body
(Scop
) then
3992 Enclosing_Body_Present
:= True;
3995 elsif In_Open_Scopes
(Scop
)
3996 and then In_Package_Body
(Scop
)
3998 Enclosing_Body_Present
:= True;
4002 exit when Is_Compilation_Unit
(Scop
);
4003 Scop
:= Scope
(Scop
);
4007 -- If front-end inlining is enabled or there are any subprograms
4008 -- marked with Inline_Always, and this is a unit for which code
4009 -- will be generated, we instantiate the body at once.
4011 -- This is done if the instance is not the main unit, and if the
4012 -- generic is not a child unit of another generic, to avoid scope
4013 -- problems and the reinstallation of parent instances.
4016 and then (not Is_Child_Unit
(Gen_Unit
)
4017 or else not Is_Generic_Unit
(Scope
(Gen_Unit
)))
4018 and then Might_Inline_Subp
4019 and then not Is_Actual_Pack
4021 if not Back_End_Inlining
4022 and then (Front_End_Inlining
or else Has_Inline_Always
)
4023 and then (Is_In_Main_Unit
(N
)
4024 or else In_Main_Context
(Current_Scope
))
4025 and then Nkind
(Parent
(N
)) /= N_Compilation_Unit
4029 -- In configurable_run_time mode we force the inlining of
4030 -- predefined subprograms marked Inline_Always, to minimize
4031 -- the use of the run-time library.
4033 elsif Is_Predefined_File_Name
4034 (Unit_File_Name
(Get_Source_Unit
(Gen_Decl
)))
4035 and then Configurable_Run_Time_Mode
4036 and then Nkind
(Parent
(N
)) /= N_Compilation_Unit
4041 -- If the current scope is itself an instance within a child
4042 -- unit, there will be duplications in the scope stack, and the
4043 -- unstacking mechanism in Inline_Instance_Body will fail.
4044 -- This loses some rare cases of optimization, and might be
4045 -- improved some day, if we can find a proper abstraction for
4046 -- "the complete compilation context" that can be saved and
4049 if Is_Generic_Instance
(Current_Scope
) then
4051 Curr_Unit
: constant Entity_Id
:=
4052 Cunit_Entity
(Current_Sem_Unit
);
4054 if Curr_Unit
/= Current_Scope
4055 and then Is_Child_Unit
(Curr_Unit
)
4057 Inline_Now
:= False;
4064 (Unit_Requires_Body
(Gen_Unit
)
4065 or else Enclosing_Body_Present
4066 or else Present
(Corresponding_Body
(Gen_Decl
)))
4067 and then (Is_In_Main_Unit
(N
) or else Might_Inline_Subp
)
4068 and then not Is_Actual_Pack
4069 and then not Inline_Now
4070 and then (Operating_Mode
= Generate_Code
4072 -- Need comment for this check ???
4074 or else (Operating_Mode
= Check_Semantics
4075 and then (ASIS_Mode
or GNATprove_Mode
)));
4077 -- If front-end inlining is enabled or there are any subprograms
4078 -- marked with Inline_Always, do not instantiate body when within
4079 -- a generic context.
4081 if ((Front_End_Inlining
or else Has_Inline_Always
)
4082 and then not Expander_Active
)
4083 or else Is_Generic_Unit
(Cunit_Entity
(Main_Unit
))
4085 Needs_Body
:= False;
4088 -- If the current context is generic, and the package being
4089 -- instantiated is declared within a formal package, there is no
4090 -- body to instantiate until the enclosing generic is instantiated
4091 -- and there is an actual for the formal package. If the formal
4092 -- package has parameters, we build a regular package instance for
4093 -- it, that precedes the original formal package declaration.
4095 if In_Open_Scopes
(Scope
(Scope
(Gen_Unit
))) then
4097 Decl
: constant Node_Id
:=
4099 (Unit_Declaration_Node
(Scope
(Gen_Unit
)));
4101 if Nkind
(Decl
) = N_Formal_Package_Declaration
4102 or else (Nkind
(Decl
) = N_Package_Declaration
4103 and then Is_List_Member
(Decl
)
4104 and then Present
(Next
(Decl
))
4106 Nkind
(Next
(Decl
)) =
4107 N_Formal_Package_Declaration
)
4109 Needs_Body
:= False;
4115 -- For RCI unit calling stubs, we omit the instance body if the
4116 -- instance is the RCI library unit itself.
4118 -- However there is a special case for nested instances: in this case
4119 -- we do generate the instance body, as it might be required, e.g.
4120 -- because it provides stream attributes for some type used in the
4121 -- profile of a remote subprogram. This is consistent with 12.3(12),
4122 -- which indicates that the instance body occurs at the place of the
4123 -- instantiation, and thus is part of the RCI declaration, which is
4124 -- present on all client partitions (this is E.2.3(18)).
4126 -- Note that AI12-0002 may make it illegal at some point to have
4127 -- stream attributes defined in an RCI unit, in which case this
4128 -- special case will become unnecessary. In the meantime, there
4129 -- is known application code in production that depends on this
4130 -- being possible, so we definitely cannot eliminate the body in
4131 -- the case of nested instances for the time being.
4133 -- When we generate a nested instance body, calling stubs for any
4134 -- relevant subprogram will be be inserted immediately after the
4135 -- subprogram declarations, and will take precedence over the
4136 -- subsequent (original) body. (The stub and original body will be
4137 -- complete homographs, but this is permitted in an instance).
4138 -- (Could we do better and remove the original body???)
4140 if Distribution_Stub_Mode
= Generate_Caller_Stub_Body
4141 and then Comes_From_Source
(N
)
4142 and then Nkind
(Parent
(N
)) = N_Compilation_Unit
4144 Needs_Body
:= False;
4149 -- Here is a defence against a ludicrous number of instantiations
4150 -- caused by a circular set of instantiation attempts.
4152 if Pending_Instantiations
.Last
> Maximum_Instantiations
then
4153 Error_Msg_Uint_1
:= UI_From_Int
(Maximum_Instantiations
);
4154 Error_Msg_N
("too many instantiations, exceeds max of^", N
);
4155 Error_Msg_N
("\limit can be changed using -gnateinn switch", N
);
4156 raise Unrecoverable_Error
;
4159 -- Indicate that the enclosing scopes contain an instantiation,
4160 -- and that cleanup actions should be delayed until after the
4161 -- instance body is expanded.
4163 Check_Forward_Instantiation
(Gen_Decl
);
4164 if Nkind
(N
) = N_Package_Instantiation
then
4166 Enclosing_Master
: Entity_Id
;
4169 -- Loop to search enclosing masters
4171 Enclosing_Master
:= Current_Scope
;
4172 Scope_Loop
: while Enclosing_Master
/= Standard_Standard
loop
4173 if Ekind
(Enclosing_Master
) = E_Package
then
4174 if Is_Compilation_Unit
(Enclosing_Master
) then
4175 if In_Package_Body
(Enclosing_Master
) then
4177 (Body_Entity
(Enclosing_Master
));
4186 Enclosing_Master
:= Scope
(Enclosing_Master
);
4189 elsif Is_Generic_Unit
(Enclosing_Master
)
4190 or else Ekind
(Enclosing_Master
) = E_Void
4192 -- Cleanup actions will eventually be performed on the
4193 -- enclosing subprogram or package instance, if any.
4194 -- Enclosing scope is void in the formal part of a
4195 -- generic subprogram.
4200 if Ekind
(Enclosing_Master
) = E_Entry
4202 Ekind
(Scope
(Enclosing_Master
)) = E_Protected_Type
4204 if not Expander_Active
then
4208 Protected_Body_Subprogram
(Enclosing_Master
);
4212 Set_Delay_Cleanups
(Enclosing_Master
);
4214 while Ekind
(Enclosing_Master
) = E_Block
loop
4215 Enclosing_Master
:= Scope
(Enclosing_Master
);
4218 if Is_Subprogram
(Enclosing_Master
) then
4219 Delay_Descriptors
(Enclosing_Master
);
4221 elsif Is_Task_Type
(Enclosing_Master
) then
4223 TBP
: constant Node_Id
:=
4224 Get_Task_Body_Procedure
4227 if Present
(TBP
) then
4228 Delay_Descriptors
(TBP
);
4229 Set_Delay_Cleanups
(TBP
);
4236 end loop Scope_Loop
;
4239 -- Make entry in table
4241 Add_Pending_Instantiation
(N
, Act_Decl
);
4245 Set_Categorization_From_Pragmas
(Act_Decl
);
4247 if Parent_Installed
then
4251 Set_Instance_Spec
(N
, Act_Decl
);
4253 -- If not a compilation unit, insert the package declaration before
4254 -- the original instantiation node.
4256 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
4257 Mark_Rewrite_Insertion
(Act_Decl
);
4258 Insert_Before
(N
, Act_Decl
);
4260 if Has_Aspects
(N
) then
4261 Analyze_Aspect_Specifications
(N
, Act_Decl_Id
);
4263 -- The pragma created for a Default_Storage_Pool aspect must
4264 -- appear ahead of the declarations in the instance spec.
4265 -- Analysis has placed it after the instance node, so remove
4266 -- it and reinsert it properly now.
4269 ASN
: constant Node_Id
:= First
(Aspect_Specifications
(N
));
4270 A_Name
: constant Name_Id
:= Chars
(Identifier
(ASN
));
4274 if A_Name
= Name_Default_Storage_Pool
then
4275 if No
(Visible_Declarations
(Act_Spec
)) then
4276 Set_Visible_Declarations
(Act_Spec
, New_List
);
4280 while Present
(Decl
) loop
4281 if Nkind
(Decl
) = N_Pragma
then
4283 Prepend
(Decl
, Visible_Declarations
(Act_Spec
));
4295 -- For an instantiation that is a compilation unit, place
4296 -- declaration on current node so context is complete for analysis
4297 -- (including nested instantiations). If this is the main unit,
4298 -- the declaration eventually replaces the instantiation node.
4299 -- If the instance body is created later, it replaces the
4300 -- instance node, and the declaration is attached to it
4301 -- (see Build_Instance_Compilation_Unit_Nodes).
4304 if Cunit_Entity
(Current_Sem_Unit
) = Defining_Entity
(N
) then
4306 -- The entity for the current unit is the newly created one,
4307 -- and all semantic information is attached to it.
4309 Set_Cunit_Entity
(Current_Sem_Unit
, Act_Decl_Id
);
4311 -- If this is the main unit, replace the main entity as well
4313 if Current_Sem_Unit
= Main_Unit
then
4314 Main_Unit_Entity
:= Act_Decl_Id
;
4318 Set_Unit
(Parent
(N
), Act_Decl
);
4319 Set_Parent_Spec
(Act_Decl
, Parent_Spec
(N
));
4320 Set_Package_Instantiation
(Act_Decl_Id
, N
);
4322 -- Process aspect specifications of the instance node, if any, to
4323 -- take into account categorization pragmas before analyzing the
4326 if Has_Aspects
(N
) then
4327 Analyze_Aspect_Specifications
(N
, Act_Decl_Id
);
4331 Set_Unit
(Parent
(N
), N
);
4332 Set_Body_Required
(Parent
(N
), False);
4334 -- We never need elaboration checks on instantiations, since by
4335 -- definition, the body instantiation is elaborated at the same
4336 -- time as the spec instantiation.
4338 Set_Suppress_Elaboration_Warnings
(Act_Decl_Id
);
4339 Set_Kill_Elaboration_Checks
(Act_Decl_Id
);
4342 Check_Elab_Instantiation
(N
);
4344 if ABE_Is_Certain
(N
) and then Needs_Body
then
4345 Pending_Instantiations
.Decrement_Last
;
4348 Check_Hidden_Child_Unit
(N
, Gen_Unit
, Act_Decl_Id
);
4350 Set_First_Private_Entity
(Defining_Unit_Name
(Unit_Renaming
),
4351 First_Private_Entity
(Act_Decl_Id
));
4353 -- If the instantiation will receive a body, the unit will be
4354 -- transformed into a package body, and receive its own elaboration
4355 -- entity. Otherwise, the nature of the unit is now a package
4358 if Nkind
(Parent
(N
)) = N_Compilation_Unit
4359 and then not Needs_Body
4361 Rewrite
(N
, Act_Decl
);
4364 if Present
(Corresponding_Body
(Gen_Decl
))
4365 or else Unit_Requires_Body
(Gen_Unit
)
4367 Set_Has_Completion
(Act_Decl_Id
);
4370 Check_Formal_Packages
(Act_Decl_Id
);
4372 Restore_Hidden_Primitives
(Vis_Prims_List
);
4373 Restore_Private_Views
(Act_Decl_Id
);
4375 Inherit_Context
(Gen_Decl
, N
);
4377 if Parent_Installed
then
4382 Env_Installed
:= False;
4385 Validate_Categorization_Dependency
(N
, Act_Decl_Id
);
4387 -- There used to be a check here to prevent instantiations in local
4388 -- contexts if the No_Local_Allocators restriction was active. This
4389 -- check was removed by a binding interpretation in AI-95-00130/07,
4390 -- but we retain the code for documentation purposes.
4392 -- if Ekind (Act_Decl_Id) /= E_Void
4393 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4395 -- Check_Restriction (No_Local_Allocators, N);
4399 Inline_Instance_Body
(N
, Gen_Unit
, Act_Decl
);
4402 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4403 -- be used as defining identifiers for a formal package and for the
4404 -- corresponding expanded package.
4406 if Nkind
(N
) = N_Formal_Package_Declaration
then
4407 Act_Decl_Id
:= New_Copy
(Defining_Entity
(N
));
4408 Set_Comes_From_Source
(Act_Decl_Id
, True);
4409 Set_Is_Generic_Instance
(Act_Decl_Id
, False);
4410 Set_Defining_Identifier
(N
, Act_Decl_Id
);
4413 Ignore_Pragma_SPARK_Mode
:= Save_IPSM
;
4414 SPARK_Mode
:= Save_SM
;
4415 SPARK_Mode_Pragma
:= Save_SMP
;
4416 Style_Check
:= Save_Style_Check
;
4418 -- Check that if N is an instantiation of System.Dim_Float_IO or
4419 -- System.Dim_Integer_IO, the formal type has a dimension system.
4421 if Nkind
(N
) = N_Package_Instantiation
4422 and then Is_Dim_IO_Package_Instantiation
(N
)
4425 Assoc
: constant Node_Id
:= First
(Generic_Associations
(N
));
4427 if not Has_Dimension_System
4428 (Etype
(Explicit_Generic_Actual_Parameter
(Assoc
)))
4430 Error_Msg_N
("type with a dimension system expected", Assoc
);
4436 if Has_Aspects
(N
) and then Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
4437 Analyze_Aspect_Specifications
(N
, Act_Decl_Id
);
4441 when Instantiation_Error
=>
4442 if Parent_Installed
then
4446 if Env_Installed
then
4450 Ignore_Pragma_SPARK_Mode
:= Save_IPSM
;
4451 SPARK_Mode
:= Save_SM
;
4452 SPARK_Mode_Pragma
:= Save_SMP
;
4453 Style_Check
:= Save_Style_Check
;
4454 end Analyze_Package_Instantiation
;
4456 --------------------------
4457 -- Inline_Instance_Body --
4458 --------------------------
4460 procedure Inline_Instance_Body
4462 Gen_Unit
: Entity_Id
;
4465 Curr_Comp
: constant Node_Id
:= Cunit
(Current_Sem_Unit
);
4466 Curr_Unit
: constant Entity_Id
:= Cunit_Entity
(Current_Sem_Unit
);
4467 Gen_Comp
: constant Entity_Id
:=
4468 Cunit_Entity
(Get_Source_Unit
(Gen_Unit
));
4470 Save_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
4471 Save_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
4472 -- Save all SPARK_Mode-related attributes as removing enclosing scopes
4473 -- to provide a clean environment for analysis of the inlined body will
4474 -- eliminate any previously set SPARK_Mode.
4476 Scope_Stack_Depth
: constant Pos
:=
4477 Scope_Stack
.Last
- Scope_Stack
.First
+ 1;
4479 Use_Clauses
: array (1 .. Scope_Stack_Depth
) of Node_Id
;
4480 Instances
: array (1 .. Scope_Stack_Depth
) of Entity_Id
;
4481 Inner_Scopes
: array (1 .. Scope_Stack_Depth
) of Entity_Id
;
4482 Curr_Scope
: Entity_Id
:= Empty
;
4484 Num_Inner
: Nat
:= 0;
4485 Num_Scopes
: Nat
:= 0;
4486 N_Instances
: Nat
:= 0;
4487 Removed
: Boolean := False;
4492 -- Case of generic unit defined in another unit. We must remove the
4493 -- complete context of the current unit to install that of the generic.
4495 if Gen_Comp
/= Cunit_Entity
(Current_Sem_Unit
) then
4497 -- Add some comments for the following two loops ???
4500 while Present
(S
) and then S
/= Standard_Standard
loop
4502 Num_Scopes
:= Num_Scopes
+ 1;
4504 Use_Clauses
(Num_Scopes
) :=
4506 (Scope_Stack
.Last
- Num_Scopes
+ 1).
4508 End_Use_Clauses
(Use_Clauses
(Num_Scopes
));
4510 exit when Scope_Stack
.Last
- Num_Scopes
+ 1 = Scope_Stack
.First
4511 or else Scope_Stack
.Table
4512 (Scope_Stack
.Last
- Num_Scopes
).Entity
= Scope
(S
);
4515 exit when Is_Generic_Instance
(S
)
4516 and then (In_Package_Body
(S
)
4517 or else Ekind
(S
) = E_Procedure
4518 or else Ekind
(S
) = E_Function
);
4522 Vis
:= Is_Immediately_Visible
(Gen_Comp
);
4524 -- Find and save all enclosing instances
4529 and then S
/= Standard_Standard
4531 if Is_Generic_Instance
(S
) then
4532 N_Instances
:= N_Instances
+ 1;
4533 Instances
(N_Instances
) := S
;
4535 exit when In_Package_Body
(S
);
4541 -- Remove context of current compilation unit, unless we are within a
4542 -- nested package instantiation, in which case the context has been
4543 -- removed previously.
4545 -- If current scope is the body of a child unit, remove context of
4546 -- spec as well. If an enclosing scope is an instance body, the
4547 -- context has already been removed, but the entities in the body
4548 -- must be made invisible as well.
4551 while Present
(S
) and then S
/= Standard_Standard
loop
4552 if Is_Generic_Instance
(S
)
4553 and then (In_Package_Body
(S
)
4554 or else Ekind_In
(S
, E_Procedure
, E_Function
))
4556 -- We still have to remove the entities of the enclosing
4557 -- instance from direct visibility.
4562 E
:= First_Entity
(S
);
4563 while Present
(E
) loop
4564 Set_Is_Immediately_Visible
(E
, False);
4573 or else (Ekind
(Curr_Unit
) = E_Package_Body
4574 and then S
= Spec_Entity
(Curr_Unit
))
4575 or else (Ekind
(Curr_Unit
) = E_Subprogram_Body
4576 and then S
= Corresponding_Spec
4577 (Unit_Declaration_Node
(Curr_Unit
)))
4581 -- Remove entities in current scopes from visibility, so that
4582 -- instance body is compiled in a clean environment.
4584 List
:= Save_Scope_Stack
(Handle_Use
=> False);
4586 if Is_Child_Unit
(S
) then
4588 -- Remove child unit from stack, as well as inner scopes.
4589 -- Removing the context of a child unit removes parent units
4592 while Current_Scope
/= S
loop
4593 Num_Inner
:= Num_Inner
+ 1;
4594 Inner_Scopes
(Num_Inner
) := Current_Scope
;
4599 Remove_Context
(Curr_Comp
);
4603 Remove_Context
(Curr_Comp
);
4606 if Ekind
(Curr_Unit
) = E_Package_Body
then
4607 Remove_Context
(Library_Unit
(Curr_Comp
));
4614 pragma Assert
(Num_Inner
< Num_Scopes
);
4616 -- The inlined package body must be analyzed with the SPARK_Mode of
4617 -- the enclosing context, otherwise the body may cause bogus errors
4618 -- if a configuration SPARK_Mode pragma in in effect.
4620 Push_Scope
(Standard_Standard
);
4621 Scope_Stack
.Table
(Scope_Stack
.Last
).Is_Active_Stack_Base
:= True;
4622 Instantiate_Package_Body
4625 Act_Decl
=> Act_Decl
,
4626 Expander_Status
=> Expander_Active
,
4627 Current_Sem_Unit
=> Current_Sem_Unit
,
4628 Scope_Suppress
=> Scope_Suppress
,
4629 Local_Suppress_Stack_Top
=> Local_Suppress_Stack_Top
,
4630 Version
=> Ada_Version
,
4631 Version_Pragma
=> Ada_Version_Pragma
,
4632 Warnings
=> Save_Warnings
,
4633 SPARK_Mode
=> Save_SM
,
4634 SPARK_Mode_Pragma
=> Save_SMP
)),
4635 Inlined_Body
=> True);
4641 Set_Is_Immediately_Visible
(Gen_Comp
, Vis
);
4643 -- Reset Generic_Instance flag so that use clauses can be installed
4644 -- in the proper order. (See Use_One_Package for effect of enclosing
4645 -- instances on processing of use clauses).
4647 for J
in 1 .. N_Instances
loop
4648 Set_Is_Generic_Instance
(Instances
(J
), False);
4652 Install_Context
(Curr_Comp
);
4654 if Present
(Curr_Scope
)
4655 and then Is_Child_Unit
(Curr_Scope
)
4657 Push_Scope
(Curr_Scope
);
4658 Set_Is_Immediately_Visible
(Curr_Scope
);
4660 -- Finally, restore inner scopes as well
4662 for J
in reverse 1 .. Num_Inner
loop
4663 Push_Scope
(Inner_Scopes
(J
));
4667 Restore_Scope_Stack
(List
, Handle_Use
=> False);
4669 if Present
(Curr_Scope
)
4671 (In_Private_Part
(Curr_Scope
)
4672 or else In_Package_Body
(Curr_Scope
))
4674 -- Install private declaration of ancestor units, which are
4675 -- currently available. Restore_Scope_Stack and Install_Context
4676 -- only install the visible part of parents.
4681 Par
:= Scope
(Curr_Scope
);
4682 while (Present
(Par
)) and then Par
/= Standard_Standard
loop
4683 Install_Private_Declarations
(Par
);
4690 -- Restore use clauses. For a child unit, use clauses in the parents
4691 -- are restored when installing the context, so only those in inner
4692 -- scopes (and those local to the child unit itself) need to be
4693 -- installed explicitly.
4695 if Is_Child_Unit
(Curr_Unit
) and then Removed
then
4696 for J
in reverse 1 .. Num_Inner
+ 1 loop
4697 Scope_Stack
.Table
(Scope_Stack
.Last
- J
+ 1).First_Use_Clause
:=
4699 Install_Use_Clauses
(Use_Clauses
(J
));
4703 for J
in reverse 1 .. Num_Scopes
loop
4704 Scope_Stack
.Table
(Scope_Stack
.Last
- J
+ 1).First_Use_Clause
:=
4706 Install_Use_Clauses
(Use_Clauses
(J
));
4710 -- Restore status of instances. If one of them is a body, make its
4711 -- local entities visible again.
4718 for J
in 1 .. N_Instances
loop
4719 Inst
:= Instances
(J
);
4720 Set_Is_Generic_Instance
(Inst
, True);
4722 if In_Package_Body
(Inst
)
4723 or else Ekind_In
(S
, E_Procedure
, E_Function
)
4725 E
:= First_Entity
(Instances
(J
));
4726 while Present
(E
) loop
4727 Set_Is_Immediately_Visible
(E
);
4734 -- If generic unit is in current unit, current context is correct. Note
4735 -- that the context is guaranteed to carry the correct SPARK_Mode as no
4736 -- enclosing scopes were removed.
4739 Instantiate_Package_Body
4742 Act_Decl
=> Act_Decl
,
4743 Expander_Status
=> Expander_Active
,
4744 Current_Sem_Unit
=> Current_Sem_Unit
,
4745 Scope_Suppress
=> Scope_Suppress
,
4746 Local_Suppress_Stack_Top
=> Local_Suppress_Stack_Top
,
4747 Version
=> Ada_Version
,
4748 Version_Pragma
=> Ada_Version_Pragma
,
4749 Warnings
=> Save_Warnings
,
4750 SPARK_Mode
=> SPARK_Mode
,
4751 SPARK_Mode_Pragma
=> SPARK_Mode_Pragma
)),
4752 Inlined_Body
=> True);
4754 end Inline_Instance_Body
;
4756 -------------------------------------
4757 -- Analyze_Procedure_Instantiation --
4758 -------------------------------------
4760 procedure Analyze_Procedure_Instantiation
(N
: Node_Id
) is
4762 Analyze_Subprogram_Instantiation
(N
, E_Procedure
);
4763 end Analyze_Procedure_Instantiation
;
4765 -----------------------------------
4766 -- Need_Subprogram_Instance_Body --
4767 -----------------------------------
4769 function Need_Subprogram_Instance_Body
4771 Subp
: Entity_Id
) return Boolean
4774 function Is_Inlined_Or_Child_Of_Inlined
(E
: Entity_Id
) return Boolean;
4775 -- Return True if E is an inlined subprogram, an inlined renaming or a
4776 -- subprogram nested in an inlined subprogram. The inlining machinery
4777 -- totally disregards nested subprograms since it considers that they
4778 -- will always be compiled if the parent is (see Inline.Is_Nested).
4780 ------------------------------------
4781 -- Is_Inlined_Or_Child_Of_Inlined --
4782 ------------------------------------
4784 function Is_Inlined_Or_Child_Of_Inlined
(E
: Entity_Id
) return Boolean is
4788 if Is_Inlined
(E
) or else Is_Inlined
(Alias
(E
)) then
4793 while Scop
/= Standard_Standard
loop
4794 if Ekind
(Scop
) in Subprogram_Kind
and then Is_Inlined
(Scop
) then
4798 Scop
:= Scope
(Scop
);
4802 end Is_Inlined_Or_Child_Of_Inlined
;
4805 -- Must be in the main unit or inlined (or child of inlined)
4807 if (Is_In_Main_Unit
(N
) or else Is_Inlined_Or_Child_Of_Inlined
(Subp
))
4809 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4811 and then (Operating_Mode
= Generate_Code
4812 or else (Operating_Mode
= Check_Semantics
4813 and then (ASIS_Mode
or GNATprove_Mode
)))
4815 -- The body is needed when generating code (full expansion), in ASIS
4816 -- mode for other tools, and in GNATprove mode (special expansion) for
4817 -- formal verification of the body itself.
4819 and then (Expander_Active
or ASIS_Mode
or GNATprove_Mode
)
4821 -- No point in inlining if ABE is inevitable
4823 and then not ABE_Is_Certain
(N
)
4825 -- Or if subprogram is eliminated
4827 and then not Is_Eliminated
(Subp
)
4829 Add_Pending_Instantiation
(N
, Unit_Declaration_Node
(Subp
));
4832 -- Here if not inlined, or we ignore the inlining
4837 end Need_Subprogram_Instance_Body
;
4839 --------------------------------------
4840 -- Analyze_Subprogram_Instantiation --
4841 --------------------------------------
4843 procedure Analyze_Subprogram_Instantiation
4847 Loc
: constant Source_Ptr
:= Sloc
(N
);
4848 Gen_Id
: constant Node_Id
:= Name
(N
);
4850 Anon_Id
: constant Entity_Id
:=
4851 Make_Defining_Identifier
(Sloc
(Defining_Entity
(N
)),
4852 Chars
=> New_External_Name
4853 (Chars
(Defining_Entity
(N
)), 'R'));
4855 Act_Decl_Id
: Entity_Id
;
4860 Env_Installed
: Boolean := False;
4861 Gen_Unit
: Entity_Id
;
4863 Pack_Id
: Entity_Id
;
4864 Parent_Installed
: Boolean := False;
4866 Renaming_List
: List_Id
;
4867 -- The list of declarations that link formals and actuals of the
4868 -- instance. These are subtype declarations for formal types, and
4869 -- renaming declarations for other formals. The subprogram declaration
4870 -- for the instance is then appended to the list, and the last item on
4871 -- the list is the renaming declaration for the instance.
4873 procedure Analyze_Instance_And_Renamings
;
4874 -- The instance must be analyzed in a context that includes the mappings
4875 -- of generic parameters into actuals. We create a package declaration
4876 -- for this purpose, and a subprogram with an internal name within the
4877 -- package. The subprogram instance is simply an alias for the internal
4878 -- subprogram, declared in the current scope.
4880 procedure Build_Subprogram_Renaming
;
4881 -- If the subprogram is recursive, there are occurrences of the name of
4882 -- the generic within the body, which must resolve to the current
4883 -- instance. We add a renaming declaration after the declaration, which
4884 -- is available in the instance body, as well as in the analysis of
4885 -- aspects that appear in the generic. This renaming declaration is
4886 -- inserted after the instance declaration which it renames.
4888 ------------------------------------
4889 -- Analyze_Instance_And_Renamings --
4890 ------------------------------------
4892 procedure Analyze_Instance_And_Renamings
is
4893 Def_Ent
: constant Entity_Id
:= Defining_Entity
(N
);
4894 Pack_Decl
: Node_Id
;
4897 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
4899 -- For the case of a compilation unit, the container package has
4900 -- the same name as the instantiation, to insure that the binder
4901 -- calls the elaboration procedure with the right name. Copy the
4902 -- entity of the instance, which may have compilation level flags
4903 -- (e.g. Is_Child_Unit) set.
4905 Pack_Id
:= New_Copy
(Def_Ent
);
4908 -- Otherwise we use the name of the instantiation concatenated
4909 -- with its source position to ensure uniqueness if there are
4910 -- several instantiations with the same name.
4913 Make_Defining_Identifier
(Loc
,
4914 Chars
=> New_External_Name
4915 (Related_Id
=> Chars
(Def_Ent
),
4917 Suffix_Index
=> Source_Offset
(Sloc
(Def_Ent
))));
4921 Make_Package_Declaration
(Loc
,
4922 Specification
=> Make_Package_Specification
(Loc
,
4923 Defining_Unit_Name
=> Pack_Id
,
4924 Visible_Declarations
=> Renaming_List
,
4925 End_Label
=> Empty
));
4927 Set_Instance_Spec
(N
, Pack_Decl
);
4928 Set_Is_Generic_Instance
(Pack_Id
);
4929 Set_Debug_Info_Needed
(Pack_Id
);
4931 -- Case of not a compilation unit
4933 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
4934 Mark_Rewrite_Insertion
(Pack_Decl
);
4935 Insert_Before
(N
, Pack_Decl
);
4936 Set_Has_Completion
(Pack_Id
);
4938 -- Case of an instantiation that is a compilation unit
4940 -- Place declaration on current node so context is complete for
4941 -- analysis (including nested instantiations), and for use in a
4942 -- context_clause (see Analyze_With_Clause).
4945 Set_Unit
(Parent
(N
), Pack_Decl
);
4946 Set_Parent_Spec
(Pack_Decl
, Parent_Spec
(N
));
4949 Analyze
(Pack_Decl
);
4950 Check_Formal_Packages
(Pack_Id
);
4951 Set_Is_Generic_Instance
(Pack_Id
, False);
4953 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4956 -- Body of the enclosing package is supplied when instantiating the
4957 -- subprogram body, after semantic analysis is completed.
4959 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
4961 -- Remove package itself from visibility, so it does not
4962 -- conflict with subprogram.
4964 Set_Name_Entity_Id
(Chars
(Pack_Id
), Homonym
(Pack_Id
));
4966 -- Set name and scope of internal subprogram so that the proper
4967 -- external name will be generated. The proper scope is the scope
4968 -- of the wrapper package. We need to generate debugging info for
4969 -- the internal subprogram, so set flag accordingly.
4971 Set_Chars
(Anon_Id
, Chars
(Defining_Entity
(N
)));
4972 Set_Scope
(Anon_Id
, Scope
(Pack_Id
));
4974 -- Mark wrapper package as referenced, to avoid spurious warnings
4975 -- if the instantiation appears in various with_ clauses of
4976 -- subunits of the main unit.
4978 Set_Referenced
(Pack_Id
);
4981 Set_Is_Generic_Instance
(Anon_Id
);
4982 Set_Debug_Info_Needed
(Anon_Id
);
4983 Act_Decl_Id
:= New_Copy
(Anon_Id
);
4985 Set_Parent
(Act_Decl_Id
, Parent
(Anon_Id
));
4986 Set_Chars
(Act_Decl_Id
, Chars
(Defining_Entity
(N
)));
4987 Set_Sloc
(Act_Decl_Id
, Sloc
(Defining_Entity
(N
)));
4989 -- Subprogram instance comes from source only if generic does
4991 Set_Comes_From_Source
(Act_Decl_Id
, Comes_From_Source
(Gen_Unit
));
4993 -- If the instance is a child unit, mark the Id accordingly. Mark
4994 -- the anonymous entity as well, which is the real subprogram and
4995 -- which is used when the instance appears in a context clause.
4996 -- Similarly, propagate the Is_Eliminated flag to handle properly
4997 -- nested eliminated subprograms.
4999 Set_Is_Child_Unit
(Act_Decl_Id
, Is_Child_Unit
(Defining_Entity
(N
)));
5000 Set_Is_Child_Unit
(Anon_Id
, Is_Child_Unit
(Defining_Entity
(N
)));
5001 New_Overloaded_Entity
(Act_Decl_Id
);
5002 Check_Eliminated
(Act_Decl_Id
);
5003 Set_Is_Eliminated
(Anon_Id
, Is_Eliminated
(Act_Decl_Id
));
5005 -- In compilation unit case, kill elaboration checks on the
5006 -- instantiation, since they are never needed -- the body is
5007 -- instantiated at the same point as the spec.
5009 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5010 Set_Suppress_Elaboration_Warnings
(Act_Decl_Id
);
5011 Set_Kill_Elaboration_Checks
(Act_Decl_Id
);
5012 Set_Is_Compilation_Unit
(Anon_Id
);
5014 Set_Cunit_Entity
(Current_Sem_Unit
, Pack_Id
);
5017 -- The instance is not a freezing point for the new subprogram.
5018 -- The anonymous subprogram may have a freeze node, created for
5019 -- some delayed aspects. This freeze node must not be inherited
5020 -- by the visible subprogram entity.
5022 Set_Is_Frozen
(Act_Decl_Id
, False);
5023 Set_Freeze_Node
(Act_Decl_Id
, Empty
);
5025 if Nkind
(Defining_Entity
(N
)) = N_Defining_Operator_Symbol
then
5026 Valid_Operator_Definition
(Act_Decl_Id
);
5029 Set_Alias
(Act_Decl_Id
, Anon_Id
);
5030 Set_Parent
(Act_Decl_Id
, Parent
(Anon_Id
));
5031 Set_Has_Completion
(Act_Decl_Id
);
5032 Set_Related_Instance
(Pack_Id
, Act_Decl_Id
);
5034 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5035 Set_Body_Required
(Parent
(N
), False);
5037 end Analyze_Instance_And_Renamings
;
5039 -------------------------------
5040 -- Build_Subprogram_Renaming --
5041 -------------------------------
5043 procedure Build_Subprogram_Renaming
is
5044 Renaming_Decl
: Node_Id
;
5045 Unit_Renaming
: Node_Id
;
5049 Make_Subprogram_Renaming_Declaration
(Loc
,
5052 (Specification
(Original_Node
(Gen_Decl
)),
5054 Instantiating
=> True),
5055 Name
=> New_Occurrence_Of
(Anon_Id
, Loc
));
5057 -- The generic may be a a child unit. The renaming needs an
5058 -- identifier with the proper name.
5060 Set_Defining_Unit_Name
(Specification
(Unit_Renaming
),
5061 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
)));
5063 -- If there is a formal subprogram with the same name as the unit
5064 -- itself, do not add this renaming declaration, to prevent
5065 -- ambiguities when there is a call with that name in the body.
5066 -- This is a partial and ugly fix for one ACATS test. ???
5068 Renaming_Decl
:= First
(Renaming_List
);
5069 while Present
(Renaming_Decl
) loop
5070 if Nkind
(Renaming_Decl
) = N_Subprogram_Renaming_Declaration
5072 Chars
(Defining_Entity
(Renaming_Decl
)) = Chars
(Gen_Unit
)
5077 Next
(Renaming_Decl
);
5080 if No
(Renaming_Decl
) then
5081 Append
(Unit_Renaming
, Renaming_List
);
5083 end Build_Subprogram_Renaming
;
5087 Save_IPSM
: constant Boolean := Ignore_Pragma_SPARK_Mode
;
5088 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
5090 Save_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
5091 Save_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
5092 -- Save the SPARK_Mode-related data for restore on exit
5094 Vis_Prims_List
: Elist_Id
:= No_Elist
;
5095 -- List of primitives made temporarily visible in the instantiation
5096 -- to match the visibility of the formal type
5098 -- Start of processing for Analyze_Subprogram_Instantiation
5101 Check_SPARK_05_Restriction
("generic is not allowed", N
);
5103 -- Very first thing: check for special Text_IO unit in case we are
5104 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5105 -- such an instantiation is bogus (these are packages, not subprograms),
5106 -- but we get a better error message if we do this.
5108 Check_Text_IO_Special_Unit
(Gen_Id
);
5110 -- Make node global for error reporting
5112 Instantiation_Node
:= N
;
5114 -- For package instantiations we turn off style checks, because they
5115 -- will have been emitted in the generic. For subprogram instantiations
5116 -- we want to apply at least the check on overriding indicators so we
5117 -- do not modify the style check status.
5119 -- The renaming declarations for the actuals do not come from source and
5120 -- will not generate spurious warnings.
5122 Preanalyze_Actuals
(N
);
5125 Env_Installed
:= True;
5126 Check_Generic_Child_Unit
(Gen_Id
, Parent_Installed
);
5127 Gen_Unit
:= Entity
(Gen_Id
);
5129 Generate_Reference
(Gen_Unit
, Gen_Id
);
5131 if Nkind
(Gen_Id
) = N_Identifier
5132 and then Chars
(Gen_Unit
) = Chars
(Defining_Entity
(N
))
5135 ("& is hidden within declaration of instance", Gen_Id
, Gen_Unit
);
5138 if Etype
(Gen_Unit
) = Any_Type
then
5143 -- Verify that it is a generic subprogram of the right kind, and that
5144 -- it does not lead to a circular instantiation.
5146 if K
= E_Procedure
and then Ekind
(Gen_Unit
) /= E_Generic_Procedure
then
5148 ("& is not the name of a generic procedure", Gen_Id
, Gen_Unit
);
5150 elsif K
= E_Function
and then Ekind
(Gen_Unit
) /= E_Generic_Function
then
5152 ("& is not the name of a generic function", Gen_Id
, Gen_Unit
);
5154 elsif In_Open_Scopes
(Gen_Unit
) then
5155 Error_Msg_NE
("instantiation of & within itself", N
, Gen_Unit
);
5158 -- If the context of the instance is subject to SPARK_Mode "off" or
5159 -- the annotation is altogether missing, set the global flag which
5160 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5163 if SPARK_Mode
/= On
then
5164 Ignore_Pragma_SPARK_Mode
:= True;
5167 Set_Entity
(Gen_Id
, Gen_Unit
);
5168 Set_Is_Instantiated
(Gen_Unit
);
5170 if In_Extended_Main_Source_Unit
(N
) then
5171 Generate_Reference
(Gen_Unit
, N
);
5174 -- If renaming, get original unit
5176 if Present
(Renamed_Object
(Gen_Unit
))
5177 and then Ekind_In
(Renamed_Object
(Gen_Unit
), E_Generic_Procedure
,
5180 Gen_Unit
:= Renamed_Object
(Gen_Unit
);
5181 Set_Is_Instantiated
(Gen_Unit
);
5182 Generate_Reference
(Gen_Unit
, N
);
5185 if Contains_Instance_Of
(Gen_Unit
, Current_Scope
, Gen_Id
) then
5186 Error_Msg_Node_2
:= Current_Scope
;
5188 ("circular Instantiation: & instantiated in &!", N
, Gen_Unit
);
5189 Circularity_Detected
:= True;
5190 Restore_Hidden_Primitives
(Vis_Prims_List
);
5194 Gen_Decl
:= Unit_Declaration_Node
(Gen_Unit
);
5196 -- Initialize renamings map, for error checking
5198 Generic_Renamings
.Set_Last
(0);
5199 Generic_Renamings_HTable
.Reset
;
5201 Create_Instantiation_Source
(N
, Gen_Unit
, S_Adjustment
);
5203 -- Copy original generic tree, to produce text for instantiation
5207 (Original_Node
(Gen_Decl
), Empty
, Instantiating
=> True);
5209 -- Inherit overriding indicator from instance node
5211 Act_Spec
:= Specification
(Act_Tree
);
5212 Set_Must_Override
(Act_Spec
, Must_Override
(N
));
5213 Set_Must_Not_Override
(Act_Spec
, Must_Not_Override
(N
));
5216 Analyze_Associations
5218 Formals
=> Generic_Formal_Declarations
(Act_Tree
),
5219 F_Copy
=> Generic_Formal_Declarations
(Gen_Decl
));
5221 Vis_Prims_List
:= Check_Hidden_Primitives
(Renaming_List
);
5223 -- The subprogram itself cannot contain a nested instance, so the
5224 -- current parent is left empty.
5226 Set_Instance_Env
(Gen_Unit
, Empty
);
5228 -- Build the subprogram declaration, which does not appear in the
5229 -- generic template, and give it a sloc consistent with that of the
5232 Set_Defining_Unit_Name
(Act_Spec
, Anon_Id
);
5233 Set_Generic_Parent
(Act_Spec
, Gen_Unit
);
5235 Make_Subprogram_Declaration
(Sloc
(Act_Spec
),
5236 Specification
=> Act_Spec
);
5238 -- The aspects have been copied previously, but they have to be
5239 -- linked explicitly to the new subprogram declaration. Explicit
5240 -- pre/postconditions on the instance are analyzed below, in a
5243 Move_Aspects
(Act_Tree
, To
=> Act_Decl
);
5244 Set_Categorization_From_Pragmas
(Act_Decl
);
5246 if Parent_Installed
then
5250 Append
(Act_Decl
, Renaming_List
);
5252 -- Contract-related source pragmas that follow a generic subprogram
5253 -- must be instantiated explicitly because they are not part of the
5254 -- subprogram template.
5256 Instantiate_Subprogram_Contract
5257 (Original_Node
(Gen_Decl
), Renaming_List
);
5259 Build_Subprogram_Renaming
;
5260 Analyze_Instance_And_Renamings
;
5262 -- If the generic is marked Import (Intrinsic), then so is the
5263 -- instance. This indicates that there is no body to instantiate. If
5264 -- generic is marked inline, so it the instance, and the anonymous
5265 -- subprogram it renames. If inlined, or else if inlining is enabled
5266 -- for the compilation, we generate the instance body even if it is
5267 -- not within the main unit.
5269 if Is_Intrinsic_Subprogram
(Gen_Unit
) then
5270 Set_Is_Intrinsic_Subprogram
(Anon_Id
);
5271 Set_Is_Intrinsic_Subprogram
(Act_Decl_Id
);
5273 if Chars
(Gen_Unit
) = Name_Unchecked_Conversion
then
5274 Validate_Unchecked_Conversion
(N
, Act_Decl_Id
);
5278 -- Inherit convention from generic unit. Intrinsic convention, as for
5279 -- an instance of unchecked conversion, is not inherited because an
5280 -- explicit Ada instance has been created.
5282 if Has_Convention_Pragma
(Gen_Unit
)
5283 and then Convention
(Gen_Unit
) /= Convention_Intrinsic
5285 Set_Convention
(Act_Decl_Id
, Convention
(Gen_Unit
));
5286 Set_Is_Exported
(Act_Decl_Id
, Is_Exported
(Gen_Unit
));
5289 Generate_Definition
(Act_Decl_Id
);
5291 -- Inherit all inlining-related flags which apply to the generic in
5292 -- the subprogram and its declaration.
5294 Set_Is_Inlined
(Act_Decl_Id
, Is_Inlined
(Gen_Unit
));
5295 Set_Is_Inlined
(Anon_Id
, Is_Inlined
(Gen_Unit
));
5297 Set_Has_Pragma_Inline
(Act_Decl_Id
, Has_Pragma_Inline
(Gen_Unit
));
5298 Set_Has_Pragma_Inline
(Anon_Id
, Has_Pragma_Inline
(Gen_Unit
));
5300 Set_Has_Pragma_Inline_Always
5301 (Act_Decl_Id
, Has_Pragma_Inline_Always
(Gen_Unit
));
5302 Set_Has_Pragma_Inline_Always
5303 (Anon_Id
, Has_Pragma_Inline_Always
(Gen_Unit
));
5305 if not Is_Intrinsic_Subprogram
(Gen_Unit
) then
5306 Check_Elab_Instantiation
(N
);
5309 if Is_Dispatching_Operation
(Act_Decl_Id
)
5310 and then Ada_Version
>= Ada_2005
5316 Formal
:= First_Formal
(Act_Decl_Id
);
5317 while Present
(Formal
) loop
5318 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
5319 and then Is_Controlling_Formal
(Formal
)
5320 and then not Can_Never_Be_Null
(Formal
)
5323 ("access parameter& is controlling,", N
, Formal
);
5325 ("\corresponding parameter of & must be "
5326 & "explicitly null-excluding", N
, Gen_Id
);
5329 Next_Formal
(Formal
);
5334 Check_Hidden_Child_Unit
(N
, Gen_Unit
, Act_Decl_Id
);
5336 Validate_Categorization_Dependency
(N
, Act_Decl_Id
);
5338 if not Is_Intrinsic_Subprogram
(Act_Decl_Id
) then
5339 Inherit_Context
(Gen_Decl
, N
);
5341 Restore_Private_Views
(Pack_Id
, False);
5343 -- If the context requires a full instantiation, mark node for
5344 -- subsequent construction of the body.
5346 if Need_Subprogram_Instance_Body
(N
, Act_Decl_Id
) then
5347 Check_Forward_Instantiation
(Gen_Decl
);
5349 -- The wrapper package is always delayed, because it does not
5350 -- constitute a freeze point, but to insure that the freeze node
5351 -- is placed properly, it is created directly when instantiating
5352 -- the body (otherwise the freeze node might appear to early for
5353 -- nested instantiations). For ASIS purposes, indicate that the
5354 -- wrapper package has replaced the instantiation node.
5356 elsif Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5357 Rewrite
(N
, Unit
(Parent
(N
)));
5358 Set_Unit
(Parent
(N
), N
);
5361 -- Replace instance node for library-level instantiations of
5362 -- intrinsic subprograms, for ASIS use.
5364 elsif Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5365 Rewrite
(N
, Unit
(Parent
(N
)));
5366 Set_Unit
(Parent
(N
), N
);
5369 if Parent_Installed
then
5373 Restore_Hidden_Primitives
(Vis_Prims_List
);
5375 Env_Installed
:= False;
5376 Generic_Renamings
.Set_Last
(0);
5377 Generic_Renamings_HTable
.Reset
;
5379 Ignore_Pragma_SPARK_Mode
:= Save_IPSM
;
5380 SPARK_Mode
:= Save_SM
;
5381 SPARK_Mode_Pragma
:= Save_SMP
;
5385 if Has_Aspects
(N
) then
5386 Analyze_Aspect_Specifications
(N
, Act_Decl_Id
);
5390 when Instantiation_Error
=>
5391 if Parent_Installed
then
5395 if Env_Installed
then
5399 Ignore_Pragma_SPARK_Mode
:= Save_IPSM
;
5400 SPARK_Mode
:= Save_SM
;
5401 SPARK_Mode_Pragma
:= Save_SMP
;
5402 end Analyze_Subprogram_Instantiation
;
5404 -------------------------
5405 -- Get_Associated_Node --
5406 -------------------------
5408 function Get_Associated_Node
(N
: Node_Id
) return Node_Id
is
5412 Assoc
:= Associated_Node
(N
);
5414 if Nkind
(Assoc
) /= Nkind
(N
) then
5417 elsif Nkind_In
(Assoc
, N_Aggregate
, N_Extension_Aggregate
) then
5421 -- If the node is part of an inner generic, it may itself have been
5422 -- remapped into a further generic copy. Associated_Node is otherwise
5423 -- used for the entity of the node, and will be of a different node
5424 -- kind, or else N has been rewritten as a literal or function call.
5426 while Present
(Associated_Node
(Assoc
))
5427 and then Nkind
(Associated_Node
(Assoc
)) = Nkind
(Assoc
)
5429 Assoc
:= Associated_Node
(Assoc
);
5432 -- Follow and additional link in case the final node was rewritten.
5433 -- This can only happen with nested generic units.
5435 if (Nkind
(Assoc
) = N_Identifier
or else Nkind
(Assoc
) in N_Op
)
5436 and then Present
(Associated_Node
(Assoc
))
5437 and then (Nkind_In
(Associated_Node
(Assoc
), N_Function_Call
,
5438 N_Explicit_Dereference
,
5443 Assoc
:= Associated_Node
(Assoc
);
5446 -- An additional special case: an unconstrained type in an object
5447 -- declaration may have been rewritten as a local subtype constrained
5448 -- by the expression in the declaration. We need to recover the
5449 -- original entity which may be global.
5451 if Present
(Original_Node
(Assoc
))
5452 and then Nkind
(Parent
(N
)) = N_Object_Declaration
5454 Assoc
:= Original_Node
(Assoc
);
5459 end Get_Associated_Node
;
5461 ----------------------------
5462 -- Build_Function_Wrapper --
5463 ----------------------------
5465 function Build_Function_Wrapper
5466 (Formal_Subp
: Entity_Id
;
5467 Actual_Subp
: Entity_Id
) return Node_Id
5469 Loc
: constant Source_Ptr
:= Sloc
(Current_Scope
);
5470 Ret_Type
: constant Entity_Id
:= Get_Instance_Of
(Etype
(Formal_Subp
));
5473 Func_Name
: Node_Id
;
5475 Parm_Type
: Node_Id
;
5476 Profile
: List_Id
:= New_List
;
5483 Func_Name
:= New_Occurrence_Of
(Actual_Subp
, Loc
);
5485 Func
:= Make_Defining_Identifier
(Loc
, Chars
(Formal_Subp
));
5486 Set_Ekind
(Func
, E_Function
);
5487 Set_Is_Generic_Actual_Subprogram
(Func
);
5489 Actuals
:= New_List
;
5490 Profile
:= New_List
;
5492 Act_F
:= First_Formal
(Actual_Subp
);
5493 Form_F
:= First_Formal
(Formal_Subp
);
5494 while Present
(Form_F
) loop
5496 -- Create new formal for profile of wrapper, and add a reference
5497 -- to it in the list of actuals for the enclosing call. The name
5498 -- must be that of the formal in the formal subprogram, because
5499 -- calls to it in the generic body may use named associations.
5501 New_F
:= Make_Defining_Identifier
(Loc
, Chars
(Form_F
));
5504 New_Occurrence_Of
(Get_Instance_Of
(Etype
(Form_F
)), Loc
);
5507 Make_Parameter_Specification
(Loc
,
5508 Defining_Identifier
=> New_F
,
5509 Parameter_Type
=> Parm_Type
));
5511 Append_To
(Actuals
, New_Occurrence_Of
(New_F
, Loc
));
5512 Next_Formal
(Form_F
);
5514 if Present
(Act_F
) then
5515 Next_Formal
(Act_F
);
5520 Make_Function_Specification
(Loc
,
5521 Defining_Unit_Name
=> Func
,
5522 Parameter_Specifications
=> Profile
,
5523 Result_Definition
=> New_Occurrence_Of
(Ret_Type
, Loc
));
5526 Make_Expression_Function
(Loc
,
5527 Specification
=> Spec
,
5529 Make_Function_Call
(Loc
,
5531 Parameter_Associations
=> Actuals
));
5534 end Build_Function_Wrapper
;
5536 ----------------------------
5537 -- Build_Operator_Wrapper --
5538 ----------------------------
5540 function Build_Operator_Wrapper
5541 (Formal_Subp
: Entity_Id
;
5542 Actual_Subp
: Entity_Id
) return Node_Id
5544 Loc
: constant Source_Ptr
:= Sloc
(Current_Scope
);
5545 Ret_Type
: constant Entity_Id
:=
5546 Get_Instance_Of
(Etype
(Formal_Subp
));
5547 Op_Type
: constant Entity_Id
:=
5548 Get_Instance_Of
(Etype
(First_Formal
(Formal_Subp
)));
5549 Is_Binary
: constant Boolean :=
5550 Present
(Next_Formal
(First_Formal
(Formal_Subp
)));
5561 Op_Name
:= Chars
(Actual_Subp
);
5563 -- Create entities for wrapper function and its formals
5565 F1
:= Make_Temporary
(Loc
, 'A');
5566 F2
:= Make_Temporary
(Loc
, 'B');
5567 L
:= New_Occurrence_Of
(F1
, Loc
);
5568 R
:= New_Occurrence_Of
(F2
, Loc
);
5570 Func
:= Make_Defining_Identifier
(Loc
, Chars
(Formal_Subp
));
5571 Set_Ekind
(Func
, E_Function
);
5572 Set_Is_Generic_Actual_Subprogram
(Func
);
5575 Make_Function_Specification
(Loc
,
5576 Defining_Unit_Name
=> Func
,
5577 Parameter_Specifications
=> New_List
(
5578 Make_Parameter_Specification
(Loc
,
5579 Defining_Identifier
=> F1
,
5580 Parameter_Type
=> New_Occurrence_Of
(Op_Type
, Loc
))),
5581 Result_Definition
=> New_Occurrence_Of
(Ret_Type
, Loc
));
5584 Append_To
(Parameter_Specifications
(Spec
),
5585 Make_Parameter_Specification
(Loc
,
5586 Defining_Identifier
=> F2
,
5587 Parameter_Type
=> New_Occurrence_Of
(Op_Type
, Loc
)));
5590 -- Build expression as a function call, or as an operator node
5591 -- that corresponds to the name of the actual, starting with
5592 -- binary operators.
5594 if Op_Name
not in Any_Operator_Name
then
5596 Make_Function_Call
(Loc
,
5598 New_Occurrence_Of
(Actual_Subp
, Loc
),
5599 Parameter_Associations
=> New_List
(L
));
5602 Append_To
(Parameter_Associations
(Expr
), R
);
5607 elsif Is_Binary
then
5608 if Op_Name
= Name_Op_And
then
5609 Expr
:= Make_Op_And
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
5610 elsif Op_Name
= Name_Op_Or
then
5611 Expr
:= Make_Op_Or
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
5612 elsif Op_Name
= Name_Op_Xor
then
5613 Expr
:= Make_Op_Xor
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
5614 elsif Op_Name
= Name_Op_Eq
then
5615 Expr
:= Make_Op_Eq
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
5616 elsif Op_Name
= Name_Op_Ne
then
5617 Expr
:= Make_Op_Ne
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
5618 elsif Op_Name
= Name_Op_Le
then
5619 Expr
:= Make_Op_Le
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
5620 elsif Op_Name
= Name_Op_Gt
then
5621 Expr
:= Make_Op_Gt
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
5622 elsif Op_Name
= Name_Op_Ge
then
5623 Expr
:= Make_Op_Ge
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
5624 elsif Op_Name
= Name_Op_Lt
then
5625 Expr
:= Make_Op_Lt
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
5626 elsif Op_Name
= Name_Op_Add
then
5627 Expr
:= Make_Op_Add
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
5628 elsif Op_Name
= Name_Op_Subtract
then
5629 Expr
:= Make_Op_Subtract
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
5630 elsif Op_Name
= Name_Op_Concat
then
5631 Expr
:= Make_Op_Concat
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
5632 elsif Op_Name
= Name_Op_Multiply
then
5633 Expr
:= Make_Op_Multiply
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
5634 elsif Op_Name
= Name_Op_Divide
then
5635 Expr
:= Make_Op_Divide
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
5636 elsif Op_Name
= Name_Op_Mod
then
5637 Expr
:= Make_Op_Mod
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
5638 elsif Op_Name
= Name_Op_Rem
then
5639 Expr
:= Make_Op_Rem
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
5640 elsif Op_Name
= Name_Op_Expon
then
5641 Expr
:= Make_Op_Expon
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
5647 if Op_Name
= Name_Op_Add
then
5648 Expr
:= Make_Op_Plus
(Loc
, Right_Opnd
=> L
);
5649 elsif Op_Name
= Name_Op_Subtract
then
5650 Expr
:= Make_Op_Minus
(Loc
, Right_Opnd
=> L
);
5651 elsif Op_Name
= Name_Op_Abs
then
5652 Expr
:= Make_Op_Abs
(Loc
, Right_Opnd
=> L
);
5653 elsif Op_Name
= Name_Op_Not
then
5654 Expr
:= Make_Op_Not
(Loc
, Right_Opnd
=> L
);
5659 Make_Expression_Function
(Loc
,
5660 Specification
=> Spec
,
5661 Expression
=> Expr
);
5664 end Build_Operator_Wrapper
;
5666 -------------------------------------------
5667 -- Build_Instance_Compilation_Unit_Nodes --
5668 -------------------------------------------
5670 procedure Build_Instance_Compilation_Unit_Nodes
5675 Decl_Cunit
: Node_Id
;
5676 Body_Cunit
: Node_Id
;
5678 New_Main
: constant Entity_Id
:= Defining_Entity
(Act_Decl
);
5679 Old_Main
: constant Entity_Id
:= Cunit_Entity
(Main_Unit
);
5682 -- A new compilation unit node is built for the instance declaration
5685 Make_Compilation_Unit
(Sloc
(N
),
5686 Context_Items
=> Empty_List
,
5688 Aux_Decls_Node
=> Make_Compilation_Unit_Aux
(Sloc
(N
)));
5690 Set_Parent_Spec
(Act_Decl
, Parent_Spec
(N
));
5692 -- The new compilation unit is linked to its body, but both share the
5693 -- same file, so we do not set Body_Required on the new unit so as not
5694 -- to create a spurious dependency on a non-existent body in the ali.
5695 -- This simplifies CodePeer unit traversal.
5697 -- We use the original instantiation compilation unit as the resulting
5698 -- compilation unit of the instance, since this is the main unit.
5700 Rewrite
(N
, Act_Body
);
5702 -- Propagate the aspect specifications from the package body template to
5703 -- the instantiated version of the package body.
5705 if Has_Aspects
(Act_Body
) then
5706 Set_Aspect_Specifications
5707 (N
, New_Copy_List_Tree
(Aspect_Specifications
(Act_Body
)));
5710 Body_Cunit
:= Parent
(N
);
5712 -- The two compilation unit nodes are linked by the Library_Unit field
5714 Set_Library_Unit
(Decl_Cunit
, Body_Cunit
);
5715 Set_Library_Unit
(Body_Cunit
, Decl_Cunit
);
5717 -- Preserve the private nature of the package if needed
5719 Set_Private_Present
(Decl_Cunit
, Private_Present
(Body_Cunit
));
5721 -- If the instance is not the main unit, its context, categorization
5722 -- and elaboration entity are not relevant to the compilation.
5724 if Body_Cunit
/= Cunit
(Main_Unit
) then
5725 Make_Instance_Unit
(Body_Cunit
, In_Main
=> False);
5729 -- The context clause items on the instantiation, which are now attached
5730 -- to the body compilation unit (since the body overwrote the original
5731 -- instantiation node), semantically belong on the spec, so copy them
5732 -- there. It's harmless to leave them on the body as well. In fact one
5733 -- could argue that they belong in both places.
5735 Citem
:= First
(Context_Items
(Body_Cunit
));
5736 while Present
(Citem
) loop
5737 Append
(New_Copy
(Citem
), Context_Items
(Decl_Cunit
));
5741 -- Propagate categorization flags on packages, so that they appear in
5742 -- the ali file for the spec of the unit.
5744 if Ekind
(New_Main
) = E_Package
then
5745 Set_Is_Pure
(Old_Main
, Is_Pure
(New_Main
));
5746 Set_Is_Preelaborated
(Old_Main
, Is_Preelaborated
(New_Main
));
5747 Set_Is_Remote_Types
(Old_Main
, Is_Remote_Types
(New_Main
));
5748 Set_Is_Shared_Passive
(Old_Main
, Is_Shared_Passive
(New_Main
));
5749 Set_Is_Remote_Call_Interface
5750 (Old_Main
, Is_Remote_Call_Interface
(New_Main
));
5753 -- Make entry in Units table, so that binder can generate call to
5754 -- elaboration procedure for body, if any.
5756 Make_Instance_Unit
(Body_Cunit
, In_Main
=> True);
5757 Main_Unit_Entity
:= New_Main
;
5758 Set_Cunit_Entity
(Main_Unit
, Main_Unit_Entity
);
5760 -- Build elaboration entity, since the instance may certainly generate
5761 -- elaboration code requiring a flag for protection.
5763 Build_Elaboration_Entity
(Decl_Cunit
, New_Main
);
5764 end Build_Instance_Compilation_Unit_Nodes
;
5766 -----------------------------
5767 -- Check_Access_Definition --
5768 -----------------------------
5770 procedure Check_Access_Definition
(N
: Node_Id
) is
5773 (Ada_Version
>= Ada_2005
and then Present
(Access_Definition
(N
)));
5775 end Check_Access_Definition
;
5777 -----------------------------------
5778 -- Check_Formal_Package_Instance --
5779 -----------------------------------
5781 -- If the formal has specific parameters, they must match those of the
5782 -- actual. Both of them are instances, and the renaming declarations for
5783 -- their formal parameters appear in the same order in both. The analyzed
5784 -- formal has been analyzed in the context of the current instance.
5786 procedure Check_Formal_Package_Instance
5787 (Formal_Pack
: Entity_Id
;
5788 Actual_Pack
: Entity_Id
)
5790 E1
: Entity_Id
:= First_Entity
(Actual_Pack
);
5791 E2
: Entity_Id
:= First_Entity
(Formal_Pack
);
5792 Prev_E1
: Entity_Id
;
5797 procedure Check_Mismatch
(B
: Boolean);
5798 -- Common error routine for mismatch between the parameters of the
5799 -- actual instance and those of the formal package.
5801 function Same_Instantiated_Constant
(E1
, E2
: Entity_Id
) return Boolean;
5802 -- The formal may come from a nested formal package, and the actual may
5803 -- have been constant-folded. To determine whether the two denote the
5804 -- same entity we may have to traverse several definitions to recover
5805 -- the ultimate entity that they refer to.
5807 function Same_Instantiated_Function
(E1
, E2
: Entity_Id
) return Boolean;
5808 -- The formal and the actual must be identical, but if both are
5809 -- given by attributes they end up renaming different generated bodies,
5810 -- and we must verify that the attributes themselves match.
5812 function Same_Instantiated_Variable
(E1
, E2
: Entity_Id
) return Boolean;
5813 -- Similarly, if the formal comes from a nested formal package, the
5814 -- actual may designate the formal through multiple renamings, which
5815 -- have to be followed to determine the original variable in question.
5817 --------------------
5818 -- Check_Mismatch --
5819 --------------------
5821 procedure Check_Mismatch
(B
: Boolean) is
5822 -- A Formal_Type_Declaration for a derived private type is rewritten
5823 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
5824 -- which is why we examine the original node.
5826 Kind
: constant Node_Kind
:= Nkind
(Original_Node
(Parent
(E2
)));
5829 if Kind
= N_Formal_Type_Declaration
then
5832 elsif Nkind_In
(Kind
, N_Formal_Object_Declaration
,
5833 N_Formal_Package_Declaration
)
5834 or else Kind
in N_Formal_Subprogram_Declaration
5838 -- Ada 2012: If both formal and actual are incomplete types they
5841 elsif Is_Incomplete_Type
(E1
) and then Is_Incomplete_Type
(E2
) then
5846 ("actual for & in actual instance does not match formal",
5847 Parent
(Actual_Pack
), E1
);
5851 --------------------------------
5852 -- Same_Instantiated_Constant --
5853 --------------------------------
5855 function Same_Instantiated_Constant
5856 (E1
, E2
: Entity_Id
) return Boolean
5862 while Present
(Ent
) loop
5866 elsif Ekind
(Ent
) /= E_Constant
then
5869 elsif Is_Entity_Name
(Constant_Value
(Ent
)) then
5870 if Entity
(Constant_Value
(Ent
)) = E1
then
5873 Ent
:= Entity
(Constant_Value
(Ent
));
5876 -- The actual may be a constant that has been folded. Recover
5879 elsif Is_Entity_Name
(Original_Node
(Constant_Value
(Ent
))) then
5880 Ent
:= Entity
(Original_Node
(Constant_Value
(Ent
)));
5888 end Same_Instantiated_Constant
;
5890 --------------------------------
5891 -- Same_Instantiated_Function --
5892 --------------------------------
5894 function Same_Instantiated_Function
5895 (E1
, E2
: Entity_Id
) return Boolean
5899 if Alias
(E1
) = Alias
(E2
) then
5902 elsif Present
(Alias
(E2
)) then
5903 U1
:= Original_Node
(Unit_Declaration_Node
(E1
));
5904 U2
:= Original_Node
(Unit_Declaration_Node
(Alias
(E2
)));
5906 return Nkind
(U1
) = N_Subprogram_Renaming_Declaration
5907 and then Nkind
(Name
(U1
)) = N_Attribute_Reference
5909 and then Nkind
(U2
) = N_Subprogram_Renaming_Declaration
5910 and then Nkind
(Name
(U2
)) = N_Attribute_Reference
5913 Attribute_Name
(Name
(U1
)) = Attribute_Name
(Name
(U2
));
5917 end Same_Instantiated_Function
;
5919 --------------------------------
5920 -- Same_Instantiated_Variable --
5921 --------------------------------
5923 function Same_Instantiated_Variable
5924 (E1
, E2
: Entity_Id
) return Boolean
5926 function Original_Entity
(E
: Entity_Id
) return Entity_Id
;
5927 -- Follow chain of renamings to the ultimate ancestor
5929 ---------------------
5930 -- Original_Entity --
5931 ---------------------
5933 function Original_Entity
(E
: Entity_Id
) return Entity_Id
is
5938 while Nkind
(Parent
(Orig
)) = N_Object_Renaming_Declaration
5939 and then Present
(Renamed_Object
(Orig
))
5940 and then Is_Entity_Name
(Renamed_Object
(Orig
))
5942 Orig
:= Entity
(Renamed_Object
(Orig
));
5946 end Original_Entity
;
5948 -- Start of processing for Same_Instantiated_Variable
5951 return Ekind
(E1
) = Ekind
(E2
)
5952 and then Original_Entity
(E1
) = Original_Entity
(E2
);
5953 end Same_Instantiated_Variable
;
5955 -- Start of processing for Check_Formal_Package_Instance
5959 while Present
(E1
) and then Present
(E2
) loop
5960 exit when Ekind
(E1
) = E_Package
5961 and then Renamed_Entity
(E1
) = Renamed_Entity
(Actual_Pack
);
5963 -- If the formal is the renaming of the formal package, this
5964 -- is the end of its formal part, which may occur before the
5965 -- end of the formal part in the actual in the presence of
5966 -- defaulted parameters in the formal package.
5968 exit when Nkind
(Parent
(E2
)) = N_Package_Renaming_Declaration
5969 and then Renamed_Entity
(E2
) = Scope
(E2
);
5971 -- The analysis of the actual may generate additional internal
5972 -- entities. If the formal is defaulted, there is no corresponding
5973 -- analysis and the internal entities must be skipped, until we
5974 -- find corresponding entities again.
5976 if Comes_From_Source
(E2
)
5977 and then not Comes_From_Source
(E1
)
5978 and then Chars
(E1
) /= Chars
(E2
)
5980 while Present
(E1
) and then Chars
(E1
) /= Chars
(E2
) loop
5988 -- Entities may be declared without full declaration, such as
5989 -- itypes and predefined operators (concatenation for arrays, eg).
5990 -- Skip it and keep the formal entity to find a later match for it.
5992 elsif No
(Parent
(E2
)) and then Ekind
(E1
) /= Ekind
(E2
) then
5996 -- If the formal entity comes from a formal declaration, it was
5997 -- defaulted in the formal package, and no check is needed on it.
5999 elsif Nkind_In
(Original_Node
(Parent
(E2
)),
6000 N_Formal_Object_Declaration
,
6001 N_Formal_Type_Declaration
)
6003 -- If the formal is a tagged type the corresponding class-wide
6004 -- type has been generated as well, and it must be skipped.
6006 if Is_Type
(E2
) and then Is_Tagged_Type
(E2
) then
6012 -- Ditto for defaulted formal subprograms.
6014 elsif Is_Overloadable
(E1
)
6015 and then Nkind
(Unit_Declaration_Node
(E2
)) in
6016 N_Formal_Subprogram_Declaration
6020 elsif Is_Type
(E1
) then
6022 -- Subtypes must statically match. E1, E2 are the local entities
6023 -- that are subtypes of the actuals. Itypes generated for other
6024 -- parameters need not be checked, the check will be performed
6025 -- on the parameters themselves.
6027 -- If E2 is a formal type declaration, it is a defaulted parameter
6028 -- and needs no checking.
6030 if not Is_Itype
(E1
) and then not Is_Itype
(E2
) then
6033 or else Etype
(E1
) /= Etype
(E2
)
6034 or else not Subtypes_Statically_Match
(E1
, E2
));
6037 elsif Ekind
(E1
) = E_Constant
then
6039 -- IN parameters must denote the same static value, or the same
6040 -- constant, or the literal null.
6042 Expr1
:= Expression
(Parent
(E1
));
6044 if Ekind
(E2
) /= E_Constant
then
6045 Check_Mismatch
(True);
6048 Expr2
:= Expression
(Parent
(E2
));
6051 if Is_OK_Static_Expression
(Expr1
) then
6052 if not Is_OK_Static_Expression
(Expr2
) then
6053 Check_Mismatch
(True);
6055 elsif Is_Discrete_Type
(Etype
(E1
)) then
6057 V1
: constant Uint
:= Expr_Value
(Expr1
);
6058 V2
: constant Uint
:= Expr_Value
(Expr2
);
6060 Check_Mismatch
(V1
/= V2
);
6063 elsif Is_Real_Type
(Etype
(E1
)) then
6065 V1
: constant Ureal
:= Expr_Value_R
(Expr1
);
6066 V2
: constant Ureal
:= Expr_Value_R
(Expr2
);
6068 Check_Mismatch
(V1
/= V2
);
6071 elsif Is_String_Type
(Etype
(E1
))
6072 and then Nkind
(Expr1
) = N_String_Literal
6074 if Nkind
(Expr2
) /= N_String_Literal
then
6075 Check_Mismatch
(True);
6078 (not String_Equal
(Strval
(Expr1
), Strval
(Expr2
)));
6082 elsif Is_Entity_Name
(Expr1
) then
6083 if Is_Entity_Name
(Expr2
) then
6084 if Entity
(Expr1
) = Entity
(Expr2
) then
6088 (not Same_Instantiated_Constant
6089 (Entity
(Expr1
), Entity
(Expr2
)));
6093 Check_Mismatch
(True);
6096 elsif Is_Entity_Name
(Original_Node
(Expr1
))
6097 and then Is_Entity_Name
(Expr2
)
6098 and then Same_Instantiated_Constant
6099 (Entity
(Original_Node
(Expr1
)), Entity
(Expr2
))
6103 elsif Nkind
(Expr1
) = N_Null
then
6104 Check_Mismatch
(Nkind
(Expr1
) /= N_Null
);
6107 Check_Mismatch
(True);
6110 elsif Ekind
(E1
) = E_Variable
then
6111 Check_Mismatch
(not Same_Instantiated_Variable
(E1
, E2
));
6113 elsif Ekind
(E1
) = E_Package
then
6115 (Ekind
(E1
) /= Ekind
(E2
)
6116 or else Renamed_Object
(E1
) /= Renamed_Object
(E2
));
6118 elsif Is_Overloadable
(E1
) then
6120 -- Verify that the actual subprograms match. Note that actuals
6121 -- that are attributes are rewritten as subprograms. If the
6122 -- subprogram in the formal package is defaulted, no check is
6123 -- needed. Note that this can only happen in Ada 2005 when the
6124 -- formal package can be partially parameterized.
6126 if Nkind
(Unit_Declaration_Node
(E1
)) =
6127 N_Subprogram_Renaming_Declaration
6128 and then From_Default
(Unit_Declaration_Node
(E1
))
6132 -- If the formal package has an "others" box association that
6133 -- covers this formal, there is no need for a check either.
6135 elsif Nkind
(Unit_Declaration_Node
(E2
)) in
6136 N_Formal_Subprogram_Declaration
6137 and then Box_Present
(Unit_Declaration_Node
(E2
))
6141 -- No check needed if subprogram is a defaulted null procedure
6143 elsif No
(Alias
(E2
))
6144 and then Ekind
(E2
) = E_Procedure
6146 Null_Present
(Specification
(Unit_Declaration_Node
(E2
)))
6150 -- Otherwise the actual in the formal and the actual in the
6151 -- instantiation of the formal must match, up to renamings.
6155 (Ekind
(E2
) /= Ekind
(E1
)
6156 or else not Same_Instantiated_Function
(E1
, E2
));
6160 raise Program_Error
;
6168 end Check_Formal_Package_Instance
;
6170 ---------------------------
6171 -- Check_Formal_Packages --
6172 ---------------------------
6174 procedure Check_Formal_Packages
(P_Id
: Entity_Id
) is
6176 Formal_P
: Entity_Id
;
6177 Formal_Decl
: Node_Id
;
6180 -- Iterate through the declarations in the instance, looking for package
6181 -- renaming declarations that denote instances of formal packages. Stop
6182 -- when we find the renaming of the current package itself. The
6183 -- declaration for a formal package without a box is followed by an
6184 -- internal entity that repeats the instantiation.
6186 E
:= First_Entity
(P_Id
);
6187 while Present
(E
) loop
6188 if Ekind
(E
) = E_Package
then
6189 if Renamed_Object
(E
) = P_Id
then
6192 elsif Nkind
(Parent
(E
)) /= N_Package_Renaming_Declaration
then
6196 Formal_Decl
:= Parent
(Associated_Formal_Package
(E
));
6198 -- Nothing to check if the formal has a box or an others_clause
6199 -- (necessarily with a box).
6201 if Box_Present
(Formal_Decl
) then
6204 elsif Nkind
(First
(Generic_Associations
(Formal_Decl
))) =
6207 -- The internal validating package was generated but formal
6208 -- and instance are known to be compatible.
6210 Formal_P
:= Next_Entity
(E
);
6211 Remove
(Unit_Declaration_Node
(Formal_P
));
6214 Formal_P
:= Next_Entity
(E
);
6215 Check_Formal_Package_Instance
(Formal_P
, E
);
6217 -- After checking, remove the internal validating package.
6218 -- It is only needed for semantic checks, and as it may
6219 -- contain generic formal declarations it should not reach
6222 Remove
(Unit_Declaration_Node
(Formal_P
));
6229 end Check_Formal_Packages
;
6231 ---------------------------------
6232 -- Check_Forward_Instantiation --
6233 ---------------------------------
6235 procedure Check_Forward_Instantiation
(Decl
: Node_Id
) is
6237 Gen_Comp
: Entity_Id
:= Cunit_Entity
(Get_Source_Unit
(Decl
));
6240 -- The instantiation appears before the generic body if we are in the
6241 -- scope of the unit containing the generic, either in its spec or in
6242 -- the package body, and before the generic body.
6244 if Ekind
(Gen_Comp
) = E_Package_Body
then
6245 Gen_Comp
:= Spec_Entity
(Gen_Comp
);
6248 if In_Open_Scopes
(Gen_Comp
)
6249 and then No
(Corresponding_Body
(Decl
))
6254 and then not Is_Compilation_Unit
(S
)
6255 and then not Is_Child_Unit
(S
)
6257 if Ekind
(S
) = E_Package
then
6258 Set_Has_Forward_Instantiation
(S
);
6264 end Check_Forward_Instantiation
;
6266 ---------------------------
6267 -- Check_Generic_Actuals --
6268 ---------------------------
6270 -- The visibility of the actuals may be different between the point of
6271 -- generic instantiation and the instantiation of the body.
6273 procedure Check_Generic_Actuals
6274 (Instance
: Entity_Id
;
6275 Is_Formal_Box
: Boolean)
6280 function Denotes_Previous_Actual
(Typ
: Entity_Id
) return Boolean;
6281 -- For a formal that is an array type, the component type is often a
6282 -- previous formal in the same unit. The privacy status of the component
6283 -- type will have been examined earlier in the traversal of the
6284 -- corresponding actuals, and this status should not be modified for
6285 -- the array (sub)type itself. However, if the base type of the array
6286 -- (sub)type is private, its full view must be restored in the body to
6287 -- be consistent with subsequent index subtypes, etc.
6289 -- To detect this case we have to rescan the list of formals, which is
6290 -- usually short enough to ignore the resulting inefficiency.
6292 -----------------------------
6293 -- Denotes_Previous_Actual --
6294 -----------------------------
6296 function Denotes_Previous_Actual
(Typ
: Entity_Id
) return Boolean is
6300 Prev
:= First_Entity
(Instance
);
6301 while Present
(Prev
) loop
6303 and then Nkind
(Parent
(Prev
)) = N_Subtype_Declaration
6304 and then Is_Entity_Name
(Subtype_Indication
(Parent
(Prev
)))
6305 and then Entity
(Subtype_Indication
(Parent
(Prev
))) = Typ
6318 end Denotes_Previous_Actual
;
6320 -- Start of processing for Check_Generic_Actuals
6323 E
:= First_Entity
(Instance
);
6324 while Present
(E
) loop
6326 and then Nkind
(Parent
(E
)) = N_Subtype_Declaration
6327 and then Scope
(Etype
(E
)) /= Instance
6328 and then Is_Entity_Name
(Subtype_Indication
(Parent
(E
)))
6330 if Is_Array_Type
(E
)
6331 and then not Is_Private_Type
(Etype
(E
))
6332 and then Denotes_Previous_Actual
(Component_Type
(E
))
6336 Check_Private_View
(Subtype_Indication
(Parent
(E
)));
6339 Set_Is_Generic_Actual_Type
(E
, True);
6340 Set_Is_Hidden
(E
, False);
6341 Set_Is_Potentially_Use_Visible
(E
,
6344 -- We constructed the generic actual type as a subtype of the
6345 -- supplied type. This means that it normally would not inherit
6346 -- subtype specific attributes of the actual, which is wrong for
6347 -- the generic case.
6349 Astype
:= Ancestor_Subtype
(E
);
6353 -- This can happen when E is an itype that is the full view of
6354 -- a private type completed, e.g. with a constrained array. In
6355 -- that case, use the first subtype, which will carry size
6356 -- information. The base type itself is unconstrained and will
6359 Astype
:= First_Subtype
(E
);
6362 Set_Size_Info
(E
, (Astype
));
6363 Set_RM_Size
(E
, RM_Size
(Astype
));
6364 Set_First_Rep_Item
(E
, First_Rep_Item
(Astype
));
6366 if Is_Discrete_Or_Fixed_Point_Type
(E
) then
6367 Set_RM_Size
(E
, RM_Size
(Astype
));
6369 -- In nested instances, the base type of an access actual may
6370 -- itself be private, and need to be exchanged.
6372 elsif Is_Access_Type
(E
)
6373 and then Is_Private_Type
(Etype
(E
))
6376 (New_Occurrence_Of
(Etype
(E
), Sloc
(Instance
)));
6379 elsif Ekind
(E
) = E_Package
then
6381 -- If this is the renaming for the current instance, we're done.
6382 -- Otherwise it is a formal package. If the corresponding formal
6383 -- was declared with a box, the (instantiations of the) generic
6384 -- formal part are also visible. Otherwise, ignore the entity
6385 -- created to validate the actuals.
6387 if Renamed_Object
(E
) = Instance
then
6390 elsif Nkind
(Parent
(E
)) /= N_Package_Renaming_Declaration
then
6393 -- The visibility of a formal of an enclosing generic is already
6396 elsif Denotes_Formal_Package
(E
) then
6399 elsif Present
(Associated_Formal_Package
(E
))
6400 and then not Is_Generic_Formal
(E
)
6402 if Box_Present
(Parent
(Associated_Formal_Package
(E
))) then
6403 Check_Generic_Actuals
(Renamed_Object
(E
), True);
6406 Check_Generic_Actuals
(Renamed_Object
(E
), False);
6409 Set_Is_Hidden
(E
, False);
6412 -- If this is a subprogram instance (in a wrapper package) the
6413 -- actual is fully visible.
6415 elsif Is_Wrapper_Package
(Instance
) then
6416 Set_Is_Hidden
(E
, False);
6418 -- If the formal package is declared with a box, or if the formal
6419 -- parameter is defaulted, it is visible in the body.
6421 elsif Is_Formal_Box
or else Is_Visible_Formal
(E
) then
6422 Set_Is_Hidden
(E
, False);
6425 if Ekind
(E
) = E_Constant
then
6427 -- If the type of the actual is a private type declared in the
6428 -- enclosing scope of the generic unit, the body of the generic
6429 -- sees the full view of the type (because it has to appear in
6430 -- the corresponding package body). If the type is private now,
6431 -- exchange views to restore the proper visiblity in the instance.
6434 Typ
: constant Entity_Id
:= Base_Type
(Etype
(E
));
6435 -- The type of the actual
6440 Parent_Scope
: Entity_Id
;
6441 -- The enclosing scope of the generic unit
6444 if Is_Wrapper_Package
(Instance
) then
6448 (Unit_Declaration_Node
6449 (Related_Instance
(Instance
))));
6452 Generic_Parent
(Package_Specification
(Instance
));
6455 Parent_Scope
:= Scope
(Gen_Id
);
6457 -- The exchange is only needed if the generic is defined
6458 -- within a package which is not a common ancestor of the
6459 -- scope of the instance, and is not already in scope.
6461 if Is_Private_Type
(Typ
)
6462 and then Scope
(Typ
) = Parent_Scope
6463 and then Scope
(Instance
) /= Parent_Scope
6464 and then Ekind
(Parent_Scope
) = E_Package
6465 and then not Is_Child_Unit
(Gen_Id
)
6469 -- If the type of the entity is a subtype, it may also have
6470 -- to be made visible, together with the base type of its
6471 -- full view, after exchange.
6473 if Is_Private_Type
(Etype
(E
)) then
6474 Switch_View
(Etype
(E
));
6475 Switch_View
(Base_Type
(Etype
(E
)));
6483 end Check_Generic_Actuals
;
6485 ------------------------------
6486 -- Check_Generic_Child_Unit --
6487 ------------------------------
6489 procedure Check_Generic_Child_Unit
6491 Parent_Installed
: in out Boolean)
6493 Loc
: constant Source_Ptr
:= Sloc
(Gen_Id
);
6494 Gen_Par
: Entity_Id
:= Empty
;
6496 Inst_Par
: Entity_Id
;
6499 function Find_Generic_Child
6501 Id
: Node_Id
) return Entity_Id
;
6502 -- Search generic parent for possible child unit with the given name
6504 function In_Enclosing_Instance
return Boolean;
6505 -- Within an instance of the parent, the child unit may be denoted by
6506 -- a simple name, or an abbreviated expanded name. Examine enclosing
6507 -- scopes to locate a possible parent instantiation.
6509 ------------------------
6510 -- Find_Generic_Child --
6511 ------------------------
6513 function Find_Generic_Child
6515 Id
: Node_Id
) return Entity_Id
6520 -- If entity of name is already set, instance has already been
6521 -- resolved, e.g. in an enclosing instantiation.
6523 if Present
(Entity
(Id
)) then
6524 if Scope
(Entity
(Id
)) = Scop
then
6531 E
:= First_Entity
(Scop
);
6532 while Present
(E
) loop
6533 if Chars
(E
) = Chars
(Id
)
6534 and then Is_Child_Unit
(E
)
6536 if Is_Child_Unit
(E
)
6537 and then not Is_Visible_Lib_Unit
(E
)
6540 ("generic child unit& is not visible", Gen_Id
, E
);
6552 end Find_Generic_Child
;
6554 ---------------------------
6555 -- In_Enclosing_Instance --
6556 ---------------------------
6558 function In_Enclosing_Instance
return Boolean is
6559 Enclosing_Instance
: Node_Id
;
6560 Instance_Decl
: Node_Id
;
6563 -- We do not inline any call that contains instantiations, except
6564 -- for instantiations of Unchecked_Conversion, so if we are within
6565 -- an inlined body the current instance does not require parents.
6567 if In_Inlined_Body
then
6568 pragma Assert
(Chars
(Gen_Id
) = Name_Unchecked_Conversion
);
6572 -- Loop to check enclosing scopes
6574 Enclosing_Instance
:= Current_Scope
;
6575 while Present
(Enclosing_Instance
) loop
6576 Instance_Decl
:= Unit_Declaration_Node
(Enclosing_Instance
);
6578 if Ekind
(Enclosing_Instance
) = E_Package
6579 and then Is_Generic_Instance
(Enclosing_Instance
)
6581 (Generic_Parent
(Specification
(Instance_Decl
)))
6583 -- Check whether the generic we are looking for is a child of
6586 E
:= Find_Generic_Child
6587 (Generic_Parent
(Specification
(Instance_Decl
)), Gen_Id
);
6588 exit when Present
(E
);
6594 Enclosing_Instance
:= Scope
(Enclosing_Instance
);
6606 Make_Expanded_Name
(Loc
,
6608 Prefix
=> New_Occurrence_Of
(Enclosing_Instance
, Loc
),
6609 Selector_Name
=> New_Occurrence_Of
(E
, Loc
)));
6611 Set_Entity
(Gen_Id
, E
);
6612 Set_Etype
(Gen_Id
, Etype
(E
));
6613 Parent_Installed
:= False; -- Already in scope.
6616 end In_Enclosing_Instance
;
6618 -- Start of processing for Check_Generic_Child_Unit
6621 -- If the name of the generic is given by a selected component, it may
6622 -- be the name of a generic child unit, and the prefix is the name of an
6623 -- instance of the parent, in which case the child unit must be visible.
6624 -- If this instance is not in scope, it must be placed there and removed
6625 -- after instantiation, because what is being instantiated is not the
6626 -- original child, but the corresponding child present in the instance
6629 -- If the child is instantiated within the parent, it can be given by
6630 -- a simple name. In this case the instance is already in scope, but
6631 -- the child generic must be recovered from the generic parent as well.
6633 if Nkind
(Gen_Id
) = N_Selected_Component
then
6634 S
:= Selector_Name
(Gen_Id
);
6635 Analyze
(Prefix
(Gen_Id
));
6636 Inst_Par
:= Entity
(Prefix
(Gen_Id
));
6638 if Ekind
(Inst_Par
) = E_Package
6639 and then Present
(Renamed_Object
(Inst_Par
))
6641 Inst_Par
:= Renamed_Object
(Inst_Par
);
6644 if Ekind
(Inst_Par
) = E_Package
then
6645 if Nkind
(Parent
(Inst_Par
)) = N_Package_Specification
then
6646 Gen_Par
:= Generic_Parent
(Parent
(Inst_Par
));
6648 elsif Nkind
(Parent
(Inst_Par
)) = N_Defining_Program_Unit_Name
6650 Nkind
(Parent
(Parent
(Inst_Par
))) = N_Package_Specification
6652 Gen_Par
:= Generic_Parent
(Parent
(Parent
(Inst_Par
)));
6655 elsif Ekind
(Inst_Par
) = E_Generic_Package
6656 and then Nkind
(Parent
(Gen_Id
)) = N_Formal_Package_Declaration
6658 -- A formal package may be a real child package, and not the
6659 -- implicit instance within a parent. In this case the child is
6660 -- not visible and has to be retrieved explicitly as well.
6662 Gen_Par
:= Inst_Par
;
6665 if Present
(Gen_Par
) then
6667 -- The prefix denotes an instantiation. The entity itself may be a
6668 -- nested generic, or a child unit.
6670 E
:= Find_Generic_Child
(Gen_Par
, S
);
6673 Change_Selected_Component_To_Expanded_Name
(Gen_Id
);
6674 Set_Entity
(Gen_Id
, E
);
6675 Set_Etype
(Gen_Id
, Etype
(E
));
6677 Set_Etype
(S
, Etype
(E
));
6679 -- Indicate that this is a reference to the parent
6681 if In_Extended_Main_Source_Unit
(Gen_Id
) then
6682 Set_Is_Instantiated
(Inst_Par
);
6685 -- A common mistake is to replicate the naming scheme of a
6686 -- hierarchy by instantiating a generic child directly, rather
6687 -- than the implicit child in a parent instance:
6689 -- generic .. package Gpar is ..
6690 -- generic .. package Gpar.Child is ..
6691 -- package Par is new Gpar ();
6694 -- package Par.Child is new Gpar.Child ();
6695 -- rather than Par.Child
6697 -- In this case the instantiation is within Par, which is an
6698 -- instance, but Gpar does not denote Par because we are not IN
6699 -- the instance of Gpar, so this is illegal. The test below
6700 -- recognizes this particular case.
6702 if Is_Child_Unit
(E
)
6703 and then not Comes_From_Source
(Entity
(Prefix
(Gen_Id
)))
6704 and then (not In_Instance
6705 or else Nkind
(Parent
(Parent
(Gen_Id
))) =
6709 ("prefix of generic child unit must be instance of parent",
6713 if not In_Open_Scopes
(Inst_Par
)
6714 and then Nkind
(Parent
(Gen_Id
)) not in
6715 N_Generic_Renaming_Declaration
6717 Install_Parent
(Inst_Par
);
6718 Parent_Installed
:= True;
6720 elsif In_Open_Scopes
(Inst_Par
) then
6722 -- If the parent is already installed, install the actuals
6723 -- for its formal packages. This is necessary when the child
6724 -- instance is a child of the parent instance: in this case,
6725 -- the parent is placed on the scope stack but the formal
6726 -- packages are not made visible.
6728 Install_Formal_Packages
(Inst_Par
);
6732 -- If the generic parent does not contain an entity that
6733 -- corresponds to the selector, the instance doesn't either.
6734 -- Analyzing the node will yield the appropriate error message.
6735 -- If the entity is not a child unit, then it is an inner
6736 -- generic in the parent.
6744 if Is_Child_Unit
(Entity
(Gen_Id
))
6746 Nkind
(Parent
(Gen_Id
)) not in N_Generic_Renaming_Declaration
6747 and then not In_Open_Scopes
(Inst_Par
)
6749 Install_Parent
(Inst_Par
);
6750 Parent_Installed
:= True;
6752 -- The generic unit may be the renaming of the implicit child
6753 -- present in an instance. In that case the parent instance is
6754 -- obtained from the name of the renamed entity.
6756 elsif Ekind
(Entity
(Gen_Id
)) = E_Generic_Package
6757 and then Present
(Renamed_Entity
(Entity
(Gen_Id
)))
6758 and then Is_Child_Unit
(Renamed_Entity
(Entity
(Gen_Id
)))
6761 Renamed_Package
: constant Node_Id
:=
6762 Name
(Parent
(Entity
(Gen_Id
)));
6764 if Nkind
(Renamed_Package
) = N_Expanded_Name
then
6765 Inst_Par
:= Entity
(Prefix
(Renamed_Package
));
6766 Install_Parent
(Inst_Par
);
6767 Parent_Installed
:= True;
6773 elsif Nkind
(Gen_Id
) = N_Expanded_Name
then
6775 -- Entity already present, analyze prefix, whose meaning may be an
6776 -- instance in the current context. If it is an instance of a
6777 -- relative within another, the proper parent may still have to be
6778 -- installed, if they are not of the same generation.
6780 Analyze
(Prefix
(Gen_Id
));
6782 -- Prevent cascaded errors
6784 if Etype
(Prefix
(Gen_Id
)) = Any_Type
then
6788 -- In the unlikely case that a local declaration hides the name of
6789 -- the parent package, locate it on the homonym chain. If the context
6790 -- is an instance of the parent, the renaming entity is flagged as
6793 Inst_Par
:= Entity
(Prefix
(Gen_Id
));
6794 while Present
(Inst_Par
)
6795 and then not Is_Package_Or_Generic_Package
(Inst_Par
)
6797 Inst_Par
:= Homonym
(Inst_Par
);
6800 pragma Assert
(Present
(Inst_Par
));
6801 Set_Entity
(Prefix
(Gen_Id
), Inst_Par
);
6803 if In_Enclosing_Instance
then
6806 elsif Present
(Entity
(Gen_Id
))
6807 and then Is_Child_Unit
(Entity
(Gen_Id
))
6808 and then not In_Open_Scopes
(Inst_Par
)
6810 Install_Parent
(Inst_Par
);
6811 Parent_Installed
:= True;
6814 elsif In_Enclosing_Instance
then
6816 -- The child unit is found in some enclosing scope
6823 -- If this is the renaming of the implicit child in a parent
6824 -- instance, recover the parent name and install it.
6826 if Is_Entity_Name
(Gen_Id
) then
6827 E
:= Entity
(Gen_Id
);
6829 if Is_Generic_Unit
(E
)
6830 and then Nkind
(Parent
(E
)) in N_Generic_Renaming_Declaration
6831 and then Is_Child_Unit
(Renamed_Object
(E
))
6832 and then Is_Generic_Unit
(Scope
(Renamed_Object
(E
)))
6833 and then Nkind
(Name
(Parent
(E
))) = N_Expanded_Name
6835 Rewrite
(Gen_Id
, New_Copy_Tree
(Name
(Parent
(E
))));
6836 Inst_Par
:= Entity
(Prefix
(Gen_Id
));
6838 if not In_Open_Scopes
(Inst_Par
) then
6839 Install_Parent
(Inst_Par
);
6840 Parent_Installed
:= True;
6843 -- If it is a child unit of a non-generic parent, it may be
6844 -- use-visible and given by a direct name. Install parent as
6847 elsif Is_Generic_Unit
(E
)
6848 and then Is_Child_Unit
(E
)
6850 Nkind
(Parent
(Gen_Id
)) not in N_Generic_Renaming_Declaration
6851 and then not Is_Generic_Unit
(Scope
(E
))
6853 if not In_Open_Scopes
(Scope
(E
)) then
6854 Install_Parent
(Scope
(E
));
6855 Parent_Installed
:= True;
6860 end Check_Generic_Child_Unit
;
6862 -----------------------------
6863 -- Check_Hidden_Child_Unit --
6864 -----------------------------
6866 procedure Check_Hidden_Child_Unit
6868 Gen_Unit
: Entity_Id
;
6869 Act_Decl_Id
: Entity_Id
)
6871 Gen_Id
: constant Node_Id
:= Name
(N
);
6874 if Is_Child_Unit
(Gen_Unit
)
6875 and then Is_Child_Unit
(Act_Decl_Id
)
6876 and then Nkind
(Gen_Id
) = N_Expanded_Name
6877 and then Entity
(Prefix
(Gen_Id
)) = Scope
(Act_Decl_Id
)
6878 and then Chars
(Gen_Unit
) = Chars
(Act_Decl_Id
)
6880 Error_Msg_Node_2
:= Scope
(Act_Decl_Id
);
6882 ("generic unit & is implicitly declared in &",
6883 Defining_Unit_Name
(N
), Gen_Unit
);
6884 Error_Msg_N
("\instance must have different name",
6885 Defining_Unit_Name
(N
));
6887 end Check_Hidden_Child_Unit
;
6889 ------------------------
6890 -- Check_Private_View --
6891 ------------------------
6893 procedure Check_Private_View
(N
: Node_Id
) is
6894 T
: constant Entity_Id
:= Etype
(N
);
6898 -- Exchange views if the type was not private in the generic but is
6899 -- private at the point of instantiation. Do not exchange views if
6900 -- the scope of the type is in scope. This can happen if both generic
6901 -- and instance are sibling units, or if type is defined in a parent.
6902 -- In this case the visibility of the type will be correct for all
6906 BT
:= Base_Type
(T
);
6908 if Is_Private_Type
(T
)
6909 and then not Has_Private_View
(N
)
6910 and then Present
(Full_View
(T
))
6911 and then not In_Open_Scopes
(Scope
(T
))
6913 -- In the generic, the full type was visible. Save the private
6914 -- entity, for subsequent exchange.
6918 elsif Has_Private_View
(N
)
6919 and then not Is_Private_Type
(T
)
6920 and then not Has_Been_Exchanged
(T
)
6921 and then Etype
(Get_Associated_Node
(N
)) /= T
6923 -- Only the private declaration was visible in the generic. If
6924 -- the type appears in a subtype declaration, the subtype in the
6925 -- instance must have a view compatible with that of its parent,
6926 -- which must be exchanged (see corresponding code in Restore_
6927 -- Private_Views). Otherwise, if the type is defined in a parent
6928 -- unit, leave full visibility within instance, which is safe.
6930 if In_Open_Scopes
(Scope
(Base_Type
(T
)))
6931 and then not Is_Private_Type
(Base_Type
(T
))
6932 and then Comes_From_Source
(Base_Type
(T
))
6936 elsif Nkind
(Parent
(N
)) = N_Subtype_Declaration
6937 or else not In_Private_Part
(Scope
(Base_Type
(T
)))
6939 Prepend_Elmt
(T
, Exchanged_Views
);
6940 Exchange_Declarations
(Etype
(Get_Associated_Node
(N
)));
6943 -- For composite types with inconsistent representation exchange
6944 -- component types accordingly.
6946 elsif Is_Access_Type
(T
)
6947 and then Is_Private_Type
(Designated_Type
(T
))
6948 and then not Has_Private_View
(N
)
6949 and then Present
(Full_View
(Designated_Type
(T
)))
6951 Switch_View
(Designated_Type
(T
));
6953 elsif Is_Array_Type
(T
) then
6954 if Is_Private_Type
(Component_Type
(T
))
6955 and then not Has_Private_View
(N
)
6956 and then Present
(Full_View
(Component_Type
(T
)))
6958 Switch_View
(Component_Type
(T
));
6961 -- The normal exchange mechanism relies on the setting of a
6962 -- flag on the reference in the generic. However, an additional
6963 -- mechanism is needed for types that are not explicitly
6964 -- mentioned in the generic, but may be needed in expanded code
6965 -- in the instance. This includes component types of arrays and
6966 -- designated types of access types. This processing must also
6967 -- include the index types of arrays which we take care of here.
6974 Indx
:= First_Index
(T
);
6975 while Present
(Indx
) loop
6976 Typ
:= Base_Type
(Etype
(Indx
));
6978 if Is_Private_Type
(Typ
)
6979 and then Present
(Full_View
(Typ
))
6988 elsif Is_Private_Type
(T
)
6989 and then Present
(Full_View
(T
))
6990 and then Is_Array_Type
(Full_View
(T
))
6991 and then Is_Private_Type
(Component_Type
(Full_View
(T
)))
6995 -- Finally, a non-private subtype may have a private base type, which
6996 -- must be exchanged for consistency. This can happen when a package
6997 -- body is instantiated, when the scope stack is empty but in fact
6998 -- the subtype and the base type are declared in an enclosing scope.
7000 -- Note that in this case we introduce an inconsistency in the view
7001 -- set, because we switch the base type BT, but there could be some
7002 -- private dependent subtypes of BT which remain unswitched. Such
7003 -- subtypes might need to be switched at a later point (see specific
7004 -- provision for that case in Switch_View).
7006 elsif not Is_Private_Type
(T
)
7007 and then not Has_Private_View
(N
)
7008 and then Is_Private_Type
(BT
)
7009 and then Present
(Full_View
(BT
))
7010 and then not Is_Generic_Type
(BT
)
7011 and then not In_Open_Scopes
(BT
)
7013 Prepend_Elmt
(Full_View
(BT
), Exchanged_Views
);
7014 Exchange_Declarations
(BT
);
7017 end Check_Private_View
;
7019 -----------------------------
7020 -- Check_Hidden_Primitives --
7021 -----------------------------
7023 function Check_Hidden_Primitives
(Assoc_List
: List_Id
) return Elist_Id
is
7026 Result
: Elist_Id
:= No_Elist
;
7029 if No
(Assoc_List
) then
7033 -- Traverse the list of associations between formals and actuals
7034 -- searching for renamings of tagged types
7036 Actual
:= First
(Assoc_List
);
7037 while Present
(Actual
) loop
7038 if Nkind
(Actual
) = N_Subtype_Declaration
then
7039 Gen_T
:= Generic_Parent_Type
(Actual
);
7041 if Present
(Gen_T
) and then Is_Tagged_Type
(Gen_T
) then
7043 -- Traverse the list of primitives of the actual types
7044 -- searching for hidden primitives that are visible in the
7045 -- corresponding generic formal; leave them visible and
7046 -- append them to Result to restore their decoration later.
7048 Install_Hidden_Primitives
7049 (Prims_List
=> Result
,
7051 Act_T
=> Entity
(Subtype_Indication
(Actual
)));
7059 end Check_Hidden_Primitives
;
7061 --------------------------
7062 -- Contains_Instance_Of --
7063 --------------------------
7065 function Contains_Instance_Of
7068 N
: Node_Id
) return Boolean
7076 -- Verify that there are no circular instantiations. We check whether
7077 -- the unit contains an instance of the current scope or some enclosing
7078 -- scope (in case one of the instances appears in a subunit). Longer
7079 -- circularities involving subunits might seem too pathological to
7080 -- consider, but they were not too pathological for the authors of
7081 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7082 -- enclosing generic scopes as containing an instance.
7085 -- Within a generic subprogram body, the scope is not generic, to
7086 -- allow for recursive subprograms. Use the declaration to determine
7087 -- whether this is a generic unit.
7089 if Ekind
(Scop
) = E_Generic_Package
7090 or else (Is_Subprogram
(Scop
)
7091 and then Nkind
(Unit_Declaration_Node
(Scop
)) =
7092 N_Generic_Subprogram_Declaration
)
7094 Elmt
:= First_Elmt
(Inner_Instances
(Inner
));
7096 while Present
(Elmt
) loop
7097 if Node
(Elmt
) = Scop
then
7098 Error_Msg_Node_2
:= Inner
;
7100 ("circular Instantiation: & instantiated within &!",
7104 elsif Node
(Elmt
) = Inner
then
7107 elsif Contains_Instance_Of
(Node
(Elmt
), Scop
, N
) then
7108 Error_Msg_Node_2
:= Inner
;
7110 ("circular Instantiation: & instantiated within &!",
7118 -- Indicate that Inner is being instantiated within Scop
7120 Append_Elmt
(Inner
, Inner_Instances
(Scop
));
7123 if Scop
= Standard_Standard
then
7126 Scop
:= Scope
(Scop
);
7131 end Contains_Instance_Of
;
7133 -----------------------
7134 -- Copy_Generic_Node --
7135 -----------------------
7137 function Copy_Generic_Node
7139 Parent_Id
: Node_Id
;
7140 Instantiating
: Boolean) return Node_Id
7145 function Copy_Generic_Descendant
(D
: Union_Id
) return Union_Id
;
7146 -- Check the given value of one of the Fields referenced by the current
7147 -- node to determine whether to copy it recursively. The field may hold
7148 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7149 -- Char) in which case it need not be copied.
7151 procedure Copy_Descendants
;
7152 -- Common utility for various nodes
7154 function Copy_Generic_Elist
(E
: Elist_Id
) return Elist_Id
;
7155 -- Make copy of element list
7157 function Copy_Generic_List
7159 Parent_Id
: Node_Id
) return List_Id
;
7160 -- Apply Copy_Node recursively to the members of a node list
7162 function In_Defining_Unit_Name
(Nam
: Node_Id
) return Boolean;
7163 -- True if an identifier is part of the defining program unit name of
7164 -- a child unit. The entity of such an identifier must be kept (for
7165 -- ASIS use) even though as the name of an enclosing generic it would
7166 -- otherwise not be preserved in the generic tree.
7168 ----------------------
7169 -- Copy_Descendants --
7170 ----------------------
7172 procedure Copy_Descendants
is
7173 use Atree
.Unchecked_Access
;
7174 -- This code section is part of the implementation of an untyped
7175 -- tree traversal, so it needs direct access to node fields.
7178 Set_Field1
(New_N
, Copy_Generic_Descendant
(Field1
(N
)));
7179 Set_Field2
(New_N
, Copy_Generic_Descendant
(Field2
(N
)));
7180 Set_Field3
(New_N
, Copy_Generic_Descendant
(Field3
(N
)));
7181 Set_Field4
(New_N
, Copy_Generic_Descendant
(Field4
(N
)));
7182 Set_Field5
(New_N
, Copy_Generic_Descendant
(Field5
(N
)));
7183 end Copy_Descendants
;
7185 -----------------------------
7186 -- Copy_Generic_Descendant --
7187 -----------------------------
7189 function Copy_Generic_Descendant
(D
: Union_Id
) return Union_Id
is
7191 if D
= Union_Id
(Empty
) then
7194 elsif D
in Node_Range
then
7196 (Copy_Generic_Node
(Node_Id
(D
), New_N
, Instantiating
));
7198 elsif D
in List_Range
then
7199 return Union_Id
(Copy_Generic_List
(List_Id
(D
), New_N
));
7201 elsif D
in Elist_Range
then
7202 return Union_Id
(Copy_Generic_Elist
(Elist_Id
(D
)));
7204 -- Nothing else is copyable (e.g. Uint values), return as is
7209 end Copy_Generic_Descendant
;
7211 ------------------------
7212 -- Copy_Generic_Elist --
7213 ------------------------
7215 function Copy_Generic_Elist
(E
: Elist_Id
) return Elist_Id
is
7222 M
:= First_Elmt
(E
);
7223 while Present
(M
) loop
7225 (Copy_Generic_Node
(Node
(M
), Empty
, Instantiating
), L
);
7234 end Copy_Generic_Elist
;
7236 -----------------------
7237 -- Copy_Generic_List --
7238 -----------------------
7240 function Copy_Generic_List
7242 Parent_Id
: Node_Id
) return List_Id
7250 Set_Parent
(New_L
, Parent_Id
);
7253 while Present
(N
) loop
7254 Append
(Copy_Generic_Node
(N
, Empty
, Instantiating
), New_L
);
7263 end Copy_Generic_List
;
7265 ---------------------------
7266 -- In_Defining_Unit_Name --
7267 ---------------------------
7269 function In_Defining_Unit_Name
(Nam
: Node_Id
) return Boolean is
7272 Present
(Parent
(Nam
))
7273 and then (Nkind
(Parent
(Nam
)) = N_Defining_Program_Unit_Name
7275 (Nkind
(Parent
(Nam
)) = N_Expanded_Name
7276 and then In_Defining_Unit_Name
(Parent
(Nam
))));
7277 end In_Defining_Unit_Name
;
7279 -- Start of processing for Copy_Generic_Node
7286 New_N
:= New_Copy
(N
);
7288 -- Copy aspects if present
7290 if Has_Aspects
(N
) then
7291 Set_Has_Aspects
(New_N
, False);
7292 Set_Aspect_Specifications
7293 (New_N
, Copy_Generic_List
(Aspect_Specifications
(N
), Parent_Id
));
7296 if Instantiating
then
7297 Adjust_Instantiation_Sloc
(New_N
, S_Adjustment
);
7300 if not Is_List_Member
(N
) then
7301 Set_Parent
(New_N
, Parent_Id
);
7304 -- Special casing for identifiers and other entity names and operators
7306 if Nkind_In
(New_N
, N_Character_Literal
,
7310 or else Nkind
(New_N
) in N_Op
7312 if not Instantiating
then
7314 -- Link both nodes in order to assign subsequently the entity of
7315 -- the copy to the original node, in case this is a global
7318 Set_Associated_Node
(N
, New_N
);
7320 -- If we are within an instantiation, this is a nested generic
7321 -- that has already been analyzed at the point of definition.
7322 -- We must preserve references that were global to the enclosing
7323 -- parent at that point. Other occurrences, whether global or
7324 -- local to the current generic, must be resolved anew, so we
7325 -- reset the entity in the generic copy. A global reference has a
7326 -- smaller depth than the parent, or else the same depth in case
7327 -- both are distinct compilation units.
7329 -- A child unit is implicitly declared within the enclosing parent
7330 -- but is in fact global to it, and must be preserved.
7332 -- It is also possible for Current_Instantiated_Parent to be
7333 -- defined, and for this not to be a nested generic, namely if
7334 -- the unit is loaded through Rtsfind. In that case, the entity of
7335 -- New_N is only a link to the associated node, and not a defining
7338 -- The entities for parent units in the defining_program_unit of a
7339 -- generic child unit are established when the context of the unit
7340 -- is first analyzed, before the generic copy is made. They are
7341 -- preserved in the copy for use in ASIS queries.
7343 Ent
:= Entity
(New_N
);
7345 if No
(Current_Instantiated_Parent
.Gen_Id
) then
7347 or else Nkind
(Ent
) /= N_Defining_Identifier
7348 or else not In_Defining_Unit_Name
(N
)
7350 Set_Associated_Node
(New_N
, Empty
);
7355 not Nkind_In
(Ent
, N_Defining_Identifier
,
7356 N_Defining_Character_Literal
,
7357 N_Defining_Operator_Symbol
)
7358 or else No
(Scope
(Ent
))
7360 (Scope
(Ent
) = Current_Instantiated_Parent
.Gen_Id
7361 and then not Is_Child_Unit
(Ent
))
7363 (Scope_Depth
(Scope
(Ent
)) >
7364 Scope_Depth
(Current_Instantiated_Parent
.Gen_Id
)
7366 Get_Source_Unit
(Ent
) =
7367 Get_Source_Unit
(Current_Instantiated_Parent
.Gen_Id
))
7369 Set_Associated_Node
(New_N
, Empty
);
7372 -- Case of instantiating identifier or some other name or operator
7375 -- If the associated node is still defined, the entity in it
7376 -- is global, and must be copied to the instance. If this copy
7377 -- is being made for a body to inline, it is applied to an
7378 -- instantiated tree, and the entity is already present and
7379 -- must be also preserved.
7382 Assoc
: constant Node_Id
:= Get_Associated_Node
(N
);
7385 if Present
(Assoc
) then
7386 if Nkind
(Assoc
) = Nkind
(N
) then
7387 Set_Entity
(New_N
, Entity
(Assoc
));
7388 Check_Private_View
(N
);
7390 -- The node is a reference to a global type and acts as the
7391 -- subtype mark of a qualified expression created in order
7392 -- to aid resolution of accidental overloading in instances.
7393 -- Since N is a reference to a type, the Associated_Node of
7394 -- N denotes an entity rather than another identifier. See
7395 -- Qualify_Universal_Operands for details.
7397 elsif Nkind
(N
) = N_Identifier
7398 and then Nkind
(Parent
(N
)) = N_Qualified_Expression
7399 and then Subtype_Mark
(Parent
(N
)) = N
7400 and then Is_Qualified_Universal_Literal
(Parent
(N
))
7402 Set_Entity
(New_N
, Assoc
);
7404 -- The name in the call may be a selected component if the
7405 -- call has not been analyzed yet, as may be the case for
7406 -- pre/post conditions in a generic unit.
7408 elsif Nkind
(Assoc
) = N_Function_Call
7409 and then Is_Entity_Name
(Name
(Assoc
))
7411 Set_Entity
(New_N
, Entity
(Name
(Assoc
)));
7413 elsif Nkind_In
(Assoc
, N_Defining_Identifier
,
7414 N_Defining_Character_Literal
,
7415 N_Defining_Operator_Symbol
)
7416 and then Expander_Active
7418 -- Inlining case: we are copying a tree that contains
7419 -- global entities, which are preserved in the copy to be
7420 -- used for subsequent inlining.
7425 Set_Entity
(New_N
, Empty
);
7431 -- For expanded name, we must copy the Prefix and Selector_Name
7433 if Nkind
(N
) = N_Expanded_Name
then
7435 (New_N
, Copy_Generic_Node
(Prefix
(N
), New_N
, Instantiating
));
7437 Set_Selector_Name
(New_N
,
7438 Copy_Generic_Node
(Selector_Name
(N
), New_N
, Instantiating
));
7440 -- For operators, we must copy the right operand
7442 elsif Nkind
(N
) in N_Op
then
7443 Set_Right_Opnd
(New_N
,
7444 Copy_Generic_Node
(Right_Opnd
(N
), New_N
, Instantiating
));
7446 -- And for binary operators, the left operand as well
7448 if Nkind
(N
) in N_Binary_Op
then
7449 Set_Left_Opnd
(New_N
,
7450 Copy_Generic_Node
(Left_Opnd
(N
), New_N
, Instantiating
));
7454 -- Establish a link between an entity from the generic template and the
7455 -- corresponding entity in the generic copy to be analyzed.
7457 elsif Nkind
(N
) in N_Entity
then
7458 if not Instantiating
then
7459 Set_Associated_Entity
(N
, New_N
);
7462 -- Clear any existing link the copy may inherit from the replicated
7463 -- generic template entity.
7465 Set_Associated_Entity
(New_N
, Empty
);
7467 -- Special casing for stubs
7469 elsif Nkind
(N
) in N_Body_Stub
then
7471 -- In any case, we must copy the specification or defining
7472 -- identifier as appropriate.
7474 if Nkind
(N
) = N_Subprogram_Body_Stub
then
7475 Set_Specification
(New_N
,
7476 Copy_Generic_Node
(Specification
(N
), New_N
, Instantiating
));
7479 Set_Defining_Identifier
(New_N
,
7481 (Defining_Identifier
(N
), New_N
, Instantiating
));
7484 -- If we are not instantiating, then this is where we load and
7485 -- analyze subunits, i.e. at the point where the stub occurs. A
7486 -- more permissive system might defer this analysis to the point
7487 -- of instantiation, but this seems too complicated for now.
7489 if not Instantiating
then
7491 Subunit_Name
: constant Unit_Name_Type
:= Get_Unit_Name
(N
);
7493 Unum
: Unit_Number_Type
;
7497 -- Make sure that, if it is a subunit of the main unit that is
7498 -- preprocessed and if -gnateG is specified, the preprocessed
7499 -- file will be written.
7501 Lib
.Analysing_Subunit_Of_Main
:=
7502 Lib
.In_Extended_Main_Source_Unit
(N
);
7505 (Load_Name
=> Subunit_Name
,
7509 Lib
.Analysing_Subunit_Of_Main
:= False;
7511 -- If the proper body is not found, a warning message will be
7512 -- emitted when analyzing the stub, or later at the point of
7513 -- instantiation. Here we just leave the stub as is.
7515 if Unum
= No_Unit
then
7516 Subunits_Missing
:= True;
7517 goto Subunit_Not_Found
;
7520 Subunit
:= Cunit
(Unum
);
7522 if Nkind
(Unit
(Subunit
)) /= N_Subunit
then
7524 ("found child unit instead of expected SEPARATE subunit",
7526 Error_Msg_Sloc
:= Sloc
(N
);
7527 Error_Msg_N
("\to complete stub #", Subunit
);
7528 goto Subunit_Not_Found
;
7531 -- We must create a generic copy of the subunit, in order to
7532 -- perform semantic analysis on it, and we must replace the
7533 -- stub in the original generic unit with the subunit, in order
7534 -- to preserve non-local references within.
7536 -- Only the proper body needs to be copied. Library_Unit and
7537 -- context clause are simply inherited by the generic copy.
7538 -- Note that the copy (which may be recursive if there are
7539 -- nested subunits) must be done first, before attaching it to
7540 -- the enclosing generic.
7544 (Proper_Body
(Unit
(Subunit
)),
7545 Empty
, Instantiating
=> False);
7547 -- Now place the original proper body in the original generic
7548 -- unit. This is a body, not a compilation unit.
7550 Rewrite
(N
, Proper_Body
(Unit
(Subunit
)));
7551 Set_Is_Compilation_Unit
(Defining_Entity
(N
), False);
7552 Set_Was_Originally_Stub
(N
);
7554 -- Finally replace the body of the subunit with its copy, and
7555 -- make this new subunit into the library unit of the generic
7556 -- copy, which does not have stubs any longer.
7558 Set_Proper_Body
(Unit
(Subunit
), New_Body
);
7559 Set_Library_Unit
(New_N
, Subunit
);
7560 Inherit_Context
(Unit
(Subunit
), N
);
7563 -- If we are instantiating, this must be an error case, since
7564 -- otherwise we would have replaced the stub node by the proper body
7565 -- that corresponds. So just ignore it in the copy (i.e. we have
7566 -- copied it, and that is good enough).
7572 <<Subunit_Not_Found
>> null;
7574 -- If the node is a compilation unit, it is the subunit of a stub, which
7575 -- has been loaded already (see code below). In this case, the library
7576 -- unit field of N points to the parent unit (which is a compilation
7577 -- unit) and need not (and cannot) be copied.
7579 -- When the proper body of the stub is analyzed, the library_unit link
7580 -- is used to establish the proper context (see sem_ch10).
7582 -- The other fields of a compilation unit are copied as usual
7584 elsif Nkind
(N
) = N_Compilation_Unit
then
7586 -- This code can only be executed when not instantiating, because in
7587 -- the copy made for an instantiation, the compilation unit node has
7588 -- disappeared at the point that a stub is replaced by its proper
7591 pragma Assert
(not Instantiating
);
7593 Set_Context_Items
(New_N
,
7594 Copy_Generic_List
(Context_Items
(N
), New_N
));
7597 Copy_Generic_Node
(Unit
(N
), New_N
, False));
7599 Set_First_Inlined_Subprogram
(New_N
,
7601 (First_Inlined_Subprogram
(N
), New_N
, False));
7603 Set_Aux_Decls_Node
(New_N
,
7604 Copy_Generic_Node
(Aux_Decls_Node
(N
), New_N
, False));
7606 -- For an assignment node, the assignment is known to be semantically
7607 -- legal if we are instantiating the template. This avoids incorrect
7608 -- diagnostics in generated code.
7610 elsif Nkind
(N
) = N_Assignment_Statement
then
7612 -- Copy name and expression fields in usual manner
7615 Copy_Generic_Node
(Name
(N
), New_N
, Instantiating
));
7617 Set_Expression
(New_N
,
7618 Copy_Generic_Node
(Expression
(N
), New_N
, Instantiating
));
7620 if Instantiating
then
7621 Set_Assignment_OK
(Name
(New_N
), True);
7624 elsif Nkind_In
(N
, N_Aggregate
, N_Extension_Aggregate
) then
7625 if not Instantiating
then
7626 Set_Associated_Node
(N
, New_N
);
7629 if Present
(Get_Associated_Node
(N
))
7630 and then Nkind
(Get_Associated_Node
(N
)) = Nkind
(N
)
7632 -- In the generic the aggregate has some composite type. If at
7633 -- the point of instantiation the type has a private view,
7634 -- install the full view (and that of its ancestors, if any).
7637 T
: Entity_Id
:= (Etype
(Get_Associated_Node
(New_N
)));
7641 if Present
(T
) and then Is_Private_Type
(T
) then
7646 and then Is_Tagged_Type
(T
)
7647 and then Is_Derived_Type
(T
)
7649 Rt
:= Root_Type
(T
);
7654 if Is_Private_Type
(T
) then
7665 -- Do not copy the associated node, which points to the generic copy
7666 -- of the aggregate.
7669 use Atree
.Unchecked_Access
;
7670 -- This code section is part of the implementation of an untyped
7671 -- tree traversal, so it needs direct access to node fields.
7674 Set_Field1
(New_N
, Copy_Generic_Descendant
(Field1
(N
)));
7675 Set_Field2
(New_N
, Copy_Generic_Descendant
(Field2
(N
)));
7676 Set_Field3
(New_N
, Copy_Generic_Descendant
(Field3
(N
)));
7677 Set_Field5
(New_N
, Copy_Generic_Descendant
(Field5
(N
)));
7680 -- Allocators do not have an identifier denoting the access type, so we
7681 -- must locate it through the expression to check whether the views are
7684 elsif Nkind
(N
) = N_Allocator
7685 and then Nkind
(Expression
(N
)) = N_Qualified_Expression
7686 and then Is_Entity_Name
(Subtype_Mark
(Expression
(N
)))
7687 and then Instantiating
7690 T
: constant Node_Id
:=
7691 Get_Associated_Node
(Subtype_Mark
(Expression
(N
)));
7697 -- Retrieve the allocator node in the generic copy
7699 Acc_T
:= Etype
(Parent
(Parent
(T
)));
7701 if Present
(Acc_T
) and then Is_Private_Type
(Acc_T
) then
7702 Switch_View
(Acc_T
);
7709 -- For a proper body, we must catch the case of a proper body that
7710 -- replaces a stub. This represents the point at which a separate
7711 -- compilation unit, and hence template file, may be referenced, so we
7712 -- must make a new source instantiation entry for the template of the
7713 -- subunit, and ensure that all nodes in the subunit are adjusted using
7714 -- this new source instantiation entry.
7716 elsif Nkind
(N
) in N_Proper_Body
then
7718 Save_Adjustment
: constant Sloc_Adjustment
:= S_Adjustment
;
7721 if Instantiating
and then Was_Originally_Stub
(N
) then
7722 Create_Instantiation_Source
7723 (Instantiation_Node
,
7724 Defining_Entity
(N
),
7728 -- Now copy the fields of the proper body, using the new
7729 -- adjustment factor if one was needed as per test above.
7733 -- Restore the original adjustment factor in case changed
7735 S_Adjustment
:= Save_Adjustment
;
7738 elsif Nkind
(N
) = N_Pragma
and then Instantiating
then
7740 -- Do not copy Comment or Ident pragmas their content is relevant to
7741 -- the generic unit, not to the instantiating unit.
7743 if Nam_In
(Pragma_Name
(N
), Name_Comment
, Name_Ident
) then
7744 New_N
:= Make_Null_Statement
(Sloc
(N
));
7746 -- Do not copy pragmas generated from aspects because the pragmas do
7747 -- not carry any semantic information, plus they will be regenerated
7750 -- However, generating C we need to copy them since postconditions
7751 -- are inlined by the front end, and the front-end inlining machinery
7752 -- relies on this routine to perform inlining.
7754 elsif From_Aspect_Specification
(N
)
7755 and then not Modify_Tree_For_C
7757 New_N
:= Make_Null_Statement
(Sloc
(N
));
7763 elsif Nkind_In
(N
, N_Integer_Literal
, N_Real_Literal
) then
7765 -- No descendant fields need traversing
7769 elsif Nkind
(N
) = N_String_Literal
7770 and then Present
(Etype
(N
))
7771 and then Instantiating
7773 -- If the string is declared in an outer scope, the string_literal
7774 -- subtype created for it may have the wrong scope. Force reanalysis
7775 -- of the constant to generate a new itype in the proper context.
7777 Set_Etype
(New_N
, Empty
);
7778 Set_Analyzed
(New_N
, False);
7780 -- For the remaining nodes, copy their descendants recursively
7785 if Instantiating
and then Nkind
(N
) = N_Subprogram_Body
then
7786 Set_Generic_Parent
(Specification
(New_N
), N
);
7788 -- Should preserve Corresponding_Spec??? (12.3(14))
7792 -- Propagate dimensions if present, so that they are reflected in the
7795 if Nkind
(N
) in N_Has_Etype
7796 and then (Nkind
(N
) in N_Op
or else Is_Entity_Name
(N
))
7797 and then Present
(Etype
(N
))
7798 and then Is_Floating_Point_Type
(Etype
(N
))
7799 and then Has_Dimension_System
(Etype
(N
))
7801 Copy_Dimensions
(N
, New_N
);
7805 end Copy_Generic_Node
;
7807 ----------------------------
7808 -- Denotes_Formal_Package --
7809 ----------------------------
7811 function Denotes_Formal_Package
7813 On_Exit
: Boolean := False;
7814 Instance
: Entity_Id
:= Empty
) return Boolean
7817 Scop
: constant Entity_Id
:= Scope
(Pack
);
7820 function Is_Actual_Of_Previous_Formal
(P
: Entity_Id
) return Boolean;
7821 -- The package in question may be an actual for a previous formal
7822 -- package P of the current instance, so examine its actuals as well.
7823 -- This must be recursive over other formal packages.
7825 ----------------------------------
7826 -- Is_Actual_Of_Previous_Formal --
7827 ----------------------------------
7829 function Is_Actual_Of_Previous_Formal
(P
: Entity_Id
) return Boolean is
7833 E1
:= First_Entity
(P
);
7834 while Present
(E1
) and then E1
/= Instance
loop
7835 if Ekind
(E1
) = E_Package
7836 and then Nkind
(Parent
(E1
)) = N_Package_Renaming_Declaration
7838 if Renamed_Object
(E1
) = Pack
then
7841 elsif E1
= P
or else Renamed_Object
(E1
) = P
then
7844 elsif Is_Actual_Of_Previous_Formal
(E1
) then
7853 end Is_Actual_Of_Previous_Formal
;
7855 -- Start of processing for Denotes_Formal_Package
7861 (Instance_Envs
.Last
).Instantiated_Parent
.Act_Id
;
7863 Par
:= Current_Instantiated_Parent
.Act_Id
;
7866 if Ekind
(Scop
) = E_Generic_Package
7867 or else Nkind
(Unit_Declaration_Node
(Scop
)) =
7868 N_Generic_Subprogram_Declaration
7872 elsif Nkind
(Original_Node
(Unit_Declaration_Node
(Pack
))) =
7873 N_Formal_Package_Declaration
7881 -- Check whether this package is associated with a formal package of
7882 -- the enclosing instantiation. Iterate over the list of renamings.
7884 E
:= First_Entity
(Par
);
7885 while Present
(E
) loop
7886 if Ekind
(E
) /= E_Package
7887 or else Nkind
(Parent
(E
)) /= N_Package_Renaming_Declaration
7891 elsif Renamed_Object
(E
) = Par
then
7894 elsif Renamed_Object
(E
) = Pack
then
7897 elsif Is_Actual_Of_Previous_Formal
(E
) then
7907 end Denotes_Formal_Package
;
7913 procedure End_Generic
is
7915 -- ??? More things could be factored out in this routine. Should
7916 -- probably be done at a later stage.
7918 Inside_A_Generic
:= Generic_Flags
.Table
(Generic_Flags
.Last
);
7919 Generic_Flags
.Decrement_Last
;
7921 Expander_Mode_Restore
;
7928 function Earlier
(N1
, N2
: Node_Id
) return Boolean is
7929 procedure Find_Depth
(P
: in out Node_Id
; D
: in out Integer);
7930 -- Find distance from given node to enclosing compilation unit
7936 procedure Find_Depth
(P
: in out Node_Id
; D
: in out Integer) is
7939 and then Nkind
(P
) /= N_Compilation_Unit
7941 P
:= True_Parent
(P
);
7946 -- Local declarations
7955 -- Start of processing for Earlier
7958 Find_Depth
(P1
, D1
);
7959 Find_Depth
(P2
, D2
);
7969 P1
:= True_Parent
(P1
);
7974 P2
:= True_Parent
(P2
);
7978 -- At this point P1 and P2 are at the same distance from the root.
7979 -- We examine their parents until we find a common declarative list.
7980 -- If we reach the root, N1 and N2 do not descend from the same
7981 -- declarative list (e.g. one is nested in the declarative part and
7982 -- the other is in a block in the statement part) and the earlier
7983 -- one is already frozen.
7985 while not Is_List_Member
(P1
)
7986 or else not Is_List_Member
(P2
)
7987 or else List_Containing
(P1
) /= List_Containing
(P2
)
7989 P1
:= True_Parent
(P1
);
7990 P2
:= True_Parent
(P2
);
7992 if Nkind
(Parent
(P1
)) = N_Subunit
then
7993 P1
:= Corresponding_Stub
(Parent
(P1
));
7996 if Nkind
(Parent
(P2
)) = N_Subunit
then
7997 P2
:= Corresponding_Stub
(Parent
(P2
));
8005 -- Expanded code usually shares the source location of the original
8006 -- construct it was generated for. This however may not necessarily
8007 -- reflect the true location of the code within the tree.
8009 -- Before comparing the slocs of the two nodes, make sure that we are
8010 -- working with correct source locations. Assume that P1 is to the left
8011 -- of P2. If either one does not come from source, traverse the common
8012 -- list heading towards the other node and locate the first source
8016 -- ----+===+===+--------------+===+===+----
8017 -- expanded code expanded code
8019 if not Comes_From_Source
(P1
) then
8020 while Present
(P1
) loop
8022 -- Neither P2 nor a source statement were located during the
8023 -- search. If we reach the end of the list, then P1 does not
8024 -- occur earlier than P2.
8027 -- start --- P2 ----- P1 --- end
8029 if No
(Next
(P1
)) then
8032 -- We encounter P2 while going to the right of the list. This
8033 -- means that P1 does indeed appear earlier.
8036 -- start --- P1 ===== P2 --- end
8037 -- expanded code in between
8042 -- No need to look any further since we have located a source
8045 elsif Comes_From_Source
(P1
) then
8055 if not Comes_From_Source
(P2
) then
8056 while Present
(P2
) loop
8058 -- Neither P1 nor a source statement were located during the
8059 -- search. If we reach the start of the list, then P1 does not
8060 -- occur earlier than P2.
8063 -- start --- P2 --- P1 --- end
8065 if No
(Prev
(P2
)) then
8068 -- We encounter P1 while going to the left of the list. This
8069 -- means that P1 does indeed appear earlier.
8072 -- start --- P1 ===== P2 --- end
8073 -- expanded code in between
8078 -- No need to look any further since we have located a source
8081 elsif Comes_From_Source
(P2
) then
8091 -- At this point either both nodes came from source or we approximated
8092 -- their source locations through neighboring source statements.
8094 T1
:= Top_Level_Location
(Sloc
(P1
));
8095 T2
:= Top_Level_Location
(Sloc
(P2
));
8097 -- When two nodes come from the same instance, they have identical top
8098 -- level locations. To determine proper relation within the tree, check
8099 -- their locations within the template.
8102 return Sloc
(P1
) < Sloc
(P2
);
8104 -- The two nodes either come from unrelated instances or do not come
8105 -- from instantiated code at all.
8112 ----------------------
8113 -- Find_Actual_Type --
8114 ----------------------
8116 function Find_Actual_Type
8118 Gen_Type
: Entity_Id
) return Entity_Id
8120 Gen_Scope
: constant Entity_Id
:= Scope
(Gen_Type
);
8124 -- Special processing only applies to child units
8126 if not Is_Child_Unit
(Gen_Scope
) then
8127 return Get_Instance_Of
(Typ
);
8129 -- If designated or component type is itself a formal of the child unit,
8130 -- its instance is available.
8132 elsif Scope
(Typ
) = Gen_Scope
then
8133 return Get_Instance_Of
(Typ
);
8135 -- If the array or access type is not declared in the parent unit,
8136 -- no special processing needed.
8138 elsif not Is_Generic_Type
(Typ
)
8139 and then Scope
(Gen_Scope
) /= Scope
(Typ
)
8141 return Get_Instance_Of
(Typ
);
8143 -- Otherwise, retrieve designated or component type by visibility
8146 T
:= Current_Entity
(Typ
);
8147 while Present
(T
) loop
8148 if In_Open_Scopes
(Scope
(T
)) then
8150 elsif Is_Generic_Actual_Type
(T
) then
8159 end Find_Actual_Type
;
8161 ----------------------------
8162 -- Freeze_Subprogram_Body --
8163 ----------------------------
8165 procedure Freeze_Subprogram_Body
8166 (Inst_Node
: Node_Id
;
8168 Pack_Id
: Entity_Id
)
8170 Gen_Unit
: constant Entity_Id
:= Get_Generic_Entity
(Inst_Node
);
8171 Par
: constant Entity_Id
:= Scope
(Gen_Unit
);
8177 function Enclosing_Package_Body
(N
: Node_Id
) return Node_Id
;
8178 -- Find innermost package body that encloses the given node, and which
8179 -- is not a compilation unit. Freeze nodes for the instance, or for its
8180 -- enclosing body, may be inserted after the enclosing_body of the
8181 -- generic unit. Used to determine proper placement of freeze node for
8182 -- both package and subprogram instances.
8184 function Package_Freeze_Node
(B
: Node_Id
) return Node_Id
;
8185 -- Find entity for given package body, and locate or create a freeze
8188 ----------------------------
8189 -- Enclosing_Package_Body --
8190 ----------------------------
8192 function Enclosing_Package_Body
(N
: Node_Id
) return Node_Id
is
8198 and then Nkind
(Parent
(P
)) /= N_Compilation_Unit
8200 if Nkind
(P
) = N_Package_Body
then
8201 if Nkind
(Parent
(P
)) = N_Subunit
then
8202 return Corresponding_Stub
(Parent
(P
));
8208 P
:= True_Parent
(P
);
8212 end Enclosing_Package_Body
;
8214 -------------------------
8215 -- Package_Freeze_Node --
8216 -------------------------
8218 function Package_Freeze_Node
(B
: Node_Id
) return Node_Id
is
8222 if Nkind
(B
) = N_Package_Body
then
8223 Id
:= Corresponding_Spec
(B
);
8224 else pragma Assert
(Nkind
(B
) = N_Package_Body_Stub
);
8225 Id
:= Corresponding_Spec
(Proper_Body
(Unit
(Library_Unit
(B
))));
8228 Ensure_Freeze_Node
(Id
);
8229 return Freeze_Node
(Id
);
8230 end Package_Freeze_Node
;
8232 -- Start of processing for Freeze_Subprogram_Body
8235 -- If the instance and the generic body appear within the same unit, and
8236 -- the instance precedes the generic, the freeze node for the instance
8237 -- must appear after that of the generic. If the generic is nested
8238 -- within another instance I2, then current instance must be frozen
8239 -- after I2. In both cases, the freeze nodes are those of enclosing
8240 -- packages. Otherwise, the freeze node is placed at the end of the
8241 -- current declarative part.
8243 Enc_G
:= Enclosing_Package_Body
(Gen_Body
);
8244 Enc_I
:= Enclosing_Package_Body
(Inst_Node
);
8245 Ensure_Freeze_Node
(Pack_Id
);
8246 F_Node
:= Freeze_Node
(Pack_Id
);
8248 if Is_Generic_Instance
(Par
)
8249 and then Present
(Freeze_Node
(Par
))
8250 and then In_Same_Declarative_Part
(Freeze_Node
(Par
), Inst_Node
)
8252 -- The parent was a premature instantiation. Insert freeze node at
8253 -- the end the current declarative part.
8255 if ABE_Is_Certain
(Get_Package_Instantiation_Node
(Par
)) then
8256 Insert_Freeze_Node_For_Instance
(Inst_Node
, F_Node
);
8258 -- Handle the following case:
8260 -- package Parent_Inst is new ...
8263 -- procedure P ... -- this body freezes Parent_Inst
8265 -- package Inst is new ...
8267 -- In this particular scenario, the freeze node for Inst must be
8268 -- inserted in the same manner as that of Parent_Inst - before the
8269 -- next source body or at the end of the declarative list (body not
8270 -- available). If body P did not exist and Parent_Inst was frozen
8271 -- after Inst, either by a body following Inst or at the end of the
8272 -- declarative region, the freeze node for Inst must be inserted
8273 -- after that of Parent_Inst. This relation is established by
8274 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8276 elsif List_Containing
(Get_Package_Instantiation_Node
(Par
)) =
8277 List_Containing
(Inst_Node
)
8278 and then Sloc
(Freeze_Node
(Par
)) < Sloc
(Inst_Node
)
8280 Insert_Freeze_Node_For_Instance
(Inst_Node
, F_Node
);
8283 Insert_After
(Freeze_Node
(Par
), F_Node
);
8286 -- The body enclosing the instance should be frozen after the body that
8287 -- includes the generic, because the body of the instance may make
8288 -- references to entities therein. If the two are not in the same
8289 -- declarative part, or if the one enclosing the instance is frozen
8290 -- already, freeze the instance at the end of the current declarative
8293 elsif Is_Generic_Instance
(Par
)
8294 and then Present
(Freeze_Node
(Par
))
8295 and then Present
(Enc_I
)
8297 if In_Same_Declarative_Part
(Freeze_Node
(Par
), Enc_I
)
8299 (Nkind
(Enc_I
) = N_Package_Body
8301 In_Same_Declarative_Part
(Freeze_Node
(Par
), Parent
(Enc_I
)))
8303 -- The enclosing package may contain several instances. Rather
8304 -- than computing the earliest point at which to insert its freeze
8305 -- node, we place it at the end of the declarative part of the
8306 -- parent of the generic.
8308 Insert_Freeze_Node_For_Instance
8309 (Freeze_Node
(Par
), Package_Freeze_Node
(Enc_I
));
8312 Insert_Freeze_Node_For_Instance
(Inst_Node
, F_Node
);
8314 elsif Present
(Enc_G
)
8315 and then Present
(Enc_I
)
8316 and then Enc_G
/= Enc_I
8317 and then Earlier
(Inst_Node
, Gen_Body
)
8319 if Nkind
(Enc_G
) = N_Package_Body
then
8321 Corresponding_Spec
(Enc_G
);
8322 else pragma Assert
(Nkind
(Enc_G
) = N_Package_Body_Stub
);
8324 Corresponding_Spec
(Proper_Body
(Unit
(Library_Unit
(Enc_G
))));
8327 -- Freeze package that encloses instance, and place node after the
8328 -- package that encloses generic. If enclosing package is already
8329 -- frozen we have to assume it is at the proper place. This may be a
8330 -- potential ABE that requires dynamic checking. Do not add a freeze
8331 -- node if the package that encloses the generic is inside the body
8332 -- that encloses the instance, because the freeze node would be in
8333 -- the wrong scope. Additional contortions needed if the bodies are
8334 -- within a subunit.
8337 Enclosing_Body
: Node_Id
;
8340 if Nkind
(Enc_I
) = N_Package_Body_Stub
then
8341 Enclosing_Body
:= Proper_Body
(Unit
(Library_Unit
(Enc_I
)));
8343 Enclosing_Body
:= Enc_I
;
8346 if Parent
(List_Containing
(Enc_G
)) /= Enclosing_Body
then
8347 Insert_Freeze_Node_For_Instance
8348 (Enc_G
, Package_Freeze_Node
(Enc_I
));
8352 -- Freeze enclosing subunit before instance
8354 Ensure_Freeze_Node
(E_G_Id
);
8356 if not Is_List_Member
(Freeze_Node
(E_G_Id
)) then
8357 Insert_After
(Enc_G
, Freeze_Node
(E_G_Id
));
8360 Insert_Freeze_Node_For_Instance
(Inst_Node
, F_Node
);
8363 -- If none of the above, insert freeze node at the end of the current
8364 -- declarative part.
8366 Insert_Freeze_Node_For_Instance
(Inst_Node
, F_Node
);
8368 end Freeze_Subprogram_Body
;
8374 function Get_Gen_Id
(E
: Assoc_Ptr
) return Entity_Id
is
8376 return Generic_Renamings
.Table
(E
).Gen_Id
;
8379 ---------------------
8380 -- Get_Instance_Of --
8381 ---------------------
8383 function Get_Instance_Of
(A
: Entity_Id
) return Entity_Id
is
8384 Res
: constant Assoc_Ptr
:= Generic_Renamings_HTable
.Get
(A
);
8387 if Res
/= Assoc_Null
then
8388 return Generic_Renamings
.Table
(Res
).Act_Id
;
8391 -- On exit, entity is not instantiated: not a generic parameter, or
8392 -- else parameter of an inner generic unit.
8396 end Get_Instance_Of
;
8398 ------------------------------------
8399 -- Get_Package_Instantiation_Node --
8400 ------------------------------------
8402 function Get_Package_Instantiation_Node
(A
: Entity_Id
) return Node_Id
is
8403 Decl
: Node_Id
:= Unit_Declaration_Node
(A
);
8407 -- If the Package_Instantiation attribute has been set on the package
8408 -- entity, then use it directly when it (or its Original_Node) refers
8409 -- to an N_Package_Instantiation node. In principle it should be
8410 -- possible to have this field set in all cases, which should be
8411 -- investigated, and would allow this function to be significantly
8414 Inst
:= Package_Instantiation
(A
);
8416 if Present
(Inst
) then
8417 if Nkind
(Inst
) = N_Package_Instantiation
then
8420 elsif Nkind
(Original_Node
(Inst
)) = N_Package_Instantiation
then
8421 return Original_Node
(Inst
);
8425 -- If the instantiation is a compilation unit that does not need body
8426 -- then the instantiation node has been rewritten as a package
8427 -- declaration for the instance, and we return the original node.
8429 -- If it is a compilation unit and the instance node has not been
8430 -- rewritten, then it is still the unit of the compilation. Finally, if
8431 -- a body is present, this is a parent of the main unit whose body has
8432 -- been compiled for inlining purposes, and the instantiation node has
8433 -- been rewritten with the instance body.
8435 -- Otherwise the instantiation node appears after the declaration. If
8436 -- the entity is a formal package, the declaration may have been
8437 -- rewritten as a generic declaration (in the case of a formal with box)
8438 -- or left as a formal package declaration if it has actuals, and is
8439 -- found with a forward search.
8441 if Nkind
(Parent
(Decl
)) = N_Compilation_Unit
then
8442 if Nkind
(Decl
) = N_Package_Declaration
8443 and then Present
(Corresponding_Body
(Decl
))
8445 Decl
:= Unit_Declaration_Node
(Corresponding_Body
(Decl
));
8448 if Nkind
(Original_Node
(Decl
)) = N_Package_Instantiation
then
8449 return Original_Node
(Decl
);
8451 return Unit
(Parent
(Decl
));
8454 elsif Nkind
(Decl
) = N_Package_Declaration
8455 and then Nkind
(Original_Node
(Decl
)) = N_Formal_Package_Declaration
8457 return Original_Node
(Decl
);
8460 Inst
:= Next
(Decl
);
8461 while not Nkind_In
(Inst
, N_Package_Instantiation
,
8462 N_Formal_Package_Declaration
)
8469 end Get_Package_Instantiation_Node
;
8471 ------------------------
8472 -- Has_Been_Exchanged --
8473 ------------------------
8475 function Has_Been_Exchanged
(E
: Entity_Id
) return Boolean is
8479 Next
:= First_Elmt
(Exchanged_Views
);
8480 while Present
(Next
) loop
8481 if Full_View
(Node
(Next
)) = E
then
8489 end Has_Been_Exchanged
;
8495 function Hash
(F
: Entity_Id
) return HTable_Range
is
8497 return HTable_Range
(F
mod HTable_Size
);
8500 ------------------------
8501 -- Hide_Current_Scope --
8502 ------------------------
8504 procedure Hide_Current_Scope
is
8505 C
: constant Entity_Id
:= Current_Scope
;
8509 Set_Is_Hidden_Open_Scope
(C
);
8511 E
:= First_Entity
(C
);
8512 while Present
(E
) loop
8513 if Is_Immediately_Visible
(E
) then
8514 Set_Is_Immediately_Visible
(E
, False);
8515 Append_Elmt
(E
, Hidden_Entities
);
8521 -- Make the scope name invisible as well. This is necessary, but might
8522 -- conflict with calls to Rtsfind later on, in case the scope is a
8523 -- predefined one. There is no clean solution to this problem, so for
8524 -- now we depend on the user not redefining Standard itself in one of
8525 -- the parent units.
8527 if Is_Immediately_Visible
(C
) and then C
/= Standard_Standard
then
8528 Set_Is_Immediately_Visible
(C
, False);
8529 Append_Elmt
(C
, Hidden_Entities
);
8532 end Hide_Current_Scope
;
8538 procedure Init_Env
is
8539 Saved
: Instance_Env
;
8542 Saved
.Instantiated_Parent
:= Current_Instantiated_Parent
;
8543 Saved
.Exchanged_Views
:= Exchanged_Views
;
8544 Saved
.Hidden_Entities
:= Hidden_Entities
;
8545 Saved
.Current_Sem_Unit
:= Current_Sem_Unit
;
8546 Saved
.Parent_Unit_Visible
:= Parent_Unit_Visible
;
8547 Saved
.Instance_Parent_Unit
:= Instance_Parent_Unit
;
8549 -- Save configuration switches. These may be reset if the unit is a
8550 -- predefined unit, and the current mode is not Ada 2005.
8552 Save_Opt_Config_Switches
(Saved
.Switches
);
8554 Instance_Envs
.Append
(Saved
);
8556 Exchanged_Views
:= New_Elmt_List
;
8557 Hidden_Entities
:= New_Elmt_List
;
8559 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8560 -- this is set properly in Set_Instance_Env.
8562 Current_Instantiated_Parent
:=
8563 (Current_Scope
, Current_Scope
, Assoc_Null
);
8566 ------------------------------
8567 -- In_Same_Declarative_Part --
8568 ------------------------------
8570 function In_Same_Declarative_Part
8572 Inst
: Node_Id
) return Boolean
8574 Decls
: constant Node_Id
:= Parent
(F_Node
);
8578 Nod
:= Parent
(Inst
);
8579 while Present
(Nod
) loop
8583 elsif Nkind_In
(Nod
, N_Subprogram_Body
,
8585 N_Package_Declaration
,
8592 elsif Nkind
(Nod
) = N_Subunit
then
8593 Nod
:= Corresponding_Stub
(Nod
);
8595 elsif Nkind
(Nod
) = N_Compilation_Unit
then
8599 Nod
:= Parent
(Nod
);
8604 end In_Same_Declarative_Part
;
8606 ---------------------
8607 -- In_Main_Context --
8608 ---------------------
8610 function In_Main_Context
(E
: Entity_Id
) return Boolean is
8616 if not Is_Compilation_Unit
(E
)
8617 or else Ekind
(E
) /= E_Package
8618 or else In_Private_Part
(E
)
8623 Context
:= Context_Items
(Cunit
(Main_Unit
));
8625 Clause
:= First
(Context
);
8626 while Present
(Clause
) loop
8627 if Nkind
(Clause
) = N_With_Clause
then
8628 Nam
:= Name
(Clause
);
8630 -- If the current scope is part of the context of the main unit,
8631 -- analysis of the corresponding with_clause is not complete, and
8632 -- the entity is not set. We use the Chars field directly, which
8633 -- might produce false positives in rare cases, but guarantees
8634 -- that we produce all the instance bodies we will need.
8636 if (Is_Entity_Name
(Nam
) and then Chars
(Nam
) = Chars
(E
))
8637 or else (Nkind
(Nam
) = N_Selected_Component
8638 and then Chars
(Selector_Name
(Nam
)) = Chars
(E
))
8648 end In_Main_Context
;
8650 ---------------------
8651 -- Inherit_Context --
8652 ---------------------
8654 procedure Inherit_Context
(Gen_Decl
: Node_Id
; Inst
: Node_Id
) is
8655 Current_Context
: List_Id
;
8656 Current_Unit
: Node_Id
;
8665 if Nkind
(Parent
(Gen_Decl
)) = N_Compilation_Unit
then
8667 -- The inherited context is attached to the enclosing compilation
8668 -- unit. This is either the main unit, or the declaration for the
8669 -- main unit (in case the instantiation appears within the package
8670 -- declaration and the main unit is its body).
8672 Current_Unit
:= Parent
(Inst
);
8673 while Present
(Current_Unit
)
8674 and then Nkind
(Current_Unit
) /= N_Compilation_Unit
8676 Current_Unit
:= Parent
(Current_Unit
);
8679 Current_Context
:= Context_Items
(Current_Unit
);
8681 Item
:= First
(Context_Items
(Parent
(Gen_Decl
)));
8682 while Present
(Item
) loop
8683 if Nkind
(Item
) = N_With_Clause
then
8684 Lib_Unit
:= Library_Unit
(Item
);
8686 -- Take care to prevent direct cyclic with's
8688 if Lib_Unit
/= Current_Unit
then
8690 -- Do not add a unit if it is already in the context
8692 Clause
:= First
(Current_Context
);
8694 while Present
(Clause
) loop
8695 if Nkind
(Clause
) = N_With_Clause
and then
8696 Library_Unit
(Clause
) = Lib_Unit
8706 New_I
:= New_Copy
(Item
);
8707 Set_Implicit_With
(New_I
, True);
8708 Set_Implicit_With_From_Instantiation
(New_I
, True);
8709 Append
(New_I
, Current_Context
);
8717 end Inherit_Context
;
8723 procedure Initialize
is
8725 Generic_Renamings
.Init
;
8728 Generic_Renamings_HTable
.Reset
;
8729 Circularity_Detected
:= False;
8730 Exchanged_Views
:= No_Elist
;
8731 Hidden_Entities
:= No_Elist
;
8734 -------------------------------------
8735 -- Insert_Freeze_Node_For_Instance --
8736 -------------------------------------
8738 procedure Insert_Freeze_Node_For_Instance
8747 function Enclosing_Body
(N
: Node_Id
) return Node_Id
;
8748 -- Find enclosing package or subprogram body, if any. Freeze node may
8749 -- be placed at end of current declarative list if previous instance
8750 -- and current one have different enclosing bodies.
8752 function Previous_Instance
(Gen
: Entity_Id
) return Entity_Id
;
8753 -- Find the local instance, if any, that declares the generic that is
8754 -- being instantiated. If present, the freeze node for this instance
8755 -- must follow the freeze node for the previous instance.
8757 --------------------
8758 -- Enclosing_Body --
8759 --------------------
8761 function Enclosing_Body
(N
: Node_Id
) return Node_Id
is
8767 and then Nkind
(Parent
(P
)) /= N_Compilation_Unit
8769 if Nkind_In
(P
, N_Package_Body
, N_Subprogram_Body
) then
8770 if Nkind
(Parent
(P
)) = N_Subunit
then
8771 return Corresponding_Stub
(Parent
(P
));
8777 P
:= True_Parent
(P
);
8783 -----------------------
8784 -- Previous_Instance --
8785 -----------------------
8787 function Previous_Instance
(Gen
: Entity_Id
) return Entity_Id
is
8792 while Present
(S
) and then S
/= Standard_Standard
loop
8793 if Is_Generic_Instance
(S
)
8794 and then In_Same_Source_Unit
(S
, N
)
8803 end Previous_Instance
;
8805 -- Start of processing for Insert_Freeze_Node_For_Instance
8808 if not Is_List_Member
(F_Node
) then
8810 Decls
:= List_Containing
(N
);
8811 Inst
:= Entity
(F_Node
);
8812 Par_N
:= Parent
(Decls
);
8814 -- When processing a subprogram instantiation, utilize the actual
8815 -- subprogram instantiation rather than its package wrapper as it
8816 -- carries all the context information.
8818 if Is_Wrapper_Package
(Inst
) then
8819 Inst
:= Related_Instance
(Inst
);
8822 -- If this is a package instance, check whether the generic is
8823 -- declared in a previous instance and the current instance is
8824 -- not within the previous one.
8826 if Present
(Generic_Parent
(Parent
(Inst
)))
8827 and then Is_In_Main_Unit
(N
)
8830 Enclosing_N
: constant Node_Id
:= Enclosing_Body
(N
);
8831 Par_I
: constant Entity_Id
:=
8833 (Generic_Parent
(Parent
(Inst
)));
8838 and then Earlier
(N
, Freeze_Node
(Par_I
))
8840 Scop
:= Scope
(Inst
);
8842 -- If the current instance is within the one that contains
8843 -- the generic, the freeze node for the current one must
8844 -- appear in the current declarative part. Ditto, if the
8845 -- current instance is within another package instance or
8846 -- within a body that does not enclose the current instance.
8847 -- In these three cases the freeze node of the previous
8848 -- instance is not relevant.
8850 while Present
(Scop
) and then Scop
/= Standard_Standard
loop
8851 exit when Scop
= Par_I
8853 (Is_Generic_Instance
(Scop
)
8854 and then Scope_Depth
(Scop
) > Scope_Depth
(Par_I
));
8855 Scop
:= Scope
(Scop
);
8858 -- Previous instance encloses current instance
8860 if Scop
= Par_I
then
8863 -- If the next node is a source body we must freeze in
8864 -- the current scope as well.
8866 elsif Present
(Next
(N
))
8867 and then Nkind_In
(Next
(N
), N_Subprogram_Body
,
8869 and then Comes_From_Source
(Next
(N
))
8873 -- Current instance is within an unrelated instance
8875 elsif Is_Generic_Instance
(Scop
) then
8878 -- Current instance is within an unrelated body
8880 elsif Present
(Enclosing_N
)
8881 and then Enclosing_N
/= Enclosing_Body
(Par_I
)
8886 Insert_After
(Freeze_Node
(Par_I
), F_Node
);
8893 -- When the instantiation occurs in a package declaration, append the
8894 -- freeze node to the private declarations (if any).
8896 if Nkind
(Par_N
) = N_Package_Specification
8897 and then Decls
= Visible_Declarations
(Par_N
)
8898 and then Present
(Private_Declarations
(Par_N
))
8899 and then not Is_Empty_List
(Private_Declarations
(Par_N
))
8901 Decls
:= Private_Declarations
(Par_N
);
8902 Decl
:= First
(Decls
);
8905 -- Determine the proper freeze point of a package instantiation. We
8906 -- adhere to the general rule of a package or subprogram body causing
8907 -- freezing of anything before it in the same declarative region. In
8908 -- this case, the proper freeze point of a package instantiation is
8909 -- before the first source body which follows, or before a stub. This
8910 -- ensures that entities coming from the instance are already frozen
8911 -- and usable in source bodies.
8913 if Nkind
(Par_N
) /= N_Package_Declaration
8914 and then Ekind
(Inst
) = E_Package
8915 and then Is_Generic_Instance
(Inst
)
8917 not In_Same_Source_Unit
(Generic_Parent
(Parent
(Inst
)), Inst
)
8919 while Present
(Decl
) loop
8920 if (Nkind
(Decl
) in N_Unit_Body
8922 Nkind
(Decl
) in N_Body_Stub
)
8923 and then Comes_From_Source
(Decl
)
8925 Insert_Before
(Decl
, F_Node
);
8933 -- In a package declaration, or if no previous body, insert at end
8936 Set_Sloc
(F_Node
, Sloc
(Last
(Decls
)));
8937 Insert_After
(Last
(Decls
), F_Node
);
8939 end Insert_Freeze_Node_For_Instance
;
8945 procedure Install_Body
8946 (Act_Body
: Node_Id
;
8951 function In_Same_Scope
(Gen_Id
, Act_Id
: Node_Id
) return Boolean;
8952 -- Check if the generic definition and the instantiation come from
8953 -- a common scope, in which case the instance must be frozen after
8954 -- the generic body.
8956 function True_Sloc
(N
, Act_Unit
: Node_Id
) return Source_Ptr
;
8957 -- If the instance is nested inside a generic unit, the Sloc of the
8958 -- instance indicates the place of the original definition, not the
8959 -- point of the current enclosing instance. Pending a better usage of
8960 -- Slocs to indicate instantiation places, we determine the place of
8961 -- origin of a node by finding the maximum sloc of any ancestor node.
8962 -- Why is this not equivalent to Top_Level_Location ???
8968 function In_Same_Scope
(Gen_Id
, Act_Id
: Node_Id
) return Boolean is
8969 Act_Scop
: Entity_Id
:= Scope
(Act_Id
);
8970 Gen_Scop
: Entity_Id
:= Scope
(Gen_Id
);
8973 while Act_Scop
/= Standard_Standard
8974 and then Gen_Scop
/= Standard_Standard
8976 if Act_Scop
= Gen_Scop
then
8980 Act_Scop
:= Scope
(Act_Scop
);
8981 Gen_Scop
:= Scope
(Gen_Scop
);
8991 function True_Sloc
(N
, Act_Unit
: Node_Id
) return Source_Ptr
is
8998 while Present
(N1
) and then N1
/= Act_Unit
loop
8999 if Sloc
(N1
) > Res
then
9009 Act_Id
: constant Entity_Id
:= Corresponding_Spec
(Act_Body
);
9010 Act_Unit
: constant Node_Id
:= Unit
(Cunit
(Get_Source_Unit
(N
)));
9011 Gen_Id
: constant Entity_Id
:= Corresponding_Spec
(Gen_Body
);
9012 Par
: constant Entity_Id
:= Scope
(Gen_Id
);
9013 Gen_Unit
: constant Node_Id
:=
9014 Unit
(Cunit
(Get_Source_Unit
(Gen_Decl
)));
9016 Body_Unit
: Node_Id
;
9018 Must_Delay
: Boolean;
9019 Orig_Body
: Node_Id
:= Gen_Body
;
9021 -- Start of processing for Install_Body
9024 -- Handle first the case of an instance with incomplete actual types.
9025 -- The instance body cannot be placed after the declaration because
9026 -- full views have not been seen yet. Any use of the non-limited views
9027 -- in the instance body requires the presence of a regular with_clause
9028 -- in the enclosing unit, and will fail if this with_clause is missing.
9029 -- We place the instance body at the beginning of the enclosing body,
9030 -- which is the unit being compiled. The freeze node for the instance
9031 -- is then placed after the instance body.
9033 if not Is_Empty_Elmt_List
(Incomplete_Actuals
(Act_Id
))
9034 and then Expander_Active
9035 and then Ekind
(Scope
(Act_Id
)) = E_Package
9038 Scop
: constant Entity_Id
:= Scope
(Act_Id
);
9039 Body_Id
: constant Node_Id
:=
9040 Corresponding_Body
(Unit_Declaration_Node
(Scop
));
9043 Ensure_Freeze_Node
(Act_Id
);
9044 F_Node
:= Freeze_Node
(Act_Id
);
9045 if Present
(Body_Id
) then
9046 Set_Is_Frozen
(Act_Id
, False);
9047 Prepend
(Act_Body
, Declarations
(Parent
(Body_Id
)));
9048 if Is_List_Member
(F_Node
) then
9052 Insert_After
(Act_Body
, F_Node
);
9058 -- If the body is a subunit, the freeze point is the corresponding stub
9059 -- in the current compilation, not the subunit itself.
9061 if Nkind
(Parent
(Gen_Body
)) = N_Subunit
then
9062 Orig_Body
:= Corresponding_Stub
(Parent
(Gen_Body
));
9064 Orig_Body
:= Gen_Body
;
9067 Body_Unit
:= Unit
(Cunit
(Get_Source_Unit
(Orig_Body
)));
9069 -- If the instantiation and the generic definition appear in the same
9070 -- package declaration, this is an early instantiation. If they appear
9071 -- in the same declarative part, it is an early instantiation only if
9072 -- the generic body appears textually later, and the generic body is
9073 -- also in the main unit.
9075 -- If instance is nested within a subprogram, and the generic body
9076 -- is not, the instance is delayed because the enclosing body is. If
9077 -- instance and body are within the same scope, or the same subprogram
9078 -- body, indicate explicitly that the instance is delayed.
9081 (Gen_Unit
= Act_Unit
9082 and then (Nkind_In
(Gen_Unit
, N_Generic_Package_Declaration
,
9083 N_Package_Declaration
)
9084 or else (Gen_Unit
= Body_Unit
9085 and then True_Sloc
(N
, Act_Unit
)
9086 < Sloc
(Orig_Body
)))
9087 and then Is_In_Main_Unit
(Original_Node
(Gen_Unit
))
9088 and then In_Same_Scope
(Gen_Id
, Act_Id
));
9090 -- If this is an early instantiation, the freeze node is placed after
9091 -- the generic body. Otherwise, if the generic appears in an instance,
9092 -- we cannot freeze the current instance until the outer one is frozen.
9093 -- This is only relevant if the current instance is nested within some
9094 -- inner scope not itself within the outer instance. If this scope is
9095 -- a package body in the same declarative part as the outer instance,
9096 -- then that body needs to be frozen after the outer instance. Finally,
9097 -- if no delay is needed, we place the freeze node at the end of the
9098 -- current declarative part.
9100 if Expander_Active
then
9101 Ensure_Freeze_Node
(Act_Id
);
9102 F_Node
:= Freeze_Node
(Act_Id
);
9105 Insert_After
(Orig_Body
, F_Node
);
9107 elsif Is_Generic_Instance
(Par
)
9108 and then Present
(Freeze_Node
(Par
))
9109 and then Scope
(Act_Id
) /= Par
9111 -- Freeze instance of inner generic after instance of enclosing
9114 if In_Same_Declarative_Part
(Freeze_Node
(Par
), N
) then
9116 -- Handle the following case:
9118 -- package Parent_Inst is new ...
9121 -- procedure P ... -- this body freezes Parent_Inst
9123 -- package Inst is new ...
9125 -- In this particular scenario, the freeze node for Inst must
9126 -- be inserted in the same manner as that of Parent_Inst,
9127 -- before the next source body or at the end of the declarative
9128 -- list (body not available). If body P did not exist and
9129 -- Parent_Inst was frozen after Inst, either by a body
9130 -- following Inst or at the end of the declarative region,
9131 -- the freeze node for Inst must be inserted after that of
9132 -- Parent_Inst. This relation is established by comparing
9133 -- the Slocs of Parent_Inst freeze node and Inst.
9135 if List_Containing
(Get_Package_Instantiation_Node
(Par
)) =
9137 and then Sloc
(Freeze_Node
(Par
)) < Sloc
(N
)
9139 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9141 Insert_After
(Freeze_Node
(Par
), F_Node
);
9144 -- Freeze package enclosing instance of inner generic after
9145 -- instance of enclosing generic.
9147 elsif Nkind_In
(Parent
(N
), N_Package_Body
, N_Subprogram_Body
)
9148 and then In_Same_Declarative_Part
(Freeze_Node
(Par
), Parent
(N
))
9151 Enclosing
: Entity_Id
;
9154 Enclosing
:= Corresponding_Spec
(Parent
(N
));
9156 if No
(Enclosing
) then
9157 Enclosing
:= Defining_Entity
(Parent
(N
));
9160 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9161 Ensure_Freeze_Node
(Enclosing
);
9163 if not Is_List_Member
(Freeze_Node
(Enclosing
)) then
9165 -- The enclosing context is a subunit, insert the freeze
9166 -- node after the stub.
9168 if Nkind
(Parent
(Parent
(N
))) = N_Subunit
then
9169 Insert_Freeze_Node_For_Instance
9170 (Corresponding_Stub
(Parent
(Parent
(N
))),
9171 Freeze_Node
(Enclosing
));
9173 -- The enclosing context is a package with a stub body
9174 -- which has already been replaced by the real body.
9175 -- Insert the freeze node after the actual body.
9177 elsif Ekind
(Enclosing
) = E_Package
9178 and then Present
(Body_Entity
(Enclosing
))
9179 and then Was_Originally_Stub
9180 (Parent
(Body_Entity
(Enclosing
)))
9182 Insert_Freeze_Node_For_Instance
9183 (Parent
(Body_Entity
(Enclosing
)),
9184 Freeze_Node
(Enclosing
));
9186 -- The parent instance has been frozen before the body of
9187 -- the enclosing package, insert the freeze node after
9190 elsif List_Containing
(Freeze_Node
(Par
)) =
9191 List_Containing
(Parent
(N
))
9192 and then Sloc
(Freeze_Node
(Par
)) < Sloc
(Parent
(N
))
9194 Insert_Freeze_Node_For_Instance
9195 (Parent
(N
), Freeze_Node
(Enclosing
));
9199 (Freeze_Node
(Par
), Freeze_Node
(Enclosing
));
9205 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9209 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9213 Set_Is_Frozen
(Act_Id
);
9214 Insert_Before
(N
, Act_Body
);
9215 Mark_Rewrite_Insertion
(Act_Body
);
9218 -----------------------------
9219 -- Install_Formal_Packages --
9220 -----------------------------
9222 procedure Install_Formal_Packages
(Par
: Entity_Id
) is
9225 Gen_E
: Entity_Id
:= Empty
;
9228 E
:= First_Entity
(Par
);
9230 -- If we are installing an instance parent, locate the formal packages
9231 -- of its generic parent.
9233 if Is_Generic_Instance
(Par
) then
9234 Gen
:= Generic_Parent
(Package_Specification
(Par
));
9235 Gen_E
:= First_Entity
(Gen
);
9238 while Present
(E
) loop
9239 if Ekind
(E
) = E_Package
9240 and then Nkind
(Parent
(E
)) = N_Package_Renaming_Declaration
9242 -- If this is the renaming for the parent instance, done
9244 if Renamed_Object
(E
) = Par
then
9247 -- The visibility of a formal of an enclosing generic is already
9250 elsif Denotes_Formal_Package
(E
) then
9253 elsif Present
(Associated_Formal_Package
(E
)) then
9254 Check_Generic_Actuals
(Renamed_Object
(E
), True);
9255 Set_Is_Hidden
(E
, False);
9257 -- Find formal package in generic unit that corresponds to
9258 -- (instance of) formal package in instance.
9260 while Present
(Gen_E
) and then Chars
(Gen_E
) /= Chars
(E
) loop
9261 Next_Entity
(Gen_E
);
9264 if Present
(Gen_E
) then
9265 Map_Formal_Package_Entities
(Gen_E
, E
);
9272 if Present
(Gen_E
) then
9273 Next_Entity
(Gen_E
);
9276 end Install_Formal_Packages
;
9278 --------------------
9279 -- Install_Parent --
9280 --------------------
9282 procedure Install_Parent
(P
: Entity_Id
; In_Body
: Boolean := False) is
9283 Ancestors
: constant Elist_Id
:= New_Elmt_List
;
9284 S
: constant Entity_Id
:= Current_Scope
;
9285 Inst_Par
: Entity_Id
;
9286 First_Par
: Entity_Id
;
9287 Inst_Node
: Node_Id
;
9288 Gen_Par
: Entity_Id
;
9289 First_Gen
: Entity_Id
;
9292 procedure Install_Noninstance_Specs
(Par
: Entity_Id
);
9293 -- Install the scopes of noninstance parent units ending with Par
9295 procedure Install_Spec
(Par
: Entity_Id
);
9296 -- The child unit is within the declarative part of the parent, so the
9297 -- declarations within the parent are immediately visible.
9299 -------------------------------
9300 -- Install_Noninstance_Specs --
9301 -------------------------------
9303 procedure Install_Noninstance_Specs
(Par
: Entity_Id
) is
9306 and then Par
/= Standard_Standard
9307 and then not In_Open_Scopes
(Par
)
9309 Install_Noninstance_Specs
(Scope
(Par
));
9312 end Install_Noninstance_Specs
;
9318 procedure Install_Spec
(Par
: Entity_Id
) is
9319 Spec
: constant Node_Id
:= Package_Specification
(Par
);
9322 -- If this parent of the child instance is a top-level unit,
9323 -- then record the unit and its visibility for later resetting in
9324 -- Remove_Parent. We exclude units that are generic instances, as we
9325 -- only want to record this information for the ultimate top-level
9326 -- noninstance parent (is that always correct???).
9328 if Scope
(Par
) = Standard_Standard
9329 and then not Is_Generic_Instance
(Par
)
9331 Parent_Unit_Visible
:= Is_Immediately_Visible
(Par
);
9332 Instance_Parent_Unit
:= Par
;
9335 -- Open the parent scope and make it and its declarations visible.
9336 -- If this point is not within a body, then only the visible
9337 -- declarations should be made visible, and installation of the
9338 -- private declarations is deferred until the appropriate point
9339 -- within analysis of the spec being instantiated (see the handling
9340 -- of parent visibility in Analyze_Package_Specification). This is
9341 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9342 -- private view problems that occur when compiling instantiations of
9343 -- a generic child of that package (Generic_Dispatching_Constructor).
9344 -- If the instance freezes a tagged type, inlinings of operations
9345 -- from Ada.Tags may need the full view of type Tag. If inlining took
9346 -- proper account of establishing visibility of inlined subprograms'
9347 -- parents then it should be possible to remove this
9348 -- special check. ???
9351 Set_Is_Immediately_Visible
(Par
);
9352 Install_Visible_Declarations
(Par
);
9353 Set_Use
(Visible_Declarations
(Spec
));
9355 if In_Body
or else Is_RTU
(Par
, Ada_Tags
) then
9356 Install_Private_Declarations
(Par
);
9357 Set_Use
(Private_Declarations
(Spec
));
9361 -- Start of processing for Install_Parent
9364 -- We need to install the parent instance to compile the instantiation
9365 -- of the child, but the child instance must appear in the current
9366 -- scope. Given that we cannot place the parent above the current scope
9367 -- in the scope stack, we duplicate the current scope and unstack both
9368 -- after the instantiation is complete.
9370 -- If the parent is itself the instantiation of a child unit, we must
9371 -- also stack the instantiation of its parent, and so on. Each such
9372 -- ancestor is the prefix of the name in a prior instantiation.
9374 -- If this is a nested instance, the parent unit itself resolves to
9375 -- a renaming of the parent instance, whose declaration we need.
9377 -- Finally, the parent may be a generic (not an instance) when the
9378 -- child unit appears as a formal package.
9382 if Present
(Renamed_Entity
(Inst_Par
)) then
9383 Inst_Par
:= Renamed_Entity
(Inst_Par
);
9386 First_Par
:= Inst_Par
;
9388 Gen_Par
:= Generic_Parent
(Package_Specification
(Inst_Par
));
9390 First_Gen
:= Gen_Par
;
9392 while Present
(Gen_Par
) and then Is_Child_Unit
(Gen_Par
) loop
9394 -- Load grandparent instance as well
9396 Inst_Node
:= Get_Package_Instantiation_Node
(Inst_Par
);
9398 if Nkind
(Name
(Inst_Node
)) = N_Expanded_Name
then
9399 Inst_Par
:= Entity
(Prefix
(Name
(Inst_Node
)));
9401 if Present
(Renamed_Entity
(Inst_Par
)) then
9402 Inst_Par
:= Renamed_Entity
(Inst_Par
);
9405 Gen_Par
:= Generic_Parent
(Package_Specification
(Inst_Par
));
9407 if Present
(Gen_Par
) then
9408 Prepend_Elmt
(Inst_Par
, Ancestors
);
9411 -- Parent is not the name of an instantiation
9413 Install_Noninstance_Specs
(Inst_Par
);
9424 if Present
(First_Gen
) then
9425 Append_Elmt
(First_Par
, Ancestors
);
9427 Install_Noninstance_Specs
(First_Par
);
9430 if not Is_Empty_Elmt_List
(Ancestors
) then
9431 Elmt
:= First_Elmt
(Ancestors
);
9432 while Present
(Elmt
) loop
9433 Install_Spec
(Node
(Elmt
));
9434 Install_Formal_Packages
(Node
(Elmt
));
9444 -------------------------------
9445 -- Install_Hidden_Primitives --
9446 -------------------------------
9448 procedure Install_Hidden_Primitives
9449 (Prims_List
: in out Elist_Id
;
9454 List
: Elist_Id
:= No_Elist
;
9455 Prim_G_Elmt
: Elmt_Id
;
9456 Prim_A_Elmt
: Elmt_Id
;
9461 -- No action needed in case of serious errors because we cannot trust
9462 -- in the order of primitives
9464 if Serious_Errors_Detected
> 0 then
9467 -- No action possible if we don't have available the list of primitive
9471 or else not Is_Record_Type
(Gen_T
)
9472 or else not Is_Tagged_Type
(Gen_T
)
9473 or else not Is_Record_Type
(Act_T
)
9474 or else not Is_Tagged_Type
(Act_T
)
9478 -- There is no need to handle interface types since their primitives
9481 elsif Is_Interface
(Gen_T
) then
9485 Prim_G_Elmt
:= First_Elmt
(Primitive_Operations
(Gen_T
));
9487 if not Is_Class_Wide_Type
(Act_T
) then
9488 Prim_A_Elmt
:= First_Elmt
(Primitive_Operations
(Act_T
));
9490 Prim_A_Elmt
:= First_Elmt
(Primitive_Operations
(Root_Type
(Act_T
)));
9494 -- Skip predefined primitives in the generic formal
9496 while Present
(Prim_G_Elmt
)
9497 and then Is_Predefined_Dispatching_Operation
(Node
(Prim_G_Elmt
))
9499 Next_Elmt
(Prim_G_Elmt
);
9502 -- Skip predefined primitives in the generic actual
9504 while Present
(Prim_A_Elmt
)
9505 and then Is_Predefined_Dispatching_Operation
(Node
(Prim_A_Elmt
))
9507 Next_Elmt
(Prim_A_Elmt
);
9510 exit when No
(Prim_G_Elmt
) or else No
(Prim_A_Elmt
);
9512 Prim_G
:= Node
(Prim_G_Elmt
);
9513 Prim_A
:= Node
(Prim_A_Elmt
);
9515 -- There is no need to handle interface primitives because their
9516 -- primitives are not hidden
9518 exit when Present
(Interface_Alias
(Prim_G
));
9520 -- Here we install one hidden primitive
9522 if Chars
(Prim_G
) /= Chars
(Prim_A
)
9523 and then Has_Suffix
(Prim_A
, 'P')
9524 and then Remove_Suffix
(Prim_A
, 'P') = Chars
(Prim_G
)
9526 Set_Chars
(Prim_A
, Chars
(Prim_G
));
9527 Append_New_Elmt
(Prim_A
, To
=> List
);
9530 Next_Elmt
(Prim_A_Elmt
);
9531 Next_Elmt
(Prim_G_Elmt
);
9534 -- Append the elements to the list of temporarily visible primitives
9535 -- avoiding duplicates.
9537 if Present
(List
) then
9538 if No
(Prims_List
) then
9539 Prims_List
:= New_Elmt_List
;
9542 Elmt
:= First_Elmt
(List
);
9543 while Present
(Elmt
) loop
9544 Append_Unique_Elmt
(Node
(Elmt
), Prims_List
);
9548 end Install_Hidden_Primitives
;
9550 -------------------------------
9551 -- Restore_Hidden_Primitives --
9552 -------------------------------
9554 procedure Restore_Hidden_Primitives
(Prims_List
: in out Elist_Id
) is
9555 Prim_Elmt
: Elmt_Id
;
9559 if Prims_List
/= No_Elist
then
9560 Prim_Elmt
:= First_Elmt
(Prims_List
);
9561 while Present
(Prim_Elmt
) loop
9562 Prim
:= Node
(Prim_Elmt
);
9563 Set_Chars
(Prim
, Add_Suffix
(Prim
, 'P'));
9564 Next_Elmt
(Prim_Elmt
);
9567 Prims_List
:= No_Elist
;
9569 end Restore_Hidden_Primitives
;
9571 --------------------------------
9572 -- Instantiate_Formal_Package --
9573 --------------------------------
9575 function Instantiate_Formal_Package
9578 Analyzed_Formal
: Node_Id
) return List_Id
9580 Loc
: constant Source_Ptr
:= Sloc
(Actual
);
9581 Actual_Pack
: Entity_Id
;
9582 Formal_Pack
: Entity_Id
;
9583 Gen_Parent
: Entity_Id
;
9586 Parent_Spec
: Node_Id
;
9588 procedure Find_Matching_Actual
9590 Act
: in out Entity_Id
);
9591 -- We need to associate each formal entity in the formal package with
9592 -- the corresponding entity in the actual package. The actual package
9593 -- has been analyzed and possibly expanded, and as a result there is
9594 -- no one-to-one correspondence between the two lists (for example,
9595 -- the actual may include subtypes, itypes, and inherited primitive
9596 -- operations, interspersed among the renaming declarations for the
9597 -- actuals). We retrieve the corresponding actual by name because each
9598 -- actual has the same name as the formal, and they do appear in the
9601 function Get_Formal_Entity
(N
: Node_Id
) return Entity_Id
;
9602 -- Retrieve entity of defining entity of generic formal parameter.
9603 -- Only the declarations of formals need to be considered when
9604 -- linking them to actuals, but the declarative list may include
9605 -- internal entities generated during analysis, and those are ignored.
9607 procedure Match_Formal_Entity
9608 (Formal_Node
: Node_Id
;
9609 Formal_Ent
: Entity_Id
;
9610 Actual_Ent
: Entity_Id
);
9611 -- Associates the formal entity with the actual. In the case where
9612 -- Formal_Ent is a formal package, this procedure iterates through all
9613 -- of its formals and enters associations between the actuals occurring
9614 -- in the formal package's corresponding actual package (given by
9615 -- Actual_Ent) and the formal package's formal parameters. This
9616 -- procedure recurses if any of the parameters is itself a package.
9618 function Is_Instance_Of
9619 (Act_Spec
: Entity_Id
;
9620 Gen_Anc
: Entity_Id
) return Boolean;
9621 -- The actual can be an instantiation of a generic within another
9622 -- instance, in which case there is no direct link from it to the
9623 -- original generic ancestor. In that case, we recognize that the
9624 -- ultimate ancestor is the same by examining names and scopes.
9626 procedure Process_Nested_Formal
(Formal
: Entity_Id
);
9627 -- If the current formal is declared with a box, its own formals are
9628 -- visible in the instance, as they were in the generic, and their
9629 -- Hidden flag must be reset. If some of these formals are themselves
9630 -- packages declared with a box, the processing must be recursive.
9632 --------------------------
9633 -- Find_Matching_Actual --
9634 --------------------------
9636 procedure Find_Matching_Actual
9638 Act
: in out Entity_Id
)
9640 Formal_Ent
: Entity_Id
;
9643 case Nkind
(Original_Node
(F
)) is
9644 when N_Formal_Object_Declaration |
9645 N_Formal_Type_Declaration
=>
9646 Formal_Ent
:= Defining_Identifier
(F
);
9648 while Chars
(Act
) /= Chars
(Formal_Ent
) loop
9652 when N_Formal_Subprogram_Declaration |
9653 N_Formal_Package_Declaration |
9654 N_Package_Declaration |
9655 N_Generic_Package_Declaration
=>
9656 Formal_Ent
:= Defining_Entity
(F
);
9658 while Chars
(Act
) /= Chars
(Formal_Ent
) loop
9663 raise Program_Error
;
9665 end Find_Matching_Actual
;
9667 -------------------------
9668 -- Match_Formal_Entity --
9669 -------------------------
9671 procedure Match_Formal_Entity
9672 (Formal_Node
: Node_Id
;
9673 Formal_Ent
: Entity_Id
;
9674 Actual_Ent
: Entity_Id
)
9676 Act_Pkg
: Entity_Id
;
9679 Set_Instance_Of
(Formal_Ent
, Actual_Ent
);
9681 if Ekind
(Actual_Ent
) = E_Package
then
9683 -- Record associations for each parameter
9685 Act_Pkg
:= Actual_Ent
;
9688 A_Ent
: Entity_Id
:= First_Entity
(Act_Pkg
);
9697 -- Retrieve the actual given in the formal package declaration
9699 Actual
:= Entity
(Name
(Original_Node
(Formal_Node
)));
9701 -- The actual in the formal package declaration may be a
9702 -- renamed generic package, in which case we want to retrieve
9703 -- the original generic in order to traverse its formal part.
9705 if Present
(Renamed_Entity
(Actual
)) then
9706 Gen_Decl
:= Unit_Declaration_Node
(Renamed_Entity
(Actual
));
9708 Gen_Decl
:= Unit_Declaration_Node
(Actual
);
9711 Formals
:= Generic_Formal_Declarations
(Gen_Decl
);
9713 if Present
(Formals
) then
9714 F_Node
:= First_Non_Pragma
(Formals
);
9719 while Present
(A_Ent
)
9720 and then Present
(F_Node
)
9721 and then A_Ent
/= First_Private_Entity
(Act_Pkg
)
9723 F_Ent
:= Get_Formal_Entity
(F_Node
);
9725 if Present
(F_Ent
) then
9727 -- This is a formal of the original package. Record
9728 -- association and recurse.
9730 Find_Matching_Actual
(F_Node
, A_Ent
);
9731 Match_Formal_Entity
(F_Node
, F_Ent
, A_Ent
);
9732 Next_Entity
(A_Ent
);
9735 Next_Non_Pragma
(F_Node
);
9739 end Match_Formal_Entity
;
9741 -----------------------
9742 -- Get_Formal_Entity --
9743 -----------------------
9745 function Get_Formal_Entity
(N
: Node_Id
) return Entity_Id
is
9746 Kind
: constant Node_Kind
:= Nkind
(Original_Node
(N
));
9749 when N_Formal_Object_Declaration
=>
9750 return Defining_Identifier
(N
);
9752 when N_Formal_Type_Declaration
=>
9753 return Defining_Identifier
(N
);
9755 when N_Formal_Subprogram_Declaration
=>
9756 return Defining_Unit_Name
(Specification
(N
));
9758 when N_Formal_Package_Declaration
=>
9759 return Defining_Identifier
(Original_Node
(N
));
9761 when N_Generic_Package_Declaration
=>
9762 return Defining_Identifier
(Original_Node
(N
));
9764 -- All other declarations are introduced by semantic analysis and
9765 -- have no match in the actual.
9770 end Get_Formal_Entity
;
9772 --------------------
9773 -- Is_Instance_Of --
9774 --------------------
9776 function Is_Instance_Of
9777 (Act_Spec
: Entity_Id
;
9778 Gen_Anc
: Entity_Id
) return Boolean
9780 Gen_Par
: constant Entity_Id
:= Generic_Parent
(Act_Spec
);
9783 if No
(Gen_Par
) then
9786 -- Simplest case: the generic parent of the actual is the formal
9788 elsif Gen_Par
= Gen_Anc
then
9791 elsif Chars
(Gen_Par
) /= Chars
(Gen_Anc
) then
9794 -- The actual may be obtained through several instantiations. Its
9795 -- scope must itself be an instance of a generic declared in the
9796 -- same scope as the formal. Any other case is detected above.
9798 elsif not Is_Generic_Instance
(Scope
(Gen_Par
)) then
9802 return Generic_Parent
(Parent
(Scope
(Gen_Par
))) = Scope
(Gen_Anc
);
9806 ---------------------------
9807 -- Process_Nested_Formal --
9808 ---------------------------
9810 procedure Process_Nested_Formal
(Formal
: Entity_Id
) is
9814 if Present
(Associated_Formal_Package
(Formal
))
9815 and then Box_Present
(Parent
(Associated_Formal_Package
(Formal
)))
9817 Ent
:= First_Entity
(Formal
);
9818 while Present
(Ent
) loop
9819 Set_Is_Hidden
(Ent
, False);
9820 Set_Is_Visible_Formal
(Ent
);
9821 Set_Is_Potentially_Use_Visible
9822 (Ent
, Is_Potentially_Use_Visible
(Formal
));
9824 if Ekind
(Ent
) = E_Package
then
9825 exit when Renamed_Entity
(Ent
) = Renamed_Entity
(Formal
);
9826 Process_Nested_Formal
(Ent
);
9832 end Process_Nested_Formal
;
9834 -- Start of processing for Instantiate_Formal_Package
9839 if not Is_Entity_Name
(Actual
)
9840 or else Ekind
(Entity
(Actual
)) /= E_Package
9843 ("expect package instance to instantiate formal", Actual
);
9844 Abandon_Instantiation
(Actual
);
9845 raise Program_Error
;
9848 Actual_Pack
:= Entity
(Actual
);
9849 Set_Is_Instantiated
(Actual_Pack
);
9851 -- The actual may be a renamed package, or an outer generic formal
9852 -- package whose instantiation is converted into a renaming.
9854 if Present
(Renamed_Object
(Actual_Pack
)) then
9855 Actual_Pack
:= Renamed_Object
(Actual_Pack
);
9858 if Nkind
(Analyzed_Formal
) = N_Formal_Package_Declaration
then
9859 Gen_Parent
:= Get_Instance_Of
(Entity
(Name
(Analyzed_Formal
)));
9860 Formal_Pack
:= Defining_Identifier
(Analyzed_Formal
);
9863 Generic_Parent
(Specification
(Analyzed_Formal
));
9865 Defining_Unit_Name
(Specification
(Analyzed_Formal
));
9868 if Nkind
(Parent
(Actual_Pack
)) = N_Defining_Program_Unit_Name
then
9869 Parent_Spec
:= Package_Specification
(Actual_Pack
);
9871 Parent_Spec
:= Parent
(Actual_Pack
);
9874 if Gen_Parent
= Any_Id
then
9876 ("previous error in declaration of formal package", Actual
);
9877 Abandon_Instantiation
(Actual
);
9880 Is_Instance_Of
(Parent_Spec
, Get_Instance_Of
(Gen_Parent
))
9886 ("actual parameter must be instance of&", Actual
, Gen_Parent
);
9887 Abandon_Instantiation
(Actual
);
9890 Set_Instance_Of
(Defining_Identifier
(Formal
), Actual_Pack
);
9891 Map_Formal_Package_Entities
(Formal_Pack
, Actual_Pack
);
9894 Make_Package_Renaming_Declaration
(Loc
,
9895 Defining_Unit_Name
=> New_Copy
(Defining_Identifier
(Formal
)),
9896 Name
=> New_Occurrence_Of
(Actual_Pack
, Loc
));
9898 Set_Associated_Formal_Package
9899 (Defining_Unit_Name
(Nod
), Defining_Identifier
(Formal
));
9900 Decls
:= New_List
(Nod
);
9902 -- If the formal F has a box, then the generic declarations are
9903 -- visible in the generic G. In an instance of G, the corresponding
9904 -- entities in the actual for F (which are the actuals for the
9905 -- instantiation of the generic that F denotes) must also be made
9906 -- visible for analysis of the current instance. On exit from the
9907 -- current instance, those entities are made private again. If the
9908 -- actual is currently in use, these entities are also use-visible.
9910 -- The loop through the actual entities also steps through the formal
9911 -- entities and enters associations from formals to actuals into the
9912 -- renaming map. This is necessary to properly handle checking of
9913 -- actual parameter associations for later formals that depend on
9914 -- actuals declared in the formal package.
9916 -- In Ada 2005, partial parameterization requires that we make
9917 -- visible the actuals corresponding to formals that were defaulted
9918 -- in the formal package. There formals are identified because they
9919 -- remain formal generics within the formal package, rather than
9920 -- being renamings of the actuals supplied.
9923 Gen_Decl
: constant Node_Id
:=
9924 Unit_Declaration_Node
(Gen_Parent
);
9925 Formals
: constant List_Id
:=
9926 Generic_Formal_Declarations
(Gen_Decl
);
9928 Actual_Ent
: Entity_Id
;
9929 Actual_Of_Formal
: Node_Id
;
9930 Formal_Node
: Node_Id
;
9931 Formal_Ent
: Entity_Id
;
9934 if Present
(Formals
) then
9935 Formal_Node
:= First_Non_Pragma
(Formals
);
9937 Formal_Node
:= Empty
;
9940 Actual_Ent
:= First_Entity
(Actual_Pack
);
9942 First
(Visible_Declarations
(Specification
(Analyzed_Formal
)));
9943 while Present
(Actual_Ent
)
9944 and then Actual_Ent
/= First_Private_Entity
(Actual_Pack
)
9946 if Present
(Formal_Node
) then
9947 Formal_Ent
:= Get_Formal_Entity
(Formal_Node
);
9949 if Present
(Formal_Ent
) then
9950 Find_Matching_Actual
(Formal_Node
, Actual_Ent
);
9951 Match_Formal_Entity
(Formal_Node
, Formal_Ent
, Actual_Ent
);
9953 -- We iterate at the same time over the actuals of the
9954 -- local package created for the formal, to determine
9955 -- which one of the formals of the original generic were
9956 -- defaulted in the formal. The corresponding actual
9957 -- entities are visible in the enclosing instance.
9959 if Box_Present
(Formal
)
9961 (Present
(Actual_Of_Formal
)
9964 (Get_Formal_Entity
(Actual_Of_Formal
)))
9966 Set_Is_Hidden
(Actual_Ent
, False);
9967 Set_Is_Visible_Formal
(Actual_Ent
);
9968 Set_Is_Potentially_Use_Visible
9969 (Actual_Ent
, In_Use
(Actual_Pack
));
9971 if Ekind
(Actual_Ent
) = E_Package
then
9972 Process_Nested_Formal
(Actual_Ent
);
9976 Set_Is_Hidden
(Actual_Ent
);
9977 Set_Is_Potentially_Use_Visible
(Actual_Ent
, False);
9981 Next_Non_Pragma
(Formal_Node
);
9982 Next
(Actual_Of_Formal
);
9985 -- No further formals to match, but the generic part may
9986 -- contain inherited operation that are not hidden in the
9987 -- enclosing instance.
9989 Next_Entity
(Actual_Ent
);
9993 -- Inherited subprograms generated by formal derived types are
9994 -- also visible if the types are.
9996 Actual_Ent
:= First_Entity
(Actual_Pack
);
9997 while Present
(Actual_Ent
)
9998 and then Actual_Ent
/= First_Private_Entity
(Actual_Pack
)
10000 if Is_Overloadable
(Actual_Ent
)
10002 Nkind
(Parent
(Actual_Ent
)) = N_Subtype_Declaration
10004 not Is_Hidden
(Defining_Identifier
(Parent
(Actual_Ent
)))
10006 Set_Is_Hidden
(Actual_Ent
, False);
10007 Set_Is_Potentially_Use_Visible
10008 (Actual_Ent
, In_Use
(Actual_Pack
));
10011 Next_Entity
(Actual_Ent
);
10015 -- If the formal is not declared with a box, reanalyze it as an
10016 -- abbreviated instantiation, to verify the matching rules of 12.7.
10017 -- The actual checks are performed after the generic associations
10018 -- have been analyzed, to guarantee the same visibility for this
10019 -- instantiation and for the actuals.
10021 -- In Ada 2005, the generic associations for the formal can include
10022 -- defaulted parameters. These are ignored during check. This
10023 -- internal instantiation is removed from the tree after conformance
10024 -- checking, because it contains formal declarations for those
10025 -- defaulted parameters, and those should not reach the back-end.
10027 if not Box_Present
(Formal
) then
10029 I_Pack
: constant Entity_Id
:=
10030 Make_Temporary
(Sloc
(Actual
), 'P');
10033 Set_Is_Internal
(I_Pack
);
10036 Make_Package_Instantiation
(Sloc
(Actual
),
10037 Defining_Unit_Name
=> I_Pack
,
10040 (Get_Instance_Of
(Gen_Parent
), Sloc
(Actual
)),
10041 Generic_Associations
=> Generic_Associations
(Formal
)));
10047 end Instantiate_Formal_Package
;
10049 -----------------------------------
10050 -- Instantiate_Formal_Subprogram --
10051 -----------------------------------
10053 function Instantiate_Formal_Subprogram
10056 Analyzed_Formal
: Node_Id
) return Node_Id
10058 Analyzed_S
: constant Entity_Id
:=
10059 Defining_Unit_Name
(Specification
(Analyzed_Formal
));
10060 Formal_Sub
: constant Entity_Id
:=
10061 Defining_Unit_Name
(Specification
(Formal
));
10063 function From_Parent_Scope
(Subp
: Entity_Id
) return Boolean;
10064 -- If the generic is a child unit, the parent has been installed on the
10065 -- scope stack, but a default subprogram cannot resolve to something
10066 -- on the parent because that parent is not really part of the visible
10067 -- context (it is there to resolve explicit local entities). If the
10068 -- default has resolved in this way, we remove the entity from immediate
10069 -- visibility and analyze the node again to emit an error message or
10070 -- find another visible candidate.
10072 procedure Valid_Actual_Subprogram
(Act
: Node_Id
);
10073 -- Perform legality check and raise exception on failure
10075 -----------------------
10076 -- From_Parent_Scope --
10077 -----------------------
10079 function From_Parent_Scope
(Subp
: Entity_Id
) return Boolean is
10080 Gen_Scope
: Node_Id
;
10083 Gen_Scope
:= Scope
(Analyzed_S
);
10084 while Present
(Gen_Scope
) and then Is_Child_Unit
(Gen_Scope
) loop
10085 if Scope
(Subp
) = Scope
(Gen_Scope
) then
10089 Gen_Scope
:= Scope
(Gen_Scope
);
10093 end From_Parent_Scope
;
10095 -----------------------------
10096 -- Valid_Actual_Subprogram --
10097 -----------------------------
10099 procedure Valid_Actual_Subprogram
(Act
: Node_Id
) is
10103 if Is_Entity_Name
(Act
) then
10104 Act_E
:= Entity
(Act
);
10106 elsif Nkind
(Act
) = N_Selected_Component
10107 and then Is_Entity_Name
(Selector_Name
(Act
))
10109 Act_E
:= Entity
(Selector_Name
(Act
));
10115 if (Present
(Act_E
) and then Is_Overloadable
(Act_E
))
10116 or else Nkind_In
(Act
, N_Attribute_Reference
,
10117 N_Indexed_Component
,
10118 N_Character_Literal
,
10119 N_Explicit_Dereference
)
10125 ("expect subprogram or entry name in instantiation of &",
10126 Instantiation_Node
, Formal_Sub
);
10127 Abandon_Instantiation
(Instantiation_Node
);
10128 end Valid_Actual_Subprogram
;
10132 Decl_Node
: Node_Id
;
10135 New_Spec
: Node_Id
;
10136 New_Subp
: Entity_Id
;
10138 -- Start of processing for Instantiate_Formal_Subprogram
10141 New_Spec
:= New_Copy_Tree
(Specification
(Formal
));
10143 -- The tree copy has created the proper instantiation sloc for the
10144 -- new specification. Use this location for all other constructed
10147 Loc
:= Sloc
(Defining_Unit_Name
(New_Spec
));
10149 -- Create new entity for the actual (New_Copy_Tree does not), and
10150 -- indicate that it is an actual.
10152 New_Subp
:= Make_Defining_Identifier
(Loc
, Chars
(Formal_Sub
));
10153 Set_Ekind
(New_Subp
, Ekind
(Analyzed_S
));
10154 Set_Is_Generic_Actual_Subprogram
(New_Subp
);
10155 Set_Defining_Unit_Name
(New_Spec
, New_Subp
);
10157 -- Create new entities for the each of the formals in the specification
10158 -- of the renaming declaration built for the actual.
10160 if Present
(Parameter_Specifications
(New_Spec
)) then
10166 F
:= First
(Parameter_Specifications
(New_Spec
));
10167 while Present
(F
) loop
10168 F_Id
:= Defining_Identifier
(F
);
10170 Set_Defining_Identifier
(F
,
10171 Make_Defining_Identifier
(Sloc
(F_Id
), Chars
(F_Id
)));
10177 -- Find entity of actual. If the actual is an attribute reference, it
10178 -- cannot be resolved here (its formal is missing) but is handled
10179 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10180 -- fully resolved subsequently, when the renaming declaration for the
10181 -- formal is analyzed. If it is an explicit dereference, resolve the
10182 -- prefix but not the actual itself, to prevent interpretation as call.
10184 if Present
(Actual
) then
10185 Loc
:= Sloc
(Actual
);
10186 Set_Sloc
(New_Spec
, Loc
);
10188 if Nkind
(Actual
) = N_Operator_Symbol
then
10189 Find_Direct_Name
(Actual
);
10191 elsif Nkind
(Actual
) = N_Explicit_Dereference
then
10192 Analyze
(Prefix
(Actual
));
10194 elsif Nkind
(Actual
) /= N_Attribute_Reference
then
10198 Valid_Actual_Subprogram
(Actual
);
10201 elsif Present
(Default_Name
(Formal
)) then
10202 if not Nkind_In
(Default_Name
(Formal
), N_Attribute_Reference
,
10203 N_Selected_Component
,
10204 N_Indexed_Component
,
10205 N_Character_Literal
)
10206 and then Present
(Entity
(Default_Name
(Formal
)))
10208 Nam
:= New_Occurrence_Of
(Entity
(Default_Name
(Formal
)), Loc
);
10210 Nam
:= New_Copy
(Default_Name
(Formal
));
10211 Set_Sloc
(Nam
, Loc
);
10214 elsif Box_Present
(Formal
) then
10216 -- Actual is resolved at the point of instantiation. Create an
10217 -- identifier or operator with the same name as the formal.
10219 if Nkind
(Formal_Sub
) = N_Defining_Operator_Symbol
then
10221 Make_Operator_Symbol
(Loc
,
10222 Chars
=> Chars
(Formal_Sub
),
10223 Strval
=> No_String
);
10225 Nam
:= Make_Identifier
(Loc
, Chars
(Formal_Sub
));
10228 elsif Nkind
(Specification
(Formal
)) = N_Procedure_Specification
10229 and then Null_Present
(Specification
(Formal
))
10231 -- Generate null body for procedure, for use in the instance
10234 Make_Subprogram_Body
(Loc
,
10235 Specification
=> New_Spec
,
10236 Declarations
=> New_List
,
10237 Handled_Statement_Sequence
=>
10238 Make_Handled_Sequence_Of_Statements
(Loc
,
10239 Statements
=> New_List
(Make_Null_Statement
(Loc
))));
10241 Set_Is_Intrinsic_Subprogram
(Defining_Unit_Name
(New_Spec
));
10245 Error_Msg_Sloc
:= Sloc
(Scope
(Analyzed_S
));
10247 ("missing actual&", Instantiation_Node
, Formal_Sub
);
10249 ("\in instantiation of & declared#",
10250 Instantiation_Node
, Scope
(Analyzed_S
));
10251 Abandon_Instantiation
(Instantiation_Node
);
10255 Make_Subprogram_Renaming_Declaration
(Loc
,
10256 Specification
=> New_Spec
,
10259 -- If we do not have an actual and the formal specified <> then set to
10260 -- get proper default.
10262 if No
(Actual
) and then Box_Present
(Formal
) then
10263 Set_From_Default
(Decl_Node
);
10266 -- Gather possible interpretations for the actual before analyzing the
10267 -- instance. If overloaded, it will be resolved when analyzing the
10268 -- renaming declaration.
10270 if Box_Present
(Formal
) and then No
(Actual
) then
10273 if Is_Child_Unit
(Scope
(Analyzed_S
))
10274 and then Present
(Entity
(Nam
))
10276 if not Is_Overloaded
(Nam
) then
10277 if From_Parent_Scope
(Entity
(Nam
)) then
10278 Set_Is_Immediately_Visible
(Entity
(Nam
), False);
10279 Set_Entity
(Nam
, Empty
);
10280 Set_Etype
(Nam
, Empty
);
10283 Set_Is_Immediately_Visible
(Entity
(Nam
));
10292 Get_First_Interp
(Nam
, I
, It
);
10293 while Present
(It
.Nam
) loop
10294 if From_Parent_Scope
(It
.Nam
) then
10298 Get_Next_Interp
(I
, It
);
10305 -- The generic instantiation freezes the actual. This can only be done
10306 -- once the actual is resolved, in the analysis of the renaming
10307 -- declaration. To make the formal subprogram entity available, we set
10308 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10309 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10310 -- of formal abstract subprograms.
10312 Set_Corresponding_Formal_Spec
(Decl_Node
, Analyzed_S
);
10314 -- We cannot analyze the renaming declaration, and thus find the actual,
10315 -- until all the actuals are assembled in the instance. For subsequent
10316 -- checks of other actuals, indicate the node that will hold the
10317 -- instance of this formal.
10319 Set_Instance_Of
(Analyzed_S
, Nam
);
10321 if Nkind
(Actual
) = N_Selected_Component
10322 and then Is_Task_Type
(Etype
(Prefix
(Actual
)))
10323 and then not Is_Frozen
(Etype
(Prefix
(Actual
)))
10325 -- The renaming declaration will create a body, which must appear
10326 -- outside of the instantiation, We move the renaming declaration
10327 -- out of the instance, and create an additional renaming inside,
10328 -- to prevent freezing anomalies.
10331 Anon_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'E');
10334 Set_Defining_Unit_Name
(New_Spec
, Anon_Id
);
10335 Insert_Before
(Instantiation_Node
, Decl_Node
);
10336 Analyze
(Decl_Node
);
10338 -- Now create renaming within the instance
10341 Make_Subprogram_Renaming_Declaration
(Loc
,
10342 Specification
=> New_Copy_Tree
(New_Spec
),
10343 Name
=> New_Occurrence_Of
(Anon_Id
, Loc
));
10345 Set_Defining_Unit_Name
(Specification
(Decl_Node
),
10346 Make_Defining_Identifier
(Loc
, Chars
(Formal_Sub
)));
10351 end Instantiate_Formal_Subprogram
;
10353 ------------------------
10354 -- Instantiate_Object --
10355 ------------------------
10357 function Instantiate_Object
10360 Analyzed_Formal
: Node_Id
) return List_Id
10362 Gen_Obj
: constant Entity_Id
:= Defining_Identifier
(Formal
);
10363 A_Gen_Obj
: constant Entity_Id
:=
10364 Defining_Identifier
(Analyzed_Formal
);
10365 Acc_Def
: Node_Id
:= Empty
;
10366 Act_Assoc
: constant Node_Id
:= Parent
(Actual
);
10367 Actual_Decl
: Node_Id
:= Empty
;
10368 Decl_Node
: Node_Id
;
10371 List
: constant List_Id
:= New_List
;
10372 Loc
: constant Source_Ptr
:= Sloc
(Actual
);
10373 Orig_Ftyp
: constant Entity_Id
:= Etype
(A_Gen_Obj
);
10374 Subt_Decl
: Node_Id
:= Empty
;
10375 Subt_Mark
: Node_Id
:= Empty
;
10377 function Copy_Access_Def
return Node_Id
;
10378 -- If formal is an anonymous access, copy access definition of formal
10379 -- for generated object declaration.
10381 ---------------------
10382 -- Copy_Access_Def --
10383 ---------------------
10385 function Copy_Access_Def
return Node_Id
is
10387 Def
:= New_Copy_Tree
(Acc_Def
);
10389 -- In addition, if formal is an access to subprogram we need to
10390 -- generate new formals for the signature of the default, so that
10391 -- the tree is properly formatted for ASIS use.
10393 if Present
(Access_To_Subprogram_Definition
(Acc_Def
)) then
10395 Par_Spec
: Node_Id
;
10398 First
(Parameter_Specifications
10399 (Access_To_Subprogram_Definition
(Def
)));
10400 while Present
(Par_Spec
) loop
10401 Set_Defining_Identifier
(Par_Spec
,
10402 Make_Defining_Identifier
(Sloc
(Acc_Def
),
10403 Chars
=> Chars
(Defining_Identifier
(Par_Spec
))));
10410 end Copy_Access_Def
;
10412 -- Start of processing for Instantiate_Object
10415 -- Formal may be an anonymous access
10417 if Present
(Subtype_Mark
(Formal
)) then
10418 Subt_Mark
:= Subtype_Mark
(Formal
);
10420 Check_Access_Definition
(Formal
);
10421 Acc_Def
:= Access_Definition
(Formal
);
10424 -- Sloc for error message on missing actual
10426 Error_Msg_Sloc
:= Sloc
(Scope
(A_Gen_Obj
));
10428 if Get_Instance_Of
(Gen_Obj
) /= Gen_Obj
then
10429 Error_Msg_N
("duplicate instantiation of generic parameter", Actual
);
10432 Set_Parent
(List
, Parent
(Actual
));
10436 if Out_Present
(Formal
) then
10438 -- An IN OUT generic actual must be a name. The instantiation is a
10439 -- renaming declaration. The actual is the name being renamed. We
10440 -- use the actual directly, rather than a copy, because it is not
10441 -- used further in the list of actuals, and because a copy or a use
10442 -- of relocate_node is incorrect if the instance is nested within a
10443 -- generic. In order to simplify ASIS searches, the Generic_Parent
10444 -- field links the declaration to the generic association.
10446 if No
(Actual
) then
10448 ("missing actual &",
10449 Instantiation_Node
, Gen_Obj
);
10451 ("\in instantiation of & declared#",
10452 Instantiation_Node
, Scope
(A_Gen_Obj
));
10453 Abandon_Instantiation
(Instantiation_Node
);
10456 if Present
(Subt_Mark
) then
10458 Make_Object_Renaming_Declaration
(Loc
,
10459 Defining_Identifier
=> New_Copy
(Gen_Obj
),
10460 Subtype_Mark
=> New_Copy_Tree
(Subt_Mark
),
10463 else pragma Assert
(Present
(Acc_Def
));
10465 Make_Object_Renaming_Declaration
(Loc
,
10466 Defining_Identifier
=> New_Copy
(Gen_Obj
),
10467 Access_Definition
=> New_Copy_Tree
(Acc_Def
),
10471 Set_Corresponding_Generic_Association
(Decl_Node
, Act_Assoc
);
10473 -- The analysis of the actual may produce Insert_Action nodes, so
10474 -- the declaration must have a context in which to attach them.
10476 Append
(Decl_Node
, List
);
10479 -- Return if the analysis of the actual reported some error
10481 if Etype
(Actual
) = Any_Type
then
10485 -- This check is performed here because Analyze_Object_Renaming will
10486 -- not check it when Comes_From_Source is False. Note though that the
10487 -- check for the actual being the name of an object will be performed
10488 -- in Analyze_Object_Renaming.
10490 if Is_Object_Reference
(Actual
)
10491 and then Is_Dependent_Component_Of_Mutable_Object
(Actual
)
10494 ("illegal discriminant-dependent component for in out parameter",
10498 -- The actual has to be resolved in order to check that it is a
10499 -- variable (due to cases such as F (1), where F returns access to
10500 -- an array, and for overloaded prefixes).
10502 Ftyp
:= Get_Instance_Of
(Etype
(A_Gen_Obj
));
10504 -- If the type of the formal is not itself a formal, and the current
10505 -- unit is a child unit, the formal type must be declared in a
10506 -- parent, and must be retrieved by visibility.
10508 if Ftyp
= Orig_Ftyp
10509 and then Is_Generic_Unit
(Scope
(Ftyp
))
10510 and then Is_Child_Unit
(Scope
(A_Gen_Obj
))
10513 Temp
: constant Node_Id
:=
10514 New_Copy_Tree
(Subtype_Mark
(Analyzed_Formal
));
10516 Set_Entity
(Temp
, Empty
);
10518 Ftyp
:= Entity
(Temp
);
10522 if Is_Private_Type
(Ftyp
)
10523 and then not Is_Private_Type
(Etype
(Actual
))
10524 and then (Base_Type
(Full_View
(Ftyp
)) = Base_Type
(Etype
(Actual
))
10525 or else Base_Type
(Etype
(Actual
)) = Ftyp
)
10527 -- If the actual has the type of the full view of the formal, or
10528 -- else a non-private subtype of the formal, then the visibility
10529 -- of the formal type has changed. Add to the actuals a subtype
10530 -- declaration that will force the exchange of views in the body
10531 -- of the instance as well.
10534 Make_Subtype_Declaration
(Loc
,
10535 Defining_Identifier
=> Make_Temporary
(Loc
, 'P'),
10536 Subtype_Indication
=> New_Occurrence_Of
(Ftyp
, Loc
));
10538 Prepend
(Subt_Decl
, List
);
10540 Prepend_Elmt
(Full_View
(Ftyp
), Exchanged_Views
);
10541 Exchange_Declarations
(Ftyp
);
10544 Resolve
(Actual
, Ftyp
);
10546 if not Denotes_Variable
(Actual
) then
10547 Error_Msg_NE
("actual for& must be a variable", Actual
, Gen_Obj
);
10549 elsif Base_Type
(Ftyp
) /= Base_Type
(Etype
(Actual
)) then
10551 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10552 -- the type of the actual shall resolve to a specific anonymous
10555 if Ada_Version
< Ada_2005
10556 or else Ekind
(Base_Type
(Ftyp
)) /=
10557 E_Anonymous_Access_Type
10558 or else Ekind
(Base_Type
(Etype
(Actual
))) /=
10559 E_Anonymous_Access_Type
10562 ("type of actual does not match type of&", Actual
, Gen_Obj
);
10566 Note_Possible_Modification
(Actual
, Sure
=> True);
10568 -- Check for instantiation of atomic/volatile actual for
10569 -- non-atomic/volatile formal (RM C.6 (12)).
10571 if Is_Atomic_Object
(Actual
) and then not Is_Atomic
(Orig_Ftyp
) then
10573 ("cannot instantiate non-atomic formal object "
10574 & "with atomic actual", Actual
);
10576 elsif Is_Volatile_Object
(Actual
) and then not Is_Volatile
(Orig_Ftyp
)
10579 ("cannot instantiate non-volatile formal object "
10580 & "with volatile actual", Actual
);
10583 -- Formal in-parameter
10586 -- The instantiation of a generic formal in-parameter is constant
10587 -- declaration. The actual is the expression for that declaration.
10588 -- Its type is a full copy of the type of the formal. This may be
10589 -- an access to subprogram, for which we need to generate entities
10590 -- for the formals in the new signature.
10592 if Present
(Actual
) then
10593 if Present
(Subt_Mark
) then
10594 Def
:= New_Copy_Tree
(Subt_Mark
);
10595 else pragma Assert
(Present
(Acc_Def
));
10596 Def
:= Copy_Access_Def
;
10600 Make_Object_Declaration
(Loc
,
10601 Defining_Identifier
=> New_Copy
(Gen_Obj
),
10602 Constant_Present
=> True,
10603 Null_Exclusion_Present
=> Null_Exclusion_Present
(Formal
),
10604 Object_Definition
=> Def
,
10605 Expression
=> Actual
);
10607 Set_Corresponding_Generic_Association
(Decl_Node
, Act_Assoc
);
10609 -- A generic formal object of a tagged type is defined to be
10610 -- aliased so the new constant must also be treated as aliased.
10612 if Is_Tagged_Type
(Etype
(A_Gen_Obj
)) then
10613 Set_Aliased_Present
(Decl_Node
);
10616 Append
(Decl_Node
, List
);
10618 -- No need to repeat (pre-)analysis of some expression nodes
10619 -- already handled in Preanalyze_Actuals.
10621 if Nkind
(Actual
) /= N_Allocator
then
10624 -- Return if the analysis of the actual reported some error
10626 if Etype
(Actual
) = Any_Type
then
10632 Formal_Type
: constant Entity_Id
:= Etype
(A_Gen_Obj
);
10636 Typ
:= Get_Instance_Of
(Formal_Type
);
10638 -- If the actual appears in the current or an enclosing scope,
10639 -- use its type directly. This is relevant if it has an actual
10640 -- subtype that is distinct from its nominal one. This cannot
10641 -- be done in general because the type of the actual may
10642 -- depend on other actuals, and only be fully determined when
10643 -- the enclosing instance is analyzed.
10645 if Present
(Etype
(Actual
))
10646 and then Is_Constr_Subt_For_U_Nominal
(Etype
(Actual
))
10648 Freeze_Before
(Instantiation_Node
, Etype
(Actual
));
10650 Freeze_Before
(Instantiation_Node
, Typ
);
10653 -- If the actual is an aggregate, perform name resolution on
10654 -- its components (the analysis of an aggregate does not do it)
10655 -- to capture local names that may be hidden if the generic is
10658 if Nkind
(Actual
) = N_Aggregate
then
10659 Preanalyze_And_Resolve
(Actual
, Typ
);
10662 if Is_Limited_Type
(Typ
)
10663 and then not OK_For_Limited_Init
(Typ
, Actual
)
10666 ("initialization not allowed for limited types", Actual
);
10667 Explain_Limited_Type
(Typ
, Actual
);
10671 elsif Present
(Default_Expression
(Formal
)) then
10673 -- Use default to construct declaration
10675 if Present
(Subt_Mark
) then
10676 Def
:= New_Copy
(Subt_Mark
);
10677 else pragma Assert
(Present
(Acc_Def
));
10678 Def
:= Copy_Access_Def
;
10682 Make_Object_Declaration
(Sloc
(Formal
),
10683 Defining_Identifier
=> New_Copy
(Gen_Obj
),
10684 Constant_Present
=> True,
10685 Null_Exclusion_Present
=> Null_Exclusion_Present
(Formal
),
10686 Object_Definition
=> Def
,
10687 Expression
=> New_Copy_Tree
10688 (Default_Expression
(Formal
)));
10690 Append
(Decl_Node
, List
);
10691 Set_Analyzed
(Expression
(Decl_Node
), False);
10694 Error_Msg_NE
("missing actual&", Instantiation_Node
, Gen_Obj
);
10695 Error_Msg_NE
("\in instantiation of & declared#",
10696 Instantiation_Node
, Scope
(A_Gen_Obj
));
10698 if Is_Scalar_Type
(Etype
(A_Gen_Obj
)) then
10700 -- Create dummy constant declaration so that instance can be
10701 -- analyzed, to minimize cascaded visibility errors.
10703 if Present
(Subt_Mark
) then
10705 else pragma Assert
(Present
(Acc_Def
));
10710 Make_Object_Declaration
(Loc
,
10711 Defining_Identifier
=> New_Copy
(Gen_Obj
),
10712 Constant_Present
=> True,
10713 Null_Exclusion_Present
=> Null_Exclusion_Present
(Formal
),
10714 Object_Definition
=> New_Copy
(Def
),
10716 Make_Attribute_Reference
(Sloc
(Gen_Obj
),
10717 Attribute_Name
=> Name_First
,
10718 Prefix
=> New_Copy
(Def
)));
10720 Append
(Decl_Node
, List
);
10723 Abandon_Instantiation
(Instantiation_Node
);
10728 if Nkind
(Actual
) in N_Has_Entity
then
10729 Actual_Decl
:= Parent
(Entity
(Actual
));
10732 -- Ada 2005 (AI-423): For a formal object declaration with a null
10733 -- exclusion or an access definition that has a null exclusion: If the
10734 -- actual matching the formal object declaration denotes a generic
10735 -- formal object of another generic unit G, and the instantiation
10736 -- containing the actual occurs within the body of G or within the body
10737 -- of a generic unit declared within the declarative region of G, then
10738 -- the declaration of the formal object of G must have a null exclusion.
10739 -- Otherwise, the subtype of the actual matching the formal object
10740 -- declaration shall exclude null.
10742 if Ada_Version
>= Ada_2005
10743 and then Present
(Actual_Decl
)
10744 and then Nkind_In
(Actual_Decl
, N_Formal_Object_Declaration
,
10745 N_Object_Declaration
)
10746 and then Nkind
(Analyzed_Formal
) = N_Formal_Object_Declaration
10747 and then not Has_Null_Exclusion
(Actual_Decl
)
10748 and then Has_Null_Exclusion
(Analyzed_Formal
)
10750 Error_Msg_Sloc
:= Sloc
(Analyzed_Formal
);
10752 ("actual must exclude null to match generic formal#", Actual
);
10755 -- An effectively volatile object cannot be used as an actual in a
10756 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
10757 -- relevant only when SPARK_Mode is on as it is not a standard Ada
10758 -- legality rule, and also verifies that the actual is an object.
10761 and then Present
(Actual
)
10762 and then Is_Object_Reference
(Actual
)
10763 and then Is_Effectively_Volatile_Object
(Actual
)
10766 ("volatile object cannot act as actual in generic instantiation",
10771 end Instantiate_Object
;
10773 ------------------------------
10774 -- Instantiate_Package_Body --
10775 ------------------------------
10777 procedure Instantiate_Package_Body
10778 (Body_Info
: Pending_Body_Info
;
10779 Inlined_Body
: Boolean := False;
10780 Body_Optional
: Boolean := False)
10782 Act_Decl
: constant Node_Id
:= Body_Info
.Act_Decl
;
10783 Inst_Node
: constant Node_Id
:= Body_Info
.Inst_Node
;
10784 Loc
: constant Source_Ptr
:= Sloc
(Inst_Node
);
10786 Gen_Id
: constant Node_Id
:= Name
(Inst_Node
);
10787 Gen_Unit
: constant Entity_Id
:= Get_Generic_Entity
(Inst_Node
);
10788 Gen_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Gen_Unit
);
10789 Act_Spec
: constant Node_Id
:= Specification
(Act_Decl
);
10790 Act_Decl_Id
: constant Entity_Id
:= Defining_Entity
(Act_Spec
);
10792 Save_IPSM
: constant Boolean := Ignore_Pragma_SPARK_Mode
;
10793 Save_Style_Check
: constant Boolean := Style_Check
;
10795 Act_Body
: Node_Id
;
10796 Act_Body_Id
: Entity_Id
;
10797 Act_Body_Name
: Node_Id
;
10798 Gen_Body
: Node_Id
;
10799 Gen_Body_Id
: Node_Id
;
10800 Par_Ent
: Entity_Id
:= Empty
;
10801 Par_Vis
: Boolean := False;
10803 Parent_Installed
: Boolean := False;
10805 Vis_Prims_List
: Elist_Id
:= No_Elist
;
10806 -- List of primitives made temporarily visible in the instantiation
10807 -- to match the visibility of the formal type
10809 procedure Check_Initialized_Types
;
10810 -- In a generic package body, an entity of a generic private type may
10811 -- appear uninitialized. This is suspicious, unless the actual is a
10812 -- fully initialized type.
10814 -----------------------------
10815 -- Check_Initialized_Types --
10816 -----------------------------
10818 procedure Check_Initialized_Types
is
10820 Formal
: Entity_Id
;
10821 Actual
: Entity_Id
;
10822 Uninit_Var
: Entity_Id
;
10825 Decl
:= First
(Generic_Formal_Declarations
(Gen_Decl
));
10826 while Present
(Decl
) loop
10827 Uninit_Var
:= Empty
;
10829 if Nkind
(Decl
) = N_Private_Extension_Declaration
then
10830 Uninit_Var
:= Uninitialized_Variable
(Decl
);
10832 elsif Nkind
(Decl
) = N_Formal_Type_Declaration
10833 and then Nkind
(Formal_Type_Definition
(Decl
)) =
10834 N_Formal_Private_Type_Definition
10837 Uninitialized_Variable
(Formal_Type_Definition
(Decl
));
10840 if Present
(Uninit_Var
) then
10841 Formal
:= Defining_Identifier
(Decl
);
10842 Actual
:= First_Entity
(Act_Decl_Id
);
10844 -- For each formal there is a subtype declaration that renames
10845 -- the actual and has the same name as the formal. Locate the
10846 -- formal for warning message about uninitialized variables
10847 -- in the generic, for which the actual type should be a fully
10848 -- initialized type.
10850 while Present
(Actual
) loop
10851 exit when Ekind
(Actual
) = E_Package
10852 and then Present
(Renamed_Object
(Actual
));
10854 if Chars
(Actual
) = Chars
(Formal
)
10855 and then not Is_Scalar_Type
(Actual
)
10856 and then not Is_Fully_Initialized_Type
(Actual
)
10857 and then Warn_On_No_Value_Assigned
10859 Error_Msg_Node_2
:= Formal
;
10861 ("generic unit has uninitialized variable& of "
10862 & "formal private type &?v?", Actual
, Uninit_Var
);
10864 ("actual type for& should be fully initialized type?v?",
10869 Next_Entity
(Actual
);
10875 end Check_Initialized_Types
;
10877 -- Start of processing for Instantiate_Package_Body
10880 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
10882 -- The instance body may already have been processed, as the parent of
10883 -- another instance that is inlined (Load_Parent_Of_Generic).
10885 if Present
(Corresponding_Body
(Instance_Spec
(Inst_Node
))) then
10889 Expander_Mode_Save_And_Set
(Body_Info
.Expander_Status
);
10891 -- Re-establish the state of information on which checks are suppressed.
10892 -- This information was set in Body_Info at the point of instantiation,
10893 -- and now we restore it so that the instance is compiled using the
10894 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
10896 Local_Suppress_Stack_Top
:= Body_Info
.Local_Suppress_Stack_Top
;
10897 Scope_Suppress
:= Body_Info
.Scope_Suppress
;
10898 Opt
.Ada_Version
:= Body_Info
.Version
;
10899 Opt
.Ada_Version_Pragma
:= Body_Info
.Version_Pragma
;
10900 Restore_Warnings
(Body_Info
.Warnings
);
10901 Opt
.SPARK_Mode
:= Body_Info
.SPARK_Mode
;
10902 Opt
.SPARK_Mode_Pragma
:= Body_Info
.SPARK_Mode_Pragma
;
10904 if No
(Gen_Body_Id
) then
10906 -- Do not look for parent of generic body if none is required.
10907 -- This may happen when the routine is called as part of the
10908 -- Pending_Instantiations processing, when nested instances
10909 -- may precede the one generated from the main unit.
10911 if not Unit_Requires_Body
(Defining_Entity
(Gen_Decl
))
10912 and then Body_Optional
10916 Load_Parent_Of_Generic
10917 (Inst_Node
, Specification
(Gen_Decl
), Body_Optional
);
10918 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
10922 -- Establish global variable for sloc adjustment and for error recovery
10923 -- In the case of an instance body for an instantiation with actuals
10924 -- from a limited view, the instance body is placed at the beginning
10925 -- of the enclosing package body: use the body entity as the source
10926 -- location for nodes of the instance body.
10928 if not Is_Empty_Elmt_List
(Incomplete_Actuals
(Act_Decl_Id
)) then
10930 Scop
: constant Entity_Id
:= Scope
(Act_Decl_Id
);
10931 Body_Id
: constant Node_Id
:=
10932 Corresponding_Body
(Unit_Declaration_Node
(Scop
));
10935 Instantiation_Node
:= Body_Id
;
10938 Instantiation_Node
:= Inst_Node
;
10941 if Present
(Gen_Body_Id
) then
10942 Save_Env
(Gen_Unit
, Act_Decl_Id
);
10943 Style_Check
:= False;
10945 -- If the context of the instance is subject to SPARK_Mode "off" or
10946 -- the annotation is altogether missing, set the global flag which
10947 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
10950 if SPARK_Mode
/= On
then
10951 Ignore_Pragma_SPARK_Mode
:= True;
10954 Current_Sem_Unit
:= Body_Info
.Current_Sem_Unit
;
10955 Gen_Body
:= Unit_Declaration_Node
(Gen_Body_Id
);
10957 Create_Instantiation_Source
10958 (Inst_Node
, Gen_Body_Id
, S_Adjustment
);
10962 (Original_Node
(Gen_Body
), Empty
, Instantiating
=> True);
10964 -- Create proper (possibly qualified) defining name for the body, to
10965 -- correspond to the one in the spec.
10968 Make_Defining_Identifier
(Sloc
(Act_Decl_Id
), Chars
(Act_Decl_Id
));
10969 Set_Comes_From_Source
(Act_Body_Id
, Comes_From_Source
(Act_Decl_Id
));
10971 -- Some attributes of spec entity are not inherited by body entity
10973 Set_Handler_Records
(Act_Body_Id
, No_List
);
10975 if Nkind
(Defining_Unit_Name
(Act_Spec
)) =
10976 N_Defining_Program_Unit_Name
10979 Make_Defining_Program_Unit_Name
(Loc
,
10981 New_Copy_Tree
(Name
(Defining_Unit_Name
(Act_Spec
))),
10982 Defining_Identifier
=> Act_Body_Id
);
10984 Act_Body_Name
:= Act_Body_Id
;
10987 Set_Defining_Unit_Name
(Act_Body
, Act_Body_Name
);
10989 Set_Corresponding_Spec
(Act_Body
, Act_Decl_Id
);
10990 Check_Generic_Actuals
(Act_Decl_Id
, False);
10991 Check_Initialized_Types
;
10993 -- Install primitives hidden at the point of the instantiation but
10994 -- visible when processing the generic formals
11000 E
:= First_Entity
(Act_Decl_Id
);
11001 while Present
(E
) loop
11003 and then not Is_Itype
(E
)
11004 and then Is_Generic_Actual_Type
(E
)
11005 and then Is_Tagged_Type
(E
)
11007 Install_Hidden_Primitives
11008 (Prims_List
=> Vis_Prims_List
,
11009 Gen_T
=> Generic_Parent_Type
(Parent
(E
)),
11017 -- If it is a child unit, make the parent instance (which is an
11018 -- instance of the parent of the generic) visible. The parent
11019 -- instance is the prefix of the name of the generic unit.
11021 if Ekind
(Scope
(Gen_Unit
)) = E_Generic_Package
11022 and then Nkind
(Gen_Id
) = N_Expanded_Name
11024 Par_Ent
:= Entity
(Prefix
(Gen_Id
));
11025 Par_Vis
:= Is_Immediately_Visible
(Par_Ent
);
11026 Install_Parent
(Par_Ent
, In_Body
=> True);
11027 Parent_Installed
:= True;
11029 elsif Is_Child_Unit
(Gen_Unit
) then
11030 Par_Ent
:= Scope
(Gen_Unit
);
11031 Par_Vis
:= Is_Immediately_Visible
(Par_Ent
);
11032 Install_Parent
(Par_Ent
, In_Body
=> True);
11033 Parent_Installed
:= True;
11036 -- If the instantiation is a library unit, and this is the main unit,
11037 -- then build the resulting compilation unit nodes for the instance.
11038 -- If this is a compilation unit but it is not the main unit, then it
11039 -- is the body of a unit in the context, that is being compiled
11040 -- because it is encloses some inlined unit or another generic unit
11041 -- being instantiated. In that case, this body is not part of the
11042 -- current compilation, and is not attached to the tree, but its
11043 -- parent must be set for analysis.
11045 if Nkind
(Parent
(Inst_Node
)) = N_Compilation_Unit
then
11047 -- Replace instance node with body of instance, and create new
11048 -- node for corresponding instance declaration.
11050 Build_Instance_Compilation_Unit_Nodes
11051 (Inst_Node
, Act_Body
, Act_Decl
);
11052 Analyze
(Inst_Node
);
11054 if Parent
(Inst_Node
) = Cunit
(Main_Unit
) then
11056 -- If the instance is a child unit itself, then set the scope
11057 -- of the expanded body to be the parent of the instantiation
11058 -- (ensuring that the fully qualified name will be generated
11059 -- for the elaboration subprogram).
11061 if Nkind
(Defining_Unit_Name
(Act_Spec
)) =
11062 N_Defining_Program_Unit_Name
11064 Set_Scope
(Defining_Entity
(Inst_Node
), Scope
(Act_Decl_Id
));
11068 -- Case where instantiation is not a library unit
11071 -- If this is an early instantiation, i.e. appears textually
11072 -- before the corresponding body and must be elaborated first,
11073 -- indicate that the body instance is to be delayed.
11075 Install_Body
(Act_Body
, Inst_Node
, Gen_Body
, Gen_Decl
);
11077 -- Now analyze the body. We turn off all checks if this is an
11078 -- internal unit, since there is no reason to have checks on for
11079 -- any predefined run-time library code. All such code is designed
11080 -- to be compiled with checks off.
11082 -- Note that we do NOT apply this criterion to children of GNAT
11083 -- The latter units must suppress checks explicitly if needed.
11085 -- We also do not suppress checks in CodePeer mode where we are
11086 -- interested in finding possible runtime errors.
11088 if not CodePeer_Mode
11089 and then Is_Predefined_File_Name
11090 (Unit_File_Name
(Get_Source_Unit
(Gen_Decl
)))
11092 Analyze
(Act_Body
, Suppress
=> All_Checks
);
11094 Analyze
(Act_Body
);
11098 Inherit_Context
(Gen_Body
, Inst_Node
);
11100 -- Remove the parent instances if they have been placed on the scope
11101 -- stack to compile the body.
11103 if Parent_Installed
then
11104 Remove_Parent
(In_Body
=> True);
11106 -- Restore the previous visibility of the parent
11108 Set_Is_Immediately_Visible
(Par_Ent
, Par_Vis
);
11111 Restore_Hidden_Primitives
(Vis_Prims_List
);
11112 Restore_Private_Views
(Act_Decl_Id
);
11114 -- Remove the current unit from visibility if this is an instance
11115 -- that is not elaborated on the fly for inlining purposes.
11117 if not Inlined_Body
then
11118 Set_Is_Immediately_Visible
(Act_Decl_Id
, False);
11122 Ignore_Pragma_SPARK_Mode
:= Save_IPSM
;
11123 Style_Check
:= Save_Style_Check
;
11125 -- If we have no body, and the unit requires a body, then complain. This
11126 -- complaint is suppressed if we have detected other errors (since a
11127 -- common reason for missing the body is that it had errors).
11128 -- In CodePeer mode, a warning has been emitted already, no need for
11129 -- further messages.
11131 elsif Unit_Requires_Body
(Gen_Unit
)
11132 and then not Body_Optional
11134 if CodePeer_Mode
then
11137 elsif Serious_Errors_Detected
= 0 then
11139 ("cannot find body of generic package &", Inst_Node
, Gen_Unit
);
11141 -- Don't attempt to perform any cleanup actions if some other error
11142 -- was already detected, since this can cause blowups.
11148 -- Case of package that does not need a body
11151 -- If the instantiation of the declaration is a library unit, rewrite
11152 -- the original package instantiation as a package declaration in the
11153 -- compilation unit node.
11155 if Nkind
(Parent
(Inst_Node
)) = N_Compilation_Unit
then
11156 Set_Parent_Spec
(Act_Decl
, Parent_Spec
(Inst_Node
));
11157 Rewrite
(Inst_Node
, Act_Decl
);
11159 -- Generate elaboration entity, in case spec has elaboration code.
11160 -- This cannot be done when the instance is analyzed, because it
11161 -- is not known yet whether the body exists.
11163 Set_Elaboration_Entity_Required
(Act_Decl_Id
, False);
11164 Build_Elaboration_Entity
(Parent
(Inst_Node
), Act_Decl_Id
);
11166 -- If the instantiation is not a library unit, then append the
11167 -- declaration to the list of implicitly generated entities, unless
11168 -- it is already a list member which means that it was already
11171 elsif not Is_List_Member
(Act_Decl
) then
11172 Mark_Rewrite_Insertion
(Act_Decl
);
11173 Insert_Before
(Inst_Node
, Act_Decl
);
11177 Expander_Mode_Restore
;
11178 end Instantiate_Package_Body
;
11180 ---------------------------------
11181 -- Instantiate_Subprogram_Body --
11182 ---------------------------------
11184 procedure Instantiate_Subprogram_Body
11185 (Body_Info
: Pending_Body_Info
;
11186 Body_Optional
: Boolean := False)
11188 Act_Decl
: constant Node_Id
:= Body_Info
.Act_Decl
;
11189 Inst_Node
: constant Node_Id
:= Body_Info
.Inst_Node
;
11190 Loc
: constant Source_Ptr
:= Sloc
(Inst_Node
);
11191 Gen_Id
: constant Node_Id
:= Name
(Inst_Node
);
11192 Gen_Unit
: constant Entity_Id
:= Get_Generic_Entity
(Inst_Node
);
11193 Gen_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Gen_Unit
);
11194 Act_Decl_Id
: constant Entity_Id
:=
11195 Defining_Unit_Name
(Specification
(Act_Decl
));
11196 Pack_Id
: constant Entity_Id
:=
11197 Defining_Unit_Name
(Parent
(Act_Decl
));
11199 Saved_IPSM
: constant Boolean := Ignore_Pragma_SPARK_Mode
;
11200 Saved_Style_Check
: constant Boolean := Style_Check
;
11201 Saved_Warnings
: constant Warning_Record
:= Save_Warnings
;
11203 Act_Body
: Node_Id
;
11204 Act_Body_Id
: Entity_Id
;
11205 Gen_Body
: Node_Id
;
11206 Gen_Body_Id
: Node_Id
;
11207 Pack_Body
: Node_Id
;
11208 Par_Ent
: Entity_Id
:= Empty
;
11209 Par_Vis
: Boolean := False;
11210 Ret_Expr
: Node_Id
;
11212 Parent_Installed
: Boolean := False;
11215 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
11217 -- Subprogram body may have been created already because of an inline
11218 -- pragma, or because of multiple elaborations of the enclosing package
11219 -- when several instances of the subprogram appear in the main unit.
11221 if Present
(Corresponding_Body
(Act_Decl
)) then
11225 Expander_Mode_Save_And_Set
(Body_Info
.Expander_Status
);
11227 -- Re-establish the state of information on which checks are suppressed.
11228 -- This information was set in Body_Info at the point of instantiation,
11229 -- and now we restore it so that the instance is compiled using the
11230 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11232 Local_Suppress_Stack_Top
:= Body_Info
.Local_Suppress_Stack_Top
;
11233 Scope_Suppress
:= Body_Info
.Scope_Suppress
;
11234 Opt
.Ada_Version
:= Body_Info
.Version
;
11235 Opt
.Ada_Version_Pragma
:= Body_Info
.Version_Pragma
;
11236 Restore_Warnings
(Body_Info
.Warnings
);
11237 Opt
.SPARK_Mode
:= Body_Info
.SPARK_Mode
;
11238 Opt
.SPARK_Mode_Pragma
:= Body_Info
.SPARK_Mode_Pragma
;
11240 if No
(Gen_Body_Id
) then
11242 -- For imported generic subprogram, no body to compile, complete
11243 -- the spec entity appropriately.
11245 if Is_Imported
(Gen_Unit
) then
11246 Set_Is_Imported
(Act_Decl_Id
);
11247 Set_First_Rep_Item
(Act_Decl_Id
, First_Rep_Item
(Gen_Unit
));
11248 Set_Interface_Name
(Act_Decl_Id
, Interface_Name
(Gen_Unit
));
11249 Set_Convention
(Act_Decl_Id
, Convention
(Gen_Unit
));
11250 Set_Has_Completion
(Act_Decl_Id
);
11253 -- For other cases, compile the body
11256 Load_Parent_Of_Generic
11257 (Inst_Node
, Specification
(Gen_Decl
), Body_Optional
);
11258 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
11262 Instantiation_Node
:= Inst_Node
;
11264 if Present
(Gen_Body_Id
) then
11265 Gen_Body
:= Unit_Declaration_Node
(Gen_Body_Id
);
11267 if Nkind
(Gen_Body
) = N_Subprogram_Body_Stub
then
11269 -- Either body is not present, or context is non-expanding, as
11270 -- when compiling a subunit. Mark the instance as completed, and
11271 -- diagnose a missing body when needed.
11274 and then Operating_Mode
= Generate_Code
11277 ("missing proper body for instantiation", Gen_Body
);
11280 Set_Has_Completion
(Act_Decl_Id
);
11284 Save_Env
(Gen_Unit
, Act_Decl_Id
);
11285 Style_Check
:= False;
11287 -- If the context of the instance is subject to SPARK_Mode "off" or
11288 -- the annotation is altogether missing, set the global flag which
11289 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
11292 if SPARK_Mode
/= On
then
11293 Ignore_Pragma_SPARK_Mode
:= True;
11296 Current_Sem_Unit
:= Body_Info
.Current_Sem_Unit
;
11297 Create_Instantiation_Source
11304 (Original_Node
(Gen_Body
), Empty
, Instantiating
=> True);
11306 -- Create proper defining name for the body, to correspond to the one
11310 Make_Defining_Identifier
(Sloc
(Act_Decl_Id
), Chars
(Act_Decl_Id
));
11312 Set_Comes_From_Source
(Act_Body_Id
, Comes_From_Source
(Act_Decl_Id
));
11313 Set_Defining_Unit_Name
(Specification
(Act_Body
), Act_Body_Id
);
11315 Set_Corresponding_Spec
(Act_Body
, Act_Decl_Id
);
11316 Set_Has_Completion
(Act_Decl_Id
);
11317 Check_Generic_Actuals
(Pack_Id
, False);
11319 -- Generate a reference to link the visible subprogram instance to
11320 -- the generic body, which for navigation purposes is the only
11321 -- available source for the instance.
11324 (Related_Instance
(Pack_Id
),
11325 Gen_Body_Id
, 'b', Set_Ref
=> False, Force
=> True);
11327 -- If it is a child unit, make the parent instance (which is an
11328 -- instance of the parent of the generic) visible. The parent
11329 -- instance is the prefix of the name of the generic unit.
11331 if Ekind
(Scope
(Gen_Unit
)) = E_Generic_Package
11332 and then Nkind
(Gen_Id
) = N_Expanded_Name
11334 Par_Ent
:= Entity
(Prefix
(Gen_Id
));
11335 Par_Vis
:= Is_Immediately_Visible
(Par_Ent
);
11336 Install_Parent
(Par_Ent
, In_Body
=> True);
11337 Parent_Installed
:= True;
11339 elsif Is_Child_Unit
(Gen_Unit
) then
11340 Par_Ent
:= Scope
(Gen_Unit
);
11341 Par_Vis
:= Is_Immediately_Visible
(Par_Ent
);
11342 Install_Parent
(Par_Ent
, In_Body
=> True);
11343 Parent_Installed
:= True;
11346 -- Subprogram body is placed in the body of wrapper package,
11347 -- whose spec contains the subprogram declaration as well as
11348 -- the renaming declarations for the generic parameters.
11351 Make_Package_Body
(Loc
,
11352 Defining_Unit_Name
=> New_Copy
(Pack_Id
),
11353 Declarations
=> New_List
(Act_Body
));
11355 Set_Corresponding_Spec
(Pack_Body
, Pack_Id
);
11357 -- If the instantiation is a library unit, then build resulting
11358 -- compilation unit nodes for the instance. The declaration of
11359 -- the enclosing package is the grandparent of the subprogram
11360 -- declaration. First replace the instantiation node as the unit
11361 -- of the corresponding compilation.
11363 if Nkind
(Parent
(Inst_Node
)) = N_Compilation_Unit
then
11364 if Parent
(Inst_Node
) = Cunit
(Main_Unit
) then
11365 Set_Unit
(Parent
(Inst_Node
), Inst_Node
);
11366 Build_Instance_Compilation_Unit_Nodes
11367 (Inst_Node
, Pack_Body
, Parent
(Parent
(Act_Decl
)));
11368 Analyze
(Inst_Node
);
11370 Set_Parent
(Pack_Body
, Parent
(Inst_Node
));
11371 Analyze
(Pack_Body
);
11375 Insert_Before
(Inst_Node
, Pack_Body
);
11376 Mark_Rewrite_Insertion
(Pack_Body
);
11377 Analyze
(Pack_Body
);
11379 if Expander_Active
then
11380 Freeze_Subprogram_Body
(Inst_Node
, Gen_Body
, Pack_Id
);
11384 Inherit_Context
(Gen_Body
, Inst_Node
);
11386 Restore_Private_Views
(Pack_Id
, False);
11388 if Parent_Installed
then
11389 Remove_Parent
(In_Body
=> True);
11391 -- Restore the previous visibility of the parent
11393 Set_Is_Immediately_Visible
(Par_Ent
, Par_Vis
);
11397 Ignore_Pragma_SPARK_Mode
:= Saved_IPSM
;
11398 Style_Check
:= Saved_Style_Check
;
11399 Restore_Warnings
(Saved_Warnings
);
11401 -- Body not found. Error was emitted already. If there were no previous
11402 -- errors, this may be an instance whose scope is a premature instance.
11403 -- In that case we must insure that the (legal) program does raise
11404 -- program error if executed. We generate a subprogram body for this
11405 -- purpose. See DEC ac30vso.
11407 -- Should not reference proprietary DEC tests in comments ???
11409 elsif Serious_Errors_Detected
= 0
11410 and then Nkind
(Parent
(Inst_Node
)) /= N_Compilation_Unit
11412 if Body_Optional
then
11415 elsif Ekind
(Act_Decl_Id
) = E_Procedure
then
11417 Make_Subprogram_Body
(Loc
,
11419 Make_Procedure_Specification
(Loc
,
11420 Defining_Unit_Name
=>
11421 Make_Defining_Identifier
(Loc
, Chars
(Act_Decl_Id
)),
11422 Parameter_Specifications
=>
11424 (Parameter_Specifications
(Parent
(Act_Decl_Id
)))),
11426 Declarations
=> Empty_List
,
11427 Handled_Statement_Sequence
=>
11428 Make_Handled_Sequence_Of_Statements
(Loc
,
11431 Make_Raise_Program_Error
(Loc
,
11433 PE_Access_Before_Elaboration
))));
11437 Make_Raise_Program_Error
(Loc
,
11438 Reason
=> PE_Access_Before_Elaboration
);
11440 Set_Etype
(Ret_Expr
, (Etype
(Act_Decl_Id
)));
11441 Set_Analyzed
(Ret_Expr
);
11444 Make_Subprogram_Body
(Loc
,
11446 Make_Function_Specification
(Loc
,
11447 Defining_Unit_Name
=>
11448 Make_Defining_Identifier
(Loc
, Chars
(Act_Decl_Id
)),
11449 Parameter_Specifications
=>
11451 (Parameter_Specifications
(Parent
(Act_Decl_Id
))),
11452 Result_Definition
=>
11453 New_Occurrence_Of
(Etype
(Act_Decl_Id
), Loc
)),
11455 Declarations
=> Empty_List
,
11456 Handled_Statement_Sequence
=>
11457 Make_Handled_Sequence_Of_Statements
(Loc
,
11460 (Make_Simple_Return_Statement
(Loc
, Ret_Expr
))));
11464 Make_Package_Body
(Loc
,
11465 Defining_Unit_Name
=> New_Copy
(Pack_Id
),
11466 Declarations
=> New_List
(Act_Body
));
11468 Insert_After
(Inst_Node
, Pack_Body
);
11469 Set_Corresponding_Spec
(Pack_Body
, Pack_Id
);
11470 Analyze
(Pack_Body
);
11473 Expander_Mode_Restore
;
11474 end Instantiate_Subprogram_Body
;
11476 ----------------------
11477 -- Instantiate_Type --
11478 ----------------------
11480 function Instantiate_Type
11483 Analyzed_Formal
: Node_Id
;
11484 Actual_Decls
: List_Id
) return List_Id
11486 Gen_T
: constant Entity_Id
:= Defining_Identifier
(Formal
);
11487 A_Gen_T
: constant Entity_Id
:=
11488 Defining_Identifier
(Analyzed_Formal
);
11489 Ancestor
: Entity_Id
:= Empty
;
11490 Def
: constant Node_Id
:= Formal_Type_Definition
(Formal
);
11492 Decl_Node
: Node_Id
;
11493 Decl_Nodes
: List_Id
;
11497 procedure Diagnose_Predicated_Actual
;
11498 -- There are a number of constructs in which a discrete type with
11499 -- predicates is illegal, e.g. as an index in an array type declaration.
11500 -- If a generic type is used is such a construct in a generic package
11501 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11502 -- of the generic contract that the actual cannot have predicates.
11504 procedure Validate_Array_Type_Instance
;
11505 procedure Validate_Access_Subprogram_Instance
;
11506 procedure Validate_Access_Type_Instance
;
11507 procedure Validate_Derived_Type_Instance
;
11508 procedure Validate_Derived_Interface_Type_Instance
;
11509 procedure Validate_Discriminated_Formal_Type
;
11510 procedure Validate_Interface_Type_Instance
;
11511 procedure Validate_Private_Type_Instance
;
11512 procedure Validate_Incomplete_Type_Instance
;
11513 -- These procedures perform validation tests for the named case.
11514 -- Validate_Discriminated_Formal_Type is shared by formal private
11515 -- types and Ada 2012 formal incomplete types.
11517 function Subtypes_Match
(Gen_T
, Act_T
: Entity_Id
) return Boolean;
11518 -- Check that base types are the same and that the subtypes match
11519 -- statically. Used in several of the above.
11521 ---------------------------------
11522 -- Diagnose_Predicated_Actual --
11523 ---------------------------------
11525 procedure Diagnose_Predicated_Actual
is
11527 if No_Predicate_On_Actual
(A_Gen_T
)
11528 and then Has_Predicates
(Act_T
)
11531 ("actual for& cannot be a type with predicate",
11532 Instantiation_Node
, A_Gen_T
);
11534 elsif No_Dynamic_Predicate_On_Actual
(A_Gen_T
)
11535 and then Has_Predicates
(Act_T
)
11536 and then not Has_Static_Predicate_Aspect
(Act_T
)
11539 ("actual for& cannot be a type with a dynamic predicate",
11540 Instantiation_Node
, A_Gen_T
);
11542 end Diagnose_Predicated_Actual
;
11544 --------------------
11545 -- Subtypes_Match --
11546 --------------------
11548 function Subtypes_Match
(Gen_T
, Act_T
: Entity_Id
) return Boolean is
11549 T
: constant Entity_Id
:= Get_Instance_Of
(Gen_T
);
11552 -- Some detailed comments would be useful here ???
11554 return ((Base_Type
(T
) = Act_T
11555 or else Base_Type
(T
) = Base_Type
(Act_T
))
11556 and then Subtypes_Statically_Match
(T
, Act_T
))
11558 or else (Is_Class_Wide_Type
(Gen_T
)
11559 and then Is_Class_Wide_Type
(Act_T
)
11560 and then Subtypes_Match
11561 (Get_Instance_Of
(Root_Type
(Gen_T
)),
11562 Root_Type
(Act_T
)))
11565 (Ekind_In
(Gen_T
, E_Anonymous_Access_Subprogram_Type
,
11566 E_Anonymous_Access_Type
)
11567 and then Ekind
(Act_T
) = Ekind
(Gen_T
)
11568 and then Subtypes_Statically_Match
11569 (Designated_Type
(Gen_T
), Designated_Type
(Act_T
)));
11570 end Subtypes_Match
;
11572 -----------------------------------------
11573 -- Validate_Access_Subprogram_Instance --
11574 -----------------------------------------
11576 procedure Validate_Access_Subprogram_Instance
is
11578 if not Is_Access_Type
(Act_T
)
11579 or else Ekind
(Designated_Type
(Act_T
)) /= E_Subprogram_Type
11582 ("expect access type in instantiation of &", Actual
, Gen_T
);
11583 Abandon_Instantiation
(Actual
);
11586 -- According to AI05-288, actuals for access_to_subprograms must be
11587 -- subtype conformant with the generic formal. Previous to AI05-288
11588 -- only mode conformance was required.
11590 -- This is a binding interpretation that applies to previous versions
11591 -- of the language, no need to maintain previous weaker checks.
11593 Check_Subtype_Conformant
11594 (Designated_Type
(Act_T
),
11595 Designated_Type
(A_Gen_T
),
11599 if Ekind
(Base_Type
(Act_T
)) = E_Access_Protected_Subprogram_Type
then
11600 if Ekind
(A_Gen_T
) = E_Access_Subprogram_Type
then
11602 ("protected access type not allowed for formal &",
11606 elsif Ekind
(A_Gen_T
) = E_Access_Protected_Subprogram_Type
then
11608 ("expect protected access type for formal &",
11612 -- If the formal has a specified convention (which in most cases
11613 -- will be StdCall) verify that the actual has the same convention.
11615 if Has_Convention_Pragma
(A_Gen_T
)
11616 and then Convention
(A_Gen_T
) /= Convention
(Act_T
)
11618 Error_Msg_Name_1
:= Get_Convention_Name
(Convention
(A_Gen_T
));
11620 ("actual for formal & must have convention %", Actual
, Gen_T
);
11622 end Validate_Access_Subprogram_Instance
;
11624 -----------------------------------
11625 -- Validate_Access_Type_Instance --
11626 -----------------------------------
11628 procedure Validate_Access_Type_Instance
is
11629 Desig_Type
: constant Entity_Id
:=
11630 Find_Actual_Type
(Designated_Type
(A_Gen_T
), A_Gen_T
);
11631 Desig_Act
: Entity_Id
;
11634 if not Is_Access_Type
(Act_T
) then
11636 ("expect access type in instantiation of &", Actual
, Gen_T
);
11637 Abandon_Instantiation
(Actual
);
11640 if Is_Access_Constant
(A_Gen_T
) then
11641 if not Is_Access_Constant
(Act_T
) then
11643 ("actual type must be access-to-constant type", Actual
);
11644 Abandon_Instantiation
(Actual
);
11647 if Is_Access_Constant
(Act_T
) then
11649 ("actual type must be access-to-variable type", Actual
);
11650 Abandon_Instantiation
(Actual
);
11652 elsif Ekind
(A_Gen_T
) = E_General_Access_Type
11653 and then Ekind
(Base_Type
(Act_T
)) /= E_General_Access_Type
11655 Error_Msg_N
-- CODEFIX
11656 ("actual must be general access type!", Actual
);
11657 Error_Msg_NE
-- CODEFIX
11658 ("add ALL to }!", Actual
, Act_T
);
11659 Abandon_Instantiation
(Actual
);
11663 -- The designated subtypes, that is to say the subtypes introduced
11664 -- by an access type declaration (and not by a subtype declaration)
11667 Desig_Act
:= Designated_Type
(Base_Type
(Act_T
));
11669 -- The designated type may have been introduced through a limited_
11670 -- with clause, in which case retrieve the non-limited view. This
11671 -- applies to incomplete types as well as to class-wide types.
11673 if From_Limited_With
(Desig_Act
) then
11674 Desig_Act
:= Available_View
(Desig_Act
);
11677 if not Subtypes_Match
(Desig_Type
, Desig_Act
) then
11679 ("designated type of actual does not match that of formal &",
11682 if not Predicates_Match
(Desig_Type
, Desig_Act
) then
11683 Error_Msg_N
("\predicates do not match", Actual
);
11686 Abandon_Instantiation
(Actual
);
11688 elsif Is_Access_Type
(Designated_Type
(Act_T
))
11689 and then Is_Constrained
(Designated_Type
(Designated_Type
(Act_T
)))
11691 Is_Constrained
(Designated_Type
(Desig_Type
))
11694 ("designated type of actual does not match that of formal &",
11697 if not Predicates_Match
(Desig_Type
, Desig_Act
) then
11698 Error_Msg_N
("\predicates do not match", Actual
);
11701 Abandon_Instantiation
(Actual
);
11704 -- Ada 2005: null-exclusion indicators of the two types must agree
11706 if Can_Never_Be_Null
(A_Gen_T
) /= Can_Never_Be_Null
(Act_T
) then
11708 ("non null exclusion of actual and formal & do not match",
11711 end Validate_Access_Type_Instance
;
11713 ----------------------------------
11714 -- Validate_Array_Type_Instance --
11715 ----------------------------------
11717 procedure Validate_Array_Type_Instance
is
11722 function Formal_Dimensions
return Nat
;
11723 -- Count number of dimensions in array type formal
11725 -----------------------
11726 -- Formal_Dimensions --
11727 -----------------------
11729 function Formal_Dimensions
return Nat
is
11734 if Nkind
(Def
) = N_Constrained_Array_Definition
then
11735 Index
:= First
(Discrete_Subtype_Definitions
(Def
));
11737 Index
:= First
(Subtype_Marks
(Def
));
11740 while Present
(Index
) loop
11742 Next_Index
(Index
);
11746 end Formal_Dimensions
;
11748 -- Start of processing for Validate_Array_Type_Instance
11751 if not Is_Array_Type
(Act_T
) then
11753 ("expect array type in instantiation of &", Actual
, Gen_T
);
11754 Abandon_Instantiation
(Actual
);
11756 elsif Nkind
(Def
) = N_Constrained_Array_Definition
then
11757 if not (Is_Constrained
(Act_T
)) then
11759 ("expect constrained array in instantiation of &",
11761 Abandon_Instantiation
(Actual
);
11765 if Is_Constrained
(Act_T
) then
11767 ("expect unconstrained array in instantiation of &",
11769 Abandon_Instantiation
(Actual
);
11773 if Formal_Dimensions
/= Number_Dimensions
(Act_T
) then
11775 ("dimensions of actual do not match formal &", Actual
, Gen_T
);
11776 Abandon_Instantiation
(Actual
);
11779 I1
:= First_Index
(A_Gen_T
);
11780 I2
:= First_Index
(Act_T
);
11781 for J
in 1 .. Formal_Dimensions
loop
11783 -- If the indexes of the actual were given by a subtype_mark,
11784 -- the index was transformed into a range attribute. Retrieve
11785 -- the original type mark for checking.
11787 if Is_Entity_Name
(Original_Node
(I2
)) then
11788 T2
:= Entity
(Original_Node
(I2
));
11793 if not Subtypes_Match
11794 (Find_Actual_Type
(Etype
(I1
), A_Gen_T
), T2
)
11797 ("index types of actual do not match those of formal &",
11799 Abandon_Instantiation
(Actual
);
11806 -- Check matching subtypes. Note that there are complex visibility
11807 -- issues when the generic is a child unit and some aspect of the
11808 -- generic type is declared in a parent unit of the generic. We do
11809 -- the test to handle this special case only after a direct check
11810 -- for static matching has failed. The case where both the component
11811 -- type and the array type are separate formals, and the component
11812 -- type is a private view may also require special checking in
11816 (Component_Type
(A_Gen_T
), Component_Type
(Act_T
))
11819 (Find_Actual_Type
(Component_Type
(A_Gen_T
), A_Gen_T
),
11820 Component_Type
(Act_T
))
11825 ("component subtype of actual does not match that of formal &",
11827 Abandon_Instantiation
(Actual
);
11830 if Has_Aliased_Components
(A_Gen_T
)
11831 and then not Has_Aliased_Components
(Act_T
)
11834 ("actual must have aliased components to match formal type &",
11837 end Validate_Array_Type_Instance
;
11839 -----------------------------------------------
11840 -- Validate_Derived_Interface_Type_Instance --
11841 -----------------------------------------------
11843 procedure Validate_Derived_Interface_Type_Instance
is
11844 Par
: constant Entity_Id
:= Entity
(Subtype_Indication
(Def
));
11848 -- First apply interface instance checks
11850 Validate_Interface_Type_Instance
;
11852 -- Verify that immediate parent interface is an ancestor of
11856 and then not Interface_Present_In_Ancestor
(Act_T
, Par
)
11859 ("interface actual must include progenitor&", Actual
, Par
);
11862 -- Now verify that the actual includes all other ancestors of
11865 Elmt
:= First_Elmt
(Interfaces
(A_Gen_T
));
11866 while Present
(Elmt
) loop
11867 if not Interface_Present_In_Ancestor
11868 (Act_T
, Get_Instance_Of
(Node
(Elmt
)))
11871 ("interface actual must include progenitor&",
11872 Actual
, Node
(Elmt
));
11877 end Validate_Derived_Interface_Type_Instance
;
11879 ------------------------------------
11880 -- Validate_Derived_Type_Instance --
11881 ------------------------------------
11883 procedure Validate_Derived_Type_Instance
is
11884 Actual_Discr
: Entity_Id
;
11885 Ancestor_Discr
: Entity_Id
;
11888 -- If the parent type in the generic declaration is itself a previous
11889 -- formal type, then it is local to the generic and absent from the
11890 -- analyzed generic definition. In that case the ancestor is the
11891 -- instance of the formal (which must have been instantiated
11892 -- previously), unless the ancestor is itself a formal derived type.
11893 -- In this latter case (which is the subject of Corrigendum 8652/0038
11894 -- (AI-202) the ancestor of the formals is the ancestor of its
11895 -- parent. Otherwise, the analyzed generic carries the parent type.
11896 -- If the parent type is defined in a previous formal package, then
11897 -- the scope of that formal package is that of the generic type
11898 -- itself, and it has already been mapped into the corresponding type
11899 -- in the actual package.
11901 -- Common case: parent type defined outside of the generic
11903 if Is_Entity_Name
(Subtype_Mark
(Def
))
11904 and then Present
(Entity
(Subtype_Mark
(Def
)))
11906 Ancestor
:= Get_Instance_Of
(Entity
(Subtype_Mark
(Def
)));
11908 -- Check whether parent is defined in a previous formal package
11911 Scope
(Scope
(Base_Type
(Etype
(A_Gen_T
)))) = Scope
(A_Gen_T
)
11914 Get_Instance_Of
(Base_Type
(Etype
(A_Gen_T
)));
11916 -- The type may be a local derivation, or a type extension of a
11917 -- previous formal, or of a formal of a parent package.
11919 elsif Is_Derived_Type
(Get_Instance_Of
(A_Gen_T
))
11921 Ekind
(Get_Instance_Of
(A_Gen_T
)) = E_Record_Type_With_Private
11923 -- Check whether the parent is another derived formal type in the
11924 -- same generic unit.
11926 if Etype
(A_Gen_T
) /= A_Gen_T
11927 and then Is_Generic_Type
(Etype
(A_Gen_T
))
11928 and then Scope
(Etype
(A_Gen_T
)) = Scope
(A_Gen_T
)
11929 and then Etype
(Etype
(A_Gen_T
)) /= Etype
(A_Gen_T
)
11931 -- Locate ancestor of parent from the subtype declaration
11932 -- created for the actual.
11938 Decl
:= First
(Actual_Decls
);
11939 while Present
(Decl
) loop
11940 if Nkind
(Decl
) = N_Subtype_Declaration
11941 and then Chars
(Defining_Identifier
(Decl
)) =
11942 Chars
(Etype
(A_Gen_T
))
11944 Ancestor
:= Generic_Parent_Type
(Decl
);
11952 pragma Assert
(Present
(Ancestor
));
11954 -- The ancestor itself may be a previous formal that has been
11957 Ancestor
:= Get_Instance_Of
(Ancestor
);
11961 Get_Instance_Of
(Base_Type
(Get_Instance_Of
(A_Gen_T
)));
11964 -- Check whether parent is a previous formal of the current generic
11966 elsif Is_Derived_Type
(A_Gen_T
)
11967 and then Is_Generic_Type
(Etype
(A_Gen_T
))
11968 and then Scope
(A_Gen_T
) = Scope
(Etype
(A_Gen_T
))
11970 Ancestor
:= Get_Instance_Of
(First_Subtype
(Etype
(A_Gen_T
)));
11972 -- An unusual case: the actual is a type declared in a parent unit,
11973 -- but is not a formal type so there is no instance_of for it.
11974 -- Retrieve it by analyzing the record extension.
11976 elsif Is_Child_Unit
(Scope
(A_Gen_T
))
11977 and then In_Open_Scopes
(Scope
(Act_T
))
11978 and then Is_Generic_Instance
(Scope
(Act_T
))
11980 Analyze
(Subtype_Mark
(Def
));
11981 Ancestor
:= Entity
(Subtype_Mark
(Def
));
11984 Ancestor
:= Get_Instance_Of
(Etype
(Base_Type
(A_Gen_T
)));
11987 -- If the formal derived type has pragma Preelaborable_Initialization
11988 -- then the actual type must have preelaborable initialization.
11990 if Known_To_Have_Preelab_Init
(A_Gen_T
)
11991 and then not Has_Preelaborable_Initialization
(Act_T
)
11994 ("actual for & must have preelaborable initialization",
11998 -- Ada 2005 (AI-251)
12000 if Ada_Version
>= Ada_2005
and then Is_Interface
(Ancestor
) then
12001 if not Interface_Present_In_Ancestor
(Act_T
, Ancestor
) then
12003 ("(Ada 2005) expected type implementing & in instantiation",
12007 -- Finally verify that the (instance of) the ancestor is an ancestor
12010 elsif not Is_Ancestor
(Base_Type
(Ancestor
), Act_T
) then
12012 ("expect type derived from & in instantiation",
12013 Actual
, First_Subtype
(Ancestor
));
12014 Abandon_Instantiation
(Actual
);
12017 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
12018 -- that the formal type declaration has been rewritten as a private
12021 if Ada_Version
>= Ada_2005
12022 and then Nkind
(Parent
(A_Gen_T
)) = N_Private_Extension_Declaration
12023 and then Synchronized_Present
(Parent
(A_Gen_T
))
12025 -- The actual must be a synchronized tagged type
12027 if not Is_Tagged_Type
(Act_T
) then
12029 ("actual of synchronized type must be tagged", Actual
);
12030 Abandon_Instantiation
(Actual
);
12032 elsif Nkind
(Parent
(Act_T
)) = N_Full_Type_Declaration
12033 and then Nkind
(Type_Definition
(Parent
(Act_T
))) =
12034 N_Derived_Type_Definition
12035 and then not Synchronized_Present
12036 (Type_Definition
(Parent
(Act_T
)))
12039 ("actual of synchronized type must be synchronized", Actual
);
12040 Abandon_Instantiation
(Actual
);
12044 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
12045 -- removes the second instance of the phrase "or allow pass by copy".
12047 if Is_Atomic
(Act_T
) and then not Is_Atomic
(Ancestor
) then
12049 ("cannot have atomic actual type for non-atomic formal type",
12052 elsif Is_Volatile
(Act_T
) and then not Is_Volatile
(Ancestor
) then
12054 ("cannot have volatile actual type for non-volatile formal type",
12058 -- It should not be necessary to check for unknown discriminants on
12059 -- Formal, but for some reason Has_Unknown_Discriminants is false for
12060 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
12061 -- needs fixing. ???
12063 if Is_Definite_Subtype
(A_Gen_T
)
12064 and then not Unknown_Discriminants_Present
(Formal
)
12065 and then not Is_Definite_Subtype
(Act_T
)
12067 Error_Msg_N
("actual subtype must be constrained", Actual
);
12068 Abandon_Instantiation
(Actual
);
12071 if not Unknown_Discriminants_Present
(Formal
) then
12072 if Is_Constrained
(Ancestor
) then
12073 if not Is_Constrained
(Act_T
) then
12074 Error_Msg_N
("actual subtype must be constrained", Actual
);
12075 Abandon_Instantiation
(Actual
);
12078 -- Ancestor is unconstrained, Check if generic formal and actual
12079 -- agree on constrainedness. The check only applies to array types
12080 -- and discriminated types.
12082 elsif Is_Constrained
(Act_T
) then
12083 if Ekind
(Ancestor
) = E_Access_Type
12084 or else (not Is_Constrained
(A_Gen_T
)
12085 and then Is_Composite_Type
(A_Gen_T
))
12087 Error_Msg_N
("actual subtype must be unconstrained", Actual
);
12088 Abandon_Instantiation
(Actual
);
12091 -- A class-wide type is only allowed if the formal has unknown
12094 elsif Is_Class_Wide_Type
(Act_T
)
12095 and then not Has_Unknown_Discriminants
(Ancestor
)
12098 ("actual for & cannot be a class-wide type", Actual
, Gen_T
);
12099 Abandon_Instantiation
(Actual
);
12101 -- Otherwise, the formal and actual must have the same number
12102 -- of discriminants and each discriminant of the actual must
12103 -- correspond to a discriminant of the formal.
12105 elsif Has_Discriminants
(Act_T
)
12106 and then not Has_Unknown_Discriminants
(Act_T
)
12107 and then Has_Discriminants
(Ancestor
)
12109 Actual_Discr
:= First_Discriminant
(Act_T
);
12110 Ancestor_Discr
:= First_Discriminant
(Ancestor
);
12111 while Present
(Actual_Discr
)
12112 and then Present
(Ancestor_Discr
)
12114 if Base_Type
(Act_T
) /= Base_Type
(Ancestor
) and then
12115 No
(Corresponding_Discriminant
(Actual_Discr
))
12118 ("discriminant & does not correspond "
12119 & "to ancestor discriminant", Actual
, Actual_Discr
);
12120 Abandon_Instantiation
(Actual
);
12123 Next_Discriminant
(Actual_Discr
);
12124 Next_Discriminant
(Ancestor_Discr
);
12127 if Present
(Actual_Discr
) or else Present
(Ancestor_Discr
) then
12129 ("actual for & must have same number of discriminants",
12131 Abandon_Instantiation
(Actual
);
12134 -- This case should be caught by the earlier check for
12135 -- constrainedness, but the check here is added for completeness.
12137 elsif Has_Discriminants
(Act_T
)
12138 and then not Has_Unknown_Discriminants
(Act_T
)
12141 ("actual for & must not have discriminants", Actual
, Gen_T
);
12142 Abandon_Instantiation
(Actual
);
12144 elsif Has_Discriminants
(Ancestor
) then
12146 ("actual for & must have known discriminants", Actual
, Gen_T
);
12147 Abandon_Instantiation
(Actual
);
12150 if not Subtypes_Statically_Compatible
12151 (Act_T
, Ancestor
, Formal_Derived_Matching
=> True)
12154 ("constraint on actual is incompatible with formal", Actual
);
12155 Abandon_Instantiation
(Actual
);
12159 -- If the formal and actual types are abstract, check that there
12160 -- are no abstract primitives of the actual type that correspond to
12161 -- nonabstract primitives of the formal type (second sentence of
12164 if Is_Abstract_Type
(A_Gen_T
) and then Is_Abstract_Type
(Act_T
) then
12165 Check_Abstract_Primitives
: declare
12166 Gen_Prims
: constant Elist_Id
:=
12167 Primitive_Operations
(A_Gen_T
);
12168 Gen_Elmt
: Elmt_Id
;
12169 Gen_Subp
: Entity_Id
;
12170 Anc_Subp
: Entity_Id
;
12171 Anc_Formal
: Entity_Id
;
12172 Anc_F_Type
: Entity_Id
;
12174 Act_Prims
: constant Elist_Id
:= Primitive_Operations
(Act_T
);
12175 Act_Elmt
: Elmt_Id
;
12176 Act_Subp
: Entity_Id
;
12177 Act_Formal
: Entity_Id
;
12178 Act_F_Type
: Entity_Id
;
12180 Subprograms_Correspond
: Boolean;
12182 function Is_Tagged_Ancestor
(T1
, T2
: Entity_Id
) return Boolean;
12183 -- Returns true if T2 is derived directly or indirectly from
12184 -- T1, including derivations from interfaces. T1 and T2 are
12185 -- required to be specific tagged base types.
12187 ------------------------
12188 -- Is_Tagged_Ancestor --
12189 ------------------------
12191 function Is_Tagged_Ancestor
(T1
, T2
: Entity_Id
) return Boolean
12193 Intfc_Elmt
: Elmt_Id
;
12196 -- The predicate is satisfied if the types are the same
12201 -- If we've reached the top of the derivation chain then
12202 -- we know that T1 is not an ancestor of T2.
12204 elsif Etype
(T2
) = T2
then
12207 -- Proceed to check T2's immediate parent
12209 elsif Is_Ancestor
(T1
, Base_Type
(Etype
(T2
))) then
12212 -- Finally, check to see if T1 is an ancestor of any of T2's
12216 Intfc_Elmt
:= First_Elmt
(Interfaces
(T2
));
12217 while Present
(Intfc_Elmt
) loop
12218 if Is_Ancestor
(T1
, Node
(Intfc_Elmt
)) then
12222 Next_Elmt
(Intfc_Elmt
);
12227 end Is_Tagged_Ancestor
;
12229 -- Start of processing for Check_Abstract_Primitives
12232 -- Loop over all of the formal derived type's primitives
12234 Gen_Elmt
:= First_Elmt
(Gen_Prims
);
12235 while Present
(Gen_Elmt
) loop
12236 Gen_Subp
:= Node
(Gen_Elmt
);
12238 -- If the primitive of the formal is not abstract, then
12239 -- determine whether there is a corresponding primitive of
12240 -- the actual type that's abstract.
12242 if not Is_Abstract_Subprogram
(Gen_Subp
) then
12243 Act_Elmt
:= First_Elmt
(Act_Prims
);
12244 while Present
(Act_Elmt
) loop
12245 Act_Subp
:= Node
(Act_Elmt
);
12247 -- If we find an abstract primitive of the actual,
12248 -- then we need to test whether it corresponds to the
12249 -- subprogram from which the generic formal primitive
12252 if Is_Abstract_Subprogram
(Act_Subp
) then
12253 Anc_Subp
:= Alias
(Gen_Subp
);
12255 -- Test whether we have a corresponding primitive
12256 -- by comparing names, kinds, formal types, and
12259 if Chars
(Anc_Subp
) = Chars
(Act_Subp
)
12260 and then Ekind
(Anc_Subp
) = Ekind
(Act_Subp
)
12262 Anc_Formal
:= First_Formal
(Anc_Subp
);
12263 Act_Formal
:= First_Formal
(Act_Subp
);
12264 while Present
(Anc_Formal
)
12265 and then Present
(Act_Formal
)
12267 Anc_F_Type
:= Etype
(Anc_Formal
);
12268 Act_F_Type
:= Etype
(Act_Formal
);
12270 if Ekind
(Anc_F_Type
) =
12271 E_Anonymous_Access_Type
12273 Anc_F_Type
:= Designated_Type
(Anc_F_Type
);
12275 if Ekind
(Act_F_Type
) =
12276 E_Anonymous_Access_Type
12279 Designated_Type
(Act_F_Type
);
12285 Ekind
(Act_F_Type
) = E_Anonymous_Access_Type
12290 Anc_F_Type
:= Base_Type
(Anc_F_Type
);
12291 Act_F_Type
:= Base_Type
(Act_F_Type
);
12293 -- If the formal is controlling, then the
12294 -- the type of the actual primitive's formal
12295 -- must be derived directly or indirectly
12296 -- from the type of the ancestor primitive's
12299 if Is_Controlling_Formal
(Anc_Formal
) then
12300 if not Is_Tagged_Ancestor
12301 (Anc_F_Type
, Act_F_Type
)
12306 -- Otherwise the types of the formals must
12309 elsif Anc_F_Type
/= Act_F_Type
then
12313 Next_Entity
(Anc_Formal
);
12314 Next_Entity
(Act_Formal
);
12317 -- If we traversed through all of the formals
12318 -- then so far the subprograms correspond, so
12319 -- now check that any result types correspond.
12321 if No
(Anc_Formal
) and then No
(Act_Formal
) then
12322 Subprograms_Correspond
:= True;
12324 if Ekind
(Act_Subp
) = E_Function
then
12325 Anc_F_Type
:= Etype
(Anc_Subp
);
12326 Act_F_Type
:= Etype
(Act_Subp
);
12328 if Ekind
(Anc_F_Type
) =
12329 E_Anonymous_Access_Type
12332 Designated_Type
(Anc_F_Type
);
12334 if Ekind
(Act_F_Type
) =
12335 E_Anonymous_Access_Type
12338 Designated_Type
(Act_F_Type
);
12340 Subprograms_Correspond
:= False;
12345 = E_Anonymous_Access_Type
12347 Subprograms_Correspond
:= False;
12350 Anc_F_Type
:= Base_Type
(Anc_F_Type
);
12351 Act_F_Type
:= Base_Type
(Act_F_Type
);
12353 -- Now either the result types must be
12354 -- the same or, if the result type is
12355 -- controlling, the result type of the
12356 -- actual primitive must descend from the
12357 -- result type of the ancestor primitive.
12359 if Subprograms_Correspond
12360 and then Anc_F_Type
/= Act_F_Type
12362 Has_Controlling_Result
(Anc_Subp
)
12363 and then not Is_Tagged_Ancestor
12364 (Anc_F_Type
, Act_F_Type
)
12366 Subprograms_Correspond
:= False;
12370 -- Found a matching subprogram belonging to
12371 -- formal ancestor type, so actual subprogram
12372 -- corresponds and this violates 3.9.3(9).
12374 if Subprograms_Correspond
then
12376 ("abstract subprogram & overrides "
12377 & "nonabstract subprogram of ancestor",
12384 Next_Elmt
(Act_Elmt
);
12388 Next_Elmt
(Gen_Elmt
);
12390 end Check_Abstract_Primitives
;
12393 -- Verify that limitedness matches. If parent is a limited
12394 -- interface then the generic formal is not unless declared
12395 -- explicitly so. If not declared limited, the actual cannot be
12396 -- limited (see AI05-0087).
12398 -- Even though this AI is a binding interpretation, we enable the
12399 -- check only in Ada 2012 mode, because this improper construct
12400 -- shows up in user code and in existing B-tests.
12402 if Is_Limited_Type
(Act_T
)
12403 and then not Is_Limited_Type
(A_Gen_T
)
12404 and then Ada_Version
>= Ada_2012
12406 if In_Instance
then
12410 ("actual for non-limited & cannot be a limited type",
12412 Explain_Limited_Type
(Act_T
, Actual
);
12413 Abandon_Instantiation
(Actual
);
12416 end Validate_Derived_Type_Instance
;
12418 ----------------------------------------
12419 -- Validate_Discriminated_Formal_Type --
12420 ----------------------------------------
12422 procedure Validate_Discriminated_Formal_Type
is
12423 Formal_Discr
: Entity_Id
;
12424 Actual_Discr
: Entity_Id
;
12425 Formal_Subt
: Entity_Id
;
12428 if Has_Discriminants
(A_Gen_T
) then
12429 if not Has_Discriminants
(Act_T
) then
12431 ("actual for & must have discriminants", Actual
, Gen_T
);
12432 Abandon_Instantiation
(Actual
);
12434 elsif Is_Constrained
(Act_T
) then
12436 ("actual for & must be unconstrained", Actual
, Gen_T
);
12437 Abandon_Instantiation
(Actual
);
12440 Formal_Discr
:= First_Discriminant
(A_Gen_T
);
12441 Actual_Discr
:= First_Discriminant
(Act_T
);
12442 while Formal_Discr
/= Empty
loop
12443 if Actual_Discr
= Empty
then
12445 ("discriminants on actual do not match formal",
12447 Abandon_Instantiation
(Actual
);
12450 Formal_Subt
:= Get_Instance_Of
(Etype
(Formal_Discr
));
12452 -- Access discriminants match if designated types do
12454 if Ekind
(Base_Type
(Formal_Subt
)) = E_Anonymous_Access_Type
12455 and then (Ekind
(Base_Type
(Etype
(Actual_Discr
)))) =
12456 E_Anonymous_Access_Type
12459 (Designated_Type
(Base_Type
(Formal_Subt
))) =
12460 Designated_Type
(Base_Type
(Etype
(Actual_Discr
)))
12464 elsif Base_Type
(Formal_Subt
) /=
12465 Base_Type
(Etype
(Actual_Discr
))
12468 ("types of actual discriminants must match formal",
12470 Abandon_Instantiation
(Actual
);
12472 elsif not Subtypes_Statically_Match
12473 (Formal_Subt
, Etype
(Actual_Discr
))
12474 and then Ada_Version
>= Ada_95
12477 ("subtypes of actual discriminants must match formal",
12479 Abandon_Instantiation
(Actual
);
12482 Next_Discriminant
(Formal_Discr
);
12483 Next_Discriminant
(Actual_Discr
);
12486 if Actual_Discr
/= Empty
then
12488 ("discriminants on actual do not match formal",
12490 Abandon_Instantiation
(Actual
);
12494 end Validate_Discriminated_Formal_Type
;
12496 ---------------------------------------
12497 -- Validate_Incomplete_Type_Instance --
12498 ---------------------------------------
12500 procedure Validate_Incomplete_Type_Instance
is
12502 if not Is_Tagged_Type
(Act_T
)
12503 and then Is_Tagged_Type
(A_Gen_T
)
12506 ("actual for & must be a tagged type", Actual
, Gen_T
);
12509 Validate_Discriminated_Formal_Type
;
12510 end Validate_Incomplete_Type_Instance
;
12512 --------------------------------------
12513 -- Validate_Interface_Type_Instance --
12514 --------------------------------------
12516 procedure Validate_Interface_Type_Instance
is
12518 if not Is_Interface
(Act_T
) then
12520 ("actual for formal interface type must be an interface",
12523 elsif Is_Limited_Type
(Act_T
) /= Is_Limited_Type
(A_Gen_T
)
12524 or else Is_Task_Interface
(A_Gen_T
) /= Is_Task_Interface
(Act_T
)
12525 or else Is_Protected_Interface
(A_Gen_T
) /=
12526 Is_Protected_Interface
(Act_T
)
12527 or else Is_Synchronized_Interface
(A_Gen_T
) /=
12528 Is_Synchronized_Interface
(Act_T
)
12531 ("actual for interface& does not match (RM 12.5.5(4))",
12534 end Validate_Interface_Type_Instance
;
12536 ------------------------------------
12537 -- Validate_Private_Type_Instance --
12538 ------------------------------------
12540 procedure Validate_Private_Type_Instance
is
12542 if Is_Limited_Type
(Act_T
)
12543 and then not Is_Limited_Type
(A_Gen_T
)
12545 if In_Instance
then
12549 ("actual for non-limited & cannot be a limited type", Actual
,
12551 Explain_Limited_Type
(Act_T
, Actual
);
12552 Abandon_Instantiation
(Actual
);
12555 elsif Known_To_Have_Preelab_Init
(A_Gen_T
)
12556 and then not Has_Preelaborable_Initialization
(Act_T
)
12559 ("actual for & must have preelaborable initialization", Actual
,
12562 elsif not Is_Definite_Subtype
(Act_T
)
12563 and then Is_Definite_Subtype
(A_Gen_T
)
12564 and then Ada_Version
>= Ada_95
12567 ("actual for & must be a definite subtype", Actual
, Gen_T
);
12569 elsif not Is_Tagged_Type
(Act_T
)
12570 and then Is_Tagged_Type
(A_Gen_T
)
12573 ("actual for & must be a tagged type", Actual
, Gen_T
);
12576 Validate_Discriminated_Formal_Type
;
12578 end Validate_Private_Type_Instance
;
12580 -- Start of processing for Instantiate_Type
12583 if Get_Instance_Of
(A_Gen_T
) /= A_Gen_T
then
12584 Error_Msg_N
("duplicate instantiation of generic type", Actual
);
12585 return New_List
(Error
);
12587 elsif not Is_Entity_Name
(Actual
)
12588 or else not Is_Type
(Entity
(Actual
))
12591 ("expect valid subtype mark to instantiate &", Actual
, Gen_T
);
12592 Abandon_Instantiation
(Actual
);
12595 Act_T
:= Entity
(Actual
);
12597 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
12598 -- as a generic actual parameter if the corresponding formal type
12599 -- does not have a known_discriminant_part, or is a formal derived
12600 -- type that is an Unchecked_Union type.
12602 if Is_Unchecked_Union
(Base_Type
(Act_T
)) then
12603 if not Has_Discriminants
(A_Gen_T
)
12604 or else (Is_Derived_Type
(A_Gen_T
)
12605 and then Is_Unchecked_Union
(A_Gen_T
))
12609 Error_Msg_N
("unchecked union cannot be the actual for a "
12610 & "discriminated formal type", Act_T
);
12615 -- Deal with fixed/floating restrictions
12617 if Is_Floating_Point_Type
(Act_T
) then
12618 Check_Restriction
(No_Floating_Point
, Actual
);
12619 elsif Is_Fixed_Point_Type
(Act_T
) then
12620 Check_Restriction
(No_Fixed_Point
, Actual
);
12623 -- Deal with error of using incomplete type as generic actual.
12624 -- This includes limited views of a type, even if the non-limited
12625 -- view may be available.
12627 if Ekind
(Act_T
) = E_Incomplete_Type
12628 or else (Is_Class_Wide_Type
(Act_T
)
12629 and then Ekind
(Root_Type
(Act_T
)) = E_Incomplete_Type
)
12631 -- If the formal is an incomplete type, the actual can be
12632 -- incomplete as well.
12634 if Ekind
(A_Gen_T
) = E_Incomplete_Type
then
12637 elsif Is_Class_Wide_Type
(Act_T
)
12638 or else No
(Full_View
(Act_T
))
12640 Error_Msg_N
("premature use of incomplete type", Actual
);
12641 Abandon_Instantiation
(Actual
);
12643 Act_T
:= Full_View
(Act_T
);
12644 Set_Entity
(Actual
, Act_T
);
12646 if Has_Private_Component
(Act_T
) then
12648 ("premature use of type with private component", Actual
);
12652 -- Deal with error of premature use of private type as generic actual
12654 elsif Is_Private_Type
(Act_T
)
12655 and then Is_Private_Type
(Base_Type
(Act_T
))
12656 and then not Is_Generic_Type
(Act_T
)
12657 and then not Is_Derived_Type
(Act_T
)
12658 and then No
(Full_View
(Root_Type
(Act_T
)))
12660 -- If the formal is an incomplete type, the actual can be
12661 -- private or incomplete as well.
12663 if Ekind
(A_Gen_T
) = E_Incomplete_Type
then
12666 Error_Msg_N
("premature use of private type", Actual
);
12669 elsif Has_Private_Component
(Act_T
) then
12671 ("premature use of type with private component", Actual
);
12674 Set_Instance_Of
(A_Gen_T
, Act_T
);
12676 -- If the type is generic, the class-wide type may also be used
12678 if Is_Tagged_Type
(A_Gen_T
)
12679 and then Is_Tagged_Type
(Act_T
)
12680 and then not Is_Class_Wide_Type
(A_Gen_T
)
12682 Set_Instance_Of
(Class_Wide_Type
(A_Gen_T
),
12683 Class_Wide_Type
(Act_T
));
12686 if not Is_Abstract_Type
(A_Gen_T
)
12687 and then Is_Abstract_Type
(Act_T
)
12690 ("actual of non-abstract formal cannot be abstract", Actual
);
12693 -- A generic scalar type is a first subtype for which we generate
12694 -- an anonymous base type. Indicate that the instance of this base
12695 -- is the base type of the actual.
12697 if Is_Scalar_Type
(A_Gen_T
) then
12698 Set_Instance_Of
(Etype
(A_Gen_T
), Etype
(Act_T
));
12702 if Error_Posted
(Act_T
) then
12705 case Nkind
(Def
) is
12706 when N_Formal_Private_Type_Definition
=>
12707 Validate_Private_Type_Instance
;
12709 when N_Formal_Incomplete_Type_Definition
=>
12710 Validate_Incomplete_Type_Instance
;
12712 when N_Formal_Derived_Type_Definition
=>
12713 Validate_Derived_Type_Instance
;
12715 when N_Formal_Discrete_Type_Definition
=>
12716 if not Is_Discrete_Type
(Act_T
) then
12718 ("expect discrete type in instantiation of&",
12720 Abandon_Instantiation
(Actual
);
12723 Diagnose_Predicated_Actual
;
12725 when N_Formal_Signed_Integer_Type_Definition
=>
12726 if not Is_Signed_Integer_Type
(Act_T
) then
12728 ("expect signed integer type in instantiation of&",
12730 Abandon_Instantiation
(Actual
);
12733 Diagnose_Predicated_Actual
;
12735 when N_Formal_Modular_Type_Definition
=>
12736 if not Is_Modular_Integer_Type
(Act_T
) then
12738 ("expect modular type in instantiation of &",
12740 Abandon_Instantiation
(Actual
);
12743 Diagnose_Predicated_Actual
;
12745 when N_Formal_Floating_Point_Definition
=>
12746 if not Is_Floating_Point_Type
(Act_T
) then
12748 ("expect float type in instantiation of &", Actual
, Gen_T
);
12749 Abandon_Instantiation
(Actual
);
12752 when N_Formal_Ordinary_Fixed_Point_Definition
=>
12753 if not Is_Ordinary_Fixed_Point_Type
(Act_T
) then
12755 ("expect ordinary fixed point type in instantiation of &",
12757 Abandon_Instantiation
(Actual
);
12760 when N_Formal_Decimal_Fixed_Point_Definition
=>
12761 if not Is_Decimal_Fixed_Point_Type
(Act_T
) then
12763 ("expect decimal type in instantiation of &",
12765 Abandon_Instantiation
(Actual
);
12768 when N_Array_Type_Definition
=>
12769 Validate_Array_Type_Instance
;
12771 when N_Access_To_Object_Definition
=>
12772 Validate_Access_Type_Instance
;
12774 when N_Access_Function_Definition |
12775 N_Access_Procedure_Definition
=>
12776 Validate_Access_Subprogram_Instance
;
12778 when N_Record_Definition
=>
12779 Validate_Interface_Type_Instance
;
12781 when N_Derived_Type_Definition
=>
12782 Validate_Derived_Interface_Type_Instance
;
12785 raise Program_Error
;
12790 Subt
:= New_Copy
(Gen_T
);
12792 -- Use adjusted sloc of subtype name as the location for other nodes in
12793 -- the subtype declaration.
12795 Loc
:= Sloc
(Subt
);
12798 Make_Subtype_Declaration
(Loc
,
12799 Defining_Identifier
=> Subt
,
12800 Subtype_Indication
=> New_Occurrence_Of
(Act_T
, Loc
));
12802 if Is_Private_Type
(Act_T
) then
12803 Set_Has_Private_View
(Subtype_Indication
(Decl_Node
));
12805 elsif Is_Access_Type
(Act_T
)
12806 and then Is_Private_Type
(Designated_Type
(Act_T
))
12808 Set_Has_Private_View
(Subtype_Indication
(Decl_Node
));
12811 -- In Ada 2012 the actual may be a limited view. Indicate that
12812 -- the local subtype must be treated as such.
12814 if From_Limited_With
(Act_T
) then
12815 Set_Ekind
(Subt
, E_Incomplete_Subtype
);
12816 Set_From_Limited_With
(Subt
);
12819 Decl_Nodes
:= New_List
(Decl_Node
);
12821 -- Flag actual derived types so their elaboration produces the
12822 -- appropriate renamings for the primitive operations of the ancestor.
12823 -- Flag actual for formal private types as well, to determine whether
12824 -- operations in the private part may override inherited operations.
12825 -- If the formal has an interface list, the ancestor is not the
12826 -- parent, but the analyzed formal that includes the interface
12827 -- operations of all its progenitors.
12829 -- Same treatment for formal private types, so we can check whether the
12830 -- type is tagged limited when validating derivations in the private
12831 -- part. (See AI05-096).
12833 if Nkind
(Def
) = N_Formal_Derived_Type_Definition
then
12834 if Present
(Interface_List
(Def
)) then
12835 Set_Generic_Parent_Type
(Decl_Node
, A_Gen_T
);
12837 Set_Generic_Parent_Type
(Decl_Node
, Ancestor
);
12840 elsif Nkind_In
(Def
, N_Formal_Private_Type_Definition
,
12841 N_Formal_Incomplete_Type_Definition
)
12843 Set_Generic_Parent_Type
(Decl_Node
, A_Gen_T
);
12846 -- If the actual is a synchronized type that implements an interface,
12847 -- the primitive operations are attached to the corresponding record,
12848 -- and we have to treat it as an additional generic actual, so that its
12849 -- primitive operations become visible in the instance. The task or
12850 -- protected type itself does not carry primitive operations.
12852 if Is_Concurrent_Type
(Act_T
)
12853 and then Is_Tagged_Type
(Act_T
)
12854 and then Present
(Corresponding_Record_Type
(Act_T
))
12855 and then Present
(Ancestor
)
12856 and then Is_Interface
(Ancestor
)
12859 Corr_Rec
: constant Entity_Id
:=
12860 Corresponding_Record_Type
(Act_T
);
12861 New_Corr
: Entity_Id
;
12862 Corr_Decl
: Node_Id
;
12865 New_Corr
:= Make_Temporary
(Loc
, 'S');
12867 Make_Subtype_Declaration
(Loc
,
12868 Defining_Identifier
=> New_Corr
,
12869 Subtype_Indication
=>
12870 New_Occurrence_Of
(Corr_Rec
, Loc
));
12871 Append_To
(Decl_Nodes
, Corr_Decl
);
12873 if Ekind
(Act_T
) = E_Task_Type
then
12874 Set_Ekind
(Subt
, E_Task_Subtype
);
12876 Set_Ekind
(Subt
, E_Protected_Subtype
);
12879 Set_Corresponding_Record_Type
(Subt
, Corr_Rec
);
12880 Set_Generic_Parent_Type
(Corr_Decl
, Ancestor
);
12881 Set_Generic_Parent_Type
(Decl_Node
, Empty
);
12885 -- For a floating-point type, capture dimension info if any, because
12886 -- the generated subtype declaration does not come from source and
12887 -- will not process dimensions.
12889 if Is_Floating_Point_Type
(Act_T
) then
12890 Copy_Dimensions
(Act_T
, Subt
);
12894 end Instantiate_Type
;
12896 ---------------------
12897 -- Is_In_Main_Unit --
12898 ---------------------
12900 function Is_In_Main_Unit
(N
: Node_Id
) return Boolean is
12901 Unum
: constant Unit_Number_Type
:= Get_Source_Unit
(N
);
12902 Current_Unit
: Node_Id
;
12905 if Unum
= Main_Unit
then
12908 -- If the current unit is a subunit then it is either the main unit or
12909 -- is being compiled as part of the main unit.
12911 elsif Nkind
(N
) = N_Compilation_Unit
then
12912 return Nkind
(Unit
(N
)) = N_Subunit
;
12915 Current_Unit
:= Parent
(N
);
12916 while Present
(Current_Unit
)
12917 and then Nkind
(Current_Unit
) /= N_Compilation_Unit
12919 Current_Unit
:= Parent
(Current_Unit
);
12922 -- The instantiation node is in the main unit, or else the current node
12923 -- (perhaps as the result of nested instantiations) is in the main unit,
12924 -- or in the declaration of the main unit, which in this last case must
12928 Current_Unit
= Cunit
(Main_Unit
)
12929 or else Current_Unit
= Library_Unit
(Cunit
(Main_Unit
))
12930 or else (Present
(Current_Unit
)
12931 and then Present
(Library_Unit
(Current_Unit
))
12932 and then Is_In_Main_Unit
(Library_Unit
(Current_Unit
)));
12933 end Is_In_Main_Unit
;
12935 ----------------------------
12936 -- Load_Parent_Of_Generic --
12937 ----------------------------
12939 procedure Load_Parent_Of_Generic
12942 Body_Optional
: Boolean := False)
12944 Comp_Unit
: constant Node_Id
:= Cunit
(Get_Source_Unit
(Spec
));
12945 Saved_Style_Check
: constant Boolean := Style_Check
;
12946 Saved_Warnings
: constant Warning_Record
:= Save_Warnings
;
12947 True_Parent
: Node_Id
;
12948 Inst_Node
: Node_Id
;
12950 Previous_Instances
: constant Elist_Id
:= New_Elmt_List
;
12952 procedure Collect_Previous_Instances
(Decls
: List_Id
);
12953 -- Collect all instantiations in the given list of declarations, that
12954 -- precede the generic that we need to load. If the bodies of these
12955 -- instantiations are available, we must analyze them, to ensure that
12956 -- the public symbols generated are the same when the unit is compiled
12957 -- to generate code, and when it is compiled in the context of a unit
12958 -- that needs a particular nested instance. This process is applied to
12959 -- both package and subprogram instances.
12961 --------------------------------
12962 -- Collect_Previous_Instances --
12963 --------------------------------
12965 procedure Collect_Previous_Instances
(Decls
: List_Id
) is
12969 Decl
:= First
(Decls
);
12970 while Present
(Decl
) loop
12971 if Sloc
(Decl
) >= Sloc
(Inst_Node
) then
12974 -- If Decl is an instantiation, then record it as requiring
12975 -- instantiation of the corresponding body, except if it is an
12976 -- abbreviated instantiation generated internally for conformance
12977 -- checking purposes only for the case of a formal package
12978 -- declared without a box (see Instantiate_Formal_Package). Such
12979 -- an instantiation does not generate any code (the actual code
12980 -- comes from actual) and thus does not need to be analyzed here.
12981 -- If the instantiation appears with a generic package body it is
12982 -- not analyzed here either.
12984 elsif Nkind
(Decl
) = N_Package_Instantiation
12985 and then not Is_Internal
(Defining_Entity
(Decl
))
12987 Append_Elmt
(Decl
, Previous_Instances
);
12989 -- For a subprogram instantiation, omit instantiations intrinsic
12990 -- operations (Unchecked_Conversions, etc.) that have no bodies.
12992 elsif Nkind_In
(Decl
, N_Function_Instantiation
,
12993 N_Procedure_Instantiation
)
12994 and then not Is_Intrinsic_Subprogram
(Entity
(Name
(Decl
)))
12996 Append_Elmt
(Decl
, Previous_Instances
);
12998 elsif Nkind
(Decl
) = N_Package_Declaration
then
12999 Collect_Previous_Instances
13000 (Visible_Declarations
(Specification
(Decl
)));
13001 Collect_Previous_Instances
13002 (Private_Declarations
(Specification
(Decl
)));
13004 -- Previous non-generic bodies may contain instances as well
13006 elsif Nkind
(Decl
) = N_Package_Body
13007 and then Ekind
(Corresponding_Spec
(Decl
)) /= E_Generic_Package
13009 Collect_Previous_Instances
(Declarations
(Decl
));
13011 elsif Nkind
(Decl
) = N_Subprogram_Body
13012 and then not Acts_As_Spec
(Decl
)
13013 and then not Is_Generic_Subprogram
(Corresponding_Spec
(Decl
))
13015 Collect_Previous_Instances
(Declarations
(Decl
));
13020 end Collect_Previous_Instances
;
13022 -- Start of processing for Load_Parent_Of_Generic
13025 if not In_Same_Source_Unit
(N
, Spec
)
13026 or else Nkind
(Unit
(Comp_Unit
)) = N_Package_Declaration
13027 or else (Nkind
(Unit
(Comp_Unit
)) = N_Package_Body
13028 and then not Is_In_Main_Unit
(Spec
))
13030 -- Find body of parent of spec, and analyze it. A special case arises
13031 -- when the parent is an instantiation, that is to say when we are
13032 -- currently instantiating a nested generic. In that case, there is
13033 -- no separate file for the body of the enclosing instance. Instead,
13034 -- the enclosing body must be instantiated as if it were a pending
13035 -- instantiation, in order to produce the body for the nested generic
13036 -- we require now. Note that in that case the generic may be defined
13037 -- in a package body, the instance defined in the same package body,
13038 -- and the original enclosing body may not be in the main unit.
13040 Inst_Node
:= Empty
;
13042 True_Parent
:= Parent
(Spec
);
13043 while Present
(True_Parent
)
13044 and then Nkind
(True_Parent
) /= N_Compilation_Unit
13046 if Nkind
(True_Parent
) = N_Package_Declaration
13048 Nkind
(Original_Node
(True_Parent
)) = N_Package_Instantiation
13050 -- Parent is a compilation unit that is an instantiation.
13051 -- Instantiation node has been replaced with package decl.
13053 Inst_Node
:= Original_Node
(True_Parent
);
13056 elsif Nkind
(True_Parent
) = N_Package_Declaration
13057 and then Present
(Generic_Parent
(Specification
(True_Parent
)))
13058 and then Nkind
(Parent
(True_Parent
)) /= N_Compilation_Unit
13060 -- Parent is an instantiation within another specification.
13061 -- Declaration for instance has been inserted before original
13062 -- instantiation node. A direct link would be preferable?
13064 Inst_Node
:= Next
(True_Parent
);
13065 while Present
(Inst_Node
)
13066 and then Nkind
(Inst_Node
) /= N_Package_Instantiation
13071 -- If the instance appears within a generic, and the generic
13072 -- unit is defined within a formal package of the enclosing
13073 -- generic, there is no generic body available, and none
13074 -- needed. A more precise test should be used ???
13076 if No
(Inst_Node
) then
13083 True_Parent
:= Parent
(True_Parent
);
13087 -- Case where we are currently instantiating a nested generic
13089 if Present
(Inst_Node
) then
13090 if Nkind
(Parent
(True_Parent
)) = N_Compilation_Unit
then
13092 -- Instantiation node and declaration of instantiated package
13093 -- were exchanged when only the declaration was needed.
13094 -- Restore instantiation node before proceeding with body.
13096 Set_Unit
(Parent
(True_Parent
), Inst_Node
);
13099 -- Now complete instantiation of enclosing body, if it appears in
13100 -- some other unit. If it appears in the current unit, the body
13101 -- will have been instantiated already.
13103 if No
(Corresponding_Body
(Instance_Spec
(Inst_Node
))) then
13105 -- We need to determine the expander mode to instantiate the
13106 -- enclosing body. Because the generic body we need may use
13107 -- global entities declared in the enclosing package (including
13108 -- aggregates) it is in general necessary to compile this body
13109 -- with expansion enabled, except if we are within a generic
13110 -- package, in which case the usual generic rule applies.
13113 Exp_Status
: Boolean := True;
13117 -- Loop through scopes looking for generic package
13119 Scop
:= Scope
(Defining_Entity
(Instance_Spec
(Inst_Node
)));
13120 while Present
(Scop
)
13121 and then Scop
/= Standard_Standard
13123 if Ekind
(Scop
) = E_Generic_Package
then
13124 Exp_Status
:= False;
13128 Scop
:= Scope
(Scop
);
13131 -- Collect previous instantiations in the unit that contains
13132 -- the desired generic.
13134 if Nkind
(Parent
(True_Parent
)) /= N_Compilation_Unit
13135 and then not Body_Optional
13139 Info
: Pending_Body_Info
;
13143 Par
:= Parent
(Inst_Node
);
13144 while Present
(Par
) loop
13145 exit when Nkind
(Parent
(Par
)) = N_Compilation_Unit
;
13146 Par
:= Parent
(Par
);
13149 pragma Assert
(Present
(Par
));
13151 if Nkind
(Par
) = N_Package_Body
then
13152 Collect_Previous_Instances
(Declarations
(Par
));
13154 elsif Nkind
(Par
) = N_Package_Declaration
then
13155 Collect_Previous_Instances
13156 (Visible_Declarations
(Specification
(Par
)));
13157 Collect_Previous_Instances
13158 (Private_Declarations
(Specification
(Par
)));
13161 -- Enclosing unit is a subprogram body. In this
13162 -- case all instance bodies are processed in order
13163 -- and there is no need to collect them separately.
13168 Decl
:= First_Elmt
(Previous_Instances
);
13169 while Present
(Decl
) loop
13171 (Inst_Node
=> Node
(Decl
),
13173 Instance_Spec
(Node
(Decl
)),
13174 Expander_Status
=> Exp_Status
,
13175 Current_Sem_Unit
=>
13176 Get_Code_Unit
(Sloc
(Node
(Decl
))),
13177 Scope_Suppress
=> Scope_Suppress
,
13178 Local_Suppress_Stack_Top
=>
13179 Local_Suppress_Stack_Top
,
13180 Version
=> Ada_Version
,
13181 Version_Pragma
=> Ada_Version_Pragma
,
13182 Warnings
=> Save_Warnings
,
13183 SPARK_Mode
=> SPARK_Mode
,
13184 SPARK_Mode_Pragma
=> SPARK_Mode_Pragma
);
13186 -- Package instance
13189 Nkind
(Node
(Decl
)) = N_Package_Instantiation
13191 Instantiate_Package_Body
13192 (Info
, Body_Optional
=> True);
13194 -- Subprogram instance
13197 -- The instance_spec is in the wrapper package,
13198 -- usually followed by its local renaming
13199 -- declaration. See Build_Subprogram_Renaming
13200 -- for details. If the instance carries aspects,
13201 -- these result in the corresponding pragmas,
13202 -- inserted after the subprogram declaration.
13203 -- They must be skipped as well when retrieving
13204 -- the desired spec. A direct link would be
13209 (Last
(Visible_Declarations
13210 (Specification
(Info
.Act_Decl
))));
13212 while Nkind_In
(Decl
,
13213 N_Subprogram_Renaming_Declaration
, N_Pragma
)
13215 Decl
:= Prev
(Decl
);
13218 Info
.Act_Decl
:= Decl
;
13221 Instantiate_Subprogram_Body
13222 (Info
, Body_Optional
=> True);
13230 Instantiate_Package_Body
13232 ((Inst_Node
=> Inst_Node
,
13233 Act_Decl
=> True_Parent
,
13234 Expander_Status
=> Exp_Status
,
13235 Current_Sem_Unit
=> Get_Code_Unit
13236 (Sloc
(Inst_Node
)),
13237 Scope_Suppress
=> Scope_Suppress
,
13238 Local_Suppress_Stack_Top
=> Local_Suppress_Stack_Top
,
13239 Version
=> Ada_Version
,
13240 Version_Pragma
=> Ada_Version_Pragma
,
13241 Warnings
=> Save_Warnings
,
13242 SPARK_Mode
=> SPARK_Mode
,
13243 SPARK_Mode_Pragma
=> SPARK_Mode_Pragma
)),
13244 Body_Optional
=> Body_Optional
);
13248 -- Case where we are not instantiating a nested generic
13251 Opt
.Style_Check
:= False;
13252 Expander_Mode_Save_And_Set
(True);
13253 Load_Needed_Body
(Comp_Unit
, OK
);
13254 Opt
.Style_Check
:= Saved_Style_Check
;
13255 Restore_Warnings
(Saved_Warnings
);
13256 Expander_Mode_Restore
;
13259 and then Unit_Requires_Body
(Defining_Entity
(Spec
))
13260 and then not Body_Optional
13263 Bname
: constant Unit_Name_Type
:=
13264 Get_Body_Name
(Get_Unit_Name
(Unit
(Comp_Unit
)));
13267 -- In CodePeer mode, the missing body may make the analysis
13268 -- incomplete, but we do not treat it as fatal.
13270 if CodePeer_Mode
then
13274 Error_Msg_Unit_1
:= Bname
;
13275 Error_Msg_N
("this instantiation requires$!", N
);
13276 Error_Msg_File_1
:=
13277 Get_File_Name
(Bname
, Subunit
=> False);
13278 Error_Msg_N
("\but file{ was not found!", N
);
13279 raise Unrecoverable_Error
;
13286 -- If loading parent of the generic caused an instantiation circularity,
13287 -- we abandon compilation at this point, because otherwise in some cases
13288 -- we get into trouble with infinite recursions after this point.
13290 if Circularity_Detected
then
13291 raise Unrecoverable_Error
;
13293 end Load_Parent_Of_Generic
;
13295 ---------------------------------
13296 -- Map_Formal_Package_Entities --
13297 ---------------------------------
13299 procedure Map_Formal_Package_Entities
(Form
: Entity_Id
; Act
: Entity_Id
) is
13304 Set_Instance_Of
(Form
, Act
);
13306 -- Traverse formal and actual package to map the corresponding entities.
13307 -- We skip over internal entities that may be generated during semantic
13308 -- analysis, and find the matching entities by name, given that they
13309 -- must appear in the same order.
13311 E1
:= First_Entity
(Form
);
13312 E2
:= First_Entity
(Act
);
13313 while Present
(E1
) and then E1
/= First_Private_Entity
(Form
) loop
13314 -- Could this test be a single condition??? Seems like it could, and
13315 -- isn't FPE (Form) a constant anyway???
13317 if not Is_Internal
(E1
)
13318 and then Present
(Parent
(E1
))
13319 and then not Is_Class_Wide_Type
(E1
)
13320 and then not Is_Internal_Name
(Chars
(E1
))
13322 while Present
(E2
) and then Chars
(E2
) /= Chars
(E1
) loop
13329 Set_Instance_Of
(E1
, E2
);
13331 if Is_Type
(E1
) and then Is_Tagged_Type
(E2
) then
13332 Set_Instance_Of
(Class_Wide_Type
(E1
), Class_Wide_Type
(E2
));
13335 if Is_Constrained
(E1
) then
13336 Set_Instance_Of
(Base_Type
(E1
), Base_Type
(E2
));
13339 if Ekind
(E1
) = E_Package
and then No
(Renamed_Object
(E1
)) then
13340 Map_Formal_Package_Entities
(E1
, E2
);
13347 end Map_Formal_Package_Entities
;
13349 -----------------------
13350 -- Move_Freeze_Nodes --
13351 -----------------------
13353 procedure Move_Freeze_Nodes
13354 (Out_Of
: Entity_Id
;
13359 Next_Decl
: Node_Id
;
13360 Next_Node
: Node_Id
:= After
;
13363 function Is_Outer_Type
(T
: Entity_Id
) return Boolean;
13364 -- Check whether entity is declared in a scope external to that of the
13367 -------------------
13368 -- Is_Outer_Type --
13369 -------------------
13371 function Is_Outer_Type
(T
: Entity_Id
) return Boolean is
13372 Scop
: Entity_Id
:= Scope
(T
);
13375 if Scope_Depth
(Scop
) < Scope_Depth
(Out_Of
) then
13379 while Scop
/= Standard_Standard
loop
13380 if Scop
= Out_Of
then
13383 Scop
:= Scope
(Scop
);
13391 -- Start of processing for Move_Freeze_Nodes
13398 -- First remove the freeze nodes that may appear before all other
13402 while Present
(Decl
)
13403 and then Nkind
(Decl
) = N_Freeze_Entity
13404 and then Is_Outer_Type
(Entity
(Decl
))
13406 Decl
:= Remove_Head
(L
);
13407 Insert_After
(Next_Node
, Decl
);
13408 Set_Analyzed
(Decl
, False);
13413 -- Next scan the list of declarations and remove each freeze node that
13414 -- appears ahead of the current node.
13416 while Present
(Decl
) loop
13417 while Present
(Next
(Decl
))
13418 and then Nkind
(Next
(Decl
)) = N_Freeze_Entity
13419 and then Is_Outer_Type
(Entity
(Next
(Decl
)))
13421 Next_Decl
:= Remove_Next
(Decl
);
13422 Insert_After
(Next_Node
, Next_Decl
);
13423 Set_Analyzed
(Next_Decl
, False);
13424 Next_Node
:= Next_Decl
;
13427 -- If the declaration is a nested package or concurrent type, then
13428 -- recurse. Nested generic packages will have been processed from the
13431 case Nkind
(Decl
) is
13432 when N_Package_Declaration
=>
13433 Spec
:= Specification
(Decl
);
13435 when N_Task_Type_Declaration
=>
13436 Spec
:= Task_Definition
(Decl
);
13438 when N_Protected_Type_Declaration
=>
13439 Spec
:= Protected_Definition
(Decl
);
13445 if Present
(Spec
) then
13446 Move_Freeze_Nodes
(Out_Of
, Next_Node
, Visible_Declarations
(Spec
));
13447 Move_Freeze_Nodes
(Out_Of
, Next_Node
, Private_Declarations
(Spec
));
13452 end Move_Freeze_Nodes
;
13458 function Next_Assoc
(E
: Assoc_Ptr
) return Assoc_Ptr
is
13460 return Generic_Renamings
.Table
(E
).Next_In_HTable
;
13463 ------------------------
13464 -- Preanalyze_Actuals --
13465 ------------------------
13467 procedure Preanalyze_Actuals
(N
: Node_Id
; Inst
: Entity_Id
:= Empty
) is
13470 Errs
: constant Nat
:= Serious_Errors_Detected
;
13472 Cur
: Entity_Id
:= Empty
;
13473 -- Current homograph of the instance name
13476 -- Saved visibility status of the current homograph
13479 Assoc
:= First
(Generic_Associations
(N
));
13481 -- If the instance is a child unit, its name may hide an outer homonym,
13482 -- so make it invisible to perform name resolution on the actuals.
13484 if Nkind
(Defining_Unit_Name
(N
)) = N_Defining_Program_Unit_Name
13486 (Current_Entity
(Defining_Identifier
(Defining_Unit_Name
(N
))))
13488 Cur
:= Current_Entity
(Defining_Identifier
(Defining_Unit_Name
(N
)));
13490 if Is_Compilation_Unit
(Cur
) then
13491 Vis
:= Is_Immediately_Visible
(Cur
);
13492 Set_Is_Immediately_Visible
(Cur
, False);
13498 while Present
(Assoc
) loop
13499 if Nkind
(Assoc
) /= N_Others_Choice
then
13500 Act
:= Explicit_Generic_Actual_Parameter
(Assoc
);
13502 -- Within a nested instantiation, a defaulted actual is an empty
13503 -- association, so nothing to analyze. If the subprogram actual
13504 -- is an attribute, analyze prefix only, because actual is not a
13505 -- complete attribute reference.
13507 -- If actual is an allocator, analyze expression only. The full
13508 -- analysis can generate code, and if instance is a compilation
13509 -- unit we have to wait until the package instance is installed
13510 -- to have a proper place to insert this code.
13512 -- String literals may be operators, but at this point we do not
13513 -- know whether the actual is a formal subprogram or a string.
13518 elsif Nkind
(Act
) = N_Attribute_Reference
then
13519 Analyze
(Prefix
(Act
));
13521 elsif Nkind
(Act
) = N_Explicit_Dereference
then
13522 Analyze
(Prefix
(Act
));
13524 elsif Nkind
(Act
) = N_Allocator
then
13526 Expr
: constant Node_Id
:= Expression
(Act
);
13529 if Nkind
(Expr
) = N_Subtype_Indication
then
13530 Analyze
(Subtype_Mark
(Expr
));
13532 -- Analyze separately each discriminant constraint, when
13533 -- given with a named association.
13539 Constr
:= First
(Constraints
(Constraint
(Expr
)));
13540 while Present
(Constr
) loop
13541 if Nkind
(Constr
) = N_Discriminant_Association
then
13542 Analyze
(Expression
(Constr
));
13556 elsif Nkind
(Act
) /= N_Operator_Symbol
then
13559 -- Within a package instance, mark actuals that are limited
13560 -- views, so their use can be moved to the body of the
13563 if Is_Entity_Name
(Act
)
13564 and then Is_Type
(Entity
(Act
))
13565 and then From_Limited_With
(Entity
(Act
))
13566 and then Present
(Inst
)
13568 Append_Elmt
(Entity
(Act
), Incomplete_Actuals
(Inst
));
13572 if Errs
/= Serious_Errors_Detected
then
13574 -- Do a minimal analysis of the generic, to prevent spurious
13575 -- warnings complaining about the generic being unreferenced,
13576 -- before abandoning the instantiation.
13578 Analyze
(Name
(N
));
13580 if Is_Entity_Name
(Name
(N
))
13581 and then Etype
(Name
(N
)) /= Any_Type
13583 Generate_Reference
(Entity
(Name
(N
)), Name
(N
));
13584 Set_Is_Instantiated
(Entity
(Name
(N
)));
13587 if Present
(Cur
) then
13589 -- For the case of a child instance hiding an outer homonym,
13590 -- provide additional warning which might explain the error.
13592 Set_Is_Immediately_Visible
(Cur
, Vis
);
13594 ("& hides outer unit with the same name??",
13595 N
, Defining_Unit_Name
(N
));
13598 Abandon_Instantiation
(Act
);
13605 if Present
(Cur
) then
13606 Set_Is_Immediately_Visible
(Cur
, Vis
);
13608 end Preanalyze_Actuals
;
13610 -------------------
13611 -- Remove_Parent --
13612 -------------------
13614 procedure Remove_Parent
(In_Body
: Boolean := False) is
13615 S
: Entity_Id
:= Current_Scope
;
13616 -- S is the scope containing the instantiation just completed. The scope
13617 -- stack contains the parent instances of the instantiation, followed by
13626 -- After child instantiation is complete, remove from scope stack the
13627 -- extra copy of the current scope, and then remove parent instances.
13629 if not In_Body
then
13632 while Current_Scope
/= S
loop
13633 P
:= Current_Scope
;
13634 End_Package_Scope
(Current_Scope
);
13636 if In_Open_Scopes
(P
) then
13637 E
:= First_Entity
(P
);
13638 while Present
(E
) loop
13639 Set_Is_Immediately_Visible
(E
, True);
13643 -- If instantiation is declared in a block, it is the enclosing
13644 -- scope that might be a parent instance. Note that only one
13645 -- block can be involved, because the parent instances have
13646 -- been installed within it.
13648 if Ekind
(P
) = E_Block
then
13649 Cur_P
:= Scope
(P
);
13654 if Is_Generic_Instance
(Cur_P
) and then P
/= Current_Scope
then
13655 -- We are within an instance of some sibling. Retain
13656 -- visibility of parent, for proper subsequent cleanup, and
13657 -- reinstall private declarations as well.
13659 Set_In_Private_Part
(P
);
13660 Install_Private_Declarations
(P
);
13663 -- If the ultimate parent is a top-level unit recorded in
13664 -- Instance_Parent_Unit, then reset its visibility to what it was
13665 -- before instantiation. (It's not clear what the purpose is of
13666 -- testing whether Scope (P) is In_Open_Scopes, but that test was
13667 -- present before the ultimate parent test was added.???)
13669 elsif not In_Open_Scopes
(Scope
(P
))
13670 or else (P
= Instance_Parent_Unit
13671 and then not Parent_Unit_Visible
)
13673 Set_Is_Immediately_Visible
(P
, False);
13675 -- If the current scope is itself an instantiation of a generic
13676 -- nested within P, and we are in the private part of body of this
13677 -- instantiation, restore the full views of P, that were removed
13678 -- in End_Package_Scope above. This obscure case can occur when a
13679 -- subunit of a generic contains an instance of a child unit of
13680 -- its generic parent unit.
13682 elsif S
= Current_Scope
and then Is_Generic_Instance
(S
) then
13684 Par
: constant Entity_Id
:=
13685 Generic_Parent
(Package_Specification
(S
));
13688 and then P
= Scope
(Par
)
13689 and then (In_Package_Body
(S
) or else In_Private_Part
(S
))
13691 Set_In_Private_Part
(P
);
13692 Install_Private_Declarations
(P
);
13698 -- Reset visibility of entities in the enclosing scope
13700 Set_Is_Hidden_Open_Scope
(Current_Scope
, False);
13702 Hidden
:= First_Elmt
(Hidden_Entities
);
13703 while Present
(Hidden
) loop
13704 Set_Is_Immediately_Visible
(Node
(Hidden
), True);
13705 Next_Elmt
(Hidden
);
13709 -- Each body is analyzed separately, and there is no context that
13710 -- needs preserving from one body instance to the next, so remove all
13711 -- parent scopes that have been installed.
13713 while Present
(S
) loop
13714 End_Package_Scope
(S
);
13715 Set_Is_Immediately_Visible
(S
, False);
13716 S
:= Current_Scope
;
13717 exit when S
= Standard_Standard
;
13726 procedure Restore_Env
is
13727 Saved
: Instance_Env
renames Instance_Envs
.Table
(Instance_Envs
.Last
);
13730 if No
(Current_Instantiated_Parent
.Act_Id
) then
13731 -- Restore environment after subprogram inlining
13733 Restore_Private_Views
(Empty
);
13736 Current_Instantiated_Parent
:= Saved
.Instantiated_Parent
;
13737 Exchanged_Views
:= Saved
.Exchanged_Views
;
13738 Hidden_Entities
:= Saved
.Hidden_Entities
;
13739 Current_Sem_Unit
:= Saved
.Current_Sem_Unit
;
13740 Parent_Unit_Visible
:= Saved
.Parent_Unit_Visible
;
13741 Instance_Parent_Unit
:= Saved
.Instance_Parent_Unit
;
13743 Restore_Opt_Config_Switches
(Saved
.Switches
);
13745 Instance_Envs
.Decrement_Last
;
13748 ---------------------------
13749 -- Restore_Private_Views --
13750 ---------------------------
13752 procedure Restore_Private_Views
13753 (Pack_Id
: Entity_Id
;
13754 Is_Package
: Boolean := True)
13759 Dep_Elmt
: Elmt_Id
;
13762 procedure Restore_Nested_Formal
(Formal
: Entity_Id
);
13763 -- Hide the generic formals of formal packages declared with box which
13764 -- were reachable in the current instantiation.
13766 ---------------------------
13767 -- Restore_Nested_Formal --
13768 ---------------------------
13770 procedure Restore_Nested_Formal
(Formal
: Entity_Id
) is
13774 if Present
(Renamed_Object
(Formal
))
13775 and then Denotes_Formal_Package
(Renamed_Object
(Formal
), True)
13779 elsif Present
(Associated_Formal_Package
(Formal
)) then
13780 Ent
:= First_Entity
(Formal
);
13781 while Present
(Ent
) loop
13782 exit when Ekind
(Ent
) = E_Package
13783 and then Renamed_Entity
(Ent
) = Renamed_Entity
(Formal
);
13785 Set_Is_Hidden
(Ent
);
13786 Set_Is_Potentially_Use_Visible
(Ent
, False);
13788 -- If package, then recurse
13790 if Ekind
(Ent
) = E_Package
then
13791 Restore_Nested_Formal
(Ent
);
13797 end Restore_Nested_Formal
;
13799 -- Start of processing for Restore_Private_Views
13802 M
:= First_Elmt
(Exchanged_Views
);
13803 while Present
(M
) loop
13806 -- Subtypes of types whose views have been exchanged, and that are
13807 -- defined within the instance, were not on the Private_Dependents
13808 -- list on entry to the instance, so they have to be exchanged
13809 -- explicitly now, in order to remain consistent with the view of the
13812 if Ekind_In
(Typ
, E_Private_Type
,
13813 E_Limited_Private_Type
,
13814 E_Record_Type_With_Private
)
13816 Dep_Elmt
:= First_Elmt
(Private_Dependents
(Typ
));
13817 while Present
(Dep_Elmt
) loop
13818 Dep_Typ
:= Node
(Dep_Elmt
);
13820 if Scope
(Dep_Typ
) = Pack_Id
13821 and then Present
(Full_View
(Dep_Typ
))
13823 Replace_Elmt
(Dep_Elmt
, Full_View
(Dep_Typ
));
13824 Exchange_Declarations
(Dep_Typ
);
13827 Next_Elmt
(Dep_Elmt
);
13831 Exchange_Declarations
(Node
(M
));
13835 if No
(Pack_Id
) then
13839 -- Make the generic formal parameters private, and make the formal types
13840 -- into subtypes of the actuals again.
13842 E
:= First_Entity
(Pack_Id
);
13843 while Present
(E
) loop
13844 Set_Is_Hidden
(E
, True);
13847 and then Nkind
(Parent
(E
)) = N_Subtype_Declaration
13849 -- If the actual for E is itself a generic actual type from
13850 -- an enclosing instance, E is still a generic actual type
13851 -- outside of the current instance. This matter when resolving
13852 -- an overloaded call that may be ambiguous in the enclosing
13853 -- instance, when two of its actuals coincide.
13855 if Is_Entity_Name
(Subtype_Indication
(Parent
(E
)))
13856 and then Is_Generic_Actual_Type
13857 (Entity
(Subtype_Indication
(Parent
(E
))))
13861 Set_Is_Generic_Actual_Type
(E
, False);
13864 -- An unusual case of aliasing: the actual may also be directly
13865 -- visible in the generic, and be private there, while it is fully
13866 -- visible in the context of the instance. The internal subtype
13867 -- is private in the instance but has full visibility like its
13868 -- parent in the enclosing scope. This enforces the invariant that
13869 -- the privacy status of all private dependents of a type coincide
13870 -- with that of the parent type. This can only happen when a
13871 -- generic child unit is instantiated within a sibling.
13873 if Is_Private_Type
(E
)
13874 and then not Is_Private_Type
(Etype
(E
))
13876 Exchange_Declarations
(E
);
13879 elsif Ekind
(E
) = E_Package
then
13881 -- The end of the renaming list is the renaming of the generic
13882 -- package itself. If the instance is a subprogram, all entities
13883 -- in the corresponding package are renamings. If this entity is
13884 -- a formal package, make its own formals private as well. The
13885 -- actual in this case is itself the renaming of an instantiation.
13886 -- If the entity is not a package renaming, it is the entity
13887 -- created to validate formal package actuals: ignore it.
13889 -- If the actual is itself a formal package for the enclosing
13890 -- generic, or the actual for such a formal package, it remains
13891 -- visible on exit from the instance, and therefore nothing needs
13892 -- to be done either, except to keep it accessible.
13894 if Is_Package
and then Renamed_Object
(E
) = Pack_Id
then
13897 elsif Nkind
(Parent
(E
)) /= N_Package_Renaming_Declaration
then
13901 Denotes_Formal_Package
(Renamed_Object
(E
), True, Pack_Id
)
13903 Set_Is_Hidden
(E
, False);
13907 Act_P
: constant Entity_Id
:= Renamed_Object
(E
);
13911 Id
:= First_Entity
(Act_P
);
13913 and then Id
/= First_Private_Entity
(Act_P
)
13915 exit when Ekind
(Id
) = E_Package
13916 and then Renamed_Object
(Id
) = Act_P
;
13918 Set_Is_Hidden
(Id
, True);
13919 Set_Is_Potentially_Use_Visible
(Id
, In_Use
(Act_P
));
13921 if Ekind
(Id
) = E_Package
then
13922 Restore_Nested_Formal
(Id
);
13933 end Restore_Private_Views
;
13940 (Gen_Unit
: Entity_Id
;
13941 Act_Unit
: Entity_Id
)
13945 Set_Instance_Env
(Gen_Unit
, Act_Unit
);
13948 ----------------------------
13949 -- Save_Global_References --
13950 ----------------------------
13952 procedure Save_Global_References
(Templ
: Node_Id
) is
13954 -- ??? it is horrible to use global variables in highly recursive code
13957 -- The entity of the current associated node
13959 Gen_Scope
: Entity_Id
;
13960 -- The scope of the generic for which references are being saved
13963 -- The current associated node
13965 function Is_Global
(E
: Entity_Id
) return Boolean;
13966 -- Check whether entity is defined outside of generic unit. Examine the
13967 -- scope of an entity, and the scope of the scope, etc, until we find
13968 -- either Standard, in which case the entity is global, or the generic
13969 -- unit itself, which indicates that the entity is local. If the entity
13970 -- is the generic unit itself, as in the case of a recursive call, or
13971 -- the enclosing generic unit, if different from the current scope, then
13972 -- it is local as well, because it will be replaced at the point of
13973 -- instantiation. On the other hand, if it is a reference to a child
13974 -- unit of a common ancestor, which appears in an instantiation, it is
13975 -- global because it is used to denote a specific compilation unit at
13976 -- the time the instantiations will be analyzed.
13978 procedure Qualify_Universal_Operands
13980 Func_Call
: Node_Id
);
13981 -- Op denotes a binary or unary operator in generic template Templ. Node
13982 -- Func_Call is the function call alternative of the operator within the
13983 -- the analyzed copy of the template. Change each operand which yields a
13984 -- universal type by wrapping it into a qualified expression
13986 -- Actual_Typ'(Operand)
13988 -- where Actual_Typ is the type of corresponding actual parameter of
13989 -- Operand in Func_Call.
13991 procedure Reset_Entity
(N
: Node_Id
);
13992 -- Save semantic information on global entity so that it is not resolved
13993 -- again at instantiation time.
13995 procedure Save_Entity_Descendants
(N
: Node_Id
);
13996 -- Apply Save_Global_References to the two syntactic descendants of
13997 -- non-terminal nodes that carry an Associated_Node and are processed
13998 -- through Reset_Entity. Once the global entity (if any) has been
13999 -- captured together with its type, only two syntactic descendants need
14000 -- to be traversed to complete the processing of the tree rooted at N.
14001 -- This applies to Selected_Components, Expanded_Names, and to Operator
14002 -- nodes. N can also be a character literal, identifier, or operator
14003 -- symbol node, but the call has no effect in these cases.
14005 procedure Save_Global_Defaults
(N1
: Node_Id
; N2
: Node_Id
);
14006 -- Default actuals in nested instances must be handled specially
14007 -- because there is no link to them from the original tree. When an
14008 -- actual subprogram is given by a default, we add an explicit generic
14009 -- association for it in the instantiation node. When we save the
14010 -- global references on the name of the instance, we recover the list
14011 -- of generic associations, and add an explicit one to the original
14012 -- generic tree, through which a global actual can be preserved.
14013 -- Similarly, if a child unit is instantiated within a sibling, in the
14014 -- context of the parent, we must preserve the identifier of the parent
14015 -- so that it can be properly resolved in a subsequent instantiation.
14017 procedure Save_Global_Descendant
(D
: Union_Id
);
14018 -- Apply Save_References recursively to the descendants of node D
14020 procedure Save_References
(N
: Node_Id
);
14021 -- This is the recursive procedure that does the work, once the
14022 -- enclosing generic scope has been established.
14028 function Is_Global
(E
: Entity_Id
) return Boolean is
14031 function Is_Instance_Node
(Decl
: Node_Id
) return Boolean;
14032 -- Determine whether the parent node of a reference to a child unit
14033 -- denotes an instantiation or a formal package, in which case the
14034 -- reference to the child unit is global, even if it appears within
14035 -- the current scope (e.g. when the instance appears within the body
14036 -- of an ancestor).
14038 ----------------------
14039 -- Is_Instance_Node --
14040 ----------------------
14042 function Is_Instance_Node
(Decl
: Node_Id
) return Boolean is
14044 return Nkind
(Decl
) in N_Generic_Instantiation
14046 Nkind
(Original_Node
(Decl
)) = N_Formal_Package_Declaration
;
14047 end Is_Instance_Node
;
14049 -- Start of processing for Is_Global
14052 if E
= Gen_Scope
then
14055 elsif E
= Standard_Standard
then
14058 elsif Is_Child_Unit
(E
)
14059 and then (Is_Instance_Node
(Parent
(N2
))
14060 or else (Nkind
(Parent
(N2
)) = N_Expanded_Name
14061 and then N2
= Selector_Name
(Parent
(N2
))
14063 Is_Instance_Node
(Parent
(Parent
(N2
)))))
14069 while Se
/= Gen_Scope
loop
14070 if Se
= Standard_Standard
then
14081 --------------------------------
14082 -- Qualify_Universal_Operands --
14083 --------------------------------
14085 procedure Qualify_Universal_Operands
14087 Func_Call
: Node_Id
)
14089 procedure Qualify_Operand
(Opnd
: Node_Id
; Actual
: Node_Id
);
14090 -- Rewrite operand Opnd as a qualified expression of the form
14092 -- Actual_Typ'(Opnd)
14094 -- where Actual is the corresponding actual parameter of Opnd in
14095 -- function call Func_Call.
14097 function Qualify_Type
14099 Typ
: Entity_Id
) return Node_Id
;
14100 -- Qualify type Typ by creating a selected component of the form
14102 -- Scope_Of_Typ.Typ
14104 ---------------------
14105 -- Qualify_Operand --
14106 ---------------------
14108 procedure Qualify_Operand
(Opnd
: Node_Id
; Actual
: Node_Id
) is
14109 Loc
: constant Source_Ptr
:= Sloc
(Opnd
);
14110 Typ
: constant Entity_Id
:= Etype
(Actual
);
14115 -- Qualify the operand when it is of a universal type. Note that
14116 -- the template is unanalyzed and it is not possible to directly
14117 -- query the type. This transformation is not done when the type
14118 -- of the actual is internally generated because the type will be
14119 -- regenerated in the instance.
14121 if Yields_Universal_Type
(Opnd
)
14122 and then Comes_From_Source
(Typ
)
14123 and then not Is_Hidden
(Typ
)
14125 -- The type of the actual may be a global reference. Save this
14126 -- information by creating a reference to it.
14128 if Is_Global
(Typ
) then
14129 Mark
:= New_Occurrence_Of
(Typ
, Loc
);
14131 -- Otherwise rely on resolution to find the proper type within
14135 Mark
:= Qualify_Type
(Loc
, Typ
);
14139 Make_Qualified_Expression
(Loc
,
14140 Subtype_Mark
=> Mark
,
14141 Expression
=> Relocate_Node
(Opnd
));
14143 -- Mark the qualification to distinguish it from other source
14144 -- constructs and signal the instantiation mechanism that this
14145 -- node requires special processing. See Copy_Generic_Node for
14148 Set_Is_Qualified_Universal_Literal
(Qual
);
14150 Rewrite
(Opnd
, Qual
);
14152 end Qualify_Operand
;
14158 function Qualify_Type
14160 Typ
: Entity_Id
) return Node_Id
14162 Scop
: constant Entity_Id
:= Scope
(Typ
);
14166 Result
:= Make_Identifier
(Loc
, Chars
(Typ
));
14168 if Present
(Scop
) and then not Is_Generic_Unit
(Scop
) then
14170 Make_Selected_Component
(Loc
,
14171 Prefix
=> Make_Identifier
(Loc
, Chars
(Scop
)),
14172 Selector_Name
=> Result
);
14180 Actuals
: constant List_Id
:= Parameter_Associations
(Func_Call
);
14182 -- Start of processing for Qualify_Universal_Operands
14185 if Nkind
(Op
) in N_Binary_Op
then
14186 Qualify_Operand
(Left_Opnd
(Op
), First
(Actuals
));
14187 Qualify_Operand
(Right_Opnd
(Op
), Next
(First
(Actuals
)));
14189 elsif Nkind
(Op
) in N_Unary_Op
then
14190 Qualify_Operand
(Right_Opnd
(Op
), First
(Actuals
));
14192 end Qualify_Universal_Operands
;
14198 procedure Reset_Entity
(N
: Node_Id
) is
14199 procedure Set_Global_Type
(N
: Node_Id
; N2
: Node_Id
);
14200 -- If the type of N2 is global to the generic unit, save the type in
14201 -- the generic node. Just as we perform name capture for explicit
14202 -- references within the generic, we must capture the global types
14203 -- of local entities because they may participate in resolution in
14206 function Top_Ancestor
(E
: Entity_Id
) return Entity_Id
;
14207 -- Find the ultimate ancestor of the current unit. If it is not a
14208 -- generic unit, then the name of the current unit in the prefix of
14209 -- an expanded name must be replaced with its generic homonym to
14210 -- ensure that it will be properly resolved in an instance.
14212 ---------------------
14213 -- Set_Global_Type --
14214 ---------------------
14216 procedure Set_Global_Type
(N
: Node_Id
; N2
: Node_Id
) is
14217 Typ
: constant Entity_Id
:= Etype
(N2
);
14220 Set_Etype
(N
, Typ
);
14222 -- If the entity of N is not the associated node, this is a
14223 -- nested generic and it has an associated node as well, whose
14224 -- type is already the full view (see below). Indicate that the
14225 -- original node has a private view.
14227 if Entity
(N
) /= N2
and then Has_Private_View
(Entity
(N
)) then
14228 Set_Has_Private_View
(N
);
14231 -- If not a private type, nothing else to do
14233 if not Is_Private_Type
(Typ
) then
14234 if Is_Array_Type
(Typ
)
14235 and then Is_Private_Type
(Component_Type
(Typ
))
14237 Set_Has_Private_View
(N
);
14240 -- If it is a derivation of a private type in a context where no
14241 -- full view is needed, nothing to do either.
14243 elsif No
(Full_View
(Typ
)) and then Typ
/= Etype
(Typ
) then
14246 -- Otherwise mark the type for flipping and use the full view when
14250 Set_Has_Private_View
(N
);
14252 if Present
(Full_View
(Typ
)) then
14253 Set_Etype
(N2
, Full_View
(Typ
));
14257 if Is_Floating_Point_Type
(Typ
)
14258 and then Has_Dimension_System
(Typ
)
14260 Copy_Dimensions
(N2
, N
);
14262 end Set_Global_Type
;
14268 function Top_Ancestor
(E
: Entity_Id
) return Entity_Id
is
14273 while Is_Child_Unit
(Par
) loop
14274 Par
:= Scope
(Par
);
14280 -- Start of processing for Reset_Entity
14283 N2
:= Get_Associated_Node
(N
);
14286 if Present
(E
) then
14288 -- If the node is an entry call to an entry in an enclosing task,
14289 -- it is rewritten as a selected component. No global entity to
14290 -- preserve in this case, since the expansion will be redone in
14293 if not Nkind_In
(E
, N_Defining_Character_Literal
,
14294 N_Defining_Identifier
,
14295 N_Defining_Operator_Symbol
)
14297 Set_Associated_Node
(N
, Empty
);
14298 Set_Etype
(N
, Empty
);
14302 -- If the entity is an itype created as a subtype of an access
14303 -- type with a null exclusion restore source entity for proper
14304 -- visibility. The itype will be created anew in the instance.
14307 and then Ekind
(E
) = E_Access_Subtype
14308 and then Is_Entity_Name
(N
)
14309 and then Chars
(Etype
(E
)) = Chars
(N
)
14312 Set_Entity
(N2
, E
);
14316 if Is_Global
(E
) then
14318 -- If the entity is a package renaming that is the prefix of
14319 -- an expanded name, it has been rewritten as the renamed
14320 -- package, which is necessary semantically but complicates
14321 -- ASIS tree traversal, so we recover the original entity to
14322 -- expose the renaming. Take into account that the context may
14323 -- be a nested generic, that the original node may itself have
14324 -- an associated node that had better be an entity, and that
14325 -- the current node is still a selected component.
14327 if Ekind
(E
) = E_Package
14328 and then Nkind
(N
) = N_Selected_Component
14329 and then Nkind
(Parent
(N
)) = N_Expanded_Name
14330 and then Present
(Original_Node
(N2
))
14331 and then Is_Entity_Name
(Original_Node
(N2
))
14332 and then Present
(Entity
(Original_Node
(N2
)))
14334 if Is_Global
(Entity
(Original_Node
(N2
))) then
14335 N2
:= Original_Node
(N2
);
14336 Set_Associated_Node
(N
, N2
);
14337 Set_Global_Type
(N
, N2
);
14339 -- Renaming is local, and will be resolved in instance
14342 Set_Associated_Node
(N
, Empty
);
14343 Set_Etype
(N
, Empty
);
14347 Set_Global_Type
(N
, N2
);
14350 elsif Nkind
(N
) = N_Op_Concat
14351 and then Is_Generic_Type
(Etype
(N2
))
14352 and then (Base_Type
(Etype
(Right_Opnd
(N2
))) = Etype
(N2
)
14354 Base_Type
(Etype
(Left_Opnd
(N2
))) = Etype
(N2
))
14355 and then Is_Intrinsic_Subprogram
(E
)
14359 -- Entity is local. Mark generic node as unresolved. Note that now
14360 -- it does not have an entity.
14363 Set_Associated_Node
(N
, Empty
);
14364 Set_Etype
(N
, Empty
);
14367 if Nkind
(Parent
(N
)) in N_Generic_Instantiation
14368 and then N
= Name
(Parent
(N
))
14370 Save_Global_Defaults
(Parent
(N
), Parent
(N2
));
14373 elsif Nkind
(Parent
(N
)) = N_Selected_Component
14374 and then Nkind
(Parent
(N2
)) = N_Expanded_Name
14376 if Is_Global
(Entity
(Parent
(N2
))) then
14377 Change_Selected_Component_To_Expanded_Name
(Parent
(N
));
14378 Set_Associated_Node
(Parent
(N
), Parent
(N2
));
14379 Set_Global_Type
(Parent
(N
), Parent
(N2
));
14380 Save_Entity_Descendants
(N
);
14382 -- If this is a reference to the current generic entity, replace
14383 -- by the name of the generic homonym of the current package. This
14384 -- is because in an instantiation Par.P.Q will not resolve to the
14385 -- name of the instance, whose enclosing scope is not necessarily
14386 -- Par. We use the generic homonym rather that the name of the
14387 -- generic itself because it may be hidden by a local declaration.
14389 elsif In_Open_Scopes
(Entity
(Parent
(N2
)))
14391 Is_Generic_Unit
(Top_Ancestor
(Entity
(Prefix
(Parent
(N2
)))))
14393 if Ekind
(Entity
(Parent
(N2
))) = E_Generic_Package
then
14394 Rewrite
(Parent
(N
),
14395 Make_Identifier
(Sloc
(N
),
14397 Chars
(Generic_Homonym
(Entity
(Parent
(N2
))))));
14399 Rewrite
(Parent
(N
),
14400 Make_Identifier
(Sloc
(N
),
14401 Chars
=> Chars
(Selector_Name
(Parent
(N2
)))));
14405 if Nkind
(Parent
(Parent
(N
))) in N_Generic_Instantiation
14406 and then Parent
(N
) = Name
(Parent
(Parent
(N
)))
14408 Save_Global_Defaults
14409 (Parent
(Parent
(N
)), Parent
(Parent
(N2
)));
14412 -- A selected component may denote a static constant that has been
14413 -- folded. If the static constant is global to the generic, capture
14414 -- its value. Otherwise the folding will happen in any instantiation.
14416 elsif Nkind
(Parent
(N
)) = N_Selected_Component
14417 and then Nkind_In
(Parent
(N2
), N_Integer_Literal
, N_Real_Literal
)
14419 if Present
(Entity
(Original_Node
(Parent
(N2
))))
14420 and then Is_Global
(Entity
(Original_Node
(Parent
(N2
))))
14422 Rewrite
(Parent
(N
), New_Copy
(Parent
(N2
)));
14423 Set_Analyzed
(Parent
(N
), False);
14426 -- A selected component may be transformed into a parameterless
14427 -- function call. If the called entity is global, rewrite the node
14428 -- appropriately, i.e. as an extended name for the global entity.
14430 elsif Nkind
(Parent
(N
)) = N_Selected_Component
14431 and then Nkind
(Parent
(N2
)) = N_Function_Call
14432 and then N
= Selector_Name
(Parent
(N
))
14434 if No
(Parameter_Associations
(Parent
(N2
))) then
14435 if Is_Global
(Entity
(Name
(Parent
(N2
)))) then
14436 Change_Selected_Component_To_Expanded_Name
(Parent
(N
));
14437 Set_Associated_Node
(Parent
(N
), Name
(Parent
(N2
)));
14438 Set_Global_Type
(Parent
(N
), Name
(Parent
(N2
)));
14439 Save_Entity_Descendants
(N
);
14442 Set_Is_Prefixed_Call
(Parent
(N
));
14443 Set_Associated_Node
(N
, Empty
);
14444 Set_Etype
(N
, Empty
);
14447 -- In Ada 2005, X.F may be a call to a primitive operation,
14448 -- rewritten as F (X). This rewriting will be done again in an
14449 -- instance, so keep the original node. Global entities will be
14450 -- captured as for other constructs. Indicate that this must
14451 -- resolve as a call, to prevent accidental overloading in the
14452 -- instance, if both a component and a primitive operation appear
14456 Set_Is_Prefixed_Call
(Parent
(N
));
14459 -- Entity is local. Reset in generic unit, so that node is resolved
14460 -- anew at the point of instantiation.
14463 Set_Associated_Node
(N
, Empty
);
14464 Set_Etype
(N
, Empty
);
14468 -----------------------------
14469 -- Save_Entity_Descendants --
14470 -----------------------------
14472 procedure Save_Entity_Descendants
(N
: Node_Id
) is
14475 when N_Binary_Op
=>
14476 Save_Global_Descendant
(Union_Id
(Left_Opnd
(N
)));
14477 Save_Global_Descendant
(Union_Id
(Right_Opnd
(N
)));
14480 Save_Global_Descendant
(Union_Id
(Right_Opnd
(N
)));
14482 when N_Expanded_Name |
14483 N_Selected_Component
=>
14484 Save_Global_Descendant
(Union_Id
(Prefix
(N
)));
14485 Save_Global_Descendant
(Union_Id
(Selector_Name
(N
)));
14487 when N_Identifier |
14488 N_Character_Literal |
14489 N_Operator_Symbol
=>
14493 raise Program_Error
;
14495 end Save_Entity_Descendants
;
14497 --------------------------
14498 -- Save_Global_Defaults --
14499 --------------------------
14501 procedure Save_Global_Defaults
(N1
: Node_Id
; N2
: Node_Id
) is
14502 Loc
: constant Source_Ptr
:= Sloc
(N1
);
14503 Assoc2
: constant List_Id
:= Generic_Associations
(N2
);
14504 Gen_Id
: constant Entity_Id
:= Get_Generic_Entity
(N2
);
14511 Actual
: Entity_Id
;
14514 Assoc1
:= Generic_Associations
(N1
);
14516 if Present
(Assoc1
) then
14517 Act1
:= First
(Assoc1
);
14520 Set_Generic_Associations
(N1
, New_List
);
14521 Assoc1
:= Generic_Associations
(N1
);
14524 if Present
(Assoc2
) then
14525 Act2
:= First
(Assoc2
);
14530 while Present
(Act1
) and then Present
(Act2
) loop
14535 -- Find the associations added for default subprograms
14537 if Present
(Act2
) then
14538 while Nkind
(Act2
) /= N_Generic_Association
14539 or else No
(Entity
(Selector_Name
(Act2
)))
14540 or else not Is_Overloadable
(Entity
(Selector_Name
(Act2
)))
14545 -- Add a similar association if the default is global. The
14546 -- renaming declaration for the actual has been analyzed, and
14547 -- its alias is the program it renames. Link the actual in the
14548 -- original generic tree with the node in the analyzed tree.
14550 while Present
(Act2
) loop
14551 Subp
:= Entity
(Selector_Name
(Act2
));
14552 Def
:= Explicit_Generic_Actual_Parameter
(Act2
);
14554 -- Following test is defence against rubbish errors
14556 if No
(Alias
(Subp
)) then
14560 -- Retrieve the resolved actual from the renaming declaration
14561 -- created for the instantiated formal.
14563 Actual
:= Entity
(Name
(Parent
(Parent
(Subp
))));
14564 Set_Entity
(Def
, Actual
);
14565 Set_Etype
(Def
, Etype
(Actual
));
14567 if Is_Global
(Actual
) then
14569 Make_Generic_Association
(Loc
,
14571 New_Occurrence_Of
(Subp
, Loc
),
14572 Explicit_Generic_Actual_Parameter
=>
14573 New_Occurrence_Of
(Actual
, Loc
));
14575 Set_Associated_Node
14576 (Explicit_Generic_Actual_Parameter
(Ndec
), Def
);
14578 Append
(Ndec
, Assoc1
);
14580 -- If there are other defaults, add a dummy association in case
14581 -- there are other defaulted formals with the same name.
14583 elsif Present
(Next
(Act2
)) then
14585 Make_Generic_Association
(Loc
,
14587 New_Occurrence_Of
(Subp
, Loc
),
14588 Explicit_Generic_Actual_Parameter
=> Empty
);
14590 Append
(Ndec
, Assoc1
);
14597 if Nkind
(Name
(N1
)) = N_Identifier
14598 and then Is_Child_Unit
(Gen_Id
)
14599 and then Is_Global
(Gen_Id
)
14600 and then Is_Generic_Unit
(Scope
(Gen_Id
))
14601 and then In_Open_Scopes
(Scope
(Gen_Id
))
14603 -- This is an instantiation of a child unit within a sibling, so
14604 -- that the generic parent is in scope. An eventual instance must
14605 -- occur within the scope of an instance of the parent. Make name
14606 -- in instance into an expanded name, to preserve the identifier
14607 -- of the parent, so it can be resolved subsequently.
14609 Rewrite
(Name
(N2
),
14610 Make_Expanded_Name
(Loc
,
14611 Chars
=> Chars
(Gen_Id
),
14612 Prefix
=> New_Occurrence_Of
(Scope
(Gen_Id
), Loc
),
14613 Selector_Name
=> New_Occurrence_Of
(Gen_Id
, Loc
)));
14614 Set_Entity
(Name
(N2
), Gen_Id
);
14616 Rewrite
(Name
(N1
),
14617 Make_Expanded_Name
(Loc
,
14618 Chars
=> Chars
(Gen_Id
),
14619 Prefix
=> New_Occurrence_Of
(Scope
(Gen_Id
), Loc
),
14620 Selector_Name
=> New_Occurrence_Of
(Gen_Id
, Loc
)));
14622 Set_Associated_Node
(Name
(N1
), Name
(N2
));
14623 Set_Associated_Node
(Prefix
(Name
(N1
)), Empty
);
14624 Set_Associated_Node
14625 (Selector_Name
(Name
(N1
)), Selector_Name
(Name
(N2
)));
14626 Set_Etype
(Name
(N1
), Etype
(Gen_Id
));
14628 end Save_Global_Defaults
;
14630 ----------------------------
14631 -- Save_Global_Descendant --
14632 ----------------------------
14634 procedure Save_Global_Descendant
(D
: Union_Id
) is
14638 if D
in Node_Range
then
14639 if D
= Union_Id
(Empty
) then
14642 elsif Nkind
(Node_Id
(D
)) /= N_Compilation_Unit
then
14643 Save_References
(Node_Id
(D
));
14646 elsif D
in List_Range
then
14647 pragma Assert
(D
/= Union_Id
(No_List
));
14648 -- Because No_List = Empty, which is in Node_Range above
14650 if Is_Empty_List
(List_Id
(D
)) then
14654 N1
:= First
(List_Id
(D
));
14655 while Present
(N1
) loop
14656 Save_References
(N1
);
14661 -- Element list or other non-node field, nothing to do
14666 end Save_Global_Descendant
;
14668 ---------------------
14669 -- Save_References --
14670 ---------------------
14672 -- This is the recursive procedure that does the work once the enclosing
14673 -- generic scope has been established. We have to treat specially a
14674 -- number of node rewritings that are required by semantic processing
14675 -- and which change the kind of nodes in the generic copy: typically
14676 -- constant-folding, replacing an operator node by a string literal, or
14677 -- a selected component by an expanded name. In each of those cases, the
14678 -- transformation is propagated to the generic unit.
14680 procedure Save_References
(N
: Node_Id
) is
14681 Loc
: constant Source_Ptr
:= Sloc
(N
);
14683 function Requires_Delayed_Save
(Nod
: Node_Id
) return Boolean;
14684 -- Determine whether arbitrary node Nod requires delayed capture of
14685 -- global references within its aspect specifications.
14687 procedure Save_References_In_Aggregate
(N
: Node_Id
);
14688 -- Save all global references in [extension] aggregate node N
14690 procedure Save_References_In_Char_Lit_Or_Op_Symbol
(N
: Node_Id
);
14691 -- Save all global references in a character literal or operator
14692 -- symbol denoted by N.
14694 procedure Save_References_In_Descendants
(N
: Node_Id
);
14695 -- Save all global references in all descendants of node N
14697 procedure Save_References_In_Identifier
(N
: Node_Id
);
14698 -- Save all global references in identifier node N
14700 procedure Save_References_In_Operator
(N
: Node_Id
);
14701 -- Save all global references in operator node N
14703 procedure Save_References_In_Pragma
(Prag
: Node_Id
);
14704 -- Save all global references found within the expression of pragma
14707 ---------------------------
14708 -- Requires_Delayed_Save --
14709 ---------------------------
14711 function Requires_Delayed_Save
(Nod
: Node_Id
) return Boolean is
14713 -- Generic packages and subprograms require delayed capture of
14714 -- global references within their aspects due to the timing of
14715 -- annotation analysis.
14717 if Nkind_In
(Nod
, N_Generic_Package_Declaration
,
14718 N_Generic_Subprogram_Declaration
,
14720 N_Package_Body_Stub
,
14722 N_Subprogram_Body_Stub
)
14724 -- Since the capture of global references is done on the
14725 -- unanalyzed generic template, there is no information around
14726 -- to infer the context. Use the Associated_Entity linkages to
14727 -- peek into the analyzed generic copy and determine what the
14728 -- template corresponds to.
14730 if Nod
= Templ
then
14732 Is_Generic_Declaration_Or_Body
14733 (Unit_Declaration_Node
14734 (Associated_Entity
(Defining_Entity
(Nod
))));
14736 -- Otherwise the generic unit being processed is not the top
14737 -- level template. It is safe to capture of global references
14738 -- within the generic unit because at this point the top level
14739 -- copy is fully analyzed.
14745 -- Otherwise capture the global references without interference
14750 end Requires_Delayed_Save
;
14752 ----------------------------------
14753 -- Save_References_In_Aggregate --
14754 ----------------------------------
14756 procedure Save_References_In_Aggregate
(N
: Node_Id
) is
14758 Qual
: Node_Id
:= Empty
;
14759 Typ
: Entity_Id
:= Empty
;
14761 use Atree
.Unchecked_Access
;
14762 -- This code section is part of implementing an untyped tree
14763 -- traversal, so it needs direct access to node fields.
14766 N2
:= Get_Associated_Node
(N
);
14768 if Present
(N2
) then
14771 -- In an instance within a generic, use the name of the actual
14772 -- and not the original generic parameter. If the actual is
14773 -- global in the current generic it must be preserved for its
14776 if Nkind
(Parent
(Typ
)) = N_Subtype_Declaration
14777 and then Present
(Generic_Parent_Type
(Parent
(Typ
)))
14779 Typ
:= Base_Type
(Typ
);
14780 Set_Etype
(N2
, Typ
);
14784 if No
(N2
) or else No
(Typ
) or else not Is_Global
(Typ
) then
14785 Set_Associated_Node
(N
, Empty
);
14787 -- If the aggregate is an actual in a call, it has been
14788 -- resolved in the current context, to some local type. The
14789 -- enclosing call may have been disambiguated by the aggregate,
14790 -- and this disambiguation might fail at instantiation time
14791 -- because the type to which the aggregate did resolve is not
14792 -- preserved. In order to preserve some of this information,
14793 -- wrap the aggregate in a qualified expression, using the id
14794 -- of its type. For further disambiguation we qualify the type
14795 -- name with its scope (if visible) because both id's will have
14796 -- corresponding entities in an instance. This resolves most of
14797 -- the problems with missing type information on aggregates in
14801 and then Nkind
(N2
) = Nkind
(N
)
14802 and then Nkind
(Parent
(N2
)) in N_Subprogram_Call
14803 and then Present
(Typ
)
14804 and then Comes_From_Source
(Typ
)
14806 Nam
:= Make_Identifier
(Loc
, Chars
(Typ
));
14808 if Is_Immediately_Visible
(Scope
(Typ
)) then
14810 Make_Selected_Component
(Loc
,
14812 Make_Identifier
(Loc
, Chars
(Scope
(Typ
))),
14813 Selector_Name
=> Nam
);
14817 Make_Qualified_Expression
(Loc
,
14818 Subtype_Mark
=> Nam
,
14819 Expression
=> Relocate_Node
(N
));
14823 Save_Global_Descendant
(Field1
(N
));
14824 Save_Global_Descendant
(Field2
(N
));
14825 Save_Global_Descendant
(Field3
(N
));
14826 Save_Global_Descendant
(Field5
(N
));
14828 if Present
(Qual
) then
14831 end Save_References_In_Aggregate
;
14833 ----------------------------------------------
14834 -- Save_References_In_Char_Lit_Or_Op_Symbol --
14835 ----------------------------------------------
14837 procedure Save_References_In_Char_Lit_Or_Op_Symbol
(N
: Node_Id
) is
14839 if Nkind
(N
) = Nkind
(Get_Associated_Node
(N
)) then
14842 elsif Nkind
(N
) = N_Operator_Symbol
14843 and then Nkind
(Get_Associated_Node
(N
)) = N_String_Literal
14845 Change_Operator_Symbol_To_String_Literal
(N
);
14847 end Save_References_In_Char_Lit_Or_Op_Symbol
;
14849 ------------------------------------
14850 -- Save_References_In_Descendants --
14851 ------------------------------------
14853 procedure Save_References_In_Descendants
(N
: Node_Id
) is
14854 use Atree
.Unchecked_Access
;
14855 -- This code section is part of implementing an untyped tree
14856 -- traversal, so it needs direct access to node fields.
14859 Save_Global_Descendant
(Field1
(N
));
14860 Save_Global_Descendant
(Field2
(N
));
14861 Save_Global_Descendant
(Field3
(N
));
14862 Save_Global_Descendant
(Field4
(N
));
14863 Save_Global_Descendant
(Field5
(N
));
14864 end Save_References_In_Descendants
;
14866 -----------------------------------
14867 -- Save_References_In_Identifier --
14868 -----------------------------------
14870 procedure Save_References_In_Identifier
(N
: Node_Id
) is
14872 -- The node did not undergo a transformation
14874 if Nkind
(N
) = Nkind
(Get_Associated_Node
(N
)) then
14876 Aux_N2
: constant Node_Id
:= Get_Associated_Node
(N
);
14877 Orig_N2_Parent
: constant Node_Id
:=
14878 Original_Node
(Parent
(Aux_N2
));
14880 -- The parent of this identifier is a selected component
14881 -- which denotes a named number that was constant folded.
14882 -- Preserve the original name for ASIS and link the parent
14883 -- with its expanded name. The constant folding will be
14884 -- repeated in the instance.
14886 if Nkind
(Parent
(N
)) = N_Selected_Component
14887 and then Nkind_In
(Parent
(Aux_N2
), N_Integer_Literal
,
14889 and then Is_Entity_Name
(Orig_N2_Parent
)
14890 and then Ekind
(Entity
(Orig_N2_Parent
)) in Named_Kind
14891 and then Is_Global
(Entity
(Orig_N2_Parent
))
14894 Set_Associated_Node
14895 (Parent
(N
), Original_Node
(Parent
(N2
)));
14900 -- If this is a discriminant reference, always save it.
14901 -- It is used in the instance to find the corresponding
14902 -- discriminant positionally rather than by name.
14904 Set_Original_Discriminant
14905 (N
, Original_Discriminant
(Get_Associated_Node
(N
)));
14911 -- The analysis of the generic copy transformed the identifier
14912 -- into another construct. Propagate the changes to the template.
14915 N2
:= Get_Associated_Node
(N
);
14917 -- The identifier denotes a call to a parameterless function.
14918 -- Mark the node as resolved when the function is external.
14920 if Nkind
(N2
) = N_Function_Call
then
14921 E
:= Entity
(Name
(N2
));
14923 if Present
(E
) and then Is_Global
(E
) then
14924 Set_Etype
(N
, Etype
(N2
));
14926 Set_Associated_Node
(N
, Empty
);
14927 Set_Etype
(N
, Empty
);
14930 -- The identifier denotes a named number that was constant
14931 -- folded. Preserve the original name for ASIS and undo the
14932 -- constant folding which will be repeated in the instance.
14934 elsif Nkind_In
(N2
, N_Integer_Literal
, N_Real_Literal
)
14935 and then Is_Entity_Name
(Original_Node
(N2
))
14937 Set_Associated_Node
(N
, Original_Node
(N2
));
14940 -- The identifier resolved to a string literal. Propagate this
14941 -- information to the generic template.
14943 elsif Nkind
(N2
) = N_String_Literal
then
14944 Rewrite
(N
, New_Copy
(N2
));
14946 -- The identifier is rewritten as a dereference if it is the
14947 -- prefix of an implicit dereference. Preserve the original
14948 -- tree as the analysis of the instance will expand the node
14949 -- again, but preserve the resolved entity if it is global.
14951 elsif Nkind
(N2
) = N_Explicit_Dereference
then
14952 if Is_Entity_Name
(Prefix
(N2
))
14953 and then Present
(Entity
(Prefix
(N2
)))
14954 and then Is_Global
(Entity
(Prefix
(N2
)))
14956 Set_Associated_Node
(N
, Prefix
(N2
));
14958 elsif Nkind
(Prefix
(N2
)) = N_Function_Call
14959 and then Present
(Entity
(Name
(Prefix
(N2
))))
14960 and then Is_Global
(Entity
(Name
(Prefix
(N2
))))
14963 Make_Explicit_Dereference
(Loc
,
14965 Make_Function_Call
(Loc
,
14968 (Entity
(Name
(Prefix
(N2
))), Loc
))));
14971 Set_Associated_Node
(N
, Empty
);
14972 Set_Etype
(N
, Empty
);
14975 -- The subtype mark of a nominally unconstrained object is
14976 -- rewritten as a subtype indication using the bounds of the
14977 -- expression. Recover the original subtype mark.
14979 elsif Nkind
(N2
) = N_Subtype_Indication
14980 and then Is_Entity_Name
(Original_Node
(N2
))
14982 Set_Associated_Node
(N
, Original_Node
(N2
));
14986 end Save_References_In_Identifier
;
14988 ---------------------------------
14989 -- Save_References_In_Operator --
14990 ---------------------------------
14992 procedure Save_References_In_Operator
(N
: Node_Id
) is
14994 -- The node did not undergo a transformation
14996 if Nkind
(N
) = Nkind
(Get_Associated_Node
(N
)) then
14997 if Nkind
(N
) = N_Op_Concat
then
14998 Set_Is_Component_Left_Opnd
(N
,
14999 Is_Component_Left_Opnd
(Get_Associated_Node
(N
)));
15001 Set_Is_Component_Right_Opnd
(N
,
15002 Is_Component_Right_Opnd
(Get_Associated_Node
(N
)));
15007 -- The analysis of the generic copy transformed the operator into
15008 -- some other construct. Propagate the changes to the template if
15012 N2
:= Get_Associated_Node
(N
);
15014 -- The operator resoved to a function call
15016 if Nkind
(N2
) = N_Function_Call
then
15018 -- Add explicit qualifications in the generic template for
15019 -- all operands of universal type. This aids resolution by
15020 -- preserving the actual type of a literal or an attribute
15021 -- that yields a universal result.
15023 Qualify_Universal_Operands
(N
, N2
);
15025 E
:= Entity
(Name
(N2
));
15027 if Present
(E
) and then Is_Global
(E
) then
15028 Set_Etype
(N
, Etype
(N2
));
15030 Set_Associated_Node
(N
, Empty
);
15031 Set_Etype
(N
, Empty
);
15034 -- The operator was folded into a literal
15036 elsif Nkind_In
(N2
, N_Integer_Literal
,
15040 if Present
(Original_Node
(N2
))
15041 and then Nkind
(Original_Node
(N2
)) = Nkind
(N
)
15043 -- Operation was constant-folded. Whenever possible,
15044 -- recover semantic information from unfolded node,
15047 Set_Associated_Node
(N
, Original_Node
(N2
));
15049 if Nkind
(N
) = N_Op_Concat
then
15050 Set_Is_Component_Left_Opnd
(N
,
15051 Is_Component_Left_Opnd
(Get_Associated_Node
(N
)));
15052 Set_Is_Component_Right_Opnd
(N
,
15053 Is_Component_Right_Opnd
(Get_Associated_Node
(N
)));
15058 -- Propagate the constant folding back to the template
15061 Rewrite
(N
, New_Copy
(N2
));
15062 Set_Analyzed
(N
, False);
15065 -- The operator was folded into an enumeration literal. Retain
15066 -- the entity to avoid spurious ambiguities if it is overloaded
15067 -- at the point of instantiation or inlining.
15069 elsif Nkind
(N2
) = N_Identifier
15070 and then Ekind
(Entity
(N2
)) = E_Enumeration_Literal
15072 Rewrite
(N
, New_Copy
(N2
));
15073 Set_Analyzed
(N
, False);
15077 -- Complete the operands check if node has not been constant
15080 if Nkind
(N
) in N_Op
then
15081 Save_Entity_Descendants
(N
);
15083 end Save_References_In_Operator
;
15085 -------------------------------
15086 -- Save_References_In_Pragma --
15087 -------------------------------
15089 procedure Save_References_In_Pragma
(Prag
: Node_Id
) is
15091 Do_Save
: Boolean := True;
15093 use Atree
.Unchecked_Access
;
15094 -- This code section is part of implementing an untyped tree
15095 -- traversal, so it needs direct access to node fields.
15098 -- Do not save global references in pragmas generated from aspects
15099 -- because the pragmas will be regenerated at instantiation time.
15101 if From_Aspect_Specification
(Prag
) then
15104 -- The capture of global references within contract-related source
15105 -- pragmas associated with generic packages, subprograms or their
15106 -- respective bodies must be delayed due to timing of annotation
15107 -- analysis. Global references are still captured in routine
15108 -- Save_Global_References_In_Contract.
15110 elsif Is_Generic_Contract_Pragma
(Prag
) and then Prag
/= Templ
then
15111 if Is_Package_Contract_Annotation
(Prag
) then
15112 Context
:= Find_Related_Package_Or_Body
(Prag
);
15114 pragma Assert
(Is_Subprogram_Contract_Annotation
(Prag
));
15115 Context
:= Find_Related_Declaration_Or_Body
(Prag
);
15118 -- The use of Original_Node accounts for the case when the
15119 -- related context is generic template.
15121 if Requires_Delayed_Save
(Original_Node
(Context
)) then
15126 -- For all other cases, save all global references within the
15127 -- descendants, but skip the following semantic fields:
15129 -- Field1 - Next_Pragma
15130 -- Field3 - Corresponding_Aspect
15131 -- Field5 - Next_Rep_Item
15134 Save_Global_Descendant
(Field2
(Prag
));
15135 Save_Global_Descendant
(Field4
(Prag
));
15137 end Save_References_In_Pragma
;
15139 -- Start of processing for Save_References
15147 elsif Nkind_In
(N
, N_Aggregate
, N_Extension_Aggregate
) then
15148 Save_References_In_Aggregate
(N
);
15150 -- Character literals, operator symbols
15152 elsif Nkind_In
(N
, N_Character_Literal
, N_Operator_Symbol
) then
15153 Save_References_In_Char_Lit_Or_Op_Symbol
(N
);
15155 -- Defining identifiers
15157 elsif Nkind
(N
) in N_Entity
then
15162 elsif Nkind
(N
) = N_Identifier
then
15163 Save_References_In_Identifier
(N
);
15167 elsif Nkind
(N
) in N_Op
then
15168 Save_References_In_Operator
(N
);
15172 elsif Nkind
(N
) = N_Pragma
then
15173 Save_References_In_Pragma
(N
);
15176 Save_References_In_Descendants
(N
);
15179 -- Save all global references found within the aspect specifications
15180 -- of the related node.
15182 if Permits_Aspect_Specifications
(N
) and then Has_Aspects
(N
) then
15184 -- The capture of global references within aspects associated with
15185 -- generic packages, subprograms or their bodies must be delayed
15186 -- due to timing of annotation analysis. Global references are
15187 -- still captured in routine Save_Global_References_In_Contract.
15189 if Requires_Delayed_Save
(N
) then
15192 -- Otherwise save all global references within the aspects
15195 Save_Global_References_In_Aspects
(N
);
15198 end Save_References
;
15200 -- Start of processing for Save_Global_References
15203 Gen_Scope
:= Current_Scope
;
15205 -- If the generic unit is a child unit, references to entities in the
15206 -- parent are treated as local, because they will be resolved anew in
15207 -- the context of the instance of the parent.
15209 while Is_Child_Unit
(Gen_Scope
)
15210 and then Ekind
(Scope
(Gen_Scope
)) = E_Generic_Package
15212 Gen_Scope
:= Scope
(Gen_Scope
);
15215 Save_References
(Templ
);
15216 end Save_Global_References
;
15218 ---------------------------------------
15219 -- Save_Global_References_In_Aspects --
15220 ---------------------------------------
15222 procedure Save_Global_References_In_Aspects
(N
: Node_Id
) is
15227 Asp
:= First
(Aspect_Specifications
(N
));
15228 while Present
(Asp
) loop
15229 Expr
:= Expression
(Asp
);
15231 if Present
(Expr
) then
15232 Save_Global_References
(Expr
);
15237 end Save_Global_References_In_Aspects
;
15239 ------------------------------------------
15240 -- Set_Copied_Sloc_For_Inherited_Pragma --
15241 ------------------------------------------
15243 procedure Set_Copied_Sloc_For_Inherited_Pragma
15248 Create_Instantiation_Source
(N
, E
,
15249 Inlined_Body
=> False,
15250 Inherited_Pragma
=> True,
15251 Factor
=> S_Adjustment
);
15252 end Set_Copied_Sloc_For_Inherited_Pragma
;
15254 --------------------------------------
15255 -- Set_Copied_Sloc_For_Inlined_Body --
15256 --------------------------------------
15258 procedure Set_Copied_Sloc_For_Inlined_Body
(N
: Node_Id
; E
: Entity_Id
) is
15260 Create_Instantiation_Source
(N
, E
,
15261 Inlined_Body
=> True,
15262 Inherited_Pragma
=> False,
15263 Factor
=> S_Adjustment
);
15264 end Set_Copied_Sloc_For_Inlined_Body
;
15266 ---------------------
15267 -- Set_Instance_Of --
15268 ---------------------
15270 procedure Set_Instance_Of
(A
: Entity_Id
; B
: Entity_Id
) is
15272 Generic_Renamings
.Table
(Generic_Renamings
.Last
) := (A
, B
, Assoc_Null
);
15273 Generic_Renamings_HTable
.Set
(Generic_Renamings
.Last
);
15274 Generic_Renamings
.Increment_Last
;
15275 end Set_Instance_Of
;
15277 --------------------
15278 -- Set_Next_Assoc --
15279 --------------------
15281 procedure Set_Next_Assoc
(E
: Assoc_Ptr
; Next
: Assoc_Ptr
) is
15283 Generic_Renamings
.Table
(E
).Next_In_HTable
:= Next
;
15284 end Set_Next_Assoc
;
15286 -------------------
15287 -- Start_Generic --
15288 -------------------
15290 procedure Start_Generic
is
15292 -- ??? More things could be factored out in this routine.
15293 -- Should probably be done at a later stage.
15295 Generic_Flags
.Append
(Inside_A_Generic
);
15296 Inside_A_Generic
:= True;
15298 Expander_Mode_Save_And_Set
(False);
15301 ----------------------
15302 -- Set_Instance_Env --
15303 ----------------------
15305 procedure Set_Instance_Env
15306 (Gen_Unit
: Entity_Id
;
15307 Act_Unit
: Entity_Id
)
15309 Assertion_Status
: constant Boolean := Assertions_Enabled
;
15310 Save_SPARK_Mode
: constant SPARK_Mode_Type
:= SPARK_Mode
;
15311 Save_SPARK_Mode_Pragma
: constant Node_Id
:= SPARK_Mode_Pragma
;
15314 -- Regardless of the current mode, predefined units are analyzed in the
15315 -- most current Ada mode, and earlier version Ada checks do not apply
15316 -- to predefined units. Nothing needs to be done for non-internal units.
15317 -- These are always analyzed in the current mode.
15319 if Is_Internal_File_Name
15320 (Fname
=> Unit_File_Name
(Get_Source_Unit
(Gen_Unit
)),
15321 Renamings_Included
=> True)
15323 Set_Opt_Config_Switches
(True, Current_Sem_Unit
= Main_Unit
);
15325 -- In Ada2012 we may want to enable assertions in an instance of a
15326 -- predefined unit, in which case we need to preserve the current
15327 -- setting for the Assertions_Enabled flag. This will become more
15328 -- critical when pre/postconditions are added to predefined units,
15329 -- as is already the case for some numeric libraries.
15331 if Ada_Version
>= Ada_2012
then
15332 Assertions_Enabled
:= Assertion_Status
;
15335 -- SPARK_Mode for an instance is the one applicable at the point of
15338 SPARK_Mode
:= Save_SPARK_Mode
;
15339 SPARK_Mode_Pragma
:= Save_SPARK_Mode_Pragma
;
15342 Current_Instantiated_Parent
:=
15343 (Gen_Id
=> Gen_Unit
,
15344 Act_Id
=> Act_Unit
,
15345 Next_In_HTable
=> Assoc_Null
);
15346 end Set_Instance_Env
;
15352 procedure Switch_View
(T
: Entity_Id
) is
15353 BT
: constant Entity_Id
:= Base_Type
(T
);
15354 Priv_Elmt
: Elmt_Id
:= No_Elmt
;
15355 Priv_Sub
: Entity_Id
;
15358 -- T may be private but its base type may have been exchanged through
15359 -- some other occurrence, in which case there is nothing to switch
15360 -- besides T itself. Note that a private dependent subtype of a private
15361 -- type might not have been switched even if the base type has been,
15362 -- because of the last branch of Check_Private_View (see comment there).
15364 if not Is_Private_Type
(BT
) then
15365 Prepend_Elmt
(Full_View
(T
), Exchanged_Views
);
15366 Exchange_Declarations
(T
);
15370 Priv_Elmt
:= First_Elmt
(Private_Dependents
(BT
));
15372 if Present
(Full_View
(BT
)) then
15373 Prepend_Elmt
(Full_View
(BT
), Exchanged_Views
);
15374 Exchange_Declarations
(BT
);
15377 while Present
(Priv_Elmt
) loop
15378 Priv_Sub
:= (Node
(Priv_Elmt
));
15380 -- We avoid flipping the subtype if the Etype of its full view is
15381 -- private because this would result in a malformed subtype. This
15382 -- occurs when the Etype of the subtype full view is the full view of
15383 -- the base type (and since the base types were just switched, the
15384 -- subtype is pointing to the wrong view). This is currently the case
15385 -- for tagged record types, access types (maybe more?) and needs to
15386 -- be resolved. ???
15388 if Present
(Full_View
(Priv_Sub
))
15389 and then not Is_Private_Type
(Etype
(Full_View
(Priv_Sub
)))
15391 Prepend_Elmt
(Full_View
(Priv_Sub
), Exchanged_Views
);
15392 Exchange_Declarations
(Priv_Sub
);
15395 Next_Elmt
(Priv_Elmt
);
15403 function True_Parent
(N
: Node_Id
) return Node_Id
is
15405 if Nkind
(Parent
(N
)) = N_Subunit
then
15406 return Parent
(Corresponding_Stub
(Parent
(N
)));
15412 -----------------------------
15413 -- Valid_Default_Attribute --
15414 -----------------------------
15416 procedure Valid_Default_Attribute
(Nam
: Entity_Id
; Def
: Node_Id
) is
15417 Attr_Id
: constant Attribute_Id
:=
15418 Get_Attribute_Id
(Attribute_Name
(Def
));
15419 T
: constant Entity_Id
:= Entity
(Prefix
(Def
));
15420 Is_Fun
: constant Boolean := (Ekind
(Nam
) = E_Function
);
15426 if No
(T
) or else T
= Any_Id
then
15431 F
:= First_Formal
(Nam
);
15432 while Present
(F
) loop
15433 Num_F
:= Num_F
+ 1;
15438 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
15439 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
15440 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
15441 Attribute_Unbiased_Rounding
=>
15444 and then Is_Floating_Point_Type
(T
);
15446 when Attribute_Image | Attribute_Pred | Attribute_Succ |
15447 Attribute_Value | Attribute_Wide_Image |
15448 Attribute_Wide_Value
=>
15449 OK
:= (Is_Fun
and then Num_F
= 1 and then Is_Scalar_Type
(T
));
15451 when Attribute_Max | Attribute_Min
=>
15452 OK
:= (Is_Fun
and then Num_F
= 2 and then Is_Scalar_Type
(T
));
15454 when Attribute_Input
=>
15455 OK
:= (Is_Fun
and then Num_F
= 1);
15457 when Attribute_Output | Attribute_Read | Attribute_Write
=>
15458 OK
:= (not Is_Fun
and then Num_F
= 2);
15466 ("attribute reference has wrong profile for subprogram", Def
);
15468 end Valid_Default_Attribute
;