[Ada] Minor reformatting
[official-gcc.git] / gcc / ada / sem_ch12.adb
blob3aa49755b39fcaa4f210284464ffaaaf8c874a93
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 1 2 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2019, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
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;
38 with Lib; use Lib;
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;
44 with Opt; use Opt;
45 with Rident; use Rident;
46 with Restrict; use Restrict;
47 with Rtsfind; use Rtsfind;
48 with Sem; use Sem;
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;
75 with Table;
76 with Tbuild; use Tbuild;
77 with Uintp; use Uintp;
78 with Urealp; use Urealp;
79 with Warnsw; use Warnsw;
81 with GNAT.HTable;
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 |
105 -- | copy | | unit |
106 -- | |==============>| |
107 -- |___________| global |__________|
108 -- references | | |
109 -- | | |
110 -- .-----|--|.
111 -- | .-----|---.
112 -- | | .----------.
113 -- | | | generic |
114 -- |__| | |
115 -- |__| instance |
116 -- |__________|
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
131 -- actuals.
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
138 -- package.
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.
165 -- generic ...
166 -- package Outer is
167 -- ...
168 -- type Semi_Global is ... -- global to inner.
170 -- generic ... -- 1
171 -- procedure inner (X1 : Global; X2 : Semi_Global);
173 -- procedure in2 is new inner (...); -- 4
174 -- end Outer;
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
216 -- with B; with A;
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
253 -- as annotations:
255 -- package subprogram [body]
256 -- Abstract_State Contract_Cases
257 -- Initial_Condition Depends
258 -- Initializes Extensions_Visible
259 -- Global
260 -- package body Post
261 -- Refined_State Post_Class
262 -- Postcondition
263 -- Pre
264 -- Pre_Class
265 -- Precondition
266 -- Refined_Depends
267 -- Refined_Global
268 -- Refined_Post
269 -- Test_Case
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
290 -- are processed.
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
306 -- unit 1.2).
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
317 -- is delayed.
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
351 -- by routine
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;
417 Def : Node_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
426 (N : Node_Id;
427 T : Entity_Id;
428 Def : Node_Id);
430 procedure Analyze_Formal_Derived_Type
431 (N : Node_Id;
432 T : Entity_Id;
433 Def : Node_Id);
435 procedure Analyze_Formal_Interface_Type
436 (N : Node_Id;
437 T : Entity_Id;
438 Def : Node_Id);
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
444 -- generic.
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
456 (N : Node_Id;
457 T : Entity_Id;
458 Def : Node_Id);
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
471 (I_Node : Node_Id;
472 Formals : List_Id;
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
481 (N : Node_Id;
482 K : Entity_Kind);
484 procedure Build_Instance_Compilation_Unit_Nodes
485 (N : Node_Id;
486 Act_Body : Node_Id;
487 Act_Decl : Node_Id);
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
529 (N : Node_Id;
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
544 -- a box.
546 function Contains_Instance_Of
547 (Inner : Entity_Id;
548 Outer : Entity_Id;
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
558 (Pack : Entity_Id;
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
579 (Typ : Entity_Id;
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;
597 Gen_Body : 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
614 -- not repeated.
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
637 -- instantiation.
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.
672 procedure Init_Env;
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
678 (N : Node_Id;
679 Gen_Unit : Entity_Id;
680 Act_Decl : Node_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
686 (N : Node_Id;
687 F_Node : Node_Id);
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
692 -- contains N.
694 procedure Install_Body
695 (Act_Body : Node_Id;
696 N : Node_Id;
697 Gen_Body : Node_Id;
698 Gen_Decl : Node_Id);
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;
719 Gen_T : Entity_Id;
720 Act_T : Entity_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
740 (Formal : Node_Id;
741 Actual : Node_Id;
742 Analyzed_Formal : Node_Id) return List_Id;
744 function Instantiate_Type
745 (Formal : Node_Id;
746 Actual : Node_Id;
747 Analyzed_Formal : Node_Id;
748 Actual_Decls : List_Id) return List_Id;
750 function Instantiate_Formal_Subprogram
751 (Formal : Node_Id;
752 Actual : Node_Id;
753 Analyzed_Formal : Node_Id) return Node_Id;
755 function Instantiate_Formal_Package
756 (Formal : Node_Id;
757 Actual : Node_Id;
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
776 -- created.
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
788 (N : Node_Id;
789 Spec : Node_Id;
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
814 (Out_Of : Entity_Id;
815 After : Node_Id;
816 L : List_Id);
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
821 -- neutral.
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
842 -- set to No_Elist.
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
860 -- parent of node.
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;
879 type Assoc is record
880 Gen_Id : Entity_Id;
881 Act_Id : Entity_Id;
882 Next_In_HTable : Assoc_Ptr;
883 end record;
885 package Generic_Renamings is new Table.Table
886 (Table_Component_Type => Assoc,
887 Table_Index_Type => Assoc_Ptr,
888 Table_Low_Bound => 0,
889 Table_Initial => 10,
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,
910 Element => Assoc,
911 Elmt_Ptr => Assoc_Ptr,
912 Null_Ptr => Assoc_Null,
913 Set_Next => Set_Next_Assoc,
914 Next => Next_Assoc,
915 Key => Entity_Id,
916 Get_Key => Get_Gen_Id,
917 Hash => Hash,
918 Equal => "=");
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;
959 end record;
961 package Instance_Envs is new Table.Table (
962 Table_Component_Type => Instance_Env,
963 Table_Index_Type => Int,
964 Table_Low_Bound => 0,
965 Table_Initial => 32,
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
975 -- renamings.
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
1023 begin
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
1033 begin
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,
1042 Inst_Node => Inst,
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
1053 begin
1054 Adjust_Instantiation_Sloc (N, S_Adjustment);
1055 end Adjust_Inherited_Pragma_Sloc;
1057 --------------------------
1058 -- Analyze_Associations --
1059 --------------------------
1061 function Analyze_Associations
1062 (I_Node : Node_Id;
1063 Formals : List_Id;
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));
1072 Actuals : List_Id;
1073 Actual : Node_Id;
1074 Analyzed_Formal : Node_Id;
1075 First_Named : Node_Id := Empty;
1076 Formal : Node_Id;
1077 Match : Node_Id;
1078 Named : Node_Id;
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
1118 (F : Entity_Id;
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;
1161 begin
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
1166 and then
1167 Chars (Defining_Unit_Name (Specification (Formal))) =
1168 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1169 then
1170 if Present (Found_Assoc) then
1171 Error_Msg_N
1172 ("named association not allowed for overloaded formal",
1173 Found_Assoc);
1175 else
1176 Error_Msg_N
1177 ("named association not allowed for overloaded formal",
1178 Others_Choice);
1179 end if;
1181 Abandon_Instantiation (Instantiation_Node);
1182 end if;
1184 Next (Temp_Formal);
1185 end loop;
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);
1195 Elem : Elmt_Id;
1196 Formal : Node_Id;
1197 Op : Entity_Id;
1199 begin
1200 -- Locate primitive operations of the type that are arithmetic
1201 -- operations.
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)) =
1217 Chars (Node (Elem))
1218 then
1219 exit;
1221 elsif Nkind (Formal) = N_Formal_Package_Declaration then
1222 declare
1223 Assoc : Node_Id;
1224 Ent : Entity_Id;
1226 begin
1227 -- Locate corresponding actual, and check whether it
1228 -- includes a fixed-point type.
1230 Assoc := First (Assoc_List);
1231 while Present (Assoc) loop
1232 exit when
1233 Nkind (Assoc) = N_Package_Renaming_Declaration
1234 and then Chars (Defining_Unit_Name (Assoc)) =
1235 Chars (Defining_Identifier (Formal));
1237 Next (Assoc);
1238 end loop;
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))
1252 then
1253 return;
1254 end if;
1256 Next_Entity (Ent);
1257 end loop;
1258 end if;
1259 end;
1260 end if;
1262 Next (Formal);
1263 end loop;
1265 if No (Formal) then
1266 Error_Msg_Sloc := Sloc (Node (Elem));
1267 Error_Msg_NE
1268 ("?instance uses predefined operation, not primitive "
1269 & "operation&#", Actual, Node (Elem));
1270 end if;
1271 end if;
1273 Next_Elmt (Elem);
1274 end loop;
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
1290 begin
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))
1295 then
1296 return False;
1298 -- An incomplete type is never fully defined
1300 elsif Is_Incomplete_Type (Typ) then
1301 return False;
1303 -- All other types are fully defined
1305 else
1306 return True;
1307 end if;
1308 end Is_Fully_Defined_Type;
1310 -- Local declarations
1312 Param : Entity_Id;
1314 -- Start of processing for Has_Fully_Defined_Profile
1316 begin
1317 -- Check the parameters
1319 Param := First_Formal (Subp);
1320 while Present (Param) loop
1321 if not Is_Fully_Defined_Type (Etype (Param)) then
1322 return False;
1323 end if;
1325 Next_Formal (Param);
1326 end loop;
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
1338 (F : Entity_Id;
1339 A_F : Entity_Id) return Node_Id
1341 Prev : Node_Id;
1342 Act : Node_Id;
1344 begin
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;
1351 Act := 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;
1359 Next (Actual);
1361 -- Otherwise scan list of named actuals to find the one with the
1362 -- desired name. All remaining actuals have explicit names.
1364 else
1365 Is_Named_Assoc := True;
1366 Found_Assoc := Empty;
1367 Act := Empty;
1368 Prev := Empty;
1370 while Present (Actual) loop
1371 if Nkind (Actual) = N_Others_Choice then
1372 Found_Assoc := Empty;
1373 Act := 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;
1383 exit;
1384 end if;
1386 Prev := Actual;
1387 Next (Actual);
1388 end loop;
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
1399 Next (First_Named);
1401 elsif Present (Actual) then
1402 Insert_Before (First_Named, Remove_Next (Prev));
1403 end if;
1405 Actual := First_Named;
1406 end if;
1408 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1409 Set_Used_As_Generic_Actual (Entity (Act));
1410 end if;
1412 return Act;
1413 end Matching_Actual;
1415 ------------------------------
1416 -- Partial_Parameterization --
1417 ------------------------------
1419 function Partial_Parameterization return Boolean is
1420 begin
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);
1432 Decl : Node_Id;
1433 Default : Node_Id;
1434 Id : Entity_Id;
1436 begin
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);
1446 else
1447 Set_Defining_Identifier (Decl, Id);
1448 end if;
1450 Append (Decl, Assoc_List);
1452 if No (Found_Assoc) then
1453 Default :=
1454 Make_Generic_Association (Loc,
1455 Selector_Name =>
1456 New_Occurrence_Of (Id, Loc),
1457 Explicit_Generic_Actual_Parameter => Empty);
1458 Set_Box_Present (Default);
1459 Append (Default, Default_Formals);
1460 end if;
1461 end Process_Default;
1463 ---------------------------------
1464 -- Renames_Standard_Subprogram --
1465 ---------------------------------
1467 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1468 Id : Entity_Id;
1470 begin
1471 Id := Alias (Subp);
1472 while Present (Id) loop
1473 if Scope (Id) = Standard_Standard then
1474 return True;
1475 end if;
1477 Id := Alias (Id);
1478 end loop;
1480 return False;
1481 end Renames_Standard_Subprogram;
1483 -------------------------
1484 -- Set_Analyzed_Formal --
1485 -------------------------
1487 procedure Set_Analyzed_Formal is
1488 Kind : Node_Kind;
1490 begin
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
1497 and then
1498 Chars
1499 (Defining_Unit_Name (Specification (Formal))) =
1500 Chars
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
1509 | N_Use_Type_Clause
1511 exit;
1513 when others =>
1515 -- Skip freeze nodes, and nodes inserted to replace
1516 -- unrecognized pragmas.
1518 exit when
1519 Kind not in N_Formal_Subprogram_Declaration
1520 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1521 N_Freeze_Entity,
1522 N_Null_Statement,
1523 N_Itype_Reference)
1524 and then Chars (Defining_Identifier (Formal)) =
1525 Chars (Defining_Identifier (Analyzed_Formal));
1526 end case;
1528 Next (Analyzed_Formal);
1529 end loop;
1530 end Set_Analyzed_Formal;
1532 -- Start of processing for Analyze_Associations
1534 begin
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);
1550 end if;
1552 -- This subprogram is used both for formal packages and for
1553 -- instantiations. For the latter, associations must all be
1554 -- explicit.
1556 if Nkind (I_Node) /= N_Formal_Package_Declaration
1557 and then Comes_From_Source (I_Node)
1558 then
1559 Error_Msg_N
1560 ("others association not allowed in an instance",
1561 Actual);
1562 end if;
1564 -- In any case, nothing to do after the others association
1566 exit;
1568 elsif Box_Present (Actual)
1569 and then Comes_From_Source (I_Node)
1570 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1571 then
1572 Error_Msg_N
1573 ("box association not allowed in an instance", Actual);
1574 end if;
1576 Next (Actual);
1577 end loop;
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))
1586 loop
1587 Num_Actuals := Num_Actuals + 1;
1588 Next (First_Named);
1589 end loop;
1590 end if;
1592 Named := First_Named;
1593 while Present (Named) loop
1594 if Nkind (Named) /= N_Others_Choice
1595 and then No (Selector_Name (Named))
1596 then
1597 Error_Msg_N ("invalid positional actual after named one", Named);
1598 Abandon_Instantiation (Named);
1599 end if;
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))
1609 then
1610 Num_Actuals := Num_Actuals + 1;
1611 end if;
1613 Next (Named);
1614 end loop;
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
1625 else
1626 Actual := Empty;
1627 end if;
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 =>
1635 Match :=
1636 Matching_Actual
1637 (Defining_Identifier (Formal),
1638 Defining_Identifier (Analyzed_Formal));
1640 if No (Match) and then Partial_Parameterization then
1641 Process_Default (Formal);
1643 else
1644 Append_List
1645 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1646 Assoc_List);
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),
1658 Selector_Name =>
1659 New_Occurrence_Of
1660 (Defining_Identifier (Formal), Sloc (I_Node)),
1661 Explicit_Generic_Actual_Parameter =>
1662 New_Copy_Tree (Default_Expression (Formal))));
1663 end if;
1664 end if;
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))
1671 and then Nkind
1672 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1673 = N_Expression_Function
1674 then
1675 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1676 end if;
1678 when N_Formal_Type_Declaration =>
1679 Match :=
1680 Matching_Actual
1681 (Defining_Identifier (Formal),
1682 Defining_Identifier (Analyzed_Formal));
1684 if No (Match) then
1685 if Partial_Parameterization then
1686 Process_Default (Formal);
1688 else
1689 Error_Msg_Sloc := Sloc (Gen_Unit);
1690 Error_Msg_NE
1691 ("missing actual&",
1692 Instantiation_Node, Defining_Identifier (Formal));
1693 Error_Msg_NE
1694 ("\in instantiation of & declared#",
1695 Instantiation_Node, Gen_Unit);
1696 Abandon_Instantiation (Instantiation_Node);
1697 end if;
1699 else
1700 Analyze (Match);
1701 Append_List
1702 (Instantiate_Type
1703 (Formal, Match, Analyzed_Formal, Assoc_List),
1704 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))
1715 then
1716 Check_Fixed_Point_Actual (Match);
1717 end if;
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
1724 or else
1725 (Ada_Version >= Ada_2012
1726 and then
1727 Ekind (Defining_Identifier (Analyzed_Formal)) =
1728 E_Incomplete_Type)
1729 then
1730 null;
1732 else
1733 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1734 end if;
1735 end if;
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
1749 and then
1750 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1751 N_Access_To_Object_Definition
1752 and then Present (Match)
1753 then
1754 declare
1755 Formal_Ent : constant Entity_Id :=
1756 Defining_Identifier (Analyzed_Formal);
1757 begin
1758 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1759 = Is_Remote_Types (Formal_Ent)
1760 then
1761 -- Remoteness of formal and actual match
1763 null;
1765 elsif Is_Remote_Types (Formal_Ent) then
1767 -- Remote formal, non-remote actual
1769 Error_Msg_NE
1770 ("actual for& must be remote", Match, Formal_Ent);
1772 else
1773 -- Non-remote formal, remote actual
1775 Error_Msg_NE
1776 ("actual for& may not be remote",
1777 Match, Formal_Ent);
1778 end if;
1779 end;
1780 end if;
1782 when N_Formal_Subprogram_Declaration =>
1783 Match :=
1784 Matching_Actual
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.
1793 if Present (Match)
1794 and then Is_Named_Assoc
1795 and then Comes_From_Source (Found_Assoc)
1796 then
1797 Check_Overloaded_Formal_Subprogram (Formal);
1798 end if;
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);
1809 end if;
1811 else
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.
1827 and then
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))
1839 then
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);
1845 end if;
1846 end if;
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
1853 declare
1854 Subp : constant Entity_Id :=
1855 Defining_Unit_Name
1856 (Specification (Last (Assoc_List)));
1858 begin
1859 Append_To (Default_Actuals,
1860 Make_Generic_Association (Sloc (I_Node),
1861 Selector_Name =>
1862 New_Occurrence_Of (Subp, Sloc (I_Node)),
1863 Explicit_Generic_Actual_Parameter =>
1864 New_Occurrence_Of (Subp, Sloc (I_Node))));
1865 end;
1866 end if;
1868 when N_Formal_Package_Declaration =>
1869 Match :=
1870 Matching_Actual
1871 (Defining_Identifier (Formal),
1872 Defining_Identifier (Original_Node (Analyzed_Formal)));
1874 if No (Match) then
1875 if Partial_Parameterization then
1876 Process_Default (Formal);
1878 else
1879 Error_Msg_Sloc := Sloc (Gen_Unit);
1880 Error_Msg_NE
1881 ("missing actual&",
1882 Instantiation_Node, Defining_Identifier (Formal));
1883 Error_Msg_NE
1884 ("\in instantiation of & declared#",
1885 Instantiation_Node, Gen_Unit);
1887 Abandon_Instantiation (Instantiation_Node);
1888 end if;
1890 else
1891 Analyze (Match);
1892 Append_List
1893 (Instantiate_Formal_Package
1894 (Formal, Match, Analyzed_Formal),
1895 Assoc_List);
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
1902 -- body.
1904 Explicit_Freeze_Check : declare
1905 Actual : constant Entity_Id := Entity (Match);
1906 Gen_Par : Entity_Id;
1908 Needs_Freezing : Boolean;
1909 S : Entity_Id;
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
1919 -- actual.
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));
1929 Par : Entity_Id;
1931 begin
1932 Par := Empty;
1934 if Nkind (Parent (Actual)) = N_Package_Specification
1935 then
1936 Par := Scope (Generic_Parent (Parent (Actual)));
1938 if Is_Generic_Instance (Par) then
1939 null;
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)) =
1950 E_Generic_Package
1951 then
1952 if Nkind (Inst) = N_Package_Instantiation
1953 and then Nkind (Name (Inst)) =
1954 N_Expanded_Name
1955 then
1956 -- Retrieve entity of parent instance
1958 Par := Entity (Prefix (Name (Inst)));
1959 end if;
1961 else
1962 Par := Empty;
1963 end if;
1964 end if;
1966 if Present (Par)
1967 and then Is_Generic_Instance (Par)
1968 and then Scope (Par) = Current_Scope
1969 and then
1970 (No (Freeze_Node (Par))
1971 or else
1972 not Is_List_Member (Freeze_Node (Par)))
1973 then
1974 Set_Has_Delayed_Freeze (Par);
1975 Append_Elmt (Par, Actuals_To_Freeze);
1976 end if;
1977 end Check_Generic_Parent;
1979 -- Start of processing for Explicit_Freeze_Check
1981 begin
1982 if Present (Renamed_Entity (Actual)) then
1983 Gen_Par :=
1984 Generic_Parent (Specification
1985 (Unit_Declaration_Node
1986 (Renamed_Entity (Actual))));
1987 else
1988 Gen_Par :=
1989 Generic_Parent (Specification
1990 (Unit_Declaration_Node (Actual)));
1991 end if;
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)
1997 or else
1998 (Present (Renamed_Entity (Actual))
1999 and then
2000 not In_Same_Source_Unit
2001 (I_Node, (Renamed_Entity (Actual))))
2002 then
2003 null;
2005 else
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;
2013 S := Current_Scope;
2014 while Present (S) loop
2015 if Ekind_In (S, E_Block,
2016 E_Function,
2017 E_Loop,
2018 E_Procedure)
2019 then
2020 Needs_Freezing := False;
2021 exit;
2022 end if;
2024 S := Scope (S);
2025 end loop;
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
2036 then
2037 Set_Has_Delayed_Freeze
2038 (Renamed_Entity (Actual));
2039 Append_Elmt
2040 (Renamed_Entity (Actual),
2041 Actuals_To_Freeze);
2042 else
2043 Set_Has_Delayed_Freeze (Actual);
2044 Append_Elmt (Actual, Actuals_To_Freeze);
2045 end if;
2046 end if;
2047 end if;
2048 end Explicit_Freeze_Check;
2049 end if;
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
2057 | N_Use_Type_Clause
2059 if Nkind (Original_Node (I_Node)) =
2060 N_Formal_Package_Declaration
2061 then
2062 Append (New_Copy_Tree (Formal), Assoc_List);
2063 else
2064 Remove (Formal);
2065 Append (Formal, Assoc_List);
2066 end if;
2068 when others =>
2069 raise Program_Error;
2070 end case;
2072 Formal := Saved_Formal;
2073 Next_Non_Pragma (Analyzed_Formal);
2074 end loop;
2076 if Num_Actuals > Num_Matched then
2077 Error_Msg_Sloc := Sloc (Gen_Unit);
2079 if Present (Selector_Name (Actual)) then
2080 Error_Msg_NE
2081 ("unmatched actual &", Actual, Selector_Name (Actual));
2082 Error_Msg_NE
2083 ("\in instantiation of & declared#", Actual, Gen_Unit);
2084 else
2085 Error_Msg_NE
2086 ("unmatched actual in instantiation of & declared#",
2087 Actual, Gen_Unit);
2088 end if;
2089 end if;
2091 elsif Present (Actuals) then
2092 Error_Msg_N
2093 ("too many actuals in generic instantiation", Instantiation_Node);
2094 end if;
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.
2100 declare
2101 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2102 begin
2103 while Present (Elmt) loop
2104 Freeze_Before (I_Node, Node (Elmt));
2105 Next_Elmt (Elmt);
2106 end loop;
2107 end;
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
2114 declare
2115 Default : Node_Id;
2117 begin
2118 Default := First (Default_Actuals);
2119 while Present (Default) loop
2120 Mark_Rewrite_Insertion (Default);
2121 Next (Default);
2122 end loop;
2124 if No (Actuals) then
2125 Set_Generic_Associations (I_Node, Default_Actuals);
2126 else
2127 Append_List_To (Actuals, Default_Actuals);
2128 end if;
2129 end;
2130 end if;
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
2134 -- Others_Choice.
2136 if not Is_Empty_List (Default_Formals) then
2137 Append_List (Default_Formals, Formals);
2138 end if;
2140 return Assoc_List;
2141 end Analyze_Associations;
2143 -------------------------------
2144 -- Analyze_Formal_Array_Type --
2145 -------------------------------
2147 procedure Analyze_Formal_Array_Type
2148 (T : in out Entity_Id;
2149 Def : Node_Id)
2151 DSS : Node_Id;
2153 begin
2154 -- Treated like a non-generic array declaration, with additional
2155 -- semantic checks.
2157 Enter_Name (T);
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,
2163 N_Range,
2164 N_Attribute_Reference)
2165 then
2166 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2167 end if;
2169 Next (DSS);
2170 end loop;
2171 end if;
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)))
2178 then
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
2189 then
2190 Error_Msg_N
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)));
2194 end if;
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
2212 (T : Entity_Id;
2213 Def : Node_Id)
2215 Loc : constant Source_Ptr := Sloc (Def);
2217 Base : constant Entity_Id :=
2218 New_Internal_Entity
2219 (E_Decimal_Fixed_Point_Type,
2220 Current_Scope,
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);
2236 begin
2237 Set_Etype (Bound, Universal_Real);
2238 return Bound;
2239 end Make_Dummy_Bound;
2241 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2243 begin
2244 Enter_Name (T);
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,
2254 Make_Range (Loc,
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
2280 (N : Node_Id;
2281 T : Entity_Id;
2282 Def : Node_Id)
2284 Loc : constant Source_Ptr := Sloc (Def);
2286 begin
2287 -- Rewrite as a type declaration of a derived type. This ensures that
2288 -- the interface list and primitive operations are properly captured.
2290 Rewrite (N,
2291 Make_Full_Type_Declaration (Loc,
2292 Defining_Identifier => T,
2293 Type_Definition => Def));
2294 Analyze (N);
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
2303 (N : Node_Id;
2304 T : Entity_Id;
2305 Def : Node_Id)
2307 Loc : constant Source_Ptr := Sloc (Def);
2308 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2309 New_N : Node_Id;
2311 begin
2312 Set_Is_Generic_Type (T);
2314 if Private_Present (Def) then
2315 New_N :=
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));
2327 else
2328 New_N :=
2329 Make_Full_Type_Declaration (Loc,
2330 Defining_Identifier => T,
2331 Discriminant_Specifications =>
2332 Discriminant_Specifications (Parent (T)),
2333 Type_Definition =>
2334 Make_Derived_Type_Definition (Loc,
2335 Subtype_Indication => Subtype_Mark (Def)));
2337 Set_Abstract_Present
2338 (Type_Definition (New_N), Abstract_Present (Def));
2339 Set_Limited_Present
2340 (Type_Definition (New_N), Limited_Present (Def));
2341 end if;
2343 Rewrite (N, New_N);
2344 Analyze (N);
2346 if Unk_Disc then
2347 if not Is_Composite_Type (T) then
2348 Error_Msg_N
2349 ("unknown discriminants not allowed for elementary types", N);
2350 else
2351 Set_Has_Unknown_Discriminants (T);
2352 Set_Is_Constrained (T, False);
2353 end if;
2354 end if;
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
2369 -- generic unit.
2371 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2372 Loc : constant Source_Ptr := Sloc (Def);
2373 Lo : Node_Id;
2374 Hi : Node_Id;
2376 Base : constant Entity_Id :=
2377 New_Internal_Entity
2378 (E_Floating_Point_Type, Current_Scope,
2379 Sloc (Defining_Identifier (Parent (Def))), 'G');
2381 begin
2382 Enter_Name (T);
2383 Set_Ekind (T, E_Enumeration_Subtype);
2384 Set_Etype (T, Base);
2385 Init_Size (T, 8);
2386 Init_Alignment (T);
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.
2395 Lo :=
2396 Make_Attribute_Reference (Loc,
2397 Attribute_Name => Name_First,
2398 Prefix => New_Occurrence_Of (T, Loc));
2399 Set_Etype (Lo, T);
2401 Hi :=
2402 Make_Attribute_Reference (Loc,
2403 Attribute_Name => Name_Last,
2404 Prefix => New_Occurrence_Of (T, Loc));
2405 Set_Etype (Hi, T);
2407 Set_Scalar_Range (T,
2408 Make_Range (Loc,
2409 Low_Bound => Lo,
2410 High_Bound => Hi));
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 :=
2427 New_Internal_Entity
2428 (E_Floating_Point_Type, Current_Scope,
2429 Sloc (Defining_Identifier (Parent (Def))), 'G');
2431 begin
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.
2437 Enter_Name (T);
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
2462 (N : Node_Id;
2463 T : Entity_Id;
2464 Def : Node_Id)
2466 Loc : constant Source_Ptr := Sloc (N);
2467 New_N : Node_Id;
2469 begin
2470 New_N :=
2471 Make_Full_Type_Declaration (Loc,
2472 Defining_Identifier => T,
2473 Type_Definition => Def);
2475 Rewrite (N, New_N);
2476 Analyze (N);
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
2485 begin
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);
2502 K : Entity_Kind;
2503 T : Node_Id;
2505 begin
2506 Enter_Name (Id);
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);
2515 end if;
2517 else
2518 K := E_Generic_In_Parameter;
2519 end if;
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
2529 Error_Msg_N
2530 ("null exclusion can only apply to an access type", N);
2532 elsif Can_Never_Be_Null (T) then
2533 Error_Msg_NE
2534 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2535 end if;
2536 end if;
2538 -- Ada 2005 (AI-423): Formal object with an access definition
2540 else
2541 Check_Access_Definition (N);
2542 T := Access_Definition
2543 (Related_Nod => N,
2544 N => Access_Definition (N));
2545 end if;
2547 if Ekind (T) = E_Incomplete_Type then
2548 declare
2549 Error_Node : Node_Id;
2551 begin
2552 if Present (Subtype_Mark (N)) then
2553 Error_Node := Subtype_Mark (N);
2554 else
2555 Check_Access_Definition (N);
2556 Error_Node := Access_Definition (N);
2557 end if;
2559 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2560 end;
2561 end if;
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
2568 Error_Msg_N
2569 ("generic formal of mode IN must not be of limited type", N);
2570 Explain_Limited_Type (T, N);
2571 end if;
2573 if Is_Abstract_Type (T) then
2574 Error_Msg_N
2575 ("generic formal of mode IN must not be of abstract type", N);
2576 end if;
2578 if Present (E) then
2579 Preanalyze_Spec_Expression (E, T);
2581 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2582 Error_Msg_N
2583 ("initialization not allowed for limited types", E);
2584 Explain_Limited_Type (T, E);
2585 end if;
2586 end if;
2588 Set_Ekind (Id, K);
2589 Set_Etype (Id, T);
2591 -- Case of generic IN OUT parameter
2593 else
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.
2598 Set_Ekind (Id, K);
2599 Set_Etype (Id, T);
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))
2603 then
2604 declare
2605 Non_Freezing_Ref : constant Node_Id :=
2606 New_Occurrence_Of (Id, Sloc (Id));
2607 Decl : Node_Id;
2609 begin
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));
2616 end;
2617 else
2618 Set_Actual_Subtype (Id, T);
2619 end if;
2621 if Present (E) then
2622 Error_Msg_N
2623 ("initialization not allowed for `IN OUT` formals", N);
2624 end if;
2625 end if;
2627 if Has_Aspects (N) then
2628 Analyze_Aspect_Specifications (N, Id);
2629 end if;
2630 end Analyze_Formal_Object_Declaration;
2632 ----------------------------------------------
2633 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2634 ----------------------------------------------
2636 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2637 (T : Entity_Id;
2638 Def : Node_Id)
2640 Loc : constant Source_Ptr := Sloc (Def);
2641 Base : constant Entity_Id :=
2642 New_Internal_Entity
2643 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2644 Sloc (Defining_Identifier (Parent (Def))), 'G');
2646 begin
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.
2650 Enter_Name (T);
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,
2658 Make_Range (Loc,
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);
2683 Formal : Entity_Id;
2684 Gen_Decl : Node_Id;
2685 Gen_Unit : Entity_Id;
2686 Renaming : Node_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
2705 Decls : List_Id;
2706 Pack_Decl : Node_Id;
2708 begin
2709 -- Within the formal, the name of the generic package is a renaming
2710 -- of the formal (as for a regular instantiation).
2712 Pack_Decl :=
2713 Make_Package_Declaration (Loc,
2714 Specification =>
2715 Copy_Generic_Node
2716 (Specification (Original_Node (Gen_Decl)),
2717 Empty, Instantiating => True));
2719 Renaming :=
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)
2727 then
2728 Error_Msg_NE
2729 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2730 end if;
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.
2741 if Box_Present (N)
2742 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2743 then
2744 declare
2745 Formal_Decl : Node_Id;
2747 begin
2748 -- TBA : for a formal package, need to recurse ???
2750 Decls := New_List;
2751 Formal_Decl :=
2752 First
2753 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2754 while Present (Formal_Decl) loop
2755 Append_To
2756 (Decls,
2757 Copy_Generic_Node
2758 (Formal_Decl, Empty, Instantiating => True));
2759 Next (Formal_Decl);
2760 end loop;
2761 end;
2763 -- If generic associations are present, use Analyze_Associations to
2764 -- create the proper renaming declarations.
2766 else
2767 declare
2768 Act_Tree : constant Node_Id :=
2769 Copy_Generic_Node
2770 (Original_Node (Gen_Decl), Empty,
2771 Instantiating => True);
2773 begin
2774 Generic_Renamings.Set_Last (0);
2775 Generic_Renamings_HTable.Reset;
2776 Instantiation_Node := N;
2778 Decls :=
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);
2785 end;
2786 end if;
2788 Append (Renaming, To => Decls);
2790 -- Add generated declarations ahead of local declarations in
2791 -- the package.
2793 if No (Visible_Declarations (Specification (Pack_Decl))) then
2794 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2795 else
2796 Insert_List_Before
2797 (First (Visible_Declarations (Specification (Pack_Decl))),
2798 Decls);
2799 end if;
2801 return Pack_Decl;
2802 end Build_Local_Package;
2804 -- Local variables
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;
2810 New_N : Node_Id;
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
2817 begin
2818 Check_Text_IO_Special_Unit (Gen_Id);
2820 Init_Env;
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);
2834 end if;
2836 Gen_Unit := Renamed_Object (Gen_Unit);
2837 end if;
2839 if Ekind (Gen_Unit) /= E_Generic_Package then
2840 Error_Msg_N ("expect generic package name", Gen_Id);
2841 Restore_Env;
2842 goto Leave;
2844 elsif Gen_Unit = Current_Scope then
2845 Error_Msg_N
2846 ("generic package cannot be used as a formal package of itself",
2847 Gen_Id);
2848 Restore_Env;
2849 goto Leave;
2851 elsif In_Open_Scopes (Gen_Unit) then
2852 if Is_Compilation_Unit (Gen_Unit)
2853 and then Is_Child_Unit (Current_Scope)
2854 then
2855 -- Special-case the error when the formal is a parent, and
2856 -- continue analysis to minimize cascaded errors.
2858 Error_Msg_N
2859 ("generic parent cannot be used as formal package of a child "
2860 & "unit", Gen_Id);
2862 else
2863 Error_Msg_N
2864 ("generic package cannot be used as a formal package within "
2865 & "itself", Gen_Id);
2866 Restore_Env;
2867 goto Leave;
2868 end if;
2869 end if;
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.
2875 declare
2876 Gen_Name : Entity_Id;
2878 begin
2879 Gen_Name := Gen_Id;
2880 while Nkind (Gen_Name) = N_Expanded_Name loop
2881 Gen_Name := Prefix (Gen_Name);
2882 end loop;
2884 if Chars (Gen_Name) = Chars (Pack_Id) then
2885 Error_Msg_NE
2886 ("& is hidden within declaration of formal package",
2887 Gen_Id, Gen_Name);
2888 end if;
2889 end;
2891 if Box_Present (N)
2892 or else No (Generic_Associations (N))
2893 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2894 then
2895 Associations := False;
2896 end if;
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);
2909 end if;
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.
2917 begin
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.
2924 exception
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
2932 Remove_Parent;
2933 end if;
2935 goto Leave;
2936 end;
2938 Rewrite (N, New_N);
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;
2967 Renaming_In_Par :=
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);
2975 end if;
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);
2989 end if;
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.
3000 declare
3001 E : Entity_Id;
3003 begin
3004 E := First_Entity (Formal);
3005 while Present (E) loop
3006 if Associations and then not Is_Generic_Formal (E) then
3007 Set_Is_Hidden (E);
3008 end if;
3010 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3011 Set_Is_Hidden (E);
3012 exit;
3013 end if;
3015 Next_Entity (E);
3016 end loop;
3017 end;
3019 End_Package_Scope (Formal);
3020 Restore_Hidden_Primitives (Vis_Prims_List);
3022 if Parent_Installed then
3023 Remove_Parent;
3024 end if;
3026 Restore_Env;
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
3037 -- use.
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);
3044 <<Leave>>
3045 if Has_Aspects (N) then
3046 Analyze_Aspect_Specifications (N, Pack_Id);
3047 end if;
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
3057 (N : Node_Id;
3058 T : Entity_Id;
3059 Def : Node_Id)
3061 begin
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
3075 (T : Entity_Id;
3076 Def : Node_Id)
3078 begin
3079 Enter_Name (T);
3080 Set_Ekind (T, E_Incomplete_Type);
3081 Set_Etype (T, T);
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);
3088 end if;
3089 end Analyze_Formal_Incomplete_Type;
3091 ----------------------------------------
3092 -- Analyze_Formal_Signed_Integer_Type --
3093 ----------------------------------------
3095 procedure Analyze_Formal_Signed_Integer_Type
3096 (T : Entity_Id;
3097 Def : Node_Id)
3099 Base : constant Entity_Id :=
3100 New_Internal_Entity
3101 (E_Signed_Integer_Type,
3102 Current_Scope,
3103 Sloc (Defining_Identifier (Parent (Def))), 'G');
3105 begin
3106 Enter_Name (T);
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);
3131 Subp : Entity_Id;
3133 begin
3134 if Nam = Error then
3135 return;
3136 end if;
3138 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3139 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3140 goto Leave;
3141 end if;
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)
3157 then
3158 Error_Msg_N
3159 ("a formal abstract subprogram cannot default to null", Spec);
3160 end if;
3162 declare
3163 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3164 begin
3165 if No (Ctrl_Type) then
3166 Error_Msg_N
3167 ("abstract formal subprogram must have a controlling type",
3170 elsif Ada_Version >= Ada_2012
3171 and then Is_Incomplete_Type (Ctrl_Type)
3172 then
3173 Error_Msg_NE
3174 ("controlling type of abstract formal subprogram cannot "
3175 & "be incomplete type", N, Ctrl_Type);
3177 else
3178 Check_Controlling_Formals (Ctrl_Type, Nam);
3179 end if;
3180 end;
3181 end if;
3183 -- Default name is resolved at the point of instantiation
3185 if Box_Present (N) then
3186 null;
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
3195 Analyze (Def);
3197 else
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);
3203 goto Leave;
3204 end if;
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
3213 goto Leave;
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);
3218 end if;
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);
3224 end if;
3226 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3227 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3228 E_Entry_Family
3229 then
3230 Error_Msg_N ("expect valid subprogram name as default", Def);
3231 end if;
3233 else
3234 Error_Msg_N ("expect valid subprogram name as default", Def);
3235 goto Leave;
3236 end if;
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))
3246 then
3247 Error_Msg_N ("expect valid subprogram name as default", Def);
3248 goto Leave;
3250 elsif not Is_Overloaded (Def) then
3251 Subp := Entity (Def);
3253 if Subp = Nam then
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);
3258 end if;
3260 -- More than one interpretation, so disambiguate as for a renaming
3262 else
3263 declare
3264 I : Interp_Index;
3265 I1 : Interp_Index := 0;
3266 It : Interp;
3267 It1 : Interp;
3269 begin
3270 Subp := Any_Id;
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);
3279 else
3280 Subp := It1.Nam;
3281 end if;
3283 exit;
3285 else
3286 I1 := I;
3287 Subp := It.Nam;
3288 end if;
3289 end if;
3291 Get_Next_Interp (I, It);
3292 end loop;
3293 end;
3295 if Subp /= Any_Id then
3297 -- Subprogram found, generate reference to it
3299 Set_Entity (Def, Subp);
3300 Generate_Reference (Subp, Def);
3302 if Subp = Nam then
3303 Error_Msg_N ("premature usage of formal subprogram", Def);
3305 elsif Ekind (Subp) /= E_Operator then
3306 Check_Mode_Conformant (Subp, Nam);
3307 end if;
3309 else
3310 Error_Msg_N ("no visible subprogram matches specification", N);
3311 end if;
3312 end if;
3313 end if;
3315 <<Leave>>
3316 if Has_Aspects (N) then
3317 Analyze_Aspect_Specifications (N, Nam);
3318 end if;
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);
3328 T : Entity_Id;
3330 begin
3331 T := Defining_Identifier (N);
3333 if Present (Discriminant_Specifications (N))
3334 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3335 then
3336 Error_Msg_N
3337 ("discriminants not allowed for this formal type", T);
3338 end if;
3340 -- Enter the new name, and branch to specific routine
3342 case Nkind (Def) is
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);
3388 when N_Error =>
3389 null;
3391 when others =>
3392 raise Program_Error;
3393 end case;
3395 Set_Is_Generic_Type (T);
3397 if Has_Aspects (N) then
3398 Analyze_Aspect_Specifications (N, T);
3399 end if;
3400 end Analyze_Formal_Type_Declaration;
3402 ------------------------------------
3403 -- Analyze_Function_Instantiation --
3404 ------------------------------------
3406 procedure Analyze_Function_Instantiation (N : Node_Id) is
3407 begin
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
3416 begin
3417 Enter_Name (T);
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))
3425 then
3426 Error_Msg_N ("premature usage of incomplete type", Def);
3428 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3429 Error_Msg_N
3430 ("only a subtype mark is allowed in a formal", Def);
3431 end if;
3433 else
3434 Access_Subprogram_Declaration (T, Def);
3435 end if;
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;
3445 begin
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
3448 -- caller.
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);
3454 end loop;
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);
3467 Decl : Node_Id;
3468 Id : Entity_Id;
3469 New_N : Node_Id;
3470 Renaming : Node_Id;
3471 Save_Parent : Node_Id;
3473 begin
3474 Check_SPARK_05_Restriction ("generic is not allowed", N);
3476 -- A generic may grant access to its private enclosing context depending
3477 -- on the placement of its corresponding body. From elaboration point of
3478 -- view, the flow of execution may enter this private context, and then
3479 -- reach an external unit, thus producing a dependency on that external
3480 -- unit. For such a path to be properly discovered and encoded in the
3481 -- ALI file of the main unit, let the ABE mechanism process the body of
3482 -- the main unit, and encode all relevant invocation constructs and the
3483 -- relations between them.
3485 Mark_Save_Invocation_Graph_Of_Body;
3487 -- We introduce a renaming of the enclosing package, to have a usable
3488 -- entity as the prefix of an expanded name for a local entity of the
3489 -- form Par.P.Q, where P is the generic package. This is because a local
3490 -- entity named P may hide it, so that the usual visibility rules in
3491 -- the instance will not resolve properly.
3493 Renaming :=
3494 Make_Package_Renaming_Declaration (Loc,
3495 Defining_Unit_Name =>
3496 Make_Defining_Identifier (Loc,
3497 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3498 Name =>
3499 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3501 -- The declaration is inserted before other declarations, but before
3502 -- pragmas that may be library-unit pragmas and must appear before other
3503 -- declarations. The pragma Compile_Time_Error is not in this class, and
3504 -- may contain an expression that includes such a qualified name, so the
3505 -- renaming declaration must appear before it.
3507 -- Are there other pragmas that require this special handling ???
3509 if Present (Decls) then
3510 Decl := First (Decls);
3511 while Present (Decl)
3512 and then Nkind (Decl) = N_Pragma
3513 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
3514 loop
3515 Next (Decl);
3516 end loop;
3518 if Present (Decl) then
3519 Insert_Before (Decl, Renaming);
3520 else
3521 Append (Renaming, Visible_Declarations (Specification (N)));
3522 end if;
3524 else
3525 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3526 end if;
3528 -- Create copy of generic unit, and save for instantiation. If the unit
3529 -- is a child unit, do not copy the specifications for the parent, which
3530 -- are not part of the generic tree.
3532 Save_Parent := Parent_Spec (N);
3533 Set_Parent_Spec (N, Empty);
3535 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3536 Set_Parent_Spec (New_N, Save_Parent);
3537 Rewrite (N, New_N);
3539 -- Once the contents of the generic copy and the template are swapped,
3540 -- do the same for their respective aspect specifications.
3542 Exchange_Aspects (N, New_N);
3544 -- Collect all contract-related source pragmas found within the template
3545 -- and attach them to the contract of the package spec. This contract is
3546 -- used in the capture of global references within annotations.
3548 Create_Generic_Contract (N);
3550 Id := Defining_Entity (N);
3551 Generate_Definition (Id);
3553 -- Expansion is not applied to generic units
3555 Start_Generic;
3557 Enter_Name (Id);
3558 Set_Ekind (Id, E_Generic_Package);
3559 Set_Etype (Id, Standard_Void_Type);
3561 -- Set SPARK_Mode from context
3563 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3564 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3565 Set_SPARK_Pragma_Inherited (Id);
3566 Set_SPARK_Aux_Pragma_Inherited (Id);
3568 -- Preserve relevant elaboration-related attributes of the context which
3569 -- are no longer available or very expensive to recompute once analysis,
3570 -- resolution, and expansion are over.
3572 Mark_Elaboration_Attributes
3573 (N_Id => Id,
3574 Checks => True,
3575 Warnings => True);
3577 -- Analyze aspects now, so that generated pragmas appear in the
3578 -- declarations before building and analyzing the generic copy.
3580 if Has_Aspects (N) then
3581 Analyze_Aspect_Specifications (N, Id);
3582 end if;
3584 Push_Scope (Id);
3585 Enter_Generic_Scope (Id);
3586 Set_Inner_Instances (Id, New_Elmt_List);
3588 Set_Categorization_From_Pragmas (N);
3589 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3591 -- Link the declaration of the generic homonym in the generic copy to
3592 -- the package it renames, so that it is always resolved properly.
3594 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3595 Set_Entity (Associated_Node (Name (Renaming)), Id);
3597 -- For a library unit, we have reconstructed the entity for the unit,
3598 -- and must reset it in the library tables.
3600 if Nkind (Parent (N)) = N_Compilation_Unit then
3601 Set_Cunit_Entity (Current_Sem_Unit, Id);
3602 end if;
3604 Analyze_Generic_Formal_Part (N);
3606 -- After processing the generic formals, analysis proceeds as for a
3607 -- non-generic package.
3609 Analyze (Specification (N));
3611 Validate_Categorization_Dependency (N, Id);
3613 End_Generic;
3615 End_Package_Scope (Id);
3616 Exit_Generic_Scope (Id);
3618 -- If the generic appears within a package unit, the body of that unit
3619 -- has to be present for instantiation and inlining.
3621 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3622 Set_Body_Needed_For_Inlining
3623 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3624 end if;
3626 if Nkind (Parent (N)) /= N_Compilation_Unit then
3627 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3628 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3629 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3631 else
3632 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3633 Validate_RT_RAT_Component (N);
3635 -- If this is a spec without a body, check that generic parameters
3636 -- are referenced.
3638 if not Body_Required (Parent (N)) then
3639 Check_References (Id);
3640 end if;
3641 end if;
3643 -- If there is a specified storage pool in the context, create an
3644 -- aspect on the package declaration, so that it is used in any
3645 -- instance that does not override it.
3647 if Present (Default_Pool) then
3648 declare
3649 ASN : Node_Id;
3651 begin
3652 ASN :=
3653 Make_Aspect_Specification (Loc,
3654 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3655 Expression => New_Copy (Default_Pool));
3657 if No (Aspect_Specifications (Specification (N))) then
3658 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3659 else
3660 Append (ASN, Aspect_Specifications (Specification (N)));
3661 end if;
3662 end;
3663 end if;
3664 end Analyze_Generic_Package_Declaration;
3666 --------------------------------------------
3667 -- Analyze_Generic_Subprogram_Declaration --
3668 --------------------------------------------
3670 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3671 Formals : List_Id;
3672 Id : Entity_Id;
3673 New_N : Node_Id;
3674 Result_Type : Entity_Id;
3675 Save_Parent : Node_Id;
3676 Spec : Node_Id;
3677 Typ : Entity_Id;
3679 begin
3680 Check_SPARK_05_Restriction ("generic is not allowed", N);
3682 -- A generic may grant access to its private enclosing context depending
3683 -- on the placement of its corresponding body. From elaboration point of
3684 -- view, the flow of execution may enter this private context, and then
3685 -- reach an external unit, thus producing a dependency on that external
3686 -- unit. For such a path to be properly discovered and encoded in the
3687 -- ALI file of the main unit, let the ABE mechanism process the body of
3688 -- the main unit, and encode all relevant invocation constructs and the
3689 -- relations between them.
3691 Mark_Save_Invocation_Graph_Of_Body;
3693 -- Create copy of generic unit, and save for instantiation. If the unit
3694 -- is a child unit, do not copy the specifications for the parent, which
3695 -- are not part of the generic tree.
3697 Save_Parent := Parent_Spec (N);
3698 Set_Parent_Spec (N, Empty);
3700 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3701 Set_Parent_Spec (New_N, Save_Parent);
3702 Rewrite (N, New_N);
3704 -- Once the contents of the generic copy and the template are swapped,
3705 -- do the same for their respective aspect specifications.
3707 Exchange_Aspects (N, New_N);
3709 -- Collect all contract-related source pragmas found within the template
3710 -- and attach them to the contract of the subprogram spec. This contract
3711 -- is used in the capture of global references within annotations.
3713 Create_Generic_Contract (N);
3715 Spec := Specification (N);
3716 Id := Defining_Entity (Spec);
3717 Generate_Definition (Id);
3719 if Nkind (Id) = N_Defining_Operator_Symbol then
3720 Error_Msg_N
3721 ("operator symbol not allowed for generic subprogram", Id);
3722 end if;
3724 Start_Generic;
3726 Enter_Name (Id);
3727 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3729 -- Analyze the aspects of the generic copy to ensure that all generated
3730 -- pragmas (if any) perform their semantic effects.
3732 if Has_Aspects (N) then
3733 Analyze_Aspect_Specifications (N, Id);
3734 end if;
3736 Push_Scope (Id);
3737 Enter_Generic_Scope (Id);
3738 Set_Inner_Instances (Id, New_Elmt_List);
3739 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3741 Analyze_Generic_Formal_Part (N);
3743 if Nkind (Spec) = N_Function_Specification then
3744 Set_Ekind (Id, E_Generic_Function);
3745 else
3746 Set_Ekind (Id, E_Generic_Procedure);
3747 end if;
3749 -- Set SPARK_Mode from context
3751 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3752 Set_SPARK_Pragma_Inherited (Id);
3754 -- Preserve relevant elaboration-related attributes of the context which
3755 -- are no longer available or very expensive to recompute once analysis,
3756 -- resolution, and expansion are over.
3758 Mark_Elaboration_Attributes
3759 (N_Id => Id,
3760 Checks => True,
3761 Warnings => True);
3763 Formals := Parameter_Specifications (Spec);
3765 if Present (Formals) then
3766 Process_Formals (Formals, Spec);
3767 end if;
3769 if Nkind (Spec) = N_Function_Specification then
3770 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3771 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3772 Set_Etype (Id, Result_Type);
3774 -- Check restriction imposed by AI05-073: a generic function
3775 -- cannot return an abstract type or an access to such.
3777 -- This is a binding interpretation should it apply to earlier
3778 -- versions of Ada as well as Ada 2012???
3780 if Is_Abstract_Type (Designated_Type (Result_Type))
3781 and then Ada_Version >= Ada_2012
3782 then
3783 Error_Msg_N
3784 ("generic function cannot have an access result "
3785 & "that designates an abstract type", Spec);
3786 end if;
3788 else
3789 Find_Type (Result_Definition (Spec));
3790 Typ := Entity (Result_Definition (Spec));
3792 if Is_Abstract_Type (Typ)
3793 and then Ada_Version >= Ada_2012
3794 then
3795 Error_Msg_N
3796 ("generic function cannot have abstract result type", Spec);
3797 end if;
3799 -- If a null exclusion is imposed on the result type, then create
3800 -- a null-excluding itype (an access subtype) and use it as the
3801 -- function's Etype.
3803 if Is_Access_Type (Typ)
3804 and then Null_Exclusion_Present (Spec)
3805 then
3806 Set_Etype (Id,
3807 Create_Null_Excluding_Itype
3808 (T => Typ,
3809 Related_Nod => Spec,
3810 Scope_Id => Defining_Unit_Name (Spec)));
3811 else
3812 Set_Etype (Id, Typ);
3813 end if;
3814 end if;
3816 else
3817 Set_Etype (Id, Standard_Void_Type);
3818 end if;
3820 -- For a library unit, we have reconstructed the entity for the unit,
3821 -- and must reset it in the library tables. We also make sure that
3822 -- Body_Required is set properly in the original compilation unit node.
3824 if Nkind (Parent (N)) = N_Compilation_Unit then
3825 Set_Cunit_Entity (Current_Sem_Unit, Id);
3826 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3827 end if;
3829 -- If the generic appears within a package unit, the body of that unit
3830 -- has to be present for instantiation and inlining.
3832 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
3833 and then Unit_Requires_Body (Id)
3834 then
3835 Set_Body_Needed_For_Inlining
3836 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3837 end if;
3839 Set_Categorization_From_Pragmas (N);
3840 Validate_Categorization_Dependency (N, Id);
3842 -- Capture all global references that occur within the profile of the
3843 -- generic subprogram. Aspects are not part of this processing because
3844 -- they must be delayed. If processed now, Save_Global_References will
3845 -- destroy the Associated_Node links and prevent the capture of global
3846 -- references when the contract of the generic subprogram is analyzed.
3848 Save_Global_References (Original_Node (N));
3850 End_Generic;
3851 End_Scope;
3852 Exit_Generic_Scope (Id);
3853 Generate_Reference_To_Formals (Id);
3855 List_Inherited_Pre_Post_Aspects (Id);
3856 end Analyze_Generic_Subprogram_Declaration;
3858 -----------------------------------
3859 -- Analyze_Package_Instantiation --
3860 -----------------------------------
3862 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
3863 -- must be replaced by gotos which jump to the end of the routine in order
3864 -- to restore the Ghost and SPARK modes.
3866 procedure Analyze_Package_Instantiation (N : Node_Id) is
3867 Has_Inline_Always : Boolean := False;
3869 procedure Delay_Descriptors (E : Entity_Id);
3870 -- Delay generation of subprogram descriptors for given entity
3872 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
3873 -- If inlining is active and the generic contains inlined subprograms,
3874 -- we instantiate the body. This may cause superfluous instantiations,
3875 -- but it is simpler than detecting the need for the body at the point
3876 -- of inlining, when the context of the instance is not available.
3878 -----------------------
3879 -- Delay_Descriptors --
3880 -----------------------
3882 procedure Delay_Descriptors (E : Entity_Id) is
3883 begin
3884 if not Delay_Subprogram_Descriptors (E) then
3885 Set_Delay_Subprogram_Descriptors (E);
3886 Pending_Descriptor.Append (E);
3887 end if;
3888 end Delay_Descriptors;
3890 -----------------------
3891 -- Might_Inline_Subp --
3892 -----------------------
3894 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
3895 E : Entity_Id;
3897 begin
3898 if not Inline_Processing_Required then
3899 return False;
3901 else
3902 E := First_Entity (Gen_Unit);
3903 while Present (E) loop
3904 if Is_Subprogram (E) and then Is_Inlined (E) then
3905 -- Remember if there are any subprograms with Inline_Always
3907 if Has_Pragma_Inline_Always (E) then
3908 Has_Inline_Always := True;
3909 end if;
3911 return True;
3912 end if;
3914 Next_Entity (E);
3915 end loop;
3916 end if;
3918 return False;
3919 end Might_Inline_Subp;
3921 -- Local declarations
3923 Gen_Id : constant Node_Id := Name (N);
3924 Inst_Id : constant Entity_Id := Defining_Entity (N);
3925 Is_Actual_Pack : constant Boolean := Is_Internal (Inst_Id);
3926 Loc : constant Source_Ptr := Sloc (N);
3928 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3929 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3930 Saved_ISMP : constant Boolean :=
3931 Ignore_SPARK_Mode_Pragmas_In_Instance;
3932 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
3933 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
3934 -- Save the Ghost and SPARK mode-related data to restore on exit
3936 Saved_Style_Check : constant Boolean := Style_Check;
3937 -- Save style check mode for restore on exit
3939 Act_Decl : Node_Id;
3940 Act_Decl_Name : Node_Id;
3941 Act_Decl_Id : Entity_Id;
3942 Act_Spec : Node_Id;
3943 Act_Tree : Node_Id;
3944 Env_Installed : Boolean := False;
3945 Gen_Decl : Node_Id;
3946 Gen_Spec : Node_Id;
3947 Gen_Unit : Entity_Id;
3948 Inline_Now : Boolean := False;
3949 Needs_Body : Boolean;
3950 Parent_Installed : Boolean := False;
3951 Renaming_List : List_Id;
3952 Unit_Renaming : Node_Id;
3954 Vis_Prims_List : Elist_Id := No_Elist;
3955 -- List of primitives made temporarily visible in the instantiation
3956 -- to match the visibility of the formal type
3958 -- Start of processing for Analyze_Package_Instantiation
3960 begin
3961 -- Preserve relevant elaboration-related attributes of the context which
3962 -- are no longer available or very expensive to recompute once analysis,
3963 -- resolution, and expansion are over.
3965 Mark_Elaboration_Attributes
3966 (N_Id => N,
3967 Checks => True,
3968 Level => True,
3969 Modes => True,
3970 Warnings => True);
3972 Check_SPARK_05_Restriction ("generic is not allowed", N);
3974 -- Very first thing: check for Text_IO special unit in case we are
3975 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3977 Check_Text_IO_Special_Unit (Name (N));
3979 -- Make node global for error reporting
3981 Instantiation_Node := N;
3983 -- Case of instantiation of a generic package
3985 if Nkind (N) = N_Package_Instantiation then
3986 Act_Decl_Id := New_Copy (Defining_Entity (N));
3987 Set_Comes_From_Source (Act_Decl_Id, True);
3989 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3990 Act_Decl_Name :=
3991 Make_Defining_Program_Unit_Name (Loc,
3992 Name =>
3993 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3994 Defining_Identifier => Act_Decl_Id);
3995 else
3996 Act_Decl_Name := Act_Decl_Id;
3997 end if;
3999 -- Case of instantiation of a formal package
4001 else
4002 Act_Decl_Id := Defining_Identifier (N);
4003 Act_Decl_Name := Act_Decl_Id;
4004 end if;
4006 Generate_Definition (Act_Decl_Id);
4007 Set_Ekind (Act_Decl_Id, E_Package);
4009 -- Initialize list of incomplete actuals before analysis
4011 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
4013 Preanalyze_Actuals (N, Act_Decl_Id);
4015 -- Turn off style checking in instances. If the check is enabled on the
4016 -- generic unit, a warning in an instance would just be noise. If not
4017 -- enabled on the generic, then a warning in an instance is just wrong.
4018 -- This must be done after analyzing the actuals, which do come from
4019 -- source and are subject to style checking.
4021 Style_Check := False;
4023 Init_Env;
4024 Env_Installed := True;
4026 -- Reset renaming map for formal types. The mapping is established
4027 -- when analyzing the generic associations, but some mappings are
4028 -- inherited from formal packages of parent units, and these are
4029 -- constructed when the parents are installed.
4031 Generic_Renamings.Set_Last (0);
4032 Generic_Renamings_HTable.Reset;
4034 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4035 Gen_Unit := Entity (Gen_Id);
4037 -- A package instantiation is Ghost when it is subject to pragma Ghost
4038 -- or the generic template is Ghost. Set the mode now to ensure that
4039 -- any nodes generated during analysis and expansion are marked as
4040 -- Ghost.
4042 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4044 -- Verify that it is the name of a generic package
4046 -- A visibility glitch: if the instance is a child unit and the generic
4047 -- is the generic unit of a parent instance (i.e. both the parent and
4048 -- the child units are instances of the same package) the name now
4049 -- denotes the renaming within the parent, not the intended generic
4050 -- unit. See if there is a homonym that is the desired generic. The
4051 -- renaming declaration must be visible inside the instance of the
4052 -- child, but not when analyzing the name in the instantiation itself.
4054 if Ekind (Gen_Unit) = E_Package
4055 and then Present (Renamed_Entity (Gen_Unit))
4056 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4057 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4058 and then Present (Homonym (Gen_Unit))
4059 then
4060 Gen_Unit := Homonym (Gen_Unit);
4061 end if;
4063 if Etype (Gen_Unit) = Any_Type then
4064 Restore_Env;
4065 goto Leave;
4067 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4069 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4071 if From_Limited_With (Gen_Unit) then
4072 Error_Msg_N
4073 ("cannot instantiate a limited withed package", Gen_Id);
4074 else
4075 Error_Msg_NE
4076 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4077 end if;
4079 Restore_Env;
4080 goto Leave;
4081 end if;
4083 if In_Extended_Main_Source_Unit (N) then
4084 Set_Is_Instantiated (Gen_Unit);
4085 Generate_Reference (Gen_Unit, N);
4087 if Present (Renamed_Object (Gen_Unit)) then
4088 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
4089 Generate_Reference (Renamed_Object (Gen_Unit), N);
4090 end if;
4091 end if;
4093 if Nkind (Gen_Id) = N_Identifier
4094 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4095 then
4096 Error_Msg_NE
4097 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4099 elsif Nkind (Gen_Id) = N_Expanded_Name
4100 and then Is_Child_Unit (Gen_Unit)
4101 and then Nkind (Prefix (Gen_Id)) = N_Identifier
4102 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
4103 then
4104 Error_Msg_N
4105 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
4106 end if;
4108 Set_Entity (Gen_Id, Gen_Unit);
4110 -- If generic is a renaming, get original generic unit
4112 if Present (Renamed_Object (Gen_Unit))
4113 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
4114 then
4115 Gen_Unit := Renamed_Object (Gen_Unit);
4116 end if;
4118 -- Verify that there are no circular instantiations
4120 if In_Open_Scopes (Gen_Unit) then
4121 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4122 Restore_Env;
4123 goto Leave;
4125 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4126 Error_Msg_Node_2 := Current_Scope;
4127 Error_Msg_NE
4128 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4129 Circularity_Detected := True;
4130 Restore_Env;
4131 goto Leave;
4133 else
4134 Set_Ekind (Inst_Id, E_Package);
4135 Set_Scope (Inst_Id, Current_Scope);
4137 -- If the context of the instance is subject to SPARK_Mode "off" or
4138 -- the annotation is altogether missing, set the global flag which
4139 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4140 -- the instance.
4142 if SPARK_Mode /= On then
4143 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4145 -- Mark the instance spec in case the body is instantiated at a
4146 -- later pass. This preserves the original context in effect for
4147 -- the body.
4149 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
4150 end if;
4152 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4153 Gen_Spec := Specification (Gen_Decl);
4155 -- Initialize renamings map, for error checking, and the list that
4156 -- holds private entities whose views have changed between generic
4157 -- definition and instantiation. If this is the instance created to
4158 -- validate an actual package, the instantiation environment is that
4159 -- of the enclosing instance.
4161 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
4163 -- Copy original generic tree, to produce text for instantiation
4165 Act_Tree :=
4166 Copy_Generic_Node
4167 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4169 Act_Spec := Specification (Act_Tree);
4171 -- If this is the instance created to validate an actual package,
4172 -- only the formals matter, do not examine the package spec itself.
4174 if Is_Actual_Pack then
4175 Set_Visible_Declarations (Act_Spec, New_List);
4176 Set_Private_Declarations (Act_Spec, New_List);
4177 end if;
4179 Renaming_List :=
4180 Analyze_Associations
4181 (I_Node => N,
4182 Formals => Generic_Formal_Declarations (Act_Tree),
4183 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4185 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4187 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
4188 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
4189 Set_Is_Generic_Instance (Act_Decl_Id);
4190 Set_Generic_Parent (Act_Spec, Gen_Unit);
4192 -- References to the generic in its own declaration or its body are
4193 -- references to the instance. Add a renaming declaration for the
4194 -- generic unit itself. This declaration, as well as the renaming
4195 -- declarations for the generic formals, must remain private to the
4196 -- unit: the formals, because this is the language semantics, and
4197 -- the unit because its use is an artifact of the implementation.
4199 Unit_Renaming :=
4200 Make_Package_Renaming_Declaration (Loc,
4201 Defining_Unit_Name =>
4202 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
4203 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
4205 Append (Unit_Renaming, Renaming_List);
4207 -- The renaming declarations are the first local declarations of the
4208 -- new unit.
4210 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
4211 Insert_List_Before
4212 (First (Visible_Declarations (Act_Spec)), Renaming_List);
4213 else
4214 Set_Visible_Declarations (Act_Spec, Renaming_List);
4215 end if;
4217 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
4219 -- Propagate the aspect specifications from the package declaration
4220 -- template to the instantiated version of the package declaration.
4222 if Has_Aspects (Act_Tree) then
4223 Set_Aspect_Specifications (Act_Decl,
4224 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4225 end if;
4227 -- The generic may have a generated Default_Storage_Pool aspect,
4228 -- set at the point of generic declaration. If the instance has
4229 -- that aspect, it overrides the one inherited from the generic.
4231 if Has_Aspects (Gen_Spec) then
4232 if No (Aspect_Specifications (N)) then
4233 Set_Aspect_Specifications (N,
4234 (New_Copy_List_Tree
4235 (Aspect_Specifications (Gen_Spec))));
4237 else
4238 declare
4239 Inherited_Aspects : constant List_Id :=
4240 New_Copy_List_Tree
4241 (Aspect_Specifications (Gen_Spec));
4243 ASN1 : Node_Id;
4244 ASN2 : Node_Id;
4245 Pool_Present : Boolean := False;
4247 begin
4248 ASN1 := First (Aspect_Specifications (N));
4249 while Present (ASN1) loop
4250 if Chars (Identifier (ASN1)) =
4251 Name_Default_Storage_Pool
4252 then
4253 Pool_Present := True;
4254 exit;
4255 end if;
4257 Next (ASN1);
4258 end loop;
4260 if Pool_Present then
4262 -- If generic carries a default storage pool, remove it
4263 -- in favor of the instance one.
4265 ASN2 := First (Inherited_Aspects);
4266 while Present (ASN2) loop
4267 if Chars (Identifier (ASN2)) =
4268 Name_Default_Storage_Pool
4269 then
4270 Remove (ASN2);
4271 exit;
4272 end if;
4274 Next (ASN2);
4275 end loop;
4276 end if;
4278 Prepend_List_To
4279 (Aspect_Specifications (N), Inherited_Aspects);
4280 end;
4281 end if;
4282 end if;
4284 -- Save the instantiation node, for subsequent instantiation of the
4285 -- body, if there is one and we are generating code for the current
4286 -- unit. Mark unit as having a body (avoids premature error message).
4288 -- We instantiate the body if we are generating code, if we are
4289 -- generating cross-reference information, or if we are building
4290 -- trees for ASIS use or GNATprove use.
4292 declare
4293 Enclosing_Body_Present : Boolean := False;
4294 -- If the generic unit is not a compilation unit, then a body may
4295 -- be present in its parent even if none is required. We create a
4296 -- tentative pending instantiation for the body, which will be
4297 -- discarded if none is actually present.
4299 Scop : Entity_Id;
4301 begin
4302 if Scope (Gen_Unit) /= Standard_Standard
4303 and then not Is_Child_Unit (Gen_Unit)
4304 then
4305 Scop := Scope (Gen_Unit);
4306 while Present (Scop) and then Scop /= Standard_Standard loop
4307 if Unit_Requires_Body (Scop) then
4308 Enclosing_Body_Present := True;
4309 exit;
4311 elsif In_Open_Scopes (Scop)
4312 and then In_Package_Body (Scop)
4313 then
4314 Enclosing_Body_Present := True;
4315 exit;
4316 end if;
4318 exit when Is_Compilation_Unit (Scop);
4319 Scop := Scope (Scop);
4320 end loop;
4321 end if;
4323 -- If front-end inlining is enabled or there are any subprograms
4324 -- marked with Inline_Always, and this is a unit for which code
4325 -- will be generated, we instantiate the body at once.
4327 -- This is done if the instance is not the main unit, and if the
4328 -- generic is not a child unit of another generic, to avoid scope
4329 -- problems and the reinstallation of parent instances.
4331 if Expander_Active
4332 and then (not Is_Child_Unit (Gen_Unit)
4333 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4334 and then Might_Inline_Subp (Gen_Unit)
4335 and then not Is_Actual_Pack
4336 then
4337 if not Back_End_Inlining
4338 and then (Front_End_Inlining or else Has_Inline_Always)
4339 and then (Is_In_Main_Unit (N)
4340 or else In_Main_Context (Current_Scope))
4341 and then Nkind (Parent (N)) /= N_Compilation_Unit
4342 then
4343 Inline_Now := True;
4345 -- In configurable_run_time mode we force the inlining of
4346 -- predefined subprograms marked Inline_Always, to minimize
4347 -- the use of the run-time library.
4349 elsif In_Predefined_Unit (Gen_Decl)
4350 and then Configurable_Run_Time_Mode
4351 and then Nkind (Parent (N)) /= N_Compilation_Unit
4352 then
4353 Inline_Now := True;
4354 end if;
4356 -- If the current scope is itself an instance within a child
4357 -- unit, there will be duplications in the scope stack, and the
4358 -- unstacking mechanism in Inline_Instance_Body will fail.
4359 -- This loses some rare cases of optimization, and might be
4360 -- improved some day, if we can find a proper abstraction for
4361 -- "the complete compilation context" that can be saved and
4362 -- restored. ???
4364 if Is_Generic_Instance (Current_Scope) then
4365 declare
4366 Curr_Unit : constant Entity_Id :=
4367 Cunit_Entity (Current_Sem_Unit);
4368 begin
4369 if Curr_Unit /= Current_Scope
4370 and then Is_Child_Unit (Curr_Unit)
4371 then
4372 Inline_Now := False;
4373 end if;
4374 end;
4375 end if;
4376 end if;
4378 Needs_Body :=
4379 (Unit_Requires_Body (Gen_Unit)
4380 or else Enclosing_Body_Present
4381 or else Present (Corresponding_Body (Gen_Decl)))
4382 and then (Is_In_Main_Unit (N)
4383 or else Might_Inline_Subp (Gen_Unit))
4384 and then not Is_Actual_Pack
4385 and then not Inline_Now
4386 and then (Operating_Mode = Generate_Code
4388 -- Need comment for this check ???
4390 or else (Operating_Mode = Check_Semantics
4391 and then (ASIS_Mode or GNATprove_Mode)));
4393 -- If front-end inlining is enabled or there are any subprograms
4394 -- marked with Inline_Always, do not instantiate body when within
4395 -- a generic context.
4397 if ((Front_End_Inlining or else Has_Inline_Always)
4398 and then not Expander_Active)
4399 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
4400 then
4401 Needs_Body := False;
4402 end if;
4404 -- If the current context is generic, and the package being
4405 -- instantiated is declared within a formal package, there is no
4406 -- body to instantiate until the enclosing generic is instantiated
4407 -- and there is an actual for the formal package. If the formal
4408 -- package has parameters, we build a regular package instance for
4409 -- it, that precedes the original formal package declaration.
4411 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4412 declare
4413 Decl : constant Node_Id :=
4414 Original_Node
4415 (Unit_Declaration_Node (Scope (Gen_Unit)));
4416 begin
4417 if Nkind (Decl) = N_Formal_Package_Declaration
4418 or else (Nkind (Decl) = N_Package_Declaration
4419 and then Is_List_Member (Decl)
4420 and then Present (Next (Decl))
4421 and then
4422 Nkind (Next (Decl)) =
4423 N_Formal_Package_Declaration)
4424 then
4425 Needs_Body := False;
4426 end if;
4427 end;
4428 end if;
4429 end;
4431 -- For RCI unit calling stubs, we omit the instance body if the
4432 -- instance is the RCI library unit itself.
4434 -- However there is a special case for nested instances: in this case
4435 -- we do generate the instance body, as it might be required, e.g.
4436 -- because it provides stream attributes for some type used in the
4437 -- profile of a remote subprogram. This is consistent with 12.3(12),
4438 -- which indicates that the instance body occurs at the place of the
4439 -- instantiation, and thus is part of the RCI declaration, which is
4440 -- present on all client partitions (this is E.2.3(18)).
4442 -- Note that AI12-0002 may make it illegal at some point to have
4443 -- stream attributes defined in an RCI unit, in which case this
4444 -- special case will become unnecessary. In the meantime, there
4445 -- is known application code in production that depends on this
4446 -- being possible, so we definitely cannot eliminate the body in
4447 -- the case of nested instances for the time being.
4449 -- When we generate a nested instance body, calling stubs for any
4450 -- relevant subprogram will be be inserted immediately after the
4451 -- subprogram declarations, and will take precedence over the
4452 -- subsequent (original) body. (The stub and original body will be
4453 -- complete homographs, but this is permitted in an instance).
4454 -- (Could we do better and remove the original body???)
4456 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4457 and then Comes_From_Source (N)
4458 and then Nkind (Parent (N)) = N_Compilation_Unit
4459 then
4460 Needs_Body := False;
4461 end if;
4463 if Needs_Body then
4465 -- Here is a defence against a ludicrous number of instantiations
4466 -- caused by a circular set of instantiation attempts.
4468 if Pending_Instantiations.Last > Maximum_Instantiations then
4469 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4470 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4471 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4472 raise Unrecoverable_Error;
4473 end if;
4475 -- Indicate that the enclosing scopes contain an instantiation,
4476 -- and that cleanup actions should be delayed until after the
4477 -- instance body is expanded.
4479 Check_Forward_Instantiation (Gen_Decl);
4480 if Nkind (N) = N_Package_Instantiation then
4481 declare
4482 Enclosing_Master : Entity_Id;
4484 begin
4485 -- Loop to search enclosing masters
4487 Enclosing_Master := Current_Scope;
4488 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4489 if Ekind (Enclosing_Master) = E_Package then
4490 if Is_Compilation_Unit (Enclosing_Master) then
4491 if In_Package_Body (Enclosing_Master) then
4492 Delay_Descriptors
4493 (Body_Entity (Enclosing_Master));
4494 else
4495 Delay_Descriptors
4496 (Enclosing_Master);
4497 end if;
4499 exit Scope_Loop;
4501 else
4502 Enclosing_Master := Scope (Enclosing_Master);
4503 end if;
4505 elsif Is_Generic_Unit (Enclosing_Master)
4506 or else Ekind (Enclosing_Master) = E_Void
4507 then
4508 -- Cleanup actions will eventually be performed on the
4509 -- enclosing subprogram or package instance, if any.
4510 -- Enclosing scope is void in the formal part of a
4511 -- generic subprogram.
4513 exit Scope_Loop;
4515 else
4516 if Ekind (Enclosing_Master) = E_Entry
4517 and then
4518 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4519 then
4520 if not Expander_Active then
4521 exit Scope_Loop;
4522 else
4523 Enclosing_Master :=
4524 Protected_Body_Subprogram (Enclosing_Master);
4525 end if;
4526 end if;
4528 Set_Delay_Cleanups (Enclosing_Master);
4530 while Ekind (Enclosing_Master) = E_Block loop
4531 Enclosing_Master := Scope (Enclosing_Master);
4532 end loop;
4534 if Is_Subprogram (Enclosing_Master) then
4535 Delay_Descriptors (Enclosing_Master);
4537 elsif Is_Task_Type (Enclosing_Master) then
4538 declare
4539 TBP : constant Node_Id :=
4540 Get_Task_Body_Procedure
4541 (Enclosing_Master);
4542 begin
4543 if Present (TBP) then
4544 Delay_Descriptors (TBP);
4545 Set_Delay_Cleanups (TBP);
4546 end if;
4547 end;
4548 end if;
4550 exit Scope_Loop;
4551 end if;
4552 end loop Scope_Loop;
4553 end;
4555 -- Make entry in table
4557 Add_Pending_Instantiation (N, Act_Decl);
4558 end if;
4559 end if;
4561 Set_Categorization_From_Pragmas (Act_Decl);
4563 if Parent_Installed then
4564 Hide_Current_Scope;
4565 end if;
4567 Set_Instance_Spec (N, Act_Decl);
4569 -- If not a compilation unit, insert the package declaration before
4570 -- the original instantiation node.
4572 if Nkind (Parent (N)) /= N_Compilation_Unit then
4573 Mark_Rewrite_Insertion (Act_Decl);
4574 Insert_Before (N, Act_Decl);
4576 if Has_Aspects (N) then
4577 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4579 -- The pragma created for a Default_Storage_Pool aspect must
4580 -- appear ahead of the declarations in the instance spec.
4581 -- Analysis has placed it after the instance node, so remove
4582 -- it and reinsert it properly now.
4584 declare
4585 ASN : constant Node_Id := First (Aspect_Specifications (N));
4586 A_Name : constant Name_Id := Chars (Identifier (ASN));
4587 Decl : Node_Id;
4589 begin
4590 if A_Name = Name_Default_Storage_Pool then
4591 if No (Visible_Declarations (Act_Spec)) then
4592 Set_Visible_Declarations (Act_Spec, New_List);
4593 end if;
4595 Decl := Next (N);
4596 while Present (Decl) loop
4597 if Nkind (Decl) = N_Pragma then
4598 Remove (Decl);
4599 Prepend (Decl, Visible_Declarations (Act_Spec));
4600 exit;
4601 end if;
4603 Next (Decl);
4604 end loop;
4605 end if;
4606 end;
4607 end if;
4609 Analyze (Act_Decl);
4611 -- For an instantiation that is a compilation unit, place
4612 -- declaration on current node so context is complete for analysis
4613 -- (including nested instantiations). If this is the main unit,
4614 -- the declaration eventually replaces the instantiation node.
4615 -- If the instance body is created later, it replaces the
4616 -- instance node, and the declaration is attached to it
4617 -- (see Build_Instance_Compilation_Unit_Nodes).
4619 else
4620 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4622 -- The entity for the current unit is the newly created one,
4623 -- and all semantic information is attached to it.
4625 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4627 -- If this is the main unit, replace the main entity as well
4629 if Current_Sem_Unit = Main_Unit then
4630 Main_Unit_Entity := Act_Decl_Id;
4631 end if;
4632 end if;
4634 Set_Unit (Parent (N), Act_Decl);
4635 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4636 Set_Package_Instantiation (Act_Decl_Id, N);
4638 -- Process aspect specifications of the instance node, if any, to
4639 -- take into account categorization pragmas before analyzing the
4640 -- instance.
4642 if Has_Aspects (N) then
4643 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4644 end if;
4646 Analyze (Act_Decl);
4647 Set_Unit (Parent (N), N);
4648 Set_Body_Required (Parent (N), False);
4650 -- We never need elaboration checks on instantiations, since by
4651 -- definition, the body instantiation is elaborated at the same
4652 -- time as the spec instantiation.
4654 if Legacy_Elaboration_Checks then
4655 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4656 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4657 end if;
4658 end if;
4660 if Legacy_Elaboration_Checks then
4661 Check_Elab_Instantiation (N);
4662 end if;
4664 -- Save the scenario for later examination by the ABE Processing
4665 -- phase.
4667 Record_Elaboration_Scenario (N);
4669 -- The instantiation results in a guaranteed ABE
4671 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
4673 -- Do not instantiate the corresponding body because gigi cannot
4674 -- handle certain types of premature instantiations.
4676 Pending_Instantiations.Decrement_Last;
4678 -- Create completing bodies for all subprogram declarations since
4679 -- their real bodies will not be instantiated.
4681 Provide_Completing_Bodies (Instance_Spec (N));
4682 end if;
4684 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4686 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4687 First_Private_Entity (Act_Decl_Id));
4689 -- If the instantiation will receive a body, the unit will be
4690 -- transformed into a package body, and receive its own elaboration
4691 -- entity. Otherwise, the nature of the unit is now a package
4692 -- declaration.
4694 if Nkind (Parent (N)) = N_Compilation_Unit
4695 and then not Needs_Body
4696 then
4697 Rewrite (N, Act_Decl);
4698 end if;
4700 if Present (Corresponding_Body (Gen_Decl))
4701 or else Unit_Requires_Body (Gen_Unit)
4702 then
4703 Set_Has_Completion (Act_Decl_Id);
4704 end if;
4706 Check_Formal_Packages (Act_Decl_Id);
4708 Restore_Hidden_Primitives (Vis_Prims_List);
4709 Restore_Private_Views (Act_Decl_Id);
4711 Inherit_Context (Gen_Decl, N);
4713 if Parent_Installed then
4714 Remove_Parent;
4715 end if;
4717 Restore_Env;
4718 Env_Installed := False;
4719 end if;
4721 Validate_Categorization_Dependency (N, Act_Decl_Id);
4723 -- There used to be a check here to prevent instantiations in local
4724 -- contexts if the No_Local_Allocators restriction was active. This
4725 -- check was removed by a binding interpretation in AI-95-00130/07,
4726 -- but we retain the code for documentation purposes.
4728 -- if Ekind (Act_Decl_Id) /= E_Void
4729 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4730 -- then
4731 -- Check_Restriction (No_Local_Allocators, N);
4732 -- end if;
4734 if Inline_Now then
4735 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4736 end if;
4738 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4739 -- be used as defining identifiers for a formal package and for the
4740 -- corresponding expanded package.
4742 if Nkind (N) = N_Formal_Package_Declaration then
4743 Act_Decl_Id := New_Copy (Defining_Entity (N));
4744 Set_Comes_From_Source (Act_Decl_Id, True);
4745 Set_Is_Generic_Instance (Act_Decl_Id, False);
4746 Set_Defining_Identifier (N, Act_Decl_Id);
4747 end if;
4749 -- Check that if N is an instantiation of System.Dim_Float_IO or
4750 -- System.Dim_Integer_IO, the formal type has a dimension system.
4752 if Nkind (N) = N_Package_Instantiation
4753 and then Is_Dim_IO_Package_Instantiation (N)
4754 then
4755 declare
4756 Assoc : constant Node_Id := First (Generic_Associations (N));
4757 begin
4758 if not Has_Dimension_System
4759 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4760 then
4761 Error_Msg_N ("type with a dimension system expected", Assoc);
4762 end if;
4763 end;
4764 end if;
4766 <<Leave>>
4767 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4768 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4769 end if;
4771 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4772 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4773 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4774 Style_Check := Saved_Style_Check;
4776 exception
4777 when Instantiation_Error =>
4778 if Parent_Installed then
4779 Remove_Parent;
4780 end if;
4782 if Env_Installed then
4783 Restore_Env;
4784 end if;
4786 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4787 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4788 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4789 Style_Check := Saved_Style_Check;
4790 end Analyze_Package_Instantiation;
4792 --------------------------
4793 -- Inline_Instance_Body --
4794 --------------------------
4796 -- WARNING: This routine manages SPARK regions. Return statements must be
4797 -- replaced by gotos which jump to the end of the routine and restore the
4798 -- SPARK mode.
4800 procedure Inline_Instance_Body
4801 (N : Node_Id;
4802 Gen_Unit : Entity_Id;
4803 Act_Decl : Node_Id)
4805 Config_Attrs : constant Config_Switches_Type := Save_Config_Switches;
4807 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4808 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4809 Gen_Comp : constant Entity_Id :=
4810 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4812 Scope_Stack_Depth : constant Pos :=
4813 Scope_Stack.Last - Scope_Stack.First + 1;
4815 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4816 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4817 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4819 Curr_Scope : Entity_Id := Empty;
4820 List : Elist_Id := No_Elist; -- init to avoid warning
4821 N_Instances : Nat := 0;
4822 Num_Inner : Nat := 0;
4823 Num_Scopes : Nat := 0;
4824 Removed : Boolean := False;
4825 S : Entity_Id;
4826 Vis : Boolean;
4828 begin
4829 -- Case of generic unit defined in another unit. We must remove the
4830 -- complete context of the current unit to install that of the generic.
4832 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4834 -- Add some comments for the following two loops ???
4836 S := Current_Scope;
4837 while Present (S) and then S /= Standard_Standard loop
4838 loop
4839 Num_Scopes := Num_Scopes + 1;
4841 Use_Clauses (Num_Scopes) :=
4842 (Scope_Stack.Table
4843 (Scope_Stack.Last - Num_Scopes + 1).
4844 First_Use_Clause);
4845 End_Use_Clauses (Use_Clauses (Num_Scopes));
4847 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4848 or else Scope_Stack.Table
4849 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4850 end loop;
4852 exit when Is_Generic_Instance (S)
4853 and then (In_Package_Body (S)
4854 or else Ekind (S) = E_Procedure
4855 or else Ekind (S) = E_Function);
4856 S := Scope (S);
4857 end loop;
4859 Vis := Is_Immediately_Visible (Gen_Comp);
4861 -- Find and save all enclosing instances
4863 S := Current_Scope;
4865 while Present (S)
4866 and then S /= Standard_Standard
4867 loop
4868 if Is_Generic_Instance (S) then
4869 N_Instances := N_Instances + 1;
4870 Instances (N_Instances) := S;
4872 exit when In_Package_Body (S);
4873 end if;
4875 S := Scope (S);
4876 end loop;
4878 -- Remove context of current compilation unit, unless we are within a
4879 -- nested package instantiation, in which case the context has been
4880 -- removed previously.
4882 -- If current scope is the body of a child unit, remove context of
4883 -- spec as well. If an enclosing scope is an instance body, the
4884 -- context has already been removed, but the entities in the body
4885 -- must be made invisible as well.
4887 S := Current_Scope;
4888 while Present (S) and then S /= Standard_Standard loop
4889 if Is_Generic_Instance (S)
4890 and then (In_Package_Body (S)
4891 or else Ekind_In (S, E_Procedure, E_Function))
4892 then
4893 -- We still have to remove the entities of the enclosing
4894 -- instance from direct visibility.
4896 declare
4897 E : Entity_Id;
4898 begin
4899 E := First_Entity (S);
4900 while Present (E) loop
4901 Set_Is_Immediately_Visible (E, False);
4902 Next_Entity (E);
4903 end loop;
4904 end;
4906 exit;
4907 end if;
4909 if S = Curr_Unit
4910 or else (Ekind (Curr_Unit) = E_Package_Body
4911 and then S = Spec_Entity (Curr_Unit))
4912 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4913 and then S = Corresponding_Spec
4914 (Unit_Declaration_Node (Curr_Unit)))
4915 then
4916 Removed := True;
4918 -- Remove entities in current scopes from visibility, so that
4919 -- instance body is compiled in a clean environment.
4921 List := Save_Scope_Stack (Handle_Use => False);
4923 if Is_Child_Unit (S) then
4925 -- Remove child unit from stack, as well as inner scopes.
4926 -- Removing the context of a child unit removes parent units
4927 -- as well.
4929 while Current_Scope /= S loop
4930 Num_Inner := Num_Inner + 1;
4931 Inner_Scopes (Num_Inner) := Current_Scope;
4932 Pop_Scope;
4933 end loop;
4935 Pop_Scope;
4936 Remove_Context (Curr_Comp);
4937 Curr_Scope := S;
4939 else
4940 Remove_Context (Curr_Comp);
4941 end if;
4943 if Ekind (Curr_Unit) = E_Package_Body then
4944 Remove_Context (Library_Unit (Curr_Comp));
4945 end if;
4946 end if;
4948 S := Scope (S);
4949 end loop;
4951 pragma Assert (Num_Inner < Num_Scopes);
4953 Push_Scope (Standard_Standard);
4954 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4956 -- The inlined package body is analyzed with the configuration state
4957 -- of the context prior to the scope manipulations performed above.
4959 -- ??? shouldn't this also use the warning state of the context prior
4960 -- to the scope manipulations?
4962 Instantiate_Package_Body
4963 (Body_Info =>
4964 ((Act_Decl => Act_Decl,
4965 Config_Switches => Config_Attrs,
4966 Current_Sem_Unit => Current_Sem_Unit,
4967 Expander_Status => Expander_Active,
4968 Inst_Node => N,
4969 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4970 Scope_Suppress => Scope_Suppress,
4971 Warnings => Save_Warnings)),
4972 Inlined_Body => True);
4974 Pop_Scope;
4976 -- Restore context
4978 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4980 -- Reset Generic_Instance flag so that use clauses can be installed
4981 -- in the proper order. (See Use_One_Package for effect of enclosing
4982 -- instances on processing of use clauses).
4984 for J in 1 .. N_Instances loop
4985 Set_Is_Generic_Instance (Instances (J), False);
4986 end loop;
4988 if Removed then
4989 Install_Context (Curr_Comp, Chain => False);
4991 if Present (Curr_Scope)
4992 and then Is_Child_Unit (Curr_Scope)
4993 then
4994 Push_Scope (Curr_Scope);
4995 Set_Is_Immediately_Visible (Curr_Scope);
4997 -- Finally, restore inner scopes as well
4999 for J in reverse 1 .. Num_Inner loop
5000 Push_Scope (Inner_Scopes (J));
5001 end loop;
5002 end if;
5004 Restore_Scope_Stack (List, Handle_Use => False);
5006 if Present (Curr_Scope)
5007 and then
5008 (In_Private_Part (Curr_Scope)
5009 or else In_Package_Body (Curr_Scope))
5010 then
5011 -- Install private declaration of ancestor units, which are
5012 -- currently available. Restore_Scope_Stack and Install_Context
5013 -- only install the visible part of parents.
5015 declare
5016 Par : Entity_Id;
5017 begin
5018 Par := Scope (Curr_Scope);
5019 while (Present (Par)) and then Par /= Standard_Standard loop
5020 Install_Private_Declarations (Par);
5021 Par := Scope (Par);
5022 end loop;
5023 end;
5024 end if;
5025 end if;
5027 -- Restore use clauses. For a child unit, use clauses in the parents
5028 -- are restored when installing the context, so only those in inner
5029 -- scopes (and those local to the child unit itself) need to be
5030 -- installed explicitly.
5032 if Is_Child_Unit (Curr_Unit) and then Removed then
5033 for J in reverse 1 .. Num_Inner + 1 loop
5034 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5035 Use_Clauses (J);
5036 Install_Use_Clauses (Use_Clauses (J));
5037 end loop;
5039 else
5040 for J in reverse 1 .. Num_Scopes loop
5041 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5042 Use_Clauses (J);
5043 Install_Use_Clauses (Use_Clauses (J));
5044 end loop;
5045 end if;
5047 -- Restore status of instances. If one of them is a body, make its
5048 -- local entities visible again.
5050 declare
5051 E : Entity_Id;
5052 Inst : Entity_Id;
5054 begin
5055 for J in 1 .. N_Instances loop
5056 Inst := Instances (J);
5057 Set_Is_Generic_Instance (Inst, True);
5059 if In_Package_Body (Inst)
5060 or else Ekind_In (S, E_Procedure, E_Function)
5061 then
5062 E := First_Entity (Instances (J));
5063 while Present (E) loop
5064 Set_Is_Immediately_Visible (E);
5065 Next_Entity (E);
5066 end loop;
5067 end if;
5068 end loop;
5069 end;
5071 -- If generic unit is in current unit, current context is correct. Note
5072 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5073 -- enclosing scopes were removed.
5075 else
5076 Instantiate_Package_Body
5077 (Body_Info =>
5078 ((Act_Decl => Act_Decl,
5079 Config_Switches => Save_Config_Switches,
5080 Current_Sem_Unit => Current_Sem_Unit,
5081 Expander_Status => Expander_Active,
5082 Inst_Node => N,
5083 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5084 Scope_Suppress => Scope_Suppress,
5085 Warnings => Save_Warnings)),
5086 Inlined_Body => True);
5087 end if;
5088 end Inline_Instance_Body;
5090 -------------------------------------
5091 -- Analyze_Procedure_Instantiation --
5092 -------------------------------------
5094 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5095 begin
5096 Analyze_Subprogram_Instantiation (N, E_Procedure);
5097 end Analyze_Procedure_Instantiation;
5099 -----------------------------------
5100 -- Need_Subprogram_Instance_Body --
5101 -----------------------------------
5103 function Need_Subprogram_Instance_Body
5104 (N : Node_Id;
5105 Subp : Entity_Id) return Boolean
5107 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
5108 -- Return True if E is an inlined subprogram, an inlined renaming or a
5109 -- subprogram nested in an inlined subprogram. The inlining machinery
5110 -- totally disregards nested subprograms since it considers that they
5111 -- will always be compiled if the parent is (see Inline.Is_Nested).
5113 ------------------------------------
5114 -- Is_Inlined_Or_Child_Of_Inlined --
5115 ------------------------------------
5117 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
5118 Scop : Entity_Id;
5120 begin
5121 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
5122 return True;
5123 end if;
5125 Scop := Scope (E);
5126 while Scop /= Standard_Standard loop
5127 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
5128 return True;
5129 end if;
5131 Scop := Scope (Scop);
5132 end loop;
5134 return False;
5135 end Is_Inlined_Or_Child_Of_Inlined;
5137 begin
5138 -- Must be in the main unit or inlined (or child of inlined)
5140 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
5142 -- Must be generating code or analyzing code in ASIS/GNATprove mode
5144 and then (Operating_Mode = Generate_Code
5145 or else (Operating_Mode = Check_Semantics
5146 and then (ASIS_Mode or GNATprove_Mode)))
5148 -- The body is needed when generating code (full expansion), in ASIS
5149 -- mode for other tools, and in GNATprove mode (special expansion) for
5150 -- formal verification of the body itself.
5152 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
5154 -- No point in inlining if ABE is inevitable
5156 and then not Is_Known_Guaranteed_ABE (N)
5158 -- Or if subprogram is eliminated
5160 and then not Is_Eliminated (Subp)
5161 then
5162 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
5163 return True;
5165 -- Here if not inlined, or we ignore the inlining
5167 else
5168 return False;
5169 end if;
5170 end Need_Subprogram_Instance_Body;
5172 --------------------------------------
5173 -- Analyze_Subprogram_Instantiation --
5174 --------------------------------------
5176 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5177 -- must be replaced by gotos which jump to the end of the routine in order
5178 -- to restore the Ghost and SPARK modes.
5180 procedure Analyze_Subprogram_Instantiation
5181 (N : Node_Id;
5182 K : Entity_Kind)
5184 Errs : constant Nat := Serious_Errors_Detected;
5185 Gen_Id : constant Node_Id := Name (N);
5186 Inst_Id : constant Entity_Id := Defining_Entity (N);
5187 Anon_Id : constant Entity_Id :=
5188 Make_Defining_Identifier (Sloc (Inst_Id),
5189 Chars => New_External_Name (Chars (Inst_Id), 'R'));
5190 Loc : constant Source_Ptr := Sloc (N);
5192 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
5193 Act_Decl : Node_Id;
5194 Act_Spec : Node_Id;
5195 Act_Tree : Node_Id;
5197 Env_Installed : Boolean := False;
5198 Gen_Unit : Entity_Id;
5199 Gen_Decl : Node_Id;
5200 Pack_Id : Entity_Id;
5201 Parent_Installed : Boolean := False;
5203 Renaming_List : List_Id;
5204 -- The list of declarations that link formals and actuals of the
5205 -- instance. These are subtype declarations for formal types, and
5206 -- renaming declarations for other formals. The subprogram declaration
5207 -- for the instance is then appended to the list, and the last item on
5208 -- the list is the renaming declaration for the instance.
5210 procedure Analyze_Instance_And_Renamings;
5211 -- The instance must be analyzed in a context that includes the mappings
5212 -- of generic parameters into actuals. We create a package declaration
5213 -- for this purpose, and a subprogram with an internal name within the
5214 -- package. The subprogram instance is simply an alias for the internal
5215 -- subprogram, declared in the current scope.
5217 procedure Build_Subprogram_Renaming;
5218 -- If the subprogram is recursive, there are occurrences of the name of
5219 -- the generic within the body, which must resolve to the current
5220 -- instance. We add a renaming declaration after the declaration, which
5221 -- is available in the instance body, as well as in the analysis of
5222 -- aspects that appear in the generic. This renaming declaration is
5223 -- inserted after the instance declaration which it renames.
5225 ------------------------------------
5226 -- Analyze_Instance_And_Renamings --
5227 ------------------------------------
5229 procedure Analyze_Instance_And_Renamings is
5230 Def_Ent : constant Entity_Id := Defining_Entity (N);
5231 Pack_Decl : Node_Id;
5233 begin
5234 if Nkind (Parent (N)) = N_Compilation_Unit then
5236 -- For the case of a compilation unit, the container package has
5237 -- the same name as the instantiation, to insure that the binder
5238 -- calls the elaboration procedure with the right name. Copy the
5239 -- entity of the instance, which may have compilation level flags
5240 -- (e.g. Is_Child_Unit) set.
5242 Pack_Id := New_Copy (Def_Ent);
5244 else
5245 -- Otherwise we use the name of the instantiation concatenated
5246 -- with its source position to ensure uniqueness if there are
5247 -- several instantiations with the same name.
5249 Pack_Id :=
5250 Make_Defining_Identifier (Loc,
5251 Chars => New_External_Name
5252 (Related_Id => Chars (Def_Ent),
5253 Suffix => "GP",
5254 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
5255 end if;
5257 Pack_Decl :=
5258 Make_Package_Declaration (Loc,
5259 Specification => Make_Package_Specification (Loc,
5260 Defining_Unit_Name => Pack_Id,
5261 Visible_Declarations => Renaming_List,
5262 End_Label => Empty));
5264 Set_Instance_Spec (N, Pack_Decl);
5265 Set_Is_Generic_Instance (Pack_Id);
5266 Set_Debug_Info_Needed (Pack_Id);
5268 -- Case of not a compilation unit
5270 if Nkind (Parent (N)) /= N_Compilation_Unit then
5271 Mark_Rewrite_Insertion (Pack_Decl);
5272 Insert_Before (N, Pack_Decl);
5273 Set_Has_Completion (Pack_Id);
5275 -- Case of an instantiation that is a compilation unit
5277 -- Place declaration on current node so context is complete for
5278 -- analysis (including nested instantiations), and for use in a
5279 -- context_clause (see Analyze_With_Clause).
5281 else
5282 Set_Unit (Parent (N), Pack_Decl);
5283 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
5284 end if;
5286 Analyze (Pack_Decl);
5287 Check_Formal_Packages (Pack_Id);
5288 Set_Is_Generic_Instance (Pack_Id, False);
5290 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
5291 -- above???
5293 -- Body of the enclosing package is supplied when instantiating the
5294 -- subprogram body, after semantic analysis is completed.
5296 if Nkind (Parent (N)) = N_Compilation_Unit then
5298 -- Remove package itself from visibility, so it does not
5299 -- conflict with subprogram.
5301 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5303 -- Set name and scope of internal subprogram so that the proper
5304 -- external name will be generated. The proper scope is the scope
5305 -- of the wrapper package. We need to generate debugging info for
5306 -- the internal subprogram, so set flag accordingly.
5308 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5309 Set_Scope (Anon_Id, Scope (Pack_Id));
5311 -- Mark wrapper package as referenced, to avoid spurious warnings
5312 -- if the instantiation appears in various with_ clauses of
5313 -- subunits of the main unit.
5315 Set_Referenced (Pack_Id);
5316 end if;
5318 Set_Is_Generic_Instance (Anon_Id);
5319 Set_Debug_Info_Needed (Anon_Id);
5320 Act_Decl_Id := New_Copy (Anon_Id);
5322 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5323 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5324 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5326 -- Subprogram instance comes from source only if generic does
5328 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
5330 -- If the instance is a child unit, mark the Id accordingly. Mark
5331 -- the anonymous entity as well, which is the real subprogram and
5332 -- which is used when the instance appears in a context clause.
5333 -- Similarly, propagate the Is_Eliminated flag to handle properly
5334 -- nested eliminated subprograms.
5336 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5337 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5338 New_Overloaded_Entity (Act_Decl_Id);
5339 Check_Eliminated (Act_Decl_Id);
5340 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5342 if Nkind (Parent (N)) = N_Compilation_Unit then
5344 -- In compilation unit case, kill elaboration checks on the
5345 -- instantiation, since they are never needed - the body is
5346 -- instantiated at the same point as the spec.
5348 if Legacy_Elaboration_Checks then
5349 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5350 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5351 end if;
5353 Set_Is_Compilation_Unit (Anon_Id);
5354 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5355 end if;
5357 -- The instance is not a freezing point for the new subprogram.
5358 -- The anonymous subprogram may have a freeze node, created for
5359 -- some delayed aspects. This freeze node must not be inherited
5360 -- by the visible subprogram entity.
5362 Set_Is_Frozen (Act_Decl_Id, False);
5363 Set_Freeze_Node (Act_Decl_Id, Empty);
5365 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5366 Valid_Operator_Definition (Act_Decl_Id);
5367 end if;
5369 Set_Alias (Act_Decl_Id, Anon_Id);
5370 Set_Has_Completion (Act_Decl_Id);
5371 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5373 if Nkind (Parent (N)) = N_Compilation_Unit then
5374 Set_Body_Required (Parent (N), False);
5375 end if;
5376 end Analyze_Instance_And_Renamings;
5378 -------------------------------
5379 -- Build_Subprogram_Renaming --
5380 -------------------------------
5382 procedure Build_Subprogram_Renaming is
5383 Renaming_Decl : Node_Id;
5384 Unit_Renaming : Node_Id;
5386 begin
5387 Unit_Renaming :=
5388 Make_Subprogram_Renaming_Declaration (Loc,
5389 Specification =>
5390 Copy_Generic_Node
5391 (Specification (Original_Node (Gen_Decl)),
5392 Empty,
5393 Instantiating => True),
5394 Name => New_Occurrence_Of (Anon_Id, Loc));
5396 -- The generic may be a a child unit. The renaming needs an
5397 -- identifier with the proper name.
5399 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5400 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5402 -- If there is a formal subprogram with the same name as the unit
5403 -- itself, do not add this renaming declaration, to prevent
5404 -- ambiguities when there is a call with that name in the body.
5405 -- This is a partial and ugly fix for one ACATS test. ???
5407 Renaming_Decl := First (Renaming_List);
5408 while Present (Renaming_Decl) loop
5409 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5410 and then
5411 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5412 then
5413 exit;
5414 end if;
5416 Next (Renaming_Decl);
5417 end loop;
5419 if No (Renaming_Decl) then
5420 Append (Unit_Renaming, Renaming_List);
5421 end if;
5422 end Build_Subprogram_Renaming;
5424 -- Local variables
5426 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5427 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
5428 Saved_ISMP : constant Boolean :=
5429 Ignore_SPARK_Mode_Pragmas_In_Instance;
5430 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
5431 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
5432 -- Save the Ghost and SPARK mode-related data to restore on exit
5434 Vis_Prims_List : Elist_Id := No_Elist;
5435 -- List of primitives made temporarily visible in the instantiation
5436 -- to match the visibility of the formal type
5438 -- Start of processing for Analyze_Subprogram_Instantiation
5440 begin
5441 -- Preserve relevant elaboration-related attributes of the context which
5442 -- are no longer available or very expensive to recompute once analysis,
5443 -- resolution, and expansion are over.
5445 Mark_Elaboration_Attributes
5446 (N_Id => N,
5447 Checks => True,
5448 Level => True,
5449 Modes => True,
5450 Warnings => True);
5452 Check_SPARK_05_Restriction ("generic is not allowed", N);
5454 -- Very first thing: check for special Text_IO unit in case we are
5455 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5456 -- such an instantiation is bogus (these are packages, not subprograms),
5457 -- but we get a better error message if we do this.
5459 Check_Text_IO_Special_Unit (Gen_Id);
5461 -- Make node global for error reporting
5463 Instantiation_Node := N;
5465 -- For package instantiations we turn off style checks, because they
5466 -- will have been emitted in the generic. For subprogram instantiations
5467 -- we want to apply at least the check on overriding indicators so we
5468 -- do not modify the style check status.
5470 -- The renaming declarations for the actuals do not come from source and
5471 -- will not generate spurious warnings.
5473 Preanalyze_Actuals (N);
5475 Init_Env;
5476 Env_Installed := True;
5477 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5478 Gen_Unit := Entity (Gen_Id);
5480 -- A subprogram instantiation is Ghost when it is subject to pragma
5481 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5482 -- that any nodes generated during analysis and expansion are marked as
5483 -- Ghost.
5485 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
5487 Generate_Reference (Gen_Unit, Gen_Id);
5489 if Nkind (Gen_Id) = N_Identifier
5490 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5491 then
5492 Error_Msg_NE
5493 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5494 end if;
5496 if Etype (Gen_Unit) = Any_Type then
5497 Restore_Env;
5498 goto Leave;
5499 end if;
5501 -- Verify that it is a generic subprogram of the right kind, and that
5502 -- it does not lead to a circular instantiation.
5504 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5505 Error_Msg_NE
5506 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5508 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5509 Error_Msg_NE
5510 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5512 elsif In_Open_Scopes (Gen_Unit) then
5513 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5515 else
5516 Set_Ekind (Inst_Id, K);
5517 Set_Scope (Inst_Id, Current_Scope);
5519 Set_Entity (Gen_Id, Gen_Unit);
5520 Set_Is_Instantiated (Gen_Unit);
5522 if In_Extended_Main_Source_Unit (N) then
5523 Generate_Reference (Gen_Unit, N);
5524 end if;
5526 -- If renaming, get original unit
5528 if Present (Renamed_Object (Gen_Unit))
5529 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5530 E_Generic_Function)
5531 then
5532 Gen_Unit := Renamed_Object (Gen_Unit);
5533 Set_Is_Instantiated (Gen_Unit);
5534 Generate_Reference (Gen_Unit, N);
5535 end if;
5537 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5538 Error_Msg_Node_2 := Current_Scope;
5539 Error_Msg_NE
5540 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5541 Circularity_Detected := True;
5542 Restore_Hidden_Primitives (Vis_Prims_List);
5543 goto Leave;
5544 end if;
5546 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5548 -- Initialize renamings map, for error checking
5550 Generic_Renamings.Set_Last (0);
5551 Generic_Renamings_HTable.Reset;
5553 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5555 -- Copy original generic tree, to produce text for instantiation
5557 Act_Tree :=
5558 Copy_Generic_Node
5559 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5561 -- Inherit overriding indicator from instance node
5563 Act_Spec := Specification (Act_Tree);
5564 Set_Must_Override (Act_Spec, Must_Override (N));
5565 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5567 Renaming_List :=
5568 Analyze_Associations
5569 (I_Node => N,
5570 Formals => Generic_Formal_Declarations (Act_Tree),
5571 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5573 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5575 -- The subprogram itself cannot contain a nested instance, so the
5576 -- current parent is left empty.
5578 Set_Instance_Env (Gen_Unit, Empty);
5580 -- Build the subprogram declaration, which does not appear in the
5581 -- generic template, and give it a sloc consistent with that of the
5582 -- template.
5584 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5585 Set_Generic_Parent (Act_Spec, Gen_Unit);
5586 Act_Decl :=
5587 Make_Subprogram_Declaration (Sloc (Act_Spec),
5588 Specification => Act_Spec);
5590 -- The aspects have been copied previously, but they have to be
5591 -- linked explicitly to the new subprogram declaration. Explicit
5592 -- pre/postconditions on the instance are analyzed below, in a
5593 -- separate step.
5595 Move_Aspects (Act_Tree, To => Act_Decl);
5596 Set_Categorization_From_Pragmas (Act_Decl);
5598 if Parent_Installed then
5599 Hide_Current_Scope;
5600 end if;
5602 Append (Act_Decl, Renaming_List);
5604 -- Contract-related source pragmas that follow a generic subprogram
5605 -- must be instantiated explicitly because they are not part of the
5606 -- subprogram template.
5608 Instantiate_Subprogram_Contract
5609 (Original_Node (Gen_Decl), Renaming_List);
5611 Build_Subprogram_Renaming;
5613 -- If the context of the instance is subject to SPARK_Mode "off" or
5614 -- the annotation is altogether missing, set the global flag which
5615 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5616 -- the instance. This should be done prior to analyzing the instance.
5618 if SPARK_Mode /= On then
5619 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5620 end if;
5622 -- If the context of an instance is not subject to SPARK_Mode "off",
5623 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5624 -- the latter should be the one applicable to the instance.
5626 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5627 and then Saved_SM /= Off
5628 and then Present (SPARK_Pragma (Gen_Unit))
5629 then
5630 Set_SPARK_Mode (Gen_Unit);
5631 end if;
5633 Analyze_Instance_And_Renamings;
5635 -- Restore SPARK_Mode from the context after analysis of the package
5636 -- declaration, so that the SPARK_Mode on the generic spec does not
5637 -- apply to the pending instance for the instance body.
5639 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5640 and then Saved_SM /= Off
5641 and then Present (SPARK_Pragma (Gen_Unit))
5642 then
5643 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5644 end if;
5646 -- If the generic is marked Import (Intrinsic), then so is the
5647 -- instance. This indicates that there is no body to instantiate. If
5648 -- generic is marked inline, so it the instance, and the anonymous
5649 -- subprogram it renames. If inlined, or else if inlining is enabled
5650 -- for the compilation, we generate the instance body even if it is
5651 -- not within the main unit.
5653 if Is_Intrinsic_Subprogram (Gen_Unit) then
5654 Set_Is_Intrinsic_Subprogram (Anon_Id);
5655 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5657 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5658 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5659 end if;
5660 end if;
5662 -- Inherit convention from generic unit. Intrinsic convention, as for
5663 -- an instance of unchecked conversion, is not inherited because an
5664 -- explicit Ada instance has been created.
5666 if Has_Convention_Pragma (Gen_Unit)
5667 and then Convention (Gen_Unit) /= Convention_Intrinsic
5668 then
5669 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5670 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5671 end if;
5673 Generate_Definition (Act_Decl_Id);
5675 -- Inherit all inlining-related flags which apply to the generic in
5676 -- the subprogram and its declaration.
5678 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5679 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5681 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5682 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5684 Set_Has_Pragma_Inline_Always
5685 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5686 Set_Has_Pragma_Inline_Always
5687 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5689 Set_Has_Pragma_No_Inline
5690 (Act_Decl_Id, Has_Pragma_No_Inline (Gen_Unit));
5691 Set_Has_Pragma_No_Inline
5692 (Anon_Id, Has_Pragma_No_Inline (Gen_Unit));
5694 -- Propagate No_Return if pragma applied to generic unit. This must
5695 -- be done explicitly because pragma does not appear in generic
5696 -- declaration (unlike the aspect case).
5698 if No_Return (Gen_Unit) then
5699 Set_No_Return (Act_Decl_Id);
5700 Set_No_Return (Anon_Id);
5701 end if;
5703 -- Mark both the instance spec and the anonymous package in case the
5704 -- body is instantiated at a later pass. This preserves the original
5705 -- context in effect for the body.
5707 if SPARK_Mode /= On then
5708 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
5709 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
5710 end if;
5712 if Legacy_Elaboration_Checks
5713 and then not Is_Intrinsic_Subprogram (Gen_Unit)
5714 then
5715 Check_Elab_Instantiation (N);
5716 end if;
5718 -- Save the scenario for later examination by the ABE Processing
5719 -- phase.
5721 Record_Elaboration_Scenario (N);
5723 -- The instantiation results in a guaranteed ABE. Create a completing
5724 -- body for the subprogram declaration because the real body will not
5725 -- be instantiated.
5727 if Is_Known_Guaranteed_ABE (N) then
5728 Provide_Completing_Bodies (Instance_Spec (N));
5729 end if;
5731 if Is_Dispatching_Operation (Act_Decl_Id)
5732 and then Ada_Version >= Ada_2005
5733 then
5734 declare
5735 Formal : Entity_Id;
5737 begin
5738 Formal := First_Formal (Act_Decl_Id);
5739 while Present (Formal) loop
5740 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5741 and then Is_Controlling_Formal (Formal)
5742 and then not Can_Never_Be_Null (Formal)
5743 then
5744 Error_Msg_NE
5745 ("access parameter& is controlling,", N, Formal);
5746 Error_Msg_NE
5747 ("\corresponding parameter of & must be explicitly "
5748 & "null-excluding", N, Gen_Id);
5749 end if;
5751 Next_Formal (Formal);
5752 end loop;
5753 end;
5754 end if;
5756 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5758 Validate_Categorization_Dependency (N, Act_Decl_Id);
5760 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5761 Inherit_Context (Gen_Decl, N);
5763 Restore_Private_Views (Pack_Id, False);
5765 -- If the context requires a full instantiation, mark node for
5766 -- subsequent construction of the body.
5768 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5769 Check_Forward_Instantiation (Gen_Decl);
5771 -- The wrapper package is always delayed, because it does not
5772 -- constitute a freeze point, but to insure that the freeze node
5773 -- is placed properly, it is created directly when instantiating
5774 -- the body (otherwise the freeze node might appear to early for
5775 -- nested instantiations). For ASIS purposes, indicate that the
5776 -- wrapper package has replaced the instantiation node.
5778 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5779 Rewrite (N, Unit (Parent (N)));
5780 Set_Unit (Parent (N), N);
5781 end if;
5783 -- Replace instance node for library-level instantiations of
5784 -- intrinsic subprograms, for ASIS use.
5786 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5787 Rewrite (N, Unit (Parent (N)));
5788 Set_Unit (Parent (N), N);
5789 end if;
5791 if Parent_Installed then
5792 Remove_Parent;
5793 end if;
5795 Restore_Hidden_Primitives (Vis_Prims_List);
5796 Restore_Env;
5797 Env_Installed := False;
5798 Generic_Renamings.Set_Last (0);
5799 Generic_Renamings_HTable.Reset;
5800 end if;
5802 <<Leave>>
5803 -- Analyze aspects in declaration if no errors appear in the instance.
5805 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
5806 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5807 end if;
5809 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5810 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5811 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5813 exception
5814 when Instantiation_Error =>
5815 if Parent_Installed then
5816 Remove_Parent;
5817 end if;
5819 if Env_Installed then
5820 Restore_Env;
5821 end if;
5823 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5824 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5825 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5826 end Analyze_Subprogram_Instantiation;
5828 -------------------------
5829 -- Get_Associated_Node --
5830 -------------------------
5832 function Get_Associated_Node (N : Node_Id) return Node_Id is
5833 Assoc : Node_Id;
5835 begin
5836 Assoc := Associated_Node (N);
5838 if Nkind (Assoc) /= Nkind (N) then
5839 return Assoc;
5841 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5842 return Assoc;
5844 else
5845 -- If the node is part of an inner generic, it may itself have been
5846 -- remapped into a further generic copy. Associated_Node is otherwise
5847 -- used for the entity of the node, and will be of a different node
5848 -- kind, or else N has been rewritten as a literal or function call.
5850 while Present (Associated_Node (Assoc))
5851 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5852 loop
5853 Assoc := Associated_Node (Assoc);
5854 end loop;
5856 -- Follow an additional link in case the final node was rewritten.
5857 -- This can only happen with nested generic units.
5859 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5860 and then Present (Associated_Node (Assoc))
5861 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5862 N_Explicit_Dereference,
5863 N_Integer_Literal,
5864 N_Real_Literal,
5865 N_String_Literal))
5866 then
5867 Assoc := Associated_Node (Assoc);
5868 end if;
5870 -- An additional special case: an unconstrained type in an object
5871 -- declaration may have been rewritten as a local subtype constrained
5872 -- by the expression in the declaration. We need to recover the
5873 -- original entity, which may be global.
5875 if Present (Original_Node (Assoc))
5876 and then Nkind (Parent (N)) = N_Object_Declaration
5877 then
5878 Assoc := Original_Node (Assoc);
5879 end if;
5881 return Assoc;
5882 end if;
5883 end Get_Associated_Node;
5885 ----------------------------
5886 -- Build_Function_Wrapper --
5887 ----------------------------
5889 function Build_Function_Wrapper
5890 (Formal_Subp : Entity_Id;
5891 Actual_Subp : Entity_Id) return Node_Id
5893 Loc : constant Source_Ptr := Sloc (Current_Scope);
5894 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5895 Actuals : List_Id;
5896 Decl : Node_Id;
5897 Func_Name : Node_Id;
5898 Func : Entity_Id;
5899 Parm_Type : Node_Id;
5900 Profile : List_Id := New_List;
5901 Spec : Node_Id;
5902 Act_F : Entity_Id;
5903 Form_F : Entity_Id;
5904 New_F : Entity_Id;
5906 begin
5907 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5909 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5910 Set_Ekind (Func, E_Function);
5911 Set_Is_Generic_Actual_Subprogram (Func);
5913 Actuals := New_List;
5914 Profile := New_List;
5916 Act_F := First_Formal (Actual_Subp);
5917 Form_F := First_Formal (Formal_Subp);
5918 while Present (Form_F) loop
5920 -- Create new formal for profile of wrapper, and add a reference
5921 -- to it in the list of actuals for the enclosing call. The name
5922 -- must be that of the formal in the formal subprogram, because
5923 -- calls to it in the generic body may use named associations.
5925 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5927 Parm_Type :=
5928 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5930 Append_To (Profile,
5931 Make_Parameter_Specification (Loc,
5932 Defining_Identifier => New_F,
5933 Parameter_Type => Parm_Type));
5935 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5936 Next_Formal (Form_F);
5938 if Present (Act_F) then
5939 Next_Formal (Act_F);
5940 end if;
5941 end loop;
5943 Spec :=
5944 Make_Function_Specification (Loc,
5945 Defining_Unit_Name => Func,
5946 Parameter_Specifications => Profile,
5947 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5949 Decl :=
5950 Make_Expression_Function (Loc,
5951 Specification => Spec,
5952 Expression =>
5953 Make_Function_Call (Loc,
5954 Name => Func_Name,
5955 Parameter_Associations => Actuals));
5957 return Decl;
5958 end Build_Function_Wrapper;
5960 ----------------------------
5961 -- Build_Operator_Wrapper --
5962 ----------------------------
5964 function Build_Operator_Wrapper
5965 (Formal_Subp : Entity_Id;
5966 Actual_Subp : Entity_Id) return Node_Id
5968 Loc : constant Source_Ptr := Sloc (Current_Scope);
5969 Ret_Type : constant Entity_Id :=
5970 Get_Instance_Of (Etype (Formal_Subp));
5971 Op_Type : constant Entity_Id :=
5972 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
5973 Is_Binary : constant Boolean :=
5974 Present (Next_Formal (First_Formal (Formal_Subp)));
5976 Decl : Node_Id;
5977 Expr : Node_Id := Empty;
5978 F1, F2 : Entity_Id;
5979 Func : Entity_Id;
5980 Op_Name : Name_Id;
5981 Spec : Node_Id;
5982 L, R : Node_Id;
5984 begin
5985 Op_Name := Chars (Actual_Subp);
5987 -- Create entities for wrapper function and its formals
5989 F1 := Make_Temporary (Loc, 'A');
5990 F2 := Make_Temporary (Loc, 'B');
5991 L := New_Occurrence_Of (F1, Loc);
5992 R := New_Occurrence_Of (F2, Loc);
5994 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5995 Set_Ekind (Func, E_Function);
5996 Set_Is_Generic_Actual_Subprogram (Func);
5998 Spec :=
5999 Make_Function_Specification (Loc,
6000 Defining_Unit_Name => Func,
6001 Parameter_Specifications => New_List (
6002 Make_Parameter_Specification (Loc,
6003 Defining_Identifier => F1,
6004 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
6005 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6007 if Is_Binary then
6008 Append_To (Parameter_Specifications (Spec),
6009 Make_Parameter_Specification (Loc,
6010 Defining_Identifier => F2,
6011 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
6012 end if;
6014 -- Build expression as a function call, or as an operator node
6015 -- that corresponds to the name of the actual, starting with
6016 -- binary operators.
6018 if Op_Name not in Any_Operator_Name then
6019 Expr :=
6020 Make_Function_Call (Loc,
6021 Name =>
6022 New_Occurrence_Of (Actual_Subp, Loc),
6023 Parameter_Associations => New_List (L));
6025 if Is_Binary then
6026 Append_To (Parameter_Associations (Expr), R);
6027 end if;
6029 -- Binary operators
6031 elsif Is_Binary then
6032 if Op_Name = Name_Op_And then
6033 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
6034 elsif Op_Name = Name_Op_Or then
6035 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
6036 elsif Op_Name = Name_Op_Xor then
6037 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
6038 elsif Op_Name = Name_Op_Eq then
6039 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
6040 elsif Op_Name = Name_Op_Ne then
6041 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
6042 elsif Op_Name = Name_Op_Le then
6043 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
6044 elsif Op_Name = Name_Op_Gt then
6045 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
6046 elsif Op_Name = Name_Op_Ge then
6047 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
6048 elsif Op_Name = Name_Op_Lt then
6049 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
6050 elsif Op_Name = Name_Op_Add then
6051 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
6052 elsif Op_Name = Name_Op_Subtract then
6053 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
6054 elsif Op_Name = Name_Op_Concat then
6055 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
6056 elsif Op_Name = Name_Op_Multiply then
6057 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
6058 elsif Op_Name = Name_Op_Divide then
6059 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
6060 elsif Op_Name = Name_Op_Mod then
6061 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
6062 elsif Op_Name = Name_Op_Rem then
6063 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
6064 elsif Op_Name = Name_Op_Expon then
6065 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
6066 end if;
6068 -- Unary operators
6070 else
6071 if Op_Name = Name_Op_Add then
6072 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
6073 elsif Op_Name = Name_Op_Subtract then
6074 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
6075 elsif Op_Name = Name_Op_Abs then
6076 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
6077 elsif Op_Name = Name_Op_Not then
6078 Expr := Make_Op_Not (Loc, Right_Opnd => L);
6079 end if;
6080 end if;
6082 Decl :=
6083 Make_Expression_Function (Loc,
6084 Specification => Spec,
6085 Expression => Expr);
6087 return Decl;
6088 end Build_Operator_Wrapper;
6090 -------------------------------------------
6091 -- Build_Instance_Compilation_Unit_Nodes --
6092 -------------------------------------------
6094 procedure Build_Instance_Compilation_Unit_Nodes
6095 (N : Node_Id;
6096 Act_Body : Node_Id;
6097 Act_Decl : Node_Id)
6099 Decl_Cunit : Node_Id;
6100 Body_Cunit : Node_Id;
6101 Citem : Node_Id;
6102 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6103 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6105 begin
6106 -- A new compilation unit node is built for the instance declaration
6108 Decl_Cunit :=
6109 Make_Compilation_Unit (Sloc (N),
6110 Context_Items => Empty_List,
6111 Unit => Act_Decl,
6112 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
6114 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6116 -- The new compilation unit is linked to its body, but both share the
6117 -- same file, so we do not set Body_Required on the new unit so as not
6118 -- to create a spurious dependency on a non-existent body in the ali.
6119 -- This simplifies CodePeer unit traversal.
6121 -- We use the original instantiation compilation unit as the resulting
6122 -- compilation unit of the instance, since this is the main unit.
6124 Rewrite (N, Act_Body);
6126 -- Propagate the aspect specifications from the package body template to
6127 -- the instantiated version of the package body.
6129 if Has_Aspects (Act_Body) then
6130 Set_Aspect_Specifications
6131 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
6132 end if;
6134 Body_Cunit := Parent (N);
6136 -- The two compilation unit nodes are linked by the Library_Unit field
6138 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6139 Set_Library_Unit (Body_Cunit, Decl_Cunit);
6141 -- Preserve the private nature of the package if needed
6143 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
6145 -- If the instance is not the main unit, its context, categorization
6146 -- and elaboration entity are not relevant to the compilation.
6148 if Body_Cunit /= Cunit (Main_Unit) then
6149 Make_Instance_Unit (Body_Cunit, In_Main => False);
6150 return;
6151 end if;
6153 -- The context clause items on the instantiation, which are now attached
6154 -- to the body compilation unit (since the body overwrote the original
6155 -- instantiation node), semantically belong on the spec, so copy them
6156 -- there. It's harmless to leave them on the body as well. In fact one
6157 -- could argue that they belong in both places.
6159 Citem := First (Context_Items (Body_Cunit));
6160 while Present (Citem) loop
6161 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
6162 Next (Citem);
6163 end loop;
6165 -- Propagate categorization flags on packages, so that they appear in
6166 -- the ali file for the spec of the unit.
6168 if Ekind (New_Main) = E_Package then
6169 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
6170 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
6171 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
6172 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
6173 Set_Is_Remote_Call_Interface
6174 (Old_Main, Is_Remote_Call_Interface (New_Main));
6175 end if;
6177 -- Make entry in Units table, so that binder can generate call to
6178 -- elaboration procedure for body, if any.
6180 Make_Instance_Unit (Body_Cunit, In_Main => True);
6181 Main_Unit_Entity := New_Main;
6182 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
6184 -- Build elaboration entity, since the instance may certainly generate
6185 -- elaboration code requiring a flag for protection.
6187 Build_Elaboration_Entity (Decl_Cunit, New_Main);
6188 end Build_Instance_Compilation_Unit_Nodes;
6190 -----------------------------
6191 -- Check_Access_Definition --
6192 -----------------------------
6194 procedure Check_Access_Definition (N : Node_Id) is
6195 begin
6196 pragma Assert
6197 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
6198 null;
6199 end Check_Access_Definition;
6201 -----------------------------------
6202 -- Check_Formal_Package_Instance --
6203 -----------------------------------
6205 -- If the formal has specific parameters, they must match those of the
6206 -- actual. Both of them are instances, and the renaming declarations for
6207 -- their formal parameters appear in the same order in both. The analyzed
6208 -- formal has been analyzed in the context of the current instance.
6210 procedure Check_Formal_Package_Instance
6211 (Formal_Pack : Entity_Id;
6212 Actual_Pack : Entity_Id)
6214 E1 : Entity_Id := First_Entity (Actual_Pack);
6215 E2 : Entity_Id := First_Entity (Formal_Pack);
6216 Prev_E1 : Entity_Id;
6218 Expr1 : Node_Id;
6219 Expr2 : Node_Id;
6221 procedure Check_Mismatch (B : Boolean);
6222 -- Common error routine for mismatch between the parameters of the
6223 -- actual instance and those of the formal package.
6225 function Is_Defaulted (Param : Entity_Id) return Boolean;
6226 -- If the formal package has partly box-initialized formals, skip
6227 -- conformance check for these formals. Previously the code assumed
6228 -- that box initialization for a formal package applied to all its
6229 -- formal parameters.
6231 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
6232 -- The formal may come from a nested formal package, and the actual may
6233 -- have been constant-folded. To determine whether the two denote the
6234 -- same entity we may have to traverse several definitions to recover
6235 -- the ultimate entity that they refer to.
6237 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
6238 -- The formal and the actual must be identical, but if both are
6239 -- given by attributes they end up renaming different generated bodies,
6240 -- and we must verify that the attributes themselves match.
6242 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
6243 -- Similarly, if the formal comes from a nested formal package, the
6244 -- actual may designate the formal through multiple renamings, which
6245 -- have to be followed to determine the original variable in question.
6247 --------------------
6248 -- Check_Mismatch --
6249 --------------------
6251 procedure Check_Mismatch (B : Boolean) is
6252 -- A Formal_Type_Declaration for a derived private type is rewritten
6253 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6254 -- which is why we examine the original node.
6256 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
6258 begin
6259 if Kind = N_Formal_Type_Declaration then
6260 return;
6262 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
6263 N_Formal_Package_Declaration)
6264 or else Kind in N_Formal_Subprogram_Declaration
6265 then
6266 null;
6268 -- Ada 2012: If both formal and actual are incomplete types they
6269 -- are conformant.
6271 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
6272 null;
6274 elsif B then
6275 Error_Msg_NE
6276 ("actual for & in actual instance does not match formal",
6277 Parent (Actual_Pack), E1);
6278 end if;
6279 end Check_Mismatch;
6281 ------------------
6282 -- Is_Defaulted --
6283 ------------------
6285 function Is_Defaulted (Param : Entity_Id) return Boolean is
6286 Assoc : Node_Id;
6288 begin
6289 Assoc :=
6290 First (Generic_Associations (Parent
6291 (Associated_Formal_Package (Actual_Pack))));
6293 while Present (Assoc) loop
6294 if Nkind (Assoc) = N_Others_Choice then
6295 return True;
6297 elsif Nkind (Assoc) = N_Generic_Association
6298 and then Chars (Selector_Name (Assoc)) = Chars (Param)
6299 then
6300 return Box_Present (Assoc);
6301 end if;
6303 Next (Assoc);
6304 end loop;
6306 return False;
6307 end Is_Defaulted;
6309 --------------------------------
6310 -- Same_Instantiated_Constant --
6311 --------------------------------
6313 function Same_Instantiated_Constant
6314 (E1, E2 : Entity_Id) return Boolean
6316 Ent : Entity_Id;
6318 begin
6319 Ent := E2;
6320 while Present (Ent) loop
6321 if E1 = Ent then
6322 return True;
6324 elsif Ekind (Ent) /= E_Constant then
6325 return False;
6327 elsif Is_Entity_Name (Constant_Value (Ent)) then
6328 if Entity (Constant_Value (Ent)) = E1 then
6329 return True;
6330 else
6331 Ent := Entity (Constant_Value (Ent));
6332 end if;
6334 -- The actual may be a constant that has been folded. Recover
6335 -- original name.
6337 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
6338 Ent := Entity (Original_Node (Constant_Value (Ent)));
6340 else
6341 return False;
6342 end if;
6343 end loop;
6345 return False;
6346 end Same_Instantiated_Constant;
6348 --------------------------------
6349 -- Same_Instantiated_Function --
6350 --------------------------------
6352 function Same_Instantiated_Function
6353 (E1, E2 : Entity_Id) return Boolean
6355 U1, U2 : Node_Id;
6356 begin
6357 if Alias (E1) = Alias (E2) then
6358 return True;
6360 elsif Present (Alias (E2)) then
6361 U1 := Original_Node (Unit_Declaration_Node (E1));
6362 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6364 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6365 and then Nkind (Name (U1)) = N_Attribute_Reference
6367 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6368 and then Nkind (Name (U2)) = N_Attribute_Reference
6370 and then
6371 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6372 else
6373 return False;
6374 end if;
6375 end Same_Instantiated_Function;
6377 --------------------------------
6378 -- Same_Instantiated_Variable --
6379 --------------------------------
6381 function Same_Instantiated_Variable
6382 (E1, E2 : Entity_Id) return Boolean
6384 function Original_Entity (E : Entity_Id) return Entity_Id;
6385 -- Follow chain of renamings to the ultimate ancestor
6387 ---------------------
6388 -- Original_Entity --
6389 ---------------------
6391 function Original_Entity (E : Entity_Id) return Entity_Id is
6392 Orig : Entity_Id;
6394 begin
6395 Orig := E;
6396 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6397 and then Present (Renamed_Object (Orig))
6398 and then Is_Entity_Name (Renamed_Object (Orig))
6399 loop
6400 Orig := Entity (Renamed_Object (Orig));
6401 end loop;
6403 return Orig;
6404 end Original_Entity;
6406 -- Start of processing for Same_Instantiated_Variable
6408 begin
6409 return Ekind (E1) = Ekind (E2)
6410 and then Original_Entity (E1) = Original_Entity (E2);
6411 end Same_Instantiated_Variable;
6413 -- Start of processing for Check_Formal_Package_Instance
6415 begin
6416 Prev_E1 := E1;
6417 while Present (E1) and then Present (E2) loop
6418 exit when Ekind (E1) = E_Package
6419 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6421 -- If the formal is the renaming of the formal package, this
6422 -- is the end of its formal part, which may occur before the
6423 -- end of the formal part in the actual in the presence of
6424 -- defaulted parameters in the formal package.
6426 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6427 and then Renamed_Entity (E2) = Scope (E2);
6429 -- The analysis of the actual may generate additional internal
6430 -- entities. If the formal is defaulted, there is no corresponding
6431 -- analysis and the internal entities must be skipped, until we
6432 -- find corresponding entities again.
6434 if Comes_From_Source (E2)
6435 and then not Comes_From_Source (E1)
6436 and then Chars (E1) /= Chars (E2)
6437 then
6438 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6439 Next_Entity (E1);
6440 end loop;
6441 end if;
6443 if No (E1) then
6444 return;
6446 -- Entities may be declared without full declaration, such as
6447 -- itypes and predefined operators (concatenation for arrays, eg).
6448 -- Skip it and keep the formal entity to find a later match for it.
6450 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6451 E1 := Prev_E1;
6452 goto Next_E;
6454 -- If the formal entity comes from a formal declaration, it was
6455 -- defaulted in the formal package, and no check is needed on it.
6457 elsif Nkind_In (Original_Node (Parent (E2)),
6458 N_Formal_Object_Declaration,
6459 N_Formal_Type_Declaration)
6460 then
6461 -- If the formal is a tagged type the corresponding class-wide
6462 -- type has been generated as well, and it must be skipped.
6464 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6465 Next_Entity (E2);
6466 end if;
6468 goto Next_E;
6470 -- Ditto for defaulted formal subprograms.
6472 elsif Is_Overloadable (E1)
6473 and then Nkind (Unit_Declaration_Node (E2)) in
6474 N_Formal_Subprogram_Declaration
6475 then
6476 goto Next_E;
6478 elsif Is_Defaulted (E1) then
6479 goto Next_E;
6481 elsif Is_Type (E1) then
6483 -- Subtypes must statically match. E1, E2 are the local entities
6484 -- that are subtypes of the actuals. Itypes generated for other
6485 -- parameters need not be checked, the check will be performed
6486 -- on the parameters themselves.
6488 -- If E2 is a formal type declaration, it is a defaulted parameter
6489 -- and needs no checking.
6491 if not Is_Itype (E1) and then not Is_Itype (E2) then
6492 Check_Mismatch
6493 (not Is_Type (E2)
6494 or else Etype (E1) /= Etype (E2)
6495 or else not Subtypes_Statically_Match (E1, E2));
6496 end if;
6498 elsif Ekind (E1) = E_Constant then
6500 -- IN parameters must denote the same static value, or the same
6501 -- constant, or the literal null.
6503 Expr1 := Expression (Parent (E1));
6505 if Ekind (E2) /= E_Constant then
6506 Check_Mismatch (True);
6507 goto Next_E;
6508 else
6509 Expr2 := Expression (Parent (E2));
6510 end if;
6512 if Is_OK_Static_Expression (Expr1) then
6513 if not Is_OK_Static_Expression (Expr2) then
6514 Check_Mismatch (True);
6516 elsif Is_Discrete_Type (Etype (E1)) then
6517 declare
6518 V1 : constant Uint := Expr_Value (Expr1);
6519 V2 : constant Uint := Expr_Value (Expr2);
6520 begin
6521 Check_Mismatch (V1 /= V2);
6522 end;
6524 elsif Is_Real_Type (Etype (E1)) then
6525 declare
6526 V1 : constant Ureal := Expr_Value_R (Expr1);
6527 V2 : constant Ureal := Expr_Value_R (Expr2);
6528 begin
6529 Check_Mismatch (V1 /= V2);
6530 end;
6532 elsif Is_String_Type (Etype (E1))
6533 and then Nkind (Expr1) = N_String_Literal
6534 then
6535 if Nkind (Expr2) /= N_String_Literal then
6536 Check_Mismatch (True);
6537 else
6538 Check_Mismatch
6539 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6540 end if;
6541 end if;
6543 elsif Is_Entity_Name (Expr1) then
6544 if Is_Entity_Name (Expr2) then
6545 if Entity (Expr1) = Entity (Expr2) then
6546 null;
6547 else
6548 Check_Mismatch
6549 (not Same_Instantiated_Constant
6550 (Entity (Expr1), Entity (Expr2)));
6551 end if;
6553 else
6554 Check_Mismatch (True);
6555 end if;
6557 elsif Is_Entity_Name (Original_Node (Expr1))
6558 and then Is_Entity_Name (Expr2)
6559 and then Same_Instantiated_Constant
6560 (Entity (Original_Node (Expr1)), Entity (Expr2))
6561 then
6562 null;
6564 elsif Nkind (Expr1) = N_Null then
6565 Check_Mismatch (Nkind (Expr1) /= N_Null);
6567 else
6568 Check_Mismatch (True);
6569 end if;
6571 elsif Ekind (E1) = E_Variable then
6572 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6574 elsif Ekind (E1) = E_Package then
6575 Check_Mismatch
6576 (Ekind (E1) /= Ekind (E2)
6577 or else (Present (Renamed_Object (E2))
6578 and then Renamed_Object (E1) /=
6579 Renamed_Object (E2)));
6581 elsif Is_Overloadable (E1) then
6582 -- Verify that the actual subprograms match. Note that actuals
6583 -- that are attributes are rewritten as subprograms. If the
6584 -- subprogram in the formal package is defaulted, no check is
6585 -- needed. Note that this can only happen in Ada 2005 when the
6586 -- formal package can be partially parameterized.
6588 if Nkind (Unit_Declaration_Node (E1)) =
6589 N_Subprogram_Renaming_Declaration
6590 and then From_Default (Unit_Declaration_Node (E1))
6591 then
6592 null;
6594 -- If the formal package has an "others" box association that
6595 -- covers this formal, there is no need for a check either.
6597 elsif Nkind (Unit_Declaration_Node (E2)) in
6598 N_Formal_Subprogram_Declaration
6599 and then Box_Present (Unit_Declaration_Node (E2))
6600 then
6601 null;
6603 -- No check needed if subprogram is a defaulted null procedure
6605 elsif No (Alias (E2))
6606 and then Ekind (E2) = E_Procedure
6607 and then
6608 Null_Present (Specification (Unit_Declaration_Node (E2)))
6609 then
6610 null;
6612 -- Otherwise the actual in the formal and the actual in the
6613 -- instantiation of the formal must match, up to renamings.
6615 else
6616 Check_Mismatch
6617 (Ekind (E2) /= Ekind (E1)
6618 or else not Same_Instantiated_Function (E1, E2));
6619 end if;
6621 else
6622 raise Program_Error;
6623 end if;
6625 <<Next_E>>
6626 Prev_E1 := E1;
6627 Next_Entity (E1);
6628 Next_Entity (E2);
6629 end loop;
6630 end Check_Formal_Package_Instance;
6632 ---------------------------
6633 -- Check_Formal_Packages --
6634 ---------------------------
6636 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6637 E : Entity_Id;
6638 Formal_P : Entity_Id;
6639 Formal_Decl : Node_Id;
6640 begin
6641 -- Iterate through the declarations in the instance, looking for package
6642 -- renaming declarations that denote instances of formal packages. Stop
6643 -- when we find the renaming of the current package itself. The
6644 -- declaration for a formal package without a box is followed by an
6645 -- internal entity that repeats the instantiation.
6647 E := First_Entity (P_Id);
6648 while Present (E) loop
6649 if Ekind (E) = E_Package then
6650 if Renamed_Object (E) = P_Id then
6651 exit;
6653 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6654 null;
6656 else
6657 Formal_Decl := Parent (Associated_Formal_Package (E));
6659 -- Nothing to check if the formal has a box or an others_clause
6660 -- (necessarily with a box), or no associations altogether
6662 if Box_Present (Formal_Decl)
6663 or else No (Generic_Associations (Formal_Decl))
6664 then
6665 null;
6667 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6668 N_Others_Choice
6669 then
6670 -- The internal validating package was generated but formal
6671 -- and instance are known to be compatible.
6673 Formal_P := Next_Entity (E);
6674 Remove (Unit_Declaration_Node (Formal_P));
6676 else
6677 Formal_P := Next_Entity (E);
6679 -- If the instance is within an enclosing instance body
6680 -- there is no need to verify the legality of current formal
6681 -- packages because they were legal in the generic body.
6682 -- This optimization may be applicable elsewhere, and it
6683 -- also removes spurious errors that may arise with
6684 -- on-the-fly inlining and confusion between private and
6685 -- full views.
6687 if not In_Instance_Body then
6688 Check_Formal_Package_Instance (Formal_P, E);
6689 end if;
6691 -- Restore the visibility of formals of the formal instance
6692 -- that are not defaulted, and are hidden within the current
6693 -- generic. These formals may be visible within an enclosing
6694 -- generic.
6696 declare
6697 Elmt : Elmt_Id;
6698 begin
6699 Elmt := First_Elmt (Hidden_In_Formal_Instance (Formal_P));
6700 while Present (Elmt) loop
6701 Set_Is_Hidden (Node (Elmt), False);
6702 Next_Elmt (Elmt);
6703 end loop;
6704 end;
6706 -- After checking, remove the internal validating package.
6707 -- It is only needed for semantic checks, and as it may
6708 -- contain generic formal declarations it should not reach
6709 -- gigi.
6711 Remove (Unit_Declaration_Node (Formal_P));
6712 end if;
6713 end if;
6714 end if;
6716 Next_Entity (E);
6717 end loop;
6718 end Check_Formal_Packages;
6720 ---------------------------------
6721 -- Check_Forward_Instantiation --
6722 ---------------------------------
6724 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6725 S : Entity_Id;
6726 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6728 begin
6729 -- The instantiation appears before the generic body if we are in the
6730 -- scope of the unit containing the generic, either in its spec or in
6731 -- the package body, and before the generic body.
6733 if Ekind (Gen_Comp) = E_Package_Body then
6734 Gen_Comp := Spec_Entity (Gen_Comp);
6735 end if;
6737 if In_Open_Scopes (Gen_Comp)
6738 and then No (Corresponding_Body (Decl))
6739 then
6740 S := Current_Scope;
6742 while Present (S)
6743 and then not Is_Compilation_Unit (S)
6744 and then not Is_Child_Unit (S)
6745 loop
6746 if Ekind (S) = E_Package then
6747 Set_Has_Forward_Instantiation (S);
6748 end if;
6750 S := Scope (S);
6751 end loop;
6752 end if;
6753 end Check_Forward_Instantiation;
6755 ---------------------------
6756 -- Check_Generic_Actuals --
6757 ---------------------------
6759 -- The visibility of the actuals may be different between the point of
6760 -- generic instantiation and the instantiation of the body.
6762 procedure Check_Generic_Actuals
6763 (Instance : Entity_Id;
6764 Is_Formal_Box : Boolean)
6766 E : Entity_Id;
6767 Astype : Entity_Id;
6769 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
6770 -- For a formal that is an array type, the component type is often a
6771 -- previous formal in the same unit. The privacy status of the component
6772 -- type will have been examined earlier in the traversal of the
6773 -- corresponding actuals, and this status should not be modified for
6774 -- the array (sub)type itself. However, if the base type of the array
6775 -- (sub)type is private, its full view must be restored in the body to
6776 -- be consistent with subsequent index subtypes, etc.
6778 -- To detect this case we have to rescan the list of formals, which is
6779 -- usually short enough to ignore the resulting inefficiency.
6781 -----------------------------
6782 -- Denotes_Previous_Actual --
6783 -----------------------------
6785 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
6786 Prev : Entity_Id;
6788 begin
6789 Prev := First_Entity (Instance);
6790 while Present (Prev) loop
6791 if Is_Type (Prev)
6792 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
6793 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
6794 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
6795 then
6796 return True;
6798 elsif Prev = E then
6799 return False;
6801 else
6802 Next_Entity (Prev);
6803 end if;
6804 end loop;
6806 return False;
6807 end Denotes_Previous_Actual;
6809 -- Start of processing for Check_Generic_Actuals
6811 begin
6812 E := First_Entity (Instance);
6813 while Present (E) loop
6814 if Is_Type (E)
6815 and then Nkind (Parent (E)) = N_Subtype_Declaration
6816 and then Scope (Etype (E)) /= Instance
6817 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6818 then
6819 if Is_Array_Type (E)
6820 and then not Is_Private_Type (Etype (E))
6821 and then Denotes_Previous_Actual (Component_Type (E))
6822 then
6823 null;
6824 else
6825 Check_Private_View (Subtype_Indication (Parent (E)));
6826 end if;
6828 Set_Is_Generic_Actual_Type (E, True);
6829 Set_Is_Hidden (E, False);
6830 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
6832 -- We constructed the generic actual type as a subtype of the
6833 -- supplied type. This means that it normally would not inherit
6834 -- subtype specific attributes of the actual, which is wrong for
6835 -- the generic case.
6837 Astype := Ancestor_Subtype (E);
6839 if No (Astype) then
6841 -- This can happen when E is an itype that is the full view of
6842 -- a private type completed, e.g. with a constrained array. In
6843 -- that case, use the first subtype, which will carry size
6844 -- information. The base type itself is unconstrained and will
6845 -- not carry it.
6847 Astype := First_Subtype (E);
6848 end if;
6850 Set_Size_Info (E, (Astype));
6851 Set_RM_Size (E, RM_Size (Astype));
6852 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6854 if Is_Discrete_Or_Fixed_Point_Type (E) then
6855 Set_RM_Size (E, RM_Size (Astype));
6857 -- In nested instances, the base type of an access actual may
6858 -- itself be private, and need to be exchanged.
6860 elsif Is_Access_Type (E)
6861 and then Is_Private_Type (Etype (E))
6862 then
6863 Check_Private_View
6864 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
6865 end if;
6867 elsif Ekind (E) = E_Package then
6869 -- If this is the renaming for the current instance, we're done.
6870 -- Otherwise it is a formal package. If the corresponding formal
6871 -- was declared with a box, the (instantiations of the) generic
6872 -- formal part are also visible. Otherwise, ignore the entity
6873 -- created to validate the actuals.
6875 if Renamed_Object (E) = Instance then
6876 exit;
6878 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6879 null;
6881 -- The visibility of a formal of an enclosing generic is already
6882 -- correct.
6884 elsif Denotes_Formal_Package (E) then
6885 null;
6887 elsif Present (Associated_Formal_Package (E))
6888 and then not Is_Generic_Formal (E)
6889 then
6890 if Box_Present (Parent (Associated_Formal_Package (E))) then
6891 Check_Generic_Actuals (Renamed_Object (E), True);
6893 else
6894 Check_Generic_Actuals (Renamed_Object (E), False);
6895 end if;
6897 Set_Is_Hidden (E, False);
6898 end if;
6900 -- If this is a subprogram instance (in a wrapper package) the
6901 -- actual is fully visible.
6903 elsif Is_Wrapper_Package (Instance) then
6904 Set_Is_Hidden (E, False);
6906 -- If the formal package is declared with a box, or if the formal
6907 -- parameter is defaulted, it is visible in the body.
6909 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6910 Set_Is_Hidden (E, False);
6911 end if;
6913 if Ekind (E) = E_Constant then
6915 -- If the type of the actual is a private type declared in the
6916 -- enclosing scope of the generic unit, the body of the generic
6917 -- sees the full view of the type (because it has to appear in
6918 -- the corresponding package body). If the type is private now,
6919 -- exchange views to restore the proper visiblity in the instance.
6921 declare
6922 Typ : constant Entity_Id := Base_Type (Etype (E));
6923 -- The type of the actual
6925 Gen_Id : Entity_Id;
6926 -- The generic unit
6928 Parent_Scope : Entity_Id;
6929 -- The enclosing scope of the generic unit
6931 begin
6932 if Is_Wrapper_Package (Instance) then
6933 Gen_Id :=
6934 Generic_Parent
6935 (Specification
6936 (Unit_Declaration_Node
6937 (Related_Instance (Instance))));
6938 else
6939 Gen_Id :=
6940 Generic_Parent (Package_Specification (Instance));
6941 end if;
6943 Parent_Scope := Scope (Gen_Id);
6945 -- The exchange is only needed if the generic is defined
6946 -- within a package which is not a common ancestor of the
6947 -- scope of the instance, and is not already in scope.
6949 if Is_Private_Type (Typ)
6950 and then Scope (Typ) = Parent_Scope
6951 and then Scope (Instance) /= Parent_Scope
6952 and then Ekind (Parent_Scope) = E_Package
6953 and then not Is_Child_Unit (Gen_Id)
6954 then
6955 Switch_View (Typ);
6957 -- If the type of the entity is a subtype, it may also have
6958 -- to be made visible, together with the base type of its
6959 -- full view, after exchange.
6961 if Is_Private_Type (Etype (E)) then
6962 Switch_View (Etype (E));
6963 Switch_View (Base_Type (Etype (E)));
6964 end if;
6965 end if;
6966 end;
6967 end if;
6969 Next_Entity (E);
6970 end loop;
6971 end Check_Generic_Actuals;
6973 ------------------------------
6974 -- Check_Generic_Child_Unit --
6975 ------------------------------
6977 procedure Check_Generic_Child_Unit
6978 (Gen_Id : Node_Id;
6979 Parent_Installed : in out Boolean)
6981 Loc : constant Source_Ptr := Sloc (Gen_Id);
6982 Gen_Par : Entity_Id := Empty;
6983 E : Entity_Id;
6984 Inst_Par : Entity_Id;
6985 S : Node_Id;
6987 function Find_Generic_Child
6988 (Scop : Entity_Id;
6989 Id : Node_Id) return Entity_Id;
6990 -- Search generic parent for possible child unit with the given name
6992 function In_Enclosing_Instance return Boolean;
6993 -- Within an instance of the parent, the child unit may be denoted by
6994 -- a simple name, or an abbreviated expanded name. Examine enclosing
6995 -- scopes to locate a possible parent instantiation.
6997 ------------------------
6998 -- Find_Generic_Child --
6999 ------------------------
7001 function Find_Generic_Child
7002 (Scop : Entity_Id;
7003 Id : Node_Id) return Entity_Id
7005 E : Entity_Id;
7007 begin
7008 -- If entity of name is already set, instance has already been
7009 -- resolved, e.g. in an enclosing instantiation.
7011 if Present (Entity (Id)) then
7012 if Scope (Entity (Id)) = Scop then
7013 return Entity (Id);
7014 else
7015 return Empty;
7016 end if;
7018 else
7019 E := First_Entity (Scop);
7020 while Present (E) loop
7021 if Chars (E) = Chars (Id)
7022 and then Is_Child_Unit (E)
7023 then
7024 if Is_Child_Unit (E)
7025 and then not Is_Visible_Lib_Unit (E)
7026 then
7027 Error_Msg_NE
7028 ("generic child unit& is not visible", Gen_Id, E);
7029 end if;
7031 Set_Entity (Id, E);
7032 return E;
7033 end if;
7035 Next_Entity (E);
7036 end loop;
7038 return Empty;
7039 end if;
7040 end Find_Generic_Child;
7042 ---------------------------
7043 -- In_Enclosing_Instance --
7044 ---------------------------
7046 function In_Enclosing_Instance return Boolean is
7047 Enclosing_Instance : Node_Id;
7048 Instance_Decl : Node_Id;
7050 begin
7051 -- We do not inline any call that contains instantiations, except
7052 -- for instantiations of Unchecked_Conversion, so if we are within
7053 -- an inlined body the current instance does not require parents.
7055 if In_Inlined_Body then
7056 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
7057 return False;
7058 end if;
7060 -- Loop to check enclosing scopes
7062 Enclosing_Instance := Current_Scope;
7063 while Present (Enclosing_Instance) loop
7064 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
7066 if Ekind (Enclosing_Instance) = E_Package
7067 and then Is_Generic_Instance (Enclosing_Instance)
7068 and then Present
7069 (Generic_Parent (Specification (Instance_Decl)))
7070 then
7071 -- Check whether the generic we are looking for is a child of
7072 -- this instance.
7074 E := Find_Generic_Child
7075 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
7076 exit when Present (E);
7078 else
7079 E := Empty;
7080 end if;
7082 Enclosing_Instance := Scope (Enclosing_Instance);
7083 end loop;
7085 if No (E) then
7087 -- Not a child unit
7089 Analyze (Gen_Id);
7090 return False;
7092 else
7093 Rewrite (Gen_Id,
7094 Make_Expanded_Name (Loc,
7095 Chars => Chars (E),
7096 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
7097 Selector_Name => New_Occurrence_Of (E, Loc)));
7099 Set_Entity (Gen_Id, E);
7100 Set_Etype (Gen_Id, Etype (E));
7101 Parent_Installed := False; -- Already in scope.
7102 return True;
7103 end if;
7104 end In_Enclosing_Instance;
7106 -- Start of processing for Check_Generic_Child_Unit
7108 begin
7109 -- If the name of the generic is given by a selected component, it may
7110 -- be the name of a generic child unit, and the prefix is the name of an
7111 -- instance of the parent, in which case the child unit must be visible.
7112 -- If this instance is not in scope, it must be placed there and removed
7113 -- after instantiation, because what is being instantiated is not the
7114 -- original child, but the corresponding child present in the instance
7115 -- of the parent.
7117 -- If the child is instantiated within the parent, it can be given by
7118 -- a simple name. In this case the instance is already in scope, but
7119 -- the child generic must be recovered from the generic parent as well.
7121 if Nkind (Gen_Id) = N_Selected_Component then
7122 S := Selector_Name (Gen_Id);
7123 Analyze (Prefix (Gen_Id));
7124 Inst_Par := Entity (Prefix (Gen_Id));
7126 if Ekind (Inst_Par) = E_Package
7127 and then Present (Renamed_Object (Inst_Par))
7128 then
7129 Inst_Par := Renamed_Object (Inst_Par);
7130 end if;
7132 if Ekind (Inst_Par) = E_Package then
7133 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
7134 Gen_Par := Generic_Parent (Parent (Inst_Par));
7136 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
7137 and then
7138 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
7139 then
7140 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
7141 end if;
7143 elsif Ekind (Inst_Par) = E_Generic_Package
7144 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
7145 then
7146 -- A formal package may be a real child package, and not the
7147 -- implicit instance within a parent. In this case the child is
7148 -- not visible and has to be retrieved explicitly as well.
7150 Gen_Par := Inst_Par;
7151 end if;
7153 if Present (Gen_Par) then
7155 -- The prefix denotes an instantiation. The entity itself may be a
7156 -- nested generic, or a child unit.
7158 E := Find_Generic_Child (Gen_Par, S);
7160 if Present (E) then
7161 Change_Selected_Component_To_Expanded_Name (Gen_Id);
7162 Set_Entity (Gen_Id, E);
7163 Set_Etype (Gen_Id, Etype (E));
7164 Set_Entity (S, E);
7165 Set_Etype (S, Etype (E));
7167 -- Indicate that this is a reference to the parent
7169 if In_Extended_Main_Source_Unit (Gen_Id) then
7170 Set_Is_Instantiated (Inst_Par);
7171 end if;
7173 -- A common mistake is to replicate the naming scheme of a
7174 -- hierarchy by instantiating a generic child directly, rather
7175 -- than the implicit child in a parent instance:
7177 -- generic .. package Gpar is ..
7178 -- generic .. package Gpar.Child is ..
7179 -- package Par is new Gpar ();
7181 -- with Gpar.Child;
7182 -- package Par.Child is new Gpar.Child ();
7183 -- rather than Par.Child
7185 -- In this case the instantiation is within Par, which is an
7186 -- instance, but Gpar does not denote Par because we are not IN
7187 -- the instance of Gpar, so this is illegal. The test below
7188 -- recognizes this particular case.
7190 if Is_Child_Unit (E)
7191 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
7192 and then (not In_Instance
7193 or else Nkind (Parent (Parent (Gen_Id))) =
7194 N_Compilation_Unit)
7195 then
7196 Error_Msg_N
7197 ("prefix of generic child unit must be instance of parent",
7198 Gen_Id);
7199 end if;
7201 if not In_Open_Scopes (Inst_Par)
7202 and then Nkind (Parent (Gen_Id)) not in
7203 N_Generic_Renaming_Declaration
7204 then
7205 Install_Parent (Inst_Par);
7206 Parent_Installed := True;
7208 elsif In_Open_Scopes (Inst_Par) then
7210 -- If the parent is already installed, install the actuals
7211 -- for its formal packages. This is necessary when the child
7212 -- instance is a child of the parent instance: in this case,
7213 -- the parent is placed on the scope stack but the formal
7214 -- packages are not made visible.
7216 Install_Formal_Packages (Inst_Par);
7217 end if;
7219 else
7220 -- If the generic parent does not contain an entity that
7221 -- corresponds to the selector, the instance doesn't either.
7222 -- Analyzing the node will yield the appropriate error message.
7223 -- If the entity is not a child unit, then it is an inner
7224 -- generic in the parent.
7226 Analyze (Gen_Id);
7227 end if;
7229 else
7230 Analyze (Gen_Id);
7232 if Is_Child_Unit (Entity (Gen_Id))
7233 and then
7234 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7235 and then not In_Open_Scopes (Inst_Par)
7236 then
7237 Install_Parent (Inst_Par);
7238 Parent_Installed := True;
7240 -- The generic unit may be the renaming of the implicit child
7241 -- present in an instance. In that case the parent instance is
7242 -- obtained from the name of the renamed entity.
7244 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
7245 and then Present (Renamed_Entity (Entity (Gen_Id)))
7246 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7247 then
7248 declare
7249 Renamed_Package : constant Node_Id :=
7250 Name (Parent (Entity (Gen_Id)));
7251 begin
7252 if Nkind (Renamed_Package) = N_Expanded_Name then
7253 Inst_Par := Entity (Prefix (Renamed_Package));
7254 Install_Parent (Inst_Par);
7255 Parent_Installed := True;
7256 end if;
7257 end;
7258 end if;
7259 end if;
7261 elsif Nkind (Gen_Id) = N_Expanded_Name then
7263 -- Entity already present, analyze prefix, whose meaning may be an
7264 -- instance in the current context. If it is an instance of a
7265 -- relative within another, the proper parent may still have to be
7266 -- installed, if they are not of the same generation.
7268 Analyze (Prefix (Gen_Id));
7270 -- Prevent cascaded errors
7272 if Etype (Prefix (Gen_Id)) = Any_Type then
7273 return;
7274 end if;
7276 -- In the unlikely case that a local declaration hides the name of
7277 -- the parent package, locate it on the homonym chain. If the context
7278 -- is an instance of the parent, the renaming entity is flagged as
7279 -- such.
7281 Inst_Par := Entity (Prefix (Gen_Id));
7282 while Present (Inst_Par)
7283 and then not Is_Package_Or_Generic_Package (Inst_Par)
7284 loop
7285 Inst_Par := Homonym (Inst_Par);
7286 end loop;
7288 pragma Assert (Present (Inst_Par));
7289 Set_Entity (Prefix (Gen_Id), Inst_Par);
7291 if In_Enclosing_Instance then
7292 null;
7294 elsif Present (Entity (Gen_Id))
7295 and then Is_Child_Unit (Entity (Gen_Id))
7296 and then not In_Open_Scopes (Inst_Par)
7297 then
7298 Install_Parent (Inst_Par);
7299 Parent_Installed := True;
7300 end if;
7302 elsif In_Enclosing_Instance then
7304 -- The child unit is found in some enclosing scope
7306 null;
7308 else
7309 Analyze (Gen_Id);
7311 -- If this is the renaming of the implicit child in a parent
7312 -- instance, recover the parent name and install it.
7314 if Is_Entity_Name (Gen_Id) then
7315 E := Entity (Gen_Id);
7317 if Is_Generic_Unit (E)
7318 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
7319 and then Is_Child_Unit (Renamed_Object (E))
7320 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
7321 and then Nkind (Name (Parent (E))) = N_Expanded_Name
7322 then
7323 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
7324 Inst_Par := Entity (Prefix (Gen_Id));
7326 if not In_Open_Scopes (Inst_Par) then
7327 Install_Parent (Inst_Par);
7328 Parent_Installed := True;
7329 end if;
7331 -- If it is a child unit of a non-generic parent, it may be
7332 -- use-visible and given by a direct name. Install parent as
7333 -- for other cases.
7335 elsif Is_Generic_Unit (E)
7336 and then Is_Child_Unit (E)
7337 and then
7338 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7339 and then not Is_Generic_Unit (Scope (E))
7340 then
7341 if not In_Open_Scopes (Scope (E)) then
7342 Install_Parent (Scope (E));
7343 Parent_Installed := True;
7344 end if;
7345 end if;
7346 end if;
7347 end if;
7348 end Check_Generic_Child_Unit;
7350 -----------------------------
7351 -- Check_Hidden_Child_Unit --
7352 -----------------------------
7354 procedure Check_Hidden_Child_Unit
7355 (N : Node_Id;
7356 Gen_Unit : Entity_Id;
7357 Act_Decl_Id : Entity_Id)
7359 Gen_Id : constant Node_Id := Name (N);
7361 begin
7362 if Is_Child_Unit (Gen_Unit)
7363 and then Is_Child_Unit (Act_Decl_Id)
7364 and then Nkind (Gen_Id) = N_Expanded_Name
7365 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
7366 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
7367 then
7368 Error_Msg_Node_2 := Scope (Act_Decl_Id);
7369 Error_Msg_NE
7370 ("generic unit & is implicitly declared in &",
7371 Defining_Unit_Name (N), Gen_Unit);
7372 Error_Msg_N ("\instance must have different name",
7373 Defining_Unit_Name (N));
7374 end if;
7375 end Check_Hidden_Child_Unit;
7377 ------------------------
7378 -- Check_Private_View --
7379 ------------------------
7381 procedure Check_Private_View (N : Node_Id) is
7382 T : constant Entity_Id := Etype (N);
7383 BT : Entity_Id;
7385 begin
7386 -- Exchange views if the type was not private in the generic but is
7387 -- private at the point of instantiation. Do not exchange views if
7388 -- the scope of the type is in scope. This can happen if both generic
7389 -- and instance are sibling units, or if type is defined in a parent.
7390 -- In this case the visibility of the type will be correct for all
7391 -- semantic checks.
7393 if Present (T) then
7394 BT := Base_Type (T);
7396 if Is_Private_Type (T)
7397 and then not Has_Private_View (N)
7398 and then Present (Full_View (T))
7399 and then not In_Open_Scopes (Scope (T))
7400 then
7401 -- In the generic, the full type was visible. Save the private
7402 -- entity, for subsequent exchange.
7404 Switch_View (T);
7406 elsif Has_Private_View (N)
7407 and then not Is_Private_Type (T)
7408 and then not Has_Been_Exchanged (T)
7409 and then Etype (Get_Associated_Node (N)) /= T
7410 then
7411 -- Only the private declaration was visible in the generic. If
7412 -- the type appears in a subtype declaration, the subtype in the
7413 -- instance must have a view compatible with that of its parent,
7414 -- which must be exchanged (see corresponding code in Restore_
7415 -- Private_Views). Otherwise, if the type is defined in a parent
7416 -- unit, leave full visibility within instance, which is safe.
7418 if In_Open_Scopes (Scope (Base_Type (T)))
7419 and then not Is_Private_Type (Base_Type (T))
7420 and then Comes_From_Source (Base_Type (T))
7421 then
7422 null;
7424 elsif Nkind (Parent (N)) = N_Subtype_Declaration
7425 or else not In_Private_Part (Scope (Base_Type (T)))
7426 then
7427 Prepend_Elmt (T, Exchanged_Views);
7428 Exchange_Declarations (Etype (Get_Associated_Node (N)));
7429 end if;
7431 -- For composite types with inconsistent representation exchange
7432 -- component types accordingly.
7434 elsif Is_Access_Type (T)
7435 and then Is_Private_Type (Designated_Type (T))
7436 and then not Has_Private_View (N)
7437 and then Present (Full_View (Designated_Type (T)))
7438 then
7439 Switch_View (Designated_Type (T));
7441 elsif Is_Array_Type (T) then
7442 if Is_Private_Type (Component_Type (T))
7443 and then not Has_Private_View (N)
7444 and then Present (Full_View (Component_Type (T)))
7445 then
7446 Switch_View (Component_Type (T));
7447 end if;
7449 -- The normal exchange mechanism relies on the setting of a
7450 -- flag on the reference in the generic. However, an additional
7451 -- mechanism is needed for types that are not explicitly
7452 -- mentioned in the generic, but may be needed in expanded code
7453 -- in the instance. This includes component types of arrays and
7454 -- designated types of access types. This processing must also
7455 -- include the index types of arrays which we take care of here.
7457 declare
7458 Indx : Node_Id;
7459 Typ : Entity_Id;
7461 begin
7462 Indx := First_Index (T);
7463 while Present (Indx) loop
7464 Typ := Base_Type (Etype (Indx));
7466 if Is_Private_Type (Typ)
7467 and then Present (Full_View (Typ))
7468 then
7469 Switch_View (Typ);
7470 end if;
7472 Next_Index (Indx);
7473 end loop;
7474 end;
7476 elsif Is_Private_Type (T)
7477 and then Present (Full_View (T))
7478 and then Is_Array_Type (Full_View (T))
7479 and then Is_Private_Type (Component_Type (Full_View (T)))
7480 then
7481 Switch_View (T);
7483 -- Finally, a non-private subtype may have a private base type, which
7484 -- must be exchanged for consistency. This can happen when a package
7485 -- body is instantiated, when the scope stack is empty but in fact
7486 -- the subtype and the base type are declared in an enclosing scope.
7488 -- Note that in this case we introduce an inconsistency in the view
7489 -- set, because we switch the base type BT, but there could be some
7490 -- private dependent subtypes of BT which remain unswitched. Such
7491 -- subtypes might need to be switched at a later point (see specific
7492 -- provision for that case in Switch_View).
7494 elsif not Is_Private_Type (T)
7495 and then not Has_Private_View (N)
7496 and then Is_Private_Type (BT)
7497 and then Present (Full_View (BT))
7498 and then not Is_Generic_Type (BT)
7499 and then not In_Open_Scopes (BT)
7500 then
7501 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7502 Exchange_Declarations (BT);
7503 end if;
7504 end if;
7505 end Check_Private_View;
7507 -----------------------------
7508 -- Check_Hidden_Primitives --
7509 -----------------------------
7511 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7512 Actual : Node_Id;
7513 Gen_T : Entity_Id;
7514 Result : Elist_Id := No_Elist;
7516 begin
7517 if No (Assoc_List) then
7518 return No_Elist;
7519 end if;
7521 -- Traverse the list of associations between formals and actuals
7522 -- searching for renamings of tagged types
7524 Actual := First (Assoc_List);
7525 while Present (Actual) loop
7526 if Nkind (Actual) = N_Subtype_Declaration then
7527 Gen_T := Generic_Parent_Type (Actual);
7529 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7531 -- Traverse the list of primitives of the actual types
7532 -- searching for hidden primitives that are visible in the
7533 -- corresponding generic formal; leave them visible and
7534 -- append them to Result to restore their decoration later.
7536 Install_Hidden_Primitives
7537 (Prims_List => Result,
7538 Gen_T => Gen_T,
7539 Act_T => Entity (Subtype_Indication (Actual)));
7540 end if;
7541 end if;
7543 Next (Actual);
7544 end loop;
7546 return Result;
7547 end Check_Hidden_Primitives;
7549 --------------------------
7550 -- Contains_Instance_Of --
7551 --------------------------
7553 function Contains_Instance_Of
7554 (Inner : Entity_Id;
7555 Outer : Entity_Id;
7556 N : Node_Id) return Boolean
7558 Elmt : Elmt_Id;
7559 Scop : Entity_Id;
7561 begin
7562 Scop := Outer;
7564 -- Verify that there are no circular instantiations. We check whether
7565 -- the unit contains an instance of the current scope or some enclosing
7566 -- scope (in case one of the instances appears in a subunit). Longer
7567 -- circularities involving subunits might seem too pathological to
7568 -- consider, but they were not too pathological for the authors of
7569 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7570 -- enclosing generic scopes as containing an instance.
7572 loop
7573 -- Within a generic subprogram body, the scope is not generic, to
7574 -- allow for recursive subprograms. Use the declaration to determine
7575 -- whether this is a generic unit.
7577 if Ekind (Scop) = E_Generic_Package
7578 or else (Is_Subprogram (Scop)
7579 and then Nkind (Unit_Declaration_Node (Scop)) =
7580 N_Generic_Subprogram_Declaration)
7581 then
7582 Elmt := First_Elmt (Inner_Instances (Inner));
7584 while Present (Elmt) loop
7585 if Node (Elmt) = Scop then
7586 Error_Msg_Node_2 := Inner;
7587 Error_Msg_NE
7588 ("circular Instantiation: & instantiated within &!",
7589 N, Scop);
7590 return True;
7592 elsif Node (Elmt) = Inner then
7593 return True;
7595 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7596 Error_Msg_Node_2 := Inner;
7597 Error_Msg_NE
7598 ("circular Instantiation: & instantiated within &!",
7599 N, Node (Elmt));
7600 return True;
7601 end if;
7603 Next_Elmt (Elmt);
7604 end loop;
7606 -- Indicate that Inner is being instantiated within Scop
7608 Append_Elmt (Inner, Inner_Instances (Scop));
7609 end if;
7611 if Scop = Standard_Standard then
7612 exit;
7613 else
7614 Scop := Scope (Scop);
7615 end if;
7616 end loop;
7618 return False;
7619 end Contains_Instance_Of;
7621 -----------------------
7622 -- Copy_Generic_Node --
7623 -----------------------
7625 function Copy_Generic_Node
7626 (N : Node_Id;
7627 Parent_Id : Node_Id;
7628 Instantiating : Boolean) return Node_Id
7630 Ent : Entity_Id;
7631 New_N : Node_Id;
7633 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7634 -- Check the given value of one of the Fields referenced by the current
7635 -- node to determine whether to copy it recursively. The field may hold
7636 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7637 -- Char) in which case it need not be copied.
7639 procedure Copy_Descendants;
7640 -- Common utility for various nodes
7642 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7643 -- Make copy of element list
7645 function Copy_Generic_List
7646 (L : List_Id;
7647 Parent_Id : Node_Id) return List_Id;
7648 -- Apply Copy_Node recursively to the members of a node list
7650 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7651 -- True if an identifier is part of the defining program unit name of
7652 -- a child unit. The entity of such an identifier must be kept (for
7653 -- ASIS use) even though as the name of an enclosing generic it would
7654 -- otherwise not be preserved in the generic tree.
7656 ----------------------
7657 -- Copy_Descendants --
7658 ----------------------
7660 procedure Copy_Descendants is
7661 use Atree.Unchecked_Access;
7662 -- This code section is part of the implementation of an untyped
7663 -- tree traversal, so it needs direct access to node fields.
7665 begin
7666 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7667 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7668 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7669 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7670 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7671 end Copy_Descendants;
7673 -----------------------------
7674 -- Copy_Generic_Descendant --
7675 -----------------------------
7677 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7678 begin
7679 if D = Union_Id (Empty) then
7680 return D;
7682 elsif D in Node_Range then
7683 return Union_Id
7684 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7686 elsif D in List_Range then
7687 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7689 elsif D in Elist_Range then
7690 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7692 -- Nothing else is copyable (e.g. Uint values), return as is
7694 else
7695 return D;
7696 end if;
7697 end Copy_Generic_Descendant;
7699 ------------------------
7700 -- Copy_Generic_Elist --
7701 ------------------------
7703 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7704 M : Elmt_Id;
7705 L : Elist_Id;
7707 begin
7708 if Present (E) then
7709 L := New_Elmt_List;
7710 M := First_Elmt (E);
7711 while Present (M) loop
7712 Append_Elmt
7713 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7714 Next_Elmt (M);
7715 end loop;
7717 return L;
7719 else
7720 return No_Elist;
7721 end if;
7722 end Copy_Generic_Elist;
7724 -----------------------
7725 -- Copy_Generic_List --
7726 -----------------------
7728 function Copy_Generic_List
7729 (L : List_Id;
7730 Parent_Id : Node_Id) return List_Id
7732 N : Node_Id;
7733 New_L : List_Id;
7735 begin
7736 if Present (L) then
7737 New_L := New_List;
7738 Set_Parent (New_L, Parent_Id);
7740 N := First (L);
7741 while Present (N) loop
7742 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7743 Next (N);
7744 end loop;
7746 return New_L;
7748 else
7749 return No_List;
7750 end if;
7751 end Copy_Generic_List;
7753 ---------------------------
7754 -- In_Defining_Unit_Name --
7755 ---------------------------
7757 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7758 begin
7759 return
7760 Present (Parent (Nam))
7761 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7762 or else
7763 (Nkind (Parent (Nam)) = N_Expanded_Name
7764 and then In_Defining_Unit_Name (Parent (Nam))));
7765 end In_Defining_Unit_Name;
7767 -- Start of processing for Copy_Generic_Node
7769 begin
7770 if N = Empty then
7771 return N;
7772 end if;
7774 New_N := New_Copy (N);
7776 -- Copy aspects if present
7778 if Has_Aspects (N) then
7779 Set_Has_Aspects (New_N, False);
7780 Set_Aspect_Specifications
7781 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7782 end if;
7784 -- If we are instantiating, we want to adjust the sloc based on the
7785 -- current S_Adjustment. However, if this is the root node of a subunit,
7786 -- we need to defer that adjustment to below (see "elsif Instantiating
7787 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
7788 -- computed the adjustment.
7790 if Instantiating
7791 and then not (Nkind (N) in N_Proper_Body
7792 and then Was_Originally_Stub (N))
7793 then
7794 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7795 end if;
7797 if not Is_List_Member (N) then
7798 Set_Parent (New_N, Parent_Id);
7799 end if;
7801 -- Special casing for identifiers and other entity names and operators
7803 if Nkind_In (New_N, N_Character_Literal,
7804 N_Expanded_Name,
7805 N_Identifier,
7806 N_Operator_Symbol)
7807 or else Nkind (New_N) in N_Op
7808 then
7809 if not Instantiating then
7811 -- Link both nodes in order to assign subsequently the entity of
7812 -- the copy to the original node, in case this is a global
7813 -- reference.
7815 Set_Associated_Node (N, New_N);
7817 -- If we are within an instantiation, this is a nested generic
7818 -- that has already been analyzed at the point of definition.
7819 -- We must preserve references that were global to the enclosing
7820 -- parent at that point. Other occurrences, whether global or
7821 -- local to the current generic, must be resolved anew, so we
7822 -- reset the entity in the generic copy. A global reference has a
7823 -- smaller depth than the parent, or else the same depth in case
7824 -- both are distinct compilation units.
7826 -- A child unit is implicitly declared within the enclosing parent
7827 -- but is in fact global to it, and must be preserved.
7829 -- It is also possible for Current_Instantiated_Parent to be
7830 -- defined, and for this not to be a nested generic, namely if
7831 -- the unit is loaded through Rtsfind. In that case, the entity of
7832 -- New_N is only a link to the associated node, and not a defining
7833 -- occurrence.
7835 -- The entities for parent units in the defining_program_unit of a
7836 -- generic child unit are established when the context of the unit
7837 -- is first analyzed, before the generic copy is made. They are
7838 -- preserved in the copy for use in ASIS queries.
7840 Ent := Entity (New_N);
7842 if No (Current_Instantiated_Parent.Gen_Id) then
7843 if No (Ent)
7844 or else Nkind (Ent) /= N_Defining_Identifier
7845 or else not In_Defining_Unit_Name (N)
7846 then
7847 Set_Associated_Node (New_N, Empty);
7848 end if;
7850 elsif No (Ent)
7851 or else
7852 not Nkind_In (Ent, N_Defining_Identifier,
7853 N_Defining_Character_Literal,
7854 N_Defining_Operator_Symbol)
7855 or else No (Scope (Ent))
7856 or else
7857 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7858 and then not Is_Child_Unit (Ent))
7859 or else
7860 (Scope_Depth (Scope (Ent)) >
7861 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7862 and then
7863 Get_Source_Unit (Ent) =
7864 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7865 then
7866 Set_Associated_Node (New_N, Empty);
7867 end if;
7869 -- Case of instantiating identifier or some other name or operator
7871 else
7872 -- If the associated node is still defined, the entity in it
7873 -- is global, and must be copied to the instance. If this copy
7874 -- is being made for a body to inline, it is applied to an
7875 -- instantiated tree, and the entity is already present and
7876 -- must be also preserved.
7878 declare
7879 Assoc : constant Node_Id := Get_Associated_Node (N);
7881 begin
7882 if Present (Assoc) then
7883 if Nkind (Assoc) = Nkind (N) then
7884 Set_Entity (New_N, Entity (Assoc));
7885 Check_Private_View (N);
7887 -- The node is a reference to a global type and acts as the
7888 -- subtype mark of a qualified expression created in order
7889 -- to aid resolution of accidental overloading in instances.
7890 -- Since N is a reference to a type, the Associated_Node of
7891 -- N denotes an entity rather than another identifier. See
7892 -- Qualify_Universal_Operands for details.
7894 elsif Nkind (N) = N_Identifier
7895 and then Nkind (Parent (N)) = N_Qualified_Expression
7896 and then Subtype_Mark (Parent (N)) = N
7897 and then Is_Qualified_Universal_Literal (Parent (N))
7898 then
7899 Set_Entity (New_N, Assoc);
7901 -- The name in the call may be a selected component if the
7902 -- call has not been analyzed yet, as may be the case for
7903 -- pre/post conditions in a generic unit.
7905 elsif Nkind (Assoc) = N_Function_Call
7906 and then Is_Entity_Name (Name (Assoc))
7907 then
7908 Set_Entity (New_N, Entity (Name (Assoc)));
7910 elsif Nkind_In (Assoc, N_Defining_Identifier,
7911 N_Defining_Character_Literal,
7912 N_Defining_Operator_Symbol)
7913 and then Expander_Active
7914 then
7915 -- Inlining case: we are copying a tree that contains
7916 -- global entities, which are preserved in the copy to be
7917 -- used for subsequent inlining.
7919 null;
7921 else
7922 Set_Entity (New_N, Empty);
7923 end if;
7924 end if;
7925 end;
7926 end if;
7928 -- For expanded name, we must copy the Prefix and Selector_Name
7930 if Nkind (N) = N_Expanded_Name then
7931 Set_Prefix
7932 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7934 Set_Selector_Name (New_N,
7935 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7937 -- For operators, copy the operands
7939 elsif Nkind (N) in N_Op then
7940 if Nkind (N) in N_Binary_Op then
7941 Set_Left_Opnd (New_N,
7942 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7943 end if;
7945 Set_Right_Opnd (New_N,
7946 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7947 end if;
7949 -- Establish a link between an entity from the generic template and the
7950 -- corresponding entity in the generic copy to be analyzed.
7952 elsif Nkind (N) in N_Entity then
7953 if not Instantiating then
7954 Set_Associated_Entity (N, New_N);
7955 end if;
7957 -- Clear any existing link the copy may inherit from the replicated
7958 -- generic template entity.
7960 Set_Associated_Entity (New_N, Empty);
7962 -- Special casing for stubs
7964 elsif Nkind (N) in N_Body_Stub then
7966 -- In any case, we must copy the specification or defining
7967 -- identifier as appropriate.
7969 if Nkind (N) = N_Subprogram_Body_Stub then
7970 Set_Specification (New_N,
7971 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7973 else
7974 Set_Defining_Identifier (New_N,
7975 Copy_Generic_Node
7976 (Defining_Identifier (N), New_N, Instantiating));
7977 end if;
7979 -- If we are not instantiating, then this is where we load and
7980 -- analyze subunits, i.e. at the point where the stub occurs. A
7981 -- more permissive system might defer this analysis to the point
7982 -- of instantiation, but this seems too complicated for now.
7984 if not Instantiating then
7985 declare
7986 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7987 Subunit : Node_Id;
7988 Unum : Unit_Number_Type;
7989 New_Body : Node_Id;
7991 begin
7992 -- Make sure that, if it is a subunit of the main unit that is
7993 -- preprocessed and if -gnateG is specified, the preprocessed
7994 -- file will be written.
7996 Lib.Analysing_Subunit_Of_Main :=
7997 Lib.In_Extended_Main_Source_Unit (N);
7998 Unum :=
7999 Load_Unit
8000 (Load_Name => Subunit_Name,
8001 Required => False,
8002 Subunit => True,
8003 Error_Node => N);
8004 Lib.Analysing_Subunit_Of_Main := False;
8006 -- If the proper body is not found, a warning message will be
8007 -- emitted when analyzing the stub, or later at the point of
8008 -- instantiation. Here we just leave the stub as is.
8010 if Unum = No_Unit then
8011 Subunits_Missing := True;
8012 goto Subunit_Not_Found;
8013 end if;
8015 Subunit := Cunit (Unum);
8017 if Nkind (Unit (Subunit)) /= N_Subunit then
8018 Error_Msg_N
8019 ("found child unit instead of expected SEPARATE subunit",
8020 Subunit);
8021 Error_Msg_Sloc := Sloc (N);
8022 Error_Msg_N ("\to complete stub #", Subunit);
8023 goto Subunit_Not_Found;
8024 end if;
8026 -- We must create a generic copy of the subunit, in order to
8027 -- perform semantic analysis on it, and we must replace the
8028 -- stub in the original generic unit with the subunit, in order
8029 -- to preserve non-local references within.
8031 -- Only the proper body needs to be copied. Library_Unit and
8032 -- context clause are simply inherited by the generic copy.
8033 -- Note that the copy (which may be recursive if there are
8034 -- nested subunits) must be done first, before attaching it to
8035 -- the enclosing generic.
8037 New_Body :=
8038 Copy_Generic_Node
8039 (Proper_Body (Unit (Subunit)),
8040 Empty, Instantiating => False);
8042 -- Now place the original proper body in the original generic
8043 -- unit. This is a body, not a compilation unit.
8045 Rewrite (N, Proper_Body (Unit (Subunit)));
8046 Set_Is_Compilation_Unit (Defining_Entity (N), False);
8047 Set_Was_Originally_Stub (N);
8049 -- Finally replace the body of the subunit with its copy, and
8050 -- make this new subunit into the library unit of the generic
8051 -- copy, which does not have stubs any longer.
8053 Set_Proper_Body (Unit (Subunit), New_Body);
8054 Set_Library_Unit (New_N, Subunit);
8055 Inherit_Context (Unit (Subunit), N);
8056 end;
8058 -- If we are instantiating, this must be an error case, since
8059 -- otherwise we would have replaced the stub node by the proper body
8060 -- that corresponds. So just ignore it in the copy (i.e. we have
8061 -- copied it, and that is good enough).
8063 else
8064 null;
8065 end if;
8067 <<Subunit_Not_Found>> null;
8069 -- If the node is a compilation unit, it is the subunit of a stub, which
8070 -- has been loaded already (see code below). In this case, the library
8071 -- unit field of N points to the parent unit (which is a compilation
8072 -- unit) and need not (and cannot) be copied.
8074 -- When the proper body of the stub is analyzed, the library_unit link
8075 -- is used to establish the proper context (see sem_ch10).
8077 -- The other fields of a compilation unit are copied as usual
8079 elsif Nkind (N) = N_Compilation_Unit then
8081 -- This code can only be executed when not instantiating, because in
8082 -- the copy made for an instantiation, the compilation unit node has
8083 -- disappeared at the point that a stub is replaced by its proper
8084 -- body.
8086 pragma Assert (not Instantiating);
8088 Set_Context_Items (New_N,
8089 Copy_Generic_List (Context_Items (N), New_N));
8091 Set_Unit (New_N,
8092 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
8094 Set_First_Inlined_Subprogram (New_N,
8095 Copy_Generic_Node
8096 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
8098 Set_Aux_Decls_Node
8099 (New_N,
8100 Copy_Generic_Node
8101 (Aux_Decls_Node (N), New_N, Instantiating => False));
8103 -- For an assignment node, the assignment is known to be semantically
8104 -- legal if we are instantiating the template. This avoids incorrect
8105 -- diagnostics in generated code.
8107 elsif Nkind (N) = N_Assignment_Statement then
8109 -- Copy name and expression fields in usual manner
8111 Set_Name (New_N,
8112 Copy_Generic_Node (Name (N), New_N, Instantiating));
8114 Set_Expression (New_N,
8115 Copy_Generic_Node (Expression (N), New_N, Instantiating));
8117 if Instantiating then
8118 Set_Assignment_OK (Name (New_N), True);
8119 end if;
8121 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
8122 if not Instantiating then
8123 Set_Associated_Node (N, New_N);
8125 else
8126 if Present (Get_Associated_Node (N))
8127 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
8128 then
8129 -- In the generic the aggregate has some composite type. If at
8130 -- the point of instantiation the type has a private view,
8131 -- install the full view (and that of its ancestors, if any).
8133 declare
8134 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
8135 Rt : Entity_Id;
8137 begin
8138 if Present (T) and then Is_Private_Type (T) then
8139 Switch_View (T);
8140 end if;
8142 if Present (T)
8143 and then Is_Tagged_Type (T)
8144 and then Is_Derived_Type (T)
8145 then
8146 Rt := Root_Type (T);
8148 loop
8149 T := Etype (T);
8151 if Is_Private_Type (T) then
8152 Switch_View (T);
8153 end if;
8155 exit when T = Rt;
8156 end loop;
8157 end if;
8158 end;
8159 end if;
8160 end if;
8162 -- Do not copy the associated node, which points to the generic copy
8163 -- of the aggregate.
8165 declare
8166 use Atree.Unchecked_Access;
8167 -- This code section is part of the implementation of an untyped
8168 -- tree traversal, so it needs direct access to node fields.
8170 begin
8171 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
8172 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
8173 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
8174 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
8175 end;
8177 -- Allocators do not have an identifier denoting the access type, so we
8178 -- must locate it through the expression to check whether the views are
8179 -- consistent.
8181 elsif Nkind (N) = N_Allocator
8182 and then Nkind (Expression (N)) = N_Qualified_Expression
8183 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
8184 and then Instantiating
8185 then
8186 declare
8187 T : constant Node_Id :=
8188 Get_Associated_Node (Subtype_Mark (Expression (N)));
8189 Acc_T : Entity_Id;
8191 begin
8192 if Present (T) then
8194 -- Retrieve the allocator node in the generic copy
8196 Acc_T := Etype (Parent (Parent (T)));
8198 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
8199 Switch_View (Acc_T);
8200 end if;
8201 end if;
8203 Copy_Descendants;
8204 end;
8206 -- For a proper body, we must catch the case of a proper body that
8207 -- replaces a stub. This represents the point at which a separate
8208 -- compilation unit, and hence template file, may be referenced, so we
8209 -- must make a new source instantiation entry for the template of the
8210 -- subunit, and ensure that all nodes in the subunit are adjusted using
8211 -- this new source instantiation entry.
8213 elsif Nkind (N) in N_Proper_Body then
8214 declare
8215 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
8216 begin
8217 if Instantiating and then Was_Originally_Stub (N) then
8218 Create_Instantiation_Source
8219 (Instantiation_Node,
8220 Defining_Entity (N),
8221 S_Adjustment);
8223 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8224 end if;
8226 -- Now copy the fields of the proper body, using the new
8227 -- adjustment factor if one was needed as per test above.
8229 Copy_Descendants;
8231 -- Restore the original adjustment factor
8233 S_Adjustment := Save_Adjustment;
8234 end;
8236 elsif Nkind (N) = N_Pragma and then Instantiating then
8238 -- Do not copy Comment or Ident pragmas their content is relevant to
8239 -- the generic unit, not to the instantiating unit.
8241 if Nam_In (Pragma_Name_Unmapped (N), Name_Comment, Name_Ident) then
8242 New_N := Make_Null_Statement (Sloc (N));
8244 -- Do not copy pragmas generated from aspects because the pragmas do
8245 -- not carry any semantic information, plus they will be regenerated
8246 -- in the instance.
8248 -- However, generating C we need to copy them since postconditions
8249 -- are inlined by the front end, and the front-end inlining machinery
8250 -- relies on this routine to perform inlining.
8252 elsif From_Aspect_Specification (N)
8253 and then not Modify_Tree_For_C
8254 then
8255 New_N := Make_Null_Statement (Sloc (N));
8257 else
8258 Copy_Descendants;
8259 end if;
8261 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
8263 -- No descendant fields need traversing
8265 null;
8267 elsif Nkind (N) = N_String_Literal
8268 and then Present (Etype (N))
8269 and then Instantiating
8270 then
8271 -- If the string is declared in an outer scope, the string_literal
8272 -- subtype created for it may have the wrong scope. Force reanalysis
8273 -- of the constant to generate a new itype in the proper context.
8275 Set_Etype (New_N, Empty);
8276 Set_Analyzed (New_N, False);
8278 -- For the remaining nodes, copy their descendants recursively
8280 else
8281 Copy_Descendants;
8283 if Instantiating and then Nkind (N) = N_Subprogram_Body then
8284 Set_Generic_Parent (Specification (New_N), N);
8286 -- Should preserve Corresponding_Spec??? (12.3(14))
8287 end if;
8288 end if;
8290 -- Propagate dimensions if present, so that they are reflected in the
8291 -- instance.
8293 if Nkind (N) in N_Has_Etype
8294 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
8295 and then Present (Etype (N))
8296 and then Is_Floating_Point_Type (Etype (N))
8297 and then Has_Dimension_System (Etype (N))
8298 then
8299 Copy_Dimensions (N, New_N);
8300 end if;
8302 return New_N;
8303 end Copy_Generic_Node;
8305 ----------------------------
8306 -- Denotes_Formal_Package --
8307 ----------------------------
8309 function Denotes_Formal_Package
8310 (Pack : Entity_Id;
8311 On_Exit : Boolean := False;
8312 Instance : Entity_Id := Empty) return Boolean
8314 Par : Entity_Id;
8315 Scop : constant Entity_Id := Scope (Pack);
8316 E : Entity_Id;
8318 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
8319 -- The package in question may be an actual for a previous formal
8320 -- package P of the current instance, so examine its actuals as well.
8321 -- This must be recursive over other formal packages.
8323 ----------------------------------
8324 -- Is_Actual_Of_Previous_Formal --
8325 ----------------------------------
8327 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
8328 E1 : Entity_Id;
8330 begin
8331 E1 := First_Entity (P);
8332 while Present (E1) and then E1 /= Instance loop
8333 if Ekind (E1) = E_Package
8334 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
8335 then
8336 if Renamed_Object (E1) = Pack then
8337 return True;
8339 elsif E1 = P or else Renamed_Object (E1) = P then
8340 return False;
8342 elsif Is_Actual_Of_Previous_Formal (E1) then
8343 return True;
8344 end if;
8345 end if;
8347 Next_Entity (E1);
8348 end loop;
8350 return False;
8351 end Is_Actual_Of_Previous_Formal;
8353 -- Start of processing for Denotes_Formal_Package
8355 begin
8356 if On_Exit then
8357 Par :=
8358 Instance_Envs.Table
8359 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
8360 else
8361 Par := Current_Instantiated_Parent.Act_Id;
8362 end if;
8364 if Ekind (Scop) = E_Generic_Package
8365 or else Nkind (Unit_Declaration_Node (Scop)) =
8366 N_Generic_Subprogram_Declaration
8367 then
8368 return True;
8370 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
8371 N_Formal_Package_Declaration
8372 then
8373 return True;
8375 elsif No (Par) then
8376 return False;
8378 else
8379 -- Check whether this package is associated with a formal package of
8380 -- the enclosing instantiation. Iterate over the list of renamings.
8382 E := First_Entity (Par);
8383 while Present (E) loop
8384 if Ekind (E) /= E_Package
8385 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
8386 then
8387 null;
8389 elsif Renamed_Object (E) = Par then
8390 return False;
8392 elsif Renamed_Object (E) = Pack then
8393 return True;
8395 elsif Is_Actual_Of_Previous_Formal (E) then
8396 return True;
8398 end if;
8400 Next_Entity (E);
8401 end loop;
8403 return False;
8404 end if;
8405 end Denotes_Formal_Package;
8407 -----------------
8408 -- End_Generic --
8409 -----------------
8411 procedure End_Generic is
8412 begin
8413 -- ??? More things could be factored out in this routine. Should
8414 -- probably be done at a later stage.
8416 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8417 Generic_Flags.Decrement_Last;
8419 Expander_Mode_Restore;
8420 end End_Generic;
8422 -------------
8423 -- Earlier --
8424 -------------
8426 function Earlier (N1, N2 : Node_Id) return Boolean is
8427 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8428 -- Find distance from given node to enclosing compilation unit
8430 ----------------
8431 -- Find_Depth --
8432 ----------------
8434 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8435 begin
8436 while Present (P)
8437 and then Nkind (P) /= N_Compilation_Unit
8438 loop
8439 P := True_Parent (P);
8440 D := D + 1;
8441 end loop;
8442 end Find_Depth;
8444 -- Local declarations
8446 D1 : Integer := 0;
8447 D2 : Integer := 0;
8448 P1 : Node_Id := N1;
8449 P2 : Node_Id := N2;
8450 T1 : Source_Ptr;
8451 T2 : Source_Ptr;
8453 -- Start of processing for Earlier
8455 begin
8456 Find_Depth (P1, D1);
8457 Find_Depth (P2, D2);
8459 if P1 /= P2 then
8460 return False;
8461 else
8462 P1 := N1;
8463 P2 := N2;
8464 end if;
8466 while D1 > D2 loop
8467 P1 := True_Parent (P1);
8468 D1 := D1 - 1;
8469 end loop;
8471 while D2 > D1 loop
8472 P2 := True_Parent (P2);
8473 D2 := D2 - 1;
8474 end loop;
8476 -- At this point P1 and P2 are at the same distance from the root.
8477 -- We examine their parents until we find a common declarative list.
8478 -- If we reach the root, N1 and N2 do not descend from the same
8479 -- declarative list (e.g. one is nested in the declarative part and
8480 -- the other is in a block in the statement part) and the earlier
8481 -- one is already frozen.
8483 while not Is_List_Member (P1)
8484 or else not Is_List_Member (P2)
8485 or else List_Containing (P1) /= List_Containing (P2)
8486 loop
8487 P1 := True_Parent (P1);
8488 P2 := True_Parent (P2);
8490 if Nkind (Parent (P1)) = N_Subunit then
8491 P1 := Corresponding_Stub (Parent (P1));
8492 end if;
8494 if Nkind (Parent (P2)) = N_Subunit then
8495 P2 := Corresponding_Stub (Parent (P2));
8496 end if;
8498 if P1 = P2 then
8499 return False;
8500 end if;
8501 end loop;
8503 -- Expanded code usually shares the source location of the original
8504 -- construct it was generated for. This however may not necessarily
8505 -- reflect the true location of the code within the tree.
8507 -- Before comparing the slocs of the two nodes, make sure that we are
8508 -- working with correct source locations. Assume that P1 is to the left
8509 -- of P2. If either one does not come from source, traverse the common
8510 -- list heading towards the other node and locate the first source
8511 -- statement.
8513 -- P1 P2
8514 -- ----+===+===+--------------+===+===+----
8515 -- expanded code expanded code
8517 if not Comes_From_Source (P1) then
8518 while Present (P1) loop
8520 -- Neither P2 nor a source statement were located during the
8521 -- search. If we reach the end of the list, then P1 does not
8522 -- occur earlier than P2.
8524 -- ---->
8525 -- start --- P2 ----- P1 --- end
8527 if No (Next (P1)) then
8528 return False;
8530 -- We encounter P2 while going to the right of the list. This
8531 -- means that P1 does indeed appear earlier.
8533 -- ---->
8534 -- start --- P1 ===== P2 --- end
8535 -- expanded code in between
8537 elsif P1 = P2 then
8538 return True;
8540 -- No need to look any further since we have located a source
8541 -- statement.
8543 elsif Comes_From_Source (P1) then
8544 exit;
8545 end if;
8547 -- Keep going right
8549 Next (P1);
8550 end loop;
8551 end if;
8553 if not Comes_From_Source (P2) then
8554 while Present (P2) loop
8556 -- Neither P1 nor a source statement were located during the
8557 -- search. If we reach the start of the list, then P1 does not
8558 -- occur earlier than P2.
8560 -- <----
8561 -- start --- P2 --- P1 --- end
8563 if No (Prev (P2)) then
8564 return False;
8566 -- We encounter P1 while going to the left of the list. This
8567 -- means that P1 does indeed appear earlier.
8569 -- <----
8570 -- start --- P1 ===== P2 --- end
8571 -- expanded code in between
8573 elsif P2 = P1 then
8574 return True;
8576 -- No need to look any further since we have located a source
8577 -- statement.
8579 elsif Comes_From_Source (P2) then
8580 exit;
8581 end if;
8583 -- Keep going left
8585 Prev (P2);
8586 end loop;
8587 end if;
8589 -- At this point either both nodes came from source or we approximated
8590 -- their source locations through neighboring source statements.
8592 T1 := Top_Level_Location (Sloc (P1));
8593 T2 := Top_Level_Location (Sloc (P2));
8595 -- When two nodes come from the same instance, they have identical top
8596 -- level locations. To determine proper relation within the tree, check
8597 -- their locations within the template.
8599 if T1 = T2 then
8600 return Sloc (P1) < Sloc (P2);
8602 -- The two nodes either come from unrelated instances or do not come
8603 -- from instantiated code at all.
8605 else
8606 return T1 < T2;
8607 end if;
8608 end Earlier;
8610 ----------------------
8611 -- Find_Actual_Type --
8612 ----------------------
8614 function Find_Actual_Type
8615 (Typ : Entity_Id;
8616 Gen_Type : Entity_Id) return Entity_Id
8618 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8619 T : Entity_Id;
8621 begin
8622 -- Special processing only applies to child units
8624 if not Is_Child_Unit (Gen_Scope) then
8625 return Get_Instance_Of (Typ);
8627 -- If designated or component type is itself a formal of the child unit,
8628 -- its instance is available.
8630 elsif Scope (Typ) = Gen_Scope then
8631 return Get_Instance_Of (Typ);
8633 -- If the array or access type is not declared in the parent unit,
8634 -- no special processing needed.
8636 elsif not Is_Generic_Type (Typ)
8637 and then Scope (Gen_Scope) /= Scope (Typ)
8638 then
8639 return Get_Instance_Of (Typ);
8641 -- Otherwise, retrieve designated or component type by visibility
8643 else
8644 T := Current_Entity (Typ);
8645 while Present (T) loop
8646 if In_Open_Scopes (Scope (T)) then
8647 return T;
8648 elsif Is_Generic_Actual_Type (T) then
8649 return T;
8650 end if;
8652 T := Homonym (T);
8653 end loop;
8655 return Typ;
8656 end if;
8657 end Find_Actual_Type;
8659 ----------------------------
8660 -- Freeze_Subprogram_Body --
8661 ----------------------------
8663 procedure Freeze_Subprogram_Body
8664 (Inst_Node : Node_Id;
8665 Gen_Body : Node_Id;
8666 Pack_Id : Entity_Id)
8668 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8669 Par : constant Entity_Id := Scope (Gen_Unit);
8670 E_G_Id : Entity_Id;
8671 Enc_G : Entity_Id;
8672 Enc_I : Node_Id;
8673 F_Node : Node_Id;
8675 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8676 -- Find innermost package body that encloses the given node, and which
8677 -- is not a compilation unit. Freeze nodes for the instance, or for its
8678 -- enclosing body, may be inserted after the enclosing_body of the
8679 -- generic unit. Used to determine proper placement of freeze node for
8680 -- both package and subprogram instances.
8682 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8683 -- Find entity for given package body, and locate or create a freeze
8684 -- node for it.
8686 ----------------------------
8687 -- Enclosing_Package_Body --
8688 ----------------------------
8690 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8691 P : Node_Id;
8693 begin
8694 P := Parent (N);
8695 while Present (P)
8696 and then Nkind (Parent (P)) /= N_Compilation_Unit
8697 loop
8698 if Nkind (P) = N_Package_Body then
8699 if Nkind (Parent (P)) = N_Subunit then
8700 return Corresponding_Stub (Parent (P));
8701 else
8702 return P;
8703 end if;
8704 end if;
8706 P := True_Parent (P);
8707 end loop;
8709 return Empty;
8710 end Enclosing_Package_Body;
8712 -------------------------
8713 -- Package_Freeze_Node --
8714 -------------------------
8716 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8717 Id : Entity_Id;
8719 begin
8720 if Nkind (B) = N_Package_Body then
8721 Id := Corresponding_Spec (B);
8722 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8723 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8724 end if;
8726 Ensure_Freeze_Node (Id);
8727 return Freeze_Node (Id);
8728 end Package_Freeze_Node;
8730 -- Start of processing for Freeze_Subprogram_Body
8732 begin
8733 -- If the instance and the generic body appear within the same unit, and
8734 -- the instance precedes the generic, the freeze node for the instance
8735 -- must appear after that of the generic. If the generic is nested
8736 -- within another instance I2, then current instance must be frozen
8737 -- after I2. In both cases, the freeze nodes are those of enclosing
8738 -- packages. Otherwise, the freeze node is placed at the end of the
8739 -- current declarative part.
8741 Enc_G := Enclosing_Package_Body (Gen_Body);
8742 Enc_I := Enclosing_Package_Body (Inst_Node);
8743 Ensure_Freeze_Node (Pack_Id);
8744 F_Node := Freeze_Node (Pack_Id);
8746 if Is_Generic_Instance (Par)
8747 and then Present (Freeze_Node (Par))
8748 and then In_Same_Declarative_Part
8749 (Parent (Freeze_Node (Par)), Inst_Node)
8750 then
8751 -- The parent was a premature instantiation. Insert freeze node at
8752 -- the end the current declarative part.
8754 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par)) then
8755 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8757 -- Handle the following case:
8759 -- package Parent_Inst is new ...
8760 -- Parent_Inst []
8762 -- procedure P ... -- this body freezes Parent_Inst
8764 -- package Inst is new ...
8766 -- In this particular scenario, the freeze node for Inst must be
8767 -- inserted in the same manner as that of Parent_Inst - before the
8768 -- next source body or at the end of the declarative list (body not
8769 -- available). If body P did not exist and Parent_Inst was frozen
8770 -- after Inst, either by a body following Inst or at the end of the
8771 -- declarative region, the freeze node for Inst must be inserted
8772 -- after that of Parent_Inst. This relation is established by
8773 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8775 elsif List_Containing (Get_Unit_Instantiation_Node (Par)) =
8776 List_Containing (Inst_Node)
8777 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8778 then
8779 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8781 else
8782 Insert_After (Freeze_Node (Par), F_Node);
8783 end if;
8785 -- The body enclosing the instance should be frozen after the body that
8786 -- includes the generic, because the body of the instance may make
8787 -- references to entities therein. If the two are not in the same
8788 -- declarative part, or if the one enclosing the instance is frozen
8789 -- already, freeze the instance at the end of the current declarative
8790 -- part.
8792 elsif Is_Generic_Instance (Par)
8793 and then Present (Freeze_Node (Par))
8794 and then Present (Enc_I)
8795 then
8796 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), Enc_I)
8797 or else
8798 (Nkind (Enc_I) = N_Package_Body
8799 and then In_Same_Declarative_Part
8800 (Parent (Freeze_Node (Par)), Parent (Enc_I)))
8801 then
8802 -- The enclosing package may contain several instances. Rather
8803 -- than computing the earliest point at which to insert its freeze
8804 -- node, we place it at the end of the declarative part of the
8805 -- parent of the generic.
8807 Insert_Freeze_Node_For_Instance
8808 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8809 end if;
8811 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8813 elsif Present (Enc_G)
8814 and then Present (Enc_I)
8815 and then Enc_G /= Enc_I
8816 and then Earlier (Inst_Node, Gen_Body)
8817 then
8818 if Nkind (Enc_G) = N_Package_Body then
8819 E_G_Id :=
8820 Corresponding_Spec (Enc_G);
8821 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8822 E_G_Id :=
8823 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8824 end if;
8826 -- Freeze package that encloses instance, and place node after the
8827 -- package that encloses generic. If enclosing package is already
8828 -- frozen we have to assume it is at the proper place. This may be a
8829 -- potential ABE that requires dynamic checking. Do not add a freeze
8830 -- node if the package that encloses the generic is inside the body
8831 -- that encloses the instance, because the freeze node would be in
8832 -- the wrong scope. Additional contortions needed if the bodies are
8833 -- within a subunit.
8835 declare
8836 Enclosing_Body : Node_Id;
8838 begin
8839 if Nkind (Enc_I) = N_Package_Body_Stub then
8840 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8841 else
8842 Enclosing_Body := Enc_I;
8843 end if;
8845 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8846 Insert_Freeze_Node_For_Instance
8847 (Enc_G, Package_Freeze_Node (Enc_I));
8848 end if;
8849 end;
8851 -- Freeze enclosing subunit before instance
8853 Ensure_Freeze_Node (E_G_Id);
8855 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8856 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8857 end if;
8859 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8861 else
8862 -- If none of the above, insert freeze node at the end of the current
8863 -- declarative part.
8865 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8866 end if;
8867 end Freeze_Subprogram_Body;
8869 ----------------
8870 -- Get_Gen_Id --
8871 ----------------
8873 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8874 begin
8875 return Generic_Renamings.Table (E).Gen_Id;
8876 end Get_Gen_Id;
8878 ---------------------
8879 -- Get_Instance_Of --
8880 ---------------------
8882 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8883 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8885 begin
8886 if Res /= Assoc_Null then
8887 return Generic_Renamings.Table (Res).Act_Id;
8889 else
8890 -- On exit, entity is not instantiated: not a generic parameter, or
8891 -- else parameter of an inner generic unit.
8893 return A;
8894 end if;
8895 end Get_Instance_Of;
8897 ---------------------------------
8898 -- Get_Unit_Instantiation_Node --
8899 ---------------------------------
8901 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
8902 Decl : Node_Id := Unit_Declaration_Node (A);
8903 Inst : Node_Id;
8905 begin
8906 -- If the Package_Instantiation attribute has been set on the package
8907 -- entity, then use it directly when it (or its Original_Node) refers
8908 -- to an N_Package_Instantiation node. In principle it should be
8909 -- possible to have this field set in all cases, which should be
8910 -- investigated, and would allow this function to be significantly
8911 -- simplified. ???
8913 Inst := Package_Instantiation (A);
8915 if Present (Inst) then
8916 if Nkind (Inst) = N_Package_Instantiation then
8917 return Inst;
8919 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8920 return Original_Node (Inst);
8921 end if;
8922 end if;
8924 -- If the instantiation is a compilation unit that does not need body
8925 -- then the instantiation node has been rewritten as a package
8926 -- declaration for the instance, and we return the original node.
8928 -- If it is a compilation unit and the instance node has not been
8929 -- rewritten, then it is still the unit of the compilation. Finally, if
8930 -- a body is present, this is a parent of the main unit whose body has
8931 -- been compiled for inlining purposes, and the instantiation node has
8932 -- been rewritten with the instance body.
8934 -- Otherwise the instantiation node appears after the declaration. If
8935 -- the entity is a formal package, the declaration may have been
8936 -- rewritten as a generic declaration (in the case of a formal with box)
8937 -- or left as a formal package declaration if it has actuals, and is
8938 -- found with a forward search.
8940 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8941 if Nkind (Decl) = N_Package_Declaration
8942 and then Present (Corresponding_Body (Decl))
8943 then
8944 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8945 end if;
8947 if Nkind_In (Original_Node (Decl), N_Function_Instantiation,
8948 N_Package_Instantiation,
8949 N_Procedure_Instantiation)
8950 then
8951 return Original_Node (Decl);
8952 else
8953 return Unit (Parent (Decl));
8954 end if;
8956 elsif Nkind (Decl) = N_Package_Declaration
8957 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8958 then
8959 return Original_Node (Decl);
8961 else
8962 Inst := Next (Decl);
8963 while not Nkind_In (Inst, N_Formal_Package_Declaration,
8964 N_Function_Instantiation,
8965 N_Package_Instantiation,
8966 N_Procedure_Instantiation)
8967 loop
8968 Next (Inst);
8969 end loop;
8971 return Inst;
8972 end if;
8973 end Get_Unit_Instantiation_Node;
8975 ------------------------
8976 -- Has_Been_Exchanged --
8977 ------------------------
8979 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8980 Next : Elmt_Id;
8982 begin
8983 Next := First_Elmt (Exchanged_Views);
8984 while Present (Next) loop
8985 if Full_View (Node (Next)) = E then
8986 return True;
8987 end if;
8989 Next_Elmt (Next);
8990 end loop;
8992 return False;
8993 end Has_Been_Exchanged;
8995 ----------
8996 -- Hash --
8997 ----------
8999 function Hash (F : Entity_Id) return HTable_Range is
9000 begin
9001 return HTable_Range (F mod HTable_Size);
9002 end Hash;
9004 ------------------------
9005 -- Hide_Current_Scope --
9006 ------------------------
9008 procedure Hide_Current_Scope is
9009 C : constant Entity_Id := Current_Scope;
9010 E : Entity_Id;
9012 begin
9013 Set_Is_Hidden_Open_Scope (C);
9015 E := First_Entity (C);
9016 while Present (E) loop
9017 if Is_Immediately_Visible (E) then
9018 Set_Is_Immediately_Visible (E, False);
9019 Append_Elmt (E, Hidden_Entities);
9020 end if;
9022 Next_Entity (E);
9023 end loop;
9025 -- Make the scope name invisible as well. This is necessary, but might
9026 -- conflict with calls to Rtsfind later on, in case the scope is a
9027 -- predefined one. There is no clean solution to this problem, so for
9028 -- now we depend on the user not redefining Standard itself in one of
9029 -- the parent units.
9031 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
9032 Set_Is_Immediately_Visible (C, False);
9033 Append_Elmt (C, Hidden_Entities);
9034 end if;
9036 end Hide_Current_Scope;
9038 --------------
9039 -- Init_Env --
9040 --------------
9042 procedure Init_Env is
9043 Saved : Instance_Env;
9045 begin
9046 Saved.Instantiated_Parent := Current_Instantiated_Parent;
9047 Saved.Exchanged_Views := Exchanged_Views;
9048 Saved.Hidden_Entities := Hidden_Entities;
9049 Saved.Current_Sem_Unit := Current_Sem_Unit;
9050 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
9051 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
9053 -- Save configuration switches. These may be reset if the unit is a
9054 -- predefined unit, and the current mode is not Ada 2005.
9056 Saved.Switches := Save_Config_Switches;
9058 Instance_Envs.Append (Saved);
9060 Exchanged_Views := New_Elmt_List;
9061 Hidden_Entities := New_Elmt_List;
9063 -- Make dummy entry for Instantiated parent. If generic unit is legal,
9064 -- this is set properly in Set_Instance_Env.
9066 Current_Instantiated_Parent :=
9067 (Current_Scope, Current_Scope, Assoc_Null);
9068 end Init_Env;
9070 ---------------------
9071 -- In_Main_Context --
9072 ---------------------
9074 function In_Main_Context (E : Entity_Id) return Boolean is
9075 Context : List_Id;
9076 Clause : Node_Id;
9077 Nam : Node_Id;
9079 begin
9080 if not Is_Compilation_Unit (E)
9081 or else Ekind (E) /= E_Package
9082 or else In_Private_Part (E)
9083 then
9084 return False;
9085 end if;
9087 Context := Context_Items (Cunit (Main_Unit));
9089 Clause := First (Context);
9090 while Present (Clause) loop
9091 if Nkind (Clause) = N_With_Clause then
9092 Nam := Name (Clause);
9094 -- If the current scope is part of the context of the main unit,
9095 -- analysis of the corresponding with_clause is not complete, and
9096 -- the entity is not set. We use the Chars field directly, which
9097 -- might produce false positives in rare cases, but guarantees
9098 -- that we produce all the instance bodies we will need.
9100 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
9101 or else (Nkind (Nam) = N_Selected_Component
9102 and then Chars (Selector_Name (Nam)) = Chars (E))
9103 then
9104 return True;
9105 end if;
9106 end if;
9108 Next (Clause);
9109 end loop;
9111 return False;
9112 end In_Main_Context;
9114 ---------------------
9115 -- Inherit_Context --
9116 ---------------------
9118 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
9119 Current_Context : List_Id;
9120 Current_Unit : Node_Id;
9121 Item : Node_Id;
9122 New_I : Node_Id;
9124 Clause : Node_Id;
9125 OK : Boolean;
9126 Lib_Unit : Node_Id;
9128 begin
9129 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
9131 -- The inherited context is attached to the enclosing compilation
9132 -- unit. This is either the main unit, or the declaration for the
9133 -- main unit (in case the instantiation appears within the package
9134 -- declaration and the main unit is its body).
9136 Current_Unit := Parent (Inst);
9137 while Present (Current_Unit)
9138 and then Nkind (Current_Unit) /= N_Compilation_Unit
9139 loop
9140 Current_Unit := Parent (Current_Unit);
9141 end loop;
9143 Current_Context := Context_Items (Current_Unit);
9145 Item := First (Context_Items (Parent (Gen_Decl)));
9146 while Present (Item) loop
9147 if Nkind (Item) = N_With_Clause then
9148 Lib_Unit := Library_Unit (Item);
9150 -- Take care to prevent direct cyclic with's
9152 if Lib_Unit /= Current_Unit then
9154 -- Do not add a unit if it is already in the context
9156 Clause := First (Current_Context);
9157 OK := True;
9158 while Present (Clause) loop
9159 if Nkind (Clause) = N_With_Clause
9160 and then Library_Unit (Clause) = Lib_Unit
9161 then
9162 OK := False;
9163 exit;
9164 end if;
9166 Next (Clause);
9167 end loop;
9169 if OK then
9170 New_I := New_Copy (Item);
9171 Set_Implicit_With (New_I);
9173 Append (New_I, Current_Context);
9174 end if;
9175 end if;
9176 end if;
9178 Next (Item);
9179 end loop;
9180 end if;
9181 end Inherit_Context;
9183 ----------------
9184 -- Initialize --
9185 ----------------
9187 procedure Initialize is
9188 begin
9189 Generic_Renamings.Init;
9190 Instance_Envs.Init;
9191 Generic_Flags.Init;
9192 Generic_Renamings_HTable.Reset;
9193 Circularity_Detected := False;
9194 Exchanged_Views := No_Elist;
9195 Hidden_Entities := No_Elist;
9196 end Initialize;
9198 -------------------------------------
9199 -- Insert_Freeze_Node_For_Instance --
9200 -------------------------------------
9202 procedure Insert_Freeze_Node_For_Instance
9203 (N : Node_Id;
9204 F_Node : Node_Id)
9206 Decl : Node_Id;
9207 Decls : List_Id;
9208 Inst : Entity_Id;
9209 Par_N : Node_Id;
9211 function Enclosing_Body (N : Node_Id) return Node_Id;
9212 -- Find enclosing package or subprogram body, if any. Freeze node may
9213 -- be placed at end of current declarative list if previous instance
9214 -- and current one have different enclosing bodies.
9216 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
9217 -- Find the local instance, if any, that declares the generic that is
9218 -- being instantiated. If present, the freeze node for this instance
9219 -- must follow the freeze node for the previous instance.
9221 --------------------
9222 -- Enclosing_Body --
9223 --------------------
9225 function Enclosing_Body (N : Node_Id) return Node_Id is
9226 P : Node_Id;
9228 begin
9229 P := Parent (N);
9230 while Present (P)
9231 and then Nkind (Parent (P)) /= N_Compilation_Unit
9232 loop
9233 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
9234 if Nkind (Parent (P)) = N_Subunit then
9235 return Corresponding_Stub (Parent (P));
9236 else
9237 return P;
9238 end if;
9239 end if;
9241 P := True_Parent (P);
9242 end loop;
9244 return Empty;
9245 end Enclosing_Body;
9247 -----------------------
9248 -- Previous_Instance --
9249 -----------------------
9251 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
9252 S : Entity_Id;
9254 begin
9255 S := Scope (Gen);
9256 while Present (S) and then S /= Standard_Standard loop
9257 if Is_Generic_Instance (S)
9258 and then In_Same_Source_Unit (S, N)
9259 then
9260 return S;
9261 end if;
9263 S := Scope (S);
9264 end loop;
9266 return Empty;
9267 end Previous_Instance;
9269 -- Start of processing for Insert_Freeze_Node_For_Instance
9271 begin
9272 if not Is_List_Member (F_Node) then
9273 Decl := N;
9274 Decls := List_Containing (N);
9275 Inst := Entity (F_Node);
9276 Par_N := Parent (Decls);
9278 -- When processing a subprogram instantiation, utilize the actual
9279 -- subprogram instantiation rather than its package wrapper as it
9280 -- carries all the context information.
9282 if Is_Wrapper_Package (Inst) then
9283 Inst := Related_Instance (Inst);
9284 end if;
9286 -- If this is a package instance, check whether the generic is
9287 -- declared in a previous instance and the current instance is
9288 -- not within the previous one.
9290 if Present (Generic_Parent (Parent (Inst)))
9291 and then Is_In_Main_Unit (N)
9292 then
9293 declare
9294 Enclosing_N : constant Node_Id := Enclosing_Body (N);
9295 Par_I : constant Entity_Id :=
9296 Previous_Instance
9297 (Generic_Parent (Parent (Inst)));
9298 Scop : Entity_Id;
9300 begin
9301 if Present (Par_I)
9302 and then Earlier (N, Freeze_Node (Par_I))
9303 then
9304 Scop := Scope (Inst);
9306 -- If the current instance is within the one that contains
9307 -- the generic, the freeze node for the current one must
9308 -- appear in the current declarative part. Ditto, if the
9309 -- current instance is within another package instance or
9310 -- within a body that does not enclose the current instance.
9311 -- In these three cases the freeze node of the previous
9312 -- instance is not relevant.
9314 while Present (Scop) and then Scop /= Standard_Standard loop
9315 exit when Scop = Par_I
9316 or else
9317 (Is_Generic_Instance (Scop)
9318 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
9319 Scop := Scope (Scop);
9320 end loop;
9322 -- Previous instance encloses current instance
9324 if Scop = Par_I then
9325 null;
9327 -- If the next node is a source body we must freeze in
9328 -- the current scope as well.
9330 elsif Present (Next (N))
9331 and then Nkind_In (Next (N), N_Subprogram_Body,
9332 N_Package_Body)
9333 and then Comes_From_Source (Next (N))
9334 then
9335 null;
9337 -- Current instance is within an unrelated instance
9339 elsif Is_Generic_Instance (Scop) then
9340 null;
9342 -- Current instance is within an unrelated body
9344 elsif Present (Enclosing_N)
9345 and then Enclosing_N /= Enclosing_Body (Par_I)
9346 then
9347 null;
9349 else
9350 Insert_After (Freeze_Node (Par_I), F_Node);
9351 return;
9352 end if;
9353 end if;
9354 end;
9355 end if;
9357 -- When the instantiation occurs in a package declaration, append the
9358 -- freeze node to the private declarations (if any).
9360 if Nkind (Par_N) = N_Package_Specification
9361 and then Decls = Visible_Declarations (Par_N)
9362 and then Present (Private_Declarations (Par_N))
9363 and then not Is_Empty_List (Private_Declarations (Par_N))
9364 then
9365 Decls := Private_Declarations (Par_N);
9366 Decl := First (Decls);
9367 end if;
9369 -- Determine the proper freeze point of a package instantiation. We
9370 -- adhere to the general rule of a package or subprogram body causing
9371 -- freezing of anything before it in the same declarative region. In
9372 -- this case, the proper freeze point of a package instantiation is
9373 -- before the first source body which follows, or before a stub. This
9374 -- ensures that entities coming from the instance are already frozen
9375 -- and usable in source bodies.
9377 if Nkind (Par_N) /= N_Package_Declaration
9378 and then Ekind (Inst) = E_Package
9379 and then Is_Generic_Instance (Inst)
9380 and then
9381 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
9382 then
9383 while Present (Decl) loop
9384 if (Nkind (Decl) in N_Unit_Body
9385 or else
9386 Nkind (Decl) in N_Body_Stub)
9387 and then Comes_From_Source (Decl)
9388 then
9389 Insert_Before (Decl, F_Node);
9390 return;
9391 end if;
9393 Next (Decl);
9394 end loop;
9395 end if;
9397 -- In a package declaration, or if no previous body, insert at end
9398 -- of list.
9400 Set_Sloc (F_Node, Sloc (Last (Decls)));
9401 Insert_After (Last (Decls), F_Node);
9402 end if;
9403 end Insert_Freeze_Node_For_Instance;
9405 ------------------
9406 -- Install_Body --
9407 ------------------
9409 procedure Install_Body
9410 (Act_Body : Node_Id;
9411 N : Node_Id;
9412 Gen_Body : Node_Id;
9413 Gen_Decl : Node_Id)
9415 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9416 -- Check if the generic definition and the instantiation come from
9417 -- a common scope, in which case the instance must be frozen after
9418 -- the generic body.
9420 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9421 -- If the instance is nested inside a generic unit, the Sloc of the
9422 -- instance indicates the place of the original definition, not the
9423 -- point of the current enclosing instance. Pending a better usage of
9424 -- Slocs to indicate instantiation places, we determine the place of
9425 -- origin of a node by finding the maximum sloc of any ancestor node.
9426 -- Why is this not equivalent to Top_Level_Location ???
9428 -------------------
9429 -- In_Same_Scope --
9430 -------------------
9432 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9433 Act_Scop : Entity_Id := Scope (Act_Id);
9434 Gen_Scop : Entity_Id := Scope (Gen_Id);
9436 begin
9437 while Act_Scop /= Standard_Standard
9438 and then Gen_Scop /= Standard_Standard
9439 loop
9440 if Act_Scop = Gen_Scop then
9441 return True;
9442 end if;
9444 Act_Scop := Scope (Act_Scop);
9445 Gen_Scop := Scope (Gen_Scop);
9446 end loop;
9448 return False;
9449 end In_Same_Scope;
9451 ---------------
9452 -- True_Sloc --
9453 ---------------
9455 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9456 N1 : Node_Id;
9457 Res : Source_Ptr;
9459 begin
9460 Res := Sloc (N);
9461 N1 := N;
9462 while Present (N1) and then N1 /= Act_Unit loop
9463 if Sloc (N1) > Res then
9464 Res := Sloc (N1);
9465 end if;
9467 N1 := Parent (N1);
9468 end loop;
9470 return Res;
9471 end True_Sloc;
9473 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
9474 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9475 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
9476 Par : constant Entity_Id := Scope (Gen_Id);
9477 Gen_Unit : constant Node_Id :=
9478 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9480 Body_Unit : Node_Id;
9481 F_Node : Node_Id;
9482 Must_Delay : Boolean;
9483 Orig_Body : Node_Id := Gen_Body;
9485 -- Start of processing for Install_Body
9487 begin
9488 -- Handle first the case of an instance with incomplete actual types.
9489 -- The instance body cannot be placed after the declaration because
9490 -- full views have not been seen yet. Any use of the non-limited views
9491 -- in the instance body requires the presence of a regular with_clause
9492 -- in the enclosing unit, and will fail if this with_clause is missing.
9493 -- We place the instance body at the beginning of the enclosing body,
9494 -- which is the unit being compiled. The freeze node for the instance
9495 -- is then placed after the instance body.
9497 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
9498 and then Expander_Active
9499 and then Ekind (Scope (Act_Id)) = E_Package
9500 then
9501 declare
9502 Scop : constant Entity_Id := Scope (Act_Id);
9503 Body_Id : constant Node_Id :=
9504 Corresponding_Body (Unit_Declaration_Node (Scop));
9506 begin
9507 Ensure_Freeze_Node (Act_Id);
9508 F_Node := Freeze_Node (Act_Id);
9509 if Present (Body_Id) then
9510 Set_Is_Frozen (Act_Id, False);
9511 Prepend (Act_Body, Declarations (Parent (Body_Id)));
9512 if Is_List_Member (F_Node) then
9513 Remove (F_Node);
9514 end if;
9516 Insert_After (Act_Body, F_Node);
9517 end if;
9518 end;
9519 return;
9520 end if;
9522 -- If the body is a subunit, the freeze point is the corresponding stub
9523 -- in the current compilation, not the subunit itself.
9525 if Nkind (Parent (Gen_Body)) = N_Subunit then
9526 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9527 else
9528 Orig_Body := Gen_Body;
9529 end if;
9531 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9533 -- If the instantiation and the generic definition appear in the same
9534 -- package declaration, this is an early instantiation. If they appear
9535 -- in the same declarative part, it is an early instantiation only if
9536 -- the generic body appears textually later, and the generic body is
9537 -- also in the main unit.
9539 -- If instance is nested within a subprogram, and the generic body
9540 -- is not, the instance is delayed because the enclosing body is. If
9541 -- instance and body are within the same scope, or the same subprogram
9542 -- body, indicate explicitly that the instance is delayed.
9544 Must_Delay :=
9545 (Gen_Unit = Act_Unit
9546 and then (Nkind_In (Gen_Unit, N_Generic_Package_Declaration,
9547 N_Package_Declaration)
9548 or else (Gen_Unit = Body_Unit
9549 and then True_Sloc (N, Act_Unit) <
9550 Sloc (Orig_Body)))
9551 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9552 and then In_Same_Scope (Gen_Id, Act_Id));
9554 -- If this is an early instantiation, the freeze node is placed after
9555 -- the generic body. Otherwise, if the generic appears in an instance,
9556 -- we cannot freeze the current instance until the outer one is frozen.
9557 -- This is only relevant if the current instance is nested within some
9558 -- inner scope not itself within the outer instance. If this scope is
9559 -- a package body in the same declarative part as the outer instance,
9560 -- then that body needs to be frozen after the outer instance. Finally,
9561 -- if no delay is needed, we place the freeze node at the end of the
9562 -- current declarative part.
9564 if Expander_Active
9565 and then (No (Freeze_Node (Act_Id))
9566 or else not Is_List_Member (Freeze_Node (Act_Id)))
9567 then
9568 Ensure_Freeze_Node (Act_Id);
9569 F_Node := Freeze_Node (Act_Id);
9571 if Must_Delay then
9572 Insert_After (Orig_Body, F_Node);
9574 elsif Is_Generic_Instance (Par)
9575 and then Present (Freeze_Node (Par))
9576 and then Scope (Act_Id) /= Par
9577 then
9578 -- Freeze instance of inner generic after instance of enclosing
9579 -- generic.
9581 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), N) then
9583 -- Handle the following case:
9585 -- package Parent_Inst is new ...
9586 -- Parent_Inst []
9588 -- procedure P ... -- this body freezes Parent_Inst
9590 -- package Inst is new ...
9592 -- In this particular scenario, the freeze node for Inst must
9593 -- be inserted in the same manner as that of Parent_Inst,
9594 -- before the next source body or at the end of the declarative
9595 -- list (body not available). If body P did not exist and
9596 -- Parent_Inst was frozen after Inst, either by a body
9597 -- following Inst or at the end of the declarative region,
9598 -- the freeze node for Inst must be inserted after that of
9599 -- Parent_Inst. This relation is established by comparing
9600 -- the Slocs of Parent_Inst freeze node and Inst.
9601 -- We examine the parents of the enclosing lists to handle
9602 -- the case where the parent instance is in the visible part
9603 -- of a package declaration, and the inner instance is in
9604 -- the corresponding private part.
9606 if Parent (List_Containing (Get_Unit_Instantiation_Node (Par)))
9607 = Parent (List_Containing (N))
9608 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9609 then
9610 Insert_Freeze_Node_For_Instance (N, F_Node);
9611 else
9612 Insert_After (Freeze_Node (Par), F_Node);
9613 end if;
9615 -- Freeze package enclosing instance of inner generic after
9616 -- instance of enclosing generic.
9618 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
9619 and then In_Same_Declarative_Part
9620 (Parent (Freeze_Node (Par)), Parent (N))
9621 then
9622 declare
9623 Enclosing : Entity_Id;
9625 begin
9626 Enclosing := Corresponding_Spec (Parent (N));
9628 if No (Enclosing) then
9629 Enclosing := Defining_Entity (Parent (N));
9630 end if;
9632 Insert_Freeze_Node_For_Instance (N, F_Node);
9633 Ensure_Freeze_Node (Enclosing);
9635 if not Is_List_Member (Freeze_Node (Enclosing)) then
9637 -- The enclosing context is a subunit, insert the freeze
9638 -- node after the stub.
9640 if Nkind (Parent (Parent (N))) = N_Subunit then
9641 Insert_Freeze_Node_For_Instance
9642 (Corresponding_Stub (Parent (Parent (N))),
9643 Freeze_Node (Enclosing));
9645 -- The enclosing context is a package with a stub body
9646 -- which has already been replaced by the real body.
9647 -- Insert the freeze node after the actual body.
9649 elsif Ekind (Enclosing) = E_Package
9650 and then Present (Body_Entity (Enclosing))
9651 and then Was_Originally_Stub
9652 (Parent (Body_Entity (Enclosing)))
9653 then
9654 Insert_Freeze_Node_For_Instance
9655 (Parent (Body_Entity (Enclosing)),
9656 Freeze_Node (Enclosing));
9658 -- The parent instance has been frozen before the body of
9659 -- the enclosing package, insert the freeze node after
9660 -- the body.
9662 elsif List_Containing (Freeze_Node (Par)) =
9663 List_Containing (Parent (N))
9664 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9665 then
9666 Insert_Freeze_Node_For_Instance
9667 (Parent (N), Freeze_Node (Enclosing));
9669 else
9670 Insert_After
9671 (Freeze_Node (Par), Freeze_Node (Enclosing));
9672 end if;
9673 end if;
9674 end;
9676 else
9677 Insert_Freeze_Node_For_Instance (N, F_Node);
9678 end if;
9680 else
9681 Insert_Freeze_Node_For_Instance (N, F_Node);
9682 end if;
9683 end if;
9685 Set_Is_Frozen (Act_Id);
9686 Insert_Before (N, Act_Body);
9687 Mark_Rewrite_Insertion (Act_Body);
9688 end Install_Body;
9690 -----------------------------
9691 -- Install_Formal_Packages --
9692 -----------------------------
9694 procedure Install_Formal_Packages (Par : Entity_Id) is
9695 E : Entity_Id;
9696 Gen : Entity_Id;
9697 Gen_E : Entity_Id := Empty;
9699 begin
9700 E := First_Entity (Par);
9702 -- If we are installing an instance parent, locate the formal packages
9703 -- of its generic parent.
9705 if Is_Generic_Instance (Par) then
9706 Gen := Generic_Parent (Package_Specification (Par));
9707 Gen_E := First_Entity (Gen);
9708 end if;
9710 while Present (E) loop
9711 if Ekind (E) = E_Package
9712 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9713 then
9714 -- If this is the renaming for the parent instance, done
9716 if Renamed_Object (E) = Par then
9717 exit;
9719 -- The visibility of a formal of an enclosing generic is already
9720 -- correct.
9722 elsif Denotes_Formal_Package (E) then
9723 null;
9725 elsif Present (Associated_Formal_Package (E)) then
9726 Check_Generic_Actuals (Renamed_Object (E), True);
9727 Set_Is_Hidden (E, False);
9729 -- Find formal package in generic unit that corresponds to
9730 -- (instance of) formal package in instance.
9732 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9733 Next_Entity (Gen_E);
9734 end loop;
9736 if Present (Gen_E) then
9737 Map_Formal_Package_Entities (Gen_E, E);
9738 end if;
9739 end if;
9740 end if;
9742 Next_Entity (E);
9744 if Present (Gen_E) then
9745 Next_Entity (Gen_E);
9746 end if;
9747 end loop;
9748 end Install_Formal_Packages;
9750 --------------------
9751 -- Install_Parent --
9752 --------------------
9754 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9755 Ancestors : constant Elist_Id := New_Elmt_List;
9756 S : constant Entity_Id := Current_Scope;
9757 Inst_Par : Entity_Id;
9758 First_Par : Entity_Id;
9759 Inst_Node : Node_Id;
9760 Gen_Par : Entity_Id;
9761 First_Gen : Entity_Id;
9762 Elmt : Elmt_Id;
9764 procedure Install_Noninstance_Specs (Par : Entity_Id);
9765 -- Install the scopes of noninstance parent units ending with Par
9767 procedure Install_Spec (Par : Entity_Id);
9768 -- The child unit is within the declarative part of the parent, so the
9769 -- declarations within the parent are immediately visible.
9771 -------------------------------
9772 -- Install_Noninstance_Specs --
9773 -------------------------------
9775 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9776 begin
9777 if Present (Par)
9778 and then Par /= Standard_Standard
9779 and then not In_Open_Scopes (Par)
9780 then
9781 Install_Noninstance_Specs (Scope (Par));
9782 Install_Spec (Par);
9783 end if;
9784 end Install_Noninstance_Specs;
9786 ------------------
9787 -- Install_Spec --
9788 ------------------
9790 procedure Install_Spec (Par : Entity_Id) is
9791 Spec : constant Node_Id := Package_Specification (Par);
9793 begin
9794 -- If this parent of the child instance is a top-level unit,
9795 -- then record the unit and its visibility for later resetting in
9796 -- Remove_Parent. We exclude units that are generic instances, as we
9797 -- only want to record this information for the ultimate top-level
9798 -- noninstance parent (is that always correct???).
9800 if Scope (Par) = Standard_Standard
9801 and then not Is_Generic_Instance (Par)
9802 then
9803 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9804 Instance_Parent_Unit := Par;
9805 end if;
9807 -- Open the parent scope and make it and its declarations visible.
9808 -- If this point is not within a body, then only the visible
9809 -- declarations should be made visible, and installation of the
9810 -- private declarations is deferred until the appropriate point
9811 -- within analysis of the spec being instantiated (see the handling
9812 -- of parent visibility in Analyze_Package_Specification). This is
9813 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9814 -- private view problems that occur when compiling instantiations of
9815 -- a generic child of that package (Generic_Dispatching_Constructor).
9816 -- If the instance freezes a tagged type, inlinings of operations
9817 -- from Ada.Tags may need the full view of type Tag. If inlining took
9818 -- proper account of establishing visibility of inlined subprograms'
9819 -- parents then it should be possible to remove this
9820 -- special check. ???
9822 Push_Scope (Par);
9823 Set_Is_Immediately_Visible (Par);
9824 Install_Visible_Declarations (Par);
9825 Set_Use (Visible_Declarations (Spec));
9827 if In_Body or else Is_RTU (Par, Ada_Tags) then
9828 Install_Private_Declarations (Par);
9829 Set_Use (Private_Declarations (Spec));
9830 end if;
9831 end Install_Spec;
9833 -- Start of processing for Install_Parent
9835 begin
9836 -- We need to install the parent instance to compile the instantiation
9837 -- of the child, but the child instance must appear in the current
9838 -- scope. Given that we cannot place the parent above the current scope
9839 -- in the scope stack, we duplicate the current scope and unstack both
9840 -- after the instantiation is complete.
9842 -- If the parent is itself the instantiation of a child unit, we must
9843 -- also stack the instantiation of its parent, and so on. Each such
9844 -- ancestor is the prefix of the name in a prior instantiation.
9846 -- If this is a nested instance, the parent unit itself resolves to
9847 -- a renaming of the parent instance, whose declaration we need.
9849 -- Finally, the parent may be a generic (not an instance) when the
9850 -- child unit appears as a formal package.
9852 Inst_Par := P;
9854 if Present (Renamed_Entity (Inst_Par)) then
9855 Inst_Par := Renamed_Entity (Inst_Par);
9856 end if;
9858 First_Par := Inst_Par;
9860 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9862 First_Gen := Gen_Par;
9864 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9866 -- Load grandparent instance as well
9868 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
9870 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9871 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9873 if Present (Renamed_Entity (Inst_Par)) then
9874 Inst_Par := Renamed_Entity (Inst_Par);
9875 end if;
9877 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9879 if Present (Gen_Par) then
9880 Prepend_Elmt (Inst_Par, Ancestors);
9882 else
9883 -- Parent is not the name of an instantiation
9885 Install_Noninstance_Specs (Inst_Par);
9886 exit;
9887 end if;
9889 else
9890 -- Previous error
9892 exit;
9893 end if;
9894 end loop;
9896 if Present (First_Gen) then
9897 Append_Elmt (First_Par, Ancestors);
9898 else
9899 Install_Noninstance_Specs (First_Par);
9900 end if;
9902 if not Is_Empty_Elmt_List (Ancestors) then
9903 Elmt := First_Elmt (Ancestors);
9904 while Present (Elmt) loop
9905 Install_Spec (Node (Elmt));
9906 Install_Formal_Packages (Node (Elmt));
9907 Next_Elmt (Elmt);
9908 end loop;
9909 end if;
9911 if not In_Body then
9912 Push_Scope (S);
9913 end if;
9914 end Install_Parent;
9916 -------------------------------
9917 -- Install_Hidden_Primitives --
9918 -------------------------------
9920 procedure Install_Hidden_Primitives
9921 (Prims_List : in out Elist_Id;
9922 Gen_T : Entity_Id;
9923 Act_T : Entity_Id)
9925 Elmt : Elmt_Id;
9926 List : Elist_Id := No_Elist;
9927 Prim_G_Elmt : Elmt_Id;
9928 Prim_A_Elmt : Elmt_Id;
9929 Prim_G : Node_Id;
9930 Prim_A : Node_Id;
9932 begin
9933 -- No action needed in case of serious errors because we cannot trust
9934 -- in the order of primitives
9936 if Serious_Errors_Detected > 0 then
9937 return;
9939 -- No action possible if we don't have available the list of primitive
9940 -- operations
9942 elsif No (Gen_T)
9943 or else not Is_Record_Type (Gen_T)
9944 or else not Is_Tagged_Type (Gen_T)
9945 or else not Is_Record_Type (Act_T)
9946 or else not Is_Tagged_Type (Act_T)
9947 then
9948 return;
9950 -- There is no need to handle interface types since their primitives
9951 -- cannot be hidden
9953 elsif Is_Interface (Gen_T) then
9954 return;
9955 end if;
9957 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9959 if not Is_Class_Wide_Type (Act_T) then
9960 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9961 else
9962 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9963 end if;
9965 loop
9966 -- Skip predefined primitives in the generic formal
9968 while Present (Prim_G_Elmt)
9969 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9970 loop
9971 Next_Elmt (Prim_G_Elmt);
9972 end loop;
9974 -- Skip predefined primitives in the generic actual
9976 while Present (Prim_A_Elmt)
9977 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9978 loop
9979 Next_Elmt (Prim_A_Elmt);
9980 end loop;
9982 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9984 Prim_G := Node (Prim_G_Elmt);
9985 Prim_A := Node (Prim_A_Elmt);
9987 -- There is no need to handle interface primitives because their
9988 -- primitives are not hidden
9990 exit when Present (Interface_Alias (Prim_G));
9992 -- Here we install one hidden primitive
9994 if Chars (Prim_G) /= Chars (Prim_A)
9995 and then Has_Suffix (Prim_A, 'P')
9996 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9997 then
9998 Set_Chars (Prim_A, Chars (Prim_G));
9999 Append_New_Elmt (Prim_A, To => List);
10000 end if;
10002 Next_Elmt (Prim_A_Elmt);
10003 Next_Elmt (Prim_G_Elmt);
10004 end loop;
10006 -- Append the elements to the list of temporarily visible primitives
10007 -- avoiding duplicates.
10009 if Present (List) then
10010 if No (Prims_List) then
10011 Prims_List := New_Elmt_List;
10012 end if;
10014 Elmt := First_Elmt (List);
10015 while Present (Elmt) loop
10016 Append_Unique_Elmt (Node (Elmt), Prims_List);
10017 Next_Elmt (Elmt);
10018 end loop;
10019 end if;
10020 end Install_Hidden_Primitives;
10022 -------------------------------
10023 -- Restore_Hidden_Primitives --
10024 -------------------------------
10026 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
10027 Prim_Elmt : Elmt_Id;
10028 Prim : Node_Id;
10030 begin
10031 if Prims_List /= No_Elist then
10032 Prim_Elmt := First_Elmt (Prims_List);
10033 while Present (Prim_Elmt) loop
10034 Prim := Node (Prim_Elmt);
10035 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
10036 Next_Elmt (Prim_Elmt);
10037 end loop;
10039 Prims_List := No_Elist;
10040 end if;
10041 end Restore_Hidden_Primitives;
10043 --------------------------------
10044 -- Instantiate_Formal_Package --
10045 --------------------------------
10047 function Instantiate_Formal_Package
10048 (Formal : Node_Id;
10049 Actual : Node_Id;
10050 Analyzed_Formal : Node_Id) return List_Id
10052 Loc : constant Source_Ptr := Sloc (Actual);
10053 Hidden_Formals : constant Elist_Id := New_Elmt_List;
10054 Actual_Pack : Entity_Id;
10055 Formal_Pack : Entity_Id;
10056 Gen_Parent : Entity_Id;
10057 Decls : List_Id;
10058 Nod : Node_Id;
10059 Parent_Spec : Node_Id;
10061 procedure Find_Matching_Actual
10062 (F : Node_Id;
10063 Act : in out Entity_Id);
10064 -- We need to associate each formal entity in the formal package with
10065 -- the corresponding entity in the actual package. The actual package
10066 -- has been analyzed and possibly expanded, and as a result there is
10067 -- no one-to-one correspondence between the two lists (for example,
10068 -- the actual may include subtypes, itypes, and inherited primitive
10069 -- operations, interspersed among the renaming declarations for the
10070 -- actuals). We retrieve the corresponding actual by name because each
10071 -- actual has the same name as the formal, and they do appear in the
10072 -- same order.
10074 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
10075 -- Retrieve entity of defining entity of generic formal parameter.
10076 -- Only the declarations of formals need to be considered when
10077 -- linking them to actuals, but the declarative list may include
10078 -- internal entities generated during analysis, and those are ignored.
10080 procedure Match_Formal_Entity
10081 (Formal_Node : Node_Id;
10082 Formal_Ent : Entity_Id;
10083 Actual_Ent : Entity_Id);
10084 -- Associates the formal entity with the actual. In the case where
10085 -- Formal_Ent is a formal package, this procedure iterates through all
10086 -- of its formals and enters associations between the actuals occurring
10087 -- in the formal package's corresponding actual package (given by
10088 -- Actual_Ent) and the formal package's formal parameters. This
10089 -- procedure recurses if any of the parameters is itself a package.
10091 function Is_Instance_Of
10092 (Act_Spec : Entity_Id;
10093 Gen_Anc : Entity_Id) return Boolean;
10094 -- The actual can be an instantiation of a generic within another
10095 -- instance, in which case there is no direct link from it to the
10096 -- original generic ancestor. In that case, we recognize that the
10097 -- ultimate ancestor is the same by examining names and scopes.
10099 procedure Process_Nested_Formal (Formal : Entity_Id);
10100 -- If the current formal is declared with a box, its own formals are
10101 -- visible in the instance, as they were in the generic, and their
10102 -- Hidden flag must be reset. If some of these formals are themselves
10103 -- packages declared with a box, the processing must be recursive.
10105 --------------------------
10106 -- Find_Matching_Actual --
10107 --------------------------
10109 procedure Find_Matching_Actual
10110 (F : Node_Id;
10111 Act : in out Entity_Id)
10113 Formal_Ent : Entity_Id;
10115 begin
10116 case Nkind (Original_Node (F)) is
10117 when N_Formal_Object_Declaration
10118 | N_Formal_Type_Declaration
10120 Formal_Ent := Defining_Identifier (F);
10122 while Chars (Act) /= Chars (Formal_Ent) loop
10123 Next_Entity (Act);
10124 end loop;
10126 when N_Formal_Package_Declaration
10127 | N_Formal_Subprogram_Declaration
10128 | N_Generic_Package_Declaration
10129 | N_Package_Declaration
10131 Formal_Ent := Defining_Entity (F);
10133 while Chars (Act) /= Chars (Formal_Ent) loop
10134 Next_Entity (Act);
10135 end loop;
10137 when others =>
10138 raise Program_Error;
10139 end case;
10140 end Find_Matching_Actual;
10142 -------------------------
10143 -- Match_Formal_Entity --
10144 -------------------------
10146 procedure Match_Formal_Entity
10147 (Formal_Node : Node_Id;
10148 Formal_Ent : Entity_Id;
10149 Actual_Ent : Entity_Id)
10151 Act_Pkg : Entity_Id;
10153 begin
10154 Set_Instance_Of (Formal_Ent, Actual_Ent);
10156 if Ekind (Actual_Ent) = E_Package then
10158 -- Record associations for each parameter
10160 Act_Pkg := Actual_Ent;
10162 declare
10163 A_Ent : Entity_Id := First_Entity (Act_Pkg);
10164 F_Ent : Entity_Id;
10165 F_Node : Node_Id;
10167 Gen_Decl : Node_Id;
10168 Formals : List_Id;
10169 Actual : Entity_Id;
10171 begin
10172 -- Retrieve the actual given in the formal package declaration
10174 Actual := Entity (Name (Original_Node (Formal_Node)));
10176 -- The actual in the formal package declaration may be a
10177 -- renamed generic package, in which case we want to retrieve
10178 -- the original generic in order to traverse its formal part.
10180 if Present (Renamed_Entity (Actual)) then
10181 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
10182 else
10183 Gen_Decl := Unit_Declaration_Node (Actual);
10184 end if;
10186 Formals := Generic_Formal_Declarations (Gen_Decl);
10188 if Present (Formals) then
10189 F_Node := First_Non_Pragma (Formals);
10190 else
10191 F_Node := Empty;
10192 end if;
10194 while Present (A_Ent)
10195 and then Present (F_Node)
10196 and then A_Ent /= First_Private_Entity (Act_Pkg)
10197 loop
10198 F_Ent := Get_Formal_Entity (F_Node);
10200 if Present (F_Ent) then
10202 -- This is a formal of the original package. Record
10203 -- association and recurse.
10205 Find_Matching_Actual (F_Node, A_Ent);
10206 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
10207 Next_Entity (A_Ent);
10208 end if;
10210 Next_Non_Pragma (F_Node);
10211 end loop;
10212 end;
10213 end if;
10214 end Match_Formal_Entity;
10216 -----------------------
10217 -- Get_Formal_Entity --
10218 -----------------------
10220 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
10221 Kind : constant Node_Kind := Nkind (Original_Node (N));
10222 begin
10223 case Kind is
10224 when N_Formal_Object_Declaration =>
10225 return Defining_Identifier (N);
10227 when N_Formal_Type_Declaration =>
10228 return Defining_Identifier (N);
10230 when N_Formal_Subprogram_Declaration =>
10231 return Defining_Unit_Name (Specification (N));
10233 when N_Formal_Package_Declaration =>
10234 return Defining_Identifier (Original_Node (N));
10236 when N_Generic_Package_Declaration =>
10237 return Defining_Identifier (Original_Node (N));
10239 -- All other declarations are introduced by semantic analysis and
10240 -- have no match in the actual.
10242 when others =>
10243 return Empty;
10244 end case;
10245 end Get_Formal_Entity;
10247 --------------------
10248 -- Is_Instance_Of --
10249 --------------------
10251 function Is_Instance_Of
10252 (Act_Spec : Entity_Id;
10253 Gen_Anc : Entity_Id) return Boolean
10255 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
10257 begin
10258 if No (Gen_Par) then
10259 return False;
10261 -- Simplest case: the generic parent of the actual is the formal
10263 elsif Gen_Par = Gen_Anc then
10264 return True;
10266 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
10267 return False;
10269 -- The actual may be obtained through several instantiations. Its
10270 -- scope must itself be an instance of a generic declared in the
10271 -- same scope as the formal. Any other case is detected above.
10273 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
10274 return False;
10276 else
10277 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
10278 end if;
10279 end Is_Instance_Of;
10281 ---------------------------
10282 -- Process_Nested_Formal --
10283 ---------------------------
10285 procedure Process_Nested_Formal (Formal : Entity_Id) is
10286 Ent : Entity_Id;
10288 begin
10289 if Present (Associated_Formal_Package (Formal))
10290 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
10291 then
10292 Ent := First_Entity (Formal);
10293 while Present (Ent) loop
10294 Set_Is_Hidden (Ent, False);
10295 Set_Is_Visible_Formal (Ent);
10296 Set_Is_Potentially_Use_Visible
10297 (Ent, Is_Potentially_Use_Visible (Formal));
10299 if Ekind (Ent) = E_Package then
10300 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
10301 Process_Nested_Formal (Ent);
10302 end if;
10304 Next_Entity (Ent);
10305 end loop;
10306 end if;
10307 end Process_Nested_Formal;
10309 -- Start of processing for Instantiate_Formal_Package
10311 begin
10312 Analyze (Actual);
10314 -- The actual must be a package instance, or else a current instance
10315 -- such as a parent generic within the body of a generic child.
10317 if not Is_Entity_Name (Actual)
10318 or else not Ekind_In (Entity (Actual), E_Generic_Package, E_Package)
10319 then
10320 Error_Msg_N
10321 ("expect package instance to instantiate formal", Actual);
10322 Abandon_Instantiation (Actual);
10323 raise Program_Error;
10325 else
10326 Actual_Pack := Entity (Actual);
10327 Set_Is_Instantiated (Actual_Pack);
10329 -- The actual may be a renamed package, or an outer generic formal
10330 -- package whose instantiation is converted into a renaming.
10332 if Present (Renamed_Object (Actual_Pack)) then
10333 Actual_Pack := Renamed_Object (Actual_Pack);
10334 end if;
10336 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
10337 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
10338 Formal_Pack := Defining_Identifier (Analyzed_Formal);
10339 else
10340 Gen_Parent :=
10341 Generic_Parent (Specification (Analyzed_Formal));
10342 Formal_Pack :=
10343 Defining_Unit_Name (Specification (Analyzed_Formal));
10344 end if;
10346 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
10347 Parent_Spec := Package_Specification (Actual_Pack);
10348 else
10349 Parent_Spec := Parent (Actual_Pack);
10350 end if;
10352 if Gen_Parent = Any_Id then
10353 Error_Msg_N
10354 ("previous error in declaration of formal package", Actual);
10355 Abandon_Instantiation (Actual);
10357 elsif Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent)) then
10358 null;
10360 -- If this is the current instance of an enclosing generic, that unit
10361 -- is the generic package we need.
10363 elsif In_Open_Scopes (Actual_Pack)
10364 and then Ekind (Actual_Pack) = E_Generic_Package
10365 then
10366 null;
10368 else
10369 Error_Msg_NE
10370 ("actual parameter must be instance of&", Actual, Gen_Parent);
10371 Abandon_Instantiation (Actual);
10372 end if;
10374 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
10375 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
10377 Nod :=
10378 Make_Package_Renaming_Declaration (Loc,
10379 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
10380 Name => New_Occurrence_Of (Actual_Pack, Loc));
10382 Set_Associated_Formal_Package
10383 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10384 Decls := New_List (Nod);
10386 -- If the formal F has a box, then the generic declarations are
10387 -- visible in the generic G. In an instance of G, the corresponding
10388 -- entities in the actual for F (which are the actuals for the
10389 -- instantiation of the generic that F denotes) must also be made
10390 -- visible for analysis of the current instance. On exit from the
10391 -- current instance, those entities are made private again. If the
10392 -- actual is currently in use, these entities are also use-visible.
10394 -- The loop through the actual entities also steps through the formal
10395 -- entities and enters associations from formals to actuals into the
10396 -- renaming map. This is necessary to properly handle checking of
10397 -- actual parameter associations for later formals that depend on
10398 -- actuals declared in the formal package.
10400 -- In Ada 2005, partial parameterization requires that we make
10401 -- visible the actuals corresponding to formals that were defaulted
10402 -- in the formal package. There formals are identified because they
10403 -- remain formal generics within the formal package, rather than
10404 -- being renamings of the actuals supplied.
10406 declare
10407 Gen_Decl : constant Node_Id :=
10408 Unit_Declaration_Node (Gen_Parent);
10409 Formals : constant List_Id :=
10410 Generic_Formal_Declarations (Gen_Decl);
10412 Actual_Ent : Entity_Id;
10413 Actual_Of_Formal : Node_Id;
10414 Formal_Node : Node_Id;
10415 Formal_Ent : Entity_Id;
10417 begin
10418 if Present (Formals) then
10419 Formal_Node := First_Non_Pragma (Formals);
10420 else
10421 Formal_Node := Empty;
10422 end if;
10424 Actual_Ent := First_Entity (Actual_Pack);
10425 Actual_Of_Formal :=
10426 First (Visible_Declarations (Specification (Analyzed_Formal)));
10427 while Present (Actual_Ent)
10428 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10429 loop
10430 if Present (Formal_Node) then
10431 Formal_Ent := Get_Formal_Entity (Formal_Node);
10433 if Present (Formal_Ent) then
10434 Find_Matching_Actual (Formal_Node, Actual_Ent);
10435 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10437 -- We iterate at the same time over the actuals of the
10438 -- local package created for the formal, to determine
10439 -- which one of the formals of the original generic were
10440 -- defaulted in the formal. The corresponding actual
10441 -- entities are visible in the enclosing instance.
10443 if Box_Present (Formal)
10444 or else
10445 (Present (Actual_Of_Formal)
10446 and then
10447 Is_Generic_Formal
10448 (Get_Formal_Entity (Actual_Of_Formal)))
10449 then
10450 Set_Is_Hidden (Actual_Ent, False);
10451 Set_Is_Visible_Formal (Actual_Ent);
10452 Set_Is_Potentially_Use_Visible
10453 (Actual_Ent, In_Use (Actual_Pack));
10455 if Ekind (Actual_Ent) = E_Package then
10456 Process_Nested_Formal (Actual_Ent);
10457 end if;
10459 else
10460 if not Is_Hidden (Actual_Ent) then
10461 Append_Elmt (Actual_Ent, Hidden_Formals);
10462 end if;
10464 Set_Is_Hidden (Actual_Ent);
10465 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10466 end if;
10467 end if;
10469 Next_Non_Pragma (Formal_Node);
10470 Next (Actual_Of_Formal);
10472 else
10473 -- No further formals to match, but the generic part may
10474 -- contain inherited operation that are not hidden in the
10475 -- enclosing instance.
10477 Next_Entity (Actual_Ent);
10478 end if;
10479 end loop;
10481 -- Inherited subprograms generated by formal derived types are
10482 -- also visible if the types are.
10484 Actual_Ent := First_Entity (Actual_Pack);
10485 while Present (Actual_Ent)
10486 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10487 loop
10488 if Is_Overloadable (Actual_Ent)
10489 and then
10490 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10491 and then
10492 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10493 then
10494 Set_Is_Hidden (Actual_Ent, False);
10495 Set_Is_Potentially_Use_Visible
10496 (Actual_Ent, In_Use (Actual_Pack));
10497 end if;
10499 Next_Entity (Actual_Ent);
10500 end loop;
10502 -- No conformance to check if the generic has no formal parameters
10503 -- and the formal package has no generic associations.
10505 if Is_Empty_List (Formals)
10506 and then
10507 (Box_Present (Formal)
10508 or else No (Generic_Associations (Formal)))
10509 then
10510 return Decls;
10511 end if;
10512 end;
10514 -- If the formal is not declared with a box, reanalyze it as an
10515 -- abbreviated instantiation, to verify the matching rules of 12.7.
10516 -- The actual checks are performed after the generic associations
10517 -- have been analyzed, to guarantee the same visibility for this
10518 -- instantiation and for the actuals.
10520 -- In Ada 2005, the generic associations for the formal can include
10521 -- defaulted parameters. These are ignored during check. This
10522 -- internal instantiation is removed from the tree after conformance
10523 -- checking, because it contains formal declarations for those
10524 -- defaulted parameters, and those should not reach the back-end.
10526 if not Box_Present (Formal) then
10527 declare
10528 I_Pack : constant Entity_Id :=
10529 Make_Temporary (Sloc (Actual), 'P');
10531 begin
10532 Set_Is_Internal (I_Pack);
10533 Set_Ekind (I_Pack, E_Package);
10534 Set_Hidden_In_Formal_Instance (I_Pack, Hidden_Formals);
10536 Append_To (Decls,
10537 Make_Package_Instantiation (Sloc (Actual),
10538 Defining_Unit_Name => I_Pack,
10539 Name =>
10540 New_Occurrence_Of
10541 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10542 Generic_Associations => Generic_Associations (Formal)));
10543 end;
10544 end if;
10546 return Decls;
10547 end if;
10548 end Instantiate_Formal_Package;
10550 -----------------------------------
10551 -- Instantiate_Formal_Subprogram --
10552 -----------------------------------
10554 function Instantiate_Formal_Subprogram
10555 (Formal : Node_Id;
10556 Actual : Node_Id;
10557 Analyzed_Formal : Node_Id) return Node_Id
10559 Analyzed_S : constant Entity_Id :=
10560 Defining_Unit_Name (Specification (Analyzed_Formal));
10561 Formal_Sub : constant Entity_Id :=
10562 Defining_Unit_Name (Specification (Formal));
10564 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10565 -- If the generic is a child unit, the parent has been installed on the
10566 -- scope stack, but a default subprogram cannot resolve to something
10567 -- on the parent because that parent is not really part of the visible
10568 -- context (it is there to resolve explicit local entities). If the
10569 -- default has resolved in this way, we remove the entity from immediate
10570 -- visibility and analyze the node again to emit an error message or
10571 -- find another visible candidate.
10573 procedure Valid_Actual_Subprogram (Act : Node_Id);
10574 -- Perform legality check and raise exception on failure
10576 -----------------------
10577 -- From_Parent_Scope --
10578 -----------------------
10580 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10581 Gen_Scope : Node_Id;
10583 begin
10584 Gen_Scope := Scope (Analyzed_S);
10585 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10586 if Scope (Subp) = Scope (Gen_Scope) then
10587 return True;
10588 end if;
10590 Gen_Scope := Scope (Gen_Scope);
10591 end loop;
10593 return False;
10594 end From_Parent_Scope;
10596 -----------------------------
10597 -- Valid_Actual_Subprogram --
10598 -----------------------------
10600 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10601 Act_E : Entity_Id;
10603 begin
10604 if Is_Entity_Name (Act) then
10605 Act_E := Entity (Act);
10607 elsif Nkind (Act) = N_Selected_Component
10608 and then Is_Entity_Name (Selector_Name (Act))
10609 then
10610 Act_E := Entity (Selector_Name (Act));
10612 else
10613 Act_E := Empty;
10614 end if;
10616 if (Present (Act_E) and then Is_Overloadable (Act_E))
10617 or else Nkind_In (Act, N_Attribute_Reference,
10618 N_Indexed_Component,
10619 N_Character_Literal,
10620 N_Explicit_Dereference)
10621 then
10622 return;
10623 end if;
10625 Error_Msg_NE
10626 ("expect subprogram or entry name in instantiation of &",
10627 Instantiation_Node, Formal_Sub);
10628 Abandon_Instantiation (Instantiation_Node);
10629 end Valid_Actual_Subprogram;
10631 -- Local variables
10633 Decl_Node : Node_Id;
10634 Loc : Source_Ptr;
10635 Nam : Node_Id;
10636 New_Spec : Node_Id;
10637 New_Subp : Entity_Id;
10639 -- Start of processing for Instantiate_Formal_Subprogram
10641 begin
10642 New_Spec := New_Copy_Tree (Specification (Formal));
10644 -- The tree copy has created the proper instantiation sloc for the
10645 -- new specification. Use this location for all other constructed
10646 -- declarations.
10648 Loc := Sloc (Defining_Unit_Name (New_Spec));
10650 -- Create new entity for the actual (New_Copy_Tree does not), and
10651 -- indicate that it is an actual.
10653 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10654 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10655 Set_Is_Generic_Actual_Subprogram (New_Subp);
10656 Set_Defining_Unit_Name (New_Spec, New_Subp);
10658 -- Create new entities for the each of the formals in the specification
10659 -- of the renaming declaration built for the actual.
10661 if Present (Parameter_Specifications (New_Spec)) then
10662 declare
10663 F : Node_Id;
10664 F_Id : Entity_Id;
10666 begin
10667 F := First (Parameter_Specifications (New_Spec));
10668 while Present (F) loop
10669 F_Id := Defining_Identifier (F);
10671 Set_Defining_Identifier (F,
10672 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10673 Next (F);
10674 end loop;
10675 end;
10676 end if;
10678 -- Find entity of actual. If the actual is an attribute reference, it
10679 -- cannot be resolved here (its formal is missing) but is handled
10680 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10681 -- fully resolved subsequently, when the renaming declaration for the
10682 -- formal is analyzed. If it is an explicit dereference, resolve the
10683 -- prefix but not the actual itself, to prevent interpretation as call.
10685 if Present (Actual) then
10686 Loc := Sloc (Actual);
10687 Set_Sloc (New_Spec, Loc);
10689 if Nkind (Actual) = N_Operator_Symbol then
10690 Find_Direct_Name (Actual);
10692 elsif Nkind (Actual) = N_Explicit_Dereference then
10693 Analyze (Prefix (Actual));
10695 elsif Nkind (Actual) /= N_Attribute_Reference then
10696 Analyze (Actual);
10697 end if;
10699 Valid_Actual_Subprogram (Actual);
10700 Nam := Actual;
10702 elsif Present (Default_Name (Formal)) then
10703 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
10704 N_Selected_Component,
10705 N_Indexed_Component,
10706 N_Character_Literal)
10707 and then Present (Entity (Default_Name (Formal)))
10708 then
10709 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10710 else
10711 Nam := New_Copy (Default_Name (Formal));
10712 Set_Sloc (Nam, Loc);
10713 end if;
10715 elsif Box_Present (Formal) then
10717 -- Actual is resolved at the point of instantiation. Create an
10718 -- identifier or operator with the same name as the formal.
10720 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10721 Nam :=
10722 Make_Operator_Symbol (Loc,
10723 Chars => Chars (Formal_Sub),
10724 Strval => No_String);
10725 else
10726 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10727 end if;
10729 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10730 and then Null_Present (Specification (Formal))
10731 then
10732 -- Generate null body for procedure, for use in the instance
10734 Decl_Node :=
10735 Make_Subprogram_Body (Loc,
10736 Specification => New_Spec,
10737 Declarations => New_List,
10738 Handled_Statement_Sequence =>
10739 Make_Handled_Sequence_Of_Statements (Loc,
10740 Statements => New_List (Make_Null_Statement (Loc))));
10742 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10743 return Decl_Node;
10745 else
10746 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10747 Error_Msg_NE
10748 ("missing actual&", Instantiation_Node, Formal_Sub);
10749 Error_Msg_NE
10750 ("\in instantiation of & declared#",
10751 Instantiation_Node, Scope (Analyzed_S));
10752 Abandon_Instantiation (Instantiation_Node);
10753 end if;
10755 Decl_Node :=
10756 Make_Subprogram_Renaming_Declaration (Loc,
10757 Specification => New_Spec,
10758 Name => Nam);
10760 -- If we do not have an actual and the formal specified <> then set to
10761 -- get proper default.
10763 if No (Actual) and then Box_Present (Formal) then
10764 Set_From_Default (Decl_Node);
10765 end if;
10767 -- Gather possible interpretations for the actual before analyzing the
10768 -- instance. If overloaded, it will be resolved when analyzing the
10769 -- renaming declaration.
10771 if Box_Present (Formal) and then No (Actual) then
10772 Analyze (Nam);
10774 if Is_Child_Unit (Scope (Analyzed_S))
10775 and then Present (Entity (Nam))
10776 then
10777 if not Is_Overloaded (Nam) then
10778 if From_Parent_Scope (Entity (Nam)) then
10779 Set_Is_Immediately_Visible (Entity (Nam), False);
10780 Set_Entity (Nam, Empty);
10781 Set_Etype (Nam, Empty);
10783 Analyze (Nam);
10784 Set_Is_Immediately_Visible (Entity (Nam));
10785 end if;
10787 else
10788 declare
10789 I : Interp_Index;
10790 It : Interp;
10792 begin
10793 Get_First_Interp (Nam, I, It);
10794 while Present (It.Nam) loop
10795 if From_Parent_Scope (It.Nam) then
10796 Remove_Interp (I);
10797 end if;
10799 Get_Next_Interp (I, It);
10800 end loop;
10801 end;
10802 end if;
10803 end if;
10804 end if;
10806 -- The generic instantiation freezes the actual. This can only be done
10807 -- once the actual is resolved, in the analysis of the renaming
10808 -- declaration. To make the formal subprogram entity available, we set
10809 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10810 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10811 -- of formal abstract subprograms.
10813 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10815 -- We cannot analyze the renaming declaration, and thus find the actual,
10816 -- until all the actuals are assembled in the instance. For subsequent
10817 -- checks of other actuals, indicate the node that will hold the
10818 -- instance of this formal.
10820 Set_Instance_Of (Analyzed_S, Nam);
10822 if Nkind (Actual) = N_Selected_Component
10823 and then Is_Task_Type (Etype (Prefix (Actual)))
10824 and then not Is_Frozen (Etype (Prefix (Actual)))
10825 then
10826 -- The renaming declaration will create a body, which must appear
10827 -- outside of the instantiation, We move the renaming declaration
10828 -- out of the instance, and create an additional renaming inside,
10829 -- to prevent freezing anomalies.
10831 declare
10832 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10834 begin
10835 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10836 Insert_Before (Instantiation_Node, Decl_Node);
10837 Analyze (Decl_Node);
10839 -- Now create renaming within the instance
10841 Decl_Node :=
10842 Make_Subprogram_Renaming_Declaration (Loc,
10843 Specification => New_Copy_Tree (New_Spec),
10844 Name => New_Occurrence_Of (Anon_Id, Loc));
10846 Set_Defining_Unit_Name (Specification (Decl_Node),
10847 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10848 end;
10849 end if;
10851 return Decl_Node;
10852 end Instantiate_Formal_Subprogram;
10854 ------------------------
10855 -- Instantiate_Object --
10856 ------------------------
10858 function Instantiate_Object
10859 (Formal : Node_Id;
10860 Actual : Node_Id;
10861 Analyzed_Formal : Node_Id) return List_Id
10863 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10864 A_Gen_Obj : constant Entity_Id :=
10865 Defining_Identifier (Analyzed_Formal);
10866 Acc_Def : Node_Id := Empty;
10867 Act_Assoc : constant Node_Id := Parent (Actual);
10868 Actual_Decl : Node_Id := Empty;
10869 Decl_Node : Node_Id;
10870 Def : Node_Id;
10871 Ftyp : Entity_Id;
10872 List : constant List_Id := New_List;
10873 Loc : constant Source_Ptr := Sloc (Actual);
10874 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10875 Subt_Decl : Node_Id := Empty;
10876 Subt_Mark : Node_Id := Empty;
10878 function Copy_Access_Def return Node_Id;
10879 -- If formal is an anonymous access, copy access definition of formal
10880 -- for generated object declaration.
10882 ---------------------
10883 -- Copy_Access_Def --
10884 ---------------------
10886 function Copy_Access_Def return Node_Id is
10887 begin
10888 Def := New_Copy_Tree (Acc_Def);
10890 -- In addition, if formal is an access to subprogram we need to
10891 -- generate new formals for the signature of the default, so that
10892 -- the tree is properly formatted for ASIS use.
10894 if Present (Access_To_Subprogram_Definition (Acc_Def)) then
10895 declare
10896 Par_Spec : Node_Id;
10897 begin
10898 Par_Spec :=
10899 First (Parameter_Specifications
10900 (Access_To_Subprogram_Definition (Def)));
10901 while Present (Par_Spec) loop
10902 Set_Defining_Identifier (Par_Spec,
10903 Make_Defining_Identifier (Sloc (Acc_Def),
10904 Chars => Chars (Defining_Identifier (Par_Spec))));
10905 Next (Par_Spec);
10906 end loop;
10907 end;
10908 end if;
10910 return Def;
10911 end Copy_Access_Def;
10913 -- Start of processing for Instantiate_Object
10915 begin
10916 -- Formal may be an anonymous access
10918 if Present (Subtype_Mark (Formal)) then
10919 Subt_Mark := Subtype_Mark (Formal);
10920 else
10921 Check_Access_Definition (Formal);
10922 Acc_Def := Access_Definition (Formal);
10923 end if;
10925 -- Sloc for error message on missing actual
10927 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10929 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10930 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10931 end if;
10933 Set_Parent (List, Parent (Actual));
10935 -- OUT present
10937 if Out_Present (Formal) then
10939 -- An IN OUT generic actual must be a name. The instantiation is a
10940 -- renaming declaration. The actual is the name being renamed. We
10941 -- use the actual directly, rather than a copy, because it is not
10942 -- used further in the list of actuals, and because a copy or a use
10943 -- of relocate_node is incorrect if the instance is nested within a
10944 -- generic. In order to simplify ASIS searches, the Generic_Parent
10945 -- field links the declaration to the generic association.
10947 if No (Actual) then
10948 Error_Msg_NE
10949 ("missing actual &",
10950 Instantiation_Node, Gen_Obj);
10951 Error_Msg_NE
10952 ("\in instantiation of & declared#",
10953 Instantiation_Node, Scope (A_Gen_Obj));
10954 Abandon_Instantiation (Instantiation_Node);
10955 end if;
10957 if Present (Subt_Mark) then
10958 Decl_Node :=
10959 Make_Object_Renaming_Declaration (Loc,
10960 Defining_Identifier => New_Copy (Gen_Obj),
10961 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10962 Name => Actual);
10964 else pragma Assert (Present (Acc_Def));
10965 Decl_Node :=
10966 Make_Object_Renaming_Declaration (Loc,
10967 Defining_Identifier => New_Copy (Gen_Obj),
10968 Access_Definition => New_Copy_Tree (Acc_Def),
10969 Name => Actual);
10970 end if;
10972 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10974 -- The analysis of the actual may produce Insert_Action nodes, so
10975 -- the declaration must have a context in which to attach them.
10977 Append (Decl_Node, List);
10978 Analyze (Actual);
10980 -- Return if the analysis of the actual reported some error
10982 if Etype (Actual) = Any_Type then
10983 return List;
10984 end if;
10986 -- This check is performed here because Analyze_Object_Renaming will
10987 -- not check it when Comes_From_Source is False. Note though that the
10988 -- check for the actual being the name of an object will be performed
10989 -- in Analyze_Object_Renaming.
10991 if Is_Object_Reference (Actual)
10992 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10993 then
10994 Error_Msg_N
10995 ("illegal discriminant-dependent component for in out parameter",
10996 Actual);
10997 end if;
10999 -- The actual has to be resolved in order to check that it is a
11000 -- variable (due to cases such as F (1), where F returns access to
11001 -- an array, and for overloaded prefixes).
11003 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
11005 -- If the type of the formal is not itself a formal, and the current
11006 -- unit is a child unit, the formal type must be declared in a
11007 -- parent, and must be retrieved by visibility.
11009 if Ftyp = Orig_Ftyp
11010 and then Is_Generic_Unit (Scope (Ftyp))
11011 and then Is_Child_Unit (Scope (A_Gen_Obj))
11012 then
11013 declare
11014 Temp : constant Node_Id :=
11015 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
11016 begin
11017 Set_Entity (Temp, Empty);
11018 Find_Type (Temp);
11019 Ftyp := Entity (Temp);
11020 end;
11021 end if;
11023 if Is_Private_Type (Ftyp)
11024 and then not Is_Private_Type (Etype (Actual))
11025 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
11026 or else Base_Type (Etype (Actual)) = Ftyp)
11027 then
11028 -- If the actual has the type of the full view of the formal, or
11029 -- else a non-private subtype of the formal, then the visibility
11030 -- of the formal type has changed. Add to the actuals a subtype
11031 -- declaration that will force the exchange of views in the body
11032 -- of the instance as well.
11034 Subt_Decl :=
11035 Make_Subtype_Declaration (Loc,
11036 Defining_Identifier => Make_Temporary (Loc, 'P'),
11037 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
11039 Prepend (Subt_Decl, List);
11041 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
11042 Exchange_Declarations (Ftyp);
11043 end if;
11045 Resolve (Actual, Ftyp);
11047 if not Denotes_Variable (Actual) then
11048 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
11050 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
11052 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
11053 -- the type of the actual shall resolve to a specific anonymous
11054 -- access type.
11056 if Ada_Version < Ada_2005
11057 or else Ekind (Base_Type (Ftyp)) /=
11058 E_Anonymous_Access_Type
11059 or else Ekind (Base_Type (Etype (Actual))) /=
11060 E_Anonymous_Access_Type
11061 then
11062 Error_Msg_NE
11063 ("type of actual does not match type of&", Actual, Gen_Obj);
11064 end if;
11065 end if;
11067 Note_Possible_Modification (Actual, Sure => True);
11069 -- Check for instantiation of atomic/volatile actual for
11070 -- non-atomic/volatile formal (RM C.6 (12)).
11072 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
11073 Error_Msg_N
11074 ("cannot instantiate non-atomic formal object "
11075 & "with atomic actual", Actual);
11077 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
11078 then
11079 Error_Msg_N
11080 ("cannot instantiate non-volatile formal object "
11081 & "with volatile actual", Actual);
11082 end if;
11084 -- Formal in-parameter
11086 else
11087 -- The instantiation of a generic formal in-parameter is constant
11088 -- declaration. The actual is the expression for that declaration.
11089 -- Its type is a full copy of the type of the formal. This may be
11090 -- an access to subprogram, for which we need to generate entities
11091 -- for the formals in the new signature.
11093 if Present (Actual) then
11094 if Present (Subt_Mark) then
11095 Def := New_Copy_Tree (Subt_Mark);
11096 else pragma Assert (Present (Acc_Def));
11097 Def := Copy_Access_Def;
11098 end if;
11100 Decl_Node :=
11101 Make_Object_Declaration (Loc,
11102 Defining_Identifier => New_Copy (Gen_Obj),
11103 Constant_Present => True,
11104 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11105 Object_Definition => Def,
11106 Expression => Actual);
11108 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11110 -- A generic formal object of a tagged type is defined to be
11111 -- aliased so the new constant must also be treated as aliased.
11113 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
11114 Set_Aliased_Present (Decl_Node);
11115 end if;
11117 Append (Decl_Node, List);
11119 -- No need to repeat (pre-)analysis of some expression nodes
11120 -- already handled in Preanalyze_Actuals.
11122 if Nkind (Actual) /= N_Allocator then
11123 Analyze (Actual);
11125 -- Return if the analysis of the actual reported some error
11127 if Etype (Actual) = Any_Type then
11128 return List;
11129 end if;
11130 end if;
11132 declare
11133 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
11134 Typ : Entity_Id;
11136 begin
11137 Typ := Get_Instance_Of (Formal_Type);
11139 -- If the actual appears in the current or an enclosing scope,
11140 -- use its type directly. This is relevant if it has an actual
11141 -- subtype that is distinct from its nominal one. This cannot
11142 -- be done in general because the type of the actual may
11143 -- depend on other actuals, and only be fully determined when
11144 -- the enclosing instance is analyzed.
11146 if Present (Etype (Actual))
11147 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
11148 then
11149 Freeze_Before (Instantiation_Node, Etype (Actual));
11150 else
11151 Freeze_Before (Instantiation_Node, Typ);
11152 end if;
11154 -- If the actual is an aggregate, perform name resolution on
11155 -- its components (the analysis of an aggregate does not do it)
11156 -- to capture local names that may be hidden if the generic is
11157 -- a child unit.
11159 if Nkind (Actual) = N_Aggregate then
11160 Preanalyze_And_Resolve (Actual, Typ);
11161 end if;
11163 if Is_Limited_Type (Typ)
11164 and then not OK_For_Limited_Init (Typ, Actual)
11165 then
11166 Error_Msg_N
11167 ("initialization not allowed for limited types", Actual);
11168 Explain_Limited_Type (Typ, Actual);
11169 end if;
11170 end;
11172 elsif Present (Default_Expression (Formal)) then
11174 -- Use default to construct declaration
11176 if Present (Subt_Mark) then
11177 Def := New_Copy (Subt_Mark);
11178 else pragma Assert (Present (Acc_Def));
11179 Def := Copy_Access_Def;
11180 end if;
11182 Decl_Node :=
11183 Make_Object_Declaration (Sloc (Formal),
11184 Defining_Identifier => New_Copy (Gen_Obj),
11185 Constant_Present => True,
11186 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11187 Object_Definition => Def,
11188 Expression => New_Copy_Tree
11189 (Default_Expression (Formal)));
11191 Set_Corresponding_Generic_Association
11192 (Decl_Node, Expression (Decl_Node));
11194 Append (Decl_Node, List);
11195 Set_Analyzed (Expression (Decl_Node), False);
11197 else
11198 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
11199 Error_Msg_NE ("\in instantiation of & declared#",
11200 Instantiation_Node, Scope (A_Gen_Obj));
11202 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
11204 -- Create dummy constant declaration so that instance can be
11205 -- analyzed, to minimize cascaded visibility errors.
11207 if Present (Subt_Mark) then
11208 Def := Subt_Mark;
11209 else pragma Assert (Present (Acc_Def));
11210 Def := Acc_Def;
11211 end if;
11213 Decl_Node :=
11214 Make_Object_Declaration (Loc,
11215 Defining_Identifier => New_Copy (Gen_Obj),
11216 Constant_Present => True,
11217 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11218 Object_Definition => New_Copy (Def),
11219 Expression =>
11220 Make_Attribute_Reference (Sloc (Gen_Obj),
11221 Attribute_Name => Name_First,
11222 Prefix => New_Copy (Def)));
11224 Append (Decl_Node, List);
11226 else
11227 Abandon_Instantiation (Instantiation_Node);
11228 end if;
11229 end if;
11230 end if;
11232 if Nkind (Actual) in N_Has_Entity then
11233 Actual_Decl := Parent (Entity (Actual));
11234 end if;
11236 -- Ada 2005 (AI-423): For a formal object declaration with a null
11237 -- exclusion or an access definition that has a null exclusion: If the
11238 -- actual matching the formal object declaration denotes a generic
11239 -- formal object of another generic unit G, and the instantiation
11240 -- containing the actual occurs within the body of G or within the body
11241 -- of a generic unit declared within the declarative region of G, then
11242 -- the declaration of the formal object of G must have a null exclusion.
11243 -- Otherwise, the subtype of the actual matching the formal object
11244 -- declaration shall exclude null.
11246 if Ada_Version >= Ada_2005
11247 and then Present (Actual_Decl)
11248 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
11249 N_Object_Declaration)
11250 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
11251 and then not Has_Null_Exclusion (Actual_Decl)
11252 and then Has_Null_Exclusion (Analyzed_Formal)
11253 then
11254 Error_Msg_Sloc := Sloc (Analyzed_Formal);
11255 Error_Msg_N
11256 ("actual must exclude null to match generic formal#", Actual);
11257 end if;
11259 -- An effectively volatile object cannot be used as an actual in a
11260 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
11261 -- relevant only when SPARK_Mode is on as it is not a standard Ada
11262 -- legality rule, and also verifies that the actual is an object.
11264 if SPARK_Mode = On
11265 and then Present (Actual)
11266 and then Is_Object_Reference (Actual)
11267 and then Is_Effectively_Volatile_Object (Actual)
11268 then
11269 Error_Msg_N
11270 ("volatile object cannot act as actual in generic instantiation",
11271 Actual);
11272 end if;
11274 return List;
11275 end Instantiate_Object;
11277 ------------------------------
11278 -- Instantiate_Package_Body --
11279 ------------------------------
11281 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11282 -- must be replaced by gotos which jump to the end of the routine in order
11283 -- to restore the Ghost and SPARK modes.
11285 procedure Instantiate_Package_Body
11286 (Body_Info : Pending_Body_Info;
11287 Inlined_Body : Boolean := False;
11288 Body_Optional : Boolean := False)
11290 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11291 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11292 Act_Spec : constant Node_Id := Specification (Act_Decl);
11293 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11294 Gen_Id : constant Node_Id := Name (Inst_Node);
11295 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11296 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11297 Loc : constant Source_Ptr := Sloc (Inst_Node);
11299 procedure Check_Initialized_Types;
11300 -- In a generic package body, an entity of a generic private type may
11301 -- appear uninitialized. This is suspicious, unless the actual is a
11302 -- fully initialized type.
11304 -----------------------------
11305 -- Check_Initialized_Types --
11306 -----------------------------
11308 procedure Check_Initialized_Types is
11309 Decl : Node_Id;
11310 Formal : Entity_Id;
11311 Actual : Entity_Id;
11312 Uninit_Var : Entity_Id;
11314 begin
11315 Decl := First (Generic_Formal_Declarations (Gen_Decl));
11316 while Present (Decl) loop
11317 Uninit_Var := Empty;
11319 if Nkind (Decl) = N_Private_Extension_Declaration then
11320 Uninit_Var := Uninitialized_Variable (Decl);
11322 elsif Nkind (Decl) = N_Formal_Type_Declaration
11323 and then Nkind (Formal_Type_Definition (Decl)) =
11324 N_Formal_Private_Type_Definition
11325 then
11326 Uninit_Var :=
11327 Uninitialized_Variable (Formal_Type_Definition (Decl));
11328 end if;
11330 if Present (Uninit_Var) then
11331 Formal := Defining_Identifier (Decl);
11332 Actual := First_Entity (Act_Decl_Id);
11334 -- For each formal there is a subtype declaration that renames
11335 -- the actual and has the same name as the formal. Locate the
11336 -- formal for warning message about uninitialized variables
11337 -- in the generic, for which the actual type should be a fully
11338 -- initialized type.
11340 while Present (Actual) loop
11341 exit when Ekind (Actual) = E_Package
11342 and then Present (Renamed_Object (Actual));
11344 if Chars (Actual) = Chars (Formal)
11345 and then not Is_Scalar_Type (Actual)
11346 and then not Is_Fully_Initialized_Type (Actual)
11347 and then Warn_On_No_Value_Assigned
11348 then
11349 Error_Msg_Node_2 := Formal;
11350 Error_Msg_NE
11351 ("generic unit has uninitialized variable& of "
11352 & "formal private type &?v?", Actual, Uninit_Var);
11353 Error_Msg_NE
11354 ("actual type for& should be fully initialized type?v?",
11355 Actual, Formal);
11356 exit;
11357 end if;
11359 Next_Entity (Actual);
11360 end loop;
11361 end if;
11363 Next (Decl);
11364 end loop;
11365 end Check_Initialized_Types;
11367 -- Local variables
11369 -- The following constants capture the context prior to instantiating
11370 -- the package body.
11372 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
11373 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11374 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11375 Saved_ISMP : constant Boolean :=
11376 Ignore_SPARK_Mode_Pragmas_In_Instance;
11377 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
11378 Local_Suppress_Stack_Top;
11379 Saved_SC : constant Boolean := Style_Check;
11380 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11381 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11382 Saved_SS : constant Suppress_Record := Scope_Suppress;
11383 Saved_Warn : constant Warning_Record := Save_Warnings;
11385 Act_Body : Node_Id;
11386 Act_Body_Id : Entity_Id;
11387 Act_Body_Name : Node_Id;
11388 Gen_Body : Node_Id;
11389 Gen_Body_Id : Node_Id;
11390 Par_Ent : Entity_Id := Empty;
11391 Par_Installed : Boolean := False;
11392 Par_Vis : Boolean := False;
11394 Vis_Prims_List : Elist_Id := No_Elist;
11395 -- List of primitives made temporarily visible in the instantiation
11396 -- to match the visibility of the formal type.
11398 -- Start of processing for Instantiate_Package_Body
11400 begin
11401 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11403 -- The instance body may already have been processed, as the parent of
11404 -- another instance that is inlined (Load_Parent_Of_Generic).
11406 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11407 return;
11408 end if;
11410 -- The package being instantiated may be subject to pragma Ghost. Set
11411 -- the mode now to ensure that any nodes generated during instantiation
11412 -- are properly marked as Ghost.
11414 Set_Ghost_Mode (Act_Decl_Id);
11416 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11418 -- Re-establish the state of information on which checks are suppressed.
11419 -- This information was set in Body_Info at the point of instantiation,
11420 -- and now we restore it so that the instance is compiled using the
11421 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11423 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11424 Scope_Suppress := Body_Info.Scope_Suppress;
11426 Restore_Config_Switches (Body_Info.Config_Switches);
11427 Restore_Warnings (Body_Info.Warnings);
11429 if No (Gen_Body_Id) then
11431 -- Do not look for parent of generic body if none is required.
11432 -- This may happen when the routine is called as part of the
11433 -- Pending_Instantiations processing, when nested instances
11434 -- may precede the one generated from the main unit.
11436 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
11437 and then Body_Optional
11438 then
11439 goto Leave;
11440 else
11441 Load_Parent_Of_Generic
11442 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11443 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11444 end if;
11445 end if;
11447 -- Establish global variable for sloc adjustment and for error recovery
11448 -- In the case of an instance body for an instantiation with actuals
11449 -- from a limited view, the instance body is placed at the beginning
11450 -- of the enclosing package body: use the body entity as the source
11451 -- location for nodes of the instance body.
11453 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
11454 declare
11455 Scop : constant Entity_Id := Scope (Act_Decl_Id);
11456 Body_Id : constant Node_Id :=
11457 Corresponding_Body (Unit_Declaration_Node (Scop));
11459 begin
11460 Instantiation_Node := Body_Id;
11461 end;
11462 else
11463 Instantiation_Node := Inst_Node;
11464 end if;
11466 if Present (Gen_Body_Id) then
11467 Save_Env (Gen_Unit, Act_Decl_Id);
11468 Style_Check := False;
11470 -- If the context of the instance is subject to SPARK_Mode "off", the
11471 -- annotation is missing, or the body is instantiated at a later pass
11472 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11473 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11474 -- instance.
11476 if SPARK_Mode /= On
11477 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11478 then
11479 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11480 end if;
11482 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11483 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11485 Create_Instantiation_Source
11486 (Inst_Node, Gen_Body_Id, S_Adjustment);
11488 Act_Body :=
11489 Copy_Generic_Node
11490 (Original_Node (Gen_Body), Empty, Instantiating => True);
11492 -- Create proper (possibly qualified) defining name for the body, to
11493 -- correspond to the one in the spec.
11495 Act_Body_Id :=
11496 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11497 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11499 -- Some attributes of spec entity are not inherited by body entity
11501 Set_Handler_Records (Act_Body_Id, No_List);
11503 if Nkind (Defining_Unit_Name (Act_Spec)) =
11504 N_Defining_Program_Unit_Name
11505 then
11506 Act_Body_Name :=
11507 Make_Defining_Program_Unit_Name (Loc,
11508 Name =>
11509 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
11510 Defining_Identifier => Act_Body_Id);
11511 else
11512 Act_Body_Name := Act_Body_Id;
11513 end if;
11515 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
11517 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11518 Check_Generic_Actuals (Act_Decl_Id, False);
11519 Check_Initialized_Types;
11521 -- Install primitives hidden at the point of the instantiation but
11522 -- visible when processing the generic formals
11524 declare
11525 E : Entity_Id;
11527 begin
11528 E := First_Entity (Act_Decl_Id);
11529 while Present (E) loop
11530 if Is_Type (E)
11531 and then not Is_Itype (E)
11532 and then Is_Generic_Actual_Type (E)
11533 and then Is_Tagged_Type (E)
11534 then
11535 Install_Hidden_Primitives
11536 (Prims_List => Vis_Prims_List,
11537 Gen_T => Generic_Parent_Type (Parent (E)),
11538 Act_T => E);
11539 end if;
11541 Next_Entity (E);
11542 end loop;
11543 end;
11545 -- If it is a child unit, make the parent instance (which is an
11546 -- instance of the parent of the generic) visible. The parent
11547 -- instance is the prefix of the name of the generic unit.
11549 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11550 and then Nkind (Gen_Id) = N_Expanded_Name
11551 then
11552 Par_Ent := Entity (Prefix (Gen_Id));
11553 Par_Vis := Is_Immediately_Visible (Par_Ent);
11554 Install_Parent (Par_Ent, In_Body => True);
11555 Par_Installed := True;
11557 elsif Is_Child_Unit (Gen_Unit) then
11558 Par_Ent := Scope (Gen_Unit);
11559 Par_Vis := Is_Immediately_Visible (Par_Ent);
11560 Install_Parent (Par_Ent, In_Body => True);
11561 Par_Installed := True;
11562 end if;
11564 -- If the instantiation is a library unit, and this is the main unit,
11565 -- then build the resulting compilation unit nodes for the instance.
11566 -- If this is a compilation unit but it is not the main unit, then it
11567 -- is the body of a unit in the context, that is being compiled
11568 -- because it is encloses some inlined unit or another generic unit
11569 -- being instantiated. In that case, this body is not part of the
11570 -- current compilation, and is not attached to the tree, but its
11571 -- parent must be set for analysis.
11573 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11575 -- Replace instance node with body of instance, and create new
11576 -- node for corresponding instance declaration.
11578 Build_Instance_Compilation_Unit_Nodes
11579 (Inst_Node, Act_Body, Act_Decl);
11580 Analyze (Inst_Node);
11582 if Parent (Inst_Node) = Cunit (Main_Unit) then
11584 -- If the instance is a child unit itself, then set the scope
11585 -- of the expanded body to be the parent of the instantiation
11586 -- (ensuring that the fully qualified name will be generated
11587 -- for the elaboration subprogram).
11589 if Nkind (Defining_Unit_Name (Act_Spec)) =
11590 N_Defining_Program_Unit_Name
11591 then
11592 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
11593 end if;
11594 end if;
11596 -- Case where instantiation is not a library unit
11598 else
11599 -- If this is an early instantiation, i.e. appears textually
11600 -- before the corresponding body and must be elaborated first,
11601 -- indicate that the body instance is to be delayed.
11603 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
11605 -- Now analyze the body. We turn off all checks if this is an
11606 -- internal unit, since there is no reason to have checks on for
11607 -- any predefined run-time library code. All such code is designed
11608 -- to be compiled with checks off.
11610 -- Note that we do NOT apply this criterion to children of GNAT
11611 -- The latter units must suppress checks explicitly if needed.
11613 -- We also do not suppress checks in CodePeer mode where we are
11614 -- interested in finding possible runtime errors.
11616 if not CodePeer_Mode
11617 and then In_Predefined_Unit (Gen_Decl)
11618 then
11619 Analyze (Act_Body, Suppress => All_Checks);
11620 else
11621 Analyze (Act_Body);
11622 end if;
11623 end if;
11625 Inherit_Context (Gen_Body, Inst_Node);
11627 -- Remove the parent instances if they have been placed on the scope
11628 -- stack to compile the body.
11630 if Par_Installed then
11631 Remove_Parent (In_Body => True);
11633 -- Restore the previous visibility of the parent
11635 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11636 end if;
11638 Restore_Hidden_Primitives (Vis_Prims_List);
11639 Restore_Private_Views (Act_Decl_Id);
11641 -- Remove the current unit from visibility if this is an instance
11642 -- that is not elaborated on the fly for inlining purposes.
11644 if not Inlined_Body then
11645 Set_Is_Immediately_Visible (Act_Decl_Id, False);
11646 end if;
11648 Restore_Env;
11650 -- If we have no body, and the unit requires a body, then complain. This
11651 -- complaint is suppressed if we have detected other errors (since a
11652 -- common reason for missing the body is that it had errors).
11653 -- In CodePeer mode, a warning has been emitted already, no need for
11654 -- further messages.
11656 elsif Unit_Requires_Body (Gen_Unit)
11657 and then not Body_Optional
11658 then
11659 if CodePeer_Mode then
11660 null;
11662 elsif Serious_Errors_Detected = 0 then
11663 Error_Msg_NE
11664 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
11666 -- Don't attempt to perform any cleanup actions if some other error
11667 -- was already detected, since this can cause blowups.
11669 else
11670 goto Leave;
11671 end if;
11673 -- Case of package that does not need a body
11675 else
11676 -- If the instantiation of the declaration is a library unit, rewrite
11677 -- the original package instantiation as a package declaration in the
11678 -- compilation unit node.
11680 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11681 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
11682 Rewrite (Inst_Node, Act_Decl);
11684 -- Generate elaboration entity, in case spec has elaboration code.
11685 -- This cannot be done when the instance is analyzed, because it
11686 -- is not known yet whether the body exists.
11688 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
11689 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
11691 -- If the instantiation is not a library unit, then append the
11692 -- declaration to the list of implicitly generated entities, unless
11693 -- it is already a list member which means that it was already
11694 -- processed
11696 elsif not Is_List_Member (Act_Decl) then
11697 Mark_Rewrite_Insertion (Act_Decl);
11698 Insert_Before (Inst_Node, Act_Decl);
11699 end if;
11700 end if;
11702 <<Leave>>
11704 -- Restore the context that was in effect prior to instantiating the
11705 -- package body.
11707 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
11708 Local_Suppress_Stack_Top := Saved_LSST;
11709 Scope_Suppress := Saved_SS;
11710 Style_Check := Saved_SC;
11712 Expander_Mode_Restore;
11713 Restore_Config_Switches (Saved_CS);
11714 Restore_Ghost_Region (Saved_GM, Saved_IGR);
11715 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
11716 Restore_Warnings (Saved_Warn);
11717 end Instantiate_Package_Body;
11719 ---------------------------------
11720 -- Instantiate_Subprogram_Body --
11721 ---------------------------------
11723 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11724 -- must be replaced by gotos which jump to the end of the routine in order
11725 -- to restore the Ghost and SPARK modes.
11727 procedure Instantiate_Subprogram_Body
11728 (Body_Info : Pending_Body_Info;
11729 Body_Optional : Boolean := False)
11731 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11732 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11733 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11734 Gen_Id : constant Node_Id := Name (Inst_Node);
11735 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11736 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11737 Loc : constant Source_Ptr := Sloc (Inst_Node);
11738 Pack_Id : constant Entity_Id :=
11739 Defining_Unit_Name (Parent (Act_Decl));
11741 -- The following constants capture the context prior to instantiating
11742 -- the subprogram body.
11744 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
11745 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11746 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11747 Saved_ISMP : constant Boolean :=
11748 Ignore_SPARK_Mode_Pragmas_In_Instance;
11749 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
11750 Local_Suppress_Stack_Top;
11751 Saved_SC : constant Boolean := Style_Check;
11752 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11753 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11754 Saved_SS : constant Suppress_Record := Scope_Suppress;
11755 Saved_Warn : constant Warning_Record := Save_Warnings;
11757 Act_Body : Node_Id;
11758 Act_Body_Id : Entity_Id;
11759 Gen_Body : Node_Id;
11760 Gen_Body_Id : Node_Id;
11761 Pack_Body : Node_Id;
11762 Par_Ent : Entity_Id := Empty;
11763 Par_Installed : Boolean := False;
11764 Par_Vis : Boolean := False;
11765 Ret_Expr : Node_Id;
11767 begin
11768 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11770 -- Subprogram body may have been created already because of an inline
11771 -- pragma, or because of multiple elaborations of the enclosing package
11772 -- when several instances of the subprogram appear in the main unit.
11774 if Present (Corresponding_Body (Act_Decl)) then
11775 return;
11776 end if;
11778 -- The subprogram being instantiated may be subject to pragma Ghost. Set
11779 -- the mode now to ensure that any nodes generated during instantiation
11780 -- are properly marked as Ghost.
11782 Set_Ghost_Mode (Act_Decl_Id);
11784 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11786 -- Re-establish the state of information on which checks are suppressed.
11787 -- This information was set in Body_Info at the point of instantiation,
11788 -- and now we restore it so that the instance is compiled using the
11789 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11791 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11792 Scope_Suppress := Body_Info.Scope_Suppress;
11794 Restore_Config_Switches (Body_Info.Config_Switches);
11795 Restore_Warnings (Body_Info.Warnings);
11797 if No (Gen_Body_Id) then
11799 -- For imported generic subprogram, no body to compile, complete
11800 -- the spec entity appropriately.
11802 if Is_Imported (Gen_Unit) then
11803 Set_Is_Imported (Act_Decl_Id);
11804 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
11805 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
11806 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
11807 Set_Has_Completion (Act_Decl_Id);
11808 goto Leave;
11810 -- For other cases, compile the body
11812 else
11813 Load_Parent_Of_Generic
11814 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11815 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11816 end if;
11817 end if;
11819 Instantiation_Node := Inst_Node;
11821 if Present (Gen_Body_Id) then
11822 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11824 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11826 -- Either body is not present, or context is non-expanding, as
11827 -- when compiling a subunit. Mark the instance as completed, and
11828 -- diagnose a missing body when needed.
11830 if Expander_Active
11831 and then Operating_Mode = Generate_Code
11832 then
11833 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
11834 end if;
11836 Set_Has_Completion (Act_Decl_Id);
11837 goto Leave;
11838 end if;
11840 Save_Env (Gen_Unit, Act_Decl_Id);
11841 Style_Check := False;
11843 -- If the context of the instance is subject to SPARK_Mode "off", the
11844 -- annotation is missing, or the body is instantiated at a later pass
11845 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11846 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11847 -- instance.
11849 if SPARK_Mode /= On
11850 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11851 then
11852 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11853 end if;
11855 -- If the context of an instance is not subject to SPARK_Mode "off",
11856 -- and the generic body is subject to an explicit SPARK_Mode pragma,
11857 -- the latter should be the one applicable to the instance.
11859 if not Ignore_SPARK_Mode_Pragmas_In_Instance
11860 and then SPARK_Mode /= Off
11861 and then Present (SPARK_Pragma (Gen_Body_Id))
11862 then
11863 Set_SPARK_Mode (Gen_Body_Id);
11864 end if;
11866 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11867 Create_Instantiation_Source
11868 (Inst_Node,
11869 Gen_Body_Id,
11870 S_Adjustment);
11872 Act_Body :=
11873 Copy_Generic_Node
11874 (Original_Node (Gen_Body), Empty, Instantiating => True);
11876 -- Create proper defining name for the body, to correspond to the one
11877 -- in the spec.
11879 Act_Body_Id :=
11880 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11882 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11883 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
11885 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11886 Set_Has_Completion (Act_Decl_Id);
11887 Check_Generic_Actuals (Pack_Id, False);
11889 -- Generate a reference to link the visible subprogram instance to
11890 -- the generic body, which for navigation purposes is the only
11891 -- available source for the instance.
11893 Generate_Reference
11894 (Related_Instance (Pack_Id),
11895 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11897 -- If it is a child unit, make the parent instance (which is an
11898 -- instance of the parent of the generic) visible. The parent
11899 -- instance is the prefix of the name of the generic unit.
11901 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11902 and then Nkind (Gen_Id) = N_Expanded_Name
11903 then
11904 Par_Ent := Entity (Prefix (Gen_Id));
11905 Par_Vis := Is_Immediately_Visible (Par_Ent);
11906 Install_Parent (Par_Ent, In_Body => True);
11907 Par_Installed := True;
11909 elsif Is_Child_Unit (Gen_Unit) then
11910 Par_Ent := Scope (Gen_Unit);
11911 Par_Vis := Is_Immediately_Visible (Par_Ent);
11912 Install_Parent (Par_Ent, In_Body => True);
11913 Par_Installed := True;
11914 end if;
11916 -- Subprogram body is placed in the body of wrapper package,
11917 -- whose spec contains the subprogram declaration as well as
11918 -- the renaming declarations for the generic parameters.
11920 Pack_Body :=
11921 Make_Package_Body (Loc,
11922 Defining_Unit_Name => New_Copy (Pack_Id),
11923 Declarations => New_List (Act_Body));
11925 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11927 -- If the instantiation is a library unit, then build resulting
11928 -- compilation unit nodes for the instance. The declaration of
11929 -- the enclosing package is the grandparent of the subprogram
11930 -- declaration. First replace the instantiation node as the unit
11931 -- of the corresponding compilation.
11933 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11934 if Parent (Inst_Node) = Cunit (Main_Unit) then
11935 Set_Unit (Parent (Inst_Node), Inst_Node);
11936 Build_Instance_Compilation_Unit_Nodes
11937 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11938 Analyze (Inst_Node);
11939 else
11940 Set_Parent (Pack_Body, Parent (Inst_Node));
11941 Analyze (Pack_Body);
11942 end if;
11944 else
11945 Insert_Before (Inst_Node, Pack_Body);
11946 Mark_Rewrite_Insertion (Pack_Body);
11947 Analyze (Pack_Body);
11949 if Expander_Active then
11950 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11951 end if;
11952 end if;
11954 Inherit_Context (Gen_Body, Inst_Node);
11956 Restore_Private_Views (Pack_Id, False);
11958 if Par_Installed then
11959 Remove_Parent (In_Body => True);
11961 -- Restore the previous visibility of the parent
11963 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11964 end if;
11966 Restore_Env;
11968 -- Body not found. Error was emitted already. If there were no previous
11969 -- errors, this may be an instance whose scope is a premature instance.
11970 -- In that case we must insure that the (legal) program does raise
11971 -- program error if executed. We generate a subprogram body for this
11972 -- purpose. See DEC ac30vso.
11974 -- Should not reference proprietary DEC tests in comments ???
11976 elsif Serious_Errors_Detected = 0
11977 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
11978 then
11979 if Body_Optional then
11980 goto Leave;
11982 elsif Ekind (Act_Decl_Id) = E_Procedure then
11983 Act_Body :=
11984 Make_Subprogram_Body (Loc,
11985 Specification =>
11986 Make_Procedure_Specification (Loc,
11987 Defining_Unit_Name =>
11988 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11989 Parameter_Specifications =>
11990 New_Copy_List
11991 (Parameter_Specifications (Parent (Act_Decl_Id)))),
11993 Declarations => Empty_List,
11994 Handled_Statement_Sequence =>
11995 Make_Handled_Sequence_Of_Statements (Loc,
11996 Statements => New_List (
11997 Make_Raise_Program_Error (Loc,
11998 Reason => PE_Access_Before_Elaboration))));
12000 else
12001 Ret_Expr :=
12002 Make_Raise_Program_Error (Loc,
12003 Reason => PE_Access_Before_Elaboration);
12005 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
12006 Set_Analyzed (Ret_Expr);
12008 Act_Body :=
12009 Make_Subprogram_Body (Loc,
12010 Specification =>
12011 Make_Function_Specification (Loc,
12012 Defining_Unit_Name =>
12013 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12014 Parameter_Specifications =>
12015 New_Copy_List
12016 (Parameter_Specifications (Parent (Act_Decl_Id))),
12017 Result_Definition =>
12018 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
12020 Declarations => Empty_List,
12021 Handled_Statement_Sequence =>
12022 Make_Handled_Sequence_Of_Statements (Loc,
12023 Statements => New_List (
12024 Make_Simple_Return_Statement (Loc, Ret_Expr))));
12025 end if;
12027 Pack_Body :=
12028 Make_Package_Body (Loc,
12029 Defining_Unit_Name => New_Copy (Pack_Id),
12030 Declarations => New_List (Act_Body));
12032 Insert_After (Inst_Node, Pack_Body);
12033 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12034 Analyze (Pack_Body);
12035 end if;
12037 <<Leave>>
12039 -- Restore the context that was in effect prior to instantiating the
12040 -- subprogram body.
12042 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12043 Local_Suppress_Stack_Top := Saved_LSST;
12044 Scope_Suppress := Saved_SS;
12045 Style_Check := Saved_SC;
12047 Expander_Mode_Restore;
12048 Restore_Config_Switches (Saved_CS);
12049 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12050 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12051 Restore_Warnings (Saved_Warn);
12052 end Instantiate_Subprogram_Body;
12054 ----------------------
12055 -- Instantiate_Type --
12056 ----------------------
12058 function Instantiate_Type
12059 (Formal : Node_Id;
12060 Actual : Node_Id;
12061 Analyzed_Formal : Node_Id;
12062 Actual_Decls : List_Id) return List_Id
12064 A_Gen_T : constant Entity_Id :=
12065 Defining_Identifier (Analyzed_Formal);
12066 Def : constant Node_Id := Formal_Type_Definition (Formal);
12067 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
12068 Act_T : Entity_Id;
12069 Ancestor : Entity_Id := Empty;
12070 Decl_Node : Node_Id;
12071 Decl_Nodes : List_Id;
12072 Loc : Source_Ptr;
12073 Subt : Entity_Id;
12075 procedure Diagnose_Predicated_Actual;
12076 -- There are a number of constructs in which a discrete type with
12077 -- predicates is illegal, e.g. as an index in an array type declaration.
12078 -- If a generic type is used is such a construct in a generic package
12079 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
12080 -- of the generic contract that the actual cannot have predicates.
12082 procedure Validate_Array_Type_Instance;
12083 procedure Validate_Access_Subprogram_Instance;
12084 procedure Validate_Access_Type_Instance;
12085 procedure Validate_Derived_Type_Instance;
12086 procedure Validate_Derived_Interface_Type_Instance;
12087 procedure Validate_Discriminated_Formal_Type;
12088 procedure Validate_Interface_Type_Instance;
12089 procedure Validate_Private_Type_Instance;
12090 procedure Validate_Incomplete_Type_Instance;
12091 -- These procedures perform validation tests for the named case.
12092 -- Validate_Discriminated_Formal_Type is shared by formal private
12093 -- types and Ada 2012 formal incomplete types.
12095 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
12096 -- Check that base types are the same and that the subtypes match
12097 -- statically. Used in several of the above.
12099 ---------------------------------
12100 -- Diagnose_Predicated_Actual --
12101 ---------------------------------
12103 procedure Diagnose_Predicated_Actual is
12104 begin
12105 if No_Predicate_On_Actual (A_Gen_T)
12106 and then Has_Predicates (Act_T)
12107 then
12108 Error_Msg_NE
12109 ("actual for& cannot be a type with predicate",
12110 Instantiation_Node, A_Gen_T);
12112 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
12113 and then Has_Predicates (Act_T)
12114 and then not Has_Static_Predicate_Aspect (Act_T)
12115 then
12116 Error_Msg_NE
12117 ("actual for& cannot be a type with a dynamic predicate",
12118 Instantiation_Node, A_Gen_T);
12119 end if;
12120 end Diagnose_Predicated_Actual;
12122 --------------------
12123 -- Subtypes_Match --
12124 --------------------
12126 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
12127 T : constant Entity_Id := Get_Instance_Of (Gen_T);
12129 begin
12130 -- Some detailed comments would be useful here ???
12132 return ((Base_Type (T) = Act_T
12133 or else Base_Type (T) = Base_Type (Act_T))
12134 and then Subtypes_Statically_Match (T, Act_T))
12136 or else (Is_Class_Wide_Type (Gen_T)
12137 and then Is_Class_Wide_Type (Act_T)
12138 and then Subtypes_Match
12139 (Get_Instance_Of (Root_Type (Gen_T)),
12140 Root_Type (Act_T)))
12142 or else
12143 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
12144 E_Anonymous_Access_Type)
12145 and then Ekind (Act_T) = Ekind (Gen_T)
12146 and then Subtypes_Statically_Match
12147 (Designated_Type (Gen_T), Designated_Type (Act_T)));
12148 end Subtypes_Match;
12150 -----------------------------------------
12151 -- Validate_Access_Subprogram_Instance --
12152 -----------------------------------------
12154 procedure Validate_Access_Subprogram_Instance is
12155 begin
12156 if not Is_Access_Type (Act_T)
12157 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
12158 then
12159 Error_Msg_NE
12160 ("expect access type in instantiation of &", Actual, Gen_T);
12161 Abandon_Instantiation (Actual);
12162 end if;
12164 -- According to AI05-288, actuals for access_to_subprograms must be
12165 -- subtype conformant with the generic formal. Previous to AI05-288
12166 -- only mode conformance was required.
12168 -- This is a binding interpretation that applies to previous versions
12169 -- of the language, no need to maintain previous weaker checks.
12171 Check_Subtype_Conformant
12172 (Designated_Type (Act_T),
12173 Designated_Type (A_Gen_T),
12174 Actual,
12175 Get_Inst => True);
12177 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
12178 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
12179 Error_Msg_NE
12180 ("protected access type not allowed for formal &",
12181 Actual, Gen_T);
12182 end if;
12184 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
12185 Error_Msg_NE
12186 ("expect protected access type for formal &",
12187 Actual, Gen_T);
12188 end if;
12190 -- If the formal has a specified convention (which in most cases
12191 -- will be StdCall) verify that the actual has the same convention.
12193 if Has_Convention_Pragma (A_Gen_T)
12194 and then Convention (A_Gen_T) /= Convention (Act_T)
12195 then
12196 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
12197 Error_Msg_NE
12198 ("actual for formal & must have convention %", Actual, Gen_T);
12199 end if;
12200 end Validate_Access_Subprogram_Instance;
12202 -----------------------------------
12203 -- Validate_Access_Type_Instance --
12204 -----------------------------------
12206 procedure Validate_Access_Type_Instance is
12207 Desig_Type : constant Entity_Id :=
12208 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
12209 Desig_Act : Entity_Id;
12211 begin
12212 if not Is_Access_Type (Act_T) then
12213 Error_Msg_NE
12214 ("expect access type in instantiation of &", Actual, Gen_T);
12215 Abandon_Instantiation (Actual);
12216 end if;
12218 if Is_Access_Constant (A_Gen_T) then
12219 if not Is_Access_Constant (Act_T) then
12220 Error_Msg_N
12221 ("actual type must be access-to-constant type", Actual);
12222 Abandon_Instantiation (Actual);
12223 end if;
12224 else
12225 if Is_Access_Constant (Act_T) then
12226 Error_Msg_N
12227 ("actual type must be access-to-variable type", Actual);
12228 Abandon_Instantiation (Actual);
12230 elsif Ekind (A_Gen_T) = E_General_Access_Type
12231 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
12232 then
12233 Error_Msg_N -- CODEFIX
12234 ("actual must be general access type!", Actual);
12235 Error_Msg_NE -- CODEFIX
12236 ("add ALL to }!", Actual, Act_T);
12237 Abandon_Instantiation (Actual);
12238 end if;
12239 end if;
12241 -- The designated subtypes, that is to say the subtypes introduced
12242 -- by an access type declaration (and not by a subtype declaration)
12243 -- must match.
12245 Desig_Act := Designated_Type (Base_Type (Act_T));
12247 -- The designated type may have been introduced through a limited_
12248 -- with clause, in which case retrieve the non-limited view. This
12249 -- applies to incomplete types as well as to class-wide types.
12251 if From_Limited_With (Desig_Act) then
12252 Desig_Act := Available_View (Desig_Act);
12253 end if;
12255 if not Subtypes_Match (Desig_Type, Desig_Act) then
12256 Error_Msg_NE
12257 ("designated type of actual does not match that of formal &",
12258 Actual, Gen_T);
12260 if not Predicates_Match (Desig_Type, Desig_Act) then
12261 Error_Msg_N ("\predicates do not match", Actual);
12262 end if;
12264 Abandon_Instantiation (Actual);
12266 elsif Is_Access_Type (Designated_Type (Act_T))
12267 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
12269 Is_Constrained (Designated_Type (Desig_Type))
12270 then
12271 Error_Msg_NE
12272 ("designated type of actual does not match that of formal &",
12273 Actual, Gen_T);
12275 if not Predicates_Match (Desig_Type, Desig_Act) then
12276 Error_Msg_N ("\predicates do not match", Actual);
12277 end if;
12279 Abandon_Instantiation (Actual);
12280 end if;
12282 -- Ada 2005: null-exclusion indicators of the two types must agree
12284 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12285 Error_Msg_NE
12286 ("non null exclusion of actual and formal & do not match",
12287 Actual, Gen_T);
12288 end if;
12289 end Validate_Access_Type_Instance;
12291 ----------------------------------
12292 -- Validate_Array_Type_Instance --
12293 ----------------------------------
12295 procedure Validate_Array_Type_Instance is
12296 I1 : Node_Id;
12297 I2 : Node_Id;
12298 T2 : Entity_Id;
12300 function Formal_Dimensions return Nat;
12301 -- Count number of dimensions in array type formal
12303 -----------------------
12304 -- Formal_Dimensions --
12305 -----------------------
12307 function Formal_Dimensions return Nat is
12308 Num : Nat := 0;
12309 Index : Node_Id;
12311 begin
12312 if Nkind (Def) = N_Constrained_Array_Definition then
12313 Index := First (Discrete_Subtype_Definitions (Def));
12314 else
12315 Index := First (Subtype_Marks (Def));
12316 end if;
12318 while Present (Index) loop
12319 Num := Num + 1;
12320 Next_Index (Index);
12321 end loop;
12323 return Num;
12324 end Formal_Dimensions;
12326 -- Start of processing for Validate_Array_Type_Instance
12328 begin
12329 if not Is_Array_Type (Act_T) then
12330 Error_Msg_NE
12331 ("expect array type in instantiation of &", Actual, Gen_T);
12332 Abandon_Instantiation (Actual);
12334 elsif Nkind (Def) = N_Constrained_Array_Definition then
12335 if not (Is_Constrained (Act_T)) then
12336 Error_Msg_NE
12337 ("expect constrained array in instantiation of &",
12338 Actual, Gen_T);
12339 Abandon_Instantiation (Actual);
12340 end if;
12342 else
12343 if Is_Constrained (Act_T) then
12344 Error_Msg_NE
12345 ("expect unconstrained array in instantiation of &",
12346 Actual, Gen_T);
12347 Abandon_Instantiation (Actual);
12348 end if;
12349 end if;
12351 if Formal_Dimensions /= Number_Dimensions (Act_T) then
12352 Error_Msg_NE
12353 ("dimensions of actual do not match formal &", Actual, Gen_T);
12354 Abandon_Instantiation (Actual);
12355 end if;
12357 I1 := First_Index (A_Gen_T);
12358 I2 := First_Index (Act_T);
12359 for J in 1 .. Formal_Dimensions loop
12361 -- If the indexes of the actual were given by a subtype_mark,
12362 -- the index was transformed into a range attribute. Retrieve
12363 -- the original type mark for checking.
12365 if Is_Entity_Name (Original_Node (I2)) then
12366 T2 := Entity (Original_Node (I2));
12367 else
12368 T2 := Etype (I2);
12369 end if;
12371 if not Subtypes_Match
12372 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
12373 then
12374 Error_Msg_NE
12375 ("index types of actual do not match those of formal &",
12376 Actual, Gen_T);
12377 Abandon_Instantiation (Actual);
12378 end if;
12380 Next_Index (I1);
12381 Next_Index (I2);
12382 end loop;
12384 -- Check matching subtypes. Note that there are complex visibility
12385 -- issues when the generic is a child unit and some aspect of the
12386 -- generic type is declared in a parent unit of the generic. We do
12387 -- the test to handle this special case only after a direct check
12388 -- for static matching has failed. The case where both the component
12389 -- type and the array type are separate formals, and the component
12390 -- type is a private view may also require special checking in
12391 -- Subtypes_Match. Finally, we assume that a child instance where
12392 -- the component type comes from a formal of a parent instance is
12393 -- correct because the generic was correct. A more precise check
12394 -- seems too complex to install???
12396 if Subtypes_Match
12397 (Component_Type (A_Gen_T), Component_Type (Act_T))
12398 or else
12399 Subtypes_Match
12400 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
12401 Component_Type (Act_T))
12402 or else
12403 (not Inside_A_Generic
12404 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
12405 then
12406 null;
12407 else
12408 Error_Msg_NE
12409 ("component subtype of actual does not match that of formal &",
12410 Actual, Gen_T);
12411 Abandon_Instantiation (Actual);
12412 end if;
12414 if Has_Aliased_Components (A_Gen_T)
12415 and then not Has_Aliased_Components (Act_T)
12416 then
12417 Error_Msg_NE
12418 ("actual must have aliased components to match formal type &",
12419 Actual, Gen_T);
12420 end if;
12421 end Validate_Array_Type_Instance;
12423 -----------------------------------------------
12424 -- Validate_Derived_Interface_Type_Instance --
12425 -----------------------------------------------
12427 procedure Validate_Derived_Interface_Type_Instance is
12428 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
12429 Elmt : Elmt_Id;
12431 begin
12432 -- First apply interface instance checks
12434 Validate_Interface_Type_Instance;
12436 -- Verify that immediate parent interface is an ancestor of
12437 -- the actual.
12439 if Present (Par)
12440 and then not Interface_Present_In_Ancestor (Act_T, Par)
12441 then
12442 Error_Msg_NE
12443 ("interface actual must include progenitor&", Actual, Par);
12444 end if;
12446 -- Now verify that the actual includes all other ancestors of
12447 -- the formal.
12449 Elmt := First_Elmt (Interfaces (A_Gen_T));
12450 while Present (Elmt) loop
12451 if not Interface_Present_In_Ancestor
12452 (Act_T, Get_Instance_Of (Node (Elmt)))
12453 then
12454 Error_Msg_NE
12455 ("interface actual must include progenitor&",
12456 Actual, Node (Elmt));
12457 end if;
12459 Next_Elmt (Elmt);
12460 end loop;
12461 end Validate_Derived_Interface_Type_Instance;
12463 ------------------------------------
12464 -- Validate_Derived_Type_Instance --
12465 ------------------------------------
12467 procedure Validate_Derived_Type_Instance is
12468 Actual_Discr : Entity_Id;
12469 Ancestor_Discr : Entity_Id;
12471 begin
12472 -- Verify that the actual includes the progenitors of the formal,
12473 -- if any. The formal may depend on previous formals and their
12474 -- instance, so we must examine instance of interfaces if present.
12475 -- The actual may be an extension of an interface, in which case
12476 -- it does not appear in the interface list, so this must be
12477 -- checked separately.
12479 if Present (Interface_List (Def)) then
12480 if not Has_Interfaces (Act_T) then
12481 Error_Msg_NE
12482 ("actual must implement all interfaces of formal&",
12483 Actual, A_Gen_T);
12485 else
12486 declare
12487 Act_Iface_List : Elist_Id;
12488 Iface : Node_Id;
12489 Iface_Ent : Entity_Id;
12491 function Instance_Exists (I : Entity_Id) return Boolean;
12492 -- If the interface entity is declared in a generic unit,
12493 -- this can only be legal if we are within an instantiation
12494 -- of a child of that generic. There is currently no
12495 -- mechanism to relate an interface declared within a
12496 -- generic to the corresponding interface in an instance,
12497 -- so we traverse the list of interfaces of the actual,
12498 -- looking for a name match.
12500 ---------------------
12501 -- Instance_Exists --
12502 ---------------------
12504 function Instance_Exists (I : Entity_Id) return Boolean is
12505 Iface_Elmt : Elmt_Id;
12507 begin
12508 Iface_Elmt := First_Elmt (Act_Iface_List);
12509 while Present (Iface_Elmt) loop
12510 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
12511 and then Chars (Node (Iface_Elmt)) = Chars (I)
12512 then
12513 return True;
12514 end if;
12516 Next_Elmt (Iface_Elmt);
12517 end loop;
12519 return False;
12520 end Instance_Exists;
12522 begin
12523 Iface := First (Abstract_Interface_List (A_Gen_T));
12524 Collect_Interfaces (Act_T, Act_Iface_List);
12526 while Present (Iface) loop
12527 Iface_Ent := Get_Instance_Of (Entity (Iface));
12529 if Is_Ancestor (Iface_Ent, Act_T)
12530 or else Is_Progenitor (Iface_Ent, Act_T)
12531 then
12532 null;
12534 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
12535 and then Instance_Exists (Iface_Ent)
12536 then
12537 null;
12539 else
12540 Error_Msg_Name_1 := Chars (Act_T);
12541 Error_Msg_NE
12542 ("Actual% must implement interface&",
12543 Actual, Etype (Iface));
12544 end if;
12546 Next (Iface);
12547 end loop;
12548 end;
12549 end if;
12550 end if;
12552 -- If the parent type in the generic declaration is itself a previous
12553 -- formal type, then it is local to the generic and absent from the
12554 -- analyzed generic definition. In that case the ancestor is the
12555 -- instance of the formal (which must have been instantiated
12556 -- previously), unless the ancestor is itself a formal derived type.
12557 -- In this latter case (which is the subject of Corrigendum 8652/0038
12558 -- (AI-202) the ancestor of the formals is the ancestor of its
12559 -- parent. Otherwise, the analyzed generic carries the parent type.
12560 -- If the parent type is defined in a previous formal package, then
12561 -- the scope of that formal package is that of the generic type
12562 -- itself, and it has already been mapped into the corresponding type
12563 -- in the actual package.
12565 -- Common case: parent type defined outside of the generic
12567 if Is_Entity_Name (Subtype_Mark (Def))
12568 and then Present (Entity (Subtype_Mark (Def)))
12569 then
12570 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
12572 -- Check whether parent is defined in a previous formal package
12574 elsif
12575 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
12576 then
12577 Ancestor :=
12578 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
12580 -- The type may be a local derivation, or a type extension of a
12581 -- previous formal, or of a formal of a parent package.
12583 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
12584 or else
12585 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
12586 then
12587 -- Check whether the parent is another derived formal type in the
12588 -- same generic unit.
12590 if Etype (A_Gen_T) /= A_Gen_T
12591 and then Is_Generic_Type (Etype (A_Gen_T))
12592 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
12593 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
12594 then
12595 -- Locate ancestor of parent from the subtype declaration
12596 -- created for the actual.
12598 declare
12599 Decl : Node_Id;
12601 begin
12602 Decl := First (Actual_Decls);
12603 while Present (Decl) loop
12604 if Nkind (Decl) = N_Subtype_Declaration
12605 and then Chars (Defining_Identifier (Decl)) =
12606 Chars (Etype (A_Gen_T))
12607 then
12608 Ancestor := Generic_Parent_Type (Decl);
12609 exit;
12610 else
12611 Next (Decl);
12612 end if;
12613 end loop;
12614 end;
12616 pragma Assert (Present (Ancestor));
12618 -- The ancestor itself may be a previous formal that has been
12619 -- instantiated.
12621 Ancestor := Get_Instance_Of (Ancestor);
12623 else
12624 Ancestor :=
12625 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
12626 end if;
12628 -- Check whether parent is a previous formal of the current generic
12630 elsif Is_Derived_Type (A_Gen_T)
12631 and then Is_Generic_Type (Etype (A_Gen_T))
12632 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
12633 then
12634 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
12636 -- An unusual case: the actual is a type declared in a parent unit,
12637 -- but is not a formal type so there is no instance_of for it.
12638 -- Retrieve it by analyzing the record extension.
12640 elsif Is_Child_Unit (Scope (A_Gen_T))
12641 and then In_Open_Scopes (Scope (Act_T))
12642 and then Is_Generic_Instance (Scope (Act_T))
12643 then
12644 Analyze (Subtype_Mark (Def));
12645 Ancestor := Entity (Subtype_Mark (Def));
12647 else
12648 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
12649 end if;
12651 -- If the formal derived type has pragma Preelaborable_Initialization
12652 -- then the actual type must have preelaborable initialization.
12654 if Known_To_Have_Preelab_Init (A_Gen_T)
12655 and then not Has_Preelaborable_Initialization (Act_T)
12656 then
12657 Error_Msg_NE
12658 ("actual for & must have preelaborable initialization",
12659 Actual, Gen_T);
12660 end if;
12662 -- Ada 2005 (AI-251)
12664 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
12665 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
12666 Error_Msg_NE
12667 ("(Ada 2005) expected type implementing & in instantiation",
12668 Actual, Ancestor);
12669 end if;
12671 -- Finally verify that the (instance of) the ancestor is an ancestor
12672 -- of the actual.
12674 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
12675 Error_Msg_NE
12676 ("expect type derived from & in instantiation",
12677 Actual, First_Subtype (Ancestor));
12678 Abandon_Instantiation (Actual);
12679 end if;
12681 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
12682 -- that the formal type declaration has been rewritten as a private
12683 -- extension.
12685 if Ada_Version >= Ada_2005
12686 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
12687 and then Synchronized_Present (Parent (A_Gen_T))
12688 then
12689 -- The actual must be a synchronized tagged type
12691 if not Is_Tagged_Type (Act_T) then
12692 Error_Msg_N
12693 ("actual of synchronized type must be tagged", Actual);
12694 Abandon_Instantiation (Actual);
12696 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
12697 and then Nkind (Type_Definition (Parent (Act_T))) =
12698 N_Derived_Type_Definition
12699 and then not Synchronized_Present
12700 (Type_Definition (Parent (Act_T)))
12701 then
12702 Error_Msg_N
12703 ("actual of synchronized type must be synchronized", Actual);
12704 Abandon_Instantiation (Actual);
12705 end if;
12706 end if;
12708 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
12709 -- removes the second instance of the phrase "or allow pass by copy".
12711 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
12712 Error_Msg_N
12713 ("cannot have atomic actual type for non-atomic formal type",
12714 Actual);
12716 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
12717 Error_Msg_N
12718 ("cannot have volatile actual type for non-volatile formal type",
12719 Actual);
12720 end if;
12722 -- It should not be necessary to check for unknown discriminants on
12723 -- Formal, but for some reason Has_Unknown_Discriminants is false for
12724 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
12725 -- needs fixing. ???
12727 if Is_Definite_Subtype (A_Gen_T)
12728 and then not Unknown_Discriminants_Present (Formal)
12729 and then not Is_Definite_Subtype (Act_T)
12730 then
12731 Error_Msg_N ("actual subtype must be constrained", Actual);
12732 Abandon_Instantiation (Actual);
12733 end if;
12735 if not Unknown_Discriminants_Present (Formal) then
12736 if Is_Constrained (Ancestor) then
12737 if not Is_Constrained (Act_T) then
12738 Error_Msg_N ("actual subtype must be constrained", Actual);
12739 Abandon_Instantiation (Actual);
12740 end if;
12742 -- Ancestor is unconstrained, Check if generic formal and actual
12743 -- agree on constrainedness. The check only applies to array types
12744 -- and discriminated types.
12746 elsif Is_Constrained (Act_T) then
12747 if Ekind (Ancestor) = E_Access_Type
12748 or else (not Is_Constrained (A_Gen_T)
12749 and then Is_Composite_Type (A_Gen_T))
12750 then
12751 Error_Msg_N ("actual subtype must be unconstrained", Actual);
12752 Abandon_Instantiation (Actual);
12753 end if;
12755 -- A class-wide type is only allowed if the formal has unknown
12756 -- discriminants.
12758 elsif Is_Class_Wide_Type (Act_T)
12759 and then not Has_Unknown_Discriminants (Ancestor)
12760 then
12761 Error_Msg_NE
12762 ("actual for & cannot be a class-wide type", Actual, Gen_T);
12763 Abandon_Instantiation (Actual);
12765 -- Otherwise, the formal and actual must have the same number
12766 -- of discriminants and each discriminant of the actual must
12767 -- correspond to a discriminant of the formal.
12769 elsif Has_Discriminants (Act_T)
12770 and then not Has_Unknown_Discriminants (Act_T)
12771 and then Has_Discriminants (Ancestor)
12772 then
12773 Actual_Discr := First_Discriminant (Act_T);
12774 Ancestor_Discr := First_Discriminant (Ancestor);
12775 while Present (Actual_Discr)
12776 and then Present (Ancestor_Discr)
12777 loop
12778 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
12779 No (Corresponding_Discriminant (Actual_Discr))
12780 then
12781 Error_Msg_NE
12782 ("discriminant & does not correspond "
12783 & "to ancestor discriminant", Actual, Actual_Discr);
12784 Abandon_Instantiation (Actual);
12785 end if;
12787 Next_Discriminant (Actual_Discr);
12788 Next_Discriminant (Ancestor_Discr);
12789 end loop;
12791 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
12792 Error_Msg_NE
12793 ("actual for & must have same number of discriminants",
12794 Actual, Gen_T);
12795 Abandon_Instantiation (Actual);
12796 end if;
12798 -- This case should be caught by the earlier check for
12799 -- constrainedness, but the check here is added for completeness.
12801 elsif Has_Discriminants (Act_T)
12802 and then not Has_Unknown_Discriminants (Act_T)
12803 then
12804 Error_Msg_NE
12805 ("actual for & must not have discriminants", Actual, Gen_T);
12806 Abandon_Instantiation (Actual);
12808 elsif Has_Discriminants (Ancestor) then
12809 Error_Msg_NE
12810 ("actual for & must have known discriminants", Actual, Gen_T);
12811 Abandon_Instantiation (Actual);
12812 end if;
12814 if not Subtypes_Statically_Compatible
12815 (Act_T, Ancestor, Formal_Derived_Matching => True)
12816 then
12817 Error_Msg_N
12818 ("constraint on actual is incompatible with formal", Actual);
12819 Abandon_Instantiation (Actual);
12820 end if;
12821 end if;
12823 -- If the formal and actual types are abstract, check that there
12824 -- are no abstract primitives of the actual type that correspond to
12825 -- nonabstract primitives of the formal type (second sentence of
12826 -- RM95 3.9.3(9)).
12828 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
12829 Check_Abstract_Primitives : declare
12830 Gen_Prims : constant Elist_Id :=
12831 Primitive_Operations (A_Gen_T);
12832 Gen_Elmt : Elmt_Id;
12833 Gen_Subp : Entity_Id;
12834 Anc_Subp : Entity_Id;
12835 Anc_Formal : Entity_Id;
12836 Anc_F_Type : Entity_Id;
12838 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
12839 Act_Elmt : Elmt_Id;
12840 Act_Subp : Entity_Id;
12841 Act_Formal : Entity_Id;
12842 Act_F_Type : Entity_Id;
12844 Subprograms_Correspond : Boolean;
12846 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
12847 -- Returns true if T2 is derived directly or indirectly from
12848 -- T1, including derivations from interfaces. T1 and T2 are
12849 -- required to be specific tagged base types.
12851 ------------------------
12852 -- Is_Tagged_Ancestor --
12853 ------------------------
12855 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
12857 Intfc_Elmt : Elmt_Id;
12859 begin
12860 -- The predicate is satisfied if the types are the same
12862 if T1 = T2 then
12863 return True;
12865 -- If we've reached the top of the derivation chain then
12866 -- we know that T1 is not an ancestor of T2.
12868 elsif Etype (T2) = T2 then
12869 return False;
12871 -- Proceed to check T2's immediate parent
12873 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
12874 return True;
12876 -- Finally, check to see if T1 is an ancestor of any of T2's
12877 -- progenitors.
12879 else
12880 Intfc_Elmt := First_Elmt (Interfaces (T2));
12881 while Present (Intfc_Elmt) loop
12882 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
12883 return True;
12884 end if;
12886 Next_Elmt (Intfc_Elmt);
12887 end loop;
12888 end if;
12890 return False;
12891 end Is_Tagged_Ancestor;
12893 -- Start of processing for Check_Abstract_Primitives
12895 begin
12896 -- Loop over all of the formal derived type's primitives
12898 Gen_Elmt := First_Elmt (Gen_Prims);
12899 while Present (Gen_Elmt) loop
12900 Gen_Subp := Node (Gen_Elmt);
12902 -- If the primitive of the formal is not abstract, then
12903 -- determine whether there is a corresponding primitive of
12904 -- the actual type that's abstract.
12906 if not Is_Abstract_Subprogram (Gen_Subp) then
12907 Act_Elmt := First_Elmt (Act_Prims);
12908 while Present (Act_Elmt) loop
12909 Act_Subp := Node (Act_Elmt);
12911 -- If we find an abstract primitive of the actual,
12912 -- then we need to test whether it corresponds to the
12913 -- subprogram from which the generic formal primitive
12914 -- is inherited.
12916 if Is_Abstract_Subprogram (Act_Subp) then
12917 Anc_Subp := Alias (Gen_Subp);
12919 -- Test whether we have a corresponding primitive
12920 -- by comparing names, kinds, formal types, and
12921 -- result types.
12923 if Chars (Anc_Subp) = Chars (Act_Subp)
12924 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
12925 then
12926 Anc_Formal := First_Formal (Anc_Subp);
12927 Act_Formal := First_Formal (Act_Subp);
12928 while Present (Anc_Formal)
12929 and then Present (Act_Formal)
12930 loop
12931 Anc_F_Type := Etype (Anc_Formal);
12932 Act_F_Type := Etype (Act_Formal);
12934 if Ekind (Anc_F_Type) =
12935 E_Anonymous_Access_Type
12936 then
12937 Anc_F_Type := Designated_Type (Anc_F_Type);
12939 if Ekind (Act_F_Type) =
12940 E_Anonymous_Access_Type
12941 then
12942 Act_F_Type :=
12943 Designated_Type (Act_F_Type);
12944 else
12945 exit;
12946 end if;
12948 elsif
12949 Ekind (Act_F_Type) = E_Anonymous_Access_Type
12950 then
12951 exit;
12952 end if;
12954 Anc_F_Type := Base_Type (Anc_F_Type);
12955 Act_F_Type := Base_Type (Act_F_Type);
12957 -- If the formal is controlling, then the
12958 -- the type of the actual primitive's formal
12959 -- must be derived directly or indirectly
12960 -- from the type of the ancestor primitive's
12961 -- formal.
12963 if Is_Controlling_Formal (Anc_Formal) then
12964 if not Is_Tagged_Ancestor
12965 (Anc_F_Type, Act_F_Type)
12966 then
12967 exit;
12968 end if;
12970 -- Otherwise the types of the formals must
12971 -- be the same.
12973 elsif Anc_F_Type /= Act_F_Type then
12974 exit;
12975 end if;
12977 Next_Entity (Anc_Formal);
12978 Next_Entity (Act_Formal);
12979 end loop;
12981 -- If we traversed through all of the formals
12982 -- then so far the subprograms correspond, so
12983 -- now check that any result types correspond.
12985 if No (Anc_Formal) and then No (Act_Formal) then
12986 Subprograms_Correspond := True;
12988 if Ekind (Act_Subp) = E_Function then
12989 Anc_F_Type := Etype (Anc_Subp);
12990 Act_F_Type := Etype (Act_Subp);
12992 if Ekind (Anc_F_Type) =
12993 E_Anonymous_Access_Type
12994 then
12995 Anc_F_Type :=
12996 Designated_Type (Anc_F_Type);
12998 if Ekind (Act_F_Type) =
12999 E_Anonymous_Access_Type
13000 then
13001 Act_F_Type :=
13002 Designated_Type (Act_F_Type);
13003 else
13004 Subprograms_Correspond := False;
13005 end if;
13007 elsif
13008 Ekind (Act_F_Type)
13009 = E_Anonymous_Access_Type
13010 then
13011 Subprograms_Correspond := False;
13012 end if;
13014 Anc_F_Type := Base_Type (Anc_F_Type);
13015 Act_F_Type := Base_Type (Act_F_Type);
13017 -- Now either the result types must be
13018 -- the same or, if the result type is
13019 -- controlling, the result type of the
13020 -- actual primitive must descend from the
13021 -- result type of the ancestor primitive.
13023 if Subprograms_Correspond
13024 and then Anc_F_Type /= Act_F_Type
13025 and then
13026 Has_Controlling_Result (Anc_Subp)
13027 and then not Is_Tagged_Ancestor
13028 (Anc_F_Type, Act_F_Type)
13029 then
13030 Subprograms_Correspond := False;
13031 end if;
13032 end if;
13034 -- Found a matching subprogram belonging to
13035 -- formal ancestor type, so actual subprogram
13036 -- corresponds and this violates 3.9.3(9).
13038 if Subprograms_Correspond then
13039 Error_Msg_NE
13040 ("abstract subprogram & overrides "
13041 & "nonabstract subprogram of ancestor",
13042 Actual, Act_Subp);
13043 end if;
13044 end if;
13045 end if;
13046 end if;
13048 Next_Elmt (Act_Elmt);
13049 end loop;
13050 end if;
13052 Next_Elmt (Gen_Elmt);
13053 end loop;
13054 end Check_Abstract_Primitives;
13055 end if;
13057 -- Verify that limitedness matches. If parent is a limited
13058 -- interface then the generic formal is not unless declared
13059 -- explicitly so. If not declared limited, the actual cannot be
13060 -- limited (see AI05-0087).
13062 -- Even though this AI is a binding interpretation, we enable the
13063 -- check only in Ada 2012 mode, because this improper construct
13064 -- shows up in user code and in existing B-tests.
13066 if Is_Limited_Type (Act_T)
13067 and then not Is_Limited_Type (A_Gen_T)
13068 and then Ada_Version >= Ada_2012
13069 then
13070 if In_Instance then
13071 null;
13072 else
13073 Error_Msg_NE
13074 ("actual for non-limited & cannot be a limited type",
13075 Actual, Gen_T);
13076 Explain_Limited_Type (Act_T, Actual);
13077 Abandon_Instantiation (Actual);
13078 end if;
13079 end if;
13080 end Validate_Derived_Type_Instance;
13082 ----------------------------------------
13083 -- Validate_Discriminated_Formal_Type --
13084 ----------------------------------------
13086 procedure Validate_Discriminated_Formal_Type is
13087 Formal_Discr : Entity_Id;
13088 Actual_Discr : Entity_Id;
13089 Formal_Subt : Entity_Id;
13091 begin
13092 if Has_Discriminants (A_Gen_T) then
13093 if not Has_Discriminants (Act_T) then
13094 Error_Msg_NE
13095 ("actual for & must have discriminants", Actual, Gen_T);
13096 Abandon_Instantiation (Actual);
13098 elsif Is_Constrained (Act_T) then
13099 Error_Msg_NE
13100 ("actual for & must be unconstrained", Actual, Gen_T);
13101 Abandon_Instantiation (Actual);
13103 else
13104 Formal_Discr := First_Discriminant (A_Gen_T);
13105 Actual_Discr := First_Discriminant (Act_T);
13106 while Formal_Discr /= Empty loop
13107 if Actual_Discr = Empty then
13108 Error_Msg_NE
13109 ("discriminants on actual do not match formal",
13110 Actual, Gen_T);
13111 Abandon_Instantiation (Actual);
13112 end if;
13114 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
13116 -- Access discriminants match if designated types do
13118 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
13119 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
13120 E_Anonymous_Access_Type
13121 and then
13122 Get_Instance_Of
13123 (Designated_Type (Base_Type (Formal_Subt))) =
13124 Designated_Type (Base_Type (Etype (Actual_Discr)))
13125 then
13126 null;
13128 elsif Base_Type (Formal_Subt) /=
13129 Base_Type (Etype (Actual_Discr))
13130 then
13131 Error_Msg_NE
13132 ("types of actual discriminants must match formal",
13133 Actual, Gen_T);
13134 Abandon_Instantiation (Actual);
13136 elsif not Subtypes_Statically_Match
13137 (Formal_Subt, Etype (Actual_Discr))
13138 and then Ada_Version >= Ada_95
13139 then
13140 Error_Msg_NE
13141 ("subtypes of actual discriminants must match formal",
13142 Actual, Gen_T);
13143 Abandon_Instantiation (Actual);
13144 end if;
13146 Next_Discriminant (Formal_Discr);
13147 Next_Discriminant (Actual_Discr);
13148 end loop;
13150 if Actual_Discr /= Empty then
13151 Error_Msg_NE
13152 ("discriminants on actual do not match formal",
13153 Actual, Gen_T);
13154 Abandon_Instantiation (Actual);
13155 end if;
13156 end if;
13157 end if;
13158 end Validate_Discriminated_Formal_Type;
13160 ---------------------------------------
13161 -- Validate_Incomplete_Type_Instance --
13162 ---------------------------------------
13164 procedure Validate_Incomplete_Type_Instance is
13165 begin
13166 if not Is_Tagged_Type (Act_T)
13167 and then Is_Tagged_Type (A_Gen_T)
13168 then
13169 Error_Msg_NE
13170 ("actual for & must be a tagged type", Actual, Gen_T);
13171 end if;
13173 Validate_Discriminated_Formal_Type;
13174 end Validate_Incomplete_Type_Instance;
13176 --------------------------------------
13177 -- Validate_Interface_Type_Instance --
13178 --------------------------------------
13180 procedure Validate_Interface_Type_Instance is
13181 begin
13182 if not Is_Interface (Act_T) then
13183 Error_Msg_NE
13184 ("actual for formal interface type must be an interface",
13185 Actual, Gen_T);
13187 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
13188 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
13189 or else Is_Protected_Interface (A_Gen_T) /=
13190 Is_Protected_Interface (Act_T)
13191 or else Is_Synchronized_Interface (A_Gen_T) /=
13192 Is_Synchronized_Interface (Act_T)
13193 then
13194 Error_Msg_NE
13195 ("actual for interface& does not match (RM 12.5.5(4))",
13196 Actual, Gen_T);
13197 end if;
13198 end Validate_Interface_Type_Instance;
13200 ------------------------------------
13201 -- Validate_Private_Type_Instance --
13202 ------------------------------------
13204 procedure Validate_Private_Type_Instance is
13205 begin
13206 if Is_Limited_Type (Act_T)
13207 and then not Is_Limited_Type (A_Gen_T)
13208 then
13209 if In_Instance then
13210 null;
13211 else
13212 Error_Msg_NE
13213 ("actual for non-limited & cannot be a limited type", Actual,
13214 Gen_T);
13215 Explain_Limited_Type (Act_T, Actual);
13216 Abandon_Instantiation (Actual);
13217 end if;
13219 elsif Known_To_Have_Preelab_Init (A_Gen_T)
13220 and then not Has_Preelaborable_Initialization (Act_T)
13221 then
13222 Error_Msg_NE
13223 ("actual for & must have preelaborable initialization", Actual,
13224 Gen_T);
13226 elsif not Is_Definite_Subtype (Act_T)
13227 and then Is_Definite_Subtype (A_Gen_T)
13228 and then Ada_Version >= Ada_95
13229 then
13230 Error_Msg_NE
13231 ("actual for & must be a definite subtype", Actual, Gen_T);
13233 elsif not Is_Tagged_Type (Act_T)
13234 and then Is_Tagged_Type (A_Gen_T)
13235 then
13236 Error_Msg_NE
13237 ("actual for & must be a tagged type", Actual, Gen_T);
13238 end if;
13240 Validate_Discriminated_Formal_Type;
13241 Ancestor := Gen_T;
13242 end Validate_Private_Type_Instance;
13244 -- Start of processing for Instantiate_Type
13246 begin
13247 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
13248 Error_Msg_N ("duplicate instantiation of generic type", Actual);
13249 return New_List (Error);
13251 elsif not Is_Entity_Name (Actual)
13252 or else not Is_Type (Entity (Actual))
13253 then
13254 Error_Msg_NE
13255 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
13256 Abandon_Instantiation (Actual);
13258 else
13259 Act_T := Entity (Actual);
13261 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
13262 -- as a generic actual parameter if the corresponding formal type
13263 -- does not have a known_discriminant_part, or is a formal derived
13264 -- type that is an Unchecked_Union type.
13266 if Is_Unchecked_Union (Base_Type (Act_T)) then
13267 if not Has_Discriminants (A_Gen_T)
13268 or else (Is_Derived_Type (A_Gen_T)
13269 and then Is_Unchecked_Union (A_Gen_T))
13270 then
13271 null;
13272 else
13273 Error_Msg_N ("unchecked union cannot be the actual for a "
13274 & "discriminated formal type", Act_T);
13276 end if;
13277 end if;
13279 -- Deal with fixed/floating restrictions
13281 if Is_Floating_Point_Type (Act_T) then
13282 Check_Restriction (No_Floating_Point, Actual);
13283 elsif Is_Fixed_Point_Type (Act_T) then
13284 Check_Restriction (No_Fixed_Point, Actual);
13285 end if;
13287 -- Deal with error of using incomplete type as generic actual.
13288 -- This includes limited views of a type, even if the non-limited
13289 -- view may be available.
13291 if Ekind (Act_T) = E_Incomplete_Type
13292 or else (Is_Class_Wide_Type (Act_T)
13293 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
13294 then
13295 -- If the formal is an incomplete type, the actual can be
13296 -- incomplete as well.
13298 if Ekind (A_Gen_T) = E_Incomplete_Type then
13299 null;
13301 elsif Is_Class_Wide_Type (Act_T)
13302 or else No (Full_View (Act_T))
13303 then
13304 Error_Msg_N ("premature use of incomplete type", Actual);
13305 Abandon_Instantiation (Actual);
13306 else
13307 Act_T := Full_View (Act_T);
13308 Set_Entity (Actual, Act_T);
13310 if Has_Private_Component (Act_T) then
13311 Error_Msg_N
13312 ("premature use of type with private component", Actual);
13313 end if;
13314 end if;
13316 -- Deal with error of premature use of private type as generic actual
13318 elsif Is_Private_Type (Act_T)
13319 and then Is_Private_Type (Base_Type (Act_T))
13320 and then not Is_Generic_Type (Act_T)
13321 and then not Is_Derived_Type (Act_T)
13322 and then No (Full_View (Root_Type (Act_T)))
13323 then
13324 -- If the formal is an incomplete type, the actual can be
13325 -- private or incomplete as well.
13327 if Ekind (A_Gen_T) = E_Incomplete_Type then
13328 null;
13329 else
13330 Error_Msg_N ("premature use of private type", Actual);
13331 end if;
13333 elsif Has_Private_Component (Act_T) then
13334 Error_Msg_N
13335 ("premature use of type with private component", Actual);
13336 end if;
13338 Set_Instance_Of (A_Gen_T, Act_T);
13340 -- If the type is generic, the class-wide type may also be used
13342 if Is_Tagged_Type (A_Gen_T)
13343 and then Is_Tagged_Type (Act_T)
13344 and then not Is_Class_Wide_Type (A_Gen_T)
13345 then
13346 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
13347 Class_Wide_Type (Act_T));
13348 end if;
13350 if not Is_Abstract_Type (A_Gen_T)
13351 and then Is_Abstract_Type (Act_T)
13352 then
13353 Error_Msg_N
13354 ("actual of non-abstract formal cannot be abstract", Actual);
13355 end if;
13357 -- A generic scalar type is a first subtype for which we generate
13358 -- an anonymous base type. Indicate that the instance of this base
13359 -- is the base type of the actual.
13361 if Is_Scalar_Type (A_Gen_T) then
13362 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
13363 end if;
13364 end if;
13366 if Error_Posted (Act_T) then
13367 null;
13368 else
13369 case Nkind (Def) is
13370 when N_Formal_Private_Type_Definition =>
13371 Validate_Private_Type_Instance;
13373 when N_Formal_Incomplete_Type_Definition =>
13374 Validate_Incomplete_Type_Instance;
13376 when N_Formal_Derived_Type_Definition =>
13377 Validate_Derived_Type_Instance;
13379 when N_Formal_Discrete_Type_Definition =>
13380 if not Is_Discrete_Type (Act_T) then
13381 Error_Msg_NE
13382 ("expect discrete type in instantiation of&",
13383 Actual, Gen_T);
13384 Abandon_Instantiation (Actual);
13385 end if;
13387 Diagnose_Predicated_Actual;
13389 when N_Formal_Signed_Integer_Type_Definition =>
13390 if not Is_Signed_Integer_Type (Act_T) then
13391 Error_Msg_NE
13392 ("expect signed integer type in instantiation of&",
13393 Actual, Gen_T);
13394 Abandon_Instantiation (Actual);
13395 end if;
13397 Diagnose_Predicated_Actual;
13399 when N_Formal_Modular_Type_Definition =>
13400 if not Is_Modular_Integer_Type (Act_T) then
13401 Error_Msg_NE
13402 ("expect modular type in instantiation of &",
13403 Actual, Gen_T);
13404 Abandon_Instantiation (Actual);
13405 end if;
13407 Diagnose_Predicated_Actual;
13409 when N_Formal_Floating_Point_Definition =>
13410 if not Is_Floating_Point_Type (Act_T) then
13411 Error_Msg_NE
13412 ("expect float type in instantiation of &", Actual, Gen_T);
13413 Abandon_Instantiation (Actual);
13414 end if;
13416 when N_Formal_Ordinary_Fixed_Point_Definition =>
13417 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
13418 Error_Msg_NE
13419 ("expect ordinary fixed point type in instantiation of &",
13420 Actual, Gen_T);
13421 Abandon_Instantiation (Actual);
13422 end if;
13424 when N_Formal_Decimal_Fixed_Point_Definition =>
13425 if not Is_Decimal_Fixed_Point_Type (Act_T) then
13426 Error_Msg_NE
13427 ("expect decimal type in instantiation of &",
13428 Actual, Gen_T);
13429 Abandon_Instantiation (Actual);
13430 end if;
13432 when N_Array_Type_Definition =>
13433 Validate_Array_Type_Instance;
13435 when N_Access_To_Object_Definition =>
13436 Validate_Access_Type_Instance;
13438 when N_Access_Function_Definition
13439 | N_Access_Procedure_Definition
13441 Validate_Access_Subprogram_Instance;
13443 when N_Record_Definition =>
13444 Validate_Interface_Type_Instance;
13446 when N_Derived_Type_Definition =>
13447 Validate_Derived_Interface_Type_Instance;
13449 when others =>
13450 raise Program_Error;
13451 end case;
13452 end if;
13454 Subt := New_Copy (Gen_T);
13456 -- Use adjusted sloc of subtype name as the location for other nodes in
13457 -- the subtype declaration.
13459 Loc := Sloc (Subt);
13461 Decl_Node :=
13462 Make_Subtype_Declaration (Loc,
13463 Defining_Identifier => Subt,
13464 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
13466 if Is_Private_Type (Act_T) then
13467 Set_Has_Private_View (Subtype_Indication (Decl_Node));
13469 elsif Is_Access_Type (Act_T)
13470 and then Is_Private_Type (Designated_Type (Act_T))
13471 then
13472 Set_Has_Private_View (Subtype_Indication (Decl_Node));
13473 end if;
13475 -- In Ada 2012 the actual may be a limited view. Indicate that
13476 -- the local subtype must be treated as such.
13478 if From_Limited_With (Act_T) then
13479 Set_Ekind (Subt, E_Incomplete_Subtype);
13480 Set_From_Limited_With (Subt);
13481 end if;
13483 Decl_Nodes := New_List (Decl_Node);
13485 -- Flag actual derived types so their elaboration produces the
13486 -- appropriate renamings for the primitive operations of the ancestor.
13487 -- Flag actual for formal private types as well, to determine whether
13488 -- operations in the private part may override inherited operations.
13489 -- If the formal has an interface list, the ancestor is not the
13490 -- parent, but the analyzed formal that includes the interface
13491 -- operations of all its progenitors.
13493 -- Same treatment for formal private types, so we can check whether the
13494 -- type is tagged limited when validating derivations in the private
13495 -- part. (See AI05-096).
13497 if Nkind (Def) = N_Formal_Derived_Type_Definition then
13498 if Present (Interface_List (Def)) then
13499 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
13500 else
13501 Set_Generic_Parent_Type (Decl_Node, Ancestor);
13502 end if;
13504 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
13505 N_Formal_Incomplete_Type_Definition)
13506 then
13507 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
13508 end if;
13510 -- If the actual is a synchronized type that implements an interface,
13511 -- the primitive operations are attached to the corresponding record,
13512 -- and we have to treat it as an additional generic actual, so that its
13513 -- primitive operations become visible in the instance. The task or
13514 -- protected type itself does not carry primitive operations.
13516 if Is_Concurrent_Type (Act_T)
13517 and then Is_Tagged_Type (Act_T)
13518 and then Present (Corresponding_Record_Type (Act_T))
13519 and then Present (Ancestor)
13520 and then Is_Interface (Ancestor)
13521 then
13522 declare
13523 Corr_Rec : constant Entity_Id :=
13524 Corresponding_Record_Type (Act_T);
13525 New_Corr : Entity_Id;
13526 Corr_Decl : Node_Id;
13528 begin
13529 New_Corr := Make_Temporary (Loc, 'S');
13530 Corr_Decl :=
13531 Make_Subtype_Declaration (Loc,
13532 Defining_Identifier => New_Corr,
13533 Subtype_Indication =>
13534 New_Occurrence_Of (Corr_Rec, Loc));
13535 Append_To (Decl_Nodes, Corr_Decl);
13537 if Ekind (Act_T) = E_Task_Type then
13538 Set_Ekind (Subt, E_Task_Subtype);
13539 else
13540 Set_Ekind (Subt, E_Protected_Subtype);
13541 end if;
13543 Set_Corresponding_Record_Type (Subt, Corr_Rec);
13544 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
13545 Set_Generic_Parent_Type (Decl_Node, Empty);
13546 end;
13547 end if;
13549 -- For a floating-point type, capture dimension info if any, because
13550 -- the generated subtype declaration does not come from source and
13551 -- will not process dimensions.
13553 if Is_Floating_Point_Type (Act_T) then
13554 Copy_Dimensions (Act_T, Subt);
13555 end if;
13557 return Decl_Nodes;
13558 end Instantiate_Type;
13560 ---------------------
13561 -- Is_In_Main_Unit --
13562 ---------------------
13564 function Is_In_Main_Unit (N : Node_Id) return Boolean is
13565 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
13566 Current_Unit : Node_Id;
13568 begin
13569 if Unum = Main_Unit then
13570 return True;
13572 -- If the current unit is a subunit then it is either the main unit or
13573 -- is being compiled as part of the main unit.
13575 elsif Nkind (N) = N_Compilation_Unit then
13576 return Nkind (Unit (N)) = N_Subunit;
13577 end if;
13579 Current_Unit := Parent (N);
13580 while Present (Current_Unit)
13581 and then Nkind (Current_Unit) /= N_Compilation_Unit
13582 loop
13583 Current_Unit := Parent (Current_Unit);
13584 end loop;
13586 -- The instantiation node is in the main unit, or else the current node
13587 -- (perhaps as the result of nested instantiations) is in the main unit,
13588 -- or in the declaration of the main unit, which in this last case must
13589 -- be a body.
13591 return
13592 Current_Unit = Cunit (Main_Unit)
13593 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
13594 or else (Present (Current_Unit)
13595 and then Present (Library_Unit (Current_Unit))
13596 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
13597 end Is_In_Main_Unit;
13599 ----------------------------
13600 -- Load_Parent_Of_Generic --
13601 ----------------------------
13603 procedure Load_Parent_Of_Generic
13604 (N : Node_Id;
13605 Spec : Node_Id;
13606 Body_Optional : Boolean := False)
13608 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
13609 Saved_Style_Check : constant Boolean := Style_Check;
13610 Saved_Warnings : constant Warning_Record := Save_Warnings;
13611 True_Parent : Node_Id;
13612 Inst_Node : Node_Id;
13613 OK : Boolean;
13614 Previous_Instances : constant Elist_Id := New_Elmt_List;
13616 procedure Collect_Previous_Instances (Decls : List_Id);
13617 -- Collect all instantiations in the given list of declarations, that
13618 -- precede the generic that we need to load. If the bodies of these
13619 -- instantiations are available, we must analyze them, to ensure that
13620 -- the public symbols generated are the same when the unit is compiled
13621 -- to generate code, and when it is compiled in the context of a unit
13622 -- that needs a particular nested instance. This process is applied to
13623 -- both package and subprogram instances.
13625 --------------------------------
13626 -- Collect_Previous_Instances --
13627 --------------------------------
13629 procedure Collect_Previous_Instances (Decls : List_Id) is
13630 Decl : Node_Id;
13632 begin
13633 Decl := First (Decls);
13634 while Present (Decl) loop
13635 if Sloc (Decl) >= Sloc (Inst_Node) then
13636 return;
13638 -- If Decl is an instantiation, then record it as requiring
13639 -- instantiation of the corresponding body, except if it is an
13640 -- abbreviated instantiation generated internally for conformance
13641 -- checking purposes only for the case of a formal package
13642 -- declared without a box (see Instantiate_Formal_Package). Such
13643 -- an instantiation does not generate any code (the actual code
13644 -- comes from actual) and thus does not need to be analyzed here.
13645 -- If the instantiation appears with a generic package body it is
13646 -- not analyzed here either.
13648 elsif Nkind (Decl) = N_Package_Instantiation
13649 and then not Is_Internal (Defining_Entity (Decl))
13650 then
13651 Append_Elmt (Decl, Previous_Instances);
13653 -- For a subprogram instantiation, omit instantiations intrinsic
13654 -- operations (Unchecked_Conversions, etc.) that have no bodies.
13656 elsif Nkind_In (Decl, N_Function_Instantiation,
13657 N_Procedure_Instantiation)
13658 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
13659 then
13660 Append_Elmt (Decl, Previous_Instances);
13662 elsif Nkind (Decl) = N_Package_Declaration then
13663 Collect_Previous_Instances
13664 (Visible_Declarations (Specification (Decl)));
13665 Collect_Previous_Instances
13666 (Private_Declarations (Specification (Decl)));
13668 -- Previous non-generic bodies may contain instances as well
13670 elsif Nkind (Decl) = N_Package_Body
13671 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
13672 then
13673 Collect_Previous_Instances (Declarations (Decl));
13675 elsif Nkind (Decl) = N_Subprogram_Body
13676 and then not Acts_As_Spec (Decl)
13677 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
13678 then
13679 Collect_Previous_Instances (Declarations (Decl));
13680 end if;
13682 Next (Decl);
13683 end loop;
13684 end Collect_Previous_Instances;
13686 -- Start of processing for Load_Parent_Of_Generic
13688 begin
13689 if not In_Same_Source_Unit (N, Spec)
13690 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
13691 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
13692 and then not Is_In_Main_Unit (Spec))
13693 then
13694 -- Find body of parent of spec, and analyze it. A special case arises
13695 -- when the parent is an instantiation, that is to say when we are
13696 -- currently instantiating a nested generic. In that case, there is
13697 -- no separate file for the body of the enclosing instance. Instead,
13698 -- the enclosing body must be instantiated as if it were a pending
13699 -- instantiation, in order to produce the body for the nested generic
13700 -- we require now. Note that in that case the generic may be defined
13701 -- in a package body, the instance defined in the same package body,
13702 -- and the original enclosing body may not be in the main unit.
13704 Inst_Node := Empty;
13706 True_Parent := Parent (Spec);
13707 while Present (True_Parent)
13708 and then Nkind (True_Parent) /= N_Compilation_Unit
13709 loop
13710 if Nkind (True_Parent) = N_Package_Declaration
13711 and then
13712 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
13713 then
13714 -- Parent is a compilation unit that is an instantiation.
13715 -- Instantiation node has been replaced with package decl.
13717 Inst_Node := Original_Node (True_Parent);
13718 exit;
13720 elsif Nkind (True_Parent) = N_Package_Declaration
13721 and then Present (Generic_Parent (Specification (True_Parent)))
13722 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13723 then
13724 -- Parent is an instantiation within another specification.
13725 -- Declaration for instance has been inserted before original
13726 -- instantiation node. A direct link would be preferable?
13728 Inst_Node := Next (True_Parent);
13729 while Present (Inst_Node)
13730 and then Nkind (Inst_Node) /= N_Package_Instantiation
13731 loop
13732 Next (Inst_Node);
13733 end loop;
13735 -- If the instance appears within a generic, and the generic
13736 -- unit is defined within a formal package of the enclosing
13737 -- generic, there is no generic body available, and none
13738 -- needed. A more precise test should be used ???
13740 if No (Inst_Node) then
13741 return;
13742 end if;
13744 exit;
13746 else
13747 True_Parent := Parent (True_Parent);
13748 end if;
13749 end loop;
13751 -- Case where we are currently instantiating a nested generic
13753 if Present (Inst_Node) then
13754 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
13756 -- Instantiation node and declaration of instantiated package
13757 -- were exchanged when only the declaration was needed.
13758 -- Restore instantiation node before proceeding with body.
13760 Set_Unit (Parent (True_Parent), Inst_Node);
13761 end if;
13763 -- Now complete instantiation of enclosing body, if it appears in
13764 -- some other unit. If it appears in the current unit, the body
13765 -- will have been instantiated already.
13767 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
13769 -- We need to determine the expander mode to instantiate the
13770 -- enclosing body. Because the generic body we need may use
13771 -- global entities declared in the enclosing package (including
13772 -- aggregates) it is in general necessary to compile this body
13773 -- with expansion enabled, except if we are within a generic
13774 -- package, in which case the usual generic rule applies.
13776 declare
13777 Exp_Status : Boolean := True;
13778 Scop : Entity_Id;
13780 begin
13781 -- Loop through scopes looking for generic package
13783 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
13784 while Present (Scop)
13785 and then Scop /= Standard_Standard
13786 loop
13787 if Ekind (Scop) = E_Generic_Package then
13788 Exp_Status := False;
13789 exit;
13790 end if;
13792 Scop := Scope (Scop);
13793 end loop;
13795 -- Collect previous instantiations in the unit that contains
13796 -- the desired generic.
13798 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13799 and then not Body_Optional
13800 then
13801 declare
13802 Decl : Elmt_Id;
13803 Info : Pending_Body_Info;
13804 Par : Node_Id;
13806 begin
13807 Par := Parent (Inst_Node);
13808 while Present (Par) loop
13809 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
13810 Par := Parent (Par);
13811 end loop;
13813 pragma Assert (Present (Par));
13815 if Nkind (Par) = N_Package_Body then
13816 Collect_Previous_Instances (Declarations (Par));
13818 elsif Nkind (Par) = N_Package_Declaration then
13819 Collect_Previous_Instances
13820 (Visible_Declarations (Specification (Par)));
13821 Collect_Previous_Instances
13822 (Private_Declarations (Specification (Par)));
13824 else
13825 -- Enclosing unit is a subprogram body. In this
13826 -- case all instance bodies are processed in order
13827 -- and there is no need to collect them separately.
13829 null;
13830 end if;
13832 Decl := First_Elmt (Previous_Instances);
13833 while Present (Decl) loop
13834 Info :=
13835 (Act_Decl =>
13836 Instance_Spec (Node (Decl)),
13837 Config_Switches => Save_Config_Switches,
13838 Current_Sem_Unit =>
13839 Get_Code_Unit (Sloc (Node (Decl))),
13840 Expander_Status => Exp_Status,
13841 Inst_Node => Node (Decl),
13842 Local_Suppress_Stack_Top =>
13843 Local_Suppress_Stack_Top,
13844 Scope_Suppress => Scope_Suppress,
13845 Warnings => Save_Warnings);
13847 -- Package instance
13849 if Nkind (Node (Decl)) = N_Package_Instantiation
13850 then
13851 Instantiate_Package_Body
13852 (Info, Body_Optional => True);
13854 -- Subprogram instance
13856 else
13857 -- The instance_spec is in the wrapper package,
13858 -- usually followed by its local renaming
13859 -- declaration. See Build_Subprogram_Renaming
13860 -- for details. If the instance carries aspects,
13861 -- these result in the corresponding pragmas,
13862 -- inserted after the subprogram declaration.
13863 -- They must be skipped as well when retrieving
13864 -- the desired spec. Some of them may have been
13865 -- rewritten as null statements.
13866 -- A direct link would be more robust ???
13868 declare
13869 Decl : Node_Id :=
13870 (Last (Visible_Declarations
13871 (Specification (Info.Act_Decl))));
13872 begin
13873 while Nkind_In (Decl,
13874 N_Null_Statement,
13875 N_Pragma,
13876 N_Subprogram_Renaming_Declaration)
13877 loop
13878 Decl := Prev (Decl);
13879 end loop;
13881 Info.Act_Decl := Decl;
13882 end;
13884 Instantiate_Subprogram_Body
13885 (Info, Body_Optional => True);
13886 end if;
13888 Next_Elmt (Decl);
13889 end loop;
13890 end;
13891 end if;
13893 Instantiate_Package_Body
13894 (Body_Info =>
13895 ((Act_Decl => True_Parent,
13896 Config_Switches => Save_Config_Switches,
13897 Current_Sem_Unit =>
13898 Get_Code_Unit (Sloc (Inst_Node)),
13899 Expander_Status => Exp_Status,
13900 Inst_Node => Inst_Node,
13901 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
13902 Scope_Suppress => Scope_Suppress,
13903 Warnings => Save_Warnings)),
13904 Body_Optional => Body_Optional);
13905 end;
13906 end if;
13908 -- Case where we are not instantiating a nested generic
13910 else
13911 Opt.Style_Check := False;
13912 Expander_Mode_Save_And_Set (True);
13913 Load_Needed_Body (Comp_Unit, OK);
13914 Opt.Style_Check := Saved_Style_Check;
13915 Restore_Warnings (Saved_Warnings);
13916 Expander_Mode_Restore;
13918 if not OK
13919 and then Unit_Requires_Body (Defining_Entity (Spec))
13920 and then not Body_Optional
13921 then
13922 declare
13923 Bname : constant Unit_Name_Type :=
13924 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
13926 begin
13927 -- In CodePeer mode, the missing body may make the analysis
13928 -- incomplete, but we do not treat it as fatal.
13930 if CodePeer_Mode then
13931 return;
13933 else
13934 Error_Msg_Unit_1 := Bname;
13935 Error_Msg_N ("this instantiation requires$!", N);
13936 Error_Msg_File_1 :=
13937 Get_File_Name (Bname, Subunit => False);
13938 Error_Msg_N ("\but file{ was not found!", N);
13939 raise Unrecoverable_Error;
13940 end if;
13941 end;
13942 end if;
13943 end if;
13944 end if;
13946 -- If loading parent of the generic caused an instantiation circularity,
13947 -- we abandon compilation at this point, because otherwise in some cases
13948 -- we get into trouble with infinite recursions after this point.
13950 if Circularity_Detected then
13951 raise Unrecoverable_Error;
13952 end if;
13953 end Load_Parent_Of_Generic;
13955 ---------------------------------
13956 -- Map_Formal_Package_Entities --
13957 ---------------------------------
13959 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
13960 E1 : Entity_Id;
13961 E2 : Entity_Id;
13963 begin
13964 Set_Instance_Of (Form, Act);
13966 -- Traverse formal and actual package to map the corresponding entities.
13967 -- We skip over internal entities that may be generated during semantic
13968 -- analysis, and find the matching entities by name, given that they
13969 -- must appear in the same order.
13971 E1 := First_Entity (Form);
13972 E2 := First_Entity (Act);
13973 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
13974 -- Could this test be a single condition??? Seems like it could, and
13975 -- isn't FPE (Form) a constant anyway???
13977 if not Is_Internal (E1)
13978 and then Present (Parent (E1))
13979 and then not Is_Class_Wide_Type (E1)
13980 and then not Is_Internal_Name (Chars (E1))
13981 then
13982 while Present (E2) and then Chars (E2) /= Chars (E1) loop
13983 Next_Entity (E2);
13984 end loop;
13986 if No (E2) then
13987 exit;
13988 else
13989 Set_Instance_Of (E1, E2);
13991 if Is_Type (E1) and then Is_Tagged_Type (E2) then
13992 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
13993 end if;
13995 if Is_Constrained (E1) then
13996 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
13997 end if;
13999 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
14000 Map_Formal_Package_Entities (E1, E2);
14001 end if;
14002 end if;
14003 end if;
14005 Next_Entity (E1);
14006 end loop;
14007 end Map_Formal_Package_Entities;
14009 -----------------------
14010 -- Move_Freeze_Nodes --
14011 -----------------------
14013 procedure Move_Freeze_Nodes
14014 (Out_Of : Entity_Id;
14015 After : Node_Id;
14016 L : List_Id)
14018 Decl : Node_Id;
14019 Next_Decl : Node_Id;
14020 Next_Node : Node_Id := After;
14021 Spec : Node_Id;
14023 function Is_Outer_Type (T : Entity_Id) return Boolean;
14024 -- Check whether entity is declared in a scope external to that of the
14025 -- generic unit.
14027 -------------------
14028 -- Is_Outer_Type --
14029 -------------------
14031 function Is_Outer_Type (T : Entity_Id) return Boolean is
14032 Scop : Entity_Id := Scope (T);
14034 begin
14035 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
14036 return True;
14038 else
14039 while Scop /= Standard_Standard loop
14040 if Scop = Out_Of then
14041 return False;
14042 else
14043 Scop := Scope (Scop);
14044 end if;
14045 end loop;
14047 return True;
14048 end if;
14049 end Is_Outer_Type;
14051 -- Start of processing for Move_Freeze_Nodes
14053 begin
14054 if No (L) then
14055 return;
14056 end if;
14058 -- First remove the freeze nodes that may appear before all other
14059 -- declarations.
14061 Decl := First (L);
14062 while Present (Decl)
14063 and then Nkind (Decl) = N_Freeze_Entity
14064 and then Is_Outer_Type (Entity (Decl))
14065 loop
14066 Decl := Remove_Head (L);
14067 Insert_After (Next_Node, Decl);
14068 Set_Analyzed (Decl, False);
14069 Next_Node := Decl;
14070 Decl := First (L);
14071 end loop;
14073 -- Next scan the list of declarations and remove each freeze node that
14074 -- appears ahead of the current node.
14076 while Present (Decl) loop
14077 while Present (Next (Decl))
14078 and then Nkind (Next (Decl)) = N_Freeze_Entity
14079 and then Is_Outer_Type (Entity (Next (Decl)))
14080 loop
14081 Next_Decl := Remove_Next (Decl);
14082 Insert_After (Next_Node, Next_Decl);
14083 Set_Analyzed (Next_Decl, False);
14084 Next_Node := Next_Decl;
14085 end loop;
14087 -- If the declaration is a nested package or concurrent type, then
14088 -- recurse. Nested generic packages will have been processed from the
14089 -- inside out.
14091 case Nkind (Decl) is
14092 when N_Package_Declaration =>
14093 Spec := Specification (Decl);
14095 when N_Task_Type_Declaration =>
14096 Spec := Task_Definition (Decl);
14098 when N_Protected_Type_Declaration =>
14099 Spec := Protected_Definition (Decl);
14101 when others =>
14102 Spec := Empty;
14103 end case;
14105 if Present (Spec) then
14106 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
14107 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
14108 end if;
14110 Next (Decl);
14111 end loop;
14112 end Move_Freeze_Nodes;
14114 ----------------
14115 -- Next_Assoc --
14116 ----------------
14118 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
14119 begin
14120 return Generic_Renamings.Table (E).Next_In_HTable;
14121 end Next_Assoc;
14123 ------------------------
14124 -- Preanalyze_Actuals --
14125 ------------------------
14127 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
14128 procedure Perform_Appropriate_Analysis (N : Node_Id);
14129 -- Determine if the actuals we are analyzing come from a generic
14130 -- instantiation that is a library unit and dispatch accordingly.
14132 ----------------------------------
14133 -- Perform_Appropriate_Analysis --
14134 ----------------------------------
14136 procedure Perform_Appropriate_Analysis (N : Node_Id) is
14137 begin
14138 -- When we have a library instantiation we cannot allow any expansion
14139 -- to occur, since there may be no place to put it. Instead, in that
14140 -- case we perform a preanalysis of the actual.
14142 if Present (Inst) and then Is_Compilation_Unit (Inst) then
14143 Preanalyze (N);
14144 else
14145 Analyze (N);
14146 end if;
14147 end Perform_Appropriate_Analysis;
14149 -- Local variables
14151 Errs : constant Nat := Serious_Errors_Detected;
14153 Assoc : Node_Id;
14154 Act : Node_Id;
14156 Cur : Entity_Id := Empty;
14157 -- Current homograph of the instance name
14159 Vis : Boolean := False;
14160 -- Saved visibility status of the current homograph
14162 -- Start of processing for Preanalyze_Actuals
14164 begin
14165 Assoc := First (Generic_Associations (N));
14167 -- If the instance is a child unit, its name may hide an outer homonym,
14168 -- so make it invisible to perform name resolution on the actuals.
14170 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
14171 and then Present
14172 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
14173 then
14174 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
14176 if Is_Compilation_Unit (Cur) then
14177 Vis := Is_Immediately_Visible (Cur);
14178 Set_Is_Immediately_Visible (Cur, False);
14179 else
14180 Cur := Empty;
14181 end if;
14182 end if;
14184 while Present (Assoc) loop
14185 if Nkind (Assoc) /= N_Others_Choice then
14186 Act := Explicit_Generic_Actual_Parameter (Assoc);
14188 -- Within a nested instantiation, a defaulted actual is an empty
14189 -- association, so nothing to analyze. If the subprogram actual
14190 -- is an attribute, analyze prefix only, because actual is not a
14191 -- complete attribute reference.
14193 -- If actual is an allocator, analyze expression only. The full
14194 -- analysis can generate code, and if instance is a compilation
14195 -- unit we have to wait until the package instance is installed
14196 -- to have a proper place to insert this code.
14198 -- String literals may be operators, but at this point we do not
14199 -- know whether the actual is a formal subprogram or a string.
14201 if No (Act) then
14202 null;
14204 elsif Nkind (Act) = N_Attribute_Reference then
14205 Perform_Appropriate_Analysis (Prefix (Act));
14207 elsif Nkind (Act) = N_Explicit_Dereference then
14208 Perform_Appropriate_Analysis (Prefix (Act));
14210 elsif Nkind (Act) = N_Allocator then
14211 declare
14212 Expr : constant Node_Id := Expression (Act);
14214 begin
14215 if Nkind (Expr) = N_Subtype_Indication then
14216 Perform_Appropriate_Analysis (Subtype_Mark (Expr));
14218 -- Analyze separately each discriminant constraint, when
14219 -- given with a named association.
14221 declare
14222 Constr : Node_Id;
14224 begin
14225 Constr := First (Constraints (Constraint (Expr)));
14226 while Present (Constr) loop
14227 if Nkind (Constr) = N_Discriminant_Association then
14228 Perform_Appropriate_Analysis
14229 (Expression (Constr));
14230 else
14231 Perform_Appropriate_Analysis (Constr);
14232 end if;
14234 Next (Constr);
14235 end loop;
14236 end;
14238 else
14239 Perform_Appropriate_Analysis (Expr);
14240 end if;
14241 end;
14243 elsif Nkind (Act) /= N_Operator_Symbol then
14244 Perform_Appropriate_Analysis (Act);
14246 -- Within a package instance, mark actuals that are limited
14247 -- views, so their use can be moved to the body of the
14248 -- enclosing unit.
14250 if Is_Entity_Name (Act)
14251 and then Is_Type (Entity (Act))
14252 and then From_Limited_With (Entity (Act))
14253 and then Present (Inst)
14254 then
14255 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
14256 end if;
14257 end if;
14259 if Errs /= Serious_Errors_Detected then
14261 -- Do a minimal analysis of the generic, to prevent spurious
14262 -- warnings complaining about the generic being unreferenced,
14263 -- before abandoning the instantiation.
14265 Perform_Appropriate_Analysis (Name (N));
14267 if Is_Entity_Name (Name (N))
14268 and then Etype (Name (N)) /= Any_Type
14269 then
14270 Generate_Reference (Entity (Name (N)), Name (N));
14271 Set_Is_Instantiated (Entity (Name (N)));
14272 end if;
14274 if Present (Cur) then
14276 -- For the case of a child instance hiding an outer homonym,
14277 -- provide additional warning which might explain the error.
14279 Set_Is_Immediately_Visible (Cur, Vis);
14280 Error_Msg_NE
14281 ("& hides outer unit with the same name??",
14282 N, Defining_Unit_Name (N));
14283 end if;
14285 Abandon_Instantiation (Act);
14286 end if;
14287 end if;
14289 Next (Assoc);
14290 end loop;
14292 if Present (Cur) then
14293 Set_Is_Immediately_Visible (Cur, Vis);
14294 end if;
14295 end Preanalyze_Actuals;
14297 -------------------------------
14298 -- Provide_Completing_Bodies --
14299 -------------------------------
14301 procedure Provide_Completing_Bodies (N : Node_Id) is
14302 procedure Build_Completing_Body (Subp_Decl : Node_Id);
14303 -- Generate the completing body for subprogram declaration Subp_Decl
14305 procedure Provide_Completing_Bodies_In (Decls : List_Id);
14306 -- Generating completing bodies for all subprograms found in declarative
14307 -- list Decls.
14309 ---------------------------
14310 -- Build_Completing_Body --
14311 ---------------------------
14313 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
14314 Loc : constant Source_Ptr := Sloc (Subp_Decl);
14315 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
14316 Spec : Node_Id;
14318 begin
14319 -- Nothing to do if the subprogram already has a completing body
14321 if Present (Corresponding_Body (Subp_Decl)) then
14322 return;
14324 -- Mark the function as having a valid return statement even though
14325 -- the body contains a single raise statement.
14327 elsif Ekind (Subp_Id) = E_Function then
14328 Set_Return_Present (Subp_Id);
14329 end if;
14331 -- Clone the specification to obtain new entities and reset the only
14332 -- semantic field.
14334 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
14335 Set_Generic_Parent (Spec, Empty);
14337 -- Generate:
14338 -- function Func ... return ... is
14339 -- <or>
14340 -- procedure Proc ... is
14341 -- begin
14342 -- raise Program_Error with "access before elaboration";
14343 -- edn Proc;
14345 Insert_After_And_Analyze (Subp_Decl,
14346 Make_Subprogram_Body (Loc,
14347 Specification => Spec,
14348 Declarations => New_List,
14349 Handled_Statement_Sequence =>
14350 Make_Handled_Sequence_Of_Statements (Loc,
14351 Statements => New_List (
14352 Make_Raise_Program_Error (Loc,
14353 Reason => PE_Access_Before_Elaboration)))));
14354 end Build_Completing_Body;
14356 ----------------------------------
14357 -- Provide_Completing_Bodies_In --
14358 ----------------------------------
14360 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
14361 Decl : Node_Id;
14363 begin
14364 if Present (Decls) then
14365 Decl := First (Decls);
14366 while Present (Decl) loop
14367 Provide_Completing_Bodies (Decl);
14368 Next (Decl);
14369 end loop;
14370 end if;
14371 end Provide_Completing_Bodies_In;
14373 -- Local variables
14375 Spec : Node_Id;
14377 -- Start of processing for Provide_Completing_Bodies
14379 begin
14380 if Nkind (N) = N_Package_Declaration then
14381 Spec := Specification (N);
14383 Push_Scope (Defining_Entity (N));
14384 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
14385 Provide_Completing_Bodies_In (Private_Declarations (Spec));
14386 Pop_Scope;
14388 elsif Nkind (N) = N_Subprogram_Declaration then
14389 Build_Completing_Body (N);
14390 end if;
14391 end Provide_Completing_Bodies;
14393 -------------------
14394 -- Remove_Parent --
14395 -------------------
14397 procedure Remove_Parent (In_Body : Boolean := False) is
14398 S : Entity_Id := Current_Scope;
14399 -- S is the scope containing the instantiation just completed. The scope
14400 -- stack contains the parent instances of the instantiation, followed by
14401 -- the original S.
14403 Cur_P : Entity_Id;
14404 E : Entity_Id;
14405 P : Entity_Id;
14406 Hidden : Elmt_Id;
14408 begin
14409 -- After child instantiation is complete, remove from scope stack the
14410 -- extra copy of the current scope, and then remove parent instances.
14412 if not In_Body then
14413 Pop_Scope;
14415 while Current_Scope /= S loop
14416 P := Current_Scope;
14417 End_Package_Scope (Current_Scope);
14419 if In_Open_Scopes (P) then
14420 E := First_Entity (P);
14421 while Present (E) loop
14422 Set_Is_Immediately_Visible (E, True);
14423 Next_Entity (E);
14424 end loop;
14426 -- If instantiation is declared in a block, it is the enclosing
14427 -- scope that might be a parent instance. Note that only one
14428 -- block can be involved, because the parent instances have
14429 -- been installed within it.
14431 if Ekind (P) = E_Block then
14432 Cur_P := Scope (P);
14433 else
14434 Cur_P := P;
14435 end if;
14437 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
14438 -- We are within an instance of some sibling. Retain
14439 -- visibility of parent, for proper subsequent cleanup, and
14440 -- reinstall private declarations as well.
14442 Set_In_Private_Part (P);
14443 Install_Private_Declarations (P);
14444 end if;
14446 -- If the ultimate parent is a top-level unit recorded in
14447 -- Instance_Parent_Unit, then reset its visibility to what it was
14448 -- before instantiation. (It's not clear what the purpose is of
14449 -- testing whether Scope (P) is In_Open_Scopes, but that test was
14450 -- present before the ultimate parent test was added.???)
14452 elsif not In_Open_Scopes (Scope (P))
14453 or else (P = Instance_Parent_Unit
14454 and then not Parent_Unit_Visible)
14455 then
14456 Set_Is_Immediately_Visible (P, False);
14458 -- If the current scope is itself an instantiation of a generic
14459 -- nested within P, and we are in the private part of body of this
14460 -- instantiation, restore the full views of P, that were removed
14461 -- in End_Package_Scope above. This obscure case can occur when a
14462 -- subunit of a generic contains an instance of a child unit of
14463 -- its generic parent unit.
14465 elsif S = Current_Scope and then Is_Generic_Instance (S) then
14466 declare
14467 Par : constant Entity_Id :=
14468 Generic_Parent (Package_Specification (S));
14469 begin
14470 if Present (Par)
14471 and then P = Scope (Par)
14472 and then (In_Package_Body (S) or else In_Private_Part (S))
14473 then
14474 Set_In_Private_Part (P);
14475 Install_Private_Declarations (P);
14476 end if;
14477 end;
14478 end if;
14479 end loop;
14481 -- Reset visibility of entities in the enclosing scope
14483 Set_Is_Hidden_Open_Scope (Current_Scope, False);
14485 Hidden := First_Elmt (Hidden_Entities);
14486 while Present (Hidden) loop
14487 Set_Is_Immediately_Visible (Node (Hidden), True);
14488 Next_Elmt (Hidden);
14489 end loop;
14491 else
14492 -- Each body is analyzed separately, and there is no context that
14493 -- needs preserving from one body instance to the next, so remove all
14494 -- parent scopes that have been installed.
14496 while Present (S) loop
14497 End_Package_Scope (S);
14498 Set_Is_Immediately_Visible (S, False);
14499 S := Current_Scope;
14500 exit when S = Standard_Standard;
14501 end loop;
14502 end if;
14503 end Remove_Parent;
14505 -----------------
14506 -- Restore_Env --
14507 -----------------
14509 procedure Restore_Env is
14510 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
14512 begin
14513 if No (Current_Instantiated_Parent.Act_Id) then
14514 -- Restore environment after subprogram inlining
14516 Restore_Private_Views (Empty);
14517 end if;
14519 Current_Instantiated_Parent := Saved.Instantiated_Parent;
14520 Exchanged_Views := Saved.Exchanged_Views;
14521 Hidden_Entities := Saved.Hidden_Entities;
14522 Current_Sem_Unit := Saved.Current_Sem_Unit;
14523 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
14524 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
14526 Restore_Config_Switches (Saved.Switches);
14528 Instance_Envs.Decrement_Last;
14529 end Restore_Env;
14531 ---------------------------
14532 -- Restore_Private_Views --
14533 ---------------------------
14535 procedure Restore_Private_Views
14536 (Pack_Id : Entity_Id;
14537 Is_Package : Boolean := True)
14539 M : Elmt_Id;
14540 E : Entity_Id;
14541 Typ : Entity_Id;
14542 Dep_Elmt : Elmt_Id;
14543 Dep_Typ : Node_Id;
14545 procedure Restore_Nested_Formal (Formal : Entity_Id);
14546 -- Hide the generic formals of formal packages declared with box which
14547 -- were reachable in the current instantiation.
14549 ---------------------------
14550 -- Restore_Nested_Formal --
14551 ---------------------------
14553 procedure Restore_Nested_Formal (Formal : Entity_Id) is
14554 Ent : Entity_Id;
14556 begin
14557 if Present (Renamed_Object (Formal))
14558 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
14559 then
14560 return;
14562 elsif Present (Associated_Formal_Package (Formal)) then
14563 Ent := First_Entity (Formal);
14564 while Present (Ent) loop
14565 exit when Ekind (Ent) = E_Package
14566 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
14568 Set_Is_Hidden (Ent);
14569 Set_Is_Potentially_Use_Visible (Ent, False);
14571 -- If package, then recurse
14573 if Ekind (Ent) = E_Package then
14574 Restore_Nested_Formal (Ent);
14575 end if;
14577 Next_Entity (Ent);
14578 end loop;
14579 end if;
14580 end Restore_Nested_Formal;
14582 -- Start of processing for Restore_Private_Views
14584 begin
14585 M := First_Elmt (Exchanged_Views);
14586 while Present (M) loop
14587 Typ := Node (M);
14589 -- Subtypes of types whose views have been exchanged, and that are
14590 -- defined within the instance, were not on the Private_Dependents
14591 -- list on entry to the instance, so they have to be exchanged
14592 -- explicitly now, in order to remain consistent with the view of the
14593 -- parent type.
14595 if Ekind_In (Typ, E_Private_Type,
14596 E_Limited_Private_Type,
14597 E_Record_Type_With_Private)
14598 then
14599 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
14600 while Present (Dep_Elmt) loop
14601 Dep_Typ := Node (Dep_Elmt);
14603 if Scope (Dep_Typ) = Pack_Id
14604 and then Present (Full_View (Dep_Typ))
14605 then
14606 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
14607 Exchange_Declarations (Dep_Typ);
14608 end if;
14610 Next_Elmt (Dep_Elmt);
14611 end loop;
14612 end if;
14614 Exchange_Declarations (Node (M));
14615 Next_Elmt (M);
14616 end loop;
14618 if No (Pack_Id) then
14619 return;
14620 end if;
14622 -- Make the generic formal parameters private, and make the formal types
14623 -- into subtypes of the actuals again.
14625 E := First_Entity (Pack_Id);
14626 while Present (E) loop
14627 Set_Is_Hidden (E, True);
14629 if Is_Type (E)
14630 and then Nkind (Parent (E)) = N_Subtype_Declaration
14631 then
14632 -- If the actual for E is itself a generic actual type from
14633 -- an enclosing instance, E is still a generic actual type
14634 -- outside of the current instance. This matter when resolving
14635 -- an overloaded call that may be ambiguous in the enclosing
14636 -- instance, when two of its actuals coincide.
14638 if Is_Entity_Name (Subtype_Indication (Parent (E)))
14639 and then Is_Generic_Actual_Type
14640 (Entity (Subtype_Indication (Parent (E))))
14641 then
14642 null;
14643 else
14644 Set_Is_Generic_Actual_Type (E, False);
14645 end if;
14647 -- An unusual case of aliasing: the actual may also be directly
14648 -- visible in the generic, and be private there, while it is fully
14649 -- visible in the context of the instance. The internal subtype
14650 -- is private in the instance but has full visibility like its
14651 -- parent in the enclosing scope. This enforces the invariant that
14652 -- the privacy status of all private dependents of a type coincide
14653 -- with that of the parent type. This can only happen when a
14654 -- generic child unit is instantiated within a sibling.
14656 if Is_Private_Type (E)
14657 and then not Is_Private_Type (Etype (E))
14658 then
14659 Exchange_Declarations (E);
14660 end if;
14662 elsif Ekind (E) = E_Package then
14664 -- The end of the renaming list is the renaming of the generic
14665 -- package itself. If the instance is a subprogram, all entities
14666 -- in the corresponding package are renamings. If this entity is
14667 -- a formal package, make its own formals private as well. The
14668 -- actual in this case is itself the renaming of an instantiation.
14669 -- If the entity is not a package renaming, it is the entity
14670 -- created to validate formal package actuals: ignore it.
14672 -- If the actual is itself a formal package for the enclosing
14673 -- generic, or the actual for such a formal package, it remains
14674 -- visible on exit from the instance, and therefore nothing needs
14675 -- to be done either, except to keep it accessible.
14677 if Is_Package and then Renamed_Object (E) = Pack_Id then
14678 exit;
14680 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
14681 null;
14683 elsif
14684 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
14685 then
14686 Set_Is_Hidden (E, False);
14688 else
14689 declare
14690 Act_P : constant Entity_Id := Renamed_Object (E);
14691 Id : Entity_Id;
14693 begin
14694 Id := First_Entity (Act_P);
14695 while Present (Id)
14696 and then Id /= First_Private_Entity (Act_P)
14697 loop
14698 exit when Ekind (Id) = E_Package
14699 and then Renamed_Object (Id) = Act_P;
14701 Set_Is_Hidden (Id, True);
14702 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
14704 if Ekind (Id) = E_Package then
14705 Restore_Nested_Formal (Id);
14706 end if;
14708 Next_Entity (Id);
14709 end loop;
14710 end;
14711 end if;
14712 end if;
14714 Next_Entity (E);
14715 end loop;
14716 end Restore_Private_Views;
14718 --------------
14719 -- Save_Env --
14720 --------------
14722 procedure Save_Env
14723 (Gen_Unit : Entity_Id;
14724 Act_Unit : Entity_Id)
14726 begin
14727 Init_Env;
14728 Set_Instance_Env (Gen_Unit, Act_Unit);
14729 end Save_Env;
14731 ----------------------------
14732 -- Save_Global_References --
14733 ----------------------------
14735 procedure Save_Global_References (Templ : Node_Id) is
14737 -- ??? it is horrible to use global variables in highly recursive code
14739 E : Entity_Id;
14740 -- The entity of the current associated node
14742 Gen_Scope : Entity_Id;
14743 -- The scope of the generic for which references are being saved
14745 N2 : Node_Id;
14746 -- The current associated node
14748 function Is_Global (E : Entity_Id) return Boolean;
14749 -- Check whether entity is defined outside of generic unit. Examine the
14750 -- scope of an entity, and the scope of the scope, etc, until we find
14751 -- either Standard, in which case the entity is global, or the generic
14752 -- unit itself, which indicates that the entity is local. If the entity
14753 -- is the generic unit itself, as in the case of a recursive call, or
14754 -- the enclosing generic unit, if different from the current scope, then
14755 -- it is local as well, because it will be replaced at the point of
14756 -- instantiation. On the other hand, if it is a reference to a child
14757 -- unit of a common ancestor, which appears in an instantiation, it is
14758 -- global because it is used to denote a specific compilation unit at
14759 -- the time the instantiations will be analyzed.
14761 procedure Qualify_Universal_Operands
14762 (Op : Node_Id;
14763 Func_Call : Node_Id);
14764 -- Op denotes a binary or unary operator in generic template Templ. Node
14765 -- Func_Call is the function call alternative of the operator within the
14766 -- the analyzed copy of the template. Change each operand which yields a
14767 -- universal type by wrapping it into a qualified expression
14769 -- Actual_Typ'(Operand)
14771 -- where Actual_Typ is the type of corresponding actual parameter of
14772 -- Operand in Func_Call.
14774 procedure Reset_Entity (N : Node_Id);
14775 -- Save semantic information on global entity so that it is not resolved
14776 -- again at instantiation time.
14778 procedure Save_Entity_Descendants (N : Node_Id);
14779 -- Apply Save_Global_References to the two syntactic descendants of
14780 -- non-terminal nodes that carry an Associated_Node and are processed
14781 -- through Reset_Entity. Once the global entity (if any) has been
14782 -- captured together with its type, only two syntactic descendants need
14783 -- to be traversed to complete the processing of the tree rooted at N.
14784 -- This applies to Selected_Components, Expanded_Names, and to Operator
14785 -- nodes. N can also be a character literal, identifier, or operator
14786 -- symbol node, but the call has no effect in these cases.
14788 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
14789 -- Default actuals in nested instances must be handled specially
14790 -- because there is no link to them from the original tree. When an
14791 -- actual subprogram is given by a default, we add an explicit generic
14792 -- association for it in the instantiation node. When we save the
14793 -- global references on the name of the instance, we recover the list
14794 -- of generic associations, and add an explicit one to the original
14795 -- generic tree, through which a global actual can be preserved.
14796 -- Similarly, if a child unit is instantiated within a sibling, in the
14797 -- context of the parent, we must preserve the identifier of the parent
14798 -- so that it can be properly resolved in a subsequent instantiation.
14800 procedure Save_Global_Descendant (D : Union_Id);
14801 -- Apply Save_References recursively to the descendants of node D
14803 procedure Save_References (N : Node_Id);
14804 -- This is the recursive procedure that does the work, once the
14805 -- enclosing generic scope has been established.
14807 ---------------
14808 -- Is_Global --
14809 ---------------
14811 function Is_Global (E : Entity_Id) return Boolean is
14812 Se : Entity_Id;
14814 function Is_Instance_Node (Decl : Node_Id) return Boolean;
14815 -- Determine whether the parent node of a reference to a child unit
14816 -- denotes an instantiation or a formal package, in which case the
14817 -- reference to the child unit is global, even if it appears within
14818 -- the current scope (e.g. when the instance appears within the body
14819 -- of an ancestor).
14821 ----------------------
14822 -- Is_Instance_Node --
14823 ----------------------
14825 function Is_Instance_Node (Decl : Node_Id) return Boolean is
14826 begin
14827 return Nkind (Decl) in N_Generic_Instantiation
14828 or else
14829 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
14830 end Is_Instance_Node;
14832 -- Start of processing for Is_Global
14834 begin
14835 if E = Gen_Scope then
14836 return False;
14838 elsif E = Standard_Standard then
14839 return True;
14841 elsif Is_Child_Unit (E)
14842 and then (Is_Instance_Node (Parent (N2))
14843 or else (Nkind (Parent (N2)) = N_Expanded_Name
14844 and then N2 = Selector_Name (Parent (N2))
14845 and then
14846 Is_Instance_Node (Parent (Parent (N2)))))
14847 then
14848 return True;
14850 else
14851 Se := Scope (E);
14852 while Se /= Gen_Scope loop
14853 if Se = Standard_Standard then
14854 return True;
14855 else
14856 Se := Scope (Se);
14857 end if;
14858 end loop;
14860 return False;
14861 end if;
14862 end Is_Global;
14864 --------------------------------
14865 -- Qualify_Universal_Operands --
14866 --------------------------------
14868 procedure Qualify_Universal_Operands
14869 (Op : Node_Id;
14870 Func_Call : Node_Id)
14872 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
14873 -- Rewrite operand Opnd as a qualified expression of the form
14875 -- Actual_Typ'(Opnd)
14877 -- where Actual is the corresponding actual parameter of Opnd in
14878 -- function call Func_Call.
14880 function Qualify_Type
14881 (Loc : Source_Ptr;
14882 Typ : Entity_Id) return Node_Id;
14883 -- Qualify type Typ by creating a selected component of the form
14885 -- Scope_Of_Typ.Typ
14887 ---------------------
14888 -- Qualify_Operand --
14889 ---------------------
14891 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
14892 Loc : constant Source_Ptr := Sloc (Opnd);
14893 Typ : constant Entity_Id := Etype (Actual);
14894 Mark : Node_Id;
14895 Qual : Node_Id;
14897 begin
14898 -- Qualify the operand when it is of a universal type. Note that
14899 -- the template is unanalyzed and it is not possible to directly
14900 -- query the type. This transformation is not done when the type
14901 -- of the actual is internally generated because the type will be
14902 -- regenerated in the instance.
14904 if Yields_Universal_Type (Opnd)
14905 and then Comes_From_Source (Typ)
14906 and then not Is_Hidden (Typ)
14907 then
14908 -- The type of the actual may be a global reference. Save this
14909 -- information by creating a reference to it.
14911 if Is_Global (Typ) then
14912 Mark := New_Occurrence_Of (Typ, Loc);
14914 -- Otherwise rely on resolution to find the proper type within
14915 -- the instance.
14917 else
14918 Mark := Qualify_Type (Loc, Typ);
14919 end if;
14921 Qual :=
14922 Make_Qualified_Expression (Loc,
14923 Subtype_Mark => Mark,
14924 Expression => Relocate_Node (Opnd));
14926 -- Mark the qualification to distinguish it from other source
14927 -- constructs and signal the instantiation mechanism that this
14928 -- node requires special processing. See Copy_Generic_Node for
14929 -- details.
14931 Set_Is_Qualified_Universal_Literal (Qual);
14933 Rewrite (Opnd, Qual);
14934 end if;
14935 end Qualify_Operand;
14937 ------------------
14938 -- Qualify_Type --
14939 ------------------
14941 function Qualify_Type
14942 (Loc : Source_Ptr;
14943 Typ : Entity_Id) return Node_Id
14945 Scop : constant Entity_Id := Scope (Typ);
14946 Result : Node_Id;
14948 begin
14949 Result := Make_Identifier (Loc, Chars (Typ));
14951 if Present (Scop) and then not Is_Generic_Unit (Scop) then
14952 Result :=
14953 Make_Selected_Component (Loc,
14954 Prefix => Make_Identifier (Loc, Chars (Scop)),
14955 Selector_Name => Result);
14956 end if;
14958 return Result;
14959 end Qualify_Type;
14961 -- Local variables
14963 Actuals : constant List_Id := Parameter_Associations (Func_Call);
14965 -- Start of processing for Qualify_Universal_Operands
14967 begin
14968 if Nkind (Op) in N_Binary_Op then
14969 Qualify_Operand (Left_Opnd (Op), First (Actuals));
14970 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
14972 elsif Nkind (Op) in N_Unary_Op then
14973 Qualify_Operand (Right_Opnd (Op), First (Actuals));
14974 end if;
14975 end Qualify_Universal_Operands;
14977 ------------------
14978 -- Reset_Entity --
14979 ------------------
14981 procedure Reset_Entity (N : Node_Id) is
14982 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
14983 -- If the type of N2 is global to the generic unit, save the type in
14984 -- the generic node. Just as we perform name capture for explicit
14985 -- references within the generic, we must capture the global types
14986 -- of local entities because they may participate in resolution in
14987 -- the instance.
14989 function Top_Ancestor (E : Entity_Id) return Entity_Id;
14990 -- Find the ultimate ancestor of the current unit. If it is not a
14991 -- generic unit, then the name of the current unit in the prefix of
14992 -- an expanded name must be replaced with its generic homonym to
14993 -- ensure that it will be properly resolved in an instance.
14995 ---------------------
14996 -- Set_Global_Type --
14997 ---------------------
14999 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
15000 Typ : constant Entity_Id := Etype (N2);
15002 begin
15003 Set_Etype (N, Typ);
15005 -- If the entity of N is not the associated node, this is a
15006 -- nested generic and it has an associated node as well, whose
15007 -- type is already the full view (see below). Indicate that the
15008 -- original node has a private view.
15010 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
15011 Set_Has_Private_View (N);
15012 end if;
15014 -- If not a private type, nothing else to do
15016 if not Is_Private_Type (Typ) then
15017 if Is_Array_Type (Typ)
15018 and then Is_Private_Type (Component_Type (Typ))
15019 then
15020 Set_Has_Private_View (N);
15021 end if;
15023 -- If it is a derivation of a private type in a context where no
15024 -- full view is needed, nothing to do either.
15026 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
15027 null;
15029 -- Otherwise mark the type for flipping and use the full view when
15030 -- available.
15032 else
15033 Set_Has_Private_View (N);
15035 if Present (Full_View (Typ)) then
15036 Set_Etype (N2, Full_View (Typ));
15037 end if;
15038 end if;
15040 if Is_Floating_Point_Type (Typ)
15041 and then Has_Dimension_System (Typ)
15042 then
15043 Copy_Dimensions (N2, N);
15044 end if;
15045 end Set_Global_Type;
15047 ------------------
15048 -- Top_Ancestor --
15049 ------------------
15051 function Top_Ancestor (E : Entity_Id) return Entity_Id is
15052 Par : Entity_Id;
15054 begin
15055 Par := E;
15056 while Is_Child_Unit (Par) loop
15057 Par := Scope (Par);
15058 end loop;
15060 return Par;
15061 end Top_Ancestor;
15063 -- Start of processing for Reset_Entity
15065 begin
15066 N2 := Get_Associated_Node (N);
15067 E := Entity (N2);
15069 if Present (E) then
15071 -- If the node is an entry call to an entry in an enclosing task,
15072 -- it is rewritten as a selected component. No global entity to
15073 -- preserve in this case, since the expansion will be redone in
15074 -- the instance.
15076 if not Nkind_In (E, N_Defining_Character_Literal,
15077 N_Defining_Identifier,
15078 N_Defining_Operator_Symbol)
15079 then
15080 Set_Associated_Node (N, Empty);
15081 Set_Etype (N, Empty);
15082 return;
15083 end if;
15085 -- If the entity is an itype created as a subtype of an access
15086 -- type with a null exclusion restore source entity for proper
15087 -- visibility. The itype will be created anew in the instance.
15089 if Is_Itype (E)
15090 and then Ekind (E) = E_Access_Subtype
15091 and then Is_Entity_Name (N)
15092 and then Chars (Etype (E)) = Chars (N)
15093 then
15094 E := Etype (E);
15095 Set_Entity (N2, E);
15096 Set_Etype (N2, E);
15097 end if;
15099 if Is_Global (E) then
15101 -- If the entity is a package renaming that is the prefix of
15102 -- an expanded name, it has been rewritten as the renamed
15103 -- package, which is necessary semantically but complicates
15104 -- ASIS tree traversal, so we recover the original entity to
15105 -- expose the renaming. Take into account that the context may
15106 -- be a nested generic, that the original node may itself have
15107 -- an associated node that had better be an entity, and that
15108 -- the current node is still a selected component.
15110 if Ekind (E) = E_Package
15111 and then Nkind (N) = N_Selected_Component
15112 and then Nkind (Parent (N)) = N_Expanded_Name
15113 and then Present (Original_Node (N2))
15114 and then Is_Entity_Name (Original_Node (N2))
15115 and then Present (Entity (Original_Node (N2)))
15116 then
15117 if Is_Global (Entity (Original_Node (N2))) then
15118 N2 := Original_Node (N2);
15119 Set_Associated_Node (N, N2);
15120 Set_Global_Type (N, N2);
15122 -- Renaming is local, and will be resolved in instance
15124 else
15125 Set_Associated_Node (N, Empty);
15126 Set_Etype (N, Empty);
15127 end if;
15129 else
15130 Set_Global_Type (N, N2);
15131 end if;
15133 elsif Nkind (N) = N_Op_Concat
15134 and then Is_Generic_Type (Etype (N2))
15135 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
15136 or else
15137 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
15138 and then Is_Intrinsic_Subprogram (E)
15139 then
15140 null;
15142 -- Entity is local. Mark generic node as unresolved. Note that now
15143 -- it does not have an entity.
15145 else
15146 Set_Associated_Node (N, Empty);
15147 Set_Etype (N, Empty);
15148 end if;
15150 if Nkind (Parent (N)) in N_Generic_Instantiation
15151 and then N = Name (Parent (N))
15152 then
15153 Save_Global_Defaults (Parent (N), Parent (N2));
15154 end if;
15156 elsif Nkind (Parent (N)) = N_Selected_Component
15157 and then Nkind (Parent (N2)) = N_Expanded_Name
15158 then
15159 if Is_Global (Entity (Parent (N2))) then
15160 Change_Selected_Component_To_Expanded_Name (Parent (N));
15161 Set_Associated_Node (Parent (N), Parent (N2));
15162 Set_Global_Type (Parent (N), Parent (N2));
15163 Save_Entity_Descendants (N);
15165 -- If this is a reference to the current generic entity, replace
15166 -- by the name of the generic homonym of the current package. This
15167 -- is because in an instantiation Par.P.Q will not resolve to the
15168 -- name of the instance, whose enclosing scope is not necessarily
15169 -- Par. We use the generic homonym rather that the name of the
15170 -- generic itself because it may be hidden by a local declaration.
15172 elsif In_Open_Scopes (Entity (Parent (N2)))
15173 and then not
15174 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
15175 then
15176 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
15177 Rewrite (Parent (N),
15178 Make_Identifier (Sloc (N),
15179 Chars =>
15180 Chars (Generic_Homonym (Entity (Parent (N2))))));
15181 else
15182 Rewrite (Parent (N),
15183 Make_Identifier (Sloc (N),
15184 Chars => Chars (Selector_Name (Parent (N2)))));
15185 end if;
15186 end if;
15188 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
15189 and then Parent (N) = Name (Parent (Parent (N)))
15190 then
15191 Save_Global_Defaults
15192 (Parent (Parent (N)), Parent (Parent (N2)));
15193 end if;
15195 -- A selected component may denote a static constant that has been
15196 -- folded. If the static constant is global to the generic, capture
15197 -- its value. Otherwise the folding will happen in any instantiation.
15199 elsif Nkind (Parent (N)) = N_Selected_Component
15200 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
15201 then
15202 if Present (Entity (Original_Node (Parent (N2))))
15203 and then Is_Global (Entity (Original_Node (Parent (N2))))
15204 then
15205 Rewrite (Parent (N), New_Copy (Parent (N2)));
15206 Set_Analyzed (Parent (N), False);
15207 end if;
15209 -- A selected component may be transformed into a parameterless
15210 -- function call. If the called entity is global, rewrite the node
15211 -- appropriately, i.e. as an extended name for the global entity.
15213 elsif Nkind (Parent (N)) = N_Selected_Component
15214 and then Nkind (Parent (N2)) = N_Function_Call
15215 and then N = Selector_Name (Parent (N))
15216 then
15217 if No (Parameter_Associations (Parent (N2))) then
15218 if Is_Global (Entity (Name (Parent (N2)))) then
15219 Change_Selected_Component_To_Expanded_Name (Parent (N));
15220 Set_Associated_Node (Parent (N), Name (Parent (N2)));
15221 Set_Global_Type (Parent (N), Name (Parent (N2)));
15222 Save_Entity_Descendants (N);
15224 else
15225 Set_Is_Prefixed_Call (Parent (N));
15226 Set_Associated_Node (N, Empty);
15227 Set_Etype (N, Empty);
15228 end if;
15230 -- In Ada 2005, X.F may be a call to a primitive operation,
15231 -- rewritten as F (X). This rewriting will be done again in an
15232 -- instance, so keep the original node. Global entities will be
15233 -- captured as for other constructs. Indicate that this must
15234 -- resolve as a call, to prevent accidental overloading in the
15235 -- instance, if both a component and a primitive operation appear
15236 -- as candidates.
15238 else
15239 Set_Is_Prefixed_Call (Parent (N));
15240 end if;
15242 -- Entity is local. Reset in generic unit, so that node is resolved
15243 -- anew at the point of instantiation.
15245 else
15246 Set_Associated_Node (N, Empty);
15247 Set_Etype (N, Empty);
15248 end if;
15249 end Reset_Entity;
15251 -----------------------------
15252 -- Save_Entity_Descendants --
15253 -----------------------------
15255 procedure Save_Entity_Descendants (N : Node_Id) is
15256 begin
15257 case Nkind (N) is
15258 when N_Binary_Op =>
15259 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
15260 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
15262 when N_Unary_Op =>
15263 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
15265 when N_Expanded_Name
15266 | N_Selected_Component
15268 Save_Global_Descendant (Union_Id (Prefix (N)));
15269 Save_Global_Descendant (Union_Id (Selector_Name (N)));
15271 when N_Character_Literal
15272 | N_Identifier
15273 | N_Operator_Symbol
15275 null;
15277 when others =>
15278 raise Program_Error;
15279 end case;
15280 end Save_Entity_Descendants;
15282 --------------------------
15283 -- Save_Global_Defaults --
15284 --------------------------
15286 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
15287 Loc : constant Source_Ptr := Sloc (N1);
15288 Assoc2 : constant List_Id := Generic_Associations (N2);
15289 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
15290 Assoc1 : List_Id;
15291 Act1 : Node_Id;
15292 Act2 : Node_Id;
15293 Def : Node_Id;
15294 Ndec : Node_Id;
15295 Subp : Entity_Id;
15296 Actual : Entity_Id;
15298 begin
15299 Assoc1 := Generic_Associations (N1);
15301 if Present (Assoc1) then
15302 Act1 := First (Assoc1);
15303 else
15304 Act1 := Empty;
15305 Set_Generic_Associations (N1, New_List);
15306 Assoc1 := Generic_Associations (N1);
15307 end if;
15309 if Present (Assoc2) then
15310 Act2 := First (Assoc2);
15311 else
15312 return;
15313 end if;
15315 while Present (Act1) and then Present (Act2) loop
15316 Next (Act1);
15317 Next (Act2);
15318 end loop;
15320 -- Find the associations added for default subprograms
15322 if Present (Act2) then
15323 while Nkind (Act2) /= N_Generic_Association
15324 or else No (Entity (Selector_Name (Act2)))
15325 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
15326 loop
15327 Next (Act2);
15328 end loop;
15330 -- Add a similar association if the default is global. The
15331 -- renaming declaration for the actual has been analyzed, and
15332 -- its alias is the program it renames. Link the actual in the
15333 -- original generic tree with the node in the analyzed tree.
15335 while Present (Act2) loop
15336 Subp := Entity (Selector_Name (Act2));
15337 Def := Explicit_Generic_Actual_Parameter (Act2);
15339 -- Following test is defence against rubbish errors
15341 if No (Alias (Subp)) then
15342 return;
15343 end if;
15345 -- Retrieve the resolved actual from the renaming declaration
15346 -- created for the instantiated formal.
15348 Actual := Entity (Name (Parent (Parent (Subp))));
15349 Set_Entity (Def, Actual);
15350 Set_Etype (Def, Etype (Actual));
15352 if Is_Global (Actual) then
15353 Ndec :=
15354 Make_Generic_Association (Loc,
15355 Selector_Name =>
15356 New_Occurrence_Of (Subp, Loc),
15357 Explicit_Generic_Actual_Parameter =>
15358 New_Occurrence_Of (Actual, Loc));
15360 Set_Associated_Node
15361 (Explicit_Generic_Actual_Parameter (Ndec), Def);
15363 Append (Ndec, Assoc1);
15365 -- If there are other defaults, add a dummy association in case
15366 -- there are other defaulted formals with the same name.
15368 elsif Present (Next (Act2)) then
15369 Ndec :=
15370 Make_Generic_Association (Loc,
15371 Selector_Name =>
15372 New_Occurrence_Of (Subp, Loc),
15373 Explicit_Generic_Actual_Parameter => Empty);
15375 Append (Ndec, Assoc1);
15376 end if;
15378 Next (Act2);
15379 end loop;
15380 end if;
15382 if Nkind (Name (N1)) = N_Identifier
15383 and then Is_Child_Unit (Gen_Id)
15384 and then Is_Global (Gen_Id)
15385 and then Is_Generic_Unit (Scope (Gen_Id))
15386 and then In_Open_Scopes (Scope (Gen_Id))
15387 then
15388 -- This is an instantiation of a child unit within a sibling, so
15389 -- that the generic parent is in scope. An eventual instance must
15390 -- occur within the scope of an instance of the parent. Make name
15391 -- in instance into an expanded name, to preserve the identifier
15392 -- of the parent, so it can be resolved subsequently.
15394 Rewrite (Name (N2),
15395 Make_Expanded_Name (Loc,
15396 Chars => Chars (Gen_Id),
15397 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
15398 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
15399 Set_Entity (Name (N2), Gen_Id);
15401 Rewrite (Name (N1),
15402 Make_Expanded_Name (Loc,
15403 Chars => Chars (Gen_Id),
15404 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
15405 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
15407 Set_Associated_Node (Name (N1), Name (N2));
15408 Set_Associated_Node (Prefix (Name (N1)), Empty);
15409 Set_Associated_Node
15410 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
15411 Set_Etype (Name (N1), Etype (Gen_Id));
15412 end if;
15413 end Save_Global_Defaults;
15415 ----------------------------
15416 -- Save_Global_Descendant --
15417 ----------------------------
15419 procedure Save_Global_Descendant (D : Union_Id) is
15420 N1 : Node_Id;
15422 begin
15423 if D in Node_Range then
15424 if D = Union_Id (Empty) then
15425 null;
15427 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
15428 Save_References (Node_Id (D));
15429 end if;
15431 elsif D in List_Range then
15432 pragma Assert (D /= Union_Id (No_List));
15433 -- Because No_List = Empty, which is in Node_Range above
15435 if Is_Empty_List (List_Id (D)) then
15436 null;
15438 else
15439 N1 := First (List_Id (D));
15440 while Present (N1) loop
15441 Save_References (N1);
15442 Next (N1);
15443 end loop;
15444 end if;
15446 -- Element list or other non-node field, nothing to do
15448 else
15449 null;
15450 end if;
15451 end Save_Global_Descendant;
15453 ---------------------
15454 -- Save_References --
15455 ---------------------
15457 -- This is the recursive procedure that does the work once the enclosing
15458 -- generic scope has been established. We have to treat specially a
15459 -- number of node rewritings that are required by semantic processing
15460 -- and which change the kind of nodes in the generic copy: typically
15461 -- constant-folding, replacing an operator node by a string literal, or
15462 -- a selected component by an expanded name. In each of those cases, the
15463 -- transformation is propagated to the generic unit.
15465 procedure Save_References (N : Node_Id) is
15466 Loc : constant Source_Ptr := Sloc (N);
15468 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
15469 -- Determine whether arbitrary node Nod requires delayed capture of
15470 -- global references within its aspect specifications.
15472 procedure Save_References_In_Aggregate (N : Node_Id);
15473 -- Save all global references in [extension] aggregate node N
15475 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
15476 -- Save all global references in a character literal or operator
15477 -- symbol denoted by N.
15479 procedure Save_References_In_Descendants (N : Node_Id);
15480 -- Save all global references in all descendants of node N
15482 procedure Save_References_In_Identifier (N : Node_Id);
15483 -- Save all global references in identifier node N
15485 procedure Save_References_In_Operator (N : Node_Id);
15486 -- Save all global references in operator node N
15488 procedure Save_References_In_Pragma (Prag : Node_Id);
15489 -- Save all global references found within the expression of pragma
15490 -- Prag.
15492 ---------------------------
15493 -- Requires_Delayed_Save --
15494 ---------------------------
15496 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
15497 begin
15498 -- Generic packages and subprograms require delayed capture of
15499 -- global references within their aspects due to the timing of
15500 -- annotation analysis.
15502 if Nkind_In (Nod, N_Generic_Package_Declaration,
15503 N_Generic_Subprogram_Declaration,
15504 N_Package_Body,
15505 N_Package_Body_Stub,
15506 N_Subprogram_Body,
15507 N_Subprogram_Body_Stub)
15508 then
15509 -- Since the capture of global references is done on the
15510 -- unanalyzed generic template, there is no information around
15511 -- to infer the context. Use the Associated_Entity linkages to
15512 -- peek into the analyzed generic copy and determine what the
15513 -- template corresponds to.
15515 if Nod = Templ then
15516 return
15517 Is_Generic_Declaration_Or_Body
15518 (Unit_Declaration_Node
15519 (Associated_Entity (Defining_Entity (Nod))));
15521 -- Otherwise the generic unit being processed is not the top
15522 -- level template. It is safe to capture of global references
15523 -- within the generic unit because at this point the top level
15524 -- copy is fully analyzed.
15526 else
15527 return False;
15528 end if;
15530 -- Otherwise capture the global references without interference
15532 else
15533 return False;
15534 end if;
15535 end Requires_Delayed_Save;
15537 ----------------------------------
15538 -- Save_References_In_Aggregate --
15539 ----------------------------------
15541 procedure Save_References_In_Aggregate (N : Node_Id) is
15542 Nam : Node_Id;
15543 Qual : Node_Id := Empty;
15544 Typ : Entity_Id := Empty;
15546 use Atree.Unchecked_Access;
15547 -- This code section is part of implementing an untyped tree
15548 -- traversal, so it needs direct access to node fields.
15550 begin
15551 N2 := Get_Associated_Node (N);
15553 if Present (N2) then
15554 Typ := Etype (N2);
15556 -- In an instance within a generic, use the name of the actual
15557 -- and not the original generic parameter. If the actual is
15558 -- global in the current generic it must be preserved for its
15559 -- instantiation.
15561 if Nkind (Parent (Typ)) = N_Subtype_Declaration
15562 and then Present (Generic_Parent_Type (Parent (Typ)))
15563 then
15564 Typ := Base_Type (Typ);
15565 Set_Etype (N2, Typ);
15566 end if;
15567 end if;
15569 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
15570 Set_Associated_Node (N, Empty);
15572 -- If the aggregate is an actual in a call, it has been
15573 -- resolved in the current context, to some local type. The
15574 -- enclosing call may have been disambiguated by the aggregate,
15575 -- and this disambiguation might fail at instantiation time
15576 -- because the type to which the aggregate did resolve is not
15577 -- preserved. In order to preserve some of this information,
15578 -- wrap the aggregate in a qualified expression, using the id
15579 -- of its type. For further disambiguation we qualify the type
15580 -- name with its scope (if visible and not hidden by a local
15581 -- homograph) because both id's will have corresponding
15582 -- entities in an instance. This resolves most of the problems
15583 -- with missing type information on aggregates in instances.
15585 if Present (N2)
15586 and then Nkind (N2) = Nkind (N)
15587 and then Nkind (Parent (N2)) in N_Subprogram_Call
15588 and then Present (Typ)
15589 and then Comes_From_Source (Typ)
15590 then
15591 Nam := Make_Identifier (Loc, Chars (Typ));
15593 if Is_Immediately_Visible (Scope (Typ))
15594 and then
15595 (not In_Open_Scopes (Scope (Typ))
15596 or else Current_Entity (Scope (Typ)) = Scope (Typ))
15597 then
15598 Nam :=
15599 Make_Selected_Component (Loc,
15600 Prefix =>
15601 Make_Identifier (Loc, Chars (Scope (Typ))),
15602 Selector_Name => Nam);
15603 end if;
15605 Qual :=
15606 Make_Qualified_Expression (Loc,
15607 Subtype_Mark => Nam,
15608 Expression => Relocate_Node (N));
15609 end if;
15610 end if;
15612 Save_Global_Descendant (Field1 (N));
15613 Save_Global_Descendant (Field2 (N));
15614 Save_Global_Descendant (Field3 (N));
15615 Save_Global_Descendant (Field5 (N));
15617 if Present (Qual) then
15618 Rewrite (N, Qual);
15619 end if;
15620 end Save_References_In_Aggregate;
15622 ----------------------------------------------
15623 -- Save_References_In_Char_Lit_Or_Op_Symbol --
15624 ----------------------------------------------
15626 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
15627 begin
15628 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15629 Reset_Entity (N);
15631 elsif Nkind (N) = N_Operator_Symbol
15632 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
15633 then
15634 Change_Operator_Symbol_To_String_Literal (N);
15635 end if;
15636 end Save_References_In_Char_Lit_Or_Op_Symbol;
15638 ------------------------------------
15639 -- Save_References_In_Descendants --
15640 ------------------------------------
15642 procedure Save_References_In_Descendants (N : Node_Id) is
15643 use Atree.Unchecked_Access;
15644 -- This code section is part of implementing an untyped tree
15645 -- traversal, so it needs direct access to node fields.
15647 begin
15648 Save_Global_Descendant (Field1 (N));
15649 Save_Global_Descendant (Field2 (N));
15650 Save_Global_Descendant (Field3 (N));
15651 Save_Global_Descendant (Field4 (N));
15652 Save_Global_Descendant (Field5 (N));
15653 end Save_References_In_Descendants;
15655 -----------------------------------
15656 -- Save_References_In_Identifier --
15657 -----------------------------------
15659 procedure Save_References_In_Identifier (N : Node_Id) is
15660 begin
15661 -- The node did not undergo a transformation
15663 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15664 declare
15665 Aux_N2 : constant Node_Id := Get_Associated_Node (N);
15666 Orig_N2_Parent : constant Node_Id :=
15667 Original_Node (Parent (Aux_N2));
15668 begin
15669 -- The parent of this identifier is a selected component
15670 -- which denotes a named number that was constant folded.
15671 -- Preserve the original name for ASIS and link the parent
15672 -- with its expanded name. The constant folding will be
15673 -- repeated in the instance.
15675 if Nkind (Parent (N)) = N_Selected_Component
15676 and then Nkind_In (Parent (Aux_N2), N_Integer_Literal,
15677 N_Real_Literal)
15678 and then Is_Entity_Name (Orig_N2_Parent)
15679 and then Ekind (Entity (Orig_N2_Parent)) in Named_Kind
15680 and then Is_Global (Entity (Orig_N2_Parent))
15681 then
15682 N2 := Aux_N2;
15683 Set_Associated_Node
15684 (Parent (N), Original_Node (Parent (N2)));
15686 -- Common case
15688 else
15689 -- If this is a discriminant reference, always save it.
15690 -- It is used in the instance to find the corresponding
15691 -- discriminant positionally rather than by name.
15693 Set_Original_Discriminant
15694 (N, Original_Discriminant (Get_Associated_Node (N)));
15695 end if;
15697 Reset_Entity (N);
15698 end;
15700 -- The analysis of the generic copy transformed the identifier
15701 -- into another construct. Propagate the changes to the template.
15703 else
15704 N2 := Get_Associated_Node (N);
15706 -- The identifier denotes a call to a parameterless function.
15707 -- Mark the node as resolved when the function is external.
15709 if Nkind (N2) = N_Function_Call then
15710 E := Entity (Name (N2));
15712 if Present (E) and then Is_Global (E) then
15713 Set_Etype (N, Etype (N2));
15714 else
15715 Set_Associated_Node (N, Empty);
15716 Set_Etype (N, Empty);
15717 end if;
15719 -- The identifier denotes a named number that was constant
15720 -- folded. Preserve the original name for ASIS and undo the
15721 -- constant folding which will be repeated in the instance.
15723 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
15724 and then Is_Entity_Name (Original_Node (N2))
15725 then
15726 Set_Associated_Node (N, Original_Node (N2));
15727 Reset_Entity (N);
15729 -- The identifier resolved to a string literal. Propagate this
15730 -- information to the generic template.
15732 elsif Nkind (N2) = N_String_Literal then
15733 Rewrite (N, New_Copy (N2));
15735 -- The identifier is rewritten as a dereference if it is the
15736 -- prefix of an implicit dereference. Preserve the original
15737 -- tree as the analysis of the instance will expand the node
15738 -- again, but preserve the resolved entity if it is global.
15740 elsif Nkind (N2) = N_Explicit_Dereference then
15741 if Is_Entity_Name (Prefix (N2))
15742 and then Present (Entity (Prefix (N2)))
15743 and then Is_Global (Entity (Prefix (N2)))
15744 then
15745 Set_Associated_Node (N, Prefix (N2));
15747 elsif Nkind (Prefix (N2)) = N_Function_Call
15748 and then Present (Entity (Name (Prefix (N2))))
15749 and then Is_Global (Entity (Name (Prefix (N2))))
15750 then
15751 Rewrite (N,
15752 Make_Explicit_Dereference (Loc,
15753 Prefix =>
15754 Make_Function_Call (Loc,
15755 Name =>
15756 New_Occurrence_Of
15757 (Entity (Name (Prefix (N2))), Loc))));
15759 else
15760 Set_Associated_Node (N, Empty);
15761 Set_Etype (N, Empty);
15762 end if;
15764 -- The subtype mark of a nominally unconstrained object is
15765 -- rewritten as a subtype indication using the bounds of the
15766 -- expression. Recover the original subtype mark.
15768 elsif Nkind (N2) = N_Subtype_Indication
15769 and then Is_Entity_Name (Original_Node (N2))
15770 then
15771 Set_Associated_Node (N, Original_Node (N2));
15772 Reset_Entity (N);
15773 end if;
15774 end if;
15775 end Save_References_In_Identifier;
15777 ---------------------------------
15778 -- Save_References_In_Operator --
15779 ---------------------------------
15781 procedure Save_References_In_Operator (N : Node_Id) is
15782 begin
15783 -- The node did not undergo a transformation
15785 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15786 if Nkind (N) = N_Op_Concat then
15787 Set_Is_Component_Left_Opnd (N,
15788 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15790 Set_Is_Component_Right_Opnd (N,
15791 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15792 end if;
15794 Reset_Entity (N);
15796 -- The analysis of the generic copy transformed the operator into
15797 -- some other construct. Propagate the changes to the template if
15798 -- applicable.
15800 else
15801 N2 := Get_Associated_Node (N);
15803 -- The operator resoved to a function call
15805 if Nkind (N2) = N_Function_Call then
15807 -- Add explicit qualifications in the generic template for
15808 -- all operands of universal type. This aids resolution by
15809 -- preserving the actual type of a literal or an attribute
15810 -- that yields a universal result.
15812 Qualify_Universal_Operands (N, N2);
15814 E := Entity (Name (N2));
15816 if Present (E) and then Is_Global (E) then
15817 Set_Etype (N, Etype (N2));
15818 else
15819 Set_Associated_Node (N, Empty);
15820 Set_Etype (N, Empty);
15821 end if;
15823 -- The operator was folded into a literal
15825 elsif Nkind_In (N2, N_Integer_Literal,
15826 N_Real_Literal,
15827 N_String_Literal)
15828 then
15829 if Present (Original_Node (N2))
15830 and then Nkind (Original_Node (N2)) = Nkind (N)
15831 then
15832 -- Operation was constant-folded. Whenever possible,
15833 -- recover semantic information from unfolded node,
15834 -- for ASIS use.
15836 Set_Associated_Node (N, Original_Node (N2));
15838 if Nkind (N) = N_Op_Concat then
15839 Set_Is_Component_Left_Opnd (N,
15840 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15841 Set_Is_Component_Right_Opnd (N,
15842 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15843 end if;
15845 Reset_Entity (N);
15847 -- Propagate the constant folding back to the template
15849 else
15850 Rewrite (N, New_Copy (N2));
15851 Set_Analyzed (N, False);
15852 end if;
15854 -- The operator was folded into an enumeration literal. Retain
15855 -- the entity to avoid spurious ambiguities if it is overloaded
15856 -- at the point of instantiation or inlining.
15858 elsif Nkind (N2) = N_Identifier
15859 and then Ekind (Entity (N2)) = E_Enumeration_Literal
15860 then
15861 Rewrite (N, New_Copy (N2));
15862 Set_Analyzed (N, False);
15863 end if;
15864 end if;
15866 -- Complete the operands check if node has not been constant
15867 -- folded.
15869 if Nkind (N) in N_Op then
15870 Save_Entity_Descendants (N);
15871 end if;
15872 end Save_References_In_Operator;
15874 -------------------------------
15875 -- Save_References_In_Pragma --
15876 -------------------------------
15878 procedure Save_References_In_Pragma (Prag : Node_Id) is
15879 Context : Node_Id;
15880 Do_Save : Boolean := True;
15882 use Atree.Unchecked_Access;
15883 -- This code section is part of implementing an untyped tree
15884 -- traversal, so it needs direct access to node fields.
15886 begin
15887 -- Do not save global references in pragmas generated from aspects
15888 -- because the pragmas will be regenerated at instantiation time.
15890 if From_Aspect_Specification (Prag) then
15891 Do_Save := False;
15893 -- The capture of global references within contract-related source
15894 -- pragmas associated with generic packages, subprograms or their
15895 -- respective bodies must be delayed due to timing of annotation
15896 -- analysis. Global references are still captured in routine
15897 -- Save_Global_References_In_Contract.
15899 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
15900 if Is_Package_Contract_Annotation (Prag) then
15901 Context := Find_Related_Package_Or_Body (Prag);
15902 else
15903 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
15904 Context := Find_Related_Declaration_Or_Body (Prag);
15905 end if;
15907 -- The use of Original_Node accounts for the case when the
15908 -- related context is generic template.
15910 if Requires_Delayed_Save (Original_Node (Context)) then
15911 Do_Save := False;
15912 end if;
15913 end if;
15915 -- For all other cases, save all global references within the
15916 -- descendants, but skip the following semantic fields:
15918 -- Field1 - Next_Pragma
15919 -- Field3 - Corresponding_Aspect
15920 -- Field5 - Next_Rep_Item
15922 if Do_Save then
15923 Save_Global_Descendant (Field2 (Prag));
15924 Save_Global_Descendant (Field4 (Prag));
15925 end if;
15926 end Save_References_In_Pragma;
15928 -- Start of processing for Save_References
15930 begin
15931 if N = Empty then
15932 null;
15934 -- Aggregates
15936 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
15937 Save_References_In_Aggregate (N);
15939 -- Character literals, operator symbols
15941 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
15942 Save_References_In_Char_Lit_Or_Op_Symbol (N);
15944 -- Defining identifiers
15946 elsif Nkind (N) in N_Entity then
15947 null;
15949 -- Identifiers
15951 elsif Nkind (N) = N_Identifier then
15952 Save_References_In_Identifier (N);
15954 -- Operators
15956 elsif Nkind (N) in N_Op then
15957 Save_References_In_Operator (N);
15959 -- Pragmas
15961 elsif Nkind (N) = N_Pragma then
15962 Save_References_In_Pragma (N);
15964 else
15965 Save_References_In_Descendants (N);
15966 end if;
15968 -- Save all global references found within the aspect specifications
15969 -- of the related node.
15971 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
15973 -- The capture of global references within aspects associated with
15974 -- generic packages, subprograms or their bodies must be delayed
15975 -- due to timing of annotation analysis. Global references are
15976 -- still captured in routine Save_Global_References_In_Contract.
15978 if Requires_Delayed_Save (N) then
15979 null;
15981 -- Otherwise save all global references within the aspects
15983 else
15984 Save_Global_References_In_Aspects (N);
15985 end if;
15986 end if;
15987 end Save_References;
15989 -- Start of processing for Save_Global_References
15991 begin
15992 Gen_Scope := Current_Scope;
15994 -- If the generic unit is a child unit, references to entities in the
15995 -- parent are treated as local, because they will be resolved anew in
15996 -- the context of the instance of the parent.
15998 while Is_Child_Unit (Gen_Scope)
15999 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
16000 loop
16001 Gen_Scope := Scope (Gen_Scope);
16002 end loop;
16004 Save_References (Templ);
16005 end Save_Global_References;
16007 ---------------------------------------
16008 -- Save_Global_References_In_Aspects --
16009 ---------------------------------------
16011 procedure Save_Global_References_In_Aspects (N : Node_Id) is
16012 Asp : Node_Id;
16013 Expr : Node_Id;
16015 begin
16016 Asp := First (Aspect_Specifications (N));
16017 while Present (Asp) loop
16018 Expr := Expression (Asp);
16020 if Present (Expr) then
16021 Save_Global_References (Expr);
16022 end if;
16024 Next (Asp);
16025 end loop;
16026 end Save_Global_References_In_Aspects;
16028 ------------------------------------------
16029 -- Set_Copied_Sloc_For_Inherited_Pragma --
16030 ------------------------------------------
16032 procedure Set_Copied_Sloc_For_Inherited_Pragma
16033 (N : Node_Id;
16034 E : Entity_Id)
16036 begin
16037 Create_Instantiation_Source (N, E,
16038 Inlined_Body => False,
16039 Inherited_Pragma => True,
16040 Factor => S_Adjustment);
16041 end Set_Copied_Sloc_For_Inherited_Pragma;
16043 --------------------------------------
16044 -- Set_Copied_Sloc_For_Inlined_Body --
16045 --------------------------------------
16047 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
16048 begin
16049 Create_Instantiation_Source (N, E,
16050 Inlined_Body => True,
16051 Inherited_Pragma => False,
16052 Factor => S_Adjustment);
16053 end Set_Copied_Sloc_For_Inlined_Body;
16055 ---------------------
16056 -- Set_Instance_Of --
16057 ---------------------
16059 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
16060 begin
16061 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
16062 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
16063 Generic_Renamings.Increment_Last;
16064 end Set_Instance_Of;
16066 --------------------
16067 -- Set_Next_Assoc --
16068 --------------------
16070 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
16071 begin
16072 Generic_Renamings.Table (E).Next_In_HTable := Next;
16073 end Set_Next_Assoc;
16075 -------------------
16076 -- Start_Generic --
16077 -------------------
16079 procedure Start_Generic is
16080 begin
16081 -- ??? More things could be factored out in this routine.
16082 -- Should probably be done at a later stage.
16084 Generic_Flags.Append (Inside_A_Generic);
16085 Inside_A_Generic := True;
16087 Expander_Mode_Save_And_Set (False);
16088 end Start_Generic;
16090 ----------------------
16091 -- Set_Instance_Env --
16092 ----------------------
16094 -- WARNING: This routine manages SPARK regions
16096 procedure Set_Instance_Env
16097 (Gen_Unit : Entity_Id;
16098 Act_Unit : Entity_Id)
16100 Saved_AE : constant Boolean := Assertions_Enabled;
16101 Saved_CPL : constant Node_Id := Check_Policy_List;
16102 Saved_DEC : constant Boolean := Dynamic_Elaboration_Checks;
16103 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
16104 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
16106 begin
16107 -- Regardless of the current mode, predefined units are analyzed in the
16108 -- most current Ada mode, and earlier version Ada checks do not apply
16109 -- to predefined units. Nothing needs to be done for non-internal units.
16110 -- These are always analyzed in the current mode.
16112 if In_Internal_Unit (Gen_Unit) then
16114 -- The following call resets all configuration attributes to default
16115 -- or the xxx_Config versions of the attributes when the current sem
16116 -- unit is the main unit. At the same time, internal units must also
16117 -- inherit certain configuration attributes from their context. It
16118 -- is unclear what these two sets are.
16120 Set_Config_Switches (True, Current_Sem_Unit = Main_Unit);
16122 -- Reinstall relevant configuration attributes of the context
16124 Assertions_Enabled := Saved_AE;
16125 Check_Policy_List := Saved_CPL;
16126 Dynamic_Elaboration_Checks := Saved_DEC;
16128 Install_SPARK_Mode (Saved_SM, Saved_SMP);
16129 end if;
16131 Current_Instantiated_Parent :=
16132 (Gen_Id => Gen_Unit,
16133 Act_Id => Act_Unit,
16134 Next_In_HTable => Assoc_Null);
16135 end Set_Instance_Env;
16137 -----------------
16138 -- Switch_View --
16139 -----------------
16141 procedure Switch_View (T : Entity_Id) is
16142 BT : constant Entity_Id := Base_Type (T);
16143 Priv_Elmt : Elmt_Id := No_Elmt;
16144 Priv_Sub : Entity_Id;
16146 begin
16147 -- T may be private but its base type may have been exchanged through
16148 -- some other occurrence, in which case there is nothing to switch
16149 -- besides T itself. Note that a private dependent subtype of a private
16150 -- type might not have been switched even if the base type has been,
16151 -- because of the last branch of Check_Private_View (see comment there).
16153 if not Is_Private_Type (BT) then
16154 Prepend_Elmt (Full_View (T), Exchanged_Views);
16155 Exchange_Declarations (T);
16156 return;
16157 end if;
16159 Priv_Elmt := First_Elmt (Private_Dependents (BT));
16161 if Present (Full_View (BT)) then
16162 Prepend_Elmt (Full_View (BT), Exchanged_Views);
16163 Exchange_Declarations (BT);
16164 end if;
16166 while Present (Priv_Elmt) loop
16167 Priv_Sub := (Node (Priv_Elmt));
16169 -- We avoid flipping the subtype if the Etype of its full view is
16170 -- private because this would result in a malformed subtype. This
16171 -- occurs when the Etype of the subtype full view is the full view of
16172 -- the base type (and since the base types were just switched, the
16173 -- subtype is pointing to the wrong view). This is currently the case
16174 -- for tagged record types, access types (maybe more?) and needs to
16175 -- be resolved. ???
16177 if Present (Full_View (Priv_Sub))
16178 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
16179 then
16180 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
16181 Exchange_Declarations (Priv_Sub);
16182 end if;
16184 Next_Elmt (Priv_Elmt);
16185 end loop;
16186 end Switch_View;
16188 -----------------
16189 -- True_Parent --
16190 -----------------
16192 function True_Parent (N : Node_Id) return Node_Id is
16193 begin
16194 if Nkind (Parent (N)) = N_Subunit then
16195 return Parent (Corresponding_Stub (Parent (N)));
16196 else
16197 return Parent (N);
16198 end if;
16199 end True_Parent;
16201 -----------------------------
16202 -- Valid_Default_Attribute --
16203 -----------------------------
16205 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
16206 Attr_Id : constant Attribute_Id :=
16207 Get_Attribute_Id (Attribute_Name (Def));
16208 T : constant Entity_Id := Entity (Prefix (Def));
16209 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
16210 F : Entity_Id;
16211 Num_F : Nat;
16212 OK : Boolean;
16214 begin
16215 if No (T) or else T = Any_Id then
16216 return;
16217 end if;
16219 Num_F := 0;
16220 F := First_Formal (Nam);
16221 while Present (F) loop
16222 Num_F := Num_F + 1;
16223 Next_Formal (F);
16224 end loop;
16226 case Attr_Id is
16227 when Attribute_Adjacent
16228 | Attribute_Ceiling
16229 | Attribute_Copy_Sign
16230 | Attribute_Floor
16231 | Attribute_Fraction
16232 | Attribute_Machine
16233 | Attribute_Model
16234 | Attribute_Remainder
16235 | Attribute_Rounding
16236 | Attribute_Unbiased_Rounding
16238 OK := Is_Fun
16239 and then Num_F = 1
16240 and then Is_Floating_Point_Type (T);
16242 when Attribute_Image
16243 | Attribute_Pred
16244 | Attribute_Succ
16245 | Attribute_Value
16246 | Attribute_Wide_Image
16247 | Attribute_Wide_Value
16249 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
16251 when Attribute_Max
16252 | Attribute_Min
16254 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
16256 when Attribute_Input =>
16257 OK := (Is_Fun and then Num_F = 1);
16259 when Attribute_Output
16260 | Attribute_Read
16261 | Attribute_Write
16263 OK := not Is_Fun and then Num_F = 2;
16265 when others =>
16266 OK := False;
16267 end case;
16269 if not OK then
16270 Error_Msg_N
16271 ("attribute reference has wrong profile for subprogram", Def);
16272 end if;
16273 end Valid_Default_Attribute;
16275 end Sem_Ch12;