1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2018, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects
; use Aspects
;
27 with Atree
; use Atree
;
28 with Contracts
; use Contracts
;
29 with Einfo
; use Einfo
;
30 with Elists
; use Elists
;
31 with Errout
; use Errout
;
32 with Expander
; use Expander
;
33 with Fname
; use Fname
;
34 with Fname
.UF
; use Fname
.UF
;
35 with Freeze
; use Freeze
;
36 with Ghost
; use Ghost
;
37 with Itypes
; use Itypes
;
39 with Lib
.Load
; use Lib
.Load
;
40 with Lib
.Xref
; use Lib
.Xref
;
41 with Nlists
; use Nlists
;
42 with Namet
; use Namet
;
43 with Nmake
; use Nmake
;
45 with Rident
; use Rident
;
46 with Restrict
; use Restrict
;
47 with Rtsfind
; use Rtsfind
;
49 with Sem_Aux
; use Sem_Aux
;
50 with Sem_Cat
; use Sem_Cat
;
51 with Sem_Ch3
; use Sem_Ch3
;
52 with Sem_Ch6
; use Sem_Ch6
;
53 with Sem_Ch7
; use Sem_Ch7
;
54 with Sem_Ch8
; use Sem_Ch8
;
55 with Sem_Ch10
; use Sem_Ch10
;
56 with Sem_Ch13
; use Sem_Ch13
;
57 with Sem_Dim
; use Sem_Dim
;
58 with Sem_Disp
; use Sem_Disp
;
59 with Sem_Elab
; use Sem_Elab
;
60 with Sem_Elim
; use Sem_Elim
;
61 with Sem_Eval
; use Sem_Eval
;
62 with Sem_Prag
; use Sem_Prag
;
63 with Sem_Res
; use Sem_Res
;
64 with Sem_Type
; use Sem_Type
;
65 with Sem_Util
; use Sem_Util
;
66 with Sem_Warn
; use Sem_Warn
;
67 with Stand
; use Stand
;
68 with Sinfo
; use Sinfo
;
69 with Sinfo
.CN
; use Sinfo
.CN
;
70 with Sinput
; use Sinput
;
71 with Sinput
.L
; use Sinput
.L
;
72 with Snames
; use Snames
;
73 with Stringt
; use Stringt
;
74 with Uname
; use Uname
;
76 with Tbuild
; use Tbuild
;
77 with Uintp
; use Uintp
;
78 with Urealp
; use Urealp
;
79 with Warnsw
; use Warnsw
;
83 package body Sem_Ch12
is
85 ----------------------------------------------------------
86 -- Implementation of Generic Analysis and Instantiation --
87 ----------------------------------------------------------
89 -- GNAT implements generics by macro expansion. No attempt is made to share
90 -- generic instantiations (for now). Analysis of a generic definition does
91 -- not perform any expansion action, but the expander must be called on the
92 -- tree for each instantiation, because the expansion may of course depend
93 -- on the generic actuals. All of this is best achieved as follows:
95 -- a) Semantic analysis of a generic unit is performed on a copy of the
96 -- tree for the generic unit. All tree modifications that follow analysis
97 -- do not affect the original tree. Links are kept between the original
98 -- tree and the copy, in order to recognize non-local references within
99 -- the generic, and propagate them to each instance (recall that name
100 -- resolution is done on the generic declaration: generics are not really
101 -- macros). This is summarized in the following diagram:
103 -- .-----------. .----------.
104 -- | semantic |<--------------| generic |
106 -- | |==============>| |
107 -- |___________| global |__________|
118 -- b) Each instantiation copies the original tree, and inserts into it a
119 -- series of declarations that describe the mapping between generic formals
120 -- and actuals. For example, a generic In OUT parameter is an object
121 -- renaming of the corresponding actual, etc. Generic IN parameters are
122 -- constant declarations.
124 -- c) In order to give the right visibility for these renamings, we use
125 -- a different scheme for package and subprogram instantiations. For
126 -- packages, the list of renamings is inserted into the package
127 -- specification, before the visible declarations of the package. The
128 -- renamings are analyzed before any of the text of the instance, and are
129 -- thus visible at the right place. Furthermore, outside of the instance,
130 -- the generic parameters are visible and denote their corresponding
133 -- For subprograms, we create a container package to hold the renamings
134 -- and the subprogram instance itself. Analysis of the package makes the
135 -- renaming declarations visible to the subprogram. After analyzing the
136 -- package, the defining entity for the subprogram is touched-up so that
137 -- it appears declared in the current scope, and not inside the container
140 -- If the instantiation is a compilation unit, the container package is
141 -- given the same name as the subprogram instance. This ensures that
142 -- the elaboration procedure called by the binder, using the compilation
143 -- unit name, calls in fact the elaboration procedure for the package.
145 -- Not surprisingly, private types complicate this approach. By saving in
146 -- the original generic object the non-local references, we guarantee that
147 -- the proper entities are referenced at the point of instantiation.
148 -- However, for private types, this by itself does not insure that the
149 -- proper VIEW of the entity is used (the full type may be visible at the
150 -- point of generic definition, but not at instantiation, or vice-versa).
151 -- In order to reference the proper view, we special-case any reference
152 -- to private types in the generic object, by saving both views, one in
153 -- the generic and one in the semantic copy. At time of instantiation, we
154 -- check whether the two views are consistent, and exchange declarations if
155 -- necessary, in order to restore the correct visibility. Similarly, if
156 -- the instance view is private when the generic view was not, we perform
157 -- the exchange. After completing the instantiation, we restore the
158 -- current visibility. The flag Has_Private_View marks identifiers in the
159 -- the generic unit that require checking.
161 -- Visibility within nested generic units requires special handling.
162 -- Consider the following scheme:
164 -- type Global is ... -- outside of generic unit.
168 -- type Semi_Global is ... -- global to inner.
171 -- procedure inner (X1 : Global; X2 : Semi_Global);
173 -- procedure in2 is new inner (...); -- 4
176 -- package New_Outer is new Outer (...); -- 2
177 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
179 -- The semantic analysis of Outer captures all occurrences of Global.
180 -- The semantic analysis of Inner (at 1) captures both occurrences of
181 -- Global and Semi_Global.
183 -- At point 2 (instantiation of Outer), we also produce a generic copy
184 -- of Inner, even though Inner is, at that point, not being instantiated.
185 -- (This is just part of the semantic analysis of New_Outer).
187 -- Critically, references to Global within Inner must be preserved, while
188 -- references to Semi_Global should not preserved, because they must now
189 -- resolve to an entity within New_Outer. To distinguish between these, we
190 -- use a global variable, Current_Instantiated_Parent, which is set when
191 -- performing a generic copy during instantiation (at 2). This variable is
192 -- used when performing a generic copy that is not an instantiation, but
193 -- that is nested within one, as the occurrence of 1 within 2. The analysis
194 -- of a nested generic only preserves references that are global to the
195 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
196 -- determine whether a reference is external to the given parent.
198 -- The instantiation at point 3 requires no special treatment. The method
199 -- works as well for further nestings of generic units, but of course the
200 -- variable Current_Instantiated_Parent must be stacked because nested
201 -- instantiations can occur, e.g. the occurrence of 4 within 2.
203 -- The instantiation of package and subprogram bodies is handled in a
204 -- similar manner, except that it is delayed until after semantic
205 -- analysis is complete. In this fashion complex cross-dependencies
206 -- between several package declarations and bodies containing generics
207 -- can be compiled which otherwise would diagnose spurious circularities.
209 -- For example, it is possible to compile two packages A and B that
210 -- have the following structure:
212 -- package A is package B is
213 -- generic ... generic ...
214 -- package G_A is package G_B is
217 -- package body A is package body B is
218 -- package N_B is new G_B (..) package N_A is new G_A (..)
220 -- The table Pending_Instantiations in package Inline is used to keep
221 -- track of body instantiations that are delayed in this manner. Inline
222 -- handles the actual calls to do the body instantiations. This activity
223 -- is part of Inline, since the processing occurs at the same point, and
224 -- for essentially the same reason, as the handling of inlined routines.
226 ----------------------------------------------
227 -- Detection of Instantiation Circularities --
228 ----------------------------------------------
230 -- If we have a chain of instantiations that is circular, this is static
231 -- error which must be detected at compile time. The detection of these
232 -- circularities is carried out at the point that we insert a generic
233 -- instance spec or body. If there is a circularity, then the analysis of
234 -- the offending spec or body will eventually result in trying to load the
235 -- same unit again, and we detect this problem as we analyze the package
236 -- instantiation for the second time.
238 -- At least in some cases after we have detected the circularity, we get
239 -- into trouble if we try to keep going. The following flag is set if a
240 -- circularity is detected, and used to abandon compilation after the
241 -- messages have been posted.
243 -----------------------------------------
244 -- Implementation of Generic Contracts --
245 -----------------------------------------
247 -- A "contract" is a collection of aspects and pragmas that either verify a
248 -- property of a construct at runtime or classify the data flow to and from
249 -- the construct in some fashion.
251 -- Generic packages, subprograms and their respective bodies may be subject
252 -- to the following contract-related aspects or pragmas collectively known
255 -- package subprogram [body]
256 -- Abstract_State Contract_Cases
257 -- Initial_Condition Depends
258 -- Initializes Extensions_Visible
261 -- Refined_State Post_Class
271 -- Most package contract annotations utilize forward references to classify
272 -- data declared within the package [body]. Subprogram annotations then use
273 -- the classifications to further refine them. These inter dependencies are
274 -- problematic with respect to the implementation of generics because their
275 -- analysis, capture of global references and instantiation does not mesh
276 -- well with the existing mechanism.
278 -- 1) Analysis of generic contracts is carried out the same way non-generic
279 -- contracts are analyzed:
281 -- 1.1) General rule - a contract is analyzed after all related aspects
282 -- and pragmas are analyzed. This is done by routines
284 -- Analyze_Package_Body_Contract
285 -- Analyze_Package_Contract
286 -- Analyze_Subprogram_Body_Contract
287 -- Analyze_Subprogram_Contract
289 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
292 -- 1.3) Compilation unit body - the contract is analyzed at the end of
293 -- the body declaration list.
295 -- 1.4) Package - the contract is analyzed at the end of the private or
296 -- visible declarations, prior to analyzing the contracts of any nested
297 -- packages or subprograms.
299 -- 1.5) Package body - the contract is analyzed at the end of the body
300 -- declaration list, prior to analyzing the contracts of any nested
301 -- packages or subprograms.
303 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
304 -- package or a subprogram, then its contract is analyzed at the end of
305 -- the enclosing declarations, otherwise the subprogram is a compilation
308 -- 1.7) Subprogram body - if the subprogram body is declared inside a
309 -- block, a package body or a subprogram body, then its contract is
310 -- analyzed at the end of the enclosing declarations, otherwise the
311 -- subprogram is a compilation unit 1.3).
313 -- 2) Capture of global references within contracts is done after capturing
314 -- global references within the generic template. There are two reasons for
315 -- this delay - pragma annotations are not part of the generic template in
316 -- the case of a generic subprogram declaration, and analysis of contracts
319 -- Contract-related source pragmas within generic templates are prepared
320 -- for delayed capture of global references by routine
322 -- Create_Generic_Contract
324 -- The routine associates these pragmas with the contract of the template.
325 -- In the case of a generic subprogram declaration, the routine creates
326 -- generic templates for the pragmas declared after the subprogram because
327 -- they are not part of the template.
329 -- generic -- template starts
330 -- procedure Gen_Proc (Input : Integer); -- template ends
331 -- pragma Precondition (Input > 0); -- requires own template
333 -- 2.1) The capture of global references with aspect specifications and
334 -- source pragmas that apply to a generic unit must be suppressed when
335 -- the generic template is being processed because the contracts have not
336 -- been analyzed yet. Any attempts to capture global references at that
337 -- point will destroy the Associated_Node linkages and leave the template
338 -- undecorated. This delay is controlled by routine
340 -- Requires_Delayed_Save
342 -- 2.2) The real capture of global references within a contract is done
343 -- after the contract has been analyzed, by routine
345 -- Save_Global_References_In_Contract
347 -- 3) The instantiation of a generic contract occurs as part of the
348 -- instantiation of the contract owner. Generic subprogram declarations
349 -- require additional processing when the contract is specified by pragmas
350 -- because the pragmas are not part of the generic template. This is done
353 -- Instantiate_Subprogram_Contract
355 Circularity_Detected
: Boolean := False;
356 -- This should really be reset on encountering a new main unit, but in
357 -- practice we are not using multiple main units so it is not critical.
359 --------------------------------------------------
360 -- Formal packages and partial parameterization --
361 --------------------------------------------------
363 -- When compiling a generic, a formal package is a local instantiation. If
364 -- declared with a box, its generic formals are visible in the enclosing
365 -- generic. If declared with a partial list of actuals, those actuals that
366 -- are defaulted (covered by an Others clause, or given an explicit box
367 -- initialization) are also visible in the enclosing generic, while those
368 -- that have a corresponding actual are not.
370 -- In our source model of instantiation, the same visibility must be
371 -- present in the spec and body of an instance: the names of the formals
372 -- that are defaulted must be made visible within the instance, and made
373 -- invisible (hidden) after the instantiation is complete, so that they
374 -- are not accessible outside of the instance.
376 -- In a generic, a formal package is treated like a special instantiation.
377 -- Our Ada 95 compiler handled formals with and without box in different
378 -- ways. With partial parameterization, we use a single model for both.
379 -- We create a package declaration that consists of the specification of
380 -- the generic package, and a set of declarations that map the actuals
381 -- into local renamings, just as we do for bona fide instantiations. For
382 -- defaulted parameters and formals with a box, we copy directly the
383 -- declarations of the formal into this local package. The result is a
384 -- a package whose visible declarations may include generic formals. This
385 -- package is only used for type checking and visibility analysis, and
386 -- never reaches the back-end, so it can freely violate the placement
387 -- rules for generic formal declarations.
389 -- The list of declarations (renamings and copies of formals) is built
390 -- by Analyze_Associations, just as for regular instantiations.
392 -- At the point of instantiation, conformance checking must be applied only
393 -- to those parameters that were specified in the formal. We perform this
394 -- checking by creating another internal instantiation, this one including
395 -- only the renamings and the formals (the rest of the package spec is not
396 -- relevant to conformance checking). We can then traverse two lists: the
397 -- list of actuals in the instance that corresponds to the formal package,
398 -- and the list of actuals produced for this bogus instantiation. We apply
399 -- the conformance rules to those actuals that are not defaulted (i.e.
400 -- which still appear as generic formals.
402 -- When we compile an instance body we must make the right parameters
403 -- visible again. The predicate Is_Generic_Formal indicates which of the
404 -- formals should have its Is_Hidden flag reset.
406 -----------------------
407 -- Local subprograms --
408 -----------------------
410 procedure Abandon_Instantiation
(N
: Node_Id
);
411 pragma No_Return
(Abandon_Instantiation
);
412 -- Posts an error message "instantiation abandoned" at the indicated node
413 -- and then raises the exception Instantiation_Error to do it.
415 procedure Analyze_Formal_Array_Type
416 (T
: in out Entity_Id
;
418 -- A formal array type is treated like an array type declaration, and
419 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
420 -- in-out, because in the case of an anonymous type the entity is
421 -- actually created in the procedure.
423 -- The following procedures treat other kinds of formal parameters
425 procedure Analyze_Formal_Derived_Interface_Type
430 procedure Analyze_Formal_Derived_Type
435 procedure Analyze_Formal_Interface_Type
440 -- The following subprograms create abbreviated declarations for formal
441 -- scalar types. We introduce an anonymous base of the proper class for
442 -- each of them, and define the formals as constrained first subtypes of
443 -- their bases. The bounds are expressions that are non-static in the
446 procedure Analyze_Formal_Decimal_Fixed_Point_Type
447 (T
: Entity_Id
; Def
: Node_Id
);
448 procedure Analyze_Formal_Discrete_Type
(T
: Entity_Id
; Def
: Node_Id
);
449 procedure Analyze_Formal_Floating_Type
(T
: Entity_Id
; Def
: Node_Id
);
450 procedure Analyze_Formal_Signed_Integer_Type
(T
: Entity_Id
; Def
: Node_Id
);
451 procedure Analyze_Formal_Modular_Type
(T
: Entity_Id
; Def
: Node_Id
);
452 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
453 (T
: Entity_Id
; Def
: Node_Id
);
455 procedure Analyze_Formal_Private_Type
459 -- Creates a new private type, which does not require completion
461 procedure Analyze_Formal_Incomplete_Type
(T
: Entity_Id
; Def
: Node_Id
);
462 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
464 procedure Analyze_Generic_Formal_Part
(N
: Node_Id
);
465 -- Analyze generic formal part
467 procedure Analyze_Generic_Access_Type
(T
: Entity_Id
; Def
: Node_Id
);
468 -- Create a new access type with the given designated type
470 function Analyze_Associations
473 F_Copy
: List_Id
) return List_Id
;
474 -- At instantiation time, build the list of associations between formals
475 -- and actuals. Each association becomes a renaming declaration for the
476 -- formal entity. F_Copy is the analyzed list of formals in the generic
477 -- copy. It is used to apply legality checks to the actuals. I_Node is the
478 -- instantiation node itself.
480 procedure Analyze_Subprogram_Instantiation
484 procedure Build_Instance_Compilation_Unit_Nodes
488 -- This procedure is used in the case where the generic instance of a
489 -- subprogram body or package body is a library unit. In this case, the
490 -- original library unit node for the generic instantiation must be
491 -- replaced by the resulting generic body, and a link made to a new
492 -- compilation unit node for the generic declaration. The argument N is
493 -- the original generic instantiation. Act_Body and Act_Decl are the body
494 -- and declaration of the instance (either package body and declaration
495 -- nodes or subprogram body and declaration nodes depending on the case).
496 -- On return, the node N has been rewritten with the actual body.
498 procedure Check_Access_Definition
(N
: Node_Id
);
499 -- Subsidiary routine to null exclusion processing. Perform an assertion
500 -- check on Ada version and the presence of an access definition in N.
502 procedure Check_Formal_Packages
(P_Id
: Entity_Id
);
503 -- Apply the following to all formal packages in generic associations.
504 -- Restore the visibility of the formals of the instance that are not
505 -- defaulted (see RM 12.7 (10)). Remove the anonymous package declaration
506 -- created for formal instances that are not defaulted.
508 procedure Check_Formal_Package_Instance
509 (Formal_Pack
: Entity_Id
;
510 Actual_Pack
: Entity_Id
);
511 -- Verify that the actuals of the actual instance match the actuals of
512 -- the template for a formal package that is not declared with a box.
514 procedure Check_Forward_Instantiation
(Decl
: Node_Id
);
515 -- If the generic is a local entity and the corresponding body has not
516 -- been seen yet, flag enclosing packages to indicate that it will be
517 -- elaborated after the generic body. Subprograms declared in the same
518 -- package cannot be inlined by the front end because front-end inlining
519 -- requires a strict linear order of elaboration.
521 function Check_Hidden_Primitives
(Assoc_List
: List_Id
) return Elist_Id
;
522 -- Check if some association between formals and actuals requires to make
523 -- visible primitives of a tagged type, and make those primitives visible.
524 -- Return the list of primitives whose visibility is modified (to restore
525 -- their visibility later through Restore_Hidden_Primitives). If no
526 -- candidate is found then return No_Elist.
528 procedure Check_Hidden_Child_Unit
530 Gen_Unit
: Entity_Id
;
531 Act_Decl_Id
: Entity_Id
);
532 -- If the generic unit is an implicit child instance within a parent
533 -- instance, we need to make an explicit test that it is not hidden by
534 -- a child instance of the same name and parent.
536 procedure Check_Generic_Actuals
537 (Instance
: Entity_Id
;
538 Is_Formal_Box
: Boolean);
539 -- Similar to previous one. Check the actuals in the instantiation,
540 -- whose views can change between the point of instantiation and the point
541 -- of instantiation of the body. In addition, mark the generic renamings
542 -- as generic actuals, so that they are not compatible with other actuals.
543 -- Recurse on an actual that is a formal package whose declaration has
546 function Contains_Instance_Of
549 N
: Node_Id
) return Boolean;
550 -- Inner is instantiated within the generic Outer. Check whether Inner
551 -- directly or indirectly contains an instance of Outer or of one of its
552 -- parents, in the case of a subunit. Each generic unit holds a list of
553 -- the entities instantiated within (at any depth). This procedure
554 -- determines whether the set of such lists contains a cycle, i.e. an
555 -- illegal circular instantiation.
557 function Denotes_Formal_Package
559 On_Exit
: Boolean := False;
560 Instance
: Entity_Id
:= Empty
) return Boolean;
561 -- Returns True if E is a formal package of an enclosing generic, or
562 -- the actual for such a formal in an enclosing instantiation. If such
563 -- a package is used as a formal in an nested generic, or as an actual
564 -- in a nested instantiation, the visibility of ITS formals should not
565 -- be modified. When called from within Restore_Private_Views, the flag
566 -- On_Exit is true, to indicate that the search for a possible enclosing
567 -- instance should ignore the current one. In that case Instance denotes
568 -- the declaration for which this is an actual. This declaration may be
569 -- an instantiation in the source, or the internal instantiation that
570 -- corresponds to the actual for a formal package.
572 function Earlier
(N1
, N2
: Node_Id
) return Boolean;
573 -- Yields True if N1 and N2 appear in the same compilation unit,
574 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
575 -- traversal of the tree for the unit. Used to determine the placement
576 -- of freeze nodes for instance bodies that may depend on other instances.
578 function Find_Actual_Type
580 Gen_Type
: Entity_Id
) return Entity_Id
;
581 -- When validating the actual types of a child instance, check whether
582 -- the formal is a formal type of the parent unit, and retrieve the current
583 -- actual for it. Typ is the entity in the analyzed formal type declaration
584 -- (component or index type of an array type, or designated type of an
585 -- access formal) and Gen_Type is the enclosing analyzed formal array
586 -- or access type. The desired actual may be a formal of a parent, or may
587 -- be declared in a formal package of a parent. In both cases it is a
588 -- generic actual type because it appears within a visible instance.
589 -- Finally, it may be declared in a parent unit without being a formal
590 -- of that unit, in which case it must be retrieved by visibility.
591 -- Ambiguities may still arise if two homonyms are declared in two formal
592 -- packages, and the prefix of the formal type may be needed to resolve
593 -- the ambiguity in the instance ???
595 procedure Freeze_Subprogram_Body
596 (Inst_Node
: Node_Id
;
598 Pack_Id
: Entity_Id
);
599 -- The generic body may appear textually after the instance, including
600 -- in the proper body of a stub, or within a different package instance.
601 -- Given that the instance can only be elaborated after the generic, we
602 -- place freeze_nodes for the instance and/or for packages that may enclose
603 -- the instance and the generic, so that the back-end can establish the
604 -- proper order of elaboration.
606 function Get_Associated_Node
(N
: Node_Id
) return Node_Id
;
607 -- In order to propagate semantic information back from the analyzed copy
608 -- to the original generic, we maintain links between selected nodes in the
609 -- generic and their corresponding copies. At the end of generic analysis,
610 -- the routine Save_Global_References traverses the generic tree, examines
611 -- the semantic information, and preserves the links to those nodes that
612 -- contain global information. At instantiation, the information from the
613 -- associated node is placed on the new copy, so that name resolution is
616 -- Three kinds of source nodes have associated nodes:
618 -- a) those that can reference (denote) entities, that is identifiers,
619 -- character literals, expanded_names, operator symbols, operators,
620 -- and attribute reference nodes. These nodes have an Entity field
621 -- and are the set of nodes that are in N_Has_Entity.
623 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
625 -- c) selected components (N_Selected_Component)
627 -- For the first class, the associated node preserves the entity if it is
628 -- global. If the generic contains nested instantiations, the associated
629 -- node itself has been recopied, and a chain of them must be followed.
631 -- For aggregates, the associated node allows retrieval of the type, which
632 -- may otherwise not appear in the generic. The view of this type may be
633 -- different between generic and instantiation, and the full view can be
634 -- installed before the instantiation is analyzed. For aggregates of type
635 -- extensions, the same view exchange may have to be performed for some of
636 -- the ancestor types, if their view is private at the point of
639 -- Nodes that are selected components in the parse tree may be rewritten
640 -- as expanded names after resolution, and must be treated as potential
641 -- entity holders, which is why they also have an Associated_Node.
643 -- Nodes that do not come from source, such as freeze nodes, do not appear
644 -- in the generic tree, and need not have an associated node.
646 -- The associated node is stored in the Associated_Node field. Note that
647 -- this field overlaps Entity, which is fine, because the whole point is
648 -- that we don't need or want the normal Entity field in this situation.
650 function Has_Been_Exchanged
(E
: Entity_Id
) return Boolean;
651 -- Traverse the Exchanged_Views list to see if a type was private
652 -- and has already been flipped during this phase of instantiation.
654 procedure Hide_Current_Scope
;
655 -- When instantiating a generic child unit, the parent context must be
656 -- present, but the instance and all entities that may be generated
657 -- must be inserted in the current scope. We leave the current scope
658 -- on the stack, but make its entities invisible to avoid visibility
659 -- problems. This is reversed at the end of the instantiation. This is
660 -- not done for the instantiation of the bodies, which only require the
661 -- instances of the generic parents to be in scope.
663 function In_Main_Context
(E
: Entity_Id
) return Boolean;
664 -- Check whether an instantiation is in the context of the main unit.
665 -- Used to determine whether its body should be elaborated to allow
666 -- front-end inlining.
668 procedure Inherit_Context
(Gen_Decl
: Node_Id
; Inst
: Node_Id
);
669 -- Add the context clause of the unit containing a generic unit to a
670 -- compilation unit that is, or contains, an instantiation.
673 -- Establish environment for subsequent instantiation. Separated from
674 -- Save_Env because data-structures for visibility handling must be
675 -- initialized before call to Check_Generic_Child_Unit.
677 procedure Inline_Instance_Body
679 Gen_Unit
: Entity_Id
;
681 -- If front-end inlining is requested, instantiate the package body,
682 -- and preserve the visibility of its compilation unit, to insure
683 -- that successive instantiations succeed.
685 procedure Insert_Freeze_Node_For_Instance
688 -- N denotes a package or a subprogram instantiation and F_Node is the
689 -- associated freeze node. Insert the freeze node before the first source
690 -- body which follows immediately after N. If no such body is found, the
691 -- freeze node is inserted at the end of the declarative region which
694 procedure Install_Body
699 -- If the instantiation happens textually before the body of the generic,
700 -- the instantiation of the body must be analyzed after the generic body,
701 -- and not at the point of instantiation. Such early instantiations can
702 -- happen if the generic and the instance appear in a package declaration
703 -- because the generic body can only appear in the corresponding package
704 -- body. Early instantiations can also appear if generic, instance and
705 -- body are all in the declarative part of a subprogram or entry. Entities
706 -- of packages that are early instantiations are delayed, and their freeze
707 -- node appears after the generic body. This rather complex machinery is
708 -- needed when nested instantiations are present, because the source does
709 -- not carry any indication of where the corresponding instance bodies must
710 -- be installed and frozen.
712 procedure Install_Formal_Packages
(Par
: Entity_Id
);
713 -- Install the visible part of any formal of the parent that is a formal
714 -- package. Note that for the case of a formal package with a box, this
715 -- includes the formal part of the formal package (12.7(10/2)).
717 procedure Install_Hidden_Primitives
718 (Prims_List
: in out Elist_Id
;
721 -- Remove suffix 'P' from hidden primitives of Act_T to match the
722 -- visibility of primitives of Gen_T. The list of primitives to which
723 -- the suffix is removed is added to Prims_List to restore them later.
725 procedure Install_Parent
(P
: Entity_Id
; In_Body
: Boolean := False);
726 -- When compiling an instance of a child unit the parent (which is
727 -- itself an instance) is an enclosing scope that must be made
728 -- immediately visible. This procedure is also used to install the non-
729 -- generic parent of a generic child unit when compiling its body, so
730 -- that full views of types in the parent are made visible.
732 -- The functions Instantiate_XXX perform various legality checks and build
733 -- the declarations for instantiated generic parameters. In all of these
734 -- Formal is the entity in the generic unit, Actual is the entity of
735 -- expression in the generic associations, and Analyzed_Formal is the
736 -- formal in the generic copy, which contains the semantic information to
737 -- be used to validate the actual.
739 function Instantiate_Object
742 Analyzed_Formal
: Node_Id
) return List_Id
;
744 function Instantiate_Type
747 Analyzed_Formal
: Node_Id
;
748 Actual_Decls
: List_Id
) return List_Id
;
750 function Instantiate_Formal_Subprogram
753 Analyzed_Formal
: Node_Id
) return Node_Id
;
755 function Instantiate_Formal_Package
758 Analyzed_Formal
: Node_Id
) return List_Id
;
759 -- If the formal package is declared with a box, special visibility rules
760 -- apply to its formals: they are in the visible part of the package. This
761 -- is true in the declarative region of the formal package, that is to say
762 -- in the enclosing generic or instantiation. For an instantiation, the
763 -- parameters of the formal package are made visible in an explicit step.
764 -- Furthermore, if the actual has a visible USE clause, these formals must
765 -- be made potentially use-visible as well. On exit from the enclosing
766 -- instantiation, the reverse must be done.
768 -- For a formal package declared without a box, there are conformance rules
769 -- that apply to the actuals in the generic declaration and the actuals of
770 -- the actual package in the enclosing instantiation. The simplest way to
771 -- apply these rules is to repeat the instantiation of the formal package
772 -- in the context of the enclosing instance, and compare the generic
773 -- associations of this instantiation with those of the actual package.
774 -- This internal instantiation only needs to contain the renamings of the
775 -- formals: the visible and private declarations themselves need not be
778 -- In Ada 2005, the formal package may be only partially parameterized.
779 -- In that case the visibility step must make visible those actuals whose
780 -- corresponding formals were given with a box. A final complication
781 -- involves inherited operations from formal derived types, which must
782 -- be visible if the type is.
784 function Is_In_Main_Unit
(N
: Node_Id
) return Boolean;
785 -- Test if given node is in the main unit
787 procedure Load_Parent_Of_Generic
790 Body_Optional
: Boolean := False);
791 -- If the generic appears in a separate non-generic library unit, load the
792 -- corresponding body to retrieve the body of the generic. N is the node
793 -- for the generic instantiation, Spec is the generic package declaration.
795 -- Body_Optional is a flag that indicates that the body is being loaded to
796 -- ensure that temporaries are generated consistently when there are other
797 -- instances in the current declarative part that precede the one being
798 -- loaded. In that case a missing body is acceptable.
800 procedure Map_Formal_Package_Entities
(Form
: Entity_Id
; Act
: Entity_Id
);
801 -- Within the generic part, entities in the formal package are
802 -- visible. To validate subsequent type declarations, indicate
803 -- the correspondence between the entities in the analyzed formal,
804 -- and the entities in the actual package. There are three packages
805 -- involved in the instantiation of a formal package: the parent
806 -- generic P1 which appears in the generic declaration, the fake
807 -- instantiation P2 which appears in the analyzed generic, and whose
808 -- visible entities may be used in subsequent formals, and the actual
809 -- P3 in the instance. To validate subsequent formals, me indicate
810 -- that the entities in P2 are mapped into those of P3. The mapping of
811 -- entities has to be done recursively for nested packages.
813 procedure Move_Freeze_Nodes
817 -- Freeze nodes can be generated in the analysis of a generic unit, but
818 -- will not be seen by the back-end. It is necessary to move those nodes
819 -- to the enclosing scope if they freeze an outer entity. We place them
820 -- at the end of the enclosing generic package, which is semantically
823 procedure Preanalyze_Actuals
(N
: Node_Id
; Inst
: Entity_Id
:= Empty
);
824 -- Analyze actuals to perform name resolution. Full resolution is done
825 -- later, when the expected types are known, but names have to be captured
826 -- before installing parents of generics, that are not visible for the
827 -- actuals themselves.
829 -- If Inst is present, it is the entity of the package instance. This
830 -- entity is marked as having a limited_view actual when some actual is
831 -- a limited view. This is used to place the instance body properly.
833 procedure Provide_Completing_Bodies
(N
: Node_Id
);
834 -- Generate completing bodies for all subprograms found within package or
835 -- subprogram declaration N.
837 procedure Remove_Parent
(In_Body
: Boolean := False);
838 -- Reverse effect after instantiation of child is complete
840 procedure Restore_Hidden_Primitives
(Prims_List
: in out Elist_Id
);
841 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
844 procedure Set_Instance_Env
845 (Gen_Unit
: Entity_Id
;
846 Act_Unit
: Entity_Id
);
847 -- Save current instance on saved environment, to be used to determine
848 -- the global status of entities in nested instances. Part of Save_Env.
849 -- called after verifying that the generic unit is legal for the instance,
850 -- The procedure also examines whether the generic unit is a predefined
851 -- unit, in order to set configuration switches accordingly. As a result
852 -- the procedure must be called after analyzing and freezing the actuals.
854 procedure Set_Instance_Of
(A
: Entity_Id
; B
: Entity_Id
);
855 -- Associate analyzed generic parameter with corresponding instance. Used
856 -- for semantic checks at instantiation time.
858 function True_Parent
(N
: Node_Id
) return Node_Id
;
859 -- For a subunit, return parent of corresponding stub, else return
862 procedure Valid_Default_Attribute
(Nam
: Entity_Id
; Def
: Node_Id
);
863 -- Verify that an attribute that appears as the default for a formal
864 -- subprogram is a function or procedure with the correct profile.
866 -------------------------------------------
867 -- Data Structures for Generic Renamings --
868 -------------------------------------------
870 -- The map Generic_Renamings associates generic entities with their
871 -- corresponding actuals. Currently used to validate type instances. It
872 -- will eventually be used for all generic parameters to eliminate the
873 -- need for overload resolution in the instance.
875 type Assoc_Ptr
is new Int
;
877 Assoc_Null
: constant Assoc_Ptr
:= -1;
882 Next_In_HTable
: Assoc_Ptr
;
885 package Generic_Renamings
is new Table
.Table
886 (Table_Component_Type
=> Assoc
,
887 Table_Index_Type
=> Assoc_Ptr
,
888 Table_Low_Bound
=> 0,
890 Table_Increment
=> 100,
891 Table_Name
=> "Generic_Renamings");
893 -- Variable to hold enclosing instantiation. When the environment is
894 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
896 Current_Instantiated_Parent
: Assoc
:= (Empty
, Empty
, Assoc_Null
);
898 -- Hash table for associations
900 HTable_Size
: constant := 37;
901 type HTable_Range
is range 0 .. HTable_Size
- 1;
903 procedure Set_Next_Assoc
(E
: Assoc_Ptr
; Next
: Assoc_Ptr
);
904 function Next_Assoc
(E
: Assoc_Ptr
) return Assoc_Ptr
;
905 function Get_Gen_Id
(E
: Assoc_Ptr
) return Entity_Id
;
906 function Hash
(F
: Entity_Id
) return HTable_Range
;
908 package Generic_Renamings_HTable
is new GNAT
.HTable
.Static_HTable
(
909 Header_Num
=> HTable_Range
,
911 Elmt_Ptr
=> Assoc_Ptr
,
912 Null_Ptr
=> Assoc_Null
,
913 Set_Next
=> Set_Next_Assoc
,
916 Get_Key
=> Get_Gen_Id
,
920 Exchanged_Views
: Elist_Id
;
921 -- This list holds the private views that have been exchanged during
922 -- instantiation to restore the visibility of the generic declaration.
923 -- (see comments above). After instantiation, the current visibility is
924 -- reestablished by means of a traversal of this list.
926 Hidden_Entities
: Elist_Id
;
927 -- This list holds the entities of the current scope that are removed
928 -- from immediate visibility when instantiating a child unit. Their
929 -- visibility is restored in Remove_Parent.
931 -- Because instantiations can be recursive, the following must be saved
932 -- on entry and restored on exit from an instantiation (spec or body).
933 -- This is done by the two procedures Save_Env and Restore_Env. For
934 -- package and subprogram instantiations (but not for the body instances)
935 -- the action of Save_Env is done in two steps: Init_Env is called before
936 -- Check_Generic_Child_Unit, because setting the parent instances requires
937 -- that the visibility data structures be properly initialized. Once the
938 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
940 Parent_Unit_Visible
: Boolean := False;
941 -- Parent_Unit_Visible is used when the generic is a child unit, and
942 -- indicates whether the ultimate parent of the generic is visible in the
943 -- instantiation environment. It is used to reset the visibility of the
944 -- parent at the end of the instantiation (see Remove_Parent).
946 Instance_Parent_Unit
: Entity_Id
:= Empty
;
947 -- This records the ultimate parent unit of an instance of a generic
948 -- child unit and is used in conjunction with Parent_Unit_Visible to
949 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
951 type Instance_Env
is record
952 Instantiated_Parent
: Assoc
;
953 Exchanged_Views
: Elist_Id
;
954 Hidden_Entities
: Elist_Id
;
955 Current_Sem_Unit
: Unit_Number_Type
;
956 Parent_Unit_Visible
: Boolean := False;
957 Instance_Parent_Unit
: Entity_Id
:= Empty
;
958 Switches
: Config_Switches_Type
;
961 package Instance_Envs
is new Table
.Table
(
962 Table_Component_Type
=> Instance_Env
,
963 Table_Index_Type
=> Int
,
964 Table_Low_Bound
=> 0,
966 Table_Increment
=> 100,
967 Table_Name
=> "Instance_Envs");
969 procedure Restore_Private_Views
970 (Pack_Id
: Entity_Id
;
971 Is_Package
: Boolean := True);
972 -- Restore the private views of external types, and unmark the generic
973 -- renamings of actuals, so that they become compatible subtypes again.
974 -- For subprograms, Pack_Id is the package constructed to hold the
977 procedure Switch_View
(T
: Entity_Id
);
978 -- Switch the partial and full views of a type and its private
979 -- dependents (i.e. its subtypes and derived types).
981 ------------------------------------
982 -- Structures for Error Reporting --
983 ------------------------------------
985 Instantiation_Node
: Node_Id
;
986 -- Used by subprograms that validate instantiation of formal parameters
987 -- where there might be no actual on which to place the error message.
988 -- Also used to locate the instantiation node for generic subunits.
990 Instantiation_Error
: exception;
991 -- When there is a semantic error in the generic parameter matching,
992 -- there is no point in continuing the instantiation, because the
993 -- number of cascaded errors is unpredictable. This exception aborts
994 -- the instantiation process altogether.
996 S_Adjustment
: Sloc_Adjustment
;
997 -- Offset created for each node in an instantiation, in order to keep
998 -- track of the source position of the instantiation in each of its nodes.
999 -- A subsequent semantic error or warning on a construct of the instance
1000 -- points to both places: the original generic node, and the point of
1001 -- instantiation. See Sinput and Sinput.L for additional details.
1003 ------------------------------------------------------------
1004 -- Data structure for keeping track when inside a Generic --
1005 ------------------------------------------------------------
1007 -- The following table is used to save values of the Inside_A_Generic
1008 -- flag (see spec of Sem) when they are saved by Start_Generic.
1010 package Generic_Flags
is new Table
.Table
(
1011 Table_Component_Type
=> Boolean,
1012 Table_Index_Type
=> Int
,
1013 Table_Low_Bound
=> 0,
1014 Table_Initial
=> 32,
1015 Table_Increment
=> 200,
1016 Table_Name
=> "Generic_Flags");
1018 ---------------------------
1019 -- Abandon_Instantiation --
1020 ---------------------------
1022 procedure Abandon_Instantiation
(N
: Node_Id
) is
1024 Error_Msg_N
("\instantiation abandoned!", N
);
1025 raise Instantiation_Error
;
1026 end Abandon_Instantiation
;
1028 --------------------------------
1029 -- Add_Pending_Instantiation --
1030 --------------------------------
1032 procedure Add_Pending_Instantiation
(Inst
: Node_Id
; Act_Decl
: Node_Id
) is
1034 -- Capture the body of the generic instantiation along with its context
1035 -- for later processing by Instantiate_Bodies.
1037 Pending_Instantiations
.Append
1038 ((Act_Decl
=> Act_Decl
,
1039 Config_Switches
=> Save_Config_Switches
,
1040 Current_Sem_Unit
=> Current_Sem_Unit
,
1041 Expander_Status
=> Expander_Active
,
1043 Local_Suppress_Stack_Top
=> Local_Suppress_Stack_Top
,
1044 Scope_Suppress
=> Scope_Suppress
,
1045 Warnings
=> Save_Warnings
));
1046 end Add_Pending_Instantiation
;
1048 ----------------------------------
1049 -- Adjust_Inherited_Pragma_Sloc --
1050 ----------------------------------
1052 procedure Adjust_Inherited_Pragma_Sloc
(N
: Node_Id
) is
1054 Adjust_Instantiation_Sloc
(N
, S_Adjustment
);
1055 end Adjust_Inherited_Pragma_Sloc
;
1057 --------------------------
1058 -- Analyze_Associations --
1059 --------------------------
1061 function Analyze_Associations
1064 F_Copy
: List_Id
) return List_Id
1066 Actuals_To_Freeze
: constant Elist_Id
:= New_Elmt_List
;
1067 Assoc_List
: constant List_Id
:= New_List
;
1068 Default_Actuals
: constant List_Id
:= New_List
;
1069 Gen_Unit
: constant Entity_Id
:=
1070 Defining_Entity
(Parent
(F_Copy
));
1074 Analyzed_Formal
: Node_Id
;
1075 First_Named
: Node_Id
:= Empty
;
1079 Saved_Formal
: Node_Id
;
1081 Default_Formals
: constant List_Id
:= New_List
;
1082 -- If an Others_Choice is present, some of the formals may be defaulted.
1083 -- To simplify the treatment of visibility in an instance, we introduce
1084 -- individual defaults for each such formal. These defaults are
1085 -- appended to the list of associations and replace the Others_Choice.
1087 Found_Assoc
: Node_Id
;
1088 -- Association for the current formal being match. Empty if there are
1089 -- no remaining actuals, or if there is no named association with the
1090 -- name of the formal.
1092 Is_Named_Assoc
: Boolean;
1093 Num_Matched
: Nat
:= 0;
1094 Num_Actuals
: Nat
:= 0;
1096 Others_Present
: Boolean := False;
1097 Others_Choice
: Node_Id
:= Empty
;
1098 -- In Ada 2005, indicates partial parameterization of a formal
1099 -- package. As usual an other association must be last in the list.
1101 procedure Check_Fixed_Point_Actual
(Actual
: Node_Id
);
1102 -- Warn if an actual fixed-point type has user-defined arithmetic
1103 -- operations, but there is no corresponding formal in the generic,
1104 -- in which case the predefined operations will be used. This merits
1105 -- a warning because of the special semantics of fixed point ops.
1107 procedure Check_Overloaded_Formal_Subprogram
(Formal
: Entity_Id
);
1108 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1109 -- cannot have a named association for it. AI05-0025 extends this rule
1110 -- to formals of formal packages by AI05-0025, and it also applies to
1111 -- box-initialized formals.
1113 function Has_Fully_Defined_Profile
(Subp
: Entity_Id
) return Boolean;
1114 -- Determine whether the parameter types and the return type of Subp
1115 -- are fully defined at the point of instantiation.
1117 function Matching_Actual
1119 A_F
: Entity_Id
) return Node_Id
;
1120 -- Find actual that corresponds to a given a formal parameter. If the
1121 -- actuals are positional, return the next one, if any. If the actuals
1122 -- are named, scan the parameter associations to find the right one.
1123 -- A_F is the corresponding entity in the analyzed generic, which is
1124 -- placed on the selector name for ASIS use.
1126 -- In Ada 2005, a named association may be given with a box, in which
1127 -- case Matching_Actual sets Found_Assoc to the generic association,
1128 -- but return Empty for the actual itself. In this case the code below
1129 -- creates a corresponding declaration for the formal.
1131 function Partial_Parameterization
return Boolean;
1132 -- Ada 2005: if no match is found for a given formal, check if the
1133 -- association for it includes a box, or whether the associations
1134 -- include an Others clause.
1136 procedure Process_Default
(F
: Entity_Id
);
1137 -- Add a copy of the declaration of generic formal F to the list of
1138 -- associations, and add an explicit box association for F if there
1139 -- is none yet, and the default comes from an Others_Choice.
1141 function Renames_Standard_Subprogram
(Subp
: Entity_Id
) return Boolean;
1142 -- Determine whether Subp renames one of the subprograms defined in the
1143 -- generated package Standard.
1145 procedure Set_Analyzed_Formal
;
1146 -- Find the node in the generic copy that corresponds to a given formal.
1147 -- The semantic information on this node is used to perform legality
1148 -- checks on the actuals. Because semantic analysis can introduce some
1149 -- anonymous entities or modify the declaration node itself, the
1150 -- correspondence between the two lists is not one-one. In addition to
1151 -- anonymous types, the presence a formal equality will introduce an
1152 -- implicit declaration for the corresponding inequality.
1154 ----------------------------------------
1155 -- Check_Overloaded_Formal_Subprogram --
1156 ----------------------------------------
1158 procedure Check_Overloaded_Formal_Subprogram
(Formal
: Entity_Id
) is
1159 Temp_Formal
: Entity_Id
;
1162 Temp_Formal
:= First
(Formals
);
1163 while Present
(Temp_Formal
) loop
1164 if Nkind
(Temp_Formal
) in N_Formal_Subprogram_Declaration
1165 and then Temp_Formal
/= Formal
1167 Chars
(Defining_Unit_Name
(Specification
(Formal
))) =
1168 Chars
(Defining_Unit_Name
(Specification
(Temp_Formal
)))
1170 if Present
(Found_Assoc
) then
1172 ("named association not allowed for overloaded formal",
1177 ("named association not allowed for overloaded formal",
1181 Abandon_Instantiation
(Instantiation_Node
);
1186 end Check_Overloaded_Formal_Subprogram
;
1188 -------------------------------
1189 -- Check_Fixed_Point_Actual --
1190 -------------------------------
1192 procedure Check_Fixed_Point_Actual
(Actual
: Node_Id
) is
1193 Typ
: constant Entity_Id
:= Entity
(Actual
);
1194 Prims
: constant Elist_Id
:= Collect_Primitive_Operations
(Typ
);
1200 -- Locate primitive operations of the type that are arithmetic
1203 Elem
:= First_Elmt
(Prims
);
1204 while Present
(Elem
) loop
1205 if Nkind
(Node
(Elem
)) = N_Defining_Operator_Symbol
then
1207 -- Check whether the generic unit has a formal subprogram of
1208 -- the same name. This does not check types but is good enough
1209 -- to justify a warning.
1211 Formal
:= First_Non_Pragma
(Formals
);
1212 Op
:= Alias
(Node
(Elem
));
1214 while Present
(Formal
) loop
1215 if Nkind
(Formal
) = N_Formal_Concrete_Subprogram_Declaration
1216 and then Chars
(Defining_Entity
(Formal
)) =
1221 elsif Nkind
(Formal
) = N_Formal_Package_Declaration
then
1227 -- Locate corresponding actual, and check whether it
1228 -- includes a fixed-point type.
1230 Assoc
:= First
(Assoc_List
);
1231 while Present
(Assoc
) loop
1233 Nkind
(Assoc
) = N_Package_Renaming_Declaration
1234 and then Chars
(Defining_Unit_Name
(Assoc
)) =
1235 Chars
(Defining_Identifier
(Formal
));
1240 if Present
(Assoc
) then
1242 -- If formal package declares a fixed-point type,
1243 -- and the user-defined operator is derived from
1244 -- a generic instance package, the fixed-point type
1245 -- does not use the corresponding predefined op.
1247 Ent
:= First_Entity
(Entity
(Name
(Assoc
)));
1248 while Present
(Ent
) loop
1249 if Is_Fixed_Point_Type
(Ent
)
1250 and then Present
(Op
)
1251 and then Is_Generic_Instance
(Scope
(Op
))
1266 Error_Msg_Sloc
:= Sloc
(Node
(Elem
));
1268 ("?instance uses predefined operation, not primitive "
1269 & "operation&#", Actual
, Node
(Elem
));
1275 end Check_Fixed_Point_Actual
;
1277 -------------------------------
1278 -- Has_Fully_Defined_Profile --
1279 -------------------------------
1281 function Has_Fully_Defined_Profile
(Subp
: Entity_Id
) return Boolean is
1282 function Is_Fully_Defined_Type
(Typ
: Entity_Id
) return Boolean;
1283 -- Determine whethet type Typ is fully defined
1285 ---------------------------
1286 -- Is_Fully_Defined_Type --
1287 ---------------------------
1289 function Is_Fully_Defined_Type
(Typ
: Entity_Id
) return Boolean is
1291 -- A private type without a full view is not fully defined
1293 if Is_Private_Type
(Typ
)
1294 and then No
(Full_View
(Typ
))
1298 -- An incomplete type is never fully defined
1300 elsif Is_Incomplete_Type
(Typ
) then
1303 -- All other types are fully defined
1308 end Is_Fully_Defined_Type
;
1310 -- Local declarations
1314 -- Start of processing for Has_Fully_Defined_Profile
1317 -- Check the parameters
1319 Param
:= First_Formal
(Subp
);
1320 while Present
(Param
) loop
1321 if not Is_Fully_Defined_Type
(Etype
(Param
)) then
1325 Next_Formal
(Param
);
1328 -- Check the return type
1330 return Is_Fully_Defined_Type
(Etype
(Subp
));
1331 end Has_Fully_Defined_Profile
;
1333 ---------------------
1334 -- Matching_Actual --
1335 ---------------------
1337 function Matching_Actual
1339 A_F
: Entity_Id
) return Node_Id
1345 Is_Named_Assoc
:= False;
1347 -- End of list of purely positional parameters
1349 if No
(Actual
) or else Nkind
(Actual
) = N_Others_Choice
then
1350 Found_Assoc
:= Empty
;
1353 -- Case of positional parameter corresponding to current formal
1355 elsif No
(Selector_Name
(Actual
)) then
1356 Found_Assoc
:= Actual
;
1357 Act
:= Explicit_Generic_Actual_Parameter
(Actual
);
1358 Num_Matched
:= Num_Matched
+ 1;
1361 -- Otherwise scan list of named actuals to find the one with the
1362 -- desired name. All remaining actuals have explicit names.
1365 Is_Named_Assoc
:= True;
1366 Found_Assoc
:= Empty
;
1370 while Present
(Actual
) loop
1371 if Nkind
(Actual
) = N_Others_Choice
then
1372 Found_Assoc
:= Empty
;
1375 elsif Chars
(Selector_Name
(Actual
)) = Chars
(F
) then
1376 Set_Entity
(Selector_Name
(Actual
), A_F
);
1377 Set_Etype
(Selector_Name
(Actual
), Etype
(A_F
));
1378 Generate_Reference
(A_F
, Selector_Name
(Actual
));
1380 Found_Assoc
:= Actual
;
1381 Act
:= Explicit_Generic_Actual_Parameter
(Actual
);
1382 Num_Matched
:= Num_Matched
+ 1;
1390 -- Reset for subsequent searches. In most cases the named
1391 -- associations are in order. If they are not, we reorder them
1392 -- to avoid scanning twice the same actual. This is not just a
1393 -- question of efficiency: there may be multiple defaults with
1394 -- boxes that have the same name. In a nested instantiation we
1395 -- insert actuals for those defaults, and cannot rely on their
1396 -- names to disambiguate them.
1398 if Actual
= First_Named
then
1401 elsif Present
(Actual
) then
1402 Insert_Before
(First_Named
, Remove_Next
(Prev
));
1405 Actual
:= First_Named
;
1408 if Is_Entity_Name
(Act
) and then Present
(Entity
(Act
)) then
1409 Set_Used_As_Generic_Actual
(Entity
(Act
));
1413 end Matching_Actual
;
1415 ------------------------------
1416 -- Partial_Parameterization --
1417 ------------------------------
1419 function Partial_Parameterization
return Boolean is
1421 return Others_Present
1422 or else (Present
(Found_Assoc
) and then Box_Present
(Found_Assoc
));
1423 end Partial_Parameterization
;
1425 ---------------------
1426 -- Process_Default --
1427 ---------------------
1429 procedure Process_Default
(F
: Entity_Id
) is
1430 Loc
: constant Source_Ptr
:= Sloc
(I_Node
);
1431 F_Id
: constant Entity_Id
:= Defining_Entity
(F
);
1437 -- Append copy of formal declaration to associations, and create new
1438 -- defining identifier for it.
1440 Decl
:= New_Copy_Tree
(F
);
1441 Id
:= Make_Defining_Identifier
(Sloc
(F_Id
), Chars
(F_Id
));
1443 if Nkind
(F
) in N_Formal_Subprogram_Declaration
then
1444 Set_Defining_Unit_Name
(Specification
(Decl
), Id
);
1447 Set_Defining_Identifier
(Decl
, Id
);
1450 Append
(Decl
, Assoc_List
);
1452 if No
(Found_Assoc
) then
1454 Make_Generic_Association
(Loc
,
1456 New_Occurrence_Of
(Id
, Loc
),
1457 Explicit_Generic_Actual_Parameter
=> Empty
);
1458 Set_Box_Present
(Default
);
1459 Append
(Default
, Default_Formals
);
1461 end Process_Default
;
1463 ---------------------------------
1464 -- Renames_Standard_Subprogram --
1465 ---------------------------------
1467 function Renames_Standard_Subprogram
(Subp
: Entity_Id
) return Boolean is
1472 while Present
(Id
) loop
1473 if Scope
(Id
) = Standard_Standard
then
1481 end Renames_Standard_Subprogram
;
1483 -------------------------
1484 -- Set_Analyzed_Formal --
1485 -------------------------
1487 procedure Set_Analyzed_Formal
is
1491 while Present
(Analyzed_Formal
) loop
1492 Kind
:= Nkind
(Analyzed_Formal
);
1494 case Nkind
(Formal
) is
1495 when N_Formal_Subprogram_Declaration
=>
1496 exit when Kind
in N_Formal_Subprogram_Declaration
1499 (Defining_Unit_Name
(Specification
(Formal
))) =
1501 (Defining_Unit_Name
(Specification
(Analyzed_Formal
)));
1503 when N_Formal_Package_Declaration
=>
1504 exit when Nkind_In
(Kind
, N_Formal_Package_Declaration
,
1505 N_Generic_Package_Declaration
,
1506 N_Package_Declaration
);
1508 when N_Use_Package_Clause
1515 -- Skip freeze nodes, and nodes inserted to replace
1516 -- unrecognized pragmas.
1519 Kind
not in N_Formal_Subprogram_Declaration
1520 and then not Nkind_In
(Kind
, N_Subprogram_Declaration
,
1524 and then Chars
(Defining_Identifier
(Formal
)) =
1525 Chars
(Defining_Identifier
(Analyzed_Formal
));
1528 Next
(Analyzed_Formal
);
1530 end Set_Analyzed_Formal
;
1532 -- Start of processing for Analyze_Associations
1535 Actuals
:= Generic_Associations
(I_Node
);
1537 if Present
(Actuals
) then
1539 -- Check for an Others choice, indicating a partial parameterization
1540 -- for a formal package.
1542 Actual
:= First
(Actuals
);
1543 while Present
(Actual
) loop
1544 if Nkind
(Actual
) = N_Others_Choice
then
1545 Others_Present
:= True;
1546 Others_Choice
:= Actual
;
1548 if Present
(Next
(Actual
)) then
1549 Error_Msg_N
("others must be last association", Actual
);
1552 -- This subprogram is used both for formal packages and for
1553 -- instantiations. For the latter, associations must all be
1556 if Nkind
(I_Node
) /= N_Formal_Package_Declaration
1557 and then Comes_From_Source
(I_Node
)
1560 ("others association not allowed in an instance",
1564 -- In any case, nothing to do after the others association
1568 elsif Box_Present
(Actual
)
1569 and then Comes_From_Source
(I_Node
)
1570 and then Nkind
(I_Node
) /= N_Formal_Package_Declaration
1573 ("box association not allowed in an instance", Actual
);
1579 -- If named associations are present, save first named association
1580 -- (it may of course be Empty) to facilitate subsequent name search.
1582 First_Named
:= First
(Actuals
);
1583 while Present
(First_Named
)
1584 and then Nkind
(First_Named
) /= N_Others_Choice
1585 and then No
(Selector_Name
(First_Named
))
1587 Num_Actuals
:= Num_Actuals
+ 1;
1592 Named
:= First_Named
;
1593 while Present
(Named
) loop
1594 if Nkind
(Named
) /= N_Others_Choice
1595 and then No
(Selector_Name
(Named
))
1597 Error_Msg_N
("invalid positional actual after named one", Named
);
1598 Abandon_Instantiation
(Named
);
1601 -- A named association may lack an actual parameter, if it was
1602 -- introduced for a default subprogram that turns out to be local
1603 -- to the outer instantiation. If it has a box association it must
1604 -- correspond to some formal in the generic.
1606 if Nkind
(Named
) /= N_Others_Choice
1607 and then (Present
(Explicit_Generic_Actual_Parameter
(Named
))
1608 or else Box_Present
(Named
))
1610 Num_Actuals
:= Num_Actuals
+ 1;
1616 if Present
(Formals
) then
1617 Formal
:= First_Non_Pragma
(Formals
);
1618 Analyzed_Formal
:= First_Non_Pragma
(F_Copy
);
1620 if Present
(Actuals
) then
1621 Actual
:= First
(Actuals
);
1623 -- All formals should have default values
1629 while Present
(Formal
) loop
1630 Set_Analyzed_Formal
;
1631 Saved_Formal
:= Next_Non_Pragma
(Formal
);
1633 case Nkind
(Formal
) is
1634 when N_Formal_Object_Declaration
=>
1637 (Defining_Identifier
(Formal
),
1638 Defining_Identifier
(Analyzed_Formal
));
1640 if No
(Match
) and then Partial_Parameterization
then
1641 Process_Default
(Formal
);
1645 (Instantiate_Object
(Formal
, Match
, Analyzed_Formal
),
1648 -- For a defaulted in_parameter, create an entry in the
1649 -- the list of defaulted actuals, for GNATProve use. Do
1650 -- not included these defaults for an instance nested
1651 -- within a generic, because the defaults are also used
1652 -- in the analysis of the enclosing generic, and only
1653 -- defaulted subprograms are relevant there.
1655 if No
(Match
) and then not Inside_A_Generic
then
1656 Append_To
(Default_Actuals
,
1657 Make_Generic_Association
(Sloc
(I_Node
),
1660 (Defining_Identifier
(Formal
), Sloc
(I_Node
)),
1661 Explicit_Generic_Actual_Parameter
=>
1662 New_Copy_Tree
(Default_Expression
(Formal
))));
1666 -- If the object is a call to an expression function, this
1667 -- is a freezing point for it.
1669 if Is_Entity_Name
(Match
)
1670 and then Present
(Entity
(Match
))
1672 (Original_Node
(Unit_Declaration_Node
(Entity
(Match
))))
1673 = N_Expression_Function
1675 Append_Elmt
(Entity
(Match
), Actuals_To_Freeze
);
1678 when N_Formal_Type_Declaration
=>
1681 (Defining_Identifier
(Formal
),
1682 Defining_Identifier
(Analyzed_Formal
));
1685 if Partial_Parameterization
then
1686 Process_Default
(Formal
);
1689 Error_Msg_Sloc
:= Sloc
(Gen_Unit
);
1692 Instantiation_Node
, Defining_Identifier
(Formal
));
1694 ("\in instantiation of & declared#",
1695 Instantiation_Node
, Gen_Unit
);
1696 Abandon_Instantiation
(Instantiation_Node
);
1703 (Formal
, Match
, Analyzed_Formal
, Assoc_List
),
1706 -- Warn when an actual is a fixed-point with user-
1707 -- defined promitives. The warning is superfluous
1708 -- if the fornal is private, because there can be
1709 -- no arithmetic operations in the generic so there
1710 -- no danger of confusion.
1712 if Is_Fixed_Point_Type
(Entity
(Match
))
1713 and then not Is_Private_Type
1714 (Defining_Identifier
(Analyzed_Formal
))
1716 Check_Fixed_Point_Actual
(Match
);
1719 -- An instantiation is a freeze point for the actuals,
1720 -- unless this is a rewritten formal package, or the
1721 -- formal is an Ada 2012 formal incomplete type.
1723 if Nkind
(I_Node
) = N_Formal_Package_Declaration
1725 (Ada_Version
>= Ada_2012
1727 Ekind
(Defining_Identifier
(Analyzed_Formal
)) =
1733 Append_Elmt
(Entity
(Match
), Actuals_To_Freeze
);
1737 -- A remote access-to-class-wide type is not a legal actual
1738 -- for a generic formal of an access type (E.2.2(17/2)).
1739 -- In GNAT an exception to this rule is introduced when
1740 -- the formal is marked as remote using implementation
1741 -- defined aspect/pragma Remote_Access_Type. In that case
1742 -- the actual must be remote as well.
1744 -- If the current instantiation is the construction of a
1745 -- local copy for a formal package the actuals may be
1746 -- defaulted, and there is no matching actual to check.
1748 if Nkind
(Analyzed_Formal
) = N_Formal_Type_Declaration
1750 Nkind
(Formal_Type_Definition
(Analyzed_Formal
)) =
1751 N_Access_To_Object_Definition
1752 and then Present
(Match
)
1755 Formal_Ent
: constant Entity_Id
:=
1756 Defining_Identifier
(Analyzed_Formal
);
1758 if Is_Remote_Access_To_Class_Wide_Type
(Entity
(Match
))
1759 = Is_Remote_Types
(Formal_Ent
)
1761 -- Remoteness of formal and actual match
1765 elsif Is_Remote_Types
(Formal_Ent
) then
1767 -- Remote formal, non-remote actual
1770 ("actual for& must be remote", Match
, Formal_Ent
);
1773 -- Non-remote formal, remote actual
1776 ("actual for& may not be remote",
1782 when N_Formal_Subprogram_Declaration
=>
1785 (Defining_Unit_Name
(Specification
(Formal
)),
1786 Defining_Unit_Name
(Specification
(Analyzed_Formal
)));
1788 -- If the formal subprogram has the same name as another
1789 -- formal subprogram of the generic, then a named
1790 -- association is illegal (12.3(9)). Exclude named
1791 -- associations that are generated for a nested instance.
1794 and then Is_Named_Assoc
1795 and then Comes_From_Source
(Found_Assoc
)
1797 Check_Overloaded_Formal_Subprogram
(Formal
);
1800 -- If there is no corresponding actual, this may be case
1801 -- of partial parameterization, or else the formal has a
1802 -- default or a box.
1804 if No
(Match
) and then Partial_Parameterization
then
1805 Process_Default
(Formal
);
1807 if Nkind
(I_Node
) = N_Formal_Package_Declaration
then
1808 Check_Overloaded_Formal_Subprogram
(Formal
);
1812 Append_To
(Assoc_List
,
1813 Instantiate_Formal_Subprogram
1814 (Formal
, Match
, Analyzed_Formal
));
1816 -- An instantiation is a freeze point for the actuals,
1817 -- unless this is a rewritten formal package.
1819 if Nkind
(I_Node
) /= N_Formal_Package_Declaration
1820 and then Nkind
(Match
) = N_Identifier
1821 and then Is_Subprogram
(Entity
(Match
))
1823 -- The actual subprogram may rename a routine defined
1824 -- in Standard. Avoid freezing such renamings because
1825 -- subprograms coming from Standard cannot be frozen.
1828 not Renames_Standard_Subprogram
(Entity
(Match
))
1830 -- If the actual subprogram comes from a different
1831 -- unit, it is already frozen, either by a body in
1832 -- that unit or by the end of the declarative part
1833 -- of the unit. This check avoids the freezing of
1834 -- subprograms defined in Standard which are used
1835 -- as generic actuals.
1837 and then In_Same_Code_Unit
(Entity
(Match
), I_Node
)
1838 and then Has_Fully_Defined_Profile
(Entity
(Match
))
1840 -- Mark the subprogram as having a delayed freeze
1841 -- since this may be an out-of-order action.
1843 Set_Has_Delayed_Freeze
(Entity
(Match
));
1844 Append_Elmt
(Entity
(Match
), Actuals_To_Freeze
);
1848 -- If this is a nested generic, preserve default for later
1849 -- instantiations. We do this as well for GNATProve use,
1850 -- so that the list of generic associations is complete.
1852 if No
(Match
) and then Box_Present
(Formal
) then
1854 Subp
: constant Entity_Id
:=
1856 (Specification
(Last
(Assoc_List
)));
1859 Append_To
(Default_Actuals
,
1860 Make_Generic_Association
(Sloc
(I_Node
),
1862 New_Occurrence_Of
(Subp
, Sloc
(I_Node
)),
1863 Explicit_Generic_Actual_Parameter
=>
1864 New_Occurrence_Of
(Subp
, Sloc
(I_Node
))));
1868 when N_Formal_Package_Declaration
=>
1871 (Defining_Identifier
(Formal
),
1872 Defining_Identifier
(Original_Node
(Analyzed_Formal
)));
1875 if Partial_Parameterization
then
1876 Process_Default
(Formal
);
1879 Error_Msg_Sloc
:= Sloc
(Gen_Unit
);
1882 Instantiation_Node
, Defining_Identifier
(Formal
));
1884 ("\in instantiation of & declared#",
1885 Instantiation_Node
, Gen_Unit
);
1887 Abandon_Instantiation
(Instantiation_Node
);
1893 (Instantiate_Formal_Package
1894 (Formal
, Match
, Analyzed_Formal
),
1897 -- Determine whether the actual package needs an explicit
1898 -- freeze node. This is only the case if the actual is
1899 -- declared in the same unit and has a body. Normally
1900 -- packages do not have explicit freeze nodes, and gigi
1901 -- only uses them to elaborate entities in a package
1904 Explicit_Freeze_Check
: declare
1905 Actual
: constant Entity_Id
:= Entity
(Match
);
1906 Gen_Par
: Entity_Id
;
1908 Needs_Freezing
: Boolean;
1911 procedure Check_Generic_Parent
;
1912 -- The actual may be an instantiation of a unit
1913 -- declared in a previous instantiation. If that
1914 -- one is also in the current compilation, it must
1915 -- itself be frozen before the actual. The actual
1916 -- may be an instantiation of a generic child unit,
1917 -- in which case the same applies to the instance
1918 -- of the parent which must be frozen before the
1920 -- Should this itself be recursive ???
1922 --------------------------
1923 -- Check_Generic_Parent --
1924 --------------------------
1926 procedure Check_Generic_Parent
is
1927 Inst
: constant Node_Id
:=
1928 Next
(Unit_Declaration_Node
(Actual
));
1934 if Nkind
(Parent
(Actual
)) = N_Package_Specification
1936 Par
:= Scope
(Generic_Parent
(Parent
(Actual
)));
1938 if Is_Generic_Instance
(Par
) then
1941 -- If the actual is a child generic unit, check
1942 -- whether the instantiation of the parent is
1943 -- also local and must also be frozen now. We
1944 -- must retrieve the instance node to locate the
1945 -- parent instance if any.
1947 elsif Ekind
(Par
) = E_Generic_Package
1948 and then Is_Child_Unit
(Gen_Par
)
1949 and then Ekind
(Scope
(Gen_Par
)) =
1952 if Nkind
(Inst
) = N_Package_Instantiation
1953 and then Nkind
(Name
(Inst
)) =
1956 -- Retrieve entity of parent instance
1958 Par
:= Entity
(Prefix
(Name
(Inst
)));
1967 and then Is_Generic_Instance
(Par
)
1968 and then Scope
(Par
) = Current_Scope
1970 (No
(Freeze_Node
(Par
))
1972 not Is_List_Member
(Freeze_Node
(Par
)))
1974 Set_Has_Delayed_Freeze
(Par
);
1975 Append_Elmt
(Par
, Actuals_To_Freeze
);
1977 end Check_Generic_Parent
;
1979 -- Start of processing for Explicit_Freeze_Check
1982 if Present
(Renamed_Entity
(Actual
)) then
1984 Generic_Parent
(Specification
1985 (Unit_Declaration_Node
1986 (Renamed_Entity
(Actual
))));
1989 Generic_Parent
(Specification
1990 (Unit_Declaration_Node
(Actual
)));
1993 if not Expander_Active
1994 or else not Has_Completion
(Actual
)
1995 or else not In_Same_Source_Unit
(I_Node
, Actual
)
1996 or else Is_Frozen
(Actual
)
1998 (Present
(Renamed_Entity
(Actual
))
2000 not In_Same_Source_Unit
2001 (I_Node
, (Renamed_Entity
(Actual
))))
2006 -- Finally we want to exclude such freeze nodes
2007 -- from statement sequences, which freeze
2008 -- everything before them.
2009 -- Is this strictly necessary ???
2011 Needs_Freezing
:= True;
2014 while Present
(S
) loop
2015 if Ekind_In
(S
, E_Block
,
2020 Needs_Freezing
:= False;
2027 if Needs_Freezing
then
2028 Check_Generic_Parent
;
2030 -- If the actual is a renaming of a proper
2031 -- instance of the formal package, indicate
2032 -- that it is the instance that must be frozen.
2034 if Nkind
(Parent
(Actual
)) =
2035 N_Package_Renaming_Declaration
2037 Set_Has_Delayed_Freeze
2038 (Renamed_Entity
(Actual
));
2040 (Renamed_Entity
(Actual
),
2043 Set_Has_Delayed_Freeze
(Actual
);
2044 Append_Elmt
(Actual
, Actuals_To_Freeze
);
2048 end Explicit_Freeze_Check
;
2051 -- For use type and use package appearing in the generic part,
2052 -- we have already copied them, so we can just move them where
2053 -- they belong (we mustn't recopy them since this would mess up
2054 -- the Sloc values).
2056 when N_Use_Package_Clause
2059 if Nkind
(Original_Node
(I_Node
)) =
2060 N_Formal_Package_Declaration
2062 Append
(New_Copy_Tree
(Formal
), Assoc_List
);
2065 Append
(Formal
, Assoc_List
);
2069 raise Program_Error
;
2072 Formal
:= Saved_Formal
;
2073 Next_Non_Pragma
(Analyzed_Formal
);
2076 if Num_Actuals
> Num_Matched
then
2077 Error_Msg_Sloc
:= Sloc
(Gen_Unit
);
2079 if Present
(Selector_Name
(Actual
)) then
2081 ("unmatched actual &", Actual
, Selector_Name
(Actual
));
2083 ("\in instantiation of & declared#", Actual
, Gen_Unit
);
2086 ("unmatched actual in instantiation of & declared#",
2091 elsif Present
(Actuals
) then
2093 ("too many actuals in generic instantiation", Instantiation_Node
);
2096 -- An instantiation freezes all generic actuals. The only exceptions
2097 -- to this are incomplete types and subprograms which are not fully
2098 -- defined at the point of instantiation.
2101 Elmt
: Elmt_Id
:= First_Elmt
(Actuals_To_Freeze
);
2103 while Present
(Elmt
) loop
2104 Freeze_Before
(I_Node
, Node
(Elmt
));
2109 -- If there are default subprograms, normalize the tree by adding
2110 -- explicit associations for them. This is required if the instance
2111 -- appears within a generic.
2113 if not Is_Empty_List
(Default_Actuals
) then
2118 Default
:= First
(Default_Actuals
);
2119 while Present
(Default
) loop
2120 Mark_Rewrite_Insertion
(Default
);
2124 if No
(Actuals
) then
2125 Set_Generic_Associations
(I_Node
, Default_Actuals
);
2127 Append_List_To
(Actuals
, Default_Actuals
);
2132 -- If this is a formal package, normalize the parameter list by adding
2133 -- explicit box associations for the formals that are covered by an
2136 if not Is_Empty_List
(Default_Formals
) then
2137 Append_List
(Default_Formals
, Formals
);
2141 end Analyze_Associations
;
2143 -------------------------------
2144 -- Analyze_Formal_Array_Type --
2145 -------------------------------
2147 procedure Analyze_Formal_Array_Type
2148 (T
: in out Entity_Id
;
2154 -- Treated like a non-generic array declaration, with additional
2159 if Nkind
(Def
) = N_Constrained_Array_Definition
then
2160 DSS
:= First
(Discrete_Subtype_Definitions
(Def
));
2161 while Present
(DSS
) loop
2162 if Nkind_In
(DSS
, N_Subtype_Indication
,
2164 N_Attribute_Reference
)
2166 Error_Msg_N
("only a subtype mark is allowed in a formal", DSS
);
2173 Array_Type_Declaration
(T
, Def
);
2174 Set_Is_Generic_Type
(Base_Type
(T
));
2176 if Ekind
(Component_Type
(T
)) = E_Incomplete_Type
2177 and then No
(Full_View
(Component_Type
(T
)))
2179 Error_Msg_N
("premature usage of incomplete type", Def
);
2181 -- Check that range constraint is not allowed on the component type
2182 -- of a generic formal array type (AARM 12.5.3(3))
2184 elsif Is_Internal
(Component_Type
(T
))
2185 and then Present
(Subtype_Indication
(Component_Definition
(Def
)))
2186 and then Nkind
(Original_Node
2187 (Subtype_Indication
(Component_Definition
(Def
)))) =
2188 N_Subtype_Indication
2191 ("in a formal, a subtype indication can only be "
2192 & "a subtype mark (RM 12.5.3(3))",
2193 Subtype_Indication
(Component_Definition
(Def
)));
2196 end Analyze_Formal_Array_Type
;
2198 ---------------------------------------------
2199 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2200 ---------------------------------------------
2202 -- As for other generic types, we create a valid type representation with
2203 -- legal but arbitrary attributes, whose values are never considered
2204 -- static. For all scalar types we introduce an anonymous base type, with
2205 -- the same attributes. We choose the corresponding integer type to be
2206 -- Standard_Integer.
2207 -- Here and in other similar routines, the Sloc of the generated internal
2208 -- type must be the same as the sloc of the defining identifier of the
2209 -- formal type declaration, to provide proper source navigation.
2211 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2215 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2217 Base
: constant Entity_Id
:=
2219 (E_Decimal_Fixed_Point_Type
,
2221 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
2223 Int_Base
: constant Entity_Id
:= Standard_Integer
;
2224 Delta_Val
: constant Ureal
:= Ureal_1
;
2225 Digs_Val
: constant Uint
:= Uint_6
;
2227 function Make_Dummy_Bound
return Node_Id
;
2228 -- Return a properly typed universal real literal to use as a bound
2230 ----------------------
2231 -- Make_Dummy_Bound --
2232 ----------------------
2234 function Make_Dummy_Bound
return Node_Id
is
2235 Bound
: constant Node_Id
:= Make_Real_Literal
(Loc
, Ureal_1
);
2237 Set_Etype
(Bound
, Universal_Real
);
2239 end Make_Dummy_Bound
;
2241 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2246 Set_Etype
(Base
, Base
);
2247 Set_Size_Info
(Base
, Int_Base
);
2248 Set_RM_Size
(Base
, RM_Size
(Int_Base
));
2249 Set_First_Rep_Item
(Base
, First_Rep_Item
(Int_Base
));
2250 Set_Digits_Value
(Base
, Digs_Val
);
2251 Set_Delta_Value
(Base
, Delta_Val
);
2252 Set_Small_Value
(Base
, Delta_Val
);
2253 Set_Scalar_Range
(Base
,
2255 Low_Bound
=> Make_Dummy_Bound
,
2256 High_Bound
=> Make_Dummy_Bound
));
2258 Set_Is_Generic_Type
(Base
);
2259 Set_Parent
(Base
, Parent
(Def
));
2261 Set_Ekind
(T
, E_Decimal_Fixed_Point_Subtype
);
2262 Set_Etype
(T
, Base
);
2263 Set_Size_Info
(T
, Int_Base
);
2264 Set_RM_Size
(T
, RM_Size
(Int_Base
));
2265 Set_First_Rep_Item
(T
, First_Rep_Item
(Int_Base
));
2266 Set_Digits_Value
(T
, Digs_Val
);
2267 Set_Delta_Value
(T
, Delta_Val
);
2268 Set_Small_Value
(T
, Delta_Val
);
2269 Set_Scalar_Range
(T
, Scalar_Range
(Base
));
2270 Set_Is_Constrained
(T
);
2272 Check_Restriction
(No_Fixed_Point
, Def
);
2273 end Analyze_Formal_Decimal_Fixed_Point_Type
;
2275 -------------------------------------------
2276 -- Analyze_Formal_Derived_Interface_Type --
2277 -------------------------------------------
2279 procedure Analyze_Formal_Derived_Interface_Type
2284 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2287 -- Rewrite as a type declaration of a derived type. This ensures that
2288 -- the interface list and primitive operations are properly captured.
2291 Make_Full_Type_Declaration
(Loc
,
2292 Defining_Identifier
=> T
,
2293 Type_Definition
=> Def
));
2295 Set_Is_Generic_Type
(T
);
2296 end Analyze_Formal_Derived_Interface_Type
;
2298 ---------------------------------
2299 -- Analyze_Formal_Derived_Type --
2300 ---------------------------------
2302 procedure Analyze_Formal_Derived_Type
2307 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2308 Unk_Disc
: constant Boolean := Unknown_Discriminants_Present
(N
);
2312 Set_Is_Generic_Type
(T
);
2314 if Private_Present
(Def
) then
2316 Make_Private_Extension_Declaration
(Loc
,
2317 Defining_Identifier
=> T
,
2318 Discriminant_Specifications
=> Discriminant_Specifications
(N
),
2319 Unknown_Discriminants_Present
=> Unk_Disc
,
2320 Subtype_Indication
=> Subtype_Mark
(Def
),
2321 Interface_List
=> Interface_List
(Def
));
2323 Set_Abstract_Present
(New_N
, Abstract_Present
(Def
));
2324 Set_Limited_Present
(New_N
, Limited_Present
(Def
));
2325 Set_Synchronized_Present
(New_N
, Synchronized_Present
(Def
));
2329 Make_Full_Type_Declaration
(Loc
,
2330 Defining_Identifier
=> T
,
2331 Discriminant_Specifications
=>
2332 Discriminant_Specifications
(Parent
(T
)),
2334 Make_Derived_Type_Definition
(Loc
,
2335 Subtype_Indication
=> Subtype_Mark
(Def
)));
2337 Set_Abstract_Present
2338 (Type_Definition
(New_N
), Abstract_Present
(Def
));
2340 (Type_Definition
(New_N
), Limited_Present
(Def
));
2347 if not Is_Composite_Type
(T
) then
2349 ("unknown discriminants not allowed for elementary types", N
);
2351 Set_Has_Unknown_Discriminants
(T
);
2352 Set_Is_Constrained
(T
, False);
2356 -- If the parent type has a known size, so does the formal, which makes
2357 -- legal representation clauses that involve the formal.
2359 Set_Size_Known_At_Compile_Time
2360 (T
, Size_Known_At_Compile_Time
(Entity
(Subtype_Mark
(Def
))));
2361 end Analyze_Formal_Derived_Type
;
2363 ----------------------------------
2364 -- Analyze_Formal_Discrete_Type --
2365 ----------------------------------
2367 -- The operations defined for a discrete types are those of an enumeration
2368 -- type. The size is set to an arbitrary value, for use in analyzing the
2371 procedure Analyze_Formal_Discrete_Type
(T
: Entity_Id
; Def
: Node_Id
) is
2372 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2376 Base
: constant Entity_Id
:=
2378 (E_Floating_Point_Type
, Current_Scope
,
2379 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
2383 Set_Ekind
(T
, E_Enumeration_Subtype
);
2384 Set_Etype
(T
, Base
);
2387 Set_Is_Generic_Type
(T
);
2388 Set_Is_Constrained
(T
);
2390 -- For semantic analysis, the bounds of the type must be set to some
2391 -- non-static value. The simplest is to create attribute nodes for those
2392 -- bounds, that refer to the type itself. These bounds are never
2393 -- analyzed but serve as place-holders.
2396 Make_Attribute_Reference
(Loc
,
2397 Attribute_Name
=> Name_First
,
2398 Prefix
=> New_Occurrence_Of
(T
, Loc
));
2402 Make_Attribute_Reference
(Loc
,
2403 Attribute_Name
=> Name_Last
,
2404 Prefix
=> New_Occurrence_Of
(T
, Loc
));
2407 Set_Scalar_Range
(T
,
2412 Set_Ekind
(Base
, E_Enumeration_Type
);
2413 Set_Etype
(Base
, Base
);
2414 Init_Size
(Base
, 8);
2415 Init_Alignment
(Base
);
2416 Set_Is_Generic_Type
(Base
);
2417 Set_Scalar_Range
(Base
, Scalar_Range
(T
));
2418 Set_Parent
(Base
, Parent
(Def
));
2419 end Analyze_Formal_Discrete_Type
;
2421 ----------------------------------
2422 -- Analyze_Formal_Floating_Type --
2423 ---------------------------------
2425 procedure Analyze_Formal_Floating_Type
(T
: Entity_Id
; Def
: Node_Id
) is
2426 Base
: constant Entity_Id
:=
2428 (E_Floating_Point_Type
, Current_Scope
,
2429 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
2432 -- The various semantic attributes are taken from the predefined type
2433 -- Float, just so that all of them are initialized. Their values are
2434 -- never used because no constant folding or expansion takes place in
2435 -- the generic itself.
2438 Set_Ekind
(T
, E_Floating_Point_Subtype
);
2439 Set_Etype
(T
, Base
);
2440 Set_Size_Info
(T
, (Standard_Float
));
2441 Set_RM_Size
(T
, RM_Size
(Standard_Float
));
2442 Set_Digits_Value
(T
, Digits_Value
(Standard_Float
));
2443 Set_Scalar_Range
(T
, Scalar_Range
(Standard_Float
));
2444 Set_Is_Constrained
(T
);
2446 Set_Is_Generic_Type
(Base
);
2447 Set_Etype
(Base
, Base
);
2448 Set_Size_Info
(Base
, (Standard_Float
));
2449 Set_RM_Size
(Base
, RM_Size
(Standard_Float
));
2450 Set_Digits_Value
(Base
, Digits_Value
(Standard_Float
));
2451 Set_Scalar_Range
(Base
, Scalar_Range
(Standard_Float
));
2452 Set_Parent
(Base
, Parent
(Def
));
2454 Check_Restriction
(No_Floating_Point
, Def
);
2455 end Analyze_Formal_Floating_Type
;
2457 -----------------------------------
2458 -- Analyze_Formal_Interface_Type;--
2459 -----------------------------------
2461 procedure Analyze_Formal_Interface_Type
2466 Loc
: constant Source_Ptr
:= Sloc
(N
);
2471 Make_Full_Type_Declaration
(Loc
,
2472 Defining_Identifier
=> T
,
2473 Type_Definition
=> Def
);
2477 Set_Is_Generic_Type
(T
);
2478 end Analyze_Formal_Interface_Type
;
2480 ---------------------------------
2481 -- Analyze_Formal_Modular_Type --
2482 ---------------------------------
2484 procedure Analyze_Formal_Modular_Type
(T
: Entity_Id
; Def
: Node_Id
) is
2486 -- Apart from their entity kind, generic modular types are treated like
2487 -- signed integer types, and have the same attributes.
2489 Analyze_Formal_Signed_Integer_Type
(T
, Def
);
2490 Set_Ekind
(T
, E_Modular_Integer_Subtype
);
2491 Set_Ekind
(Etype
(T
), E_Modular_Integer_Type
);
2493 end Analyze_Formal_Modular_Type
;
2495 ---------------------------------------
2496 -- Analyze_Formal_Object_Declaration --
2497 ---------------------------------------
2499 procedure Analyze_Formal_Object_Declaration
(N
: Node_Id
) is
2500 E
: constant Node_Id
:= Default_Expression
(N
);
2501 Id
: constant Node_Id
:= Defining_Identifier
(N
);
2508 -- Determine the mode of the formal object
2510 if Out_Present
(N
) then
2511 K
:= E_Generic_In_Out_Parameter
;
2513 if not In_Present
(N
) then
2514 Error_Msg_N
("formal generic objects cannot have mode OUT", N
);
2518 K
:= E_Generic_In_Parameter
;
2521 if Present
(Subtype_Mark
(N
)) then
2522 Find_Type
(Subtype_Mark
(N
));
2523 T
:= Entity
(Subtype_Mark
(N
));
2525 -- Verify that there is no redundant null exclusion
2527 if Null_Exclusion_Present
(N
) then
2528 if not Is_Access_Type
(T
) then
2530 ("null exclusion can only apply to an access type", N
);
2532 elsif Can_Never_Be_Null
(T
) then
2534 ("`NOT NULL` not allowed (& already excludes null)", N
, T
);
2538 -- Ada 2005 (AI-423): Formal object with an access definition
2541 Check_Access_Definition
(N
);
2542 T
:= Access_Definition
2544 N
=> Access_Definition
(N
));
2547 if Ekind
(T
) = E_Incomplete_Type
then
2549 Error_Node
: Node_Id
;
2552 if Present
(Subtype_Mark
(N
)) then
2553 Error_Node
:= Subtype_Mark
(N
);
2555 Check_Access_Definition
(N
);
2556 Error_Node
:= Access_Definition
(N
);
2559 Error_Msg_N
("premature usage of incomplete type", Error_Node
);
2563 if K
= E_Generic_In_Parameter
then
2565 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2567 if Ada_Version
< Ada_2005
and then Is_Limited_Type
(T
) then
2569 ("generic formal of mode IN must not be of limited type", N
);
2570 Explain_Limited_Type
(T
, N
);
2573 if Is_Abstract_Type
(T
) then
2575 ("generic formal of mode IN must not be of abstract type", N
);
2579 Preanalyze_Spec_Expression
(E
, T
);
2581 if Is_Limited_Type
(T
) and then not OK_For_Limited_Init
(T
, E
) then
2583 ("initialization not allowed for limited types", E
);
2584 Explain_Limited_Type
(T
, E
);
2591 -- Case of generic IN OUT parameter
2594 -- If the formal has an unconstrained type, construct its actual
2595 -- subtype, as is done for subprogram formals. In this fashion, all
2596 -- its uses can refer to specific bounds.
2601 if (Is_Array_Type
(T
) and then not Is_Constrained
(T
))
2602 or else (Ekind
(T
) = E_Record_Type
and then Has_Discriminants
(T
))
2605 Non_Freezing_Ref
: constant Node_Id
:=
2606 New_Occurrence_Of
(Id
, Sloc
(Id
));
2610 -- Make sure the actual subtype doesn't generate bogus freezing
2612 Set_Must_Not_Freeze
(Non_Freezing_Ref
);
2613 Decl
:= Build_Actual_Subtype
(T
, Non_Freezing_Ref
);
2614 Insert_Before_And_Analyze
(N
, Decl
);
2615 Set_Actual_Subtype
(Id
, Defining_Identifier
(Decl
));
2618 Set_Actual_Subtype
(Id
, T
);
2623 ("initialization not allowed for `IN OUT` formals", N
);
2627 if Has_Aspects
(N
) then
2628 Analyze_Aspect_Specifications
(N
, Id
);
2630 end Analyze_Formal_Object_Declaration
;
2632 ----------------------------------------------
2633 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2634 ----------------------------------------------
2636 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2640 Loc
: constant Source_Ptr
:= Sloc
(Def
);
2641 Base
: constant Entity_Id
:=
2643 (E_Ordinary_Fixed_Point_Type
, Current_Scope
,
2644 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
2647 -- The semantic attributes are set for completeness only, their values
2648 -- will never be used, since all properties of the type are non-static.
2651 Set_Ekind
(T
, E_Ordinary_Fixed_Point_Subtype
);
2652 Set_Etype
(T
, Base
);
2653 Set_Size_Info
(T
, Standard_Integer
);
2654 Set_RM_Size
(T
, RM_Size
(Standard_Integer
));
2655 Set_Small_Value
(T
, Ureal_1
);
2656 Set_Delta_Value
(T
, Ureal_1
);
2657 Set_Scalar_Range
(T
,
2659 Low_Bound
=> Make_Real_Literal
(Loc
, Ureal_1
),
2660 High_Bound
=> Make_Real_Literal
(Loc
, Ureal_1
)));
2661 Set_Is_Constrained
(T
);
2663 Set_Is_Generic_Type
(Base
);
2664 Set_Etype
(Base
, Base
);
2665 Set_Size_Info
(Base
, Standard_Integer
);
2666 Set_RM_Size
(Base
, RM_Size
(Standard_Integer
));
2667 Set_Small_Value
(Base
, Ureal_1
);
2668 Set_Delta_Value
(Base
, Ureal_1
);
2669 Set_Scalar_Range
(Base
, Scalar_Range
(T
));
2670 Set_Parent
(Base
, Parent
(Def
));
2672 Check_Restriction
(No_Fixed_Point
, Def
);
2673 end Analyze_Formal_Ordinary_Fixed_Point_Type
;
2675 ----------------------------------------
2676 -- Analyze_Formal_Package_Declaration --
2677 ----------------------------------------
2679 procedure Analyze_Formal_Package_Declaration
(N
: Node_Id
) is
2680 Gen_Id
: constant Node_Id
:= Name
(N
);
2681 Loc
: constant Source_Ptr
:= Sloc
(N
);
2682 Pack_Id
: constant Entity_Id
:= Defining_Identifier
(N
);
2685 Gen_Unit
: Entity_Id
;
2688 Vis_Prims_List
: Elist_Id
:= No_Elist
;
2689 -- List of primitives made temporarily visible in the instantiation
2690 -- to match the visibility of the formal type.
2692 function Build_Local_Package
return Node_Id
;
2693 -- The formal package is rewritten so that its parameters are replaced
2694 -- with corresponding declarations. For parameters with bona fide
2695 -- associations these declarations are created by Analyze_Associations
2696 -- as for a regular instantiation. For boxed parameters, we preserve
2697 -- the formal declarations and analyze them, in order to introduce
2698 -- entities of the right kind in the environment of the formal.
2700 -------------------------
2701 -- Build_Local_Package --
2702 -------------------------
2704 function Build_Local_Package
return Node_Id
is
2706 Pack_Decl
: Node_Id
;
2709 -- Within the formal, the name of the generic package is a renaming
2710 -- of the formal (as for a regular instantiation).
2713 Make_Package_Declaration
(Loc
,
2716 (Specification
(Original_Node
(Gen_Decl
)),
2717 Empty
, Instantiating
=> True));
2720 Make_Package_Renaming_Declaration
(Loc
,
2721 Defining_Unit_Name
=>
2722 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
)),
2723 Name
=> New_Occurrence_Of
(Formal
, Loc
));
2725 if Nkind
(Gen_Id
) = N_Identifier
2726 and then Chars
(Gen_Id
) = Chars
(Pack_Id
)
2729 ("& is hidden within declaration of instance", Gen_Id
, Gen_Unit
);
2732 -- If the formal is declared with a box, or with an others choice,
2733 -- create corresponding declarations for all entities in the formal
2734 -- part, so that names with the proper types are available in the
2735 -- specification of the formal package.
2737 -- On the other hand, if there are no associations, then all the
2738 -- formals must have defaults, and this will be checked by the
2739 -- call to Analyze_Associations.
2742 or else Nkind
(First
(Generic_Associations
(N
))) = N_Others_Choice
2745 Formal_Decl
: Node_Id
;
2748 -- TBA : for a formal package, need to recurse ???
2753 (Generic_Formal_Declarations
(Original_Node
(Gen_Decl
)));
2754 while Present
(Formal_Decl
) loop
2758 (Formal_Decl
, Empty
, Instantiating
=> True));
2763 -- If generic associations are present, use Analyze_Associations to
2764 -- create the proper renaming declarations.
2768 Act_Tree
: constant Node_Id
:=
2770 (Original_Node
(Gen_Decl
), Empty
,
2771 Instantiating
=> True);
2774 Generic_Renamings
.Set_Last
(0);
2775 Generic_Renamings_HTable
.Reset
;
2776 Instantiation_Node
:= N
;
2779 Analyze_Associations
2780 (I_Node
=> Original_Node
(N
),
2781 Formals
=> Generic_Formal_Declarations
(Act_Tree
),
2782 F_Copy
=> Generic_Formal_Declarations
(Gen_Decl
));
2784 Vis_Prims_List
:= Check_Hidden_Primitives
(Decls
);
2788 Append
(Renaming
, To
=> Decls
);
2790 -- Add generated declarations ahead of local declarations in
2793 if No
(Visible_Declarations
(Specification
(Pack_Decl
))) then
2794 Set_Visible_Declarations
(Specification
(Pack_Decl
), Decls
);
2797 (First
(Visible_Declarations
(Specification
(Pack_Decl
))),
2802 end Build_Local_Package
;
2806 Save_ISMP
: constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance
;
2807 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2809 Associations
: Boolean := True;
2811 Parent_Installed
: Boolean := False;
2812 Parent_Instance
: Entity_Id
;
2813 Renaming_In_Par
: Entity_Id
;
2815 -- Start of processing for Analyze_Formal_Package_Declaration
2818 Check_Text_IO_Special_Unit
(Gen_Id
);
2821 Check_Generic_Child_Unit
(Gen_Id
, Parent_Installed
);
2822 Gen_Unit
:= Entity
(Gen_Id
);
2824 -- Check for a formal package that is a package renaming
2826 if Present
(Renamed_Object
(Gen_Unit
)) then
2828 -- Indicate that unit is used, before replacing it with renamed
2829 -- entity for use below.
2831 if In_Extended_Main_Source_Unit
(N
) then
2832 Set_Is_Instantiated
(Gen_Unit
);
2833 Generate_Reference
(Gen_Unit
, N
);
2836 Gen_Unit
:= Renamed_Object
(Gen_Unit
);
2839 if Ekind
(Gen_Unit
) /= E_Generic_Package
then
2840 Error_Msg_N
("expect generic package name", Gen_Id
);
2844 elsif Gen_Unit
= Current_Scope
then
2846 ("generic package cannot be used as a formal package of itself",
2851 elsif In_Open_Scopes
(Gen_Unit
) then
2852 if Is_Compilation_Unit
(Gen_Unit
)
2853 and then Is_Child_Unit
(Current_Scope
)
2855 -- Special-case the error when the formal is a parent, and
2856 -- continue analysis to minimize cascaded errors.
2859 ("generic parent cannot be used as formal package of a child "
2864 ("generic package cannot be used as a formal package within "
2865 & "itself", Gen_Id
);
2871 -- Check that name of formal package does not hide name of generic,
2872 -- or its leading prefix. This check must be done separately because
2873 -- the name of the generic has already been analyzed.
2876 Gen_Name
: Entity_Id
;
2880 while Nkind
(Gen_Name
) = N_Expanded_Name
loop
2881 Gen_Name
:= Prefix
(Gen_Name
);
2884 if Chars
(Gen_Name
) = Chars
(Pack_Id
) then
2886 ("& is hidden within declaration of formal package",
2892 or else No
(Generic_Associations
(N
))
2893 or else Nkind
(First
(Generic_Associations
(N
))) = N_Others_Choice
2895 Associations
:= False;
2898 -- If there are no generic associations, the generic parameters appear
2899 -- as local entities and are instantiated like them. We copy the generic
2900 -- package declaration as if it were an instantiation, and analyze it
2901 -- like a regular package, except that we treat the formals as
2902 -- additional visible components.
2904 Gen_Decl
:= Unit_Declaration_Node
(Gen_Unit
);
2906 if In_Extended_Main_Source_Unit
(N
) then
2907 Set_Is_Instantiated
(Gen_Unit
);
2908 Generate_Reference
(Gen_Unit
, N
);
2911 Formal
:= New_Copy
(Pack_Id
);
2912 Create_Instantiation_Source
(N
, Gen_Unit
, S_Adjustment
);
2914 -- Make local generic without formals. The formals will be replaced with
2915 -- internal declarations.
2918 New_N
:= Build_Local_Package
;
2920 -- If there are errors in the parameter list, Analyze_Associations
2921 -- raises Instantiation_Error. Patch the declaration to prevent further
2922 -- exception propagation.
2925 when Instantiation_Error
=>
2926 Enter_Name
(Formal
);
2927 Set_Ekind
(Formal
, E_Variable
);
2928 Set_Etype
(Formal
, Any_Type
);
2929 Restore_Hidden_Primitives
(Vis_Prims_List
);
2931 if Parent_Installed
then
2939 Set_Defining_Unit_Name
(Specification
(New_N
), Formal
);
2940 Set_Generic_Parent
(Specification
(N
), Gen_Unit
);
2941 Set_Instance_Env
(Gen_Unit
, Formal
);
2942 Set_Is_Generic_Instance
(Formal
);
2944 Enter_Name
(Formal
);
2945 Set_Ekind
(Formal
, E_Package
);
2946 Set_Etype
(Formal
, Standard_Void_Type
);
2947 Set_Inner_Instances
(Formal
, New_Elmt_List
);
2948 Push_Scope
(Formal
);
2950 -- Manually set the SPARK_Mode from the context because the package
2951 -- declaration is never analyzed.
2953 Set_SPARK_Pragma
(Formal
, SPARK_Mode_Pragma
);
2954 Set_SPARK_Aux_Pragma
(Formal
, SPARK_Mode_Pragma
);
2955 Set_SPARK_Pragma_Inherited
(Formal
);
2956 Set_SPARK_Aux_Pragma_Inherited
(Formal
);
2958 if Is_Child_Unit
(Gen_Unit
) and then Parent_Installed
then
2960 -- Similarly, we have to make the name of the formal visible in the
2961 -- parent instance, to resolve properly fully qualified names that
2962 -- may appear in the generic unit. The parent instance has been
2963 -- placed on the scope stack ahead of the current scope.
2965 Parent_Instance
:= Scope_Stack
.Table
(Scope_Stack
.Last
- 1).Entity
;
2968 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
));
2969 Set_Ekind
(Renaming_In_Par
, E_Package
);
2970 Set_Etype
(Renaming_In_Par
, Standard_Void_Type
);
2971 Set_Scope
(Renaming_In_Par
, Parent_Instance
);
2972 Set_Parent
(Renaming_In_Par
, Parent
(Formal
));
2973 Set_Renamed_Object
(Renaming_In_Par
, Formal
);
2974 Append_Entity
(Renaming_In_Par
, Parent_Instance
);
2977 -- A formal package declaration behaves as a package instantiation with
2978 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
2979 -- missing, set the global flag which signals Analyze_Pragma to ingnore
2980 -- all SPARK_Mode pragmas within the generic_package_name.
2982 if SPARK_Mode
/= On
then
2983 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
2985 -- Mark the formal spec in case the body is instantiated at a later
2986 -- pass. This preserves the original context in effect for the body.
2988 Set_Ignore_SPARK_Mode_Pragmas
(Formal
);
2991 Analyze
(Specification
(N
));
2993 -- The formals for which associations are provided are not visible
2994 -- outside of the formal package. The others are still declared by a
2995 -- formal parameter declaration.
2997 -- If there are no associations, the only local entity to hide is the
2998 -- generated package renaming itself.
3004 E
:= First_Entity
(Formal
);
3005 while Present
(E
) loop
3006 if Associations
and then not Is_Generic_Formal
(E
) then
3010 if Ekind
(E
) = E_Package
and then Renamed_Entity
(E
) = Formal
then
3019 End_Package_Scope
(Formal
);
3020 Restore_Hidden_Primitives
(Vis_Prims_List
);
3022 if Parent_Installed
then
3028 -- Inside the generic unit, the formal package is a regular package, but
3029 -- no body is needed for it. Note that after instantiation, the defining
3030 -- unit name we need is in the new tree and not in the original (see
3031 -- Package_Instantiation). A generic formal package is an instance, and
3032 -- can be used as an actual for an inner instance.
3034 Set_Has_Completion
(Formal
, True);
3036 -- Add semantic information to the original defining identifier for ASIS
3039 Set_Ekind
(Pack_Id
, E_Package
);
3040 Set_Etype
(Pack_Id
, Standard_Void_Type
);
3041 Set_Scope
(Pack_Id
, Scope
(Formal
));
3042 Set_Has_Completion
(Pack_Id
, True);
3045 if Has_Aspects
(N
) then
3046 Analyze_Aspect_Specifications
(N
, Pack_Id
);
3049 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Save_ISMP
;
3050 end Analyze_Formal_Package_Declaration
;
3052 ---------------------------------
3053 -- Analyze_Formal_Private_Type --
3054 ---------------------------------
3056 procedure Analyze_Formal_Private_Type
3062 New_Private_Type
(N
, T
, Def
);
3064 -- Set the size to an arbitrary but legal value
3066 Set_Size_Info
(T
, Standard_Integer
);
3067 Set_RM_Size
(T
, RM_Size
(Standard_Integer
));
3068 end Analyze_Formal_Private_Type
;
3070 ------------------------------------
3071 -- Analyze_Formal_Incomplete_Type --
3072 ------------------------------------
3074 procedure Analyze_Formal_Incomplete_Type
3080 Set_Ekind
(T
, E_Incomplete_Type
);
3082 Set_Private_Dependents
(T
, New_Elmt_List
);
3084 if Tagged_Present
(Def
) then
3085 Set_Is_Tagged_Type
(T
);
3086 Make_Class_Wide_Type
(T
);
3087 Set_Direct_Primitive_Operations
(T
, New_Elmt_List
);
3089 end Analyze_Formal_Incomplete_Type
;
3091 ----------------------------------------
3092 -- Analyze_Formal_Signed_Integer_Type --
3093 ----------------------------------------
3095 procedure Analyze_Formal_Signed_Integer_Type
3099 Base
: constant Entity_Id
:=
3101 (E_Signed_Integer_Type
,
3103 Sloc
(Defining_Identifier
(Parent
(Def
))), 'G');
3108 Set_Ekind
(T
, E_Signed_Integer_Subtype
);
3109 Set_Etype
(T
, Base
);
3110 Set_Size_Info
(T
, Standard_Integer
);
3111 Set_RM_Size
(T
, RM_Size
(Standard_Integer
));
3112 Set_Scalar_Range
(T
, Scalar_Range
(Standard_Integer
));
3113 Set_Is_Constrained
(T
);
3115 Set_Is_Generic_Type
(Base
);
3116 Set_Size_Info
(Base
, Standard_Integer
);
3117 Set_RM_Size
(Base
, RM_Size
(Standard_Integer
));
3118 Set_Etype
(Base
, Base
);
3119 Set_Scalar_Range
(Base
, Scalar_Range
(Standard_Integer
));
3120 Set_Parent
(Base
, Parent
(Def
));
3121 end Analyze_Formal_Signed_Integer_Type
;
3123 -------------------------------------------
3124 -- Analyze_Formal_Subprogram_Declaration --
3125 -------------------------------------------
3127 procedure Analyze_Formal_Subprogram_Declaration
(N
: Node_Id
) is
3128 Spec
: constant Node_Id
:= Specification
(N
);
3129 Def
: constant Node_Id
:= Default_Name
(N
);
3130 Nam
: constant Entity_Id
:= Defining_Unit_Name
(Spec
);
3138 if Nkind
(Nam
) = N_Defining_Program_Unit_Name
then
3139 Error_Msg_N
("name of formal subprogram must be a direct name", Nam
);
3143 Analyze_Subprogram_Declaration
(N
);
3144 Set_Is_Formal_Subprogram
(Nam
);
3145 Set_Has_Completion
(Nam
);
3147 if Nkind
(N
) = N_Formal_Abstract_Subprogram_Declaration
then
3148 Set_Is_Abstract_Subprogram
(Nam
);
3150 Set_Is_Dispatching_Operation
(Nam
);
3152 -- A formal abstract procedure cannot have a null default
3153 -- (RM 12.6(4.1/2)).
3155 if Nkind
(Spec
) = N_Procedure_Specification
3156 and then Null_Present
(Spec
)
3159 ("a formal abstract subprogram cannot default to null", Spec
);
3163 Ctrl_Type
: constant Entity_Id
:= Find_Dispatching_Type
(Nam
);
3165 if No
(Ctrl_Type
) then
3167 ("abstract formal subprogram must have a controlling type",
3170 elsif Ada_Version
>= Ada_2012
3171 and then Is_Incomplete_Type
(Ctrl_Type
)
3174 ("controlling type of abstract formal subprogram cannot "
3175 & "be incomplete type", N
, Ctrl_Type
);
3178 Check_Controlling_Formals
(Ctrl_Type
, Nam
);
3183 -- Default name is resolved at the point of instantiation
3185 if Box_Present
(N
) then
3188 -- Else default is bound at the point of generic declaration
3190 elsif Present
(Def
) then
3191 if Nkind
(Def
) = N_Operator_Symbol
then
3192 Find_Direct_Name
(Def
);
3194 elsif Nkind
(Def
) /= N_Attribute_Reference
then
3198 -- For an attribute reference, analyze the prefix and verify
3199 -- that it has the proper profile for the subprogram.
3201 Analyze
(Prefix
(Def
));
3202 Valid_Default_Attribute
(Nam
, Def
);
3206 -- Default name may be overloaded, in which case the interpretation
3207 -- with the correct profile must be selected, as for a renaming.
3208 -- If the definition is an indexed component, it must denote a
3209 -- member of an entry family. If it is a selected component, it
3210 -- can be a protected operation.
3212 if Etype
(Def
) = Any_Type
then
3215 elsif Nkind
(Def
) = N_Selected_Component
then
3216 if not Is_Overloadable
(Entity
(Selector_Name
(Def
))) then
3217 Error_Msg_N
("expect valid subprogram name as default", Def
);
3220 elsif Nkind
(Def
) = N_Indexed_Component
then
3221 if Is_Entity_Name
(Prefix
(Def
)) then
3222 if Ekind
(Entity
(Prefix
(Def
))) /= E_Entry_Family
then
3223 Error_Msg_N
("expect valid subprogram name as default", Def
);
3226 elsif Nkind
(Prefix
(Def
)) = N_Selected_Component
then
3227 if Ekind
(Entity
(Selector_Name
(Prefix
(Def
)))) /=
3230 Error_Msg_N
("expect valid subprogram name as default", Def
);
3234 Error_Msg_N
("expect valid subprogram name as default", Def
);
3238 elsif Nkind
(Def
) = N_Character_Literal
then
3240 -- Needs some type checks: subprogram should be parameterless???
3242 Resolve
(Def
, (Etype
(Nam
)));
3244 elsif not Is_Entity_Name
(Def
)
3245 or else not Is_Overloadable
(Entity
(Def
))
3247 Error_Msg_N
("expect valid subprogram name as default", Def
);
3250 elsif not Is_Overloaded
(Def
) then
3251 Subp
:= Entity
(Def
);
3254 Error_Msg_N
("premature usage of formal subprogram", Def
);
3256 elsif not Entity_Matches_Spec
(Subp
, Nam
) then
3257 Error_Msg_N
("no visible entity matches specification", Def
);
3260 -- More than one interpretation, so disambiguate as for a renaming
3265 I1
: Interp_Index
:= 0;
3271 Get_First_Interp
(Def
, I
, It
);
3272 while Present
(It
.Nam
) loop
3273 if Entity_Matches_Spec
(It
.Nam
, Nam
) then
3274 if Subp
/= Any_Id
then
3275 It1
:= Disambiguate
(Def
, I1
, I
, Etype
(Subp
));
3277 if It1
= No_Interp
then
3278 Error_Msg_N
("ambiguous default subprogram", Def
);
3291 Get_Next_Interp
(I
, It
);
3295 if Subp
/= Any_Id
then
3297 -- Subprogram found, generate reference to it
3299 Set_Entity
(Def
, Subp
);
3300 Generate_Reference
(Subp
, Def
);
3303 Error_Msg_N
("premature usage of formal subprogram", Def
);
3305 elsif Ekind
(Subp
) /= E_Operator
then
3306 Check_Mode_Conformant
(Subp
, Nam
);
3310 Error_Msg_N
("no visible subprogram matches specification", N
);
3316 if Has_Aspects
(N
) then
3317 Analyze_Aspect_Specifications
(N
, Nam
);
3320 end Analyze_Formal_Subprogram_Declaration
;
3322 -------------------------------------
3323 -- Analyze_Formal_Type_Declaration --
3324 -------------------------------------
3326 procedure Analyze_Formal_Type_Declaration
(N
: Node_Id
) is
3327 Def
: constant Node_Id
:= Formal_Type_Definition
(N
);
3331 T
:= Defining_Identifier
(N
);
3333 if Present
(Discriminant_Specifications
(N
))
3334 and then Nkind
(Def
) /= N_Formal_Private_Type_Definition
3337 ("discriminants not allowed for this formal type", T
);
3340 -- Enter the new name, and branch to specific routine
3343 when N_Formal_Private_Type_Definition
=>
3344 Analyze_Formal_Private_Type
(N
, T
, Def
);
3346 when N_Formal_Derived_Type_Definition
=>
3347 Analyze_Formal_Derived_Type
(N
, T
, Def
);
3349 when N_Formal_Incomplete_Type_Definition
=>
3350 Analyze_Formal_Incomplete_Type
(T
, Def
);
3352 when N_Formal_Discrete_Type_Definition
=>
3353 Analyze_Formal_Discrete_Type
(T
, Def
);
3355 when N_Formal_Signed_Integer_Type_Definition
=>
3356 Analyze_Formal_Signed_Integer_Type
(T
, Def
);
3358 when N_Formal_Modular_Type_Definition
=>
3359 Analyze_Formal_Modular_Type
(T
, Def
);
3361 when N_Formal_Floating_Point_Definition
=>
3362 Analyze_Formal_Floating_Type
(T
, Def
);
3364 when N_Formal_Ordinary_Fixed_Point_Definition
=>
3365 Analyze_Formal_Ordinary_Fixed_Point_Type
(T
, Def
);
3367 when N_Formal_Decimal_Fixed_Point_Definition
=>
3368 Analyze_Formal_Decimal_Fixed_Point_Type
(T
, Def
);
3370 when N_Array_Type_Definition
=>
3371 Analyze_Formal_Array_Type
(T
, Def
);
3373 when N_Access_Function_Definition
3374 | N_Access_Procedure_Definition
3375 | N_Access_To_Object_Definition
3377 Analyze_Generic_Access_Type
(T
, Def
);
3379 -- Ada 2005: a interface declaration is encoded as an abstract
3380 -- record declaration or a abstract type derivation.
3382 when N_Record_Definition
=>
3383 Analyze_Formal_Interface_Type
(N
, T
, Def
);
3385 when N_Derived_Type_Definition
=>
3386 Analyze_Formal_Derived_Interface_Type
(N
, T
, Def
);
3392 raise Program_Error
;
3395 Set_Is_Generic_Type
(T
);
3397 if Has_Aspects
(N
) then
3398 Analyze_Aspect_Specifications
(N
, T
);
3400 end Analyze_Formal_Type_Declaration
;
3402 ------------------------------------
3403 -- Analyze_Function_Instantiation --
3404 ------------------------------------
3406 procedure Analyze_Function_Instantiation
(N
: Node_Id
) is
3408 Analyze_Subprogram_Instantiation
(N
, E_Function
);
3409 end Analyze_Function_Instantiation
;
3411 ---------------------------------
3412 -- Analyze_Generic_Access_Type --
3413 ---------------------------------
3415 procedure Analyze_Generic_Access_Type
(T
: Entity_Id
; Def
: Node_Id
) is
3419 if Nkind
(Def
) = N_Access_To_Object_Definition
then
3420 Access_Type_Declaration
(T
, Def
);
3422 if Is_Incomplete_Or_Private_Type
(Designated_Type
(T
))
3423 and then No
(Full_View
(Designated_Type
(T
)))
3424 and then not Is_Generic_Type
(Designated_Type
(T
))
3426 Error_Msg_N
("premature usage of incomplete type", Def
);
3428 elsif not Is_Entity_Name
(Subtype_Indication
(Def
)) then
3430 ("only a subtype mark is allowed in a formal", Def
);
3434 Access_Subprogram_Declaration
(T
, Def
);
3436 end Analyze_Generic_Access_Type
;
3438 ---------------------------------
3439 -- Analyze_Generic_Formal_Part --
3440 ---------------------------------
3442 procedure Analyze_Generic_Formal_Part
(N
: Node_Id
) is
3443 Gen_Parm_Decl
: Node_Id
;
3446 -- The generic formals are processed in the scope of the generic unit,
3447 -- where they are immediately visible. The scope is installed by the
3450 Gen_Parm_Decl
:= First
(Generic_Formal_Declarations
(N
));
3451 while Present
(Gen_Parm_Decl
) loop
3452 Analyze
(Gen_Parm_Decl
);
3453 Next
(Gen_Parm_Decl
);
3456 Generate_Reference_To_Generic_Formals
(Current_Scope
);
3457 end Analyze_Generic_Formal_Part
;
3459 ------------------------------------------
3460 -- Analyze_Generic_Package_Declaration --
3461 ------------------------------------------
3463 procedure Analyze_Generic_Package_Declaration
(N
: Node_Id
) is
3464 Decls
: constant List_Id
:= Visible_Declarations
(Specification
(N
));
3465 Loc
: constant Source_Ptr
:= Sloc
(N
);
3471 Save_Parent
: Node_Id
;
3474 Check_SPARK_05_Restriction
("generic is not allowed", N
);
3476 -- We introduce a renaming of the enclosing package, to have a usable
3477 -- entity as the prefix of an expanded name for a local entity of the
3478 -- form Par.P.Q, where P is the generic package. This is because a local
3479 -- entity named P may hide it, so that the usual visibility rules in
3480 -- the instance will not resolve properly.
3483 Make_Package_Renaming_Declaration
(Loc
,
3484 Defining_Unit_Name
=>
3485 Make_Defining_Identifier
(Loc
,
3486 Chars
=> New_External_Name
(Chars
(Defining_Entity
(N
)), "GH")),
3488 Make_Identifier
(Loc
, Chars
(Defining_Entity
(N
))));
3490 -- The declaration is inserted before other declarations, but before
3491 -- pragmas that may be library-unit pragmas and must appear before other
3492 -- declarations. The pragma Compile_Time_Error is not in this class, and
3493 -- may contain an expression that includes such a qualified name, so the
3494 -- renaming declaration must appear before it.
3496 -- Are there other pragmas that require this special handling ???
3498 if Present
(Decls
) then
3499 Decl
:= First
(Decls
);
3500 while Present
(Decl
)
3501 and then Nkind
(Decl
) = N_Pragma
3502 and then Get_Pragma_Id
(Decl
) /= Pragma_Compile_Time_Error
3507 if Present
(Decl
) then
3508 Insert_Before
(Decl
, Renaming
);
3510 Append
(Renaming
, Visible_Declarations
(Specification
(N
)));
3514 Set_Visible_Declarations
(Specification
(N
), New_List
(Renaming
));
3517 -- Create copy of generic unit, and save for instantiation. If the unit
3518 -- is a child unit, do not copy the specifications for the parent, which
3519 -- are not part of the generic tree.
3521 Save_Parent
:= Parent_Spec
(N
);
3522 Set_Parent_Spec
(N
, Empty
);
3524 New_N
:= Copy_Generic_Node
(N
, Empty
, Instantiating
=> False);
3525 Set_Parent_Spec
(New_N
, Save_Parent
);
3528 -- Once the contents of the generic copy and the template are swapped,
3529 -- do the same for their respective aspect specifications.
3531 Exchange_Aspects
(N
, New_N
);
3533 -- Collect all contract-related source pragmas found within the template
3534 -- and attach them to the contract of the package spec. This contract is
3535 -- used in the capture of global references within annotations.
3537 Create_Generic_Contract
(N
);
3539 Id
:= Defining_Entity
(N
);
3540 Generate_Definition
(Id
);
3542 -- Expansion is not applied to generic units
3547 Set_Ekind
(Id
, E_Generic_Package
);
3548 Set_Etype
(Id
, Standard_Void_Type
);
3550 -- Set SPARK_Mode from context
3552 Set_SPARK_Pragma
(Id
, SPARK_Mode_Pragma
);
3553 Set_SPARK_Aux_Pragma
(Id
, SPARK_Mode_Pragma
);
3554 Set_SPARK_Pragma_Inherited
(Id
);
3555 Set_SPARK_Aux_Pragma_Inherited
(Id
);
3557 -- Preserve relevant elaboration-related attributes of the context which
3558 -- are no longer available or very expensive to recompute once analysis,
3559 -- resolution, and expansion are over.
3561 Mark_Elaboration_Attributes
3566 -- Analyze aspects now, so that generated pragmas appear in the
3567 -- declarations before building and analyzing the generic copy.
3569 if Has_Aspects
(N
) then
3570 Analyze_Aspect_Specifications
(N
, Id
);
3574 Enter_Generic_Scope
(Id
);
3575 Set_Inner_Instances
(Id
, New_Elmt_List
);
3577 Set_Categorization_From_Pragmas
(N
);
3578 Set_Is_Pure
(Id
, Is_Pure
(Current_Scope
));
3580 -- Link the declaration of the generic homonym in the generic copy to
3581 -- the package it renames, so that it is always resolved properly.
3583 Set_Generic_Homonym
(Id
, Defining_Unit_Name
(Renaming
));
3584 Set_Entity
(Associated_Node
(Name
(Renaming
)), Id
);
3586 -- For a library unit, we have reconstructed the entity for the unit,
3587 -- and must reset it in the library tables.
3589 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
3590 Set_Cunit_Entity
(Current_Sem_Unit
, Id
);
3593 Analyze_Generic_Formal_Part
(N
);
3595 -- After processing the generic formals, analysis proceeds as for a
3596 -- non-generic package.
3598 Analyze
(Specification
(N
));
3600 Validate_Categorization_Dependency
(N
, Id
);
3604 End_Package_Scope
(Id
);
3605 Exit_Generic_Scope
(Id
);
3607 -- If the generic appears within a package unit, the body of that unit
3608 -- has to be present for instantiation and inlining.
3610 if Nkind
(Unit
(Cunit
(Current_Sem_Unit
))) = N_Package_Declaration
then
3611 Set_Body_Needed_For_Inlining
3612 (Defining_Entity
(Unit
(Cunit
(Current_Sem_Unit
))));
3615 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
3616 Move_Freeze_Nodes
(Id
, N
, Visible_Declarations
(Specification
(N
)));
3617 Move_Freeze_Nodes
(Id
, N
, Private_Declarations
(Specification
(N
)));
3618 Move_Freeze_Nodes
(Id
, N
, Generic_Formal_Declarations
(N
));
3621 Set_Body_Required
(Parent
(N
), Unit_Requires_Body
(Id
));
3622 Validate_RT_RAT_Component
(N
);
3624 -- If this is a spec without a body, check that generic parameters
3627 if not Body_Required
(Parent
(N
)) then
3628 Check_References
(Id
);
3632 -- If there is a specified storage pool in the context, create an
3633 -- aspect on the package declaration, so that it is used in any
3634 -- instance that does not override it.
3636 if Present
(Default_Pool
) then
3642 Make_Aspect_Specification
(Loc
,
3643 Identifier
=> Make_Identifier
(Loc
, Name_Default_Storage_Pool
),
3644 Expression
=> New_Copy
(Default_Pool
));
3646 if No
(Aspect_Specifications
(Specification
(N
))) then
3647 Set_Aspect_Specifications
(Specification
(N
), New_List
(ASN
));
3649 Append
(ASN
, Aspect_Specifications
(Specification
(N
)));
3653 end Analyze_Generic_Package_Declaration
;
3655 --------------------------------------------
3656 -- Analyze_Generic_Subprogram_Declaration --
3657 --------------------------------------------
3659 procedure Analyze_Generic_Subprogram_Declaration
(N
: Node_Id
) is
3663 Result_Type
: Entity_Id
;
3664 Save_Parent
: Node_Id
;
3669 Check_SPARK_05_Restriction
("generic is not allowed", N
);
3671 -- Create copy of generic unit, and save for instantiation. If the unit
3672 -- is a child unit, do not copy the specifications for the parent, which
3673 -- are not part of the generic tree.
3675 Save_Parent
:= Parent_Spec
(N
);
3676 Set_Parent_Spec
(N
, Empty
);
3678 New_N
:= Copy_Generic_Node
(N
, Empty
, Instantiating
=> False);
3679 Set_Parent_Spec
(New_N
, Save_Parent
);
3682 -- Once the contents of the generic copy and the template are swapped,
3683 -- do the same for their respective aspect specifications.
3685 Exchange_Aspects
(N
, New_N
);
3687 -- Collect all contract-related source pragmas found within the template
3688 -- and attach them to the contract of the subprogram spec. This contract
3689 -- is used in the capture of global references within annotations.
3691 Create_Generic_Contract
(N
);
3693 Spec
:= Specification
(N
);
3694 Id
:= Defining_Entity
(Spec
);
3695 Generate_Definition
(Id
);
3697 if Nkind
(Id
) = N_Defining_Operator_Symbol
then
3699 ("operator symbol not allowed for generic subprogram", Id
);
3705 Set_Scope_Depth_Value
(Id
, Scope_Depth
(Current_Scope
) + 1);
3707 -- Analyze the aspects of the generic copy to ensure that all generated
3708 -- pragmas (if any) perform their semantic effects.
3710 if Has_Aspects
(N
) then
3711 Analyze_Aspect_Specifications
(N
, Id
);
3715 Enter_Generic_Scope
(Id
);
3716 Set_Inner_Instances
(Id
, New_Elmt_List
);
3717 Set_Is_Pure
(Id
, Is_Pure
(Current_Scope
));
3719 Analyze_Generic_Formal_Part
(N
);
3721 if Nkind
(Spec
) = N_Function_Specification
then
3722 Set_Ekind
(Id
, E_Generic_Function
);
3724 Set_Ekind
(Id
, E_Generic_Procedure
);
3727 -- Set SPARK_Mode from context
3729 Set_SPARK_Pragma
(Id
, SPARK_Mode_Pragma
);
3730 Set_SPARK_Pragma_Inherited
(Id
);
3732 -- Preserve relevant elaboration-related attributes of the context which
3733 -- are no longer available or very expensive to recompute once analysis,
3734 -- resolution, and expansion are over.
3736 Mark_Elaboration_Attributes
3741 Formals
:= Parameter_Specifications
(Spec
);
3743 if Present
(Formals
) then
3744 Process_Formals
(Formals
, Spec
);
3747 if Nkind
(Spec
) = N_Function_Specification
then
3748 if Nkind
(Result_Definition
(Spec
)) = N_Access_Definition
then
3749 Result_Type
:= Access_Definition
(Spec
, Result_Definition
(Spec
));
3750 Set_Etype
(Id
, Result_Type
);
3752 -- Check restriction imposed by AI05-073: a generic function
3753 -- cannot return an abstract type or an access to such.
3755 -- This is a binding interpretation should it apply to earlier
3756 -- versions of Ada as well as Ada 2012???
3758 if Is_Abstract_Type
(Designated_Type
(Result_Type
))
3759 and then Ada_Version
>= Ada_2012
3762 ("generic function cannot have an access result "
3763 & "that designates an abstract type", Spec
);
3767 Find_Type
(Result_Definition
(Spec
));
3768 Typ
:= Entity
(Result_Definition
(Spec
));
3770 if Is_Abstract_Type
(Typ
)
3771 and then Ada_Version
>= Ada_2012
3774 ("generic function cannot have abstract result type", Spec
);
3777 -- If a null exclusion is imposed on the result type, then create
3778 -- a null-excluding itype (an access subtype) and use it as the
3779 -- function's Etype.
3781 if Is_Access_Type
(Typ
)
3782 and then Null_Exclusion_Present
(Spec
)
3785 Create_Null_Excluding_Itype
3787 Related_Nod
=> Spec
,
3788 Scope_Id
=> Defining_Unit_Name
(Spec
)));
3790 Set_Etype
(Id
, Typ
);
3795 Set_Etype
(Id
, Standard_Void_Type
);
3798 -- For a library unit, we have reconstructed the entity for the unit,
3799 -- and must reset it in the library tables. We also make sure that
3800 -- Body_Required is set properly in the original compilation unit node.
3802 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
3803 Set_Cunit_Entity
(Current_Sem_Unit
, Id
);
3804 Set_Body_Required
(Parent
(N
), Unit_Requires_Body
(Id
));
3807 -- If the generic appears within a package unit, the body of that unit
3808 -- has to be present for instantiation and inlining.
3810 if Nkind
(Unit
(Cunit
(Current_Sem_Unit
))) = N_Package_Declaration
3811 and then Unit_Requires_Body
(Id
)
3813 Set_Body_Needed_For_Inlining
3814 (Defining_Entity
(Unit
(Cunit
(Current_Sem_Unit
))));
3817 Set_Categorization_From_Pragmas
(N
);
3818 Validate_Categorization_Dependency
(N
, Id
);
3820 -- Capture all global references that occur within the profile of the
3821 -- generic subprogram. Aspects are not part of this processing because
3822 -- they must be delayed. If processed now, Save_Global_References will
3823 -- destroy the Associated_Node links and prevent the capture of global
3824 -- references when the contract of the generic subprogram is analyzed.
3826 Save_Global_References
(Original_Node
(N
));
3830 Exit_Generic_Scope
(Id
);
3831 Generate_Reference_To_Formals
(Id
);
3833 List_Inherited_Pre_Post_Aspects
(Id
);
3834 end Analyze_Generic_Subprogram_Declaration
;
3836 -----------------------------------
3837 -- Analyze_Package_Instantiation --
3838 -----------------------------------
3840 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
3841 -- must be replaced by gotos which jump to the end of the routine in order
3842 -- to restore the Ghost and SPARK modes.
3844 procedure Analyze_Package_Instantiation
(N
: Node_Id
) is
3845 Has_Inline_Always
: Boolean := False;
3847 procedure Delay_Descriptors
(E
: Entity_Id
);
3848 -- Delay generation of subprogram descriptors for given entity
3850 function Might_Inline_Subp
(Gen_Unit
: Entity_Id
) return Boolean;
3851 -- If inlining is active and the generic contains inlined subprograms,
3852 -- we instantiate the body. This may cause superfluous instantiations,
3853 -- but it is simpler than detecting the need for the body at the point
3854 -- of inlining, when the context of the instance is not available.
3856 -----------------------
3857 -- Delay_Descriptors --
3858 -----------------------
3860 procedure Delay_Descriptors
(E
: Entity_Id
) is
3862 if not Delay_Subprogram_Descriptors
(E
) then
3863 Set_Delay_Subprogram_Descriptors
(E
);
3864 Pending_Descriptor
.Append
(E
);
3866 end Delay_Descriptors
;
3868 -----------------------
3869 -- Might_Inline_Subp --
3870 -----------------------
3872 function Might_Inline_Subp
(Gen_Unit
: Entity_Id
) return Boolean is
3876 if not Inline_Processing_Required
then
3880 E
:= First_Entity
(Gen_Unit
);
3881 while Present
(E
) loop
3882 if Is_Subprogram
(E
) and then Is_Inlined
(E
) then
3883 -- Remember if there are any subprograms with Inline_Always
3885 if Has_Pragma_Inline_Always
(E
) then
3886 Has_Inline_Always
:= True;
3897 end Might_Inline_Subp
;
3899 -- Local declarations
3901 Gen_Id
: constant Node_Id
:= Name
(N
);
3902 Is_Actual_Pack
: constant Boolean :=
3903 Is_Internal
(Defining_Entity
(N
));
3904 Loc
: constant Source_Ptr
:= Sloc
(N
);
3906 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
3907 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
3908 Saved_ISMP
: constant Boolean :=
3909 Ignore_SPARK_Mode_Pragmas_In_Instance
;
3910 Saved_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
3911 Saved_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
3912 -- Save the Ghost and SPARK mode-related data to restore on exit
3914 Saved_Style_Check
: constant Boolean := Style_Check
;
3915 -- Save style check mode for restore on exit
3918 Act_Decl_Name
: Node_Id
;
3919 Act_Decl_Id
: Entity_Id
;
3922 Env_Installed
: Boolean := False;
3925 Gen_Unit
: Entity_Id
;
3926 Inline_Now
: Boolean := False;
3927 Needs_Body
: Boolean;
3928 Parent_Installed
: Boolean := False;
3929 Renaming_List
: List_Id
;
3930 Unit_Renaming
: Node_Id
;
3932 Vis_Prims_List
: Elist_Id
:= No_Elist
;
3933 -- List of primitives made temporarily visible in the instantiation
3934 -- to match the visibility of the formal type
3936 -- Start of processing for Analyze_Package_Instantiation
3939 -- Preserve relevant elaboration-related attributes of the context which
3940 -- are no longer available or very expensive to recompute once analysis,
3941 -- resolution, and expansion are over.
3943 Mark_Elaboration_Attributes
3950 Check_SPARK_05_Restriction
("generic is not allowed", N
);
3952 -- Very first thing: check for Text_IO special unit in case we are
3953 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3955 Check_Text_IO_Special_Unit
(Name
(N
));
3957 -- Make node global for error reporting
3959 Instantiation_Node
:= N
;
3961 -- Case of instantiation of a generic package
3963 if Nkind
(N
) = N_Package_Instantiation
then
3964 Act_Decl_Id
:= New_Copy
(Defining_Entity
(N
));
3965 Set_Comes_From_Source
(Act_Decl_Id
, True);
3967 if Nkind
(Defining_Unit_Name
(N
)) = N_Defining_Program_Unit_Name
then
3969 Make_Defining_Program_Unit_Name
(Loc
,
3971 New_Copy_Tree
(Name
(Defining_Unit_Name
(N
))),
3972 Defining_Identifier
=> Act_Decl_Id
);
3974 Act_Decl_Name
:= Act_Decl_Id
;
3977 -- Case of instantiation of a formal package
3980 Act_Decl_Id
:= Defining_Identifier
(N
);
3981 Act_Decl_Name
:= Act_Decl_Id
;
3984 Generate_Definition
(Act_Decl_Id
);
3985 Set_Ekind
(Act_Decl_Id
, E_Package
);
3987 -- Initialize list of incomplete actuals before analysis
3989 Set_Incomplete_Actuals
(Act_Decl_Id
, New_Elmt_List
);
3991 Preanalyze_Actuals
(N
, Act_Decl_Id
);
3993 -- Turn off style checking in instances. If the check is enabled on the
3994 -- generic unit, a warning in an instance would just be noise. If not
3995 -- enabled on the generic, then a warning in an instance is just wrong.
3996 -- This must be done after analyzing the actuals, which do come from
3997 -- source and are subject to style checking.
3999 Style_Check
:= False;
4002 Env_Installed
:= True;
4004 -- Reset renaming map for formal types. The mapping is established
4005 -- when analyzing the generic associations, but some mappings are
4006 -- inherited from formal packages of parent units, and these are
4007 -- constructed when the parents are installed.
4009 Generic_Renamings
.Set_Last
(0);
4010 Generic_Renamings_HTable
.Reset
;
4012 Check_Generic_Child_Unit
(Gen_Id
, Parent_Installed
);
4013 Gen_Unit
:= Entity
(Gen_Id
);
4015 -- A package instantiation is Ghost when it is subject to pragma Ghost
4016 -- or the generic template is Ghost. Set the mode now to ensure that
4017 -- any nodes generated during analysis and expansion are marked as
4020 Mark_And_Set_Ghost_Instantiation
(N
, Gen_Unit
);
4022 -- Verify that it is the name of a generic package
4024 -- A visibility glitch: if the instance is a child unit and the generic
4025 -- is the generic unit of a parent instance (i.e. both the parent and
4026 -- the child units are instances of the same package) the name now
4027 -- denotes the renaming within the parent, not the intended generic
4028 -- unit. See if there is a homonym that is the desired generic. The
4029 -- renaming declaration must be visible inside the instance of the
4030 -- child, but not when analyzing the name in the instantiation itself.
4032 if Ekind
(Gen_Unit
) = E_Package
4033 and then Present
(Renamed_Entity
(Gen_Unit
))
4034 and then In_Open_Scopes
(Renamed_Entity
(Gen_Unit
))
4035 and then Is_Generic_Instance
(Renamed_Entity
(Gen_Unit
))
4036 and then Present
(Homonym
(Gen_Unit
))
4038 Gen_Unit
:= Homonym
(Gen_Unit
);
4041 if Etype
(Gen_Unit
) = Any_Type
then
4045 elsif Ekind
(Gen_Unit
) /= E_Generic_Package
then
4047 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4049 if From_Limited_With
(Gen_Unit
) then
4051 ("cannot instantiate a limited withed package", Gen_Id
);
4054 ("& is not the name of a generic package", Gen_Id
, Gen_Unit
);
4061 if In_Extended_Main_Source_Unit
(N
) then
4062 Set_Is_Instantiated
(Gen_Unit
);
4063 Generate_Reference
(Gen_Unit
, N
);
4065 if Present
(Renamed_Object
(Gen_Unit
)) then
4066 Set_Is_Instantiated
(Renamed_Object
(Gen_Unit
));
4067 Generate_Reference
(Renamed_Object
(Gen_Unit
), N
);
4071 if Nkind
(Gen_Id
) = N_Identifier
4072 and then Chars
(Gen_Unit
) = Chars
(Defining_Entity
(N
))
4075 ("& is hidden within declaration of instance", Gen_Id
, Gen_Unit
);
4077 elsif Nkind
(Gen_Id
) = N_Expanded_Name
4078 and then Is_Child_Unit
(Gen_Unit
)
4079 and then Nkind
(Prefix
(Gen_Id
)) = N_Identifier
4080 and then Chars
(Act_Decl_Id
) = Chars
(Prefix
(Gen_Id
))
4083 ("& is hidden within declaration of instance ", Prefix
(Gen_Id
));
4086 Set_Entity
(Gen_Id
, Gen_Unit
);
4088 -- If generic is a renaming, get original generic unit
4090 if Present
(Renamed_Object
(Gen_Unit
))
4091 and then Ekind
(Renamed_Object
(Gen_Unit
)) = E_Generic_Package
4093 Gen_Unit
:= Renamed_Object
(Gen_Unit
);
4096 -- Verify that there are no circular instantiations
4098 if In_Open_Scopes
(Gen_Unit
) then
4099 Error_Msg_NE
("instantiation of & within itself", N
, Gen_Unit
);
4103 elsif Contains_Instance_Of
(Gen_Unit
, Current_Scope
, Gen_Id
) then
4104 Error_Msg_Node_2
:= Current_Scope
;
4106 ("circular Instantiation: & instantiated in &!", N
, Gen_Unit
);
4107 Circularity_Detected
:= True;
4112 -- If the context of the instance is subject to SPARK_Mode "off" or
4113 -- the annotation is altogether missing, set the global flag which
4114 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4117 if SPARK_Mode
/= On
then
4118 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
4120 -- Mark the instance spec in case the body is instantiated at a
4121 -- later pass. This preserves the original context in effect for
4124 Set_Ignore_SPARK_Mode_Pragmas
(Act_Decl_Id
);
4127 Gen_Decl
:= Unit_Declaration_Node
(Gen_Unit
);
4128 Gen_Spec
:= Specification
(Gen_Decl
);
4130 -- Initialize renamings map, for error checking, and the list that
4131 -- holds private entities whose views have changed between generic
4132 -- definition and instantiation. If this is the instance created to
4133 -- validate an actual package, the instantiation environment is that
4134 -- of the enclosing instance.
4136 Create_Instantiation_Source
(N
, Gen_Unit
, S_Adjustment
);
4138 -- Copy original generic tree, to produce text for instantiation
4142 (Original_Node
(Gen_Decl
), Empty
, Instantiating
=> True);
4144 Act_Spec
:= Specification
(Act_Tree
);
4146 -- If this is the instance created to validate an actual package,
4147 -- only the formals matter, do not examine the package spec itself.
4149 if Is_Actual_Pack
then
4150 Set_Visible_Declarations
(Act_Spec
, New_List
);
4151 Set_Private_Declarations
(Act_Spec
, New_List
);
4155 Analyze_Associations
4157 Formals
=> Generic_Formal_Declarations
(Act_Tree
),
4158 F_Copy
=> Generic_Formal_Declarations
(Gen_Decl
));
4160 Vis_Prims_List
:= Check_Hidden_Primitives
(Renaming_List
);
4162 Set_Instance_Env
(Gen_Unit
, Act_Decl_Id
);
4163 Set_Defining_Unit_Name
(Act_Spec
, Act_Decl_Name
);
4164 Set_Is_Generic_Instance
(Act_Decl_Id
);
4165 Set_Generic_Parent
(Act_Spec
, Gen_Unit
);
4167 -- References to the generic in its own declaration or its body are
4168 -- references to the instance. Add a renaming declaration for the
4169 -- generic unit itself. This declaration, as well as the renaming
4170 -- declarations for the generic formals, must remain private to the
4171 -- unit: the formals, because this is the language semantics, and
4172 -- the unit because its use is an artifact of the implementation.
4175 Make_Package_Renaming_Declaration
(Loc
,
4176 Defining_Unit_Name
=>
4177 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
)),
4178 Name
=> New_Occurrence_Of
(Act_Decl_Id
, Loc
));
4180 Append
(Unit_Renaming
, Renaming_List
);
4182 -- The renaming declarations are the first local declarations of the
4185 if Is_Non_Empty_List
(Visible_Declarations
(Act_Spec
)) then
4187 (First
(Visible_Declarations
(Act_Spec
)), Renaming_List
);
4189 Set_Visible_Declarations
(Act_Spec
, Renaming_List
);
4192 Act_Decl
:= Make_Package_Declaration
(Loc
, Specification
=> Act_Spec
);
4194 -- Propagate the aspect specifications from the package declaration
4195 -- template to the instantiated version of the package declaration.
4197 if Has_Aspects
(Act_Tree
) then
4198 Set_Aspect_Specifications
(Act_Decl
,
4199 New_Copy_List_Tree
(Aspect_Specifications
(Act_Tree
)));
4202 -- The generic may have a generated Default_Storage_Pool aspect,
4203 -- set at the point of generic declaration. If the instance has
4204 -- that aspect, it overrides the one inherited from the generic.
4206 if Has_Aspects
(Gen_Spec
) then
4207 if No
(Aspect_Specifications
(N
)) then
4208 Set_Aspect_Specifications
(N
,
4210 (Aspect_Specifications
(Gen_Spec
))));
4214 Inherited_Aspects
: constant List_Id
:=
4216 (Aspect_Specifications
(Gen_Spec
));
4220 Pool_Present
: Boolean := False;
4223 ASN1
:= First
(Aspect_Specifications
(N
));
4224 while Present
(ASN1
) loop
4225 if Chars
(Identifier
(ASN1
)) =
4226 Name_Default_Storage_Pool
4228 Pool_Present
:= True;
4235 if Pool_Present
then
4237 -- If generic carries a default storage pool, remove it
4238 -- in favor of the instance one.
4240 ASN2
:= First
(Inherited_Aspects
);
4241 while Present
(ASN2
) loop
4242 if Chars
(Identifier
(ASN2
)) =
4243 Name_Default_Storage_Pool
4254 (Aspect_Specifications
(N
), Inherited_Aspects
);
4259 -- Save the instantiation node, for subsequent instantiation of the
4260 -- body, if there is one and we are generating code for the current
4261 -- unit. Mark unit as having a body (avoids premature error message).
4263 -- We instantiate the body if we are generating code, if we are
4264 -- generating cross-reference information, or if we are building
4265 -- trees for ASIS use or GNATprove use.
4268 Enclosing_Body_Present
: Boolean := False;
4269 -- If the generic unit is not a compilation unit, then a body may
4270 -- be present in its parent even if none is required. We create a
4271 -- tentative pending instantiation for the body, which will be
4272 -- discarded if none is actually present.
4277 if Scope
(Gen_Unit
) /= Standard_Standard
4278 and then not Is_Child_Unit
(Gen_Unit
)
4280 Scop
:= Scope
(Gen_Unit
);
4281 while Present
(Scop
) and then Scop
/= Standard_Standard
loop
4282 if Unit_Requires_Body
(Scop
) then
4283 Enclosing_Body_Present
:= True;
4286 elsif In_Open_Scopes
(Scop
)
4287 and then In_Package_Body
(Scop
)
4289 Enclosing_Body_Present
:= True;
4293 exit when Is_Compilation_Unit
(Scop
);
4294 Scop
:= Scope
(Scop
);
4298 -- If front-end inlining is enabled or there are any subprograms
4299 -- marked with Inline_Always, and this is a unit for which code
4300 -- will be generated, we instantiate the body at once.
4302 -- This is done if the instance is not the main unit, and if the
4303 -- generic is not a child unit of another generic, to avoid scope
4304 -- problems and the reinstallation of parent instances.
4307 and then (not Is_Child_Unit
(Gen_Unit
)
4308 or else not Is_Generic_Unit
(Scope
(Gen_Unit
)))
4309 and then Might_Inline_Subp
(Gen_Unit
)
4310 and then not Is_Actual_Pack
4312 if not Back_End_Inlining
4313 and then (Front_End_Inlining
or else Has_Inline_Always
)
4314 and then (Is_In_Main_Unit
(N
)
4315 or else In_Main_Context
(Current_Scope
))
4316 and then Nkind
(Parent
(N
)) /= N_Compilation_Unit
4320 -- In configurable_run_time mode we force the inlining of
4321 -- predefined subprograms marked Inline_Always, to minimize
4322 -- the use of the run-time library.
4324 elsif In_Predefined_Unit
(Gen_Decl
)
4325 and then Configurable_Run_Time_Mode
4326 and then Nkind
(Parent
(N
)) /= N_Compilation_Unit
4331 -- If the current scope is itself an instance within a child
4332 -- unit, there will be duplications in the scope stack, and the
4333 -- unstacking mechanism in Inline_Instance_Body will fail.
4334 -- This loses some rare cases of optimization, and might be
4335 -- improved some day, if we can find a proper abstraction for
4336 -- "the complete compilation context" that can be saved and
4339 if Is_Generic_Instance
(Current_Scope
) then
4341 Curr_Unit
: constant Entity_Id
:=
4342 Cunit_Entity
(Current_Sem_Unit
);
4344 if Curr_Unit
/= Current_Scope
4345 and then Is_Child_Unit
(Curr_Unit
)
4347 Inline_Now
:= False;
4354 (Unit_Requires_Body
(Gen_Unit
)
4355 or else Enclosing_Body_Present
4356 or else Present
(Corresponding_Body
(Gen_Decl
)))
4357 and then (Is_In_Main_Unit
(N
)
4358 or else Might_Inline_Subp
(Gen_Unit
))
4359 and then not Is_Actual_Pack
4360 and then not Inline_Now
4361 and then (Operating_Mode
= Generate_Code
4363 -- Need comment for this check ???
4365 or else (Operating_Mode
= Check_Semantics
4366 and then (ASIS_Mode
or GNATprove_Mode
)));
4368 -- If front-end inlining is enabled or there are any subprograms
4369 -- marked with Inline_Always, do not instantiate body when within
4370 -- a generic context.
4372 if ((Front_End_Inlining
or else Has_Inline_Always
)
4373 and then not Expander_Active
)
4374 or else Is_Generic_Unit
(Cunit_Entity
(Main_Unit
))
4376 Needs_Body
:= False;
4379 -- If the current context is generic, and the package being
4380 -- instantiated is declared within a formal package, there is no
4381 -- body to instantiate until the enclosing generic is instantiated
4382 -- and there is an actual for the formal package. If the formal
4383 -- package has parameters, we build a regular package instance for
4384 -- it, that precedes the original formal package declaration.
4386 if In_Open_Scopes
(Scope
(Scope
(Gen_Unit
))) then
4388 Decl
: constant Node_Id
:=
4390 (Unit_Declaration_Node
(Scope
(Gen_Unit
)));
4392 if Nkind
(Decl
) = N_Formal_Package_Declaration
4393 or else (Nkind
(Decl
) = N_Package_Declaration
4394 and then Is_List_Member
(Decl
)
4395 and then Present
(Next
(Decl
))
4397 Nkind
(Next
(Decl
)) =
4398 N_Formal_Package_Declaration
)
4400 Needs_Body
:= False;
4406 -- For RCI unit calling stubs, we omit the instance body if the
4407 -- instance is the RCI library unit itself.
4409 -- However there is a special case for nested instances: in this case
4410 -- we do generate the instance body, as it might be required, e.g.
4411 -- because it provides stream attributes for some type used in the
4412 -- profile of a remote subprogram. This is consistent with 12.3(12),
4413 -- which indicates that the instance body occurs at the place of the
4414 -- instantiation, and thus is part of the RCI declaration, which is
4415 -- present on all client partitions (this is E.2.3(18)).
4417 -- Note that AI12-0002 may make it illegal at some point to have
4418 -- stream attributes defined in an RCI unit, in which case this
4419 -- special case will become unnecessary. In the meantime, there
4420 -- is known application code in production that depends on this
4421 -- being possible, so we definitely cannot eliminate the body in
4422 -- the case of nested instances for the time being.
4424 -- When we generate a nested instance body, calling stubs for any
4425 -- relevant subprogram will be be inserted immediately after the
4426 -- subprogram declarations, and will take precedence over the
4427 -- subsequent (original) body. (The stub and original body will be
4428 -- complete homographs, but this is permitted in an instance).
4429 -- (Could we do better and remove the original body???)
4431 if Distribution_Stub_Mode
= Generate_Caller_Stub_Body
4432 and then Comes_From_Source
(N
)
4433 and then Nkind
(Parent
(N
)) = N_Compilation_Unit
4435 Needs_Body
:= False;
4440 -- Here is a defence against a ludicrous number of instantiations
4441 -- caused by a circular set of instantiation attempts.
4443 if Pending_Instantiations
.Last
> Maximum_Instantiations
then
4444 Error_Msg_Uint_1
:= UI_From_Int
(Maximum_Instantiations
);
4445 Error_Msg_N
("too many instantiations, exceeds max of^", N
);
4446 Error_Msg_N
("\limit can be changed using -gnateinn switch", N
);
4447 raise Unrecoverable_Error
;
4450 -- Indicate that the enclosing scopes contain an instantiation,
4451 -- and that cleanup actions should be delayed until after the
4452 -- instance body is expanded.
4454 Check_Forward_Instantiation
(Gen_Decl
);
4455 if Nkind
(N
) = N_Package_Instantiation
then
4457 Enclosing_Master
: Entity_Id
;
4460 -- Loop to search enclosing masters
4462 Enclosing_Master
:= Current_Scope
;
4463 Scope_Loop
: while Enclosing_Master
/= Standard_Standard
loop
4464 if Ekind
(Enclosing_Master
) = E_Package
then
4465 if Is_Compilation_Unit
(Enclosing_Master
) then
4466 if In_Package_Body
(Enclosing_Master
) then
4468 (Body_Entity
(Enclosing_Master
));
4477 Enclosing_Master
:= Scope
(Enclosing_Master
);
4480 elsif Is_Generic_Unit
(Enclosing_Master
)
4481 or else Ekind
(Enclosing_Master
) = E_Void
4483 -- Cleanup actions will eventually be performed on the
4484 -- enclosing subprogram or package instance, if any.
4485 -- Enclosing scope is void in the formal part of a
4486 -- generic subprogram.
4491 if Ekind
(Enclosing_Master
) = E_Entry
4493 Ekind
(Scope
(Enclosing_Master
)) = E_Protected_Type
4495 if not Expander_Active
then
4499 Protected_Body_Subprogram
(Enclosing_Master
);
4503 Set_Delay_Cleanups
(Enclosing_Master
);
4505 while Ekind
(Enclosing_Master
) = E_Block
loop
4506 Enclosing_Master
:= Scope
(Enclosing_Master
);
4509 if Is_Subprogram
(Enclosing_Master
) then
4510 Delay_Descriptors
(Enclosing_Master
);
4512 elsif Is_Task_Type
(Enclosing_Master
) then
4514 TBP
: constant Node_Id
:=
4515 Get_Task_Body_Procedure
4518 if Present
(TBP
) then
4519 Delay_Descriptors
(TBP
);
4520 Set_Delay_Cleanups
(TBP
);
4527 end loop Scope_Loop
;
4530 -- Make entry in table
4532 Add_Pending_Instantiation
(N
, Act_Decl
);
4536 Set_Categorization_From_Pragmas
(Act_Decl
);
4538 if Parent_Installed
then
4542 Set_Instance_Spec
(N
, Act_Decl
);
4544 -- If not a compilation unit, insert the package declaration before
4545 -- the original instantiation node.
4547 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
4548 Mark_Rewrite_Insertion
(Act_Decl
);
4549 Insert_Before
(N
, Act_Decl
);
4551 if Has_Aspects
(N
) then
4552 Analyze_Aspect_Specifications
(N
, Act_Decl_Id
);
4554 -- The pragma created for a Default_Storage_Pool aspect must
4555 -- appear ahead of the declarations in the instance spec.
4556 -- Analysis has placed it after the instance node, so remove
4557 -- it and reinsert it properly now.
4560 ASN
: constant Node_Id
:= First
(Aspect_Specifications
(N
));
4561 A_Name
: constant Name_Id
:= Chars
(Identifier
(ASN
));
4565 if A_Name
= Name_Default_Storage_Pool
then
4566 if No
(Visible_Declarations
(Act_Spec
)) then
4567 Set_Visible_Declarations
(Act_Spec
, New_List
);
4571 while Present
(Decl
) loop
4572 if Nkind
(Decl
) = N_Pragma
then
4574 Prepend
(Decl
, Visible_Declarations
(Act_Spec
));
4586 -- For an instantiation that is a compilation unit, place
4587 -- declaration on current node so context is complete for analysis
4588 -- (including nested instantiations). If this is the main unit,
4589 -- the declaration eventually replaces the instantiation node.
4590 -- If the instance body is created later, it replaces the
4591 -- instance node, and the declaration is attached to it
4592 -- (see Build_Instance_Compilation_Unit_Nodes).
4595 if Cunit_Entity
(Current_Sem_Unit
) = Defining_Entity
(N
) then
4597 -- The entity for the current unit is the newly created one,
4598 -- and all semantic information is attached to it.
4600 Set_Cunit_Entity
(Current_Sem_Unit
, Act_Decl_Id
);
4602 -- If this is the main unit, replace the main entity as well
4604 if Current_Sem_Unit
= Main_Unit
then
4605 Main_Unit_Entity
:= Act_Decl_Id
;
4609 Set_Unit
(Parent
(N
), Act_Decl
);
4610 Set_Parent_Spec
(Act_Decl
, Parent_Spec
(N
));
4611 Set_Package_Instantiation
(Act_Decl_Id
, N
);
4613 -- Process aspect specifications of the instance node, if any, to
4614 -- take into account categorization pragmas before analyzing the
4617 if Has_Aspects
(N
) then
4618 Analyze_Aspect_Specifications
(N
, Act_Decl_Id
);
4622 Set_Unit
(Parent
(N
), N
);
4623 Set_Body_Required
(Parent
(N
), False);
4625 -- We never need elaboration checks on instantiations, since by
4626 -- definition, the body instantiation is elaborated at the same
4627 -- time as the spec instantiation.
4629 if Legacy_Elaboration_Checks
then
4630 Set_Kill_Elaboration_Checks
(Act_Decl_Id
);
4631 Set_Suppress_Elaboration_Warnings
(Act_Decl_Id
);
4635 if Legacy_Elaboration_Checks
then
4636 Check_Elab_Instantiation
(N
);
4639 -- Save the scenario for later examination by the ABE Processing
4642 Record_Elaboration_Scenario
(N
);
4644 -- The instantiation results in a guaranteed ABE
4646 if Is_Known_Guaranteed_ABE
(N
) and then Needs_Body
then
4648 -- Do not instantiate the corresponding body because gigi cannot
4649 -- handle certain types of premature instantiations.
4651 Pending_Instantiations
.Decrement_Last
;
4653 -- Create completing bodies for all subprogram declarations since
4654 -- their real bodies will not be instantiated.
4656 Provide_Completing_Bodies
(Instance_Spec
(N
));
4659 Check_Hidden_Child_Unit
(N
, Gen_Unit
, Act_Decl_Id
);
4661 Set_First_Private_Entity
(Defining_Unit_Name
(Unit_Renaming
),
4662 First_Private_Entity
(Act_Decl_Id
));
4664 -- If the instantiation will receive a body, the unit will be
4665 -- transformed into a package body, and receive its own elaboration
4666 -- entity. Otherwise, the nature of the unit is now a package
4669 if Nkind
(Parent
(N
)) = N_Compilation_Unit
4670 and then not Needs_Body
4672 Rewrite
(N
, Act_Decl
);
4675 if Present
(Corresponding_Body
(Gen_Decl
))
4676 or else Unit_Requires_Body
(Gen_Unit
)
4678 Set_Has_Completion
(Act_Decl_Id
);
4681 Check_Formal_Packages
(Act_Decl_Id
);
4683 Restore_Hidden_Primitives
(Vis_Prims_List
);
4684 Restore_Private_Views
(Act_Decl_Id
);
4686 Inherit_Context
(Gen_Decl
, N
);
4688 if Parent_Installed
then
4693 Env_Installed
:= False;
4696 Validate_Categorization_Dependency
(N
, Act_Decl_Id
);
4698 -- There used to be a check here to prevent instantiations in local
4699 -- contexts if the No_Local_Allocators restriction was active. This
4700 -- check was removed by a binding interpretation in AI-95-00130/07,
4701 -- but we retain the code for documentation purposes.
4703 -- if Ekind (Act_Decl_Id) /= E_Void
4704 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4706 -- Check_Restriction (No_Local_Allocators, N);
4710 Inline_Instance_Body
(N
, Gen_Unit
, Act_Decl
);
4713 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4714 -- be used as defining identifiers for a formal package and for the
4715 -- corresponding expanded package.
4717 if Nkind
(N
) = N_Formal_Package_Declaration
then
4718 Act_Decl_Id
:= New_Copy
(Defining_Entity
(N
));
4719 Set_Comes_From_Source
(Act_Decl_Id
, True);
4720 Set_Is_Generic_Instance
(Act_Decl_Id
, False);
4721 Set_Defining_Identifier
(N
, Act_Decl_Id
);
4724 -- Check that if N is an instantiation of System.Dim_Float_IO or
4725 -- System.Dim_Integer_IO, the formal type has a dimension system.
4727 if Nkind
(N
) = N_Package_Instantiation
4728 and then Is_Dim_IO_Package_Instantiation
(N
)
4731 Assoc
: constant Node_Id
:= First
(Generic_Associations
(N
));
4733 if not Has_Dimension_System
4734 (Etype
(Explicit_Generic_Actual_Parameter
(Assoc
)))
4736 Error_Msg_N
("type with a dimension system expected", Assoc
);
4742 if Has_Aspects
(N
) and then Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
4743 Analyze_Aspect_Specifications
(N
, Act_Decl_Id
);
4746 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
4747 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
4748 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
4749 Style_Check
:= Saved_Style_Check
;
4752 when Instantiation_Error
=>
4753 if Parent_Installed
then
4757 if Env_Installed
then
4761 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
4762 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
4763 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
4764 Style_Check
:= Saved_Style_Check
;
4765 end Analyze_Package_Instantiation
;
4767 --------------------------
4768 -- Inline_Instance_Body --
4769 --------------------------
4771 -- WARNING: This routine manages SPARK regions. Return statements must be
4772 -- replaced by gotos which jump to the end of the routine and restore the
4775 procedure Inline_Instance_Body
4777 Gen_Unit
: Entity_Id
;
4780 Config_Attrs
: constant Config_Switches_Type
:= Save_Config_Switches
;
4782 Curr_Comp
: constant Node_Id
:= Cunit
(Current_Sem_Unit
);
4783 Curr_Unit
: constant Entity_Id
:= Cunit_Entity
(Current_Sem_Unit
);
4784 Gen_Comp
: constant Entity_Id
:=
4785 Cunit_Entity
(Get_Source_Unit
(Gen_Unit
));
4787 Scope_Stack_Depth
: constant Pos
:=
4788 Scope_Stack
.Last
- Scope_Stack
.First
+ 1;
4790 Inner_Scopes
: array (1 .. Scope_Stack_Depth
) of Entity_Id
;
4791 Instances
: array (1 .. Scope_Stack_Depth
) of Entity_Id
;
4792 Use_Clauses
: array (1 .. Scope_Stack_Depth
) of Node_Id
;
4794 Curr_Scope
: Entity_Id
:= Empty
;
4795 List
: Elist_Id
:= No_Elist
; -- init to avoid warning
4796 N_Instances
: Nat
:= 0;
4797 Num_Inner
: Nat
:= 0;
4798 Num_Scopes
: Nat
:= 0;
4799 Removed
: Boolean := False;
4804 -- Case of generic unit defined in another unit. We must remove the
4805 -- complete context of the current unit to install that of the generic.
4807 if Gen_Comp
/= Cunit_Entity
(Current_Sem_Unit
) then
4809 -- Add some comments for the following two loops ???
4812 while Present
(S
) and then S
/= Standard_Standard
loop
4814 Num_Scopes
:= Num_Scopes
+ 1;
4816 Use_Clauses
(Num_Scopes
) :=
4818 (Scope_Stack
.Last
- Num_Scopes
+ 1).
4820 End_Use_Clauses
(Use_Clauses
(Num_Scopes
));
4822 exit when Scope_Stack
.Last
- Num_Scopes
+ 1 = Scope_Stack
.First
4823 or else Scope_Stack
.Table
4824 (Scope_Stack
.Last
- Num_Scopes
).Entity
= Scope
(S
);
4827 exit when Is_Generic_Instance
(S
)
4828 and then (In_Package_Body
(S
)
4829 or else Ekind
(S
) = E_Procedure
4830 or else Ekind
(S
) = E_Function
);
4834 Vis
:= Is_Immediately_Visible
(Gen_Comp
);
4836 -- Find and save all enclosing instances
4841 and then S
/= Standard_Standard
4843 if Is_Generic_Instance
(S
) then
4844 N_Instances
:= N_Instances
+ 1;
4845 Instances
(N_Instances
) := S
;
4847 exit when In_Package_Body
(S
);
4853 -- Remove context of current compilation unit, unless we are within a
4854 -- nested package instantiation, in which case the context has been
4855 -- removed previously.
4857 -- If current scope is the body of a child unit, remove context of
4858 -- spec as well. If an enclosing scope is an instance body, the
4859 -- context has already been removed, but the entities in the body
4860 -- must be made invisible as well.
4863 while Present
(S
) and then S
/= Standard_Standard
loop
4864 if Is_Generic_Instance
(S
)
4865 and then (In_Package_Body
(S
)
4866 or else Ekind_In
(S
, E_Procedure
, E_Function
))
4868 -- We still have to remove the entities of the enclosing
4869 -- instance from direct visibility.
4874 E
:= First_Entity
(S
);
4875 while Present
(E
) loop
4876 Set_Is_Immediately_Visible
(E
, False);
4885 or else (Ekind
(Curr_Unit
) = E_Package_Body
4886 and then S
= Spec_Entity
(Curr_Unit
))
4887 or else (Ekind
(Curr_Unit
) = E_Subprogram_Body
4888 and then S
= Corresponding_Spec
4889 (Unit_Declaration_Node
(Curr_Unit
)))
4893 -- Remove entities in current scopes from visibility, so that
4894 -- instance body is compiled in a clean environment.
4896 List
:= Save_Scope_Stack
(Handle_Use
=> False);
4898 if Is_Child_Unit
(S
) then
4900 -- Remove child unit from stack, as well as inner scopes.
4901 -- Removing the context of a child unit removes parent units
4904 while Current_Scope
/= S
loop
4905 Num_Inner
:= Num_Inner
+ 1;
4906 Inner_Scopes
(Num_Inner
) := Current_Scope
;
4911 Remove_Context
(Curr_Comp
);
4915 Remove_Context
(Curr_Comp
);
4918 if Ekind
(Curr_Unit
) = E_Package_Body
then
4919 Remove_Context
(Library_Unit
(Curr_Comp
));
4926 pragma Assert
(Num_Inner
< Num_Scopes
);
4928 Push_Scope
(Standard_Standard
);
4929 Scope_Stack
.Table
(Scope_Stack
.Last
).Is_Active_Stack_Base
:= True;
4931 -- The inlined package body is analyzed with the configuration state
4932 -- of the context prior to the scope manipulations performed above.
4934 -- ??? shouldn't this also use the warning state of the context prior
4935 -- to the scope manipulations?
4937 Instantiate_Package_Body
4939 ((Act_Decl
=> Act_Decl
,
4940 Config_Switches
=> Config_Attrs
,
4941 Current_Sem_Unit
=> Current_Sem_Unit
,
4942 Expander_Status
=> Expander_Active
,
4944 Local_Suppress_Stack_Top
=> Local_Suppress_Stack_Top
,
4945 Scope_Suppress
=> Scope_Suppress
,
4946 Warnings
=> Save_Warnings
)),
4947 Inlined_Body
=> True);
4953 Set_Is_Immediately_Visible
(Gen_Comp
, Vis
);
4955 -- Reset Generic_Instance flag so that use clauses can be installed
4956 -- in the proper order. (See Use_One_Package for effect of enclosing
4957 -- instances on processing of use clauses).
4959 for J
in 1 .. N_Instances
loop
4960 Set_Is_Generic_Instance
(Instances
(J
), False);
4964 Install_Context
(Curr_Comp
, Chain
=> False);
4966 if Present
(Curr_Scope
)
4967 and then Is_Child_Unit
(Curr_Scope
)
4969 Push_Scope
(Curr_Scope
);
4970 Set_Is_Immediately_Visible
(Curr_Scope
);
4972 -- Finally, restore inner scopes as well
4974 for J
in reverse 1 .. Num_Inner
loop
4975 Push_Scope
(Inner_Scopes
(J
));
4979 Restore_Scope_Stack
(List
, Handle_Use
=> False);
4981 if Present
(Curr_Scope
)
4983 (In_Private_Part
(Curr_Scope
)
4984 or else In_Package_Body
(Curr_Scope
))
4986 -- Install private declaration of ancestor units, which are
4987 -- currently available. Restore_Scope_Stack and Install_Context
4988 -- only install the visible part of parents.
4993 Par
:= Scope
(Curr_Scope
);
4994 while (Present
(Par
)) and then Par
/= Standard_Standard
loop
4995 Install_Private_Declarations
(Par
);
5002 -- Restore use clauses. For a child unit, use clauses in the parents
5003 -- are restored when installing the context, so only those in inner
5004 -- scopes (and those local to the child unit itself) need to be
5005 -- installed explicitly.
5007 if Is_Child_Unit
(Curr_Unit
) and then Removed
then
5008 for J
in reverse 1 .. Num_Inner
+ 1 loop
5009 Scope_Stack
.Table
(Scope_Stack
.Last
- J
+ 1).First_Use_Clause
:=
5011 Install_Use_Clauses
(Use_Clauses
(J
));
5015 for J
in reverse 1 .. Num_Scopes
loop
5016 Scope_Stack
.Table
(Scope_Stack
.Last
- J
+ 1).First_Use_Clause
:=
5018 Install_Use_Clauses
(Use_Clauses
(J
));
5022 -- Restore status of instances. If one of them is a body, make its
5023 -- local entities visible again.
5030 for J
in 1 .. N_Instances
loop
5031 Inst
:= Instances
(J
);
5032 Set_Is_Generic_Instance
(Inst
, True);
5034 if In_Package_Body
(Inst
)
5035 or else Ekind_In
(S
, E_Procedure
, E_Function
)
5037 E
:= First_Entity
(Instances
(J
));
5038 while Present
(E
) loop
5039 Set_Is_Immediately_Visible
(E
);
5046 -- If generic unit is in current unit, current context is correct. Note
5047 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5048 -- enclosing scopes were removed.
5051 Instantiate_Package_Body
5053 ((Act_Decl
=> Act_Decl
,
5054 Config_Switches
=> Save_Config_Switches
,
5055 Current_Sem_Unit
=> Current_Sem_Unit
,
5056 Expander_Status
=> Expander_Active
,
5058 Local_Suppress_Stack_Top
=> Local_Suppress_Stack_Top
,
5059 Scope_Suppress
=> Scope_Suppress
,
5060 Warnings
=> Save_Warnings
)),
5061 Inlined_Body
=> True);
5063 end Inline_Instance_Body
;
5065 -------------------------------------
5066 -- Analyze_Procedure_Instantiation --
5067 -------------------------------------
5069 procedure Analyze_Procedure_Instantiation
(N
: Node_Id
) is
5071 Analyze_Subprogram_Instantiation
(N
, E_Procedure
);
5072 end Analyze_Procedure_Instantiation
;
5074 -----------------------------------
5075 -- Need_Subprogram_Instance_Body --
5076 -----------------------------------
5078 function Need_Subprogram_Instance_Body
5080 Subp
: Entity_Id
) return Boolean
5082 function Is_Inlined_Or_Child_Of_Inlined
(E
: Entity_Id
) return Boolean;
5083 -- Return True if E is an inlined subprogram, an inlined renaming or a
5084 -- subprogram nested in an inlined subprogram. The inlining machinery
5085 -- totally disregards nested subprograms since it considers that they
5086 -- will always be compiled if the parent is (see Inline.Is_Nested).
5088 ------------------------------------
5089 -- Is_Inlined_Or_Child_Of_Inlined --
5090 ------------------------------------
5092 function Is_Inlined_Or_Child_Of_Inlined
(E
: Entity_Id
) return Boolean is
5096 if Is_Inlined
(E
) or else Is_Inlined
(Alias
(E
)) then
5101 while Scop
/= Standard_Standard
loop
5102 if Ekind
(Scop
) in Subprogram_Kind
and then Is_Inlined
(Scop
) then
5106 Scop
:= Scope
(Scop
);
5110 end Is_Inlined_Or_Child_Of_Inlined
;
5113 -- Must be in the main unit or inlined (or child of inlined)
5115 if (Is_In_Main_Unit
(N
) or else Is_Inlined_Or_Child_Of_Inlined
(Subp
))
5117 -- Must be generating code or analyzing code in ASIS/GNATprove mode
5119 and then (Operating_Mode
= Generate_Code
5120 or else (Operating_Mode
= Check_Semantics
5121 and then (ASIS_Mode
or GNATprove_Mode
)))
5123 -- The body is needed when generating code (full expansion), in ASIS
5124 -- mode for other tools, and in GNATprove mode (special expansion) for
5125 -- formal verification of the body itself.
5127 and then (Expander_Active
or ASIS_Mode
or GNATprove_Mode
)
5129 -- No point in inlining if ABE is inevitable
5131 and then not Is_Known_Guaranteed_ABE
(N
)
5133 -- Or if subprogram is eliminated
5135 and then not Is_Eliminated
(Subp
)
5137 Add_Pending_Instantiation
(N
, Unit_Declaration_Node
(Subp
));
5140 -- Here if not inlined, or we ignore the inlining
5145 end Need_Subprogram_Instance_Body
;
5147 --------------------------------------
5148 -- Analyze_Subprogram_Instantiation --
5149 --------------------------------------
5151 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5152 -- must be replaced by gotos which jump to the end of the routine in order
5153 -- to restore the Ghost and SPARK modes.
5155 procedure Analyze_Subprogram_Instantiation
5159 Loc
: constant Source_Ptr
:= Sloc
(N
);
5160 Gen_Id
: constant Node_Id
:= Name
(N
);
5161 Errs
: constant Nat
:= Serious_Errors_Detected
;
5163 Anon_Id
: constant Entity_Id
:=
5164 Make_Defining_Identifier
(Sloc
(Defining_Entity
(N
)),
5165 Chars
=> New_External_Name
5166 (Chars
(Defining_Entity
(N
)), 'R'));
5168 Act_Decl_Id
: Entity_Id
:= Empty
; -- init to avoid warning
5173 Env_Installed
: Boolean := False;
5174 Gen_Unit
: Entity_Id
;
5176 Pack_Id
: Entity_Id
;
5177 Parent_Installed
: Boolean := False;
5179 Renaming_List
: List_Id
;
5180 -- The list of declarations that link formals and actuals of the
5181 -- instance. These are subtype declarations for formal types, and
5182 -- renaming declarations for other formals. The subprogram declaration
5183 -- for the instance is then appended to the list, and the last item on
5184 -- the list is the renaming declaration for the instance.
5186 procedure Analyze_Instance_And_Renamings
;
5187 -- The instance must be analyzed in a context that includes the mappings
5188 -- of generic parameters into actuals. We create a package declaration
5189 -- for this purpose, and a subprogram with an internal name within the
5190 -- package. The subprogram instance is simply an alias for the internal
5191 -- subprogram, declared in the current scope.
5193 procedure Build_Subprogram_Renaming
;
5194 -- If the subprogram is recursive, there are occurrences of the name of
5195 -- the generic within the body, which must resolve to the current
5196 -- instance. We add a renaming declaration after the declaration, which
5197 -- is available in the instance body, as well as in the analysis of
5198 -- aspects that appear in the generic. This renaming declaration is
5199 -- inserted after the instance declaration which it renames.
5201 ------------------------------------
5202 -- Analyze_Instance_And_Renamings --
5203 ------------------------------------
5205 procedure Analyze_Instance_And_Renamings
is
5206 Def_Ent
: constant Entity_Id
:= Defining_Entity
(N
);
5207 Pack_Decl
: Node_Id
;
5210 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5212 -- For the case of a compilation unit, the container package has
5213 -- the same name as the instantiation, to insure that the binder
5214 -- calls the elaboration procedure with the right name. Copy the
5215 -- entity of the instance, which may have compilation level flags
5216 -- (e.g. Is_Child_Unit) set.
5218 Pack_Id
:= New_Copy
(Def_Ent
);
5221 -- Otherwise we use the name of the instantiation concatenated
5222 -- with its source position to ensure uniqueness if there are
5223 -- several instantiations with the same name.
5226 Make_Defining_Identifier
(Loc
,
5227 Chars
=> New_External_Name
5228 (Related_Id
=> Chars
(Def_Ent
),
5230 Suffix_Index
=> Source_Offset
(Sloc
(Def_Ent
))));
5234 Make_Package_Declaration
(Loc
,
5235 Specification
=> Make_Package_Specification
(Loc
,
5236 Defining_Unit_Name
=> Pack_Id
,
5237 Visible_Declarations
=> Renaming_List
,
5238 End_Label
=> Empty
));
5240 Set_Instance_Spec
(N
, Pack_Decl
);
5241 Set_Is_Generic_Instance
(Pack_Id
);
5242 Set_Debug_Info_Needed
(Pack_Id
);
5244 -- Case of not a compilation unit
5246 if Nkind
(Parent
(N
)) /= N_Compilation_Unit
then
5247 Mark_Rewrite_Insertion
(Pack_Decl
);
5248 Insert_Before
(N
, Pack_Decl
);
5249 Set_Has_Completion
(Pack_Id
);
5251 -- Case of an instantiation that is a compilation unit
5253 -- Place declaration on current node so context is complete for
5254 -- analysis (including nested instantiations), and for use in a
5255 -- context_clause (see Analyze_With_Clause).
5258 Set_Unit
(Parent
(N
), Pack_Decl
);
5259 Set_Parent_Spec
(Pack_Decl
, Parent_Spec
(N
));
5262 Analyze
(Pack_Decl
);
5263 Check_Formal_Packages
(Pack_Id
);
5264 Set_Is_Generic_Instance
(Pack_Id
, False);
5266 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
5269 -- Body of the enclosing package is supplied when instantiating the
5270 -- subprogram body, after semantic analysis is completed.
5272 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5274 -- Remove package itself from visibility, so it does not
5275 -- conflict with subprogram.
5277 Set_Name_Entity_Id
(Chars
(Pack_Id
), Homonym
(Pack_Id
));
5279 -- Set name and scope of internal subprogram so that the proper
5280 -- external name will be generated. The proper scope is the scope
5281 -- of the wrapper package. We need to generate debugging info for
5282 -- the internal subprogram, so set flag accordingly.
5284 Set_Chars
(Anon_Id
, Chars
(Defining_Entity
(N
)));
5285 Set_Scope
(Anon_Id
, Scope
(Pack_Id
));
5287 -- Mark wrapper package as referenced, to avoid spurious warnings
5288 -- if the instantiation appears in various with_ clauses of
5289 -- subunits of the main unit.
5291 Set_Referenced
(Pack_Id
);
5294 Set_Is_Generic_Instance
(Anon_Id
);
5295 Set_Debug_Info_Needed
(Anon_Id
);
5296 Act_Decl_Id
:= New_Copy
(Anon_Id
);
5298 Set_Parent
(Act_Decl_Id
, Parent
(Anon_Id
));
5299 Set_Chars
(Act_Decl_Id
, Chars
(Defining_Entity
(N
)));
5300 Set_Sloc
(Act_Decl_Id
, Sloc
(Defining_Entity
(N
)));
5302 -- Subprogram instance comes from source only if generic does
5304 Set_Comes_From_Source
(Act_Decl_Id
, Comes_From_Source
(Gen_Unit
));
5306 -- If the instance is a child unit, mark the Id accordingly. Mark
5307 -- the anonymous entity as well, which is the real subprogram and
5308 -- which is used when the instance appears in a context clause.
5309 -- Similarly, propagate the Is_Eliminated flag to handle properly
5310 -- nested eliminated subprograms.
5312 Set_Is_Child_Unit
(Act_Decl_Id
, Is_Child_Unit
(Defining_Entity
(N
)));
5313 Set_Is_Child_Unit
(Anon_Id
, Is_Child_Unit
(Defining_Entity
(N
)));
5314 New_Overloaded_Entity
(Act_Decl_Id
);
5315 Check_Eliminated
(Act_Decl_Id
);
5316 Set_Is_Eliminated
(Anon_Id
, Is_Eliminated
(Act_Decl_Id
));
5318 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5320 -- In compilation unit case, kill elaboration checks on the
5321 -- instantiation, since they are never needed - the body is
5322 -- instantiated at the same point as the spec.
5324 if Legacy_Elaboration_Checks
then
5325 Set_Kill_Elaboration_Checks
(Act_Decl_Id
);
5326 Set_Suppress_Elaboration_Warnings
(Act_Decl_Id
);
5329 Set_Is_Compilation_Unit
(Anon_Id
);
5330 Set_Cunit_Entity
(Current_Sem_Unit
, Pack_Id
);
5333 -- The instance is not a freezing point for the new subprogram.
5334 -- The anonymous subprogram may have a freeze node, created for
5335 -- some delayed aspects. This freeze node must not be inherited
5336 -- by the visible subprogram entity.
5338 Set_Is_Frozen
(Act_Decl_Id
, False);
5339 Set_Freeze_Node
(Act_Decl_Id
, Empty
);
5341 if Nkind
(Defining_Entity
(N
)) = N_Defining_Operator_Symbol
then
5342 Valid_Operator_Definition
(Act_Decl_Id
);
5345 Set_Alias
(Act_Decl_Id
, Anon_Id
);
5346 Set_Has_Completion
(Act_Decl_Id
);
5347 Set_Related_Instance
(Pack_Id
, Act_Decl_Id
);
5349 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5350 Set_Body_Required
(Parent
(N
), False);
5352 end Analyze_Instance_And_Renamings
;
5354 -------------------------------
5355 -- Build_Subprogram_Renaming --
5356 -------------------------------
5358 procedure Build_Subprogram_Renaming
is
5359 Renaming_Decl
: Node_Id
;
5360 Unit_Renaming
: Node_Id
;
5364 Make_Subprogram_Renaming_Declaration
(Loc
,
5367 (Specification
(Original_Node
(Gen_Decl
)),
5369 Instantiating
=> True),
5370 Name
=> New_Occurrence_Of
(Anon_Id
, Loc
));
5372 -- The generic may be a a child unit. The renaming needs an
5373 -- identifier with the proper name.
5375 Set_Defining_Unit_Name
(Specification
(Unit_Renaming
),
5376 Make_Defining_Identifier
(Loc
, Chars
(Gen_Unit
)));
5378 -- If there is a formal subprogram with the same name as the unit
5379 -- itself, do not add this renaming declaration, to prevent
5380 -- ambiguities when there is a call with that name in the body.
5381 -- This is a partial and ugly fix for one ACATS test. ???
5383 Renaming_Decl
:= First
(Renaming_List
);
5384 while Present
(Renaming_Decl
) loop
5385 if Nkind
(Renaming_Decl
) = N_Subprogram_Renaming_Declaration
5387 Chars
(Defining_Entity
(Renaming_Decl
)) = Chars
(Gen_Unit
)
5392 Next
(Renaming_Decl
);
5395 if No
(Renaming_Decl
) then
5396 Append
(Unit_Renaming
, Renaming_List
);
5398 end Build_Subprogram_Renaming
;
5402 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
5403 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
5404 Saved_ISMP
: constant Boolean :=
5405 Ignore_SPARK_Mode_Pragmas_In_Instance
;
5406 Saved_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
5407 Saved_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
5408 -- Save the Ghost and SPARK mode-related data to restore on exit
5410 Vis_Prims_List
: Elist_Id
:= No_Elist
;
5411 -- List of primitives made temporarily visible in the instantiation
5412 -- to match the visibility of the formal type
5414 -- Start of processing for Analyze_Subprogram_Instantiation
5417 -- Preserve relevant elaboration-related attributes of the context which
5418 -- are no longer available or very expensive to recompute once analysis,
5419 -- resolution, and expansion are over.
5421 Mark_Elaboration_Attributes
5428 Check_SPARK_05_Restriction
("generic is not allowed", N
);
5430 -- Very first thing: check for special Text_IO unit in case we are
5431 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5432 -- such an instantiation is bogus (these are packages, not subprograms),
5433 -- but we get a better error message if we do this.
5435 Check_Text_IO_Special_Unit
(Gen_Id
);
5437 -- Make node global for error reporting
5439 Instantiation_Node
:= N
;
5441 -- For package instantiations we turn off style checks, because they
5442 -- will have been emitted in the generic. For subprogram instantiations
5443 -- we want to apply at least the check on overriding indicators so we
5444 -- do not modify the style check status.
5446 -- The renaming declarations for the actuals do not come from source and
5447 -- will not generate spurious warnings.
5449 Preanalyze_Actuals
(N
);
5452 Env_Installed
:= True;
5453 Check_Generic_Child_Unit
(Gen_Id
, Parent_Installed
);
5454 Gen_Unit
:= Entity
(Gen_Id
);
5456 -- A subprogram instantiation is Ghost when it is subject to pragma
5457 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5458 -- that any nodes generated during analysis and expansion are marked as
5461 Mark_And_Set_Ghost_Instantiation
(N
, Gen_Unit
);
5463 Generate_Reference
(Gen_Unit
, Gen_Id
);
5465 if Nkind
(Gen_Id
) = N_Identifier
5466 and then Chars
(Gen_Unit
) = Chars
(Defining_Entity
(N
))
5469 ("& is hidden within declaration of instance", Gen_Id
, Gen_Unit
);
5472 if Etype
(Gen_Unit
) = Any_Type
then
5477 -- Verify that it is a generic subprogram of the right kind, and that
5478 -- it does not lead to a circular instantiation.
5480 if K
= E_Procedure
and then Ekind
(Gen_Unit
) /= E_Generic_Procedure
then
5482 ("& is not the name of a generic procedure", Gen_Id
, Gen_Unit
);
5484 elsif K
= E_Function
and then Ekind
(Gen_Unit
) /= E_Generic_Function
then
5486 ("& is not the name of a generic function", Gen_Id
, Gen_Unit
);
5488 elsif In_Open_Scopes
(Gen_Unit
) then
5489 Error_Msg_NE
("instantiation of & within itself", N
, Gen_Unit
);
5492 Set_Entity
(Gen_Id
, Gen_Unit
);
5493 Set_Is_Instantiated
(Gen_Unit
);
5495 if In_Extended_Main_Source_Unit
(N
) then
5496 Generate_Reference
(Gen_Unit
, N
);
5499 -- If renaming, get original unit
5501 if Present
(Renamed_Object
(Gen_Unit
))
5502 and then Ekind_In
(Renamed_Object
(Gen_Unit
), E_Generic_Procedure
,
5505 Gen_Unit
:= Renamed_Object
(Gen_Unit
);
5506 Set_Is_Instantiated
(Gen_Unit
);
5507 Generate_Reference
(Gen_Unit
, N
);
5510 if Contains_Instance_Of
(Gen_Unit
, Current_Scope
, Gen_Id
) then
5511 Error_Msg_Node_2
:= Current_Scope
;
5513 ("circular Instantiation: & instantiated in &!", N
, Gen_Unit
);
5514 Circularity_Detected
:= True;
5515 Restore_Hidden_Primitives
(Vis_Prims_List
);
5519 Gen_Decl
:= Unit_Declaration_Node
(Gen_Unit
);
5521 -- Initialize renamings map, for error checking
5523 Generic_Renamings
.Set_Last
(0);
5524 Generic_Renamings_HTable
.Reset
;
5526 Create_Instantiation_Source
(N
, Gen_Unit
, S_Adjustment
);
5528 -- Copy original generic tree, to produce text for instantiation
5532 (Original_Node
(Gen_Decl
), Empty
, Instantiating
=> True);
5534 -- Inherit overriding indicator from instance node
5536 Act_Spec
:= Specification
(Act_Tree
);
5537 Set_Must_Override
(Act_Spec
, Must_Override
(N
));
5538 Set_Must_Not_Override
(Act_Spec
, Must_Not_Override
(N
));
5541 Analyze_Associations
5543 Formals
=> Generic_Formal_Declarations
(Act_Tree
),
5544 F_Copy
=> Generic_Formal_Declarations
(Gen_Decl
));
5546 Vis_Prims_List
:= Check_Hidden_Primitives
(Renaming_List
);
5548 -- The subprogram itself cannot contain a nested instance, so the
5549 -- current parent is left empty.
5551 Set_Instance_Env
(Gen_Unit
, Empty
);
5553 -- Build the subprogram declaration, which does not appear in the
5554 -- generic template, and give it a sloc consistent with that of the
5557 Set_Defining_Unit_Name
(Act_Spec
, Anon_Id
);
5558 Set_Generic_Parent
(Act_Spec
, Gen_Unit
);
5560 Make_Subprogram_Declaration
(Sloc
(Act_Spec
),
5561 Specification
=> Act_Spec
);
5563 -- The aspects have been copied previously, but they have to be
5564 -- linked explicitly to the new subprogram declaration. Explicit
5565 -- pre/postconditions on the instance are analyzed below, in a
5568 Move_Aspects
(Act_Tree
, To
=> Act_Decl
);
5569 Set_Categorization_From_Pragmas
(Act_Decl
);
5571 if Parent_Installed
then
5575 Append
(Act_Decl
, Renaming_List
);
5577 -- Contract-related source pragmas that follow a generic subprogram
5578 -- must be instantiated explicitly because they are not part of the
5579 -- subprogram template.
5581 Instantiate_Subprogram_Contract
5582 (Original_Node
(Gen_Decl
), Renaming_List
);
5584 Build_Subprogram_Renaming
;
5586 -- If the context of the instance is subject to SPARK_Mode "off" or
5587 -- the annotation is altogether missing, set the global flag which
5588 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5589 -- the instance. This should be done prior to analyzing the instance.
5591 if SPARK_Mode
/= On
then
5592 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
5595 -- If the context of an instance is not subject to SPARK_Mode "off",
5596 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5597 -- the latter should be the one applicable to the instance.
5599 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5600 and then Saved_SM
/= Off
5601 and then Present
(SPARK_Pragma
(Gen_Unit
))
5603 Set_SPARK_Mode
(Gen_Unit
);
5606 Analyze_Instance_And_Renamings
;
5608 -- Restore SPARK_Mode from the context after analysis of the package
5609 -- declaration, so that the SPARK_Mode on the generic spec does not
5610 -- apply to the pending instance for the instance body.
5612 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5613 and then Saved_SM
/= Off
5614 and then Present
(SPARK_Pragma
(Gen_Unit
))
5616 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
5619 -- If the generic is marked Import (Intrinsic), then so is the
5620 -- instance. This indicates that there is no body to instantiate. If
5621 -- generic is marked inline, so it the instance, and the anonymous
5622 -- subprogram it renames. If inlined, or else if inlining is enabled
5623 -- for the compilation, we generate the instance body even if it is
5624 -- not within the main unit.
5626 if Is_Intrinsic_Subprogram
(Gen_Unit
) then
5627 Set_Is_Intrinsic_Subprogram
(Anon_Id
);
5628 Set_Is_Intrinsic_Subprogram
(Act_Decl_Id
);
5630 if Chars
(Gen_Unit
) = Name_Unchecked_Conversion
then
5631 Validate_Unchecked_Conversion
(N
, Act_Decl_Id
);
5635 -- Inherit convention from generic unit. Intrinsic convention, as for
5636 -- an instance of unchecked conversion, is not inherited because an
5637 -- explicit Ada instance has been created.
5639 if Has_Convention_Pragma
(Gen_Unit
)
5640 and then Convention
(Gen_Unit
) /= Convention_Intrinsic
5642 Set_Convention
(Act_Decl_Id
, Convention
(Gen_Unit
));
5643 Set_Is_Exported
(Act_Decl_Id
, Is_Exported
(Gen_Unit
));
5646 Generate_Definition
(Act_Decl_Id
);
5648 -- Inherit all inlining-related flags which apply to the generic in
5649 -- the subprogram and its declaration.
5651 Set_Is_Inlined
(Act_Decl_Id
, Is_Inlined
(Gen_Unit
));
5652 Set_Is_Inlined
(Anon_Id
, Is_Inlined
(Gen_Unit
));
5654 Set_Has_Pragma_Inline
(Act_Decl_Id
, Has_Pragma_Inline
(Gen_Unit
));
5655 Set_Has_Pragma_Inline
(Anon_Id
, Has_Pragma_Inline
(Gen_Unit
));
5657 -- Propagate No_Return if pragma applied to generic unit. This must
5658 -- be done explicitly because pragma does not appear in generic
5659 -- declaration (unlike the aspect case).
5661 if No_Return
(Gen_Unit
) then
5662 Set_No_Return
(Act_Decl_Id
);
5663 Set_No_Return
(Anon_Id
);
5666 Set_Has_Pragma_Inline_Always
5667 (Act_Decl_Id
, Has_Pragma_Inline_Always
(Gen_Unit
));
5668 Set_Has_Pragma_Inline_Always
5669 (Anon_Id
, Has_Pragma_Inline_Always
(Gen_Unit
));
5671 -- Mark both the instance spec and the anonymous package in case the
5672 -- body is instantiated at a later pass. This preserves the original
5673 -- context in effect for the body.
5675 if SPARK_Mode
/= On
then
5676 Set_Ignore_SPARK_Mode_Pragmas
(Act_Decl_Id
);
5677 Set_Ignore_SPARK_Mode_Pragmas
(Anon_Id
);
5680 if Legacy_Elaboration_Checks
5681 and then not Is_Intrinsic_Subprogram
(Gen_Unit
)
5683 Check_Elab_Instantiation
(N
);
5686 -- Save the scenario for later examination by the ABE Processing
5689 Record_Elaboration_Scenario
(N
);
5691 -- The instantiation results in a guaranteed ABE. Create a completing
5692 -- body for the subprogram declaration because the real body will not
5695 if Is_Known_Guaranteed_ABE
(N
) then
5696 Provide_Completing_Bodies
(Instance_Spec
(N
));
5699 if Is_Dispatching_Operation
(Act_Decl_Id
)
5700 and then Ada_Version
>= Ada_2005
5706 Formal
:= First_Formal
(Act_Decl_Id
);
5707 while Present
(Formal
) loop
5708 if Ekind
(Etype
(Formal
)) = E_Anonymous_Access_Type
5709 and then Is_Controlling_Formal
(Formal
)
5710 and then not Can_Never_Be_Null
(Formal
)
5713 ("access parameter& is controlling,", N
, Formal
);
5715 ("\corresponding parameter of & must be explicitly "
5716 & "null-excluding", N
, Gen_Id
);
5719 Next_Formal
(Formal
);
5724 Check_Hidden_Child_Unit
(N
, Gen_Unit
, Act_Decl_Id
);
5726 Validate_Categorization_Dependency
(N
, Act_Decl_Id
);
5728 if not Is_Intrinsic_Subprogram
(Act_Decl_Id
) then
5729 Inherit_Context
(Gen_Decl
, N
);
5731 Restore_Private_Views
(Pack_Id
, False);
5733 -- If the context requires a full instantiation, mark node for
5734 -- subsequent construction of the body.
5736 if Need_Subprogram_Instance_Body
(N
, Act_Decl_Id
) then
5737 Check_Forward_Instantiation
(Gen_Decl
);
5739 -- The wrapper package is always delayed, because it does not
5740 -- constitute a freeze point, but to insure that the freeze node
5741 -- is placed properly, it is created directly when instantiating
5742 -- the body (otherwise the freeze node might appear to early for
5743 -- nested instantiations). For ASIS purposes, indicate that the
5744 -- wrapper package has replaced the instantiation node.
5746 elsif Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5747 Rewrite
(N
, Unit
(Parent
(N
)));
5748 Set_Unit
(Parent
(N
), N
);
5751 -- Replace instance node for library-level instantiations of
5752 -- intrinsic subprograms, for ASIS use.
5754 elsif Nkind
(Parent
(N
)) = N_Compilation_Unit
then
5755 Rewrite
(N
, Unit
(Parent
(N
)));
5756 Set_Unit
(Parent
(N
), N
);
5759 if Parent_Installed
then
5763 Restore_Hidden_Primitives
(Vis_Prims_List
);
5765 Env_Installed
:= False;
5766 Generic_Renamings
.Set_Last
(0);
5767 Generic_Renamings_HTable
.Reset
;
5771 -- Analyze aspects in declaration if no errors appear in the instance.
5773 if Has_Aspects
(N
) and then Serious_Errors_Detected
= Errs
then
5774 Analyze_Aspect_Specifications
(N
, Act_Decl_Id
);
5777 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
5778 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
5779 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
5782 when Instantiation_Error
=>
5783 if Parent_Installed
then
5787 if Env_Installed
then
5791 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
5792 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
5793 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
5794 end Analyze_Subprogram_Instantiation
;
5796 -------------------------
5797 -- Get_Associated_Node --
5798 -------------------------
5800 function Get_Associated_Node
(N
: Node_Id
) return Node_Id
is
5804 Assoc
:= Associated_Node
(N
);
5806 if Nkind
(Assoc
) /= Nkind
(N
) then
5809 elsif Nkind_In
(Assoc
, N_Aggregate
, N_Extension_Aggregate
) then
5813 -- If the node is part of an inner generic, it may itself have been
5814 -- remapped into a further generic copy. Associated_Node is otherwise
5815 -- used for the entity of the node, and will be of a different node
5816 -- kind, or else N has been rewritten as a literal or function call.
5818 while Present
(Associated_Node
(Assoc
))
5819 and then Nkind
(Associated_Node
(Assoc
)) = Nkind
(Assoc
)
5821 Assoc
:= Associated_Node
(Assoc
);
5824 -- Follow an additional link in case the final node was rewritten.
5825 -- This can only happen with nested generic units.
5827 if (Nkind
(Assoc
) = N_Identifier
or else Nkind
(Assoc
) in N_Op
)
5828 and then Present
(Associated_Node
(Assoc
))
5829 and then (Nkind_In
(Associated_Node
(Assoc
), N_Function_Call
,
5830 N_Explicit_Dereference
,
5835 Assoc
:= Associated_Node
(Assoc
);
5838 -- An additional special case: an unconstrained type in an object
5839 -- declaration may have been rewritten as a local subtype constrained
5840 -- by the expression in the declaration. We need to recover the
5841 -- original entity, which may be global.
5843 if Present
(Original_Node
(Assoc
))
5844 and then Nkind
(Parent
(N
)) = N_Object_Declaration
5846 Assoc
:= Original_Node
(Assoc
);
5851 end Get_Associated_Node
;
5853 ----------------------------
5854 -- Build_Function_Wrapper --
5855 ----------------------------
5857 function Build_Function_Wrapper
5858 (Formal_Subp
: Entity_Id
;
5859 Actual_Subp
: Entity_Id
) return Node_Id
5861 Loc
: constant Source_Ptr
:= Sloc
(Current_Scope
);
5862 Ret_Type
: constant Entity_Id
:= Get_Instance_Of
(Etype
(Formal_Subp
));
5865 Func_Name
: Node_Id
;
5867 Parm_Type
: Node_Id
;
5868 Profile
: List_Id
:= New_List
;
5875 Func_Name
:= New_Occurrence_Of
(Actual_Subp
, Loc
);
5877 Func
:= Make_Defining_Identifier
(Loc
, Chars
(Formal_Subp
));
5878 Set_Ekind
(Func
, E_Function
);
5879 Set_Is_Generic_Actual_Subprogram
(Func
);
5881 Actuals
:= New_List
;
5882 Profile
:= New_List
;
5884 Act_F
:= First_Formal
(Actual_Subp
);
5885 Form_F
:= First_Formal
(Formal_Subp
);
5886 while Present
(Form_F
) loop
5888 -- Create new formal for profile of wrapper, and add a reference
5889 -- to it in the list of actuals for the enclosing call. The name
5890 -- must be that of the formal in the formal subprogram, because
5891 -- calls to it in the generic body may use named associations.
5893 New_F
:= Make_Defining_Identifier
(Loc
, Chars
(Form_F
));
5896 New_Occurrence_Of
(Get_Instance_Of
(Etype
(Form_F
)), Loc
);
5899 Make_Parameter_Specification
(Loc
,
5900 Defining_Identifier
=> New_F
,
5901 Parameter_Type
=> Parm_Type
));
5903 Append_To
(Actuals
, New_Occurrence_Of
(New_F
, Loc
));
5904 Next_Formal
(Form_F
);
5906 if Present
(Act_F
) then
5907 Next_Formal
(Act_F
);
5912 Make_Function_Specification
(Loc
,
5913 Defining_Unit_Name
=> Func
,
5914 Parameter_Specifications
=> Profile
,
5915 Result_Definition
=> New_Occurrence_Of
(Ret_Type
, Loc
));
5918 Make_Expression_Function
(Loc
,
5919 Specification
=> Spec
,
5921 Make_Function_Call
(Loc
,
5923 Parameter_Associations
=> Actuals
));
5926 end Build_Function_Wrapper
;
5928 ----------------------------
5929 -- Build_Operator_Wrapper --
5930 ----------------------------
5932 function Build_Operator_Wrapper
5933 (Formal_Subp
: Entity_Id
;
5934 Actual_Subp
: Entity_Id
) return Node_Id
5936 Loc
: constant Source_Ptr
:= Sloc
(Current_Scope
);
5937 Ret_Type
: constant Entity_Id
:=
5938 Get_Instance_Of
(Etype
(Formal_Subp
));
5939 Op_Type
: constant Entity_Id
:=
5940 Get_Instance_Of
(Etype
(First_Formal
(Formal_Subp
)));
5941 Is_Binary
: constant Boolean :=
5942 Present
(Next_Formal
(First_Formal
(Formal_Subp
)));
5945 Expr
: Node_Id
:= Empty
;
5953 Op_Name
:= Chars
(Actual_Subp
);
5955 -- Create entities for wrapper function and its formals
5957 F1
:= Make_Temporary
(Loc
, 'A');
5958 F2
:= Make_Temporary
(Loc
, 'B');
5959 L
:= New_Occurrence_Of
(F1
, Loc
);
5960 R
:= New_Occurrence_Of
(F2
, Loc
);
5962 Func
:= Make_Defining_Identifier
(Loc
, Chars
(Formal_Subp
));
5963 Set_Ekind
(Func
, E_Function
);
5964 Set_Is_Generic_Actual_Subprogram
(Func
);
5967 Make_Function_Specification
(Loc
,
5968 Defining_Unit_Name
=> Func
,
5969 Parameter_Specifications
=> New_List
(
5970 Make_Parameter_Specification
(Loc
,
5971 Defining_Identifier
=> F1
,
5972 Parameter_Type
=> New_Occurrence_Of
(Op_Type
, Loc
))),
5973 Result_Definition
=> New_Occurrence_Of
(Ret_Type
, Loc
));
5976 Append_To
(Parameter_Specifications
(Spec
),
5977 Make_Parameter_Specification
(Loc
,
5978 Defining_Identifier
=> F2
,
5979 Parameter_Type
=> New_Occurrence_Of
(Op_Type
, Loc
)));
5982 -- Build expression as a function call, or as an operator node
5983 -- that corresponds to the name of the actual, starting with
5984 -- binary operators.
5986 if Op_Name
not in Any_Operator_Name
then
5988 Make_Function_Call
(Loc
,
5990 New_Occurrence_Of
(Actual_Subp
, Loc
),
5991 Parameter_Associations
=> New_List
(L
));
5994 Append_To
(Parameter_Associations
(Expr
), R
);
5999 elsif Is_Binary
then
6000 if Op_Name
= Name_Op_And
then
6001 Expr
:= Make_Op_And
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6002 elsif Op_Name
= Name_Op_Or
then
6003 Expr
:= Make_Op_Or
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6004 elsif Op_Name
= Name_Op_Xor
then
6005 Expr
:= Make_Op_Xor
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6006 elsif Op_Name
= Name_Op_Eq
then
6007 Expr
:= Make_Op_Eq
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6008 elsif Op_Name
= Name_Op_Ne
then
6009 Expr
:= Make_Op_Ne
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6010 elsif Op_Name
= Name_Op_Le
then
6011 Expr
:= Make_Op_Le
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6012 elsif Op_Name
= Name_Op_Gt
then
6013 Expr
:= Make_Op_Gt
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6014 elsif Op_Name
= Name_Op_Ge
then
6015 Expr
:= Make_Op_Ge
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6016 elsif Op_Name
= Name_Op_Lt
then
6017 Expr
:= Make_Op_Lt
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6018 elsif Op_Name
= Name_Op_Add
then
6019 Expr
:= Make_Op_Add
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6020 elsif Op_Name
= Name_Op_Subtract
then
6021 Expr
:= Make_Op_Subtract
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6022 elsif Op_Name
= Name_Op_Concat
then
6023 Expr
:= Make_Op_Concat
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6024 elsif Op_Name
= Name_Op_Multiply
then
6025 Expr
:= Make_Op_Multiply
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6026 elsif Op_Name
= Name_Op_Divide
then
6027 Expr
:= Make_Op_Divide
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6028 elsif Op_Name
= Name_Op_Mod
then
6029 Expr
:= Make_Op_Mod
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6030 elsif Op_Name
= Name_Op_Rem
then
6031 Expr
:= Make_Op_Rem
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6032 elsif Op_Name
= Name_Op_Expon
then
6033 Expr
:= Make_Op_Expon
(Loc
, Left_Opnd
=> L
, Right_Opnd
=> R
);
6039 if Op_Name
= Name_Op_Add
then
6040 Expr
:= Make_Op_Plus
(Loc
, Right_Opnd
=> L
);
6041 elsif Op_Name
= Name_Op_Subtract
then
6042 Expr
:= Make_Op_Minus
(Loc
, Right_Opnd
=> L
);
6043 elsif Op_Name
= Name_Op_Abs
then
6044 Expr
:= Make_Op_Abs
(Loc
, Right_Opnd
=> L
);
6045 elsif Op_Name
= Name_Op_Not
then
6046 Expr
:= Make_Op_Not
(Loc
, Right_Opnd
=> L
);
6051 Make_Expression_Function
(Loc
,
6052 Specification
=> Spec
,
6053 Expression
=> Expr
);
6056 end Build_Operator_Wrapper
;
6058 -------------------------------------------
6059 -- Build_Instance_Compilation_Unit_Nodes --
6060 -------------------------------------------
6062 procedure Build_Instance_Compilation_Unit_Nodes
6067 Decl_Cunit
: Node_Id
;
6068 Body_Cunit
: Node_Id
;
6070 New_Main
: constant Entity_Id
:= Defining_Entity
(Act_Decl
);
6071 Old_Main
: constant Entity_Id
:= Cunit_Entity
(Main_Unit
);
6074 -- A new compilation unit node is built for the instance declaration
6077 Make_Compilation_Unit
(Sloc
(N
),
6078 Context_Items
=> Empty_List
,
6080 Aux_Decls_Node
=> Make_Compilation_Unit_Aux
(Sloc
(N
)));
6082 Set_Parent_Spec
(Act_Decl
, Parent_Spec
(N
));
6084 -- The new compilation unit is linked to its body, but both share the
6085 -- same file, so we do not set Body_Required on the new unit so as not
6086 -- to create a spurious dependency on a non-existent body in the ali.
6087 -- This simplifies CodePeer unit traversal.
6089 -- We use the original instantiation compilation unit as the resulting
6090 -- compilation unit of the instance, since this is the main unit.
6092 Rewrite
(N
, Act_Body
);
6094 -- Propagate the aspect specifications from the package body template to
6095 -- the instantiated version of the package body.
6097 if Has_Aspects
(Act_Body
) then
6098 Set_Aspect_Specifications
6099 (N
, New_Copy_List_Tree
(Aspect_Specifications
(Act_Body
)));
6102 Body_Cunit
:= Parent
(N
);
6104 -- The two compilation unit nodes are linked by the Library_Unit field
6106 Set_Library_Unit
(Decl_Cunit
, Body_Cunit
);
6107 Set_Library_Unit
(Body_Cunit
, Decl_Cunit
);
6109 -- Preserve the private nature of the package if needed
6111 Set_Private_Present
(Decl_Cunit
, Private_Present
(Body_Cunit
));
6113 -- If the instance is not the main unit, its context, categorization
6114 -- and elaboration entity are not relevant to the compilation.
6116 if Body_Cunit
/= Cunit
(Main_Unit
) then
6117 Make_Instance_Unit
(Body_Cunit
, In_Main
=> False);
6121 -- The context clause items on the instantiation, which are now attached
6122 -- to the body compilation unit (since the body overwrote the original
6123 -- instantiation node), semantically belong on the spec, so copy them
6124 -- there. It's harmless to leave them on the body as well. In fact one
6125 -- could argue that they belong in both places.
6127 Citem
:= First
(Context_Items
(Body_Cunit
));
6128 while Present
(Citem
) loop
6129 Append
(New_Copy
(Citem
), Context_Items
(Decl_Cunit
));
6133 -- Propagate categorization flags on packages, so that they appear in
6134 -- the ali file for the spec of the unit.
6136 if Ekind
(New_Main
) = E_Package
then
6137 Set_Is_Pure
(Old_Main
, Is_Pure
(New_Main
));
6138 Set_Is_Preelaborated
(Old_Main
, Is_Preelaborated
(New_Main
));
6139 Set_Is_Remote_Types
(Old_Main
, Is_Remote_Types
(New_Main
));
6140 Set_Is_Shared_Passive
(Old_Main
, Is_Shared_Passive
(New_Main
));
6141 Set_Is_Remote_Call_Interface
6142 (Old_Main
, Is_Remote_Call_Interface
(New_Main
));
6145 -- Make entry in Units table, so that binder can generate call to
6146 -- elaboration procedure for body, if any.
6148 Make_Instance_Unit
(Body_Cunit
, In_Main
=> True);
6149 Main_Unit_Entity
:= New_Main
;
6150 Set_Cunit_Entity
(Main_Unit
, Main_Unit_Entity
);
6152 -- Build elaboration entity, since the instance may certainly generate
6153 -- elaboration code requiring a flag for protection.
6155 Build_Elaboration_Entity
(Decl_Cunit
, New_Main
);
6156 end Build_Instance_Compilation_Unit_Nodes
;
6158 -----------------------------
6159 -- Check_Access_Definition --
6160 -----------------------------
6162 procedure Check_Access_Definition
(N
: Node_Id
) is
6165 (Ada_Version
>= Ada_2005
and then Present
(Access_Definition
(N
)));
6167 end Check_Access_Definition
;
6169 -----------------------------------
6170 -- Check_Formal_Package_Instance --
6171 -----------------------------------
6173 -- If the formal has specific parameters, they must match those of the
6174 -- actual. Both of them are instances, and the renaming declarations for
6175 -- their formal parameters appear in the same order in both. The analyzed
6176 -- formal has been analyzed in the context of the current instance.
6178 procedure Check_Formal_Package_Instance
6179 (Formal_Pack
: Entity_Id
;
6180 Actual_Pack
: Entity_Id
)
6182 E1
: Entity_Id
:= First_Entity
(Actual_Pack
);
6183 E2
: Entity_Id
:= First_Entity
(Formal_Pack
);
6184 Prev_E1
: Entity_Id
;
6189 procedure Check_Mismatch
(B
: Boolean);
6190 -- Common error routine for mismatch between the parameters of the
6191 -- actual instance and those of the formal package.
6193 function Same_Instantiated_Constant
(E1
, E2
: Entity_Id
) return Boolean;
6194 -- The formal may come from a nested formal package, and the actual may
6195 -- have been constant-folded. To determine whether the two denote the
6196 -- same entity we may have to traverse several definitions to recover
6197 -- the ultimate entity that they refer to.
6199 function Same_Instantiated_Function
(E1
, E2
: Entity_Id
) return Boolean;
6200 -- The formal and the actual must be identical, but if both are
6201 -- given by attributes they end up renaming different generated bodies,
6202 -- and we must verify that the attributes themselves match.
6204 function Same_Instantiated_Variable
(E1
, E2
: Entity_Id
) return Boolean;
6205 -- Similarly, if the formal comes from a nested formal package, the
6206 -- actual may designate the formal through multiple renamings, which
6207 -- have to be followed to determine the original variable in question.
6209 --------------------
6210 -- Check_Mismatch --
6211 --------------------
6213 procedure Check_Mismatch
(B
: Boolean) is
6214 -- A Formal_Type_Declaration for a derived private type is rewritten
6215 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6216 -- which is why we examine the original node.
6218 Kind
: constant Node_Kind
:= Nkind
(Original_Node
(Parent
(E2
)));
6221 if Kind
= N_Formal_Type_Declaration
then
6224 elsif Nkind_In
(Kind
, N_Formal_Object_Declaration
,
6225 N_Formal_Package_Declaration
)
6226 or else Kind
in N_Formal_Subprogram_Declaration
6230 -- Ada 2012: If both formal and actual are incomplete types they
6233 elsif Is_Incomplete_Type
(E1
) and then Is_Incomplete_Type
(E2
) then
6238 ("actual for & in actual instance does not match formal",
6239 Parent
(Actual_Pack
), E1
);
6243 --------------------------------
6244 -- Same_Instantiated_Constant --
6245 --------------------------------
6247 function Same_Instantiated_Constant
6248 (E1
, E2
: Entity_Id
) return Boolean
6254 while Present
(Ent
) loop
6258 elsif Ekind
(Ent
) /= E_Constant
then
6261 elsif Is_Entity_Name
(Constant_Value
(Ent
)) then
6262 if Entity
(Constant_Value
(Ent
)) = E1
then
6265 Ent
:= Entity
(Constant_Value
(Ent
));
6268 -- The actual may be a constant that has been folded. Recover
6271 elsif Is_Entity_Name
(Original_Node
(Constant_Value
(Ent
))) then
6272 Ent
:= Entity
(Original_Node
(Constant_Value
(Ent
)));
6280 end Same_Instantiated_Constant
;
6282 --------------------------------
6283 -- Same_Instantiated_Function --
6284 --------------------------------
6286 function Same_Instantiated_Function
6287 (E1
, E2
: Entity_Id
) return Boolean
6291 if Alias
(E1
) = Alias
(E2
) then
6294 elsif Present
(Alias
(E2
)) then
6295 U1
:= Original_Node
(Unit_Declaration_Node
(E1
));
6296 U2
:= Original_Node
(Unit_Declaration_Node
(Alias
(E2
)));
6298 return Nkind
(U1
) = N_Subprogram_Renaming_Declaration
6299 and then Nkind
(Name
(U1
)) = N_Attribute_Reference
6301 and then Nkind
(U2
) = N_Subprogram_Renaming_Declaration
6302 and then Nkind
(Name
(U2
)) = N_Attribute_Reference
6305 Attribute_Name
(Name
(U1
)) = Attribute_Name
(Name
(U2
));
6309 end Same_Instantiated_Function
;
6311 --------------------------------
6312 -- Same_Instantiated_Variable --
6313 --------------------------------
6315 function Same_Instantiated_Variable
6316 (E1
, E2
: Entity_Id
) return Boolean
6318 function Original_Entity
(E
: Entity_Id
) return Entity_Id
;
6319 -- Follow chain of renamings to the ultimate ancestor
6321 ---------------------
6322 -- Original_Entity --
6323 ---------------------
6325 function Original_Entity
(E
: Entity_Id
) return Entity_Id
is
6330 while Nkind
(Parent
(Orig
)) = N_Object_Renaming_Declaration
6331 and then Present
(Renamed_Object
(Orig
))
6332 and then Is_Entity_Name
(Renamed_Object
(Orig
))
6334 Orig
:= Entity
(Renamed_Object
(Orig
));
6338 end Original_Entity
;
6340 -- Start of processing for Same_Instantiated_Variable
6343 return Ekind
(E1
) = Ekind
(E2
)
6344 and then Original_Entity
(E1
) = Original_Entity
(E2
);
6345 end Same_Instantiated_Variable
;
6347 -- Start of processing for Check_Formal_Package_Instance
6351 while Present
(E1
) and then Present
(E2
) loop
6352 exit when Ekind
(E1
) = E_Package
6353 and then Renamed_Entity
(E1
) = Renamed_Entity
(Actual_Pack
);
6355 -- If the formal is the renaming of the formal package, this
6356 -- is the end of its formal part, which may occur before the
6357 -- end of the formal part in the actual in the presence of
6358 -- defaulted parameters in the formal package.
6360 exit when Nkind
(Parent
(E2
)) = N_Package_Renaming_Declaration
6361 and then Renamed_Entity
(E2
) = Scope
(E2
);
6363 -- The analysis of the actual may generate additional internal
6364 -- entities. If the formal is defaulted, there is no corresponding
6365 -- analysis and the internal entities must be skipped, until we
6366 -- find corresponding entities again.
6368 if Comes_From_Source
(E2
)
6369 and then not Comes_From_Source
(E1
)
6370 and then Chars
(E1
) /= Chars
(E2
)
6372 while Present
(E1
) and then Chars
(E1
) /= Chars
(E2
) loop
6380 -- Entities may be declared without full declaration, such as
6381 -- itypes and predefined operators (concatenation for arrays, eg).
6382 -- Skip it and keep the formal entity to find a later match for it.
6384 elsif No
(Parent
(E2
)) and then Ekind
(E1
) /= Ekind
(E2
) then
6388 -- If the formal entity comes from a formal declaration, it was
6389 -- defaulted in the formal package, and no check is needed on it.
6391 elsif Nkind_In
(Original_Node
(Parent
(E2
)),
6392 N_Formal_Object_Declaration
,
6393 N_Formal_Type_Declaration
)
6395 -- If the formal is a tagged type the corresponding class-wide
6396 -- type has been generated as well, and it must be skipped.
6398 if Is_Type
(E2
) and then Is_Tagged_Type
(E2
) then
6404 -- Ditto for defaulted formal subprograms.
6406 elsif Is_Overloadable
(E1
)
6407 and then Nkind
(Unit_Declaration_Node
(E2
)) in
6408 N_Formal_Subprogram_Declaration
6412 elsif Is_Type
(E1
) then
6414 -- Subtypes must statically match. E1, E2 are the local entities
6415 -- that are subtypes of the actuals. Itypes generated for other
6416 -- parameters need not be checked, the check will be performed
6417 -- on the parameters themselves.
6419 -- If E2 is a formal type declaration, it is a defaulted parameter
6420 -- and needs no checking.
6422 if not Is_Itype
(E1
) and then not Is_Itype
(E2
) then
6425 or else Etype
(E1
) /= Etype
(E2
)
6426 or else not Subtypes_Statically_Match
(E1
, E2
));
6429 elsif Ekind
(E1
) = E_Constant
then
6431 -- IN parameters must denote the same static value, or the same
6432 -- constant, or the literal null.
6434 Expr1
:= Expression
(Parent
(E1
));
6436 if Ekind
(E2
) /= E_Constant
then
6437 Check_Mismatch
(True);
6440 Expr2
:= Expression
(Parent
(E2
));
6443 if Is_OK_Static_Expression
(Expr1
) then
6444 if not Is_OK_Static_Expression
(Expr2
) then
6445 Check_Mismatch
(True);
6447 elsif Is_Discrete_Type
(Etype
(E1
)) then
6449 V1
: constant Uint
:= Expr_Value
(Expr1
);
6450 V2
: constant Uint
:= Expr_Value
(Expr2
);
6452 Check_Mismatch
(V1
/= V2
);
6455 elsif Is_Real_Type
(Etype
(E1
)) then
6457 V1
: constant Ureal
:= Expr_Value_R
(Expr1
);
6458 V2
: constant Ureal
:= Expr_Value_R
(Expr2
);
6460 Check_Mismatch
(V1
/= V2
);
6463 elsif Is_String_Type
(Etype
(E1
))
6464 and then Nkind
(Expr1
) = N_String_Literal
6466 if Nkind
(Expr2
) /= N_String_Literal
then
6467 Check_Mismatch
(True);
6470 (not String_Equal
(Strval
(Expr1
), Strval
(Expr2
)));
6474 elsif Is_Entity_Name
(Expr1
) then
6475 if Is_Entity_Name
(Expr2
) then
6476 if Entity
(Expr1
) = Entity
(Expr2
) then
6480 (not Same_Instantiated_Constant
6481 (Entity
(Expr1
), Entity
(Expr2
)));
6485 Check_Mismatch
(True);
6488 elsif Is_Entity_Name
(Original_Node
(Expr1
))
6489 and then Is_Entity_Name
(Expr2
)
6490 and then Same_Instantiated_Constant
6491 (Entity
(Original_Node
(Expr1
)), Entity
(Expr2
))
6495 elsif Nkind
(Expr1
) = N_Null
then
6496 Check_Mismatch
(Nkind
(Expr1
) /= N_Null
);
6499 Check_Mismatch
(True);
6502 elsif Ekind
(E1
) = E_Variable
then
6503 Check_Mismatch
(not Same_Instantiated_Variable
(E1
, E2
));
6505 elsif Ekind
(E1
) = E_Package
then
6507 (Ekind
(E1
) /= Ekind
(E2
)
6508 or else (Present
(Renamed_Object
(E2
))
6509 and then Renamed_Object
(E1
) /=
6510 Renamed_Object
(E2
)));
6512 elsif Is_Overloadable
(E1
) then
6513 -- Verify that the actual subprograms match. Note that actuals
6514 -- that are attributes are rewritten as subprograms. If the
6515 -- subprogram in the formal package is defaulted, no check is
6516 -- needed. Note that this can only happen in Ada 2005 when the
6517 -- formal package can be partially parameterized.
6519 if Nkind
(Unit_Declaration_Node
(E1
)) =
6520 N_Subprogram_Renaming_Declaration
6521 and then From_Default
(Unit_Declaration_Node
(E1
))
6525 -- If the formal package has an "others" box association that
6526 -- covers this formal, there is no need for a check either.
6528 elsif Nkind
(Unit_Declaration_Node
(E2
)) in
6529 N_Formal_Subprogram_Declaration
6530 and then Box_Present
(Unit_Declaration_Node
(E2
))
6534 -- No check needed if subprogram is a defaulted null procedure
6536 elsif No
(Alias
(E2
))
6537 and then Ekind
(E2
) = E_Procedure
6539 Null_Present
(Specification
(Unit_Declaration_Node
(E2
)))
6543 -- Otherwise the actual in the formal and the actual in the
6544 -- instantiation of the formal must match, up to renamings.
6548 (Ekind
(E2
) /= Ekind
(E1
)
6549 or else not Same_Instantiated_Function
(E1
, E2
));
6553 raise Program_Error
;
6561 end Check_Formal_Package_Instance
;
6563 ---------------------------
6564 -- Check_Formal_Packages --
6565 ---------------------------
6567 procedure Check_Formal_Packages
(P_Id
: Entity_Id
) is
6569 Formal_P
: Entity_Id
;
6570 Formal_Decl
: Node_Id
;
6572 -- Iterate through the declarations in the instance, looking for package
6573 -- renaming declarations that denote instances of formal packages. Stop
6574 -- when we find the renaming of the current package itself. The
6575 -- declaration for a formal package without a box is followed by an
6576 -- internal entity that repeats the instantiation.
6578 E
:= First_Entity
(P_Id
);
6579 while Present
(E
) loop
6580 if Ekind
(E
) = E_Package
then
6581 if Renamed_Object
(E
) = P_Id
then
6584 elsif Nkind
(Parent
(E
)) /= N_Package_Renaming_Declaration
then
6588 Formal_Decl
:= Parent
(Associated_Formal_Package
(E
));
6590 -- Nothing to check if the formal has a box or an others_clause
6591 -- (necessarily with a box).
6593 if Box_Present
(Formal_Decl
) then
6596 elsif Nkind
(First
(Generic_Associations
(Formal_Decl
))) =
6599 -- The internal validating package was generated but formal
6600 -- and instance are known to be compatible.
6602 Formal_P
:= Next_Entity
(E
);
6603 Remove
(Unit_Declaration_Node
(Formal_P
));
6606 Formal_P
:= Next_Entity
(E
);
6608 -- If the instance is within an enclosing instance body
6609 -- there is no need to verify the legality of current formal
6610 -- packages because they were legal in the generic body.
6611 -- This optimization may be applicable elsewhere, and it
6612 -- also removes spurious errors that may arise with
6613 -- on-the-fly inlining and confusion between private and
6616 if not In_Instance_Body
then
6617 Check_Formal_Package_Instance
(Formal_P
, E
);
6620 -- Restore the visibility of formals of the formal instance
6621 -- that are not defaulted, and are hidden within the current
6622 -- generic. These formals may be visible within an enclosing
6628 Elmt
:= First_Elmt
(Hidden_In_Formal_Instance
(Formal_P
));
6629 while Present
(Elmt
) loop
6630 Set_Is_Hidden
(Node
(Elmt
), False);
6635 -- After checking, remove the internal validating package.
6636 -- It is only needed for semantic checks, and as it may
6637 -- contain generic formal declarations it should not reach
6640 Remove
(Unit_Declaration_Node
(Formal_P
));
6647 end Check_Formal_Packages
;
6649 ---------------------------------
6650 -- Check_Forward_Instantiation --
6651 ---------------------------------
6653 procedure Check_Forward_Instantiation
(Decl
: Node_Id
) is
6655 Gen_Comp
: Entity_Id
:= Cunit_Entity
(Get_Source_Unit
(Decl
));
6658 -- The instantiation appears before the generic body if we are in the
6659 -- scope of the unit containing the generic, either in its spec or in
6660 -- the package body, and before the generic body.
6662 if Ekind
(Gen_Comp
) = E_Package_Body
then
6663 Gen_Comp
:= Spec_Entity
(Gen_Comp
);
6666 if In_Open_Scopes
(Gen_Comp
)
6667 and then No
(Corresponding_Body
(Decl
))
6672 and then not Is_Compilation_Unit
(S
)
6673 and then not Is_Child_Unit
(S
)
6675 if Ekind
(S
) = E_Package
then
6676 Set_Has_Forward_Instantiation
(S
);
6682 end Check_Forward_Instantiation
;
6684 ---------------------------
6685 -- Check_Generic_Actuals --
6686 ---------------------------
6688 -- The visibility of the actuals may be different between the point of
6689 -- generic instantiation and the instantiation of the body.
6691 procedure Check_Generic_Actuals
6692 (Instance
: Entity_Id
;
6693 Is_Formal_Box
: Boolean)
6698 function Denotes_Previous_Actual
(Typ
: Entity_Id
) return Boolean;
6699 -- For a formal that is an array type, the component type is often a
6700 -- previous formal in the same unit. The privacy status of the component
6701 -- type will have been examined earlier in the traversal of the
6702 -- corresponding actuals, and this status should not be modified for
6703 -- the array (sub)type itself. However, if the base type of the array
6704 -- (sub)type is private, its full view must be restored in the body to
6705 -- be consistent with subsequent index subtypes, etc.
6707 -- To detect this case we have to rescan the list of formals, which is
6708 -- usually short enough to ignore the resulting inefficiency.
6710 -----------------------------
6711 -- Denotes_Previous_Actual --
6712 -----------------------------
6714 function Denotes_Previous_Actual
(Typ
: Entity_Id
) return Boolean is
6718 Prev
:= First_Entity
(Instance
);
6719 while Present
(Prev
) loop
6721 and then Nkind
(Parent
(Prev
)) = N_Subtype_Declaration
6722 and then Is_Entity_Name
(Subtype_Indication
(Parent
(Prev
)))
6723 and then Entity
(Subtype_Indication
(Parent
(Prev
))) = Typ
6736 end Denotes_Previous_Actual
;
6738 -- Start of processing for Check_Generic_Actuals
6741 E
:= First_Entity
(Instance
);
6742 while Present
(E
) loop
6744 and then Nkind
(Parent
(E
)) = N_Subtype_Declaration
6745 and then Scope
(Etype
(E
)) /= Instance
6746 and then Is_Entity_Name
(Subtype_Indication
(Parent
(E
)))
6748 if Is_Array_Type
(E
)
6749 and then not Is_Private_Type
(Etype
(E
))
6750 and then Denotes_Previous_Actual
(Component_Type
(E
))
6754 Check_Private_View
(Subtype_Indication
(Parent
(E
)));
6757 Set_Is_Generic_Actual_Type
(E
, True);
6758 Set_Is_Hidden
(E
, False);
6759 Set_Is_Potentially_Use_Visible
(E
, In_Use
(Instance
));
6761 -- We constructed the generic actual type as a subtype of the
6762 -- supplied type. This means that it normally would not inherit
6763 -- subtype specific attributes of the actual, which is wrong for
6764 -- the generic case.
6766 Astype
:= Ancestor_Subtype
(E
);
6770 -- This can happen when E is an itype that is the full view of
6771 -- a private type completed, e.g. with a constrained array. In
6772 -- that case, use the first subtype, which will carry size
6773 -- information. The base type itself is unconstrained and will
6776 Astype
:= First_Subtype
(E
);
6779 Set_Size_Info
(E
, (Astype
));
6780 Set_RM_Size
(E
, RM_Size
(Astype
));
6781 Set_First_Rep_Item
(E
, First_Rep_Item
(Astype
));
6783 if Is_Discrete_Or_Fixed_Point_Type
(E
) then
6784 Set_RM_Size
(E
, RM_Size
(Astype
));
6786 -- In nested instances, the base type of an access actual may
6787 -- itself be private, and need to be exchanged.
6789 elsif Is_Access_Type
(E
)
6790 and then Is_Private_Type
(Etype
(E
))
6793 (New_Occurrence_Of
(Etype
(E
), Sloc
(Instance
)));
6796 elsif Ekind
(E
) = E_Package
then
6798 -- If this is the renaming for the current instance, we're done.
6799 -- Otherwise it is a formal package. If the corresponding formal
6800 -- was declared with a box, the (instantiations of the) generic
6801 -- formal part are also visible. Otherwise, ignore the entity
6802 -- created to validate the actuals.
6804 if Renamed_Object
(E
) = Instance
then
6807 elsif Nkind
(Parent
(E
)) /= N_Package_Renaming_Declaration
then
6810 -- The visibility of a formal of an enclosing generic is already
6813 elsif Denotes_Formal_Package
(E
) then
6816 elsif Present
(Associated_Formal_Package
(E
))
6817 and then not Is_Generic_Formal
(E
)
6819 if Box_Present
(Parent
(Associated_Formal_Package
(E
))) then
6820 Check_Generic_Actuals
(Renamed_Object
(E
), True);
6823 Check_Generic_Actuals
(Renamed_Object
(E
), False);
6826 Set_Is_Hidden
(E
, False);
6829 -- If this is a subprogram instance (in a wrapper package) the
6830 -- actual is fully visible.
6832 elsif Is_Wrapper_Package
(Instance
) then
6833 Set_Is_Hidden
(E
, False);
6835 -- If the formal package is declared with a box, or if the formal
6836 -- parameter is defaulted, it is visible in the body.
6838 elsif Is_Formal_Box
or else Is_Visible_Formal
(E
) then
6839 Set_Is_Hidden
(E
, False);
6842 if Ekind
(E
) = E_Constant
then
6844 -- If the type of the actual is a private type declared in the
6845 -- enclosing scope of the generic unit, the body of the generic
6846 -- sees the full view of the type (because it has to appear in
6847 -- the corresponding package body). If the type is private now,
6848 -- exchange views to restore the proper visiblity in the instance.
6851 Typ
: constant Entity_Id
:= Base_Type
(Etype
(E
));
6852 -- The type of the actual
6857 Parent_Scope
: Entity_Id
;
6858 -- The enclosing scope of the generic unit
6861 if Is_Wrapper_Package
(Instance
) then
6865 (Unit_Declaration_Node
6866 (Related_Instance
(Instance
))));
6869 Generic_Parent
(Package_Specification
(Instance
));
6872 Parent_Scope
:= Scope
(Gen_Id
);
6874 -- The exchange is only needed if the generic is defined
6875 -- within a package which is not a common ancestor of the
6876 -- scope of the instance, and is not already in scope.
6878 if Is_Private_Type
(Typ
)
6879 and then Scope
(Typ
) = Parent_Scope
6880 and then Scope
(Instance
) /= Parent_Scope
6881 and then Ekind
(Parent_Scope
) = E_Package
6882 and then not Is_Child_Unit
(Gen_Id
)
6886 -- If the type of the entity is a subtype, it may also have
6887 -- to be made visible, together with the base type of its
6888 -- full view, after exchange.
6890 if Is_Private_Type
(Etype
(E
)) then
6891 Switch_View
(Etype
(E
));
6892 Switch_View
(Base_Type
(Etype
(E
)));
6900 end Check_Generic_Actuals
;
6902 ------------------------------
6903 -- Check_Generic_Child_Unit --
6904 ------------------------------
6906 procedure Check_Generic_Child_Unit
6908 Parent_Installed
: in out Boolean)
6910 Loc
: constant Source_Ptr
:= Sloc
(Gen_Id
);
6911 Gen_Par
: Entity_Id
:= Empty
;
6913 Inst_Par
: Entity_Id
;
6916 function Find_Generic_Child
6918 Id
: Node_Id
) return Entity_Id
;
6919 -- Search generic parent for possible child unit with the given name
6921 function In_Enclosing_Instance
return Boolean;
6922 -- Within an instance of the parent, the child unit may be denoted by
6923 -- a simple name, or an abbreviated expanded name. Examine enclosing
6924 -- scopes to locate a possible parent instantiation.
6926 ------------------------
6927 -- Find_Generic_Child --
6928 ------------------------
6930 function Find_Generic_Child
6932 Id
: Node_Id
) return Entity_Id
6937 -- If entity of name is already set, instance has already been
6938 -- resolved, e.g. in an enclosing instantiation.
6940 if Present
(Entity
(Id
)) then
6941 if Scope
(Entity
(Id
)) = Scop
then
6948 E
:= First_Entity
(Scop
);
6949 while Present
(E
) loop
6950 if Chars
(E
) = Chars
(Id
)
6951 and then Is_Child_Unit
(E
)
6953 if Is_Child_Unit
(E
)
6954 and then not Is_Visible_Lib_Unit
(E
)
6957 ("generic child unit& is not visible", Gen_Id
, E
);
6969 end Find_Generic_Child
;
6971 ---------------------------
6972 -- In_Enclosing_Instance --
6973 ---------------------------
6975 function In_Enclosing_Instance
return Boolean is
6976 Enclosing_Instance
: Node_Id
;
6977 Instance_Decl
: Node_Id
;
6980 -- We do not inline any call that contains instantiations, except
6981 -- for instantiations of Unchecked_Conversion, so if we are within
6982 -- an inlined body the current instance does not require parents.
6984 if In_Inlined_Body
then
6985 pragma Assert
(Chars
(Gen_Id
) = Name_Unchecked_Conversion
);
6989 -- Loop to check enclosing scopes
6991 Enclosing_Instance
:= Current_Scope
;
6992 while Present
(Enclosing_Instance
) loop
6993 Instance_Decl
:= Unit_Declaration_Node
(Enclosing_Instance
);
6995 if Ekind
(Enclosing_Instance
) = E_Package
6996 and then Is_Generic_Instance
(Enclosing_Instance
)
6998 (Generic_Parent
(Specification
(Instance_Decl
)))
7000 -- Check whether the generic we are looking for is a child of
7003 E
:= Find_Generic_Child
7004 (Generic_Parent
(Specification
(Instance_Decl
)), Gen_Id
);
7005 exit when Present
(E
);
7011 Enclosing_Instance
:= Scope
(Enclosing_Instance
);
7023 Make_Expanded_Name
(Loc
,
7025 Prefix
=> New_Occurrence_Of
(Enclosing_Instance
, Loc
),
7026 Selector_Name
=> New_Occurrence_Of
(E
, Loc
)));
7028 Set_Entity
(Gen_Id
, E
);
7029 Set_Etype
(Gen_Id
, Etype
(E
));
7030 Parent_Installed
:= False; -- Already in scope.
7033 end In_Enclosing_Instance
;
7035 -- Start of processing for Check_Generic_Child_Unit
7038 -- If the name of the generic is given by a selected component, it may
7039 -- be the name of a generic child unit, and the prefix is the name of an
7040 -- instance of the parent, in which case the child unit must be visible.
7041 -- If this instance is not in scope, it must be placed there and removed
7042 -- after instantiation, because what is being instantiated is not the
7043 -- original child, but the corresponding child present in the instance
7046 -- If the child is instantiated within the parent, it can be given by
7047 -- a simple name. In this case the instance is already in scope, but
7048 -- the child generic must be recovered from the generic parent as well.
7050 if Nkind
(Gen_Id
) = N_Selected_Component
then
7051 S
:= Selector_Name
(Gen_Id
);
7052 Analyze
(Prefix
(Gen_Id
));
7053 Inst_Par
:= Entity
(Prefix
(Gen_Id
));
7055 if Ekind
(Inst_Par
) = E_Package
7056 and then Present
(Renamed_Object
(Inst_Par
))
7058 Inst_Par
:= Renamed_Object
(Inst_Par
);
7061 if Ekind
(Inst_Par
) = E_Package
then
7062 if Nkind
(Parent
(Inst_Par
)) = N_Package_Specification
then
7063 Gen_Par
:= Generic_Parent
(Parent
(Inst_Par
));
7065 elsif Nkind
(Parent
(Inst_Par
)) = N_Defining_Program_Unit_Name
7067 Nkind
(Parent
(Parent
(Inst_Par
))) = N_Package_Specification
7069 Gen_Par
:= Generic_Parent
(Parent
(Parent
(Inst_Par
)));
7072 elsif Ekind
(Inst_Par
) = E_Generic_Package
7073 and then Nkind
(Parent
(Gen_Id
)) = N_Formal_Package_Declaration
7075 -- A formal package may be a real child package, and not the
7076 -- implicit instance within a parent. In this case the child is
7077 -- not visible and has to be retrieved explicitly as well.
7079 Gen_Par
:= Inst_Par
;
7082 if Present
(Gen_Par
) then
7084 -- The prefix denotes an instantiation. The entity itself may be a
7085 -- nested generic, or a child unit.
7087 E
:= Find_Generic_Child
(Gen_Par
, S
);
7090 Change_Selected_Component_To_Expanded_Name
(Gen_Id
);
7091 Set_Entity
(Gen_Id
, E
);
7092 Set_Etype
(Gen_Id
, Etype
(E
));
7094 Set_Etype
(S
, Etype
(E
));
7096 -- Indicate that this is a reference to the parent
7098 if In_Extended_Main_Source_Unit
(Gen_Id
) then
7099 Set_Is_Instantiated
(Inst_Par
);
7102 -- A common mistake is to replicate the naming scheme of a
7103 -- hierarchy by instantiating a generic child directly, rather
7104 -- than the implicit child in a parent instance:
7106 -- generic .. package Gpar is ..
7107 -- generic .. package Gpar.Child is ..
7108 -- package Par is new Gpar ();
7111 -- package Par.Child is new Gpar.Child ();
7112 -- rather than Par.Child
7114 -- In this case the instantiation is within Par, which is an
7115 -- instance, but Gpar does not denote Par because we are not IN
7116 -- the instance of Gpar, so this is illegal. The test below
7117 -- recognizes this particular case.
7119 if Is_Child_Unit
(E
)
7120 and then not Comes_From_Source
(Entity
(Prefix
(Gen_Id
)))
7121 and then (not In_Instance
7122 or else Nkind
(Parent
(Parent
(Gen_Id
))) =
7126 ("prefix of generic child unit must be instance of parent",
7130 if not In_Open_Scopes
(Inst_Par
)
7131 and then Nkind
(Parent
(Gen_Id
)) not in
7132 N_Generic_Renaming_Declaration
7134 Install_Parent
(Inst_Par
);
7135 Parent_Installed
:= True;
7137 elsif In_Open_Scopes
(Inst_Par
) then
7139 -- If the parent is already installed, install the actuals
7140 -- for its formal packages. This is necessary when the child
7141 -- instance is a child of the parent instance: in this case,
7142 -- the parent is placed on the scope stack but the formal
7143 -- packages are not made visible.
7145 Install_Formal_Packages
(Inst_Par
);
7149 -- If the generic parent does not contain an entity that
7150 -- corresponds to the selector, the instance doesn't either.
7151 -- Analyzing the node will yield the appropriate error message.
7152 -- If the entity is not a child unit, then it is an inner
7153 -- generic in the parent.
7161 if Is_Child_Unit
(Entity
(Gen_Id
))
7163 Nkind
(Parent
(Gen_Id
)) not in N_Generic_Renaming_Declaration
7164 and then not In_Open_Scopes
(Inst_Par
)
7166 Install_Parent
(Inst_Par
);
7167 Parent_Installed
:= True;
7169 -- The generic unit may be the renaming of the implicit child
7170 -- present in an instance. In that case the parent instance is
7171 -- obtained from the name of the renamed entity.
7173 elsif Ekind
(Entity
(Gen_Id
)) = E_Generic_Package
7174 and then Present
(Renamed_Entity
(Entity
(Gen_Id
)))
7175 and then Is_Child_Unit
(Renamed_Entity
(Entity
(Gen_Id
)))
7178 Renamed_Package
: constant Node_Id
:=
7179 Name
(Parent
(Entity
(Gen_Id
)));
7181 if Nkind
(Renamed_Package
) = N_Expanded_Name
then
7182 Inst_Par
:= Entity
(Prefix
(Renamed_Package
));
7183 Install_Parent
(Inst_Par
);
7184 Parent_Installed
:= True;
7190 elsif Nkind
(Gen_Id
) = N_Expanded_Name
then
7192 -- Entity already present, analyze prefix, whose meaning may be an
7193 -- instance in the current context. If it is an instance of a
7194 -- relative within another, the proper parent may still have to be
7195 -- installed, if they are not of the same generation.
7197 Analyze
(Prefix
(Gen_Id
));
7199 -- Prevent cascaded errors
7201 if Etype
(Prefix
(Gen_Id
)) = Any_Type
then
7205 -- In the unlikely case that a local declaration hides the name of
7206 -- the parent package, locate it on the homonym chain. If the context
7207 -- is an instance of the parent, the renaming entity is flagged as
7210 Inst_Par
:= Entity
(Prefix
(Gen_Id
));
7211 while Present
(Inst_Par
)
7212 and then not Is_Package_Or_Generic_Package
(Inst_Par
)
7214 Inst_Par
:= Homonym
(Inst_Par
);
7217 pragma Assert
(Present
(Inst_Par
));
7218 Set_Entity
(Prefix
(Gen_Id
), Inst_Par
);
7220 if In_Enclosing_Instance
then
7223 elsif Present
(Entity
(Gen_Id
))
7224 and then Is_Child_Unit
(Entity
(Gen_Id
))
7225 and then not In_Open_Scopes
(Inst_Par
)
7227 Install_Parent
(Inst_Par
);
7228 Parent_Installed
:= True;
7231 elsif In_Enclosing_Instance
then
7233 -- The child unit is found in some enclosing scope
7240 -- If this is the renaming of the implicit child in a parent
7241 -- instance, recover the parent name and install it.
7243 if Is_Entity_Name
(Gen_Id
) then
7244 E
:= Entity
(Gen_Id
);
7246 if Is_Generic_Unit
(E
)
7247 and then Nkind
(Parent
(E
)) in N_Generic_Renaming_Declaration
7248 and then Is_Child_Unit
(Renamed_Object
(E
))
7249 and then Is_Generic_Unit
(Scope
(Renamed_Object
(E
)))
7250 and then Nkind
(Name
(Parent
(E
))) = N_Expanded_Name
7252 Rewrite
(Gen_Id
, New_Copy_Tree
(Name
(Parent
(E
))));
7253 Inst_Par
:= Entity
(Prefix
(Gen_Id
));
7255 if not In_Open_Scopes
(Inst_Par
) then
7256 Install_Parent
(Inst_Par
);
7257 Parent_Installed
:= True;
7260 -- If it is a child unit of a non-generic parent, it may be
7261 -- use-visible and given by a direct name. Install parent as
7264 elsif Is_Generic_Unit
(E
)
7265 and then Is_Child_Unit
(E
)
7267 Nkind
(Parent
(Gen_Id
)) not in N_Generic_Renaming_Declaration
7268 and then not Is_Generic_Unit
(Scope
(E
))
7270 if not In_Open_Scopes
(Scope
(E
)) then
7271 Install_Parent
(Scope
(E
));
7272 Parent_Installed
:= True;
7277 end Check_Generic_Child_Unit
;
7279 -----------------------------
7280 -- Check_Hidden_Child_Unit --
7281 -----------------------------
7283 procedure Check_Hidden_Child_Unit
7285 Gen_Unit
: Entity_Id
;
7286 Act_Decl_Id
: Entity_Id
)
7288 Gen_Id
: constant Node_Id
:= Name
(N
);
7291 if Is_Child_Unit
(Gen_Unit
)
7292 and then Is_Child_Unit
(Act_Decl_Id
)
7293 and then Nkind
(Gen_Id
) = N_Expanded_Name
7294 and then Entity
(Prefix
(Gen_Id
)) = Scope
(Act_Decl_Id
)
7295 and then Chars
(Gen_Unit
) = Chars
(Act_Decl_Id
)
7297 Error_Msg_Node_2
:= Scope
(Act_Decl_Id
);
7299 ("generic unit & is implicitly declared in &",
7300 Defining_Unit_Name
(N
), Gen_Unit
);
7301 Error_Msg_N
("\instance must have different name",
7302 Defining_Unit_Name
(N
));
7304 end Check_Hidden_Child_Unit
;
7306 ------------------------
7307 -- Check_Private_View --
7308 ------------------------
7310 procedure Check_Private_View
(N
: Node_Id
) is
7311 T
: constant Entity_Id
:= Etype
(N
);
7315 -- Exchange views if the type was not private in the generic but is
7316 -- private at the point of instantiation. Do not exchange views if
7317 -- the scope of the type is in scope. This can happen if both generic
7318 -- and instance are sibling units, or if type is defined in a parent.
7319 -- In this case the visibility of the type will be correct for all
7323 BT
:= Base_Type
(T
);
7325 if Is_Private_Type
(T
)
7326 and then not Has_Private_View
(N
)
7327 and then Present
(Full_View
(T
))
7328 and then not In_Open_Scopes
(Scope
(T
))
7330 -- In the generic, the full type was visible. Save the private
7331 -- entity, for subsequent exchange.
7335 elsif Has_Private_View
(N
)
7336 and then not Is_Private_Type
(T
)
7337 and then not Has_Been_Exchanged
(T
)
7338 and then Etype
(Get_Associated_Node
(N
)) /= T
7340 -- Only the private declaration was visible in the generic. If
7341 -- the type appears in a subtype declaration, the subtype in the
7342 -- instance must have a view compatible with that of its parent,
7343 -- which must be exchanged (see corresponding code in Restore_
7344 -- Private_Views). Otherwise, if the type is defined in a parent
7345 -- unit, leave full visibility within instance, which is safe.
7347 if In_Open_Scopes
(Scope
(Base_Type
(T
)))
7348 and then not Is_Private_Type
(Base_Type
(T
))
7349 and then Comes_From_Source
(Base_Type
(T
))
7353 elsif Nkind
(Parent
(N
)) = N_Subtype_Declaration
7354 or else not In_Private_Part
(Scope
(Base_Type
(T
)))
7356 Prepend_Elmt
(T
, Exchanged_Views
);
7357 Exchange_Declarations
(Etype
(Get_Associated_Node
(N
)));
7360 -- For composite types with inconsistent representation exchange
7361 -- component types accordingly.
7363 elsif Is_Access_Type
(T
)
7364 and then Is_Private_Type
(Designated_Type
(T
))
7365 and then not Has_Private_View
(N
)
7366 and then Present
(Full_View
(Designated_Type
(T
)))
7368 Switch_View
(Designated_Type
(T
));
7370 elsif Is_Array_Type
(T
) then
7371 if Is_Private_Type
(Component_Type
(T
))
7372 and then not Has_Private_View
(N
)
7373 and then Present
(Full_View
(Component_Type
(T
)))
7375 Switch_View
(Component_Type
(T
));
7378 -- The normal exchange mechanism relies on the setting of a
7379 -- flag on the reference in the generic. However, an additional
7380 -- mechanism is needed for types that are not explicitly
7381 -- mentioned in the generic, but may be needed in expanded code
7382 -- in the instance. This includes component types of arrays and
7383 -- designated types of access types. This processing must also
7384 -- include the index types of arrays which we take care of here.
7391 Indx
:= First_Index
(T
);
7392 while Present
(Indx
) loop
7393 Typ
:= Base_Type
(Etype
(Indx
));
7395 if Is_Private_Type
(Typ
)
7396 and then Present
(Full_View
(Typ
))
7405 elsif Is_Private_Type
(T
)
7406 and then Present
(Full_View
(T
))
7407 and then Is_Array_Type
(Full_View
(T
))
7408 and then Is_Private_Type
(Component_Type
(Full_View
(T
)))
7412 -- Finally, a non-private subtype may have a private base type, which
7413 -- must be exchanged for consistency. This can happen when a package
7414 -- body is instantiated, when the scope stack is empty but in fact
7415 -- the subtype and the base type are declared in an enclosing scope.
7417 -- Note that in this case we introduce an inconsistency in the view
7418 -- set, because we switch the base type BT, but there could be some
7419 -- private dependent subtypes of BT which remain unswitched. Such
7420 -- subtypes might need to be switched at a later point (see specific
7421 -- provision for that case in Switch_View).
7423 elsif not Is_Private_Type
(T
)
7424 and then not Has_Private_View
(N
)
7425 and then Is_Private_Type
(BT
)
7426 and then Present
(Full_View
(BT
))
7427 and then not Is_Generic_Type
(BT
)
7428 and then not In_Open_Scopes
(BT
)
7430 Prepend_Elmt
(Full_View
(BT
), Exchanged_Views
);
7431 Exchange_Declarations
(BT
);
7434 end Check_Private_View
;
7436 -----------------------------
7437 -- Check_Hidden_Primitives --
7438 -----------------------------
7440 function Check_Hidden_Primitives
(Assoc_List
: List_Id
) return Elist_Id
is
7443 Result
: Elist_Id
:= No_Elist
;
7446 if No
(Assoc_List
) then
7450 -- Traverse the list of associations between formals and actuals
7451 -- searching for renamings of tagged types
7453 Actual
:= First
(Assoc_List
);
7454 while Present
(Actual
) loop
7455 if Nkind
(Actual
) = N_Subtype_Declaration
then
7456 Gen_T
:= Generic_Parent_Type
(Actual
);
7458 if Present
(Gen_T
) and then Is_Tagged_Type
(Gen_T
) then
7460 -- Traverse the list of primitives of the actual types
7461 -- searching for hidden primitives that are visible in the
7462 -- corresponding generic formal; leave them visible and
7463 -- append them to Result to restore their decoration later.
7465 Install_Hidden_Primitives
7466 (Prims_List
=> Result
,
7468 Act_T
=> Entity
(Subtype_Indication
(Actual
)));
7476 end Check_Hidden_Primitives
;
7478 --------------------------
7479 -- Contains_Instance_Of --
7480 --------------------------
7482 function Contains_Instance_Of
7485 N
: Node_Id
) return Boolean
7493 -- Verify that there are no circular instantiations. We check whether
7494 -- the unit contains an instance of the current scope or some enclosing
7495 -- scope (in case one of the instances appears in a subunit). Longer
7496 -- circularities involving subunits might seem too pathological to
7497 -- consider, but they were not too pathological for the authors of
7498 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7499 -- enclosing generic scopes as containing an instance.
7502 -- Within a generic subprogram body, the scope is not generic, to
7503 -- allow for recursive subprograms. Use the declaration to determine
7504 -- whether this is a generic unit.
7506 if Ekind
(Scop
) = E_Generic_Package
7507 or else (Is_Subprogram
(Scop
)
7508 and then Nkind
(Unit_Declaration_Node
(Scop
)) =
7509 N_Generic_Subprogram_Declaration
)
7511 Elmt
:= First_Elmt
(Inner_Instances
(Inner
));
7513 while Present
(Elmt
) loop
7514 if Node
(Elmt
) = Scop
then
7515 Error_Msg_Node_2
:= Inner
;
7517 ("circular Instantiation: & instantiated within &!",
7521 elsif Node
(Elmt
) = Inner
then
7524 elsif Contains_Instance_Of
(Node
(Elmt
), Scop
, N
) then
7525 Error_Msg_Node_2
:= Inner
;
7527 ("circular Instantiation: & instantiated within &!",
7535 -- Indicate that Inner is being instantiated within Scop
7537 Append_Elmt
(Inner
, Inner_Instances
(Scop
));
7540 if Scop
= Standard_Standard
then
7543 Scop
:= Scope
(Scop
);
7548 end Contains_Instance_Of
;
7550 -----------------------
7551 -- Copy_Generic_Node --
7552 -----------------------
7554 function Copy_Generic_Node
7556 Parent_Id
: Node_Id
;
7557 Instantiating
: Boolean) return Node_Id
7562 function Copy_Generic_Descendant
(D
: Union_Id
) return Union_Id
;
7563 -- Check the given value of one of the Fields referenced by the current
7564 -- node to determine whether to copy it recursively. The field may hold
7565 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7566 -- Char) in which case it need not be copied.
7568 procedure Copy_Descendants
;
7569 -- Common utility for various nodes
7571 function Copy_Generic_Elist
(E
: Elist_Id
) return Elist_Id
;
7572 -- Make copy of element list
7574 function Copy_Generic_List
7576 Parent_Id
: Node_Id
) return List_Id
;
7577 -- Apply Copy_Node recursively to the members of a node list
7579 function In_Defining_Unit_Name
(Nam
: Node_Id
) return Boolean;
7580 -- True if an identifier is part of the defining program unit name of
7581 -- a child unit. The entity of such an identifier must be kept (for
7582 -- ASIS use) even though as the name of an enclosing generic it would
7583 -- otherwise not be preserved in the generic tree.
7585 ----------------------
7586 -- Copy_Descendants --
7587 ----------------------
7589 procedure Copy_Descendants
is
7590 use Atree
.Unchecked_Access
;
7591 -- This code section is part of the implementation of an untyped
7592 -- tree traversal, so it needs direct access to node fields.
7595 Set_Field1
(New_N
, Copy_Generic_Descendant
(Field1
(N
)));
7596 Set_Field2
(New_N
, Copy_Generic_Descendant
(Field2
(N
)));
7597 Set_Field3
(New_N
, Copy_Generic_Descendant
(Field3
(N
)));
7598 Set_Field4
(New_N
, Copy_Generic_Descendant
(Field4
(N
)));
7599 Set_Field5
(New_N
, Copy_Generic_Descendant
(Field5
(N
)));
7600 end Copy_Descendants
;
7602 -----------------------------
7603 -- Copy_Generic_Descendant --
7604 -----------------------------
7606 function Copy_Generic_Descendant
(D
: Union_Id
) return Union_Id
is
7608 if D
= Union_Id
(Empty
) then
7611 elsif D
in Node_Range
then
7613 (Copy_Generic_Node
(Node_Id
(D
), New_N
, Instantiating
));
7615 elsif D
in List_Range
then
7616 return Union_Id
(Copy_Generic_List
(List_Id
(D
), New_N
));
7618 elsif D
in Elist_Range
then
7619 return Union_Id
(Copy_Generic_Elist
(Elist_Id
(D
)));
7621 -- Nothing else is copyable (e.g. Uint values), return as is
7626 end Copy_Generic_Descendant
;
7628 ------------------------
7629 -- Copy_Generic_Elist --
7630 ------------------------
7632 function Copy_Generic_Elist
(E
: Elist_Id
) return Elist_Id
is
7639 M
:= First_Elmt
(E
);
7640 while Present
(M
) loop
7642 (Copy_Generic_Node
(Node
(M
), Empty
, Instantiating
), L
);
7651 end Copy_Generic_Elist
;
7653 -----------------------
7654 -- Copy_Generic_List --
7655 -----------------------
7657 function Copy_Generic_List
7659 Parent_Id
: Node_Id
) return List_Id
7667 Set_Parent
(New_L
, Parent_Id
);
7670 while Present
(N
) loop
7671 Append
(Copy_Generic_Node
(N
, Empty
, Instantiating
), New_L
);
7680 end Copy_Generic_List
;
7682 ---------------------------
7683 -- In_Defining_Unit_Name --
7684 ---------------------------
7686 function In_Defining_Unit_Name
(Nam
: Node_Id
) return Boolean is
7689 Present
(Parent
(Nam
))
7690 and then (Nkind
(Parent
(Nam
)) = N_Defining_Program_Unit_Name
7692 (Nkind
(Parent
(Nam
)) = N_Expanded_Name
7693 and then In_Defining_Unit_Name
(Parent
(Nam
))));
7694 end In_Defining_Unit_Name
;
7696 -- Start of processing for Copy_Generic_Node
7703 New_N
:= New_Copy
(N
);
7705 -- Copy aspects if present
7707 if Has_Aspects
(N
) then
7708 Set_Has_Aspects
(New_N
, False);
7709 Set_Aspect_Specifications
7710 (New_N
, Copy_Generic_List
(Aspect_Specifications
(N
), Parent_Id
));
7713 -- If we are instantiating, we want to adjust the sloc based on the
7714 -- current S_Adjustment. However, if this is the root node of a subunit,
7715 -- we need to defer that adjustment to below (see "elsif Instantiating
7716 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
7717 -- computed the adjustment.
7720 and then not (Nkind
(N
) in N_Proper_Body
7721 and then Was_Originally_Stub
(N
))
7723 Adjust_Instantiation_Sloc
(New_N
, S_Adjustment
);
7726 if not Is_List_Member
(N
) then
7727 Set_Parent
(New_N
, Parent_Id
);
7730 -- Special casing for identifiers and other entity names and operators
7732 if Nkind_In
(New_N
, N_Character_Literal
,
7736 or else Nkind
(New_N
) in N_Op
7738 if not Instantiating
then
7740 -- Link both nodes in order to assign subsequently the entity of
7741 -- the copy to the original node, in case this is a global
7744 Set_Associated_Node
(N
, New_N
);
7746 -- If we are within an instantiation, this is a nested generic
7747 -- that has already been analyzed at the point of definition.
7748 -- We must preserve references that were global to the enclosing
7749 -- parent at that point. Other occurrences, whether global or
7750 -- local to the current generic, must be resolved anew, so we
7751 -- reset the entity in the generic copy. A global reference has a
7752 -- smaller depth than the parent, or else the same depth in case
7753 -- both are distinct compilation units.
7755 -- A child unit is implicitly declared within the enclosing parent
7756 -- but is in fact global to it, and must be preserved.
7758 -- It is also possible for Current_Instantiated_Parent to be
7759 -- defined, and for this not to be a nested generic, namely if
7760 -- the unit is loaded through Rtsfind. In that case, the entity of
7761 -- New_N is only a link to the associated node, and not a defining
7764 -- The entities for parent units in the defining_program_unit of a
7765 -- generic child unit are established when the context of the unit
7766 -- is first analyzed, before the generic copy is made. They are
7767 -- preserved in the copy for use in ASIS queries.
7769 Ent
:= Entity
(New_N
);
7771 if No
(Current_Instantiated_Parent
.Gen_Id
) then
7773 or else Nkind
(Ent
) /= N_Defining_Identifier
7774 or else not In_Defining_Unit_Name
(N
)
7776 Set_Associated_Node
(New_N
, Empty
);
7781 not Nkind_In
(Ent
, N_Defining_Identifier
,
7782 N_Defining_Character_Literal
,
7783 N_Defining_Operator_Symbol
)
7784 or else No
(Scope
(Ent
))
7786 (Scope
(Ent
) = Current_Instantiated_Parent
.Gen_Id
7787 and then not Is_Child_Unit
(Ent
))
7789 (Scope_Depth
(Scope
(Ent
)) >
7790 Scope_Depth
(Current_Instantiated_Parent
.Gen_Id
)
7792 Get_Source_Unit
(Ent
) =
7793 Get_Source_Unit
(Current_Instantiated_Parent
.Gen_Id
))
7795 Set_Associated_Node
(New_N
, Empty
);
7798 -- Case of instantiating identifier or some other name or operator
7801 -- If the associated node is still defined, the entity in it
7802 -- is global, and must be copied to the instance. If this copy
7803 -- is being made for a body to inline, it is applied to an
7804 -- instantiated tree, and the entity is already present and
7805 -- must be also preserved.
7808 Assoc
: constant Node_Id
:= Get_Associated_Node
(N
);
7811 if Present
(Assoc
) then
7812 if Nkind
(Assoc
) = Nkind
(N
) then
7813 Set_Entity
(New_N
, Entity
(Assoc
));
7814 Check_Private_View
(N
);
7816 -- The node is a reference to a global type and acts as the
7817 -- subtype mark of a qualified expression created in order
7818 -- to aid resolution of accidental overloading in instances.
7819 -- Since N is a reference to a type, the Associated_Node of
7820 -- N denotes an entity rather than another identifier. See
7821 -- Qualify_Universal_Operands for details.
7823 elsif Nkind
(N
) = N_Identifier
7824 and then Nkind
(Parent
(N
)) = N_Qualified_Expression
7825 and then Subtype_Mark
(Parent
(N
)) = N
7826 and then Is_Qualified_Universal_Literal
(Parent
(N
))
7828 Set_Entity
(New_N
, Assoc
);
7830 -- The name in the call may be a selected component if the
7831 -- call has not been analyzed yet, as may be the case for
7832 -- pre/post conditions in a generic unit.
7834 elsif Nkind
(Assoc
) = N_Function_Call
7835 and then Is_Entity_Name
(Name
(Assoc
))
7837 Set_Entity
(New_N
, Entity
(Name
(Assoc
)));
7839 elsif Nkind_In
(Assoc
, N_Defining_Identifier
,
7840 N_Defining_Character_Literal
,
7841 N_Defining_Operator_Symbol
)
7842 and then Expander_Active
7844 -- Inlining case: we are copying a tree that contains
7845 -- global entities, which are preserved in the copy to be
7846 -- used for subsequent inlining.
7851 Set_Entity
(New_N
, Empty
);
7857 -- For expanded name, we must copy the Prefix and Selector_Name
7859 if Nkind
(N
) = N_Expanded_Name
then
7861 (New_N
, Copy_Generic_Node
(Prefix
(N
), New_N
, Instantiating
));
7863 Set_Selector_Name
(New_N
,
7864 Copy_Generic_Node
(Selector_Name
(N
), New_N
, Instantiating
));
7866 -- For operators, copy the operands
7868 elsif Nkind
(N
) in N_Op
then
7869 if Nkind
(N
) in N_Binary_Op
then
7870 Set_Left_Opnd
(New_N
,
7871 Copy_Generic_Node
(Left_Opnd
(N
), New_N
, Instantiating
));
7874 Set_Right_Opnd
(New_N
,
7875 Copy_Generic_Node
(Right_Opnd
(N
), New_N
, Instantiating
));
7878 -- Establish a link between an entity from the generic template and the
7879 -- corresponding entity in the generic copy to be analyzed.
7881 elsif Nkind
(N
) in N_Entity
then
7882 if not Instantiating
then
7883 Set_Associated_Entity
(N
, New_N
);
7886 -- Clear any existing link the copy may inherit from the replicated
7887 -- generic template entity.
7889 Set_Associated_Entity
(New_N
, Empty
);
7891 -- Special casing for stubs
7893 elsif Nkind
(N
) in N_Body_Stub
then
7895 -- In any case, we must copy the specification or defining
7896 -- identifier as appropriate.
7898 if Nkind
(N
) = N_Subprogram_Body_Stub
then
7899 Set_Specification
(New_N
,
7900 Copy_Generic_Node
(Specification
(N
), New_N
, Instantiating
));
7903 Set_Defining_Identifier
(New_N
,
7905 (Defining_Identifier
(N
), New_N
, Instantiating
));
7908 -- If we are not instantiating, then this is where we load and
7909 -- analyze subunits, i.e. at the point where the stub occurs. A
7910 -- more permissive system might defer this analysis to the point
7911 -- of instantiation, but this seems too complicated for now.
7913 if not Instantiating
then
7915 Subunit_Name
: constant Unit_Name_Type
:= Get_Unit_Name
(N
);
7917 Unum
: Unit_Number_Type
;
7921 -- Make sure that, if it is a subunit of the main unit that is
7922 -- preprocessed and if -gnateG is specified, the preprocessed
7923 -- file will be written.
7925 Lib
.Analysing_Subunit_Of_Main
:=
7926 Lib
.In_Extended_Main_Source_Unit
(N
);
7929 (Load_Name
=> Subunit_Name
,
7933 Lib
.Analysing_Subunit_Of_Main
:= False;
7935 -- If the proper body is not found, a warning message will be
7936 -- emitted when analyzing the stub, or later at the point of
7937 -- instantiation. Here we just leave the stub as is.
7939 if Unum
= No_Unit
then
7940 Subunits_Missing
:= True;
7941 goto Subunit_Not_Found
;
7944 Subunit
:= Cunit
(Unum
);
7946 if Nkind
(Unit
(Subunit
)) /= N_Subunit
then
7948 ("found child unit instead of expected SEPARATE subunit",
7950 Error_Msg_Sloc
:= Sloc
(N
);
7951 Error_Msg_N
("\to complete stub #", Subunit
);
7952 goto Subunit_Not_Found
;
7955 -- We must create a generic copy of the subunit, in order to
7956 -- perform semantic analysis on it, and we must replace the
7957 -- stub in the original generic unit with the subunit, in order
7958 -- to preserve non-local references within.
7960 -- Only the proper body needs to be copied. Library_Unit and
7961 -- context clause are simply inherited by the generic copy.
7962 -- Note that the copy (which may be recursive if there are
7963 -- nested subunits) must be done first, before attaching it to
7964 -- the enclosing generic.
7968 (Proper_Body
(Unit
(Subunit
)),
7969 Empty
, Instantiating
=> False);
7971 -- Now place the original proper body in the original generic
7972 -- unit. This is a body, not a compilation unit.
7974 Rewrite
(N
, Proper_Body
(Unit
(Subunit
)));
7975 Set_Is_Compilation_Unit
(Defining_Entity
(N
), False);
7976 Set_Was_Originally_Stub
(N
);
7978 -- Finally replace the body of the subunit with its copy, and
7979 -- make this new subunit into the library unit of the generic
7980 -- copy, which does not have stubs any longer.
7982 Set_Proper_Body
(Unit
(Subunit
), New_Body
);
7983 Set_Library_Unit
(New_N
, Subunit
);
7984 Inherit_Context
(Unit
(Subunit
), N
);
7987 -- If we are instantiating, this must be an error case, since
7988 -- otherwise we would have replaced the stub node by the proper body
7989 -- that corresponds. So just ignore it in the copy (i.e. we have
7990 -- copied it, and that is good enough).
7996 <<Subunit_Not_Found
>> null;
7998 -- If the node is a compilation unit, it is the subunit of a stub, which
7999 -- has been loaded already (see code below). In this case, the library
8000 -- unit field of N points to the parent unit (which is a compilation
8001 -- unit) and need not (and cannot) be copied.
8003 -- When the proper body of the stub is analyzed, the library_unit link
8004 -- is used to establish the proper context (see sem_ch10).
8006 -- The other fields of a compilation unit are copied as usual
8008 elsif Nkind
(N
) = N_Compilation_Unit
then
8010 -- This code can only be executed when not instantiating, because in
8011 -- the copy made for an instantiation, the compilation unit node has
8012 -- disappeared at the point that a stub is replaced by its proper
8015 pragma Assert
(not Instantiating
);
8017 Set_Context_Items
(New_N
,
8018 Copy_Generic_List
(Context_Items
(N
), New_N
));
8021 Copy_Generic_Node
(Unit
(N
), New_N
, Instantiating
=> False));
8023 Set_First_Inlined_Subprogram
(New_N
,
8025 (First_Inlined_Subprogram
(N
), New_N
, Instantiating
=> False));
8030 (Aux_Decls_Node
(N
), New_N
, Instantiating
=> False));
8032 -- For an assignment node, the assignment is known to be semantically
8033 -- legal if we are instantiating the template. This avoids incorrect
8034 -- diagnostics in generated code.
8036 elsif Nkind
(N
) = N_Assignment_Statement
then
8038 -- Copy name and expression fields in usual manner
8041 Copy_Generic_Node
(Name
(N
), New_N
, Instantiating
));
8043 Set_Expression
(New_N
,
8044 Copy_Generic_Node
(Expression
(N
), New_N
, Instantiating
));
8046 if Instantiating
then
8047 Set_Assignment_OK
(Name
(New_N
), True);
8050 elsif Nkind_In
(N
, N_Aggregate
, N_Extension_Aggregate
) then
8051 if not Instantiating
then
8052 Set_Associated_Node
(N
, New_N
);
8055 if Present
(Get_Associated_Node
(N
))
8056 and then Nkind
(Get_Associated_Node
(N
)) = Nkind
(N
)
8058 -- In the generic the aggregate has some composite type. If at
8059 -- the point of instantiation the type has a private view,
8060 -- install the full view (and that of its ancestors, if any).
8063 T
: Entity_Id
:= (Etype
(Get_Associated_Node
(New_N
)));
8067 if Present
(T
) and then Is_Private_Type
(T
) then
8072 and then Is_Tagged_Type
(T
)
8073 and then Is_Derived_Type
(T
)
8075 Rt
:= Root_Type
(T
);
8080 if Is_Private_Type
(T
) then
8091 -- Do not copy the associated node, which points to the generic copy
8092 -- of the aggregate.
8095 use Atree
.Unchecked_Access
;
8096 -- This code section is part of the implementation of an untyped
8097 -- tree traversal, so it needs direct access to node fields.
8100 Set_Field1
(New_N
, Copy_Generic_Descendant
(Field1
(N
)));
8101 Set_Field2
(New_N
, Copy_Generic_Descendant
(Field2
(N
)));
8102 Set_Field3
(New_N
, Copy_Generic_Descendant
(Field3
(N
)));
8103 Set_Field5
(New_N
, Copy_Generic_Descendant
(Field5
(N
)));
8106 -- Allocators do not have an identifier denoting the access type, so we
8107 -- must locate it through the expression to check whether the views are
8110 elsif Nkind
(N
) = N_Allocator
8111 and then Nkind
(Expression
(N
)) = N_Qualified_Expression
8112 and then Is_Entity_Name
(Subtype_Mark
(Expression
(N
)))
8113 and then Instantiating
8116 T
: constant Node_Id
:=
8117 Get_Associated_Node
(Subtype_Mark
(Expression
(N
)));
8123 -- Retrieve the allocator node in the generic copy
8125 Acc_T
:= Etype
(Parent
(Parent
(T
)));
8127 if Present
(Acc_T
) and then Is_Private_Type
(Acc_T
) then
8128 Switch_View
(Acc_T
);
8135 -- For a proper body, we must catch the case of a proper body that
8136 -- replaces a stub. This represents the point at which a separate
8137 -- compilation unit, and hence template file, may be referenced, so we
8138 -- must make a new source instantiation entry for the template of the
8139 -- subunit, and ensure that all nodes in the subunit are adjusted using
8140 -- this new source instantiation entry.
8142 elsif Nkind
(N
) in N_Proper_Body
then
8144 Save_Adjustment
: constant Sloc_Adjustment
:= S_Adjustment
;
8146 if Instantiating
and then Was_Originally_Stub
(N
) then
8147 Create_Instantiation_Source
8148 (Instantiation_Node
,
8149 Defining_Entity
(N
),
8152 Adjust_Instantiation_Sloc
(New_N
, S_Adjustment
);
8155 -- Now copy the fields of the proper body, using the new
8156 -- adjustment factor if one was needed as per test above.
8160 -- Restore the original adjustment factor
8162 S_Adjustment
:= Save_Adjustment
;
8165 elsif Nkind
(N
) = N_Pragma
and then Instantiating
then
8167 -- Do not copy Comment or Ident pragmas their content is relevant to
8168 -- the generic unit, not to the instantiating unit.
8170 if Nam_In
(Pragma_Name_Unmapped
(N
), Name_Comment
, Name_Ident
) then
8171 New_N
:= Make_Null_Statement
(Sloc
(N
));
8173 -- Do not copy pragmas generated from aspects because the pragmas do
8174 -- not carry any semantic information, plus they will be regenerated
8177 -- However, generating C we need to copy them since postconditions
8178 -- are inlined by the front end, and the front-end inlining machinery
8179 -- relies on this routine to perform inlining.
8181 elsif From_Aspect_Specification
(N
)
8182 and then not Modify_Tree_For_C
8184 New_N
:= Make_Null_Statement
(Sloc
(N
));
8190 elsif Nkind_In
(N
, N_Integer_Literal
, N_Real_Literal
) then
8192 -- No descendant fields need traversing
8196 elsif Nkind
(N
) = N_String_Literal
8197 and then Present
(Etype
(N
))
8198 and then Instantiating
8200 -- If the string is declared in an outer scope, the string_literal
8201 -- subtype created for it may have the wrong scope. Force reanalysis
8202 -- of the constant to generate a new itype in the proper context.
8204 Set_Etype
(New_N
, Empty
);
8205 Set_Analyzed
(New_N
, False);
8207 -- For the remaining nodes, copy their descendants recursively
8212 if Instantiating
and then Nkind
(N
) = N_Subprogram_Body
then
8213 Set_Generic_Parent
(Specification
(New_N
), N
);
8215 -- Should preserve Corresponding_Spec??? (12.3(14))
8219 -- Propagate dimensions if present, so that they are reflected in the
8222 if Nkind
(N
) in N_Has_Etype
8223 and then (Nkind
(N
) in N_Op
or else Is_Entity_Name
(N
))
8224 and then Present
(Etype
(N
))
8225 and then Is_Floating_Point_Type
(Etype
(N
))
8226 and then Has_Dimension_System
(Etype
(N
))
8228 Copy_Dimensions
(N
, New_N
);
8232 end Copy_Generic_Node
;
8234 ----------------------------
8235 -- Denotes_Formal_Package --
8236 ----------------------------
8238 function Denotes_Formal_Package
8240 On_Exit
: Boolean := False;
8241 Instance
: Entity_Id
:= Empty
) return Boolean
8244 Scop
: constant Entity_Id
:= Scope
(Pack
);
8247 function Is_Actual_Of_Previous_Formal
(P
: Entity_Id
) return Boolean;
8248 -- The package in question may be an actual for a previous formal
8249 -- package P of the current instance, so examine its actuals as well.
8250 -- This must be recursive over other formal packages.
8252 ----------------------------------
8253 -- Is_Actual_Of_Previous_Formal --
8254 ----------------------------------
8256 function Is_Actual_Of_Previous_Formal
(P
: Entity_Id
) return Boolean is
8260 E1
:= First_Entity
(P
);
8261 while Present
(E1
) and then E1
/= Instance
loop
8262 if Ekind
(E1
) = E_Package
8263 and then Nkind
(Parent
(E1
)) = N_Package_Renaming_Declaration
8265 if Renamed_Object
(E1
) = Pack
then
8268 elsif E1
= P
or else Renamed_Object
(E1
) = P
then
8271 elsif Is_Actual_Of_Previous_Formal
(E1
) then
8280 end Is_Actual_Of_Previous_Formal
;
8282 -- Start of processing for Denotes_Formal_Package
8288 (Instance_Envs
.Last
).Instantiated_Parent
.Act_Id
;
8290 Par
:= Current_Instantiated_Parent
.Act_Id
;
8293 if Ekind
(Scop
) = E_Generic_Package
8294 or else Nkind
(Unit_Declaration_Node
(Scop
)) =
8295 N_Generic_Subprogram_Declaration
8299 elsif Nkind
(Original_Node
(Unit_Declaration_Node
(Pack
))) =
8300 N_Formal_Package_Declaration
8308 -- Check whether this package is associated with a formal package of
8309 -- the enclosing instantiation. Iterate over the list of renamings.
8311 E
:= First_Entity
(Par
);
8312 while Present
(E
) loop
8313 if Ekind
(E
) /= E_Package
8314 or else Nkind
(Parent
(E
)) /= N_Package_Renaming_Declaration
8318 elsif Renamed_Object
(E
) = Par
then
8321 elsif Renamed_Object
(E
) = Pack
then
8324 elsif Is_Actual_Of_Previous_Formal
(E
) then
8334 end Denotes_Formal_Package
;
8340 procedure End_Generic
is
8342 -- ??? More things could be factored out in this routine. Should
8343 -- probably be done at a later stage.
8345 Inside_A_Generic
:= Generic_Flags
.Table
(Generic_Flags
.Last
);
8346 Generic_Flags
.Decrement_Last
;
8348 Expander_Mode_Restore
;
8355 function Earlier
(N1
, N2
: Node_Id
) return Boolean is
8356 procedure Find_Depth
(P
: in out Node_Id
; D
: in out Integer);
8357 -- Find distance from given node to enclosing compilation unit
8363 procedure Find_Depth
(P
: in out Node_Id
; D
: in out Integer) is
8366 and then Nkind
(P
) /= N_Compilation_Unit
8368 P
:= True_Parent
(P
);
8373 -- Local declarations
8382 -- Start of processing for Earlier
8385 Find_Depth
(P1
, D1
);
8386 Find_Depth
(P2
, D2
);
8396 P1
:= True_Parent
(P1
);
8401 P2
:= True_Parent
(P2
);
8405 -- At this point P1 and P2 are at the same distance from the root.
8406 -- We examine their parents until we find a common declarative list.
8407 -- If we reach the root, N1 and N2 do not descend from the same
8408 -- declarative list (e.g. one is nested in the declarative part and
8409 -- the other is in a block in the statement part) and the earlier
8410 -- one is already frozen.
8412 while not Is_List_Member
(P1
)
8413 or else not Is_List_Member
(P2
)
8414 or else List_Containing
(P1
) /= List_Containing
(P2
)
8416 P1
:= True_Parent
(P1
);
8417 P2
:= True_Parent
(P2
);
8419 if Nkind
(Parent
(P1
)) = N_Subunit
then
8420 P1
:= Corresponding_Stub
(Parent
(P1
));
8423 if Nkind
(Parent
(P2
)) = N_Subunit
then
8424 P2
:= Corresponding_Stub
(Parent
(P2
));
8432 -- Expanded code usually shares the source location of the original
8433 -- construct it was generated for. This however may not necessarily
8434 -- reflect the true location of the code within the tree.
8436 -- Before comparing the slocs of the two nodes, make sure that we are
8437 -- working with correct source locations. Assume that P1 is to the left
8438 -- of P2. If either one does not come from source, traverse the common
8439 -- list heading towards the other node and locate the first source
8443 -- ----+===+===+--------------+===+===+----
8444 -- expanded code expanded code
8446 if not Comes_From_Source
(P1
) then
8447 while Present
(P1
) loop
8449 -- Neither P2 nor a source statement were located during the
8450 -- search. If we reach the end of the list, then P1 does not
8451 -- occur earlier than P2.
8454 -- start --- P2 ----- P1 --- end
8456 if No
(Next
(P1
)) then
8459 -- We encounter P2 while going to the right of the list. This
8460 -- means that P1 does indeed appear earlier.
8463 -- start --- P1 ===== P2 --- end
8464 -- expanded code in between
8469 -- No need to look any further since we have located a source
8472 elsif Comes_From_Source
(P1
) then
8482 if not Comes_From_Source
(P2
) then
8483 while Present
(P2
) loop
8485 -- Neither P1 nor a source statement were located during the
8486 -- search. If we reach the start of the list, then P1 does not
8487 -- occur earlier than P2.
8490 -- start --- P2 --- P1 --- end
8492 if No
(Prev
(P2
)) then
8495 -- We encounter P1 while going to the left of the list. This
8496 -- means that P1 does indeed appear earlier.
8499 -- start --- P1 ===== P2 --- end
8500 -- expanded code in between
8505 -- No need to look any further since we have located a source
8508 elsif Comes_From_Source
(P2
) then
8518 -- At this point either both nodes came from source or we approximated
8519 -- their source locations through neighboring source statements.
8521 T1
:= Top_Level_Location
(Sloc
(P1
));
8522 T2
:= Top_Level_Location
(Sloc
(P2
));
8524 -- When two nodes come from the same instance, they have identical top
8525 -- level locations. To determine proper relation within the tree, check
8526 -- their locations within the template.
8529 return Sloc
(P1
) < Sloc
(P2
);
8531 -- The two nodes either come from unrelated instances or do not come
8532 -- from instantiated code at all.
8539 ----------------------
8540 -- Find_Actual_Type --
8541 ----------------------
8543 function Find_Actual_Type
8545 Gen_Type
: Entity_Id
) return Entity_Id
8547 Gen_Scope
: constant Entity_Id
:= Scope
(Gen_Type
);
8551 -- Special processing only applies to child units
8553 if not Is_Child_Unit
(Gen_Scope
) then
8554 return Get_Instance_Of
(Typ
);
8556 -- If designated or component type is itself a formal of the child unit,
8557 -- its instance is available.
8559 elsif Scope
(Typ
) = Gen_Scope
then
8560 return Get_Instance_Of
(Typ
);
8562 -- If the array or access type is not declared in the parent unit,
8563 -- no special processing needed.
8565 elsif not Is_Generic_Type
(Typ
)
8566 and then Scope
(Gen_Scope
) /= Scope
(Typ
)
8568 return Get_Instance_Of
(Typ
);
8570 -- Otherwise, retrieve designated or component type by visibility
8573 T
:= Current_Entity
(Typ
);
8574 while Present
(T
) loop
8575 if In_Open_Scopes
(Scope
(T
)) then
8577 elsif Is_Generic_Actual_Type
(T
) then
8586 end Find_Actual_Type
;
8588 ----------------------------
8589 -- Freeze_Subprogram_Body --
8590 ----------------------------
8592 procedure Freeze_Subprogram_Body
8593 (Inst_Node
: Node_Id
;
8595 Pack_Id
: Entity_Id
)
8597 Gen_Unit
: constant Entity_Id
:= Get_Generic_Entity
(Inst_Node
);
8598 Par
: constant Entity_Id
:= Scope
(Gen_Unit
);
8604 function Enclosing_Package_Body
(N
: Node_Id
) return Node_Id
;
8605 -- Find innermost package body that encloses the given node, and which
8606 -- is not a compilation unit. Freeze nodes for the instance, or for its
8607 -- enclosing body, may be inserted after the enclosing_body of the
8608 -- generic unit. Used to determine proper placement of freeze node for
8609 -- both package and subprogram instances.
8611 function Package_Freeze_Node
(B
: Node_Id
) return Node_Id
;
8612 -- Find entity for given package body, and locate or create a freeze
8615 ----------------------------
8616 -- Enclosing_Package_Body --
8617 ----------------------------
8619 function Enclosing_Package_Body
(N
: Node_Id
) return Node_Id
is
8625 and then Nkind
(Parent
(P
)) /= N_Compilation_Unit
8627 if Nkind
(P
) = N_Package_Body
then
8628 if Nkind
(Parent
(P
)) = N_Subunit
then
8629 return Corresponding_Stub
(Parent
(P
));
8635 P
:= True_Parent
(P
);
8639 end Enclosing_Package_Body
;
8641 -------------------------
8642 -- Package_Freeze_Node --
8643 -------------------------
8645 function Package_Freeze_Node
(B
: Node_Id
) return Node_Id
is
8649 if Nkind
(B
) = N_Package_Body
then
8650 Id
:= Corresponding_Spec
(B
);
8651 else pragma Assert
(Nkind
(B
) = N_Package_Body_Stub
);
8652 Id
:= Corresponding_Spec
(Proper_Body
(Unit
(Library_Unit
(B
))));
8655 Ensure_Freeze_Node
(Id
);
8656 return Freeze_Node
(Id
);
8657 end Package_Freeze_Node
;
8659 -- Start of processing for Freeze_Subprogram_Body
8662 -- If the instance and the generic body appear within the same unit, and
8663 -- the instance precedes the generic, the freeze node for the instance
8664 -- must appear after that of the generic. If the generic is nested
8665 -- within another instance I2, then current instance must be frozen
8666 -- after I2. In both cases, the freeze nodes are those of enclosing
8667 -- packages. Otherwise, the freeze node is placed at the end of the
8668 -- current declarative part.
8670 Enc_G
:= Enclosing_Package_Body
(Gen_Body
);
8671 Enc_I
:= Enclosing_Package_Body
(Inst_Node
);
8672 Ensure_Freeze_Node
(Pack_Id
);
8673 F_Node
:= Freeze_Node
(Pack_Id
);
8675 if Is_Generic_Instance
(Par
)
8676 and then Present
(Freeze_Node
(Par
))
8677 and then In_Same_Declarative_Part
8678 (Parent
(Freeze_Node
(Par
)), Inst_Node
)
8680 -- The parent was a premature instantiation. Insert freeze node at
8681 -- the end the current declarative part.
8683 if Is_Known_Guaranteed_ABE
(Get_Unit_Instantiation_Node
(Par
)) then
8684 Insert_Freeze_Node_For_Instance
(Inst_Node
, F_Node
);
8686 -- Handle the following case:
8688 -- package Parent_Inst is new ...
8691 -- procedure P ... -- this body freezes Parent_Inst
8693 -- package Inst is new ...
8695 -- In this particular scenario, the freeze node for Inst must be
8696 -- inserted in the same manner as that of Parent_Inst - before the
8697 -- next source body or at the end of the declarative list (body not
8698 -- available). If body P did not exist and Parent_Inst was frozen
8699 -- after Inst, either by a body following Inst or at the end of the
8700 -- declarative region, the freeze node for Inst must be inserted
8701 -- after that of Parent_Inst. This relation is established by
8702 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8704 elsif List_Containing
(Get_Unit_Instantiation_Node
(Par
)) =
8705 List_Containing
(Inst_Node
)
8706 and then Sloc
(Freeze_Node
(Par
)) < Sloc
(Inst_Node
)
8708 Insert_Freeze_Node_For_Instance
(Inst_Node
, F_Node
);
8711 Insert_After
(Freeze_Node
(Par
), F_Node
);
8714 -- The body enclosing the instance should be frozen after the body that
8715 -- includes the generic, because the body of the instance may make
8716 -- references to entities therein. If the two are not in the same
8717 -- declarative part, or if the one enclosing the instance is frozen
8718 -- already, freeze the instance at the end of the current declarative
8721 elsif Is_Generic_Instance
(Par
)
8722 and then Present
(Freeze_Node
(Par
))
8723 and then Present
(Enc_I
)
8725 if In_Same_Declarative_Part
(Parent
(Freeze_Node
(Par
)), Enc_I
)
8727 (Nkind
(Enc_I
) = N_Package_Body
8728 and then In_Same_Declarative_Part
8729 (Parent
(Freeze_Node
(Par
)), Parent
(Enc_I
)))
8731 -- The enclosing package may contain several instances. Rather
8732 -- than computing the earliest point at which to insert its freeze
8733 -- node, we place it at the end of the declarative part of the
8734 -- parent of the generic.
8736 Insert_Freeze_Node_For_Instance
8737 (Freeze_Node
(Par
), Package_Freeze_Node
(Enc_I
));
8740 Insert_Freeze_Node_For_Instance
(Inst_Node
, F_Node
);
8742 elsif Present
(Enc_G
)
8743 and then Present
(Enc_I
)
8744 and then Enc_G
/= Enc_I
8745 and then Earlier
(Inst_Node
, Gen_Body
)
8747 if Nkind
(Enc_G
) = N_Package_Body
then
8749 Corresponding_Spec
(Enc_G
);
8750 else pragma Assert
(Nkind
(Enc_G
) = N_Package_Body_Stub
);
8752 Corresponding_Spec
(Proper_Body
(Unit
(Library_Unit
(Enc_G
))));
8755 -- Freeze package that encloses instance, and place node after the
8756 -- package that encloses generic. If enclosing package is already
8757 -- frozen we have to assume it is at the proper place. This may be a
8758 -- potential ABE that requires dynamic checking. Do not add a freeze
8759 -- node if the package that encloses the generic is inside the body
8760 -- that encloses the instance, because the freeze node would be in
8761 -- the wrong scope. Additional contortions needed if the bodies are
8762 -- within a subunit.
8765 Enclosing_Body
: Node_Id
;
8768 if Nkind
(Enc_I
) = N_Package_Body_Stub
then
8769 Enclosing_Body
:= Proper_Body
(Unit
(Library_Unit
(Enc_I
)));
8771 Enclosing_Body
:= Enc_I
;
8774 if Parent
(List_Containing
(Enc_G
)) /= Enclosing_Body
then
8775 Insert_Freeze_Node_For_Instance
8776 (Enc_G
, Package_Freeze_Node
(Enc_I
));
8780 -- Freeze enclosing subunit before instance
8782 Ensure_Freeze_Node
(E_G_Id
);
8784 if not Is_List_Member
(Freeze_Node
(E_G_Id
)) then
8785 Insert_After
(Enc_G
, Freeze_Node
(E_G_Id
));
8788 Insert_Freeze_Node_For_Instance
(Inst_Node
, F_Node
);
8791 -- If none of the above, insert freeze node at the end of the current
8792 -- declarative part.
8794 Insert_Freeze_Node_For_Instance
(Inst_Node
, F_Node
);
8796 end Freeze_Subprogram_Body
;
8802 function Get_Gen_Id
(E
: Assoc_Ptr
) return Entity_Id
is
8804 return Generic_Renamings
.Table
(E
).Gen_Id
;
8807 ---------------------
8808 -- Get_Instance_Of --
8809 ---------------------
8811 function Get_Instance_Of
(A
: Entity_Id
) return Entity_Id
is
8812 Res
: constant Assoc_Ptr
:= Generic_Renamings_HTable
.Get
(A
);
8815 if Res
/= Assoc_Null
then
8816 return Generic_Renamings
.Table
(Res
).Act_Id
;
8819 -- On exit, entity is not instantiated: not a generic parameter, or
8820 -- else parameter of an inner generic unit.
8824 end Get_Instance_Of
;
8826 ---------------------------------
8827 -- Get_Unit_Instantiation_Node --
8828 ---------------------------------
8830 function Get_Unit_Instantiation_Node
(A
: Entity_Id
) return Node_Id
is
8831 Decl
: Node_Id
:= Unit_Declaration_Node
(A
);
8835 -- If the Package_Instantiation attribute has been set on the package
8836 -- entity, then use it directly when it (or its Original_Node) refers
8837 -- to an N_Package_Instantiation node. In principle it should be
8838 -- possible to have this field set in all cases, which should be
8839 -- investigated, and would allow this function to be significantly
8842 Inst
:= Package_Instantiation
(A
);
8844 if Present
(Inst
) then
8845 if Nkind
(Inst
) = N_Package_Instantiation
then
8848 elsif Nkind
(Original_Node
(Inst
)) = N_Package_Instantiation
then
8849 return Original_Node
(Inst
);
8853 -- If the instantiation is a compilation unit that does not need body
8854 -- then the instantiation node has been rewritten as a package
8855 -- declaration for the instance, and we return the original node.
8857 -- If it is a compilation unit and the instance node has not been
8858 -- rewritten, then it is still the unit of the compilation. Finally, if
8859 -- a body is present, this is a parent of the main unit whose body has
8860 -- been compiled for inlining purposes, and the instantiation node has
8861 -- been rewritten with the instance body.
8863 -- Otherwise the instantiation node appears after the declaration. If
8864 -- the entity is a formal package, the declaration may have been
8865 -- rewritten as a generic declaration (in the case of a formal with box)
8866 -- or left as a formal package declaration if it has actuals, and is
8867 -- found with a forward search.
8869 if Nkind
(Parent
(Decl
)) = N_Compilation_Unit
then
8870 if Nkind
(Decl
) = N_Package_Declaration
8871 and then Present
(Corresponding_Body
(Decl
))
8873 Decl
:= Unit_Declaration_Node
(Corresponding_Body
(Decl
));
8876 if Nkind_In
(Original_Node
(Decl
), N_Function_Instantiation
,
8877 N_Package_Instantiation
,
8878 N_Procedure_Instantiation
)
8880 return Original_Node
(Decl
);
8882 return Unit
(Parent
(Decl
));
8885 elsif Nkind
(Decl
) = N_Package_Declaration
8886 and then Nkind
(Original_Node
(Decl
)) = N_Formal_Package_Declaration
8888 return Original_Node
(Decl
);
8891 Inst
:= Next
(Decl
);
8892 while not Nkind_In
(Inst
, N_Formal_Package_Declaration
,
8893 N_Function_Instantiation
,
8894 N_Package_Instantiation
,
8895 N_Procedure_Instantiation
)
8902 end Get_Unit_Instantiation_Node
;
8904 ------------------------
8905 -- Has_Been_Exchanged --
8906 ------------------------
8908 function Has_Been_Exchanged
(E
: Entity_Id
) return Boolean is
8912 Next
:= First_Elmt
(Exchanged_Views
);
8913 while Present
(Next
) loop
8914 if Full_View
(Node
(Next
)) = E
then
8922 end Has_Been_Exchanged
;
8928 function Hash
(F
: Entity_Id
) return HTable_Range
is
8930 return HTable_Range
(F
mod HTable_Size
);
8933 ------------------------
8934 -- Hide_Current_Scope --
8935 ------------------------
8937 procedure Hide_Current_Scope
is
8938 C
: constant Entity_Id
:= Current_Scope
;
8942 Set_Is_Hidden_Open_Scope
(C
);
8944 E
:= First_Entity
(C
);
8945 while Present
(E
) loop
8946 if Is_Immediately_Visible
(E
) then
8947 Set_Is_Immediately_Visible
(E
, False);
8948 Append_Elmt
(E
, Hidden_Entities
);
8954 -- Make the scope name invisible as well. This is necessary, but might
8955 -- conflict with calls to Rtsfind later on, in case the scope is a
8956 -- predefined one. There is no clean solution to this problem, so for
8957 -- now we depend on the user not redefining Standard itself in one of
8958 -- the parent units.
8960 if Is_Immediately_Visible
(C
) and then C
/= Standard_Standard
then
8961 Set_Is_Immediately_Visible
(C
, False);
8962 Append_Elmt
(C
, Hidden_Entities
);
8965 end Hide_Current_Scope
;
8971 procedure Init_Env
is
8972 Saved
: Instance_Env
;
8975 Saved
.Instantiated_Parent
:= Current_Instantiated_Parent
;
8976 Saved
.Exchanged_Views
:= Exchanged_Views
;
8977 Saved
.Hidden_Entities
:= Hidden_Entities
;
8978 Saved
.Current_Sem_Unit
:= Current_Sem_Unit
;
8979 Saved
.Parent_Unit_Visible
:= Parent_Unit_Visible
;
8980 Saved
.Instance_Parent_Unit
:= Instance_Parent_Unit
;
8982 -- Save configuration switches. These may be reset if the unit is a
8983 -- predefined unit, and the current mode is not Ada 2005.
8985 Saved
.Switches
:= Save_Config_Switches
;
8987 Instance_Envs
.Append
(Saved
);
8989 Exchanged_Views
:= New_Elmt_List
;
8990 Hidden_Entities
:= New_Elmt_List
;
8992 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8993 -- this is set properly in Set_Instance_Env.
8995 Current_Instantiated_Parent
:=
8996 (Current_Scope
, Current_Scope
, Assoc_Null
);
8999 ---------------------
9000 -- In_Main_Context --
9001 ---------------------
9003 function In_Main_Context
(E
: Entity_Id
) return Boolean is
9009 if not Is_Compilation_Unit
(E
)
9010 or else Ekind
(E
) /= E_Package
9011 or else In_Private_Part
(E
)
9016 Context
:= Context_Items
(Cunit
(Main_Unit
));
9018 Clause
:= First
(Context
);
9019 while Present
(Clause
) loop
9020 if Nkind
(Clause
) = N_With_Clause
then
9021 Nam
:= Name
(Clause
);
9023 -- If the current scope is part of the context of the main unit,
9024 -- analysis of the corresponding with_clause is not complete, and
9025 -- the entity is not set. We use the Chars field directly, which
9026 -- might produce false positives in rare cases, but guarantees
9027 -- that we produce all the instance bodies we will need.
9029 if (Is_Entity_Name
(Nam
) and then Chars
(Nam
) = Chars
(E
))
9030 or else (Nkind
(Nam
) = N_Selected_Component
9031 and then Chars
(Selector_Name
(Nam
)) = Chars
(E
))
9041 end In_Main_Context
;
9043 ---------------------
9044 -- Inherit_Context --
9045 ---------------------
9047 procedure Inherit_Context
(Gen_Decl
: Node_Id
; Inst
: Node_Id
) is
9048 Current_Context
: List_Id
;
9049 Current_Unit
: Node_Id
;
9058 if Nkind
(Parent
(Gen_Decl
)) = N_Compilation_Unit
then
9060 -- The inherited context is attached to the enclosing compilation
9061 -- unit. This is either the main unit, or the declaration for the
9062 -- main unit (in case the instantiation appears within the package
9063 -- declaration and the main unit is its body).
9065 Current_Unit
:= Parent
(Inst
);
9066 while Present
(Current_Unit
)
9067 and then Nkind
(Current_Unit
) /= N_Compilation_Unit
9069 Current_Unit
:= Parent
(Current_Unit
);
9072 Current_Context
:= Context_Items
(Current_Unit
);
9074 Item
:= First
(Context_Items
(Parent
(Gen_Decl
)));
9075 while Present
(Item
) loop
9076 if Nkind
(Item
) = N_With_Clause
then
9077 Lib_Unit
:= Library_Unit
(Item
);
9079 -- Take care to prevent direct cyclic with's
9081 if Lib_Unit
/= Current_Unit
then
9083 -- Do not add a unit if it is already in the context
9085 Clause
:= First
(Current_Context
);
9087 while Present
(Clause
) loop
9088 if Nkind
(Clause
) = N_With_Clause
9089 and then Library_Unit
(Clause
) = Lib_Unit
9099 New_I
:= New_Copy
(Item
);
9100 Set_Implicit_With
(New_I
);
9102 Append
(New_I
, Current_Context
);
9110 end Inherit_Context
;
9116 procedure Initialize
is
9118 Generic_Renamings
.Init
;
9121 Generic_Renamings_HTable
.Reset
;
9122 Circularity_Detected
:= False;
9123 Exchanged_Views
:= No_Elist
;
9124 Hidden_Entities
:= No_Elist
;
9127 -------------------------------------
9128 -- Insert_Freeze_Node_For_Instance --
9129 -------------------------------------
9131 procedure Insert_Freeze_Node_For_Instance
9140 function Enclosing_Body
(N
: Node_Id
) return Node_Id
;
9141 -- Find enclosing package or subprogram body, if any. Freeze node may
9142 -- be placed at end of current declarative list if previous instance
9143 -- and current one have different enclosing bodies.
9145 function Previous_Instance
(Gen
: Entity_Id
) return Entity_Id
;
9146 -- Find the local instance, if any, that declares the generic that is
9147 -- being instantiated. If present, the freeze node for this instance
9148 -- must follow the freeze node for the previous instance.
9150 --------------------
9151 -- Enclosing_Body --
9152 --------------------
9154 function Enclosing_Body
(N
: Node_Id
) return Node_Id
is
9160 and then Nkind
(Parent
(P
)) /= N_Compilation_Unit
9162 if Nkind_In
(P
, N_Package_Body
, N_Subprogram_Body
) then
9163 if Nkind
(Parent
(P
)) = N_Subunit
then
9164 return Corresponding_Stub
(Parent
(P
));
9170 P
:= True_Parent
(P
);
9176 -----------------------
9177 -- Previous_Instance --
9178 -----------------------
9180 function Previous_Instance
(Gen
: Entity_Id
) return Entity_Id
is
9185 while Present
(S
) and then S
/= Standard_Standard
loop
9186 if Is_Generic_Instance
(S
)
9187 and then In_Same_Source_Unit
(S
, N
)
9196 end Previous_Instance
;
9198 -- Start of processing for Insert_Freeze_Node_For_Instance
9201 if not Is_List_Member
(F_Node
) then
9203 Decls
:= List_Containing
(N
);
9204 Inst
:= Entity
(F_Node
);
9205 Par_N
:= Parent
(Decls
);
9207 -- When processing a subprogram instantiation, utilize the actual
9208 -- subprogram instantiation rather than its package wrapper as it
9209 -- carries all the context information.
9211 if Is_Wrapper_Package
(Inst
) then
9212 Inst
:= Related_Instance
(Inst
);
9215 -- If this is a package instance, check whether the generic is
9216 -- declared in a previous instance and the current instance is
9217 -- not within the previous one.
9219 if Present
(Generic_Parent
(Parent
(Inst
)))
9220 and then Is_In_Main_Unit
(N
)
9223 Enclosing_N
: constant Node_Id
:= Enclosing_Body
(N
);
9224 Par_I
: constant Entity_Id
:=
9226 (Generic_Parent
(Parent
(Inst
)));
9231 and then Earlier
(N
, Freeze_Node
(Par_I
))
9233 Scop
:= Scope
(Inst
);
9235 -- If the current instance is within the one that contains
9236 -- the generic, the freeze node for the current one must
9237 -- appear in the current declarative part. Ditto, if the
9238 -- current instance is within another package instance or
9239 -- within a body that does not enclose the current instance.
9240 -- In these three cases the freeze node of the previous
9241 -- instance is not relevant.
9243 while Present
(Scop
) and then Scop
/= Standard_Standard
loop
9244 exit when Scop
= Par_I
9246 (Is_Generic_Instance
(Scop
)
9247 and then Scope_Depth
(Scop
) > Scope_Depth
(Par_I
));
9248 Scop
:= Scope
(Scop
);
9251 -- Previous instance encloses current instance
9253 if Scop
= Par_I
then
9256 -- If the next node is a source body we must freeze in
9257 -- the current scope as well.
9259 elsif Present
(Next
(N
))
9260 and then Nkind_In
(Next
(N
), N_Subprogram_Body
,
9262 and then Comes_From_Source
(Next
(N
))
9266 -- Current instance is within an unrelated instance
9268 elsif Is_Generic_Instance
(Scop
) then
9271 -- Current instance is within an unrelated body
9273 elsif Present
(Enclosing_N
)
9274 and then Enclosing_N
/= Enclosing_Body
(Par_I
)
9279 Insert_After
(Freeze_Node
(Par_I
), F_Node
);
9286 -- When the instantiation occurs in a package declaration, append the
9287 -- freeze node to the private declarations (if any).
9289 if Nkind
(Par_N
) = N_Package_Specification
9290 and then Decls
= Visible_Declarations
(Par_N
)
9291 and then Present
(Private_Declarations
(Par_N
))
9292 and then not Is_Empty_List
(Private_Declarations
(Par_N
))
9294 Decls
:= Private_Declarations
(Par_N
);
9295 Decl
:= First
(Decls
);
9298 -- Determine the proper freeze point of a package instantiation. We
9299 -- adhere to the general rule of a package or subprogram body causing
9300 -- freezing of anything before it in the same declarative region. In
9301 -- this case, the proper freeze point of a package instantiation is
9302 -- before the first source body which follows, or before a stub. This
9303 -- ensures that entities coming from the instance are already frozen
9304 -- and usable in source bodies.
9306 if Nkind
(Par_N
) /= N_Package_Declaration
9307 and then Ekind
(Inst
) = E_Package
9308 and then Is_Generic_Instance
(Inst
)
9310 not In_Same_Source_Unit
(Generic_Parent
(Parent
(Inst
)), Inst
)
9312 while Present
(Decl
) loop
9313 if (Nkind
(Decl
) in N_Unit_Body
9315 Nkind
(Decl
) in N_Body_Stub
)
9316 and then Comes_From_Source
(Decl
)
9318 Insert_Before
(Decl
, F_Node
);
9326 -- In a package declaration, or if no previous body, insert at end
9329 Set_Sloc
(F_Node
, Sloc
(Last
(Decls
)));
9330 Insert_After
(Last
(Decls
), F_Node
);
9332 end Insert_Freeze_Node_For_Instance
;
9338 procedure Install_Body
9339 (Act_Body
: Node_Id
;
9344 function In_Same_Scope
(Gen_Id
, Act_Id
: Node_Id
) return Boolean;
9345 -- Check if the generic definition and the instantiation come from
9346 -- a common scope, in which case the instance must be frozen after
9347 -- the generic body.
9349 function True_Sloc
(N
, Act_Unit
: Node_Id
) return Source_Ptr
;
9350 -- If the instance is nested inside a generic unit, the Sloc of the
9351 -- instance indicates the place of the original definition, not the
9352 -- point of the current enclosing instance. Pending a better usage of
9353 -- Slocs to indicate instantiation places, we determine the place of
9354 -- origin of a node by finding the maximum sloc of any ancestor node.
9355 -- Why is this not equivalent to Top_Level_Location ???
9361 function In_Same_Scope
(Gen_Id
, Act_Id
: Node_Id
) return Boolean is
9362 Act_Scop
: Entity_Id
:= Scope
(Act_Id
);
9363 Gen_Scop
: Entity_Id
:= Scope
(Gen_Id
);
9366 while Act_Scop
/= Standard_Standard
9367 and then Gen_Scop
/= Standard_Standard
9369 if Act_Scop
= Gen_Scop
then
9373 Act_Scop
:= Scope
(Act_Scop
);
9374 Gen_Scop
:= Scope
(Gen_Scop
);
9384 function True_Sloc
(N
, Act_Unit
: Node_Id
) return Source_Ptr
is
9391 while Present
(N1
) and then N1
/= Act_Unit
loop
9392 if Sloc
(N1
) > Res
then
9402 Act_Id
: constant Entity_Id
:= Corresponding_Spec
(Act_Body
);
9403 Act_Unit
: constant Node_Id
:= Unit
(Cunit
(Get_Source_Unit
(N
)));
9404 Gen_Id
: constant Entity_Id
:= Corresponding_Spec
(Gen_Body
);
9405 Par
: constant Entity_Id
:= Scope
(Gen_Id
);
9406 Gen_Unit
: constant Node_Id
:=
9407 Unit
(Cunit
(Get_Source_Unit
(Gen_Decl
)));
9409 Body_Unit
: Node_Id
;
9411 Must_Delay
: Boolean;
9412 Orig_Body
: Node_Id
:= Gen_Body
;
9414 -- Start of processing for Install_Body
9417 -- Handle first the case of an instance with incomplete actual types.
9418 -- The instance body cannot be placed after the declaration because
9419 -- full views have not been seen yet. Any use of the non-limited views
9420 -- in the instance body requires the presence of a regular with_clause
9421 -- in the enclosing unit, and will fail if this with_clause is missing.
9422 -- We place the instance body at the beginning of the enclosing body,
9423 -- which is the unit being compiled. The freeze node for the instance
9424 -- is then placed after the instance body.
9426 if not Is_Empty_Elmt_List
(Incomplete_Actuals
(Act_Id
))
9427 and then Expander_Active
9428 and then Ekind
(Scope
(Act_Id
)) = E_Package
9431 Scop
: constant Entity_Id
:= Scope
(Act_Id
);
9432 Body_Id
: constant Node_Id
:=
9433 Corresponding_Body
(Unit_Declaration_Node
(Scop
));
9436 Ensure_Freeze_Node
(Act_Id
);
9437 F_Node
:= Freeze_Node
(Act_Id
);
9438 if Present
(Body_Id
) then
9439 Set_Is_Frozen
(Act_Id
, False);
9440 Prepend
(Act_Body
, Declarations
(Parent
(Body_Id
)));
9441 if Is_List_Member
(F_Node
) then
9445 Insert_After
(Act_Body
, F_Node
);
9451 -- If the body is a subunit, the freeze point is the corresponding stub
9452 -- in the current compilation, not the subunit itself.
9454 if Nkind
(Parent
(Gen_Body
)) = N_Subunit
then
9455 Orig_Body
:= Corresponding_Stub
(Parent
(Gen_Body
));
9457 Orig_Body
:= Gen_Body
;
9460 Body_Unit
:= Unit
(Cunit
(Get_Source_Unit
(Orig_Body
)));
9462 -- If the instantiation and the generic definition appear in the same
9463 -- package declaration, this is an early instantiation. If they appear
9464 -- in the same declarative part, it is an early instantiation only if
9465 -- the generic body appears textually later, and the generic body is
9466 -- also in the main unit.
9468 -- If instance is nested within a subprogram, and the generic body
9469 -- is not, the instance is delayed because the enclosing body is. If
9470 -- instance and body are within the same scope, or the same subprogram
9471 -- body, indicate explicitly that the instance is delayed.
9474 (Gen_Unit
= Act_Unit
9475 and then (Nkind_In
(Gen_Unit
, N_Generic_Package_Declaration
,
9476 N_Package_Declaration
)
9477 or else (Gen_Unit
= Body_Unit
9478 and then True_Sloc
(N
, Act_Unit
) <
9480 and then Is_In_Main_Unit
(Original_Node
(Gen_Unit
))
9481 and then In_Same_Scope
(Gen_Id
, Act_Id
));
9483 -- If this is an early instantiation, the freeze node is placed after
9484 -- the generic body. Otherwise, if the generic appears in an instance,
9485 -- we cannot freeze the current instance until the outer one is frozen.
9486 -- This is only relevant if the current instance is nested within some
9487 -- inner scope not itself within the outer instance. If this scope is
9488 -- a package body in the same declarative part as the outer instance,
9489 -- then that body needs to be frozen after the outer instance. Finally,
9490 -- if no delay is needed, we place the freeze node at the end of the
9491 -- current declarative part.
9494 and then (No
(Freeze_Node
(Act_Id
))
9495 or else not Is_List_Member
(Freeze_Node
(Act_Id
)))
9497 Ensure_Freeze_Node
(Act_Id
);
9498 F_Node
:= Freeze_Node
(Act_Id
);
9501 Insert_After
(Orig_Body
, F_Node
);
9503 elsif Is_Generic_Instance
(Par
)
9504 and then Present
(Freeze_Node
(Par
))
9505 and then Scope
(Act_Id
) /= Par
9507 -- Freeze instance of inner generic after instance of enclosing
9510 if In_Same_Declarative_Part
(Parent
(Freeze_Node
(Par
)), N
) then
9512 -- Handle the following case:
9514 -- package Parent_Inst is new ...
9517 -- procedure P ... -- this body freezes Parent_Inst
9519 -- package Inst is new ...
9521 -- In this particular scenario, the freeze node for Inst must
9522 -- be inserted in the same manner as that of Parent_Inst,
9523 -- before the next source body or at the end of the declarative
9524 -- list (body not available). If body P did not exist and
9525 -- Parent_Inst was frozen after Inst, either by a body
9526 -- following Inst or at the end of the declarative region,
9527 -- the freeze node for Inst must be inserted after that of
9528 -- Parent_Inst. This relation is established by comparing
9529 -- the Slocs of Parent_Inst freeze node and Inst.
9530 -- We examine the parents of the enclosing lists to handle
9531 -- the case where the parent instance is in the visible part
9532 -- of a package declaration, and the inner instance is in
9533 -- the corresponding private part.
9535 if Parent
(List_Containing
(Get_Unit_Instantiation_Node
(Par
)))
9536 = Parent
(List_Containing
(N
))
9537 and then Sloc
(Freeze_Node
(Par
)) < Sloc
(N
)
9539 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9541 Insert_After
(Freeze_Node
(Par
), F_Node
);
9544 -- Freeze package enclosing instance of inner generic after
9545 -- instance of enclosing generic.
9547 elsif Nkind_In
(Parent
(N
), N_Package_Body
, N_Subprogram_Body
)
9548 and then In_Same_Declarative_Part
9549 (Parent
(Freeze_Node
(Par
)), Parent
(N
))
9552 Enclosing
: Entity_Id
;
9555 Enclosing
:= Corresponding_Spec
(Parent
(N
));
9557 if No
(Enclosing
) then
9558 Enclosing
:= Defining_Entity
(Parent
(N
));
9561 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9562 Ensure_Freeze_Node
(Enclosing
);
9564 if not Is_List_Member
(Freeze_Node
(Enclosing
)) then
9566 -- The enclosing context is a subunit, insert the freeze
9567 -- node after the stub.
9569 if Nkind
(Parent
(Parent
(N
))) = N_Subunit
then
9570 Insert_Freeze_Node_For_Instance
9571 (Corresponding_Stub
(Parent
(Parent
(N
))),
9572 Freeze_Node
(Enclosing
));
9574 -- The enclosing context is a package with a stub body
9575 -- which has already been replaced by the real body.
9576 -- Insert the freeze node after the actual body.
9578 elsif Ekind
(Enclosing
) = E_Package
9579 and then Present
(Body_Entity
(Enclosing
))
9580 and then Was_Originally_Stub
9581 (Parent
(Body_Entity
(Enclosing
)))
9583 Insert_Freeze_Node_For_Instance
9584 (Parent
(Body_Entity
(Enclosing
)),
9585 Freeze_Node
(Enclosing
));
9587 -- The parent instance has been frozen before the body of
9588 -- the enclosing package, insert the freeze node after
9591 elsif List_Containing
(Freeze_Node
(Par
)) =
9592 List_Containing
(Parent
(N
))
9593 and then Sloc
(Freeze_Node
(Par
)) < Sloc
(Parent
(N
))
9595 Insert_Freeze_Node_For_Instance
9596 (Parent
(N
), Freeze_Node
(Enclosing
));
9600 (Freeze_Node
(Par
), Freeze_Node
(Enclosing
));
9606 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9610 Insert_Freeze_Node_For_Instance
(N
, F_Node
);
9614 Set_Is_Frozen
(Act_Id
);
9615 Insert_Before
(N
, Act_Body
);
9616 Mark_Rewrite_Insertion
(Act_Body
);
9619 -----------------------------
9620 -- Install_Formal_Packages --
9621 -----------------------------
9623 procedure Install_Formal_Packages
(Par
: Entity_Id
) is
9626 Gen_E
: Entity_Id
:= Empty
;
9629 E
:= First_Entity
(Par
);
9631 -- If we are installing an instance parent, locate the formal packages
9632 -- of its generic parent.
9634 if Is_Generic_Instance
(Par
) then
9635 Gen
:= Generic_Parent
(Package_Specification
(Par
));
9636 Gen_E
:= First_Entity
(Gen
);
9639 while Present
(E
) loop
9640 if Ekind
(E
) = E_Package
9641 and then Nkind
(Parent
(E
)) = N_Package_Renaming_Declaration
9643 -- If this is the renaming for the parent instance, done
9645 if Renamed_Object
(E
) = Par
then
9648 -- The visibility of a formal of an enclosing generic is already
9651 elsif Denotes_Formal_Package
(E
) then
9654 elsif Present
(Associated_Formal_Package
(E
)) then
9655 Check_Generic_Actuals
(Renamed_Object
(E
), True);
9656 Set_Is_Hidden
(E
, False);
9658 -- Find formal package in generic unit that corresponds to
9659 -- (instance of) formal package in instance.
9661 while Present
(Gen_E
) and then Chars
(Gen_E
) /= Chars
(E
) loop
9662 Next_Entity
(Gen_E
);
9665 if Present
(Gen_E
) then
9666 Map_Formal_Package_Entities
(Gen_E
, E
);
9673 if Present
(Gen_E
) then
9674 Next_Entity
(Gen_E
);
9677 end Install_Formal_Packages
;
9679 --------------------
9680 -- Install_Parent --
9681 --------------------
9683 procedure Install_Parent
(P
: Entity_Id
; In_Body
: Boolean := False) is
9684 Ancestors
: constant Elist_Id
:= New_Elmt_List
;
9685 S
: constant Entity_Id
:= Current_Scope
;
9686 Inst_Par
: Entity_Id
;
9687 First_Par
: Entity_Id
;
9688 Inst_Node
: Node_Id
;
9689 Gen_Par
: Entity_Id
;
9690 First_Gen
: Entity_Id
;
9693 procedure Install_Noninstance_Specs
(Par
: Entity_Id
);
9694 -- Install the scopes of noninstance parent units ending with Par
9696 procedure Install_Spec
(Par
: Entity_Id
);
9697 -- The child unit is within the declarative part of the parent, so the
9698 -- declarations within the parent are immediately visible.
9700 -------------------------------
9701 -- Install_Noninstance_Specs --
9702 -------------------------------
9704 procedure Install_Noninstance_Specs
(Par
: Entity_Id
) is
9707 and then Par
/= Standard_Standard
9708 and then not In_Open_Scopes
(Par
)
9710 Install_Noninstance_Specs
(Scope
(Par
));
9713 end Install_Noninstance_Specs
;
9719 procedure Install_Spec
(Par
: Entity_Id
) is
9720 Spec
: constant Node_Id
:= Package_Specification
(Par
);
9723 -- If this parent of the child instance is a top-level unit,
9724 -- then record the unit and its visibility for later resetting in
9725 -- Remove_Parent. We exclude units that are generic instances, as we
9726 -- only want to record this information for the ultimate top-level
9727 -- noninstance parent (is that always correct???).
9729 if Scope
(Par
) = Standard_Standard
9730 and then not Is_Generic_Instance
(Par
)
9732 Parent_Unit_Visible
:= Is_Immediately_Visible
(Par
);
9733 Instance_Parent_Unit
:= Par
;
9736 -- Open the parent scope and make it and its declarations visible.
9737 -- If this point is not within a body, then only the visible
9738 -- declarations should be made visible, and installation of the
9739 -- private declarations is deferred until the appropriate point
9740 -- within analysis of the spec being instantiated (see the handling
9741 -- of parent visibility in Analyze_Package_Specification). This is
9742 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9743 -- private view problems that occur when compiling instantiations of
9744 -- a generic child of that package (Generic_Dispatching_Constructor).
9745 -- If the instance freezes a tagged type, inlinings of operations
9746 -- from Ada.Tags may need the full view of type Tag. If inlining took
9747 -- proper account of establishing visibility of inlined subprograms'
9748 -- parents then it should be possible to remove this
9749 -- special check. ???
9752 Set_Is_Immediately_Visible
(Par
);
9753 Install_Visible_Declarations
(Par
);
9754 Set_Use
(Visible_Declarations
(Spec
));
9756 if In_Body
or else Is_RTU
(Par
, Ada_Tags
) then
9757 Install_Private_Declarations
(Par
);
9758 Set_Use
(Private_Declarations
(Spec
));
9762 -- Start of processing for Install_Parent
9765 -- We need to install the parent instance to compile the instantiation
9766 -- of the child, but the child instance must appear in the current
9767 -- scope. Given that we cannot place the parent above the current scope
9768 -- in the scope stack, we duplicate the current scope and unstack both
9769 -- after the instantiation is complete.
9771 -- If the parent is itself the instantiation of a child unit, we must
9772 -- also stack the instantiation of its parent, and so on. Each such
9773 -- ancestor is the prefix of the name in a prior instantiation.
9775 -- If this is a nested instance, the parent unit itself resolves to
9776 -- a renaming of the parent instance, whose declaration we need.
9778 -- Finally, the parent may be a generic (not an instance) when the
9779 -- child unit appears as a formal package.
9783 if Present
(Renamed_Entity
(Inst_Par
)) then
9784 Inst_Par
:= Renamed_Entity
(Inst_Par
);
9787 First_Par
:= Inst_Par
;
9789 Gen_Par
:= Generic_Parent
(Package_Specification
(Inst_Par
));
9791 First_Gen
:= Gen_Par
;
9793 while Present
(Gen_Par
) and then Is_Child_Unit
(Gen_Par
) loop
9795 -- Load grandparent instance as well
9797 Inst_Node
:= Get_Unit_Instantiation_Node
(Inst_Par
);
9799 if Nkind
(Name
(Inst_Node
)) = N_Expanded_Name
then
9800 Inst_Par
:= Entity
(Prefix
(Name
(Inst_Node
)));
9802 if Present
(Renamed_Entity
(Inst_Par
)) then
9803 Inst_Par
:= Renamed_Entity
(Inst_Par
);
9806 Gen_Par
:= Generic_Parent
(Package_Specification
(Inst_Par
));
9808 if Present
(Gen_Par
) then
9809 Prepend_Elmt
(Inst_Par
, Ancestors
);
9812 -- Parent is not the name of an instantiation
9814 Install_Noninstance_Specs
(Inst_Par
);
9825 if Present
(First_Gen
) then
9826 Append_Elmt
(First_Par
, Ancestors
);
9828 Install_Noninstance_Specs
(First_Par
);
9831 if not Is_Empty_Elmt_List
(Ancestors
) then
9832 Elmt
:= First_Elmt
(Ancestors
);
9833 while Present
(Elmt
) loop
9834 Install_Spec
(Node
(Elmt
));
9835 Install_Formal_Packages
(Node
(Elmt
));
9845 -------------------------------
9846 -- Install_Hidden_Primitives --
9847 -------------------------------
9849 procedure Install_Hidden_Primitives
9850 (Prims_List
: in out Elist_Id
;
9855 List
: Elist_Id
:= No_Elist
;
9856 Prim_G_Elmt
: Elmt_Id
;
9857 Prim_A_Elmt
: Elmt_Id
;
9862 -- No action needed in case of serious errors because we cannot trust
9863 -- in the order of primitives
9865 if Serious_Errors_Detected
> 0 then
9868 -- No action possible if we don't have available the list of primitive
9872 or else not Is_Record_Type
(Gen_T
)
9873 or else not Is_Tagged_Type
(Gen_T
)
9874 or else not Is_Record_Type
(Act_T
)
9875 or else not Is_Tagged_Type
(Act_T
)
9879 -- There is no need to handle interface types since their primitives
9882 elsif Is_Interface
(Gen_T
) then
9886 Prim_G_Elmt
:= First_Elmt
(Primitive_Operations
(Gen_T
));
9888 if not Is_Class_Wide_Type
(Act_T
) then
9889 Prim_A_Elmt
:= First_Elmt
(Primitive_Operations
(Act_T
));
9891 Prim_A_Elmt
:= First_Elmt
(Primitive_Operations
(Root_Type
(Act_T
)));
9895 -- Skip predefined primitives in the generic formal
9897 while Present
(Prim_G_Elmt
)
9898 and then Is_Predefined_Dispatching_Operation
(Node
(Prim_G_Elmt
))
9900 Next_Elmt
(Prim_G_Elmt
);
9903 -- Skip predefined primitives in the generic actual
9905 while Present
(Prim_A_Elmt
)
9906 and then Is_Predefined_Dispatching_Operation
(Node
(Prim_A_Elmt
))
9908 Next_Elmt
(Prim_A_Elmt
);
9911 exit when No
(Prim_G_Elmt
) or else No
(Prim_A_Elmt
);
9913 Prim_G
:= Node
(Prim_G_Elmt
);
9914 Prim_A
:= Node
(Prim_A_Elmt
);
9916 -- There is no need to handle interface primitives because their
9917 -- primitives are not hidden
9919 exit when Present
(Interface_Alias
(Prim_G
));
9921 -- Here we install one hidden primitive
9923 if Chars
(Prim_G
) /= Chars
(Prim_A
)
9924 and then Has_Suffix
(Prim_A
, 'P')
9925 and then Remove_Suffix
(Prim_A
, 'P') = Chars
(Prim_G
)
9927 Set_Chars
(Prim_A
, Chars
(Prim_G
));
9928 Append_New_Elmt
(Prim_A
, To
=> List
);
9931 Next_Elmt
(Prim_A_Elmt
);
9932 Next_Elmt
(Prim_G_Elmt
);
9935 -- Append the elements to the list of temporarily visible primitives
9936 -- avoiding duplicates.
9938 if Present
(List
) then
9939 if No
(Prims_List
) then
9940 Prims_List
:= New_Elmt_List
;
9943 Elmt
:= First_Elmt
(List
);
9944 while Present
(Elmt
) loop
9945 Append_Unique_Elmt
(Node
(Elmt
), Prims_List
);
9949 end Install_Hidden_Primitives
;
9951 -------------------------------
9952 -- Restore_Hidden_Primitives --
9953 -------------------------------
9955 procedure Restore_Hidden_Primitives
(Prims_List
: in out Elist_Id
) is
9956 Prim_Elmt
: Elmt_Id
;
9960 if Prims_List
/= No_Elist
then
9961 Prim_Elmt
:= First_Elmt
(Prims_List
);
9962 while Present
(Prim_Elmt
) loop
9963 Prim
:= Node
(Prim_Elmt
);
9964 Set_Chars
(Prim
, Add_Suffix
(Prim
, 'P'));
9965 Next_Elmt
(Prim_Elmt
);
9968 Prims_List
:= No_Elist
;
9970 end Restore_Hidden_Primitives
;
9972 --------------------------------
9973 -- Instantiate_Formal_Package --
9974 --------------------------------
9976 function Instantiate_Formal_Package
9979 Analyzed_Formal
: Node_Id
) return List_Id
9981 Loc
: constant Source_Ptr
:= Sloc
(Actual
);
9982 Hidden_Formals
: constant Elist_Id
:= New_Elmt_List
;
9983 Actual_Pack
: Entity_Id
;
9984 Formal_Pack
: Entity_Id
;
9985 Gen_Parent
: Entity_Id
;
9988 Parent_Spec
: Node_Id
;
9990 procedure Find_Matching_Actual
9992 Act
: in out Entity_Id
);
9993 -- We need to associate each formal entity in the formal package with
9994 -- the corresponding entity in the actual package. The actual package
9995 -- has been analyzed and possibly expanded, and as a result there is
9996 -- no one-to-one correspondence between the two lists (for example,
9997 -- the actual may include subtypes, itypes, and inherited primitive
9998 -- operations, interspersed among the renaming declarations for the
9999 -- actuals). We retrieve the corresponding actual by name because each
10000 -- actual has the same name as the formal, and they do appear in the
10003 function Get_Formal_Entity
(N
: Node_Id
) return Entity_Id
;
10004 -- Retrieve entity of defining entity of generic formal parameter.
10005 -- Only the declarations of formals need to be considered when
10006 -- linking them to actuals, but the declarative list may include
10007 -- internal entities generated during analysis, and those are ignored.
10009 procedure Match_Formal_Entity
10010 (Formal_Node
: Node_Id
;
10011 Formal_Ent
: Entity_Id
;
10012 Actual_Ent
: Entity_Id
);
10013 -- Associates the formal entity with the actual. In the case where
10014 -- Formal_Ent is a formal package, this procedure iterates through all
10015 -- of its formals and enters associations between the actuals occurring
10016 -- in the formal package's corresponding actual package (given by
10017 -- Actual_Ent) and the formal package's formal parameters. This
10018 -- procedure recurses if any of the parameters is itself a package.
10020 function Is_Instance_Of
10021 (Act_Spec
: Entity_Id
;
10022 Gen_Anc
: Entity_Id
) return Boolean;
10023 -- The actual can be an instantiation of a generic within another
10024 -- instance, in which case there is no direct link from it to the
10025 -- original generic ancestor. In that case, we recognize that the
10026 -- ultimate ancestor is the same by examining names and scopes.
10028 procedure Process_Nested_Formal
(Formal
: Entity_Id
);
10029 -- If the current formal is declared with a box, its own formals are
10030 -- visible in the instance, as they were in the generic, and their
10031 -- Hidden flag must be reset. If some of these formals are themselves
10032 -- packages declared with a box, the processing must be recursive.
10034 --------------------------
10035 -- Find_Matching_Actual --
10036 --------------------------
10038 procedure Find_Matching_Actual
10040 Act
: in out Entity_Id
)
10042 Formal_Ent
: Entity_Id
;
10045 case Nkind
(Original_Node
(F
)) is
10046 when N_Formal_Object_Declaration
10047 | N_Formal_Type_Declaration
10049 Formal_Ent
:= Defining_Identifier
(F
);
10051 while Chars
(Act
) /= Chars
(Formal_Ent
) loop
10055 when N_Formal_Package_Declaration
10056 | N_Formal_Subprogram_Declaration
10057 | N_Generic_Package_Declaration
10058 | N_Package_Declaration
10060 Formal_Ent
:= Defining_Entity
(F
);
10062 while Chars
(Act
) /= Chars
(Formal_Ent
) loop
10067 raise Program_Error
;
10069 end Find_Matching_Actual
;
10071 -------------------------
10072 -- Match_Formal_Entity --
10073 -------------------------
10075 procedure Match_Formal_Entity
10076 (Formal_Node
: Node_Id
;
10077 Formal_Ent
: Entity_Id
;
10078 Actual_Ent
: Entity_Id
)
10080 Act_Pkg
: Entity_Id
;
10083 Set_Instance_Of
(Formal_Ent
, Actual_Ent
);
10085 if Ekind
(Actual_Ent
) = E_Package
then
10087 -- Record associations for each parameter
10089 Act_Pkg
:= Actual_Ent
;
10092 A_Ent
: Entity_Id
:= First_Entity
(Act_Pkg
);
10096 Gen_Decl
: Node_Id
;
10098 Actual
: Entity_Id
;
10101 -- Retrieve the actual given in the formal package declaration
10103 Actual
:= Entity
(Name
(Original_Node
(Formal_Node
)));
10105 -- The actual in the formal package declaration may be a
10106 -- renamed generic package, in which case we want to retrieve
10107 -- the original generic in order to traverse its formal part.
10109 if Present
(Renamed_Entity
(Actual
)) then
10110 Gen_Decl
:= Unit_Declaration_Node
(Renamed_Entity
(Actual
));
10112 Gen_Decl
:= Unit_Declaration_Node
(Actual
);
10115 Formals
:= Generic_Formal_Declarations
(Gen_Decl
);
10117 if Present
(Formals
) then
10118 F_Node
:= First_Non_Pragma
(Formals
);
10123 while Present
(A_Ent
)
10124 and then Present
(F_Node
)
10125 and then A_Ent
/= First_Private_Entity
(Act_Pkg
)
10127 F_Ent
:= Get_Formal_Entity
(F_Node
);
10129 if Present
(F_Ent
) then
10131 -- This is a formal of the original package. Record
10132 -- association and recurse.
10134 Find_Matching_Actual
(F_Node
, A_Ent
);
10135 Match_Formal_Entity
(F_Node
, F_Ent
, A_Ent
);
10136 Next_Entity
(A_Ent
);
10139 Next_Non_Pragma
(F_Node
);
10143 end Match_Formal_Entity
;
10145 -----------------------
10146 -- Get_Formal_Entity --
10147 -----------------------
10149 function Get_Formal_Entity
(N
: Node_Id
) return Entity_Id
is
10150 Kind
: constant Node_Kind
:= Nkind
(Original_Node
(N
));
10153 when N_Formal_Object_Declaration
=>
10154 return Defining_Identifier
(N
);
10156 when N_Formal_Type_Declaration
=>
10157 return Defining_Identifier
(N
);
10159 when N_Formal_Subprogram_Declaration
=>
10160 return Defining_Unit_Name
(Specification
(N
));
10162 when N_Formal_Package_Declaration
=>
10163 return Defining_Identifier
(Original_Node
(N
));
10165 when N_Generic_Package_Declaration
=>
10166 return Defining_Identifier
(Original_Node
(N
));
10168 -- All other declarations are introduced by semantic analysis and
10169 -- have no match in the actual.
10174 end Get_Formal_Entity
;
10176 --------------------
10177 -- Is_Instance_Of --
10178 --------------------
10180 function Is_Instance_Of
10181 (Act_Spec
: Entity_Id
;
10182 Gen_Anc
: Entity_Id
) return Boolean
10184 Gen_Par
: constant Entity_Id
:= Generic_Parent
(Act_Spec
);
10187 if No
(Gen_Par
) then
10190 -- Simplest case: the generic parent of the actual is the formal
10192 elsif Gen_Par
= Gen_Anc
then
10195 elsif Chars
(Gen_Par
) /= Chars
(Gen_Anc
) then
10198 -- The actual may be obtained through several instantiations. Its
10199 -- scope must itself be an instance of a generic declared in the
10200 -- same scope as the formal. Any other case is detected above.
10202 elsif not Is_Generic_Instance
(Scope
(Gen_Par
)) then
10206 return Generic_Parent
(Parent
(Scope
(Gen_Par
))) = Scope
(Gen_Anc
);
10208 end Is_Instance_Of
;
10210 ---------------------------
10211 -- Process_Nested_Formal --
10212 ---------------------------
10214 procedure Process_Nested_Formal
(Formal
: Entity_Id
) is
10218 if Present
(Associated_Formal_Package
(Formal
))
10219 and then Box_Present
(Parent
(Associated_Formal_Package
(Formal
)))
10221 Ent
:= First_Entity
(Formal
);
10222 while Present
(Ent
) loop
10223 Set_Is_Hidden
(Ent
, False);
10224 Set_Is_Visible_Formal
(Ent
);
10225 Set_Is_Potentially_Use_Visible
10226 (Ent
, Is_Potentially_Use_Visible
(Formal
));
10228 if Ekind
(Ent
) = E_Package
then
10229 exit when Renamed_Entity
(Ent
) = Renamed_Entity
(Formal
);
10230 Process_Nested_Formal
(Ent
);
10236 end Process_Nested_Formal
;
10238 -- Start of processing for Instantiate_Formal_Package
10243 if not Is_Entity_Name
(Actual
)
10244 or else Ekind
(Entity
(Actual
)) /= E_Package
10247 ("expect package instance to instantiate formal", Actual
);
10248 Abandon_Instantiation
(Actual
);
10249 raise Program_Error
;
10252 Actual_Pack
:= Entity
(Actual
);
10253 Set_Is_Instantiated
(Actual_Pack
);
10255 -- The actual may be a renamed package, or an outer generic formal
10256 -- package whose instantiation is converted into a renaming.
10258 if Present
(Renamed_Object
(Actual_Pack
)) then
10259 Actual_Pack
:= Renamed_Object
(Actual_Pack
);
10262 if Nkind
(Analyzed_Formal
) = N_Formal_Package_Declaration
then
10263 Gen_Parent
:= Get_Instance_Of
(Entity
(Name
(Analyzed_Formal
)));
10264 Formal_Pack
:= Defining_Identifier
(Analyzed_Formal
);
10267 Generic_Parent
(Specification
(Analyzed_Formal
));
10269 Defining_Unit_Name
(Specification
(Analyzed_Formal
));
10272 if Nkind
(Parent
(Actual_Pack
)) = N_Defining_Program_Unit_Name
then
10273 Parent_Spec
:= Package_Specification
(Actual_Pack
);
10275 Parent_Spec
:= Parent
(Actual_Pack
);
10278 if Gen_Parent
= Any_Id
then
10280 ("previous error in declaration of formal package", Actual
);
10281 Abandon_Instantiation
(Actual
);
10284 Is_Instance_Of
(Parent_Spec
, Get_Instance_Of
(Gen_Parent
))
10290 ("actual parameter must be instance of&", Actual
, Gen_Parent
);
10291 Abandon_Instantiation
(Actual
);
10294 Set_Instance_Of
(Defining_Identifier
(Formal
), Actual_Pack
);
10295 Map_Formal_Package_Entities
(Formal_Pack
, Actual_Pack
);
10298 Make_Package_Renaming_Declaration
(Loc
,
10299 Defining_Unit_Name
=> New_Copy
(Defining_Identifier
(Formal
)),
10300 Name
=> New_Occurrence_Of
(Actual_Pack
, Loc
));
10302 Set_Associated_Formal_Package
10303 (Defining_Unit_Name
(Nod
), Defining_Identifier
(Formal
));
10304 Decls
:= New_List
(Nod
);
10306 -- If the formal F has a box, then the generic declarations are
10307 -- visible in the generic G. In an instance of G, the corresponding
10308 -- entities in the actual for F (which are the actuals for the
10309 -- instantiation of the generic that F denotes) must also be made
10310 -- visible for analysis of the current instance. On exit from the
10311 -- current instance, those entities are made private again. If the
10312 -- actual is currently in use, these entities are also use-visible.
10314 -- The loop through the actual entities also steps through the formal
10315 -- entities and enters associations from formals to actuals into the
10316 -- renaming map. This is necessary to properly handle checking of
10317 -- actual parameter associations for later formals that depend on
10318 -- actuals declared in the formal package.
10320 -- In Ada 2005, partial parameterization requires that we make
10321 -- visible the actuals corresponding to formals that were defaulted
10322 -- in the formal package. There formals are identified because they
10323 -- remain formal generics within the formal package, rather than
10324 -- being renamings of the actuals supplied.
10327 Gen_Decl
: constant Node_Id
:=
10328 Unit_Declaration_Node
(Gen_Parent
);
10329 Formals
: constant List_Id
:=
10330 Generic_Formal_Declarations
(Gen_Decl
);
10332 Actual_Ent
: Entity_Id
;
10333 Actual_Of_Formal
: Node_Id
;
10334 Formal_Node
: Node_Id
;
10335 Formal_Ent
: Entity_Id
;
10338 if Present
(Formals
) then
10339 Formal_Node
:= First_Non_Pragma
(Formals
);
10341 Formal_Node
:= Empty
;
10344 Actual_Ent
:= First_Entity
(Actual_Pack
);
10345 Actual_Of_Formal
:=
10346 First
(Visible_Declarations
(Specification
(Analyzed_Formal
)));
10347 while Present
(Actual_Ent
)
10348 and then Actual_Ent
/= First_Private_Entity
(Actual_Pack
)
10350 if Present
(Formal_Node
) then
10351 Formal_Ent
:= Get_Formal_Entity
(Formal_Node
);
10353 if Present
(Formal_Ent
) then
10354 Find_Matching_Actual
(Formal_Node
, Actual_Ent
);
10355 Match_Formal_Entity
(Formal_Node
, Formal_Ent
, Actual_Ent
);
10357 -- We iterate at the same time over the actuals of the
10358 -- local package created for the formal, to determine
10359 -- which one of the formals of the original generic were
10360 -- defaulted in the formal. The corresponding actual
10361 -- entities are visible in the enclosing instance.
10363 if Box_Present
(Formal
)
10365 (Present
(Actual_Of_Formal
)
10368 (Get_Formal_Entity
(Actual_Of_Formal
)))
10370 Set_Is_Hidden
(Actual_Ent
, False);
10371 Set_Is_Visible_Formal
(Actual_Ent
);
10372 Set_Is_Potentially_Use_Visible
10373 (Actual_Ent
, In_Use
(Actual_Pack
));
10375 if Ekind
(Actual_Ent
) = E_Package
then
10376 Process_Nested_Formal
(Actual_Ent
);
10380 if not Is_Hidden
(Actual_Ent
) then
10381 Append_Elmt
(Actual_Ent
, Hidden_Formals
);
10384 Set_Is_Hidden
(Actual_Ent
);
10385 Set_Is_Potentially_Use_Visible
(Actual_Ent
, False);
10389 Next_Non_Pragma
(Formal_Node
);
10390 Next
(Actual_Of_Formal
);
10393 -- No further formals to match, but the generic part may
10394 -- contain inherited operation that are not hidden in the
10395 -- enclosing instance.
10397 Next_Entity
(Actual_Ent
);
10401 -- Inherited subprograms generated by formal derived types are
10402 -- also visible if the types are.
10404 Actual_Ent
:= First_Entity
(Actual_Pack
);
10405 while Present
(Actual_Ent
)
10406 and then Actual_Ent
/= First_Private_Entity
(Actual_Pack
)
10408 if Is_Overloadable
(Actual_Ent
)
10410 Nkind
(Parent
(Actual_Ent
)) = N_Subtype_Declaration
10412 not Is_Hidden
(Defining_Identifier
(Parent
(Actual_Ent
)))
10414 Set_Is_Hidden
(Actual_Ent
, False);
10415 Set_Is_Potentially_Use_Visible
10416 (Actual_Ent
, In_Use
(Actual_Pack
));
10419 Next_Entity
(Actual_Ent
);
10423 -- If the formal is not declared with a box, reanalyze it as an
10424 -- abbreviated instantiation, to verify the matching rules of 12.7.
10425 -- The actual checks are performed after the generic associations
10426 -- have been analyzed, to guarantee the same visibility for this
10427 -- instantiation and for the actuals.
10429 -- In Ada 2005, the generic associations for the formal can include
10430 -- defaulted parameters. These are ignored during check. This
10431 -- internal instantiation is removed from the tree after conformance
10432 -- checking, because it contains formal declarations for those
10433 -- defaulted parameters, and those should not reach the back-end.
10435 if not Box_Present
(Formal
) then
10437 I_Pack
: constant Entity_Id
:=
10438 Make_Temporary
(Sloc
(Actual
), 'P');
10441 Set_Is_Internal
(I_Pack
);
10442 Set_Ekind
(I_Pack
, E_Package
);
10443 Set_Hidden_In_Formal_Instance
(I_Pack
, Hidden_Formals
);
10446 Make_Package_Instantiation
(Sloc
(Actual
),
10447 Defining_Unit_Name
=> I_Pack
,
10450 (Get_Instance_Of
(Gen_Parent
), Sloc
(Actual
)),
10451 Generic_Associations
=> Generic_Associations
(Formal
)));
10457 end Instantiate_Formal_Package
;
10459 -----------------------------------
10460 -- Instantiate_Formal_Subprogram --
10461 -----------------------------------
10463 function Instantiate_Formal_Subprogram
10466 Analyzed_Formal
: Node_Id
) return Node_Id
10468 Analyzed_S
: constant Entity_Id
:=
10469 Defining_Unit_Name
(Specification
(Analyzed_Formal
));
10470 Formal_Sub
: constant Entity_Id
:=
10471 Defining_Unit_Name
(Specification
(Formal
));
10473 function From_Parent_Scope
(Subp
: Entity_Id
) return Boolean;
10474 -- If the generic is a child unit, the parent has been installed on the
10475 -- scope stack, but a default subprogram cannot resolve to something
10476 -- on the parent because that parent is not really part of the visible
10477 -- context (it is there to resolve explicit local entities). If the
10478 -- default has resolved in this way, we remove the entity from immediate
10479 -- visibility and analyze the node again to emit an error message or
10480 -- find another visible candidate.
10482 procedure Valid_Actual_Subprogram
(Act
: Node_Id
);
10483 -- Perform legality check and raise exception on failure
10485 -----------------------
10486 -- From_Parent_Scope --
10487 -----------------------
10489 function From_Parent_Scope
(Subp
: Entity_Id
) return Boolean is
10490 Gen_Scope
: Node_Id
;
10493 Gen_Scope
:= Scope
(Analyzed_S
);
10494 while Present
(Gen_Scope
) and then Is_Child_Unit
(Gen_Scope
) loop
10495 if Scope
(Subp
) = Scope
(Gen_Scope
) then
10499 Gen_Scope
:= Scope
(Gen_Scope
);
10503 end From_Parent_Scope
;
10505 -----------------------------
10506 -- Valid_Actual_Subprogram --
10507 -----------------------------
10509 procedure Valid_Actual_Subprogram
(Act
: Node_Id
) is
10513 if Is_Entity_Name
(Act
) then
10514 Act_E
:= Entity
(Act
);
10516 elsif Nkind
(Act
) = N_Selected_Component
10517 and then Is_Entity_Name
(Selector_Name
(Act
))
10519 Act_E
:= Entity
(Selector_Name
(Act
));
10525 if (Present
(Act_E
) and then Is_Overloadable
(Act_E
))
10526 or else Nkind_In
(Act
, N_Attribute_Reference
,
10527 N_Indexed_Component
,
10528 N_Character_Literal
,
10529 N_Explicit_Dereference
)
10535 ("expect subprogram or entry name in instantiation of &",
10536 Instantiation_Node
, Formal_Sub
);
10537 Abandon_Instantiation
(Instantiation_Node
);
10538 end Valid_Actual_Subprogram
;
10542 Decl_Node
: Node_Id
;
10545 New_Spec
: Node_Id
;
10546 New_Subp
: Entity_Id
;
10548 -- Start of processing for Instantiate_Formal_Subprogram
10551 New_Spec
:= New_Copy_Tree
(Specification
(Formal
));
10553 -- The tree copy has created the proper instantiation sloc for the
10554 -- new specification. Use this location for all other constructed
10557 Loc
:= Sloc
(Defining_Unit_Name
(New_Spec
));
10559 -- Create new entity for the actual (New_Copy_Tree does not), and
10560 -- indicate that it is an actual.
10562 New_Subp
:= Make_Defining_Identifier
(Loc
, Chars
(Formal_Sub
));
10563 Set_Ekind
(New_Subp
, Ekind
(Analyzed_S
));
10564 Set_Is_Generic_Actual_Subprogram
(New_Subp
);
10565 Set_Defining_Unit_Name
(New_Spec
, New_Subp
);
10567 -- Create new entities for the each of the formals in the specification
10568 -- of the renaming declaration built for the actual.
10570 if Present
(Parameter_Specifications
(New_Spec
)) then
10576 F
:= First
(Parameter_Specifications
(New_Spec
));
10577 while Present
(F
) loop
10578 F_Id
:= Defining_Identifier
(F
);
10580 Set_Defining_Identifier
(F
,
10581 Make_Defining_Identifier
(Sloc
(F_Id
), Chars
(F_Id
)));
10587 -- Find entity of actual. If the actual is an attribute reference, it
10588 -- cannot be resolved here (its formal is missing) but is handled
10589 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10590 -- fully resolved subsequently, when the renaming declaration for the
10591 -- formal is analyzed. If it is an explicit dereference, resolve the
10592 -- prefix but not the actual itself, to prevent interpretation as call.
10594 if Present
(Actual
) then
10595 Loc
:= Sloc
(Actual
);
10596 Set_Sloc
(New_Spec
, Loc
);
10598 if Nkind
(Actual
) = N_Operator_Symbol
then
10599 Find_Direct_Name
(Actual
);
10601 elsif Nkind
(Actual
) = N_Explicit_Dereference
then
10602 Analyze
(Prefix
(Actual
));
10604 elsif Nkind
(Actual
) /= N_Attribute_Reference
then
10608 Valid_Actual_Subprogram
(Actual
);
10611 elsif Present
(Default_Name
(Formal
)) then
10612 if not Nkind_In
(Default_Name
(Formal
), N_Attribute_Reference
,
10613 N_Selected_Component
,
10614 N_Indexed_Component
,
10615 N_Character_Literal
)
10616 and then Present
(Entity
(Default_Name
(Formal
)))
10618 Nam
:= New_Occurrence_Of
(Entity
(Default_Name
(Formal
)), Loc
);
10620 Nam
:= New_Copy
(Default_Name
(Formal
));
10621 Set_Sloc
(Nam
, Loc
);
10624 elsif Box_Present
(Formal
) then
10626 -- Actual is resolved at the point of instantiation. Create an
10627 -- identifier or operator with the same name as the formal.
10629 if Nkind
(Formal_Sub
) = N_Defining_Operator_Symbol
then
10631 Make_Operator_Symbol
(Loc
,
10632 Chars
=> Chars
(Formal_Sub
),
10633 Strval
=> No_String
);
10635 Nam
:= Make_Identifier
(Loc
, Chars
(Formal_Sub
));
10638 elsif Nkind
(Specification
(Formal
)) = N_Procedure_Specification
10639 and then Null_Present
(Specification
(Formal
))
10641 -- Generate null body for procedure, for use in the instance
10644 Make_Subprogram_Body
(Loc
,
10645 Specification
=> New_Spec
,
10646 Declarations
=> New_List
,
10647 Handled_Statement_Sequence
=>
10648 Make_Handled_Sequence_Of_Statements
(Loc
,
10649 Statements
=> New_List
(Make_Null_Statement
(Loc
))));
10651 Set_Is_Intrinsic_Subprogram
(Defining_Unit_Name
(New_Spec
));
10655 Error_Msg_Sloc
:= Sloc
(Scope
(Analyzed_S
));
10657 ("missing actual&", Instantiation_Node
, Formal_Sub
);
10659 ("\in instantiation of & declared#",
10660 Instantiation_Node
, Scope
(Analyzed_S
));
10661 Abandon_Instantiation
(Instantiation_Node
);
10665 Make_Subprogram_Renaming_Declaration
(Loc
,
10666 Specification
=> New_Spec
,
10669 -- If we do not have an actual and the formal specified <> then set to
10670 -- get proper default.
10672 if No
(Actual
) and then Box_Present
(Formal
) then
10673 Set_From_Default
(Decl_Node
);
10676 -- Gather possible interpretations for the actual before analyzing the
10677 -- instance. If overloaded, it will be resolved when analyzing the
10678 -- renaming declaration.
10680 if Box_Present
(Formal
) and then No
(Actual
) then
10683 if Is_Child_Unit
(Scope
(Analyzed_S
))
10684 and then Present
(Entity
(Nam
))
10686 if not Is_Overloaded
(Nam
) then
10687 if From_Parent_Scope
(Entity
(Nam
)) then
10688 Set_Is_Immediately_Visible
(Entity
(Nam
), False);
10689 Set_Entity
(Nam
, Empty
);
10690 Set_Etype
(Nam
, Empty
);
10693 Set_Is_Immediately_Visible
(Entity
(Nam
));
10702 Get_First_Interp
(Nam
, I
, It
);
10703 while Present
(It
.Nam
) loop
10704 if From_Parent_Scope
(It
.Nam
) then
10708 Get_Next_Interp
(I
, It
);
10715 -- The generic instantiation freezes the actual. This can only be done
10716 -- once the actual is resolved, in the analysis of the renaming
10717 -- declaration. To make the formal subprogram entity available, we set
10718 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10719 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10720 -- of formal abstract subprograms.
10722 Set_Corresponding_Formal_Spec
(Decl_Node
, Analyzed_S
);
10724 -- We cannot analyze the renaming declaration, and thus find the actual,
10725 -- until all the actuals are assembled in the instance. For subsequent
10726 -- checks of other actuals, indicate the node that will hold the
10727 -- instance of this formal.
10729 Set_Instance_Of
(Analyzed_S
, Nam
);
10731 if Nkind
(Actual
) = N_Selected_Component
10732 and then Is_Task_Type
(Etype
(Prefix
(Actual
)))
10733 and then not Is_Frozen
(Etype
(Prefix
(Actual
)))
10735 -- The renaming declaration will create a body, which must appear
10736 -- outside of the instantiation, We move the renaming declaration
10737 -- out of the instance, and create an additional renaming inside,
10738 -- to prevent freezing anomalies.
10741 Anon_Id
: constant Entity_Id
:= Make_Temporary
(Loc
, 'E');
10744 Set_Defining_Unit_Name
(New_Spec
, Anon_Id
);
10745 Insert_Before
(Instantiation_Node
, Decl_Node
);
10746 Analyze
(Decl_Node
);
10748 -- Now create renaming within the instance
10751 Make_Subprogram_Renaming_Declaration
(Loc
,
10752 Specification
=> New_Copy_Tree
(New_Spec
),
10753 Name
=> New_Occurrence_Of
(Anon_Id
, Loc
));
10755 Set_Defining_Unit_Name
(Specification
(Decl_Node
),
10756 Make_Defining_Identifier
(Loc
, Chars
(Formal_Sub
)));
10761 end Instantiate_Formal_Subprogram
;
10763 ------------------------
10764 -- Instantiate_Object --
10765 ------------------------
10767 function Instantiate_Object
10770 Analyzed_Formal
: Node_Id
) return List_Id
10772 Gen_Obj
: constant Entity_Id
:= Defining_Identifier
(Formal
);
10773 A_Gen_Obj
: constant Entity_Id
:=
10774 Defining_Identifier
(Analyzed_Formal
);
10775 Acc_Def
: Node_Id
:= Empty
;
10776 Act_Assoc
: constant Node_Id
:= Parent
(Actual
);
10777 Actual_Decl
: Node_Id
:= Empty
;
10778 Decl_Node
: Node_Id
;
10781 List
: constant List_Id
:= New_List
;
10782 Loc
: constant Source_Ptr
:= Sloc
(Actual
);
10783 Orig_Ftyp
: constant Entity_Id
:= Etype
(A_Gen_Obj
);
10784 Subt_Decl
: Node_Id
:= Empty
;
10785 Subt_Mark
: Node_Id
:= Empty
;
10787 function Copy_Access_Def
return Node_Id
;
10788 -- If formal is an anonymous access, copy access definition of formal
10789 -- for generated object declaration.
10791 ---------------------
10792 -- Copy_Access_Def --
10793 ---------------------
10795 function Copy_Access_Def
return Node_Id
is
10797 Def
:= New_Copy_Tree
(Acc_Def
);
10799 -- In addition, if formal is an access to subprogram we need to
10800 -- generate new formals for the signature of the default, so that
10801 -- the tree is properly formatted for ASIS use.
10803 if Present
(Access_To_Subprogram_Definition
(Acc_Def
)) then
10805 Par_Spec
: Node_Id
;
10808 First
(Parameter_Specifications
10809 (Access_To_Subprogram_Definition
(Def
)));
10810 while Present
(Par_Spec
) loop
10811 Set_Defining_Identifier
(Par_Spec
,
10812 Make_Defining_Identifier
(Sloc
(Acc_Def
),
10813 Chars
=> Chars
(Defining_Identifier
(Par_Spec
))));
10820 end Copy_Access_Def
;
10822 -- Start of processing for Instantiate_Object
10825 -- Formal may be an anonymous access
10827 if Present
(Subtype_Mark
(Formal
)) then
10828 Subt_Mark
:= Subtype_Mark
(Formal
);
10830 Check_Access_Definition
(Formal
);
10831 Acc_Def
:= Access_Definition
(Formal
);
10834 -- Sloc for error message on missing actual
10836 Error_Msg_Sloc
:= Sloc
(Scope
(A_Gen_Obj
));
10838 if Get_Instance_Of
(Gen_Obj
) /= Gen_Obj
then
10839 Error_Msg_N
("duplicate instantiation of generic parameter", Actual
);
10842 Set_Parent
(List
, Parent
(Actual
));
10846 if Out_Present
(Formal
) then
10848 -- An IN OUT generic actual must be a name. The instantiation is a
10849 -- renaming declaration. The actual is the name being renamed. We
10850 -- use the actual directly, rather than a copy, because it is not
10851 -- used further in the list of actuals, and because a copy or a use
10852 -- of relocate_node is incorrect if the instance is nested within a
10853 -- generic. In order to simplify ASIS searches, the Generic_Parent
10854 -- field links the declaration to the generic association.
10856 if No
(Actual
) then
10858 ("missing actual &",
10859 Instantiation_Node
, Gen_Obj
);
10861 ("\in instantiation of & declared#",
10862 Instantiation_Node
, Scope
(A_Gen_Obj
));
10863 Abandon_Instantiation
(Instantiation_Node
);
10866 if Present
(Subt_Mark
) then
10868 Make_Object_Renaming_Declaration
(Loc
,
10869 Defining_Identifier
=> New_Copy
(Gen_Obj
),
10870 Subtype_Mark
=> New_Copy_Tree
(Subt_Mark
),
10873 else pragma Assert
(Present
(Acc_Def
));
10875 Make_Object_Renaming_Declaration
(Loc
,
10876 Defining_Identifier
=> New_Copy
(Gen_Obj
),
10877 Access_Definition
=> New_Copy_Tree
(Acc_Def
),
10881 Set_Corresponding_Generic_Association
(Decl_Node
, Act_Assoc
);
10883 -- The analysis of the actual may produce Insert_Action nodes, so
10884 -- the declaration must have a context in which to attach them.
10886 Append
(Decl_Node
, List
);
10889 -- Return if the analysis of the actual reported some error
10891 if Etype
(Actual
) = Any_Type
then
10895 -- This check is performed here because Analyze_Object_Renaming will
10896 -- not check it when Comes_From_Source is False. Note though that the
10897 -- check for the actual being the name of an object will be performed
10898 -- in Analyze_Object_Renaming.
10900 if Is_Object_Reference
(Actual
)
10901 and then Is_Dependent_Component_Of_Mutable_Object
(Actual
)
10904 ("illegal discriminant-dependent component for in out parameter",
10908 -- The actual has to be resolved in order to check that it is a
10909 -- variable (due to cases such as F (1), where F returns access to
10910 -- an array, and for overloaded prefixes).
10912 Ftyp
:= Get_Instance_Of
(Etype
(A_Gen_Obj
));
10914 -- If the type of the formal is not itself a formal, and the current
10915 -- unit is a child unit, the formal type must be declared in a
10916 -- parent, and must be retrieved by visibility.
10918 if Ftyp
= Orig_Ftyp
10919 and then Is_Generic_Unit
(Scope
(Ftyp
))
10920 and then Is_Child_Unit
(Scope
(A_Gen_Obj
))
10923 Temp
: constant Node_Id
:=
10924 New_Copy_Tree
(Subtype_Mark
(Analyzed_Formal
));
10926 Set_Entity
(Temp
, Empty
);
10928 Ftyp
:= Entity
(Temp
);
10932 if Is_Private_Type
(Ftyp
)
10933 and then not Is_Private_Type
(Etype
(Actual
))
10934 and then (Base_Type
(Full_View
(Ftyp
)) = Base_Type
(Etype
(Actual
))
10935 or else Base_Type
(Etype
(Actual
)) = Ftyp
)
10937 -- If the actual has the type of the full view of the formal, or
10938 -- else a non-private subtype of the formal, then the visibility
10939 -- of the formal type has changed. Add to the actuals a subtype
10940 -- declaration that will force the exchange of views in the body
10941 -- of the instance as well.
10944 Make_Subtype_Declaration
(Loc
,
10945 Defining_Identifier
=> Make_Temporary
(Loc
, 'P'),
10946 Subtype_Indication
=> New_Occurrence_Of
(Ftyp
, Loc
));
10948 Prepend
(Subt_Decl
, List
);
10950 Prepend_Elmt
(Full_View
(Ftyp
), Exchanged_Views
);
10951 Exchange_Declarations
(Ftyp
);
10954 Resolve
(Actual
, Ftyp
);
10956 if not Denotes_Variable
(Actual
) then
10957 Error_Msg_NE
("actual for& must be a variable", Actual
, Gen_Obj
);
10959 elsif Base_Type
(Ftyp
) /= Base_Type
(Etype
(Actual
)) then
10961 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10962 -- the type of the actual shall resolve to a specific anonymous
10965 if Ada_Version
< Ada_2005
10966 or else Ekind
(Base_Type
(Ftyp
)) /=
10967 E_Anonymous_Access_Type
10968 or else Ekind
(Base_Type
(Etype
(Actual
))) /=
10969 E_Anonymous_Access_Type
10972 ("type of actual does not match type of&", Actual
, Gen_Obj
);
10976 Note_Possible_Modification
(Actual
, Sure
=> True);
10978 -- Check for instantiation of atomic/volatile actual for
10979 -- non-atomic/volatile formal (RM C.6 (12)).
10981 if Is_Atomic_Object
(Actual
) and then not Is_Atomic
(Orig_Ftyp
) then
10983 ("cannot instantiate non-atomic formal object "
10984 & "with atomic actual", Actual
);
10986 elsif Is_Volatile_Object
(Actual
) and then not Is_Volatile
(Orig_Ftyp
)
10989 ("cannot instantiate non-volatile formal object "
10990 & "with volatile actual", Actual
);
10993 -- Formal in-parameter
10996 -- The instantiation of a generic formal in-parameter is constant
10997 -- declaration. The actual is the expression for that declaration.
10998 -- Its type is a full copy of the type of the formal. This may be
10999 -- an access to subprogram, for which we need to generate entities
11000 -- for the formals in the new signature.
11002 if Present
(Actual
) then
11003 if Present
(Subt_Mark
) then
11004 Def
:= New_Copy_Tree
(Subt_Mark
);
11005 else pragma Assert
(Present
(Acc_Def
));
11006 Def
:= Copy_Access_Def
;
11010 Make_Object_Declaration
(Loc
,
11011 Defining_Identifier
=> New_Copy
(Gen_Obj
),
11012 Constant_Present
=> True,
11013 Null_Exclusion_Present
=> Null_Exclusion_Present
(Formal
),
11014 Object_Definition
=> Def
,
11015 Expression
=> Actual
);
11017 Set_Corresponding_Generic_Association
(Decl_Node
, Act_Assoc
);
11019 -- A generic formal object of a tagged type is defined to be
11020 -- aliased so the new constant must also be treated as aliased.
11022 if Is_Tagged_Type
(Etype
(A_Gen_Obj
)) then
11023 Set_Aliased_Present
(Decl_Node
);
11026 Append
(Decl_Node
, List
);
11028 -- No need to repeat (pre-)analysis of some expression nodes
11029 -- already handled in Preanalyze_Actuals.
11031 if Nkind
(Actual
) /= N_Allocator
then
11034 -- Return if the analysis of the actual reported some error
11036 if Etype
(Actual
) = Any_Type
then
11042 Formal_Type
: constant Entity_Id
:= Etype
(A_Gen_Obj
);
11046 Typ
:= Get_Instance_Of
(Formal_Type
);
11048 -- If the actual appears in the current or an enclosing scope,
11049 -- use its type directly. This is relevant if it has an actual
11050 -- subtype that is distinct from its nominal one. This cannot
11051 -- be done in general because the type of the actual may
11052 -- depend on other actuals, and only be fully determined when
11053 -- the enclosing instance is analyzed.
11055 if Present
(Etype
(Actual
))
11056 and then Is_Constr_Subt_For_U_Nominal
(Etype
(Actual
))
11058 Freeze_Before
(Instantiation_Node
, Etype
(Actual
));
11060 Freeze_Before
(Instantiation_Node
, Typ
);
11063 -- If the actual is an aggregate, perform name resolution on
11064 -- its components (the analysis of an aggregate does not do it)
11065 -- to capture local names that may be hidden if the generic is
11068 if Nkind
(Actual
) = N_Aggregate
then
11069 Preanalyze_And_Resolve
(Actual
, Typ
);
11072 if Is_Limited_Type
(Typ
)
11073 and then not OK_For_Limited_Init
(Typ
, Actual
)
11076 ("initialization not allowed for limited types", Actual
);
11077 Explain_Limited_Type
(Typ
, Actual
);
11081 elsif Present
(Default_Expression
(Formal
)) then
11083 -- Use default to construct declaration
11085 if Present
(Subt_Mark
) then
11086 Def
:= New_Copy
(Subt_Mark
);
11087 else pragma Assert
(Present
(Acc_Def
));
11088 Def
:= Copy_Access_Def
;
11092 Make_Object_Declaration
(Sloc
(Formal
),
11093 Defining_Identifier
=> New_Copy
(Gen_Obj
),
11094 Constant_Present
=> True,
11095 Null_Exclusion_Present
=> Null_Exclusion_Present
(Formal
),
11096 Object_Definition
=> Def
,
11097 Expression
=> New_Copy_Tree
11098 (Default_Expression
(Formal
)));
11100 Append
(Decl_Node
, List
);
11101 Set_Analyzed
(Expression
(Decl_Node
), False);
11104 Error_Msg_NE
("missing actual&", Instantiation_Node
, Gen_Obj
);
11105 Error_Msg_NE
("\in instantiation of & declared#",
11106 Instantiation_Node
, Scope
(A_Gen_Obj
));
11108 if Is_Scalar_Type
(Etype
(A_Gen_Obj
)) then
11110 -- Create dummy constant declaration so that instance can be
11111 -- analyzed, to minimize cascaded visibility errors.
11113 if Present
(Subt_Mark
) then
11115 else pragma Assert
(Present
(Acc_Def
));
11120 Make_Object_Declaration
(Loc
,
11121 Defining_Identifier
=> New_Copy
(Gen_Obj
),
11122 Constant_Present
=> True,
11123 Null_Exclusion_Present
=> Null_Exclusion_Present
(Formal
),
11124 Object_Definition
=> New_Copy
(Def
),
11126 Make_Attribute_Reference
(Sloc
(Gen_Obj
),
11127 Attribute_Name
=> Name_First
,
11128 Prefix
=> New_Copy
(Def
)));
11130 Append
(Decl_Node
, List
);
11133 Abandon_Instantiation
(Instantiation_Node
);
11138 if Nkind
(Actual
) in N_Has_Entity
then
11139 Actual_Decl
:= Parent
(Entity
(Actual
));
11142 -- Ada 2005 (AI-423): For a formal object declaration with a null
11143 -- exclusion or an access definition that has a null exclusion: If the
11144 -- actual matching the formal object declaration denotes a generic
11145 -- formal object of another generic unit G, and the instantiation
11146 -- containing the actual occurs within the body of G or within the body
11147 -- of a generic unit declared within the declarative region of G, then
11148 -- the declaration of the formal object of G must have a null exclusion.
11149 -- Otherwise, the subtype of the actual matching the formal object
11150 -- declaration shall exclude null.
11152 if Ada_Version
>= Ada_2005
11153 and then Present
(Actual_Decl
)
11154 and then Nkind_In
(Actual_Decl
, N_Formal_Object_Declaration
,
11155 N_Object_Declaration
)
11156 and then Nkind
(Analyzed_Formal
) = N_Formal_Object_Declaration
11157 and then not Has_Null_Exclusion
(Actual_Decl
)
11158 and then Has_Null_Exclusion
(Analyzed_Formal
)
11160 Error_Msg_Sloc
:= Sloc
(Analyzed_Formal
);
11162 ("actual must exclude null to match generic formal#", Actual
);
11165 -- An effectively volatile object cannot be used as an actual in a
11166 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
11167 -- relevant only when SPARK_Mode is on as it is not a standard Ada
11168 -- legality rule, and also verifies that the actual is an object.
11171 and then Present
(Actual
)
11172 and then Is_Object_Reference
(Actual
)
11173 and then Is_Effectively_Volatile_Object
(Actual
)
11176 ("volatile object cannot act as actual in generic instantiation",
11181 end Instantiate_Object
;
11183 ------------------------------
11184 -- Instantiate_Package_Body --
11185 ------------------------------
11187 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11188 -- must be replaced by gotos which jump to the end of the routine in order
11189 -- to restore the Ghost and SPARK modes.
11191 procedure Instantiate_Package_Body
11192 (Body_Info
: Pending_Body_Info
;
11193 Inlined_Body
: Boolean := False;
11194 Body_Optional
: Boolean := False)
11196 Act_Decl
: constant Node_Id
:= Body_Info
.Act_Decl
;
11197 Act_Decl_Id
: constant Entity_Id
:= Defining_Entity
(Act_Decl
);
11198 Act_Spec
: constant Node_Id
:= Specification
(Act_Decl
);
11199 Inst_Node
: constant Node_Id
:= Body_Info
.Inst_Node
;
11200 Gen_Id
: constant Node_Id
:= Name
(Inst_Node
);
11201 Gen_Unit
: constant Entity_Id
:= Get_Generic_Entity
(Inst_Node
);
11202 Gen_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Gen_Unit
);
11203 Loc
: constant Source_Ptr
:= Sloc
(Inst_Node
);
11205 Saved_ISMP
: constant Boolean :=
11206 Ignore_SPARK_Mode_Pragmas_In_Instance
;
11207 Saved_Style_Check
: constant Boolean := Style_Check
;
11209 procedure Check_Initialized_Types
;
11210 -- In a generic package body, an entity of a generic private type may
11211 -- appear uninitialized. This is suspicious, unless the actual is a
11212 -- fully initialized type.
11214 -----------------------------
11215 -- Check_Initialized_Types --
11216 -----------------------------
11218 procedure Check_Initialized_Types
is
11220 Formal
: Entity_Id
;
11221 Actual
: Entity_Id
;
11222 Uninit_Var
: Entity_Id
;
11225 Decl
:= First
(Generic_Formal_Declarations
(Gen_Decl
));
11226 while Present
(Decl
) loop
11227 Uninit_Var
:= Empty
;
11229 if Nkind
(Decl
) = N_Private_Extension_Declaration
then
11230 Uninit_Var
:= Uninitialized_Variable
(Decl
);
11232 elsif Nkind
(Decl
) = N_Formal_Type_Declaration
11233 and then Nkind
(Formal_Type_Definition
(Decl
)) =
11234 N_Formal_Private_Type_Definition
11237 Uninitialized_Variable
(Formal_Type_Definition
(Decl
));
11240 if Present
(Uninit_Var
) then
11241 Formal
:= Defining_Identifier
(Decl
);
11242 Actual
:= First_Entity
(Act_Decl_Id
);
11244 -- For each formal there is a subtype declaration that renames
11245 -- the actual and has the same name as the formal. Locate the
11246 -- formal for warning message about uninitialized variables
11247 -- in the generic, for which the actual type should be a fully
11248 -- initialized type.
11250 while Present
(Actual
) loop
11251 exit when Ekind
(Actual
) = E_Package
11252 and then Present
(Renamed_Object
(Actual
));
11254 if Chars
(Actual
) = Chars
(Formal
)
11255 and then not Is_Scalar_Type
(Actual
)
11256 and then not Is_Fully_Initialized_Type
(Actual
)
11257 and then Warn_On_No_Value_Assigned
11259 Error_Msg_Node_2
:= Formal
;
11261 ("generic unit has uninitialized variable& of "
11262 & "formal private type &?v?", Actual
, Uninit_Var
);
11264 ("actual type for& should be fully initialized type?v?",
11269 Next_Entity
(Actual
);
11275 end Check_Initialized_Types
;
11279 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
11280 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
11281 Saved_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
11282 Saved_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
11283 -- Save the Ghost and SPARK mode-related data to restore on exit
11285 Act_Body
: Node_Id
;
11286 Act_Body_Id
: Entity_Id
;
11287 Act_Body_Name
: Node_Id
;
11288 Gen_Body
: Node_Id
;
11289 Gen_Body_Id
: Node_Id
;
11290 Par_Ent
: Entity_Id
:= Empty
;
11291 Par_Vis
: Boolean := False;
11292 Parent_Installed
: Boolean := False;
11294 Vis_Prims_List
: Elist_Id
:= No_Elist
;
11295 -- List of primitives made temporarily visible in the instantiation
11296 -- to match the visibility of the formal type.
11298 -- Start of processing for Instantiate_Package_Body
11301 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
11303 -- The instance body may already have been processed, as the parent of
11304 -- another instance that is inlined (Load_Parent_Of_Generic).
11306 if Present
(Corresponding_Body
(Instance_Spec
(Inst_Node
))) then
11310 -- The package being instantiated may be subject to pragma Ghost. Set
11311 -- the mode now to ensure that any nodes generated during instantiation
11312 -- are properly marked as Ghost.
11314 Set_Ghost_Mode
(Act_Decl_Id
);
11316 Expander_Mode_Save_And_Set
(Body_Info
.Expander_Status
);
11318 -- Re-establish the state of information on which checks are suppressed.
11319 -- This information was set in Body_Info at the point of instantiation,
11320 -- and now we restore it so that the instance is compiled using the
11321 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11323 Local_Suppress_Stack_Top
:= Body_Info
.Local_Suppress_Stack_Top
;
11324 Scope_Suppress
:= Body_Info
.Scope_Suppress
;
11326 Restore_Config_Switches
(Body_Info
.Config_Switches
);
11327 Restore_Warnings
(Body_Info
.Warnings
);
11329 if No
(Gen_Body_Id
) then
11331 -- Do not look for parent of generic body if none is required.
11332 -- This may happen when the routine is called as part of the
11333 -- Pending_Instantiations processing, when nested instances
11334 -- may precede the one generated from the main unit.
11336 if not Unit_Requires_Body
(Defining_Entity
(Gen_Decl
))
11337 and then Body_Optional
11341 Load_Parent_Of_Generic
11342 (Inst_Node
, Specification
(Gen_Decl
), Body_Optional
);
11343 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
11347 -- Establish global variable for sloc adjustment and for error recovery
11348 -- In the case of an instance body for an instantiation with actuals
11349 -- from a limited view, the instance body is placed at the beginning
11350 -- of the enclosing package body: use the body entity as the source
11351 -- location for nodes of the instance body.
11353 if not Is_Empty_Elmt_List
(Incomplete_Actuals
(Act_Decl_Id
)) then
11355 Scop
: constant Entity_Id
:= Scope
(Act_Decl_Id
);
11356 Body_Id
: constant Node_Id
:=
11357 Corresponding_Body
(Unit_Declaration_Node
(Scop
));
11360 Instantiation_Node
:= Body_Id
;
11363 Instantiation_Node
:= Inst_Node
;
11366 if Present
(Gen_Body_Id
) then
11367 Save_Env
(Gen_Unit
, Act_Decl_Id
);
11368 Style_Check
:= False;
11370 -- If the context of the instance is subject to SPARK_Mode "off", the
11371 -- annotation is missing, or the body is instantiated at a later pass
11372 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11373 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11376 if SPARK_Mode
/= On
11377 or else Ignore_SPARK_Mode_Pragmas
(Act_Decl_Id
)
11379 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
11382 Current_Sem_Unit
:= Body_Info
.Current_Sem_Unit
;
11383 Gen_Body
:= Unit_Declaration_Node
(Gen_Body_Id
);
11385 Create_Instantiation_Source
11386 (Inst_Node
, Gen_Body_Id
, S_Adjustment
);
11390 (Original_Node
(Gen_Body
), Empty
, Instantiating
=> True);
11392 -- Create proper (possibly qualified) defining name for the body, to
11393 -- correspond to the one in the spec.
11396 Make_Defining_Identifier
(Sloc
(Act_Decl_Id
), Chars
(Act_Decl_Id
));
11397 Set_Comes_From_Source
(Act_Body_Id
, Comes_From_Source
(Act_Decl_Id
));
11399 -- Some attributes of spec entity are not inherited by body entity
11401 Set_Handler_Records
(Act_Body_Id
, No_List
);
11403 if Nkind
(Defining_Unit_Name
(Act_Spec
)) =
11404 N_Defining_Program_Unit_Name
11407 Make_Defining_Program_Unit_Name
(Loc
,
11409 New_Copy_Tree
(Name
(Defining_Unit_Name
(Act_Spec
))),
11410 Defining_Identifier
=> Act_Body_Id
);
11412 Act_Body_Name
:= Act_Body_Id
;
11415 Set_Defining_Unit_Name
(Act_Body
, Act_Body_Name
);
11417 Set_Corresponding_Spec
(Act_Body
, Act_Decl_Id
);
11418 Check_Generic_Actuals
(Act_Decl_Id
, False);
11419 Check_Initialized_Types
;
11421 -- Install primitives hidden at the point of the instantiation but
11422 -- visible when processing the generic formals
11428 E
:= First_Entity
(Act_Decl_Id
);
11429 while Present
(E
) loop
11431 and then not Is_Itype
(E
)
11432 and then Is_Generic_Actual_Type
(E
)
11433 and then Is_Tagged_Type
(E
)
11435 Install_Hidden_Primitives
11436 (Prims_List
=> Vis_Prims_List
,
11437 Gen_T
=> Generic_Parent_Type
(Parent
(E
)),
11445 -- If it is a child unit, make the parent instance (which is an
11446 -- instance of the parent of the generic) visible. The parent
11447 -- instance is the prefix of the name of the generic unit.
11449 if Ekind
(Scope
(Gen_Unit
)) = E_Generic_Package
11450 and then Nkind
(Gen_Id
) = N_Expanded_Name
11452 Par_Ent
:= Entity
(Prefix
(Gen_Id
));
11453 Par_Vis
:= Is_Immediately_Visible
(Par_Ent
);
11454 Install_Parent
(Par_Ent
, In_Body
=> True);
11455 Parent_Installed
:= True;
11457 elsif Is_Child_Unit
(Gen_Unit
) then
11458 Par_Ent
:= Scope
(Gen_Unit
);
11459 Par_Vis
:= Is_Immediately_Visible
(Par_Ent
);
11460 Install_Parent
(Par_Ent
, In_Body
=> True);
11461 Parent_Installed
:= True;
11464 -- If the instantiation is a library unit, and this is the main unit,
11465 -- then build the resulting compilation unit nodes for the instance.
11466 -- If this is a compilation unit but it is not the main unit, then it
11467 -- is the body of a unit in the context, that is being compiled
11468 -- because it is encloses some inlined unit or another generic unit
11469 -- being instantiated. In that case, this body is not part of the
11470 -- current compilation, and is not attached to the tree, but its
11471 -- parent must be set for analysis.
11473 if Nkind
(Parent
(Inst_Node
)) = N_Compilation_Unit
then
11475 -- Replace instance node with body of instance, and create new
11476 -- node for corresponding instance declaration.
11478 Build_Instance_Compilation_Unit_Nodes
11479 (Inst_Node
, Act_Body
, Act_Decl
);
11480 Analyze
(Inst_Node
);
11482 if Parent
(Inst_Node
) = Cunit
(Main_Unit
) then
11484 -- If the instance is a child unit itself, then set the scope
11485 -- of the expanded body to be the parent of the instantiation
11486 -- (ensuring that the fully qualified name will be generated
11487 -- for the elaboration subprogram).
11489 if Nkind
(Defining_Unit_Name
(Act_Spec
)) =
11490 N_Defining_Program_Unit_Name
11492 Set_Scope
(Defining_Entity
(Inst_Node
), Scope
(Act_Decl_Id
));
11496 -- Case where instantiation is not a library unit
11499 -- If this is an early instantiation, i.e. appears textually
11500 -- before the corresponding body and must be elaborated first,
11501 -- indicate that the body instance is to be delayed.
11503 Install_Body
(Act_Body
, Inst_Node
, Gen_Body
, Gen_Decl
);
11505 -- Now analyze the body. We turn off all checks if this is an
11506 -- internal unit, since there is no reason to have checks on for
11507 -- any predefined run-time library code. All such code is designed
11508 -- to be compiled with checks off.
11510 -- Note that we do NOT apply this criterion to children of GNAT
11511 -- The latter units must suppress checks explicitly if needed.
11513 -- We also do not suppress checks in CodePeer mode where we are
11514 -- interested in finding possible runtime errors.
11516 if not CodePeer_Mode
11517 and then In_Predefined_Unit
(Gen_Decl
)
11519 Analyze
(Act_Body
, Suppress
=> All_Checks
);
11521 Analyze
(Act_Body
);
11525 Inherit_Context
(Gen_Body
, Inst_Node
);
11527 -- Remove the parent instances if they have been placed on the scope
11528 -- stack to compile the body.
11530 if Parent_Installed
then
11531 Remove_Parent
(In_Body
=> True);
11533 -- Restore the previous visibility of the parent
11535 Set_Is_Immediately_Visible
(Par_Ent
, Par_Vis
);
11538 Restore_Hidden_Primitives
(Vis_Prims_List
);
11539 Restore_Private_Views
(Act_Decl_Id
);
11541 -- Remove the current unit from visibility if this is an instance
11542 -- that is not elaborated on the fly for inlining purposes.
11544 if not Inlined_Body
then
11545 Set_Is_Immediately_Visible
(Act_Decl_Id
, False);
11550 -- If we have no body, and the unit requires a body, then complain. This
11551 -- complaint is suppressed if we have detected other errors (since a
11552 -- common reason for missing the body is that it had errors).
11553 -- In CodePeer mode, a warning has been emitted already, no need for
11554 -- further messages.
11556 elsif Unit_Requires_Body
(Gen_Unit
)
11557 and then not Body_Optional
11559 if CodePeer_Mode
then
11562 elsif Serious_Errors_Detected
= 0 then
11564 ("cannot find body of generic package &", Inst_Node
, Gen_Unit
);
11566 -- Don't attempt to perform any cleanup actions if some other error
11567 -- was already detected, since this can cause blowups.
11573 -- Case of package that does not need a body
11576 -- If the instantiation of the declaration is a library unit, rewrite
11577 -- the original package instantiation as a package declaration in the
11578 -- compilation unit node.
11580 if Nkind
(Parent
(Inst_Node
)) = N_Compilation_Unit
then
11581 Set_Parent_Spec
(Act_Decl
, Parent_Spec
(Inst_Node
));
11582 Rewrite
(Inst_Node
, Act_Decl
);
11584 -- Generate elaboration entity, in case spec has elaboration code.
11585 -- This cannot be done when the instance is analyzed, because it
11586 -- is not known yet whether the body exists.
11588 Set_Elaboration_Entity_Required
(Act_Decl_Id
, False);
11589 Build_Elaboration_Entity
(Parent
(Inst_Node
), Act_Decl_Id
);
11591 -- If the instantiation is not a library unit, then append the
11592 -- declaration to the list of implicitly generated entities, unless
11593 -- it is already a list member which means that it was already
11596 elsif not Is_List_Member
(Act_Decl
) then
11597 Mark_Rewrite_Insertion
(Act_Decl
);
11598 Insert_Before
(Inst_Node
, Act_Decl
);
11602 Expander_Mode_Restore
;
11605 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
11606 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
11607 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
11608 Style_Check
:= Saved_Style_Check
;
11609 end Instantiate_Package_Body
;
11611 ---------------------------------
11612 -- Instantiate_Subprogram_Body --
11613 ---------------------------------
11615 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11616 -- must be replaced by gotos which jump to the end of the routine in order
11617 -- to restore the Ghost and SPARK modes.
11619 procedure Instantiate_Subprogram_Body
11620 (Body_Info
: Pending_Body_Info
;
11621 Body_Optional
: Boolean := False)
11623 Act_Decl
: constant Node_Id
:= Body_Info
.Act_Decl
;
11624 Act_Decl_Id
: constant Entity_Id
:= Defining_Entity
(Act_Decl
);
11625 Inst_Node
: constant Node_Id
:= Body_Info
.Inst_Node
;
11626 Gen_Id
: constant Node_Id
:= Name
(Inst_Node
);
11627 Gen_Unit
: constant Entity_Id
:= Get_Generic_Entity
(Inst_Node
);
11628 Gen_Decl
: constant Node_Id
:= Unit_Declaration_Node
(Gen_Unit
);
11629 Loc
: constant Source_Ptr
:= Sloc
(Inst_Node
);
11630 Pack_Id
: constant Entity_Id
:=
11631 Defining_Unit_Name
(Parent
(Act_Decl
));
11633 Saved_GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
11634 Saved_IGR
: constant Node_Id
:= Ignored_Ghost_Region
;
11635 Saved_ISMP
: constant Boolean :=
11636 Ignore_SPARK_Mode_Pragmas_In_Instance
;
11637 Saved_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
11638 Saved_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
11639 -- Save the Ghost and SPARK mode-related data to restore on exit
11641 Saved_Style_Check
: constant Boolean := Style_Check
;
11642 Saved_Warnings
: constant Warning_Record
:= Save_Warnings
;
11644 Act_Body
: Node_Id
;
11645 Act_Body_Id
: Entity_Id
;
11646 Gen_Body
: Node_Id
;
11647 Gen_Body_Id
: Node_Id
;
11648 Pack_Body
: Node_Id
;
11649 Par_Ent
: Entity_Id
:= Empty
;
11650 Par_Vis
: Boolean := False;
11651 Ret_Expr
: Node_Id
;
11653 Parent_Installed
: Boolean := False;
11656 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
11658 -- Subprogram body may have been created already because of an inline
11659 -- pragma, or because of multiple elaborations of the enclosing package
11660 -- when several instances of the subprogram appear in the main unit.
11662 if Present
(Corresponding_Body
(Act_Decl
)) then
11666 -- The subprogram being instantiated may be subject to pragma Ghost. Set
11667 -- the mode now to ensure that any nodes generated during instantiation
11668 -- are properly marked as Ghost.
11670 Set_Ghost_Mode
(Act_Decl_Id
);
11672 Expander_Mode_Save_And_Set
(Body_Info
.Expander_Status
);
11674 -- Re-establish the state of information on which checks are suppressed.
11675 -- This information was set in Body_Info at the point of instantiation,
11676 -- and now we restore it so that the instance is compiled using the
11677 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11679 Local_Suppress_Stack_Top
:= Body_Info
.Local_Suppress_Stack_Top
;
11680 Scope_Suppress
:= Body_Info
.Scope_Suppress
;
11682 Restore_Config_Switches
(Body_Info
.Config_Switches
);
11683 Restore_Warnings
(Body_Info
.Warnings
);
11685 if No
(Gen_Body_Id
) then
11687 -- For imported generic subprogram, no body to compile, complete
11688 -- the spec entity appropriately.
11690 if Is_Imported
(Gen_Unit
) then
11691 Set_Is_Imported
(Act_Decl_Id
);
11692 Set_First_Rep_Item
(Act_Decl_Id
, First_Rep_Item
(Gen_Unit
));
11693 Set_Interface_Name
(Act_Decl_Id
, Interface_Name
(Gen_Unit
));
11694 Set_Convention
(Act_Decl_Id
, Convention
(Gen_Unit
));
11695 Set_Has_Completion
(Act_Decl_Id
);
11698 -- For other cases, compile the body
11701 Load_Parent_Of_Generic
11702 (Inst_Node
, Specification
(Gen_Decl
), Body_Optional
);
11703 Gen_Body_Id
:= Corresponding_Body
(Gen_Decl
);
11707 Instantiation_Node
:= Inst_Node
;
11709 if Present
(Gen_Body_Id
) then
11710 Gen_Body
:= Unit_Declaration_Node
(Gen_Body_Id
);
11712 if Nkind
(Gen_Body
) = N_Subprogram_Body_Stub
then
11714 -- Either body is not present, or context is non-expanding, as
11715 -- when compiling a subunit. Mark the instance as completed, and
11716 -- diagnose a missing body when needed.
11719 and then Operating_Mode
= Generate_Code
11721 Error_Msg_N
("missing proper body for instantiation", Gen_Body
);
11724 Set_Has_Completion
(Act_Decl_Id
);
11728 Save_Env
(Gen_Unit
, Act_Decl_Id
);
11729 Style_Check
:= False;
11731 -- If the context of the instance is subject to SPARK_Mode "off", the
11732 -- annotation is missing, or the body is instantiated at a later pass
11733 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11734 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11737 if SPARK_Mode
/= On
11738 or else Ignore_SPARK_Mode_Pragmas
(Act_Decl_Id
)
11740 Ignore_SPARK_Mode_Pragmas_In_Instance
:= True;
11743 -- If the context of an instance is not subject to SPARK_Mode "off",
11744 -- and the generic body is subject to an explicit SPARK_Mode pragma,
11745 -- the latter should be the one applicable to the instance.
11747 if not Ignore_SPARK_Mode_Pragmas_In_Instance
11748 and then SPARK_Mode
/= Off
11749 and then Present
(SPARK_Pragma
(Gen_Body_Id
))
11751 Set_SPARK_Mode
(Gen_Body_Id
);
11754 Current_Sem_Unit
:= Body_Info
.Current_Sem_Unit
;
11755 Create_Instantiation_Source
11762 (Original_Node
(Gen_Body
), Empty
, Instantiating
=> True);
11764 -- Create proper defining name for the body, to correspond to the one
11768 Make_Defining_Identifier
(Sloc
(Act_Decl_Id
), Chars
(Act_Decl_Id
));
11770 Set_Comes_From_Source
(Act_Body_Id
, Comes_From_Source
(Act_Decl_Id
));
11771 Set_Defining_Unit_Name
(Specification
(Act_Body
), Act_Body_Id
);
11773 Set_Corresponding_Spec
(Act_Body
, Act_Decl_Id
);
11774 Set_Has_Completion
(Act_Decl_Id
);
11775 Check_Generic_Actuals
(Pack_Id
, False);
11777 -- Generate a reference to link the visible subprogram instance to
11778 -- the generic body, which for navigation purposes is the only
11779 -- available source for the instance.
11782 (Related_Instance
(Pack_Id
),
11783 Gen_Body_Id
, 'b', Set_Ref
=> False, Force
=> True);
11785 -- If it is a child unit, make the parent instance (which is an
11786 -- instance of the parent of the generic) visible. The parent
11787 -- instance is the prefix of the name of the generic unit.
11789 if Ekind
(Scope
(Gen_Unit
)) = E_Generic_Package
11790 and then Nkind
(Gen_Id
) = N_Expanded_Name
11792 Par_Ent
:= Entity
(Prefix
(Gen_Id
));
11793 Par_Vis
:= Is_Immediately_Visible
(Par_Ent
);
11794 Install_Parent
(Par_Ent
, In_Body
=> True);
11795 Parent_Installed
:= True;
11797 elsif Is_Child_Unit
(Gen_Unit
) then
11798 Par_Ent
:= Scope
(Gen_Unit
);
11799 Par_Vis
:= Is_Immediately_Visible
(Par_Ent
);
11800 Install_Parent
(Par_Ent
, In_Body
=> True);
11801 Parent_Installed
:= True;
11804 -- Subprogram body is placed in the body of wrapper package,
11805 -- whose spec contains the subprogram declaration as well as
11806 -- the renaming declarations for the generic parameters.
11809 Make_Package_Body
(Loc
,
11810 Defining_Unit_Name
=> New_Copy
(Pack_Id
),
11811 Declarations
=> New_List
(Act_Body
));
11813 Set_Corresponding_Spec
(Pack_Body
, Pack_Id
);
11815 -- If the instantiation is a library unit, then build resulting
11816 -- compilation unit nodes for the instance. The declaration of
11817 -- the enclosing package is the grandparent of the subprogram
11818 -- declaration. First replace the instantiation node as the unit
11819 -- of the corresponding compilation.
11821 if Nkind
(Parent
(Inst_Node
)) = N_Compilation_Unit
then
11822 if Parent
(Inst_Node
) = Cunit
(Main_Unit
) then
11823 Set_Unit
(Parent
(Inst_Node
), Inst_Node
);
11824 Build_Instance_Compilation_Unit_Nodes
11825 (Inst_Node
, Pack_Body
, Parent
(Parent
(Act_Decl
)));
11826 Analyze
(Inst_Node
);
11828 Set_Parent
(Pack_Body
, Parent
(Inst_Node
));
11829 Analyze
(Pack_Body
);
11833 Insert_Before
(Inst_Node
, Pack_Body
);
11834 Mark_Rewrite_Insertion
(Pack_Body
);
11835 Analyze
(Pack_Body
);
11837 if Expander_Active
then
11838 Freeze_Subprogram_Body
(Inst_Node
, Gen_Body
, Pack_Id
);
11842 Inherit_Context
(Gen_Body
, Inst_Node
);
11844 Restore_Private_Views
(Pack_Id
, False);
11846 if Parent_Installed
then
11847 Remove_Parent
(In_Body
=> True);
11849 -- Restore the previous visibility of the parent
11851 Set_Is_Immediately_Visible
(Par_Ent
, Par_Vis
);
11855 Restore_Warnings
(Saved_Warnings
);
11857 -- Body not found. Error was emitted already. If there were no previous
11858 -- errors, this may be an instance whose scope is a premature instance.
11859 -- In that case we must insure that the (legal) program does raise
11860 -- program error if executed. We generate a subprogram body for this
11861 -- purpose. See DEC ac30vso.
11863 -- Should not reference proprietary DEC tests in comments ???
11865 elsif Serious_Errors_Detected
= 0
11866 and then Nkind
(Parent
(Inst_Node
)) /= N_Compilation_Unit
11868 if Body_Optional
then
11871 elsif Ekind
(Act_Decl_Id
) = E_Procedure
then
11873 Make_Subprogram_Body
(Loc
,
11875 Make_Procedure_Specification
(Loc
,
11876 Defining_Unit_Name
=>
11877 Make_Defining_Identifier
(Loc
, Chars
(Act_Decl_Id
)),
11878 Parameter_Specifications
=>
11880 (Parameter_Specifications
(Parent
(Act_Decl_Id
)))),
11882 Declarations
=> Empty_List
,
11883 Handled_Statement_Sequence
=>
11884 Make_Handled_Sequence_Of_Statements
(Loc
,
11885 Statements
=> New_List
(
11886 Make_Raise_Program_Error
(Loc
,
11887 Reason
=> PE_Access_Before_Elaboration
))));
11891 Make_Raise_Program_Error
(Loc
,
11892 Reason
=> PE_Access_Before_Elaboration
);
11894 Set_Etype
(Ret_Expr
, (Etype
(Act_Decl_Id
)));
11895 Set_Analyzed
(Ret_Expr
);
11898 Make_Subprogram_Body
(Loc
,
11900 Make_Function_Specification
(Loc
,
11901 Defining_Unit_Name
=>
11902 Make_Defining_Identifier
(Loc
, Chars
(Act_Decl_Id
)),
11903 Parameter_Specifications
=>
11905 (Parameter_Specifications
(Parent
(Act_Decl_Id
))),
11906 Result_Definition
=>
11907 New_Occurrence_Of
(Etype
(Act_Decl_Id
), Loc
)),
11909 Declarations
=> Empty_List
,
11910 Handled_Statement_Sequence
=>
11911 Make_Handled_Sequence_Of_Statements
(Loc
,
11912 Statements
=> New_List
(
11913 Make_Simple_Return_Statement
(Loc
, Ret_Expr
))));
11917 Make_Package_Body
(Loc
,
11918 Defining_Unit_Name
=> New_Copy
(Pack_Id
),
11919 Declarations
=> New_List
(Act_Body
));
11921 Insert_After
(Inst_Node
, Pack_Body
);
11922 Set_Corresponding_Spec
(Pack_Body
, Pack_Id
);
11923 Analyze
(Pack_Body
);
11926 Expander_Mode_Restore
;
11929 Ignore_SPARK_Mode_Pragmas_In_Instance
:= Saved_ISMP
;
11930 Restore_Ghost_Region
(Saved_GM
, Saved_IGR
);
11931 Restore_SPARK_Mode
(Saved_SM
, Saved_SMP
);
11932 Style_Check
:= Saved_Style_Check
;
11933 end Instantiate_Subprogram_Body
;
11935 ----------------------
11936 -- Instantiate_Type --
11937 ----------------------
11939 function Instantiate_Type
11942 Analyzed_Formal
: Node_Id
;
11943 Actual_Decls
: List_Id
) return List_Id
11945 A_Gen_T
: constant Entity_Id
:=
11946 Defining_Identifier
(Analyzed_Formal
);
11947 Def
: constant Node_Id
:= Formal_Type_Definition
(Formal
);
11948 Gen_T
: constant Entity_Id
:= Defining_Identifier
(Formal
);
11950 Ancestor
: Entity_Id
:= Empty
;
11951 Decl_Node
: Node_Id
;
11952 Decl_Nodes
: List_Id
;
11956 procedure Diagnose_Predicated_Actual
;
11957 -- There are a number of constructs in which a discrete type with
11958 -- predicates is illegal, e.g. as an index in an array type declaration.
11959 -- If a generic type is used is such a construct in a generic package
11960 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11961 -- of the generic contract that the actual cannot have predicates.
11963 procedure Validate_Array_Type_Instance
;
11964 procedure Validate_Access_Subprogram_Instance
;
11965 procedure Validate_Access_Type_Instance
;
11966 procedure Validate_Derived_Type_Instance
;
11967 procedure Validate_Derived_Interface_Type_Instance
;
11968 procedure Validate_Discriminated_Formal_Type
;
11969 procedure Validate_Interface_Type_Instance
;
11970 procedure Validate_Private_Type_Instance
;
11971 procedure Validate_Incomplete_Type_Instance
;
11972 -- These procedures perform validation tests for the named case.
11973 -- Validate_Discriminated_Formal_Type is shared by formal private
11974 -- types and Ada 2012 formal incomplete types.
11976 function Subtypes_Match
(Gen_T
, Act_T
: Entity_Id
) return Boolean;
11977 -- Check that base types are the same and that the subtypes match
11978 -- statically. Used in several of the above.
11980 ---------------------------------
11981 -- Diagnose_Predicated_Actual --
11982 ---------------------------------
11984 procedure Diagnose_Predicated_Actual
is
11986 if No_Predicate_On_Actual
(A_Gen_T
)
11987 and then Has_Predicates
(Act_T
)
11990 ("actual for& cannot be a type with predicate",
11991 Instantiation_Node
, A_Gen_T
);
11993 elsif No_Dynamic_Predicate_On_Actual
(A_Gen_T
)
11994 and then Has_Predicates
(Act_T
)
11995 and then not Has_Static_Predicate_Aspect
(Act_T
)
11998 ("actual for& cannot be a type with a dynamic predicate",
11999 Instantiation_Node
, A_Gen_T
);
12001 end Diagnose_Predicated_Actual
;
12003 --------------------
12004 -- Subtypes_Match --
12005 --------------------
12007 function Subtypes_Match
(Gen_T
, Act_T
: Entity_Id
) return Boolean is
12008 T
: constant Entity_Id
:= Get_Instance_Of
(Gen_T
);
12011 -- Some detailed comments would be useful here ???
12013 return ((Base_Type
(T
) = Act_T
12014 or else Base_Type
(T
) = Base_Type
(Act_T
))
12015 and then Subtypes_Statically_Match
(T
, Act_T
))
12017 or else (Is_Class_Wide_Type
(Gen_T
)
12018 and then Is_Class_Wide_Type
(Act_T
)
12019 and then Subtypes_Match
12020 (Get_Instance_Of
(Root_Type
(Gen_T
)),
12021 Root_Type
(Act_T
)))
12024 (Ekind_In
(Gen_T
, E_Anonymous_Access_Subprogram_Type
,
12025 E_Anonymous_Access_Type
)
12026 and then Ekind
(Act_T
) = Ekind
(Gen_T
)
12027 and then Subtypes_Statically_Match
12028 (Designated_Type
(Gen_T
), Designated_Type
(Act_T
)));
12029 end Subtypes_Match
;
12031 -----------------------------------------
12032 -- Validate_Access_Subprogram_Instance --
12033 -----------------------------------------
12035 procedure Validate_Access_Subprogram_Instance
is
12037 if not Is_Access_Type
(Act_T
)
12038 or else Ekind
(Designated_Type
(Act_T
)) /= E_Subprogram_Type
12041 ("expect access type in instantiation of &", Actual
, Gen_T
);
12042 Abandon_Instantiation
(Actual
);
12045 -- According to AI05-288, actuals for access_to_subprograms must be
12046 -- subtype conformant with the generic formal. Previous to AI05-288
12047 -- only mode conformance was required.
12049 -- This is a binding interpretation that applies to previous versions
12050 -- of the language, no need to maintain previous weaker checks.
12052 Check_Subtype_Conformant
12053 (Designated_Type
(Act_T
),
12054 Designated_Type
(A_Gen_T
),
12058 if Ekind
(Base_Type
(Act_T
)) = E_Access_Protected_Subprogram_Type
then
12059 if Ekind
(A_Gen_T
) = E_Access_Subprogram_Type
then
12061 ("protected access type not allowed for formal &",
12065 elsif Ekind
(A_Gen_T
) = E_Access_Protected_Subprogram_Type
then
12067 ("expect protected access type for formal &",
12071 -- If the formal has a specified convention (which in most cases
12072 -- will be StdCall) verify that the actual has the same convention.
12074 if Has_Convention_Pragma
(A_Gen_T
)
12075 and then Convention
(A_Gen_T
) /= Convention
(Act_T
)
12077 Error_Msg_Name_1
:= Get_Convention_Name
(Convention
(A_Gen_T
));
12079 ("actual for formal & must have convention %", Actual
, Gen_T
);
12081 end Validate_Access_Subprogram_Instance
;
12083 -----------------------------------
12084 -- Validate_Access_Type_Instance --
12085 -----------------------------------
12087 procedure Validate_Access_Type_Instance
is
12088 Desig_Type
: constant Entity_Id
:=
12089 Find_Actual_Type
(Designated_Type
(A_Gen_T
), A_Gen_T
);
12090 Desig_Act
: Entity_Id
;
12093 if not Is_Access_Type
(Act_T
) then
12095 ("expect access type in instantiation of &", Actual
, Gen_T
);
12096 Abandon_Instantiation
(Actual
);
12099 if Is_Access_Constant
(A_Gen_T
) then
12100 if not Is_Access_Constant
(Act_T
) then
12102 ("actual type must be access-to-constant type", Actual
);
12103 Abandon_Instantiation
(Actual
);
12106 if Is_Access_Constant
(Act_T
) then
12108 ("actual type must be access-to-variable type", Actual
);
12109 Abandon_Instantiation
(Actual
);
12111 elsif Ekind
(A_Gen_T
) = E_General_Access_Type
12112 and then Ekind
(Base_Type
(Act_T
)) /= E_General_Access_Type
12114 Error_Msg_N
-- CODEFIX
12115 ("actual must be general access type!", Actual
);
12116 Error_Msg_NE
-- CODEFIX
12117 ("add ALL to }!", Actual
, Act_T
);
12118 Abandon_Instantiation
(Actual
);
12122 -- The designated subtypes, that is to say the subtypes introduced
12123 -- by an access type declaration (and not by a subtype declaration)
12126 Desig_Act
:= Designated_Type
(Base_Type
(Act_T
));
12128 -- The designated type may have been introduced through a limited_
12129 -- with clause, in which case retrieve the non-limited view. This
12130 -- applies to incomplete types as well as to class-wide types.
12132 if From_Limited_With
(Desig_Act
) then
12133 Desig_Act
:= Available_View
(Desig_Act
);
12136 if not Subtypes_Match
(Desig_Type
, Desig_Act
) then
12138 ("designated type of actual does not match that of formal &",
12141 if not Predicates_Match
(Desig_Type
, Desig_Act
) then
12142 Error_Msg_N
("\predicates do not match", Actual
);
12145 Abandon_Instantiation
(Actual
);
12147 elsif Is_Access_Type
(Designated_Type
(Act_T
))
12148 and then Is_Constrained
(Designated_Type
(Designated_Type
(Act_T
)))
12150 Is_Constrained
(Designated_Type
(Desig_Type
))
12153 ("designated type of actual does not match that of formal &",
12156 if not Predicates_Match
(Desig_Type
, Desig_Act
) then
12157 Error_Msg_N
("\predicates do not match", Actual
);
12160 Abandon_Instantiation
(Actual
);
12163 -- Ada 2005: null-exclusion indicators of the two types must agree
12165 if Can_Never_Be_Null
(A_Gen_T
) /= Can_Never_Be_Null
(Act_T
) then
12167 ("non null exclusion of actual and formal & do not match",
12170 end Validate_Access_Type_Instance
;
12172 ----------------------------------
12173 -- Validate_Array_Type_Instance --
12174 ----------------------------------
12176 procedure Validate_Array_Type_Instance
is
12181 function Formal_Dimensions
return Nat
;
12182 -- Count number of dimensions in array type formal
12184 -----------------------
12185 -- Formal_Dimensions --
12186 -----------------------
12188 function Formal_Dimensions
return Nat
is
12193 if Nkind
(Def
) = N_Constrained_Array_Definition
then
12194 Index
:= First
(Discrete_Subtype_Definitions
(Def
));
12196 Index
:= First
(Subtype_Marks
(Def
));
12199 while Present
(Index
) loop
12201 Next_Index
(Index
);
12205 end Formal_Dimensions
;
12207 -- Start of processing for Validate_Array_Type_Instance
12210 if not Is_Array_Type
(Act_T
) then
12212 ("expect array type in instantiation of &", Actual
, Gen_T
);
12213 Abandon_Instantiation
(Actual
);
12215 elsif Nkind
(Def
) = N_Constrained_Array_Definition
then
12216 if not (Is_Constrained
(Act_T
)) then
12218 ("expect constrained array in instantiation of &",
12220 Abandon_Instantiation
(Actual
);
12224 if Is_Constrained
(Act_T
) then
12226 ("expect unconstrained array in instantiation of &",
12228 Abandon_Instantiation
(Actual
);
12232 if Formal_Dimensions
/= Number_Dimensions
(Act_T
) then
12234 ("dimensions of actual do not match formal &", Actual
, Gen_T
);
12235 Abandon_Instantiation
(Actual
);
12238 I1
:= First_Index
(A_Gen_T
);
12239 I2
:= First_Index
(Act_T
);
12240 for J
in 1 .. Formal_Dimensions
loop
12242 -- If the indexes of the actual were given by a subtype_mark,
12243 -- the index was transformed into a range attribute. Retrieve
12244 -- the original type mark for checking.
12246 if Is_Entity_Name
(Original_Node
(I2
)) then
12247 T2
:= Entity
(Original_Node
(I2
));
12252 if not Subtypes_Match
12253 (Find_Actual_Type
(Etype
(I1
), A_Gen_T
), T2
)
12256 ("index types of actual do not match those of formal &",
12258 Abandon_Instantiation
(Actual
);
12265 -- Check matching subtypes. Note that there are complex visibility
12266 -- issues when the generic is a child unit and some aspect of the
12267 -- generic type is declared in a parent unit of the generic. We do
12268 -- the test to handle this special case only after a direct check
12269 -- for static matching has failed. The case where both the component
12270 -- type and the array type are separate formals, and the component
12271 -- type is a private view may also require special checking in
12272 -- Subtypes_Match. Finally, we assume that a child instance where
12273 -- the component type comes from a formal of a parent instance is
12274 -- correct because the generic was correct. A more precise check
12275 -- seems too complex to install???
12278 (Component_Type
(A_Gen_T
), Component_Type
(Act_T
))
12281 (Find_Actual_Type
(Component_Type
(A_Gen_T
), A_Gen_T
),
12282 Component_Type
(Act_T
))
12284 (not Inside_A_Generic
12285 and then Is_Child_Unit
(Scope
(Component_Type
(A_Gen_T
))))
12290 ("component subtype of actual does not match that of formal &",
12292 Abandon_Instantiation
(Actual
);
12295 if Has_Aliased_Components
(A_Gen_T
)
12296 and then not Has_Aliased_Components
(Act_T
)
12299 ("actual must have aliased components to match formal type &",
12302 end Validate_Array_Type_Instance
;
12304 -----------------------------------------------
12305 -- Validate_Derived_Interface_Type_Instance --
12306 -----------------------------------------------
12308 procedure Validate_Derived_Interface_Type_Instance
is
12309 Par
: constant Entity_Id
:= Entity
(Subtype_Indication
(Def
));
12313 -- First apply interface instance checks
12315 Validate_Interface_Type_Instance
;
12317 -- Verify that immediate parent interface is an ancestor of
12321 and then not Interface_Present_In_Ancestor
(Act_T
, Par
)
12324 ("interface actual must include progenitor&", Actual
, Par
);
12327 -- Now verify that the actual includes all other ancestors of
12330 Elmt
:= First_Elmt
(Interfaces
(A_Gen_T
));
12331 while Present
(Elmt
) loop
12332 if not Interface_Present_In_Ancestor
12333 (Act_T
, Get_Instance_Of
(Node
(Elmt
)))
12336 ("interface actual must include progenitor&",
12337 Actual
, Node
(Elmt
));
12342 end Validate_Derived_Interface_Type_Instance
;
12344 ------------------------------------
12345 -- Validate_Derived_Type_Instance --
12346 ------------------------------------
12348 procedure Validate_Derived_Type_Instance
is
12349 Actual_Discr
: Entity_Id
;
12350 Ancestor_Discr
: Entity_Id
;
12353 -- Verify that the actual includes the progenitors of the formal,
12354 -- if any. The formal may depend on previous formals and their
12355 -- instance, so we must examine instance of interfaces if present.
12356 -- The actual may be an extension of an interface, in which case
12357 -- it does not appear in the interface list, so this must be
12358 -- checked separately.
12360 if Present
(Interface_List
(Def
)) then
12361 if not Has_Interfaces
(Act_T
) then
12363 ("actual must implement all interfaces of formal&",
12368 Act_Iface_List
: Elist_Id
;
12370 Iface_Ent
: Entity_Id
;
12372 function Instance_Exists
(I
: Entity_Id
) return Boolean;
12373 -- If the interface entity is declared in a generic unit,
12374 -- this can only be legal if we are within an instantiation
12375 -- of a child of that generic. There is currently no
12376 -- mechanism to relate an interface declared within a
12377 -- generic to the corresponding interface in an instance,
12378 -- so we traverse the list of interfaces of the actual,
12379 -- looking for a name match.
12381 ---------------------
12382 -- Instance_Exists --
12383 ---------------------
12385 function Instance_Exists
(I
: Entity_Id
) return Boolean is
12386 Iface_Elmt
: Elmt_Id
;
12389 Iface_Elmt
:= First_Elmt
(Act_Iface_List
);
12390 while Present
(Iface_Elmt
) loop
12391 if Is_Generic_Instance
(Scope
(Node
(Iface_Elmt
)))
12392 and then Chars
(Node
(Iface_Elmt
)) = Chars
(I
)
12397 Next_Elmt
(Iface_Elmt
);
12401 end Instance_Exists
;
12404 Iface
:= First
(Abstract_Interface_List
(A_Gen_T
));
12405 Collect_Interfaces
(Act_T
, Act_Iface_List
);
12407 while Present
(Iface
) loop
12408 Iface_Ent
:= Get_Instance_Of
(Entity
(Iface
));
12410 if Is_Ancestor
(Iface_Ent
, Act_T
)
12411 or else Is_Progenitor
(Iface_Ent
, Act_T
)
12415 elsif Ekind
(Scope
(Iface_Ent
)) = E_Generic_Package
12416 and then Instance_Exists
(Iface_Ent
)
12421 Error_Msg_Name_1
:= Chars
(Act_T
);
12423 ("Actual% must implement interface&",
12424 Actual
, Etype
(Iface
));
12433 -- If the parent type in the generic declaration is itself a previous
12434 -- formal type, then it is local to the generic and absent from the
12435 -- analyzed generic definition. In that case the ancestor is the
12436 -- instance of the formal (which must have been instantiated
12437 -- previously), unless the ancestor is itself a formal derived type.
12438 -- In this latter case (which is the subject of Corrigendum 8652/0038
12439 -- (AI-202) the ancestor of the formals is the ancestor of its
12440 -- parent. Otherwise, the analyzed generic carries the parent type.
12441 -- If the parent type is defined in a previous formal package, then
12442 -- the scope of that formal package is that of the generic type
12443 -- itself, and it has already been mapped into the corresponding type
12444 -- in the actual package.
12446 -- Common case: parent type defined outside of the generic
12448 if Is_Entity_Name
(Subtype_Mark
(Def
))
12449 and then Present
(Entity
(Subtype_Mark
(Def
)))
12451 Ancestor
:= Get_Instance_Of
(Entity
(Subtype_Mark
(Def
)));
12453 -- Check whether parent is defined in a previous formal package
12456 Scope
(Scope
(Base_Type
(Etype
(A_Gen_T
)))) = Scope
(A_Gen_T
)
12459 Get_Instance_Of
(Base_Type
(Etype
(A_Gen_T
)));
12461 -- The type may be a local derivation, or a type extension of a
12462 -- previous formal, or of a formal of a parent package.
12464 elsif Is_Derived_Type
(Get_Instance_Of
(A_Gen_T
))
12466 Ekind
(Get_Instance_Of
(A_Gen_T
)) = E_Record_Type_With_Private
12468 -- Check whether the parent is another derived formal type in the
12469 -- same generic unit.
12471 if Etype
(A_Gen_T
) /= A_Gen_T
12472 and then Is_Generic_Type
(Etype
(A_Gen_T
))
12473 and then Scope
(Etype
(A_Gen_T
)) = Scope
(A_Gen_T
)
12474 and then Etype
(Etype
(A_Gen_T
)) /= Etype
(A_Gen_T
)
12476 -- Locate ancestor of parent from the subtype declaration
12477 -- created for the actual.
12483 Decl
:= First
(Actual_Decls
);
12484 while Present
(Decl
) loop
12485 if Nkind
(Decl
) = N_Subtype_Declaration
12486 and then Chars
(Defining_Identifier
(Decl
)) =
12487 Chars
(Etype
(A_Gen_T
))
12489 Ancestor
:= Generic_Parent_Type
(Decl
);
12497 pragma Assert
(Present
(Ancestor
));
12499 -- The ancestor itself may be a previous formal that has been
12502 Ancestor
:= Get_Instance_Of
(Ancestor
);
12506 Get_Instance_Of
(Base_Type
(Get_Instance_Of
(A_Gen_T
)));
12509 -- Check whether parent is a previous formal of the current generic
12511 elsif Is_Derived_Type
(A_Gen_T
)
12512 and then Is_Generic_Type
(Etype
(A_Gen_T
))
12513 and then Scope
(A_Gen_T
) = Scope
(Etype
(A_Gen_T
))
12515 Ancestor
:= Get_Instance_Of
(First_Subtype
(Etype
(A_Gen_T
)));
12517 -- An unusual case: the actual is a type declared in a parent unit,
12518 -- but is not a formal type so there is no instance_of for it.
12519 -- Retrieve it by analyzing the record extension.
12521 elsif Is_Child_Unit
(Scope
(A_Gen_T
))
12522 and then In_Open_Scopes
(Scope
(Act_T
))
12523 and then Is_Generic_Instance
(Scope
(Act_T
))
12525 Analyze
(Subtype_Mark
(Def
));
12526 Ancestor
:= Entity
(Subtype_Mark
(Def
));
12529 Ancestor
:= Get_Instance_Of
(Etype
(Base_Type
(A_Gen_T
)));
12532 -- If the formal derived type has pragma Preelaborable_Initialization
12533 -- then the actual type must have preelaborable initialization.
12535 if Known_To_Have_Preelab_Init
(A_Gen_T
)
12536 and then not Has_Preelaborable_Initialization
(Act_T
)
12539 ("actual for & must have preelaborable initialization",
12543 -- Ada 2005 (AI-251)
12545 if Ada_Version
>= Ada_2005
and then Is_Interface
(Ancestor
) then
12546 if not Interface_Present_In_Ancestor
(Act_T
, Ancestor
) then
12548 ("(Ada 2005) expected type implementing & in instantiation",
12552 -- Finally verify that the (instance of) the ancestor is an ancestor
12555 elsif not Is_Ancestor
(Base_Type
(Ancestor
), Act_T
) then
12557 ("expect type derived from & in instantiation",
12558 Actual
, First_Subtype
(Ancestor
));
12559 Abandon_Instantiation
(Actual
);
12562 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
12563 -- that the formal type declaration has been rewritten as a private
12566 if Ada_Version
>= Ada_2005
12567 and then Nkind
(Parent
(A_Gen_T
)) = N_Private_Extension_Declaration
12568 and then Synchronized_Present
(Parent
(A_Gen_T
))
12570 -- The actual must be a synchronized tagged type
12572 if not Is_Tagged_Type
(Act_T
) then
12574 ("actual of synchronized type must be tagged", Actual
);
12575 Abandon_Instantiation
(Actual
);
12577 elsif Nkind
(Parent
(Act_T
)) = N_Full_Type_Declaration
12578 and then Nkind
(Type_Definition
(Parent
(Act_T
))) =
12579 N_Derived_Type_Definition
12580 and then not Synchronized_Present
12581 (Type_Definition
(Parent
(Act_T
)))
12584 ("actual of synchronized type must be synchronized", Actual
);
12585 Abandon_Instantiation
(Actual
);
12589 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
12590 -- removes the second instance of the phrase "or allow pass by copy".
12592 if Is_Atomic
(Act_T
) and then not Is_Atomic
(Ancestor
) then
12594 ("cannot have atomic actual type for non-atomic formal type",
12597 elsif Is_Volatile
(Act_T
) and then not Is_Volatile
(Ancestor
) then
12599 ("cannot have volatile actual type for non-volatile formal type",
12603 -- It should not be necessary to check for unknown discriminants on
12604 -- Formal, but for some reason Has_Unknown_Discriminants is false for
12605 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
12606 -- needs fixing. ???
12608 if Is_Definite_Subtype
(A_Gen_T
)
12609 and then not Unknown_Discriminants_Present
(Formal
)
12610 and then not Is_Definite_Subtype
(Act_T
)
12612 Error_Msg_N
("actual subtype must be constrained", Actual
);
12613 Abandon_Instantiation
(Actual
);
12616 if not Unknown_Discriminants_Present
(Formal
) then
12617 if Is_Constrained
(Ancestor
) then
12618 if not Is_Constrained
(Act_T
) then
12619 Error_Msg_N
("actual subtype must be constrained", Actual
);
12620 Abandon_Instantiation
(Actual
);
12623 -- Ancestor is unconstrained, Check if generic formal and actual
12624 -- agree on constrainedness. The check only applies to array types
12625 -- and discriminated types.
12627 elsif Is_Constrained
(Act_T
) then
12628 if Ekind
(Ancestor
) = E_Access_Type
12629 or else (not Is_Constrained
(A_Gen_T
)
12630 and then Is_Composite_Type
(A_Gen_T
))
12632 Error_Msg_N
("actual subtype must be unconstrained", Actual
);
12633 Abandon_Instantiation
(Actual
);
12636 -- A class-wide type is only allowed if the formal has unknown
12639 elsif Is_Class_Wide_Type
(Act_T
)
12640 and then not Has_Unknown_Discriminants
(Ancestor
)
12643 ("actual for & cannot be a class-wide type", Actual
, Gen_T
);
12644 Abandon_Instantiation
(Actual
);
12646 -- Otherwise, the formal and actual must have the same number
12647 -- of discriminants and each discriminant of the actual must
12648 -- correspond to a discriminant of the formal.
12650 elsif Has_Discriminants
(Act_T
)
12651 and then not Has_Unknown_Discriminants
(Act_T
)
12652 and then Has_Discriminants
(Ancestor
)
12654 Actual_Discr
:= First_Discriminant
(Act_T
);
12655 Ancestor_Discr
:= First_Discriminant
(Ancestor
);
12656 while Present
(Actual_Discr
)
12657 and then Present
(Ancestor_Discr
)
12659 if Base_Type
(Act_T
) /= Base_Type
(Ancestor
) and then
12660 No
(Corresponding_Discriminant
(Actual_Discr
))
12663 ("discriminant & does not correspond "
12664 & "to ancestor discriminant", Actual
, Actual_Discr
);
12665 Abandon_Instantiation
(Actual
);
12668 Next_Discriminant
(Actual_Discr
);
12669 Next_Discriminant
(Ancestor_Discr
);
12672 if Present
(Actual_Discr
) or else Present
(Ancestor_Discr
) then
12674 ("actual for & must have same number of discriminants",
12676 Abandon_Instantiation
(Actual
);
12679 -- This case should be caught by the earlier check for
12680 -- constrainedness, but the check here is added for completeness.
12682 elsif Has_Discriminants
(Act_T
)
12683 and then not Has_Unknown_Discriminants
(Act_T
)
12686 ("actual for & must not have discriminants", Actual
, Gen_T
);
12687 Abandon_Instantiation
(Actual
);
12689 elsif Has_Discriminants
(Ancestor
) then
12691 ("actual for & must have known discriminants", Actual
, Gen_T
);
12692 Abandon_Instantiation
(Actual
);
12695 if not Subtypes_Statically_Compatible
12696 (Act_T
, Ancestor
, Formal_Derived_Matching
=> True)
12699 ("constraint on actual is incompatible with formal", Actual
);
12700 Abandon_Instantiation
(Actual
);
12704 -- If the formal and actual types are abstract, check that there
12705 -- are no abstract primitives of the actual type that correspond to
12706 -- nonabstract primitives of the formal type (second sentence of
12709 if Is_Abstract_Type
(A_Gen_T
) and then Is_Abstract_Type
(Act_T
) then
12710 Check_Abstract_Primitives
: declare
12711 Gen_Prims
: constant Elist_Id
:=
12712 Primitive_Operations
(A_Gen_T
);
12713 Gen_Elmt
: Elmt_Id
;
12714 Gen_Subp
: Entity_Id
;
12715 Anc_Subp
: Entity_Id
;
12716 Anc_Formal
: Entity_Id
;
12717 Anc_F_Type
: Entity_Id
;
12719 Act_Prims
: constant Elist_Id
:= Primitive_Operations
(Act_T
);
12720 Act_Elmt
: Elmt_Id
;
12721 Act_Subp
: Entity_Id
;
12722 Act_Formal
: Entity_Id
;
12723 Act_F_Type
: Entity_Id
;
12725 Subprograms_Correspond
: Boolean;
12727 function Is_Tagged_Ancestor
(T1
, T2
: Entity_Id
) return Boolean;
12728 -- Returns true if T2 is derived directly or indirectly from
12729 -- T1, including derivations from interfaces. T1 and T2 are
12730 -- required to be specific tagged base types.
12732 ------------------------
12733 -- Is_Tagged_Ancestor --
12734 ------------------------
12736 function Is_Tagged_Ancestor
(T1
, T2
: Entity_Id
) return Boolean
12738 Intfc_Elmt
: Elmt_Id
;
12741 -- The predicate is satisfied if the types are the same
12746 -- If we've reached the top of the derivation chain then
12747 -- we know that T1 is not an ancestor of T2.
12749 elsif Etype
(T2
) = T2
then
12752 -- Proceed to check T2's immediate parent
12754 elsif Is_Ancestor
(T1
, Base_Type
(Etype
(T2
))) then
12757 -- Finally, check to see if T1 is an ancestor of any of T2's
12761 Intfc_Elmt
:= First_Elmt
(Interfaces
(T2
));
12762 while Present
(Intfc_Elmt
) loop
12763 if Is_Ancestor
(T1
, Node
(Intfc_Elmt
)) then
12767 Next_Elmt
(Intfc_Elmt
);
12772 end Is_Tagged_Ancestor
;
12774 -- Start of processing for Check_Abstract_Primitives
12777 -- Loop over all of the formal derived type's primitives
12779 Gen_Elmt
:= First_Elmt
(Gen_Prims
);
12780 while Present
(Gen_Elmt
) loop
12781 Gen_Subp
:= Node
(Gen_Elmt
);
12783 -- If the primitive of the formal is not abstract, then
12784 -- determine whether there is a corresponding primitive of
12785 -- the actual type that's abstract.
12787 if not Is_Abstract_Subprogram
(Gen_Subp
) then
12788 Act_Elmt
:= First_Elmt
(Act_Prims
);
12789 while Present
(Act_Elmt
) loop
12790 Act_Subp
:= Node
(Act_Elmt
);
12792 -- If we find an abstract primitive of the actual,
12793 -- then we need to test whether it corresponds to the
12794 -- subprogram from which the generic formal primitive
12797 if Is_Abstract_Subprogram
(Act_Subp
) then
12798 Anc_Subp
:= Alias
(Gen_Subp
);
12800 -- Test whether we have a corresponding primitive
12801 -- by comparing names, kinds, formal types, and
12804 if Chars
(Anc_Subp
) = Chars
(Act_Subp
)
12805 and then Ekind
(Anc_Subp
) = Ekind
(Act_Subp
)
12807 Anc_Formal
:= First_Formal
(Anc_Subp
);
12808 Act_Formal
:= First_Formal
(Act_Subp
);
12809 while Present
(Anc_Formal
)
12810 and then Present
(Act_Formal
)
12812 Anc_F_Type
:= Etype
(Anc_Formal
);
12813 Act_F_Type
:= Etype
(Act_Formal
);
12815 if Ekind
(Anc_F_Type
) =
12816 E_Anonymous_Access_Type
12818 Anc_F_Type
:= Designated_Type
(Anc_F_Type
);
12820 if Ekind
(Act_F_Type
) =
12821 E_Anonymous_Access_Type
12824 Designated_Type
(Act_F_Type
);
12830 Ekind
(Act_F_Type
) = E_Anonymous_Access_Type
12835 Anc_F_Type
:= Base_Type
(Anc_F_Type
);
12836 Act_F_Type
:= Base_Type
(Act_F_Type
);
12838 -- If the formal is controlling, then the
12839 -- the type of the actual primitive's formal
12840 -- must be derived directly or indirectly
12841 -- from the type of the ancestor primitive's
12844 if Is_Controlling_Formal
(Anc_Formal
) then
12845 if not Is_Tagged_Ancestor
12846 (Anc_F_Type
, Act_F_Type
)
12851 -- Otherwise the types of the formals must
12854 elsif Anc_F_Type
/= Act_F_Type
then
12858 Next_Entity
(Anc_Formal
);
12859 Next_Entity
(Act_Formal
);
12862 -- If we traversed through all of the formals
12863 -- then so far the subprograms correspond, so
12864 -- now check that any result types correspond.
12866 if No
(Anc_Formal
) and then No
(Act_Formal
) then
12867 Subprograms_Correspond
:= True;
12869 if Ekind
(Act_Subp
) = E_Function
then
12870 Anc_F_Type
:= Etype
(Anc_Subp
);
12871 Act_F_Type
:= Etype
(Act_Subp
);
12873 if Ekind
(Anc_F_Type
) =
12874 E_Anonymous_Access_Type
12877 Designated_Type
(Anc_F_Type
);
12879 if Ekind
(Act_F_Type
) =
12880 E_Anonymous_Access_Type
12883 Designated_Type
(Act_F_Type
);
12885 Subprograms_Correspond
:= False;
12890 = E_Anonymous_Access_Type
12892 Subprograms_Correspond
:= False;
12895 Anc_F_Type
:= Base_Type
(Anc_F_Type
);
12896 Act_F_Type
:= Base_Type
(Act_F_Type
);
12898 -- Now either the result types must be
12899 -- the same or, if the result type is
12900 -- controlling, the result type of the
12901 -- actual primitive must descend from the
12902 -- result type of the ancestor primitive.
12904 if Subprograms_Correspond
12905 and then Anc_F_Type
/= Act_F_Type
12907 Has_Controlling_Result
(Anc_Subp
)
12908 and then not Is_Tagged_Ancestor
12909 (Anc_F_Type
, Act_F_Type
)
12911 Subprograms_Correspond
:= False;
12915 -- Found a matching subprogram belonging to
12916 -- formal ancestor type, so actual subprogram
12917 -- corresponds and this violates 3.9.3(9).
12919 if Subprograms_Correspond
then
12921 ("abstract subprogram & overrides "
12922 & "nonabstract subprogram of ancestor",
12929 Next_Elmt
(Act_Elmt
);
12933 Next_Elmt
(Gen_Elmt
);
12935 end Check_Abstract_Primitives
;
12938 -- Verify that limitedness matches. If parent is a limited
12939 -- interface then the generic formal is not unless declared
12940 -- explicitly so. If not declared limited, the actual cannot be
12941 -- limited (see AI05-0087).
12943 -- Even though this AI is a binding interpretation, we enable the
12944 -- check only in Ada 2012 mode, because this improper construct
12945 -- shows up in user code and in existing B-tests.
12947 if Is_Limited_Type
(Act_T
)
12948 and then not Is_Limited_Type
(A_Gen_T
)
12949 and then Ada_Version
>= Ada_2012
12951 if In_Instance
then
12955 ("actual for non-limited & cannot be a limited type",
12957 Explain_Limited_Type
(Act_T
, Actual
);
12958 Abandon_Instantiation
(Actual
);
12961 end Validate_Derived_Type_Instance
;
12963 ----------------------------------------
12964 -- Validate_Discriminated_Formal_Type --
12965 ----------------------------------------
12967 procedure Validate_Discriminated_Formal_Type
is
12968 Formal_Discr
: Entity_Id
;
12969 Actual_Discr
: Entity_Id
;
12970 Formal_Subt
: Entity_Id
;
12973 if Has_Discriminants
(A_Gen_T
) then
12974 if not Has_Discriminants
(Act_T
) then
12976 ("actual for & must have discriminants", Actual
, Gen_T
);
12977 Abandon_Instantiation
(Actual
);
12979 elsif Is_Constrained
(Act_T
) then
12981 ("actual for & must be unconstrained", Actual
, Gen_T
);
12982 Abandon_Instantiation
(Actual
);
12985 Formal_Discr
:= First_Discriminant
(A_Gen_T
);
12986 Actual_Discr
:= First_Discriminant
(Act_T
);
12987 while Formal_Discr
/= Empty
loop
12988 if Actual_Discr
= Empty
then
12990 ("discriminants on actual do not match formal",
12992 Abandon_Instantiation
(Actual
);
12995 Formal_Subt
:= Get_Instance_Of
(Etype
(Formal_Discr
));
12997 -- Access discriminants match if designated types do
12999 if Ekind
(Base_Type
(Formal_Subt
)) = E_Anonymous_Access_Type
13000 and then (Ekind
(Base_Type
(Etype
(Actual_Discr
)))) =
13001 E_Anonymous_Access_Type
13004 (Designated_Type
(Base_Type
(Formal_Subt
))) =
13005 Designated_Type
(Base_Type
(Etype
(Actual_Discr
)))
13009 elsif Base_Type
(Formal_Subt
) /=
13010 Base_Type
(Etype
(Actual_Discr
))
13013 ("types of actual discriminants must match formal",
13015 Abandon_Instantiation
(Actual
);
13017 elsif not Subtypes_Statically_Match
13018 (Formal_Subt
, Etype
(Actual_Discr
))
13019 and then Ada_Version
>= Ada_95
13022 ("subtypes of actual discriminants must match formal",
13024 Abandon_Instantiation
(Actual
);
13027 Next_Discriminant
(Formal_Discr
);
13028 Next_Discriminant
(Actual_Discr
);
13031 if Actual_Discr
/= Empty
then
13033 ("discriminants on actual do not match formal",
13035 Abandon_Instantiation
(Actual
);
13039 end Validate_Discriminated_Formal_Type
;
13041 ---------------------------------------
13042 -- Validate_Incomplete_Type_Instance --
13043 ---------------------------------------
13045 procedure Validate_Incomplete_Type_Instance
is
13047 if not Is_Tagged_Type
(Act_T
)
13048 and then Is_Tagged_Type
(A_Gen_T
)
13051 ("actual for & must be a tagged type", Actual
, Gen_T
);
13054 Validate_Discriminated_Formal_Type
;
13055 end Validate_Incomplete_Type_Instance
;
13057 --------------------------------------
13058 -- Validate_Interface_Type_Instance --
13059 --------------------------------------
13061 procedure Validate_Interface_Type_Instance
is
13063 if not Is_Interface
(Act_T
) then
13065 ("actual for formal interface type must be an interface",
13068 elsif Is_Limited_Type
(Act_T
) /= Is_Limited_Type
(A_Gen_T
)
13069 or else Is_Task_Interface
(A_Gen_T
) /= Is_Task_Interface
(Act_T
)
13070 or else Is_Protected_Interface
(A_Gen_T
) /=
13071 Is_Protected_Interface
(Act_T
)
13072 or else Is_Synchronized_Interface
(A_Gen_T
) /=
13073 Is_Synchronized_Interface
(Act_T
)
13076 ("actual for interface& does not match (RM 12.5.5(4))",
13079 end Validate_Interface_Type_Instance
;
13081 ------------------------------------
13082 -- Validate_Private_Type_Instance --
13083 ------------------------------------
13085 procedure Validate_Private_Type_Instance
is
13087 if Is_Limited_Type
(Act_T
)
13088 and then not Is_Limited_Type
(A_Gen_T
)
13090 if In_Instance
then
13094 ("actual for non-limited & cannot be a limited type", Actual
,
13096 Explain_Limited_Type
(Act_T
, Actual
);
13097 Abandon_Instantiation
(Actual
);
13100 elsif Known_To_Have_Preelab_Init
(A_Gen_T
)
13101 and then not Has_Preelaborable_Initialization
(Act_T
)
13104 ("actual for & must have preelaborable initialization", Actual
,
13107 elsif not Is_Definite_Subtype
(Act_T
)
13108 and then Is_Definite_Subtype
(A_Gen_T
)
13109 and then Ada_Version
>= Ada_95
13112 ("actual for & must be a definite subtype", Actual
, Gen_T
);
13114 elsif not Is_Tagged_Type
(Act_T
)
13115 and then Is_Tagged_Type
(A_Gen_T
)
13118 ("actual for & must be a tagged type", Actual
, Gen_T
);
13121 Validate_Discriminated_Formal_Type
;
13123 end Validate_Private_Type_Instance
;
13125 -- Start of processing for Instantiate_Type
13128 if Get_Instance_Of
(A_Gen_T
) /= A_Gen_T
then
13129 Error_Msg_N
("duplicate instantiation of generic type", Actual
);
13130 return New_List
(Error
);
13132 elsif not Is_Entity_Name
(Actual
)
13133 or else not Is_Type
(Entity
(Actual
))
13136 ("expect valid subtype mark to instantiate &", Actual
, Gen_T
);
13137 Abandon_Instantiation
(Actual
);
13140 Act_T
:= Entity
(Actual
);
13142 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
13143 -- as a generic actual parameter if the corresponding formal type
13144 -- does not have a known_discriminant_part, or is a formal derived
13145 -- type that is an Unchecked_Union type.
13147 if Is_Unchecked_Union
(Base_Type
(Act_T
)) then
13148 if not Has_Discriminants
(A_Gen_T
)
13149 or else (Is_Derived_Type
(A_Gen_T
)
13150 and then Is_Unchecked_Union
(A_Gen_T
))
13154 Error_Msg_N
("unchecked union cannot be the actual for a "
13155 & "discriminated formal type", Act_T
);
13160 -- Deal with fixed/floating restrictions
13162 if Is_Floating_Point_Type
(Act_T
) then
13163 Check_Restriction
(No_Floating_Point
, Actual
);
13164 elsif Is_Fixed_Point_Type
(Act_T
) then
13165 Check_Restriction
(No_Fixed_Point
, Actual
);
13168 -- Deal with error of using incomplete type as generic actual.
13169 -- This includes limited views of a type, even if the non-limited
13170 -- view may be available.
13172 if Ekind
(Act_T
) = E_Incomplete_Type
13173 or else (Is_Class_Wide_Type
(Act_T
)
13174 and then Ekind
(Root_Type
(Act_T
)) = E_Incomplete_Type
)
13176 -- If the formal is an incomplete type, the actual can be
13177 -- incomplete as well.
13179 if Ekind
(A_Gen_T
) = E_Incomplete_Type
then
13182 elsif Is_Class_Wide_Type
(Act_T
)
13183 or else No
(Full_View
(Act_T
))
13185 Error_Msg_N
("premature use of incomplete type", Actual
);
13186 Abandon_Instantiation
(Actual
);
13188 Act_T
:= Full_View
(Act_T
);
13189 Set_Entity
(Actual
, Act_T
);
13191 if Has_Private_Component
(Act_T
) then
13193 ("premature use of type with private component", Actual
);
13197 -- Deal with error of premature use of private type as generic actual
13199 elsif Is_Private_Type
(Act_T
)
13200 and then Is_Private_Type
(Base_Type
(Act_T
))
13201 and then not Is_Generic_Type
(Act_T
)
13202 and then not Is_Derived_Type
(Act_T
)
13203 and then No
(Full_View
(Root_Type
(Act_T
)))
13205 -- If the formal is an incomplete type, the actual can be
13206 -- private or incomplete as well.
13208 if Ekind
(A_Gen_T
) = E_Incomplete_Type
then
13211 Error_Msg_N
("premature use of private type", Actual
);
13214 elsif Has_Private_Component
(Act_T
) then
13216 ("premature use of type with private component", Actual
);
13219 Set_Instance_Of
(A_Gen_T
, Act_T
);
13221 -- If the type is generic, the class-wide type may also be used
13223 if Is_Tagged_Type
(A_Gen_T
)
13224 and then Is_Tagged_Type
(Act_T
)
13225 and then not Is_Class_Wide_Type
(A_Gen_T
)
13227 Set_Instance_Of
(Class_Wide_Type
(A_Gen_T
),
13228 Class_Wide_Type
(Act_T
));
13231 if not Is_Abstract_Type
(A_Gen_T
)
13232 and then Is_Abstract_Type
(Act_T
)
13235 ("actual of non-abstract formal cannot be abstract", Actual
);
13238 -- A generic scalar type is a first subtype for which we generate
13239 -- an anonymous base type. Indicate that the instance of this base
13240 -- is the base type of the actual.
13242 if Is_Scalar_Type
(A_Gen_T
) then
13243 Set_Instance_Of
(Etype
(A_Gen_T
), Etype
(Act_T
));
13247 if Error_Posted
(Act_T
) then
13250 case Nkind
(Def
) is
13251 when N_Formal_Private_Type_Definition
=>
13252 Validate_Private_Type_Instance
;
13254 when N_Formal_Incomplete_Type_Definition
=>
13255 Validate_Incomplete_Type_Instance
;
13257 when N_Formal_Derived_Type_Definition
=>
13258 Validate_Derived_Type_Instance
;
13260 when N_Formal_Discrete_Type_Definition
=>
13261 if not Is_Discrete_Type
(Act_T
) then
13263 ("expect discrete type in instantiation of&",
13265 Abandon_Instantiation
(Actual
);
13268 Diagnose_Predicated_Actual
;
13270 when N_Formal_Signed_Integer_Type_Definition
=>
13271 if not Is_Signed_Integer_Type
(Act_T
) then
13273 ("expect signed integer type in instantiation of&",
13275 Abandon_Instantiation
(Actual
);
13278 Diagnose_Predicated_Actual
;
13280 when N_Formal_Modular_Type_Definition
=>
13281 if not Is_Modular_Integer_Type
(Act_T
) then
13283 ("expect modular type in instantiation of &",
13285 Abandon_Instantiation
(Actual
);
13288 Diagnose_Predicated_Actual
;
13290 when N_Formal_Floating_Point_Definition
=>
13291 if not Is_Floating_Point_Type
(Act_T
) then
13293 ("expect float type in instantiation of &", Actual
, Gen_T
);
13294 Abandon_Instantiation
(Actual
);
13297 when N_Formal_Ordinary_Fixed_Point_Definition
=>
13298 if not Is_Ordinary_Fixed_Point_Type
(Act_T
) then
13300 ("expect ordinary fixed point type in instantiation of &",
13302 Abandon_Instantiation
(Actual
);
13305 when N_Formal_Decimal_Fixed_Point_Definition
=>
13306 if not Is_Decimal_Fixed_Point_Type
(Act_T
) then
13308 ("expect decimal type in instantiation of &",
13310 Abandon_Instantiation
(Actual
);
13313 when N_Array_Type_Definition
=>
13314 Validate_Array_Type_Instance
;
13316 when N_Access_To_Object_Definition
=>
13317 Validate_Access_Type_Instance
;
13319 when N_Access_Function_Definition
13320 | N_Access_Procedure_Definition
13322 Validate_Access_Subprogram_Instance
;
13324 when N_Record_Definition
=>
13325 Validate_Interface_Type_Instance
;
13327 when N_Derived_Type_Definition
=>
13328 Validate_Derived_Interface_Type_Instance
;
13331 raise Program_Error
;
13335 Subt
:= New_Copy
(Gen_T
);
13337 -- Use adjusted sloc of subtype name as the location for other nodes in
13338 -- the subtype declaration.
13340 Loc
:= Sloc
(Subt
);
13343 Make_Subtype_Declaration
(Loc
,
13344 Defining_Identifier
=> Subt
,
13345 Subtype_Indication
=> New_Occurrence_Of
(Act_T
, Loc
));
13347 if Is_Private_Type
(Act_T
) then
13348 Set_Has_Private_View
(Subtype_Indication
(Decl_Node
));
13350 elsif Is_Access_Type
(Act_T
)
13351 and then Is_Private_Type
(Designated_Type
(Act_T
))
13353 Set_Has_Private_View
(Subtype_Indication
(Decl_Node
));
13356 -- In Ada 2012 the actual may be a limited view. Indicate that
13357 -- the local subtype must be treated as such.
13359 if From_Limited_With
(Act_T
) then
13360 Set_Ekind
(Subt
, E_Incomplete_Subtype
);
13361 Set_From_Limited_With
(Subt
);
13364 Decl_Nodes
:= New_List
(Decl_Node
);
13366 -- Flag actual derived types so their elaboration produces the
13367 -- appropriate renamings for the primitive operations of the ancestor.
13368 -- Flag actual for formal private types as well, to determine whether
13369 -- operations in the private part may override inherited operations.
13370 -- If the formal has an interface list, the ancestor is not the
13371 -- parent, but the analyzed formal that includes the interface
13372 -- operations of all its progenitors.
13374 -- Same treatment for formal private types, so we can check whether the
13375 -- type is tagged limited when validating derivations in the private
13376 -- part. (See AI05-096).
13378 if Nkind
(Def
) = N_Formal_Derived_Type_Definition
then
13379 if Present
(Interface_List
(Def
)) then
13380 Set_Generic_Parent_Type
(Decl_Node
, A_Gen_T
);
13382 Set_Generic_Parent_Type
(Decl_Node
, Ancestor
);
13385 elsif Nkind_In
(Def
, N_Formal_Private_Type_Definition
,
13386 N_Formal_Incomplete_Type_Definition
)
13388 Set_Generic_Parent_Type
(Decl_Node
, A_Gen_T
);
13391 -- If the actual is a synchronized type that implements an interface,
13392 -- the primitive operations are attached to the corresponding record,
13393 -- and we have to treat it as an additional generic actual, so that its
13394 -- primitive operations become visible in the instance. The task or
13395 -- protected type itself does not carry primitive operations.
13397 if Is_Concurrent_Type
(Act_T
)
13398 and then Is_Tagged_Type
(Act_T
)
13399 and then Present
(Corresponding_Record_Type
(Act_T
))
13400 and then Present
(Ancestor
)
13401 and then Is_Interface
(Ancestor
)
13404 Corr_Rec
: constant Entity_Id
:=
13405 Corresponding_Record_Type
(Act_T
);
13406 New_Corr
: Entity_Id
;
13407 Corr_Decl
: Node_Id
;
13410 New_Corr
:= Make_Temporary
(Loc
, 'S');
13412 Make_Subtype_Declaration
(Loc
,
13413 Defining_Identifier
=> New_Corr
,
13414 Subtype_Indication
=>
13415 New_Occurrence_Of
(Corr_Rec
, Loc
));
13416 Append_To
(Decl_Nodes
, Corr_Decl
);
13418 if Ekind
(Act_T
) = E_Task_Type
then
13419 Set_Ekind
(Subt
, E_Task_Subtype
);
13421 Set_Ekind
(Subt
, E_Protected_Subtype
);
13424 Set_Corresponding_Record_Type
(Subt
, Corr_Rec
);
13425 Set_Generic_Parent_Type
(Corr_Decl
, Ancestor
);
13426 Set_Generic_Parent_Type
(Decl_Node
, Empty
);
13430 -- For a floating-point type, capture dimension info if any, because
13431 -- the generated subtype declaration does not come from source and
13432 -- will not process dimensions.
13434 if Is_Floating_Point_Type
(Act_T
) then
13435 Copy_Dimensions
(Act_T
, Subt
);
13439 end Instantiate_Type
;
13441 ---------------------
13442 -- Is_In_Main_Unit --
13443 ---------------------
13445 function Is_In_Main_Unit
(N
: Node_Id
) return Boolean is
13446 Unum
: constant Unit_Number_Type
:= Get_Source_Unit
(N
);
13447 Current_Unit
: Node_Id
;
13450 if Unum
= Main_Unit
then
13453 -- If the current unit is a subunit then it is either the main unit or
13454 -- is being compiled as part of the main unit.
13456 elsif Nkind
(N
) = N_Compilation_Unit
then
13457 return Nkind
(Unit
(N
)) = N_Subunit
;
13460 Current_Unit
:= Parent
(N
);
13461 while Present
(Current_Unit
)
13462 and then Nkind
(Current_Unit
) /= N_Compilation_Unit
13464 Current_Unit
:= Parent
(Current_Unit
);
13467 -- The instantiation node is in the main unit, or else the current node
13468 -- (perhaps as the result of nested instantiations) is in the main unit,
13469 -- or in the declaration of the main unit, which in this last case must
13473 Current_Unit
= Cunit
(Main_Unit
)
13474 or else Current_Unit
= Library_Unit
(Cunit
(Main_Unit
))
13475 or else (Present
(Current_Unit
)
13476 and then Present
(Library_Unit
(Current_Unit
))
13477 and then Is_In_Main_Unit
(Library_Unit
(Current_Unit
)));
13478 end Is_In_Main_Unit
;
13480 ----------------------------
13481 -- Load_Parent_Of_Generic --
13482 ----------------------------
13484 procedure Load_Parent_Of_Generic
13487 Body_Optional
: Boolean := False)
13489 Comp_Unit
: constant Node_Id
:= Cunit
(Get_Source_Unit
(Spec
));
13490 Saved_Style_Check
: constant Boolean := Style_Check
;
13491 Saved_Warnings
: constant Warning_Record
:= Save_Warnings
;
13492 True_Parent
: Node_Id
;
13493 Inst_Node
: Node_Id
;
13495 Previous_Instances
: constant Elist_Id
:= New_Elmt_List
;
13497 procedure Collect_Previous_Instances
(Decls
: List_Id
);
13498 -- Collect all instantiations in the given list of declarations, that
13499 -- precede the generic that we need to load. If the bodies of these
13500 -- instantiations are available, we must analyze them, to ensure that
13501 -- the public symbols generated are the same when the unit is compiled
13502 -- to generate code, and when it is compiled in the context of a unit
13503 -- that needs a particular nested instance. This process is applied to
13504 -- both package and subprogram instances.
13506 --------------------------------
13507 -- Collect_Previous_Instances --
13508 --------------------------------
13510 procedure Collect_Previous_Instances
(Decls
: List_Id
) is
13514 Decl
:= First
(Decls
);
13515 while Present
(Decl
) loop
13516 if Sloc
(Decl
) >= Sloc
(Inst_Node
) then
13519 -- If Decl is an instantiation, then record it as requiring
13520 -- instantiation of the corresponding body, except if it is an
13521 -- abbreviated instantiation generated internally for conformance
13522 -- checking purposes only for the case of a formal package
13523 -- declared without a box (see Instantiate_Formal_Package). Such
13524 -- an instantiation does not generate any code (the actual code
13525 -- comes from actual) and thus does not need to be analyzed here.
13526 -- If the instantiation appears with a generic package body it is
13527 -- not analyzed here either.
13529 elsif Nkind
(Decl
) = N_Package_Instantiation
13530 and then not Is_Internal
(Defining_Entity
(Decl
))
13532 Append_Elmt
(Decl
, Previous_Instances
);
13534 -- For a subprogram instantiation, omit instantiations intrinsic
13535 -- operations (Unchecked_Conversions, etc.) that have no bodies.
13537 elsif Nkind_In
(Decl
, N_Function_Instantiation
,
13538 N_Procedure_Instantiation
)
13539 and then not Is_Intrinsic_Subprogram
(Entity
(Name
(Decl
)))
13541 Append_Elmt
(Decl
, Previous_Instances
);
13543 elsif Nkind
(Decl
) = N_Package_Declaration
then
13544 Collect_Previous_Instances
13545 (Visible_Declarations
(Specification
(Decl
)));
13546 Collect_Previous_Instances
13547 (Private_Declarations
(Specification
(Decl
)));
13549 -- Previous non-generic bodies may contain instances as well
13551 elsif Nkind
(Decl
) = N_Package_Body
13552 and then Ekind
(Corresponding_Spec
(Decl
)) /= E_Generic_Package
13554 Collect_Previous_Instances
(Declarations
(Decl
));
13556 elsif Nkind
(Decl
) = N_Subprogram_Body
13557 and then not Acts_As_Spec
(Decl
)
13558 and then not Is_Generic_Subprogram
(Corresponding_Spec
(Decl
))
13560 Collect_Previous_Instances
(Declarations
(Decl
));
13565 end Collect_Previous_Instances
;
13567 -- Start of processing for Load_Parent_Of_Generic
13570 if not In_Same_Source_Unit
(N
, Spec
)
13571 or else Nkind
(Unit
(Comp_Unit
)) = N_Package_Declaration
13572 or else (Nkind
(Unit
(Comp_Unit
)) = N_Package_Body
13573 and then not Is_In_Main_Unit
(Spec
))
13575 -- Find body of parent of spec, and analyze it. A special case arises
13576 -- when the parent is an instantiation, that is to say when we are
13577 -- currently instantiating a nested generic. In that case, there is
13578 -- no separate file for the body of the enclosing instance. Instead,
13579 -- the enclosing body must be instantiated as if it were a pending
13580 -- instantiation, in order to produce the body for the nested generic
13581 -- we require now. Note that in that case the generic may be defined
13582 -- in a package body, the instance defined in the same package body,
13583 -- and the original enclosing body may not be in the main unit.
13585 Inst_Node
:= Empty
;
13587 True_Parent
:= Parent
(Spec
);
13588 while Present
(True_Parent
)
13589 and then Nkind
(True_Parent
) /= N_Compilation_Unit
13591 if Nkind
(True_Parent
) = N_Package_Declaration
13593 Nkind
(Original_Node
(True_Parent
)) = N_Package_Instantiation
13595 -- Parent is a compilation unit that is an instantiation.
13596 -- Instantiation node has been replaced with package decl.
13598 Inst_Node
:= Original_Node
(True_Parent
);
13601 elsif Nkind
(True_Parent
) = N_Package_Declaration
13602 and then Present
(Generic_Parent
(Specification
(True_Parent
)))
13603 and then Nkind
(Parent
(True_Parent
)) /= N_Compilation_Unit
13605 -- Parent is an instantiation within another specification.
13606 -- Declaration for instance has been inserted before original
13607 -- instantiation node. A direct link would be preferable?
13609 Inst_Node
:= Next
(True_Parent
);
13610 while Present
(Inst_Node
)
13611 and then Nkind
(Inst_Node
) /= N_Package_Instantiation
13616 -- If the instance appears within a generic, and the generic
13617 -- unit is defined within a formal package of the enclosing
13618 -- generic, there is no generic body available, and none
13619 -- needed. A more precise test should be used ???
13621 if No
(Inst_Node
) then
13628 True_Parent
:= Parent
(True_Parent
);
13632 -- Case where we are currently instantiating a nested generic
13634 if Present
(Inst_Node
) then
13635 if Nkind
(Parent
(True_Parent
)) = N_Compilation_Unit
then
13637 -- Instantiation node and declaration of instantiated package
13638 -- were exchanged when only the declaration was needed.
13639 -- Restore instantiation node before proceeding with body.
13641 Set_Unit
(Parent
(True_Parent
), Inst_Node
);
13644 -- Now complete instantiation of enclosing body, if it appears in
13645 -- some other unit. If it appears in the current unit, the body
13646 -- will have been instantiated already.
13648 if No
(Corresponding_Body
(Instance_Spec
(Inst_Node
))) then
13650 -- We need to determine the expander mode to instantiate the
13651 -- enclosing body. Because the generic body we need may use
13652 -- global entities declared in the enclosing package (including
13653 -- aggregates) it is in general necessary to compile this body
13654 -- with expansion enabled, except if we are within a generic
13655 -- package, in which case the usual generic rule applies.
13658 Exp_Status
: Boolean := True;
13662 -- Loop through scopes looking for generic package
13664 Scop
:= Scope
(Defining_Entity
(Instance_Spec
(Inst_Node
)));
13665 while Present
(Scop
)
13666 and then Scop
/= Standard_Standard
13668 if Ekind
(Scop
) = E_Generic_Package
then
13669 Exp_Status
:= False;
13673 Scop
:= Scope
(Scop
);
13676 -- Collect previous instantiations in the unit that contains
13677 -- the desired generic.
13679 if Nkind
(Parent
(True_Parent
)) /= N_Compilation_Unit
13680 and then not Body_Optional
13684 Info
: Pending_Body_Info
;
13688 Par
:= Parent
(Inst_Node
);
13689 while Present
(Par
) loop
13690 exit when Nkind
(Parent
(Par
)) = N_Compilation_Unit
;
13691 Par
:= Parent
(Par
);
13694 pragma Assert
(Present
(Par
));
13696 if Nkind
(Par
) = N_Package_Body
then
13697 Collect_Previous_Instances
(Declarations
(Par
));
13699 elsif Nkind
(Par
) = N_Package_Declaration
then
13700 Collect_Previous_Instances
13701 (Visible_Declarations
(Specification
(Par
)));
13702 Collect_Previous_Instances
13703 (Private_Declarations
(Specification
(Par
)));
13706 -- Enclosing unit is a subprogram body. In this
13707 -- case all instance bodies are processed in order
13708 -- and there is no need to collect them separately.
13713 Decl
:= First_Elmt
(Previous_Instances
);
13714 while Present
(Decl
) loop
13717 Instance_Spec
(Node
(Decl
)),
13718 Config_Switches
=> Save_Config_Switches
,
13719 Current_Sem_Unit
=>
13720 Get_Code_Unit
(Sloc
(Node
(Decl
))),
13721 Expander_Status
=> Exp_Status
,
13722 Inst_Node
=> Node
(Decl
),
13723 Local_Suppress_Stack_Top
=>
13724 Local_Suppress_Stack_Top
,
13725 Scope_Suppress
=> Scope_Suppress
,
13726 Warnings
=> Save_Warnings
);
13728 -- Package instance
13730 if Nkind
(Node
(Decl
)) = N_Package_Instantiation
13732 Instantiate_Package_Body
13733 (Info
, Body_Optional
=> True);
13735 -- Subprogram instance
13738 -- The instance_spec is in the wrapper package,
13739 -- usually followed by its local renaming
13740 -- declaration. See Build_Subprogram_Renaming
13741 -- for details. If the instance carries aspects,
13742 -- these result in the corresponding pragmas,
13743 -- inserted after the subprogram declaration.
13744 -- They must be skipped as well when retrieving
13745 -- the desired spec. Some of them may have been
13746 -- rewritten as null statements.
13747 -- A direct link would be more robust ???
13751 (Last
(Visible_Declarations
13752 (Specification
(Info
.Act_Decl
))));
13754 while Nkind_In
(Decl
,
13757 N_Subprogram_Renaming_Declaration
)
13759 Decl
:= Prev
(Decl
);
13762 Info
.Act_Decl
:= Decl
;
13765 Instantiate_Subprogram_Body
13766 (Info
, Body_Optional
=> True);
13774 Instantiate_Package_Body
13776 ((Act_Decl
=> True_Parent
,
13777 Config_Switches
=> Save_Config_Switches
,
13778 Current_Sem_Unit
=>
13779 Get_Code_Unit
(Sloc
(Inst_Node
)),
13780 Expander_Status
=> Exp_Status
,
13781 Inst_Node
=> Inst_Node
,
13782 Local_Suppress_Stack_Top
=> Local_Suppress_Stack_Top
,
13783 Scope_Suppress
=> Scope_Suppress
,
13784 Warnings
=> Save_Warnings
)),
13785 Body_Optional
=> Body_Optional
);
13789 -- Case where we are not instantiating a nested generic
13792 Opt
.Style_Check
:= False;
13793 Expander_Mode_Save_And_Set
(True);
13794 Load_Needed_Body
(Comp_Unit
, OK
);
13795 Opt
.Style_Check
:= Saved_Style_Check
;
13796 Restore_Warnings
(Saved_Warnings
);
13797 Expander_Mode_Restore
;
13800 and then Unit_Requires_Body
(Defining_Entity
(Spec
))
13801 and then not Body_Optional
13804 Bname
: constant Unit_Name_Type
:=
13805 Get_Body_Name
(Get_Unit_Name
(Unit
(Comp_Unit
)));
13808 -- In CodePeer mode, the missing body may make the analysis
13809 -- incomplete, but we do not treat it as fatal.
13811 if CodePeer_Mode
then
13815 Error_Msg_Unit_1
:= Bname
;
13816 Error_Msg_N
("this instantiation requires$!", N
);
13817 Error_Msg_File_1
:=
13818 Get_File_Name
(Bname
, Subunit
=> False);
13819 Error_Msg_N
("\but file{ was not found!", N
);
13820 raise Unrecoverable_Error
;
13827 -- If loading parent of the generic caused an instantiation circularity,
13828 -- we abandon compilation at this point, because otherwise in some cases
13829 -- we get into trouble with infinite recursions after this point.
13831 if Circularity_Detected
then
13832 raise Unrecoverable_Error
;
13834 end Load_Parent_Of_Generic
;
13836 ---------------------------------
13837 -- Map_Formal_Package_Entities --
13838 ---------------------------------
13840 procedure Map_Formal_Package_Entities
(Form
: Entity_Id
; Act
: Entity_Id
) is
13845 Set_Instance_Of
(Form
, Act
);
13847 -- Traverse formal and actual package to map the corresponding entities.
13848 -- We skip over internal entities that may be generated during semantic
13849 -- analysis, and find the matching entities by name, given that they
13850 -- must appear in the same order.
13852 E1
:= First_Entity
(Form
);
13853 E2
:= First_Entity
(Act
);
13854 while Present
(E1
) and then E1
/= First_Private_Entity
(Form
) loop
13855 -- Could this test be a single condition??? Seems like it could, and
13856 -- isn't FPE (Form) a constant anyway???
13858 if not Is_Internal
(E1
)
13859 and then Present
(Parent
(E1
))
13860 and then not Is_Class_Wide_Type
(E1
)
13861 and then not Is_Internal_Name
(Chars
(E1
))
13863 while Present
(E2
) and then Chars
(E2
) /= Chars
(E1
) loop
13870 Set_Instance_Of
(E1
, E2
);
13872 if Is_Type
(E1
) and then Is_Tagged_Type
(E2
) then
13873 Set_Instance_Of
(Class_Wide_Type
(E1
), Class_Wide_Type
(E2
));
13876 if Is_Constrained
(E1
) then
13877 Set_Instance_Of
(Base_Type
(E1
), Base_Type
(E2
));
13880 if Ekind
(E1
) = E_Package
and then No
(Renamed_Object
(E1
)) then
13881 Map_Formal_Package_Entities
(E1
, E2
);
13888 end Map_Formal_Package_Entities
;
13890 -----------------------
13891 -- Move_Freeze_Nodes --
13892 -----------------------
13894 procedure Move_Freeze_Nodes
13895 (Out_Of
: Entity_Id
;
13900 Next_Decl
: Node_Id
;
13901 Next_Node
: Node_Id
:= After
;
13904 function Is_Outer_Type
(T
: Entity_Id
) return Boolean;
13905 -- Check whether entity is declared in a scope external to that of the
13908 -------------------
13909 -- Is_Outer_Type --
13910 -------------------
13912 function Is_Outer_Type
(T
: Entity_Id
) return Boolean is
13913 Scop
: Entity_Id
:= Scope
(T
);
13916 if Scope_Depth
(Scop
) < Scope_Depth
(Out_Of
) then
13920 while Scop
/= Standard_Standard
loop
13921 if Scop
= Out_Of
then
13924 Scop
:= Scope
(Scop
);
13932 -- Start of processing for Move_Freeze_Nodes
13939 -- First remove the freeze nodes that may appear before all other
13943 while Present
(Decl
)
13944 and then Nkind
(Decl
) = N_Freeze_Entity
13945 and then Is_Outer_Type
(Entity
(Decl
))
13947 Decl
:= Remove_Head
(L
);
13948 Insert_After
(Next_Node
, Decl
);
13949 Set_Analyzed
(Decl
, False);
13954 -- Next scan the list of declarations and remove each freeze node that
13955 -- appears ahead of the current node.
13957 while Present
(Decl
) loop
13958 while Present
(Next
(Decl
))
13959 and then Nkind
(Next
(Decl
)) = N_Freeze_Entity
13960 and then Is_Outer_Type
(Entity
(Next
(Decl
)))
13962 Next_Decl
:= Remove_Next
(Decl
);
13963 Insert_After
(Next_Node
, Next_Decl
);
13964 Set_Analyzed
(Next_Decl
, False);
13965 Next_Node
:= Next_Decl
;
13968 -- If the declaration is a nested package or concurrent type, then
13969 -- recurse. Nested generic packages will have been processed from the
13972 case Nkind
(Decl
) is
13973 when N_Package_Declaration
=>
13974 Spec
:= Specification
(Decl
);
13976 when N_Task_Type_Declaration
=>
13977 Spec
:= Task_Definition
(Decl
);
13979 when N_Protected_Type_Declaration
=>
13980 Spec
:= Protected_Definition
(Decl
);
13986 if Present
(Spec
) then
13987 Move_Freeze_Nodes
(Out_Of
, Next_Node
, Visible_Declarations
(Spec
));
13988 Move_Freeze_Nodes
(Out_Of
, Next_Node
, Private_Declarations
(Spec
));
13993 end Move_Freeze_Nodes
;
13999 function Next_Assoc
(E
: Assoc_Ptr
) return Assoc_Ptr
is
14001 return Generic_Renamings
.Table
(E
).Next_In_HTable
;
14004 ------------------------
14005 -- Preanalyze_Actuals --
14006 ------------------------
14008 procedure Preanalyze_Actuals
(N
: Node_Id
; Inst
: Entity_Id
:= Empty
) is
14011 Errs
: constant Nat
:= Serious_Errors_Detected
;
14013 Cur
: Entity_Id
:= Empty
;
14014 -- Current homograph of the instance name
14016 Vis
: Boolean := False;
14017 -- Saved visibility status of the current homograph
14020 Assoc
:= First
(Generic_Associations
(N
));
14022 -- If the instance is a child unit, its name may hide an outer homonym,
14023 -- so make it invisible to perform name resolution on the actuals.
14025 if Nkind
(Defining_Unit_Name
(N
)) = N_Defining_Program_Unit_Name
14027 (Current_Entity
(Defining_Identifier
(Defining_Unit_Name
(N
))))
14029 Cur
:= Current_Entity
(Defining_Identifier
(Defining_Unit_Name
(N
)));
14031 if Is_Compilation_Unit
(Cur
) then
14032 Vis
:= Is_Immediately_Visible
(Cur
);
14033 Set_Is_Immediately_Visible
(Cur
, False);
14039 while Present
(Assoc
) loop
14040 if Nkind
(Assoc
) /= N_Others_Choice
then
14041 Act
:= Explicit_Generic_Actual_Parameter
(Assoc
);
14043 -- Within a nested instantiation, a defaulted actual is an empty
14044 -- association, so nothing to analyze. If the subprogram actual
14045 -- is an attribute, analyze prefix only, because actual is not a
14046 -- complete attribute reference.
14048 -- If actual is an allocator, analyze expression only. The full
14049 -- analysis can generate code, and if instance is a compilation
14050 -- unit we have to wait until the package instance is installed
14051 -- to have a proper place to insert this code.
14053 -- String literals may be operators, but at this point we do not
14054 -- know whether the actual is a formal subprogram or a string.
14059 elsif Nkind
(Act
) = N_Attribute_Reference
then
14060 Analyze
(Prefix
(Act
));
14062 elsif Nkind
(Act
) = N_Explicit_Dereference
then
14063 Analyze
(Prefix
(Act
));
14065 elsif Nkind
(Act
) = N_Allocator
then
14067 Expr
: constant Node_Id
:= Expression
(Act
);
14070 if Nkind
(Expr
) = N_Subtype_Indication
then
14071 Analyze
(Subtype_Mark
(Expr
));
14073 -- Analyze separately each discriminant constraint, when
14074 -- given with a named association.
14080 Constr
:= First
(Constraints
(Constraint
(Expr
)));
14081 while Present
(Constr
) loop
14082 if Nkind
(Constr
) = N_Discriminant_Association
then
14083 Analyze
(Expression
(Constr
));
14097 elsif Nkind
(Act
) /= N_Operator_Symbol
then
14100 -- Within a package instance, mark actuals that are limited
14101 -- views, so their use can be moved to the body of the
14104 if Is_Entity_Name
(Act
)
14105 and then Is_Type
(Entity
(Act
))
14106 and then From_Limited_With
(Entity
(Act
))
14107 and then Present
(Inst
)
14109 Append_Elmt
(Entity
(Act
), Incomplete_Actuals
(Inst
));
14113 if Errs
/= Serious_Errors_Detected
then
14115 -- Do a minimal analysis of the generic, to prevent spurious
14116 -- warnings complaining about the generic being unreferenced,
14117 -- before abandoning the instantiation.
14119 Analyze
(Name
(N
));
14121 if Is_Entity_Name
(Name
(N
))
14122 and then Etype
(Name
(N
)) /= Any_Type
14124 Generate_Reference
(Entity
(Name
(N
)), Name
(N
));
14125 Set_Is_Instantiated
(Entity
(Name
(N
)));
14128 if Present
(Cur
) then
14130 -- For the case of a child instance hiding an outer homonym,
14131 -- provide additional warning which might explain the error.
14133 Set_Is_Immediately_Visible
(Cur
, Vis
);
14135 ("& hides outer unit with the same name??",
14136 N
, Defining_Unit_Name
(N
));
14139 Abandon_Instantiation
(Act
);
14146 if Present
(Cur
) then
14147 Set_Is_Immediately_Visible
(Cur
, Vis
);
14149 end Preanalyze_Actuals
;
14151 -------------------------------
14152 -- Provide_Completing_Bodies --
14153 -------------------------------
14155 procedure Provide_Completing_Bodies
(N
: Node_Id
) is
14156 procedure Build_Completing_Body
(Subp_Decl
: Node_Id
);
14157 -- Generate the completing body for subprogram declaration Subp_Decl
14159 procedure Provide_Completing_Bodies_In
(Decls
: List_Id
);
14160 -- Generating completing bodies for all subprograms found in declarative
14163 ---------------------------
14164 -- Build_Completing_Body --
14165 ---------------------------
14167 procedure Build_Completing_Body
(Subp_Decl
: Node_Id
) is
14168 Loc
: constant Source_Ptr
:= Sloc
(Subp_Decl
);
14169 Subp_Id
: constant Entity_Id
:= Defining_Entity
(Subp_Decl
);
14173 -- Nothing to do if the subprogram already has a completing body
14175 if Present
(Corresponding_Body
(Subp_Decl
)) then
14178 -- Mark the function as having a valid return statement even though
14179 -- the body contains a single raise statement.
14181 elsif Ekind
(Subp_Id
) = E_Function
then
14182 Set_Return_Present
(Subp_Id
);
14185 -- Clone the specification to obtain new entities and reset the only
14188 Spec
:= Copy_Subprogram_Spec
(Specification
(Subp_Decl
));
14189 Set_Generic_Parent
(Spec
, Empty
);
14192 -- function Func ... return ... is
14194 -- procedure Proc ... is
14196 -- raise Program_Error with "access before elaboration";
14199 Insert_After_And_Analyze
(Subp_Decl
,
14200 Make_Subprogram_Body
(Loc
,
14201 Specification
=> Spec
,
14202 Declarations
=> New_List
,
14203 Handled_Statement_Sequence
=>
14204 Make_Handled_Sequence_Of_Statements
(Loc
,
14205 Statements
=> New_List
(
14206 Make_Raise_Program_Error
(Loc
,
14207 Reason
=> PE_Access_Before_Elaboration
)))));
14208 end Build_Completing_Body
;
14210 ----------------------------------
14211 -- Provide_Completing_Bodies_In --
14212 ----------------------------------
14214 procedure Provide_Completing_Bodies_In
(Decls
: List_Id
) is
14218 if Present
(Decls
) then
14219 Decl
:= First
(Decls
);
14220 while Present
(Decl
) loop
14221 Provide_Completing_Bodies
(Decl
);
14225 end Provide_Completing_Bodies_In
;
14231 -- Start of processing for Provide_Completing_Bodies
14234 if Nkind
(N
) = N_Package_Declaration
then
14235 Spec
:= Specification
(N
);
14237 Push_Scope
(Defining_Entity
(N
));
14238 Provide_Completing_Bodies_In
(Visible_Declarations
(Spec
));
14239 Provide_Completing_Bodies_In
(Private_Declarations
(Spec
));
14242 elsif Nkind
(N
) = N_Subprogram_Declaration
then
14243 Build_Completing_Body
(N
);
14245 end Provide_Completing_Bodies
;
14247 -------------------
14248 -- Remove_Parent --
14249 -------------------
14251 procedure Remove_Parent
(In_Body
: Boolean := False) is
14252 S
: Entity_Id
:= Current_Scope
;
14253 -- S is the scope containing the instantiation just completed. The scope
14254 -- stack contains the parent instances of the instantiation, followed by
14263 -- After child instantiation is complete, remove from scope stack the
14264 -- extra copy of the current scope, and then remove parent instances.
14266 if not In_Body
then
14269 while Current_Scope
/= S
loop
14270 P
:= Current_Scope
;
14271 End_Package_Scope
(Current_Scope
);
14273 if In_Open_Scopes
(P
) then
14274 E
:= First_Entity
(P
);
14275 while Present
(E
) loop
14276 Set_Is_Immediately_Visible
(E
, True);
14280 -- If instantiation is declared in a block, it is the enclosing
14281 -- scope that might be a parent instance. Note that only one
14282 -- block can be involved, because the parent instances have
14283 -- been installed within it.
14285 if Ekind
(P
) = E_Block
then
14286 Cur_P
:= Scope
(P
);
14291 if Is_Generic_Instance
(Cur_P
) and then P
/= Current_Scope
then
14292 -- We are within an instance of some sibling. Retain
14293 -- visibility of parent, for proper subsequent cleanup, and
14294 -- reinstall private declarations as well.
14296 Set_In_Private_Part
(P
);
14297 Install_Private_Declarations
(P
);
14300 -- If the ultimate parent is a top-level unit recorded in
14301 -- Instance_Parent_Unit, then reset its visibility to what it was
14302 -- before instantiation. (It's not clear what the purpose is of
14303 -- testing whether Scope (P) is In_Open_Scopes, but that test was
14304 -- present before the ultimate parent test was added.???)
14306 elsif not In_Open_Scopes
(Scope
(P
))
14307 or else (P
= Instance_Parent_Unit
14308 and then not Parent_Unit_Visible
)
14310 Set_Is_Immediately_Visible
(P
, False);
14312 -- If the current scope is itself an instantiation of a generic
14313 -- nested within P, and we are in the private part of body of this
14314 -- instantiation, restore the full views of P, that were removed
14315 -- in End_Package_Scope above. This obscure case can occur when a
14316 -- subunit of a generic contains an instance of a child unit of
14317 -- its generic parent unit.
14319 elsif S
= Current_Scope
and then Is_Generic_Instance
(S
) then
14321 Par
: constant Entity_Id
:=
14322 Generic_Parent
(Package_Specification
(S
));
14325 and then P
= Scope
(Par
)
14326 and then (In_Package_Body
(S
) or else In_Private_Part
(S
))
14328 Set_In_Private_Part
(P
);
14329 Install_Private_Declarations
(P
);
14335 -- Reset visibility of entities in the enclosing scope
14337 Set_Is_Hidden_Open_Scope
(Current_Scope
, False);
14339 Hidden
:= First_Elmt
(Hidden_Entities
);
14340 while Present
(Hidden
) loop
14341 Set_Is_Immediately_Visible
(Node
(Hidden
), True);
14342 Next_Elmt
(Hidden
);
14346 -- Each body is analyzed separately, and there is no context that
14347 -- needs preserving from one body instance to the next, so remove all
14348 -- parent scopes that have been installed.
14350 while Present
(S
) loop
14351 End_Package_Scope
(S
);
14352 Set_Is_Immediately_Visible
(S
, False);
14353 S
:= Current_Scope
;
14354 exit when S
= Standard_Standard
;
14363 procedure Restore_Env
is
14364 Saved
: Instance_Env
renames Instance_Envs
.Table
(Instance_Envs
.Last
);
14367 if No
(Current_Instantiated_Parent
.Act_Id
) then
14368 -- Restore environment after subprogram inlining
14370 Restore_Private_Views
(Empty
);
14373 Current_Instantiated_Parent
:= Saved
.Instantiated_Parent
;
14374 Exchanged_Views
:= Saved
.Exchanged_Views
;
14375 Hidden_Entities
:= Saved
.Hidden_Entities
;
14376 Current_Sem_Unit
:= Saved
.Current_Sem_Unit
;
14377 Parent_Unit_Visible
:= Saved
.Parent_Unit_Visible
;
14378 Instance_Parent_Unit
:= Saved
.Instance_Parent_Unit
;
14380 Restore_Config_Switches
(Saved
.Switches
);
14382 Instance_Envs
.Decrement_Last
;
14385 ---------------------------
14386 -- Restore_Private_Views --
14387 ---------------------------
14389 procedure Restore_Private_Views
14390 (Pack_Id
: Entity_Id
;
14391 Is_Package
: Boolean := True)
14396 Dep_Elmt
: Elmt_Id
;
14399 procedure Restore_Nested_Formal
(Formal
: Entity_Id
);
14400 -- Hide the generic formals of formal packages declared with box which
14401 -- were reachable in the current instantiation.
14403 ---------------------------
14404 -- Restore_Nested_Formal --
14405 ---------------------------
14407 procedure Restore_Nested_Formal
(Formal
: Entity_Id
) is
14411 if Present
(Renamed_Object
(Formal
))
14412 and then Denotes_Formal_Package
(Renamed_Object
(Formal
), True)
14416 elsif Present
(Associated_Formal_Package
(Formal
)) then
14417 Ent
:= First_Entity
(Formal
);
14418 while Present
(Ent
) loop
14419 exit when Ekind
(Ent
) = E_Package
14420 and then Renamed_Entity
(Ent
) = Renamed_Entity
(Formal
);
14422 Set_Is_Hidden
(Ent
);
14423 Set_Is_Potentially_Use_Visible
(Ent
, False);
14425 -- If package, then recurse
14427 if Ekind
(Ent
) = E_Package
then
14428 Restore_Nested_Formal
(Ent
);
14434 end Restore_Nested_Formal
;
14436 -- Start of processing for Restore_Private_Views
14439 M
:= First_Elmt
(Exchanged_Views
);
14440 while Present
(M
) loop
14443 -- Subtypes of types whose views have been exchanged, and that are
14444 -- defined within the instance, were not on the Private_Dependents
14445 -- list on entry to the instance, so they have to be exchanged
14446 -- explicitly now, in order to remain consistent with the view of the
14449 if Ekind_In
(Typ
, E_Private_Type
,
14450 E_Limited_Private_Type
,
14451 E_Record_Type_With_Private
)
14453 Dep_Elmt
:= First_Elmt
(Private_Dependents
(Typ
));
14454 while Present
(Dep_Elmt
) loop
14455 Dep_Typ
:= Node
(Dep_Elmt
);
14457 if Scope
(Dep_Typ
) = Pack_Id
14458 and then Present
(Full_View
(Dep_Typ
))
14460 Replace_Elmt
(Dep_Elmt
, Full_View
(Dep_Typ
));
14461 Exchange_Declarations
(Dep_Typ
);
14464 Next_Elmt
(Dep_Elmt
);
14468 Exchange_Declarations
(Node
(M
));
14472 if No
(Pack_Id
) then
14476 -- Make the generic formal parameters private, and make the formal types
14477 -- into subtypes of the actuals again.
14479 E
:= First_Entity
(Pack_Id
);
14480 while Present
(E
) loop
14481 Set_Is_Hidden
(E
, True);
14484 and then Nkind
(Parent
(E
)) = N_Subtype_Declaration
14486 -- If the actual for E is itself a generic actual type from
14487 -- an enclosing instance, E is still a generic actual type
14488 -- outside of the current instance. This matter when resolving
14489 -- an overloaded call that may be ambiguous in the enclosing
14490 -- instance, when two of its actuals coincide.
14492 if Is_Entity_Name
(Subtype_Indication
(Parent
(E
)))
14493 and then Is_Generic_Actual_Type
14494 (Entity
(Subtype_Indication
(Parent
(E
))))
14498 Set_Is_Generic_Actual_Type
(E
, False);
14501 -- An unusual case of aliasing: the actual may also be directly
14502 -- visible in the generic, and be private there, while it is fully
14503 -- visible in the context of the instance. The internal subtype
14504 -- is private in the instance but has full visibility like its
14505 -- parent in the enclosing scope. This enforces the invariant that
14506 -- the privacy status of all private dependents of a type coincide
14507 -- with that of the parent type. This can only happen when a
14508 -- generic child unit is instantiated within a sibling.
14510 if Is_Private_Type
(E
)
14511 and then not Is_Private_Type
(Etype
(E
))
14513 Exchange_Declarations
(E
);
14516 elsif Ekind
(E
) = E_Package
then
14518 -- The end of the renaming list is the renaming of the generic
14519 -- package itself. If the instance is a subprogram, all entities
14520 -- in the corresponding package are renamings. If this entity is
14521 -- a formal package, make its own formals private as well. The
14522 -- actual in this case is itself the renaming of an instantiation.
14523 -- If the entity is not a package renaming, it is the entity
14524 -- created to validate formal package actuals: ignore it.
14526 -- If the actual is itself a formal package for the enclosing
14527 -- generic, or the actual for such a formal package, it remains
14528 -- visible on exit from the instance, and therefore nothing needs
14529 -- to be done either, except to keep it accessible.
14531 if Is_Package
and then Renamed_Object
(E
) = Pack_Id
then
14534 elsif Nkind
(Parent
(E
)) /= N_Package_Renaming_Declaration
then
14538 Denotes_Formal_Package
(Renamed_Object
(E
), True, Pack_Id
)
14540 Set_Is_Hidden
(E
, False);
14544 Act_P
: constant Entity_Id
:= Renamed_Object
(E
);
14548 Id
:= First_Entity
(Act_P
);
14550 and then Id
/= First_Private_Entity
(Act_P
)
14552 exit when Ekind
(Id
) = E_Package
14553 and then Renamed_Object
(Id
) = Act_P
;
14555 Set_Is_Hidden
(Id
, True);
14556 Set_Is_Potentially_Use_Visible
(Id
, In_Use
(Act_P
));
14558 if Ekind
(Id
) = E_Package
then
14559 Restore_Nested_Formal
(Id
);
14570 end Restore_Private_Views
;
14577 (Gen_Unit
: Entity_Id
;
14578 Act_Unit
: Entity_Id
)
14582 Set_Instance_Env
(Gen_Unit
, Act_Unit
);
14585 ----------------------------
14586 -- Save_Global_References --
14587 ----------------------------
14589 procedure Save_Global_References
(Templ
: Node_Id
) is
14591 -- ??? it is horrible to use global variables in highly recursive code
14594 -- The entity of the current associated node
14596 Gen_Scope
: Entity_Id
;
14597 -- The scope of the generic for which references are being saved
14600 -- The current associated node
14602 function Is_Global
(E
: Entity_Id
) return Boolean;
14603 -- Check whether entity is defined outside of generic unit. Examine the
14604 -- scope of an entity, and the scope of the scope, etc, until we find
14605 -- either Standard, in which case the entity is global, or the generic
14606 -- unit itself, which indicates that the entity is local. If the entity
14607 -- is the generic unit itself, as in the case of a recursive call, or
14608 -- the enclosing generic unit, if different from the current scope, then
14609 -- it is local as well, because it will be replaced at the point of
14610 -- instantiation. On the other hand, if it is a reference to a child
14611 -- unit of a common ancestor, which appears in an instantiation, it is
14612 -- global because it is used to denote a specific compilation unit at
14613 -- the time the instantiations will be analyzed.
14615 procedure Qualify_Universal_Operands
14617 Func_Call
: Node_Id
);
14618 -- Op denotes a binary or unary operator in generic template Templ. Node
14619 -- Func_Call is the function call alternative of the operator within the
14620 -- the analyzed copy of the template. Change each operand which yields a
14621 -- universal type by wrapping it into a qualified expression
14623 -- Actual_Typ'(Operand)
14625 -- where Actual_Typ is the type of corresponding actual parameter of
14626 -- Operand in Func_Call.
14628 procedure Reset_Entity
(N
: Node_Id
);
14629 -- Save semantic information on global entity so that it is not resolved
14630 -- again at instantiation time.
14632 procedure Save_Entity_Descendants
(N
: Node_Id
);
14633 -- Apply Save_Global_References to the two syntactic descendants of
14634 -- non-terminal nodes that carry an Associated_Node and are processed
14635 -- through Reset_Entity. Once the global entity (if any) has been
14636 -- captured together with its type, only two syntactic descendants need
14637 -- to be traversed to complete the processing of the tree rooted at N.
14638 -- This applies to Selected_Components, Expanded_Names, and to Operator
14639 -- nodes. N can also be a character literal, identifier, or operator
14640 -- symbol node, but the call has no effect in these cases.
14642 procedure Save_Global_Defaults
(N1
: Node_Id
; N2
: Node_Id
);
14643 -- Default actuals in nested instances must be handled specially
14644 -- because there is no link to them from the original tree. When an
14645 -- actual subprogram is given by a default, we add an explicit generic
14646 -- association for it in the instantiation node. When we save the
14647 -- global references on the name of the instance, we recover the list
14648 -- of generic associations, and add an explicit one to the original
14649 -- generic tree, through which a global actual can be preserved.
14650 -- Similarly, if a child unit is instantiated within a sibling, in the
14651 -- context of the parent, we must preserve the identifier of the parent
14652 -- so that it can be properly resolved in a subsequent instantiation.
14654 procedure Save_Global_Descendant
(D
: Union_Id
);
14655 -- Apply Save_References recursively to the descendants of node D
14657 procedure Save_References
(N
: Node_Id
);
14658 -- This is the recursive procedure that does the work, once the
14659 -- enclosing generic scope has been established.
14665 function Is_Global
(E
: Entity_Id
) return Boolean is
14668 function Is_Instance_Node
(Decl
: Node_Id
) return Boolean;
14669 -- Determine whether the parent node of a reference to a child unit
14670 -- denotes an instantiation or a formal package, in which case the
14671 -- reference to the child unit is global, even if it appears within
14672 -- the current scope (e.g. when the instance appears within the body
14673 -- of an ancestor).
14675 ----------------------
14676 -- Is_Instance_Node --
14677 ----------------------
14679 function Is_Instance_Node
(Decl
: Node_Id
) return Boolean is
14681 return Nkind
(Decl
) in N_Generic_Instantiation
14683 Nkind
(Original_Node
(Decl
)) = N_Formal_Package_Declaration
;
14684 end Is_Instance_Node
;
14686 -- Start of processing for Is_Global
14689 if E
= Gen_Scope
then
14692 elsif E
= Standard_Standard
then
14695 elsif Is_Child_Unit
(E
)
14696 and then (Is_Instance_Node
(Parent
(N2
))
14697 or else (Nkind
(Parent
(N2
)) = N_Expanded_Name
14698 and then N2
= Selector_Name
(Parent
(N2
))
14700 Is_Instance_Node
(Parent
(Parent
(N2
)))))
14706 while Se
/= Gen_Scope
loop
14707 if Se
= Standard_Standard
then
14718 --------------------------------
14719 -- Qualify_Universal_Operands --
14720 --------------------------------
14722 procedure Qualify_Universal_Operands
14724 Func_Call
: Node_Id
)
14726 procedure Qualify_Operand
(Opnd
: Node_Id
; Actual
: Node_Id
);
14727 -- Rewrite operand Opnd as a qualified expression of the form
14729 -- Actual_Typ'(Opnd)
14731 -- where Actual is the corresponding actual parameter of Opnd in
14732 -- function call Func_Call.
14734 function Qualify_Type
14736 Typ
: Entity_Id
) return Node_Id
;
14737 -- Qualify type Typ by creating a selected component of the form
14739 -- Scope_Of_Typ.Typ
14741 ---------------------
14742 -- Qualify_Operand --
14743 ---------------------
14745 procedure Qualify_Operand
(Opnd
: Node_Id
; Actual
: Node_Id
) is
14746 Loc
: constant Source_Ptr
:= Sloc
(Opnd
);
14747 Typ
: constant Entity_Id
:= Etype
(Actual
);
14752 -- Qualify the operand when it is of a universal type. Note that
14753 -- the template is unanalyzed and it is not possible to directly
14754 -- query the type. This transformation is not done when the type
14755 -- of the actual is internally generated because the type will be
14756 -- regenerated in the instance.
14758 if Yields_Universal_Type
(Opnd
)
14759 and then Comes_From_Source
(Typ
)
14760 and then not Is_Hidden
(Typ
)
14762 -- The type of the actual may be a global reference. Save this
14763 -- information by creating a reference to it.
14765 if Is_Global
(Typ
) then
14766 Mark
:= New_Occurrence_Of
(Typ
, Loc
);
14768 -- Otherwise rely on resolution to find the proper type within
14772 Mark
:= Qualify_Type
(Loc
, Typ
);
14776 Make_Qualified_Expression
(Loc
,
14777 Subtype_Mark
=> Mark
,
14778 Expression
=> Relocate_Node
(Opnd
));
14780 -- Mark the qualification to distinguish it from other source
14781 -- constructs and signal the instantiation mechanism that this
14782 -- node requires special processing. See Copy_Generic_Node for
14785 Set_Is_Qualified_Universal_Literal
(Qual
);
14787 Rewrite
(Opnd
, Qual
);
14789 end Qualify_Operand
;
14795 function Qualify_Type
14797 Typ
: Entity_Id
) return Node_Id
14799 Scop
: constant Entity_Id
:= Scope
(Typ
);
14803 Result
:= Make_Identifier
(Loc
, Chars
(Typ
));
14805 if Present
(Scop
) and then not Is_Generic_Unit
(Scop
) then
14807 Make_Selected_Component
(Loc
,
14808 Prefix
=> Make_Identifier
(Loc
, Chars
(Scop
)),
14809 Selector_Name
=> Result
);
14817 Actuals
: constant List_Id
:= Parameter_Associations
(Func_Call
);
14819 -- Start of processing for Qualify_Universal_Operands
14822 if Nkind
(Op
) in N_Binary_Op
then
14823 Qualify_Operand
(Left_Opnd
(Op
), First
(Actuals
));
14824 Qualify_Operand
(Right_Opnd
(Op
), Next
(First
(Actuals
)));
14826 elsif Nkind
(Op
) in N_Unary_Op
then
14827 Qualify_Operand
(Right_Opnd
(Op
), First
(Actuals
));
14829 end Qualify_Universal_Operands
;
14835 procedure Reset_Entity
(N
: Node_Id
) is
14836 procedure Set_Global_Type
(N
: Node_Id
; N2
: Node_Id
);
14837 -- If the type of N2 is global to the generic unit, save the type in
14838 -- the generic node. Just as we perform name capture for explicit
14839 -- references within the generic, we must capture the global types
14840 -- of local entities because they may participate in resolution in
14843 function Top_Ancestor
(E
: Entity_Id
) return Entity_Id
;
14844 -- Find the ultimate ancestor of the current unit. If it is not a
14845 -- generic unit, then the name of the current unit in the prefix of
14846 -- an expanded name must be replaced with its generic homonym to
14847 -- ensure that it will be properly resolved in an instance.
14849 ---------------------
14850 -- Set_Global_Type --
14851 ---------------------
14853 procedure Set_Global_Type
(N
: Node_Id
; N2
: Node_Id
) is
14854 Typ
: constant Entity_Id
:= Etype
(N2
);
14857 Set_Etype
(N
, Typ
);
14859 -- If the entity of N is not the associated node, this is a
14860 -- nested generic and it has an associated node as well, whose
14861 -- type is already the full view (see below). Indicate that the
14862 -- original node has a private view.
14864 if Entity
(N
) /= N2
and then Has_Private_View
(Entity
(N
)) then
14865 Set_Has_Private_View
(N
);
14868 -- If not a private type, nothing else to do
14870 if not Is_Private_Type
(Typ
) then
14871 if Is_Array_Type
(Typ
)
14872 and then Is_Private_Type
(Component_Type
(Typ
))
14874 Set_Has_Private_View
(N
);
14877 -- If it is a derivation of a private type in a context where no
14878 -- full view is needed, nothing to do either.
14880 elsif No
(Full_View
(Typ
)) and then Typ
/= Etype
(Typ
) then
14883 -- Otherwise mark the type for flipping and use the full view when
14887 Set_Has_Private_View
(N
);
14889 if Present
(Full_View
(Typ
)) then
14890 Set_Etype
(N2
, Full_View
(Typ
));
14894 if Is_Floating_Point_Type
(Typ
)
14895 and then Has_Dimension_System
(Typ
)
14897 Copy_Dimensions
(N2
, N
);
14899 end Set_Global_Type
;
14905 function Top_Ancestor
(E
: Entity_Id
) return Entity_Id
is
14910 while Is_Child_Unit
(Par
) loop
14911 Par
:= Scope
(Par
);
14917 -- Start of processing for Reset_Entity
14920 N2
:= Get_Associated_Node
(N
);
14923 if Present
(E
) then
14925 -- If the node is an entry call to an entry in an enclosing task,
14926 -- it is rewritten as a selected component. No global entity to
14927 -- preserve in this case, since the expansion will be redone in
14930 if not Nkind_In
(E
, N_Defining_Character_Literal
,
14931 N_Defining_Identifier
,
14932 N_Defining_Operator_Symbol
)
14934 Set_Associated_Node
(N
, Empty
);
14935 Set_Etype
(N
, Empty
);
14939 -- If the entity is an itype created as a subtype of an access
14940 -- type with a null exclusion restore source entity for proper
14941 -- visibility. The itype will be created anew in the instance.
14944 and then Ekind
(E
) = E_Access_Subtype
14945 and then Is_Entity_Name
(N
)
14946 and then Chars
(Etype
(E
)) = Chars
(N
)
14949 Set_Entity
(N2
, E
);
14953 if Is_Global
(E
) then
14955 -- If the entity is a package renaming that is the prefix of
14956 -- an expanded name, it has been rewritten as the renamed
14957 -- package, which is necessary semantically but complicates
14958 -- ASIS tree traversal, so we recover the original entity to
14959 -- expose the renaming. Take into account that the context may
14960 -- be a nested generic, that the original node may itself have
14961 -- an associated node that had better be an entity, and that
14962 -- the current node is still a selected component.
14964 if Ekind
(E
) = E_Package
14965 and then Nkind
(N
) = N_Selected_Component
14966 and then Nkind
(Parent
(N
)) = N_Expanded_Name
14967 and then Present
(Original_Node
(N2
))
14968 and then Is_Entity_Name
(Original_Node
(N2
))
14969 and then Present
(Entity
(Original_Node
(N2
)))
14971 if Is_Global
(Entity
(Original_Node
(N2
))) then
14972 N2
:= Original_Node
(N2
);
14973 Set_Associated_Node
(N
, N2
);
14974 Set_Global_Type
(N
, N2
);
14976 -- Renaming is local, and will be resolved in instance
14979 Set_Associated_Node
(N
, Empty
);
14980 Set_Etype
(N
, Empty
);
14984 Set_Global_Type
(N
, N2
);
14987 elsif Nkind
(N
) = N_Op_Concat
14988 and then Is_Generic_Type
(Etype
(N2
))
14989 and then (Base_Type
(Etype
(Right_Opnd
(N2
))) = Etype
(N2
)
14991 Base_Type
(Etype
(Left_Opnd
(N2
))) = Etype
(N2
))
14992 and then Is_Intrinsic_Subprogram
(E
)
14996 -- Entity is local. Mark generic node as unresolved. Note that now
14997 -- it does not have an entity.
15000 Set_Associated_Node
(N
, Empty
);
15001 Set_Etype
(N
, Empty
);
15004 if Nkind
(Parent
(N
)) in N_Generic_Instantiation
15005 and then N
= Name
(Parent
(N
))
15007 Save_Global_Defaults
(Parent
(N
), Parent
(N2
));
15010 elsif Nkind
(Parent
(N
)) = N_Selected_Component
15011 and then Nkind
(Parent
(N2
)) = N_Expanded_Name
15013 if Is_Global
(Entity
(Parent
(N2
))) then
15014 Change_Selected_Component_To_Expanded_Name
(Parent
(N
));
15015 Set_Associated_Node
(Parent
(N
), Parent
(N2
));
15016 Set_Global_Type
(Parent
(N
), Parent
(N2
));
15017 Save_Entity_Descendants
(N
);
15019 -- If this is a reference to the current generic entity, replace
15020 -- by the name of the generic homonym of the current package. This
15021 -- is because in an instantiation Par.P.Q will not resolve to the
15022 -- name of the instance, whose enclosing scope is not necessarily
15023 -- Par. We use the generic homonym rather that the name of the
15024 -- generic itself because it may be hidden by a local declaration.
15026 elsif In_Open_Scopes
(Entity
(Parent
(N2
)))
15028 Is_Generic_Unit
(Top_Ancestor
(Entity
(Prefix
(Parent
(N2
)))))
15030 if Ekind
(Entity
(Parent
(N2
))) = E_Generic_Package
then
15031 Rewrite
(Parent
(N
),
15032 Make_Identifier
(Sloc
(N
),
15034 Chars
(Generic_Homonym
(Entity
(Parent
(N2
))))));
15036 Rewrite
(Parent
(N
),
15037 Make_Identifier
(Sloc
(N
),
15038 Chars
=> Chars
(Selector_Name
(Parent
(N2
)))));
15042 if Nkind
(Parent
(Parent
(N
))) in N_Generic_Instantiation
15043 and then Parent
(N
) = Name
(Parent
(Parent
(N
)))
15045 Save_Global_Defaults
15046 (Parent
(Parent
(N
)), Parent
(Parent
(N2
)));
15049 -- A selected component may denote a static constant that has been
15050 -- folded. If the static constant is global to the generic, capture
15051 -- its value. Otherwise the folding will happen in any instantiation.
15053 elsif Nkind
(Parent
(N
)) = N_Selected_Component
15054 and then Nkind_In
(Parent
(N2
), N_Integer_Literal
, N_Real_Literal
)
15056 if Present
(Entity
(Original_Node
(Parent
(N2
))))
15057 and then Is_Global
(Entity
(Original_Node
(Parent
(N2
))))
15059 Rewrite
(Parent
(N
), New_Copy
(Parent
(N2
)));
15060 Set_Analyzed
(Parent
(N
), False);
15063 -- A selected component may be transformed into a parameterless
15064 -- function call. If the called entity is global, rewrite the node
15065 -- appropriately, i.e. as an extended name for the global entity.
15067 elsif Nkind
(Parent
(N
)) = N_Selected_Component
15068 and then Nkind
(Parent
(N2
)) = N_Function_Call
15069 and then N
= Selector_Name
(Parent
(N
))
15071 if No
(Parameter_Associations
(Parent
(N2
))) then
15072 if Is_Global
(Entity
(Name
(Parent
(N2
)))) then
15073 Change_Selected_Component_To_Expanded_Name
(Parent
(N
));
15074 Set_Associated_Node
(Parent
(N
), Name
(Parent
(N2
)));
15075 Set_Global_Type
(Parent
(N
), Name
(Parent
(N2
)));
15076 Save_Entity_Descendants
(N
);
15079 Set_Is_Prefixed_Call
(Parent
(N
));
15080 Set_Associated_Node
(N
, Empty
);
15081 Set_Etype
(N
, Empty
);
15084 -- In Ada 2005, X.F may be a call to a primitive operation,
15085 -- rewritten as F (X). This rewriting will be done again in an
15086 -- instance, so keep the original node. Global entities will be
15087 -- captured as for other constructs. Indicate that this must
15088 -- resolve as a call, to prevent accidental overloading in the
15089 -- instance, if both a component and a primitive operation appear
15093 Set_Is_Prefixed_Call
(Parent
(N
));
15096 -- Entity is local. Reset in generic unit, so that node is resolved
15097 -- anew at the point of instantiation.
15100 Set_Associated_Node
(N
, Empty
);
15101 Set_Etype
(N
, Empty
);
15105 -----------------------------
15106 -- Save_Entity_Descendants --
15107 -----------------------------
15109 procedure Save_Entity_Descendants
(N
: Node_Id
) is
15112 when N_Binary_Op
=>
15113 Save_Global_Descendant
(Union_Id
(Left_Opnd
(N
)));
15114 Save_Global_Descendant
(Union_Id
(Right_Opnd
(N
)));
15117 Save_Global_Descendant
(Union_Id
(Right_Opnd
(N
)));
15119 when N_Expanded_Name
15120 | N_Selected_Component
15122 Save_Global_Descendant
(Union_Id
(Prefix
(N
)));
15123 Save_Global_Descendant
(Union_Id
(Selector_Name
(N
)));
15125 when N_Character_Literal
15127 | N_Operator_Symbol
15132 raise Program_Error
;
15134 end Save_Entity_Descendants
;
15136 --------------------------
15137 -- Save_Global_Defaults --
15138 --------------------------
15140 procedure Save_Global_Defaults
(N1
: Node_Id
; N2
: Node_Id
) is
15141 Loc
: constant Source_Ptr
:= Sloc
(N1
);
15142 Assoc2
: constant List_Id
:= Generic_Associations
(N2
);
15143 Gen_Id
: constant Entity_Id
:= Get_Generic_Entity
(N2
);
15150 Actual
: Entity_Id
;
15153 Assoc1
:= Generic_Associations
(N1
);
15155 if Present
(Assoc1
) then
15156 Act1
:= First
(Assoc1
);
15159 Set_Generic_Associations
(N1
, New_List
);
15160 Assoc1
:= Generic_Associations
(N1
);
15163 if Present
(Assoc2
) then
15164 Act2
:= First
(Assoc2
);
15169 while Present
(Act1
) and then Present
(Act2
) loop
15174 -- Find the associations added for default subprograms
15176 if Present
(Act2
) then
15177 while Nkind
(Act2
) /= N_Generic_Association
15178 or else No
(Entity
(Selector_Name
(Act2
)))
15179 or else not Is_Overloadable
(Entity
(Selector_Name
(Act2
)))
15184 -- Add a similar association if the default is global. The
15185 -- renaming declaration for the actual has been analyzed, and
15186 -- its alias is the program it renames. Link the actual in the
15187 -- original generic tree with the node in the analyzed tree.
15189 while Present
(Act2
) loop
15190 Subp
:= Entity
(Selector_Name
(Act2
));
15191 Def
:= Explicit_Generic_Actual_Parameter
(Act2
);
15193 -- Following test is defence against rubbish errors
15195 if No
(Alias
(Subp
)) then
15199 -- Retrieve the resolved actual from the renaming declaration
15200 -- created for the instantiated formal.
15202 Actual
:= Entity
(Name
(Parent
(Parent
(Subp
))));
15203 Set_Entity
(Def
, Actual
);
15204 Set_Etype
(Def
, Etype
(Actual
));
15206 if Is_Global
(Actual
) then
15208 Make_Generic_Association
(Loc
,
15210 New_Occurrence_Of
(Subp
, Loc
),
15211 Explicit_Generic_Actual_Parameter
=>
15212 New_Occurrence_Of
(Actual
, Loc
));
15214 Set_Associated_Node
15215 (Explicit_Generic_Actual_Parameter
(Ndec
), Def
);
15217 Append
(Ndec
, Assoc1
);
15219 -- If there are other defaults, add a dummy association in case
15220 -- there are other defaulted formals with the same name.
15222 elsif Present
(Next
(Act2
)) then
15224 Make_Generic_Association
(Loc
,
15226 New_Occurrence_Of
(Subp
, Loc
),
15227 Explicit_Generic_Actual_Parameter
=> Empty
);
15229 Append
(Ndec
, Assoc1
);
15236 if Nkind
(Name
(N1
)) = N_Identifier
15237 and then Is_Child_Unit
(Gen_Id
)
15238 and then Is_Global
(Gen_Id
)
15239 and then Is_Generic_Unit
(Scope
(Gen_Id
))
15240 and then In_Open_Scopes
(Scope
(Gen_Id
))
15242 -- This is an instantiation of a child unit within a sibling, so
15243 -- that the generic parent is in scope. An eventual instance must
15244 -- occur within the scope of an instance of the parent. Make name
15245 -- in instance into an expanded name, to preserve the identifier
15246 -- of the parent, so it can be resolved subsequently.
15248 Rewrite
(Name
(N2
),
15249 Make_Expanded_Name
(Loc
,
15250 Chars
=> Chars
(Gen_Id
),
15251 Prefix
=> New_Occurrence_Of
(Scope
(Gen_Id
), Loc
),
15252 Selector_Name
=> New_Occurrence_Of
(Gen_Id
, Loc
)));
15253 Set_Entity
(Name
(N2
), Gen_Id
);
15255 Rewrite
(Name
(N1
),
15256 Make_Expanded_Name
(Loc
,
15257 Chars
=> Chars
(Gen_Id
),
15258 Prefix
=> New_Occurrence_Of
(Scope
(Gen_Id
), Loc
),
15259 Selector_Name
=> New_Occurrence_Of
(Gen_Id
, Loc
)));
15261 Set_Associated_Node
(Name
(N1
), Name
(N2
));
15262 Set_Associated_Node
(Prefix
(Name
(N1
)), Empty
);
15263 Set_Associated_Node
15264 (Selector_Name
(Name
(N1
)), Selector_Name
(Name
(N2
)));
15265 Set_Etype
(Name
(N1
), Etype
(Gen_Id
));
15267 end Save_Global_Defaults
;
15269 ----------------------------
15270 -- Save_Global_Descendant --
15271 ----------------------------
15273 procedure Save_Global_Descendant
(D
: Union_Id
) is
15277 if D
in Node_Range
then
15278 if D
= Union_Id
(Empty
) then
15281 elsif Nkind
(Node_Id
(D
)) /= N_Compilation_Unit
then
15282 Save_References
(Node_Id
(D
));
15285 elsif D
in List_Range
then
15286 pragma Assert
(D
/= Union_Id
(No_List
));
15287 -- Because No_List = Empty, which is in Node_Range above
15289 if Is_Empty_List
(List_Id
(D
)) then
15293 N1
:= First
(List_Id
(D
));
15294 while Present
(N1
) loop
15295 Save_References
(N1
);
15300 -- Element list or other non-node field, nothing to do
15305 end Save_Global_Descendant
;
15307 ---------------------
15308 -- Save_References --
15309 ---------------------
15311 -- This is the recursive procedure that does the work once the enclosing
15312 -- generic scope has been established. We have to treat specially a
15313 -- number of node rewritings that are required by semantic processing
15314 -- and which change the kind of nodes in the generic copy: typically
15315 -- constant-folding, replacing an operator node by a string literal, or
15316 -- a selected component by an expanded name. In each of those cases, the
15317 -- transformation is propagated to the generic unit.
15319 procedure Save_References
(N
: Node_Id
) is
15320 Loc
: constant Source_Ptr
:= Sloc
(N
);
15322 function Requires_Delayed_Save
(Nod
: Node_Id
) return Boolean;
15323 -- Determine whether arbitrary node Nod requires delayed capture of
15324 -- global references within its aspect specifications.
15326 procedure Save_References_In_Aggregate
(N
: Node_Id
);
15327 -- Save all global references in [extension] aggregate node N
15329 procedure Save_References_In_Char_Lit_Or_Op_Symbol
(N
: Node_Id
);
15330 -- Save all global references in a character literal or operator
15331 -- symbol denoted by N.
15333 procedure Save_References_In_Descendants
(N
: Node_Id
);
15334 -- Save all global references in all descendants of node N
15336 procedure Save_References_In_Identifier
(N
: Node_Id
);
15337 -- Save all global references in identifier node N
15339 procedure Save_References_In_Operator
(N
: Node_Id
);
15340 -- Save all global references in operator node N
15342 procedure Save_References_In_Pragma
(Prag
: Node_Id
);
15343 -- Save all global references found within the expression of pragma
15346 ---------------------------
15347 -- Requires_Delayed_Save --
15348 ---------------------------
15350 function Requires_Delayed_Save
(Nod
: Node_Id
) return Boolean is
15352 -- Generic packages and subprograms require delayed capture of
15353 -- global references within their aspects due to the timing of
15354 -- annotation analysis.
15356 if Nkind_In
(Nod
, N_Generic_Package_Declaration
,
15357 N_Generic_Subprogram_Declaration
,
15359 N_Package_Body_Stub
,
15361 N_Subprogram_Body_Stub
)
15363 -- Since the capture of global references is done on the
15364 -- unanalyzed generic template, there is no information around
15365 -- to infer the context. Use the Associated_Entity linkages to
15366 -- peek into the analyzed generic copy and determine what the
15367 -- template corresponds to.
15369 if Nod
= Templ
then
15371 Is_Generic_Declaration_Or_Body
15372 (Unit_Declaration_Node
15373 (Associated_Entity
(Defining_Entity
(Nod
))));
15375 -- Otherwise the generic unit being processed is not the top
15376 -- level template. It is safe to capture of global references
15377 -- within the generic unit because at this point the top level
15378 -- copy is fully analyzed.
15384 -- Otherwise capture the global references without interference
15389 end Requires_Delayed_Save
;
15391 ----------------------------------
15392 -- Save_References_In_Aggregate --
15393 ----------------------------------
15395 procedure Save_References_In_Aggregate
(N
: Node_Id
) is
15397 Qual
: Node_Id
:= Empty
;
15398 Typ
: Entity_Id
:= Empty
;
15400 use Atree
.Unchecked_Access
;
15401 -- This code section is part of implementing an untyped tree
15402 -- traversal, so it needs direct access to node fields.
15405 N2
:= Get_Associated_Node
(N
);
15407 if Present
(N2
) then
15410 -- In an instance within a generic, use the name of the actual
15411 -- and not the original generic parameter. If the actual is
15412 -- global in the current generic it must be preserved for its
15415 if Nkind
(Parent
(Typ
)) = N_Subtype_Declaration
15416 and then Present
(Generic_Parent_Type
(Parent
(Typ
)))
15418 Typ
:= Base_Type
(Typ
);
15419 Set_Etype
(N2
, Typ
);
15423 if No
(N2
) or else No
(Typ
) or else not Is_Global
(Typ
) then
15424 Set_Associated_Node
(N
, Empty
);
15426 -- If the aggregate is an actual in a call, it has been
15427 -- resolved in the current context, to some local type. The
15428 -- enclosing call may have been disambiguated by the aggregate,
15429 -- and this disambiguation might fail at instantiation time
15430 -- because the type to which the aggregate did resolve is not
15431 -- preserved. In order to preserve some of this information,
15432 -- wrap the aggregate in a qualified expression, using the id
15433 -- of its type. For further disambiguation we qualify the type
15434 -- name with its scope (if visible and not hidden by a local
15435 -- homograph) because both id's will have corresponding
15436 -- entities in an instance. This resolves most of the problems
15437 -- with missing type information on aggregates in instances.
15440 and then Nkind
(N2
) = Nkind
(N
)
15441 and then Nkind
(Parent
(N2
)) in N_Subprogram_Call
15442 and then Present
(Typ
)
15443 and then Comes_From_Source
(Typ
)
15445 Nam
:= Make_Identifier
(Loc
, Chars
(Typ
));
15447 if Is_Immediately_Visible
(Scope
(Typ
))
15449 (not In_Open_Scopes
(Scope
(Typ
))
15450 or else Current_Entity
(Scope
(Typ
)) = Scope
(Typ
))
15453 Make_Selected_Component
(Loc
,
15455 Make_Identifier
(Loc
, Chars
(Scope
(Typ
))),
15456 Selector_Name
=> Nam
);
15460 Make_Qualified_Expression
(Loc
,
15461 Subtype_Mark
=> Nam
,
15462 Expression
=> Relocate_Node
(N
));
15466 Save_Global_Descendant
(Field1
(N
));
15467 Save_Global_Descendant
(Field2
(N
));
15468 Save_Global_Descendant
(Field3
(N
));
15469 Save_Global_Descendant
(Field5
(N
));
15471 if Present
(Qual
) then
15474 end Save_References_In_Aggregate
;
15476 ----------------------------------------------
15477 -- Save_References_In_Char_Lit_Or_Op_Symbol --
15478 ----------------------------------------------
15480 procedure Save_References_In_Char_Lit_Or_Op_Symbol
(N
: Node_Id
) is
15482 if Nkind
(N
) = Nkind
(Get_Associated_Node
(N
)) then
15485 elsif Nkind
(N
) = N_Operator_Symbol
15486 and then Nkind
(Get_Associated_Node
(N
)) = N_String_Literal
15488 Change_Operator_Symbol_To_String_Literal
(N
);
15490 end Save_References_In_Char_Lit_Or_Op_Symbol
;
15492 ------------------------------------
15493 -- Save_References_In_Descendants --
15494 ------------------------------------
15496 procedure Save_References_In_Descendants
(N
: Node_Id
) is
15497 use Atree
.Unchecked_Access
;
15498 -- This code section is part of implementing an untyped tree
15499 -- traversal, so it needs direct access to node fields.
15502 Save_Global_Descendant
(Field1
(N
));
15503 Save_Global_Descendant
(Field2
(N
));
15504 Save_Global_Descendant
(Field3
(N
));
15505 Save_Global_Descendant
(Field4
(N
));
15506 Save_Global_Descendant
(Field5
(N
));
15507 end Save_References_In_Descendants
;
15509 -----------------------------------
15510 -- Save_References_In_Identifier --
15511 -----------------------------------
15513 procedure Save_References_In_Identifier
(N
: Node_Id
) is
15515 -- The node did not undergo a transformation
15517 if Nkind
(N
) = Nkind
(Get_Associated_Node
(N
)) then
15519 Aux_N2
: constant Node_Id
:= Get_Associated_Node
(N
);
15520 Orig_N2_Parent
: constant Node_Id
:=
15521 Original_Node
(Parent
(Aux_N2
));
15523 -- The parent of this identifier is a selected component
15524 -- which denotes a named number that was constant folded.
15525 -- Preserve the original name for ASIS and link the parent
15526 -- with its expanded name. The constant folding will be
15527 -- repeated in the instance.
15529 if Nkind
(Parent
(N
)) = N_Selected_Component
15530 and then Nkind_In
(Parent
(Aux_N2
), N_Integer_Literal
,
15532 and then Is_Entity_Name
(Orig_N2_Parent
)
15533 and then Ekind
(Entity
(Orig_N2_Parent
)) in Named_Kind
15534 and then Is_Global
(Entity
(Orig_N2_Parent
))
15537 Set_Associated_Node
15538 (Parent
(N
), Original_Node
(Parent
(N2
)));
15543 -- If this is a discriminant reference, always save it.
15544 -- It is used in the instance to find the corresponding
15545 -- discriminant positionally rather than by name.
15547 Set_Original_Discriminant
15548 (N
, Original_Discriminant
(Get_Associated_Node
(N
)));
15554 -- The analysis of the generic copy transformed the identifier
15555 -- into another construct. Propagate the changes to the template.
15558 N2
:= Get_Associated_Node
(N
);
15560 -- The identifier denotes a call to a parameterless function.
15561 -- Mark the node as resolved when the function is external.
15563 if Nkind
(N2
) = N_Function_Call
then
15564 E
:= Entity
(Name
(N2
));
15566 if Present
(E
) and then Is_Global
(E
) then
15567 Set_Etype
(N
, Etype
(N2
));
15569 Set_Associated_Node
(N
, Empty
);
15570 Set_Etype
(N
, Empty
);
15573 -- The identifier denotes a named number that was constant
15574 -- folded. Preserve the original name for ASIS and undo the
15575 -- constant folding which will be repeated in the instance.
15577 elsif Nkind_In
(N2
, N_Integer_Literal
, N_Real_Literal
)
15578 and then Is_Entity_Name
(Original_Node
(N2
))
15580 Set_Associated_Node
(N
, Original_Node
(N2
));
15583 -- The identifier resolved to a string literal. Propagate this
15584 -- information to the generic template.
15586 elsif Nkind
(N2
) = N_String_Literal
then
15587 Rewrite
(N
, New_Copy
(N2
));
15589 -- The identifier is rewritten as a dereference if it is the
15590 -- prefix of an implicit dereference. Preserve the original
15591 -- tree as the analysis of the instance will expand the node
15592 -- again, but preserve the resolved entity if it is global.
15594 elsif Nkind
(N2
) = N_Explicit_Dereference
then
15595 if Is_Entity_Name
(Prefix
(N2
))
15596 and then Present
(Entity
(Prefix
(N2
)))
15597 and then Is_Global
(Entity
(Prefix
(N2
)))
15599 Set_Associated_Node
(N
, Prefix
(N2
));
15601 elsif Nkind
(Prefix
(N2
)) = N_Function_Call
15602 and then Present
(Entity
(Name
(Prefix
(N2
))))
15603 and then Is_Global
(Entity
(Name
(Prefix
(N2
))))
15606 Make_Explicit_Dereference
(Loc
,
15608 Make_Function_Call
(Loc
,
15611 (Entity
(Name
(Prefix
(N2
))), Loc
))));
15614 Set_Associated_Node
(N
, Empty
);
15615 Set_Etype
(N
, Empty
);
15618 -- The subtype mark of a nominally unconstrained object is
15619 -- rewritten as a subtype indication using the bounds of the
15620 -- expression. Recover the original subtype mark.
15622 elsif Nkind
(N2
) = N_Subtype_Indication
15623 and then Is_Entity_Name
(Original_Node
(N2
))
15625 Set_Associated_Node
(N
, Original_Node
(N2
));
15629 end Save_References_In_Identifier
;
15631 ---------------------------------
15632 -- Save_References_In_Operator --
15633 ---------------------------------
15635 procedure Save_References_In_Operator
(N
: Node_Id
) is
15637 -- The node did not undergo a transformation
15639 if Nkind
(N
) = Nkind
(Get_Associated_Node
(N
)) then
15640 if Nkind
(N
) = N_Op_Concat
then
15641 Set_Is_Component_Left_Opnd
(N
,
15642 Is_Component_Left_Opnd
(Get_Associated_Node
(N
)));
15644 Set_Is_Component_Right_Opnd
(N
,
15645 Is_Component_Right_Opnd
(Get_Associated_Node
(N
)));
15650 -- The analysis of the generic copy transformed the operator into
15651 -- some other construct. Propagate the changes to the template if
15655 N2
:= Get_Associated_Node
(N
);
15657 -- The operator resoved to a function call
15659 if Nkind
(N2
) = N_Function_Call
then
15661 -- Add explicit qualifications in the generic template for
15662 -- all operands of universal type. This aids resolution by
15663 -- preserving the actual type of a literal or an attribute
15664 -- that yields a universal result.
15666 Qualify_Universal_Operands
(N
, N2
);
15668 E
:= Entity
(Name
(N2
));
15670 if Present
(E
) and then Is_Global
(E
) then
15671 Set_Etype
(N
, Etype
(N2
));
15673 Set_Associated_Node
(N
, Empty
);
15674 Set_Etype
(N
, Empty
);
15677 -- The operator was folded into a literal
15679 elsif Nkind_In
(N2
, N_Integer_Literal
,
15683 if Present
(Original_Node
(N2
))
15684 and then Nkind
(Original_Node
(N2
)) = Nkind
(N
)
15686 -- Operation was constant-folded. Whenever possible,
15687 -- recover semantic information from unfolded node,
15690 Set_Associated_Node
(N
, Original_Node
(N2
));
15692 if Nkind
(N
) = N_Op_Concat
then
15693 Set_Is_Component_Left_Opnd
(N
,
15694 Is_Component_Left_Opnd
(Get_Associated_Node
(N
)));
15695 Set_Is_Component_Right_Opnd
(N
,
15696 Is_Component_Right_Opnd
(Get_Associated_Node
(N
)));
15701 -- Propagate the constant folding back to the template
15704 Rewrite
(N
, New_Copy
(N2
));
15705 Set_Analyzed
(N
, False);
15708 -- The operator was folded into an enumeration literal. Retain
15709 -- the entity to avoid spurious ambiguities if it is overloaded
15710 -- at the point of instantiation or inlining.
15712 elsif Nkind
(N2
) = N_Identifier
15713 and then Ekind
(Entity
(N2
)) = E_Enumeration_Literal
15715 Rewrite
(N
, New_Copy
(N2
));
15716 Set_Analyzed
(N
, False);
15720 -- Complete the operands check if node has not been constant
15723 if Nkind
(N
) in N_Op
then
15724 Save_Entity_Descendants
(N
);
15726 end Save_References_In_Operator
;
15728 -------------------------------
15729 -- Save_References_In_Pragma --
15730 -------------------------------
15732 procedure Save_References_In_Pragma
(Prag
: Node_Id
) is
15734 Do_Save
: Boolean := True;
15736 use Atree
.Unchecked_Access
;
15737 -- This code section is part of implementing an untyped tree
15738 -- traversal, so it needs direct access to node fields.
15741 -- Do not save global references in pragmas generated from aspects
15742 -- because the pragmas will be regenerated at instantiation time.
15744 if From_Aspect_Specification
(Prag
) then
15747 -- The capture of global references within contract-related source
15748 -- pragmas associated with generic packages, subprograms or their
15749 -- respective bodies must be delayed due to timing of annotation
15750 -- analysis. Global references are still captured in routine
15751 -- Save_Global_References_In_Contract.
15753 elsif Is_Generic_Contract_Pragma
(Prag
) and then Prag
/= Templ
then
15754 if Is_Package_Contract_Annotation
(Prag
) then
15755 Context
:= Find_Related_Package_Or_Body
(Prag
);
15757 pragma Assert
(Is_Subprogram_Contract_Annotation
(Prag
));
15758 Context
:= Find_Related_Declaration_Or_Body
(Prag
);
15761 -- The use of Original_Node accounts for the case when the
15762 -- related context is generic template.
15764 if Requires_Delayed_Save
(Original_Node
(Context
)) then
15769 -- For all other cases, save all global references within the
15770 -- descendants, but skip the following semantic fields:
15772 -- Field1 - Next_Pragma
15773 -- Field3 - Corresponding_Aspect
15774 -- Field5 - Next_Rep_Item
15777 Save_Global_Descendant
(Field2
(Prag
));
15778 Save_Global_Descendant
(Field4
(Prag
));
15780 end Save_References_In_Pragma
;
15782 -- Start of processing for Save_References
15790 elsif Nkind_In
(N
, N_Aggregate
, N_Extension_Aggregate
) then
15791 Save_References_In_Aggregate
(N
);
15793 -- Character literals, operator symbols
15795 elsif Nkind_In
(N
, N_Character_Literal
, N_Operator_Symbol
) then
15796 Save_References_In_Char_Lit_Or_Op_Symbol
(N
);
15798 -- Defining identifiers
15800 elsif Nkind
(N
) in N_Entity
then
15805 elsif Nkind
(N
) = N_Identifier
then
15806 Save_References_In_Identifier
(N
);
15810 elsif Nkind
(N
) in N_Op
then
15811 Save_References_In_Operator
(N
);
15815 elsif Nkind
(N
) = N_Pragma
then
15816 Save_References_In_Pragma
(N
);
15819 Save_References_In_Descendants
(N
);
15822 -- Save all global references found within the aspect specifications
15823 -- of the related node.
15825 if Permits_Aspect_Specifications
(N
) and then Has_Aspects
(N
) then
15827 -- The capture of global references within aspects associated with
15828 -- generic packages, subprograms or their bodies must be delayed
15829 -- due to timing of annotation analysis. Global references are
15830 -- still captured in routine Save_Global_References_In_Contract.
15832 if Requires_Delayed_Save
(N
) then
15835 -- Otherwise save all global references within the aspects
15838 Save_Global_References_In_Aspects
(N
);
15841 end Save_References
;
15843 -- Start of processing for Save_Global_References
15846 Gen_Scope
:= Current_Scope
;
15848 -- If the generic unit is a child unit, references to entities in the
15849 -- parent are treated as local, because they will be resolved anew in
15850 -- the context of the instance of the parent.
15852 while Is_Child_Unit
(Gen_Scope
)
15853 and then Ekind
(Scope
(Gen_Scope
)) = E_Generic_Package
15855 Gen_Scope
:= Scope
(Gen_Scope
);
15858 Save_References
(Templ
);
15859 end Save_Global_References
;
15861 ---------------------------------------
15862 -- Save_Global_References_In_Aspects --
15863 ---------------------------------------
15865 procedure Save_Global_References_In_Aspects
(N
: Node_Id
) is
15870 Asp
:= First
(Aspect_Specifications
(N
));
15871 while Present
(Asp
) loop
15872 Expr
:= Expression
(Asp
);
15874 if Present
(Expr
) then
15875 Save_Global_References
(Expr
);
15880 end Save_Global_References_In_Aspects
;
15882 ------------------------------------------
15883 -- Set_Copied_Sloc_For_Inherited_Pragma --
15884 ------------------------------------------
15886 procedure Set_Copied_Sloc_For_Inherited_Pragma
15891 Create_Instantiation_Source
(N
, E
,
15892 Inlined_Body
=> False,
15893 Inherited_Pragma
=> True,
15894 Factor
=> S_Adjustment
);
15895 end Set_Copied_Sloc_For_Inherited_Pragma
;
15897 --------------------------------------
15898 -- Set_Copied_Sloc_For_Inlined_Body --
15899 --------------------------------------
15901 procedure Set_Copied_Sloc_For_Inlined_Body
(N
: Node_Id
; E
: Entity_Id
) is
15903 Create_Instantiation_Source
(N
, E
,
15904 Inlined_Body
=> True,
15905 Inherited_Pragma
=> False,
15906 Factor
=> S_Adjustment
);
15907 end Set_Copied_Sloc_For_Inlined_Body
;
15909 ---------------------
15910 -- Set_Instance_Of --
15911 ---------------------
15913 procedure Set_Instance_Of
(A
: Entity_Id
; B
: Entity_Id
) is
15915 Generic_Renamings
.Table
(Generic_Renamings
.Last
) := (A
, B
, Assoc_Null
);
15916 Generic_Renamings_HTable
.Set
(Generic_Renamings
.Last
);
15917 Generic_Renamings
.Increment_Last
;
15918 end Set_Instance_Of
;
15920 --------------------
15921 -- Set_Next_Assoc --
15922 --------------------
15924 procedure Set_Next_Assoc
(E
: Assoc_Ptr
; Next
: Assoc_Ptr
) is
15926 Generic_Renamings
.Table
(E
).Next_In_HTable
:= Next
;
15927 end Set_Next_Assoc
;
15929 -------------------
15930 -- Start_Generic --
15931 -------------------
15933 procedure Start_Generic
is
15935 -- ??? More things could be factored out in this routine.
15936 -- Should probably be done at a later stage.
15938 Generic_Flags
.Append
(Inside_A_Generic
);
15939 Inside_A_Generic
:= True;
15941 Expander_Mode_Save_And_Set
(False);
15944 ----------------------
15945 -- Set_Instance_Env --
15946 ----------------------
15948 -- WARNING: This routine manages SPARK regions
15950 procedure Set_Instance_Env
15951 (Gen_Unit
: Entity_Id
;
15952 Act_Unit
: Entity_Id
)
15954 Saved_AE
: constant Boolean := Assertions_Enabled
;
15955 Saved_CPL
: constant Node_Id
:= Check_Policy_List
;
15956 Saved_DEC
: constant Boolean := Dynamic_Elaboration_Checks
;
15957 Saved_SM
: constant SPARK_Mode_Type
:= SPARK_Mode
;
15958 Saved_SMP
: constant Node_Id
:= SPARK_Mode_Pragma
;
15961 -- Regardless of the current mode, predefined units are analyzed in the
15962 -- most current Ada mode, and earlier version Ada checks do not apply
15963 -- to predefined units. Nothing needs to be done for non-internal units.
15964 -- These are always analyzed in the current mode.
15966 if In_Internal_Unit
(Gen_Unit
) then
15968 -- The following call resets all configuration attributes to default
15969 -- or the xxx_Config versions of the attributes when the current sem
15970 -- unit is the main unit. At the same time, internal units must also
15971 -- inherit certain configuration attributes from their context. It
15972 -- is unclear what these two sets are.
15974 Set_Config_Switches
(True, Current_Sem_Unit
= Main_Unit
);
15976 -- Reinstall relevant configuration attributes of the context
15978 Assertions_Enabled
:= Saved_AE
;
15979 Check_Policy_List
:= Saved_CPL
;
15980 Dynamic_Elaboration_Checks
:= Saved_DEC
;
15982 Install_SPARK_Mode
(Saved_SM
, Saved_SMP
);
15985 Current_Instantiated_Parent
:=
15986 (Gen_Id
=> Gen_Unit
,
15987 Act_Id
=> Act_Unit
,
15988 Next_In_HTable
=> Assoc_Null
);
15989 end Set_Instance_Env
;
15995 procedure Switch_View
(T
: Entity_Id
) is
15996 BT
: constant Entity_Id
:= Base_Type
(T
);
15997 Priv_Elmt
: Elmt_Id
:= No_Elmt
;
15998 Priv_Sub
: Entity_Id
;
16001 -- T may be private but its base type may have been exchanged through
16002 -- some other occurrence, in which case there is nothing to switch
16003 -- besides T itself. Note that a private dependent subtype of a private
16004 -- type might not have been switched even if the base type has been,
16005 -- because of the last branch of Check_Private_View (see comment there).
16007 if not Is_Private_Type
(BT
) then
16008 Prepend_Elmt
(Full_View
(T
), Exchanged_Views
);
16009 Exchange_Declarations
(T
);
16013 Priv_Elmt
:= First_Elmt
(Private_Dependents
(BT
));
16015 if Present
(Full_View
(BT
)) then
16016 Prepend_Elmt
(Full_View
(BT
), Exchanged_Views
);
16017 Exchange_Declarations
(BT
);
16020 while Present
(Priv_Elmt
) loop
16021 Priv_Sub
:= (Node
(Priv_Elmt
));
16023 -- We avoid flipping the subtype if the Etype of its full view is
16024 -- private because this would result in a malformed subtype. This
16025 -- occurs when the Etype of the subtype full view is the full view of
16026 -- the base type (and since the base types were just switched, the
16027 -- subtype is pointing to the wrong view). This is currently the case
16028 -- for tagged record types, access types (maybe more?) and needs to
16029 -- be resolved. ???
16031 if Present
(Full_View
(Priv_Sub
))
16032 and then not Is_Private_Type
(Etype
(Full_View
(Priv_Sub
)))
16034 Prepend_Elmt
(Full_View
(Priv_Sub
), Exchanged_Views
);
16035 Exchange_Declarations
(Priv_Sub
);
16038 Next_Elmt
(Priv_Elmt
);
16046 function True_Parent
(N
: Node_Id
) return Node_Id
is
16048 if Nkind
(Parent
(N
)) = N_Subunit
then
16049 return Parent
(Corresponding_Stub
(Parent
(N
)));
16055 -----------------------------
16056 -- Valid_Default_Attribute --
16057 -----------------------------
16059 procedure Valid_Default_Attribute
(Nam
: Entity_Id
; Def
: Node_Id
) is
16060 Attr_Id
: constant Attribute_Id
:=
16061 Get_Attribute_Id
(Attribute_Name
(Def
));
16062 T
: constant Entity_Id
:= Entity
(Prefix
(Def
));
16063 Is_Fun
: constant Boolean := (Ekind
(Nam
) = E_Function
);
16069 if No
(T
) or else T
= Any_Id
then
16074 F
:= First_Formal
(Nam
);
16075 while Present
(F
) loop
16076 Num_F
:= Num_F
+ 1;
16081 when Attribute_Adjacent
16082 | Attribute_Ceiling
16083 | Attribute_Copy_Sign
16085 | Attribute_Fraction
16086 | Attribute_Machine
16088 | Attribute_Remainder
16089 | Attribute_Rounding
16090 | Attribute_Unbiased_Rounding
16094 and then Is_Floating_Point_Type
(T
);
16096 when Attribute_Image
16100 | Attribute_Wide_Image
16101 | Attribute_Wide_Value
16103 OK
:= Is_Fun
and then Num_F
= 1 and then Is_Scalar_Type
(T
);
16108 OK
:= Is_Fun
and then Num_F
= 2 and then Is_Scalar_Type
(T
);
16110 when Attribute_Input
=>
16111 OK
:= (Is_Fun
and then Num_F
= 1);
16113 when Attribute_Output
16117 OK
:= not Is_Fun
and then Num_F
= 2;
16125 ("attribute reference has wrong profile for subprogram", Def
);
16127 end Valid_Default_Attribute
;