gcc/testsuite/ChangeLog:
[official-gcc.git] / gcc / ada / sem_ch12.adb
blob391d1e3ae7c299452e659d8685cf31a30fb9d6f1
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-2018, 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 -- We introduce a renaming of the enclosing package, to have a usable
3477 -- entity as the prefix of an expanded name for a local entity of the
3478 -- form Par.P.Q, where P is the generic package. This is because a local
3479 -- entity named P may hide it, so that the usual visibility rules in
3480 -- the instance will not resolve properly.
3482 Renaming :=
3483 Make_Package_Renaming_Declaration (Loc,
3484 Defining_Unit_Name =>
3485 Make_Defining_Identifier (Loc,
3486 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3487 Name =>
3488 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3490 -- The declaration is inserted before other declarations, but before
3491 -- pragmas that may be library-unit pragmas and must appear before other
3492 -- declarations. The pragma Compile_Time_Error is not in this class, and
3493 -- may contain an expression that includes such a qualified name, so the
3494 -- renaming declaration must appear before it.
3496 -- Are there other pragmas that require this special handling ???
3498 if Present (Decls) then
3499 Decl := First (Decls);
3500 while Present (Decl)
3501 and then Nkind (Decl) = N_Pragma
3502 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
3503 loop
3504 Next (Decl);
3505 end loop;
3507 if Present (Decl) then
3508 Insert_Before (Decl, Renaming);
3509 else
3510 Append (Renaming, Visible_Declarations (Specification (N)));
3511 end if;
3513 else
3514 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3515 end if;
3517 -- Create copy of generic unit, and save for instantiation. If the unit
3518 -- is a child unit, do not copy the specifications for the parent, which
3519 -- are not part of the generic tree.
3521 Save_Parent := Parent_Spec (N);
3522 Set_Parent_Spec (N, Empty);
3524 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3525 Set_Parent_Spec (New_N, Save_Parent);
3526 Rewrite (N, New_N);
3528 -- Once the contents of the generic copy and the template are swapped,
3529 -- do the same for their respective aspect specifications.
3531 Exchange_Aspects (N, New_N);
3533 -- Collect all contract-related source pragmas found within the template
3534 -- and attach them to the contract of the package spec. This contract is
3535 -- used in the capture of global references within annotations.
3537 Create_Generic_Contract (N);
3539 Id := Defining_Entity (N);
3540 Generate_Definition (Id);
3542 -- Expansion is not applied to generic units
3544 Start_Generic;
3546 Enter_Name (Id);
3547 Set_Ekind (Id, E_Generic_Package);
3548 Set_Etype (Id, Standard_Void_Type);
3550 -- Set SPARK_Mode from context
3552 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3553 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3554 Set_SPARK_Pragma_Inherited (Id);
3555 Set_SPARK_Aux_Pragma_Inherited (Id);
3557 -- Preserve relevant elaboration-related attributes of the context which
3558 -- are no longer available or very expensive to recompute once analysis,
3559 -- resolution, and expansion are over.
3561 Mark_Elaboration_Attributes
3562 (N_Id => Id,
3563 Checks => True,
3564 Warnings => True);
3566 -- Analyze aspects now, so that generated pragmas appear in the
3567 -- declarations before building and analyzing the generic copy.
3569 if Has_Aspects (N) then
3570 Analyze_Aspect_Specifications (N, Id);
3571 end if;
3573 Push_Scope (Id);
3574 Enter_Generic_Scope (Id);
3575 Set_Inner_Instances (Id, New_Elmt_List);
3577 Set_Categorization_From_Pragmas (N);
3578 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3580 -- Link the declaration of the generic homonym in the generic copy to
3581 -- the package it renames, so that it is always resolved properly.
3583 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3584 Set_Entity (Associated_Node (Name (Renaming)), Id);
3586 -- For a library unit, we have reconstructed the entity for the unit,
3587 -- and must reset it in the library tables.
3589 if Nkind (Parent (N)) = N_Compilation_Unit then
3590 Set_Cunit_Entity (Current_Sem_Unit, Id);
3591 end if;
3593 Analyze_Generic_Formal_Part (N);
3595 -- After processing the generic formals, analysis proceeds as for a
3596 -- non-generic package.
3598 Analyze (Specification (N));
3600 Validate_Categorization_Dependency (N, Id);
3602 End_Generic;
3604 End_Package_Scope (Id);
3605 Exit_Generic_Scope (Id);
3607 -- If the generic appears within a package unit, the body of that unit
3608 -- has to be present for instantiation and inlining.
3610 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3611 Set_Body_Needed_For_Inlining
3612 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3613 end if;
3615 if Nkind (Parent (N)) /= N_Compilation_Unit then
3616 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3617 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3618 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3620 else
3621 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3622 Validate_RT_RAT_Component (N);
3624 -- If this is a spec without a body, check that generic parameters
3625 -- are referenced.
3627 if not Body_Required (Parent (N)) then
3628 Check_References (Id);
3629 end if;
3630 end if;
3632 -- If there is a specified storage pool in the context, create an
3633 -- aspect on the package declaration, so that it is used in any
3634 -- instance that does not override it.
3636 if Present (Default_Pool) then
3637 declare
3638 ASN : Node_Id;
3640 begin
3641 ASN :=
3642 Make_Aspect_Specification (Loc,
3643 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3644 Expression => New_Copy (Default_Pool));
3646 if No (Aspect_Specifications (Specification (N))) then
3647 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3648 else
3649 Append (ASN, Aspect_Specifications (Specification (N)));
3650 end if;
3651 end;
3652 end if;
3653 end Analyze_Generic_Package_Declaration;
3655 --------------------------------------------
3656 -- Analyze_Generic_Subprogram_Declaration --
3657 --------------------------------------------
3659 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3660 Formals : List_Id;
3661 Id : Entity_Id;
3662 New_N : Node_Id;
3663 Result_Type : Entity_Id;
3664 Save_Parent : Node_Id;
3665 Spec : Node_Id;
3666 Typ : Entity_Id;
3668 begin
3669 Check_SPARK_05_Restriction ("generic is not allowed", N);
3671 -- Create copy of generic unit, and save for instantiation. If the unit
3672 -- is a child unit, do not copy the specifications for the parent, which
3673 -- are not part of the generic tree.
3675 Save_Parent := Parent_Spec (N);
3676 Set_Parent_Spec (N, Empty);
3678 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3679 Set_Parent_Spec (New_N, Save_Parent);
3680 Rewrite (N, New_N);
3682 -- Once the contents of the generic copy and the template are swapped,
3683 -- do the same for their respective aspect specifications.
3685 Exchange_Aspects (N, New_N);
3687 -- Collect all contract-related source pragmas found within the template
3688 -- and attach them to the contract of the subprogram spec. This contract
3689 -- is used in the capture of global references within annotations.
3691 Create_Generic_Contract (N);
3693 Spec := Specification (N);
3694 Id := Defining_Entity (Spec);
3695 Generate_Definition (Id);
3697 if Nkind (Id) = N_Defining_Operator_Symbol then
3698 Error_Msg_N
3699 ("operator symbol not allowed for generic subprogram", Id);
3700 end if;
3702 Start_Generic;
3704 Enter_Name (Id);
3705 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3707 -- Analyze the aspects of the generic copy to ensure that all generated
3708 -- pragmas (if any) perform their semantic effects.
3710 if Has_Aspects (N) then
3711 Analyze_Aspect_Specifications (N, Id);
3712 end if;
3714 Push_Scope (Id);
3715 Enter_Generic_Scope (Id);
3716 Set_Inner_Instances (Id, New_Elmt_List);
3717 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3719 Analyze_Generic_Formal_Part (N);
3721 if Nkind (Spec) = N_Function_Specification then
3722 Set_Ekind (Id, E_Generic_Function);
3723 else
3724 Set_Ekind (Id, E_Generic_Procedure);
3725 end if;
3727 -- Set SPARK_Mode from context
3729 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3730 Set_SPARK_Pragma_Inherited (Id);
3732 -- Preserve relevant elaboration-related attributes of the context which
3733 -- are no longer available or very expensive to recompute once analysis,
3734 -- resolution, and expansion are over.
3736 Mark_Elaboration_Attributes
3737 (N_Id => Id,
3738 Checks => True,
3739 Warnings => True);
3741 Formals := Parameter_Specifications (Spec);
3743 if Present (Formals) then
3744 Process_Formals (Formals, Spec);
3745 end if;
3747 if Nkind (Spec) = N_Function_Specification then
3748 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3749 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3750 Set_Etype (Id, Result_Type);
3752 -- Check restriction imposed by AI05-073: a generic function
3753 -- cannot return an abstract type or an access to such.
3755 -- This is a binding interpretation should it apply to earlier
3756 -- versions of Ada as well as Ada 2012???
3758 if Is_Abstract_Type (Designated_Type (Result_Type))
3759 and then Ada_Version >= Ada_2012
3760 then
3761 Error_Msg_N
3762 ("generic function cannot have an access result "
3763 & "that designates an abstract type", Spec);
3764 end if;
3766 else
3767 Find_Type (Result_Definition (Spec));
3768 Typ := Entity (Result_Definition (Spec));
3770 if Is_Abstract_Type (Typ)
3771 and then Ada_Version >= Ada_2012
3772 then
3773 Error_Msg_N
3774 ("generic function cannot have abstract result type", Spec);
3775 end if;
3777 -- If a null exclusion is imposed on the result type, then create
3778 -- a null-excluding itype (an access subtype) and use it as the
3779 -- function's Etype.
3781 if Is_Access_Type (Typ)
3782 and then Null_Exclusion_Present (Spec)
3783 then
3784 Set_Etype (Id,
3785 Create_Null_Excluding_Itype
3786 (T => Typ,
3787 Related_Nod => Spec,
3788 Scope_Id => Defining_Unit_Name (Spec)));
3789 else
3790 Set_Etype (Id, Typ);
3791 end if;
3792 end if;
3794 else
3795 Set_Etype (Id, Standard_Void_Type);
3796 end if;
3798 -- For a library unit, we have reconstructed the entity for the unit,
3799 -- and must reset it in the library tables. We also make sure that
3800 -- Body_Required is set properly in the original compilation unit node.
3802 if Nkind (Parent (N)) = N_Compilation_Unit then
3803 Set_Cunit_Entity (Current_Sem_Unit, Id);
3804 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3805 end if;
3807 -- If the generic appears within a package unit, the body of that unit
3808 -- has to be present for instantiation and inlining.
3810 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
3811 and then Unit_Requires_Body (Id)
3812 then
3813 Set_Body_Needed_For_Inlining
3814 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3815 end if;
3817 Set_Categorization_From_Pragmas (N);
3818 Validate_Categorization_Dependency (N, Id);
3820 -- Capture all global references that occur within the profile of the
3821 -- generic subprogram. Aspects are not part of this processing because
3822 -- they must be delayed. If processed now, Save_Global_References will
3823 -- destroy the Associated_Node links and prevent the capture of global
3824 -- references when the contract of the generic subprogram is analyzed.
3826 Save_Global_References (Original_Node (N));
3828 End_Generic;
3829 End_Scope;
3830 Exit_Generic_Scope (Id);
3831 Generate_Reference_To_Formals (Id);
3833 List_Inherited_Pre_Post_Aspects (Id);
3834 end Analyze_Generic_Subprogram_Declaration;
3836 -----------------------------------
3837 -- Analyze_Package_Instantiation --
3838 -----------------------------------
3840 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
3841 -- must be replaced by gotos which jump to the end of the routine in order
3842 -- to restore the Ghost and SPARK modes.
3844 procedure Analyze_Package_Instantiation (N : Node_Id) is
3845 Has_Inline_Always : Boolean := False;
3847 procedure Delay_Descriptors (E : Entity_Id);
3848 -- Delay generation of subprogram descriptors for given entity
3850 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
3851 -- If inlining is active and the generic contains inlined subprograms,
3852 -- we instantiate the body. This may cause superfluous instantiations,
3853 -- but it is simpler than detecting the need for the body at the point
3854 -- of inlining, when the context of the instance is not available.
3856 -----------------------
3857 -- Delay_Descriptors --
3858 -----------------------
3860 procedure Delay_Descriptors (E : Entity_Id) is
3861 begin
3862 if not Delay_Subprogram_Descriptors (E) then
3863 Set_Delay_Subprogram_Descriptors (E);
3864 Pending_Descriptor.Append (E);
3865 end if;
3866 end Delay_Descriptors;
3868 -----------------------
3869 -- Might_Inline_Subp --
3870 -----------------------
3872 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
3873 E : Entity_Id;
3875 begin
3876 if not Inline_Processing_Required then
3877 return False;
3879 else
3880 E := First_Entity (Gen_Unit);
3881 while Present (E) loop
3882 if Is_Subprogram (E) and then Is_Inlined (E) then
3883 -- Remember if there are any subprograms with Inline_Always
3885 if Has_Pragma_Inline_Always (E) then
3886 Has_Inline_Always := True;
3887 end if;
3889 return True;
3890 end if;
3892 Next_Entity (E);
3893 end loop;
3894 end if;
3896 return False;
3897 end Might_Inline_Subp;
3899 -- Local declarations
3901 Gen_Id : constant Node_Id := Name (N);
3902 Is_Actual_Pack : constant Boolean :=
3903 Is_Internal (Defining_Entity (N));
3904 Loc : constant Source_Ptr := Sloc (N);
3906 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3907 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3908 Saved_ISMP : constant Boolean :=
3909 Ignore_SPARK_Mode_Pragmas_In_Instance;
3910 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
3911 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
3912 -- Save the Ghost and SPARK mode-related data to restore on exit
3914 Saved_Style_Check : constant Boolean := Style_Check;
3915 -- Save style check mode for restore on exit
3917 Act_Decl : Node_Id;
3918 Act_Decl_Name : Node_Id;
3919 Act_Decl_Id : Entity_Id;
3920 Act_Spec : Node_Id;
3921 Act_Tree : Node_Id;
3922 Env_Installed : Boolean := False;
3923 Gen_Decl : Node_Id;
3924 Gen_Spec : Node_Id;
3925 Gen_Unit : Entity_Id;
3926 Inline_Now : Boolean := False;
3927 Needs_Body : Boolean;
3928 Parent_Installed : Boolean := False;
3929 Renaming_List : List_Id;
3930 Unit_Renaming : Node_Id;
3932 Vis_Prims_List : Elist_Id := No_Elist;
3933 -- List of primitives made temporarily visible in the instantiation
3934 -- to match the visibility of the formal type
3936 -- Start of processing for Analyze_Package_Instantiation
3938 begin
3939 -- Preserve relevant elaboration-related attributes of the context which
3940 -- are no longer available or very expensive to recompute once analysis,
3941 -- resolution, and expansion are over.
3943 Mark_Elaboration_Attributes
3944 (N_Id => N,
3945 Checks => True,
3946 Level => True,
3947 Modes => True,
3948 Warnings => True);
3950 Check_SPARK_05_Restriction ("generic is not allowed", N);
3952 -- Very first thing: check for Text_IO special unit in case we are
3953 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3955 Check_Text_IO_Special_Unit (Name (N));
3957 -- Make node global for error reporting
3959 Instantiation_Node := N;
3961 -- Case of instantiation of a generic package
3963 if Nkind (N) = N_Package_Instantiation then
3964 Act_Decl_Id := New_Copy (Defining_Entity (N));
3965 Set_Comes_From_Source (Act_Decl_Id, True);
3967 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3968 Act_Decl_Name :=
3969 Make_Defining_Program_Unit_Name (Loc,
3970 Name =>
3971 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3972 Defining_Identifier => Act_Decl_Id);
3973 else
3974 Act_Decl_Name := Act_Decl_Id;
3975 end if;
3977 -- Case of instantiation of a formal package
3979 else
3980 Act_Decl_Id := Defining_Identifier (N);
3981 Act_Decl_Name := Act_Decl_Id;
3982 end if;
3984 Generate_Definition (Act_Decl_Id);
3985 Set_Ekind (Act_Decl_Id, E_Package);
3987 -- Initialize list of incomplete actuals before analysis
3989 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
3991 Preanalyze_Actuals (N, Act_Decl_Id);
3993 -- Turn off style checking in instances. If the check is enabled on the
3994 -- generic unit, a warning in an instance would just be noise. If not
3995 -- enabled on the generic, then a warning in an instance is just wrong.
3996 -- This must be done after analyzing the actuals, which do come from
3997 -- source and are subject to style checking.
3999 Style_Check := False;
4001 Init_Env;
4002 Env_Installed := True;
4004 -- Reset renaming map for formal types. The mapping is established
4005 -- when analyzing the generic associations, but some mappings are
4006 -- inherited from formal packages of parent units, and these are
4007 -- constructed when the parents are installed.
4009 Generic_Renamings.Set_Last (0);
4010 Generic_Renamings_HTable.Reset;
4012 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4013 Gen_Unit := Entity (Gen_Id);
4015 -- A package instantiation is Ghost when it is subject to pragma Ghost
4016 -- or the generic template is Ghost. Set the mode now to ensure that
4017 -- any nodes generated during analysis and expansion are marked as
4018 -- Ghost.
4020 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4022 -- Verify that it is the name of a generic package
4024 -- A visibility glitch: if the instance is a child unit and the generic
4025 -- is the generic unit of a parent instance (i.e. both the parent and
4026 -- the child units are instances of the same package) the name now
4027 -- denotes the renaming within the parent, not the intended generic
4028 -- unit. See if there is a homonym that is the desired generic. The
4029 -- renaming declaration must be visible inside the instance of the
4030 -- child, but not when analyzing the name in the instantiation itself.
4032 if Ekind (Gen_Unit) = E_Package
4033 and then Present (Renamed_Entity (Gen_Unit))
4034 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4035 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4036 and then Present (Homonym (Gen_Unit))
4037 then
4038 Gen_Unit := Homonym (Gen_Unit);
4039 end if;
4041 if Etype (Gen_Unit) = Any_Type then
4042 Restore_Env;
4043 goto Leave;
4045 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4047 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4049 if From_Limited_With (Gen_Unit) then
4050 Error_Msg_N
4051 ("cannot instantiate a limited withed package", Gen_Id);
4052 else
4053 Error_Msg_NE
4054 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4055 end if;
4057 Restore_Env;
4058 goto Leave;
4059 end if;
4061 if In_Extended_Main_Source_Unit (N) then
4062 Set_Is_Instantiated (Gen_Unit);
4063 Generate_Reference (Gen_Unit, N);
4065 if Present (Renamed_Object (Gen_Unit)) then
4066 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
4067 Generate_Reference (Renamed_Object (Gen_Unit), N);
4068 end if;
4069 end if;
4071 if Nkind (Gen_Id) = N_Identifier
4072 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4073 then
4074 Error_Msg_NE
4075 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4077 elsif Nkind (Gen_Id) = N_Expanded_Name
4078 and then Is_Child_Unit (Gen_Unit)
4079 and then Nkind (Prefix (Gen_Id)) = N_Identifier
4080 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
4081 then
4082 Error_Msg_N
4083 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
4084 end if;
4086 Set_Entity (Gen_Id, Gen_Unit);
4088 -- If generic is a renaming, get original generic unit
4090 if Present (Renamed_Object (Gen_Unit))
4091 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
4092 then
4093 Gen_Unit := Renamed_Object (Gen_Unit);
4094 end if;
4096 -- Verify that there are no circular instantiations
4098 if In_Open_Scopes (Gen_Unit) then
4099 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4100 Restore_Env;
4101 goto Leave;
4103 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4104 Error_Msg_Node_2 := Current_Scope;
4105 Error_Msg_NE
4106 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4107 Circularity_Detected := True;
4108 Restore_Env;
4109 goto Leave;
4111 else
4112 -- If the context of the instance is subject to SPARK_Mode "off" or
4113 -- the annotation is altogether missing, set the global flag which
4114 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4115 -- the instance.
4117 if SPARK_Mode /= On then
4118 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4120 -- Mark the instance spec in case the body is instantiated at a
4121 -- later pass. This preserves the original context in effect for
4122 -- the body.
4124 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
4125 end if;
4127 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4128 Gen_Spec := Specification (Gen_Decl);
4130 -- Initialize renamings map, for error checking, and the list that
4131 -- holds private entities whose views have changed between generic
4132 -- definition and instantiation. If this is the instance created to
4133 -- validate an actual package, the instantiation environment is that
4134 -- of the enclosing instance.
4136 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
4138 -- Copy original generic tree, to produce text for instantiation
4140 Act_Tree :=
4141 Copy_Generic_Node
4142 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4144 Act_Spec := Specification (Act_Tree);
4146 -- If this is the instance created to validate an actual package,
4147 -- only the formals matter, do not examine the package spec itself.
4149 if Is_Actual_Pack then
4150 Set_Visible_Declarations (Act_Spec, New_List);
4151 Set_Private_Declarations (Act_Spec, New_List);
4152 end if;
4154 Renaming_List :=
4155 Analyze_Associations
4156 (I_Node => N,
4157 Formals => Generic_Formal_Declarations (Act_Tree),
4158 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4160 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4162 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
4163 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
4164 Set_Is_Generic_Instance (Act_Decl_Id);
4165 Set_Generic_Parent (Act_Spec, Gen_Unit);
4167 -- References to the generic in its own declaration or its body are
4168 -- references to the instance. Add a renaming declaration for the
4169 -- generic unit itself. This declaration, as well as the renaming
4170 -- declarations for the generic formals, must remain private to the
4171 -- unit: the formals, because this is the language semantics, and
4172 -- the unit because its use is an artifact of the implementation.
4174 Unit_Renaming :=
4175 Make_Package_Renaming_Declaration (Loc,
4176 Defining_Unit_Name =>
4177 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
4178 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
4180 Append (Unit_Renaming, Renaming_List);
4182 -- The renaming declarations are the first local declarations of the
4183 -- new unit.
4185 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
4186 Insert_List_Before
4187 (First (Visible_Declarations (Act_Spec)), Renaming_List);
4188 else
4189 Set_Visible_Declarations (Act_Spec, Renaming_List);
4190 end if;
4192 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
4194 -- Propagate the aspect specifications from the package declaration
4195 -- template to the instantiated version of the package declaration.
4197 if Has_Aspects (Act_Tree) then
4198 Set_Aspect_Specifications (Act_Decl,
4199 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4200 end if;
4202 -- The generic may have a generated Default_Storage_Pool aspect,
4203 -- set at the point of generic declaration. If the instance has
4204 -- that aspect, it overrides the one inherited from the generic.
4206 if Has_Aspects (Gen_Spec) then
4207 if No (Aspect_Specifications (N)) then
4208 Set_Aspect_Specifications (N,
4209 (New_Copy_List_Tree
4210 (Aspect_Specifications (Gen_Spec))));
4212 else
4213 declare
4214 Inherited_Aspects : constant List_Id :=
4215 New_Copy_List_Tree
4216 (Aspect_Specifications (Gen_Spec));
4218 ASN1 : Node_Id;
4219 ASN2 : Node_Id;
4220 Pool_Present : Boolean := False;
4222 begin
4223 ASN1 := First (Aspect_Specifications (N));
4224 while Present (ASN1) loop
4225 if Chars (Identifier (ASN1)) =
4226 Name_Default_Storage_Pool
4227 then
4228 Pool_Present := True;
4229 exit;
4230 end if;
4232 Next (ASN1);
4233 end loop;
4235 if Pool_Present then
4237 -- If generic carries a default storage pool, remove it
4238 -- in favor of the instance one.
4240 ASN2 := First (Inherited_Aspects);
4241 while Present (ASN2) loop
4242 if Chars (Identifier (ASN2)) =
4243 Name_Default_Storage_Pool
4244 then
4245 Remove (ASN2);
4246 exit;
4247 end if;
4249 Next (ASN2);
4250 end loop;
4251 end if;
4253 Prepend_List_To
4254 (Aspect_Specifications (N), Inherited_Aspects);
4255 end;
4256 end if;
4257 end if;
4259 -- Save the instantiation node, for subsequent instantiation of the
4260 -- body, if there is one and we are generating code for the current
4261 -- unit. Mark unit as having a body (avoids premature error message).
4263 -- We instantiate the body if we are generating code, if we are
4264 -- generating cross-reference information, or if we are building
4265 -- trees for ASIS use or GNATprove use.
4267 declare
4268 Enclosing_Body_Present : Boolean := False;
4269 -- If the generic unit is not a compilation unit, then a body may
4270 -- be present in its parent even if none is required. We create a
4271 -- tentative pending instantiation for the body, which will be
4272 -- discarded if none is actually present.
4274 Scop : Entity_Id;
4276 begin
4277 if Scope (Gen_Unit) /= Standard_Standard
4278 and then not Is_Child_Unit (Gen_Unit)
4279 then
4280 Scop := Scope (Gen_Unit);
4281 while Present (Scop) and then Scop /= Standard_Standard loop
4282 if Unit_Requires_Body (Scop) then
4283 Enclosing_Body_Present := True;
4284 exit;
4286 elsif In_Open_Scopes (Scop)
4287 and then In_Package_Body (Scop)
4288 then
4289 Enclosing_Body_Present := True;
4290 exit;
4291 end if;
4293 exit when Is_Compilation_Unit (Scop);
4294 Scop := Scope (Scop);
4295 end loop;
4296 end if;
4298 -- If front-end inlining is enabled or there are any subprograms
4299 -- marked with Inline_Always, and this is a unit for which code
4300 -- will be generated, we instantiate the body at once.
4302 -- This is done if the instance is not the main unit, and if the
4303 -- generic is not a child unit of another generic, to avoid scope
4304 -- problems and the reinstallation of parent instances.
4306 if Expander_Active
4307 and then (not Is_Child_Unit (Gen_Unit)
4308 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4309 and then Might_Inline_Subp (Gen_Unit)
4310 and then not Is_Actual_Pack
4311 then
4312 if not Back_End_Inlining
4313 and then (Front_End_Inlining or else Has_Inline_Always)
4314 and then (Is_In_Main_Unit (N)
4315 or else In_Main_Context (Current_Scope))
4316 and then Nkind (Parent (N)) /= N_Compilation_Unit
4317 then
4318 Inline_Now := True;
4320 -- In configurable_run_time mode we force the inlining of
4321 -- predefined subprograms marked Inline_Always, to minimize
4322 -- the use of the run-time library.
4324 elsif In_Predefined_Unit (Gen_Decl)
4325 and then Configurable_Run_Time_Mode
4326 and then Nkind (Parent (N)) /= N_Compilation_Unit
4327 then
4328 Inline_Now := True;
4329 end if;
4331 -- If the current scope is itself an instance within a child
4332 -- unit, there will be duplications in the scope stack, and the
4333 -- unstacking mechanism in Inline_Instance_Body will fail.
4334 -- This loses some rare cases of optimization, and might be
4335 -- improved some day, if we can find a proper abstraction for
4336 -- "the complete compilation context" that can be saved and
4337 -- restored. ???
4339 if Is_Generic_Instance (Current_Scope) then
4340 declare
4341 Curr_Unit : constant Entity_Id :=
4342 Cunit_Entity (Current_Sem_Unit);
4343 begin
4344 if Curr_Unit /= Current_Scope
4345 and then Is_Child_Unit (Curr_Unit)
4346 then
4347 Inline_Now := False;
4348 end if;
4349 end;
4350 end if;
4351 end if;
4353 Needs_Body :=
4354 (Unit_Requires_Body (Gen_Unit)
4355 or else Enclosing_Body_Present
4356 or else Present (Corresponding_Body (Gen_Decl)))
4357 and then (Is_In_Main_Unit (N)
4358 or else Might_Inline_Subp (Gen_Unit))
4359 and then not Is_Actual_Pack
4360 and then not Inline_Now
4361 and then (Operating_Mode = Generate_Code
4363 -- Need comment for this check ???
4365 or else (Operating_Mode = Check_Semantics
4366 and then (ASIS_Mode or GNATprove_Mode)));
4368 -- If front-end inlining is enabled or there are any subprograms
4369 -- marked with Inline_Always, do not instantiate body when within
4370 -- a generic context.
4372 if ((Front_End_Inlining or else Has_Inline_Always)
4373 and then not Expander_Active)
4374 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
4375 then
4376 Needs_Body := False;
4377 end if;
4379 -- If the current context is generic, and the package being
4380 -- instantiated is declared within a formal package, there is no
4381 -- body to instantiate until the enclosing generic is instantiated
4382 -- and there is an actual for the formal package. If the formal
4383 -- package has parameters, we build a regular package instance for
4384 -- it, that precedes the original formal package declaration.
4386 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4387 declare
4388 Decl : constant Node_Id :=
4389 Original_Node
4390 (Unit_Declaration_Node (Scope (Gen_Unit)));
4391 begin
4392 if Nkind (Decl) = N_Formal_Package_Declaration
4393 or else (Nkind (Decl) = N_Package_Declaration
4394 and then Is_List_Member (Decl)
4395 and then Present (Next (Decl))
4396 and then
4397 Nkind (Next (Decl)) =
4398 N_Formal_Package_Declaration)
4399 then
4400 Needs_Body := False;
4401 end if;
4402 end;
4403 end if;
4404 end;
4406 -- For RCI unit calling stubs, we omit the instance body if the
4407 -- instance is the RCI library unit itself.
4409 -- However there is a special case for nested instances: in this case
4410 -- we do generate the instance body, as it might be required, e.g.
4411 -- because it provides stream attributes for some type used in the
4412 -- profile of a remote subprogram. This is consistent with 12.3(12),
4413 -- which indicates that the instance body occurs at the place of the
4414 -- instantiation, and thus is part of the RCI declaration, which is
4415 -- present on all client partitions (this is E.2.3(18)).
4417 -- Note that AI12-0002 may make it illegal at some point to have
4418 -- stream attributes defined in an RCI unit, in which case this
4419 -- special case will become unnecessary. In the meantime, there
4420 -- is known application code in production that depends on this
4421 -- being possible, so we definitely cannot eliminate the body in
4422 -- the case of nested instances for the time being.
4424 -- When we generate a nested instance body, calling stubs for any
4425 -- relevant subprogram will be be inserted immediately after the
4426 -- subprogram declarations, and will take precedence over the
4427 -- subsequent (original) body. (The stub and original body will be
4428 -- complete homographs, but this is permitted in an instance).
4429 -- (Could we do better and remove the original body???)
4431 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4432 and then Comes_From_Source (N)
4433 and then Nkind (Parent (N)) = N_Compilation_Unit
4434 then
4435 Needs_Body := False;
4436 end if;
4438 if Needs_Body then
4440 -- Here is a defence against a ludicrous number of instantiations
4441 -- caused by a circular set of instantiation attempts.
4443 if Pending_Instantiations.Last > Maximum_Instantiations then
4444 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4445 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4446 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4447 raise Unrecoverable_Error;
4448 end if;
4450 -- Indicate that the enclosing scopes contain an instantiation,
4451 -- and that cleanup actions should be delayed until after the
4452 -- instance body is expanded.
4454 Check_Forward_Instantiation (Gen_Decl);
4455 if Nkind (N) = N_Package_Instantiation then
4456 declare
4457 Enclosing_Master : Entity_Id;
4459 begin
4460 -- Loop to search enclosing masters
4462 Enclosing_Master := Current_Scope;
4463 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4464 if Ekind (Enclosing_Master) = E_Package then
4465 if Is_Compilation_Unit (Enclosing_Master) then
4466 if In_Package_Body (Enclosing_Master) then
4467 Delay_Descriptors
4468 (Body_Entity (Enclosing_Master));
4469 else
4470 Delay_Descriptors
4471 (Enclosing_Master);
4472 end if;
4474 exit Scope_Loop;
4476 else
4477 Enclosing_Master := Scope (Enclosing_Master);
4478 end if;
4480 elsif Is_Generic_Unit (Enclosing_Master)
4481 or else Ekind (Enclosing_Master) = E_Void
4482 then
4483 -- Cleanup actions will eventually be performed on the
4484 -- enclosing subprogram or package instance, if any.
4485 -- Enclosing scope is void in the formal part of a
4486 -- generic subprogram.
4488 exit Scope_Loop;
4490 else
4491 if Ekind (Enclosing_Master) = E_Entry
4492 and then
4493 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4494 then
4495 if not Expander_Active then
4496 exit Scope_Loop;
4497 else
4498 Enclosing_Master :=
4499 Protected_Body_Subprogram (Enclosing_Master);
4500 end if;
4501 end if;
4503 Set_Delay_Cleanups (Enclosing_Master);
4505 while Ekind (Enclosing_Master) = E_Block loop
4506 Enclosing_Master := Scope (Enclosing_Master);
4507 end loop;
4509 if Is_Subprogram (Enclosing_Master) then
4510 Delay_Descriptors (Enclosing_Master);
4512 elsif Is_Task_Type (Enclosing_Master) then
4513 declare
4514 TBP : constant Node_Id :=
4515 Get_Task_Body_Procedure
4516 (Enclosing_Master);
4517 begin
4518 if Present (TBP) then
4519 Delay_Descriptors (TBP);
4520 Set_Delay_Cleanups (TBP);
4521 end if;
4522 end;
4523 end if;
4525 exit Scope_Loop;
4526 end if;
4527 end loop Scope_Loop;
4528 end;
4530 -- Make entry in table
4532 Add_Pending_Instantiation (N, Act_Decl);
4533 end if;
4534 end if;
4536 Set_Categorization_From_Pragmas (Act_Decl);
4538 if Parent_Installed then
4539 Hide_Current_Scope;
4540 end if;
4542 Set_Instance_Spec (N, Act_Decl);
4544 -- If not a compilation unit, insert the package declaration before
4545 -- the original instantiation node.
4547 if Nkind (Parent (N)) /= N_Compilation_Unit then
4548 Mark_Rewrite_Insertion (Act_Decl);
4549 Insert_Before (N, Act_Decl);
4551 if Has_Aspects (N) then
4552 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4554 -- The pragma created for a Default_Storage_Pool aspect must
4555 -- appear ahead of the declarations in the instance spec.
4556 -- Analysis has placed it after the instance node, so remove
4557 -- it and reinsert it properly now.
4559 declare
4560 ASN : constant Node_Id := First (Aspect_Specifications (N));
4561 A_Name : constant Name_Id := Chars (Identifier (ASN));
4562 Decl : Node_Id;
4564 begin
4565 if A_Name = Name_Default_Storage_Pool then
4566 if No (Visible_Declarations (Act_Spec)) then
4567 Set_Visible_Declarations (Act_Spec, New_List);
4568 end if;
4570 Decl := Next (N);
4571 while Present (Decl) loop
4572 if Nkind (Decl) = N_Pragma then
4573 Remove (Decl);
4574 Prepend (Decl, Visible_Declarations (Act_Spec));
4575 exit;
4576 end if;
4578 Next (Decl);
4579 end loop;
4580 end if;
4581 end;
4582 end if;
4584 Analyze (Act_Decl);
4586 -- For an instantiation that is a compilation unit, place
4587 -- declaration on current node so context is complete for analysis
4588 -- (including nested instantiations). If this is the main unit,
4589 -- the declaration eventually replaces the instantiation node.
4590 -- If the instance body is created later, it replaces the
4591 -- instance node, and the declaration is attached to it
4592 -- (see Build_Instance_Compilation_Unit_Nodes).
4594 else
4595 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4597 -- The entity for the current unit is the newly created one,
4598 -- and all semantic information is attached to it.
4600 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4602 -- If this is the main unit, replace the main entity as well
4604 if Current_Sem_Unit = Main_Unit then
4605 Main_Unit_Entity := Act_Decl_Id;
4606 end if;
4607 end if;
4609 Set_Unit (Parent (N), Act_Decl);
4610 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4611 Set_Package_Instantiation (Act_Decl_Id, N);
4613 -- Process aspect specifications of the instance node, if any, to
4614 -- take into account categorization pragmas before analyzing the
4615 -- instance.
4617 if Has_Aspects (N) then
4618 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4619 end if;
4621 Analyze (Act_Decl);
4622 Set_Unit (Parent (N), N);
4623 Set_Body_Required (Parent (N), False);
4625 -- We never need elaboration checks on instantiations, since by
4626 -- definition, the body instantiation is elaborated at the same
4627 -- time as the spec instantiation.
4629 if Legacy_Elaboration_Checks then
4630 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4631 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4632 end if;
4633 end if;
4635 if Legacy_Elaboration_Checks then
4636 Check_Elab_Instantiation (N);
4637 end if;
4639 -- Save the scenario for later examination by the ABE Processing
4640 -- phase.
4642 Record_Elaboration_Scenario (N);
4644 -- The instantiation results in a guaranteed ABE
4646 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
4648 -- Do not instantiate the corresponding body because gigi cannot
4649 -- handle certain types of premature instantiations.
4651 Pending_Instantiations.Decrement_Last;
4653 -- Create completing bodies for all subprogram declarations since
4654 -- their real bodies will not be instantiated.
4656 Provide_Completing_Bodies (Instance_Spec (N));
4657 end if;
4659 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4661 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4662 First_Private_Entity (Act_Decl_Id));
4664 -- If the instantiation will receive a body, the unit will be
4665 -- transformed into a package body, and receive its own elaboration
4666 -- entity. Otherwise, the nature of the unit is now a package
4667 -- declaration.
4669 if Nkind (Parent (N)) = N_Compilation_Unit
4670 and then not Needs_Body
4671 then
4672 Rewrite (N, Act_Decl);
4673 end if;
4675 if Present (Corresponding_Body (Gen_Decl))
4676 or else Unit_Requires_Body (Gen_Unit)
4677 then
4678 Set_Has_Completion (Act_Decl_Id);
4679 end if;
4681 Check_Formal_Packages (Act_Decl_Id);
4683 Restore_Hidden_Primitives (Vis_Prims_List);
4684 Restore_Private_Views (Act_Decl_Id);
4686 Inherit_Context (Gen_Decl, N);
4688 if Parent_Installed then
4689 Remove_Parent;
4690 end if;
4692 Restore_Env;
4693 Env_Installed := False;
4694 end if;
4696 Validate_Categorization_Dependency (N, Act_Decl_Id);
4698 -- There used to be a check here to prevent instantiations in local
4699 -- contexts if the No_Local_Allocators restriction was active. This
4700 -- check was removed by a binding interpretation in AI-95-00130/07,
4701 -- but we retain the code for documentation purposes.
4703 -- if Ekind (Act_Decl_Id) /= E_Void
4704 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4705 -- then
4706 -- Check_Restriction (No_Local_Allocators, N);
4707 -- end if;
4709 if Inline_Now then
4710 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4711 end if;
4713 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4714 -- be used as defining identifiers for a formal package and for the
4715 -- corresponding expanded package.
4717 if Nkind (N) = N_Formal_Package_Declaration then
4718 Act_Decl_Id := New_Copy (Defining_Entity (N));
4719 Set_Comes_From_Source (Act_Decl_Id, True);
4720 Set_Is_Generic_Instance (Act_Decl_Id, False);
4721 Set_Defining_Identifier (N, Act_Decl_Id);
4722 end if;
4724 -- Check that if N is an instantiation of System.Dim_Float_IO or
4725 -- System.Dim_Integer_IO, the formal type has a dimension system.
4727 if Nkind (N) = N_Package_Instantiation
4728 and then Is_Dim_IO_Package_Instantiation (N)
4729 then
4730 declare
4731 Assoc : constant Node_Id := First (Generic_Associations (N));
4732 begin
4733 if not Has_Dimension_System
4734 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4735 then
4736 Error_Msg_N ("type with a dimension system expected", Assoc);
4737 end if;
4738 end;
4739 end if;
4741 <<Leave>>
4742 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4743 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4744 end if;
4746 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4747 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4748 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4749 Style_Check := Saved_Style_Check;
4751 exception
4752 when Instantiation_Error =>
4753 if Parent_Installed then
4754 Remove_Parent;
4755 end if;
4757 if Env_Installed then
4758 Restore_Env;
4759 end if;
4761 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4762 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4763 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4764 Style_Check := Saved_Style_Check;
4765 end Analyze_Package_Instantiation;
4767 --------------------------
4768 -- Inline_Instance_Body --
4769 --------------------------
4771 -- WARNING: This routine manages SPARK regions. Return statements must be
4772 -- replaced by gotos which jump to the end of the routine and restore the
4773 -- SPARK mode.
4775 procedure Inline_Instance_Body
4776 (N : Node_Id;
4777 Gen_Unit : Entity_Id;
4778 Act_Decl : Node_Id)
4780 Config_Attrs : constant Config_Switches_Type := Save_Config_Switches;
4782 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4783 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4784 Gen_Comp : constant Entity_Id :=
4785 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4787 Scope_Stack_Depth : constant Pos :=
4788 Scope_Stack.Last - Scope_Stack.First + 1;
4790 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4791 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4792 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4794 Curr_Scope : Entity_Id := Empty;
4795 List : Elist_Id := No_Elist; -- init to avoid warning
4796 N_Instances : Nat := 0;
4797 Num_Inner : Nat := 0;
4798 Num_Scopes : Nat := 0;
4799 Removed : Boolean := False;
4800 S : Entity_Id;
4801 Vis : Boolean;
4803 begin
4804 -- Case of generic unit defined in another unit. We must remove the
4805 -- complete context of the current unit to install that of the generic.
4807 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4809 -- Add some comments for the following two loops ???
4811 S := Current_Scope;
4812 while Present (S) and then S /= Standard_Standard loop
4813 loop
4814 Num_Scopes := Num_Scopes + 1;
4816 Use_Clauses (Num_Scopes) :=
4817 (Scope_Stack.Table
4818 (Scope_Stack.Last - Num_Scopes + 1).
4819 First_Use_Clause);
4820 End_Use_Clauses (Use_Clauses (Num_Scopes));
4822 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4823 or else Scope_Stack.Table
4824 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4825 end loop;
4827 exit when Is_Generic_Instance (S)
4828 and then (In_Package_Body (S)
4829 or else Ekind (S) = E_Procedure
4830 or else Ekind (S) = E_Function);
4831 S := Scope (S);
4832 end loop;
4834 Vis := Is_Immediately_Visible (Gen_Comp);
4836 -- Find and save all enclosing instances
4838 S := Current_Scope;
4840 while Present (S)
4841 and then S /= Standard_Standard
4842 loop
4843 if Is_Generic_Instance (S) then
4844 N_Instances := N_Instances + 1;
4845 Instances (N_Instances) := S;
4847 exit when In_Package_Body (S);
4848 end if;
4850 S := Scope (S);
4851 end loop;
4853 -- Remove context of current compilation unit, unless we are within a
4854 -- nested package instantiation, in which case the context has been
4855 -- removed previously.
4857 -- If current scope is the body of a child unit, remove context of
4858 -- spec as well. If an enclosing scope is an instance body, the
4859 -- context has already been removed, but the entities in the body
4860 -- must be made invisible as well.
4862 S := Current_Scope;
4863 while Present (S) and then S /= Standard_Standard loop
4864 if Is_Generic_Instance (S)
4865 and then (In_Package_Body (S)
4866 or else Ekind_In (S, E_Procedure, E_Function))
4867 then
4868 -- We still have to remove the entities of the enclosing
4869 -- instance from direct visibility.
4871 declare
4872 E : Entity_Id;
4873 begin
4874 E := First_Entity (S);
4875 while Present (E) loop
4876 Set_Is_Immediately_Visible (E, False);
4877 Next_Entity (E);
4878 end loop;
4879 end;
4881 exit;
4882 end if;
4884 if S = Curr_Unit
4885 or else (Ekind (Curr_Unit) = E_Package_Body
4886 and then S = Spec_Entity (Curr_Unit))
4887 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4888 and then S = Corresponding_Spec
4889 (Unit_Declaration_Node (Curr_Unit)))
4890 then
4891 Removed := True;
4893 -- Remove entities in current scopes from visibility, so that
4894 -- instance body is compiled in a clean environment.
4896 List := Save_Scope_Stack (Handle_Use => False);
4898 if Is_Child_Unit (S) then
4900 -- Remove child unit from stack, as well as inner scopes.
4901 -- Removing the context of a child unit removes parent units
4902 -- as well.
4904 while Current_Scope /= S loop
4905 Num_Inner := Num_Inner + 1;
4906 Inner_Scopes (Num_Inner) := Current_Scope;
4907 Pop_Scope;
4908 end loop;
4910 Pop_Scope;
4911 Remove_Context (Curr_Comp);
4912 Curr_Scope := S;
4914 else
4915 Remove_Context (Curr_Comp);
4916 end if;
4918 if Ekind (Curr_Unit) = E_Package_Body then
4919 Remove_Context (Library_Unit (Curr_Comp));
4920 end if;
4921 end if;
4923 S := Scope (S);
4924 end loop;
4926 pragma Assert (Num_Inner < Num_Scopes);
4928 Push_Scope (Standard_Standard);
4929 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4931 -- The inlined package body is analyzed with the configuration state
4932 -- of the context prior to the scope manipulations performed above.
4934 -- ??? shouldn't this also use the warning state of the context prior
4935 -- to the scope manipulations?
4937 Instantiate_Package_Body
4938 (Body_Info =>
4939 ((Act_Decl => Act_Decl,
4940 Config_Switches => Config_Attrs,
4941 Current_Sem_Unit => Current_Sem_Unit,
4942 Expander_Status => Expander_Active,
4943 Inst_Node => N,
4944 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4945 Scope_Suppress => Scope_Suppress,
4946 Warnings => Save_Warnings)),
4947 Inlined_Body => True);
4949 Pop_Scope;
4951 -- Restore context
4953 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4955 -- Reset Generic_Instance flag so that use clauses can be installed
4956 -- in the proper order. (See Use_One_Package for effect of enclosing
4957 -- instances on processing of use clauses).
4959 for J in 1 .. N_Instances loop
4960 Set_Is_Generic_Instance (Instances (J), False);
4961 end loop;
4963 if Removed then
4964 Install_Context (Curr_Comp, Chain => False);
4966 if Present (Curr_Scope)
4967 and then Is_Child_Unit (Curr_Scope)
4968 then
4969 Push_Scope (Curr_Scope);
4970 Set_Is_Immediately_Visible (Curr_Scope);
4972 -- Finally, restore inner scopes as well
4974 for J in reverse 1 .. Num_Inner loop
4975 Push_Scope (Inner_Scopes (J));
4976 end loop;
4977 end if;
4979 Restore_Scope_Stack (List, Handle_Use => False);
4981 if Present (Curr_Scope)
4982 and then
4983 (In_Private_Part (Curr_Scope)
4984 or else In_Package_Body (Curr_Scope))
4985 then
4986 -- Install private declaration of ancestor units, which are
4987 -- currently available. Restore_Scope_Stack and Install_Context
4988 -- only install the visible part of parents.
4990 declare
4991 Par : Entity_Id;
4992 begin
4993 Par := Scope (Curr_Scope);
4994 while (Present (Par)) and then Par /= Standard_Standard loop
4995 Install_Private_Declarations (Par);
4996 Par := Scope (Par);
4997 end loop;
4998 end;
4999 end if;
5000 end if;
5002 -- Restore use clauses. For a child unit, use clauses in the parents
5003 -- are restored when installing the context, so only those in inner
5004 -- scopes (and those local to the child unit itself) need to be
5005 -- installed explicitly.
5007 if Is_Child_Unit (Curr_Unit) and then Removed then
5008 for J in reverse 1 .. Num_Inner + 1 loop
5009 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5010 Use_Clauses (J);
5011 Install_Use_Clauses (Use_Clauses (J));
5012 end loop;
5014 else
5015 for J in reverse 1 .. Num_Scopes loop
5016 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5017 Use_Clauses (J);
5018 Install_Use_Clauses (Use_Clauses (J));
5019 end loop;
5020 end if;
5022 -- Restore status of instances. If one of them is a body, make its
5023 -- local entities visible again.
5025 declare
5026 E : Entity_Id;
5027 Inst : Entity_Id;
5029 begin
5030 for J in 1 .. N_Instances loop
5031 Inst := Instances (J);
5032 Set_Is_Generic_Instance (Inst, True);
5034 if In_Package_Body (Inst)
5035 or else Ekind_In (S, E_Procedure, E_Function)
5036 then
5037 E := First_Entity (Instances (J));
5038 while Present (E) loop
5039 Set_Is_Immediately_Visible (E);
5040 Next_Entity (E);
5041 end loop;
5042 end if;
5043 end loop;
5044 end;
5046 -- If generic unit is in current unit, current context is correct. Note
5047 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5048 -- enclosing scopes were removed.
5050 else
5051 Instantiate_Package_Body
5052 (Body_Info =>
5053 ((Act_Decl => Act_Decl,
5054 Config_Switches => Save_Config_Switches,
5055 Current_Sem_Unit => Current_Sem_Unit,
5056 Expander_Status => Expander_Active,
5057 Inst_Node => N,
5058 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5059 Scope_Suppress => Scope_Suppress,
5060 Warnings => Save_Warnings)),
5061 Inlined_Body => True);
5062 end if;
5063 end Inline_Instance_Body;
5065 -------------------------------------
5066 -- Analyze_Procedure_Instantiation --
5067 -------------------------------------
5069 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5070 begin
5071 Analyze_Subprogram_Instantiation (N, E_Procedure);
5072 end Analyze_Procedure_Instantiation;
5074 -----------------------------------
5075 -- Need_Subprogram_Instance_Body --
5076 -----------------------------------
5078 function Need_Subprogram_Instance_Body
5079 (N : Node_Id;
5080 Subp : Entity_Id) return Boolean
5082 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
5083 -- Return True if E is an inlined subprogram, an inlined renaming or a
5084 -- subprogram nested in an inlined subprogram. The inlining machinery
5085 -- totally disregards nested subprograms since it considers that they
5086 -- will always be compiled if the parent is (see Inline.Is_Nested).
5088 ------------------------------------
5089 -- Is_Inlined_Or_Child_Of_Inlined --
5090 ------------------------------------
5092 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
5093 Scop : Entity_Id;
5095 begin
5096 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
5097 return True;
5098 end if;
5100 Scop := Scope (E);
5101 while Scop /= Standard_Standard loop
5102 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
5103 return True;
5104 end if;
5106 Scop := Scope (Scop);
5107 end loop;
5109 return False;
5110 end Is_Inlined_Or_Child_Of_Inlined;
5112 begin
5113 -- Must be in the main unit or inlined (or child of inlined)
5115 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
5117 -- Must be generating code or analyzing code in ASIS/GNATprove mode
5119 and then (Operating_Mode = Generate_Code
5120 or else (Operating_Mode = Check_Semantics
5121 and then (ASIS_Mode or GNATprove_Mode)))
5123 -- The body is needed when generating code (full expansion), in ASIS
5124 -- mode for other tools, and in GNATprove mode (special expansion) for
5125 -- formal verification of the body itself.
5127 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
5129 -- No point in inlining if ABE is inevitable
5131 and then not Is_Known_Guaranteed_ABE (N)
5133 -- Or if subprogram is eliminated
5135 and then not Is_Eliminated (Subp)
5136 then
5137 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
5138 return True;
5140 -- Here if not inlined, or we ignore the inlining
5142 else
5143 return False;
5144 end if;
5145 end Need_Subprogram_Instance_Body;
5147 --------------------------------------
5148 -- Analyze_Subprogram_Instantiation --
5149 --------------------------------------
5151 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5152 -- must be replaced by gotos which jump to the end of the routine in order
5153 -- to restore the Ghost and SPARK modes.
5155 procedure Analyze_Subprogram_Instantiation
5156 (N : Node_Id;
5157 K : Entity_Kind)
5159 Loc : constant Source_Ptr := Sloc (N);
5160 Gen_Id : constant Node_Id := Name (N);
5161 Errs : constant Nat := Serious_Errors_Detected;
5163 Anon_Id : constant Entity_Id :=
5164 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
5165 Chars => New_External_Name
5166 (Chars (Defining_Entity (N)), 'R'));
5168 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
5169 Act_Decl : Node_Id;
5170 Act_Spec : Node_Id;
5171 Act_Tree : Node_Id;
5173 Env_Installed : Boolean := False;
5174 Gen_Unit : Entity_Id;
5175 Gen_Decl : Node_Id;
5176 Pack_Id : Entity_Id;
5177 Parent_Installed : Boolean := False;
5179 Renaming_List : List_Id;
5180 -- The list of declarations that link formals and actuals of the
5181 -- instance. These are subtype declarations for formal types, and
5182 -- renaming declarations for other formals. The subprogram declaration
5183 -- for the instance is then appended to the list, and the last item on
5184 -- the list is the renaming declaration for the instance.
5186 procedure Analyze_Instance_And_Renamings;
5187 -- The instance must be analyzed in a context that includes the mappings
5188 -- of generic parameters into actuals. We create a package declaration
5189 -- for this purpose, and a subprogram with an internal name within the
5190 -- package. The subprogram instance is simply an alias for the internal
5191 -- subprogram, declared in the current scope.
5193 procedure Build_Subprogram_Renaming;
5194 -- If the subprogram is recursive, there are occurrences of the name of
5195 -- the generic within the body, which must resolve to the current
5196 -- instance. We add a renaming declaration after the declaration, which
5197 -- is available in the instance body, as well as in the analysis of
5198 -- aspects that appear in the generic. This renaming declaration is
5199 -- inserted after the instance declaration which it renames.
5201 ------------------------------------
5202 -- Analyze_Instance_And_Renamings --
5203 ------------------------------------
5205 procedure Analyze_Instance_And_Renamings is
5206 Def_Ent : constant Entity_Id := Defining_Entity (N);
5207 Pack_Decl : Node_Id;
5209 begin
5210 if Nkind (Parent (N)) = N_Compilation_Unit then
5212 -- For the case of a compilation unit, the container package has
5213 -- the same name as the instantiation, to insure that the binder
5214 -- calls the elaboration procedure with the right name. Copy the
5215 -- entity of the instance, which may have compilation level flags
5216 -- (e.g. Is_Child_Unit) set.
5218 Pack_Id := New_Copy (Def_Ent);
5220 else
5221 -- Otherwise we use the name of the instantiation concatenated
5222 -- with its source position to ensure uniqueness if there are
5223 -- several instantiations with the same name.
5225 Pack_Id :=
5226 Make_Defining_Identifier (Loc,
5227 Chars => New_External_Name
5228 (Related_Id => Chars (Def_Ent),
5229 Suffix => "GP",
5230 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
5231 end if;
5233 Pack_Decl :=
5234 Make_Package_Declaration (Loc,
5235 Specification => Make_Package_Specification (Loc,
5236 Defining_Unit_Name => Pack_Id,
5237 Visible_Declarations => Renaming_List,
5238 End_Label => Empty));
5240 Set_Instance_Spec (N, Pack_Decl);
5241 Set_Is_Generic_Instance (Pack_Id);
5242 Set_Debug_Info_Needed (Pack_Id);
5244 -- Case of not a compilation unit
5246 if Nkind (Parent (N)) /= N_Compilation_Unit then
5247 Mark_Rewrite_Insertion (Pack_Decl);
5248 Insert_Before (N, Pack_Decl);
5249 Set_Has_Completion (Pack_Id);
5251 -- Case of an instantiation that is a compilation unit
5253 -- Place declaration on current node so context is complete for
5254 -- analysis (including nested instantiations), and for use in a
5255 -- context_clause (see Analyze_With_Clause).
5257 else
5258 Set_Unit (Parent (N), Pack_Decl);
5259 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
5260 end if;
5262 Analyze (Pack_Decl);
5263 Check_Formal_Packages (Pack_Id);
5264 Set_Is_Generic_Instance (Pack_Id, False);
5266 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
5267 -- above???
5269 -- Body of the enclosing package is supplied when instantiating the
5270 -- subprogram body, after semantic analysis is completed.
5272 if Nkind (Parent (N)) = N_Compilation_Unit then
5274 -- Remove package itself from visibility, so it does not
5275 -- conflict with subprogram.
5277 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5279 -- Set name and scope of internal subprogram so that the proper
5280 -- external name will be generated. The proper scope is the scope
5281 -- of the wrapper package. We need to generate debugging info for
5282 -- the internal subprogram, so set flag accordingly.
5284 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5285 Set_Scope (Anon_Id, Scope (Pack_Id));
5287 -- Mark wrapper package as referenced, to avoid spurious warnings
5288 -- if the instantiation appears in various with_ clauses of
5289 -- subunits of the main unit.
5291 Set_Referenced (Pack_Id);
5292 end if;
5294 Set_Is_Generic_Instance (Anon_Id);
5295 Set_Debug_Info_Needed (Anon_Id);
5296 Act_Decl_Id := New_Copy (Anon_Id);
5298 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5299 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5300 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5302 -- Subprogram instance comes from source only if generic does
5304 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
5306 -- If the instance is a child unit, mark the Id accordingly. Mark
5307 -- the anonymous entity as well, which is the real subprogram and
5308 -- which is used when the instance appears in a context clause.
5309 -- Similarly, propagate the Is_Eliminated flag to handle properly
5310 -- nested eliminated subprograms.
5312 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5313 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5314 New_Overloaded_Entity (Act_Decl_Id);
5315 Check_Eliminated (Act_Decl_Id);
5316 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5318 if Nkind (Parent (N)) = N_Compilation_Unit then
5320 -- In compilation unit case, kill elaboration checks on the
5321 -- instantiation, since they are never needed - the body is
5322 -- instantiated at the same point as the spec.
5324 if Legacy_Elaboration_Checks then
5325 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5326 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5327 end if;
5329 Set_Is_Compilation_Unit (Anon_Id);
5330 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5331 end if;
5333 -- The instance is not a freezing point for the new subprogram.
5334 -- The anonymous subprogram may have a freeze node, created for
5335 -- some delayed aspects. This freeze node must not be inherited
5336 -- by the visible subprogram entity.
5338 Set_Is_Frozen (Act_Decl_Id, False);
5339 Set_Freeze_Node (Act_Decl_Id, Empty);
5341 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5342 Valid_Operator_Definition (Act_Decl_Id);
5343 end if;
5345 Set_Alias (Act_Decl_Id, Anon_Id);
5346 Set_Has_Completion (Act_Decl_Id);
5347 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5349 if Nkind (Parent (N)) = N_Compilation_Unit then
5350 Set_Body_Required (Parent (N), False);
5351 end if;
5352 end Analyze_Instance_And_Renamings;
5354 -------------------------------
5355 -- Build_Subprogram_Renaming --
5356 -------------------------------
5358 procedure Build_Subprogram_Renaming is
5359 Renaming_Decl : Node_Id;
5360 Unit_Renaming : Node_Id;
5362 begin
5363 Unit_Renaming :=
5364 Make_Subprogram_Renaming_Declaration (Loc,
5365 Specification =>
5366 Copy_Generic_Node
5367 (Specification (Original_Node (Gen_Decl)),
5368 Empty,
5369 Instantiating => True),
5370 Name => New_Occurrence_Of (Anon_Id, Loc));
5372 -- The generic may be a a child unit. The renaming needs an
5373 -- identifier with the proper name.
5375 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5376 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5378 -- If there is a formal subprogram with the same name as the unit
5379 -- itself, do not add this renaming declaration, to prevent
5380 -- ambiguities when there is a call with that name in the body.
5381 -- This is a partial and ugly fix for one ACATS test. ???
5383 Renaming_Decl := First (Renaming_List);
5384 while Present (Renaming_Decl) loop
5385 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5386 and then
5387 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5388 then
5389 exit;
5390 end if;
5392 Next (Renaming_Decl);
5393 end loop;
5395 if No (Renaming_Decl) then
5396 Append (Unit_Renaming, Renaming_List);
5397 end if;
5398 end Build_Subprogram_Renaming;
5400 -- Local variables
5402 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5403 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
5404 Saved_ISMP : constant Boolean :=
5405 Ignore_SPARK_Mode_Pragmas_In_Instance;
5406 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
5407 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
5408 -- Save the Ghost and SPARK mode-related data to restore on exit
5410 Vis_Prims_List : Elist_Id := No_Elist;
5411 -- List of primitives made temporarily visible in the instantiation
5412 -- to match the visibility of the formal type
5414 -- Start of processing for Analyze_Subprogram_Instantiation
5416 begin
5417 -- Preserve relevant elaboration-related attributes of the context which
5418 -- are no longer available or very expensive to recompute once analysis,
5419 -- resolution, and expansion are over.
5421 Mark_Elaboration_Attributes
5422 (N_Id => N,
5423 Checks => True,
5424 Level => True,
5425 Modes => True,
5426 Warnings => True);
5428 Check_SPARK_05_Restriction ("generic is not allowed", N);
5430 -- Very first thing: check for special Text_IO unit in case we are
5431 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5432 -- such an instantiation is bogus (these are packages, not subprograms),
5433 -- but we get a better error message if we do this.
5435 Check_Text_IO_Special_Unit (Gen_Id);
5437 -- Make node global for error reporting
5439 Instantiation_Node := N;
5441 -- For package instantiations we turn off style checks, because they
5442 -- will have been emitted in the generic. For subprogram instantiations
5443 -- we want to apply at least the check on overriding indicators so we
5444 -- do not modify the style check status.
5446 -- The renaming declarations for the actuals do not come from source and
5447 -- will not generate spurious warnings.
5449 Preanalyze_Actuals (N);
5451 Init_Env;
5452 Env_Installed := True;
5453 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5454 Gen_Unit := Entity (Gen_Id);
5456 -- A subprogram instantiation is Ghost when it is subject to pragma
5457 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5458 -- that any nodes generated during analysis and expansion are marked as
5459 -- Ghost.
5461 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
5463 Generate_Reference (Gen_Unit, Gen_Id);
5465 if Nkind (Gen_Id) = N_Identifier
5466 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5467 then
5468 Error_Msg_NE
5469 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5470 end if;
5472 if Etype (Gen_Unit) = Any_Type then
5473 Restore_Env;
5474 goto Leave;
5475 end if;
5477 -- Verify that it is a generic subprogram of the right kind, and that
5478 -- it does not lead to a circular instantiation.
5480 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5481 Error_Msg_NE
5482 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5484 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5485 Error_Msg_NE
5486 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5488 elsif In_Open_Scopes (Gen_Unit) then
5489 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5491 else
5492 Set_Entity (Gen_Id, Gen_Unit);
5493 Set_Is_Instantiated (Gen_Unit);
5495 if In_Extended_Main_Source_Unit (N) then
5496 Generate_Reference (Gen_Unit, N);
5497 end if;
5499 -- If renaming, get original unit
5501 if Present (Renamed_Object (Gen_Unit))
5502 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5503 E_Generic_Function)
5504 then
5505 Gen_Unit := Renamed_Object (Gen_Unit);
5506 Set_Is_Instantiated (Gen_Unit);
5507 Generate_Reference (Gen_Unit, N);
5508 end if;
5510 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5511 Error_Msg_Node_2 := Current_Scope;
5512 Error_Msg_NE
5513 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5514 Circularity_Detected := True;
5515 Restore_Hidden_Primitives (Vis_Prims_List);
5516 goto Leave;
5517 end if;
5519 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5521 -- Initialize renamings map, for error checking
5523 Generic_Renamings.Set_Last (0);
5524 Generic_Renamings_HTable.Reset;
5526 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5528 -- Copy original generic tree, to produce text for instantiation
5530 Act_Tree :=
5531 Copy_Generic_Node
5532 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5534 -- Inherit overriding indicator from instance node
5536 Act_Spec := Specification (Act_Tree);
5537 Set_Must_Override (Act_Spec, Must_Override (N));
5538 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5540 Renaming_List :=
5541 Analyze_Associations
5542 (I_Node => N,
5543 Formals => Generic_Formal_Declarations (Act_Tree),
5544 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5546 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5548 -- The subprogram itself cannot contain a nested instance, so the
5549 -- current parent is left empty.
5551 Set_Instance_Env (Gen_Unit, Empty);
5553 -- Build the subprogram declaration, which does not appear in the
5554 -- generic template, and give it a sloc consistent with that of the
5555 -- template.
5557 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5558 Set_Generic_Parent (Act_Spec, Gen_Unit);
5559 Act_Decl :=
5560 Make_Subprogram_Declaration (Sloc (Act_Spec),
5561 Specification => Act_Spec);
5563 -- The aspects have been copied previously, but they have to be
5564 -- linked explicitly to the new subprogram declaration. Explicit
5565 -- pre/postconditions on the instance are analyzed below, in a
5566 -- separate step.
5568 Move_Aspects (Act_Tree, To => Act_Decl);
5569 Set_Categorization_From_Pragmas (Act_Decl);
5571 if Parent_Installed then
5572 Hide_Current_Scope;
5573 end if;
5575 Append (Act_Decl, Renaming_List);
5577 -- Contract-related source pragmas that follow a generic subprogram
5578 -- must be instantiated explicitly because they are not part of the
5579 -- subprogram template.
5581 Instantiate_Subprogram_Contract
5582 (Original_Node (Gen_Decl), Renaming_List);
5584 Build_Subprogram_Renaming;
5586 -- If the context of the instance is subject to SPARK_Mode "off" or
5587 -- the annotation is altogether missing, set the global flag which
5588 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5589 -- the instance. This should be done prior to analyzing the instance.
5591 if SPARK_Mode /= On then
5592 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5593 end if;
5595 -- If the context of an instance is not subject to SPARK_Mode "off",
5596 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5597 -- the latter should be the one applicable to the instance.
5599 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5600 and then Saved_SM /= Off
5601 and then Present (SPARK_Pragma (Gen_Unit))
5602 then
5603 Set_SPARK_Mode (Gen_Unit);
5604 end if;
5606 Analyze_Instance_And_Renamings;
5608 -- Restore SPARK_Mode from the context after analysis of the package
5609 -- declaration, so that the SPARK_Mode on the generic spec does not
5610 -- apply to the pending instance for the instance body.
5612 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5613 and then Saved_SM /= Off
5614 and then Present (SPARK_Pragma (Gen_Unit))
5615 then
5616 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5617 end if;
5619 -- If the generic is marked Import (Intrinsic), then so is the
5620 -- instance. This indicates that there is no body to instantiate. If
5621 -- generic is marked inline, so it the instance, and the anonymous
5622 -- subprogram it renames. If inlined, or else if inlining is enabled
5623 -- for the compilation, we generate the instance body even if it is
5624 -- not within the main unit.
5626 if Is_Intrinsic_Subprogram (Gen_Unit) then
5627 Set_Is_Intrinsic_Subprogram (Anon_Id);
5628 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5630 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5631 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5632 end if;
5633 end if;
5635 -- Inherit convention from generic unit. Intrinsic convention, as for
5636 -- an instance of unchecked conversion, is not inherited because an
5637 -- explicit Ada instance has been created.
5639 if Has_Convention_Pragma (Gen_Unit)
5640 and then Convention (Gen_Unit) /= Convention_Intrinsic
5641 then
5642 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5643 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5644 end if;
5646 Generate_Definition (Act_Decl_Id);
5648 -- Inherit all inlining-related flags which apply to the generic in
5649 -- the subprogram and its declaration.
5651 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5652 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5654 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5655 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5657 -- Propagate No_Return if pragma applied to generic unit. This must
5658 -- be done explicitly because pragma does not appear in generic
5659 -- declaration (unlike the aspect case).
5661 if No_Return (Gen_Unit) then
5662 Set_No_Return (Act_Decl_Id);
5663 Set_No_Return (Anon_Id);
5664 end if;
5666 Set_Has_Pragma_Inline_Always
5667 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5668 Set_Has_Pragma_Inline_Always
5669 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5671 -- Mark both the instance spec and the anonymous package in case the
5672 -- body is instantiated at a later pass. This preserves the original
5673 -- context in effect for the body.
5675 if SPARK_Mode /= On then
5676 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
5677 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
5678 end if;
5680 if Legacy_Elaboration_Checks
5681 and then not Is_Intrinsic_Subprogram (Gen_Unit)
5682 then
5683 Check_Elab_Instantiation (N);
5684 end if;
5686 -- Save the scenario for later examination by the ABE Processing
5687 -- phase.
5689 Record_Elaboration_Scenario (N);
5691 -- The instantiation results in a guaranteed ABE. Create a completing
5692 -- body for the subprogram declaration because the real body will not
5693 -- be instantiated.
5695 if Is_Known_Guaranteed_ABE (N) then
5696 Provide_Completing_Bodies (Instance_Spec (N));
5697 end if;
5699 if Is_Dispatching_Operation (Act_Decl_Id)
5700 and then Ada_Version >= Ada_2005
5701 then
5702 declare
5703 Formal : Entity_Id;
5705 begin
5706 Formal := First_Formal (Act_Decl_Id);
5707 while Present (Formal) loop
5708 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5709 and then Is_Controlling_Formal (Formal)
5710 and then not Can_Never_Be_Null (Formal)
5711 then
5712 Error_Msg_NE
5713 ("access parameter& is controlling,", N, Formal);
5714 Error_Msg_NE
5715 ("\corresponding parameter of & must be explicitly "
5716 & "null-excluding", N, Gen_Id);
5717 end if;
5719 Next_Formal (Formal);
5720 end loop;
5721 end;
5722 end if;
5724 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5726 Validate_Categorization_Dependency (N, Act_Decl_Id);
5728 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5729 Inherit_Context (Gen_Decl, N);
5731 Restore_Private_Views (Pack_Id, False);
5733 -- If the context requires a full instantiation, mark node for
5734 -- subsequent construction of the body.
5736 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5737 Check_Forward_Instantiation (Gen_Decl);
5739 -- The wrapper package is always delayed, because it does not
5740 -- constitute a freeze point, but to insure that the freeze node
5741 -- is placed properly, it is created directly when instantiating
5742 -- the body (otherwise the freeze node might appear to early for
5743 -- nested instantiations). For ASIS purposes, indicate that the
5744 -- wrapper package has replaced the instantiation node.
5746 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5747 Rewrite (N, Unit (Parent (N)));
5748 Set_Unit (Parent (N), N);
5749 end if;
5751 -- Replace instance node for library-level instantiations of
5752 -- intrinsic subprograms, for ASIS use.
5754 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5755 Rewrite (N, Unit (Parent (N)));
5756 Set_Unit (Parent (N), N);
5757 end if;
5759 if Parent_Installed then
5760 Remove_Parent;
5761 end if;
5763 Restore_Hidden_Primitives (Vis_Prims_List);
5764 Restore_Env;
5765 Env_Installed := False;
5766 Generic_Renamings.Set_Last (0);
5767 Generic_Renamings_HTable.Reset;
5768 end if;
5770 <<Leave>>
5771 -- Analyze aspects in declaration if no errors appear in the instance.
5773 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
5774 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5775 end if;
5777 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5778 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5779 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5781 exception
5782 when Instantiation_Error =>
5783 if Parent_Installed then
5784 Remove_Parent;
5785 end if;
5787 if Env_Installed then
5788 Restore_Env;
5789 end if;
5791 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5792 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5793 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5794 end Analyze_Subprogram_Instantiation;
5796 -------------------------
5797 -- Get_Associated_Node --
5798 -------------------------
5800 function Get_Associated_Node (N : Node_Id) return Node_Id is
5801 Assoc : Node_Id;
5803 begin
5804 Assoc := Associated_Node (N);
5806 if Nkind (Assoc) /= Nkind (N) then
5807 return Assoc;
5809 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5810 return Assoc;
5812 else
5813 -- If the node is part of an inner generic, it may itself have been
5814 -- remapped into a further generic copy. Associated_Node is otherwise
5815 -- used for the entity of the node, and will be of a different node
5816 -- kind, or else N has been rewritten as a literal or function call.
5818 while Present (Associated_Node (Assoc))
5819 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5820 loop
5821 Assoc := Associated_Node (Assoc);
5822 end loop;
5824 -- Follow an additional link in case the final node was rewritten.
5825 -- This can only happen with nested generic units.
5827 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5828 and then Present (Associated_Node (Assoc))
5829 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5830 N_Explicit_Dereference,
5831 N_Integer_Literal,
5832 N_Real_Literal,
5833 N_String_Literal))
5834 then
5835 Assoc := Associated_Node (Assoc);
5836 end if;
5838 -- An additional special case: an unconstrained type in an object
5839 -- declaration may have been rewritten as a local subtype constrained
5840 -- by the expression in the declaration. We need to recover the
5841 -- original entity, which may be global.
5843 if Present (Original_Node (Assoc))
5844 and then Nkind (Parent (N)) = N_Object_Declaration
5845 then
5846 Assoc := Original_Node (Assoc);
5847 end if;
5849 return Assoc;
5850 end if;
5851 end Get_Associated_Node;
5853 ----------------------------
5854 -- Build_Function_Wrapper --
5855 ----------------------------
5857 function Build_Function_Wrapper
5858 (Formal_Subp : Entity_Id;
5859 Actual_Subp : Entity_Id) return Node_Id
5861 Loc : constant Source_Ptr := Sloc (Current_Scope);
5862 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5863 Actuals : List_Id;
5864 Decl : Node_Id;
5865 Func_Name : Node_Id;
5866 Func : Entity_Id;
5867 Parm_Type : Node_Id;
5868 Profile : List_Id := New_List;
5869 Spec : Node_Id;
5870 Act_F : Entity_Id;
5871 Form_F : Entity_Id;
5872 New_F : Entity_Id;
5874 begin
5875 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5877 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5878 Set_Ekind (Func, E_Function);
5879 Set_Is_Generic_Actual_Subprogram (Func);
5881 Actuals := New_List;
5882 Profile := New_List;
5884 Act_F := First_Formal (Actual_Subp);
5885 Form_F := First_Formal (Formal_Subp);
5886 while Present (Form_F) loop
5888 -- Create new formal for profile of wrapper, and add a reference
5889 -- to it in the list of actuals for the enclosing call. The name
5890 -- must be that of the formal in the formal subprogram, because
5891 -- calls to it in the generic body may use named associations.
5893 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5895 Parm_Type :=
5896 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5898 Append_To (Profile,
5899 Make_Parameter_Specification (Loc,
5900 Defining_Identifier => New_F,
5901 Parameter_Type => Parm_Type));
5903 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5904 Next_Formal (Form_F);
5906 if Present (Act_F) then
5907 Next_Formal (Act_F);
5908 end if;
5909 end loop;
5911 Spec :=
5912 Make_Function_Specification (Loc,
5913 Defining_Unit_Name => Func,
5914 Parameter_Specifications => Profile,
5915 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5917 Decl :=
5918 Make_Expression_Function (Loc,
5919 Specification => Spec,
5920 Expression =>
5921 Make_Function_Call (Loc,
5922 Name => Func_Name,
5923 Parameter_Associations => Actuals));
5925 return Decl;
5926 end Build_Function_Wrapper;
5928 ----------------------------
5929 -- Build_Operator_Wrapper --
5930 ----------------------------
5932 function Build_Operator_Wrapper
5933 (Formal_Subp : Entity_Id;
5934 Actual_Subp : Entity_Id) return Node_Id
5936 Loc : constant Source_Ptr := Sloc (Current_Scope);
5937 Ret_Type : constant Entity_Id :=
5938 Get_Instance_Of (Etype (Formal_Subp));
5939 Op_Type : constant Entity_Id :=
5940 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
5941 Is_Binary : constant Boolean :=
5942 Present (Next_Formal (First_Formal (Formal_Subp)));
5944 Decl : Node_Id;
5945 Expr : Node_Id := Empty;
5946 F1, F2 : Entity_Id;
5947 Func : Entity_Id;
5948 Op_Name : Name_Id;
5949 Spec : Node_Id;
5950 L, R : Node_Id;
5952 begin
5953 Op_Name := Chars (Actual_Subp);
5955 -- Create entities for wrapper function and its formals
5957 F1 := Make_Temporary (Loc, 'A');
5958 F2 := Make_Temporary (Loc, 'B');
5959 L := New_Occurrence_Of (F1, Loc);
5960 R := New_Occurrence_Of (F2, Loc);
5962 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5963 Set_Ekind (Func, E_Function);
5964 Set_Is_Generic_Actual_Subprogram (Func);
5966 Spec :=
5967 Make_Function_Specification (Loc,
5968 Defining_Unit_Name => Func,
5969 Parameter_Specifications => New_List (
5970 Make_Parameter_Specification (Loc,
5971 Defining_Identifier => F1,
5972 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
5973 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5975 if Is_Binary then
5976 Append_To (Parameter_Specifications (Spec),
5977 Make_Parameter_Specification (Loc,
5978 Defining_Identifier => F2,
5979 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
5980 end if;
5982 -- Build expression as a function call, or as an operator node
5983 -- that corresponds to the name of the actual, starting with
5984 -- binary operators.
5986 if Op_Name not in Any_Operator_Name then
5987 Expr :=
5988 Make_Function_Call (Loc,
5989 Name =>
5990 New_Occurrence_Of (Actual_Subp, Loc),
5991 Parameter_Associations => New_List (L));
5993 if Is_Binary then
5994 Append_To (Parameter_Associations (Expr), R);
5995 end if;
5997 -- Binary operators
5999 elsif Is_Binary then
6000 if Op_Name = Name_Op_And then
6001 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
6002 elsif Op_Name = Name_Op_Or then
6003 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
6004 elsif Op_Name = Name_Op_Xor then
6005 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
6006 elsif Op_Name = Name_Op_Eq then
6007 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
6008 elsif Op_Name = Name_Op_Ne then
6009 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
6010 elsif Op_Name = Name_Op_Le then
6011 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
6012 elsif Op_Name = Name_Op_Gt then
6013 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
6014 elsif Op_Name = Name_Op_Ge then
6015 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
6016 elsif Op_Name = Name_Op_Lt then
6017 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
6018 elsif Op_Name = Name_Op_Add then
6019 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
6020 elsif Op_Name = Name_Op_Subtract then
6021 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
6022 elsif Op_Name = Name_Op_Concat then
6023 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
6024 elsif Op_Name = Name_Op_Multiply then
6025 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
6026 elsif Op_Name = Name_Op_Divide then
6027 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
6028 elsif Op_Name = Name_Op_Mod then
6029 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
6030 elsif Op_Name = Name_Op_Rem then
6031 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
6032 elsif Op_Name = Name_Op_Expon then
6033 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
6034 end if;
6036 -- Unary operators
6038 else
6039 if Op_Name = Name_Op_Add then
6040 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
6041 elsif Op_Name = Name_Op_Subtract then
6042 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
6043 elsif Op_Name = Name_Op_Abs then
6044 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
6045 elsif Op_Name = Name_Op_Not then
6046 Expr := Make_Op_Not (Loc, Right_Opnd => L);
6047 end if;
6048 end if;
6050 Decl :=
6051 Make_Expression_Function (Loc,
6052 Specification => Spec,
6053 Expression => Expr);
6055 return Decl;
6056 end Build_Operator_Wrapper;
6058 -------------------------------------------
6059 -- Build_Instance_Compilation_Unit_Nodes --
6060 -------------------------------------------
6062 procedure Build_Instance_Compilation_Unit_Nodes
6063 (N : Node_Id;
6064 Act_Body : Node_Id;
6065 Act_Decl : Node_Id)
6067 Decl_Cunit : Node_Id;
6068 Body_Cunit : Node_Id;
6069 Citem : Node_Id;
6070 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6071 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6073 begin
6074 -- A new compilation unit node is built for the instance declaration
6076 Decl_Cunit :=
6077 Make_Compilation_Unit (Sloc (N),
6078 Context_Items => Empty_List,
6079 Unit => Act_Decl,
6080 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
6082 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6084 -- The new compilation unit is linked to its body, but both share the
6085 -- same file, so we do not set Body_Required on the new unit so as not
6086 -- to create a spurious dependency on a non-existent body in the ali.
6087 -- This simplifies CodePeer unit traversal.
6089 -- We use the original instantiation compilation unit as the resulting
6090 -- compilation unit of the instance, since this is the main unit.
6092 Rewrite (N, Act_Body);
6094 -- Propagate the aspect specifications from the package body template to
6095 -- the instantiated version of the package body.
6097 if Has_Aspects (Act_Body) then
6098 Set_Aspect_Specifications
6099 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
6100 end if;
6102 Body_Cunit := Parent (N);
6104 -- The two compilation unit nodes are linked by the Library_Unit field
6106 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6107 Set_Library_Unit (Body_Cunit, Decl_Cunit);
6109 -- Preserve the private nature of the package if needed
6111 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
6113 -- If the instance is not the main unit, its context, categorization
6114 -- and elaboration entity are not relevant to the compilation.
6116 if Body_Cunit /= Cunit (Main_Unit) then
6117 Make_Instance_Unit (Body_Cunit, In_Main => False);
6118 return;
6119 end if;
6121 -- The context clause items on the instantiation, which are now attached
6122 -- to the body compilation unit (since the body overwrote the original
6123 -- instantiation node), semantically belong on the spec, so copy them
6124 -- there. It's harmless to leave them on the body as well. In fact one
6125 -- could argue that they belong in both places.
6127 Citem := First (Context_Items (Body_Cunit));
6128 while Present (Citem) loop
6129 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
6130 Next (Citem);
6131 end loop;
6133 -- Propagate categorization flags on packages, so that they appear in
6134 -- the ali file for the spec of the unit.
6136 if Ekind (New_Main) = E_Package then
6137 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
6138 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
6139 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
6140 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
6141 Set_Is_Remote_Call_Interface
6142 (Old_Main, Is_Remote_Call_Interface (New_Main));
6143 end if;
6145 -- Make entry in Units table, so that binder can generate call to
6146 -- elaboration procedure for body, if any.
6148 Make_Instance_Unit (Body_Cunit, In_Main => True);
6149 Main_Unit_Entity := New_Main;
6150 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
6152 -- Build elaboration entity, since the instance may certainly generate
6153 -- elaboration code requiring a flag for protection.
6155 Build_Elaboration_Entity (Decl_Cunit, New_Main);
6156 end Build_Instance_Compilation_Unit_Nodes;
6158 -----------------------------
6159 -- Check_Access_Definition --
6160 -----------------------------
6162 procedure Check_Access_Definition (N : Node_Id) is
6163 begin
6164 pragma Assert
6165 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
6166 null;
6167 end Check_Access_Definition;
6169 -----------------------------------
6170 -- Check_Formal_Package_Instance --
6171 -----------------------------------
6173 -- If the formal has specific parameters, they must match those of the
6174 -- actual. Both of them are instances, and the renaming declarations for
6175 -- their formal parameters appear in the same order in both. The analyzed
6176 -- formal has been analyzed in the context of the current instance.
6178 procedure Check_Formal_Package_Instance
6179 (Formal_Pack : Entity_Id;
6180 Actual_Pack : Entity_Id)
6182 E1 : Entity_Id := First_Entity (Actual_Pack);
6183 E2 : Entity_Id := First_Entity (Formal_Pack);
6184 Prev_E1 : Entity_Id;
6186 Expr1 : Node_Id;
6187 Expr2 : Node_Id;
6189 procedure Check_Mismatch (B : Boolean);
6190 -- Common error routine for mismatch between the parameters of the
6191 -- actual instance and those of the formal package.
6193 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
6194 -- The formal may come from a nested formal package, and the actual may
6195 -- have been constant-folded. To determine whether the two denote the
6196 -- same entity we may have to traverse several definitions to recover
6197 -- the ultimate entity that they refer to.
6199 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
6200 -- The formal and the actual must be identical, but if both are
6201 -- given by attributes they end up renaming different generated bodies,
6202 -- and we must verify that the attributes themselves match.
6204 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
6205 -- Similarly, if the formal comes from a nested formal package, the
6206 -- actual may designate the formal through multiple renamings, which
6207 -- have to be followed to determine the original variable in question.
6209 --------------------
6210 -- Check_Mismatch --
6211 --------------------
6213 procedure Check_Mismatch (B : Boolean) is
6214 -- A Formal_Type_Declaration for a derived private type is rewritten
6215 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6216 -- which is why we examine the original node.
6218 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
6220 begin
6221 if Kind = N_Formal_Type_Declaration then
6222 return;
6224 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
6225 N_Formal_Package_Declaration)
6226 or else Kind in N_Formal_Subprogram_Declaration
6227 then
6228 null;
6230 -- Ada 2012: If both formal and actual are incomplete types they
6231 -- are conformant.
6233 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
6234 null;
6236 elsif B then
6237 Error_Msg_NE
6238 ("actual for & in actual instance does not match formal",
6239 Parent (Actual_Pack), E1);
6240 end if;
6241 end Check_Mismatch;
6243 --------------------------------
6244 -- Same_Instantiated_Constant --
6245 --------------------------------
6247 function Same_Instantiated_Constant
6248 (E1, E2 : Entity_Id) return Boolean
6250 Ent : Entity_Id;
6252 begin
6253 Ent := E2;
6254 while Present (Ent) loop
6255 if E1 = Ent then
6256 return True;
6258 elsif Ekind (Ent) /= E_Constant then
6259 return False;
6261 elsif Is_Entity_Name (Constant_Value (Ent)) then
6262 if Entity (Constant_Value (Ent)) = E1 then
6263 return True;
6264 else
6265 Ent := Entity (Constant_Value (Ent));
6266 end if;
6268 -- The actual may be a constant that has been folded. Recover
6269 -- original name.
6271 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
6272 Ent := Entity (Original_Node (Constant_Value (Ent)));
6274 else
6275 return False;
6276 end if;
6277 end loop;
6279 return False;
6280 end Same_Instantiated_Constant;
6282 --------------------------------
6283 -- Same_Instantiated_Function --
6284 --------------------------------
6286 function Same_Instantiated_Function
6287 (E1, E2 : Entity_Id) return Boolean
6289 U1, U2 : Node_Id;
6290 begin
6291 if Alias (E1) = Alias (E2) then
6292 return True;
6294 elsif Present (Alias (E2)) then
6295 U1 := Original_Node (Unit_Declaration_Node (E1));
6296 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6298 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6299 and then Nkind (Name (U1)) = N_Attribute_Reference
6301 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6302 and then Nkind (Name (U2)) = N_Attribute_Reference
6304 and then
6305 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6306 else
6307 return False;
6308 end if;
6309 end Same_Instantiated_Function;
6311 --------------------------------
6312 -- Same_Instantiated_Variable --
6313 --------------------------------
6315 function Same_Instantiated_Variable
6316 (E1, E2 : Entity_Id) return Boolean
6318 function Original_Entity (E : Entity_Id) return Entity_Id;
6319 -- Follow chain of renamings to the ultimate ancestor
6321 ---------------------
6322 -- Original_Entity --
6323 ---------------------
6325 function Original_Entity (E : Entity_Id) return Entity_Id is
6326 Orig : Entity_Id;
6328 begin
6329 Orig := E;
6330 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6331 and then Present (Renamed_Object (Orig))
6332 and then Is_Entity_Name (Renamed_Object (Orig))
6333 loop
6334 Orig := Entity (Renamed_Object (Orig));
6335 end loop;
6337 return Orig;
6338 end Original_Entity;
6340 -- Start of processing for Same_Instantiated_Variable
6342 begin
6343 return Ekind (E1) = Ekind (E2)
6344 and then Original_Entity (E1) = Original_Entity (E2);
6345 end Same_Instantiated_Variable;
6347 -- Start of processing for Check_Formal_Package_Instance
6349 begin
6350 Prev_E1 := E1;
6351 while Present (E1) and then Present (E2) loop
6352 exit when Ekind (E1) = E_Package
6353 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6355 -- If the formal is the renaming of the formal package, this
6356 -- is the end of its formal part, which may occur before the
6357 -- end of the formal part in the actual in the presence of
6358 -- defaulted parameters in the formal package.
6360 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6361 and then Renamed_Entity (E2) = Scope (E2);
6363 -- The analysis of the actual may generate additional internal
6364 -- entities. If the formal is defaulted, there is no corresponding
6365 -- analysis and the internal entities must be skipped, until we
6366 -- find corresponding entities again.
6368 if Comes_From_Source (E2)
6369 and then not Comes_From_Source (E1)
6370 and then Chars (E1) /= Chars (E2)
6371 then
6372 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6373 Next_Entity (E1);
6374 end loop;
6375 end if;
6377 if No (E1) then
6378 return;
6380 -- Entities may be declared without full declaration, such as
6381 -- itypes and predefined operators (concatenation for arrays, eg).
6382 -- Skip it and keep the formal entity to find a later match for it.
6384 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6385 E1 := Prev_E1;
6386 goto Next_E;
6388 -- If the formal entity comes from a formal declaration, it was
6389 -- defaulted in the formal package, and no check is needed on it.
6391 elsif Nkind_In (Original_Node (Parent (E2)),
6392 N_Formal_Object_Declaration,
6393 N_Formal_Type_Declaration)
6394 then
6395 -- If the formal is a tagged type the corresponding class-wide
6396 -- type has been generated as well, and it must be skipped.
6398 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6399 Next_Entity (E2);
6400 end if;
6402 goto Next_E;
6404 -- Ditto for defaulted formal subprograms.
6406 elsif Is_Overloadable (E1)
6407 and then Nkind (Unit_Declaration_Node (E2)) in
6408 N_Formal_Subprogram_Declaration
6409 then
6410 goto Next_E;
6412 elsif Is_Type (E1) then
6414 -- Subtypes must statically match. E1, E2 are the local entities
6415 -- that are subtypes of the actuals. Itypes generated for other
6416 -- parameters need not be checked, the check will be performed
6417 -- on the parameters themselves.
6419 -- If E2 is a formal type declaration, it is a defaulted parameter
6420 -- and needs no checking.
6422 if not Is_Itype (E1) and then not Is_Itype (E2) then
6423 Check_Mismatch
6424 (not Is_Type (E2)
6425 or else Etype (E1) /= Etype (E2)
6426 or else not Subtypes_Statically_Match (E1, E2));
6427 end if;
6429 elsif Ekind (E1) = E_Constant then
6431 -- IN parameters must denote the same static value, or the same
6432 -- constant, or the literal null.
6434 Expr1 := Expression (Parent (E1));
6436 if Ekind (E2) /= E_Constant then
6437 Check_Mismatch (True);
6438 goto Next_E;
6439 else
6440 Expr2 := Expression (Parent (E2));
6441 end if;
6443 if Is_OK_Static_Expression (Expr1) then
6444 if not Is_OK_Static_Expression (Expr2) then
6445 Check_Mismatch (True);
6447 elsif Is_Discrete_Type (Etype (E1)) then
6448 declare
6449 V1 : constant Uint := Expr_Value (Expr1);
6450 V2 : constant Uint := Expr_Value (Expr2);
6451 begin
6452 Check_Mismatch (V1 /= V2);
6453 end;
6455 elsif Is_Real_Type (Etype (E1)) then
6456 declare
6457 V1 : constant Ureal := Expr_Value_R (Expr1);
6458 V2 : constant Ureal := Expr_Value_R (Expr2);
6459 begin
6460 Check_Mismatch (V1 /= V2);
6461 end;
6463 elsif Is_String_Type (Etype (E1))
6464 and then Nkind (Expr1) = N_String_Literal
6465 then
6466 if Nkind (Expr2) /= N_String_Literal then
6467 Check_Mismatch (True);
6468 else
6469 Check_Mismatch
6470 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6471 end if;
6472 end if;
6474 elsif Is_Entity_Name (Expr1) then
6475 if Is_Entity_Name (Expr2) then
6476 if Entity (Expr1) = Entity (Expr2) then
6477 null;
6478 else
6479 Check_Mismatch
6480 (not Same_Instantiated_Constant
6481 (Entity (Expr1), Entity (Expr2)));
6482 end if;
6484 else
6485 Check_Mismatch (True);
6486 end if;
6488 elsif Is_Entity_Name (Original_Node (Expr1))
6489 and then Is_Entity_Name (Expr2)
6490 and then Same_Instantiated_Constant
6491 (Entity (Original_Node (Expr1)), Entity (Expr2))
6492 then
6493 null;
6495 elsif Nkind (Expr1) = N_Null then
6496 Check_Mismatch (Nkind (Expr1) /= N_Null);
6498 else
6499 Check_Mismatch (True);
6500 end if;
6502 elsif Ekind (E1) = E_Variable then
6503 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6505 elsif Ekind (E1) = E_Package then
6506 Check_Mismatch
6507 (Ekind (E1) /= Ekind (E2)
6508 or else (Present (Renamed_Object (E2))
6509 and then Renamed_Object (E1) /=
6510 Renamed_Object (E2)));
6512 elsif Is_Overloadable (E1) then
6513 -- Verify that the actual subprograms match. Note that actuals
6514 -- that are attributes are rewritten as subprograms. If the
6515 -- subprogram in the formal package is defaulted, no check is
6516 -- needed. Note that this can only happen in Ada 2005 when the
6517 -- formal package can be partially parameterized.
6519 if Nkind (Unit_Declaration_Node (E1)) =
6520 N_Subprogram_Renaming_Declaration
6521 and then From_Default (Unit_Declaration_Node (E1))
6522 then
6523 null;
6525 -- If the formal package has an "others" box association that
6526 -- covers this formal, there is no need for a check either.
6528 elsif Nkind (Unit_Declaration_Node (E2)) in
6529 N_Formal_Subprogram_Declaration
6530 and then Box_Present (Unit_Declaration_Node (E2))
6531 then
6532 null;
6534 -- No check needed if subprogram is a defaulted null procedure
6536 elsif No (Alias (E2))
6537 and then Ekind (E2) = E_Procedure
6538 and then
6539 Null_Present (Specification (Unit_Declaration_Node (E2)))
6540 then
6541 null;
6543 -- Otherwise the actual in the formal and the actual in the
6544 -- instantiation of the formal must match, up to renamings.
6546 else
6547 Check_Mismatch
6548 (Ekind (E2) /= Ekind (E1)
6549 or else not Same_Instantiated_Function (E1, E2));
6550 end if;
6552 else
6553 raise Program_Error;
6554 end if;
6556 <<Next_E>>
6557 Prev_E1 := E1;
6558 Next_Entity (E1);
6559 Next_Entity (E2);
6560 end loop;
6561 end Check_Formal_Package_Instance;
6563 ---------------------------
6564 -- Check_Formal_Packages --
6565 ---------------------------
6567 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6568 E : Entity_Id;
6569 Formal_P : Entity_Id;
6570 Formal_Decl : Node_Id;
6571 begin
6572 -- Iterate through the declarations in the instance, looking for package
6573 -- renaming declarations that denote instances of formal packages. Stop
6574 -- when we find the renaming of the current package itself. The
6575 -- declaration for a formal package without a box is followed by an
6576 -- internal entity that repeats the instantiation.
6578 E := First_Entity (P_Id);
6579 while Present (E) loop
6580 if Ekind (E) = E_Package then
6581 if Renamed_Object (E) = P_Id then
6582 exit;
6584 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6585 null;
6587 else
6588 Formal_Decl := Parent (Associated_Formal_Package (E));
6590 -- Nothing to check if the formal has a box or an others_clause
6591 -- (necessarily with a box).
6593 if Box_Present (Formal_Decl) then
6594 null;
6596 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6597 N_Others_Choice
6598 then
6599 -- The internal validating package was generated but formal
6600 -- and instance are known to be compatible.
6602 Formal_P := Next_Entity (E);
6603 Remove (Unit_Declaration_Node (Formal_P));
6605 else
6606 Formal_P := Next_Entity (E);
6608 -- If the instance is within an enclosing instance body
6609 -- there is no need to verify the legality of current formal
6610 -- packages because they were legal in the generic body.
6611 -- This optimization may be applicable elsewhere, and it
6612 -- also removes spurious errors that may arise with
6613 -- on-the-fly inlining and confusion between private and
6614 -- full views.
6616 if not In_Instance_Body then
6617 Check_Formal_Package_Instance (Formal_P, E);
6618 end if;
6620 -- Restore the visibility of formals of the formal instance
6621 -- that are not defaulted, and are hidden within the current
6622 -- generic. These formals may be visible within an enclosing
6623 -- generic.
6625 declare
6626 Elmt : Elmt_Id;
6627 begin
6628 Elmt := First_Elmt (Hidden_In_Formal_Instance (Formal_P));
6629 while Present (Elmt) loop
6630 Set_Is_Hidden (Node (Elmt), False);
6631 Next_Elmt (Elmt);
6632 end loop;
6633 end;
6635 -- After checking, remove the internal validating package.
6636 -- It is only needed for semantic checks, and as it may
6637 -- contain generic formal declarations it should not reach
6638 -- gigi.
6640 Remove (Unit_Declaration_Node (Formal_P));
6641 end if;
6642 end if;
6643 end if;
6645 Next_Entity (E);
6646 end loop;
6647 end Check_Formal_Packages;
6649 ---------------------------------
6650 -- Check_Forward_Instantiation --
6651 ---------------------------------
6653 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6654 S : Entity_Id;
6655 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6657 begin
6658 -- The instantiation appears before the generic body if we are in the
6659 -- scope of the unit containing the generic, either in its spec or in
6660 -- the package body, and before the generic body.
6662 if Ekind (Gen_Comp) = E_Package_Body then
6663 Gen_Comp := Spec_Entity (Gen_Comp);
6664 end if;
6666 if In_Open_Scopes (Gen_Comp)
6667 and then No (Corresponding_Body (Decl))
6668 then
6669 S := Current_Scope;
6671 while Present (S)
6672 and then not Is_Compilation_Unit (S)
6673 and then not Is_Child_Unit (S)
6674 loop
6675 if Ekind (S) = E_Package then
6676 Set_Has_Forward_Instantiation (S);
6677 end if;
6679 S := Scope (S);
6680 end loop;
6681 end if;
6682 end Check_Forward_Instantiation;
6684 ---------------------------
6685 -- Check_Generic_Actuals --
6686 ---------------------------
6688 -- The visibility of the actuals may be different between the point of
6689 -- generic instantiation and the instantiation of the body.
6691 procedure Check_Generic_Actuals
6692 (Instance : Entity_Id;
6693 Is_Formal_Box : Boolean)
6695 E : Entity_Id;
6696 Astype : Entity_Id;
6698 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
6699 -- For a formal that is an array type, the component type is often a
6700 -- previous formal in the same unit. The privacy status of the component
6701 -- type will have been examined earlier in the traversal of the
6702 -- corresponding actuals, and this status should not be modified for
6703 -- the array (sub)type itself. However, if the base type of the array
6704 -- (sub)type is private, its full view must be restored in the body to
6705 -- be consistent with subsequent index subtypes, etc.
6707 -- To detect this case we have to rescan the list of formals, which is
6708 -- usually short enough to ignore the resulting inefficiency.
6710 -----------------------------
6711 -- Denotes_Previous_Actual --
6712 -----------------------------
6714 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
6715 Prev : Entity_Id;
6717 begin
6718 Prev := First_Entity (Instance);
6719 while Present (Prev) loop
6720 if Is_Type (Prev)
6721 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
6722 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
6723 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
6724 then
6725 return True;
6727 elsif Prev = E then
6728 return False;
6730 else
6731 Next_Entity (Prev);
6732 end if;
6733 end loop;
6735 return False;
6736 end Denotes_Previous_Actual;
6738 -- Start of processing for Check_Generic_Actuals
6740 begin
6741 E := First_Entity (Instance);
6742 while Present (E) loop
6743 if Is_Type (E)
6744 and then Nkind (Parent (E)) = N_Subtype_Declaration
6745 and then Scope (Etype (E)) /= Instance
6746 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6747 then
6748 if Is_Array_Type (E)
6749 and then not Is_Private_Type (Etype (E))
6750 and then Denotes_Previous_Actual (Component_Type (E))
6751 then
6752 null;
6753 else
6754 Check_Private_View (Subtype_Indication (Parent (E)));
6755 end if;
6757 Set_Is_Generic_Actual_Type (E, True);
6758 Set_Is_Hidden (E, False);
6759 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
6761 -- We constructed the generic actual type as a subtype of the
6762 -- supplied type. This means that it normally would not inherit
6763 -- subtype specific attributes of the actual, which is wrong for
6764 -- the generic case.
6766 Astype := Ancestor_Subtype (E);
6768 if No (Astype) then
6770 -- This can happen when E is an itype that is the full view of
6771 -- a private type completed, e.g. with a constrained array. In
6772 -- that case, use the first subtype, which will carry size
6773 -- information. The base type itself is unconstrained and will
6774 -- not carry it.
6776 Astype := First_Subtype (E);
6777 end if;
6779 Set_Size_Info (E, (Astype));
6780 Set_RM_Size (E, RM_Size (Astype));
6781 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6783 if Is_Discrete_Or_Fixed_Point_Type (E) then
6784 Set_RM_Size (E, RM_Size (Astype));
6786 -- In nested instances, the base type of an access actual may
6787 -- itself be private, and need to be exchanged.
6789 elsif Is_Access_Type (E)
6790 and then Is_Private_Type (Etype (E))
6791 then
6792 Check_Private_View
6793 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
6794 end if;
6796 elsif Ekind (E) = E_Package then
6798 -- If this is the renaming for the current instance, we're done.
6799 -- Otherwise it is a formal package. If the corresponding formal
6800 -- was declared with a box, the (instantiations of the) generic
6801 -- formal part are also visible. Otherwise, ignore the entity
6802 -- created to validate the actuals.
6804 if Renamed_Object (E) = Instance then
6805 exit;
6807 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6808 null;
6810 -- The visibility of a formal of an enclosing generic is already
6811 -- correct.
6813 elsif Denotes_Formal_Package (E) then
6814 null;
6816 elsif Present (Associated_Formal_Package (E))
6817 and then not Is_Generic_Formal (E)
6818 then
6819 if Box_Present (Parent (Associated_Formal_Package (E))) then
6820 Check_Generic_Actuals (Renamed_Object (E), True);
6822 else
6823 Check_Generic_Actuals (Renamed_Object (E), False);
6824 end if;
6826 Set_Is_Hidden (E, False);
6827 end if;
6829 -- If this is a subprogram instance (in a wrapper package) the
6830 -- actual is fully visible.
6832 elsif Is_Wrapper_Package (Instance) then
6833 Set_Is_Hidden (E, False);
6835 -- If the formal package is declared with a box, or if the formal
6836 -- parameter is defaulted, it is visible in the body.
6838 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6839 Set_Is_Hidden (E, False);
6840 end if;
6842 if Ekind (E) = E_Constant then
6844 -- If the type of the actual is a private type declared in the
6845 -- enclosing scope of the generic unit, the body of the generic
6846 -- sees the full view of the type (because it has to appear in
6847 -- the corresponding package body). If the type is private now,
6848 -- exchange views to restore the proper visiblity in the instance.
6850 declare
6851 Typ : constant Entity_Id := Base_Type (Etype (E));
6852 -- The type of the actual
6854 Gen_Id : Entity_Id;
6855 -- The generic unit
6857 Parent_Scope : Entity_Id;
6858 -- The enclosing scope of the generic unit
6860 begin
6861 if Is_Wrapper_Package (Instance) then
6862 Gen_Id :=
6863 Generic_Parent
6864 (Specification
6865 (Unit_Declaration_Node
6866 (Related_Instance (Instance))));
6867 else
6868 Gen_Id :=
6869 Generic_Parent (Package_Specification (Instance));
6870 end if;
6872 Parent_Scope := Scope (Gen_Id);
6874 -- The exchange is only needed if the generic is defined
6875 -- within a package which is not a common ancestor of the
6876 -- scope of the instance, and is not already in scope.
6878 if Is_Private_Type (Typ)
6879 and then Scope (Typ) = Parent_Scope
6880 and then Scope (Instance) /= Parent_Scope
6881 and then Ekind (Parent_Scope) = E_Package
6882 and then not Is_Child_Unit (Gen_Id)
6883 then
6884 Switch_View (Typ);
6886 -- If the type of the entity is a subtype, it may also have
6887 -- to be made visible, together with the base type of its
6888 -- full view, after exchange.
6890 if Is_Private_Type (Etype (E)) then
6891 Switch_View (Etype (E));
6892 Switch_View (Base_Type (Etype (E)));
6893 end if;
6894 end if;
6895 end;
6896 end if;
6898 Next_Entity (E);
6899 end loop;
6900 end Check_Generic_Actuals;
6902 ------------------------------
6903 -- Check_Generic_Child_Unit --
6904 ------------------------------
6906 procedure Check_Generic_Child_Unit
6907 (Gen_Id : Node_Id;
6908 Parent_Installed : in out Boolean)
6910 Loc : constant Source_Ptr := Sloc (Gen_Id);
6911 Gen_Par : Entity_Id := Empty;
6912 E : Entity_Id;
6913 Inst_Par : Entity_Id;
6914 S : Node_Id;
6916 function Find_Generic_Child
6917 (Scop : Entity_Id;
6918 Id : Node_Id) return Entity_Id;
6919 -- Search generic parent for possible child unit with the given name
6921 function In_Enclosing_Instance return Boolean;
6922 -- Within an instance of the parent, the child unit may be denoted by
6923 -- a simple name, or an abbreviated expanded name. Examine enclosing
6924 -- scopes to locate a possible parent instantiation.
6926 ------------------------
6927 -- Find_Generic_Child --
6928 ------------------------
6930 function Find_Generic_Child
6931 (Scop : Entity_Id;
6932 Id : Node_Id) return Entity_Id
6934 E : Entity_Id;
6936 begin
6937 -- If entity of name is already set, instance has already been
6938 -- resolved, e.g. in an enclosing instantiation.
6940 if Present (Entity (Id)) then
6941 if Scope (Entity (Id)) = Scop then
6942 return Entity (Id);
6943 else
6944 return Empty;
6945 end if;
6947 else
6948 E := First_Entity (Scop);
6949 while Present (E) loop
6950 if Chars (E) = Chars (Id)
6951 and then Is_Child_Unit (E)
6952 then
6953 if Is_Child_Unit (E)
6954 and then not Is_Visible_Lib_Unit (E)
6955 then
6956 Error_Msg_NE
6957 ("generic child unit& is not visible", Gen_Id, E);
6958 end if;
6960 Set_Entity (Id, E);
6961 return E;
6962 end if;
6964 Next_Entity (E);
6965 end loop;
6967 return Empty;
6968 end if;
6969 end Find_Generic_Child;
6971 ---------------------------
6972 -- In_Enclosing_Instance --
6973 ---------------------------
6975 function In_Enclosing_Instance return Boolean is
6976 Enclosing_Instance : Node_Id;
6977 Instance_Decl : Node_Id;
6979 begin
6980 -- We do not inline any call that contains instantiations, except
6981 -- for instantiations of Unchecked_Conversion, so if we are within
6982 -- an inlined body the current instance does not require parents.
6984 if In_Inlined_Body then
6985 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6986 return False;
6987 end if;
6989 -- Loop to check enclosing scopes
6991 Enclosing_Instance := Current_Scope;
6992 while Present (Enclosing_Instance) loop
6993 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6995 if Ekind (Enclosing_Instance) = E_Package
6996 and then Is_Generic_Instance (Enclosing_Instance)
6997 and then Present
6998 (Generic_Parent (Specification (Instance_Decl)))
6999 then
7000 -- Check whether the generic we are looking for is a child of
7001 -- this instance.
7003 E := Find_Generic_Child
7004 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
7005 exit when Present (E);
7007 else
7008 E := Empty;
7009 end if;
7011 Enclosing_Instance := Scope (Enclosing_Instance);
7012 end loop;
7014 if No (E) then
7016 -- Not a child unit
7018 Analyze (Gen_Id);
7019 return False;
7021 else
7022 Rewrite (Gen_Id,
7023 Make_Expanded_Name (Loc,
7024 Chars => Chars (E),
7025 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
7026 Selector_Name => New_Occurrence_Of (E, Loc)));
7028 Set_Entity (Gen_Id, E);
7029 Set_Etype (Gen_Id, Etype (E));
7030 Parent_Installed := False; -- Already in scope.
7031 return True;
7032 end if;
7033 end In_Enclosing_Instance;
7035 -- Start of processing for Check_Generic_Child_Unit
7037 begin
7038 -- If the name of the generic is given by a selected component, it may
7039 -- be the name of a generic child unit, and the prefix is the name of an
7040 -- instance of the parent, in which case the child unit must be visible.
7041 -- If this instance is not in scope, it must be placed there and removed
7042 -- after instantiation, because what is being instantiated is not the
7043 -- original child, but the corresponding child present in the instance
7044 -- of the parent.
7046 -- If the child is instantiated within the parent, it can be given by
7047 -- a simple name. In this case the instance is already in scope, but
7048 -- the child generic must be recovered from the generic parent as well.
7050 if Nkind (Gen_Id) = N_Selected_Component then
7051 S := Selector_Name (Gen_Id);
7052 Analyze (Prefix (Gen_Id));
7053 Inst_Par := Entity (Prefix (Gen_Id));
7055 if Ekind (Inst_Par) = E_Package
7056 and then Present (Renamed_Object (Inst_Par))
7057 then
7058 Inst_Par := Renamed_Object (Inst_Par);
7059 end if;
7061 if Ekind (Inst_Par) = E_Package then
7062 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
7063 Gen_Par := Generic_Parent (Parent (Inst_Par));
7065 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
7066 and then
7067 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
7068 then
7069 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
7070 end if;
7072 elsif Ekind (Inst_Par) = E_Generic_Package
7073 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
7074 then
7075 -- A formal package may be a real child package, and not the
7076 -- implicit instance within a parent. In this case the child is
7077 -- not visible and has to be retrieved explicitly as well.
7079 Gen_Par := Inst_Par;
7080 end if;
7082 if Present (Gen_Par) then
7084 -- The prefix denotes an instantiation. The entity itself may be a
7085 -- nested generic, or a child unit.
7087 E := Find_Generic_Child (Gen_Par, S);
7089 if Present (E) then
7090 Change_Selected_Component_To_Expanded_Name (Gen_Id);
7091 Set_Entity (Gen_Id, E);
7092 Set_Etype (Gen_Id, Etype (E));
7093 Set_Entity (S, E);
7094 Set_Etype (S, Etype (E));
7096 -- Indicate that this is a reference to the parent
7098 if In_Extended_Main_Source_Unit (Gen_Id) then
7099 Set_Is_Instantiated (Inst_Par);
7100 end if;
7102 -- A common mistake is to replicate the naming scheme of a
7103 -- hierarchy by instantiating a generic child directly, rather
7104 -- than the implicit child in a parent instance:
7106 -- generic .. package Gpar is ..
7107 -- generic .. package Gpar.Child is ..
7108 -- package Par is new Gpar ();
7110 -- with Gpar.Child;
7111 -- package Par.Child is new Gpar.Child ();
7112 -- rather than Par.Child
7114 -- In this case the instantiation is within Par, which is an
7115 -- instance, but Gpar does not denote Par because we are not IN
7116 -- the instance of Gpar, so this is illegal. The test below
7117 -- recognizes this particular case.
7119 if Is_Child_Unit (E)
7120 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
7121 and then (not In_Instance
7122 or else Nkind (Parent (Parent (Gen_Id))) =
7123 N_Compilation_Unit)
7124 then
7125 Error_Msg_N
7126 ("prefix of generic child unit must be instance of parent",
7127 Gen_Id);
7128 end if;
7130 if not In_Open_Scopes (Inst_Par)
7131 and then Nkind (Parent (Gen_Id)) not in
7132 N_Generic_Renaming_Declaration
7133 then
7134 Install_Parent (Inst_Par);
7135 Parent_Installed := True;
7137 elsif In_Open_Scopes (Inst_Par) then
7139 -- If the parent is already installed, install the actuals
7140 -- for its formal packages. This is necessary when the child
7141 -- instance is a child of the parent instance: in this case,
7142 -- the parent is placed on the scope stack but the formal
7143 -- packages are not made visible.
7145 Install_Formal_Packages (Inst_Par);
7146 end if;
7148 else
7149 -- If the generic parent does not contain an entity that
7150 -- corresponds to the selector, the instance doesn't either.
7151 -- Analyzing the node will yield the appropriate error message.
7152 -- If the entity is not a child unit, then it is an inner
7153 -- generic in the parent.
7155 Analyze (Gen_Id);
7156 end if;
7158 else
7159 Analyze (Gen_Id);
7161 if Is_Child_Unit (Entity (Gen_Id))
7162 and then
7163 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7164 and then not In_Open_Scopes (Inst_Par)
7165 then
7166 Install_Parent (Inst_Par);
7167 Parent_Installed := True;
7169 -- The generic unit may be the renaming of the implicit child
7170 -- present in an instance. In that case the parent instance is
7171 -- obtained from the name of the renamed entity.
7173 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
7174 and then Present (Renamed_Entity (Entity (Gen_Id)))
7175 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7176 then
7177 declare
7178 Renamed_Package : constant Node_Id :=
7179 Name (Parent (Entity (Gen_Id)));
7180 begin
7181 if Nkind (Renamed_Package) = N_Expanded_Name then
7182 Inst_Par := Entity (Prefix (Renamed_Package));
7183 Install_Parent (Inst_Par);
7184 Parent_Installed := True;
7185 end if;
7186 end;
7187 end if;
7188 end if;
7190 elsif Nkind (Gen_Id) = N_Expanded_Name then
7192 -- Entity already present, analyze prefix, whose meaning may be an
7193 -- instance in the current context. If it is an instance of a
7194 -- relative within another, the proper parent may still have to be
7195 -- installed, if they are not of the same generation.
7197 Analyze (Prefix (Gen_Id));
7199 -- Prevent cascaded errors
7201 if Etype (Prefix (Gen_Id)) = Any_Type then
7202 return;
7203 end if;
7205 -- In the unlikely case that a local declaration hides the name of
7206 -- the parent package, locate it on the homonym chain. If the context
7207 -- is an instance of the parent, the renaming entity is flagged as
7208 -- such.
7210 Inst_Par := Entity (Prefix (Gen_Id));
7211 while Present (Inst_Par)
7212 and then not Is_Package_Or_Generic_Package (Inst_Par)
7213 loop
7214 Inst_Par := Homonym (Inst_Par);
7215 end loop;
7217 pragma Assert (Present (Inst_Par));
7218 Set_Entity (Prefix (Gen_Id), Inst_Par);
7220 if In_Enclosing_Instance then
7221 null;
7223 elsif Present (Entity (Gen_Id))
7224 and then Is_Child_Unit (Entity (Gen_Id))
7225 and then not In_Open_Scopes (Inst_Par)
7226 then
7227 Install_Parent (Inst_Par);
7228 Parent_Installed := True;
7229 end if;
7231 elsif In_Enclosing_Instance then
7233 -- The child unit is found in some enclosing scope
7235 null;
7237 else
7238 Analyze (Gen_Id);
7240 -- If this is the renaming of the implicit child in a parent
7241 -- instance, recover the parent name and install it.
7243 if Is_Entity_Name (Gen_Id) then
7244 E := Entity (Gen_Id);
7246 if Is_Generic_Unit (E)
7247 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
7248 and then Is_Child_Unit (Renamed_Object (E))
7249 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
7250 and then Nkind (Name (Parent (E))) = N_Expanded_Name
7251 then
7252 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
7253 Inst_Par := Entity (Prefix (Gen_Id));
7255 if not In_Open_Scopes (Inst_Par) then
7256 Install_Parent (Inst_Par);
7257 Parent_Installed := True;
7258 end if;
7260 -- If it is a child unit of a non-generic parent, it may be
7261 -- use-visible and given by a direct name. Install parent as
7262 -- for other cases.
7264 elsif Is_Generic_Unit (E)
7265 and then Is_Child_Unit (E)
7266 and then
7267 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7268 and then not Is_Generic_Unit (Scope (E))
7269 then
7270 if not In_Open_Scopes (Scope (E)) then
7271 Install_Parent (Scope (E));
7272 Parent_Installed := True;
7273 end if;
7274 end if;
7275 end if;
7276 end if;
7277 end Check_Generic_Child_Unit;
7279 -----------------------------
7280 -- Check_Hidden_Child_Unit --
7281 -----------------------------
7283 procedure Check_Hidden_Child_Unit
7284 (N : Node_Id;
7285 Gen_Unit : Entity_Id;
7286 Act_Decl_Id : Entity_Id)
7288 Gen_Id : constant Node_Id := Name (N);
7290 begin
7291 if Is_Child_Unit (Gen_Unit)
7292 and then Is_Child_Unit (Act_Decl_Id)
7293 and then Nkind (Gen_Id) = N_Expanded_Name
7294 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
7295 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
7296 then
7297 Error_Msg_Node_2 := Scope (Act_Decl_Id);
7298 Error_Msg_NE
7299 ("generic unit & is implicitly declared in &",
7300 Defining_Unit_Name (N), Gen_Unit);
7301 Error_Msg_N ("\instance must have different name",
7302 Defining_Unit_Name (N));
7303 end if;
7304 end Check_Hidden_Child_Unit;
7306 ------------------------
7307 -- Check_Private_View --
7308 ------------------------
7310 procedure Check_Private_View (N : Node_Id) is
7311 T : constant Entity_Id := Etype (N);
7312 BT : Entity_Id;
7314 begin
7315 -- Exchange views if the type was not private in the generic but is
7316 -- private at the point of instantiation. Do not exchange views if
7317 -- the scope of the type is in scope. This can happen if both generic
7318 -- and instance are sibling units, or if type is defined in a parent.
7319 -- In this case the visibility of the type will be correct for all
7320 -- semantic checks.
7322 if Present (T) then
7323 BT := Base_Type (T);
7325 if Is_Private_Type (T)
7326 and then not Has_Private_View (N)
7327 and then Present (Full_View (T))
7328 and then not In_Open_Scopes (Scope (T))
7329 then
7330 -- In the generic, the full type was visible. Save the private
7331 -- entity, for subsequent exchange.
7333 Switch_View (T);
7335 elsif Has_Private_View (N)
7336 and then not Is_Private_Type (T)
7337 and then not Has_Been_Exchanged (T)
7338 and then Etype (Get_Associated_Node (N)) /= T
7339 then
7340 -- Only the private declaration was visible in the generic. If
7341 -- the type appears in a subtype declaration, the subtype in the
7342 -- instance must have a view compatible with that of its parent,
7343 -- which must be exchanged (see corresponding code in Restore_
7344 -- Private_Views). Otherwise, if the type is defined in a parent
7345 -- unit, leave full visibility within instance, which is safe.
7347 if In_Open_Scopes (Scope (Base_Type (T)))
7348 and then not Is_Private_Type (Base_Type (T))
7349 and then Comes_From_Source (Base_Type (T))
7350 then
7351 null;
7353 elsif Nkind (Parent (N)) = N_Subtype_Declaration
7354 or else not In_Private_Part (Scope (Base_Type (T)))
7355 then
7356 Prepend_Elmt (T, Exchanged_Views);
7357 Exchange_Declarations (Etype (Get_Associated_Node (N)));
7358 end if;
7360 -- For composite types with inconsistent representation exchange
7361 -- component types accordingly.
7363 elsif Is_Access_Type (T)
7364 and then Is_Private_Type (Designated_Type (T))
7365 and then not Has_Private_View (N)
7366 and then Present (Full_View (Designated_Type (T)))
7367 then
7368 Switch_View (Designated_Type (T));
7370 elsif Is_Array_Type (T) then
7371 if Is_Private_Type (Component_Type (T))
7372 and then not Has_Private_View (N)
7373 and then Present (Full_View (Component_Type (T)))
7374 then
7375 Switch_View (Component_Type (T));
7376 end if;
7378 -- The normal exchange mechanism relies on the setting of a
7379 -- flag on the reference in the generic. However, an additional
7380 -- mechanism is needed for types that are not explicitly
7381 -- mentioned in the generic, but may be needed in expanded code
7382 -- in the instance. This includes component types of arrays and
7383 -- designated types of access types. This processing must also
7384 -- include the index types of arrays which we take care of here.
7386 declare
7387 Indx : Node_Id;
7388 Typ : Entity_Id;
7390 begin
7391 Indx := First_Index (T);
7392 while Present (Indx) loop
7393 Typ := Base_Type (Etype (Indx));
7395 if Is_Private_Type (Typ)
7396 and then Present (Full_View (Typ))
7397 then
7398 Switch_View (Typ);
7399 end if;
7401 Next_Index (Indx);
7402 end loop;
7403 end;
7405 elsif Is_Private_Type (T)
7406 and then Present (Full_View (T))
7407 and then Is_Array_Type (Full_View (T))
7408 and then Is_Private_Type (Component_Type (Full_View (T)))
7409 then
7410 Switch_View (T);
7412 -- Finally, a non-private subtype may have a private base type, which
7413 -- must be exchanged for consistency. This can happen when a package
7414 -- body is instantiated, when the scope stack is empty but in fact
7415 -- the subtype and the base type are declared in an enclosing scope.
7417 -- Note that in this case we introduce an inconsistency in the view
7418 -- set, because we switch the base type BT, but there could be some
7419 -- private dependent subtypes of BT which remain unswitched. Such
7420 -- subtypes might need to be switched at a later point (see specific
7421 -- provision for that case in Switch_View).
7423 elsif not Is_Private_Type (T)
7424 and then not Has_Private_View (N)
7425 and then Is_Private_Type (BT)
7426 and then Present (Full_View (BT))
7427 and then not Is_Generic_Type (BT)
7428 and then not In_Open_Scopes (BT)
7429 then
7430 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7431 Exchange_Declarations (BT);
7432 end if;
7433 end if;
7434 end Check_Private_View;
7436 -----------------------------
7437 -- Check_Hidden_Primitives --
7438 -----------------------------
7440 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7441 Actual : Node_Id;
7442 Gen_T : Entity_Id;
7443 Result : Elist_Id := No_Elist;
7445 begin
7446 if No (Assoc_List) then
7447 return No_Elist;
7448 end if;
7450 -- Traverse the list of associations between formals and actuals
7451 -- searching for renamings of tagged types
7453 Actual := First (Assoc_List);
7454 while Present (Actual) loop
7455 if Nkind (Actual) = N_Subtype_Declaration then
7456 Gen_T := Generic_Parent_Type (Actual);
7458 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7460 -- Traverse the list of primitives of the actual types
7461 -- searching for hidden primitives that are visible in the
7462 -- corresponding generic formal; leave them visible and
7463 -- append them to Result to restore their decoration later.
7465 Install_Hidden_Primitives
7466 (Prims_List => Result,
7467 Gen_T => Gen_T,
7468 Act_T => Entity (Subtype_Indication (Actual)));
7469 end if;
7470 end if;
7472 Next (Actual);
7473 end loop;
7475 return Result;
7476 end Check_Hidden_Primitives;
7478 --------------------------
7479 -- Contains_Instance_Of --
7480 --------------------------
7482 function Contains_Instance_Of
7483 (Inner : Entity_Id;
7484 Outer : Entity_Id;
7485 N : Node_Id) return Boolean
7487 Elmt : Elmt_Id;
7488 Scop : Entity_Id;
7490 begin
7491 Scop := Outer;
7493 -- Verify that there are no circular instantiations. We check whether
7494 -- the unit contains an instance of the current scope or some enclosing
7495 -- scope (in case one of the instances appears in a subunit). Longer
7496 -- circularities involving subunits might seem too pathological to
7497 -- consider, but they were not too pathological for the authors of
7498 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7499 -- enclosing generic scopes as containing an instance.
7501 loop
7502 -- Within a generic subprogram body, the scope is not generic, to
7503 -- allow for recursive subprograms. Use the declaration to determine
7504 -- whether this is a generic unit.
7506 if Ekind (Scop) = E_Generic_Package
7507 or else (Is_Subprogram (Scop)
7508 and then Nkind (Unit_Declaration_Node (Scop)) =
7509 N_Generic_Subprogram_Declaration)
7510 then
7511 Elmt := First_Elmt (Inner_Instances (Inner));
7513 while Present (Elmt) loop
7514 if Node (Elmt) = Scop then
7515 Error_Msg_Node_2 := Inner;
7516 Error_Msg_NE
7517 ("circular Instantiation: & instantiated within &!",
7518 N, Scop);
7519 return True;
7521 elsif Node (Elmt) = Inner then
7522 return True;
7524 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7525 Error_Msg_Node_2 := Inner;
7526 Error_Msg_NE
7527 ("circular Instantiation: & instantiated within &!",
7528 N, Node (Elmt));
7529 return True;
7530 end if;
7532 Next_Elmt (Elmt);
7533 end loop;
7535 -- Indicate that Inner is being instantiated within Scop
7537 Append_Elmt (Inner, Inner_Instances (Scop));
7538 end if;
7540 if Scop = Standard_Standard then
7541 exit;
7542 else
7543 Scop := Scope (Scop);
7544 end if;
7545 end loop;
7547 return False;
7548 end Contains_Instance_Of;
7550 -----------------------
7551 -- Copy_Generic_Node --
7552 -----------------------
7554 function Copy_Generic_Node
7555 (N : Node_Id;
7556 Parent_Id : Node_Id;
7557 Instantiating : Boolean) return Node_Id
7559 Ent : Entity_Id;
7560 New_N : Node_Id;
7562 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7563 -- Check the given value of one of the Fields referenced by the current
7564 -- node to determine whether to copy it recursively. The field may hold
7565 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7566 -- Char) in which case it need not be copied.
7568 procedure Copy_Descendants;
7569 -- Common utility for various nodes
7571 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7572 -- Make copy of element list
7574 function Copy_Generic_List
7575 (L : List_Id;
7576 Parent_Id : Node_Id) return List_Id;
7577 -- Apply Copy_Node recursively to the members of a node list
7579 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7580 -- True if an identifier is part of the defining program unit name of
7581 -- a child unit. The entity of such an identifier must be kept (for
7582 -- ASIS use) even though as the name of an enclosing generic it would
7583 -- otherwise not be preserved in the generic tree.
7585 ----------------------
7586 -- Copy_Descendants --
7587 ----------------------
7589 procedure Copy_Descendants is
7590 use Atree.Unchecked_Access;
7591 -- This code section is part of the implementation of an untyped
7592 -- tree traversal, so it needs direct access to node fields.
7594 begin
7595 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7596 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7597 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7598 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7599 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7600 end Copy_Descendants;
7602 -----------------------------
7603 -- Copy_Generic_Descendant --
7604 -----------------------------
7606 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7607 begin
7608 if D = Union_Id (Empty) then
7609 return D;
7611 elsif D in Node_Range then
7612 return Union_Id
7613 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7615 elsif D in List_Range then
7616 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7618 elsif D in Elist_Range then
7619 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7621 -- Nothing else is copyable (e.g. Uint values), return as is
7623 else
7624 return D;
7625 end if;
7626 end Copy_Generic_Descendant;
7628 ------------------------
7629 -- Copy_Generic_Elist --
7630 ------------------------
7632 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7633 M : Elmt_Id;
7634 L : Elist_Id;
7636 begin
7637 if Present (E) then
7638 L := New_Elmt_List;
7639 M := First_Elmt (E);
7640 while Present (M) loop
7641 Append_Elmt
7642 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7643 Next_Elmt (M);
7644 end loop;
7646 return L;
7648 else
7649 return No_Elist;
7650 end if;
7651 end Copy_Generic_Elist;
7653 -----------------------
7654 -- Copy_Generic_List --
7655 -----------------------
7657 function Copy_Generic_List
7658 (L : List_Id;
7659 Parent_Id : Node_Id) return List_Id
7661 N : Node_Id;
7662 New_L : List_Id;
7664 begin
7665 if Present (L) then
7666 New_L := New_List;
7667 Set_Parent (New_L, Parent_Id);
7669 N := First (L);
7670 while Present (N) loop
7671 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7672 Next (N);
7673 end loop;
7675 return New_L;
7677 else
7678 return No_List;
7679 end if;
7680 end Copy_Generic_List;
7682 ---------------------------
7683 -- In_Defining_Unit_Name --
7684 ---------------------------
7686 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7687 begin
7688 return
7689 Present (Parent (Nam))
7690 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7691 or else
7692 (Nkind (Parent (Nam)) = N_Expanded_Name
7693 and then In_Defining_Unit_Name (Parent (Nam))));
7694 end In_Defining_Unit_Name;
7696 -- Start of processing for Copy_Generic_Node
7698 begin
7699 if N = Empty then
7700 return N;
7701 end if;
7703 New_N := New_Copy (N);
7705 -- Copy aspects if present
7707 if Has_Aspects (N) then
7708 Set_Has_Aspects (New_N, False);
7709 Set_Aspect_Specifications
7710 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7711 end if;
7713 -- If we are instantiating, we want to adjust the sloc based on the
7714 -- current S_Adjustment. However, if this is the root node of a subunit,
7715 -- we need to defer that adjustment to below (see "elsif Instantiating
7716 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
7717 -- computed the adjustment.
7719 if Instantiating
7720 and then not (Nkind (N) in N_Proper_Body
7721 and then Was_Originally_Stub (N))
7722 then
7723 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7724 end if;
7726 if not Is_List_Member (N) then
7727 Set_Parent (New_N, Parent_Id);
7728 end if;
7730 -- Special casing for identifiers and other entity names and operators
7732 if Nkind_In (New_N, N_Character_Literal,
7733 N_Expanded_Name,
7734 N_Identifier,
7735 N_Operator_Symbol)
7736 or else Nkind (New_N) in N_Op
7737 then
7738 if not Instantiating then
7740 -- Link both nodes in order to assign subsequently the entity of
7741 -- the copy to the original node, in case this is a global
7742 -- reference.
7744 Set_Associated_Node (N, New_N);
7746 -- If we are within an instantiation, this is a nested generic
7747 -- that has already been analyzed at the point of definition.
7748 -- We must preserve references that were global to the enclosing
7749 -- parent at that point. Other occurrences, whether global or
7750 -- local to the current generic, must be resolved anew, so we
7751 -- reset the entity in the generic copy. A global reference has a
7752 -- smaller depth than the parent, or else the same depth in case
7753 -- both are distinct compilation units.
7755 -- A child unit is implicitly declared within the enclosing parent
7756 -- but is in fact global to it, and must be preserved.
7758 -- It is also possible for Current_Instantiated_Parent to be
7759 -- defined, and for this not to be a nested generic, namely if
7760 -- the unit is loaded through Rtsfind. In that case, the entity of
7761 -- New_N is only a link to the associated node, and not a defining
7762 -- occurrence.
7764 -- The entities for parent units in the defining_program_unit of a
7765 -- generic child unit are established when the context of the unit
7766 -- is first analyzed, before the generic copy is made. They are
7767 -- preserved in the copy for use in ASIS queries.
7769 Ent := Entity (New_N);
7771 if No (Current_Instantiated_Parent.Gen_Id) then
7772 if No (Ent)
7773 or else Nkind (Ent) /= N_Defining_Identifier
7774 or else not In_Defining_Unit_Name (N)
7775 then
7776 Set_Associated_Node (New_N, Empty);
7777 end if;
7779 elsif No (Ent)
7780 or else
7781 not Nkind_In (Ent, N_Defining_Identifier,
7782 N_Defining_Character_Literal,
7783 N_Defining_Operator_Symbol)
7784 or else No (Scope (Ent))
7785 or else
7786 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7787 and then not Is_Child_Unit (Ent))
7788 or else
7789 (Scope_Depth (Scope (Ent)) >
7790 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7791 and then
7792 Get_Source_Unit (Ent) =
7793 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7794 then
7795 Set_Associated_Node (New_N, Empty);
7796 end if;
7798 -- Case of instantiating identifier or some other name or operator
7800 else
7801 -- If the associated node is still defined, the entity in it
7802 -- is global, and must be copied to the instance. If this copy
7803 -- is being made for a body to inline, it is applied to an
7804 -- instantiated tree, and the entity is already present and
7805 -- must be also preserved.
7807 declare
7808 Assoc : constant Node_Id := Get_Associated_Node (N);
7810 begin
7811 if Present (Assoc) then
7812 if Nkind (Assoc) = Nkind (N) then
7813 Set_Entity (New_N, Entity (Assoc));
7814 Check_Private_View (N);
7816 -- The node is a reference to a global type and acts as the
7817 -- subtype mark of a qualified expression created in order
7818 -- to aid resolution of accidental overloading in instances.
7819 -- Since N is a reference to a type, the Associated_Node of
7820 -- N denotes an entity rather than another identifier. See
7821 -- Qualify_Universal_Operands for details.
7823 elsif Nkind (N) = N_Identifier
7824 and then Nkind (Parent (N)) = N_Qualified_Expression
7825 and then Subtype_Mark (Parent (N)) = N
7826 and then Is_Qualified_Universal_Literal (Parent (N))
7827 then
7828 Set_Entity (New_N, Assoc);
7830 -- The name in the call may be a selected component if the
7831 -- call has not been analyzed yet, as may be the case for
7832 -- pre/post conditions in a generic unit.
7834 elsif Nkind (Assoc) = N_Function_Call
7835 and then Is_Entity_Name (Name (Assoc))
7836 then
7837 Set_Entity (New_N, Entity (Name (Assoc)));
7839 elsif Nkind_In (Assoc, N_Defining_Identifier,
7840 N_Defining_Character_Literal,
7841 N_Defining_Operator_Symbol)
7842 and then Expander_Active
7843 then
7844 -- Inlining case: we are copying a tree that contains
7845 -- global entities, which are preserved in the copy to be
7846 -- used for subsequent inlining.
7848 null;
7850 else
7851 Set_Entity (New_N, Empty);
7852 end if;
7853 end if;
7854 end;
7855 end if;
7857 -- For expanded name, we must copy the Prefix and Selector_Name
7859 if Nkind (N) = N_Expanded_Name then
7860 Set_Prefix
7861 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7863 Set_Selector_Name (New_N,
7864 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7866 -- For operators, copy the operands
7868 elsif Nkind (N) in N_Op then
7869 if Nkind (N) in N_Binary_Op then
7870 Set_Left_Opnd (New_N,
7871 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7872 end if;
7874 Set_Right_Opnd (New_N,
7875 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7876 end if;
7878 -- Establish a link between an entity from the generic template and the
7879 -- corresponding entity in the generic copy to be analyzed.
7881 elsif Nkind (N) in N_Entity then
7882 if not Instantiating then
7883 Set_Associated_Entity (N, New_N);
7884 end if;
7886 -- Clear any existing link the copy may inherit from the replicated
7887 -- generic template entity.
7889 Set_Associated_Entity (New_N, Empty);
7891 -- Special casing for stubs
7893 elsif Nkind (N) in N_Body_Stub then
7895 -- In any case, we must copy the specification or defining
7896 -- identifier as appropriate.
7898 if Nkind (N) = N_Subprogram_Body_Stub then
7899 Set_Specification (New_N,
7900 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7902 else
7903 Set_Defining_Identifier (New_N,
7904 Copy_Generic_Node
7905 (Defining_Identifier (N), New_N, Instantiating));
7906 end if;
7908 -- If we are not instantiating, then this is where we load and
7909 -- analyze subunits, i.e. at the point where the stub occurs. A
7910 -- more permissive system might defer this analysis to the point
7911 -- of instantiation, but this seems too complicated for now.
7913 if not Instantiating then
7914 declare
7915 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7916 Subunit : Node_Id;
7917 Unum : Unit_Number_Type;
7918 New_Body : Node_Id;
7920 begin
7921 -- Make sure that, if it is a subunit of the main unit that is
7922 -- preprocessed and if -gnateG is specified, the preprocessed
7923 -- file will be written.
7925 Lib.Analysing_Subunit_Of_Main :=
7926 Lib.In_Extended_Main_Source_Unit (N);
7927 Unum :=
7928 Load_Unit
7929 (Load_Name => Subunit_Name,
7930 Required => False,
7931 Subunit => True,
7932 Error_Node => N);
7933 Lib.Analysing_Subunit_Of_Main := False;
7935 -- If the proper body is not found, a warning message will be
7936 -- emitted when analyzing the stub, or later at the point of
7937 -- instantiation. Here we just leave the stub as is.
7939 if Unum = No_Unit then
7940 Subunits_Missing := True;
7941 goto Subunit_Not_Found;
7942 end if;
7944 Subunit := Cunit (Unum);
7946 if Nkind (Unit (Subunit)) /= N_Subunit then
7947 Error_Msg_N
7948 ("found child unit instead of expected SEPARATE subunit",
7949 Subunit);
7950 Error_Msg_Sloc := Sloc (N);
7951 Error_Msg_N ("\to complete stub #", Subunit);
7952 goto Subunit_Not_Found;
7953 end if;
7955 -- We must create a generic copy of the subunit, in order to
7956 -- perform semantic analysis on it, and we must replace the
7957 -- stub in the original generic unit with the subunit, in order
7958 -- to preserve non-local references within.
7960 -- Only the proper body needs to be copied. Library_Unit and
7961 -- context clause are simply inherited by the generic copy.
7962 -- Note that the copy (which may be recursive if there are
7963 -- nested subunits) must be done first, before attaching it to
7964 -- the enclosing generic.
7966 New_Body :=
7967 Copy_Generic_Node
7968 (Proper_Body (Unit (Subunit)),
7969 Empty, Instantiating => False);
7971 -- Now place the original proper body in the original generic
7972 -- unit. This is a body, not a compilation unit.
7974 Rewrite (N, Proper_Body (Unit (Subunit)));
7975 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7976 Set_Was_Originally_Stub (N);
7978 -- Finally replace the body of the subunit with its copy, and
7979 -- make this new subunit into the library unit of the generic
7980 -- copy, which does not have stubs any longer.
7982 Set_Proper_Body (Unit (Subunit), New_Body);
7983 Set_Library_Unit (New_N, Subunit);
7984 Inherit_Context (Unit (Subunit), N);
7985 end;
7987 -- If we are instantiating, this must be an error case, since
7988 -- otherwise we would have replaced the stub node by the proper body
7989 -- that corresponds. So just ignore it in the copy (i.e. we have
7990 -- copied it, and that is good enough).
7992 else
7993 null;
7994 end if;
7996 <<Subunit_Not_Found>> null;
7998 -- If the node is a compilation unit, it is the subunit of a stub, which
7999 -- has been loaded already (see code below). In this case, the library
8000 -- unit field of N points to the parent unit (which is a compilation
8001 -- unit) and need not (and cannot) be copied.
8003 -- When the proper body of the stub is analyzed, the library_unit link
8004 -- is used to establish the proper context (see sem_ch10).
8006 -- The other fields of a compilation unit are copied as usual
8008 elsif Nkind (N) = N_Compilation_Unit then
8010 -- This code can only be executed when not instantiating, because in
8011 -- the copy made for an instantiation, the compilation unit node has
8012 -- disappeared at the point that a stub is replaced by its proper
8013 -- body.
8015 pragma Assert (not Instantiating);
8017 Set_Context_Items (New_N,
8018 Copy_Generic_List (Context_Items (N), New_N));
8020 Set_Unit (New_N,
8021 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
8023 Set_First_Inlined_Subprogram (New_N,
8024 Copy_Generic_Node
8025 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
8027 Set_Aux_Decls_Node
8028 (New_N,
8029 Copy_Generic_Node
8030 (Aux_Decls_Node (N), New_N, Instantiating => False));
8032 -- For an assignment node, the assignment is known to be semantically
8033 -- legal if we are instantiating the template. This avoids incorrect
8034 -- diagnostics in generated code.
8036 elsif Nkind (N) = N_Assignment_Statement then
8038 -- Copy name and expression fields in usual manner
8040 Set_Name (New_N,
8041 Copy_Generic_Node (Name (N), New_N, Instantiating));
8043 Set_Expression (New_N,
8044 Copy_Generic_Node (Expression (N), New_N, Instantiating));
8046 if Instantiating then
8047 Set_Assignment_OK (Name (New_N), True);
8048 end if;
8050 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
8051 if not Instantiating then
8052 Set_Associated_Node (N, New_N);
8054 else
8055 if Present (Get_Associated_Node (N))
8056 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
8057 then
8058 -- In the generic the aggregate has some composite type. If at
8059 -- the point of instantiation the type has a private view,
8060 -- install the full view (and that of its ancestors, if any).
8062 declare
8063 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
8064 Rt : Entity_Id;
8066 begin
8067 if Present (T) and then Is_Private_Type (T) then
8068 Switch_View (T);
8069 end if;
8071 if Present (T)
8072 and then Is_Tagged_Type (T)
8073 and then Is_Derived_Type (T)
8074 then
8075 Rt := Root_Type (T);
8077 loop
8078 T := Etype (T);
8080 if Is_Private_Type (T) then
8081 Switch_View (T);
8082 end if;
8084 exit when T = Rt;
8085 end loop;
8086 end if;
8087 end;
8088 end if;
8089 end if;
8091 -- Do not copy the associated node, which points to the generic copy
8092 -- of the aggregate.
8094 declare
8095 use Atree.Unchecked_Access;
8096 -- This code section is part of the implementation of an untyped
8097 -- tree traversal, so it needs direct access to node fields.
8099 begin
8100 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
8101 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
8102 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
8103 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
8104 end;
8106 -- Allocators do not have an identifier denoting the access type, so we
8107 -- must locate it through the expression to check whether the views are
8108 -- consistent.
8110 elsif Nkind (N) = N_Allocator
8111 and then Nkind (Expression (N)) = N_Qualified_Expression
8112 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
8113 and then Instantiating
8114 then
8115 declare
8116 T : constant Node_Id :=
8117 Get_Associated_Node (Subtype_Mark (Expression (N)));
8118 Acc_T : Entity_Id;
8120 begin
8121 if Present (T) then
8123 -- Retrieve the allocator node in the generic copy
8125 Acc_T := Etype (Parent (Parent (T)));
8127 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
8128 Switch_View (Acc_T);
8129 end if;
8130 end if;
8132 Copy_Descendants;
8133 end;
8135 -- For a proper body, we must catch the case of a proper body that
8136 -- replaces a stub. This represents the point at which a separate
8137 -- compilation unit, and hence template file, may be referenced, so we
8138 -- must make a new source instantiation entry for the template of the
8139 -- subunit, and ensure that all nodes in the subunit are adjusted using
8140 -- this new source instantiation entry.
8142 elsif Nkind (N) in N_Proper_Body then
8143 declare
8144 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
8145 begin
8146 if Instantiating and then Was_Originally_Stub (N) then
8147 Create_Instantiation_Source
8148 (Instantiation_Node,
8149 Defining_Entity (N),
8150 S_Adjustment);
8152 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8153 end if;
8155 -- Now copy the fields of the proper body, using the new
8156 -- adjustment factor if one was needed as per test above.
8158 Copy_Descendants;
8160 -- Restore the original adjustment factor
8162 S_Adjustment := Save_Adjustment;
8163 end;
8165 elsif Nkind (N) = N_Pragma and then Instantiating then
8167 -- Do not copy Comment or Ident pragmas their content is relevant to
8168 -- the generic unit, not to the instantiating unit.
8170 if Nam_In (Pragma_Name_Unmapped (N), Name_Comment, Name_Ident) then
8171 New_N := Make_Null_Statement (Sloc (N));
8173 -- Do not copy pragmas generated from aspects because the pragmas do
8174 -- not carry any semantic information, plus they will be regenerated
8175 -- in the instance.
8177 -- However, generating C we need to copy them since postconditions
8178 -- are inlined by the front end, and the front-end inlining machinery
8179 -- relies on this routine to perform inlining.
8181 elsif From_Aspect_Specification (N)
8182 and then not Modify_Tree_For_C
8183 then
8184 New_N := Make_Null_Statement (Sloc (N));
8186 else
8187 Copy_Descendants;
8188 end if;
8190 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
8192 -- No descendant fields need traversing
8194 null;
8196 elsif Nkind (N) = N_String_Literal
8197 and then Present (Etype (N))
8198 and then Instantiating
8199 then
8200 -- If the string is declared in an outer scope, the string_literal
8201 -- subtype created for it may have the wrong scope. Force reanalysis
8202 -- of the constant to generate a new itype in the proper context.
8204 Set_Etype (New_N, Empty);
8205 Set_Analyzed (New_N, False);
8207 -- For the remaining nodes, copy their descendants recursively
8209 else
8210 Copy_Descendants;
8212 if Instantiating and then Nkind (N) = N_Subprogram_Body then
8213 Set_Generic_Parent (Specification (New_N), N);
8215 -- Should preserve Corresponding_Spec??? (12.3(14))
8216 end if;
8217 end if;
8219 -- Propagate dimensions if present, so that they are reflected in the
8220 -- instance.
8222 if Nkind (N) in N_Has_Etype
8223 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
8224 and then Present (Etype (N))
8225 and then Is_Floating_Point_Type (Etype (N))
8226 and then Has_Dimension_System (Etype (N))
8227 then
8228 Copy_Dimensions (N, New_N);
8229 end if;
8231 return New_N;
8232 end Copy_Generic_Node;
8234 ----------------------------
8235 -- Denotes_Formal_Package --
8236 ----------------------------
8238 function Denotes_Formal_Package
8239 (Pack : Entity_Id;
8240 On_Exit : Boolean := False;
8241 Instance : Entity_Id := Empty) return Boolean
8243 Par : Entity_Id;
8244 Scop : constant Entity_Id := Scope (Pack);
8245 E : Entity_Id;
8247 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
8248 -- The package in question may be an actual for a previous formal
8249 -- package P of the current instance, so examine its actuals as well.
8250 -- This must be recursive over other formal packages.
8252 ----------------------------------
8253 -- Is_Actual_Of_Previous_Formal --
8254 ----------------------------------
8256 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
8257 E1 : Entity_Id;
8259 begin
8260 E1 := First_Entity (P);
8261 while Present (E1) and then E1 /= Instance loop
8262 if Ekind (E1) = E_Package
8263 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
8264 then
8265 if Renamed_Object (E1) = Pack then
8266 return True;
8268 elsif E1 = P or else Renamed_Object (E1) = P then
8269 return False;
8271 elsif Is_Actual_Of_Previous_Formal (E1) then
8272 return True;
8273 end if;
8274 end if;
8276 Next_Entity (E1);
8277 end loop;
8279 return False;
8280 end Is_Actual_Of_Previous_Formal;
8282 -- Start of processing for Denotes_Formal_Package
8284 begin
8285 if On_Exit then
8286 Par :=
8287 Instance_Envs.Table
8288 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
8289 else
8290 Par := Current_Instantiated_Parent.Act_Id;
8291 end if;
8293 if Ekind (Scop) = E_Generic_Package
8294 or else Nkind (Unit_Declaration_Node (Scop)) =
8295 N_Generic_Subprogram_Declaration
8296 then
8297 return True;
8299 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
8300 N_Formal_Package_Declaration
8301 then
8302 return True;
8304 elsif No (Par) then
8305 return False;
8307 else
8308 -- Check whether this package is associated with a formal package of
8309 -- the enclosing instantiation. Iterate over the list of renamings.
8311 E := First_Entity (Par);
8312 while Present (E) loop
8313 if Ekind (E) /= E_Package
8314 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
8315 then
8316 null;
8318 elsif Renamed_Object (E) = Par then
8319 return False;
8321 elsif Renamed_Object (E) = Pack then
8322 return True;
8324 elsif Is_Actual_Of_Previous_Formal (E) then
8325 return True;
8327 end if;
8329 Next_Entity (E);
8330 end loop;
8332 return False;
8333 end if;
8334 end Denotes_Formal_Package;
8336 -----------------
8337 -- End_Generic --
8338 -----------------
8340 procedure End_Generic is
8341 begin
8342 -- ??? More things could be factored out in this routine. Should
8343 -- probably be done at a later stage.
8345 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8346 Generic_Flags.Decrement_Last;
8348 Expander_Mode_Restore;
8349 end End_Generic;
8351 -------------
8352 -- Earlier --
8353 -------------
8355 function Earlier (N1, N2 : Node_Id) return Boolean is
8356 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8357 -- Find distance from given node to enclosing compilation unit
8359 ----------------
8360 -- Find_Depth --
8361 ----------------
8363 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8364 begin
8365 while Present (P)
8366 and then Nkind (P) /= N_Compilation_Unit
8367 loop
8368 P := True_Parent (P);
8369 D := D + 1;
8370 end loop;
8371 end Find_Depth;
8373 -- Local declarations
8375 D1 : Integer := 0;
8376 D2 : Integer := 0;
8377 P1 : Node_Id := N1;
8378 P2 : Node_Id := N2;
8379 T1 : Source_Ptr;
8380 T2 : Source_Ptr;
8382 -- Start of processing for Earlier
8384 begin
8385 Find_Depth (P1, D1);
8386 Find_Depth (P2, D2);
8388 if P1 /= P2 then
8389 return False;
8390 else
8391 P1 := N1;
8392 P2 := N2;
8393 end if;
8395 while D1 > D2 loop
8396 P1 := True_Parent (P1);
8397 D1 := D1 - 1;
8398 end loop;
8400 while D2 > D1 loop
8401 P2 := True_Parent (P2);
8402 D2 := D2 - 1;
8403 end loop;
8405 -- At this point P1 and P2 are at the same distance from the root.
8406 -- We examine their parents until we find a common declarative list.
8407 -- If we reach the root, N1 and N2 do not descend from the same
8408 -- declarative list (e.g. one is nested in the declarative part and
8409 -- the other is in a block in the statement part) and the earlier
8410 -- one is already frozen.
8412 while not Is_List_Member (P1)
8413 or else not Is_List_Member (P2)
8414 or else List_Containing (P1) /= List_Containing (P2)
8415 loop
8416 P1 := True_Parent (P1);
8417 P2 := True_Parent (P2);
8419 if Nkind (Parent (P1)) = N_Subunit then
8420 P1 := Corresponding_Stub (Parent (P1));
8421 end if;
8423 if Nkind (Parent (P2)) = N_Subunit then
8424 P2 := Corresponding_Stub (Parent (P2));
8425 end if;
8427 if P1 = P2 then
8428 return False;
8429 end if;
8430 end loop;
8432 -- Expanded code usually shares the source location of the original
8433 -- construct it was generated for. This however may not necessarily
8434 -- reflect the true location of the code within the tree.
8436 -- Before comparing the slocs of the two nodes, make sure that we are
8437 -- working with correct source locations. Assume that P1 is to the left
8438 -- of P2. If either one does not come from source, traverse the common
8439 -- list heading towards the other node and locate the first source
8440 -- statement.
8442 -- P1 P2
8443 -- ----+===+===+--------------+===+===+----
8444 -- expanded code expanded code
8446 if not Comes_From_Source (P1) then
8447 while Present (P1) loop
8449 -- Neither P2 nor a source statement were located during the
8450 -- search. If we reach the end of the list, then P1 does not
8451 -- occur earlier than P2.
8453 -- ---->
8454 -- start --- P2 ----- P1 --- end
8456 if No (Next (P1)) then
8457 return False;
8459 -- We encounter P2 while going to the right of the list. This
8460 -- means that P1 does indeed appear earlier.
8462 -- ---->
8463 -- start --- P1 ===== P2 --- end
8464 -- expanded code in between
8466 elsif P1 = P2 then
8467 return True;
8469 -- No need to look any further since we have located a source
8470 -- statement.
8472 elsif Comes_From_Source (P1) then
8473 exit;
8474 end if;
8476 -- Keep going right
8478 Next (P1);
8479 end loop;
8480 end if;
8482 if not Comes_From_Source (P2) then
8483 while Present (P2) loop
8485 -- Neither P1 nor a source statement were located during the
8486 -- search. If we reach the start of the list, then P1 does not
8487 -- occur earlier than P2.
8489 -- <----
8490 -- start --- P2 --- P1 --- end
8492 if No (Prev (P2)) then
8493 return False;
8495 -- We encounter P1 while going to the left of the list. This
8496 -- means that P1 does indeed appear earlier.
8498 -- <----
8499 -- start --- P1 ===== P2 --- end
8500 -- expanded code in between
8502 elsif P2 = P1 then
8503 return True;
8505 -- No need to look any further since we have located a source
8506 -- statement.
8508 elsif Comes_From_Source (P2) then
8509 exit;
8510 end if;
8512 -- Keep going left
8514 Prev (P2);
8515 end loop;
8516 end if;
8518 -- At this point either both nodes came from source or we approximated
8519 -- their source locations through neighboring source statements.
8521 T1 := Top_Level_Location (Sloc (P1));
8522 T2 := Top_Level_Location (Sloc (P2));
8524 -- When two nodes come from the same instance, they have identical top
8525 -- level locations. To determine proper relation within the tree, check
8526 -- their locations within the template.
8528 if T1 = T2 then
8529 return Sloc (P1) < Sloc (P2);
8531 -- The two nodes either come from unrelated instances or do not come
8532 -- from instantiated code at all.
8534 else
8535 return T1 < T2;
8536 end if;
8537 end Earlier;
8539 ----------------------
8540 -- Find_Actual_Type --
8541 ----------------------
8543 function Find_Actual_Type
8544 (Typ : Entity_Id;
8545 Gen_Type : Entity_Id) return Entity_Id
8547 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8548 T : Entity_Id;
8550 begin
8551 -- Special processing only applies to child units
8553 if not Is_Child_Unit (Gen_Scope) then
8554 return Get_Instance_Of (Typ);
8556 -- If designated or component type is itself a formal of the child unit,
8557 -- its instance is available.
8559 elsif Scope (Typ) = Gen_Scope then
8560 return Get_Instance_Of (Typ);
8562 -- If the array or access type is not declared in the parent unit,
8563 -- no special processing needed.
8565 elsif not Is_Generic_Type (Typ)
8566 and then Scope (Gen_Scope) /= Scope (Typ)
8567 then
8568 return Get_Instance_Of (Typ);
8570 -- Otherwise, retrieve designated or component type by visibility
8572 else
8573 T := Current_Entity (Typ);
8574 while Present (T) loop
8575 if In_Open_Scopes (Scope (T)) then
8576 return T;
8577 elsif Is_Generic_Actual_Type (T) then
8578 return T;
8579 end if;
8581 T := Homonym (T);
8582 end loop;
8584 return Typ;
8585 end if;
8586 end Find_Actual_Type;
8588 ----------------------------
8589 -- Freeze_Subprogram_Body --
8590 ----------------------------
8592 procedure Freeze_Subprogram_Body
8593 (Inst_Node : Node_Id;
8594 Gen_Body : Node_Id;
8595 Pack_Id : Entity_Id)
8597 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8598 Par : constant Entity_Id := Scope (Gen_Unit);
8599 E_G_Id : Entity_Id;
8600 Enc_G : Entity_Id;
8601 Enc_I : Node_Id;
8602 F_Node : Node_Id;
8604 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8605 -- Find innermost package body that encloses the given node, and which
8606 -- is not a compilation unit. Freeze nodes for the instance, or for its
8607 -- enclosing body, may be inserted after the enclosing_body of the
8608 -- generic unit. Used to determine proper placement of freeze node for
8609 -- both package and subprogram instances.
8611 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8612 -- Find entity for given package body, and locate or create a freeze
8613 -- node for it.
8615 ----------------------------
8616 -- Enclosing_Package_Body --
8617 ----------------------------
8619 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8620 P : Node_Id;
8622 begin
8623 P := Parent (N);
8624 while Present (P)
8625 and then Nkind (Parent (P)) /= N_Compilation_Unit
8626 loop
8627 if Nkind (P) = N_Package_Body then
8628 if Nkind (Parent (P)) = N_Subunit then
8629 return Corresponding_Stub (Parent (P));
8630 else
8631 return P;
8632 end if;
8633 end if;
8635 P := True_Parent (P);
8636 end loop;
8638 return Empty;
8639 end Enclosing_Package_Body;
8641 -------------------------
8642 -- Package_Freeze_Node --
8643 -------------------------
8645 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8646 Id : Entity_Id;
8648 begin
8649 if Nkind (B) = N_Package_Body then
8650 Id := Corresponding_Spec (B);
8651 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8652 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8653 end if;
8655 Ensure_Freeze_Node (Id);
8656 return Freeze_Node (Id);
8657 end Package_Freeze_Node;
8659 -- Start of processing for Freeze_Subprogram_Body
8661 begin
8662 -- If the instance and the generic body appear within the same unit, and
8663 -- the instance precedes the generic, the freeze node for the instance
8664 -- must appear after that of the generic. If the generic is nested
8665 -- within another instance I2, then current instance must be frozen
8666 -- after I2. In both cases, the freeze nodes are those of enclosing
8667 -- packages. Otherwise, the freeze node is placed at the end of the
8668 -- current declarative part.
8670 Enc_G := Enclosing_Package_Body (Gen_Body);
8671 Enc_I := Enclosing_Package_Body (Inst_Node);
8672 Ensure_Freeze_Node (Pack_Id);
8673 F_Node := Freeze_Node (Pack_Id);
8675 if Is_Generic_Instance (Par)
8676 and then Present (Freeze_Node (Par))
8677 and then In_Same_Declarative_Part
8678 (Parent (Freeze_Node (Par)), Inst_Node)
8679 then
8680 -- The parent was a premature instantiation. Insert freeze node at
8681 -- the end the current declarative part.
8683 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par)) then
8684 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8686 -- Handle the following case:
8688 -- package Parent_Inst is new ...
8689 -- Parent_Inst []
8691 -- procedure P ... -- this body freezes Parent_Inst
8693 -- package Inst is new ...
8695 -- In this particular scenario, the freeze node for Inst must be
8696 -- inserted in the same manner as that of Parent_Inst - before the
8697 -- next source body or at the end of the declarative list (body not
8698 -- available). If body P did not exist and Parent_Inst was frozen
8699 -- after Inst, either by a body following Inst or at the end of the
8700 -- declarative region, the freeze node for Inst must be inserted
8701 -- after that of Parent_Inst. This relation is established by
8702 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8704 elsif List_Containing (Get_Unit_Instantiation_Node (Par)) =
8705 List_Containing (Inst_Node)
8706 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8707 then
8708 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8710 else
8711 Insert_After (Freeze_Node (Par), F_Node);
8712 end if;
8714 -- The body enclosing the instance should be frozen after the body that
8715 -- includes the generic, because the body of the instance may make
8716 -- references to entities therein. If the two are not in the same
8717 -- declarative part, or if the one enclosing the instance is frozen
8718 -- already, freeze the instance at the end of the current declarative
8719 -- part.
8721 elsif Is_Generic_Instance (Par)
8722 and then Present (Freeze_Node (Par))
8723 and then Present (Enc_I)
8724 then
8725 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), Enc_I)
8726 or else
8727 (Nkind (Enc_I) = N_Package_Body
8728 and then In_Same_Declarative_Part
8729 (Parent (Freeze_Node (Par)), Parent (Enc_I)))
8730 then
8731 -- The enclosing package may contain several instances. Rather
8732 -- than computing the earliest point at which to insert its freeze
8733 -- node, we place it at the end of the declarative part of the
8734 -- parent of the generic.
8736 Insert_Freeze_Node_For_Instance
8737 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8738 end if;
8740 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8742 elsif Present (Enc_G)
8743 and then Present (Enc_I)
8744 and then Enc_G /= Enc_I
8745 and then Earlier (Inst_Node, Gen_Body)
8746 then
8747 if Nkind (Enc_G) = N_Package_Body then
8748 E_G_Id :=
8749 Corresponding_Spec (Enc_G);
8750 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8751 E_G_Id :=
8752 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8753 end if;
8755 -- Freeze package that encloses instance, and place node after the
8756 -- package that encloses generic. If enclosing package is already
8757 -- frozen we have to assume it is at the proper place. This may be a
8758 -- potential ABE that requires dynamic checking. Do not add a freeze
8759 -- node if the package that encloses the generic is inside the body
8760 -- that encloses the instance, because the freeze node would be in
8761 -- the wrong scope. Additional contortions needed if the bodies are
8762 -- within a subunit.
8764 declare
8765 Enclosing_Body : Node_Id;
8767 begin
8768 if Nkind (Enc_I) = N_Package_Body_Stub then
8769 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8770 else
8771 Enclosing_Body := Enc_I;
8772 end if;
8774 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8775 Insert_Freeze_Node_For_Instance
8776 (Enc_G, Package_Freeze_Node (Enc_I));
8777 end if;
8778 end;
8780 -- Freeze enclosing subunit before instance
8782 Ensure_Freeze_Node (E_G_Id);
8784 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8785 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8786 end if;
8788 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8790 else
8791 -- If none of the above, insert freeze node at the end of the current
8792 -- declarative part.
8794 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8795 end if;
8796 end Freeze_Subprogram_Body;
8798 ----------------
8799 -- Get_Gen_Id --
8800 ----------------
8802 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8803 begin
8804 return Generic_Renamings.Table (E).Gen_Id;
8805 end Get_Gen_Id;
8807 ---------------------
8808 -- Get_Instance_Of --
8809 ---------------------
8811 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8812 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8814 begin
8815 if Res /= Assoc_Null then
8816 return Generic_Renamings.Table (Res).Act_Id;
8818 else
8819 -- On exit, entity is not instantiated: not a generic parameter, or
8820 -- else parameter of an inner generic unit.
8822 return A;
8823 end if;
8824 end Get_Instance_Of;
8826 ---------------------------------
8827 -- Get_Unit_Instantiation_Node --
8828 ---------------------------------
8830 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
8831 Decl : Node_Id := Unit_Declaration_Node (A);
8832 Inst : Node_Id;
8834 begin
8835 -- If the Package_Instantiation attribute has been set on the package
8836 -- entity, then use it directly when it (or its Original_Node) refers
8837 -- to an N_Package_Instantiation node. In principle it should be
8838 -- possible to have this field set in all cases, which should be
8839 -- investigated, and would allow this function to be significantly
8840 -- simplified. ???
8842 Inst := Package_Instantiation (A);
8844 if Present (Inst) then
8845 if Nkind (Inst) = N_Package_Instantiation then
8846 return Inst;
8848 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8849 return Original_Node (Inst);
8850 end if;
8851 end if;
8853 -- If the instantiation is a compilation unit that does not need body
8854 -- then the instantiation node has been rewritten as a package
8855 -- declaration for the instance, and we return the original node.
8857 -- If it is a compilation unit and the instance node has not been
8858 -- rewritten, then it is still the unit of the compilation. Finally, if
8859 -- a body is present, this is a parent of the main unit whose body has
8860 -- been compiled for inlining purposes, and the instantiation node has
8861 -- been rewritten with the instance body.
8863 -- Otherwise the instantiation node appears after the declaration. If
8864 -- the entity is a formal package, the declaration may have been
8865 -- rewritten as a generic declaration (in the case of a formal with box)
8866 -- or left as a formal package declaration if it has actuals, and is
8867 -- found with a forward search.
8869 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8870 if Nkind (Decl) = N_Package_Declaration
8871 and then Present (Corresponding_Body (Decl))
8872 then
8873 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8874 end if;
8876 if Nkind_In (Original_Node (Decl), N_Function_Instantiation,
8877 N_Package_Instantiation,
8878 N_Procedure_Instantiation)
8879 then
8880 return Original_Node (Decl);
8881 else
8882 return Unit (Parent (Decl));
8883 end if;
8885 elsif Nkind (Decl) = N_Package_Declaration
8886 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8887 then
8888 return Original_Node (Decl);
8890 else
8891 Inst := Next (Decl);
8892 while not Nkind_In (Inst, N_Formal_Package_Declaration,
8893 N_Function_Instantiation,
8894 N_Package_Instantiation,
8895 N_Procedure_Instantiation)
8896 loop
8897 Next (Inst);
8898 end loop;
8900 return Inst;
8901 end if;
8902 end Get_Unit_Instantiation_Node;
8904 ------------------------
8905 -- Has_Been_Exchanged --
8906 ------------------------
8908 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8909 Next : Elmt_Id;
8911 begin
8912 Next := First_Elmt (Exchanged_Views);
8913 while Present (Next) loop
8914 if Full_View (Node (Next)) = E then
8915 return True;
8916 end if;
8918 Next_Elmt (Next);
8919 end loop;
8921 return False;
8922 end Has_Been_Exchanged;
8924 ----------
8925 -- Hash --
8926 ----------
8928 function Hash (F : Entity_Id) return HTable_Range is
8929 begin
8930 return HTable_Range (F mod HTable_Size);
8931 end Hash;
8933 ------------------------
8934 -- Hide_Current_Scope --
8935 ------------------------
8937 procedure Hide_Current_Scope is
8938 C : constant Entity_Id := Current_Scope;
8939 E : Entity_Id;
8941 begin
8942 Set_Is_Hidden_Open_Scope (C);
8944 E := First_Entity (C);
8945 while Present (E) loop
8946 if Is_Immediately_Visible (E) then
8947 Set_Is_Immediately_Visible (E, False);
8948 Append_Elmt (E, Hidden_Entities);
8949 end if;
8951 Next_Entity (E);
8952 end loop;
8954 -- Make the scope name invisible as well. This is necessary, but might
8955 -- conflict with calls to Rtsfind later on, in case the scope is a
8956 -- predefined one. There is no clean solution to this problem, so for
8957 -- now we depend on the user not redefining Standard itself in one of
8958 -- the parent units.
8960 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8961 Set_Is_Immediately_Visible (C, False);
8962 Append_Elmt (C, Hidden_Entities);
8963 end if;
8965 end Hide_Current_Scope;
8967 --------------
8968 -- Init_Env --
8969 --------------
8971 procedure Init_Env is
8972 Saved : Instance_Env;
8974 begin
8975 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8976 Saved.Exchanged_Views := Exchanged_Views;
8977 Saved.Hidden_Entities := Hidden_Entities;
8978 Saved.Current_Sem_Unit := Current_Sem_Unit;
8979 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8980 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8982 -- Save configuration switches. These may be reset if the unit is a
8983 -- predefined unit, and the current mode is not Ada 2005.
8985 Saved.Switches := Save_Config_Switches;
8987 Instance_Envs.Append (Saved);
8989 Exchanged_Views := New_Elmt_List;
8990 Hidden_Entities := New_Elmt_List;
8992 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8993 -- this is set properly in Set_Instance_Env.
8995 Current_Instantiated_Parent :=
8996 (Current_Scope, Current_Scope, Assoc_Null);
8997 end Init_Env;
8999 ---------------------
9000 -- In_Main_Context --
9001 ---------------------
9003 function In_Main_Context (E : Entity_Id) return Boolean is
9004 Context : List_Id;
9005 Clause : Node_Id;
9006 Nam : Node_Id;
9008 begin
9009 if not Is_Compilation_Unit (E)
9010 or else Ekind (E) /= E_Package
9011 or else In_Private_Part (E)
9012 then
9013 return False;
9014 end if;
9016 Context := Context_Items (Cunit (Main_Unit));
9018 Clause := First (Context);
9019 while Present (Clause) loop
9020 if Nkind (Clause) = N_With_Clause then
9021 Nam := Name (Clause);
9023 -- If the current scope is part of the context of the main unit,
9024 -- analysis of the corresponding with_clause is not complete, and
9025 -- the entity is not set. We use the Chars field directly, which
9026 -- might produce false positives in rare cases, but guarantees
9027 -- that we produce all the instance bodies we will need.
9029 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
9030 or else (Nkind (Nam) = N_Selected_Component
9031 and then Chars (Selector_Name (Nam)) = Chars (E))
9032 then
9033 return True;
9034 end if;
9035 end if;
9037 Next (Clause);
9038 end loop;
9040 return False;
9041 end In_Main_Context;
9043 ---------------------
9044 -- Inherit_Context --
9045 ---------------------
9047 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
9048 Current_Context : List_Id;
9049 Current_Unit : Node_Id;
9050 Item : Node_Id;
9051 New_I : Node_Id;
9053 Clause : Node_Id;
9054 OK : Boolean;
9055 Lib_Unit : Node_Id;
9057 begin
9058 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
9060 -- The inherited context is attached to the enclosing compilation
9061 -- unit. This is either the main unit, or the declaration for the
9062 -- main unit (in case the instantiation appears within the package
9063 -- declaration and the main unit is its body).
9065 Current_Unit := Parent (Inst);
9066 while Present (Current_Unit)
9067 and then Nkind (Current_Unit) /= N_Compilation_Unit
9068 loop
9069 Current_Unit := Parent (Current_Unit);
9070 end loop;
9072 Current_Context := Context_Items (Current_Unit);
9074 Item := First (Context_Items (Parent (Gen_Decl)));
9075 while Present (Item) loop
9076 if Nkind (Item) = N_With_Clause then
9077 Lib_Unit := Library_Unit (Item);
9079 -- Take care to prevent direct cyclic with's
9081 if Lib_Unit /= Current_Unit then
9083 -- Do not add a unit if it is already in the context
9085 Clause := First (Current_Context);
9086 OK := True;
9087 while Present (Clause) loop
9088 if Nkind (Clause) = N_With_Clause
9089 and then Library_Unit (Clause) = Lib_Unit
9090 then
9091 OK := False;
9092 exit;
9093 end if;
9095 Next (Clause);
9096 end loop;
9098 if OK then
9099 New_I := New_Copy (Item);
9100 Set_Implicit_With (New_I);
9102 Append (New_I, Current_Context);
9103 end if;
9104 end if;
9105 end if;
9107 Next (Item);
9108 end loop;
9109 end if;
9110 end Inherit_Context;
9112 ----------------
9113 -- Initialize --
9114 ----------------
9116 procedure Initialize is
9117 begin
9118 Generic_Renamings.Init;
9119 Instance_Envs.Init;
9120 Generic_Flags.Init;
9121 Generic_Renamings_HTable.Reset;
9122 Circularity_Detected := False;
9123 Exchanged_Views := No_Elist;
9124 Hidden_Entities := No_Elist;
9125 end Initialize;
9127 -------------------------------------
9128 -- Insert_Freeze_Node_For_Instance --
9129 -------------------------------------
9131 procedure Insert_Freeze_Node_For_Instance
9132 (N : Node_Id;
9133 F_Node : Node_Id)
9135 Decl : Node_Id;
9136 Decls : List_Id;
9137 Inst : Entity_Id;
9138 Par_N : Node_Id;
9140 function Enclosing_Body (N : Node_Id) return Node_Id;
9141 -- Find enclosing package or subprogram body, if any. Freeze node may
9142 -- be placed at end of current declarative list if previous instance
9143 -- and current one have different enclosing bodies.
9145 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
9146 -- Find the local instance, if any, that declares the generic that is
9147 -- being instantiated. If present, the freeze node for this instance
9148 -- must follow the freeze node for the previous instance.
9150 --------------------
9151 -- Enclosing_Body --
9152 --------------------
9154 function Enclosing_Body (N : Node_Id) return Node_Id is
9155 P : Node_Id;
9157 begin
9158 P := Parent (N);
9159 while Present (P)
9160 and then Nkind (Parent (P)) /= N_Compilation_Unit
9161 loop
9162 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
9163 if Nkind (Parent (P)) = N_Subunit then
9164 return Corresponding_Stub (Parent (P));
9165 else
9166 return P;
9167 end if;
9168 end if;
9170 P := True_Parent (P);
9171 end loop;
9173 return Empty;
9174 end Enclosing_Body;
9176 -----------------------
9177 -- Previous_Instance --
9178 -----------------------
9180 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
9181 S : Entity_Id;
9183 begin
9184 S := Scope (Gen);
9185 while Present (S) and then S /= Standard_Standard loop
9186 if Is_Generic_Instance (S)
9187 and then In_Same_Source_Unit (S, N)
9188 then
9189 return S;
9190 end if;
9192 S := Scope (S);
9193 end loop;
9195 return Empty;
9196 end Previous_Instance;
9198 -- Start of processing for Insert_Freeze_Node_For_Instance
9200 begin
9201 if not Is_List_Member (F_Node) then
9202 Decl := N;
9203 Decls := List_Containing (N);
9204 Inst := Entity (F_Node);
9205 Par_N := Parent (Decls);
9207 -- When processing a subprogram instantiation, utilize the actual
9208 -- subprogram instantiation rather than its package wrapper as it
9209 -- carries all the context information.
9211 if Is_Wrapper_Package (Inst) then
9212 Inst := Related_Instance (Inst);
9213 end if;
9215 -- If this is a package instance, check whether the generic is
9216 -- declared in a previous instance and the current instance is
9217 -- not within the previous one.
9219 if Present (Generic_Parent (Parent (Inst)))
9220 and then Is_In_Main_Unit (N)
9221 then
9222 declare
9223 Enclosing_N : constant Node_Id := Enclosing_Body (N);
9224 Par_I : constant Entity_Id :=
9225 Previous_Instance
9226 (Generic_Parent (Parent (Inst)));
9227 Scop : Entity_Id;
9229 begin
9230 if Present (Par_I)
9231 and then Earlier (N, Freeze_Node (Par_I))
9232 then
9233 Scop := Scope (Inst);
9235 -- If the current instance is within the one that contains
9236 -- the generic, the freeze node for the current one must
9237 -- appear in the current declarative part. Ditto, if the
9238 -- current instance is within another package instance or
9239 -- within a body that does not enclose the current instance.
9240 -- In these three cases the freeze node of the previous
9241 -- instance is not relevant.
9243 while Present (Scop) and then Scop /= Standard_Standard loop
9244 exit when Scop = Par_I
9245 or else
9246 (Is_Generic_Instance (Scop)
9247 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
9248 Scop := Scope (Scop);
9249 end loop;
9251 -- Previous instance encloses current instance
9253 if Scop = Par_I then
9254 null;
9256 -- If the next node is a source body we must freeze in
9257 -- the current scope as well.
9259 elsif Present (Next (N))
9260 and then Nkind_In (Next (N), N_Subprogram_Body,
9261 N_Package_Body)
9262 and then Comes_From_Source (Next (N))
9263 then
9264 null;
9266 -- Current instance is within an unrelated instance
9268 elsif Is_Generic_Instance (Scop) then
9269 null;
9271 -- Current instance is within an unrelated body
9273 elsif Present (Enclosing_N)
9274 and then Enclosing_N /= Enclosing_Body (Par_I)
9275 then
9276 null;
9278 else
9279 Insert_After (Freeze_Node (Par_I), F_Node);
9280 return;
9281 end if;
9282 end if;
9283 end;
9284 end if;
9286 -- When the instantiation occurs in a package declaration, append the
9287 -- freeze node to the private declarations (if any).
9289 if Nkind (Par_N) = N_Package_Specification
9290 and then Decls = Visible_Declarations (Par_N)
9291 and then Present (Private_Declarations (Par_N))
9292 and then not Is_Empty_List (Private_Declarations (Par_N))
9293 then
9294 Decls := Private_Declarations (Par_N);
9295 Decl := First (Decls);
9296 end if;
9298 -- Determine the proper freeze point of a package instantiation. We
9299 -- adhere to the general rule of a package or subprogram body causing
9300 -- freezing of anything before it in the same declarative region. In
9301 -- this case, the proper freeze point of a package instantiation is
9302 -- before the first source body which follows, or before a stub. This
9303 -- ensures that entities coming from the instance are already frozen
9304 -- and usable in source bodies.
9306 if Nkind (Par_N) /= N_Package_Declaration
9307 and then Ekind (Inst) = E_Package
9308 and then Is_Generic_Instance (Inst)
9309 and then
9310 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
9311 then
9312 while Present (Decl) loop
9313 if (Nkind (Decl) in N_Unit_Body
9314 or else
9315 Nkind (Decl) in N_Body_Stub)
9316 and then Comes_From_Source (Decl)
9317 then
9318 Insert_Before (Decl, F_Node);
9319 return;
9320 end if;
9322 Next (Decl);
9323 end loop;
9324 end if;
9326 -- In a package declaration, or if no previous body, insert at end
9327 -- of list.
9329 Set_Sloc (F_Node, Sloc (Last (Decls)));
9330 Insert_After (Last (Decls), F_Node);
9331 end if;
9332 end Insert_Freeze_Node_For_Instance;
9334 ------------------
9335 -- Install_Body --
9336 ------------------
9338 procedure Install_Body
9339 (Act_Body : Node_Id;
9340 N : Node_Id;
9341 Gen_Body : Node_Id;
9342 Gen_Decl : Node_Id)
9344 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9345 -- Check if the generic definition and the instantiation come from
9346 -- a common scope, in which case the instance must be frozen after
9347 -- the generic body.
9349 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9350 -- If the instance is nested inside a generic unit, the Sloc of the
9351 -- instance indicates the place of the original definition, not the
9352 -- point of the current enclosing instance. Pending a better usage of
9353 -- Slocs to indicate instantiation places, we determine the place of
9354 -- origin of a node by finding the maximum sloc of any ancestor node.
9355 -- Why is this not equivalent to Top_Level_Location ???
9357 -------------------
9358 -- In_Same_Scope --
9359 -------------------
9361 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9362 Act_Scop : Entity_Id := Scope (Act_Id);
9363 Gen_Scop : Entity_Id := Scope (Gen_Id);
9365 begin
9366 while Act_Scop /= Standard_Standard
9367 and then Gen_Scop /= Standard_Standard
9368 loop
9369 if Act_Scop = Gen_Scop then
9370 return True;
9371 end if;
9373 Act_Scop := Scope (Act_Scop);
9374 Gen_Scop := Scope (Gen_Scop);
9375 end loop;
9377 return False;
9378 end In_Same_Scope;
9380 ---------------
9381 -- True_Sloc --
9382 ---------------
9384 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9385 N1 : Node_Id;
9386 Res : Source_Ptr;
9388 begin
9389 Res := Sloc (N);
9390 N1 := N;
9391 while Present (N1) and then N1 /= Act_Unit loop
9392 if Sloc (N1) > Res then
9393 Res := Sloc (N1);
9394 end if;
9396 N1 := Parent (N1);
9397 end loop;
9399 return Res;
9400 end True_Sloc;
9402 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
9403 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9404 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
9405 Par : constant Entity_Id := Scope (Gen_Id);
9406 Gen_Unit : constant Node_Id :=
9407 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9409 Body_Unit : Node_Id;
9410 F_Node : Node_Id;
9411 Must_Delay : Boolean;
9412 Orig_Body : Node_Id := Gen_Body;
9414 -- Start of processing for Install_Body
9416 begin
9417 -- Handle first the case of an instance with incomplete actual types.
9418 -- The instance body cannot be placed after the declaration because
9419 -- full views have not been seen yet. Any use of the non-limited views
9420 -- in the instance body requires the presence of a regular with_clause
9421 -- in the enclosing unit, and will fail if this with_clause is missing.
9422 -- We place the instance body at the beginning of the enclosing body,
9423 -- which is the unit being compiled. The freeze node for the instance
9424 -- is then placed after the instance body.
9426 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
9427 and then Expander_Active
9428 and then Ekind (Scope (Act_Id)) = E_Package
9429 then
9430 declare
9431 Scop : constant Entity_Id := Scope (Act_Id);
9432 Body_Id : constant Node_Id :=
9433 Corresponding_Body (Unit_Declaration_Node (Scop));
9435 begin
9436 Ensure_Freeze_Node (Act_Id);
9437 F_Node := Freeze_Node (Act_Id);
9438 if Present (Body_Id) then
9439 Set_Is_Frozen (Act_Id, False);
9440 Prepend (Act_Body, Declarations (Parent (Body_Id)));
9441 if Is_List_Member (F_Node) then
9442 Remove (F_Node);
9443 end if;
9445 Insert_After (Act_Body, F_Node);
9446 end if;
9447 end;
9448 return;
9449 end if;
9451 -- If the body is a subunit, the freeze point is the corresponding stub
9452 -- in the current compilation, not the subunit itself.
9454 if Nkind (Parent (Gen_Body)) = N_Subunit then
9455 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9456 else
9457 Orig_Body := Gen_Body;
9458 end if;
9460 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9462 -- If the instantiation and the generic definition appear in the same
9463 -- package declaration, this is an early instantiation. If they appear
9464 -- in the same declarative part, it is an early instantiation only if
9465 -- the generic body appears textually later, and the generic body is
9466 -- also in the main unit.
9468 -- If instance is nested within a subprogram, and the generic body
9469 -- is not, the instance is delayed because the enclosing body is. If
9470 -- instance and body are within the same scope, or the same subprogram
9471 -- body, indicate explicitly that the instance is delayed.
9473 Must_Delay :=
9474 (Gen_Unit = Act_Unit
9475 and then (Nkind_In (Gen_Unit, N_Generic_Package_Declaration,
9476 N_Package_Declaration)
9477 or else (Gen_Unit = Body_Unit
9478 and then True_Sloc (N, Act_Unit) <
9479 Sloc (Orig_Body)))
9480 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9481 and then In_Same_Scope (Gen_Id, Act_Id));
9483 -- If this is an early instantiation, the freeze node is placed after
9484 -- the generic body. Otherwise, if the generic appears in an instance,
9485 -- we cannot freeze the current instance until the outer one is frozen.
9486 -- This is only relevant if the current instance is nested within some
9487 -- inner scope not itself within the outer instance. If this scope is
9488 -- a package body in the same declarative part as the outer instance,
9489 -- then that body needs to be frozen after the outer instance. Finally,
9490 -- if no delay is needed, we place the freeze node at the end of the
9491 -- current declarative part.
9493 if Expander_Active
9494 and then (No (Freeze_Node (Act_Id))
9495 or else not Is_List_Member (Freeze_Node (Act_Id)))
9496 then
9497 Ensure_Freeze_Node (Act_Id);
9498 F_Node := Freeze_Node (Act_Id);
9500 if Must_Delay then
9501 Insert_After (Orig_Body, F_Node);
9503 elsif Is_Generic_Instance (Par)
9504 and then Present (Freeze_Node (Par))
9505 and then Scope (Act_Id) /= Par
9506 then
9507 -- Freeze instance of inner generic after instance of enclosing
9508 -- generic.
9510 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), N) then
9512 -- Handle the following case:
9514 -- package Parent_Inst is new ...
9515 -- Parent_Inst []
9517 -- procedure P ... -- this body freezes Parent_Inst
9519 -- package Inst is new ...
9521 -- In this particular scenario, the freeze node for Inst must
9522 -- be inserted in the same manner as that of Parent_Inst,
9523 -- before the next source body or at the end of the declarative
9524 -- list (body not available). If body P did not exist and
9525 -- Parent_Inst was frozen after Inst, either by a body
9526 -- following Inst or at the end of the declarative region,
9527 -- the freeze node for Inst must be inserted after that of
9528 -- Parent_Inst. This relation is established by comparing
9529 -- the Slocs of Parent_Inst freeze node and Inst.
9530 -- We examine the parents of the enclosing lists to handle
9531 -- the case where the parent instance is in the visible part
9532 -- of a package declaration, and the inner instance is in
9533 -- the corresponding private part.
9535 if Parent (List_Containing (Get_Unit_Instantiation_Node (Par)))
9536 = Parent (List_Containing (N))
9537 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9538 then
9539 Insert_Freeze_Node_For_Instance (N, F_Node);
9540 else
9541 Insert_After (Freeze_Node (Par), F_Node);
9542 end if;
9544 -- Freeze package enclosing instance of inner generic after
9545 -- instance of enclosing generic.
9547 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
9548 and then In_Same_Declarative_Part
9549 (Parent (Freeze_Node (Par)), Parent (N))
9550 then
9551 declare
9552 Enclosing : Entity_Id;
9554 begin
9555 Enclosing := Corresponding_Spec (Parent (N));
9557 if No (Enclosing) then
9558 Enclosing := Defining_Entity (Parent (N));
9559 end if;
9561 Insert_Freeze_Node_For_Instance (N, F_Node);
9562 Ensure_Freeze_Node (Enclosing);
9564 if not Is_List_Member (Freeze_Node (Enclosing)) then
9566 -- The enclosing context is a subunit, insert the freeze
9567 -- node after the stub.
9569 if Nkind (Parent (Parent (N))) = N_Subunit then
9570 Insert_Freeze_Node_For_Instance
9571 (Corresponding_Stub (Parent (Parent (N))),
9572 Freeze_Node (Enclosing));
9574 -- The enclosing context is a package with a stub body
9575 -- which has already been replaced by the real body.
9576 -- Insert the freeze node after the actual body.
9578 elsif Ekind (Enclosing) = E_Package
9579 and then Present (Body_Entity (Enclosing))
9580 and then Was_Originally_Stub
9581 (Parent (Body_Entity (Enclosing)))
9582 then
9583 Insert_Freeze_Node_For_Instance
9584 (Parent (Body_Entity (Enclosing)),
9585 Freeze_Node (Enclosing));
9587 -- The parent instance has been frozen before the body of
9588 -- the enclosing package, insert the freeze node after
9589 -- the body.
9591 elsif List_Containing (Freeze_Node (Par)) =
9592 List_Containing (Parent (N))
9593 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9594 then
9595 Insert_Freeze_Node_For_Instance
9596 (Parent (N), Freeze_Node (Enclosing));
9598 else
9599 Insert_After
9600 (Freeze_Node (Par), Freeze_Node (Enclosing));
9601 end if;
9602 end if;
9603 end;
9605 else
9606 Insert_Freeze_Node_For_Instance (N, F_Node);
9607 end if;
9609 else
9610 Insert_Freeze_Node_For_Instance (N, F_Node);
9611 end if;
9612 end if;
9614 Set_Is_Frozen (Act_Id);
9615 Insert_Before (N, Act_Body);
9616 Mark_Rewrite_Insertion (Act_Body);
9617 end Install_Body;
9619 -----------------------------
9620 -- Install_Formal_Packages --
9621 -----------------------------
9623 procedure Install_Formal_Packages (Par : Entity_Id) is
9624 E : Entity_Id;
9625 Gen : Entity_Id;
9626 Gen_E : Entity_Id := Empty;
9628 begin
9629 E := First_Entity (Par);
9631 -- If we are installing an instance parent, locate the formal packages
9632 -- of its generic parent.
9634 if Is_Generic_Instance (Par) then
9635 Gen := Generic_Parent (Package_Specification (Par));
9636 Gen_E := First_Entity (Gen);
9637 end if;
9639 while Present (E) loop
9640 if Ekind (E) = E_Package
9641 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9642 then
9643 -- If this is the renaming for the parent instance, done
9645 if Renamed_Object (E) = Par then
9646 exit;
9648 -- The visibility of a formal of an enclosing generic is already
9649 -- correct.
9651 elsif Denotes_Formal_Package (E) then
9652 null;
9654 elsif Present (Associated_Formal_Package (E)) then
9655 Check_Generic_Actuals (Renamed_Object (E), True);
9656 Set_Is_Hidden (E, False);
9658 -- Find formal package in generic unit that corresponds to
9659 -- (instance of) formal package in instance.
9661 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9662 Next_Entity (Gen_E);
9663 end loop;
9665 if Present (Gen_E) then
9666 Map_Formal_Package_Entities (Gen_E, E);
9667 end if;
9668 end if;
9669 end if;
9671 Next_Entity (E);
9673 if Present (Gen_E) then
9674 Next_Entity (Gen_E);
9675 end if;
9676 end loop;
9677 end Install_Formal_Packages;
9679 --------------------
9680 -- Install_Parent --
9681 --------------------
9683 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9684 Ancestors : constant Elist_Id := New_Elmt_List;
9685 S : constant Entity_Id := Current_Scope;
9686 Inst_Par : Entity_Id;
9687 First_Par : Entity_Id;
9688 Inst_Node : Node_Id;
9689 Gen_Par : Entity_Id;
9690 First_Gen : Entity_Id;
9691 Elmt : Elmt_Id;
9693 procedure Install_Noninstance_Specs (Par : Entity_Id);
9694 -- Install the scopes of noninstance parent units ending with Par
9696 procedure Install_Spec (Par : Entity_Id);
9697 -- The child unit is within the declarative part of the parent, so the
9698 -- declarations within the parent are immediately visible.
9700 -------------------------------
9701 -- Install_Noninstance_Specs --
9702 -------------------------------
9704 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9705 begin
9706 if Present (Par)
9707 and then Par /= Standard_Standard
9708 and then not In_Open_Scopes (Par)
9709 then
9710 Install_Noninstance_Specs (Scope (Par));
9711 Install_Spec (Par);
9712 end if;
9713 end Install_Noninstance_Specs;
9715 ------------------
9716 -- Install_Spec --
9717 ------------------
9719 procedure Install_Spec (Par : Entity_Id) is
9720 Spec : constant Node_Id := Package_Specification (Par);
9722 begin
9723 -- If this parent of the child instance is a top-level unit,
9724 -- then record the unit and its visibility for later resetting in
9725 -- Remove_Parent. We exclude units that are generic instances, as we
9726 -- only want to record this information for the ultimate top-level
9727 -- noninstance parent (is that always correct???).
9729 if Scope (Par) = Standard_Standard
9730 and then not Is_Generic_Instance (Par)
9731 then
9732 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9733 Instance_Parent_Unit := Par;
9734 end if;
9736 -- Open the parent scope and make it and its declarations visible.
9737 -- If this point is not within a body, then only the visible
9738 -- declarations should be made visible, and installation of the
9739 -- private declarations is deferred until the appropriate point
9740 -- within analysis of the spec being instantiated (see the handling
9741 -- of parent visibility in Analyze_Package_Specification). This is
9742 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9743 -- private view problems that occur when compiling instantiations of
9744 -- a generic child of that package (Generic_Dispatching_Constructor).
9745 -- If the instance freezes a tagged type, inlinings of operations
9746 -- from Ada.Tags may need the full view of type Tag. If inlining took
9747 -- proper account of establishing visibility of inlined subprograms'
9748 -- parents then it should be possible to remove this
9749 -- special check. ???
9751 Push_Scope (Par);
9752 Set_Is_Immediately_Visible (Par);
9753 Install_Visible_Declarations (Par);
9754 Set_Use (Visible_Declarations (Spec));
9756 if In_Body or else Is_RTU (Par, Ada_Tags) then
9757 Install_Private_Declarations (Par);
9758 Set_Use (Private_Declarations (Spec));
9759 end if;
9760 end Install_Spec;
9762 -- Start of processing for Install_Parent
9764 begin
9765 -- We need to install the parent instance to compile the instantiation
9766 -- of the child, but the child instance must appear in the current
9767 -- scope. Given that we cannot place the parent above the current scope
9768 -- in the scope stack, we duplicate the current scope and unstack both
9769 -- after the instantiation is complete.
9771 -- If the parent is itself the instantiation of a child unit, we must
9772 -- also stack the instantiation of its parent, and so on. Each such
9773 -- ancestor is the prefix of the name in a prior instantiation.
9775 -- If this is a nested instance, the parent unit itself resolves to
9776 -- a renaming of the parent instance, whose declaration we need.
9778 -- Finally, the parent may be a generic (not an instance) when the
9779 -- child unit appears as a formal package.
9781 Inst_Par := P;
9783 if Present (Renamed_Entity (Inst_Par)) then
9784 Inst_Par := Renamed_Entity (Inst_Par);
9785 end if;
9787 First_Par := Inst_Par;
9789 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9791 First_Gen := Gen_Par;
9793 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9795 -- Load grandparent instance as well
9797 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
9799 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9800 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9802 if Present (Renamed_Entity (Inst_Par)) then
9803 Inst_Par := Renamed_Entity (Inst_Par);
9804 end if;
9806 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9808 if Present (Gen_Par) then
9809 Prepend_Elmt (Inst_Par, Ancestors);
9811 else
9812 -- Parent is not the name of an instantiation
9814 Install_Noninstance_Specs (Inst_Par);
9815 exit;
9816 end if;
9818 else
9819 -- Previous error
9821 exit;
9822 end if;
9823 end loop;
9825 if Present (First_Gen) then
9826 Append_Elmt (First_Par, Ancestors);
9827 else
9828 Install_Noninstance_Specs (First_Par);
9829 end if;
9831 if not Is_Empty_Elmt_List (Ancestors) then
9832 Elmt := First_Elmt (Ancestors);
9833 while Present (Elmt) loop
9834 Install_Spec (Node (Elmt));
9835 Install_Formal_Packages (Node (Elmt));
9836 Next_Elmt (Elmt);
9837 end loop;
9838 end if;
9840 if not In_Body then
9841 Push_Scope (S);
9842 end if;
9843 end Install_Parent;
9845 -------------------------------
9846 -- Install_Hidden_Primitives --
9847 -------------------------------
9849 procedure Install_Hidden_Primitives
9850 (Prims_List : in out Elist_Id;
9851 Gen_T : Entity_Id;
9852 Act_T : Entity_Id)
9854 Elmt : Elmt_Id;
9855 List : Elist_Id := No_Elist;
9856 Prim_G_Elmt : Elmt_Id;
9857 Prim_A_Elmt : Elmt_Id;
9858 Prim_G : Node_Id;
9859 Prim_A : Node_Id;
9861 begin
9862 -- No action needed in case of serious errors because we cannot trust
9863 -- in the order of primitives
9865 if Serious_Errors_Detected > 0 then
9866 return;
9868 -- No action possible if we don't have available the list of primitive
9869 -- operations
9871 elsif No (Gen_T)
9872 or else not Is_Record_Type (Gen_T)
9873 or else not Is_Tagged_Type (Gen_T)
9874 or else not Is_Record_Type (Act_T)
9875 or else not Is_Tagged_Type (Act_T)
9876 then
9877 return;
9879 -- There is no need to handle interface types since their primitives
9880 -- cannot be hidden
9882 elsif Is_Interface (Gen_T) then
9883 return;
9884 end if;
9886 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9888 if not Is_Class_Wide_Type (Act_T) then
9889 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9890 else
9891 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9892 end if;
9894 loop
9895 -- Skip predefined primitives in the generic formal
9897 while Present (Prim_G_Elmt)
9898 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9899 loop
9900 Next_Elmt (Prim_G_Elmt);
9901 end loop;
9903 -- Skip predefined primitives in the generic actual
9905 while Present (Prim_A_Elmt)
9906 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9907 loop
9908 Next_Elmt (Prim_A_Elmt);
9909 end loop;
9911 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9913 Prim_G := Node (Prim_G_Elmt);
9914 Prim_A := Node (Prim_A_Elmt);
9916 -- There is no need to handle interface primitives because their
9917 -- primitives are not hidden
9919 exit when Present (Interface_Alias (Prim_G));
9921 -- Here we install one hidden primitive
9923 if Chars (Prim_G) /= Chars (Prim_A)
9924 and then Has_Suffix (Prim_A, 'P')
9925 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9926 then
9927 Set_Chars (Prim_A, Chars (Prim_G));
9928 Append_New_Elmt (Prim_A, To => List);
9929 end if;
9931 Next_Elmt (Prim_A_Elmt);
9932 Next_Elmt (Prim_G_Elmt);
9933 end loop;
9935 -- Append the elements to the list of temporarily visible primitives
9936 -- avoiding duplicates.
9938 if Present (List) then
9939 if No (Prims_List) then
9940 Prims_List := New_Elmt_List;
9941 end if;
9943 Elmt := First_Elmt (List);
9944 while Present (Elmt) loop
9945 Append_Unique_Elmt (Node (Elmt), Prims_List);
9946 Next_Elmt (Elmt);
9947 end loop;
9948 end if;
9949 end Install_Hidden_Primitives;
9951 -------------------------------
9952 -- Restore_Hidden_Primitives --
9953 -------------------------------
9955 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9956 Prim_Elmt : Elmt_Id;
9957 Prim : Node_Id;
9959 begin
9960 if Prims_List /= No_Elist then
9961 Prim_Elmt := First_Elmt (Prims_List);
9962 while Present (Prim_Elmt) loop
9963 Prim := Node (Prim_Elmt);
9964 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9965 Next_Elmt (Prim_Elmt);
9966 end loop;
9968 Prims_List := No_Elist;
9969 end if;
9970 end Restore_Hidden_Primitives;
9972 --------------------------------
9973 -- Instantiate_Formal_Package --
9974 --------------------------------
9976 function Instantiate_Formal_Package
9977 (Formal : Node_Id;
9978 Actual : Node_Id;
9979 Analyzed_Formal : Node_Id) return List_Id
9981 Loc : constant Source_Ptr := Sloc (Actual);
9982 Hidden_Formals : constant Elist_Id := New_Elmt_List;
9983 Actual_Pack : Entity_Id;
9984 Formal_Pack : Entity_Id;
9985 Gen_Parent : Entity_Id;
9986 Decls : List_Id;
9987 Nod : Node_Id;
9988 Parent_Spec : Node_Id;
9990 procedure Find_Matching_Actual
9991 (F : Node_Id;
9992 Act : in out Entity_Id);
9993 -- We need to associate each formal entity in the formal package with
9994 -- the corresponding entity in the actual package. The actual package
9995 -- has been analyzed and possibly expanded, and as a result there is
9996 -- no one-to-one correspondence between the two lists (for example,
9997 -- the actual may include subtypes, itypes, and inherited primitive
9998 -- operations, interspersed among the renaming declarations for the
9999 -- actuals). We retrieve the corresponding actual by name because each
10000 -- actual has the same name as the formal, and they do appear in the
10001 -- same order.
10003 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
10004 -- Retrieve entity of defining entity of generic formal parameter.
10005 -- Only the declarations of formals need to be considered when
10006 -- linking them to actuals, but the declarative list may include
10007 -- internal entities generated during analysis, and those are ignored.
10009 procedure Match_Formal_Entity
10010 (Formal_Node : Node_Id;
10011 Formal_Ent : Entity_Id;
10012 Actual_Ent : Entity_Id);
10013 -- Associates the formal entity with the actual. In the case where
10014 -- Formal_Ent is a formal package, this procedure iterates through all
10015 -- of its formals and enters associations between the actuals occurring
10016 -- in the formal package's corresponding actual package (given by
10017 -- Actual_Ent) and the formal package's formal parameters. This
10018 -- procedure recurses if any of the parameters is itself a package.
10020 function Is_Instance_Of
10021 (Act_Spec : Entity_Id;
10022 Gen_Anc : Entity_Id) return Boolean;
10023 -- The actual can be an instantiation of a generic within another
10024 -- instance, in which case there is no direct link from it to the
10025 -- original generic ancestor. In that case, we recognize that the
10026 -- ultimate ancestor is the same by examining names and scopes.
10028 procedure Process_Nested_Formal (Formal : Entity_Id);
10029 -- If the current formal is declared with a box, its own formals are
10030 -- visible in the instance, as they were in the generic, and their
10031 -- Hidden flag must be reset. If some of these formals are themselves
10032 -- packages declared with a box, the processing must be recursive.
10034 --------------------------
10035 -- Find_Matching_Actual --
10036 --------------------------
10038 procedure Find_Matching_Actual
10039 (F : Node_Id;
10040 Act : in out Entity_Id)
10042 Formal_Ent : Entity_Id;
10044 begin
10045 case Nkind (Original_Node (F)) is
10046 when N_Formal_Object_Declaration
10047 | N_Formal_Type_Declaration
10049 Formal_Ent := Defining_Identifier (F);
10051 while Chars (Act) /= Chars (Formal_Ent) loop
10052 Next_Entity (Act);
10053 end loop;
10055 when N_Formal_Package_Declaration
10056 | N_Formal_Subprogram_Declaration
10057 | N_Generic_Package_Declaration
10058 | N_Package_Declaration
10060 Formal_Ent := Defining_Entity (F);
10062 while Chars (Act) /= Chars (Formal_Ent) loop
10063 Next_Entity (Act);
10064 end loop;
10066 when others =>
10067 raise Program_Error;
10068 end case;
10069 end Find_Matching_Actual;
10071 -------------------------
10072 -- Match_Formal_Entity --
10073 -------------------------
10075 procedure Match_Formal_Entity
10076 (Formal_Node : Node_Id;
10077 Formal_Ent : Entity_Id;
10078 Actual_Ent : Entity_Id)
10080 Act_Pkg : Entity_Id;
10082 begin
10083 Set_Instance_Of (Formal_Ent, Actual_Ent);
10085 if Ekind (Actual_Ent) = E_Package then
10087 -- Record associations for each parameter
10089 Act_Pkg := Actual_Ent;
10091 declare
10092 A_Ent : Entity_Id := First_Entity (Act_Pkg);
10093 F_Ent : Entity_Id;
10094 F_Node : Node_Id;
10096 Gen_Decl : Node_Id;
10097 Formals : List_Id;
10098 Actual : Entity_Id;
10100 begin
10101 -- Retrieve the actual given in the formal package declaration
10103 Actual := Entity (Name (Original_Node (Formal_Node)));
10105 -- The actual in the formal package declaration may be a
10106 -- renamed generic package, in which case we want to retrieve
10107 -- the original generic in order to traverse its formal part.
10109 if Present (Renamed_Entity (Actual)) then
10110 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
10111 else
10112 Gen_Decl := Unit_Declaration_Node (Actual);
10113 end if;
10115 Formals := Generic_Formal_Declarations (Gen_Decl);
10117 if Present (Formals) then
10118 F_Node := First_Non_Pragma (Formals);
10119 else
10120 F_Node := Empty;
10121 end if;
10123 while Present (A_Ent)
10124 and then Present (F_Node)
10125 and then A_Ent /= First_Private_Entity (Act_Pkg)
10126 loop
10127 F_Ent := Get_Formal_Entity (F_Node);
10129 if Present (F_Ent) then
10131 -- This is a formal of the original package. Record
10132 -- association and recurse.
10134 Find_Matching_Actual (F_Node, A_Ent);
10135 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
10136 Next_Entity (A_Ent);
10137 end if;
10139 Next_Non_Pragma (F_Node);
10140 end loop;
10141 end;
10142 end if;
10143 end Match_Formal_Entity;
10145 -----------------------
10146 -- Get_Formal_Entity --
10147 -----------------------
10149 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
10150 Kind : constant Node_Kind := Nkind (Original_Node (N));
10151 begin
10152 case Kind is
10153 when N_Formal_Object_Declaration =>
10154 return Defining_Identifier (N);
10156 when N_Formal_Type_Declaration =>
10157 return Defining_Identifier (N);
10159 when N_Formal_Subprogram_Declaration =>
10160 return Defining_Unit_Name (Specification (N));
10162 when N_Formal_Package_Declaration =>
10163 return Defining_Identifier (Original_Node (N));
10165 when N_Generic_Package_Declaration =>
10166 return Defining_Identifier (Original_Node (N));
10168 -- All other declarations are introduced by semantic analysis and
10169 -- have no match in the actual.
10171 when others =>
10172 return Empty;
10173 end case;
10174 end Get_Formal_Entity;
10176 --------------------
10177 -- Is_Instance_Of --
10178 --------------------
10180 function Is_Instance_Of
10181 (Act_Spec : Entity_Id;
10182 Gen_Anc : Entity_Id) return Boolean
10184 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
10186 begin
10187 if No (Gen_Par) then
10188 return False;
10190 -- Simplest case: the generic parent of the actual is the formal
10192 elsif Gen_Par = Gen_Anc then
10193 return True;
10195 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
10196 return False;
10198 -- The actual may be obtained through several instantiations. Its
10199 -- scope must itself be an instance of a generic declared in the
10200 -- same scope as the formal. Any other case is detected above.
10202 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
10203 return False;
10205 else
10206 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
10207 end if;
10208 end Is_Instance_Of;
10210 ---------------------------
10211 -- Process_Nested_Formal --
10212 ---------------------------
10214 procedure Process_Nested_Formal (Formal : Entity_Id) is
10215 Ent : Entity_Id;
10217 begin
10218 if Present (Associated_Formal_Package (Formal))
10219 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
10220 then
10221 Ent := First_Entity (Formal);
10222 while Present (Ent) loop
10223 Set_Is_Hidden (Ent, False);
10224 Set_Is_Visible_Formal (Ent);
10225 Set_Is_Potentially_Use_Visible
10226 (Ent, Is_Potentially_Use_Visible (Formal));
10228 if Ekind (Ent) = E_Package then
10229 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
10230 Process_Nested_Formal (Ent);
10231 end if;
10233 Next_Entity (Ent);
10234 end loop;
10235 end if;
10236 end Process_Nested_Formal;
10238 -- Start of processing for Instantiate_Formal_Package
10240 begin
10241 Analyze (Actual);
10243 if not Is_Entity_Name (Actual)
10244 or else Ekind (Entity (Actual)) /= E_Package
10245 then
10246 Error_Msg_N
10247 ("expect package instance to instantiate formal", Actual);
10248 Abandon_Instantiation (Actual);
10249 raise Program_Error;
10251 else
10252 Actual_Pack := Entity (Actual);
10253 Set_Is_Instantiated (Actual_Pack);
10255 -- The actual may be a renamed package, or an outer generic formal
10256 -- package whose instantiation is converted into a renaming.
10258 if Present (Renamed_Object (Actual_Pack)) then
10259 Actual_Pack := Renamed_Object (Actual_Pack);
10260 end if;
10262 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
10263 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
10264 Formal_Pack := Defining_Identifier (Analyzed_Formal);
10265 else
10266 Gen_Parent :=
10267 Generic_Parent (Specification (Analyzed_Formal));
10268 Formal_Pack :=
10269 Defining_Unit_Name (Specification (Analyzed_Formal));
10270 end if;
10272 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
10273 Parent_Spec := Package_Specification (Actual_Pack);
10274 else
10275 Parent_Spec := Parent (Actual_Pack);
10276 end if;
10278 if Gen_Parent = Any_Id then
10279 Error_Msg_N
10280 ("previous error in declaration of formal package", Actual);
10281 Abandon_Instantiation (Actual);
10283 elsif
10284 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
10285 then
10286 null;
10288 else
10289 Error_Msg_NE
10290 ("actual parameter must be instance of&", Actual, Gen_Parent);
10291 Abandon_Instantiation (Actual);
10292 end if;
10294 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
10295 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
10297 Nod :=
10298 Make_Package_Renaming_Declaration (Loc,
10299 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
10300 Name => New_Occurrence_Of (Actual_Pack, Loc));
10302 Set_Associated_Formal_Package
10303 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10304 Decls := New_List (Nod);
10306 -- If the formal F has a box, then the generic declarations are
10307 -- visible in the generic G. In an instance of G, the corresponding
10308 -- entities in the actual for F (which are the actuals for the
10309 -- instantiation of the generic that F denotes) must also be made
10310 -- visible for analysis of the current instance. On exit from the
10311 -- current instance, those entities are made private again. If the
10312 -- actual is currently in use, these entities are also use-visible.
10314 -- The loop through the actual entities also steps through the formal
10315 -- entities and enters associations from formals to actuals into the
10316 -- renaming map. This is necessary to properly handle checking of
10317 -- actual parameter associations for later formals that depend on
10318 -- actuals declared in the formal package.
10320 -- In Ada 2005, partial parameterization requires that we make
10321 -- visible the actuals corresponding to formals that were defaulted
10322 -- in the formal package. There formals are identified because they
10323 -- remain formal generics within the formal package, rather than
10324 -- being renamings of the actuals supplied.
10326 declare
10327 Gen_Decl : constant Node_Id :=
10328 Unit_Declaration_Node (Gen_Parent);
10329 Formals : constant List_Id :=
10330 Generic_Formal_Declarations (Gen_Decl);
10332 Actual_Ent : Entity_Id;
10333 Actual_Of_Formal : Node_Id;
10334 Formal_Node : Node_Id;
10335 Formal_Ent : Entity_Id;
10337 begin
10338 if Present (Formals) then
10339 Formal_Node := First_Non_Pragma (Formals);
10340 else
10341 Formal_Node := Empty;
10342 end if;
10344 Actual_Ent := First_Entity (Actual_Pack);
10345 Actual_Of_Formal :=
10346 First (Visible_Declarations (Specification (Analyzed_Formal)));
10347 while Present (Actual_Ent)
10348 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10349 loop
10350 if Present (Formal_Node) then
10351 Formal_Ent := Get_Formal_Entity (Formal_Node);
10353 if Present (Formal_Ent) then
10354 Find_Matching_Actual (Formal_Node, Actual_Ent);
10355 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10357 -- We iterate at the same time over the actuals of the
10358 -- local package created for the formal, to determine
10359 -- which one of the formals of the original generic were
10360 -- defaulted in the formal. The corresponding actual
10361 -- entities are visible in the enclosing instance.
10363 if Box_Present (Formal)
10364 or else
10365 (Present (Actual_Of_Formal)
10366 and then
10367 Is_Generic_Formal
10368 (Get_Formal_Entity (Actual_Of_Formal)))
10369 then
10370 Set_Is_Hidden (Actual_Ent, False);
10371 Set_Is_Visible_Formal (Actual_Ent);
10372 Set_Is_Potentially_Use_Visible
10373 (Actual_Ent, In_Use (Actual_Pack));
10375 if Ekind (Actual_Ent) = E_Package then
10376 Process_Nested_Formal (Actual_Ent);
10377 end if;
10379 else
10380 if not Is_Hidden (Actual_Ent) then
10381 Append_Elmt (Actual_Ent, Hidden_Formals);
10382 end if;
10384 Set_Is_Hidden (Actual_Ent);
10385 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10386 end if;
10387 end if;
10389 Next_Non_Pragma (Formal_Node);
10390 Next (Actual_Of_Formal);
10392 else
10393 -- No further formals to match, but the generic part may
10394 -- contain inherited operation that are not hidden in the
10395 -- enclosing instance.
10397 Next_Entity (Actual_Ent);
10398 end if;
10399 end loop;
10401 -- Inherited subprograms generated by formal derived types are
10402 -- also visible if the types are.
10404 Actual_Ent := First_Entity (Actual_Pack);
10405 while Present (Actual_Ent)
10406 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10407 loop
10408 if Is_Overloadable (Actual_Ent)
10409 and then
10410 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10411 and then
10412 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10413 then
10414 Set_Is_Hidden (Actual_Ent, False);
10415 Set_Is_Potentially_Use_Visible
10416 (Actual_Ent, In_Use (Actual_Pack));
10417 end if;
10419 Next_Entity (Actual_Ent);
10420 end loop;
10421 end;
10423 -- If the formal is not declared with a box, reanalyze it as an
10424 -- abbreviated instantiation, to verify the matching rules of 12.7.
10425 -- The actual checks are performed after the generic associations
10426 -- have been analyzed, to guarantee the same visibility for this
10427 -- instantiation and for the actuals.
10429 -- In Ada 2005, the generic associations for the formal can include
10430 -- defaulted parameters. These are ignored during check. This
10431 -- internal instantiation is removed from the tree after conformance
10432 -- checking, because it contains formal declarations for those
10433 -- defaulted parameters, and those should not reach the back-end.
10435 if not Box_Present (Formal) then
10436 declare
10437 I_Pack : constant Entity_Id :=
10438 Make_Temporary (Sloc (Actual), 'P');
10440 begin
10441 Set_Is_Internal (I_Pack);
10442 Set_Ekind (I_Pack, E_Package);
10443 Set_Hidden_In_Formal_Instance (I_Pack, Hidden_Formals);
10445 Append_To (Decls,
10446 Make_Package_Instantiation (Sloc (Actual),
10447 Defining_Unit_Name => I_Pack,
10448 Name =>
10449 New_Occurrence_Of
10450 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10451 Generic_Associations => Generic_Associations (Formal)));
10452 end;
10453 end if;
10455 return Decls;
10456 end if;
10457 end Instantiate_Formal_Package;
10459 -----------------------------------
10460 -- Instantiate_Formal_Subprogram --
10461 -----------------------------------
10463 function Instantiate_Formal_Subprogram
10464 (Formal : Node_Id;
10465 Actual : Node_Id;
10466 Analyzed_Formal : Node_Id) return Node_Id
10468 Analyzed_S : constant Entity_Id :=
10469 Defining_Unit_Name (Specification (Analyzed_Formal));
10470 Formal_Sub : constant Entity_Id :=
10471 Defining_Unit_Name (Specification (Formal));
10473 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10474 -- If the generic is a child unit, the parent has been installed on the
10475 -- scope stack, but a default subprogram cannot resolve to something
10476 -- on the parent because that parent is not really part of the visible
10477 -- context (it is there to resolve explicit local entities). If the
10478 -- default has resolved in this way, we remove the entity from immediate
10479 -- visibility and analyze the node again to emit an error message or
10480 -- find another visible candidate.
10482 procedure Valid_Actual_Subprogram (Act : Node_Id);
10483 -- Perform legality check and raise exception on failure
10485 -----------------------
10486 -- From_Parent_Scope --
10487 -----------------------
10489 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10490 Gen_Scope : Node_Id;
10492 begin
10493 Gen_Scope := Scope (Analyzed_S);
10494 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10495 if Scope (Subp) = Scope (Gen_Scope) then
10496 return True;
10497 end if;
10499 Gen_Scope := Scope (Gen_Scope);
10500 end loop;
10502 return False;
10503 end From_Parent_Scope;
10505 -----------------------------
10506 -- Valid_Actual_Subprogram --
10507 -----------------------------
10509 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10510 Act_E : Entity_Id;
10512 begin
10513 if Is_Entity_Name (Act) then
10514 Act_E := Entity (Act);
10516 elsif Nkind (Act) = N_Selected_Component
10517 and then Is_Entity_Name (Selector_Name (Act))
10518 then
10519 Act_E := Entity (Selector_Name (Act));
10521 else
10522 Act_E := Empty;
10523 end if;
10525 if (Present (Act_E) and then Is_Overloadable (Act_E))
10526 or else Nkind_In (Act, N_Attribute_Reference,
10527 N_Indexed_Component,
10528 N_Character_Literal,
10529 N_Explicit_Dereference)
10530 then
10531 return;
10532 end if;
10534 Error_Msg_NE
10535 ("expect subprogram or entry name in instantiation of &",
10536 Instantiation_Node, Formal_Sub);
10537 Abandon_Instantiation (Instantiation_Node);
10538 end Valid_Actual_Subprogram;
10540 -- Local variables
10542 Decl_Node : Node_Id;
10543 Loc : Source_Ptr;
10544 Nam : Node_Id;
10545 New_Spec : Node_Id;
10546 New_Subp : Entity_Id;
10548 -- Start of processing for Instantiate_Formal_Subprogram
10550 begin
10551 New_Spec := New_Copy_Tree (Specification (Formal));
10553 -- The tree copy has created the proper instantiation sloc for the
10554 -- new specification. Use this location for all other constructed
10555 -- declarations.
10557 Loc := Sloc (Defining_Unit_Name (New_Spec));
10559 -- Create new entity for the actual (New_Copy_Tree does not), and
10560 -- indicate that it is an actual.
10562 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10563 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10564 Set_Is_Generic_Actual_Subprogram (New_Subp);
10565 Set_Defining_Unit_Name (New_Spec, New_Subp);
10567 -- Create new entities for the each of the formals in the specification
10568 -- of the renaming declaration built for the actual.
10570 if Present (Parameter_Specifications (New_Spec)) then
10571 declare
10572 F : Node_Id;
10573 F_Id : Entity_Id;
10575 begin
10576 F := First (Parameter_Specifications (New_Spec));
10577 while Present (F) loop
10578 F_Id := Defining_Identifier (F);
10580 Set_Defining_Identifier (F,
10581 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10582 Next (F);
10583 end loop;
10584 end;
10585 end if;
10587 -- Find entity of actual. If the actual is an attribute reference, it
10588 -- cannot be resolved here (its formal is missing) but is handled
10589 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10590 -- fully resolved subsequently, when the renaming declaration for the
10591 -- formal is analyzed. If it is an explicit dereference, resolve the
10592 -- prefix but not the actual itself, to prevent interpretation as call.
10594 if Present (Actual) then
10595 Loc := Sloc (Actual);
10596 Set_Sloc (New_Spec, Loc);
10598 if Nkind (Actual) = N_Operator_Symbol then
10599 Find_Direct_Name (Actual);
10601 elsif Nkind (Actual) = N_Explicit_Dereference then
10602 Analyze (Prefix (Actual));
10604 elsif Nkind (Actual) /= N_Attribute_Reference then
10605 Analyze (Actual);
10606 end if;
10608 Valid_Actual_Subprogram (Actual);
10609 Nam := Actual;
10611 elsif Present (Default_Name (Formal)) then
10612 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
10613 N_Selected_Component,
10614 N_Indexed_Component,
10615 N_Character_Literal)
10616 and then Present (Entity (Default_Name (Formal)))
10617 then
10618 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10619 else
10620 Nam := New_Copy (Default_Name (Formal));
10621 Set_Sloc (Nam, Loc);
10622 end if;
10624 elsif Box_Present (Formal) then
10626 -- Actual is resolved at the point of instantiation. Create an
10627 -- identifier or operator with the same name as the formal.
10629 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10630 Nam :=
10631 Make_Operator_Symbol (Loc,
10632 Chars => Chars (Formal_Sub),
10633 Strval => No_String);
10634 else
10635 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10636 end if;
10638 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10639 and then Null_Present (Specification (Formal))
10640 then
10641 -- Generate null body for procedure, for use in the instance
10643 Decl_Node :=
10644 Make_Subprogram_Body (Loc,
10645 Specification => New_Spec,
10646 Declarations => New_List,
10647 Handled_Statement_Sequence =>
10648 Make_Handled_Sequence_Of_Statements (Loc,
10649 Statements => New_List (Make_Null_Statement (Loc))));
10651 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10652 return Decl_Node;
10654 else
10655 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10656 Error_Msg_NE
10657 ("missing actual&", Instantiation_Node, Formal_Sub);
10658 Error_Msg_NE
10659 ("\in instantiation of & declared#",
10660 Instantiation_Node, Scope (Analyzed_S));
10661 Abandon_Instantiation (Instantiation_Node);
10662 end if;
10664 Decl_Node :=
10665 Make_Subprogram_Renaming_Declaration (Loc,
10666 Specification => New_Spec,
10667 Name => Nam);
10669 -- If we do not have an actual and the formal specified <> then set to
10670 -- get proper default.
10672 if No (Actual) and then Box_Present (Formal) then
10673 Set_From_Default (Decl_Node);
10674 end if;
10676 -- Gather possible interpretations for the actual before analyzing the
10677 -- instance. If overloaded, it will be resolved when analyzing the
10678 -- renaming declaration.
10680 if Box_Present (Formal) and then No (Actual) then
10681 Analyze (Nam);
10683 if Is_Child_Unit (Scope (Analyzed_S))
10684 and then Present (Entity (Nam))
10685 then
10686 if not Is_Overloaded (Nam) then
10687 if From_Parent_Scope (Entity (Nam)) then
10688 Set_Is_Immediately_Visible (Entity (Nam), False);
10689 Set_Entity (Nam, Empty);
10690 Set_Etype (Nam, Empty);
10692 Analyze (Nam);
10693 Set_Is_Immediately_Visible (Entity (Nam));
10694 end if;
10696 else
10697 declare
10698 I : Interp_Index;
10699 It : Interp;
10701 begin
10702 Get_First_Interp (Nam, I, It);
10703 while Present (It.Nam) loop
10704 if From_Parent_Scope (It.Nam) then
10705 Remove_Interp (I);
10706 end if;
10708 Get_Next_Interp (I, It);
10709 end loop;
10710 end;
10711 end if;
10712 end if;
10713 end if;
10715 -- The generic instantiation freezes the actual. This can only be done
10716 -- once the actual is resolved, in the analysis of the renaming
10717 -- declaration. To make the formal subprogram entity available, we set
10718 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10719 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10720 -- of formal abstract subprograms.
10722 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10724 -- We cannot analyze the renaming declaration, and thus find the actual,
10725 -- until all the actuals are assembled in the instance. For subsequent
10726 -- checks of other actuals, indicate the node that will hold the
10727 -- instance of this formal.
10729 Set_Instance_Of (Analyzed_S, Nam);
10731 if Nkind (Actual) = N_Selected_Component
10732 and then Is_Task_Type (Etype (Prefix (Actual)))
10733 and then not Is_Frozen (Etype (Prefix (Actual)))
10734 then
10735 -- The renaming declaration will create a body, which must appear
10736 -- outside of the instantiation, We move the renaming declaration
10737 -- out of the instance, and create an additional renaming inside,
10738 -- to prevent freezing anomalies.
10740 declare
10741 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10743 begin
10744 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10745 Insert_Before (Instantiation_Node, Decl_Node);
10746 Analyze (Decl_Node);
10748 -- Now create renaming within the instance
10750 Decl_Node :=
10751 Make_Subprogram_Renaming_Declaration (Loc,
10752 Specification => New_Copy_Tree (New_Spec),
10753 Name => New_Occurrence_Of (Anon_Id, Loc));
10755 Set_Defining_Unit_Name (Specification (Decl_Node),
10756 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10757 end;
10758 end if;
10760 return Decl_Node;
10761 end Instantiate_Formal_Subprogram;
10763 ------------------------
10764 -- Instantiate_Object --
10765 ------------------------
10767 function Instantiate_Object
10768 (Formal : Node_Id;
10769 Actual : Node_Id;
10770 Analyzed_Formal : Node_Id) return List_Id
10772 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10773 A_Gen_Obj : constant Entity_Id :=
10774 Defining_Identifier (Analyzed_Formal);
10775 Acc_Def : Node_Id := Empty;
10776 Act_Assoc : constant Node_Id := Parent (Actual);
10777 Actual_Decl : Node_Id := Empty;
10778 Decl_Node : Node_Id;
10779 Def : Node_Id;
10780 Ftyp : Entity_Id;
10781 List : constant List_Id := New_List;
10782 Loc : constant Source_Ptr := Sloc (Actual);
10783 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10784 Subt_Decl : Node_Id := Empty;
10785 Subt_Mark : Node_Id := Empty;
10787 function Copy_Access_Def return Node_Id;
10788 -- If formal is an anonymous access, copy access definition of formal
10789 -- for generated object declaration.
10791 ---------------------
10792 -- Copy_Access_Def --
10793 ---------------------
10795 function Copy_Access_Def return Node_Id is
10796 begin
10797 Def := New_Copy_Tree (Acc_Def);
10799 -- In addition, if formal is an access to subprogram we need to
10800 -- generate new formals for the signature of the default, so that
10801 -- the tree is properly formatted for ASIS use.
10803 if Present (Access_To_Subprogram_Definition (Acc_Def)) then
10804 declare
10805 Par_Spec : Node_Id;
10806 begin
10807 Par_Spec :=
10808 First (Parameter_Specifications
10809 (Access_To_Subprogram_Definition (Def)));
10810 while Present (Par_Spec) loop
10811 Set_Defining_Identifier (Par_Spec,
10812 Make_Defining_Identifier (Sloc (Acc_Def),
10813 Chars => Chars (Defining_Identifier (Par_Spec))));
10814 Next (Par_Spec);
10815 end loop;
10816 end;
10817 end if;
10819 return Def;
10820 end Copy_Access_Def;
10822 -- Start of processing for Instantiate_Object
10824 begin
10825 -- Formal may be an anonymous access
10827 if Present (Subtype_Mark (Formal)) then
10828 Subt_Mark := Subtype_Mark (Formal);
10829 else
10830 Check_Access_Definition (Formal);
10831 Acc_Def := Access_Definition (Formal);
10832 end if;
10834 -- Sloc for error message on missing actual
10836 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10838 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10839 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10840 end if;
10842 Set_Parent (List, Parent (Actual));
10844 -- OUT present
10846 if Out_Present (Formal) then
10848 -- An IN OUT generic actual must be a name. The instantiation is a
10849 -- renaming declaration. The actual is the name being renamed. We
10850 -- use the actual directly, rather than a copy, because it is not
10851 -- used further in the list of actuals, and because a copy or a use
10852 -- of relocate_node is incorrect if the instance is nested within a
10853 -- generic. In order to simplify ASIS searches, the Generic_Parent
10854 -- field links the declaration to the generic association.
10856 if No (Actual) then
10857 Error_Msg_NE
10858 ("missing actual &",
10859 Instantiation_Node, Gen_Obj);
10860 Error_Msg_NE
10861 ("\in instantiation of & declared#",
10862 Instantiation_Node, Scope (A_Gen_Obj));
10863 Abandon_Instantiation (Instantiation_Node);
10864 end if;
10866 if Present (Subt_Mark) then
10867 Decl_Node :=
10868 Make_Object_Renaming_Declaration (Loc,
10869 Defining_Identifier => New_Copy (Gen_Obj),
10870 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10871 Name => Actual);
10873 else pragma Assert (Present (Acc_Def));
10874 Decl_Node :=
10875 Make_Object_Renaming_Declaration (Loc,
10876 Defining_Identifier => New_Copy (Gen_Obj),
10877 Access_Definition => New_Copy_Tree (Acc_Def),
10878 Name => Actual);
10879 end if;
10881 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10883 -- The analysis of the actual may produce Insert_Action nodes, so
10884 -- the declaration must have a context in which to attach them.
10886 Append (Decl_Node, List);
10887 Analyze (Actual);
10889 -- Return if the analysis of the actual reported some error
10891 if Etype (Actual) = Any_Type then
10892 return List;
10893 end if;
10895 -- This check is performed here because Analyze_Object_Renaming will
10896 -- not check it when Comes_From_Source is False. Note though that the
10897 -- check for the actual being the name of an object will be performed
10898 -- in Analyze_Object_Renaming.
10900 if Is_Object_Reference (Actual)
10901 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10902 then
10903 Error_Msg_N
10904 ("illegal discriminant-dependent component for in out parameter",
10905 Actual);
10906 end if;
10908 -- The actual has to be resolved in order to check that it is a
10909 -- variable (due to cases such as F (1), where F returns access to
10910 -- an array, and for overloaded prefixes).
10912 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10914 -- If the type of the formal is not itself a formal, and the current
10915 -- unit is a child unit, the formal type must be declared in a
10916 -- parent, and must be retrieved by visibility.
10918 if Ftyp = Orig_Ftyp
10919 and then Is_Generic_Unit (Scope (Ftyp))
10920 and then Is_Child_Unit (Scope (A_Gen_Obj))
10921 then
10922 declare
10923 Temp : constant Node_Id :=
10924 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10925 begin
10926 Set_Entity (Temp, Empty);
10927 Find_Type (Temp);
10928 Ftyp := Entity (Temp);
10929 end;
10930 end if;
10932 if Is_Private_Type (Ftyp)
10933 and then not Is_Private_Type (Etype (Actual))
10934 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10935 or else Base_Type (Etype (Actual)) = Ftyp)
10936 then
10937 -- If the actual has the type of the full view of the formal, or
10938 -- else a non-private subtype of the formal, then the visibility
10939 -- of the formal type has changed. Add to the actuals a subtype
10940 -- declaration that will force the exchange of views in the body
10941 -- of the instance as well.
10943 Subt_Decl :=
10944 Make_Subtype_Declaration (Loc,
10945 Defining_Identifier => Make_Temporary (Loc, 'P'),
10946 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10948 Prepend (Subt_Decl, List);
10950 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10951 Exchange_Declarations (Ftyp);
10952 end if;
10954 Resolve (Actual, Ftyp);
10956 if not Denotes_Variable (Actual) then
10957 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
10959 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10961 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10962 -- the type of the actual shall resolve to a specific anonymous
10963 -- access type.
10965 if Ada_Version < Ada_2005
10966 or else Ekind (Base_Type (Ftyp)) /=
10967 E_Anonymous_Access_Type
10968 or else Ekind (Base_Type (Etype (Actual))) /=
10969 E_Anonymous_Access_Type
10970 then
10971 Error_Msg_NE
10972 ("type of actual does not match type of&", Actual, Gen_Obj);
10973 end if;
10974 end if;
10976 Note_Possible_Modification (Actual, Sure => True);
10978 -- Check for instantiation of atomic/volatile actual for
10979 -- non-atomic/volatile formal (RM C.6 (12)).
10981 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
10982 Error_Msg_N
10983 ("cannot instantiate non-atomic formal object "
10984 & "with atomic actual", Actual);
10986 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
10987 then
10988 Error_Msg_N
10989 ("cannot instantiate non-volatile formal object "
10990 & "with volatile actual", Actual);
10991 end if;
10993 -- Formal in-parameter
10995 else
10996 -- The instantiation of a generic formal in-parameter is constant
10997 -- declaration. The actual is the expression for that declaration.
10998 -- Its type is a full copy of the type of the formal. This may be
10999 -- an access to subprogram, for which we need to generate entities
11000 -- for the formals in the new signature.
11002 if Present (Actual) then
11003 if Present (Subt_Mark) then
11004 Def := New_Copy_Tree (Subt_Mark);
11005 else pragma Assert (Present (Acc_Def));
11006 Def := Copy_Access_Def;
11007 end if;
11009 Decl_Node :=
11010 Make_Object_Declaration (Loc,
11011 Defining_Identifier => New_Copy (Gen_Obj),
11012 Constant_Present => True,
11013 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11014 Object_Definition => Def,
11015 Expression => Actual);
11017 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11019 -- A generic formal object of a tagged type is defined to be
11020 -- aliased so the new constant must also be treated as aliased.
11022 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
11023 Set_Aliased_Present (Decl_Node);
11024 end if;
11026 Append (Decl_Node, List);
11028 -- No need to repeat (pre-)analysis of some expression nodes
11029 -- already handled in Preanalyze_Actuals.
11031 if Nkind (Actual) /= N_Allocator then
11032 Analyze (Actual);
11034 -- Return if the analysis of the actual reported some error
11036 if Etype (Actual) = Any_Type then
11037 return List;
11038 end if;
11039 end if;
11041 declare
11042 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
11043 Typ : Entity_Id;
11045 begin
11046 Typ := Get_Instance_Of (Formal_Type);
11048 -- If the actual appears in the current or an enclosing scope,
11049 -- use its type directly. This is relevant if it has an actual
11050 -- subtype that is distinct from its nominal one. This cannot
11051 -- be done in general because the type of the actual may
11052 -- depend on other actuals, and only be fully determined when
11053 -- the enclosing instance is analyzed.
11055 if Present (Etype (Actual))
11056 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
11057 then
11058 Freeze_Before (Instantiation_Node, Etype (Actual));
11059 else
11060 Freeze_Before (Instantiation_Node, Typ);
11061 end if;
11063 -- If the actual is an aggregate, perform name resolution on
11064 -- its components (the analysis of an aggregate does not do it)
11065 -- to capture local names that may be hidden if the generic is
11066 -- a child unit.
11068 if Nkind (Actual) = N_Aggregate then
11069 Preanalyze_And_Resolve (Actual, Typ);
11070 end if;
11072 if Is_Limited_Type (Typ)
11073 and then not OK_For_Limited_Init (Typ, Actual)
11074 then
11075 Error_Msg_N
11076 ("initialization not allowed for limited types", Actual);
11077 Explain_Limited_Type (Typ, Actual);
11078 end if;
11079 end;
11081 elsif Present (Default_Expression (Formal)) then
11083 -- Use default to construct declaration
11085 if Present (Subt_Mark) then
11086 Def := New_Copy (Subt_Mark);
11087 else pragma Assert (Present (Acc_Def));
11088 Def := Copy_Access_Def;
11089 end if;
11091 Decl_Node :=
11092 Make_Object_Declaration (Sloc (Formal),
11093 Defining_Identifier => New_Copy (Gen_Obj),
11094 Constant_Present => True,
11095 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11096 Object_Definition => Def,
11097 Expression => New_Copy_Tree
11098 (Default_Expression (Formal)));
11100 Append (Decl_Node, List);
11101 Set_Analyzed (Expression (Decl_Node), False);
11103 else
11104 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
11105 Error_Msg_NE ("\in instantiation of & declared#",
11106 Instantiation_Node, Scope (A_Gen_Obj));
11108 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
11110 -- Create dummy constant declaration so that instance can be
11111 -- analyzed, to minimize cascaded visibility errors.
11113 if Present (Subt_Mark) then
11114 Def := Subt_Mark;
11115 else pragma Assert (Present (Acc_Def));
11116 Def := Acc_Def;
11117 end if;
11119 Decl_Node :=
11120 Make_Object_Declaration (Loc,
11121 Defining_Identifier => New_Copy (Gen_Obj),
11122 Constant_Present => True,
11123 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11124 Object_Definition => New_Copy (Def),
11125 Expression =>
11126 Make_Attribute_Reference (Sloc (Gen_Obj),
11127 Attribute_Name => Name_First,
11128 Prefix => New_Copy (Def)));
11130 Append (Decl_Node, List);
11132 else
11133 Abandon_Instantiation (Instantiation_Node);
11134 end if;
11135 end if;
11136 end if;
11138 if Nkind (Actual) in N_Has_Entity then
11139 Actual_Decl := Parent (Entity (Actual));
11140 end if;
11142 -- Ada 2005 (AI-423): For a formal object declaration with a null
11143 -- exclusion or an access definition that has a null exclusion: If the
11144 -- actual matching the formal object declaration denotes a generic
11145 -- formal object of another generic unit G, and the instantiation
11146 -- containing the actual occurs within the body of G or within the body
11147 -- of a generic unit declared within the declarative region of G, then
11148 -- the declaration of the formal object of G must have a null exclusion.
11149 -- Otherwise, the subtype of the actual matching the formal object
11150 -- declaration shall exclude null.
11152 if Ada_Version >= Ada_2005
11153 and then Present (Actual_Decl)
11154 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
11155 N_Object_Declaration)
11156 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
11157 and then not Has_Null_Exclusion (Actual_Decl)
11158 and then Has_Null_Exclusion (Analyzed_Formal)
11159 then
11160 Error_Msg_Sloc := Sloc (Analyzed_Formal);
11161 Error_Msg_N
11162 ("actual must exclude null to match generic formal#", Actual);
11163 end if;
11165 -- An effectively volatile object cannot be used as an actual in a
11166 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
11167 -- relevant only when SPARK_Mode is on as it is not a standard Ada
11168 -- legality rule, and also verifies that the actual is an object.
11170 if SPARK_Mode = On
11171 and then Present (Actual)
11172 and then Is_Object_Reference (Actual)
11173 and then Is_Effectively_Volatile_Object (Actual)
11174 then
11175 Error_Msg_N
11176 ("volatile object cannot act as actual in generic instantiation",
11177 Actual);
11178 end if;
11180 return List;
11181 end Instantiate_Object;
11183 ------------------------------
11184 -- Instantiate_Package_Body --
11185 ------------------------------
11187 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11188 -- must be replaced by gotos which jump to the end of the routine in order
11189 -- to restore the Ghost and SPARK modes.
11191 procedure Instantiate_Package_Body
11192 (Body_Info : Pending_Body_Info;
11193 Inlined_Body : Boolean := False;
11194 Body_Optional : Boolean := False)
11196 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11197 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11198 Act_Spec : constant Node_Id := Specification (Act_Decl);
11199 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11200 Gen_Id : constant Node_Id := Name (Inst_Node);
11201 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11202 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11203 Loc : constant Source_Ptr := Sloc (Inst_Node);
11205 Saved_ISMP : constant Boolean :=
11206 Ignore_SPARK_Mode_Pragmas_In_Instance;
11207 Saved_Style_Check : constant Boolean := Style_Check;
11209 procedure Check_Initialized_Types;
11210 -- In a generic package body, an entity of a generic private type may
11211 -- appear uninitialized. This is suspicious, unless the actual is a
11212 -- fully initialized type.
11214 -----------------------------
11215 -- Check_Initialized_Types --
11216 -----------------------------
11218 procedure Check_Initialized_Types is
11219 Decl : Node_Id;
11220 Formal : Entity_Id;
11221 Actual : Entity_Id;
11222 Uninit_Var : Entity_Id;
11224 begin
11225 Decl := First (Generic_Formal_Declarations (Gen_Decl));
11226 while Present (Decl) loop
11227 Uninit_Var := Empty;
11229 if Nkind (Decl) = N_Private_Extension_Declaration then
11230 Uninit_Var := Uninitialized_Variable (Decl);
11232 elsif Nkind (Decl) = N_Formal_Type_Declaration
11233 and then Nkind (Formal_Type_Definition (Decl)) =
11234 N_Formal_Private_Type_Definition
11235 then
11236 Uninit_Var :=
11237 Uninitialized_Variable (Formal_Type_Definition (Decl));
11238 end if;
11240 if Present (Uninit_Var) then
11241 Formal := Defining_Identifier (Decl);
11242 Actual := First_Entity (Act_Decl_Id);
11244 -- For each formal there is a subtype declaration that renames
11245 -- the actual and has the same name as the formal. Locate the
11246 -- formal for warning message about uninitialized variables
11247 -- in the generic, for which the actual type should be a fully
11248 -- initialized type.
11250 while Present (Actual) loop
11251 exit when Ekind (Actual) = E_Package
11252 and then Present (Renamed_Object (Actual));
11254 if Chars (Actual) = Chars (Formal)
11255 and then not Is_Scalar_Type (Actual)
11256 and then not Is_Fully_Initialized_Type (Actual)
11257 and then Warn_On_No_Value_Assigned
11258 then
11259 Error_Msg_Node_2 := Formal;
11260 Error_Msg_NE
11261 ("generic unit has uninitialized variable& of "
11262 & "formal private type &?v?", Actual, Uninit_Var);
11263 Error_Msg_NE
11264 ("actual type for& should be fully initialized type?v?",
11265 Actual, Formal);
11266 exit;
11267 end if;
11269 Next_Entity (Actual);
11270 end loop;
11271 end if;
11273 Next (Decl);
11274 end loop;
11275 end Check_Initialized_Types;
11277 -- Local variables
11279 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11280 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11281 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11282 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11283 -- Save the Ghost and SPARK mode-related data to restore on exit
11285 Act_Body : Node_Id;
11286 Act_Body_Id : Entity_Id;
11287 Act_Body_Name : Node_Id;
11288 Gen_Body : Node_Id;
11289 Gen_Body_Id : Node_Id;
11290 Par_Ent : Entity_Id := Empty;
11291 Par_Vis : Boolean := False;
11292 Parent_Installed : Boolean := False;
11294 Vis_Prims_List : Elist_Id := No_Elist;
11295 -- List of primitives made temporarily visible in the instantiation
11296 -- to match the visibility of the formal type.
11298 -- Start of processing for Instantiate_Package_Body
11300 begin
11301 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11303 -- The instance body may already have been processed, as the parent of
11304 -- another instance that is inlined (Load_Parent_Of_Generic).
11306 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11307 return;
11308 end if;
11310 -- The package being instantiated may be subject to pragma Ghost. Set
11311 -- the mode now to ensure that any nodes generated during instantiation
11312 -- are properly marked as Ghost.
11314 Set_Ghost_Mode (Act_Decl_Id);
11316 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11318 -- Re-establish the state of information on which checks are suppressed.
11319 -- This information was set in Body_Info at the point of instantiation,
11320 -- and now we restore it so that the instance is compiled using the
11321 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11323 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11324 Scope_Suppress := Body_Info.Scope_Suppress;
11326 Restore_Config_Switches (Body_Info.Config_Switches);
11327 Restore_Warnings (Body_Info.Warnings);
11329 if No (Gen_Body_Id) then
11331 -- Do not look for parent of generic body if none is required.
11332 -- This may happen when the routine is called as part of the
11333 -- Pending_Instantiations processing, when nested instances
11334 -- may precede the one generated from the main unit.
11336 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
11337 and then Body_Optional
11338 then
11339 goto Leave;
11340 else
11341 Load_Parent_Of_Generic
11342 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11343 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11344 end if;
11345 end if;
11347 -- Establish global variable for sloc adjustment and for error recovery
11348 -- In the case of an instance body for an instantiation with actuals
11349 -- from a limited view, the instance body is placed at the beginning
11350 -- of the enclosing package body: use the body entity as the source
11351 -- location for nodes of the instance body.
11353 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
11354 declare
11355 Scop : constant Entity_Id := Scope (Act_Decl_Id);
11356 Body_Id : constant Node_Id :=
11357 Corresponding_Body (Unit_Declaration_Node (Scop));
11359 begin
11360 Instantiation_Node := Body_Id;
11361 end;
11362 else
11363 Instantiation_Node := Inst_Node;
11364 end if;
11366 if Present (Gen_Body_Id) then
11367 Save_Env (Gen_Unit, Act_Decl_Id);
11368 Style_Check := False;
11370 -- If the context of the instance is subject to SPARK_Mode "off", the
11371 -- annotation is missing, or the body is instantiated at a later pass
11372 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11373 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11374 -- instance.
11376 if SPARK_Mode /= On
11377 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11378 then
11379 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11380 end if;
11382 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11383 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11385 Create_Instantiation_Source
11386 (Inst_Node, Gen_Body_Id, S_Adjustment);
11388 Act_Body :=
11389 Copy_Generic_Node
11390 (Original_Node (Gen_Body), Empty, Instantiating => True);
11392 -- Create proper (possibly qualified) defining name for the body, to
11393 -- correspond to the one in the spec.
11395 Act_Body_Id :=
11396 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11397 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11399 -- Some attributes of spec entity are not inherited by body entity
11401 Set_Handler_Records (Act_Body_Id, No_List);
11403 if Nkind (Defining_Unit_Name (Act_Spec)) =
11404 N_Defining_Program_Unit_Name
11405 then
11406 Act_Body_Name :=
11407 Make_Defining_Program_Unit_Name (Loc,
11408 Name =>
11409 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
11410 Defining_Identifier => Act_Body_Id);
11411 else
11412 Act_Body_Name := Act_Body_Id;
11413 end if;
11415 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
11417 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11418 Check_Generic_Actuals (Act_Decl_Id, False);
11419 Check_Initialized_Types;
11421 -- Install primitives hidden at the point of the instantiation but
11422 -- visible when processing the generic formals
11424 declare
11425 E : Entity_Id;
11427 begin
11428 E := First_Entity (Act_Decl_Id);
11429 while Present (E) loop
11430 if Is_Type (E)
11431 and then not Is_Itype (E)
11432 and then Is_Generic_Actual_Type (E)
11433 and then Is_Tagged_Type (E)
11434 then
11435 Install_Hidden_Primitives
11436 (Prims_List => Vis_Prims_List,
11437 Gen_T => Generic_Parent_Type (Parent (E)),
11438 Act_T => E);
11439 end if;
11441 Next_Entity (E);
11442 end loop;
11443 end;
11445 -- If it is a child unit, make the parent instance (which is an
11446 -- instance of the parent of the generic) visible. The parent
11447 -- instance is the prefix of the name of the generic unit.
11449 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11450 and then Nkind (Gen_Id) = N_Expanded_Name
11451 then
11452 Par_Ent := Entity (Prefix (Gen_Id));
11453 Par_Vis := Is_Immediately_Visible (Par_Ent);
11454 Install_Parent (Par_Ent, In_Body => True);
11455 Parent_Installed := True;
11457 elsif Is_Child_Unit (Gen_Unit) then
11458 Par_Ent := Scope (Gen_Unit);
11459 Par_Vis := Is_Immediately_Visible (Par_Ent);
11460 Install_Parent (Par_Ent, In_Body => True);
11461 Parent_Installed := True;
11462 end if;
11464 -- If the instantiation is a library unit, and this is the main unit,
11465 -- then build the resulting compilation unit nodes for the instance.
11466 -- If this is a compilation unit but it is not the main unit, then it
11467 -- is the body of a unit in the context, that is being compiled
11468 -- because it is encloses some inlined unit or another generic unit
11469 -- being instantiated. In that case, this body is not part of the
11470 -- current compilation, and is not attached to the tree, but its
11471 -- parent must be set for analysis.
11473 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11475 -- Replace instance node with body of instance, and create new
11476 -- node for corresponding instance declaration.
11478 Build_Instance_Compilation_Unit_Nodes
11479 (Inst_Node, Act_Body, Act_Decl);
11480 Analyze (Inst_Node);
11482 if Parent (Inst_Node) = Cunit (Main_Unit) then
11484 -- If the instance is a child unit itself, then set the scope
11485 -- of the expanded body to be the parent of the instantiation
11486 -- (ensuring that the fully qualified name will be generated
11487 -- for the elaboration subprogram).
11489 if Nkind (Defining_Unit_Name (Act_Spec)) =
11490 N_Defining_Program_Unit_Name
11491 then
11492 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
11493 end if;
11494 end if;
11496 -- Case where instantiation is not a library unit
11498 else
11499 -- If this is an early instantiation, i.e. appears textually
11500 -- before the corresponding body and must be elaborated first,
11501 -- indicate that the body instance is to be delayed.
11503 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
11505 -- Now analyze the body. We turn off all checks if this is an
11506 -- internal unit, since there is no reason to have checks on for
11507 -- any predefined run-time library code. All such code is designed
11508 -- to be compiled with checks off.
11510 -- Note that we do NOT apply this criterion to children of GNAT
11511 -- The latter units must suppress checks explicitly if needed.
11513 -- We also do not suppress checks in CodePeer mode where we are
11514 -- interested in finding possible runtime errors.
11516 if not CodePeer_Mode
11517 and then In_Predefined_Unit (Gen_Decl)
11518 then
11519 Analyze (Act_Body, Suppress => All_Checks);
11520 else
11521 Analyze (Act_Body);
11522 end if;
11523 end if;
11525 Inherit_Context (Gen_Body, Inst_Node);
11527 -- Remove the parent instances if they have been placed on the scope
11528 -- stack to compile the body.
11530 if Parent_Installed then
11531 Remove_Parent (In_Body => True);
11533 -- Restore the previous visibility of the parent
11535 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11536 end if;
11538 Restore_Hidden_Primitives (Vis_Prims_List);
11539 Restore_Private_Views (Act_Decl_Id);
11541 -- Remove the current unit from visibility if this is an instance
11542 -- that is not elaborated on the fly for inlining purposes.
11544 if not Inlined_Body then
11545 Set_Is_Immediately_Visible (Act_Decl_Id, False);
11546 end if;
11548 Restore_Env;
11550 -- If we have no body, and the unit requires a body, then complain. This
11551 -- complaint is suppressed if we have detected other errors (since a
11552 -- common reason for missing the body is that it had errors).
11553 -- In CodePeer mode, a warning has been emitted already, no need for
11554 -- further messages.
11556 elsif Unit_Requires_Body (Gen_Unit)
11557 and then not Body_Optional
11558 then
11559 if CodePeer_Mode then
11560 null;
11562 elsif Serious_Errors_Detected = 0 then
11563 Error_Msg_NE
11564 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
11566 -- Don't attempt to perform any cleanup actions if some other error
11567 -- was already detected, since this can cause blowups.
11569 else
11570 goto Leave;
11571 end if;
11573 -- Case of package that does not need a body
11575 else
11576 -- If the instantiation of the declaration is a library unit, rewrite
11577 -- the original package instantiation as a package declaration in the
11578 -- compilation unit node.
11580 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11581 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
11582 Rewrite (Inst_Node, Act_Decl);
11584 -- Generate elaboration entity, in case spec has elaboration code.
11585 -- This cannot be done when the instance is analyzed, because it
11586 -- is not known yet whether the body exists.
11588 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
11589 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
11591 -- If the instantiation is not a library unit, then append the
11592 -- declaration to the list of implicitly generated entities, unless
11593 -- it is already a list member which means that it was already
11594 -- processed
11596 elsif not Is_List_Member (Act_Decl) then
11597 Mark_Rewrite_Insertion (Act_Decl);
11598 Insert_Before (Inst_Node, Act_Decl);
11599 end if;
11600 end if;
11602 Expander_Mode_Restore;
11604 <<Leave>>
11605 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
11606 Restore_Ghost_Region (Saved_GM, Saved_IGR);
11607 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
11608 Style_Check := Saved_Style_Check;
11609 end Instantiate_Package_Body;
11611 ---------------------------------
11612 -- Instantiate_Subprogram_Body --
11613 ---------------------------------
11615 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11616 -- must be replaced by gotos which jump to the end of the routine in order
11617 -- to restore the Ghost and SPARK modes.
11619 procedure Instantiate_Subprogram_Body
11620 (Body_Info : Pending_Body_Info;
11621 Body_Optional : Boolean := False)
11623 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11624 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11625 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11626 Gen_Id : constant Node_Id := Name (Inst_Node);
11627 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11628 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11629 Loc : constant Source_Ptr := Sloc (Inst_Node);
11630 Pack_Id : constant Entity_Id :=
11631 Defining_Unit_Name (Parent (Act_Decl));
11633 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11634 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11635 Saved_ISMP : constant Boolean :=
11636 Ignore_SPARK_Mode_Pragmas_In_Instance;
11637 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11638 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11639 -- Save the Ghost and SPARK mode-related data to restore on exit
11641 Saved_Style_Check : constant Boolean := Style_Check;
11642 Saved_Warnings : constant Warning_Record := Save_Warnings;
11644 Act_Body : Node_Id;
11645 Act_Body_Id : Entity_Id;
11646 Gen_Body : Node_Id;
11647 Gen_Body_Id : Node_Id;
11648 Pack_Body : Node_Id;
11649 Par_Ent : Entity_Id := Empty;
11650 Par_Vis : Boolean := False;
11651 Ret_Expr : Node_Id;
11653 Parent_Installed : Boolean := False;
11655 begin
11656 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11658 -- Subprogram body may have been created already because of an inline
11659 -- pragma, or because of multiple elaborations of the enclosing package
11660 -- when several instances of the subprogram appear in the main unit.
11662 if Present (Corresponding_Body (Act_Decl)) then
11663 return;
11664 end if;
11666 -- The subprogram being instantiated may be subject to pragma Ghost. Set
11667 -- the mode now to ensure that any nodes generated during instantiation
11668 -- are properly marked as Ghost.
11670 Set_Ghost_Mode (Act_Decl_Id);
11672 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11674 -- Re-establish the state of information on which checks are suppressed.
11675 -- This information was set in Body_Info at the point of instantiation,
11676 -- and now we restore it so that the instance is compiled using the
11677 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11679 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11680 Scope_Suppress := Body_Info.Scope_Suppress;
11682 Restore_Config_Switches (Body_Info.Config_Switches);
11683 Restore_Warnings (Body_Info.Warnings);
11685 if No (Gen_Body_Id) then
11687 -- For imported generic subprogram, no body to compile, complete
11688 -- the spec entity appropriately.
11690 if Is_Imported (Gen_Unit) then
11691 Set_Is_Imported (Act_Decl_Id);
11692 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
11693 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
11694 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
11695 Set_Has_Completion (Act_Decl_Id);
11696 goto Leave;
11698 -- For other cases, compile the body
11700 else
11701 Load_Parent_Of_Generic
11702 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11703 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11704 end if;
11705 end if;
11707 Instantiation_Node := Inst_Node;
11709 if Present (Gen_Body_Id) then
11710 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11712 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11714 -- Either body is not present, or context is non-expanding, as
11715 -- when compiling a subunit. Mark the instance as completed, and
11716 -- diagnose a missing body when needed.
11718 if Expander_Active
11719 and then Operating_Mode = Generate_Code
11720 then
11721 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
11722 end if;
11724 Set_Has_Completion (Act_Decl_Id);
11725 goto Leave;
11726 end if;
11728 Save_Env (Gen_Unit, Act_Decl_Id);
11729 Style_Check := False;
11731 -- If the context of the instance is subject to SPARK_Mode "off", the
11732 -- annotation is missing, or the body is instantiated at a later pass
11733 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11734 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11735 -- instance.
11737 if SPARK_Mode /= On
11738 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11739 then
11740 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11741 end if;
11743 -- If the context of an instance is not subject to SPARK_Mode "off",
11744 -- and the generic body is subject to an explicit SPARK_Mode pragma,
11745 -- the latter should be the one applicable to the instance.
11747 if not Ignore_SPARK_Mode_Pragmas_In_Instance
11748 and then SPARK_Mode /= Off
11749 and then Present (SPARK_Pragma (Gen_Body_Id))
11750 then
11751 Set_SPARK_Mode (Gen_Body_Id);
11752 end if;
11754 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11755 Create_Instantiation_Source
11756 (Inst_Node,
11757 Gen_Body_Id,
11758 S_Adjustment);
11760 Act_Body :=
11761 Copy_Generic_Node
11762 (Original_Node (Gen_Body), Empty, Instantiating => True);
11764 -- Create proper defining name for the body, to correspond to the one
11765 -- in the spec.
11767 Act_Body_Id :=
11768 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11770 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11771 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
11773 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11774 Set_Has_Completion (Act_Decl_Id);
11775 Check_Generic_Actuals (Pack_Id, False);
11777 -- Generate a reference to link the visible subprogram instance to
11778 -- the generic body, which for navigation purposes is the only
11779 -- available source for the instance.
11781 Generate_Reference
11782 (Related_Instance (Pack_Id),
11783 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11785 -- If it is a child unit, make the parent instance (which is an
11786 -- instance of the parent of the generic) visible. The parent
11787 -- instance is the prefix of the name of the generic unit.
11789 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11790 and then Nkind (Gen_Id) = N_Expanded_Name
11791 then
11792 Par_Ent := Entity (Prefix (Gen_Id));
11793 Par_Vis := Is_Immediately_Visible (Par_Ent);
11794 Install_Parent (Par_Ent, In_Body => True);
11795 Parent_Installed := True;
11797 elsif Is_Child_Unit (Gen_Unit) then
11798 Par_Ent := Scope (Gen_Unit);
11799 Par_Vis := Is_Immediately_Visible (Par_Ent);
11800 Install_Parent (Par_Ent, In_Body => True);
11801 Parent_Installed := True;
11802 end if;
11804 -- Subprogram body is placed in the body of wrapper package,
11805 -- whose spec contains the subprogram declaration as well as
11806 -- the renaming declarations for the generic parameters.
11808 Pack_Body :=
11809 Make_Package_Body (Loc,
11810 Defining_Unit_Name => New_Copy (Pack_Id),
11811 Declarations => New_List (Act_Body));
11813 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11815 -- If the instantiation is a library unit, then build resulting
11816 -- compilation unit nodes for the instance. The declaration of
11817 -- the enclosing package is the grandparent of the subprogram
11818 -- declaration. First replace the instantiation node as the unit
11819 -- of the corresponding compilation.
11821 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11822 if Parent (Inst_Node) = Cunit (Main_Unit) then
11823 Set_Unit (Parent (Inst_Node), Inst_Node);
11824 Build_Instance_Compilation_Unit_Nodes
11825 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11826 Analyze (Inst_Node);
11827 else
11828 Set_Parent (Pack_Body, Parent (Inst_Node));
11829 Analyze (Pack_Body);
11830 end if;
11832 else
11833 Insert_Before (Inst_Node, Pack_Body);
11834 Mark_Rewrite_Insertion (Pack_Body);
11835 Analyze (Pack_Body);
11837 if Expander_Active then
11838 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11839 end if;
11840 end if;
11842 Inherit_Context (Gen_Body, Inst_Node);
11844 Restore_Private_Views (Pack_Id, False);
11846 if Parent_Installed then
11847 Remove_Parent (In_Body => True);
11849 -- Restore the previous visibility of the parent
11851 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11852 end if;
11854 Restore_Env;
11855 Restore_Warnings (Saved_Warnings);
11857 -- Body not found. Error was emitted already. If there were no previous
11858 -- errors, this may be an instance whose scope is a premature instance.
11859 -- In that case we must insure that the (legal) program does raise
11860 -- program error if executed. We generate a subprogram body for this
11861 -- purpose. See DEC ac30vso.
11863 -- Should not reference proprietary DEC tests in comments ???
11865 elsif Serious_Errors_Detected = 0
11866 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
11867 then
11868 if Body_Optional then
11869 goto Leave;
11871 elsif Ekind (Act_Decl_Id) = E_Procedure then
11872 Act_Body :=
11873 Make_Subprogram_Body (Loc,
11874 Specification =>
11875 Make_Procedure_Specification (Loc,
11876 Defining_Unit_Name =>
11877 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11878 Parameter_Specifications =>
11879 New_Copy_List
11880 (Parameter_Specifications (Parent (Act_Decl_Id)))),
11882 Declarations => Empty_List,
11883 Handled_Statement_Sequence =>
11884 Make_Handled_Sequence_Of_Statements (Loc,
11885 Statements => New_List (
11886 Make_Raise_Program_Error (Loc,
11887 Reason => PE_Access_Before_Elaboration))));
11889 else
11890 Ret_Expr :=
11891 Make_Raise_Program_Error (Loc,
11892 Reason => PE_Access_Before_Elaboration);
11894 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
11895 Set_Analyzed (Ret_Expr);
11897 Act_Body :=
11898 Make_Subprogram_Body (Loc,
11899 Specification =>
11900 Make_Function_Specification (Loc,
11901 Defining_Unit_Name =>
11902 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11903 Parameter_Specifications =>
11904 New_Copy_List
11905 (Parameter_Specifications (Parent (Act_Decl_Id))),
11906 Result_Definition =>
11907 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
11909 Declarations => Empty_List,
11910 Handled_Statement_Sequence =>
11911 Make_Handled_Sequence_Of_Statements (Loc,
11912 Statements => New_List (
11913 Make_Simple_Return_Statement (Loc, Ret_Expr))));
11914 end if;
11916 Pack_Body :=
11917 Make_Package_Body (Loc,
11918 Defining_Unit_Name => New_Copy (Pack_Id),
11919 Declarations => New_List (Act_Body));
11921 Insert_After (Inst_Node, Pack_Body);
11922 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11923 Analyze (Pack_Body);
11924 end if;
11926 Expander_Mode_Restore;
11928 <<Leave>>
11929 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
11930 Restore_Ghost_Region (Saved_GM, Saved_IGR);
11931 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
11932 Style_Check := Saved_Style_Check;
11933 end Instantiate_Subprogram_Body;
11935 ----------------------
11936 -- Instantiate_Type --
11937 ----------------------
11939 function Instantiate_Type
11940 (Formal : Node_Id;
11941 Actual : Node_Id;
11942 Analyzed_Formal : Node_Id;
11943 Actual_Decls : List_Id) return List_Id
11945 A_Gen_T : constant Entity_Id :=
11946 Defining_Identifier (Analyzed_Formal);
11947 Def : constant Node_Id := Formal_Type_Definition (Formal);
11948 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
11949 Act_T : Entity_Id;
11950 Ancestor : Entity_Id := Empty;
11951 Decl_Node : Node_Id;
11952 Decl_Nodes : List_Id;
11953 Loc : Source_Ptr;
11954 Subt : Entity_Id;
11956 procedure Diagnose_Predicated_Actual;
11957 -- There are a number of constructs in which a discrete type with
11958 -- predicates is illegal, e.g. as an index in an array type declaration.
11959 -- If a generic type is used is such a construct in a generic package
11960 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11961 -- of the generic contract that the actual cannot have predicates.
11963 procedure Validate_Array_Type_Instance;
11964 procedure Validate_Access_Subprogram_Instance;
11965 procedure Validate_Access_Type_Instance;
11966 procedure Validate_Derived_Type_Instance;
11967 procedure Validate_Derived_Interface_Type_Instance;
11968 procedure Validate_Discriminated_Formal_Type;
11969 procedure Validate_Interface_Type_Instance;
11970 procedure Validate_Private_Type_Instance;
11971 procedure Validate_Incomplete_Type_Instance;
11972 -- These procedures perform validation tests for the named case.
11973 -- Validate_Discriminated_Formal_Type is shared by formal private
11974 -- types and Ada 2012 formal incomplete types.
11976 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
11977 -- Check that base types are the same and that the subtypes match
11978 -- statically. Used in several of the above.
11980 ---------------------------------
11981 -- Diagnose_Predicated_Actual --
11982 ---------------------------------
11984 procedure Diagnose_Predicated_Actual is
11985 begin
11986 if No_Predicate_On_Actual (A_Gen_T)
11987 and then Has_Predicates (Act_T)
11988 then
11989 Error_Msg_NE
11990 ("actual for& cannot be a type with predicate",
11991 Instantiation_Node, A_Gen_T);
11993 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
11994 and then Has_Predicates (Act_T)
11995 and then not Has_Static_Predicate_Aspect (Act_T)
11996 then
11997 Error_Msg_NE
11998 ("actual for& cannot be a type with a dynamic predicate",
11999 Instantiation_Node, A_Gen_T);
12000 end if;
12001 end Diagnose_Predicated_Actual;
12003 --------------------
12004 -- Subtypes_Match --
12005 --------------------
12007 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
12008 T : constant Entity_Id := Get_Instance_Of (Gen_T);
12010 begin
12011 -- Some detailed comments would be useful here ???
12013 return ((Base_Type (T) = Act_T
12014 or else Base_Type (T) = Base_Type (Act_T))
12015 and then Subtypes_Statically_Match (T, Act_T))
12017 or else (Is_Class_Wide_Type (Gen_T)
12018 and then Is_Class_Wide_Type (Act_T)
12019 and then Subtypes_Match
12020 (Get_Instance_Of (Root_Type (Gen_T)),
12021 Root_Type (Act_T)))
12023 or else
12024 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
12025 E_Anonymous_Access_Type)
12026 and then Ekind (Act_T) = Ekind (Gen_T)
12027 and then Subtypes_Statically_Match
12028 (Designated_Type (Gen_T), Designated_Type (Act_T)));
12029 end Subtypes_Match;
12031 -----------------------------------------
12032 -- Validate_Access_Subprogram_Instance --
12033 -----------------------------------------
12035 procedure Validate_Access_Subprogram_Instance is
12036 begin
12037 if not Is_Access_Type (Act_T)
12038 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
12039 then
12040 Error_Msg_NE
12041 ("expect access type in instantiation of &", Actual, Gen_T);
12042 Abandon_Instantiation (Actual);
12043 end if;
12045 -- According to AI05-288, actuals for access_to_subprograms must be
12046 -- subtype conformant with the generic formal. Previous to AI05-288
12047 -- only mode conformance was required.
12049 -- This is a binding interpretation that applies to previous versions
12050 -- of the language, no need to maintain previous weaker checks.
12052 Check_Subtype_Conformant
12053 (Designated_Type (Act_T),
12054 Designated_Type (A_Gen_T),
12055 Actual,
12056 Get_Inst => True);
12058 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
12059 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
12060 Error_Msg_NE
12061 ("protected access type not allowed for formal &",
12062 Actual, Gen_T);
12063 end if;
12065 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
12066 Error_Msg_NE
12067 ("expect protected access type for formal &",
12068 Actual, Gen_T);
12069 end if;
12071 -- If the formal has a specified convention (which in most cases
12072 -- will be StdCall) verify that the actual has the same convention.
12074 if Has_Convention_Pragma (A_Gen_T)
12075 and then Convention (A_Gen_T) /= Convention (Act_T)
12076 then
12077 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
12078 Error_Msg_NE
12079 ("actual for formal & must have convention %", Actual, Gen_T);
12080 end if;
12081 end Validate_Access_Subprogram_Instance;
12083 -----------------------------------
12084 -- Validate_Access_Type_Instance --
12085 -----------------------------------
12087 procedure Validate_Access_Type_Instance is
12088 Desig_Type : constant Entity_Id :=
12089 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
12090 Desig_Act : Entity_Id;
12092 begin
12093 if not Is_Access_Type (Act_T) then
12094 Error_Msg_NE
12095 ("expect access type in instantiation of &", Actual, Gen_T);
12096 Abandon_Instantiation (Actual);
12097 end if;
12099 if Is_Access_Constant (A_Gen_T) then
12100 if not Is_Access_Constant (Act_T) then
12101 Error_Msg_N
12102 ("actual type must be access-to-constant type", Actual);
12103 Abandon_Instantiation (Actual);
12104 end if;
12105 else
12106 if Is_Access_Constant (Act_T) then
12107 Error_Msg_N
12108 ("actual type must be access-to-variable type", Actual);
12109 Abandon_Instantiation (Actual);
12111 elsif Ekind (A_Gen_T) = E_General_Access_Type
12112 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
12113 then
12114 Error_Msg_N -- CODEFIX
12115 ("actual must be general access type!", Actual);
12116 Error_Msg_NE -- CODEFIX
12117 ("add ALL to }!", Actual, Act_T);
12118 Abandon_Instantiation (Actual);
12119 end if;
12120 end if;
12122 -- The designated subtypes, that is to say the subtypes introduced
12123 -- by an access type declaration (and not by a subtype declaration)
12124 -- must match.
12126 Desig_Act := Designated_Type (Base_Type (Act_T));
12128 -- The designated type may have been introduced through a limited_
12129 -- with clause, in which case retrieve the non-limited view. This
12130 -- applies to incomplete types as well as to class-wide types.
12132 if From_Limited_With (Desig_Act) then
12133 Desig_Act := Available_View (Desig_Act);
12134 end if;
12136 if not Subtypes_Match (Desig_Type, Desig_Act) then
12137 Error_Msg_NE
12138 ("designated type of actual does not match that of formal &",
12139 Actual, Gen_T);
12141 if not Predicates_Match (Desig_Type, Desig_Act) then
12142 Error_Msg_N ("\predicates do not match", Actual);
12143 end if;
12145 Abandon_Instantiation (Actual);
12147 elsif Is_Access_Type (Designated_Type (Act_T))
12148 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
12150 Is_Constrained (Designated_Type (Desig_Type))
12151 then
12152 Error_Msg_NE
12153 ("designated type of actual does not match that of formal &",
12154 Actual, Gen_T);
12156 if not Predicates_Match (Desig_Type, Desig_Act) then
12157 Error_Msg_N ("\predicates do not match", Actual);
12158 end if;
12160 Abandon_Instantiation (Actual);
12161 end if;
12163 -- Ada 2005: null-exclusion indicators of the two types must agree
12165 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12166 Error_Msg_NE
12167 ("non null exclusion of actual and formal & do not match",
12168 Actual, Gen_T);
12169 end if;
12170 end Validate_Access_Type_Instance;
12172 ----------------------------------
12173 -- Validate_Array_Type_Instance --
12174 ----------------------------------
12176 procedure Validate_Array_Type_Instance is
12177 I1 : Node_Id;
12178 I2 : Node_Id;
12179 T2 : Entity_Id;
12181 function Formal_Dimensions return Nat;
12182 -- Count number of dimensions in array type formal
12184 -----------------------
12185 -- Formal_Dimensions --
12186 -----------------------
12188 function Formal_Dimensions return Nat is
12189 Num : Nat := 0;
12190 Index : Node_Id;
12192 begin
12193 if Nkind (Def) = N_Constrained_Array_Definition then
12194 Index := First (Discrete_Subtype_Definitions (Def));
12195 else
12196 Index := First (Subtype_Marks (Def));
12197 end if;
12199 while Present (Index) loop
12200 Num := Num + 1;
12201 Next_Index (Index);
12202 end loop;
12204 return Num;
12205 end Formal_Dimensions;
12207 -- Start of processing for Validate_Array_Type_Instance
12209 begin
12210 if not Is_Array_Type (Act_T) then
12211 Error_Msg_NE
12212 ("expect array type in instantiation of &", Actual, Gen_T);
12213 Abandon_Instantiation (Actual);
12215 elsif Nkind (Def) = N_Constrained_Array_Definition then
12216 if not (Is_Constrained (Act_T)) then
12217 Error_Msg_NE
12218 ("expect constrained array in instantiation of &",
12219 Actual, Gen_T);
12220 Abandon_Instantiation (Actual);
12221 end if;
12223 else
12224 if Is_Constrained (Act_T) then
12225 Error_Msg_NE
12226 ("expect unconstrained array in instantiation of &",
12227 Actual, Gen_T);
12228 Abandon_Instantiation (Actual);
12229 end if;
12230 end if;
12232 if Formal_Dimensions /= Number_Dimensions (Act_T) then
12233 Error_Msg_NE
12234 ("dimensions of actual do not match formal &", Actual, Gen_T);
12235 Abandon_Instantiation (Actual);
12236 end if;
12238 I1 := First_Index (A_Gen_T);
12239 I2 := First_Index (Act_T);
12240 for J in 1 .. Formal_Dimensions loop
12242 -- If the indexes of the actual were given by a subtype_mark,
12243 -- the index was transformed into a range attribute. Retrieve
12244 -- the original type mark for checking.
12246 if Is_Entity_Name (Original_Node (I2)) then
12247 T2 := Entity (Original_Node (I2));
12248 else
12249 T2 := Etype (I2);
12250 end if;
12252 if not Subtypes_Match
12253 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
12254 then
12255 Error_Msg_NE
12256 ("index types of actual do not match those of formal &",
12257 Actual, Gen_T);
12258 Abandon_Instantiation (Actual);
12259 end if;
12261 Next_Index (I1);
12262 Next_Index (I2);
12263 end loop;
12265 -- Check matching subtypes. Note that there are complex visibility
12266 -- issues when the generic is a child unit and some aspect of the
12267 -- generic type is declared in a parent unit of the generic. We do
12268 -- the test to handle this special case only after a direct check
12269 -- for static matching has failed. The case where both the component
12270 -- type and the array type are separate formals, and the component
12271 -- type is a private view may also require special checking in
12272 -- Subtypes_Match. Finally, we assume that a child instance where
12273 -- the component type comes from a formal of a parent instance is
12274 -- correct because the generic was correct. A more precise check
12275 -- seems too complex to install???
12277 if Subtypes_Match
12278 (Component_Type (A_Gen_T), Component_Type (Act_T))
12279 or else
12280 Subtypes_Match
12281 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
12282 Component_Type (Act_T))
12283 or else
12284 (not Inside_A_Generic
12285 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
12286 then
12287 null;
12288 else
12289 Error_Msg_NE
12290 ("component subtype of actual does not match that of formal &",
12291 Actual, Gen_T);
12292 Abandon_Instantiation (Actual);
12293 end if;
12295 if Has_Aliased_Components (A_Gen_T)
12296 and then not Has_Aliased_Components (Act_T)
12297 then
12298 Error_Msg_NE
12299 ("actual must have aliased components to match formal type &",
12300 Actual, Gen_T);
12301 end if;
12302 end Validate_Array_Type_Instance;
12304 -----------------------------------------------
12305 -- Validate_Derived_Interface_Type_Instance --
12306 -----------------------------------------------
12308 procedure Validate_Derived_Interface_Type_Instance is
12309 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
12310 Elmt : Elmt_Id;
12312 begin
12313 -- First apply interface instance checks
12315 Validate_Interface_Type_Instance;
12317 -- Verify that immediate parent interface is an ancestor of
12318 -- the actual.
12320 if Present (Par)
12321 and then not Interface_Present_In_Ancestor (Act_T, Par)
12322 then
12323 Error_Msg_NE
12324 ("interface actual must include progenitor&", Actual, Par);
12325 end if;
12327 -- Now verify that the actual includes all other ancestors of
12328 -- the formal.
12330 Elmt := First_Elmt (Interfaces (A_Gen_T));
12331 while Present (Elmt) loop
12332 if not Interface_Present_In_Ancestor
12333 (Act_T, Get_Instance_Of (Node (Elmt)))
12334 then
12335 Error_Msg_NE
12336 ("interface actual must include progenitor&",
12337 Actual, Node (Elmt));
12338 end if;
12340 Next_Elmt (Elmt);
12341 end loop;
12342 end Validate_Derived_Interface_Type_Instance;
12344 ------------------------------------
12345 -- Validate_Derived_Type_Instance --
12346 ------------------------------------
12348 procedure Validate_Derived_Type_Instance is
12349 Actual_Discr : Entity_Id;
12350 Ancestor_Discr : Entity_Id;
12352 begin
12353 -- Verify that the actual includes the progenitors of the formal,
12354 -- if any. The formal may depend on previous formals and their
12355 -- instance, so we must examine instance of interfaces if present.
12356 -- The actual may be an extension of an interface, in which case
12357 -- it does not appear in the interface list, so this must be
12358 -- checked separately.
12360 if Present (Interface_List (Def)) then
12361 if not Has_Interfaces (Act_T) then
12362 Error_Msg_NE
12363 ("actual must implement all interfaces of formal&",
12364 Actual, A_Gen_T);
12366 else
12367 declare
12368 Act_Iface_List : Elist_Id;
12369 Iface : Node_Id;
12370 Iface_Ent : Entity_Id;
12372 function Instance_Exists (I : Entity_Id) return Boolean;
12373 -- If the interface entity is declared in a generic unit,
12374 -- this can only be legal if we are within an instantiation
12375 -- of a child of that generic. There is currently no
12376 -- mechanism to relate an interface declared within a
12377 -- generic to the corresponding interface in an instance,
12378 -- so we traverse the list of interfaces of the actual,
12379 -- looking for a name match.
12381 ---------------------
12382 -- Instance_Exists --
12383 ---------------------
12385 function Instance_Exists (I : Entity_Id) return Boolean is
12386 Iface_Elmt : Elmt_Id;
12388 begin
12389 Iface_Elmt := First_Elmt (Act_Iface_List);
12390 while Present (Iface_Elmt) loop
12391 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
12392 and then Chars (Node (Iface_Elmt)) = Chars (I)
12393 then
12394 return True;
12395 end if;
12397 Next_Elmt (Iface_Elmt);
12398 end loop;
12400 return False;
12401 end Instance_Exists;
12403 begin
12404 Iface := First (Abstract_Interface_List (A_Gen_T));
12405 Collect_Interfaces (Act_T, Act_Iface_List);
12407 while Present (Iface) loop
12408 Iface_Ent := Get_Instance_Of (Entity (Iface));
12410 if Is_Ancestor (Iface_Ent, Act_T)
12411 or else Is_Progenitor (Iface_Ent, Act_T)
12412 then
12413 null;
12415 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
12416 and then Instance_Exists (Iface_Ent)
12417 then
12418 null;
12420 else
12421 Error_Msg_Name_1 := Chars (Act_T);
12422 Error_Msg_NE
12423 ("Actual% must implement interface&",
12424 Actual, Etype (Iface));
12425 end if;
12427 Next (Iface);
12428 end loop;
12429 end;
12430 end if;
12431 end if;
12433 -- If the parent type in the generic declaration is itself a previous
12434 -- formal type, then it is local to the generic and absent from the
12435 -- analyzed generic definition. In that case the ancestor is the
12436 -- instance of the formal (which must have been instantiated
12437 -- previously), unless the ancestor is itself a formal derived type.
12438 -- In this latter case (which is the subject of Corrigendum 8652/0038
12439 -- (AI-202) the ancestor of the formals is the ancestor of its
12440 -- parent. Otherwise, the analyzed generic carries the parent type.
12441 -- If the parent type is defined in a previous formal package, then
12442 -- the scope of that formal package is that of the generic type
12443 -- itself, and it has already been mapped into the corresponding type
12444 -- in the actual package.
12446 -- Common case: parent type defined outside of the generic
12448 if Is_Entity_Name (Subtype_Mark (Def))
12449 and then Present (Entity (Subtype_Mark (Def)))
12450 then
12451 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
12453 -- Check whether parent is defined in a previous formal package
12455 elsif
12456 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
12457 then
12458 Ancestor :=
12459 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
12461 -- The type may be a local derivation, or a type extension of a
12462 -- previous formal, or of a formal of a parent package.
12464 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
12465 or else
12466 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
12467 then
12468 -- Check whether the parent is another derived formal type in the
12469 -- same generic unit.
12471 if Etype (A_Gen_T) /= A_Gen_T
12472 and then Is_Generic_Type (Etype (A_Gen_T))
12473 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
12474 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
12475 then
12476 -- Locate ancestor of parent from the subtype declaration
12477 -- created for the actual.
12479 declare
12480 Decl : Node_Id;
12482 begin
12483 Decl := First (Actual_Decls);
12484 while Present (Decl) loop
12485 if Nkind (Decl) = N_Subtype_Declaration
12486 and then Chars (Defining_Identifier (Decl)) =
12487 Chars (Etype (A_Gen_T))
12488 then
12489 Ancestor := Generic_Parent_Type (Decl);
12490 exit;
12491 else
12492 Next (Decl);
12493 end if;
12494 end loop;
12495 end;
12497 pragma Assert (Present (Ancestor));
12499 -- The ancestor itself may be a previous formal that has been
12500 -- instantiated.
12502 Ancestor := Get_Instance_Of (Ancestor);
12504 else
12505 Ancestor :=
12506 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
12507 end if;
12509 -- Check whether parent is a previous formal of the current generic
12511 elsif Is_Derived_Type (A_Gen_T)
12512 and then Is_Generic_Type (Etype (A_Gen_T))
12513 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
12514 then
12515 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
12517 -- An unusual case: the actual is a type declared in a parent unit,
12518 -- but is not a formal type so there is no instance_of for it.
12519 -- Retrieve it by analyzing the record extension.
12521 elsif Is_Child_Unit (Scope (A_Gen_T))
12522 and then In_Open_Scopes (Scope (Act_T))
12523 and then Is_Generic_Instance (Scope (Act_T))
12524 then
12525 Analyze (Subtype_Mark (Def));
12526 Ancestor := Entity (Subtype_Mark (Def));
12528 else
12529 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
12530 end if;
12532 -- If the formal derived type has pragma Preelaborable_Initialization
12533 -- then the actual type must have preelaborable initialization.
12535 if Known_To_Have_Preelab_Init (A_Gen_T)
12536 and then not Has_Preelaborable_Initialization (Act_T)
12537 then
12538 Error_Msg_NE
12539 ("actual for & must have preelaborable initialization",
12540 Actual, Gen_T);
12541 end if;
12543 -- Ada 2005 (AI-251)
12545 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
12546 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
12547 Error_Msg_NE
12548 ("(Ada 2005) expected type implementing & in instantiation",
12549 Actual, Ancestor);
12550 end if;
12552 -- Finally verify that the (instance of) the ancestor is an ancestor
12553 -- of the actual.
12555 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
12556 Error_Msg_NE
12557 ("expect type derived from & in instantiation",
12558 Actual, First_Subtype (Ancestor));
12559 Abandon_Instantiation (Actual);
12560 end if;
12562 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
12563 -- that the formal type declaration has been rewritten as a private
12564 -- extension.
12566 if Ada_Version >= Ada_2005
12567 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
12568 and then Synchronized_Present (Parent (A_Gen_T))
12569 then
12570 -- The actual must be a synchronized tagged type
12572 if not Is_Tagged_Type (Act_T) then
12573 Error_Msg_N
12574 ("actual of synchronized type must be tagged", Actual);
12575 Abandon_Instantiation (Actual);
12577 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
12578 and then Nkind (Type_Definition (Parent (Act_T))) =
12579 N_Derived_Type_Definition
12580 and then not Synchronized_Present
12581 (Type_Definition (Parent (Act_T)))
12582 then
12583 Error_Msg_N
12584 ("actual of synchronized type must be synchronized", Actual);
12585 Abandon_Instantiation (Actual);
12586 end if;
12587 end if;
12589 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
12590 -- removes the second instance of the phrase "or allow pass by copy".
12592 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
12593 Error_Msg_N
12594 ("cannot have atomic actual type for non-atomic formal type",
12595 Actual);
12597 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
12598 Error_Msg_N
12599 ("cannot have volatile actual type for non-volatile formal type",
12600 Actual);
12601 end if;
12603 -- It should not be necessary to check for unknown discriminants on
12604 -- Formal, but for some reason Has_Unknown_Discriminants is false for
12605 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
12606 -- needs fixing. ???
12608 if Is_Definite_Subtype (A_Gen_T)
12609 and then not Unknown_Discriminants_Present (Formal)
12610 and then not Is_Definite_Subtype (Act_T)
12611 then
12612 Error_Msg_N ("actual subtype must be constrained", Actual);
12613 Abandon_Instantiation (Actual);
12614 end if;
12616 if not Unknown_Discriminants_Present (Formal) then
12617 if Is_Constrained (Ancestor) then
12618 if not Is_Constrained (Act_T) then
12619 Error_Msg_N ("actual subtype must be constrained", Actual);
12620 Abandon_Instantiation (Actual);
12621 end if;
12623 -- Ancestor is unconstrained, Check if generic formal and actual
12624 -- agree on constrainedness. The check only applies to array types
12625 -- and discriminated types.
12627 elsif Is_Constrained (Act_T) then
12628 if Ekind (Ancestor) = E_Access_Type
12629 or else (not Is_Constrained (A_Gen_T)
12630 and then Is_Composite_Type (A_Gen_T))
12631 then
12632 Error_Msg_N ("actual subtype must be unconstrained", Actual);
12633 Abandon_Instantiation (Actual);
12634 end if;
12636 -- A class-wide type is only allowed if the formal has unknown
12637 -- discriminants.
12639 elsif Is_Class_Wide_Type (Act_T)
12640 and then not Has_Unknown_Discriminants (Ancestor)
12641 then
12642 Error_Msg_NE
12643 ("actual for & cannot be a class-wide type", Actual, Gen_T);
12644 Abandon_Instantiation (Actual);
12646 -- Otherwise, the formal and actual must have the same number
12647 -- of discriminants and each discriminant of the actual must
12648 -- correspond to a discriminant of the formal.
12650 elsif Has_Discriminants (Act_T)
12651 and then not Has_Unknown_Discriminants (Act_T)
12652 and then Has_Discriminants (Ancestor)
12653 then
12654 Actual_Discr := First_Discriminant (Act_T);
12655 Ancestor_Discr := First_Discriminant (Ancestor);
12656 while Present (Actual_Discr)
12657 and then Present (Ancestor_Discr)
12658 loop
12659 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
12660 No (Corresponding_Discriminant (Actual_Discr))
12661 then
12662 Error_Msg_NE
12663 ("discriminant & does not correspond "
12664 & "to ancestor discriminant", Actual, Actual_Discr);
12665 Abandon_Instantiation (Actual);
12666 end if;
12668 Next_Discriminant (Actual_Discr);
12669 Next_Discriminant (Ancestor_Discr);
12670 end loop;
12672 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
12673 Error_Msg_NE
12674 ("actual for & must have same number of discriminants",
12675 Actual, Gen_T);
12676 Abandon_Instantiation (Actual);
12677 end if;
12679 -- This case should be caught by the earlier check for
12680 -- constrainedness, but the check here is added for completeness.
12682 elsif Has_Discriminants (Act_T)
12683 and then not Has_Unknown_Discriminants (Act_T)
12684 then
12685 Error_Msg_NE
12686 ("actual for & must not have discriminants", Actual, Gen_T);
12687 Abandon_Instantiation (Actual);
12689 elsif Has_Discriminants (Ancestor) then
12690 Error_Msg_NE
12691 ("actual for & must have known discriminants", Actual, Gen_T);
12692 Abandon_Instantiation (Actual);
12693 end if;
12695 if not Subtypes_Statically_Compatible
12696 (Act_T, Ancestor, Formal_Derived_Matching => True)
12697 then
12698 Error_Msg_N
12699 ("constraint on actual is incompatible with formal", Actual);
12700 Abandon_Instantiation (Actual);
12701 end if;
12702 end if;
12704 -- If the formal and actual types are abstract, check that there
12705 -- are no abstract primitives of the actual type that correspond to
12706 -- nonabstract primitives of the formal type (second sentence of
12707 -- RM95 3.9.3(9)).
12709 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
12710 Check_Abstract_Primitives : declare
12711 Gen_Prims : constant Elist_Id :=
12712 Primitive_Operations (A_Gen_T);
12713 Gen_Elmt : Elmt_Id;
12714 Gen_Subp : Entity_Id;
12715 Anc_Subp : Entity_Id;
12716 Anc_Formal : Entity_Id;
12717 Anc_F_Type : Entity_Id;
12719 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
12720 Act_Elmt : Elmt_Id;
12721 Act_Subp : Entity_Id;
12722 Act_Formal : Entity_Id;
12723 Act_F_Type : Entity_Id;
12725 Subprograms_Correspond : Boolean;
12727 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
12728 -- Returns true if T2 is derived directly or indirectly from
12729 -- T1, including derivations from interfaces. T1 and T2 are
12730 -- required to be specific tagged base types.
12732 ------------------------
12733 -- Is_Tagged_Ancestor --
12734 ------------------------
12736 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
12738 Intfc_Elmt : Elmt_Id;
12740 begin
12741 -- The predicate is satisfied if the types are the same
12743 if T1 = T2 then
12744 return True;
12746 -- If we've reached the top of the derivation chain then
12747 -- we know that T1 is not an ancestor of T2.
12749 elsif Etype (T2) = T2 then
12750 return False;
12752 -- Proceed to check T2's immediate parent
12754 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
12755 return True;
12757 -- Finally, check to see if T1 is an ancestor of any of T2's
12758 -- progenitors.
12760 else
12761 Intfc_Elmt := First_Elmt (Interfaces (T2));
12762 while Present (Intfc_Elmt) loop
12763 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
12764 return True;
12765 end if;
12767 Next_Elmt (Intfc_Elmt);
12768 end loop;
12769 end if;
12771 return False;
12772 end Is_Tagged_Ancestor;
12774 -- Start of processing for Check_Abstract_Primitives
12776 begin
12777 -- Loop over all of the formal derived type's primitives
12779 Gen_Elmt := First_Elmt (Gen_Prims);
12780 while Present (Gen_Elmt) loop
12781 Gen_Subp := Node (Gen_Elmt);
12783 -- If the primitive of the formal is not abstract, then
12784 -- determine whether there is a corresponding primitive of
12785 -- the actual type that's abstract.
12787 if not Is_Abstract_Subprogram (Gen_Subp) then
12788 Act_Elmt := First_Elmt (Act_Prims);
12789 while Present (Act_Elmt) loop
12790 Act_Subp := Node (Act_Elmt);
12792 -- If we find an abstract primitive of the actual,
12793 -- then we need to test whether it corresponds to the
12794 -- subprogram from which the generic formal primitive
12795 -- is inherited.
12797 if Is_Abstract_Subprogram (Act_Subp) then
12798 Anc_Subp := Alias (Gen_Subp);
12800 -- Test whether we have a corresponding primitive
12801 -- by comparing names, kinds, formal types, and
12802 -- result types.
12804 if Chars (Anc_Subp) = Chars (Act_Subp)
12805 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
12806 then
12807 Anc_Formal := First_Formal (Anc_Subp);
12808 Act_Formal := First_Formal (Act_Subp);
12809 while Present (Anc_Formal)
12810 and then Present (Act_Formal)
12811 loop
12812 Anc_F_Type := Etype (Anc_Formal);
12813 Act_F_Type := Etype (Act_Formal);
12815 if Ekind (Anc_F_Type) =
12816 E_Anonymous_Access_Type
12817 then
12818 Anc_F_Type := Designated_Type (Anc_F_Type);
12820 if Ekind (Act_F_Type) =
12821 E_Anonymous_Access_Type
12822 then
12823 Act_F_Type :=
12824 Designated_Type (Act_F_Type);
12825 else
12826 exit;
12827 end if;
12829 elsif
12830 Ekind (Act_F_Type) = E_Anonymous_Access_Type
12831 then
12832 exit;
12833 end if;
12835 Anc_F_Type := Base_Type (Anc_F_Type);
12836 Act_F_Type := Base_Type (Act_F_Type);
12838 -- If the formal is controlling, then the
12839 -- the type of the actual primitive's formal
12840 -- must be derived directly or indirectly
12841 -- from the type of the ancestor primitive's
12842 -- formal.
12844 if Is_Controlling_Formal (Anc_Formal) then
12845 if not Is_Tagged_Ancestor
12846 (Anc_F_Type, Act_F_Type)
12847 then
12848 exit;
12849 end if;
12851 -- Otherwise the types of the formals must
12852 -- be the same.
12854 elsif Anc_F_Type /= Act_F_Type then
12855 exit;
12856 end if;
12858 Next_Entity (Anc_Formal);
12859 Next_Entity (Act_Formal);
12860 end loop;
12862 -- If we traversed through all of the formals
12863 -- then so far the subprograms correspond, so
12864 -- now check that any result types correspond.
12866 if No (Anc_Formal) and then No (Act_Formal) then
12867 Subprograms_Correspond := True;
12869 if Ekind (Act_Subp) = E_Function then
12870 Anc_F_Type := Etype (Anc_Subp);
12871 Act_F_Type := Etype (Act_Subp);
12873 if Ekind (Anc_F_Type) =
12874 E_Anonymous_Access_Type
12875 then
12876 Anc_F_Type :=
12877 Designated_Type (Anc_F_Type);
12879 if Ekind (Act_F_Type) =
12880 E_Anonymous_Access_Type
12881 then
12882 Act_F_Type :=
12883 Designated_Type (Act_F_Type);
12884 else
12885 Subprograms_Correspond := False;
12886 end if;
12888 elsif
12889 Ekind (Act_F_Type)
12890 = E_Anonymous_Access_Type
12891 then
12892 Subprograms_Correspond := False;
12893 end if;
12895 Anc_F_Type := Base_Type (Anc_F_Type);
12896 Act_F_Type := Base_Type (Act_F_Type);
12898 -- Now either the result types must be
12899 -- the same or, if the result type is
12900 -- controlling, the result type of the
12901 -- actual primitive must descend from the
12902 -- result type of the ancestor primitive.
12904 if Subprograms_Correspond
12905 and then Anc_F_Type /= Act_F_Type
12906 and then
12907 Has_Controlling_Result (Anc_Subp)
12908 and then not Is_Tagged_Ancestor
12909 (Anc_F_Type, Act_F_Type)
12910 then
12911 Subprograms_Correspond := False;
12912 end if;
12913 end if;
12915 -- Found a matching subprogram belonging to
12916 -- formal ancestor type, so actual subprogram
12917 -- corresponds and this violates 3.9.3(9).
12919 if Subprograms_Correspond then
12920 Error_Msg_NE
12921 ("abstract subprogram & overrides "
12922 & "nonabstract subprogram of ancestor",
12923 Actual, Act_Subp);
12924 end if;
12925 end if;
12926 end if;
12927 end if;
12929 Next_Elmt (Act_Elmt);
12930 end loop;
12931 end if;
12933 Next_Elmt (Gen_Elmt);
12934 end loop;
12935 end Check_Abstract_Primitives;
12936 end if;
12938 -- Verify that limitedness matches. If parent is a limited
12939 -- interface then the generic formal is not unless declared
12940 -- explicitly so. If not declared limited, the actual cannot be
12941 -- limited (see AI05-0087).
12943 -- Even though this AI is a binding interpretation, we enable the
12944 -- check only in Ada 2012 mode, because this improper construct
12945 -- shows up in user code and in existing B-tests.
12947 if Is_Limited_Type (Act_T)
12948 and then not Is_Limited_Type (A_Gen_T)
12949 and then Ada_Version >= Ada_2012
12950 then
12951 if In_Instance then
12952 null;
12953 else
12954 Error_Msg_NE
12955 ("actual for non-limited & cannot be a limited type",
12956 Actual, Gen_T);
12957 Explain_Limited_Type (Act_T, Actual);
12958 Abandon_Instantiation (Actual);
12959 end if;
12960 end if;
12961 end Validate_Derived_Type_Instance;
12963 ----------------------------------------
12964 -- Validate_Discriminated_Formal_Type --
12965 ----------------------------------------
12967 procedure Validate_Discriminated_Formal_Type is
12968 Formal_Discr : Entity_Id;
12969 Actual_Discr : Entity_Id;
12970 Formal_Subt : Entity_Id;
12972 begin
12973 if Has_Discriminants (A_Gen_T) then
12974 if not Has_Discriminants (Act_T) then
12975 Error_Msg_NE
12976 ("actual for & must have discriminants", Actual, Gen_T);
12977 Abandon_Instantiation (Actual);
12979 elsif Is_Constrained (Act_T) then
12980 Error_Msg_NE
12981 ("actual for & must be unconstrained", Actual, Gen_T);
12982 Abandon_Instantiation (Actual);
12984 else
12985 Formal_Discr := First_Discriminant (A_Gen_T);
12986 Actual_Discr := First_Discriminant (Act_T);
12987 while Formal_Discr /= Empty loop
12988 if Actual_Discr = Empty then
12989 Error_Msg_NE
12990 ("discriminants on actual do not match formal",
12991 Actual, Gen_T);
12992 Abandon_Instantiation (Actual);
12993 end if;
12995 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
12997 -- Access discriminants match if designated types do
12999 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
13000 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
13001 E_Anonymous_Access_Type
13002 and then
13003 Get_Instance_Of
13004 (Designated_Type (Base_Type (Formal_Subt))) =
13005 Designated_Type (Base_Type (Etype (Actual_Discr)))
13006 then
13007 null;
13009 elsif Base_Type (Formal_Subt) /=
13010 Base_Type (Etype (Actual_Discr))
13011 then
13012 Error_Msg_NE
13013 ("types of actual discriminants must match formal",
13014 Actual, Gen_T);
13015 Abandon_Instantiation (Actual);
13017 elsif not Subtypes_Statically_Match
13018 (Formal_Subt, Etype (Actual_Discr))
13019 and then Ada_Version >= Ada_95
13020 then
13021 Error_Msg_NE
13022 ("subtypes of actual discriminants must match formal",
13023 Actual, Gen_T);
13024 Abandon_Instantiation (Actual);
13025 end if;
13027 Next_Discriminant (Formal_Discr);
13028 Next_Discriminant (Actual_Discr);
13029 end loop;
13031 if Actual_Discr /= Empty then
13032 Error_Msg_NE
13033 ("discriminants on actual do not match formal",
13034 Actual, Gen_T);
13035 Abandon_Instantiation (Actual);
13036 end if;
13037 end if;
13038 end if;
13039 end Validate_Discriminated_Formal_Type;
13041 ---------------------------------------
13042 -- Validate_Incomplete_Type_Instance --
13043 ---------------------------------------
13045 procedure Validate_Incomplete_Type_Instance is
13046 begin
13047 if not Is_Tagged_Type (Act_T)
13048 and then Is_Tagged_Type (A_Gen_T)
13049 then
13050 Error_Msg_NE
13051 ("actual for & must be a tagged type", Actual, Gen_T);
13052 end if;
13054 Validate_Discriminated_Formal_Type;
13055 end Validate_Incomplete_Type_Instance;
13057 --------------------------------------
13058 -- Validate_Interface_Type_Instance --
13059 --------------------------------------
13061 procedure Validate_Interface_Type_Instance is
13062 begin
13063 if not Is_Interface (Act_T) then
13064 Error_Msg_NE
13065 ("actual for formal interface type must be an interface",
13066 Actual, Gen_T);
13068 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
13069 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
13070 or else Is_Protected_Interface (A_Gen_T) /=
13071 Is_Protected_Interface (Act_T)
13072 or else Is_Synchronized_Interface (A_Gen_T) /=
13073 Is_Synchronized_Interface (Act_T)
13074 then
13075 Error_Msg_NE
13076 ("actual for interface& does not match (RM 12.5.5(4))",
13077 Actual, Gen_T);
13078 end if;
13079 end Validate_Interface_Type_Instance;
13081 ------------------------------------
13082 -- Validate_Private_Type_Instance --
13083 ------------------------------------
13085 procedure Validate_Private_Type_Instance is
13086 begin
13087 if Is_Limited_Type (Act_T)
13088 and then not Is_Limited_Type (A_Gen_T)
13089 then
13090 if In_Instance then
13091 null;
13092 else
13093 Error_Msg_NE
13094 ("actual for non-limited & cannot be a limited type", Actual,
13095 Gen_T);
13096 Explain_Limited_Type (Act_T, Actual);
13097 Abandon_Instantiation (Actual);
13098 end if;
13100 elsif Known_To_Have_Preelab_Init (A_Gen_T)
13101 and then not Has_Preelaborable_Initialization (Act_T)
13102 then
13103 Error_Msg_NE
13104 ("actual for & must have preelaborable initialization", Actual,
13105 Gen_T);
13107 elsif not Is_Definite_Subtype (Act_T)
13108 and then Is_Definite_Subtype (A_Gen_T)
13109 and then Ada_Version >= Ada_95
13110 then
13111 Error_Msg_NE
13112 ("actual for & must be a definite subtype", Actual, Gen_T);
13114 elsif not Is_Tagged_Type (Act_T)
13115 and then Is_Tagged_Type (A_Gen_T)
13116 then
13117 Error_Msg_NE
13118 ("actual for & must be a tagged type", Actual, Gen_T);
13119 end if;
13121 Validate_Discriminated_Formal_Type;
13122 Ancestor := Gen_T;
13123 end Validate_Private_Type_Instance;
13125 -- Start of processing for Instantiate_Type
13127 begin
13128 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
13129 Error_Msg_N ("duplicate instantiation of generic type", Actual);
13130 return New_List (Error);
13132 elsif not Is_Entity_Name (Actual)
13133 or else not Is_Type (Entity (Actual))
13134 then
13135 Error_Msg_NE
13136 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
13137 Abandon_Instantiation (Actual);
13139 else
13140 Act_T := Entity (Actual);
13142 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
13143 -- as a generic actual parameter if the corresponding formal type
13144 -- does not have a known_discriminant_part, or is a formal derived
13145 -- type that is an Unchecked_Union type.
13147 if Is_Unchecked_Union (Base_Type (Act_T)) then
13148 if not Has_Discriminants (A_Gen_T)
13149 or else (Is_Derived_Type (A_Gen_T)
13150 and then Is_Unchecked_Union (A_Gen_T))
13151 then
13152 null;
13153 else
13154 Error_Msg_N ("unchecked union cannot be the actual for a "
13155 & "discriminated formal type", Act_T);
13157 end if;
13158 end if;
13160 -- Deal with fixed/floating restrictions
13162 if Is_Floating_Point_Type (Act_T) then
13163 Check_Restriction (No_Floating_Point, Actual);
13164 elsif Is_Fixed_Point_Type (Act_T) then
13165 Check_Restriction (No_Fixed_Point, Actual);
13166 end if;
13168 -- Deal with error of using incomplete type as generic actual.
13169 -- This includes limited views of a type, even if the non-limited
13170 -- view may be available.
13172 if Ekind (Act_T) = E_Incomplete_Type
13173 or else (Is_Class_Wide_Type (Act_T)
13174 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
13175 then
13176 -- If the formal is an incomplete type, the actual can be
13177 -- incomplete as well.
13179 if Ekind (A_Gen_T) = E_Incomplete_Type then
13180 null;
13182 elsif Is_Class_Wide_Type (Act_T)
13183 or else No (Full_View (Act_T))
13184 then
13185 Error_Msg_N ("premature use of incomplete type", Actual);
13186 Abandon_Instantiation (Actual);
13187 else
13188 Act_T := Full_View (Act_T);
13189 Set_Entity (Actual, Act_T);
13191 if Has_Private_Component (Act_T) then
13192 Error_Msg_N
13193 ("premature use of type with private component", Actual);
13194 end if;
13195 end if;
13197 -- Deal with error of premature use of private type as generic actual
13199 elsif Is_Private_Type (Act_T)
13200 and then Is_Private_Type (Base_Type (Act_T))
13201 and then not Is_Generic_Type (Act_T)
13202 and then not Is_Derived_Type (Act_T)
13203 and then No (Full_View (Root_Type (Act_T)))
13204 then
13205 -- If the formal is an incomplete type, the actual can be
13206 -- private or incomplete as well.
13208 if Ekind (A_Gen_T) = E_Incomplete_Type then
13209 null;
13210 else
13211 Error_Msg_N ("premature use of private type", Actual);
13212 end if;
13214 elsif Has_Private_Component (Act_T) then
13215 Error_Msg_N
13216 ("premature use of type with private component", Actual);
13217 end if;
13219 Set_Instance_Of (A_Gen_T, Act_T);
13221 -- If the type is generic, the class-wide type may also be used
13223 if Is_Tagged_Type (A_Gen_T)
13224 and then Is_Tagged_Type (Act_T)
13225 and then not Is_Class_Wide_Type (A_Gen_T)
13226 then
13227 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
13228 Class_Wide_Type (Act_T));
13229 end if;
13231 if not Is_Abstract_Type (A_Gen_T)
13232 and then Is_Abstract_Type (Act_T)
13233 then
13234 Error_Msg_N
13235 ("actual of non-abstract formal cannot be abstract", Actual);
13236 end if;
13238 -- A generic scalar type is a first subtype for which we generate
13239 -- an anonymous base type. Indicate that the instance of this base
13240 -- is the base type of the actual.
13242 if Is_Scalar_Type (A_Gen_T) then
13243 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
13244 end if;
13245 end if;
13247 if Error_Posted (Act_T) then
13248 null;
13249 else
13250 case Nkind (Def) is
13251 when N_Formal_Private_Type_Definition =>
13252 Validate_Private_Type_Instance;
13254 when N_Formal_Incomplete_Type_Definition =>
13255 Validate_Incomplete_Type_Instance;
13257 when N_Formal_Derived_Type_Definition =>
13258 Validate_Derived_Type_Instance;
13260 when N_Formal_Discrete_Type_Definition =>
13261 if not Is_Discrete_Type (Act_T) then
13262 Error_Msg_NE
13263 ("expect discrete type in instantiation of&",
13264 Actual, Gen_T);
13265 Abandon_Instantiation (Actual);
13266 end if;
13268 Diagnose_Predicated_Actual;
13270 when N_Formal_Signed_Integer_Type_Definition =>
13271 if not Is_Signed_Integer_Type (Act_T) then
13272 Error_Msg_NE
13273 ("expect signed integer type in instantiation of&",
13274 Actual, Gen_T);
13275 Abandon_Instantiation (Actual);
13276 end if;
13278 Diagnose_Predicated_Actual;
13280 when N_Formal_Modular_Type_Definition =>
13281 if not Is_Modular_Integer_Type (Act_T) then
13282 Error_Msg_NE
13283 ("expect modular type in instantiation of &",
13284 Actual, Gen_T);
13285 Abandon_Instantiation (Actual);
13286 end if;
13288 Diagnose_Predicated_Actual;
13290 when N_Formal_Floating_Point_Definition =>
13291 if not Is_Floating_Point_Type (Act_T) then
13292 Error_Msg_NE
13293 ("expect float type in instantiation of &", Actual, Gen_T);
13294 Abandon_Instantiation (Actual);
13295 end if;
13297 when N_Formal_Ordinary_Fixed_Point_Definition =>
13298 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
13299 Error_Msg_NE
13300 ("expect ordinary fixed point type in instantiation of &",
13301 Actual, Gen_T);
13302 Abandon_Instantiation (Actual);
13303 end if;
13305 when N_Formal_Decimal_Fixed_Point_Definition =>
13306 if not Is_Decimal_Fixed_Point_Type (Act_T) then
13307 Error_Msg_NE
13308 ("expect decimal type in instantiation of &",
13309 Actual, Gen_T);
13310 Abandon_Instantiation (Actual);
13311 end if;
13313 when N_Array_Type_Definition =>
13314 Validate_Array_Type_Instance;
13316 when N_Access_To_Object_Definition =>
13317 Validate_Access_Type_Instance;
13319 when N_Access_Function_Definition
13320 | N_Access_Procedure_Definition
13322 Validate_Access_Subprogram_Instance;
13324 when N_Record_Definition =>
13325 Validate_Interface_Type_Instance;
13327 when N_Derived_Type_Definition =>
13328 Validate_Derived_Interface_Type_Instance;
13330 when others =>
13331 raise Program_Error;
13332 end case;
13333 end if;
13335 Subt := New_Copy (Gen_T);
13337 -- Use adjusted sloc of subtype name as the location for other nodes in
13338 -- the subtype declaration.
13340 Loc := Sloc (Subt);
13342 Decl_Node :=
13343 Make_Subtype_Declaration (Loc,
13344 Defining_Identifier => Subt,
13345 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
13347 if Is_Private_Type (Act_T) then
13348 Set_Has_Private_View (Subtype_Indication (Decl_Node));
13350 elsif Is_Access_Type (Act_T)
13351 and then Is_Private_Type (Designated_Type (Act_T))
13352 then
13353 Set_Has_Private_View (Subtype_Indication (Decl_Node));
13354 end if;
13356 -- In Ada 2012 the actual may be a limited view. Indicate that
13357 -- the local subtype must be treated as such.
13359 if From_Limited_With (Act_T) then
13360 Set_Ekind (Subt, E_Incomplete_Subtype);
13361 Set_From_Limited_With (Subt);
13362 end if;
13364 Decl_Nodes := New_List (Decl_Node);
13366 -- Flag actual derived types so their elaboration produces the
13367 -- appropriate renamings for the primitive operations of the ancestor.
13368 -- Flag actual for formal private types as well, to determine whether
13369 -- operations in the private part may override inherited operations.
13370 -- If the formal has an interface list, the ancestor is not the
13371 -- parent, but the analyzed formal that includes the interface
13372 -- operations of all its progenitors.
13374 -- Same treatment for formal private types, so we can check whether the
13375 -- type is tagged limited when validating derivations in the private
13376 -- part. (See AI05-096).
13378 if Nkind (Def) = N_Formal_Derived_Type_Definition then
13379 if Present (Interface_List (Def)) then
13380 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
13381 else
13382 Set_Generic_Parent_Type (Decl_Node, Ancestor);
13383 end if;
13385 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
13386 N_Formal_Incomplete_Type_Definition)
13387 then
13388 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
13389 end if;
13391 -- If the actual is a synchronized type that implements an interface,
13392 -- the primitive operations are attached to the corresponding record,
13393 -- and we have to treat it as an additional generic actual, so that its
13394 -- primitive operations become visible in the instance. The task or
13395 -- protected type itself does not carry primitive operations.
13397 if Is_Concurrent_Type (Act_T)
13398 and then Is_Tagged_Type (Act_T)
13399 and then Present (Corresponding_Record_Type (Act_T))
13400 and then Present (Ancestor)
13401 and then Is_Interface (Ancestor)
13402 then
13403 declare
13404 Corr_Rec : constant Entity_Id :=
13405 Corresponding_Record_Type (Act_T);
13406 New_Corr : Entity_Id;
13407 Corr_Decl : Node_Id;
13409 begin
13410 New_Corr := Make_Temporary (Loc, 'S');
13411 Corr_Decl :=
13412 Make_Subtype_Declaration (Loc,
13413 Defining_Identifier => New_Corr,
13414 Subtype_Indication =>
13415 New_Occurrence_Of (Corr_Rec, Loc));
13416 Append_To (Decl_Nodes, Corr_Decl);
13418 if Ekind (Act_T) = E_Task_Type then
13419 Set_Ekind (Subt, E_Task_Subtype);
13420 else
13421 Set_Ekind (Subt, E_Protected_Subtype);
13422 end if;
13424 Set_Corresponding_Record_Type (Subt, Corr_Rec);
13425 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
13426 Set_Generic_Parent_Type (Decl_Node, Empty);
13427 end;
13428 end if;
13430 -- For a floating-point type, capture dimension info if any, because
13431 -- the generated subtype declaration does not come from source and
13432 -- will not process dimensions.
13434 if Is_Floating_Point_Type (Act_T) then
13435 Copy_Dimensions (Act_T, Subt);
13436 end if;
13438 return Decl_Nodes;
13439 end Instantiate_Type;
13441 ---------------------
13442 -- Is_In_Main_Unit --
13443 ---------------------
13445 function Is_In_Main_Unit (N : Node_Id) return Boolean is
13446 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
13447 Current_Unit : Node_Id;
13449 begin
13450 if Unum = Main_Unit then
13451 return True;
13453 -- If the current unit is a subunit then it is either the main unit or
13454 -- is being compiled as part of the main unit.
13456 elsif Nkind (N) = N_Compilation_Unit then
13457 return Nkind (Unit (N)) = N_Subunit;
13458 end if;
13460 Current_Unit := Parent (N);
13461 while Present (Current_Unit)
13462 and then Nkind (Current_Unit) /= N_Compilation_Unit
13463 loop
13464 Current_Unit := Parent (Current_Unit);
13465 end loop;
13467 -- The instantiation node is in the main unit, or else the current node
13468 -- (perhaps as the result of nested instantiations) is in the main unit,
13469 -- or in the declaration of the main unit, which in this last case must
13470 -- be a body.
13472 return
13473 Current_Unit = Cunit (Main_Unit)
13474 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
13475 or else (Present (Current_Unit)
13476 and then Present (Library_Unit (Current_Unit))
13477 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
13478 end Is_In_Main_Unit;
13480 ----------------------------
13481 -- Load_Parent_Of_Generic --
13482 ----------------------------
13484 procedure Load_Parent_Of_Generic
13485 (N : Node_Id;
13486 Spec : Node_Id;
13487 Body_Optional : Boolean := False)
13489 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
13490 Saved_Style_Check : constant Boolean := Style_Check;
13491 Saved_Warnings : constant Warning_Record := Save_Warnings;
13492 True_Parent : Node_Id;
13493 Inst_Node : Node_Id;
13494 OK : Boolean;
13495 Previous_Instances : constant Elist_Id := New_Elmt_List;
13497 procedure Collect_Previous_Instances (Decls : List_Id);
13498 -- Collect all instantiations in the given list of declarations, that
13499 -- precede the generic that we need to load. If the bodies of these
13500 -- instantiations are available, we must analyze them, to ensure that
13501 -- the public symbols generated are the same when the unit is compiled
13502 -- to generate code, and when it is compiled in the context of a unit
13503 -- that needs a particular nested instance. This process is applied to
13504 -- both package and subprogram instances.
13506 --------------------------------
13507 -- Collect_Previous_Instances --
13508 --------------------------------
13510 procedure Collect_Previous_Instances (Decls : List_Id) is
13511 Decl : Node_Id;
13513 begin
13514 Decl := First (Decls);
13515 while Present (Decl) loop
13516 if Sloc (Decl) >= Sloc (Inst_Node) then
13517 return;
13519 -- If Decl is an instantiation, then record it as requiring
13520 -- instantiation of the corresponding body, except if it is an
13521 -- abbreviated instantiation generated internally for conformance
13522 -- checking purposes only for the case of a formal package
13523 -- declared without a box (see Instantiate_Formal_Package). Such
13524 -- an instantiation does not generate any code (the actual code
13525 -- comes from actual) and thus does not need to be analyzed here.
13526 -- If the instantiation appears with a generic package body it is
13527 -- not analyzed here either.
13529 elsif Nkind (Decl) = N_Package_Instantiation
13530 and then not Is_Internal (Defining_Entity (Decl))
13531 then
13532 Append_Elmt (Decl, Previous_Instances);
13534 -- For a subprogram instantiation, omit instantiations intrinsic
13535 -- operations (Unchecked_Conversions, etc.) that have no bodies.
13537 elsif Nkind_In (Decl, N_Function_Instantiation,
13538 N_Procedure_Instantiation)
13539 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
13540 then
13541 Append_Elmt (Decl, Previous_Instances);
13543 elsif Nkind (Decl) = N_Package_Declaration then
13544 Collect_Previous_Instances
13545 (Visible_Declarations (Specification (Decl)));
13546 Collect_Previous_Instances
13547 (Private_Declarations (Specification (Decl)));
13549 -- Previous non-generic bodies may contain instances as well
13551 elsif Nkind (Decl) = N_Package_Body
13552 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
13553 then
13554 Collect_Previous_Instances (Declarations (Decl));
13556 elsif Nkind (Decl) = N_Subprogram_Body
13557 and then not Acts_As_Spec (Decl)
13558 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
13559 then
13560 Collect_Previous_Instances (Declarations (Decl));
13561 end if;
13563 Next (Decl);
13564 end loop;
13565 end Collect_Previous_Instances;
13567 -- Start of processing for Load_Parent_Of_Generic
13569 begin
13570 if not In_Same_Source_Unit (N, Spec)
13571 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
13572 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
13573 and then not Is_In_Main_Unit (Spec))
13574 then
13575 -- Find body of parent of spec, and analyze it. A special case arises
13576 -- when the parent is an instantiation, that is to say when we are
13577 -- currently instantiating a nested generic. In that case, there is
13578 -- no separate file for the body of the enclosing instance. Instead,
13579 -- the enclosing body must be instantiated as if it were a pending
13580 -- instantiation, in order to produce the body for the nested generic
13581 -- we require now. Note that in that case the generic may be defined
13582 -- in a package body, the instance defined in the same package body,
13583 -- and the original enclosing body may not be in the main unit.
13585 Inst_Node := Empty;
13587 True_Parent := Parent (Spec);
13588 while Present (True_Parent)
13589 and then Nkind (True_Parent) /= N_Compilation_Unit
13590 loop
13591 if Nkind (True_Parent) = N_Package_Declaration
13592 and then
13593 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
13594 then
13595 -- Parent is a compilation unit that is an instantiation.
13596 -- Instantiation node has been replaced with package decl.
13598 Inst_Node := Original_Node (True_Parent);
13599 exit;
13601 elsif Nkind (True_Parent) = N_Package_Declaration
13602 and then Present (Generic_Parent (Specification (True_Parent)))
13603 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13604 then
13605 -- Parent is an instantiation within another specification.
13606 -- Declaration for instance has been inserted before original
13607 -- instantiation node. A direct link would be preferable?
13609 Inst_Node := Next (True_Parent);
13610 while Present (Inst_Node)
13611 and then Nkind (Inst_Node) /= N_Package_Instantiation
13612 loop
13613 Next (Inst_Node);
13614 end loop;
13616 -- If the instance appears within a generic, and the generic
13617 -- unit is defined within a formal package of the enclosing
13618 -- generic, there is no generic body available, and none
13619 -- needed. A more precise test should be used ???
13621 if No (Inst_Node) then
13622 return;
13623 end if;
13625 exit;
13627 else
13628 True_Parent := Parent (True_Parent);
13629 end if;
13630 end loop;
13632 -- Case where we are currently instantiating a nested generic
13634 if Present (Inst_Node) then
13635 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
13637 -- Instantiation node and declaration of instantiated package
13638 -- were exchanged when only the declaration was needed.
13639 -- Restore instantiation node before proceeding with body.
13641 Set_Unit (Parent (True_Parent), Inst_Node);
13642 end if;
13644 -- Now complete instantiation of enclosing body, if it appears in
13645 -- some other unit. If it appears in the current unit, the body
13646 -- will have been instantiated already.
13648 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
13650 -- We need to determine the expander mode to instantiate the
13651 -- enclosing body. Because the generic body we need may use
13652 -- global entities declared in the enclosing package (including
13653 -- aggregates) it is in general necessary to compile this body
13654 -- with expansion enabled, except if we are within a generic
13655 -- package, in which case the usual generic rule applies.
13657 declare
13658 Exp_Status : Boolean := True;
13659 Scop : Entity_Id;
13661 begin
13662 -- Loop through scopes looking for generic package
13664 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
13665 while Present (Scop)
13666 and then Scop /= Standard_Standard
13667 loop
13668 if Ekind (Scop) = E_Generic_Package then
13669 Exp_Status := False;
13670 exit;
13671 end if;
13673 Scop := Scope (Scop);
13674 end loop;
13676 -- Collect previous instantiations in the unit that contains
13677 -- the desired generic.
13679 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13680 and then not Body_Optional
13681 then
13682 declare
13683 Decl : Elmt_Id;
13684 Info : Pending_Body_Info;
13685 Par : Node_Id;
13687 begin
13688 Par := Parent (Inst_Node);
13689 while Present (Par) loop
13690 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
13691 Par := Parent (Par);
13692 end loop;
13694 pragma Assert (Present (Par));
13696 if Nkind (Par) = N_Package_Body then
13697 Collect_Previous_Instances (Declarations (Par));
13699 elsif Nkind (Par) = N_Package_Declaration then
13700 Collect_Previous_Instances
13701 (Visible_Declarations (Specification (Par)));
13702 Collect_Previous_Instances
13703 (Private_Declarations (Specification (Par)));
13705 else
13706 -- Enclosing unit is a subprogram body. In this
13707 -- case all instance bodies are processed in order
13708 -- and there is no need to collect them separately.
13710 null;
13711 end if;
13713 Decl := First_Elmt (Previous_Instances);
13714 while Present (Decl) loop
13715 Info :=
13716 (Act_Decl =>
13717 Instance_Spec (Node (Decl)),
13718 Config_Switches => Save_Config_Switches,
13719 Current_Sem_Unit =>
13720 Get_Code_Unit (Sloc (Node (Decl))),
13721 Expander_Status => Exp_Status,
13722 Inst_Node => Node (Decl),
13723 Local_Suppress_Stack_Top =>
13724 Local_Suppress_Stack_Top,
13725 Scope_Suppress => Scope_Suppress,
13726 Warnings => Save_Warnings);
13728 -- Package instance
13730 if Nkind (Node (Decl)) = N_Package_Instantiation
13731 then
13732 Instantiate_Package_Body
13733 (Info, Body_Optional => True);
13735 -- Subprogram instance
13737 else
13738 -- The instance_spec is in the wrapper package,
13739 -- usually followed by its local renaming
13740 -- declaration. See Build_Subprogram_Renaming
13741 -- for details. If the instance carries aspects,
13742 -- these result in the corresponding pragmas,
13743 -- inserted after the subprogram declaration.
13744 -- They must be skipped as well when retrieving
13745 -- the desired spec. Some of them may have been
13746 -- rewritten as null statements.
13747 -- A direct link would be more robust ???
13749 declare
13750 Decl : Node_Id :=
13751 (Last (Visible_Declarations
13752 (Specification (Info.Act_Decl))));
13753 begin
13754 while Nkind_In (Decl,
13755 N_Null_Statement,
13756 N_Pragma,
13757 N_Subprogram_Renaming_Declaration)
13758 loop
13759 Decl := Prev (Decl);
13760 end loop;
13762 Info.Act_Decl := Decl;
13763 end;
13765 Instantiate_Subprogram_Body
13766 (Info, Body_Optional => True);
13767 end if;
13769 Next_Elmt (Decl);
13770 end loop;
13771 end;
13772 end if;
13774 Instantiate_Package_Body
13775 (Body_Info =>
13776 ((Act_Decl => True_Parent,
13777 Config_Switches => Save_Config_Switches,
13778 Current_Sem_Unit =>
13779 Get_Code_Unit (Sloc (Inst_Node)),
13780 Expander_Status => Exp_Status,
13781 Inst_Node => Inst_Node,
13782 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
13783 Scope_Suppress => Scope_Suppress,
13784 Warnings => Save_Warnings)),
13785 Body_Optional => Body_Optional);
13786 end;
13787 end if;
13789 -- Case where we are not instantiating a nested generic
13791 else
13792 Opt.Style_Check := False;
13793 Expander_Mode_Save_And_Set (True);
13794 Load_Needed_Body (Comp_Unit, OK);
13795 Opt.Style_Check := Saved_Style_Check;
13796 Restore_Warnings (Saved_Warnings);
13797 Expander_Mode_Restore;
13799 if not OK
13800 and then Unit_Requires_Body (Defining_Entity (Spec))
13801 and then not Body_Optional
13802 then
13803 declare
13804 Bname : constant Unit_Name_Type :=
13805 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
13807 begin
13808 -- In CodePeer mode, the missing body may make the analysis
13809 -- incomplete, but we do not treat it as fatal.
13811 if CodePeer_Mode then
13812 return;
13814 else
13815 Error_Msg_Unit_1 := Bname;
13816 Error_Msg_N ("this instantiation requires$!", N);
13817 Error_Msg_File_1 :=
13818 Get_File_Name (Bname, Subunit => False);
13819 Error_Msg_N ("\but file{ was not found!", N);
13820 raise Unrecoverable_Error;
13821 end if;
13822 end;
13823 end if;
13824 end if;
13825 end if;
13827 -- If loading parent of the generic caused an instantiation circularity,
13828 -- we abandon compilation at this point, because otherwise in some cases
13829 -- we get into trouble with infinite recursions after this point.
13831 if Circularity_Detected then
13832 raise Unrecoverable_Error;
13833 end if;
13834 end Load_Parent_Of_Generic;
13836 ---------------------------------
13837 -- Map_Formal_Package_Entities --
13838 ---------------------------------
13840 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
13841 E1 : Entity_Id;
13842 E2 : Entity_Id;
13844 begin
13845 Set_Instance_Of (Form, Act);
13847 -- Traverse formal and actual package to map the corresponding entities.
13848 -- We skip over internal entities that may be generated during semantic
13849 -- analysis, and find the matching entities by name, given that they
13850 -- must appear in the same order.
13852 E1 := First_Entity (Form);
13853 E2 := First_Entity (Act);
13854 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
13855 -- Could this test be a single condition??? Seems like it could, and
13856 -- isn't FPE (Form) a constant anyway???
13858 if not Is_Internal (E1)
13859 and then Present (Parent (E1))
13860 and then not Is_Class_Wide_Type (E1)
13861 and then not Is_Internal_Name (Chars (E1))
13862 then
13863 while Present (E2) and then Chars (E2) /= Chars (E1) loop
13864 Next_Entity (E2);
13865 end loop;
13867 if No (E2) then
13868 exit;
13869 else
13870 Set_Instance_Of (E1, E2);
13872 if Is_Type (E1) and then Is_Tagged_Type (E2) then
13873 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
13874 end if;
13876 if Is_Constrained (E1) then
13877 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
13878 end if;
13880 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
13881 Map_Formal_Package_Entities (E1, E2);
13882 end if;
13883 end if;
13884 end if;
13886 Next_Entity (E1);
13887 end loop;
13888 end Map_Formal_Package_Entities;
13890 -----------------------
13891 -- Move_Freeze_Nodes --
13892 -----------------------
13894 procedure Move_Freeze_Nodes
13895 (Out_Of : Entity_Id;
13896 After : Node_Id;
13897 L : List_Id)
13899 Decl : Node_Id;
13900 Next_Decl : Node_Id;
13901 Next_Node : Node_Id := After;
13902 Spec : Node_Id;
13904 function Is_Outer_Type (T : Entity_Id) return Boolean;
13905 -- Check whether entity is declared in a scope external to that of the
13906 -- generic unit.
13908 -------------------
13909 -- Is_Outer_Type --
13910 -------------------
13912 function Is_Outer_Type (T : Entity_Id) return Boolean is
13913 Scop : Entity_Id := Scope (T);
13915 begin
13916 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
13917 return True;
13919 else
13920 while Scop /= Standard_Standard loop
13921 if Scop = Out_Of then
13922 return False;
13923 else
13924 Scop := Scope (Scop);
13925 end if;
13926 end loop;
13928 return True;
13929 end if;
13930 end Is_Outer_Type;
13932 -- Start of processing for Move_Freeze_Nodes
13934 begin
13935 if No (L) then
13936 return;
13937 end if;
13939 -- First remove the freeze nodes that may appear before all other
13940 -- declarations.
13942 Decl := First (L);
13943 while Present (Decl)
13944 and then Nkind (Decl) = N_Freeze_Entity
13945 and then Is_Outer_Type (Entity (Decl))
13946 loop
13947 Decl := Remove_Head (L);
13948 Insert_After (Next_Node, Decl);
13949 Set_Analyzed (Decl, False);
13950 Next_Node := Decl;
13951 Decl := First (L);
13952 end loop;
13954 -- Next scan the list of declarations and remove each freeze node that
13955 -- appears ahead of the current node.
13957 while Present (Decl) loop
13958 while Present (Next (Decl))
13959 and then Nkind (Next (Decl)) = N_Freeze_Entity
13960 and then Is_Outer_Type (Entity (Next (Decl)))
13961 loop
13962 Next_Decl := Remove_Next (Decl);
13963 Insert_After (Next_Node, Next_Decl);
13964 Set_Analyzed (Next_Decl, False);
13965 Next_Node := Next_Decl;
13966 end loop;
13968 -- If the declaration is a nested package or concurrent type, then
13969 -- recurse. Nested generic packages will have been processed from the
13970 -- inside out.
13972 case Nkind (Decl) is
13973 when N_Package_Declaration =>
13974 Spec := Specification (Decl);
13976 when N_Task_Type_Declaration =>
13977 Spec := Task_Definition (Decl);
13979 when N_Protected_Type_Declaration =>
13980 Spec := Protected_Definition (Decl);
13982 when others =>
13983 Spec := Empty;
13984 end case;
13986 if Present (Spec) then
13987 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
13988 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
13989 end if;
13991 Next (Decl);
13992 end loop;
13993 end Move_Freeze_Nodes;
13995 ----------------
13996 -- Next_Assoc --
13997 ----------------
13999 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
14000 begin
14001 return Generic_Renamings.Table (E).Next_In_HTable;
14002 end Next_Assoc;
14004 ------------------------
14005 -- Preanalyze_Actuals --
14006 ------------------------
14008 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
14009 Assoc : Node_Id;
14010 Act : Node_Id;
14011 Errs : constant Nat := Serious_Errors_Detected;
14013 Cur : Entity_Id := Empty;
14014 -- Current homograph of the instance name
14016 Vis : Boolean := False;
14017 -- Saved visibility status of the current homograph
14019 begin
14020 Assoc := First (Generic_Associations (N));
14022 -- If the instance is a child unit, its name may hide an outer homonym,
14023 -- so make it invisible to perform name resolution on the actuals.
14025 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
14026 and then Present
14027 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
14028 then
14029 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
14031 if Is_Compilation_Unit (Cur) then
14032 Vis := Is_Immediately_Visible (Cur);
14033 Set_Is_Immediately_Visible (Cur, False);
14034 else
14035 Cur := Empty;
14036 end if;
14037 end if;
14039 while Present (Assoc) loop
14040 if Nkind (Assoc) /= N_Others_Choice then
14041 Act := Explicit_Generic_Actual_Parameter (Assoc);
14043 -- Within a nested instantiation, a defaulted actual is an empty
14044 -- association, so nothing to analyze. If the subprogram actual
14045 -- is an attribute, analyze prefix only, because actual is not a
14046 -- complete attribute reference.
14048 -- If actual is an allocator, analyze expression only. The full
14049 -- analysis can generate code, and if instance is a compilation
14050 -- unit we have to wait until the package instance is installed
14051 -- to have a proper place to insert this code.
14053 -- String literals may be operators, but at this point we do not
14054 -- know whether the actual is a formal subprogram or a string.
14056 if No (Act) then
14057 null;
14059 elsif Nkind (Act) = N_Attribute_Reference then
14060 Analyze (Prefix (Act));
14062 elsif Nkind (Act) = N_Explicit_Dereference then
14063 Analyze (Prefix (Act));
14065 elsif Nkind (Act) = N_Allocator then
14066 declare
14067 Expr : constant Node_Id := Expression (Act);
14069 begin
14070 if Nkind (Expr) = N_Subtype_Indication then
14071 Analyze (Subtype_Mark (Expr));
14073 -- Analyze separately each discriminant constraint, when
14074 -- given with a named association.
14076 declare
14077 Constr : Node_Id;
14079 begin
14080 Constr := First (Constraints (Constraint (Expr)));
14081 while Present (Constr) loop
14082 if Nkind (Constr) = N_Discriminant_Association then
14083 Analyze (Expression (Constr));
14084 else
14085 Analyze (Constr);
14086 end if;
14088 Next (Constr);
14089 end loop;
14090 end;
14092 else
14093 Analyze (Expr);
14094 end if;
14095 end;
14097 elsif Nkind (Act) /= N_Operator_Symbol then
14098 Analyze (Act);
14100 -- Within a package instance, mark actuals that are limited
14101 -- views, so their use can be moved to the body of the
14102 -- enclosing unit.
14104 if Is_Entity_Name (Act)
14105 and then Is_Type (Entity (Act))
14106 and then From_Limited_With (Entity (Act))
14107 and then Present (Inst)
14108 then
14109 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
14110 end if;
14111 end if;
14113 if Errs /= Serious_Errors_Detected then
14115 -- Do a minimal analysis of the generic, to prevent spurious
14116 -- warnings complaining about the generic being unreferenced,
14117 -- before abandoning the instantiation.
14119 Analyze (Name (N));
14121 if Is_Entity_Name (Name (N))
14122 and then Etype (Name (N)) /= Any_Type
14123 then
14124 Generate_Reference (Entity (Name (N)), Name (N));
14125 Set_Is_Instantiated (Entity (Name (N)));
14126 end if;
14128 if Present (Cur) then
14130 -- For the case of a child instance hiding an outer homonym,
14131 -- provide additional warning which might explain the error.
14133 Set_Is_Immediately_Visible (Cur, Vis);
14134 Error_Msg_NE
14135 ("& hides outer unit with the same name??",
14136 N, Defining_Unit_Name (N));
14137 end if;
14139 Abandon_Instantiation (Act);
14140 end if;
14141 end if;
14143 Next (Assoc);
14144 end loop;
14146 if Present (Cur) then
14147 Set_Is_Immediately_Visible (Cur, Vis);
14148 end if;
14149 end Preanalyze_Actuals;
14151 -------------------------------
14152 -- Provide_Completing_Bodies --
14153 -------------------------------
14155 procedure Provide_Completing_Bodies (N : Node_Id) is
14156 procedure Build_Completing_Body (Subp_Decl : Node_Id);
14157 -- Generate the completing body for subprogram declaration Subp_Decl
14159 procedure Provide_Completing_Bodies_In (Decls : List_Id);
14160 -- Generating completing bodies for all subprograms found in declarative
14161 -- list Decls.
14163 ---------------------------
14164 -- Build_Completing_Body --
14165 ---------------------------
14167 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
14168 Loc : constant Source_Ptr := Sloc (Subp_Decl);
14169 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
14170 Spec : Node_Id;
14172 begin
14173 -- Nothing to do if the subprogram already has a completing body
14175 if Present (Corresponding_Body (Subp_Decl)) then
14176 return;
14178 -- Mark the function as having a valid return statement even though
14179 -- the body contains a single raise statement.
14181 elsif Ekind (Subp_Id) = E_Function then
14182 Set_Return_Present (Subp_Id);
14183 end if;
14185 -- Clone the specification to obtain new entities and reset the only
14186 -- semantic field.
14188 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
14189 Set_Generic_Parent (Spec, Empty);
14191 -- Generate:
14192 -- function Func ... return ... is
14193 -- <or>
14194 -- procedure Proc ... is
14195 -- begin
14196 -- raise Program_Error with "access before elaboration";
14197 -- edn Proc;
14199 Insert_After_And_Analyze (Subp_Decl,
14200 Make_Subprogram_Body (Loc,
14201 Specification => Spec,
14202 Declarations => New_List,
14203 Handled_Statement_Sequence =>
14204 Make_Handled_Sequence_Of_Statements (Loc,
14205 Statements => New_List (
14206 Make_Raise_Program_Error (Loc,
14207 Reason => PE_Access_Before_Elaboration)))));
14208 end Build_Completing_Body;
14210 ----------------------------------
14211 -- Provide_Completing_Bodies_In --
14212 ----------------------------------
14214 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
14215 Decl : Node_Id;
14217 begin
14218 if Present (Decls) then
14219 Decl := First (Decls);
14220 while Present (Decl) loop
14221 Provide_Completing_Bodies (Decl);
14222 Next (Decl);
14223 end loop;
14224 end if;
14225 end Provide_Completing_Bodies_In;
14227 -- Local variables
14229 Spec : Node_Id;
14231 -- Start of processing for Provide_Completing_Bodies
14233 begin
14234 if Nkind (N) = N_Package_Declaration then
14235 Spec := Specification (N);
14237 Push_Scope (Defining_Entity (N));
14238 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
14239 Provide_Completing_Bodies_In (Private_Declarations (Spec));
14240 Pop_Scope;
14242 elsif Nkind (N) = N_Subprogram_Declaration then
14243 Build_Completing_Body (N);
14244 end if;
14245 end Provide_Completing_Bodies;
14247 -------------------
14248 -- Remove_Parent --
14249 -------------------
14251 procedure Remove_Parent (In_Body : Boolean := False) is
14252 S : Entity_Id := Current_Scope;
14253 -- S is the scope containing the instantiation just completed. The scope
14254 -- stack contains the parent instances of the instantiation, followed by
14255 -- the original S.
14257 Cur_P : Entity_Id;
14258 E : Entity_Id;
14259 P : Entity_Id;
14260 Hidden : Elmt_Id;
14262 begin
14263 -- After child instantiation is complete, remove from scope stack the
14264 -- extra copy of the current scope, and then remove parent instances.
14266 if not In_Body then
14267 Pop_Scope;
14269 while Current_Scope /= S loop
14270 P := Current_Scope;
14271 End_Package_Scope (Current_Scope);
14273 if In_Open_Scopes (P) then
14274 E := First_Entity (P);
14275 while Present (E) loop
14276 Set_Is_Immediately_Visible (E, True);
14277 Next_Entity (E);
14278 end loop;
14280 -- If instantiation is declared in a block, it is the enclosing
14281 -- scope that might be a parent instance. Note that only one
14282 -- block can be involved, because the parent instances have
14283 -- been installed within it.
14285 if Ekind (P) = E_Block then
14286 Cur_P := Scope (P);
14287 else
14288 Cur_P := P;
14289 end if;
14291 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
14292 -- We are within an instance of some sibling. Retain
14293 -- visibility of parent, for proper subsequent cleanup, and
14294 -- reinstall private declarations as well.
14296 Set_In_Private_Part (P);
14297 Install_Private_Declarations (P);
14298 end if;
14300 -- If the ultimate parent is a top-level unit recorded in
14301 -- Instance_Parent_Unit, then reset its visibility to what it was
14302 -- before instantiation. (It's not clear what the purpose is of
14303 -- testing whether Scope (P) is In_Open_Scopes, but that test was
14304 -- present before the ultimate parent test was added.???)
14306 elsif not In_Open_Scopes (Scope (P))
14307 or else (P = Instance_Parent_Unit
14308 and then not Parent_Unit_Visible)
14309 then
14310 Set_Is_Immediately_Visible (P, False);
14312 -- If the current scope is itself an instantiation of a generic
14313 -- nested within P, and we are in the private part of body of this
14314 -- instantiation, restore the full views of P, that were removed
14315 -- in End_Package_Scope above. This obscure case can occur when a
14316 -- subunit of a generic contains an instance of a child unit of
14317 -- its generic parent unit.
14319 elsif S = Current_Scope and then Is_Generic_Instance (S) then
14320 declare
14321 Par : constant Entity_Id :=
14322 Generic_Parent (Package_Specification (S));
14323 begin
14324 if Present (Par)
14325 and then P = Scope (Par)
14326 and then (In_Package_Body (S) or else In_Private_Part (S))
14327 then
14328 Set_In_Private_Part (P);
14329 Install_Private_Declarations (P);
14330 end if;
14331 end;
14332 end if;
14333 end loop;
14335 -- Reset visibility of entities in the enclosing scope
14337 Set_Is_Hidden_Open_Scope (Current_Scope, False);
14339 Hidden := First_Elmt (Hidden_Entities);
14340 while Present (Hidden) loop
14341 Set_Is_Immediately_Visible (Node (Hidden), True);
14342 Next_Elmt (Hidden);
14343 end loop;
14345 else
14346 -- Each body is analyzed separately, and there is no context that
14347 -- needs preserving from one body instance to the next, so remove all
14348 -- parent scopes that have been installed.
14350 while Present (S) loop
14351 End_Package_Scope (S);
14352 Set_Is_Immediately_Visible (S, False);
14353 S := Current_Scope;
14354 exit when S = Standard_Standard;
14355 end loop;
14356 end if;
14357 end Remove_Parent;
14359 -----------------
14360 -- Restore_Env --
14361 -----------------
14363 procedure Restore_Env is
14364 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
14366 begin
14367 if No (Current_Instantiated_Parent.Act_Id) then
14368 -- Restore environment after subprogram inlining
14370 Restore_Private_Views (Empty);
14371 end if;
14373 Current_Instantiated_Parent := Saved.Instantiated_Parent;
14374 Exchanged_Views := Saved.Exchanged_Views;
14375 Hidden_Entities := Saved.Hidden_Entities;
14376 Current_Sem_Unit := Saved.Current_Sem_Unit;
14377 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
14378 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
14380 Restore_Config_Switches (Saved.Switches);
14382 Instance_Envs.Decrement_Last;
14383 end Restore_Env;
14385 ---------------------------
14386 -- Restore_Private_Views --
14387 ---------------------------
14389 procedure Restore_Private_Views
14390 (Pack_Id : Entity_Id;
14391 Is_Package : Boolean := True)
14393 M : Elmt_Id;
14394 E : Entity_Id;
14395 Typ : Entity_Id;
14396 Dep_Elmt : Elmt_Id;
14397 Dep_Typ : Node_Id;
14399 procedure Restore_Nested_Formal (Formal : Entity_Id);
14400 -- Hide the generic formals of formal packages declared with box which
14401 -- were reachable in the current instantiation.
14403 ---------------------------
14404 -- Restore_Nested_Formal --
14405 ---------------------------
14407 procedure Restore_Nested_Formal (Formal : Entity_Id) is
14408 Ent : Entity_Id;
14410 begin
14411 if Present (Renamed_Object (Formal))
14412 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
14413 then
14414 return;
14416 elsif Present (Associated_Formal_Package (Formal)) then
14417 Ent := First_Entity (Formal);
14418 while Present (Ent) loop
14419 exit when Ekind (Ent) = E_Package
14420 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
14422 Set_Is_Hidden (Ent);
14423 Set_Is_Potentially_Use_Visible (Ent, False);
14425 -- If package, then recurse
14427 if Ekind (Ent) = E_Package then
14428 Restore_Nested_Formal (Ent);
14429 end if;
14431 Next_Entity (Ent);
14432 end loop;
14433 end if;
14434 end Restore_Nested_Formal;
14436 -- Start of processing for Restore_Private_Views
14438 begin
14439 M := First_Elmt (Exchanged_Views);
14440 while Present (M) loop
14441 Typ := Node (M);
14443 -- Subtypes of types whose views have been exchanged, and that are
14444 -- defined within the instance, were not on the Private_Dependents
14445 -- list on entry to the instance, so they have to be exchanged
14446 -- explicitly now, in order to remain consistent with the view of the
14447 -- parent type.
14449 if Ekind_In (Typ, E_Private_Type,
14450 E_Limited_Private_Type,
14451 E_Record_Type_With_Private)
14452 then
14453 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
14454 while Present (Dep_Elmt) loop
14455 Dep_Typ := Node (Dep_Elmt);
14457 if Scope (Dep_Typ) = Pack_Id
14458 and then Present (Full_View (Dep_Typ))
14459 then
14460 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
14461 Exchange_Declarations (Dep_Typ);
14462 end if;
14464 Next_Elmt (Dep_Elmt);
14465 end loop;
14466 end if;
14468 Exchange_Declarations (Node (M));
14469 Next_Elmt (M);
14470 end loop;
14472 if No (Pack_Id) then
14473 return;
14474 end if;
14476 -- Make the generic formal parameters private, and make the formal types
14477 -- into subtypes of the actuals again.
14479 E := First_Entity (Pack_Id);
14480 while Present (E) loop
14481 Set_Is_Hidden (E, True);
14483 if Is_Type (E)
14484 and then Nkind (Parent (E)) = N_Subtype_Declaration
14485 then
14486 -- If the actual for E is itself a generic actual type from
14487 -- an enclosing instance, E is still a generic actual type
14488 -- outside of the current instance. This matter when resolving
14489 -- an overloaded call that may be ambiguous in the enclosing
14490 -- instance, when two of its actuals coincide.
14492 if Is_Entity_Name (Subtype_Indication (Parent (E)))
14493 and then Is_Generic_Actual_Type
14494 (Entity (Subtype_Indication (Parent (E))))
14495 then
14496 null;
14497 else
14498 Set_Is_Generic_Actual_Type (E, False);
14499 end if;
14501 -- An unusual case of aliasing: the actual may also be directly
14502 -- visible in the generic, and be private there, while it is fully
14503 -- visible in the context of the instance. The internal subtype
14504 -- is private in the instance but has full visibility like its
14505 -- parent in the enclosing scope. This enforces the invariant that
14506 -- the privacy status of all private dependents of a type coincide
14507 -- with that of the parent type. This can only happen when a
14508 -- generic child unit is instantiated within a sibling.
14510 if Is_Private_Type (E)
14511 and then not Is_Private_Type (Etype (E))
14512 then
14513 Exchange_Declarations (E);
14514 end if;
14516 elsif Ekind (E) = E_Package then
14518 -- The end of the renaming list is the renaming of the generic
14519 -- package itself. If the instance is a subprogram, all entities
14520 -- in the corresponding package are renamings. If this entity is
14521 -- a formal package, make its own formals private as well. The
14522 -- actual in this case is itself the renaming of an instantiation.
14523 -- If the entity is not a package renaming, it is the entity
14524 -- created to validate formal package actuals: ignore it.
14526 -- If the actual is itself a formal package for the enclosing
14527 -- generic, or the actual for such a formal package, it remains
14528 -- visible on exit from the instance, and therefore nothing needs
14529 -- to be done either, except to keep it accessible.
14531 if Is_Package and then Renamed_Object (E) = Pack_Id then
14532 exit;
14534 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
14535 null;
14537 elsif
14538 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
14539 then
14540 Set_Is_Hidden (E, False);
14542 else
14543 declare
14544 Act_P : constant Entity_Id := Renamed_Object (E);
14545 Id : Entity_Id;
14547 begin
14548 Id := First_Entity (Act_P);
14549 while Present (Id)
14550 and then Id /= First_Private_Entity (Act_P)
14551 loop
14552 exit when Ekind (Id) = E_Package
14553 and then Renamed_Object (Id) = Act_P;
14555 Set_Is_Hidden (Id, True);
14556 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
14558 if Ekind (Id) = E_Package then
14559 Restore_Nested_Formal (Id);
14560 end if;
14562 Next_Entity (Id);
14563 end loop;
14564 end;
14565 end if;
14566 end if;
14568 Next_Entity (E);
14569 end loop;
14570 end Restore_Private_Views;
14572 --------------
14573 -- Save_Env --
14574 --------------
14576 procedure Save_Env
14577 (Gen_Unit : Entity_Id;
14578 Act_Unit : Entity_Id)
14580 begin
14581 Init_Env;
14582 Set_Instance_Env (Gen_Unit, Act_Unit);
14583 end Save_Env;
14585 ----------------------------
14586 -- Save_Global_References --
14587 ----------------------------
14589 procedure Save_Global_References (Templ : Node_Id) is
14591 -- ??? it is horrible to use global variables in highly recursive code
14593 E : Entity_Id;
14594 -- The entity of the current associated node
14596 Gen_Scope : Entity_Id;
14597 -- The scope of the generic for which references are being saved
14599 N2 : Node_Id;
14600 -- The current associated node
14602 function Is_Global (E : Entity_Id) return Boolean;
14603 -- Check whether entity is defined outside of generic unit. Examine the
14604 -- scope of an entity, and the scope of the scope, etc, until we find
14605 -- either Standard, in which case the entity is global, or the generic
14606 -- unit itself, which indicates that the entity is local. If the entity
14607 -- is the generic unit itself, as in the case of a recursive call, or
14608 -- the enclosing generic unit, if different from the current scope, then
14609 -- it is local as well, because it will be replaced at the point of
14610 -- instantiation. On the other hand, if it is a reference to a child
14611 -- unit of a common ancestor, which appears in an instantiation, it is
14612 -- global because it is used to denote a specific compilation unit at
14613 -- the time the instantiations will be analyzed.
14615 procedure Qualify_Universal_Operands
14616 (Op : Node_Id;
14617 Func_Call : Node_Id);
14618 -- Op denotes a binary or unary operator in generic template Templ. Node
14619 -- Func_Call is the function call alternative of the operator within the
14620 -- the analyzed copy of the template. Change each operand which yields a
14621 -- universal type by wrapping it into a qualified expression
14623 -- Actual_Typ'(Operand)
14625 -- where Actual_Typ is the type of corresponding actual parameter of
14626 -- Operand in Func_Call.
14628 procedure Reset_Entity (N : Node_Id);
14629 -- Save semantic information on global entity so that it is not resolved
14630 -- again at instantiation time.
14632 procedure Save_Entity_Descendants (N : Node_Id);
14633 -- Apply Save_Global_References to the two syntactic descendants of
14634 -- non-terminal nodes that carry an Associated_Node and are processed
14635 -- through Reset_Entity. Once the global entity (if any) has been
14636 -- captured together with its type, only two syntactic descendants need
14637 -- to be traversed to complete the processing of the tree rooted at N.
14638 -- This applies to Selected_Components, Expanded_Names, and to Operator
14639 -- nodes. N can also be a character literal, identifier, or operator
14640 -- symbol node, but the call has no effect in these cases.
14642 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
14643 -- Default actuals in nested instances must be handled specially
14644 -- because there is no link to them from the original tree. When an
14645 -- actual subprogram is given by a default, we add an explicit generic
14646 -- association for it in the instantiation node. When we save the
14647 -- global references on the name of the instance, we recover the list
14648 -- of generic associations, and add an explicit one to the original
14649 -- generic tree, through which a global actual can be preserved.
14650 -- Similarly, if a child unit is instantiated within a sibling, in the
14651 -- context of the parent, we must preserve the identifier of the parent
14652 -- so that it can be properly resolved in a subsequent instantiation.
14654 procedure Save_Global_Descendant (D : Union_Id);
14655 -- Apply Save_References recursively to the descendants of node D
14657 procedure Save_References (N : Node_Id);
14658 -- This is the recursive procedure that does the work, once the
14659 -- enclosing generic scope has been established.
14661 ---------------
14662 -- Is_Global --
14663 ---------------
14665 function Is_Global (E : Entity_Id) return Boolean is
14666 Se : Entity_Id;
14668 function Is_Instance_Node (Decl : Node_Id) return Boolean;
14669 -- Determine whether the parent node of a reference to a child unit
14670 -- denotes an instantiation or a formal package, in which case the
14671 -- reference to the child unit is global, even if it appears within
14672 -- the current scope (e.g. when the instance appears within the body
14673 -- of an ancestor).
14675 ----------------------
14676 -- Is_Instance_Node --
14677 ----------------------
14679 function Is_Instance_Node (Decl : Node_Id) return Boolean is
14680 begin
14681 return Nkind (Decl) in N_Generic_Instantiation
14682 or else
14683 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
14684 end Is_Instance_Node;
14686 -- Start of processing for Is_Global
14688 begin
14689 if E = Gen_Scope then
14690 return False;
14692 elsif E = Standard_Standard then
14693 return True;
14695 elsif Is_Child_Unit (E)
14696 and then (Is_Instance_Node (Parent (N2))
14697 or else (Nkind (Parent (N2)) = N_Expanded_Name
14698 and then N2 = Selector_Name (Parent (N2))
14699 and then
14700 Is_Instance_Node (Parent (Parent (N2)))))
14701 then
14702 return True;
14704 else
14705 Se := Scope (E);
14706 while Se /= Gen_Scope loop
14707 if Se = Standard_Standard then
14708 return True;
14709 else
14710 Se := Scope (Se);
14711 end if;
14712 end loop;
14714 return False;
14715 end if;
14716 end Is_Global;
14718 --------------------------------
14719 -- Qualify_Universal_Operands --
14720 --------------------------------
14722 procedure Qualify_Universal_Operands
14723 (Op : Node_Id;
14724 Func_Call : Node_Id)
14726 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
14727 -- Rewrite operand Opnd as a qualified expression of the form
14729 -- Actual_Typ'(Opnd)
14731 -- where Actual is the corresponding actual parameter of Opnd in
14732 -- function call Func_Call.
14734 function Qualify_Type
14735 (Loc : Source_Ptr;
14736 Typ : Entity_Id) return Node_Id;
14737 -- Qualify type Typ by creating a selected component of the form
14739 -- Scope_Of_Typ.Typ
14741 ---------------------
14742 -- Qualify_Operand --
14743 ---------------------
14745 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
14746 Loc : constant Source_Ptr := Sloc (Opnd);
14747 Typ : constant Entity_Id := Etype (Actual);
14748 Mark : Node_Id;
14749 Qual : Node_Id;
14751 begin
14752 -- Qualify the operand when it is of a universal type. Note that
14753 -- the template is unanalyzed and it is not possible to directly
14754 -- query the type. This transformation is not done when the type
14755 -- of the actual is internally generated because the type will be
14756 -- regenerated in the instance.
14758 if Yields_Universal_Type (Opnd)
14759 and then Comes_From_Source (Typ)
14760 and then not Is_Hidden (Typ)
14761 then
14762 -- The type of the actual may be a global reference. Save this
14763 -- information by creating a reference to it.
14765 if Is_Global (Typ) then
14766 Mark := New_Occurrence_Of (Typ, Loc);
14768 -- Otherwise rely on resolution to find the proper type within
14769 -- the instance.
14771 else
14772 Mark := Qualify_Type (Loc, Typ);
14773 end if;
14775 Qual :=
14776 Make_Qualified_Expression (Loc,
14777 Subtype_Mark => Mark,
14778 Expression => Relocate_Node (Opnd));
14780 -- Mark the qualification to distinguish it from other source
14781 -- constructs and signal the instantiation mechanism that this
14782 -- node requires special processing. See Copy_Generic_Node for
14783 -- details.
14785 Set_Is_Qualified_Universal_Literal (Qual);
14787 Rewrite (Opnd, Qual);
14788 end if;
14789 end Qualify_Operand;
14791 ------------------
14792 -- Qualify_Type --
14793 ------------------
14795 function Qualify_Type
14796 (Loc : Source_Ptr;
14797 Typ : Entity_Id) return Node_Id
14799 Scop : constant Entity_Id := Scope (Typ);
14800 Result : Node_Id;
14802 begin
14803 Result := Make_Identifier (Loc, Chars (Typ));
14805 if Present (Scop) and then not Is_Generic_Unit (Scop) then
14806 Result :=
14807 Make_Selected_Component (Loc,
14808 Prefix => Make_Identifier (Loc, Chars (Scop)),
14809 Selector_Name => Result);
14810 end if;
14812 return Result;
14813 end Qualify_Type;
14815 -- Local variables
14817 Actuals : constant List_Id := Parameter_Associations (Func_Call);
14819 -- Start of processing for Qualify_Universal_Operands
14821 begin
14822 if Nkind (Op) in N_Binary_Op then
14823 Qualify_Operand (Left_Opnd (Op), First (Actuals));
14824 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
14826 elsif Nkind (Op) in N_Unary_Op then
14827 Qualify_Operand (Right_Opnd (Op), First (Actuals));
14828 end if;
14829 end Qualify_Universal_Operands;
14831 ------------------
14832 -- Reset_Entity --
14833 ------------------
14835 procedure Reset_Entity (N : Node_Id) is
14836 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
14837 -- If the type of N2 is global to the generic unit, save the type in
14838 -- the generic node. Just as we perform name capture for explicit
14839 -- references within the generic, we must capture the global types
14840 -- of local entities because they may participate in resolution in
14841 -- the instance.
14843 function Top_Ancestor (E : Entity_Id) return Entity_Id;
14844 -- Find the ultimate ancestor of the current unit. If it is not a
14845 -- generic unit, then the name of the current unit in the prefix of
14846 -- an expanded name must be replaced with its generic homonym to
14847 -- ensure that it will be properly resolved in an instance.
14849 ---------------------
14850 -- Set_Global_Type --
14851 ---------------------
14853 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
14854 Typ : constant Entity_Id := Etype (N2);
14856 begin
14857 Set_Etype (N, Typ);
14859 -- If the entity of N is not the associated node, this is a
14860 -- nested generic and it has an associated node as well, whose
14861 -- type is already the full view (see below). Indicate that the
14862 -- original node has a private view.
14864 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
14865 Set_Has_Private_View (N);
14866 end if;
14868 -- If not a private type, nothing else to do
14870 if not Is_Private_Type (Typ) then
14871 if Is_Array_Type (Typ)
14872 and then Is_Private_Type (Component_Type (Typ))
14873 then
14874 Set_Has_Private_View (N);
14875 end if;
14877 -- If it is a derivation of a private type in a context where no
14878 -- full view is needed, nothing to do either.
14880 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
14881 null;
14883 -- Otherwise mark the type for flipping and use the full view when
14884 -- available.
14886 else
14887 Set_Has_Private_View (N);
14889 if Present (Full_View (Typ)) then
14890 Set_Etype (N2, Full_View (Typ));
14891 end if;
14892 end if;
14894 if Is_Floating_Point_Type (Typ)
14895 and then Has_Dimension_System (Typ)
14896 then
14897 Copy_Dimensions (N2, N);
14898 end if;
14899 end Set_Global_Type;
14901 ------------------
14902 -- Top_Ancestor --
14903 ------------------
14905 function Top_Ancestor (E : Entity_Id) return Entity_Id is
14906 Par : Entity_Id;
14908 begin
14909 Par := E;
14910 while Is_Child_Unit (Par) loop
14911 Par := Scope (Par);
14912 end loop;
14914 return Par;
14915 end Top_Ancestor;
14917 -- Start of processing for Reset_Entity
14919 begin
14920 N2 := Get_Associated_Node (N);
14921 E := Entity (N2);
14923 if Present (E) then
14925 -- If the node is an entry call to an entry in an enclosing task,
14926 -- it is rewritten as a selected component. No global entity to
14927 -- preserve in this case, since the expansion will be redone in
14928 -- the instance.
14930 if not Nkind_In (E, N_Defining_Character_Literal,
14931 N_Defining_Identifier,
14932 N_Defining_Operator_Symbol)
14933 then
14934 Set_Associated_Node (N, Empty);
14935 Set_Etype (N, Empty);
14936 return;
14937 end if;
14939 -- If the entity is an itype created as a subtype of an access
14940 -- type with a null exclusion restore source entity for proper
14941 -- visibility. The itype will be created anew in the instance.
14943 if Is_Itype (E)
14944 and then Ekind (E) = E_Access_Subtype
14945 and then Is_Entity_Name (N)
14946 and then Chars (Etype (E)) = Chars (N)
14947 then
14948 E := Etype (E);
14949 Set_Entity (N2, E);
14950 Set_Etype (N2, E);
14951 end if;
14953 if Is_Global (E) then
14955 -- If the entity is a package renaming that is the prefix of
14956 -- an expanded name, it has been rewritten as the renamed
14957 -- package, which is necessary semantically but complicates
14958 -- ASIS tree traversal, so we recover the original entity to
14959 -- expose the renaming. Take into account that the context may
14960 -- be a nested generic, that the original node may itself have
14961 -- an associated node that had better be an entity, and that
14962 -- the current node is still a selected component.
14964 if Ekind (E) = E_Package
14965 and then Nkind (N) = N_Selected_Component
14966 and then Nkind (Parent (N)) = N_Expanded_Name
14967 and then Present (Original_Node (N2))
14968 and then Is_Entity_Name (Original_Node (N2))
14969 and then Present (Entity (Original_Node (N2)))
14970 then
14971 if Is_Global (Entity (Original_Node (N2))) then
14972 N2 := Original_Node (N2);
14973 Set_Associated_Node (N, N2);
14974 Set_Global_Type (N, N2);
14976 -- Renaming is local, and will be resolved in instance
14978 else
14979 Set_Associated_Node (N, Empty);
14980 Set_Etype (N, Empty);
14981 end if;
14983 else
14984 Set_Global_Type (N, N2);
14985 end if;
14987 elsif Nkind (N) = N_Op_Concat
14988 and then Is_Generic_Type (Etype (N2))
14989 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
14990 or else
14991 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
14992 and then Is_Intrinsic_Subprogram (E)
14993 then
14994 null;
14996 -- Entity is local. Mark generic node as unresolved. Note that now
14997 -- it does not have an entity.
14999 else
15000 Set_Associated_Node (N, Empty);
15001 Set_Etype (N, Empty);
15002 end if;
15004 if Nkind (Parent (N)) in N_Generic_Instantiation
15005 and then N = Name (Parent (N))
15006 then
15007 Save_Global_Defaults (Parent (N), Parent (N2));
15008 end if;
15010 elsif Nkind (Parent (N)) = N_Selected_Component
15011 and then Nkind (Parent (N2)) = N_Expanded_Name
15012 then
15013 if Is_Global (Entity (Parent (N2))) then
15014 Change_Selected_Component_To_Expanded_Name (Parent (N));
15015 Set_Associated_Node (Parent (N), Parent (N2));
15016 Set_Global_Type (Parent (N), Parent (N2));
15017 Save_Entity_Descendants (N);
15019 -- If this is a reference to the current generic entity, replace
15020 -- by the name of the generic homonym of the current package. This
15021 -- is because in an instantiation Par.P.Q will not resolve to the
15022 -- name of the instance, whose enclosing scope is not necessarily
15023 -- Par. We use the generic homonym rather that the name of the
15024 -- generic itself because it may be hidden by a local declaration.
15026 elsif In_Open_Scopes (Entity (Parent (N2)))
15027 and then not
15028 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
15029 then
15030 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
15031 Rewrite (Parent (N),
15032 Make_Identifier (Sloc (N),
15033 Chars =>
15034 Chars (Generic_Homonym (Entity (Parent (N2))))));
15035 else
15036 Rewrite (Parent (N),
15037 Make_Identifier (Sloc (N),
15038 Chars => Chars (Selector_Name (Parent (N2)))));
15039 end if;
15040 end if;
15042 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
15043 and then Parent (N) = Name (Parent (Parent (N)))
15044 then
15045 Save_Global_Defaults
15046 (Parent (Parent (N)), Parent (Parent (N2)));
15047 end if;
15049 -- A selected component may denote a static constant that has been
15050 -- folded. If the static constant is global to the generic, capture
15051 -- its value. Otherwise the folding will happen in any instantiation.
15053 elsif Nkind (Parent (N)) = N_Selected_Component
15054 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
15055 then
15056 if Present (Entity (Original_Node (Parent (N2))))
15057 and then Is_Global (Entity (Original_Node (Parent (N2))))
15058 then
15059 Rewrite (Parent (N), New_Copy (Parent (N2)));
15060 Set_Analyzed (Parent (N), False);
15061 end if;
15063 -- A selected component may be transformed into a parameterless
15064 -- function call. If the called entity is global, rewrite the node
15065 -- appropriately, i.e. as an extended name for the global entity.
15067 elsif Nkind (Parent (N)) = N_Selected_Component
15068 and then Nkind (Parent (N2)) = N_Function_Call
15069 and then N = Selector_Name (Parent (N))
15070 then
15071 if No (Parameter_Associations (Parent (N2))) then
15072 if Is_Global (Entity (Name (Parent (N2)))) then
15073 Change_Selected_Component_To_Expanded_Name (Parent (N));
15074 Set_Associated_Node (Parent (N), Name (Parent (N2)));
15075 Set_Global_Type (Parent (N), Name (Parent (N2)));
15076 Save_Entity_Descendants (N);
15078 else
15079 Set_Is_Prefixed_Call (Parent (N));
15080 Set_Associated_Node (N, Empty);
15081 Set_Etype (N, Empty);
15082 end if;
15084 -- In Ada 2005, X.F may be a call to a primitive operation,
15085 -- rewritten as F (X). This rewriting will be done again in an
15086 -- instance, so keep the original node. Global entities will be
15087 -- captured as for other constructs. Indicate that this must
15088 -- resolve as a call, to prevent accidental overloading in the
15089 -- instance, if both a component and a primitive operation appear
15090 -- as candidates.
15092 else
15093 Set_Is_Prefixed_Call (Parent (N));
15094 end if;
15096 -- Entity is local. Reset in generic unit, so that node is resolved
15097 -- anew at the point of instantiation.
15099 else
15100 Set_Associated_Node (N, Empty);
15101 Set_Etype (N, Empty);
15102 end if;
15103 end Reset_Entity;
15105 -----------------------------
15106 -- Save_Entity_Descendants --
15107 -----------------------------
15109 procedure Save_Entity_Descendants (N : Node_Id) is
15110 begin
15111 case Nkind (N) is
15112 when N_Binary_Op =>
15113 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
15114 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
15116 when N_Unary_Op =>
15117 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
15119 when N_Expanded_Name
15120 | N_Selected_Component
15122 Save_Global_Descendant (Union_Id (Prefix (N)));
15123 Save_Global_Descendant (Union_Id (Selector_Name (N)));
15125 when N_Character_Literal
15126 | N_Identifier
15127 | N_Operator_Symbol
15129 null;
15131 when others =>
15132 raise Program_Error;
15133 end case;
15134 end Save_Entity_Descendants;
15136 --------------------------
15137 -- Save_Global_Defaults --
15138 --------------------------
15140 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
15141 Loc : constant Source_Ptr := Sloc (N1);
15142 Assoc2 : constant List_Id := Generic_Associations (N2);
15143 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
15144 Assoc1 : List_Id;
15145 Act1 : Node_Id;
15146 Act2 : Node_Id;
15147 Def : Node_Id;
15148 Ndec : Node_Id;
15149 Subp : Entity_Id;
15150 Actual : Entity_Id;
15152 begin
15153 Assoc1 := Generic_Associations (N1);
15155 if Present (Assoc1) then
15156 Act1 := First (Assoc1);
15157 else
15158 Act1 := Empty;
15159 Set_Generic_Associations (N1, New_List);
15160 Assoc1 := Generic_Associations (N1);
15161 end if;
15163 if Present (Assoc2) then
15164 Act2 := First (Assoc2);
15165 else
15166 return;
15167 end if;
15169 while Present (Act1) and then Present (Act2) loop
15170 Next (Act1);
15171 Next (Act2);
15172 end loop;
15174 -- Find the associations added for default subprograms
15176 if Present (Act2) then
15177 while Nkind (Act2) /= N_Generic_Association
15178 or else No (Entity (Selector_Name (Act2)))
15179 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
15180 loop
15181 Next (Act2);
15182 end loop;
15184 -- Add a similar association if the default is global. The
15185 -- renaming declaration for the actual has been analyzed, and
15186 -- its alias is the program it renames. Link the actual in the
15187 -- original generic tree with the node in the analyzed tree.
15189 while Present (Act2) loop
15190 Subp := Entity (Selector_Name (Act2));
15191 Def := Explicit_Generic_Actual_Parameter (Act2);
15193 -- Following test is defence against rubbish errors
15195 if No (Alias (Subp)) then
15196 return;
15197 end if;
15199 -- Retrieve the resolved actual from the renaming declaration
15200 -- created for the instantiated formal.
15202 Actual := Entity (Name (Parent (Parent (Subp))));
15203 Set_Entity (Def, Actual);
15204 Set_Etype (Def, Etype (Actual));
15206 if Is_Global (Actual) then
15207 Ndec :=
15208 Make_Generic_Association (Loc,
15209 Selector_Name =>
15210 New_Occurrence_Of (Subp, Loc),
15211 Explicit_Generic_Actual_Parameter =>
15212 New_Occurrence_Of (Actual, Loc));
15214 Set_Associated_Node
15215 (Explicit_Generic_Actual_Parameter (Ndec), Def);
15217 Append (Ndec, Assoc1);
15219 -- If there are other defaults, add a dummy association in case
15220 -- there are other defaulted formals with the same name.
15222 elsif Present (Next (Act2)) then
15223 Ndec :=
15224 Make_Generic_Association (Loc,
15225 Selector_Name =>
15226 New_Occurrence_Of (Subp, Loc),
15227 Explicit_Generic_Actual_Parameter => Empty);
15229 Append (Ndec, Assoc1);
15230 end if;
15232 Next (Act2);
15233 end loop;
15234 end if;
15236 if Nkind (Name (N1)) = N_Identifier
15237 and then Is_Child_Unit (Gen_Id)
15238 and then Is_Global (Gen_Id)
15239 and then Is_Generic_Unit (Scope (Gen_Id))
15240 and then In_Open_Scopes (Scope (Gen_Id))
15241 then
15242 -- This is an instantiation of a child unit within a sibling, so
15243 -- that the generic parent is in scope. An eventual instance must
15244 -- occur within the scope of an instance of the parent. Make name
15245 -- in instance into an expanded name, to preserve the identifier
15246 -- of the parent, so it can be resolved subsequently.
15248 Rewrite (Name (N2),
15249 Make_Expanded_Name (Loc,
15250 Chars => Chars (Gen_Id),
15251 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
15252 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
15253 Set_Entity (Name (N2), Gen_Id);
15255 Rewrite (Name (N1),
15256 Make_Expanded_Name (Loc,
15257 Chars => Chars (Gen_Id),
15258 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
15259 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
15261 Set_Associated_Node (Name (N1), Name (N2));
15262 Set_Associated_Node (Prefix (Name (N1)), Empty);
15263 Set_Associated_Node
15264 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
15265 Set_Etype (Name (N1), Etype (Gen_Id));
15266 end if;
15267 end Save_Global_Defaults;
15269 ----------------------------
15270 -- Save_Global_Descendant --
15271 ----------------------------
15273 procedure Save_Global_Descendant (D : Union_Id) is
15274 N1 : Node_Id;
15276 begin
15277 if D in Node_Range then
15278 if D = Union_Id (Empty) then
15279 null;
15281 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
15282 Save_References (Node_Id (D));
15283 end if;
15285 elsif D in List_Range then
15286 pragma Assert (D /= Union_Id (No_List));
15287 -- Because No_List = Empty, which is in Node_Range above
15289 if Is_Empty_List (List_Id (D)) then
15290 null;
15292 else
15293 N1 := First (List_Id (D));
15294 while Present (N1) loop
15295 Save_References (N1);
15296 Next (N1);
15297 end loop;
15298 end if;
15300 -- Element list or other non-node field, nothing to do
15302 else
15303 null;
15304 end if;
15305 end Save_Global_Descendant;
15307 ---------------------
15308 -- Save_References --
15309 ---------------------
15311 -- This is the recursive procedure that does the work once the enclosing
15312 -- generic scope has been established. We have to treat specially a
15313 -- number of node rewritings that are required by semantic processing
15314 -- and which change the kind of nodes in the generic copy: typically
15315 -- constant-folding, replacing an operator node by a string literal, or
15316 -- a selected component by an expanded name. In each of those cases, the
15317 -- transformation is propagated to the generic unit.
15319 procedure Save_References (N : Node_Id) is
15320 Loc : constant Source_Ptr := Sloc (N);
15322 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
15323 -- Determine whether arbitrary node Nod requires delayed capture of
15324 -- global references within its aspect specifications.
15326 procedure Save_References_In_Aggregate (N : Node_Id);
15327 -- Save all global references in [extension] aggregate node N
15329 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
15330 -- Save all global references in a character literal or operator
15331 -- symbol denoted by N.
15333 procedure Save_References_In_Descendants (N : Node_Id);
15334 -- Save all global references in all descendants of node N
15336 procedure Save_References_In_Identifier (N : Node_Id);
15337 -- Save all global references in identifier node N
15339 procedure Save_References_In_Operator (N : Node_Id);
15340 -- Save all global references in operator node N
15342 procedure Save_References_In_Pragma (Prag : Node_Id);
15343 -- Save all global references found within the expression of pragma
15344 -- Prag.
15346 ---------------------------
15347 -- Requires_Delayed_Save --
15348 ---------------------------
15350 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
15351 begin
15352 -- Generic packages and subprograms require delayed capture of
15353 -- global references within their aspects due to the timing of
15354 -- annotation analysis.
15356 if Nkind_In (Nod, N_Generic_Package_Declaration,
15357 N_Generic_Subprogram_Declaration,
15358 N_Package_Body,
15359 N_Package_Body_Stub,
15360 N_Subprogram_Body,
15361 N_Subprogram_Body_Stub)
15362 then
15363 -- Since the capture of global references is done on the
15364 -- unanalyzed generic template, there is no information around
15365 -- to infer the context. Use the Associated_Entity linkages to
15366 -- peek into the analyzed generic copy and determine what the
15367 -- template corresponds to.
15369 if Nod = Templ then
15370 return
15371 Is_Generic_Declaration_Or_Body
15372 (Unit_Declaration_Node
15373 (Associated_Entity (Defining_Entity (Nod))));
15375 -- Otherwise the generic unit being processed is not the top
15376 -- level template. It is safe to capture of global references
15377 -- within the generic unit because at this point the top level
15378 -- copy is fully analyzed.
15380 else
15381 return False;
15382 end if;
15384 -- Otherwise capture the global references without interference
15386 else
15387 return False;
15388 end if;
15389 end Requires_Delayed_Save;
15391 ----------------------------------
15392 -- Save_References_In_Aggregate --
15393 ----------------------------------
15395 procedure Save_References_In_Aggregate (N : Node_Id) is
15396 Nam : Node_Id;
15397 Qual : Node_Id := Empty;
15398 Typ : Entity_Id := Empty;
15400 use Atree.Unchecked_Access;
15401 -- This code section is part of implementing an untyped tree
15402 -- traversal, so it needs direct access to node fields.
15404 begin
15405 N2 := Get_Associated_Node (N);
15407 if Present (N2) then
15408 Typ := Etype (N2);
15410 -- In an instance within a generic, use the name of the actual
15411 -- and not the original generic parameter. If the actual is
15412 -- global in the current generic it must be preserved for its
15413 -- instantiation.
15415 if Nkind (Parent (Typ)) = N_Subtype_Declaration
15416 and then Present (Generic_Parent_Type (Parent (Typ)))
15417 then
15418 Typ := Base_Type (Typ);
15419 Set_Etype (N2, Typ);
15420 end if;
15421 end if;
15423 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
15424 Set_Associated_Node (N, Empty);
15426 -- If the aggregate is an actual in a call, it has been
15427 -- resolved in the current context, to some local type. The
15428 -- enclosing call may have been disambiguated by the aggregate,
15429 -- and this disambiguation might fail at instantiation time
15430 -- because the type to which the aggregate did resolve is not
15431 -- preserved. In order to preserve some of this information,
15432 -- wrap the aggregate in a qualified expression, using the id
15433 -- of its type. For further disambiguation we qualify the type
15434 -- name with its scope (if visible and not hidden by a local
15435 -- homograph) because both id's will have corresponding
15436 -- entities in an instance. This resolves most of the problems
15437 -- with missing type information on aggregates in instances.
15439 if Present (N2)
15440 and then Nkind (N2) = Nkind (N)
15441 and then Nkind (Parent (N2)) in N_Subprogram_Call
15442 and then Present (Typ)
15443 and then Comes_From_Source (Typ)
15444 then
15445 Nam := Make_Identifier (Loc, Chars (Typ));
15447 if Is_Immediately_Visible (Scope (Typ))
15448 and then
15449 (not In_Open_Scopes (Scope (Typ))
15450 or else Current_Entity (Scope (Typ)) = Scope (Typ))
15451 then
15452 Nam :=
15453 Make_Selected_Component (Loc,
15454 Prefix =>
15455 Make_Identifier (Loc, Chars (Scope (Typ))),
15456 Selector_Name => Nam);
15457 end if;
15459 Qual :=
15460 Make_Qualified_Expression (Loc,
15461 Subtype_Mark => Nam,
15462 Expression => Relocate_Node (N));
15463 end if;
15464 end if;
15466 Save_Global_Descendant (Field1 (N));
15467 Save_Global_Descendant (Field2 (N));
15468 Save_Global_Descendant (Field3 (N));
15469 Save_Global_Descendant (Field5 (N));
15471 if Present (Qual) then
15472 Rewrite (N, Qual);
15473 end if;
15474 end Save_References_In_Aggregate;
15476 ----------------------------------------------
15477 -- Save_References_In_Char_Lit_Or_Op_Symbol --
15478 ----------------------------------------------
15480 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
15481 begin
15482 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15483 Reset_Entity (N);
15485 elsif Nkind (N) = N_Operator_Symbol
15486 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
15487 then
15488 Change_Operator_Symbol_To_String_Literal (N);
15489 end if;
15490 end Save_References_In_Char_Lit_Or_Op_Symbol;
15492 ------------------------------------
15493 -- Save_References_In_Descendants --
15494 ------------------------------------
15496 procedure Save_References_In_Descendants (N : Node_Id) is
15497 use Atree.Unchecked_Access;
15498 -- This code section is part of implementing an untyped tree
15499 -- traversal, so it needs direct access to node fields.
15501 begin
15502 Save_Global_Descendant (Field1 (N));
15503 Save_Global_Descendant (Field2 (N));
15504 Save_Global_Descendant (Field3 (N));
15505 Save_Global_Descendant (Field4 (N));
15506 Save_Global_Descendant (Field5 (N));
15507 end Save_References_In_Descendants;
15509 -----------------------------------
15510 -- Save_References_In_Identifier --
15511 -----------------------------------
15513 procedure Save_References_In_Identifier (N : Node_Id) is
15514 begin
15515 -- The node did not undergo a transformation
15517 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15518 declare
15519 Aux_N2 : constant Node_Id := Get_Associated_Node (N);
15520 Orig_N2_Parent : constant Node_Id :=
15521 Original_Node (Parent (Aux_N2));
15522 begin
15523 -- The parent of this identifier is a selected component
15524 -- which denotes a named number that was constant folded.
15525 -- Preserve the original name for ASIS and link the parent
15526 -- with its expanded name. The constant folding will be
15527 -- repeated in the instance.
15529 if Nkind (Parent (N)) = N_Selected_Component
15530 and then Nkind_In (Parent (Aux_N2), N_Integer_Literal,
15531 N_Real_Literal)
15532 and then Is_Entity_Name (Orig_N2_Parent)
15533 and then Ekind (Entity (Orig_N2_Parent)) in Named_Kind
15534 and then Is_Global (Entity (Orig_N2_Parent))
15535 then
15536 N2 := Aux_N2;
15537 Set_Associated_Node
15538 (Parent (N), Original_Node (Parent (N2)));
15540 -- Common case
15542 else
15543 -- If this is a discriminant reference, always save it.
15544 -- It is used in the instance to find the corresponding
15545 -- discriminant positionally rather than by name.
15547 Set_Original_Discriminant
15548 (N, Original_Discriminant (Get_Associated_Node (N)));
15549 end if;
15551 Reset_Entity (N);
15552 end;
15554 -- The analysis of the generic copy transformed the identifier
15555 -- into another construct. Propagate the changes to the template.
15557 else
15558 N2 := Get_Associated_Node (N);
15560 -- The identifier denotes a call to a parameterless function.
15561 -- Mark the node as resolved when the function is external.
15563 if Nkind (N2) = N_Function_Call then
15564 E := Entity (Name (N2));
15566 if Present (E) and then Is_Global (E) then
15567 Set_Etype (N, Etype (N2));
15568 else
15569 Set_Associated_Node (N, Empty);
15570 Set_Etype (N, Empty);
15571 end if;
15573 -- The identifier denotes a named number that was constant
15574 -- folded. Preserve the original name for ASIS and undo the
15575 -- constant folding which will be repeated in the instance.
15577 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
15578 and then Is_Entity_Name (Original_Node (N2))
15579 then
15580 Set_Associated_Node (N, Original_Node (N2));
15581 Reset_Entity (N);
15583 -- The identifier resolved to a string literal. Propagate this
15584 -- information to the generic template.
15586 elsif Nkind (N2) = N_String_Literal then
15587 Rewrite (N, New_Copy (N2));
15589 -- The identifier is rewritten as a dereference if it is the
15590 -- prefix of an implicit dereference. Preserve the original
15591 -- tree as the analysis of the instance will expand the node
15592 -- again, but preserve the resolved entity if it is global.
15594 elsif Nkind (N2) = N_Explicit_Dereference then
15595 if Is_Entity_Name (Prefix (N2))
15596 and then Present (Entity (Prefix (N2)))
15597 and then Is_Global (Entity (Prefix (N2)))
15598 then
15599 Set_Associated_Node (N, Prefix (N2));
15601 elsif Nkind (Prefix (N2)) = N_Function_Call
15602 and then Present (Entity (Name (Prefix (N2))))
15603 and then Is_Global (Entity (Name (Prefix (N2))))
15604 then
15605 Rewrite (N,
15606 Make_Explicit_Dereference (Loc,
15607 Prefix =>
15608 Make_Function_Call (Loc,
15609 Name =>
15610 New_Occurrence_Of
15611 (Entity (Name (Prefix (N2))), Loc))));
15613 else
15614 Set_Associated_Node (N, Empty);
15615 Set_Etype (N, Empty);
15616 end if;
15618 -- The subtype mark of a nominally unconstrained object is
15619 -- rewritten as a subtype indication using the bounds of the
15620 -- expression. Recover the original subtype mark.
15622 elsif Nkind (N2) = N_Subtype_Indication
15623 and then Is_Entity_Name (Original_Node (N2))
15624 then
15625 Set_Associated_Node (N, Original_Node (N2));
15626 Reset_Entity (N);
15627 end if;
15628 end if;
15629 end Save_References_In_Identifier;
15631 ---------------------------------
15632 -- Save_References_In_Operator --
15633 ---------------------------------
15635 procedure Save_References_In_Operator (N : Node_Id) is
15636 begin
15637 -- The node did not undergo a transformation
15639 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15640 if Nkind (N) = N_Op_Concat then
15641 Set_Is_Component_Left_Opnd (N,
15642 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15644 Set_Is_Component_Right_Opnd (N,
15645 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15646 end if;
15648 Reset_Entity (N);
15650 -- The analysis of the generic copy transformed the operator into
15651 -- some other construct. Propagate the changes to the template if
15652 -- applicable.
15654 else
15655 N2 := Get_Associated_Node (N);
15657 -- The operator resoved to a function call
15659 if Nkind (N2) = N_Function_Call then
15661 -- Add explicit qualifications in the generic template for
15662 -- all operands of universal type. This aids resolution by
15663 -- preserving the actual type of a literal or an attribute
15664 -- that yields a universal result.
15666 Qualify_Universal_Operands (N, N2);
15668 E := Entity (Name (N2));
15670 if Present (E) and then Is_Global (E) then
15671 Set_Etype (N, Etype (N2));
15672 else
15673 Set_Associated_Node (N, Empty);
15674 Set_Etype (N, Empty);
15675 end if;
15677 -- The operator was folded into a literal
15679 elsif Nkind_In (N2, N_Integer_Literal,
15680 N_Real_Literal,
15681 N_String_Literal)
15682 then
15683 if Present (Original_Node (N2))
15684 and then Nkind (Original_Node (N2)) = Nkind (N)
15685 then
15686 -- Operation was constant-folded. Whenever possible,
15687 -- recover semantic information from unfolded node,
15688 -- for ASIS use.
15690 Set_Associated_Node (N, Original_Node (N2));
15692 if Nkind (N) = N_Op_Concat then
15693 Set_Is_Component_Left_Opnd (N,
15694 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15695 Set_Is_Component_Right_Opnd (N,
15696 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15697 end if;
15699 Reset_Entity (N);
15701 -- Propagate the constant folding back to the template
15703 else
15704 Rewrite (N, New_Copy (N2));
15705 Set_Analyzed (N, False);
15706 end if;
15708 -- The operator was folded into an enumeration literal. Retain
15709 -- the entity to avoid spurious ambiguities if it is overloaded
15710 -- at the point of instantiation or inlining.
15712 elsif Nkind (N2) = N_Identifier
15713 and then Ekind (Entity (N2)) = E_Enumeration_Literal
15714 then
15715 Rewrite (N, New_Copy (N2));
15716 Set_Analyzed (N, False);
15717 end if;
15718 end if;
15720 -- Complete the operands check if node has not been constant
15721 -- folded.
15723 if Nkind (N) in N_Op then
15724 Save_Entity_Descendants (N);
15725 end if;
15726 end Save_References_In_Operator;
15728 -------------------------------
15729 -- Save_References_In_Pragma --
15730 -------------------------------
15732 procedure Save_References_In_Pragma (Prag : Node_Id) is
15733 Context : Node_Id;
15734 Do_Save : Boolean := True;
15736 use Atree.Unchecked_Access;
15737 -- This code section is part of implementing an untyped tree
15738 -- traversal, so it needs direct access to node fields.
15740 begin
15741 -- Do not save global references in pragmas generated from aspects
15742 -- because the pragmas will be regenerated at instantiation time.
15744 if From_Aspect_Specification (Prag) then
15745 Do_Save := False;
15747 -- The capture of global references within contract-related source
15748 -- pragmas associated with generic packages, subprograms or their
15749 -- respective bodies must be delayed due to timing of annotation
15750 -- analysis. Global references are still captured in routine
15751 -- Save_Global_References_In_Contract.
15753 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
15754 if Is_Package_Contract_Annotation (Prag) then
15755 Context := Find_Related_Package_Or_Body (Prag);
15756 else
15757 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
15758 Context := Find_Related_Declaration_Or_Body (Prag);
15759 end if;
15761 -- The use of Original_Node accounts for the case when the
15762 -- related context is generic template.
15764 if Requires_Delayed_Save (Original_Node (Context)) then
15765 Do_Save := False;
15766 end if;
15767 end if;
15769 -- For all other cases, save all global references within the
15770 -- descendants, but skip the following semantic fields:
15772 -- Field1 - Next_Pragma
15773 -- Field3 - Corresponding_Aspect
15774 -- Field5 - Next_Rep_Item
15776 if Do_Save then
15777 Save_Global_Descendant (Field2 (Prag));
15778 Save_Global_Descendant (Field4 (Prag));
15779 end if;
15780 end Save_References_In_Pragma;
15782 -- Start of processing for Save_References
15784 begin
15785 if N = Empty then
15786 null;
15788 -- Aggregates
15790 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
15791 Save_References_In_Aggregate (N);
15793 -- Character literals, operator symbols
15795 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
15796 Save_References_In_Char_Lit_Or_Op_Symbol (N);
15798 -- Defining identifiers
15800 elsif Nkind (N) in N_Entity then
15801 null;
15803 -- Identifiers
15805 elsif Nkind (N) = N_Identifier then
15806 Save_References_In_Identifier (N);
15808 -- Operators
15810 elsif Nkind (N) in N_Op then
15811 Save_References_In_Operator (N);
15813 -- Pragmas
15815 elsif Nkind (N) = N_Pragma then
15816 Save_References_In_Pragma (N);
15818 else
15819 Save_References_In_Descendants (N);
15820 end if;
15822 -- Save all global references found within the aspect specifications
15823 -- of the related node.
15825 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
15827 -- The capture of global references within aspects associated with
15828 -- generic packages, subprograms or their bodies must be delayed
15829 -- due to timing of annotation analysis. Global references are
15830 -- still captured in routine Save_Global_References_In_Contract.
15832 if Requires_Delayed_Save (N) then
15833 null;
15835 -- Otherwise save all global references within the aspects
15837 else
15838 Save_Global_References_In_Aspects (N);
15839 end if;
15840 end if;
15841 end Save_References;
15843 -- Start of processing for Save_Global_References
15845 begin
15846 Gen_Scope := Current_Scope;
15848 -- If the generic unit is a child unit, references to entities in the
15849 -- parent are treated as local, because they will be resolved anew in
15850 -- the context of the instance of the parent.
15852 while Is_Child_Unit (Gen_Scope)
15853 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
15854 loop
15855 Gen_Scope := Scope (Gen_Scope);
15856 end loop;
15858 Save_References (Templ);
15859 end Save_Global_References;
15861 ---------------------------------------
15862 -- Save_Global_References_In_Aspects --
15863 ---------------------------------------
15865 procedure Save_Global_References_In_Aspects (N : Node_Id) is
15866 Asp : Node_Id;
15867 Expr : Node_Id;
15869 begin
15870 Asp := First (Aspect_Specifications (N));
15871 while Present (Asp) loop
15872 Expr := Expression (Asp);
15874 if Present (Expr) then
15875 Save_Global_References (Expr);
15876 end if;
15878 Next (Asp);
15879 end loop;
15880 end Save_Global_References_In_Aspects;
15882 ------------------------------------------
15883 -- Set_Copied_Sloc_For_Inherited_Pragma --
15884 ------------------------------------------
15886 procedure Set_Copied_Sloc_For_Inherited_Pragma
15887 (N : Node_Id;
15888 E : Entity_Id)
15890 begin
15891 Create_Instantiation_Source (N, E,
15892 Inlined_Body => False,
15893 Inherited_Pragma => True,
15894 Factor => S_Adjustment);
15895 end Set_Copied_Sloc_For_Inherited_Pragma;
15897 --------------------------------------
15898 -- Set_Copied_Sloc_For_Inlined_Body --
15899 --------------------------------------
15901 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
15902 begin
15903 Create_Instantiation_Source (N, E,
15904 Inlined_Body => True,
15905 Inherited_Pragma => False,
15906 Factor => S_Adjustment);
15907 end Set_Copied_Sloc_For_Inlined_Body;
15909 ---------------------
15910 -- Set_Instance_Of --
15911 ---------------------
15913 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
15914 begin
15915 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
15916 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
15917 Generic_Renamings.Increment_Last;
15918 end Set_Instance_Of;
15920 --------------------
15921 -- Set_Next_Assoc --
15922 --------------------
15924 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
15925 begin
15926 Generic_Renamings.Table (E).Next_In_HTable := Next;
15927 end Set_Next_Assoc;
15929 -------------------
15930 -- Start_Generic --
15931 -------------------
15933 procedure Start_Generic is
15934 begin
15935 -- ??? More things could be factored out in this routine.
15936 -- Should probably be done at a later stage.
15938 Generic_Flags.Append (Inside_A_Generic);
15939 Inside_A_Generic := True;
15941 Expander_Mode_Save_And_Set (False);
15942 end Start_Generic;
15944 ----------------------
15945 -- Set_Instance_Env --
15946 ----------------------
15948 -- WARNING: This routine manages SPARK regions
15950 procedure Set_Instance_Env
15951 (Gen_Unit : Entity_Id;
15952 Act_Unit : Entity_Id)
15954 Saved_AE : constant Boolean := Assertions_Enabled;
15955 Saved_CPL : constant Node_Id := Check_Policy_List;
15956 Saved_DEC : constant Boolean := Dynamic_Elaboration_Checks;
15957 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
15958 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
15960 begin
15961 -- Regardless of the current mode, predefined units are analyzed in the
15962 -- most current Ada mode, and earlier version Ada checks do not apply
15963 -- to predefined units. Nothing needs to be done for non-internal units.
15964 -- These are always analyzed in the current mode.
15966 if In_Internal_Unit (Gen_Unit) then
15968 -- The following call resets all configuration attributes to default
15969 -- or the xxx_Config versions of the attributes when the current sem
15970 -- unit is the main unit. At the same time, internal units must also
15971 -- inherit certain configuration attributes from their context. It
15972 -- is unclear what these two sets are.
15974 Set_Config_Switches (True, Current_Sem_Unit = Main_Unit);
15976 -- Reinstall relevant configuration attributes of the context
15978 Assertions_Enabled := Saved_AE;
15979 Check_Policy_List := Saved_CPL;
15980 Dynamic_Elaboration_Checks := Saved_DEC;
15982 Install_SPARK_Mode (Saved_SM, Saved_SMP);
15983 end if;
15985 Current_Instantiated_Parent :=
15986 (Gen_Id => Gen_Unit,
15987 Act_Id => Act_Unit,
15988 Next_In_HTable => Assoc_Null);
15989 end Set_Instance_Env;
15991 -----------------
15992 -- Switch_View --
15993 -----------------
15995 procedure Switch_View (T : Entity_Id) is
15996 BT : constant Entity_Id := Base_Type (T);
15997 Priv_Elmt : Elmt_Id := No_Elmt;
15998 Priv_Sub : Entity_Id;
16000 begin
16001 -- T may be private but its base type may have been exchanged through
16002 -- some other occurrence, in which case there is nothing to switch
16003 -- besides T itself. Note that a private dependent subtype of a private
16004 -- type might not have been switched even if the base type has been,
16005 -- because of the last branch of Check_Private_View (see comment there).
16007 if not Is_Private_Type (BT) then
16008 Prepend_Elmt (Full_View (T), Exchanged_Views);
16009 Exchange_Declarations (T);
16010 return;
16011 end if;
16013 Priv_Elmt := First_Elmt (Private_Dependents (BT));
16015 if Present (Full_View (BT)) then
16016 Prepend_Elmt (Full_View (BT), Exchanged_Views);
16017 Exchange_Declarations (BT);
16018 end if;
16020 while Present (Priv_Elmt) loop
16021 Priv_Sub := (Node (Priv_Elmt));
16023 -- We avoid flipping the subtype if the Etype of its full view is
16024 -- private because this would result in a malformed subtype. This
16025 -- occurs when the Etype of the subtype full view is the full view of
16026 -- the base type (and since the base types were just switched, the
16027 -- subtype is pointing to the wrong view). This is currently the case
16028 -- for tagged record types, access types (maybe more?) and needs to
16029 -- be resolved. ???
16031 if Present (Full_View (Priv_Sub))
16032 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
16033 then
16034 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
16035 Exchange_Declarations (Priv_Sub);
16036 end if;
16038 Next_Elmt (Priv_Elmt);
16039 end loop;
16040 end Switch_View;
16042 -----------------
16043 -- True_Parent --
16044 -----------------
16046 function True_Parent (N : Node_Id) return Node_Id is
16047 begin
16048 if Nkind (Parent (N)) = N_Subunit then
16049 return Parent (Corresponding_Stub (Parent (N)));
16050 else
16051 return Parent (N);
16052 end if;
16053 end True_Parent;
16055 -----------------------------
16056 -- Valid_Default_Attribute --
16057 -----------------------------
16059 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
16060 Attr_Id : constant Attribute_Id :=
16061 Get_Attribute_Id (Attribute_Name (Def));
16062 T : constant Entity_Id := Entity (Prefix (Def));
16063 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
16064 F : Entity_Id;
16065 Num_F : Nat;
16066 OK : Boolean;
16068 begin
16069 if No (T) or else T = Any_Id then
16070 return;
16071 end if;
16073 Num_F := 0;
16074 F := First_Formal (Nam);
16075 while Present (F) loop
16076 Num_F := Num_F + 1;
16077 Next_Formal (F);
16078 end loop;
16080 case Attr_Id is
16081 when Attribute_Adjacent
16082 | Attribute_Ceiling
16083 | Attribute_Copy_Sign
16084 | Attribute_Floor
16085 | Attribute_Fraction
16086 | Attribute_Machine
16087 | Attribute_Model
16088 | Attribute_Remainder
16089 | Attribute_Rounding
16090 | Attribute_Unbiased_Rounding
16092 OK := Is_Fun
16093 and then Num_F = 1
16094 and then Is_Floating_Point_Type (T);
16096 when Attribute_Image
16097 | Attribute_Pred
16098 | Attribute_Succ
16099 | Attribute_Value
16100 | Attribute_Wide_Image
16101 | Attribute_Wide_Value
16103 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
16105 when Attribute_Max
16106 | Attribute_Min
16108 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
16110 when Attribute_Input =>
16111 OK := (Is_Fun and then Num_F = 1);
16113 when Attribute_Output
16114 | Attribute_Read
16115 | Attribute_Write
16117 OK := not Is_Fun and then Num_F = 2;
16119 when others =>
16120 OK := False;
16121 end case;
16123 if not OK then
16124 Error_Msg_N
16125 ("attribute reference has wrong profile for subprogram", Def);
16126 end if;
16127 end Valid_Default_Attribute;
16129 end Sem_Ch12;