* lto.c (do_stream_out): Add PART parameter; open dump file.
[official-gcc.git] / gcc / ada / sem_ch12.adb
blob246d9eb9dc089c82f1bf627159ecdc63cb8c68f4
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
1035 -- Add to the instantiation node and the corresponding unit declaration
1036 -- the current values of global flags to be used when analyzing the
1037 -- instance body.
1039 Pending_Instantiations.Append
1040 ((Inst_Node => Inst,
1041 Act_Decl => Act_Decl,
1042 Expander_Status => Expander_Active,
1043 Current_Sem_Unit => Current_Sem_Unit,
1044 Scope_Suppress => Scope_Suppress,
1045 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
1046 Version => Ada_Version,
1047 Version_Pragma => Ada_Version_Pragma,
1048 Warnings => Save_Warnings,
1049 SPARK_Mode => SPARK_Mode,
1050 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
1051 end Add_Pending_Instantiation;
1053 ----------------------------------
1054 -- Adjust_Inherited_Pragma_Sloc --
1055 ----------------------------------
1057 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
1058 begin
1059 Adjust_Instantiation_Sloc (N, S_Adjustment);
1060 end Adjust_Inherited_Pragma_Sloc;
1062 --------------------------
1063 -- Analyze_Associations --
1064 --------------------------
1066 function Analyze_Associations
1067 (I_Node : Node_Id;
1068 Formals : List_Id;
1069 F_Copy : List_Id) return List_Id
1071 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1072 Assoc_List : constant List_Id := New_List;
1073 Default_Actuals : constant List_Id := New_List;
1074 Gen_Unit : constant Entity_Id :=
1075 Defining_Entity (Parent (F_Copy));
1077 Actuals : List_Id;
1078 Actual : Node_Id;
1079 Analyzed_Formal : Node_Id;
1080 First_Named : Node_Id := Empty;
1081 Formal : Node_Id;
1082 Match : Node_Id;
1083 Named : Node_Id;
1084 Saved_Formal : Node_Id;
1086 Default_Formals : constant List_Id := New_List;
1087 -- If an Others_Choice is present, some of the formals may be defaulted.
1088 -- To simplify the treatment of visibility in an instance, we introduce
1089 -- individual defaults for each such formal. These defaults are
1090 -- appended to the list of associations and replace the Others_Choice.
1092 Found_Assoc : Node_Id;
1093 -- Association for the current formal being match. Empty if there are
1094 -- no remaining actuals, or if there is no named association with the
1095 -- name of the formal.
1097 Is_Named_Assoc : Boolean;
1098 Num_Matched : Nat := 0;
1099 Num_Actuals : Nat := 0;
1101 Others_Present : Boolean := False;
1102 Others_Choice : Node_Id := Empty;
1103 -- In Ada 2005, indicates partial parameterization of a formal
1104 -- package. As usual an other association must be last in the list.
1106 procedure Check_Fixed_Point_Actual (Actual : Node_Id);
1107 -- Warn if an actual fixed-point type has user-defined arithmetic
1108 -- operations, but there is no corresponding formal in the generic,
1109 -- in which case the predefined operations will be used. This merits
1110 -- a warning because of the special semantics of fixed point ops.
1112 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1113 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1114 -- cannot have a named association for it. AI05-0025 extends this rule
1115 -- to formals of formal packages by AI05-0025, and it also applies to
1116 -- box-initialized formals.
1118 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1119 -- Determine whether the parameter types and the return type of Subp
1120 -- are fully defined at the point of instantiation.
1122 function Matching_Actual
1123 (F : Entity_Id;
1124 A_F : Entity_Id) return Node_Id;
1125 -- Find actual that corresponds to a given a formal parameter. If the
1126 -- actuals are positional, return the next one, if any. If the actuals
1127 -- are named, scan the parameter associations to find the right one.
1128 -- A_F is the corresponding entity in the analyzed generic, which is
1129 -- placed on the selector name for ASIS use.
1131 -- In Ada 2005, a named association may be given with a box, in which
1132 -- case Matching_Actual sets Found_Assoc to the generic association,
1133 -- but return Empty for the actual itself. In this case the code below
1134 -- creates a corresponding declaration for the formal.
1136 function Partial_Parameterization return Boolean;
1137 -- Ada 2005: if no match is found for a given formal, check if the
1138 -- association for it includes a box, or whether the associations
1139 -- include an Others clause.
1141 procedure Process_Default (F : Entity_Id);
1142 -- Add a copy of the declaration of generic formal F to the list of
1143 -- associations, and add an explicit box association for F if there
1144 -- is none yet, and the default comes from an Others_Choice.
1146 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1147 -- Determine whether Subp renames one of the subprograms defined in the
1148 -- generated package Standard.
1150 procedure Set_Analyzed_Formal;
1151 -- Find the node in the generic copy that corresponds to a given formal.
1152 -- The semantic information on this node is used to perform legality
1153 -- checks on the actuals. Because semantic analysis can introduce some
1154 -- anonymous entities or modify the declaration node itself, the
1155 -- correspondence between the two lists is not one-one. In addition to
1156 -- anonymous types, the presence a formal equality will introduce an
1157 -- implicit declaration for the corresponding inequality.
1159 ----------------------------------------
1160 -- Check_Overloaded_Formal_Subprogram --
1161 ----------------------------------------
1163 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1164 Temp_Formal : Entity_Id;
1166 begin
1167 Temp_Formal := First (Formals);
1168 while Present (Temp_Formal) loop
1169 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1170 and then Temp_Formal /= Formal
1171 and then
1172 Chars (Defining_Unit_Name (Specification (Formal))) =
1173 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1174 then
1175 if Present (Found_Assoc) then
1176 Error_Msg_N
1177 ("named association not allowed for overloaded formal",
1178 Found_Assoc);
1180 else
1181 Error_Msg_N
1182 ("named association not allowed for overloaded formal",
1183 Others_Choice);
1184 end if;
1186 Abandon_Instantiation (Instantiation_Node);
1187 end if;
1189 Next (Temp_Formal);
1190 end loop;
1191 end Check_Overloaded_Formal_Subprogram;
1193 -------------------------------
1194 -- Check_Fixed_Point_Actual --
1195 -------------------------------
1197 procedure Check_Fixed_Point_Actual (Actual : Node_Id) is
1198 Typ : constant Entity_Id := Entity (Actual);
1199 Prims : constant Elist_Id := Collect_Primitive_Operations (Typ);
1200 Elem : Elmt_Id;
1201 Formal : Node_Id;
1202 Op : Entity_Id;
1204 begin
1205 -- Locate primitive operations of the type that are arithmetic
1206 -- operations.
1208 Elem := First_Elmt (Prims);
1209 while Present (Elem) loop
1210 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
1212 -- Check whether the generic unit has a formal subprogram of
1213 -- the same name. This does not check types but is good enough
1214 -- to justify a warning.
1216 Formal := First_Non_Pragma (Formals);
1217 Op := Alias (Node (Elem));
1219 while Present (Formal) loop
1220 if Nkind (Formal) = N_Formal_Concrete_Subprogram_Declaration
1221 and then Chars (Defining_Entity (Formal)) =
1222 Chars (Node (Elem))
1223 then
1224 exit;
1226 elsif Nkind (Formal) = N_Formal_Package_Declaration then
1227 declare
1228 Assoc : Node_Id;
1229 Ent : Entity_Id;
1231 begin
1232 -- Locate corresponding actual, and check whether it
1233 -- includes a fixed-point type.
1235 Assoc := First (Assoc_List);
1236 while Present (Assoc) loop
1237 exit when
1238 Nkind (Assoc) = N_Package_Renaming_Declaration
1239 and then Chars (Defining_Unit_Name (Assoc)) =
1240 Chars (Defining_Identifier (Formal));
1242 Next (Assoc);
1243 end loop;
1245 if Present (Assoc) then
1247 -- If formal package declares a fixed-point type,
1248 -- and the user-defined operator is derived from
1249 -- a generic instance package, the fixed-point type
1250 -- does not use the corresponding predefined op.
1252 Ent := First_Entity (Entity (Name (Assoc)));
1253 while Present (Ent) loop
1254 if Is_Fixed_Point_Type (Ent)
1255 and then Present (Op)
1256 and then Is_Generic_Instance (Scope (Op))
1257 then
1258 return;
1259 end if;
1261 Next_Entity (Ent);
1262 end loop;
1263 end if;
1264 end;
1265 end if;
1267 Next (Formal);
1268 end loop;
1270 if No (Formal) then
1271 Error_Msg_Sloc := Sloc (Node (Elem));
1272 Error_Msg_NE
1273 ("?instance uses predefined operation, not primitive "
1274 & "operation&#", Actual, Node (Elem));
1275 end if;
1276 end if;
1278 Next_Elmt (Elem);
1279 end loop;
1280 end Check_Fixed_Point_Actual;
1282 -------------------------------
1283 -- Has_Fully_Defined_Profile --
1284 -------------------------------
1286 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1287 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1288 -- Determine whethet type Typ is fully defined
1290 ---------------------------
1291 -- Is_Fully_Defined_Type --
1292 ---------------------------
1294 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1295 begin
1296 -- A private type without a full view is not fully defined
1298 if Is_Private_Type (Typ)
1299 and then No (Full_View (Typ))
1300 then
1301 return False;
1303 -- An incomplete type is never fully defined
1305 elsif Is_Incomplete_Type (Typ) then
1306 return False;
1308 -- All other types are fully defined
1310 else
1311 return True;
1312 end if;
1313 end Is_Fully_Defined_Type;
1315 -- Local declarations
1317 Param : Entity_Id;
1319 -- Start of processing for Has_Fully_Defined_Profile
1321 begin
1322 -- Check the parameters
1324 Param := First_Formal (Subp);
1325 while Present (Param) loop
1326 if not Is_Fully_Defined_Type (Etype (Param)) then
1327 return False;
1328 end if;
1330 Next_Formal (Param);
1331 end loop;
1333 -- Check the return type
1335 return Is_Fully_Defined_Type (Etype (Subp));
1336 end Has_Fully_Defined_Profile;
1338 ---------------------
1339 -- Matching_Actual --
1340 ---------------------
1342 function Matching_Actual
1343 (F : Entity_Id;
1344 A_F : Entity_Id) return Node_Id
1346 Prev : Node_Id;
1347 Act : Node_Id;
1349 begin
1350 Is_Named_Assoc := False;
1352 -- End of list of purely positional parameters
1354 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1355 Found_Assoc := Empty;
1356 Act := Empty;
1358 -- Case of positional parameter corresponding to current formal
1360 elsif No (Selector_Name (Actual)) then
1361 Found_Assoc := Actual;
1362 Act := Explicit_Generic_Actual_Parameter (Actual);
1363 Num_Matched := Num_Matched + 1;
1364 Next (Actual);
1366 -- Otherwise scan list of named actuals to find the one with the
1367 -- desired name. All remaining actuals have explicit names.
1369 else
1370 Is_Named_Assoc := True;
1371 Found_Assoc := Empty;
1372 Act := Empty;
1373 Prev := Empty;
1375 while Present (Actual) loop
1376 if Nkind (Actual) = N_Others_Choice then
1377 Found_Assoc := Empty;
1378 Act := Empty;
1380 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1381 Set_Entity (Selector_Name (Actual), A_F);
1382 Set_Etype (Selector_Name (Actual), Etype (A_F));
1383 Generate_Reference (A_F, Selector_Name (Actual));
1385 Found_Assoc := Actual;
1386 Act := Explicit_Generic_Actual_Parameter (Actual);
1387 Num_Matched := Num_Matched + 1;
1388 exit;
1389 end if;
1391 Prev := Actual;
1392 Next (Actual);
1393 end loop;
1395 -- Reset for subsequent searches. In most cases the named
1396 -- associations are in order. If they are not, we reorder them
1397 -- to avoid scanning twice the same actual. This is not just a
1398 -- question of efficiency: there may be multiple defaults with
1399 -- boxes that have the same name. In a nested instantiation we
1400 -- insert actuals for those defaults, and cannot rely on their
1401 -- names to disambiguate them.
1403 if Actual = First_Named then
1404 Next (First_Named);
1406 elsif Present (Actual) then
1407 Insert_Before (First_Named, Remove_Next (Prev));
1408 end if;
1410 Actual := First_Named;
1411 end if;
1413 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1414 Set_Used_As_Generic_Actual (Entity (Act));
1415 end if;
1417 return Act;
1418 end Matching_Actual;
1420 ------------------------------
1421 -- Partial_Parameterization --
1422 ------------------------------
1424 function Partial_Parameterization return Boolean is
1425 begin
1426 return Others_Present
1427 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1428 end Partial_Parameterization;
1430 ---------------------
1431 -- Process_Default --
1432 ---------------------
1434 procedure Process_Default (F : Entity_Id) is
1435 Loc : constant Source_Ptr := Sloc (I_Node);
1436 F_Id : constant Entity_Id := Defining_Entity (F);
1437 Decl : Node_Id;
1438 Default : Node_Id;
1439 Id : Entity_Id;
1441 begin
1442 -- Append copy of formal declaration to associations, and create new
1443 -- defining identifier for it.
1445 Decl := New_Copy_Tree (F);
1446 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1448 if Nkind (F) in N_Formal_Subprogram_Declaration then
1449 Set_Defining_Unit_Name (Specification (Decl), Id);
1451 else
1452 Set_Defining_Identifier (Decl, Id);
1453 end if;
1455 Append (Decl, Assoc_List);
1457 if No (Found_Assoc) then
1458 Default :=
1459 Make_Generic_Association (Loc,
1460 Selector_Name =>
1461 New_Occurrence_Of (Id, Loc),
1462 Explicit_Generic_Actual_Parameter => Empty);
1463 Set_Box_Present (Default);
1464 Append (Default, Default_Formals);
1465 end if;
1466 end Process_Default;
1468 ---------------------------------
1469 -- Renames_Standard_Subprogram --
1470 ---------------------------------
1472 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1473 Id : Entity_Id;
1475 begin
1476 Id := Alias (Subp);
1477 while Present (Id) loop
1478 if Scope (Id) = Standard_Standard then
1479 return True;
1480 end if;
1482 Id := Alias (Id);
1483 end loop;
1485 return False;
1486 end Renames_Standard_Subprogram;
1488 -------------------------
1489 -- Set_Analyzed_Formal --
1490 -------------------------
1492 procedure Set_Analyzed_Formal is
1493 Kind : Node_Kind;
1495 begin
1496 while Present (Analyzed_Formal) loop
1497 Kind := Nkind (Analyzed_Formal);
1499 case Nkind (Formal) is
1500 when N_Formal_Subprogram_Declaration =>
1501 exit when Kind in N_Formal_Subprogram_Declaration
1502 and then
1503 Chars
1504 (Defining_Unit_Name (Specification (Formal))) =
1505 Chars
1506 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1508 when N_Formal_Package_Declaration =>
1509 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1510 N_Generic_Package_Declaration,
1511 N_Package_Declaration);
1513 when N_Use_Package_Clause
1514 | N_Use_Type_Clause
1516 exit;
1518 when others =>
1520 -- Skip freeze nodes, and nodes inserted to replace
1521 -- unrecognized pragmas.
1523 exit when
1524 Kind not in N_Formal_Subprogram_Declaration
1525 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1526 N_Freeze_Entity,
1527 N_Null_Statement,
1528 N_Itype_Reference)
1529 and then Chars (Defining_Identifier (Formal)) =
1530 Chars (Defining_Identifier (Analyzed_Formal));
1531 end case;
1533 Next (Analyzed_Formal);
1534 end loop;
1535 end Set_Analyzed_Formal;
1537 -- Start of processing for Analyze_Associations
1539 begin
1540 Actuals := Generic_Associations (I_Node);
1542 if Present (Actuals) then
1544 -- Check for an Others choice, indicating a partial parameterization
1545 -- for a formal package.
1547 Actual := First (Actuals);
1548 while Present (Actual) loop
1549 if Nkind (Actual) = N_Others_Choice then
1550 Others_Present := True;
1551 Others_Choice := Actual;
1553 if Present (Next (Actual)) then
1554 Error_Msg_N ("others must be last association", Actual);
1555 end if;
1557 -- This subprogram is used both for formal packages and for
1558 -- instantiations. For the latter, associations must all be
1559 -- explicit.
1561 if Nkind (I_Node) /= N_Formal_Package_Declaration
1562 and then Comes_From_Source (I_Node)
1563 then
1564 Error_Msg_N
1565 ("others association not allowed in an instance",
1566 Actual);
1567 end if;
1569 -- In any case, nothing to do after the others association
1571 exit;
1573 elsif Box_Present (Actual)
1574 and then Comes_From_Source (I_Node)
1575 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1576 then
1577 Error_Msg_N
1578 ("box association not allowed in an instance", Actual);
1579 end if;
1581 Next (Actual);
1582 end loop;
1584 -- If named associations are present, save first named association
1585 -- (it may of course be Empty) to facilitate subsequent name search.
1587 First_Named := First (Actuals);
1588 while Present (First_Named)
1589 and then Nkind (First_Named) /= N_Others_Choice
1590 and then No (Selector_Name (First_Named))
1591 loop
1592 Num_Actuals := Num_Actuals + 1;
1593 Next (First_Named);
1594 end loop;
1595 end if;
1597 Named := First_Named;
1598 while Present (Named) loop
1599 if Nkind (Named) /= N_Others_Choice
1600 and then No (Selector_Name (Named))
1601 then
1602 Error_Msg_N ("invalid positional actual after named one", Named);
1603 Abandon_Instantiation (Named);
1604 end if;
1606 -- A named association may lack an actual parameter, if it was
1607 -- introduced for a default subprogram that turns out to be local
1608 -- to the outer instantiation. If it has a box association it must
1609 -- correspond to some formal in the generic.
1611 if Nkind (Named) /= N_Others_Choice
1612 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1613 or else Box_Present (Named))
1614 then
1615 Num_Actuals := Num_Actuals + 1;
1616 end if;
1618 Next (Named);
1619 end loop;
1621 if Present (Formals) then
1622 Formal := First_Non_Pragma (Formals);
1623 Analyzed_Formal := First_Non_Pragma (F_Copy);
1625 if Present (Actuals) then
1626 Actual := First (Actuals);
1628 -- All formals should have default values
1630 else
1631 Actual := Empty;
1632 end if;
1634 while Present (Formal) loop
1635 Set_Analyzed_Formal;
1636 Saved_Formal := Next_Non_Pragma (Formal);
1638 case Nkind (Formal) is
1639 when N_Formal_Object_Declaration =>
1640 Match :=
1641 Matching_Actual
1642 (Defining_Identifier (Formal),
1643 Defining_Identifier (Analyzed_Formal));
1645 if No (Match) and then Partial_Parameterization then
1646 Process_Default (Formal);
1648 else
1649 Append_List
1650 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1651 Assoc_List);
1653 -- For a defaulted in_parameter, create an entry in the
1654 -- the list of defaulted actuals, for GNATProve use. Do
1655 -- not included these defaults for an instance nested
1656 -- within a generic, because the defaults are also used
1657 -- in the analysis of the enclosing generic, and only
1658 -- defaulted subprograms are relevant there.
1660 if No (Match) and then not Inside_A_Generic then
1661 Append_To (Default_Actuals,
1662 Make_Generic_Association (Sloc (I_Node),
1663 Selector_Name =>
1664 New_Occurrence_Of
1665 (Defining_Identifier (Formal), Sloc (I_Node)),
1666 Explicit_Generic_Actual_Parameter =>
1667 New_Copy_Tree (Default_Expression (Formal))));
1668 end if;
1669 end if;
1671 -- If the object is a call to an expression function, this
1672 -- is a freezing point for it.
1674 if Is_Entity_Name (Match)
1675 and then Present (Entity (Match))
1676 and then Nkind
1677 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1678 = N_Expression_Function
1679 then
1680 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1681 end if;
1683 when N_Formal_Type_Declaration =>
1684 Match :=
1685 Matching_Actual
1686 (Defining_Identifier (Formal),
1687 Defining_Identifier (Analyzed_Formal));
1689 if No (Match) then
1690 if Partial_Parameterization then
1691 Process_Default (Formal);
1693 else
1694 Error_Msg_Sloc := Sloc (Gen_Unit);
1695 Error_Msg_NE
1696 ("missing actual&",
1697 Instantiation_Node, Defining_Identifier (Formal));
1698 Error_Msg_NE
1699 ("\in instantiation of & declared#",
1700 Instantiation_Node, Gen_Unit);
1701 Abandon_Instantiation (Instantiation_Node);
1702 end if;
1704 else
1705 Analyze (Match);
1706 Append_List
1707 (Instantiate_Type
1708 (Formal, Match, Analyzed_Formal, Assoc_List),
1709 Assoc_List);
1711 -- Warn when an actual is a fixed-point with user-
1712 -- defined promitives. The warning is superfluous
1713 -- if the fornal is private, because there can be
1714 -- no arithmetic operations in the generic so there
1715 -- no danger of confusion.
1717 if Is_Fixed_Point_Type (Entity (Match))
1718 and then not Is_Private_Type
1719 (Defining_Identifier (Analyzed_Formal))
1720 then
1721 Check_Fixed_Point_Actual (Match);
1722 end if;
1724 -- An instantiation is a freeze point for the actuals,
1725 -- unless this is a rewritten formal package, or the
1726 -- formal is an Ada 2012 formal incomplete type.
1728 if Nkind (I_Node) = N_Formal_Package_Declaration
1729 or else
1730 (Ada_Version >= Ada_2012
1731 and then
1732 Ekind (Defining_Identifier (Analyzed_Formal)) =
1733 E_Incomplete_Type)
1734 then
1735 null;
1737 else
1738 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1739 end if;
1740 end if;
1742 -- A remote access-to-class-wide type is not a legal actual
1743 -- for a generic formal of an access type (E.2.2(17/2)).
1744 -- In GNAT an exception to this rule is introduced when
1745 -- the formal is marked as remote using implementation
1746 -- defined aspect/pragma Remote_Access_Type. In that case
1747 -- the actual must be remote as well.
1749 -- If the current instantiation is the construction of a
1750 -- local copy for a formal package the actuals may be
1751 -- defaulted, and there is no matching actual to check.
1753 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1754 and then
1755 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1756 N_Access_To_Object_Definition
1757 and then Present (Match)
1758 then
1759 declare
1760 Formal_Ent : constant Entity_Id :=
1761 Defining_Identifier (Analyzed_Formal);
1762 begin
1763 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1764 = Is_Remote_Types (Formal_Ent)
1765 then
1766 -- Remoteness of formal and actual match
1768 null;
1770 elsif Is_Remote_Types (Formal_Ent) then
1772 -- Remote formal, non-remote actual
1774 Error_Msg_NE
1775 ("actual for& must be remote", Match, Formal_Ent);
1777 else
1778 -- Non-remote formal, remote actual
1780 Error_Msg_NE
1781 ("actual for& may not be remote",
1782 Match, Formal_Ent);
1783 end if;
1784 end;
1785 end if;
1787 when N_Formal_Subprogram_Declaration =>
1788 Match :=
1789 Matching_Actual
1790 (Defining_Unit_Name (Specification (Formal)),
1791 Defining_Unit_Name (Specification (Analyzed_Formal)));
1793 -- If the formal subprogram has the same name as another
1794 -- formal subprogram of the generic, then a named
1795 -- association is illegal (12.3(9)). Exclude named
1796 -- associations that are generated for a nested instance.
1798 if Present (Match)
1799 and then Is_Named_Assoc
1800 and then Comes_From_Source (Found_Assoc)
1801 then
1802 Check_Overloaded_Formal_Subprogram (Formal);
1803 end if;
1805 -- If there is no corresponding actual, this may be case
1806 -- of partial parameterization, or else the formal has a
1807 -- default or a box.
1809 if No (Match) and then Partial_Parameterization then
1810 Process_Default (Formal);
1812 if Nkind (I_Node) = N_Formal_Package_Declaration then
1813 Check_Overloaded_Formal_Subprogram (Formal);
1814 end if;
1816 else
1817 Append_To (Assoc_List,
1818 Instantiate_Formal_Subprogram
1819 (Formal, Match, Analyzed_Formal));
1821 -- An instantiation is a freeze point for the actuals,
1822 -- unless this is a rewritten formal package.
1824 if Nkind (I_Node) /= N_Formal_Package_Declaration
1825 and then Nkind (Match) = N_Identifier
1826 and then Is_Subprogram (Entity (Match))
1828 -- The actual subprogram may rename a routine defined
1829 -- in Standard. Avoid freezing such renamings because
1830 -- subprograms coming from Standard cannot be frozen.
1832 and then
1833 not Renames_Standard_Subprogram (Entity (Match))
1835 -- If the actual subprogram comes from a different
1836 -- unit, it is already frozen, either by a body in
1837 -- that unit or by the end of the declarative part
1838 -- of the unit. This check avoids the freezing of
1839 -- subprograms defined in Standard which are used
1840 -- as generic actuals.
1842 and then In_Same_Code_Unit (Entity (Match), I_Node)
1843 and then Has_Fully_Defined_Profile (Entity (Match))
1844 then
1845 -- Mark the subprogram as having a delayed freeze
1846 -- since this may be an out-of-order action.
1848 Set_Has_Delayed_Freeze (Entity (Match));
1849 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1850 end if;
1851 end if;
1853 -- If this is a nested generic, preserve default for later
1854 -- instantiations. We do this as well for GNATProve use,
1855 -- so that the list of generic associations is complete.
1857 if No (Match) and then Box_Present (Formal) then
1858 declare
1859 Subp : constant Entity_Id :=
1860 Defining_Unit_Name
1861 (Specification (Last (Assoc_List)));
1863 begin
1864 Append_To (Default_Actuals,
1865 Make_Generic_Association (Sloc (I_Node),
1866 Selector_Name =>
1867 New_Occurrence_Of (Subp, Sloc (I_Node)),
1868 Explicit_Generic_Actual_Parameter =>
1869 New_Occurrence_Of (Subp, Sloc (I_Node))));
1870 end;
1871 end if;
1873 when N_Formal_Package_Declaration =>
1874 Match :=
1875 Matching_Actual
1876 (Defining_Identifier (Formal),
1877 Defining_Identifier (Original_Node (Analyzed_Formal)));
1879 if No (Match) then
1880 if Partial_Parameterization then
1881 Process_Default (Formal);
1883 else
1884 Error_Msg_Sloc := Sloc (Gen_Unit);
1885 Error_Msg_NE
1886 ("missing actual&",
1887 Instantiation_Node, Defining_Identifier (Formal));
1888 Error_Msg_NE
1889 ("\in instantiation of & declared#",
1890 Instantiation_Node, Gen_Unit);
1892 Abandon_Instantiation (Instantiation_Node);
1893 end if;
1895 else
1896 Analyze (Match);
1897 Append_List
1898 (Instantiate_Formal_Package
1899 (Formal, Match, Analyzed_Formal),
1900 Assoc_List);
1902 -- Determine whether the actual package needs an explicit
1903 -- freeze node. This is only the case if the actual is
1904 -- declared in the same unit and has a body. Normally
1905 -- packages do not have explicit freeze nodes, and gigi
1906 -- only uses them to elaborate entities in a package
1907 -- body.
1909 Explicit_Freeze_Check : declare
1910 Actual : constant Entity_Id := Entity (Match);
1911 Gen_Par : Entity_Id;
1913 Needs_Freezing : Boolean;
1914 S : Entity_Id;
1916 procedure Check_Generic_Parent;
1917 -- The actual may be an instantiation of a unit
1918 -- declared in a previous instantiation. If that
1919 -- one is also in the current compilation, it must
1920 -- itself be frozen before the actual. The actual
1921 -- may be an instantiation of a generic child unit,
1922 -- in which case the same applies to the instance
1923 -- of the parent which must be frozen before the
1924 -- actual.
1925 -- Should this itself be recursive ???
1927 --------------------------
1928 -- Check_Generic_Parent --
1929 --------------------------
1931 procedure Check_Generic_Parent is
1932 Inst : constant Node_Id :=
1933 Next (Unit_Declaration_Node (Actual));
1934 Par : Entity_Id;
1936 begin
1937 Par := Empty;
1939 if Nkind (Parent (Actual)) = N_Package_Specification
1940 then
1941 Par := Scope (Generic_Parent (Parent (Actual)));
1943 if Is_Generic_Instance (Par) then
1944 null;
1946 -- If the actual is a child generic unit, check
1947 -- whether the instantiation of the parent is
1948 -- also local and must also be frozen now. We
1949 -- must retrieve the instance node to locate the
1950 -- parent instance if any.
1952 elsif Ekind (Par) = E_Generic_Package
1953 and then Is_Child_Unit (Gen_Par)
1954 and then Ekind (Scope (Gen_Par)) =
1955 E_Generic_Package
1956 then
1957 if Nkind (Inst) = N_Package_Instantiation
1958 and then Nkind (Name (Inst)) =
1959 N_Expanded_Name
1960 then
1961 -- Retrieve entity of parent instance
1963 Par := Entity (Prefix (Name (Inst)));
1964 end if;
1966 else
1967 Par := Empty;
1968 end if;
1969 end if;
1971 if Present (Par)
1972 and then Is_Generic_Instance (Par)
1973 and then Scope (Par) = Current_Scope
1974 and then
1975 (No (Freeze_Node (Par))
1976 or else
1977 not Is_List_Member (Freeze_Node (Par)))
1978 then
1979 Set_Has_Delayed_Freeze (Par);
1980 Append_Elmt (Par, Actuals_To_Freeze);
1981 end if;
1982 end Check_Generic_Parent;
1984 -- Start of processing for Explicit_Freeze_Check
1986 begin
1987 if Present (Renamed_Entity (Actual)) then
1988 Gen_Par :=
1989 Generic_Parent (Specification
1990 (Unit_Declaration_Node
1991 (Renamed_Entity (Actual))));
1992 else
1993 Gen_Par :=
1994 Generic_Parent (Specification
1995 (Unit_Declaration_Node (Actual)));
1996 end if;
1998 if not Expander_Active
1999 or else not Has_Completion (Actual)
2000 or else not In_Same_Source_Unit (I_Node, Actual)
2001 or else Is_Frozen (Actual)
2002 or else
2003 (Present (Renamed_Entity (Actual))
2004 and then
2005 not In_Same_Source_Unit
2006 (I_Node, (Renamed_Entity (Actual))))
2007 then
2008 null;
2010 else
2011 -- Finally we want to exclude such freeze nodes
2012 -- from statement sequences, which freeze
2013 -- everything before them.
2014 -- Is this strictly necessary ???
2016 Needs_Freezing := True;
2018 S := Current_Scope;
2019 while Present (S) loop
2020 if Ekind_In (S, E_Block,
2021 E_Function,
2022 E_Loop,
2023 E_Procedure)
2024 then
2025 Needs_Freezing := False;
2026 exit;
2027 end if;
2029 S := Scope (S);
2030 end loop;
2032 if Needs_Freezing then
2033 Check_Generic_Parent;
2035 -- If the actual is a renaming of a proper
2036 -- instance of the formal package, indicate
2037 -- that it is the instance that must be frozen.
2039 if Nkind (Parent (Actual)) =
2040 N_Package_Renaming_Declaration
2041 then
2042 Set_Has_Delayed_Freeze
2043 (Renamed_Entity (Actual));
2044 Append_Elmt
2045 (Renamed_Entity (Actual),
2046 Actuals_To_Freeze);
2047 else
2048 Set_Has_Delayed_Freeze (Actual);
2049 Append_Elmt (Actual, Actuals_To_Freeze);
2050 end if;
2051 end if;
2052 end if;
2053 end Explicit_Freeze_Check;
2054 end if;
2056 -- For use type and use package appearing in the generic part,
2057 -- we have already copied them, so we can just move them where
2058 -- they belong (we mustn't recopy them since this would mess up
2059 -- the Sloc values).
2061 when N_Use_Package_Clause
2062 | N_Use_Type_Clause
2064 if Nkind (Original_Node (I_Node)) =
2065 N_Formal_Package_Declaration
2066 then
2067 Append (New_Copy_Tree (Formal), Assoc_List);
2068 else
2069 Remove (Formal);
2070 Append (Formal, Assoc_List);
2071 end if;
2073 when others =>
2074 raise Program_Error;
2075 end case;
2077 Formal := Saved_Formal;
2078 Next_Non_Pragma (Analyzed_Formal);
2079 end loop;
2081 if Num_Actuals > Num_Matched then
2082 Error_Msg_Sloc := Sloc (Gen_Unit);
2084 if Present (Selector_Name (Actual)) then
2085 Error_Msg_NE
2086 ("unmatched actual &", Actual, Selector_Name (Actual));
2087 Error_Msg_NE
2088 ("\in instantiation of & declared#", Actual, Gen_Unit);
2089 else
2090 Error_Msg_NE
2091 ("unmatched actual in instantiation of & declared#",
2092 Actual, Gen_Unit);
2093 end if;
2094 end if;
2096 elsif Present (Actuals) then
2097 Error_Msg_N
2098 ("too many actuals in generic instantiation", Instantiation_Node);
2099 end if;
2101 -- An instantiation freezes all generic actuals. The only exceptions
2102 -- to this are incomplete types and subprograms which are not fully
2103 -- defined at the point of instantiation.
2105 declare
2106 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2107 begin
2108 while Present (Elmt) loop
2109 Freeze_Before (I_Node, Node (Elmt));
2110 Next_Elmt (Elmt);
2111 end loop;
2112 end;
2114 -- If there are default subprograms, normalize the tree by adding
2115 -- explicit associations for them. This is required if the instance
2116 -- appears within a generic.
2118 if not Is_Empty_List (Default_Actuals) then
2119 declare
2120 Default : Node_Id;
2122 begin
2123 Default := First (Default_Actuals);
2124 while Present (Default) loop
2125 Mark_Rewrite_Insertion (Default);
2126 Next (Default);
2127 end loop;
2129 if No (Actuals) then
2130 Set_Generic_Associations (I_Node, Default_Actuals);
2131 else
2132 Append_List_To (Actuals, Default_Actuals);
2133 end if;
2134 end;
2135 end if;
2137 -- If this is a formal package, normalize the parameter list by adding
2138 -- explicit box associations for the formals that are covered by an
2139 -- Others_Choice.
2141 if not Is_Empty_List (Default_Formals) then
2142 Append_List (Default_Formals, Formals);
2143 end if;
2145 return Assoc_List;
2146 end Analyze_Associations;
2148 -------------------------------
2149 -- Analyze_Formal_Array_Type --
2150 -------------------------------
2152 procedure Analyze_Formal_Array_Type
2153 (T : in out Entity_Id;
2154 Def : Node_Id)
2156 DSS : Node_Id;
2158 begin
2159 -- Treated like a non-generic array declaration, with additional
2160 -- semantic checks.
2162 Enter_Name (T);
2164 if Nkind (Def) = N_Constrained_Array_Definition then
2165 DSS := First (Discrete_Subtype_Definitions (Def));
2166 while Present (DSS) loop
2167 if Nkind_In (DSS, N_Subtype_Indication,
2168 N_Range,
2169 N_Attribute_Reference)
2170 then
2171 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2172 end if;
2174 Next (DSS);
2175 end loop;
2176 end if;
2178 Array_Type_Declaration (T, Def);
2179 Set_Is_Generic_Type (Base_Type (T));
2181 if Ekind (Component_Type (T)) = E_Incomplete_Type
2182 and then No (Full_View (Component_Type (T)))
2183 then
2184 Error_Msg_N ("premature usage of incomplete type", Def);
2186 -- Check that range constraint is not allowed on the component type
2187 -- of a generic formal array type (AARM 12.5.3(3))
2189 elsif Is_Internal (Component_Type (T))
2190 and then Present (Subtype_Indication (Component_Definition (Def)))
2191 and then Nkind (Original_Node
2192 (Subtype_Indication (Component_Definition (Def)))) =
2193 N_Subtype_Indication
2194 then
2195 Error_Msg_N
2196 ("in a formal, a subtype indication can only be "
2197 & "a subtype mark (RM 12.5.3(3))",
2198 Subtype_Indication (Component_Definition (Def)));
2199 end if;
2201 end Analyze_Formal_Array_Type;
2203 ---------------------------------------------
2204 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2205 ---------------------------------------------
2207 -- As for other generic types, we create a valid type representation with
2208 -- legal but arbitrary attributes, whose values are never considered
2209 -- static. For all scalar types we introduce an anonymous base type, with
2210 -- the same attributes. We choose the corresponding integer type to be
2211 -- Standard_Integer.
2212 -- Here and in other similar routines, the Sloc of the generated internal
2213 -- type must be the same as the sloc of the defining identifier of the
2214 -- formal type declaration, to provide proper source navigation.
2216 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2217 (T : Entity_Id;
2218 Def : Node_Id)
2220 Loc : constant Source_Ptr := Sloc (Def);
2222 Base : constant Entity_Id :=
2223 New_Internal_Entity
2224 (E_Decimal_Fixed_Point_Type,
2225 Current_Scope,
2226 Sloc (Defining_Identifier (Parent (Def))), 'G');
2228 Int_Base : constant Entity_Id := Standard_Integer;
2229 Delta_Val : constant Ureal := Ureal_1;
2230 Digs_Val : constant Uint := Uint_6;
2232 function Make_Dummy_Bound return Node_Id;
2233 -- Return a properly typed universal real literal to use as a bound
2235 ----------------------
2236 -- Make_Dummy_Bound --
2237 ----------------------
2239 function Make_Dummy_Bound return Node_Id is
2240 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2241 begin
2242 Set_Etype (Bound, Universal_Real);
2243 return Bound;
2244 end Make_Dummy_Bound;
2246 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2248 begin
2249 Enter_Name (T);
2251 Set_Etype (Base, Base);
2252 Set_Size_Info (Base, Int_Base);
2253 Set_RM_Size (Base, RM_Size (Int_Base));
2254 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2255 Set_Digits_Value (Base, Digs_Val);
2256 Set_Delta_Value (Base, Delta_Val);
2257 Set_Small_Value (Base, Delta_Val);
2258 Set_Scalar_Range (Base,
2259 Make_Range (Loc,
2260 Low_Bound => Make_Dummy_Bound,
2261 High_Bound => Make_Dummy_Bound));
2263 Set_Is_Generic_Type (Base);
2264 Set_Parent (Base, Parent (Def));
2266 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2267 Set_Etype (T, Base);
2268 Set_Size_Info (T, Int_Base);
2269 Set_RM_Size (T, RM_Size (Int_Base));
2270 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2271 Set_Digits_Value (T, Digs_Val);
2272 Set_Delta_Value (T, Delta_Val);
2273 Set_Small_Value (T, Delta_Val);
2274 Set_Scalar_Range (T, Scalar_Range (Base));
2275 Set_Is_Constrained (T);
2277 Check_Restriction (No_Fixed_Point, Def);
2278 end Analyze_Formal_Decimal_Fixed_Point_Type;
2280 -------------------------------------------
2281 -- Analyze_Formal_Derived_Interface_Type --
2282 -------------------------------------------
2284 procedure Analyze_Formal_Derived_Interface_Type
2285 (N : Node_Id;
2286 T : Entity_Id;
2287 Def : Node_Id)
2289 Loc : constant Source_Ptr := Sloc (Def);
2291 begin
2292 -- Rewrite as a type declaration of a derived type. This ensures that
2293 -- the interface list and primitive operations are properly captured.
2295 Rewrite (N,
2296 Make_Full_Type_Declaration (Loc,
2297 Defining_Identifier => T,
2298 Type_Definition => Def));
2299 Analyze (N);
2300 Set_Is_Generic_Type (T);
2301 end Analyze_Formal_Derived_Interface_Type;
2303 ---------------------------------
2304 -- Analyze_Formal_Derived_Type --
2305 ---------------------------------
2307 procedure Analyze_Formal_Derived_Type
2308 (N : Node_Id;
2309 T : Entity_Id;
2310 Def : Node_Id)
2312 Loc : constant Source_Ptr := Sloc (Def);
2313 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2314 New_N : Node_Id;
2316 begin
2317 Set_Is_Generic_Type (T);
2319 if Private_Present (Def) then
2320 New_N :=
2321 Make_Private_Extension_Declaration (Loc,
2322 Defining_Identifier => T,
2323 Discriminant_Specifications => Discriminant_Specifications (N),
2324 Unknown_Discriminants_Present => Unk_Disc,
2325 Subtype_Indication => Subtype_Mark (Def),
2326 Interface_List => Interface_List (Def));
2328 Set_Abstract_Present (New_N, Abstract_Present (Def));
2329 Set_Limited_Present (New_N, Limited_Present (Def));
2330 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2332 else
2333 New_N :=
2334 Make_Full_Type_Declaration (Loc,
2335 Defining_Identifier => T,
2336 Discriminant_Specifications =>
2337 Discriminant_Specifications (Parent (T)),
2338 Type_Definition =>
2339 Make_Derived_Type_Definition (Loc,
2340 Subtype_Indication => Subtype_Mark (Def)));
2342 Set_Abstract_Present
2343 (Type_Definition (New_N), Abstract_Present (Def));
2344 Set_Limited_Present
2345 (Type_Definition (New_N), Limited_Present (Def));
2346 end if;
2348 Rewrite (N, New_N);
2349 Analyze (N);
2351 if Unk_Disc then
2352 if not Is_Composite_Type (T) then
2353 Error_Msg_N
2354 ("unknown discriminants not allowed for elementary types", N);
2355 else
2356 Set_Has_Unknown_Discriminants (T);
2357 Set_Is_Constrained (T, False);
2358 end if;
2359 end if;
2361 -- If the parent type has a known size, so does the formal, which makes
2362 -- legal representation clauses that involve the formal.
2364 Set_Size_Known_At_Compile_Time
2365 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2366 end Analyze_Formal_Derived_Type;
2368 ----------------------------------
2369 -- Analyze_Formal_Discrete_Type --
2370 ----------------------------------
2372 -- The operations defined for a discrete types are those of an enumeration
2373 -- type. The size is set to an arbitrary value, for use in analyzing the
2374 -- generic unit.
2376 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2377 Loc : constant Source_Ptr := Sloc (Def);
2378 Lo : Node_Id;
2379 Hi : Node_Id;
2381 Base : constant Entity_Id :=
2382 New_Internal_Entity
2383 (E_Floating_Point_Type, Current_Scope,
2384 Sloc (Defining_Identifier (Parent (Def))), 'G');
2386 begin
2387 Enter_Name (T);
2388 Set_Ekind (T, E_Enumeration_Subtype);
2389 Set_Etype (T, Base);
2390 Init_Size (T, 8);
2391 Init_Alignment (T);
2392 Set_Is_Generic_Type (T);
2393 Set_Is_Constrained (T);
2395 -- For semantic analysis, the bounds of the type must be set to some
2396 -- non-static value. The simplest is to create attribute nodes for those
2397 -- bounds, that refer to the type itself. These bounds are never
2398 -- analyzed but serve as place-holders.
2400 Lo :=
2401 Make_Attribute_Reference (Loc,
2402 Attribute_Name => Name_First,
2403 Prefix => New_Occurrence_Of (T, Loc));
2404 Set_Etype (Lo, T);
2406 Hi :=
2407 Make_Attribute_Reference (Loc,
2408 Attribute_Name => Name_Last,
2409 Prefix => New_Occurrence_Of (T, Loc));
2410 Set_Etype (Hi, T);
2412 Set_Scalar_Range (T,
2413 Make_Range (Loc,
2414 Low_Bound => Lo,
2415 High_Bound => Hi));
2417 Set_Ekind (Base, E_Enumeration_Type);
2418 Set_Etype (Base, Base);
2419 Init_Size (Base, 8);
2420 Init_Alignment (Base);
2421 Set_Is_Generic_Type (Base);
2422 Set_Scalar_Range (Base, Scalar_Range (T));
2423 Set_Parent (Base, Parent (Def));
2424 end Analyze_Formal_Discrete_Type;
2426 ----------------------------------
2427 -- Analyze_Formal_Floating_Type --
2428 ---------------------------------
2430 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2431 Base : constant Entity_Id :=
2432 New_Internal_Entity
2433 (E_Floating_Point_Type, Current_Scope,
2434 Sloc (Defining_Identifier (Parent (Def))), 'G');
2436 begin
2437 -- The various semantic attributes are taken from the predefined type
2438 -- Float, just so that all of them are initialized. Their values are
2439 -- never used because no constant folding or expansion takes place in
2440 -- the generic itself.
2442 Enter_Name (T);
2443 Set_Ekind (T, E_Floating_Point_Subtype);
2444 Set_Etype (T, Base);
2445 Set_Size_Info (T, (Standard_Float));
2446 Set_RM_Size (T, RM_Size (Standard_Float));
2447 Set_Digits_Value (T, Digits_Value (Standard_Float));
2448 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2449 Set_Is_Constrained (T);
2451 Set_Is_Generic_Type (Base);
2452 Set_Etype (Base, Base);
2453 Set_Size_Info (Base, (Standard_Float));
2454 Set_RM_Size (Base, RM_Size (Standard_Float));
2455 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2456 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2457 Set_Parent (Base, Parent (Def));
2459 Check_Restriction (No_Floating_Point, Def);
2460 end Analyze_Formal_Floating_Type;
2462 -----------------------------------
2463 -- Analyze_Formal_Interface_Type;--
2464 -----------------------------------
2466 procedure Analyze_Formal_Interface_Type
2467 (N : Node_Id;
2468 T : Entity_Id;
2469 Def : Node_Id)
2471 Loc : constant Source_Ptr := Sloc (N);
2472 New_N : Node_Id;
2474 begin
2475 New_N :=
2476 Make_Full_Type_Declaration (Loc,
2477 Defining_Identifier => T,
2478 Type_Definition => Def);
2480 Rewrite (N, New_N);
2481 Analyze (N);
2482 Set_Is_Generic_Type (T);
2483 end Analyze_Formal_Interface_Type;
2485 ---------------------------------
2486 -- Analyze_Formal_Modular_Type --
2487 ---------------------------------
2489 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2490 begin
2491 -- Apart from their entity kind, generic modular types are treated like
2492 -- signed integer types, and have the same attributes.
2494 Analyze_Formal_Signed_Integer_Type (T, Def);
2495 Set_Ekind (T, E_Modular_Integer_Subtype);
2496 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2498 end Analyze_Formal_Modular_Type;
2500 ---------------------------------------
2501 -- Analyze_Formal_Object_Declaration --
2502 ---------------------------------------
2504 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2505 E : constant Node_Id := Default_Expression (N);
2506 Id : constant Node_Id := Defining_Identifier (N);
2507 K : Entity_Kind;
2508 T : Node_Id;
2510 begin
2511 Enter_Name (Id);
2513 -- Determine the mode of the formal object
2515 if Out_Present (N) then
2516 K := E_Generic_In_Out_Parameter;
2518 if not In_Present (N) then
2519 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2520 end if;
2522 else
2523 K := E_Generic_In_Parameter;
2524 end if;
2526 if Present (Subtype_Mark (N)) then
2527 Find_Type (Subtype_Mark (N));
2528 T := Entity (Subtype_Mark (N));
2530 -- Verify that there is no redundant null exclusion
2532 if Null_Exclusion_Present (N) then
2533 if not Is_Access_Type (T) then
2534 Error_Msg_N
2535 ("null exclusion can only apply to an access type", N);
2537 elsif Can_Never_Be_Null (T) then
2538 Error_Msg_NE
2539 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2540 end if;
2541 end if;
2543 -- Ada 2005 (AI-423): Formal object with an access definition
2545 else
2546 Check_Access_Definition (N);
2547 T := Access_Definition
2548 (Related_Nod => N,
2549 N => Access_Definition (N));
2550 end if;
2552 if Ekind (T) = E_Incomplete_Type then
2553 declare
2554 Error_Node : Node_Id;
2556 begin
2557 if Present (Subtype_Mark (N)) then
2558 Error_Node := Subtype_Mark (N);
2559 else
2560 Check_Access_Definition (N);
2561 Error_Node := Access_Definition (N);
2562 end if;
2564 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2565 end;
2566 end if;
2568 if K = E_Generic_In_Parameter then
2570 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2572 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2573 Error_Msg_N
2574 ("generic formal of mode IN must not be of limited type", N);
2575 Explain_Limited_Type (T, N);
2576 end if;
2578 if Is_Abstract_Type (T) then
2579 Error_Msg_N
2580 ("generic formal of mode IN must not be of abstract type", N);
2581 end if;
2583 if Present (E) then
2584 Preanalyze_Spec_Expression (E, T);
2586 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2587 Error_Msg_N
2588 ("initialization not allowed for limited types", E);
2589 Explain_Limited_Type (T, E);
2590 end if;
2591 end if;
2593 Set_Ekind (Id, K);
2594 Set_Etype (Id, T);
2596 -- Case of generic IN OUT parameter
2598 else
2599 -- If the formal has an unconstrained type, construct its actual
2600 -- subtype, as is done for subprogram formals. In this fashion, all
2601 -- its uses can refer to specific bounds.
2603 Set_Ekind (Id, K);
2604 Set_Etype (Id, T);
2606 if (Is_Array_Type (T) and then not Is_Constrained (T))
2607 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2608 then
2609 declare
2610 Non_Freezing_Ref : constant Node_Id :=
2611 New_Occurrence_Of (Id, Sloc (Id));
2612 Decl : Node_Id;
2614 begin
2615 -- Make sure the actual subtype doesn't generate bogus freezing
2617 Set_Must_Not_Freeze (Non_Freezing_Ref);
2618 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2619 Insert_Before_And_Analyze (N, Decl);
2620 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2621 end;
2622 else
2623 Set_Actual_Subtype (Id, T);
2624 end if;
2626 if Present (E) then
2627 Error_Msg_N
2628 ("initialization not allowed for `IN OUT` formals", N);
2629 end if;
2630 end if;
2632 if Has_Aspects (N) then
2633 Analyze_Aspect_Specifications (N, Id);
2634 end if;
2635 end Analyze_Formal_Object_Declaration;
2637 ----------------------------------------------
2638 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2639 ----------------------------------------------
2641 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2642 (T : Entity_Id;
2643 Def : Node_Id)
2645 Loc : constant Source_Ptr := Sloc (Def);
2646 Base : constant Entity_Id :=
2647 New_Internal_Entity
2648 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2649 Sloc (Defining_Identifier (Parent (Def))), 'G');
2651 begin
2652 -- The semantic attributes are set for completeness only, their values
2653 -- will never be used, since all properties of the type are non-static.
2655 Enter_Name (T);
2656 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2657 Set_Etype (T, Base);
2658 Set_Size_Info (T, Standard_Integer);
2659 Set_RM_Size (T, RM_Size (Standard_Integer));
2660 Set_Small_Value (T, Ureal_1);
2661 Set_Delta_Value (T, Ureal_1);
2662 Set_Scalar_Range (T,
2663 Make_Range (Loc,
2664 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2665 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2666 Set_Is_Constrained (T);
2668 Set_Is_Generic_Type (Base);
2669 Set_Etype (Base, Base);
2670 Set_Size_Info (Base, Standard_Integer);
2671 Set_RM_Size (Base, RM_Size (Standard_Integer));
2672 Set_Small_Value (Base, Ureal_1);
2673 Set_Delta_Value (Base, Ureal_1);
2674 Set_Scalar_Range (Base, Scalar_Range (T));
2675 Set_Parent (Base, Parent (Def));
2677 Check_Restriction (No_Fixed_Point, Def);
2678 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2680 ----------------------------------------
2681 -- Analyze_Formal_Package_Declaration --
2682 ----------------------------------------
2684 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2685 Gen_Id : constant Node_Id := Name (N);
2686 Loc : constant Source_Ptr := Sloc (N);
2687 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2688 Formal : Entity_Id;
2689 Gen_Decl : Node_Id;
2690 Gen_Unit : Entity_Id;
2691 Renaming : Node_Id;
2693 Vis_Prims_List : Elist_Id := No_Elist;
2694 -- List of primitives made temporarily visible in the instantiation
2695 -- to match the visibility of the formal type.
2697 function Build_Local_Package return Node_Id;
2698 -- The formal package is rewritten so that its parameters are replaced
2699 -- with corresponding declarations. For parameters with bona fide
2700 -- associations these declarations are created by Analyze_Associations
2701 -- as for a regular instantiation. For boxed parameters, we preserve
2702 -- the formal declarations and analyze them, in order to introduce
2703 -- entities of the right kind in the environment of the formal.
2705 -------------------------
2706 -- Build_Local_Package --
2707 -------------------------
2709 function Build_Local_Package return Node_Id is
2710 Decls : List_Id;
2711 Pack_Decl : Node_Id;
2713 begin
2714 -- Within the formal, the name of the generic package is a renaming
2715 -- of the formal (as for a regular instantiation).
2717 Pack_Decl :=
2718 Make_Package_Declaration (Loc,
2719 Specification =>
2720 Copy_Generic_Node
2721 (Specification (Original_Node (Gen_Decl)),
2722 Empty, Instantiating => True));
2724 Renaming :=
2725 Make_Package_Renaming_Declaration (Loc,
2726 Defining_Unit_Name =>
2727 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2728 Name => New_Occurrence_Of (Formal, Loc));
2730 if Nkind (Gen_Id) = N_Identifier
2731 and then Chars (Gen_Id) = Chars (Pack_Id)
2732 then
2733 Error_Msg_NE
2734 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2735 end if;
2737 -- If the formal is declared with a box, or with an others choice,
2738 -- create corresponding declarations for all entities in the formal
2739 -- part, so that names with the proper types are available in the
2740 -- specification of the formal package.
2742 -- On the other hand, if there are no associations, then all the
2743 -- formals must have defaults, and this will be checked by the
2744 -- call to Analyze_Associations.
2746 if Box_Present (N)
2747 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2748 then
2749 declare
2750 Formal_Decl : Node_Id;
2752 begin
2753 -- TBA : for a formal package, need to recurse ???
2755 Decls := New_List;
2756 Formal_Decl :=
2757 First
2758 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2759 while Present (Formal_Decl) loop
2760 Append_To
2761 (Decls,
2762 Copy_Generic_Node
2763 (Formal_Decl, Empty, Instantiating => True));
2764 Next (Formal_Decl);
2765 end loop;
2766 end;
2768 -- If generic associations are present, use Analyze_Associations to
2769 -- create the proper renaming declarations.
2771 else
2772 declare
2773 Act_Tree : constant Node_Id :=
2774 Copy_Generic_Node
2775 (Original_Node (Gen_Decl), Empty,
2776 Instantiating => True);
2778 begin
2779 Generic_Renamings.Set_Last (0);
2780 Generic_Renamings_HTable.Reset;
2781 Instantiation_Node := N;
2783 Decls :=
2784 Analyze_Associations
2785 (I_Node => Original_Node (N),
2786 Formals => Generic_Formal_Declarations (Act_Tree),
2787 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2789 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2790 end;
2791 end if;
2793 Append (Renaming, To => Decls);
2795 -- Add generated declarations ahead of local declarations in
2796 -- the package.
2798 if No (Visible_Declarations (Specification (Pack_Decl))) then
2799 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2800 else
2801 Insert_List_Before
2802 (First (Visible_Declarations (Specification (Pack_Decl))),
2803 Decls);
2804 end if;
2806 return Pack_Decl;
2807 end Build_Local_Package;
2809 -- Local variables
2811 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
2812 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2814 Associations : Boolean := True;
2815 New_N : Node_Id;
2816 Parent_Installed : Boolean := False;
2817 Parent_Instance : Entity_Id;
2818 Renaming_In_Par : Entity_Id;
2820 -- Start of processing for Analyze_Formal_Package_Declaration
2822 begin
2823 Check_Text_IO_Special_Unit (Gen_Id);
2825 Init_Env;
2826 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2827 Gen_Unit := Entity (Gen_Id);
2829 -- Check for a formal package that is a package renaming
2831 if Present (Renamed_Object (Gen_Unit)) then
2833 -- Indicate that unit is used, before replacing it with renamed
2834 -- entity for use below.
2836 if In_Extended_Main_Source_Unit (N) then
2837 Set_Is_Instantiated (Gen_Unit);
2838 Generate_Reference (Gen_Unit, N);
2839 end if;
2841 Gen_Unit := Renamed_Object (Gen_Unit);
2842 end if;
2844 if Ekind (Gen_Unit) /= E_Generic_Package then
2845 Error_Msg_N ("expect generic package name", Gen_Id);
2846 Restore_Env;
2847 goto Leave;
2849 elsif Gen_Unit = Current_Scope then
2850 Error_Msg_N
2851 ("generic package cannot be used as a formal package of itself",
2852 Gen_Id);
2853 Restore_Env;
2854 goto Leave;
2856 elsif In_Open_Scopes (Gen_Unit) then
2857 if Is_Compilation_Unit (Gen_Unit)
2858 and then Is_Child_Unit (Current_Scope)
2859 then
2860 -- Special-case the error when the formal is a parent, and
2861 -- continue analysis to minimize cascaded errors.
2863 Error_Msg_N
2864 ("generic parent cannot be used as formal package of a child "
2865 & "unit", Gen_Id);
2867 else
2868 Error_Msg_N
2869 ("generic package cannot be used as a formal package within "
2870 & "itself", Gen_Id);
2871 Restore_Env;
2872 goto Leave;
2873 end if;
2874 end if;
2876 -- Check that name of formal package does not hide name of generic,
2877 -- or its leading prefix. This check must be done separately because
2878 -- the name of the generic has already been analyzed.
2880 declare
2881 Gen_Name : Entity_Id;
2883 begin
2884 Gen_Name := Gen_Id;
2885 while Nkind (Gen_Name) = N_Expanded_Name loop
2886 Gen_Name := Prefix (Gen_Name);
2887 end loop;
2889 if Chars (Gen_Name) = Chars (Pack_Id) then
2890 Error_Msg_NE
2891 ("& is hidden within declaration of formal package",
2892 Gen_Id, Gen_Name);
2893 end if;
2894 end;
2896 if Box_Present (N)
2897 or else No (Generic_Associations (N))
2898 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2899 then
2900 Associations := False;
2901 end if;
2903 -- If there are no generic associations, the generic parameters appear
2904 -- as local entities and are instantiated like them. We copy the generic
2905 -- package declaration as if it were an instantiation, and analyze it
2906 -- like a regular package, except that we treat the formals as
2907 -- additional visible components.
2909 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2911 if In_Extended_Main_Source_Unit (N) then
2912 Set_Is_Instantiated (Gen_Unit);
2913 Generate_Reference (Gen_Unit, N);
2914 end if;
2916 Formal := New_Copy (Pack_Id);
2917 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
2919 -- Make local generic without formals. The formals will be replaced with
2920 -- internal declarations.
2922 begin
2923 New_N := Build_Local_Package;
2925 -- If there are errors in the parameter list, Analyze_Associations
2926 -- raises Instantiation_Error. Patch the declaration to prevent further
2927 -- exception propagation.
2929 exception
2930 when Instantiation_Error =>
2931 Enter_Name (Formal);
2932 Set_Ekind (Formal, E_Variable);
2933 Set_Etype (Formal, Any_Type);
2934 Restore_Hidden_Primitives (Vis_Prims_List);
2936 if Parent_Installed then
2937 Remove_Parent;
2938 end if;
2940 goto Leave;
2941 end;
2943 Rewrite (N, New_N);
2944 Set_Defining_Unit_Name (Specification (New_N), Formal);
2945 Set_Generic_Parent (Specification (N), Gen_Unit);
2946 Set_Instance_Env (Gen_Unit, Formal);
2947 Set_Is_Generic_Instance (Formal);
2949 Enter_Name (Formal);
2950 Set_Ekind (Formal, E_Package);
2951 Set_Etype (Formal, Standard_Void_Type);
2952 Set_Inner_Instances (Formal, New_Elmt_List);
2953 Push_Scope (Formal);
2955 -- Manually set the SPARK_Mode from the context because the package
2956 -- declaration is never analyzed.
2958 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
2959 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
2960 Set_SPARK_Pragma_Inherited (Formal);
2961 Set_SPARK_Aux_Pragma_Inherited (Formal);
2963 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2965 -- Similarly, we have to make the name of the formal visible in the
2966 -- parent instance, to resolve properly fully qualified names that
2967 -- may appear in the generic unit. The parent instance has been
2968 -- placed on the scope stack ahead of the current scope.
2970 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2972 Renaming_In_Par :=
2973 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2974 Set_Ekind (Renaming_In_Par, E_Package);
2975 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2976 Set_Scope (Renaming_In_Par, Parent_Instance);
2977 Set_Parent (Renaming_In_Par, Parent (Formal));
2978 Set_Renamed_Object (Renaming_In_Par, Formal);
2979 Append_Entity (Renaming_In_Par, Parent_Instance);
2980 end if;
2982 -- A formal package declaration behaves as a package instantiation with
2983 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
2984 -- missing, set the global flag which signals Analyze_Pragma to ingnore
2985 -- all SPARK_Mode pragmas within the generic_package_name.
2987 if SPARK_Mode /= On then
2988 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
2990 -- Mark the formal spec in case the body is instantiated at a later
2991 -- pass. This preserves the original context in effect for the body.
2993 Set_Ignore_SPARK_Mode_Pragmas (Formal);
2994 end if;
2996 Analyze (Specification (N));
2998 -- The formals for which associations are provided are not visible
2999 -- outside of the formal package. The others are still declared by a
3000 -- formal parameter declaration.
3002 -- If there are no associations, the only local entity to hide is the
3003 -- generated package renaming itself.
3005 declare
3006 E : Entity_Id;
3008 begin
3009 E := First_Entity (Formal);
3010 while Present (E) loop
3011 if Associations and then not Is_Generic_Formal (E) then
3012 Set_Is_Hidden (E);
3013 end if;
3015 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3016 Set_Is_Hidden (E);
3017 exit;
3018 end if;
3020 Next_Entity (E);
3021 end loop;
3022 end;
3024 End_Package_Scope (Formal);
3025 Restore_Hidden_Primitives (Vis_Prims_List);
3027 if Parent_Installed then
3028 Remove_Parent;
3029 end if;
3031 Restore_Env;
3033 -- Inside the generic unit, the formal package is a regular package, but
3034 -- no body is needed for it. Note that after instantiation, the defining
3035 -- unit name we need is in the new tree and not in the original (see
3036 -- Package_Instantiation). A generic formal package is an instance, and
3037 -- can be used as an actual for an inner instance.
3039 Set_Has_Completion (Formal, True);
3041 -- Add semantic information to the original defining identifier for ASIS
3042 -- use.
3044 Set_Ekind (Pack_Id, E_Package);
3045 Set_Etype (Pack_Id, Standard_Void_Type);
3046 Set_Scope (Pack_Id, Scope (Formal));
3047 Set_Has_Completion (Pack_Id, True);
3049 <<Leave>>
3050 if Has_Aspects (N) then
3051 Analyze_Aspect_Specifications (N, Pack_Id);
3052 end if;
3054 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
3055 end Analyze_Formal_Package_Declaration;
3057 ---------------------------------
3058 -- Analyze_Formal_Private_Type --
3059 ---------------------------------
3061 procedure Analyze_Formal_Private_Type
3062 (N : Node_Id;
3063 T : Entity_Id;
3064 Def : Node_Id)
3066 begin
3067 New_Private_Type (N, T, Def);
3069 -- Set the size to an arbitrary but legal value
3071 Set_Size_Info (T, Standard_Integer);
3072 Set_RM_Size (T, RM_Size (Standard_Integer));
3073 end Analyze_Formal_Private_Type;
3075 ------------------------------------
3076 -- Analyze_Formal_Incomplete_Type --
3077 ------------------------------------
3079 procedure Analyze_Formal_Incomplete_Type
3080 (T : Entity_Id;
3081 Def : Node_Id)
3083 begin
3084 Enter_Name (T);
3085 Set_Ekind (T, E_Incomplete_Type);
3086 Set_Etype (T, T);
3087 Set_Private_Dependents (T, New_Elmt_List);
3089 if Tagged_Present (Def) then
3090 Set_Is_Tagged_Type (T);
3091 Make_Class_Wide_Type (T);
3092 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3093 end if;
3094 end Analyze_Formal_Incomplete_Type;
3096 ----------------------------------------
3097 -- Analyze_Formal_Signed_Integer_Type --
3098 ----------------------------------------
3100 procedure Analyze_Formal_Signed_Integer_Type
3101 (T : Entity_Id;
3102 Def : Node_Id)
3104 Base : constant Entity_Id :=
3105 New_Internal_Entity
3106 (E_Signed_Integer_Type,
3107 Current_Scope,
3108 Sloc (Defining_Identifier (Parent (Def))), 'G');
3110 begin
3111 Enter_Name (T);
3113 Set_Ekind (T, E_Signed_Integer_Subtype);
3114 Set_Etype (T, Base);
3115 Set_Size_Info (T, Standard_Integer);
3116 Set_RM_Size (T, RM_Size (Standard_Integer));
3117 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3118 Set_Is_Constrained (T);
3120 Set_Is_Generic_Type (Base);
3121 Set_Size_Info (Base, Standard_Integer);
3122 Set_RM_Size (Base, RM_Size (Standard_Integer));
3123 Set_Etype (Base, Base);
3124 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3125 Set_Parent (Base, Parent (Def));
3126 end Analyze_Formal_Signed_Integer_Type;
3128 -------------------------------------------
3129 -- Analyze_Formal_Subprogram_Declaration --
3130 -------------------------------------------
3132 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3133 Spec : constant Node_Id := Specification (N);
3134 Def : constant Node_Id := Default_Name (N);
3135 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3136 Subp : Entity_Id;
3138 begin
3139 if Nam = Error then
3140 return;
3141 end if;
3143 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3144 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3145 goto Leave;
3146 end if;
3148 Analyze_Subprogram_Declaration (N);
3149 Set_Is_Formal_Subprogram (Nam);
3150 Set_Has_Completion (Nam);
3152 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
3153 Set_Is_Abstract_Subprogram (Nam);
3155 Set_Is_Dispatching_Operation (Nam);
3157 -- A formal abstract procedure cannot have a null default
3158 -- (RM 12.6(4.1/2)).
3160 if Nkind (Spec) = N_Procedure_Specification
3161 and then Null_Present (Spec)
3162 then
3163 Error_Msg_N
3164 ("a formal abstract subprogram cannot default to null", Spec);
3165 end if;
3167 declare
3168 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3169 begin
3170 if No (Ctrl_Type) then
3171 Error_Msg_N
3172 ("abstract formal subprogram must have a controlling type",
3175 elsif Ada_Version >= Ada_2012
3176 and then Is_Incomplete_Type (Ctrl_Type)
3177 then
3178 Error_Msg_NE
3179 ("controlling type of abstract formal subprogram cannot "
3180 & "be incomplete type", N, Ctrl_Type);
3182 else
3183 Check_Controlling_Formals (Ctrl_Type, Nam);
3184 end if;
3185 end;
3186 end if;
3188 -- Default name is resolved at the point of instantiation
3190 if Box_Present (N) then
3191 null;
3193 -- Else default is bound at the point of generic declaration
3195 elsif Present (Def) then
3196 if Nkind (Def) = N_Operator_Symbol then
3197 Find_Direct_Name (Def);
3199 elsif Nkind (Def) /= N_Attribute_Reference then
3200 Analyze (Def);
3202 else
3203 -- For an attribute reference, analyze the prefix and verify
3204 -- that it has the proper profile for the subprogram.
3206 Analyze (Prefix (Def));
3207 Valid_Default_Attribute (Nam, Def);
3208 goto Leave;
3209 end if;
3211 -- Default name may be overloaded, in which case the interpretation
3212 -- with the correct profile must be selected, as for a renaming.
3213 -- If the definition is an indexed component, it must denote a
3214 -- member of an entry family. If it is a selected component, it
3215 -- can be a protected operation.
3217 if Etype (Def) = Any_Type then
3218 goto Leave;
3220 elsif Nkind (Def) = N_Selected_Component then
3221 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3222 Error_Msg_N ("expect valid subprogram name as default", Def);
3223 end if;
3225 elsif Nkind (Def) = N_Indexed_Component then
3226 if Is_Entity_Name (Prefix (Def)) then
3227 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3228 Error_Msg_N ("expect valid subprogram name as default", Def);
3229 end if;
3231 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3232 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3233 E_Entry_Family
3234 then
3235 Error_Msg_N ("expect valid subprogram name as default", Def);
3236 end if;
3238 else
3239 Error_Msg_N ("expect valid subprogram name as default", Def);
3240 goto Leave;
3241 end if;
3243 elsif Nkind (Def) = N_Character_Literal then
3245 -- Needs some type checks: subprogram should be parameterless???
3247 Resolve (Def, (Etype (Nam)));
3249 elsif not Is_Entity_Name (Def)
3250 or else not Is_Overloadable (Entity (Def))
3251 then
3252 Error_Msg_N ("expect valid subprogram name as default", Def);
3253 goto Leave;
3255 elsif not Is_Overloaded (Def) then
3256 Subp := Entity (Def);
3258 if Subp = Nam then
3259 Error_Msg_N ("premature usage of formal subprogram", Def);
3261 elsif not Entity_Matches_Spec (Subp, Nam) then
3262 Error_Msg_N ("no visible entity matches specification", Def);
3263 end if;
3265 -- More than one interpretation, so disambiguate as for a renaming
3267 else
3268 declare
3269 I : Interp_Index;
3270 I1 : Interp_Index := 0;
3271 It : Interp;
3272 It1 : Interp;
3274 begin
3275 Subp := Any_Id;
3276 Get_First_Interp (Def, I, It);
3277 while Present (It.Nam) loop
3278 if Entity_Matches_Spec (It.Nam, Nam) then
3279 if Subp /= Any_Id then
3280 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3282 if It1 = No_Interp then
3283 Error_Msg_N ("ambiguous default subprogram", Def);
3284 else
3285 Subp := It1.Nam;
3286 end if;
3288 exit;
3290 else
3291 I1 := I;
3292 Subp := It.Nam;
3293 end if;
3294 end if;
3296 Get_Next_Interp (I, It);
3297 end loop;
3298 end;
3300 if Subp /= Any_Id then
3302 -- Subprogram found, generate reference to it
3304 Set_Entity (Def, Subp);
3305 Generate_Reference (Subp, Def);
3307 if Subp = Nam then
3308 Error_Msg_N ("premature usage of formal subprogram", Def);
3310 elsif Ekind (Subp) /= E_Operator then
3311 Check_Mode_Conformant (Subp, Nam);
3312 end if;
3314 else
3315 Error_Msg_N ("no visible subprogram matches specification", N);
3316 end if;
3317 end if;
3318 end if;
3320 <<Leave>>
3321 if Has_Aspects (N) then
3322 Analyze_Aspect_Specifications (N, Nam);
3323 end if;
3325 end Analyze_Formal_Subprogram_Declaration;
3327 -------------------------------------
3328 -- Analyze_Formal_Type_Declaration --
3329 -------------------------------------
3331 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3332 Def : constant Node_Id := Formal_Type_Definition (N);
3333 T : Entity_Id;
3335 begin
3336 T := Defining_Identifier (N);
3338 if Present (Discriminant_Specifications (N))
3339 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3340 then
3341 Error_Msg_N
3342 ("discriminants not allowed for this formal type", T);
3343 end if;
3345 -- Enter the new name, and branch to specific routine
3347 case Nkind (Def) is
3348 when N_Formal_Private_Type_Definition =>
3349 Analyze_Formal_Private_Type (N, T, Def);
3351 when N_Formal_Derived_Type_Definition =>
3352 Analyze_Formal_Derived_Type (N, T, Def);
3354 when N_Formal_Incomplete_Type_Definition =>
3355 Analyze_Formal_Incomplete_Type (T, Def);
3357 when N_Formal_Discrete_Type_Definition =>
3358 Analyze_Formal_Discrete_Type (T, Def);
3360 when N_Formal_Signed_Integer_Type_Definition =>
3361 Analyze_Formal_Signed_Integer_Type (T, Def);
3363 when N_Formal_Modular_Type_Definition =>
3364 Analyze_Formal_Modular_Type (T, Def);
3366 when N_Formal_Floating_Point_Definition =>
3367 Analyze_Formal_Floating_Type (T, Def);
3369 when N_Formal_Ordinary_Fixed_Point_Definition =>
3370 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3372 when N_Formal_Decimal_Fixed_Point_Definition =>
3373 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3375 when N_Array_Type_Definition =>
3376 Analyze_Formal_Array_Type (T, Def);
3378 when N_Access_Function_Definition
3379 | N_Access_Procedure_Definition
3380 | N_Access_To_Object_Definition
3382 Analyze_Generic_Access_Type (T, Def);
3384 -- Ada 2005: a interface declaration is encoded as an abstract
3385 -- record declaration or a abstract type derivation.
3387 when N_Record_Definition =>
3388 Analyze_Formal_Interface_Type (N, T, Def);
3390 when N_Derived_Type_Definition =>
3391 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3393 when N_Error =>
3394 null;
3396 when others =>
3397 raise Program_Error;
3398 end case;
3400 Set_Is_Generic_Type (T);
3402 if Has_Aspects (N) then
3403 Analyze_Aspect_Specifications (N, T);
3404 end if;
3405 end Analyze_Formal_Type_Declaration;
3407 ------------------------------------
3408 -- Analyze_Function_Instantiation --
3409 ------------------------------------
3411 procedure Analyze_Function_Instantiation (N : Node_Id) is
3412 begin
3413 Analyze_Subprogram_Instantiation (N, E_Function);
3414 end Analyze_Function_Instantiation;
3416 ---------------------------------
3417 -- Analyze_Generic_Access_Type --
3418 ---------------------------------
3420 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3421 begin
3422 Enter_Name (T);
3424 if Nkind (Def) = N_Access_To_Object_Definition then
3425 Access_Type_Declaration (T, Def);
3427 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3428 and then No (Full_View (Designated_Type (T)))
3429 and then not Is_Generic_Type (Designated_Type (T))
3430 then
3431 Error_Msg_N ("premature usage of incomplete type", Def);
3433 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3434 Error_Msg_N
3435 ("only a subtype mark is allowed in a formal", Def);
3436 end if;
3438 else
3439 Access_Subprogram_Declaration (T, Def);
3440 end if;
3441 end Analyze_Generic_Access_Type;
3443 ---------------------------------
3444 -- Analyze_Generic_Formal_Part --
3445 ---------------------------------
3447 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3448 Gen_Parm_Decl : Node_Id;
3450 begin
3451 -- The generic formals are processed in the scope of the generic unit,
3452 -- where they are immediately visible. The scope is installed by the
3453 -- caller.
3455 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3456 while Present (Gen_Parm_Decl) loop
3457 Analyze (Gen_Parm_Decl);
3458 Next (Gen_Parm_Decl);
3459 end loop;
3461 Generate_Reference_To_Generic_Formals (Current_Scope);
3462 end Analyze_Generic_Formal_Part;
3464 ------------------------------------------
3465 -- Analyze_Generic_Package_Declaration --
3466 ------------------------------------------
3468 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3469 Decls : constant List_Id := Visible_Declarations (Specification (N));
3470 Loc : constant Source_Ptr := Sloc (N);
3472 Decl : Node_Id;
3473 Id : Entity_Id;
3474 New_N : Node_Id;
3475 Renaming : Node_Id;
3476 Save_Parent : Node_Id;
3478 begin
3479 Check_SPARK_05_Restriction ("generic is not allowed", N);
3481 -- We introduce a renaming of the enclosing package, to have a usable
3482 -- entity as the prefix of an expanded name for a local entity of the
3483 -- form Par.P.Q, where P is the generic package. This is because a local
3484 -- entity named P may hide it, so that the usual visibility rules in
3485 -- the instance will not resolve properly.
3487 Renaming :=
3488 Make_Package_Renaming_Declaration (Loc,
3489 Defining_Unit_Name =>
3490 Make_Defining_Identifier (Loc,
3491 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3492 Name =>
3493 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3495 -- The declaration is inserted before other declarations, but before
3496 -- pragmas that may be library-unit pragmas and must appear before other
3497 -- declarations. The pragma Compile_Time_Error is not in this class, and
3498 -- may contain an expression that includes such a qualified name, so the
3499 -- renaming declaration must appear before it.
3501 -- Are there other pragmas that require this special handling ???
3503 if Present (Decls) then
3504 Decl := First (Decls);
3505 while Present (Decl)
3506 and then Nkind (Decl) = N_Pragma
3507 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
3508 loop
3509 Next (Decl);
3510 end loop;
3512 if Present (Decl) then
3513 Insert_Before (Decl, Renaming);
3514 else
3515 Append (Renaming, Visible_Declarations (Specification (N)));
3516 end if;
3518 else
3519 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3520 end if;
3522 -- Create copy of generic unit, and save for instantiation. If the unit
3523 -- is a child unit, do not copy the specifications for the parent, which
3524 -- are not part of the generic tree.
3526 Save_Parent := Parent_Spec (N);
3527 Set_Parent_Spec (N, Empty);
3529 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3530 Set_Parent_Spec (New_N, Save_Parent);
3531 Rewrite (N, New_N);
3533 -- Once the contents of the generic copy and the template are swapped,
3534 -- do the same for their respective aspect specifications.
3536 Exchange_Aspects (N, New_N);
3538 -- Collect all contract-related source pragmas found within the template
3539 -- and attach them to the contract of the package spec. This contract is
3540 -- used in the capture of global references within annotations.
3542 Create_Generic_Contract (N);
3544 Id := Defining_Entity (N);
3545 Generate_Definition (Id);
3547 -- Expansion is not applied to generic units
3549 Start_Generic;
3551 Enter_Name (Id);
3552 Set_Ekind (Id, E_Generic_Package);
3553 Set_Etype (Id, Standard_Void_Type);
3555 -- Set SPARK_Mode from context
3557 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3558 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3559 Set_SPARK_Pragma_Inherited (Id);
3560 Set_SPARK_Aux_Pragma_Inherited (Id);
3562 -- Preserve relevant elaboration-related attributes of the context which
3563 -- are no longer available or very expensive to recompute once analysis,
3564 -- resolution, and expansion are over.
3566 Mark_Elaboration_Attributes
3567 (N_Id => Id,
3568 Checks => True,
3569 Warnings => True);
3571 -- Analyze aspects now, so that generated pragmas appear in the
3572 -- declarations before building and analyzing the generic copy.
3574 if Has_Aspects (N) then
3575 Analyze_Aspect_Specifications (N, Id);
3576 end if;
3578 Push_Scope (Id);
3579 Enter_Generic_Scope (Id);
3580 Set_Inner_Instances (Id, New_Elmt_List);
3582 Set_Categorization_From_Pragmas (N);
3583 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3585 -- Link the declaration of the generic homonym in the generic copy to
3586 -- the package it renames, so that it is always resolved properly.
3588 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3589 Set_Entity (Associated_Node (Name (Renaming)), Id);
3591 -- For a library unit, we have reconstructed the entity for the unit,
3592 -- and must reset it in the library tables.
3594 if Nkind (Parent (N)) = N_Compilation_Unit then
3595 Set_Cunit_Entity (Current_Sem_Unit, Id);
3596 end if;
3598 Analyze_Generic_Formal_Part (N);
3600 -- After processing the generic formals, analysis proceeds as for a
3601 -- non-generic package.
3603 Analyze (Specification (N));
3605 Validate_Categorization_Dependency (N, Id);
3607 End_Generic;
3609 End_Package_Scope (Id);
3610 Exit_Generic_Scope (Id);
3612 -- If the generic appears within a package unit, the body of that unit
3613 -- has to be present for instantiation and inlining.
3615 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3616 Set_Body_Needed_For_Inlining
3617 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3618 end if;
3620 if Nkind (Parent (N)) /= N_Compilation_Unit then
3621 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3622 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3623 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3625 else
3626 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3627 Validate_RT_RAT_Component (N);
3629 -- If this is a spec without a body, check that generic parameters
3630 -- are referenced.
3632 if not Body_Required (Parent (N)) then
3633 Check_References (Id);
3634 end if;
3635 end if;
3637 -- If there is a specified storage pool in the context, create an
3638 -- aspect on the package declaration, so that it is used in any
3639 -- instance that does not override it.
3641 if Present (Default_Pool) then
3642 declare
3643 ASN : Node_Id;
3645 begin
3646 ASN :=
3647 Make_Aspect_Specification (Loc,
3648 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3649 Expression => New_Copy (Default_Pool));
3651 if No (Aspect_Specifications (Specification (N))) then
3652 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3653 else
3654 Append (ASN, Aspect_Specifications (Specification (N)));
3655 end if;
3656 end;
3657 end if;
3658 end Analyze_Generic_Package_Declaration;
3660 --------------------------------------------
3661 -- Analyze_Generic_Subprogram_Declaration --
3662 --------------------------------------------
3664 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3665 Formals : List_Id;
3666 Id : Entity_Id;
3667 New_N : Node_Id;
3668 Result_Type : Entity_Id;
3669 Save_Parent : Node_Id;
3670 Spec : Node_Id;
3671 Typ : Entity_Id;
3673 begin
3674 Check_SPARK_05_Restriction ("generic is not allowed", N);
3676 -- Create copy of generic unit, and save for instantiation. If the unit
3677 -- is a child unit, do not copy the specifications for the parent, which
3678 -- are not part of the generic tree.
3680 Save_Parent := Parent_Spec (N);
3681 Set_Parent_Spec (N, Empty);
3683 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3684 Set_Parent_Spec (New_N, Save_Parent);
3685 Rewrite (N, New_N);
3687 -- Once the contents of the generic copy and the template are swapped,
3688 -- do the same for their respective aspect specifications.
3690 Exchange_Aspects (N, New_N);
3692 -- Collect all contract-related source pragmas found within the template
3693 -- and attach them to the contract of the subprogram spec. This contract
3694 -- is used in the capture of global references within annotations.
3696 Create_Generic_Contract (N);
3698 Spec := Specification (N);
3699 Id := Defining_Entity (Spec);
3700 Generate_Definition (Id);
3702 if Nkind (Id) = N_Defining_Operator_Symbol then
3703 Error_Msg_N
3704 ("operator symbol not allowed for generic subprogram", Id);
3705 end if;
3707 Start_Generic;
3709 Enter_Name (Id);
3710 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3712 -- Analyze the aspects of the generic copy to ensure that all generated
3713 -- pragmas (if any) perform their semantic effects.
3715 if Has_Aspects (N) then
3716 Analyze_Aspect_Specifications (N, Id);
3717 end if;
3719 Push_Scope (Id);
3720 Enter_Generic_Scope (Id);
3721 Set_Inner_Instances (Id, New_Elmt_List);
3722 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3724 Analyze_Generic_Formal_Part (N);
3726 if Nkind (Spec) = N_Function_Specification then
3727 Set_Ekind (Id, E_Generic_Function);
3728 else
3729 Set_Ekind (Id, E_Generic_Procedure);
3730 end if;
3732 -- Set SPARK_Mode from context
3734 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3735 Set_SPARK_Pragma_Inherited (Id);
3737 -- Preserve relevant elaboration-related attributes of the context which
3738 -- are no longer available or very expensive to recompute once analysis,
3739 -- resolution, and expansion are over.
3741 Mark_Elaboration_Attributes
3742 (N_Id => Id,
3743 Checks => True,
3744 Warnings => True);
3746 Formals := Parameter_Specifications (Spec);
3748 if Present (Formals) then
3749 Process_Formals (Formals, Spec);
3750 end if;
3752 if Nkind (Spec) = N_Function_Specification then
3753 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3754 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3755 Set_Etype (Id, Result_Type);
3757 -- Check restriction imposed by AI05-073: a generic function
3758 -- cannot return an abstract type or an access to such.
3760 -- This is a binding interpretation should it apply to earlier
3761 -- versions of Ada as well as Ada 2012???
3763 if Is_Abstract_Type (Designated_Type (Result_Type))
3764 and then Ada_Version >= Ada_2012
3765 then
3766 Error_Msg_N
3767 ("generic function cannot have an access result "
3768 & "that designates an abstract type", Spec);
3769 end if;
3771 else
3772 Find_Type (Result_Definition (Spec));
3773 Typ := Entity (Result_Definition (Spec));
3775 if Is_Abstract_Type (Typ)
3776 and then Ada_Version >= Ada_2012
3777 then
3778 Error_Msg_N
3779 ("generic function cannot have abstract result type", Spec);
3780 end if;
3782 -- If a null exclusion is imposed on the result type, then create
3783 -- a null-excluding itype (an access subtype) and use it as the
3784 -- function's Etype.
3786 if Is_Access_Type (Typ)
3787 and then Null_Exclusion_Present (Spec)
3788 then
3789 Set_Etype (Id,
3790 Create_Null_Excluding_Itype
3791 (T => Typ,
3792 Related_Nod => Spec,
3793 Scope_Id => Defining_Unit_Name (Spec)));
3794 else
3795 Set_Etype (Id, Typ);
3796 end if;
3797 end if;
3799 else
3800 Set_Etype (Id, Standard_Void_Type);
3801 end if;
3803 -- For a library unit, we have reconstructed the entity for the unit,
3804 -- and must reset it in the library tables. We also make sure that
3805 -- Body_Required is set properly in the original compilation unit node.
3807 if Nkind (Parent (N)) = N_Compilation_Unit then
3808 Set_Cunit_Entity (Current_Sem_Unit, Id);
3809 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3810 end if;
3812 -- If the generic appears within a package unit, the body of that unit
3813 -- has to be present for instantiation and inlining.
3815 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
3816 and then Unit_Requires_Body (Id)
3817 then
3818 Set_Body_Needed_For_Inlining
3819 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3820 end if;
3822 Set_Categorization_From_Pragmas (N);
3823 Validate_Categorization_Dependency (N, Id);
3825 -- Capture all global references that occur within the profile of the
3826 -- generic subprogram. Aspects are not part of this processing because
3827 -- they must be delayed. If processed now, Save_Global_References will
3828 -- destroy the Associated_Node links and prevent the capture of global
3829 -- references when the contract of the generic subprogram is analyzed.
3831 Save_Global_References (Original_Node (N));
3833 End_Generic;
3834 End_Scope;
3835 Exit_Generic_Scope (Id);
3836 Generate_Reference_To_Formals (Id);
3838 List_Inherited_Pre_Post_Aspects (Id);
3839 end Analyze_Generic_Subprogram_Declaration;
3841 -----------------------------------
3842 -- Analyze_Package_Instantiation --
3843 -----------------------------------
3845 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
3846 -- must be replaced by gotos which jump to the end of the routine in order
3847 -- to restore the Ghost and SPARK modes.
3849 procedure Analyze_Package_Instantiation (N : Node_Id) is
3850 Has_Inline_Always : Boolean := False;
3852 procedure Delay_Descriptors (E : Entity_Id);
3853 -- Delay generation of subprogram descriptors for given entity
3855 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
3856 -- If inlining is active and the generic contains inlined subprograms,
3857 -- we instantiate the body. This may cause superfluous instantiations,
3858 -- but it is simpler than detecting the need for the body at the point
3859 -- of inlining, when the context of the instance is not available.
3861 -----------------------
3862 -- Delay_Descriptors --
3863 -----------------------
3865 procedure Delay_Descriptors (E : Entity_Id) is
3866 begin
3867 if not Delay_Subprogram_Descriptors (E) then
3868 Set_Delay_Subprogram_Descriptors (E);
3869 Pending_Descriptor.Append (E);
3870 end if;
3871 end Delay_Descriptors;
3873 -----------------------
3874 -- Might_Inline_Subp --
3875 -----------------------
3877 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
3878 E : Entity_Id;
3880 begin
3881 if not Inline_Processing_Required then
3882 return False;
3884 else
3885 E := First_Entity (Gen_Unit);
3886 while Present (E) loop
3887 if Is_Subprogram (E) and then Is_Inlined (E) then
3888 -- Remember if there are any subprograms with Inline_Always
3890 if Has_Pragma_Inline_Always (E) then
3891 Has_Inline_Always := True;
3892 end if;
3894 return True;
3895 end if;
3897 Next_Entity (E);
3898 end loop;
3899 end if;
3901 return False;
3902 end Might_Inline_Subp;
3904 -- Local declarations
3906 Gen_Id : constant Node_Id := Name (N);
3907 Is_Actual_Pack : constant Boolean :=
3908 Is_Internal (Defining_Entity (N));
3909 Loc : constant Source_Ptr := Sloc (N);
3911 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3912 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3913 Saved_ISMP : constant Boolean :=
3914 Ignore_SPARK_Mode_Pragmas_In_Instance;
3915 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
3916 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
3917 -- Save the Ghost and SPARK mode-related data to restore on exit
3919 Saved_Style_Check : constant Boolean := Style_Check;
3920 -- Save style check mode for restore on exit
3922 Act_Decl : Node_Id;
3923 Act_Decl_Name : Node_Id;
3924 Act_Decl_Id : Entity_Id;
3925 Act_Spec : Node_Id;
3926 Act_Tree : Node_Id;
3927 Env_Installed : Boolean := False;
3928 Gen_Decl : Node_Id;
3929 Gen_Spec : Node_Id;
3930 Gen_Unit : Entity_Id;
3931 Inline_Now : Boolean := False;
3932 Needs_Body : Boolean;
3933 Parent_Installed : Boolean := False;
3934 Renaming_List : List_Id;
3935 Unit_Renaming : Node_Id;
3937 Vis_Prims_List : Elist_Id := No_Elist;
3938 -- List of primitives made temporarily visible in the instantiation
3939 -- to match the visibility of the formal type
3941 -- Start of processing for Analyze_Package_Instantiation
3943 begin
3944 -- Preserve relevant elaboration-related attributes of the context which
3945 -- are no longer available or very expensive to recompute once analysis,
3946 -- resolution, and expansion are over.
3948 Mark_Elaboration_Attributes
3949 (N_Id => N,
3950 Checks => True,
3951 Level => True,
3952 Modes => True,
3953 Warnings => True);
3955 Check_SPARK_05_Restriction ("generic is not allowed", N);
3957 -- Very first thing: check for Text_IO special unit in case we are
3958 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3960 Check_Text_IO_Special_Unit (Name (N));
3962 -- Make node global for error reporting
3964 Instantiation_Node := N;
3966 -- Case of instantiation of a generic package
3968 if Nkind (N) = N_Package_Instantiation then
3969 Act_Decl_Id := New_Copy (Defining_Entity (N));
3970 Set_Comes_From_Source (Act_Decl_Id, True);
3972 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3973 Act_Decl_Name :=
3974 Make_Defining_Program_Unit_Name (Loc,
3975 Name =>
3976 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3977 Defining_Identifier => Act_Decl_Id);
3978 else
3979 Act_Decl_Name := Act_Decl_Id;
3980 end if;
3982 -- Case of instantiation of a formal package
3984 else
3985 Act_Decl_Id := Defining_Identifier (N);
3986 Act_Decl_Name := Act_Decl_Id;
3987 end if;
3989 Generate_Definition (Act_Decl_Id);
3990 Set_Ekind (Act_Decl_Id, E_Package);
3992 -- Initialize list of incomplete actuals before analysis
3994 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
3996 Preanalyze_Actuals (N, Act_Decl_Id);
3998 -- Turn off style checking in instances. If the check is enabled on the
3999 -- generic unit, a warning in an instance would just be noise. If not
4000 -- enabled on the generic, then a warning in an instance is just wrong.
4001 -- This must be done after analyzing the actuals, which do come from
4002 -- source and are subject to style checking.
4004 Style_Check := False;
4006 Init_Env;
4007 Env_Installed := True;
4009 -- Reset renaming map for formal types. The mapping is established
4010 -- when analyzing the generic associations, but some mappings are
4011 -- inherited from formal packages of parent units, and these are
4012 -- constructed when the parents are installed.
4014 Generic_Renamings.Set_Last (0);
4015 Generic_Renamings_HTable.Reset;
4017 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4018 Gen_Unit := Entity (Gen_Id);
4020 -- A package instantiation is Ghost when it is subject to pragma Ghost
4021 -- or the generic template is Ghost. Set the mode now to ensure that
4022 -- any nodes generated during analysis and expansion are marked as
4023 -- Ghost.
4025 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4027 -- Verify that it is the name of a generic package
4029 -- A visibility glitch: if the instance is a child unit and the generic
4030 -- is the generic unit of a parent instance (i.e. both the parent and
4031 -- the child units are instances of the same package) the name now
4032 -- denotes the renaming within the parent, not the intended generic
4033 -- unit. See if there is a homonym that is the desired generic. The
4034 -- renaming declaration must be visible inside the instance of the
4035 -- child, but not when analyzing the name in the instantiation itself.
4037 if Ekind (Gen_Unit) = E_Package
4038 and then Present (Renamed_Entity (Gen_Unit))
4039 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4040 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4041 and then Present (Homonym (Gen_Unit))
4042 then
4043 Gen_Unit := Homonym (Gen_Unit);
4044 end if;
4046 if Etype (Gen_Unit) = Any_Type then
4047 Restore_Env;
4048 goto Leave;
4050 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4052 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4054 if From_Limited_With (Gen_Unit) then
4055 Error_Msg_N
4056 ("cannot instantiate a limited withed package", Gen_Id);
4057 else
4058 Error_Msg_NE
4059 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4060 end if;
4062 Restore_Env;
4063 goto Leave;
4064 end if;
4066 if In_Extended_Main_Source_Unit (N) then
4067 Set_Is_Instantiated (Gen_Unit);
4068 Generate_Reference (Gen_Unit, N);
4070 if Present (Renamed_Object (Gen_Unit)) then
4071 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
4072 Generate_Reference (Renamed_Object (Gen_Unit), N);
4073 end if;
4074 end if;
4076 if Nkind (Gen_Id) = N_Identifier
4077 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4078 then
4079 Error_Msg_NE
4080 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4082 elsif Nkind (Gen_Id) = N_Expanded_Name
4083 and then Is_Child_Unit (Gen_Unit)
4084 and then Nkind (Prefix (Gen_Id)) = N_Identifier
4085 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
4086 then
4087 Error_Msg_N
4088 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
4089 end if;
4091 Set_Entity (Gen_Id, Gen_Unit);
4093 -- If generic is a renaming, get original generic unit
4095 if Present (Renamed_Object (Gen_Unit))
4096 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
4097 then
4098 Gen_Unit := Renamed_Object (Gen_Unit);
4099 end if;
4101 -- Verify that there are no circular instantiations
4103 if In_Open_Scopes (Gen_Unit) then
4104 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4105 Restore_Env;
4106 goto Leave;
4108 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4109 Error_Msg_Node_2 := Current_Scope;
4110 Error_Msg_NE
4111 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4112 Circularity_Detected := True;
4113 Restore_Env;
4114 goto Leave;
4116 else
4117 -- If the context of the instance is subject to SPARK_Mode "off" or
4118 -- the annotation is altogether missing, set the global flag which
4119 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4120 -- the instance.
4122 if SPARK_Mode /= On then
4123 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4125 -- Mark the instance spec in case the body is instantiated at a
4126 -- later pass. This preserves the original context in effect for
4127 -- the body.
4129 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
4130 end if;
4132 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4133 Gen_Spec := Specification (Gen_Decl);
4135 -- Initialize renamings map, for error checking, and the list that
4136 -- holds private entities whose views have changed between generic
4137 -- definition and instantiation. If this is the instance created to
4138 -- validate an actual package, the instantiation environment is that
4139 -- of the enclosing instance.
4141 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
4143 -- Copy original generic tree, to produce text for instantiation
4145 Act_Tree :=
4146 Copy_Generic_Node
4147 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4149 Act_Spec := Specification (Act_Tree);
4151 -- If this is the instance created to validate an actual package,
4152 -- only the formals matter, do not examine the package spec itself.
4154 if Is_Actual_Pack then
4155 Set_Visible_Declarations (Act_Spec, New_List);
4156 Set_Private_Declarations (Act_Spec, New_List);
4157 end if;
4159 Renaming_List :=
4160 Analyze_Associations
4161 (I_Node => N,
4162 Formals => Generic_Formal_Declarations (Act_Tree),
4163 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4165 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4167 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
4168 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
4169 Set_Is_Generic_Instance (Act_Decl_Id);
4170 Set_Generic_Parent (Act_Spec, Gen_Unit);
4172 -- References to the generic in its own declaration or its body are
4173 -- references to the instance. Add a renaming declaration for the
4174 -- generic unit itself. This declaration, as well as the renaming
4175 -- declarations for the generic formals, must remain private to the
4176 -- unit: the formals, because this is the language semantics, and
4177 -- the unit because its use is an artifact of the implementation.
4179 Unit_Renaming :=
4180 Make_Package_Renaming_Declaration (Loc,
4181 Defining_Unit_Name =>
4182 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
4183 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
4185 Append (Unit_Renaming, Renaming_List);
4187 -- The renaming declarations are the first local declarations of the
4188 -- new unit.
4190 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
4191 Insert_List_Before
4192 (First (Visible_Declarations (Act_Spec)), Renaming_List);
4193 else
4194 Set_Visible_Declarations (Act_Spec, Renaming_List);
4195 end if;
4197 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
4199 -- Propagate the aspect specifications from the package declaration
4200 -- template to the instantiated version of the package declaration.
4202 if Has_Aspects (Act_Tree) then
4203 Set_Aspect_Specifications (Act_Decl,
4204 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4205 end if;
4207 -- The generic may have a generated Default_Storage_Pool aspect,
4208 -- set at the point of generic declaration. If the instance has
4209 -- that aspect, it overrides the one inherited from the generic.
4211 if Has_Aspects (Gen_Spec) then
4212 if No (Aspect_Specifications (N)) then
4213 Set_Aspect_Specifications (N,
4214 (New_Copy_List_Tree
4215 (Aspect_Specifications (Gen_Spec))));
4217 else
4218 declare
4219 ASN1, ASN2 : Node_Id;
4221 begin
4222 ASN1 := First (Aspect_Specifications (N));
4223 while Present (ASN1) loop
4224 if Chars (Identifier (ASN1)) = Name_Default_Storage_Pool
4225 then
4226 -- If generic carries a default storage pool, remove
4227 -- it in favor of the instance one.
4229 ASN2 := First (Aspect_Specifications (Gen_Spec));
4230 while Present (ASN2) loop
4231 if Chars (Identifier (ASN2)) =
4232 Name_Default_Storage_Pool
4233 then
4234 Remove (ASN2);
4235 exit;
4236 end if;
4238 Next (ASN2);
4239 end loop;
4240 end if;
4242 Next (ASN1);
4243 end loop;
4245 Prepend_List_To (Aspect_Specifications (N),
4246 (New_Copy_List_Tree
4247 (Aspect_Specifications (Gen_Spec))));
4248 end;
4249 end if;
4250 end if;
4252 -- Save the instantiation node, for subsequent instantiation of the
4253 -- body, if there is one and we are generating code for the current
4254 -- unit. Mark unit as having a body (avoids premature error message).
4256 -- We instantiate the body if we are generating code, if we are
4257 -- generating cross-reference information, or if we are building
4258 -- trees for ASIS use or GNATprove use.
4260 declare
4261 Enclosing_Body_Present : Boolean := False;
4262 -- If the generic unit is not a compilation unit, then a body may
4263 -- be present in its parent even if none is required. We create a
4264 -- tentative pending instantiation for the body, which will be
4265 -- discarded if none is actually present.
4267 Scop : Entity_Id;
4269 begin
4270 if Scope (Gen_Unit) /= Standard_Standard
4271 and then not Is_Child_Unit (Gen_Unit)
4272 then
4273 Scop := Scope (Gen_Unit);
4274 while Present (Scop) and then Scop /= Standard_Standard loop
4275 if Unit_Requires_Body (Scop) then
4276 Enclosing_Body_Present := True;
4277 exit;
4279 elsif In_Open_Scopes (Scop)
4280 and then In_Package_Body (Scop)
4281 then
4282 Enclosing_Body_Present := True;
4283 exit;
4284 end if;
4286 exit when Is_Compilation_Unit (Scop);
4287 Scop := Scope (Scop);
4288 end loop;
4289 end if;
4291 -- If front-end inlining is enabled or there are any subprograms
4292 -- marked with Inline_Always, and this is a unit for which code
4293 -- will be generated, we instantiate the body at once.
4295 -- This is done if the instance is not the main unit, and if the
4296 -- generic is not a child unit of another generic, to avoid scope
4297 -- problems and the reinstallation of parent instances.
4299 if Expander_Active
4300 and then (not Is_Child_Unit (Gen_Unit)
4301 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4302 and then Might_Inline_Subp (Gen_Unit)
4303 and then not Is_Actual_Pack
4304 then
4305 if not Back_End_Inlining
4306 and then (Front_End_Inlining or else Has_Inline_Always)
4307 and then (Is_In_Main_Unit (N)
4308 or else In_Main_Context (Current_Scope))
4309 and then Nkind (Parent (N)) /= N_Compilation_Unit
4310 then
4311 Inline_Now := True;
4313 -- In configurable_run_time mode we force the inlining of
4314 -- predefined subprograms marked Inline_Always, to minimize
4315 -- the use of the run-time library.
4317 elsif In_Predefined_Unit (Gen_Decl)
4318 and then Configurable_Run_Time_Mode
4319 and then Nkind (Parent (N)) /= N_Compilation_Unit
4320 then
4321 Inline_Now := True;
4322 end if;
4324 -- If the current scope is itself an instance within a child
4325 -- unit, there will be duplications in the scope stack, and the
4326 -- unstacking mechanism in Inline_Instance_Body will fail.
4327 -- This loses some rare cases of optimization, and might be
4328 -- improved some day, if we can find a proper abstraction for
4329 -- "the complete compilation context" that can be saved and
4330 -- restored. ???
4332 if Is_Generic_Instance (Current_Scope) then
4333 declare
4334 Curr_Unit : constant Entity_Id :=
4335 Cunit_Entity (Current_Sem_Unit);
4336 begin
4337 if Curr_Unit /= Current_Scope
4338 and then Is_Child_Unit (Curr_Unit)
4339 then
4340 Inline_Now := False;
4341 end if;
4342 end;
4343 end if;
4344 end if;
4346 Needs_Body :=
4347 (Unit_Requires_Body (Gen_Unit)
4348 or else Enclosing_Body_Present
4349 or else Present (Corresponding_Body (Gen_Decl)))
4350 and then (Is_In_Main_Unit (N)
4351 or else Might_Inline_Subp (Gen_Unit))
4352 and then not Is_Actual_Pack
4353 and then not Inline_Now
4354 and then (Operating_Mode = Generate_Code
4356 -- Need comment for this check ???
4358 or else (Operating_Mode = Check_Semantics
4359 and then (ASIS_Mode or GNATprove_Mode)));
4361 -- If front-end inlining is enabled or there are any subprograms
4362 -- marked with Inline_Always, do not instantiate body when within
4363 -- a generic context.
4365 if ((Front_End_Inlining or else Has_Inline_Always)
4366 and then not Expander_Active)
4367 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
4368 then
4369 Needs_Body := False;
4370 end if;
4372 -- If the current context is generic, and the package being
4373 -- instantiated is declared within a formal package, there is no
4374 -- body to instantiate until the enclosing generic is instantiated
4375 -- and there is an actual for the formal package. If the formal
4376 -- package has parameters, we build a regular package instance for
4377 -- it, that precedes the original formal package declaration.
4379 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4380 declare
4381 Decl : constant Node_Id :=
4382 Original_Node
4383 (Unit_Declaration_Node (Scope (Gen_Unit)));
4384 begin
4385 if Nkind (Decl) = N_Formal_Package_Declaration
4386 or else (Nkind (Decl) = N_Package_Declaration
4387 and then Is_List_Member (Decl)
4388 and then Present (Next (Decl))
4389 and then
4390 Nkind (Next (Decl)) =
4391 N_Formal_Package_Declaration)
4392 then
4393 Needs_Body := False;
4394 end if;
4395 end;
4396 end if;
4397 end;
4399 -- For RCI unit calling stubs, we omit the instance body if the
4400 -- instance is the RCI library unit itself.
4402 -- However there is a special case for nested instances: in this case
4403 -- we do generate the instance body, as it might be required, e.g.
4404 -- because it provides stream attributes for some type used in the
4405 -- profile of a remote subprogram. This is consistent with 12.3(12),
4406 -- which indicates that the instance body occurs at the place of the
4407 -- instantiation, and thus is part of the RCI declaration, which is
4408 -- present on all client partitions (this is E.2.3(18)).
4410 -- Note that AI12-0002 may make it illegal at some point to have
4411 -- stream attributes defined in an RCI unit, in which case this
4412 -- special case will become unnecessary. In the meantime, there
4413 -- is known application code in production that depends on this
4414 -- being possible, so we definitely cannot eliminate the body in
4415 -- the case of nested instances for the time being.
4417 -- When we generate a nested instance body, calling stubs for any
4418 -- relevant subprogram will be be inserted immediately after the
4419 -- subprogram declarations, and will take precedence over the
4420 -- subsequent (original) body. (The stub and original body will be
4421 -- complete homographs, but this is permitted in an instance).
4422 -- (Could we do better and remove the original body???)
4424 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4425 and then Comes_From_Source (N)
4426 and then Nkind (Parent (N)) = N_Compilation_Unit
4427 then
4428 Needs_Body := False;
4429 end if;
4431 if Needs_Body then
4433 -- Here is a defence against a ludicrous number of instantiations
4434 -- caused by a circular set of instantiation attempts.
4436 if Pending_Instantiations.Last > Maximum_Instantiations then
4437 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4438 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4439 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4440 raise Unrecoverable_Error;
4441 end if;
4443 -- Indicate that the enclosing scopes contain an instantiation,
4444 -- and that cleanup actions should be delayed until after the
4445 -- instance body is expanded.
4447 Check_Forward_Instantiation (Gen_Decl);
4448 if Nkind (N) = N_Package_Instantiation then
4449 declare
4450 Enclosing_Master : Entity_Id;
4452 begin
4453 -- Loop to search enclosing masters
4455 Enclosing_Master := Current_Scope;
4456 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4457 if Ekind (Enclosing_Master) = E_Package then
4458 if Is_Compilation_Unit (Enclosing_Master) then
4459 if In_Package_Body (Enclosing_Master) then
4460 Delay_Descriptors
4461 (Body_Entity (Enclosing_Master));
4462 else
4463 Delay_Descriptors
4464 (Enclosing_Master);
4465 end if;
4467 exit Scope_Loop;
4469 else
4470 Enclosing_Master := Scope (Enclosing_Master);
4471 end if;
4473 elsif Is_Generic_Unit (Enclosing_Master)
4474 or else Ekind (Enclosing_Master) = E_Void
4475 then
4476 -- Cleanup actions will eventually be performed on the
4477 -- enclosing subprogram or package instance, if any.
4478 -- Enclosing scope is void in the formal part of a
4479 -- generic subprogram.
4481 exit Scope_Loop;
4483 else
4484 if Ekind (Enclosing_Master) = E_Entry
4485 and then
4486 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4487 then
4488 if not Expander_Active then
4489 exit Scope_Loop;
4490 else
4491 Enclosing_Master :=
4492 Protected_Body_Subprogram (Enclosing_Master);
4493 end if;
4494 end if;
4496 Set_Delay_Cleanups (Enclosing_Master);
4498 while Ekind (Enclosing_Master) = E_Block loop
4499 Enclosing_Master := Scope (Enclosing_Master);
4500 end loop;
4502 if Is_Subprogram (Enclosing_Master) then
4503 Delay_Descriptors (Enclosing_Master);
4505 elsif Is_Task_Type (Enclosing_Master) then
4506 declare
4507 TBP : constant Node_Id :=
4508 Get_Task_Body_Procedure
4509 (Enclosing_Master);
4510 begin
4511 if Present (TBP) then
4512 Delay_Descriptors (TBP);
4513 Set_Delay_Cleanups (TBP);
4514 end if;
4515 end;
4516 end if;
4518 exit Scope_Loop;
4519 end if;
4520 end loop Scope_Loop;
4521 end;
4523 -- Make entry in table
4525 Add_Pending_Instantiation (N, Act_Decl);
4526 end if;
4527 end if;
4529 Set_Categorization_From_Pragmas (Act_Decl);
4531 if Parent_Installed then
4532 Hide_Current_Scope;
4533 end if;
4535 Set_Instance_Spec (N, Act_Decl);
4537 -- If not a compilation unit, insert the package declaration before
4538 -- the original instantiation node.
4540 if Nkind (Parent (N)) /= N_Compilation_Unit then
4541 Mark_Rewrite_Insertion (Act_Decl);
4542 Insert_Before (N, Act_Decl);
4544 if Has_Aspects (N) then
4545 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4547 -- The pragma created for a Default_Storage_Pool aspect must
4548 -- appear ahead of the declarations in the instance spec.
4549 -- Analysis has placed it after the instance node, so remove
4550 -- it and reinsert it properly now.
4552 declare
4553 ASN : constant Node_Id := First (Aspect_Specifications (N));
4554 A_Name : constant Name_Id := Chars (Identifier (ASN));
4555 Decl : Node_Id;
4557 begin
4558 if A_Name = Name_Default_Storage_Pool then
4559 if No (Visible_Declarations (Act_Spec)) then
4560 Set_Visible_Declarations (Act_Spec, New_List);
4561 end if;
4563 Decl := Next (N);
4564 while Present (Decl) loop
4565 if Nkind (Decl) = N_Pragma then
4566 Remove (Decl);
4567 Prepend (Decl, Visible_Declarations (Act_Spec));
4568 exit;
4569 end if;
4571 Next (Decl);
4572 end loop;
4573 end if;
4574 end;
4575 end if;
4577 Analyze (Act_Decl);
4579 -- For an instantiation that is a compilation unit, place
4580 -- declaration on current node so context is complete for analysis
4581 -- (including nested instantiations). If this is the main unit,
4582 -- the declaration eventually replaces the instantiation node.
4583 -- If the instance body is created later, it replaces the
4584 -- instance node, and the declaration is attached to it
4585 -- (see Build_Instance_Compilation_Unit_Nodes).
4587 else
4588 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4590 -- The entity for the current unit is the newly created one,
4591 -- and all semantic information is attached to it.
4593 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4595 -- If this is the main unit, replace the main entity as well
4597 if Current_Sem_Unit = Main_Unit then
4598 Main_Unit_Entity := Act_Decl_Id;
4599 end if;
4600 end if;
4602 Set_Unit (Parent (N), Act_Decl);
4603 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4604 Set_Package_Instantiation (Act_Decl_Id, N);
4606 -- Process aspect specifications of the instance node, if any, to
4607 -- take into account categorization pragmas before analyzing the
4608 -- instance.
4610 if Has_Aspects (N) then
4611 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4612 end if;
4614 Analyze (Act_Decl);
4615 Set_Unit (Parent (N), N);
4616 Set_Body_Required (Parent (N), False);
4618 -- We never need elaboration checks on instantiations, since by
4619 -- definition, the body instantiation is elaborated at the same
4620 -- time as the spec instantiation.
4622 if Legacy_Elaboration_Checks then
4623 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4624 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4625 end if;
4626 end if;
4628 if Legacy_Elaboration_Checks then
4629 Check_Elab_Instantiation (N);
4630 end if;
4632 -- Save the scenario for later examination by the ABE Processing
4633 -- phase.
4635 Record_Elaboration_Scenario (N);
4637 -- The instantiation results in a guaranteed ABE
4639 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
4641 -- Do not instantiate the corresponding body because gigi cannot
4642 -- handle certain types of premature instantiations.
4644 Pending_Instantiations.Decrement_Last;
4646 -- Create completing bodies for all subprogram declarations since
4647 -- their real bodies will not be instantiated.
4649 Provide_Completing_Bodies (Instance_Spec (N));
4650 end if;
4652 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4654 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4655 First_Private_Entity (Act_Decl_Id));
4657 -- If the instantiation will receive a body, the unit will be
4658 -- transformed into a package body, and receive its own elaboration
4659 -- entity. Otherwise, the nature of the unit is now a package
4660 -- declaration.
4662 if Nkind (Parent (N)) = N_Compilation_Unit
4663 and then not Needs_Body
4664 then
4665 Rewrite (N, Act_Decl);
4666 end if;
4668 if Present (Corresponding_Body (Gen_Decl))
4669 or else Unit_Requires_Body (Gen_Unit)
4670 then
4671 Set_Has_Completion (Act_Decl_Id);
4672 end if;
4674 Check_Formal_Packages (Act_Decl_Id);
4676 Restore_Hidden_Primitives (Vis_Prims_List);
4677 Restore_Private_Views (Act_Decl_Id);
4679 Inherit_Context (Gen_Decl, N);
4681 if Parent_Installed then
4682 Remove_Parent;
4683 end if;
4685 Restore_Env;
4686 Env_Installed := False;
4687 end if;
4689 Validate_Categorization_Dependency (N, Act_Decl_Id);
4691 -- There used to be a check here to prevent instantiations in local
4692 -- contexts if the No_Local_Allocators restriction was active. This
4693 -- check was removed by a binding interpretation in AI-95-00130/07,
4694 -- but we retain the code for documentation purposes.
4696 -- if Ekind (Act_Decl_Id) /= E_Void
4697 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4698 -- then
4699 -- Check_Restriction (No_Local_Allocators, N);
4700 -- end if;
4702 if Inline_Now then
4703 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4704 end if;
4706 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4707 -- be used as defining identifiers for a formal package and for the
4708 -- corresponding expanded package.
4710 if Nkind (N) = N_Formal_Package_Declaration then
4711 Act_Decl_Id := New_Copy (Defining_Entity (N));
4712 Set_Comes_From_Source (Act_Decl_Id, True);
4713 Set_Is_Generic_Instance (Act_Decl_Id, False);
4714 Set_Defining_Identifier (N, Act_Decl_Id);
4715 end if;
4717 -- Check that if N is an instantiation of System.Dim_Float_IO or
4718 -- System.Dim_Integer_IO, the formal type has a dimension system.
4720 if Nkind (N) = N_Package_Instantiation
4721 and then Is_Dim_IO_Package_Instantiation (N)
4722 then
4723 declare
4724 Assoc : constant Node_Id := First (Generic_Associations (N));
4725 begin
4726 if not Has_Dimension_System
4727 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4728 then
4729 Error_Msg_N ("type with a dimension system expected", Assoc);
4730 end if;
4731 end;
4732 end if;
4734 <<Leave>>
4735 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4736 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4737 end if;
4739 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4740 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4741 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4742 Style_Check := Saved_Style_Check;
4744 exception
4745 when Instantiation_Error =>
4746 if Parent_Installed then
4747 Remove_Parent;
4748 end if;
4750 if Env_Installed then
4751 Restore_Env;
4752 end if;
4754 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4755 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4756 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4757 Style_Check := Saved_Style_Check;
4758 end Analyze_Package_Instantiation;
4760 --------------------------
4761 -- Inline_Instance_Body --
4762 --------------------------
4764 -- WARNING: This routine manages SPARK regions. Return statements must be
4765 -- replaced by gotos which jump to the end of the routine and restore the
4766 -- SPARK mode.
4768 procedure Inline_Instance_Body
4769 (N : Node_Id;
4770 Gen_Unit : Entity_Id;
4771 Act_Decl : Node_Id)
4773 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4774 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4775 Gen_Comp : constant Entity_Id :=
4776 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4778 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
4779 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
4780 -- Save the SPARK mode-related data to restore on exit. Removing
4781 -- enclosing scopes to provide a clean environment for analysis of
4782 -- the inlined body will eliminate any previously set SPARK_Mode.
4784 Scope_Stack_Depth : constant Pos :=
4785 Scope_Stack.Last - Scope_Stack.First + 1;
4787 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4788 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4789 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4791 Curr_Scope : Entity_Id := Empty;
4792 List : Elist_Id := No_Elist; -- init to avoid warning
4793 N_Instances : Nat := 0;
4794 Num_Inner : Nat := 0;
4795 Num_Scopes : Nat := 0;
4796 Removed : Boolean := False;
4797 S : Entity_Id;
4798 Vis : Boolean;
4800 begin
4801 -- Case of generic unit defined in another unit. We must remove the
4802 -- complete context of the current unit to install that of the generic.
4804 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4806 -- Add some comments for the following two loops ???
4808 S := Current_Scope;
4809 while Present (S) and then S /= Standard_Standard loop
4810 loop
4811 Num_Scopes := Num_Scopes + 1;
4813 Use_Clauses (Num_Scopes) :=
4814 (Scope_Stack.Table
4815 (Scope_Stack.Last - Num_Scopes + 1).
4816 First_Use_Clause);
4817 End_Use_Clauses (Use_Clauses (Num_Scopes));
4819 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4820 or else Scope_Stack.Table
4821 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4822 end loop;
4824 exit when Is_Generic_Instance (S)
4825 and then (In_Package_Body (S)
4826 or else Ekind (S) = E_Procedure
4827 or else Ekind (S) = E_Function);
4828 S := Scope (S);
4829 end loop;
4831 Vis := Is_Immediately_Visible (Gen_Comp);
4833 -- Find and save all enclosing instances
4835 S := Current_Scope;
4837 while Present (S)
4838 and then S /= Standard_Standard
4839 loop
4840 if Is_Generic_Instance (S) then
4841 N_Instances := N_Instances + 1;
4842 Instances (N_Instances) := S;
4844 exit when In_Package_Body (S);
4845 end if;
4847 S := Scope (S);
4848 end loop;
4850 -- Remove context of current compilation unit, unless we are within a
4851 -- nested package instantiation, in which case the context has been
4852 -- removed previously.
4854 -- If current scope is the body of a child unit, remove context of
4855 -- spec as well. If an enclosing scope is an instance body, the
4856 -- context has already been removed, but the entities in the body
4857 -- must be made invisible as well.
4859 S := Current_Scope;
4860 while Present (S) and then S /= Standard_Standard loop
4861 if Is_Generic_Instance (S)
4862 and then (In_Package_Body (S)
4863 or else Ekind_In (S, E_Procedure, E_Function))
4864 then
4865 -- We still have to remove the entities of the enclosing
4866 -- instance from direct visibility.
4868 declare
4869 E : Entity_Id;
4870 begin
4871 E := First_Entity (S);
4872 while Present (E) loop
4873 Set_Is_Immediately_Visible (E, False);
4874 Next_Entity (E);
4875 end loop;
4876 end;
4878 exit;
4879 end if;
4881 if S = Curr_Unit
4882 or else (Ekind (Curr_Unit) = E_Package_Body
4883 and then S = Spec_Entity (Curr_Unit))
4884 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4885 and then S = Corresponding_Spec
4886 (Unit_Declaration_Node (Curr_Unit)))
4887 then
4888 Removed := True;
4890 -- Remove entities in current scopes from visibility, so that
4891 -- instance body is compiled in a clean environment.
4893 List := Save_Scope_Stack (Handle_Use => False);
4895 if Is_Child_Unit (S) then
4897 -- Remove child unit from stack, as well as inner scopes.
4898 -- Removing the context of a child unit removes parent units
4899 -- as well.
4901 while Current_Scope /= S loop
4902 Num_Inner := Num_Inner + 1;
4903 Inner_Scopes (Num_Inner) := Current_Scope;
4904 Pop_Scope;
4905 end loop;
4907 Pop_Scope;
4908 Remove_Context (Curr_Comp);
4909 Curr_Scope := S;
4911 else
4912 Remove_Context (Curr_Comp);
4913 end if;
4915 if Ekind (Curr_Unit) = E_Package_Body then
4916 Remove_Context (Library_Unit (Curr_Comp));
4917 end if;
4918 end if;
4920 S := Scope (S);
4921 end loop;
4923 pragma Assert (Num_Inner < Num_Scopes);
4925 -- The inlined package body must be analyzed with the SPARK_Mode of
4926 -- the enclosing context, otherwise the body may cause bogus errors
4927 -- if a configuration SPARK_Mode pragma in in effect.
4929 Push_Scope (Standard_Standard);
4930 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4931 Instantiate_Package_Body
4932 (Body_Info =>
4933 ((Inst_Node => N,
4934 Act_Decl => Act_Decl,
4935 Expander_Status => Expander_Active,
4936 Current_Sem_Unit => Current_Sem_Unit,
4937 Scope_Suppress => Scope_Suppress,
4938 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4939 Version => Ada_Version,
4940 Version_Pragma => Ada_Version_Pragma,
4941 Warnings => Save_Warnings,
4942 SPARK_Mode => Saved_SM,
4943 SPARK_Mode_Pragma => Saved_SMP)),
4944 Inlined_Body => True);
4946 Pop_Scope;
4948 -- Restore context
4950 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4952 -- Reset Generic_Instance flag so that use clauses can be installed
4953 -- in the proper order. (See Use_One_Package for effect of enclosing
4954 -- instances on processing of use clauses).
4956 for J in 1 .. N_Instances loop
4957 Set_Is_Generic_Instance (Instances (J), False);
4958 end loop;
4960 if Removed then
4961 Install_Context (Curr_Comp, Chain => False);
4963 if Present (Curr_Scope)
4964 and then Is_Child_Unit (Curr_Scope)
4965 then
4966 Push_Scope (Curr_Scope);
4967 Set_Is_Immediately_Visible (Curr_Scope);
4969 -- Finally, restore inner scopes as well
4971 for J in reverse 1 .. Num_Inner loop
4972 Push_Scope (Inner_Scopes (J));
4973 end loop;
4974 end if;
4976 Restore_Scope_Stack (List, Handle_Use => False);
4978 if Present (Curr_Scope)
4979 and then
4980 (In_Private_Part (Curr_Scope)
4981 or else In_Package_Body (Curr_Scope))
4982 then
4983 -- Install private declaration of ancestor units, which are
4984 -- currently available. Restore_Scope_Stack and Install_Context
4985 -- only install the visible part of parents.
4987 declare
4988 Par : Entity_Id;
4989 begin
4990 Par := Scope (Curr_Scope);
4991 while (Present (Par)) and then Par /= Standard_Standard loop
4992 Install_Private_Declarations (Par);
4993 Par := Scope (Par);
4994 end loop;
4995 end;
4996 end if;
4997 end if;
4999 -- Restore use clauses. For a child unit, use clauses in the parents
5000 -- are restored when installing the context, so only those in inner
5001 -- scopes (and those local to the child unit itself) need to be
5002 -- installed explicitly.
5004 if Is_Child_Unit (Curr_Unit) and then Removed then
5005 for J in reverse 1 .. Num_Inner + 1 loop
5006 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5007 Use_Clauses (J);
5008 Install_Use_Clauses (Use_Clauses (J));
5009 end loop;
5011 else
5012 for J in reverse 1 .. Num_Scopes loop
5013 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5014 Use_Clauses (J);
5015 Install_Use_Clauses (Use_Clauses (J));
5016 end loop;
5017 end if;
5019 -- Restore status of instances. If one of them is a body, make its
5020 -- local entities visible again.
5022 declare
5023 E : Entity_Id;
5024 Inst : Entity_Id;
5026 begin
5027 for J in 1 .. N_Instances loop
5028 Inst := Instances (J);
5029 Set_Is_Generic_Instance (Inst, True);
5031 if In_Package_Body (Inst)
5032 or else Ekind_In (S, E_Procedure, E_Function)
5033 then
5034 E := First_Entity (Instances (J));
5035 while Present (E) loop
5036 Set_Is_Immediately_Visible (E);
5037 Next_Entity (E);
5038 end loop;
5039 end if;
5040 end loop;
5041 end;
5043 -- If generic unit is in current unit, current context is correct. Note
5044 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5045 -- enclosing scopes were removed.
5047 else
5048 Instantiate_Package_Body
5049 (Body_Info =>
5050 ((Inst_Node => N,
5051 Act_Decl => Act_Decl,
5052 Expander_Status => Expander_Active,
5053 Current_Sem_Unit => Current_Sem_Unit,
5054 Scope_Suppress => Scope_Suppress,
5055 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5056 Version => Ada_Version,
5057 Version_Pragma => Ada_Version_Pragma,
5058 Warnings => Save_Warnings,
5059 SPARK_Mode => SPARK_Mode,
5060 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
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 Save_Opt_Config_Switches (Saved.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;
11325 Opt.Ada_Version := Body_Info.Version;
11326 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11327 Restore_Warnings (Body_Info.Warnings);
11329 -- Install the SPARK mode which applies to the package body
11331 Install_SPARK_Mode (Body_Info.SPARK_Mode, Body_Info.SPARK_Mode_Pragma);
11333 if No (Gen_Body_Id) then
11335 -- Do not look for parent of generic body if none is required.
11336 -- This may happen when the routine is called as part of the
11337 -- Pending_Instantiations processing, when nested instances
11338 -- may precede the one generated from the main unit.
11340 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
11341 and then Body_Optional
11342 then
11343 goto Leave;
11344 else
11345 Load_Parent_Of_Generic
11346 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11347 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11348 end if;
11349 end if;
11351 -- Establish global variable for sloc adjustment and for error recovery
11352 -- In the case of an instance body for an instantiation with actuals
11353 -- from a limited view, the instance body is placed at the beginning
11354 -- of the enclosing package body: use the body entity as the source
11355 -- location for nodes of the instance body.
11357 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
11358 declare
11359 Scop : constant Entity_Id := Scope (Act_Decl_Id);
11360 Body_Id : constant Node_Id :=
11361 Corresponding_Body (Unit_Declaration_Node (Scop));
11363 begin
11364 Instantiation_Node := Body_Id;
11365 end;
11366 else
11367 Instantiation_Node := Inst_Node;
11368 end if;
11370 if Present (Gen_Body_Id) then
11371 Save_Env (Gen_Unit, Act_Decl_Id);
11372 Style_Check := False;
11374 -- If the context of the instance is subject to SPARK_Mode "off", the
11375 -- annotation is missing, or the body is instantiated at a later pass
11376 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11377 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11378 -- instance.
11380 if SPARK_Mode /= On
11381 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11382 then
11383 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11384 end if;
11386 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11387 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11389 Create_Instantiation_Source
11390 (Inst_Node, Gen_Body_Id, S_Adjustment);
11392 Act_Body :=
11393 Copy_Generic_Node
11394 (Original_Node (Gen_Body), Empty, Instantiating => True);
11396 -- Create proper (possibly qualified) defining name for the body, to
11397 -- correspond to the one in the spec.
11399 Act_Body_Id :=
11400 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11401 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11403 -- Some attributes of spec entity are not inherited by body entity
11405 Set_Handler_Records (Act_Body_Id, No_List);
11407 if Nkind (Defining_Unit_Name (Act_Spec)) =
11408 N_Defining_Program_Unit_Name
11409 then
11410 Act_Body_Name :=
11411 Make_Defining_Program_Unit_Name (Loc,
11412 Name =>
11413 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
11414 Defining_Identifier => Act_Body_Id);
11415 else
11416 Act_Body_Name := Act_Body_Id;
11417 end if;
11419 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
11421 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11422 Check_Generic_Actuals (Act_Decl_Id, False);
11423 Check_Initialized_Types;
11425 -- Install primitives hidden at the point of the instantiation but
11426 -- visible when processing the generic formals
11428 declare
11429 E : Entity_Id;
11431 begin
11432 E := First_Entity (Act_Decl_Id);
11433 while Present (E) loop
11434 if Is_Type (E)
11435 and then not Is_Itype (E)
11436 and then Is_Generic_Actual_Type (E)
11437 and then Is_Tagged_Type (E)
11438 then
11439 Install_Hidden_Primitives
11440 (Prims_List => Vis_Prims_List,
11441 Gen_T => Generic_Parent_Type (Parent (E)),
11442 Act_T => E);
11443 end if;
11445 Next_Entity (E);
11446 end loop;
11447 end;
11449 -- If it is a child unit, make the parent instance (which is an
11450 -- instance of the parent of the generic) visible. The parent
11451 -- instance is the prefix of the name of the generic unit.
11453 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11454 and then Nkind (Gen_Id) = N_Expanded_Name
11455 then
11456 Par_Ent := Entity (Prefix (Gen_Id));
11457 Par_Vis := Is_Immediately_Visible (Par_Ent);
11458 Install_Parent (Par_Ent, In_Body => True);
11459 Parent_Installed := True;
11461 elsif Is_Child_Unit (Gen_Unit) then
11462 Par_Ent := Scope (Gen_Unit);
11463 Par_Vis := Is_Immediately_Visible (Par_Ent);
11464 Install_Parent (Par_Ent, In_Body => True);
11465 Parent_Installed := True;
11466 end if;
11468 -- If the instantiation is a library unit, and this is the main unit,
11469 -- then build the resulting compilation unit nodes for the instance.
11470 -- If this is a compilation unit but it is not the main unit, then it
11471 -- is the body of a unit in the context, that is being compiled
11472 -- because it is encloses some inlined unit or another generic unit
11473 -- being instantiated. In that case, this body is not part of the
11474 -- current compilation, and is not attached to the tree, but its
11475 -- parent must be set for analysis.
11477 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11479 -- Replace instance node with body of instance, and create new
11480 -- node for corresponding instance declaration.
11482 Build_Instance_Compilation_Unit_Nodes
11483 (Inst_Node, Act_Body, Act_Decl);
11484 Analyze (Inst_Node);
11486 if Parent (Inst_Node) = Cunit (Main_Unit) then
11488 -- If the instance is a child unit itself, then set the scope
11489 -- of the expanded body to be the parent of the instantiation
11490 -- (ensuring that the fully qualified name will be generated
11491 -- for the elaboration subprogram).
11493 if Nkind (Defining_Unit_Name (Act_Spec)) =
11494 N_Defining_Program_Unit_Name
11495 then
11496 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
11497 end if;
11498 end if;
11500 -- Case where instantiation is not a library unit
11502 else
11503 -- If this is an early instantiation, i.e. appears textually
11504 -- before the corresponding body and must be elaborated first,
11505 -- indicate that the body instance is to be delayed.
11507 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
11509 -- Now analyze the body. We turn off all checks if this is an
11510 -- internal unit, since there is no reason to have checks on for
11511 -- any predefined run-time library code. All such code is designed
11512 -- to be compiled with checks off.
11514 -- Note that we do NOT apply this criterion to children of GNAT
11515 -- The latter units must suppress checks explicitly if needed.
11517 -- We also do not suppress checks in CodePeer mode where we are
11518 -- interested in finding possible runtime errors.
11520 if not CodePeer_Mode
11521 and then In_Predefined_Unit (Gen_Decl)
11522 then
11523 Analyze (Act_Body, Suppress => All_Checks);
11524 else
11525 Analyze (Act_Body);
11526 end if;
11527 end if;
11529 Inherit_Context (Gen_Body, Inst_Node);
11531 -- Remove the parent instances if they have been placed on the scope
11532 -- stack to compile the body.
11534 if Parent_Installed then
11535 Remove_Parent (In_Body => True);
11537 -- Restore the previous visibility of the parent
11539 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11540 end if;
11542 Restore_Hidden_Primitives (Vis_Prims_List);
11543 Restore_Private_Views (Act_Decl_Id);
11545 -- Remove the current unit from visibility if this is an instance
11546 -- that is not elaborated on the fly for inlining purposes.
11548 if not Inlined_Body then
11549 Set_Is_Immediately_Visible (Act_Decl_Id, False);
11550 end if;
11552 Restore_Env;
11554 -- If we have no body, and the unit requires a body, then complain. This
11555 -- complaint is suppressed if we have detected other errors (since a
11556 -- common reason for missing the body is that it had errors).
11557 -- In CodePeer mode, a warning has been emitted already, no need for
11558 -- further messages.
11560 elsif Unit_Requires_Body (Gen_Unit)
11561 and then not Body_Optional
11562 then
11563 if CodePeer_Mode then
11564 null;
11566 elsif Serious_Errors_Detected = 0 then
11567 Error_Msg_NE
11568 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
11570 -- Don't attempt to perform any cleanup actions if some other error
11571 -- was already detected, since this can cause blowups.
11573 else
11574 goto Leave;
11575 end if;
11577 -- Case of package that does not need a body
11579 else
11580 -- If the instantiation of the declaration is a library unit, rewrite
11581 -- the original package instantiation as a package declaration in the
11582 -- compilation unit node.
11584 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11585 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
11586 Rewrite (Inst_Node, Act_Decl);
11588 -- Generate elaboration entity, in case spec has elaboration code.
11589 -- This cannot be done when the instance is analyzed, because it
11590 -- is not known yet whether the body exists.
11592 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
11593 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
11595 -- If the instantiation is not a library unit, then append the
11596 -- declaration to the list of implicitly generated entities, unless
11597 -- it is already a list member which means that it was already
11598 -- processed
11600 elsif not Is_List_Member (Act_Decl) then
11601 Mark_Rewrite_Insertion (Act_Decl);
11602 Insert_Before (Inst_Node, Act_Decl);
11603 end if;
11604 end if;
11606 Expander_Mode_Restore;
11608 <<Leave>>
11609 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
11610 Restore_Ghost_Region (Saved_GM, Saved_IGR);
11611 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
11612 Style_Check := Saved_Style_Check;
11613 end Instantiate_Package_Body;
11615 ---------------------------------
11616 -- Instantiate_Subprogram_Body --
11617 ---------------------------------
11619 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11620 -- must be replaced by gotos which jump to the end of the routine in order
11621 -- to restore the Ghost and SPARK modes.
11623 procedure Instantiate_Subprogram_Body
11624 (Body_Info : Pending_Body_Info;
11625 Body_Optional : Boolean := False)
11627 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11628 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11629 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11630 Gen_Id : constant Node_Id := Name (Inst_Node);
11631 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11632 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11633 Loc : constant Source_Ptr := Sloc (Inst_Node);
11634 Pack_Id : constant Entity_Id :=
11635 Defining_Unit_Name (Parent (Act_Decl));
11637 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11638 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11639 Saved_ISMP : constant Boolean :=
11640 Ignore_SPARK_Mode_Pragmas_In_Instance;
11641 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11642 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11643 -- Save the Ghost and SPARK mode-related data to restore on exit
11645 Saved_Style_Check : constant Boolean := Style_Check;
11646 Saved_Warnings : constant Warning_Record := Save_Warnings;
11648 Act_Body : Node_Id;
11649 Act_Body_Id : Entity_Id;
11650 Gen_Body : Node_Id;
11651 Gen_Body_Id : Node_Id;
11652 Pack_Body : Node_Id;
11653 Par_Ent : Entity_Id := Empty;
11654 Par_Vis : Boolean := False;
11655 Ret_Expr : Node_Id;
11657 Parent_Installed : Boolean := False;
11659 begin
11660 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11662 -- Subprogram body may have been created already because of an inline
11663 -- pragma, or because of multiple elaborations of the enclosing package
11664 -- when several instances of the subprogram appear in the main unit.
11666 if Present (Corresponding_Body (Act_Decl)) then
11667 return;
11668 end if;
11670 -- The subprogram being instantiated may be subject to pragma Ghost. Set
11671 -- the mode now to ensure that any nodes generated during instantiation
11672 -- are properly marked as Ghost.
11674 Set_Ghost_Mode (Act_Decl_Id);
11676 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11678 -- Re-establish the state of information on which checks are suppressed.
11679 -- This information was set in Body_Info at the point of instantiation,
11680 -- and now we restore it so that the instance is compiled using the
11681 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11683 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11684 Scope_Suppress := Body_Info.Scope_Suppress;
11685 Opt.Ada_Version := Body_Info.Version;
11686 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11687 Restore_Warnings (Body_Info.Warnings);
11689 -- Install the SPARK mode which applies to the subprogram body from the
11690 -- instantiation context. This may be refined further if an explicit
11691 -- SPARK_Mode pragma applies to the generic body.
11693 Install_SPARK_Mode (Body_Info.SPARK_Mode, Body_Info.SPARK_Mode_Pragma);
11695 if No (Gen_Body_Id) then
11697 -- For imported generic subprogram, no body to compile, complete
11698 -- the spec entity appropriately.
11700 if Is_Imported (Gen_Unit) then
11701 Set_Is_Imported (Act_Decl_Id);
11702 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
11703 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
11704 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
11705 Set_Has_Completion (Act_Decl_Id);
11706 goto Leave;
11708 -- For other cases, compile the body
11710 else
11711 Load_Parent_Of_Generic
11712 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11713 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11714 end if;
11715 end if;
11717 Instantiation_Node := Inst_Node;
11719 if Present (Gen_Body_Id) then
11720 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11722 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11724 -- Either body is not present, or context is non-expanding, as
11725 -- when compiling a subunit. Mark the instance as completed, and
11726 -- diagnose a missing body when needed.
11728 if Expander_Active
11729 and then Operating_Mode = Generate_Code
11730 then
11731 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
11732 end if;
11734 Set_Has_Completion (Act_Decl_Id);
11735 goto Leave;
11736 end if;
11738 Save_Env (Gen_Unit, Act_Decl_Id);
11739 Style_Check := False;
11741 -- If the context of the instance is subject to SPARK_Mode "off", the
11742 -- annotation is missing, or the body is instantiated at a later pass
11743 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11744 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11745 -- instance.
11747 if SPARK_Mode /= On
11748 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11749 then
11750 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11751 end if;
11753 -- If the context of an instance is not subject to SPARK_Mode "off",
11754 -- and the generic body is subject to an explicit SPARK_Mode pragma,
11755 -- the latter should be the one applicable to the instance.
11757 if not Ignore_SPARK_Mode_Pragmas_In_Instance
11758 and then SPARK_Mode /= Off
11759 and then Present (SPARK_Pragma (Gen_Body_Id))
11760 then
11761 Set_SPARK_Mode (Gen_Body_Id);
11762 end if;
11764 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11765 Create_Instantiation_Source
11766 (Inst_Node,
11767 Gen_Body_Id,
11768 S_Adjustment);
11770 Act_Body :=
11771 Copy_Generic_Node
11772 (Original_Node (Gen_Body), Empty, Instantiating => True);
11774 -- Create proper defining name for the body, to correspond to the one
11775 -- in the spec.
11777 Act_Body_Id :=
11778 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11780 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11781 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
11783 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11784 Set_Has_Completion (Act_Decl_Id);
11785 Check_Generic_Actuals (Pack_Id, False);
11787 -- Generate a reference to link the visible subprogram instance to
11788 -- the generic body, which for navigation purposes is the only
11789 -- available source for the instance.
11791 Generate_Reference
11792 (Related_Instance (Pack_Id),
11793 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11795 -- If it is a child unit, make the parent instance (which is an
11796 -- instance of the parent of the generic) visible. The parent
11797 -- instance is the prefix of the name of the generic unit.
11799 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11800 and then Nkind (Gen_Id) = N_Expanded_Name
11801 then
11802 Par_Ent := Entity (Prefix (Gen_Id));
11803 Par_Vis := Is_Immediately_Visible (Par_Ent);
11804 Install_Parent (Par_Ent, In_Body => True);
11805 Parent_Installed := True;
11807 elsif Is_Child_Unit (Gen_Unit) then
11808 Par_Ent := Scope (Gen_Unit);
11809 Par_Vis := Is_Immediately_Visible (Par_Ent);
11810 Install_Parent (Par_Ent, In_Body => True);
11811 Parent_Installed := True;
11812 end if;
11814 -- Subprogram body is placed in the body of wrapper package,
11815 -- whose spec contains the subprogram declaration as well as
11816 -- the renaming declarations for the generic parameters.
11818 Pack_Body :=
11819 Make_Package_Body (Loc,
11820 Defining_Unit_Name => New_Copy (Pack_Id),
11821 Declarations => New_List (Act_Body));
11823 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11825 -- If the instantiation is a library unit, then build resulting
11826 -- compilation unit nodes for the instance. The declaration of
11827 -- the enclosing package is the grandparent of the subprogram
11828 -- declaration. First replace the instantiation node as the unit
11829 -- of the corresponding compilation.
11831 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11832 if Parent (Inst_Node) = Cunit (Main_Unit) then
11833 Set_Unit (Parent (Inst_Node), Inst_Node);
11834 Build_Instance_Compilation_Unit_Nodes
11835 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11836 Analyze (Inst_Node);
11837 else
11838 Set_Parent (Pack_Body, Parent (Inst_Node));
11839 Analyze (Pack_Body);
11840 end if;
11842 else
11843 Insert_Before (Inst_Node, Pack_Body);
11844 Mark_Rewrite_Insertion (Pack_Body);
11845 Analyze (Pack_Body);
11847 if Expander_Active then
11848 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11849 end if;
11850 end if;
11852 Inherit_Context (Gen_Body, Inst_Node);
11854 Restore_Private_Views (Pack_Id, False);
11856 if Parent_Installed then
11857 Remove_Parent (In_Body => True);
11859 -- Restore the previous visibility of the parent
11861 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11862 end if;
11864 Restore_Env;
11865 Restore_Warnings (Saved_Warnings);
11867 -- Body not found. Error was emitted already. If there were no previous
11868 -- errors, this may be an instance whose scope is a premature instance.
11869 -- In that case we must insure that the (legal) program does raise
11870 -- program error if executed. We generate a subprogram body for this
11871 -- purpose. See DEC ac30vso.
11873 -- Should not reference proprietary DEC tests in comments ???
11875 elsif Serious_Errors_Detected = 0
11876 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
11877 then
11878 if Body_Optional then
11879 goto Leave;
11881 elsif Ekind (Act_Decl_Id) = E_Procedure then
11882 Act_Body :=
11883 Make_Subprogram_Body (Loc,
11884 Specification =>
11885 Make_Procedure_Specification (Loc,
11886 Defining_Unit_Name =>
11887 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11888 Parameter_Specifications =>
11889 New_Copy_List
11890 (Parameter_Specifications (Parent (Act_Decl_Id)))),
11892 Declarations => Empty_List,
11893 Handled_Statement_Sequence =>
11894 Make_Handled_Sequence_Of_Statements (Loc,
11895 Statements => New_List (
11896 Make_Raise_Program_Error (Loc,
11897 Reason => PE_Access_Before_Elaboration))));
11899 else
11900 Ret_Expr :=
11901 Make_Raise_Program_Error (Loc,
11902 Reason => PE_Access_Before_Elaboration);
11904 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
11905 Set_Analyzed (Ret_Expr);
11907 Act_Body :=
11908 Make_Subprogram_Body (Loc,
11909 Specification =>
11910 Make_Function_Specification (Loc,
11911 Defining_Unit_Name =>
11912 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11913 Parameter_Specifications =>
11914 New_Copy_List
11915 (Parameter_Specifications (Parent (Act_Decl_Id))),
11916 Result_Definition =>
11917 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
11919 Declarations => Empty_List,
11920 Handled_Statement_Sequence =>
11921 Make_Handled_Sequence_Of_Statements (Loc,
11922 Statements => New_List (
11923 Make_Simple_Return_Statement (Loc, Ret_Expr))));
11924 end if;
11926 Pack_Body :=
11927 Make_Package_Body (Loc,
11928 Defining_Unit_Name => New_Copy (Pack_Id),
11929 Declarations => New_List (Act_Body));
11931 Insert_After (Inst_Node, Pack_Body);
11932 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11933 Analyze (Pack_Body);
11934 end if;
11936 Expander_Mode_Restore;
11938 <<Leave>>
11939 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
11940 Restore_Ghost_Region (Saved_GM, Saved_IGR);
11941 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
11942 Style_Check := Saved_Style_Check;
11943 end Instantiate_Subprogram_Body;
11945 ----------------------
11946 -- Instantiate_Type --
11947 ----------------------
11949 function Instantiate_Type
11950 (Formal : Node_Id;
11951 Actual : Node_Id;
11952 Analyzed_Formal : Node_Id;
11953 Actual_Decls : List_Id) return List_Id
11955 A_Gen_T : constant Entity_Id :=
11956 Defining_Identifier (Analyzed_Formal);
11957 Def : constant Node_Id := Formal_Type_Definition (Formal);
11958 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
11959 Act_T : Entity_Id;
11960 Ancestor : Entity_Id := Empty;
11961 Decl_Node : Node_Id;
11962 Decl_Nodes : List_Id;
11963 Loc : Source_Ptr;
11964 Subt : Entity_Id;
11966 procedure Diagnose_Predicated_Actual;
11967 -- There are a number of constructs in which a discrete type with
11968 -- predicates is illegal, e.g. as an index in an array type declaration.
11969 -- If a generic type is used is such a construct in a generic package
11970 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11971 -- of the generic contract that the actual cannot have predicates.
11973 procedure Validate_Array_Type_Instance;
11974 procedure Validate_Access_Subprogram_Instance;
11975 procedure Validate_Access_Type_Instance;
11976 procedure Validate_Derived_Type_Instance;
11977 procedure Validate_Derived_Interface_Type_Instance;
11978 procedure Validate_Discriminated_Formal_Type;
11979 procedure Validate_Interface_Type_Instance;
11980 procedure Validate_Private_Type_Instance;
11981 procedure Validate_Incomplete_Type_Instance;
11982 -- These procedures perform validation tests for the named case.
11983 -- Validate_Discriminated_Formal_Type is shared by formal private
11984 -- types and Ada 2012 formal incomplete types.
11986 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
11987 -- Check that base types are the same and that the subtypes match
11988 -- statically. Used in several of the above.
11990 ---------------------------------
11991 -- Diagnose_Predicated_Actual --
11992 ---------------------------------
11994 procedure Diagnose_Predicated_Actual is
11995 begin
11996 if No_Predicate_On_Actual (A_Gen_T)
11997 and then Has_Predicates (Act_T)
11998 then
11999 Error_Msg_NE
12000 ("actual for& cannot be a type with predicate",
12001 Instantiation_Node, A_Gen_T);
12003 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
12004 and then Has_Predicates (Act_T)
12005 and then not Has_Static_Predicate_Aspect (Act_T)
12006 then
12007 Error_Msg_NE
12008 ("actual for& cannot be a type with a dynamic predicate",
12009 Instantiation_Node, A_Gen_T);
12010 end if;
12011 end Diagnose_Predicated_Actual;
12013 --------------------
12014 -- Subtypes_Match --
12015 --------------------
12017 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
12018 T : constant Entity_Id := Get_Instance_Of (Gen_T);
12020 begin
12021 -- Some detailed comments would be useful here ???
12023 return ((Base_Type (T) = Act_T
12024 or else Base_Type (T) = Base_Type (Act_T))
12025 and then Subtypes_Statically_Match (T, Act_T))
12027 or else (Is_Class_Wide_Type (Gen_T)
12028 and then Is_Class_Wide_Type (Act_T)
12029 and then Subtypes_Match
12030 (Get_Instance_Of (Root_Type (Gen_T)),
12031 Root_Type (Act_T)))
12033 or else
12034 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
12035 E_Anonymous_Access_Type)
12036 and then Ekind (Act_T) = Ekind (Gen_T)
12037 and then Subtypes_Statically_Match
12038 (Designated_Type (Gen_T), Designated_Type (Act_T)));
12039 end Subtypes_Match;
12041 -----------------------------------------
12042 -- Validate_Access_Subprogram_Instance --
12043 -----------------------------------------
12045 procedure Validate_Access_Subprogram_Instance is
12046 begin
12047 if not Is_Access_Type (Act_T)
12048 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
12049 then
12050 Error_Msg_NE
12051 ("expect access type in instantiation of &", Actual, Gen_T);
12052 Abandon_Instantiation (Actual);
12053 end if;
12055 -- According to AI05-288, actuals for access_to_subprograms must be
12056 -- subtype conformant with the generic formal. Previous to AI05-288
12057 -- only mode conformance was required.
12059 -- This is a binding interpretation that applies to previous versions
12060 -- of the language, no need to maintain previous weaker checks.
12062 Check_Subtype_Conformant
12063 (Designated_Type (Act_T),
12064 Designated_Type (A_Gen_T),
12065 Actual,
12066 Get_Inst => True);
12068 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
12069 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
12070 Error_Msg_NE
12071 ("protected access type not allowed for formal &",
12072 Actual, Gen_T);
12073 end if;
12075 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
12076 Error_Msg_NE
12077 ("expect protected access type for formal &",
12078 Actual, Gen_T);
12079 end if;
12081 -- If the formal has a specified convention (which in most cases
12082 -- will be StdCall) verify that the actual has the same convention.
12084 if Has_Convention_Pragma (A_Gen_T)
12085 and then Convention (A_Gen_T) /= Convention (Act_T)
12086 then
12087 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
12088 Error_Msg_NE
12089 ("actual for formal & must have convention %", Actual, Gen_T);
12090 end if;
12091 end Validate_Access_Subprogram_Instance;
12093 -----------------------------------
12094 -- Validate_Access_Type_Instance --
12095 -----------------------------------
12097 procedure Validate_Access_Type_Instance is
12098 Desig_Type : constant Entity_Id :=
12099 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
12100 Desig_Act : Entity_Id;
12102 begin
12103 if not Is_Access_Type (Act_T) then
12104 Error_Msg_NE
12105 ("expect access type in instantiation of &", Actual, Gen_T);
12106 Abandon_Instantiation (Actual);
12107 end if;
12109 if Is_Access_Constant (A_Gen_T) then
12110 if not Is_Access_Constant (Act_T) then
12111 Error_Msg_N
12112 ("actual type must be access-to-constant type", Actual);
12113 Abandon_Instantiation (Actual);
12114 end if;
12115 else
12116 if Is_Access_Constant (Act_T) then
12117 Error_Msg_N
12118 ("actual type must be access-to-variable type", Actual);
12119 Abandon_Instantiation (Actual);
12121 elsif Ekind (A_Gen_T) = E_General_Access_Type
12122 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
12123 then
12124 Error_Msg_N -- CODEFIX
12125 ("actual must be general access type!", Actual);
12126 Error_Msg_NE -- CODEFIX
12127 ("add ALL to }!", Actual, Act_T);
12128 Abandon_Instantiation (Actual);
12129 end if;
12130 end if;
12132 -- The designated subtypes, that is to say the subtypes introduced
12133 -- by an access type declaration (and not by a subtype declaration)
12134 -- must match.
12136 Desig_Act := Designated_Type (Base_Type (Act_T));
12138 -- The designated type may have been introduced through a limited_
12139 -- with clause, in which case retrieve the non-limited view. This
12140 -- applies to incomplete types as well as to class-wide types.
12142 if From_Limited_With (Desig_Act) then
12143 Desig_Act := Available_View (Desig_Act);
12144 end if;
12146 if not Subtypes_Match (Desig_Type, Desig_Act) then
12147 Error_Msg_NE
12148 ("designated type of actual does not match that of formal &",
12149 Actual, Gen_T);
12151 if not Predicates_Match (Desig_Type, Desig_Act) then
12152 Error_Msg_N ("\predicates do not match", Actual);
12153 end if;
12155 Abandon_Instantiation (Actual);
12157 elsif Is_Access_Type (Designated_Type (Act_T))
12158 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
12160 Is_Constrained (Designated_Type (Desig_Type))
12161 then
12162 Error_Msg_NE
12163 ("designated type of actual does not match that of formal &",
12164 Actual, Gen_T);
12166 if not Predicates_Match (Desig_Type, Desig_Act) then
12167 Error_Msg_N ("\predicates do not match", Actual);
12168 end if;
12170 Abandon_Instantiation (Actual);
12171 end if;
12173 -- Ada 2005: null-exclusion indicators of the two types must agree
12175 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12176 Error_Msg_NE
12177 ("non null exclusion of actual and formal & do not match",
12178 Actual, Gen_T);
12179 end if;
12180 end Validate_Access_Type_Instance;
12182 ----------------------------------
12183 -- Validate_Array_Type_Instance --
12184 ----------------------------------
12186 procedure Validate_Array_Type_Instance is
12187 I1 : Node_Id;
12188 I2 : Node_Id;
12189 T2 : Entity_Id;
12191 function Formal_Dimensions return Nat;
12192 -- Count number of dimensions in array type formal
12194 -----------------------
12195 -- Formal_Dimensions --
12196 -----------------------
12198 function Formal_Dimensions return Nat is
12199 Num : Nat := 0;
12200 Index : Node_Id;
12202 begin
12203 if Nkind (Def) = N_Constrained_Array_Definition then
12204 Index := First (Discrete_Subtype_Definitions (Def));
12205 else
12206 Index := First (Subtype_Marks (Def));
12207 end if;
12209 while Present (Index) loop
12210 Num := Num + 1;
12211 Next_Index (Index);
12212 end loop;
12214 return Num;
12215 end Formal_Dimensions;
12217 -- Start of processing for Validate_Array_Type_Instance
12219 begin
12220 if not Is_Array_Type (Act_T) then
12221 Error_Msg_NE
12222 ("expect array type in instantiation of &", Actual, Gen_T);
12223 Abandon_Instantiation (Actual);
12225 elsif Nkind (Def) = N_Constrained_Array_Definition then
12226 if not (Is_Constrained (Act_T)) then
12227 Error_Msg_NE
12228 ("expect constrained array in instantiation of &",
12229 Actual, Gen_T);
12230 Abandon_Instantiation (Actual);
12231 end if;
12233 else
12234 if Is_Constrained (Act_T) then
12235 Error_Msg_NE
12236 ("expect unconstrained array in instantiation of &",
12237 Actual, Gen_T);
12238 Abandon_Instantiation (Actual);
12239 end if;
12240 end if;
12242 if Formal_Dimensions /= Number_Dimensions (Act_T) then
12243 Error_Msg_NE
12244 ("dimensions of actual do not match formal &", Actual, Gen_T);
12245 Abandon_Instantiation (Actual);
12246 end if;
12248 I1 := First_Index (A_Gen_T);
12249 I2 := First_Index (Act_T);
12250 for J in 1 .. Formal_Dimensions loop
12252 -- If the indexes of the actual were given by a subtype_mark,
12253 -- the index was transformed into a range attribute. Retrieve
12254 -- the original type mark for checking.
12256 if Is_Entity_Name (Original_Node (I2)) then
12257 T2 := Entity (Original_Node (I2));
12258 else
12259 T2 := Etype (I2);
12260 end if;
12262 if not Subtypes_Match
12263 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
12264 then
12265 Error_Msg_NE
12266 ("index types of actual do not match those of formal &",
12267 Actual, Gen_T);
12268 Abandon_Instantiation (Actual);
12269 end if;
12271 Next_Index (I1);
12272 Next_Index (I2);
12273 end loop;
12275 -- Check matching subtypes. Note that there are complex visibility
12276 -- issues when the generic is a child unit and some aspect of the
12277 -- generic type is declared in a parent unit of the generic. We do
12278 -- the test to handle this special case only after a direct check
12279 -- for static matching has failed. The case where both the component
12280 -- type and the array type are separate formals, and the component
12281 -- type is a private view may also require special checking in
12282 -- Subtypes_Match. Finally, we assume that a child instance where
12283 -- the component type comes from a formal of a parent instance is
12284 -- correct because the generic was correct. A more precise check
12285 -- seems too complex to install???
12287 if Subtypes_Match
12288 (Component_Type (A_Gen_T), Component_Type (Act_T))
12289 or else
12290 Subtypes_Match
12291 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
12292 Component_Type (Act_T))
12293 or else
12294 (not Inside_A_Generic
12295 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
12296 then
12297 null;
12298 else
12299 Error_Msg_NE
12300 ("component subtype of actual does not match that of formal &",
12301 Actual, Gen_T);
12302 Abandon_Instantiation (Actual);
12303 end if;
12305 if Has_Aliased_Components (A_Gen_T)
12306 and then not Has_Aliased_Components (Act_T)
12307 then
12308 Error_Msg_NE
12309 ("actual must have aliased components to match formal type &",
12310 Actual, Gen_T);
12311 end if;
12312 end Validate_Array_Type_Instance;
12314 -----------------------------------------------
12315 -- Validate_Derived_Interface_Type_Instance --
12316 -----------------------------------------------
12318 procedure Validate_Derived_Interface_Type_Instance is
12319 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
12320 Elmt : Elmt_Id;
12322 begin
12323 -- First apply interface instance checks
12325 Validate_Interface_Type_Instance;
12327 -- Verify that immediate parent interface is an ancestor of
12328 -- the actual.
12330 if Present (Par)
12331 and then not Interface_Present_In_Ancestor (Act_T, Par)
12332 then
12333 Error_Msg_NE
12334 ("interface actual must include progenitor&", Actual, Par);
12335 end if;
12337 -- Now verify that the actual includes all other ancestors of
12338 -- the formal.
12340 Elmt := First_Elmt (Interfaces (A_Gen_T));
12341 while Present (Elmt) loop
12342 if not Interface_Present_In_Ancestor
12343 (Act_T, Get_Instance_Of (Node (Elmt)))
12344 then
12345 Error_Msg_NE
12346 ("interface actual must include progenitor&",
12347 Actual, Node (Elmt));
12348 end if;
12350 Next_Elmt (Elmt);
12351 end loop;
12352 end Validate_Derived_Interface_Type_Instance;
12354 ------------------------------------
12355 -- Validate_Derived_Type_Instance --
12356 ------------------------------------
12358 procedure Validate_Derived_Type_Instance is
12359 Actual_Discr : Entity_Id;
12360 Ancestor_Discr : Entity_Id;
12362 begin
12363 -- Verify that the actual includes the progenitors of the formal,
12364 -- if any. The formal may depend on previous formals and their
12365 -- instance, so we must examine instance of interfaces if present.
12366 -- The actual may be an extension of an interface, in which case
12367 -- it does not appear in the interface list, so this must be
12368 -- checked separately.
12370 if Present (Interface_List (Def)) then
12371 if not Has_Interfaces (Act_T) then
12372 Error_Msg_NE
12373 ("actual must implement all interfaces of formal&",
12374 Actual, A_Gen_T);
12376 else
12377 declare
12378 Act_Iface_List : Elist_Id;
12379 Iface : Node_Id;
12380 Iface_Ent : Entity_Id;
12382 function Instance_Exists (I : Entity_Id) return Boolean;
12383 -- If the interface entity is declared in a generic unit,
12384 -- this can only be legal if we are within an instantiation
12385 -- of a child of that generic. There is currently no
12386 -- mechanism to relate an interface declared within a
12387 -- generic to the corresponding interface in an instance,
12388 -- so we traverse the list of interfaces of the actual,
12389 -- looking for a name match.
12391 ---------------------
12392 -- Instance_Exists --
12393 ---------------------
12395 function Instance_Exists (I : Entity_Id) return Boolean is
12396 Iface_Elmt : Elmt_Id;
12398 begin
12399 Iface_Elmt := First_Elmt (Act_Iface_List);
12400 while Present (Iface_Elmt) loop
12401 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
12402 and then Chars (Node (Iface_Elmt)) = Chars (I)
12403 then
12404 return True;
12405 end if;
12407 Next_Elmt (Iface_Elmt);
12408 end loop;
12410 return False;
12411 end Instance_Exists;
12413 begin
12414 Iface := First (Abstract_Interface_List (A_Gen_T));
12415 Collect_Interfaces (Act_T, Act_Iface_List);
12417 while Present (Iface) loop
12418 Iface_Ent := Get_Instance_Of (Entity (Iface));
12420 if Is_Ancestor (Iface_Ent, Act_T)
12421 or else Is_Progenitor (Iface_Ent, Act_T)
12422 then
12423 null;
12425 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
12426 and then Instance_Exists (Iface_Ent)
12427 then
12428 null;
12430 else
12431 Error_Msg_Name_1 := Chars (Act_T);
12432 Error_Msg_NE
12433 ("Actual% must implement interface&",
12434 Actual, Etype (Iface));
12435 end if;
12437 Next (Iface);
12438 end loop;
12439 end;
12440 end if;
12441 end if;
12443 -- If the parent type in the generic declaration is itself a previous
12444 -- formal type, then it is local to the generic and absent from the
12445 -- analyzed generic definition. In that case the ancestor is the
12446 -- instance of the formal (which must have been instantiated
12447 -- previously), unless the ancestor is itself a formal derived type.
12448 -- In this latter case (which is the subject of Corrigendum 8652/0038
12449 -- (AI-202) the ancestor of the formals is the ancestor of its
12450 -- parent. Otherwise, the analyzed generic carries the parent type.
12451 -- If the parent type is defined in a previous formal package, then
12452 -- the scope of that formal package is that of the generic type
12453 -- itself, and it has already been mapped into the corresponding type
12454 -- in the actual package.
12456 -- Common case: parent type defined outside of the generic
12458 if Is_Entity_Name (Subtype_Mark (Def))
12459 and then Present (Entity (Subtype_Mark (Def)))
12460 then
12461 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
12463 -- Check whether parent is defined in a previous formal package
12465 elsif
12466 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
12467 then
12468 Ancestor :=
12469 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
12471 -- The type may be a local derivation, or a type extension of a
12472 -- previous formal, or of a formal of a parent package.
12474 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
12475 or else
12476 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
12477 then
12478 -- Check whether the parent is another derived formal type in the
12479 -- same generic unit.
12481 if Etype (A_Gen_T) /= A_Gen_T
12482 and then Is_Generic_Type (Etype (A_Gen_T))
12483 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
12484 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
12485 then
12486 -- Locate ancestor of parent from the subtype declaration
12487 -- created for the actual.
12489 declare
12490 Decl : Node_Id;
12492 begin
12493 Decl := First (Actual_Decls);
12494 while Present (Decl) loop
12495 if Nkind (Decl) = N_Subtype_Declaration
12496 and then Chars (Defining_Identifier (Decl)) =
12497 Chars (Etype (A_Gen_T))
12498 then
12499 Ancestor := Generic_Parent_Type (Decl);
12500 exit;
12501 else
12502 Next (Decl);
12503 end if;
12504 end loop;
12505 end;
12507 pragma Assert (Present (Ancestor));
12509 -- The ancestor itself may be a previous formal that has been
12510 -- instantiated.
12512 Ancestor := Get_Instance_Of (Ancestor);
12514 else
12515 Ancestor :=
12516 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
12517 end if;
12519 -- Check whether parent is a previous formal of the current generic
12521 elsif Is_Derived_Type (A_Gen_T)
12522 and then Is_Generic_Type (Etype (A_Gen_T))
12523 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
12524 then
12525 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
12527 -- An unusual case: the actual is a type declared in a parent unit,
12528 -- but is not a formal type so there is no instance_of for it.
12529 -- Retrieve it by analyzing the record extension.
12531 elsif Is_Child_Unit (Scope (A_Gen_T))
12532 and then In_Open_Scopes (Scope (Act_T))
12533 and then Is_Generic_Instance (Scope (Act_T))
12534 then
12535 Analyze (Subtype_Mark (Def));
12536 Ancestor := Entity (Subtype_Mark (Def));
12538 else
12539 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
12540 end if;
12542 -- If the formal derived type has pragma Preelaborable_Initialization
12543 -- then the actual type must have preelaborable initialization.
12545 if Known_To_Have_Preelab_Init (A_Gen_T)
12546 and then not Has_Preelaborable_Initialization (Act_T)
12547 then
12548 Error_Msg_NE
12549 ("actual for & must have preelaborable initialization",
12550 Actual, Gen_T);
12551 end if;
12553 -- Ada 2005 (AI-251)
12555 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
12556 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
12557 Error_Msg_NE
12558 ("(Ada 2005) expected type implementing & in instantiation",
12559 Actual, Ancestor);
12560 end if;
12562 -- Finally verify that the (instance of) the ancestor is an ancestor
12563 -- of the actual.
12565 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
12566 Error_Msg_NE
12567 ("expect type derived from & in instantiation",
12568 Actual, First_Subtype (Ancestor));
12569 Abandon_Instantiation (Actual);
12570 end if;
12572 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
12573 -- that the formal type declaration has been rewritten as a private
12574 -- extension.
12576 if Ada_Version >= Ada_2005
12577 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
12578 and then Synchronized_Present (Parent (A_Gen_T))
12579 then
12580 -- The actual must be a synchronized tagged type
12582 if not Is_Tagged_Type (Act_T) then
12583 Error_Msg_N
12584 ("actual of synchronized type must be tagged", Actual);
12585 Abandon_Instantiation (Actual);
12587 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
12588 and then Nkind (Type_Definition (Parent (Act_T))) =
12589 N_Derived_Type_Definition
12590 and then not Synchronized_Present
12591 (Type_Definition (Parent (Act_T)))
12592 then
12593 Error_Msg_N
12594 ("actual of synchronized type must be synchronized", Actual);
12595 Abandon_Instantiation (Actual);
12596 end if;
12597 end if;
12599 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
12600 -- removes the second instance of the phrase "or allow pass by copy".
12602 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
12603 Error_Msg_N
12604 ("cannot have atomic actual type for non-atomic formal type",
12605 Actual);
12607 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
12608 Error_Msg_N
12609 ("cannot have volatile actual type for non-volatile formal type",
12610 Actual);
12611 end if;
12613 -- It should not be necessary to check for unknown discriminants on
12614 -- Formal, but for some reason Has_Unknown_Discriminants is false for
12615 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
12616 -- needs fixing. ???
12618 if Is_Definite_Subtype (A_Gen_T)
12619 and then not Unknown_Discriminants_Present (Formal)
12620 and then not Is_Definite_Subtype (Act_T)
12621 then
12622 Error_Msg_N ("actual subtype must be constrained", Actual);
12623 Abandon_Instantiation (Actual);
12624 end if;
12626 if not Unknown_Discriminants_Present (Formal) then
12627 if Is_Constrained (Ancestor) then
12628 if not Is_Constrained (Act_T) then
12629 Error_Msg_N ("actual subtype must be constrained", Actual);
12630 Abandon_Instantiation (Actual);
12631 end if;
12633 -- Ancestor is unconstrained, Check if generic formal and actual
12634 -- agree on constrainedness. The check only applies to array types
12635 -- and discriminated types.
12637 elsif Is_Constrained (Act_T) then
12638 if Ekind (Ancestor) = E_Access_Type
12639 or else (not Is_Constrained (A_Gen_T)
12640 and then Is_Composite_Type (A_Gen_T))
12641 then
12642 Error_Msg_N ("actual subtype must be unconstrained", Actual);
12643 Abandon_Instantiation (Actual);
12644 end if;
12646 -- A class-wide type is only allowed if the formal has unknown
12647 -- discriminants.
12649 elsif Is_Class_Wide_Type (Act_T)
12650 and then not Has_Unknown_Discriminants (Ancestor)
12651 then
12652 Error_Msg_NE
12653 ("actual for & cannot be a class-wide type", Actual, Gen_T);
12654 Abandon_Instantiation (Actual);
12656 -- Otherwise, the formal and actual must have the same number
12657 -- of discriminants and each discriminant of the actual must
12658 -- correspond to a discriminant of the formal.
12660 elsif Has_Discriminants (Act_T)
12661 and then not Has_Unknown_Discriminants (Act_T)
12662 and then Has_Discriminants (Ancestor)
12663 then
12664 Actual_Discr := First_Discriminant (Act_T);
12665 Ancestor_Discr := First_Discriminant (Ancestor);
12666 while Present (Actual_Discr)
12667 and then Present (Ancestor_Discr)
12668 loop
12669 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
12670 No (Corresponding_Discriminant (Actual_Discr))
12671 then
12672 Error_Msg_NE
12673 ("discriminant & does not correspond "
12674 & "to ancestor discriminant", Actual, Actual_Discr);
12675 Abandon_Instantiation (Actual);
12676 end if;
12678 Next_Discriminant (Actual_Discr);
12679 Next_Discriminant (Ancestor_Discr);
12680 end loop;
12682 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
12683 Error_Msg_NE
12684 ("actual for & must have same number of discriminants",
12685 Actual, Gen_T);
12686 Abandon_Instantiation (Actual);
12687 end if;
12689 -- This case should be caught by the earlier check for
12690 -- constrainedness, but the check here is added for completeness.
12692 elsif Has_Discriminants (Act_T)
12693 and then not Has_Unknown_Discriminants (Act_T)
12694 then
12695 Error_Msg_NE
12696 ("actual for & must not have discriminants", Actual, Gen_T);
12697 Abandon_Instantiation (Actual);
12699 elsif Has_Discriminants (Ancestor) then
12700 Error_Msg_NE
12701 ("actual for & must have known discriminants", Actual, Gen_T);
12702 Abandon_Instantiation (Actual);
12703 end if;
12705 if not Subtypes_Statically_Compatible
12706 (Act_T, Ancestor, Formal_Derived_Matching => True)
12707 then
12708 Error_Msg_N
12709 ("constraint on actual is incompatible with formal", Actual);
12710 Abandon_Instantiation (Actual);
12711 end if;
12712 end if;
12714 -- If the formal and actual types are abstract, check that there
12715 -- are no abstract primitives of the actual type that correspond to
12716 -- nonabstract primitives of the formal type (second sentence of
12717 -- RM95 3.9.3(9)).
12719 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
12720 Check_Abstract_Primitives : declare
12721 Gen_Prims : constant Elist_Id :=
12722 Primitive_Operations (A_Gen_T);
12723 Gen_Elmt : Elmt_Id;
12724 Gen_Subp : Entity_Id;
12725 Anc_Subp : Entity_Id;
12726 Anc_Formal : Entity_Id;
12727 Anc_F_Type : Entity_Id;
12729 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
12730 Act_Elmt : Elmt_Id;
12731 Act_Subp : Entity_Id;
12732 Act_Formal : Entity_Id;
12733 Act_F_Type : Entity_Id;
12735 Subprograms_Correspond : Boolean;
12737 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
12738 -- Returns true if T2 is derived directly or indirectly from
12739 -- T1, including derivations from interfaces. T1 and T2 are
12740 -- required to be specific tagged base types.
12742 ------------------------
12743 -- Is_Tagged_Ancestor --
12744 ------------------------
12746 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
12748 Intfc_Elmt : Elmt_Id;
12750 begin
12751 -- The predicate is satisfied if the types are the same
12753 if T1 = T2 then
12754 return True;
12756 -- If we've reached the top of the derivation chain then
12757 -- we know that T1 is not an ancestor of T2.
12759 elsif Etype (T2) = T2 then
12760 return False;
12762 -- Proceed to check T2's immediate parent
12764 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
12765 return True;
12767 -- Finally, check to see if T1 is an ancestor of any of T2's
12768 -- progenitors.
12770 else
12771 Intfc_Elmt := First_Elmt (Interfaces (T2));
12772 while Present (Intfc_Elmt) loop
12773 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
12774 return True;
12775 end if;
12777 Next_Elmt (Intfc_Elmt);
12778 end loop;
12779 end if;
12781 return False;
12782 end Is_Tagged_Ancestor;
12784 -- Start of processing for Check_Abstract_Primitives
12786 begin
12787 -- Loop over all of the formal derived type's primitives
12789 Gen_Elmt := First_Elmt (Gen_Prims);
12790 while Present (Gen_Elmt) loop
12791 Gen_Subp := Node (Gen_Elmt);
12793 -- If the primitive of the formal is not abstract, then
12794 -- determine whether there is a corresponding primitive of
12795 -- the actual type that's abstract.
12797 if not Is_Abstract_Subprogram (Gen_Subp) then
12798 Act_Elmt := First_Elmt (Act_Prims);
12799 while Present (Act_Elmt) loop
12800 Act_Subp := Node (Act_Elmt);
12802 -- If we find an abstract primitive of the actual,
12803 -- then we need to test whether it corresponds to the
12804 -- subprogram from which the generic formal primitive
12805 -- is inherited.
12807 if Is_Abstract_Subprogram (Act_Subp) then
12808 Anc_Subp := Alias (Gen_Subp);
12810 -- Test whether we have a corresponding primitive
12811 -- by comparing names, kinds, formal types, and
12812 -- result types.
12814 if Chars (Anc_Subp) = Chars (Act_Subp)
12815 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
12816 then
12817 Anc_Formal := First_Formal (Anc_Subp);
12818 Act_Formal := First_Formal (Act_Subp);
12819 while Present (Anc_Formal)
12820 and then Present (Act_Formal)
12821 loop
12822 Anc_F_Type := Etype (Anc_Formal);
12823 Act_F_Type := Etype (Act_Formal);
12825 if Ekind (Anc_F_Type) =
12826 E_Anonymous_Access_Type
12827 then
12828 Anc_F_Type := Designated_Type (Anc_F_Type);
12830 if Ekind (Act_F_Type) =
12831 E_Anonymous_Access_Type
12832 then
12833 Act_F_Type :=
12834 Designated_Type (Act_F_Type);
12835 else
12836 exit;
12837 end if;
12839 elsif
12840 Ekind (Act_F_Type) = E_Anonymous_Access_Type
12841 then
12842 exit;
12843 end if;
12845 Anc_F_Type := Base_Type (Anc_F_Type);
12846 Act_F_Type := Base_Type (Act_F_Type);
12848 -- If the formal is controlling, then the
12849 -- the type of the actual primitive's formal
12850 -- must be derived directly or indirectly
12851 -- from the type of the ancestor primitive's
12852 -- formal.
12854 if Is_Controlling_Formal (Anc_Formal) then
12855 if not Is_Tagged_Ancestor
12856 (Anc_F_Type, Act_F_Type)
12857 then
12858 exit;
12859 end if;
12861 -- Otherwise the types of the formals must
12862 -- be the same.
12864 elsif Anc_F_Type /= Act_F_Type then
12865 exit;
12866 end if;
12868 Next_Entity (Anc_Formal);
12869 Next_Entity (Act_Formal);
12870 end loop;
12872 -- If we traversed through all of the formals
12873 -- then so far the subprograms correspond, so
12874 -- now check that any result types correspond.
12876 if No (Anc_Formal) and then No (Act_Formal) then
12877 Subprograms_Correspond := True;
12879 if Ekind (Act_Subp) = E_Function then
12880 Anc_F_Type := Etype (Anc_Subp);
12881 Act_F_Type := Etype (Act_Subp);
12883 if Ekind (Anc_F_Type) =
12884 E_Anonymous_Access_Type
12885 then
12886 Anc_F_Type :=
12887 Designated_Type (Anc_F_Type);
12889 if Ekind (Act_F_Type) =
12890 E_Anonymous_Access_Type
12891 then
12892 Act_F_Type :=
12893 Designated_Type (Act_F_Type);
12894 else
12895 Subprograms_Correspond := False;
12896 end if;
12898 elsif
12899 Ekind (Act_F_Type)
12900 = E_Anonymous_Access_Type
12901 then
12902 Subprograms_Correspond := False;
12903 end if;
12905 Anc_F_Type := Base_Type (Anc_F_Type);
12906 Act_F_Type := Base_Type (Act_F_Type);
12908 -- Now either the result types must be
12909 -- the same or, if the result type is
12910 -- controlling, the result type of the
12911 -- actual primitive must descend from the
12912 -- result type of the ancestor primitive.
12914 if Subprograms_Correspond
12915 and then Anc_F_Type /= Act_F_Type
12916 and then
12917 Has_Controlling_Result (Anc_Subp)
12918 and then not Is_Tagged_Ancestor
12919 (Anc_F_Type, Act_F_Type)
12920 then
12921 Subprograms_Correspond := False;
12922 end if;
12923 end if;
12925 -- Found a matching subprogram belonging to
12926 -- formal ancestor type, so actual subprogram
12927 -- corresponds and this violates 3.9.3(9).
12929 if Subprograms_Correspond then
12930 Error_Msg_NE
12931 ("abstract subprogram & overrides "
12932 & "nonabstract subprogram of ancestor",
12933 Actual, Act_Subp);
12934 end if;
12935 end if;
12936 end if;
12937 end if;
12939 Next_Elmt (Act_Elmt);
12940 end loop;
12941 end if;
12943 Next_Elmt (Gen_Elmt);
12944 end loop;
12945 end Check_Abstract_Primitives;
12946 end if;
12948 -- Verify that limitedness matches. If parent is a limited
12949 -- interface then the generic formal is not unless declared
12950 -- explicitly so. If not declared limited, the actual cannot be
12951 -- limited (see AI05-0087).
12953 -- Even though this AI is a binding interpretation, we enable the
12954 -- check only in Ada 2012 mode, because this improper construct
12955 -- shows up in user code and in existing B-tests.
12957 if Is_Limited_Type (Act_T)
12958 and then not Is_Limited_Type (A_Gen_T)
12959 and then Ada_Version >= Ada_2012
12960 then
12961 if In_Instance then
12962 null;
12963 else
12964 Error_Msg_NE
12965 ("actual for non-limited & cannot be a limited type",
12966 Actual, Gen_T);
12967 Explain_Limited_Type (Act_T, Actual);
12968 Abandon_Instantiation (Actual);
12969 end if;
12970 end if;
12971 end Validate_Derived_Type_Instance;
12973 ----------------------------------------
12974 -- Validate_Discriminated_Formal_Type --
12975 ----------------------------------------
12977 procedure Validate_Discriminated_Formal_Type is
12978 Formal_Discr : Entity_Id;
12979 Actual_Discr : Entity_Id;
12980 Formal_Subt : Entity_Id;
12982 begin
12983 if Has_Discriminants (A_Gen_T) then
12984 if not Has_Discriminants (Act_T) then
12985 Error_Msg_NE
12986 ("actual for & must have discriminants", Actual, Gen_T);
12987 Abandon_Instantiation (Actual);
12989 elsif Is_Constrained (Act_T) then
12990 Error_Msg_NE
12991 ("actual for & must be unconstrained", Actual, Gen_T);
12992 Abandon_Instantiation (Actual);
12994 else
12995 Formal_Discr := First_Discriminant (A_Gen_T);
12996 Actual_Discr := First_Discriminant (Act_T);
12997 while Formal_Discr /= Empty loop
12998 if Actual_Discr = Empty then
12999 Error_Msg_NE
13000 ("discriminants on actual do not match formal",
13001 Actual, Gen_T);
13002 Abandon_Instantiation (Actual);
13003 end if;
13005 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
13007 -- Access discriminants match if designated types do
13009 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
13010 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
13011 E_Anonymous_Access_Type
13012 and then
13013 Get_Instance_Of
13014 (Designated_Type (Base_Type (Formal_Subt))) =
13015 Designated_Type (Base_Type (Etype (Actual_Discr)))
13016 then
13017 null;
13019 elsif Base_Type (Formal_Subt) /=
13020 Base_Type (Etype (Actual_Discr))
13021 then
13022 Error_Msg_NE
13023 ("types of actual discriminants must match formal",
13024 Actual, Gen_T);
13025 Abandon_Instantiation (Actual);
13027 elsif not Subtypes_Statically_Match
13028 (Formal_Subt, Etype (Actual_Discr))
13029 and then Ada_Version >= Ada_95
13030 then
13031 Error_Msg_NE
13032 ("subtypes of actual discriminants must match formal",
13033 Actual, Gen_T);
13034 Abandon_Instantiation (Actual);
13035 end if;
13037 Next_Discriminant (Formal_Discr);
13038 Next_Discriminant (Actual_Discr);
13039 end loop;
13041 if Actual_Discr /= Empty then
13042 Error_Msg_NE
13043 ("discriminants on actual do not match formal",
13044 Actual, Gen_T);
13045 Abandon_Instantiation (Actual);
13046 end if;
13047 end if;
13048 end if;
13049 end Validate_Discriminated_Formal_Type;
13051 ---------------------------------------
13052 -- Validate_Incomplete_Type_Instance --
13053 ---------------------------------------
13055 procedure Validate_Incomplete_Type_Instance is
13056 begin
13057 if not Is_Tagged_Type (Act_T)
13058 and then Is_Tagged_Type (A_Gen_T)
13059 then
13060 Error_Msg_NE
13061 ("actual for & must be a tagged type", Actual, Gen_T);
13062 end if;
13064 Validate_Discriminated_Formal_Type;
13065 end Validate_Incomplete_Type_Instance;
13067 --------------------------------------
13068 -- Validate_Interface_Type_Instance --
13069 --------------------------------------
13071 procedure Validate_Interface_Type_Instance is
13072 begin
13073 if not Is_Interface (Act_T) then
13074 Error_Msg_NE
13075 ("actual for formal interface type must be an interface",
13076 Actual, Gen_T);
13078 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
13079 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
13080 or else Is_Protected_Interface (A_Gen_T) /=
13081 Is_Protected_Interface (Act_T)
13082 or else Is_Synchronized_Interface (A_Gen_T) /=
13083 Is_Synchronized_Interface (Act_T)
13084 then
13085 Error_Msg_NE
13086 ("actual for interface& does not match (RM 12.5.5(4))",
13087 Actual, Gen_T);
13088 end if;
13089 end Validate_Interface_Type_Instance;
13091 ------------------------------------
13092 -- Validate_Private_Type_Instance --
13093 ------------------------------------
13095 procedure Validate_Private_Type_Instance is
13096 begin
13097 if Is_Limited_Type (Act_T)
13098 and then not Is_Limited_Type (A_Gen_T)
13099 then
13100 if In_Instance then
13101 null;
13102 else
13103 Error_Msg_NE
13104 ("actual for non-limited & cannot be a limited type", Actual,
13105 Gen_T);
13106 Explain_Limited_Type (Act_T, Actual);
13107 Abandon_Instantiation (Actual);
13108 end if;
13110 elsif Known_To_Have_Preelab_Init (A_Gen_T)
13111 and then not Has_Preelaborable_Initialization (Act_T)
13112 then
13113 Error_Msg_NE
13114 ("actual for & must have preelaborable initialization", Actual,
13115 Gen_T);
13117 elsif not Is_Definite_Subtype (Act_T)
13118 and then Is_Definite_Subtype (A_Gen_T)
13119 and then Ada_Version >= Ada_95
13120 then
13121 Error_Msg_NE
13122 ("actual for & must be a definite subtype", Actual, Gen_T);
13124 elsif not Is_Tagged_Type (Act_T)
13125 and then Is_Tagged_Type (A_Gen_T)
13126 then
13127 Error_Msg_NE
13128 ("actual for & must be a tagged type", Actual, Gen_T);
13129 end if;
13131 Validate_Discriminated_Formal_Type;
13132 Ancestor := Gen_T;
13133 end Validate_Private_Type_Instance;
13135 -- Start of processing for Instantiate_Type
13137 begin
13138 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
13139 Error_Msg_N ("duplicate instantiation of generic type", Actual);
13140 return New_List (Error);
13142 elsif not Is_Entity_Name (Actual)
13143 or else not Is_Type (Entity (Actual))
13144 then
13145 Error_Msg_NE
13146 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
13147 Abandon_Instantiation (Actual);
13149 else
13150 Act_T := Entity (Actual);
13152 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
13153 -- as a generic actual parameter if the corresponding formal type
13154 -- does not have a known_discriminant_part, or is a formal derived
13155 -- type that is an Unchecked_Union type.
13157 if Is_Unchecked_Union (Base_Type (Act_T)) then
13158 if not Has_Discriminants (A_Gen_T)
13159 or else (Is_Derived_Type (A_Gen_T)
13160 and then Is_Unchecked_Union (A_Gen_T))
13161 then
13162 null;
13163 else
13164 Error_Msg_N ("unchecked union cannot be the actual for a "
13165 & "discriminated formal type", Act_T);
13167 end if;
13168 end if;
13170 -- Deal with fixed/floating restrictions
13172 if Is_Floating_Point_Type (Act_T) then
13173 Check_Restriction (No_Floating_Point, Actual);
13174 elsif Is_Fixed_Point_Type (Act_T) then
13175 Check_Restriction (No_Fixed_Point, Actual);
13176 end if;
13178 -- Deal with error of using incomplete type as generic actual.
13179 -- This includes limited views of a type, even if the non-limited
13180 -- view may be available.
13182 if Ekind (Act_T) = E_Incomplete_Type
13183 or else (Is_Class_Wide_Type (Act_T)
13184 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
13185 then
13186 -- If the formal is an incomplete type, the actual can be
13187 -- incomplete as well.
13189 if Ekind (A_Gen_T) = E_Incomplete_Type then
13190 null;
13192 elsif Is_Class_Wide_Type (Act_T)
13193 or else No (Full_View (Act_T))
13194 then
13195 Error_Msg_N ("premature use of incomplete type", Actual);
13196 Abandon_Instantiation (Actual);
13197 else
13198 Act_T := Full_View (Act_T);
13199 Set_Entity (Actual, Act_T);
13201 if Has_Private_Component (Act_T) then
13202 Error_Msg_N
13203 ("premature use of type with private component", Actual);
13204 end if;
13205 end if;
13207 -- Deal with error of premature use of private type as generic actual
13209 elsif Is_Private_Type (Act_T)
13210 and then Is_Private_Type (Base_Type (Act_T))
13211 and then not Is_Generic_Type (Act_T)
13212 and then not Is_Derived_Type (Act_T)
13213 and then No (Full_View (Root_Type (Act_T)))
13214 then
13215 -- If the formal is an incomplete type, the actual can be
13216 -- private or incomplete as well.
13218 if Ekind (A_Gen_T) = E_Incomplete_Type then
13219 null;
13220 else
13221 Error_Msg_N ("premature use of private type", Actual);
13222 end if;
13224 elsif Has_Private_Component (Act_T) then
13225 Error_Msg_N
13226 ("premature use of type with private component", Actual);
13227 end if;
13229 Set_Instance_Of (A_Gen_T, Act_T);
13231 -- If the type is generic, the class-wide type may also be used
13233 if Is_Tagged_Type (A_Gen_T)
13234 and then Is_Tagged_Type (Act_T)
13235 and then not Is_Class_Wide_Type (A_Gen_T)
13236 then
13237 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
13238 Class_Wide_Type (Act_T));
13239 end if;
13241 if not Is_Abstract_Type (A_Gen_T)
13242 and then Is_Abstract_Type (Act_T)
13243 then
13244 Error_Msg_N
13245 ("actual of non-abstract formal cannot be abstract", Actual);
13246 end if;
13248 -- A generic scalar type is a first subtype for which we generate
13249 -- an anonymous base type. Indicate that the instance of this base
13250 -- is the base type of the actual.
13252 if Is_Scalar_Type (A_Gen_T) then
13253 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
13254 end if;
13255 end if;
13257 if Error_Posted (Act_T) then
13258 null;
13259 else
13260 case Nkind (Def) is
13261 when N_Formal_Private_Type_Definition =>
13262 Validate_Private_Type_Instance;
13264 when N_Formal_Incomplete_Type_Definition =>
13265 Validate_Incomplete_Type_Instance;
13267 when N_Formal_Derived_Type_Definition =>
13268 Validate_Derived_Type_Instance;
13270 when N_Formal_Discrete_Type_Definition =>
13271 if not Is_Discrete_Type (Act_T) then
13272 Error_Msg_NE
13273 ("expect discrete type in instantiation of&",
13274 Actual, Gen_T);
13275 Abandon_Instantiation (Actual);
13276 end if;
13278 Diagnose_Predicated_Actual;
13280 when N_Formal_Signed_Integer_Type_Definition =>
13281 if not Is_Signed_Integer_Type (Act_T) then
13282 Error_Msg_NE
13283 ("expect signed integer type in instantiation of&",
13284 Actual, Gen_T);
13285 Abandon_Instantiation (Actual);
13286 end if;
13288 Diagnose_Predicated_Actual;
13290 when N_Formal_Modular_Type_Definition =>
13291 if not Is_Modular_Integer_Type (Act_T) then
13292 Error_Msg_NE
13293 ("expect modular type in instantiation of &",
13294 Actual, Gen_T);
13295 Abandon_Instantiation (Actual);
13296 end if;
13298 Diagnose_Predicated_Actual;
13300 when N_Formal_Floating_Point_Definition =>
13301 if not Is_Floating_Point_Type (Act_T) then
13302 Error_Msg_NE
13303 ("expect float type in instantiation of &", Actual, Gen_T);
13304 Abandon_Instantiation (Actual);
13305 end if;
13307 when N_Formal_Ordinary_Fixed_Point_Definition =>
13308 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
13309 Error_Msg_NE
13310 ("expect ordinary fixed point type in instantiation of &",
13311 Actual, Gen_T);
13312 Abandon_Instantiation (Actual);
13313 end if;
13315 when N_Formal_Decimal_Fixed_Point_Definition =>
13316 if not Is_Decimal_Fixed_Point_Type (Act_T) then
13317 Error_Msg_NE
13318 ("expect decimal type in instantiation of &",
13319 Actual, Gen_T);
13320 Abandon_Instantiation (Actual);
13321 end if;
13323 when N_Array_Type_Definition =>
13324 Validate_Array_Type_Instance;
13326 when N_Access_To_Object_Definition =>
13327 Validate_Access_Type_Instance;
13329 when N_Access_Function_Definition
13330 | N_Access_Procedure_Definition
13332 Validate_Access_Subprogram_Instance;
13334 when N_Record_Definition =>
13335 Validate_Interface_Type_Instance;
13337 when N_Derived_Type_Definition =>
13338 Validate_Derived_Interface_Type_Instance;
13340 when others =>
13341 raise Program_Error;
13342 end case;
13343 end if;
13345 Subt := New_Copy (Gen_T);
13347 -- Use adjusted sloc of subtype name as the location for other nodes in
13348 -- the subtype declaration.
13350 Loc := Sloc (Subt);
13352 Decl_Node :=
13353 Make_Subtype_Declaration (Loc,
13354 Defining_Identifier => Subt,
13355 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
13357 if Is_Private_Type (Act_T) then
13358 Set_Has_Private_View (Subtype_Indication (Decl_Node));
13360 elsif Is_Access_Type (Act_T)
13361 and then Is_Private_Type (Designated_Type (Act_T))
13362 then
13363 Set_Has_Private_View (Subtype_Indication (Decl_Node));
13364 end if;
13366 -- In Ada 2012 the actual may be a limited view. Indicate that
13367 -- the local subtype must be treated as such.
13369 if From_Limited_With (Act_T) then
13370 Set_Ekind (Subt, E_Incomplete_Subtype);
13371 Set_From_Limited_With (Subt);
13372 end if;
13374 Decl_Nodes := New_List (Decl_Node);
13376 -- Flag actual derived types so their elaboration produces the
13377 -- appropriate renamings for the primitive operations of the ancestor.
13378 -- Flag actual for formal private types as well, to determine whether
13379 -- operations in the private part may override inherited operations.
13380 -- If the formal has an interface list, the ancestor is not the
13381 -- parent, but the analyzed formal that includes the interface
13382 -- operations of all its progenitors.
13384 -- Same treatment for formal private types, so we can check whether the
13385 -- type is tagged limited when validating derivations in the private
13386 -- part. (See AI05-096).
13388 if Nkind (Def) = N_Formal_Derived_Type_Definition then
13389 if Present (Interface_List (Def)) then
13390 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
13391 else
13392 Set_Generic_Parent_Type (Decl_Node, Ancestor);
13393 end if;
13395 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
13396 N_Formal_Incomplete_Type_Definition)
13397 then
13398 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
13399 end if;
13401 -- If the actual is a synchronized type that implements an interface,
13402 -- the primitive operations are attached to the corresponding record,
13403 -- and we have to treat it as an additional generic actual, so that its
13404 -- primitive operations become visible in the instance. The task or
13405 -- protected type itself does not carry primitive operations.
13407 if Is_Concurrent_Type (Act_T)
13408 and then Is_Tagged_Type (Act_T)
13409 and then Present (Corresponding_Record_Type (Act_T))
13410 and then Present (Ancestor)
13411 and then Is_Interface (Ancestor)
13412 then
13413 declare
13414 Corr_Rec : constant Entity_Id :=
13415 Corresponding_Record_Type (Act_T);
13416 New_Corr : Entity_Id;
13417 Corr_Decl : Node_Id;
13419 begin
13420 New_Corr := Make_Temporary (Loc, 'S');
13421 Corr_Decl :=
13422 Make_Subtype_Declaration (Loc,
13423 Defining_Identifier => New_Corr,
13424 Subtype_Indication =>
13425 New_Occurrence_Of (Corr_Rec, Loc));
13426 Append_To (Decl_Nodes, Corr_Decl);
13428 if Ekind (Act_T) = E_Task_Type then
13429 Set_Ekind (Subt, E_Task_Subtype);
13430 else
13431 Set_Ekind (Subt, E_Protected_Subtype);
13432 end if;
13434 Set_Corresponding_Record_Type (Subt, Corr_Rec);
13435 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
13436 Set_Generic_Parent_Type (Decl_Node, Empty);
13437 end;
13438 end if;
13440 -- For a floating-point type, capture dimension info if any, because
13441 -- the generated subtype declaration does not come from source and
13442 -- will not process dimensions.
13444 if Is_Floating_Point_Type (Act_T) then
13445 Copy_Dimensions (Act_T, Subt);
13446 end if;
13448 return Decl_Nodes;
13449 end Instantiate_Type;
13451 ---------------------
13452 -- Is_In_Main_Unit --
13453 ---------------------
13455 function Is_In_Main_Unit (N : Node_Id) return Boolean is
13456 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
13457 Current_Unit : Node_Id;
13459 begin
13460 if Unum = Main_Unit then
13461 return True;
13463 -- If the current unit is a subunit then it is either the main unit or
13464 -- is being compiled as part of the main unit.
13466 elsif Nkind (N) = N_Compilation_Unit then
13467 return Nkind (Unit (N)) = N_Subunit;
13468 end if;
13470 Current_Unit := Parent (N);
13471 while Present (Current_Unit)
13472 and then Nkind (Current_Unit) /= N_Compilation_Unit
13473 loop
13474 Current_Unit := Parent (Current_Unit);
13475 end loop;
13477 -- The instantiation node is in the main unit, or else the current node
13478 -- (perhaps as the result of nested instantiations) is in the main unit,
13479 -- or in the declaration of the main unit, which in this last case must
13480 -- be a body.
13482 return
13483 Current_Unit = Cunit (Main_Unit)
13484 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
13485 or else (Present (Current_Unit)
13486 and then Present (Library_Unit (Current_Unit))
13487 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
13488 end Is_In_Main_Unit;
13490 ----------------------------
13491 -- Load_Parent_Of_Generic --
13492 ----------------------------
13494 procedure Load_Parent_Of_Generic
13495 (N : Node_Id;
13496 Spec : Node_Id;
13497 Body_Optional : Boolean := False)
13499 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
13500 Saved_Style_Check : constant Boolean := Style_Check;
13501 Saved_Warnings : constant Warning_Record := Save_Warnings;
13502 True_Parent : Node_Id;
13503 Inst_Node : Node_Id;
13504 OK : Boolean;
13505 Previous_Instances : constant Elist_Id := New_Elmt_List;
13507 procedure Collect_Previous_Instances (Decls : List_Id);
13508 -- Collect all instantiations in the given list of declarations, that
13509 -- precede the generic that we need to load. If the bodies of these
13510 -- instantiations are available, we must analyze them, to ensure that
13511 -- the public symbols generated are the same when the unit is compiled
13512 -- to generate code, and when it is compiled in the context of a unit
13513 -- that needs a particular nested instance. This process is applied to
13514 -- both package and subprogram instances.
13516 --------------------------------
13517 -- Collect_Previous_Instances --
13518 --------------------------------
13520 procedure Collect_Previous_Instances (Decls : List_Id) is
13521 Decl : Node_Id;
13523 begin
13524 Decl := First (Decls);
13525 while Present (Decl) loop
13526 if Sloc (Decl) >= Sloc (Inst_Node) then
13527 return;
13529 -- If Decl is an instantiation, then record it as requiring
13530 -- instantiation of the corresponding body, except if it is an
13531 -- abbreviated instantiation generated internally for conformance
13532 -- checking purposes only for the case of a formal package
13533 -- declared without a box (see Instantiate_Formal_Package). Such
13534 -- an instantiation does not generate any code (the actual code
13535 -- comes from actual) and thus does not need to be analyzed here.
13536 -- If the instantiation appears with a generic package body it is
13537 -- not analyzed here either.
13539 elsif Nkind (Decl) = N_Package_Instantiation
13540 and then not Is_Internal (Defining_Entity (Decl))
13541 then
13542 Append_Elmt (Decl, Previous_Instances);
13544 -- For a subprogram instantiation, omit instantiations intrinsic
13545 -- operations (Unchecked_Conversions, etc.) that have no bodies.
13547 elsif Nkind_In (Decl, N_Function_Instantiation,
13548 N_Procedure_Instantiation)
13549 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
13550 then
13551 Append_Elmt (Decl, Previous_Instances);
13553 elsif Nkind (Decl) = N_Package_Declaration then
13554 Collect_Previous_Instances
13555 (Visible_Declarations (Specification (Decl)));
13556 Collect_Previous_Instances
13557 (Private_Declarations (Specification (Decl)));
13559 -- Previous non-generic bodies may contain instances as well
13561 elsif Nkind (Decl) = N_Package_Body
13562 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
13563 then
13564 Collect_Previous_Instances (Declarations (Decl));
13566 elsif Nkind (Decl) = N_Subprogram_Body
13567 and then not Acts_As_Spec (Decl)
13568 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
13569 then
13570 Collect_Previous_Instances (Declarations (Decl));
13571 end if;
13573 Next (Decl);
13574 end loop;
13575 end Collect_Previous_Instances;
13577 -- Start of processing for Load_Parent_Of_Generic
13579 begin
13580 if not In_Same_Source_Unit (N, Spec)
13581 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
13582 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
13583 and then not Is_In_Main_Unit (Spec))
13584 then
13585 -- Find body of parent of spec, and analyze it. A special case arises
13586 -- when the parent is an instantiation, that is to say when we are
13587 -- currently instantiating a nested generic. In that case, there is
13588 -- no separate file for the body of the enclosing instance. Instead,
13589 -- the enclosing body must be instantiated as if it were a pending
13590 -- instantiation, in order to produce the body for the nested generic
13591 -- we require now. Note that in that case the generic may be defined
13592 -- in a package body, the instance defined in the same package body,
13593 -- and the original enclosing body may not be in the main unit.
13595 Inst_Node := Empty;
13597 True_Parent := Parent (Spec);
13598 while Present (True_Parent)
13599 and then Nkind (True_Parent) /= N_Compilation_Unit
13600 loop
13601 if Nkind (True_Parent) = N_Package_Declaration
13602 and then
13603 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
13604 then
13605 -- Parent is a compilation unit that is an instantiation.
13606 -- Instantiation node has been replaced with package decl.
13608 Inst_Node := Original_Node (True_Parent);
13609 exit;
13611 elsif Nkind (True_Parent) = N_Package_Declaration
13612 and then Present (Generic_Parent (Specification (True_Parent)))
13613 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13614 then
13615 -- Parent is an instantiation within another specification.
13616 -- Declaration for instance has been inserted before original
13617 -- instantiation node. A direct link would be preferable?
13619 Inst_Node := Next (True_Parent);
13620 while Present (Inst_Node)
13621 and then Nkind (Inst_Node) /= N_Package_Instantiation
13622 loop
13623 Next (Inst_Node);
13624 end loop;
13626 -- If the instance appears within a generic, and the generic
13627 -- unit is defined within a formal package of the enclosing
13628 -- generic, there is no generic body available, and none
13629 -- needed. A more precise test should be used ???
13631 if No (Inst_Node) then
13632 return;
13633 end if;
13635 exit;
13637 else
13638 True_Parent := Parent (True_Parent);
13639 end if;
13640 end loop;
13642 -- Case where we are currently instantiating a nested generic
13644 if Present (Inst_Node) then
13645 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
13647 -- Instantiation node and declaration of instantiated package
13648 -- were exchanged when only the declaration was needed.
13649 -- Restore instantiation node before proceeding with body.
13651 Set_Unit (Parent (True_Parent), Inst_Node);
13652 end if;
13654 -- Now complete instantiation of enclosing body, if it appears in
13655 -- some other unit. If it appears in the current unit, the body
13656 -- will have been instantiated already.
13658 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
13660 -- We need to determine the expander mode to instantiate the
13661 -- enclosing body. Because the generic body we need may use
13662 -- global entities declared in the enclosing package (including
13663 -- aggregates) it is in general necessary to compile this body
13664 -- with expansion enabled, except if we are within a generic
13665 -- package, in which case the usual generic rule applies.
13667 declare
13668 Exp_Status : Boolean := True;
13669 Scop : Entity_Id;
13671 begin
13672 -- Loop through scopes looking for generic package
13674 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
13675 while Present (Scop)
13676 and then Scop /= Standard_Standard
13677 loop
13678 if Ekind (Scop) = E_Generic_Package then
13679 Exp_Status := False;
13680 exit;
13681 end if;
13683 Scop := Scope (Scop);
13684 end loop;
13686 -- Collect previous instantiations in the unit that contains
13687 -- the desired generic.
13689 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13690 and then not Body_Optional
13691 then
13692 declare
13693 Decl : Elmt_Id;
13694 Info : Pending_Body_Info;
13695 Par : Node_Id;
13697 begin
13698 Par := Parent (Inst_Node);
13699 while Present (Par) loop
13700 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
13701 Par := Parent (Par);
13702 end loop;
13704 pragma Assert (Present (Par));
13706 if Nkind (Par) = N_Package_Body then
13707 Collect_Previous_Instances (Declarations (Par));
13709 elsif Nkind (Par) = N_Package_Declaration then
13710 Collect_Previous_Instances
13711 (Visible_Declarations (Specification (Par)));
13712 Collect_Previous_Instances
13713 (Private_Declarations (Specification (Par)));
13715 else
13716 -- Enclosing unit is a subprogram body. In this
13717 -- case all instance bodies are processed in order
13718 -- and there is no need to collect them separately.
13720 null;
13721 end if;
13723 Decl := First_Elmt (Previous_Instances);
13724 while Present (Decl) loop
13725 Info :=
13726 (Inst_Node => Node (Decl),
13727 Act_Decl =>
13728 Instance_Spec (Node (Decl)),
13729 Expander_Status => Exp_Status,
13730 Current_Sem_Unit =>
13731 Get_Code_Unit (Sloc (Node (Decl))),
13732 Scope_Suppress => Scope_Suppress,
13733 Local_Suppress_Stack_Top =>
13734 Local_Suppress_Stack_Top,
13735 Version => Ada_Version,
13736 Version_Pragma => Ada_Version_Pragma,
13737 Warnings => Save_Warnings,
13738 SPARK_Mode => SPARK_Mode,
13739 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
13741 -- Package instance
13743 if Nkind (Node (Decl)) = N_Package_Instantiation
13744 then
13745 Instantiate_Package_Body
13746 (Info, Body_Optional => True);
13748 -- Subprogram instance
13750 else
13751 -- The instance_spec is in the wrapper package,
13752 -- usually followed by its local renaming
13753 -- declaration. See Build_Subprogram_Renaming
13754 -- for details. If the instance carries aspects,
13755 -- these result in the corresponding pragmas,
13756 -- inserted after the subprogram declaration.
13757 -- They must be skipped as well when retrieving
13758 -- the desired spec. Some of them may have been
13759 -- rewritten as null statements.
13760 -- A direct link would be more robust ???
13762 declare
13763 Decl : Node_Id :=
13764 (Last (Visible_Declarations
13765 (Specification (Info.Act_Decl))));
13766 begin
13767 while Nkind_In (Decl,
13768 N_Null_Statement,
13769 N_Pragma,
13770 N_Subprogram_Renaming_Declaration)
13771 loop
13772 Decl := Prev (Decl);
13773 end loop;
13775 Info.Act_Decl := Decl;
13776 end;
13778 Instantiate_Subprogram_Body
13779 (Info, Body_Optional => True);
13780 end if;
13782 Next_Elmt (Decl);
13783 end loop;
13784 end;
13785 end if;
13787 Instantiate_Package_Body
13788 (Body_Info =>
13789 ((Inst_Node => Inst_Node,
13790 Act_Decl => True_Parent,
13791 Expander_Status => Exp_Status,
13792 Current_Sem_Unit => Get_Code_Unit
13793 (Sloc (Inst_Node)),
13794 Scope_Suppress => Scope_Suppress,
13795 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
13796 Version => Ada_Version,
13797 Version_Pragma => Ada_Version_Pragma,
13798 Warnings => Save_Warnings,
13799 SPARK_Mode => SPARK_Mode,
13800 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
13801 Body_Optional => Body_Optional);
13802 end;
13803 end if;
13805 -- Case where we are not instantiating a nested generic
13807 else
13808 Opt.Style_Check := False;
13809 Expander_Mode_Save_And_Set (True);
13810 Load_Needed_Body (Comp_Unit, OK);
13811 Opt.Style_Check := Saved_Style_Check;
13812 Restore_Warnings (Saved_Warnings);
13813 Expander_Mode_Restore;
13815 if not OK
13816 and then Unit_Requires_Body (Defining_Entity (Spec))
13817 and then not Body_Optional
13818 then
13819 declare
13820 Bname : constant Unit_Name_Type :=
13821 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
13823 begin
13824 -- In CodePeer mode, the missing body may make the analysis
13825 -- incomplete, but we do not treat it as fatal.
13827 if CodePeer_Mode then
13828 return;
13830 else
13831 Error_Msg_Unit_1 := Bname;
13832 Error_Msg_N ("this instantiation requires$!", N);
13833 Error_Msg_File_1 :=
13834 Get_File_Name (Bname, Subunit => False);
13835 Error_Msg_N ("\but file{ was not found!", N);
13836 raise Unrecoverable_Error;
13837 end if;
13838 end;
13839 end if;
13840 end if;
13841 end if;
13843 -- If loading parent of the generic caused an instantiation circularity,
13844 -- we abandon compilation at this point, because otherwise in some cases
13845 -- we get into trouble with infinite recursions after this point.
13847 if Circularity_Detected then
13848 raise Unrecoverable_Error;
13849 end if;
13850 end Load_Parent_Of_Generic;
13852 ---------------------------------
13853 -- Map_Formal_Package_Entities --
13854 ---------------------------------
13856 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
13857 E1 : Entity_Id;
13858 E2 : Entity_Id;
13860 begin
13861 Set_Instance_Of (Form, Act);
13863 -- Traverse formal and actual package to map the corresponding entities.
13864 -- We skip over internal entities that may be generated during semantic
13865 -- analysis, and find the matching entities by name, given that they
13866 -- must appear in the same order.
13868 E1 := First_Entity (Form);
13869 E2 := First_Entity (Act);
13870 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
13871 -- Could this test be a single condition??? Seems like it could, and
13872 -- isn't FPE (Form) a constant anyway???
13874 if not Is_Internal (E1)
13875 and then Present (Parent (E1))
13876 and then not Is_Class_Wide_Type (E1)
13877 and then not Is_Internal_Name (Chars (E1))
13878 then
13879 while Present (E2) and then Chars (E2) /= Chars (E1) loop
13880 Next_Entity (E2);
13881 end loop;
13883 if No (E2) then
13884 exit;
13885 else
13886 Set_Instance_Of (E1, E2);
13888 if Is_Type (E1) and then Is_Tagged_Type (E2) then
13889 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
13890 end if;
13892 if Is_Constrained (E1) then
13893 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
13894 end if;
13896 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
13897 Map_Formal_Package_Entities (E1, E2);
13898 end if;
13899 end if;
13900 end if;
13902 Next_Entity (E1);
13903 end loop;
13904 end Map_Formal_Package_Entities;
13906 -----------------------
13907 -- Move_Freeze_Nodes --
13908 -----------------------
13910 procedure Move_Freeze_Nodes
13911 (Out_Of : Entity_Id;
13912 After : Node_Id;
13913 L : List_Id)
13915 Decl : Node_Id;
13916 Next_Decl : Node_Id;
13917 Next_Node : Node_Id := After;
13918 Spec : Node_Id;
13920 function Is_Outer_Type (T : Entity_Id) return Boolean;
13921 -- Check whether entity is declared in a scope external to that of the
13922 -- generic unit.
13924 -------------------
13925 -- Is_Outer_Type --
13926 -------------------
13928 function Is_Outer_Type (T : Entity_Id) return Boolean is
13929 Scop : Entity_Id := Scope (T);
13931 begin
13932 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
13933 return True;
13935 else
13936 while Scop /= Standard_Standard loop
13937 if Scop = Out_Of then
13938 return False;
13939 else
13940 Scop := Scope (Scop);
13941 end if;
13942 end loop;
13944 return True;
13945 end if;
13946 end Is_Outer_Type;
13948 -- Start of processing for Move_Freeze_Nodes
13950 begin
13951 if No (L) then
13952 return;
13953 end if;
13955 -- First remove the freeze nodes that may appear before all other
13956 -- declarations.
13958 Decl := First (L);
13959 while Present (Decl)
13960 and then Nkind (Decl) = N_Freeze_Entity
13961 and then Is_Outer_Type (Entity (Decl))
13962 loop
13963 Decl := Remove_Head (L);
13964 Insert_After (Next_Node, Decl);
13965 Set_Analyzed (Decl, False);
13966 Next_Node := Decl;
13967 Decl := First (L);
13968 end loop;
13970 -- Next scan the list of declarations and remove each freeze node that
13971 -- appears ahead of the current node.
13973 while Present (Decl) loop
13974 while Present (Next (Decl))
13975 and then Nkind (Next (Decl)) = N_Freeze_Entity
13976 and then Is_Outer_Type (Entity (Next (Decl)))
13977 loop
13978 Next_Decl := Remove_Next (Decl);
13979 Insert_After (Next_Node, Next_Decl);
13980 Set_Analyzed (Next_Decl, False);
13981 Next_Node := Next_Decl;
13982 end loop;
13984 -- If the declaration is a nested package or concurrent type, then
13985 -- recurse. Nested generic packages will have been processed from the
13986 -- inside out.
13988 case Nkind (Decl) is
13989 when N_Package_Declaration =>
13990 Spec := Specification (Decl);
13992 when N_Task_Type_Declaration =>
13993 Spec := Task_Definition (Decl);
13995 when N_Protected_Type_Declaration =>
13996 Spec := Protected_Definition (Decl);
13998 when others =>
13999 Spec := Empty;
14000 end case;
14002 if Present (Spec) then
14003 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
14004 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
14005 end if;
14007 Next (Decl);
14008 end loop;
14009 end Move_Freeze_Nodes;
14011 ----------------
14012 -- Next_Assoc --
14013 ----------------
14015 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
14016 begin
14017 return Generic_Renamings.Table (E).Next_In_HTable;
14018 end Next_Assoc;
14020 ------------------------
14021 -- Preanalyze_Actuals --
14022 ------------------------
14024 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
14025 Assoc : Node_Id;
14026 Act : Node_Id;
14027 Errs : constant Nat := Serious_Errors_Detected;
14029 Cur : Entity_Id := Empty;
14030 -- Current homograph of the instance name
14032 Vis : Boolean := False;
14033 -- Saved visibility status of the current homograph
14035 begin
14036 Assoc := First (Generic_Associations (N));
14038 -- If the instance is a child unit, its name may hide an outer homonym,
14039 -- so make it invisible to perform name resolution on the actuals.
14041 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
14042 and then Present
14043 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
14044 then
14045 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
14047 if Is_Compilation_Unit (Cur) then
14048 Vis := Is_Immediately_Visible (Cur);
14049 Set_Is_Immediately_Visible (Cur, False);
14050 else
14051 Cur := Empty;
14052 end if;
14053 end if;
14055 while Present (Assoc) loop
14056 if Nkind (Assoc) /= N_Others_Choice then
14057 Act := Explicit_Generic_Actual_Parameter (Assoc);
14059 -- Within a nested instantiation, a defaulted actual is an empty
14060 -- association, so nothing to analyze. If the subprogram actual
14061 -- is an attribute, analyze prefix only, because actual is not a
14062 -- complete attribute reference.
14064 -- If actual is an allocator, analyze expression only. The full
14065 -- analysis can generate code, and if instance is a compilation
14066 -- unit we have to wait until the package instance is installed
14067 -- to have a proper place to insert this code.
14069 -- String literals may be operators, but at this point we do not
14070 -- know whether the actual is a formal subprogram or a string.
14072 if No (Act) then
14073 null;
14075 elsif Nkind (Act) = N_Attribute_Reference then
14076 Analyze (Prefix (Act));
14078 elsif Nkind (Act) = N_Explicit_Dereference then
14079 Analyze (Prefix (Act));
14081 elsif Nkind (Act) = N_Allocator then
14082 declare
14083 Expr : constant Node_Id := Expression (Act);
14085 begin
14086 if Nkind (Expr) = N_Subtype_Indication then
14087 Analyze (Subtype_Mark (Expr));
14089 -- Analyze separately each discriminant constraint, when
14090 -- given with a named association.
14092 declare
14093 Constr : Node_Id;
14095 begin
14096 Constr := First (Constraints (Constraint (Expr)));
14097 while Present (Constr) loop
14098 if Nkind (Constr) = N_Discriminant_Association then
14099 Analyze (Expression (Constr));
14100 else
14101 Analyze (Constr);
14102 end if;
14104 Next (Constr);
14105 end loop;
14106 end;
14108 else
14109 Analyze (Expr);
14110 end if;
14111 end;
14113 elsif Nkind (Act) /= N_Operator_Symbol then
14114 Analyze (Act);
14116 -- Within a package instance, mark actuals that are limited
14117 -- views, so their use can be moved to the body of the
14118 -- enclosing unit.
14120 if Is_Entity_Name (Act)
14121 and then Is_Type (Entity (Act))
14122 and then From_Limited_With (Entity (Act))
14123 and then Present (Inst)
14124 then
14125 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
14126 end if;
14127 end if;
14129 if Errs /= Serious_Errors_Detected then
14131 -- Do a minimal analysis of the generic, to prevent spurious
14132 -- warnings complaining about the generic being unreferenced,
14133 -- before abandoning the instantiation.
14135 Analyze (Name (N));
14137 if Is_Entity_Name (Name (N))
14138 and then Etype (Name (N)) /= Any_Type
14139 then
14140 Generate_Reference (Entity (Name (N)), Name (N));
14141 Set_Is_Instantiated (Entity (Name (N)));
14142 end if;
14144 if Present (Cur) then
14146 -- For the case of a child instance hiding an outer homonym,
14147 -- provide additional warning which might explain the error.
14149 Set_Is_Immediately_Visible (Cur, Vis);
14150 Error_Msg_NE
14151 ("& hides outer unit with the same name??",
14152 N, Defining_Unit_Name (N));
14153 end if;
14155 Abandon_Instantiation (Act);
14156 end if;
14157 end if;
14159 Next (Assoc);
14160 end loop;
14162 if Present (Cur) then
14163 Set_Is_Immediately_Visible (Cur, Vis);
14164 end if;
14165 end Preanalyze_Actuals;
14167 -------------------------------
14168 -- Provide_Completing_Bodies --
14169 -------------------------------
14171 procedure Provide_Completing_Bodies (N : Node_Id) is
14172 procedure Build_Completing_Body (Subp_Decl : Node_Id);
14173 -- Generate the completing body for subprogram declaration Subp_Decl
14175 procedure Provide_Completing_Bodies_In (Decls : List_Id);
14176 -- Generating completing bodies for all subprograms found in declarative
14177 -- list Decls.
14179 ---------------------------
14180 -- Build_Completing_Body --
14181 ---------------------------
14183 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
14184 Loc : constant Source_Ptr := Sloc (Subp_Decl);
14185 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
14186 Spec : Node_Id;
14188 begin
14189 -- Nothing to do if the subprogram already has a completing body
14191 if Present (Corresponding_Body (Subp_Decl)) then
14192 return;
14194 -- Mark the function as having a valid return statement even though
14195 -- the body contains a single raise statement.
14197 elsif Ekind (Subp_Id) = E_Function then
14198 Set_Return_Present (Subp_Id);
14199 end if;
14201 -- Clone the specification to obtain new entities and reset the only
14202 -- semantic field.
14204 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
14205 Set_Generic_Parent (Spec, Empty);
14207 -- Generate:
14208 -- function Func ... return ... is
14209 -- <or>
14210 -- procedure Proc ... is
14211 -- begin
14212 -- raise Program_Error with "access before elaboration";
14213 -- edn Proc;
14215 Insert_After_And_Analyze (Subp_Decl,
14216 Make_Subprogram_Body (Loc,
14217 Specification => Spec,
14218 Declarations => New_List,
14219 Handled_Statement_Sequence =>
14220 Make_Handled_Sequence_Of_Statements (Loc,
14221 Statements => New_List (
14222 Make_Raise_Program_Error (Loc,
14223 Reason => PE_Access_Before_Elaboration)))));
14224 end Build_Completing_Body;
14226 ----------------------------------
14227 -- Provide_Completing_Bodies_In --
14228 ----------------------------------
14230 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
14231 Decl : Node_Id;
14233 begin
14234 if Present (Decls) then
14235 Decl := First (Decls);
14236 while Present (Decl) loop
14237 Provide_Completing_Bodies (Decl);
14238 Next (Decl);
14239 end loop;
14240 end if;
14241 end Provide_Completing_Bodies_In;
14243 -- Local variables
14245 Spec : Node_Id;
14247 -- Start of processing for Provide_Completing_Bodies
14249 begin
14250 if Nkind (N) = N_Package_Declaration then
14251 Spec := Specification (N);
14253 Push_Scope (Defining_Entity (N));
14254 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
14255 Provide_Completing_Bodies_In (Private_Declarations (Spec));
14256 Pop_Scope;
14258 elsif Nkind (N) = N_Subprogram_Declaration then
14259 Build_Completing_Body (N);
14260 end if;
14261 end Provide_Completing_Bodies;
14263 -------------------
14264 -- Remove_Parent --
14265 -------------------
14267 procedure Remove_Parent (In_Body : Boolean := False) is
14268 S : Entity_Id := Current_Scope;
14269 -- S is the scope containing the instantiation just completed. The scope
14270 -- stack contains the parent instances of the instantiation, followed by
14271 -- the original S.
14273 Cur_P : Entity_Id;
14274 E : Entity_Id;
14275 P : Entity_Id;
14276 Hidden : Elmt_Id;
14278 begin
14279 -- After child instantiation is complete, remove from scope stack the
14280 -- extra copy of the current scope, and then remove parent instances.
14282 if not In_Body then
14283 Pop_Scope;
14285 while Current_Scope /= S loop
14286 P := Current_Scope;
14287 End_Package_Scope (Current_Scope);
14289 if In_Open_Scopes (P) then
14290 E := First_Entity (P);
14291 while Present (E) loop
14292 Set_Is_Immediately_Visible (E, True);
14293 Next_Entity (E);
14294 end loop;
14296 -- If instantiation is declared in a block, it is the enclosing
14297 -- scope that might be a parent instance. Note that only one
14298 -- block can be involved, because the parent instances have
14299 -- been installed within it.
14301 if Ekind (P) = E_Block then
14302 Cur_P := Scope (P);
14303 else
14304 Cur_P := P;
14305 end if;
14307 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
14308 -- We are within an instance of some sibling. Retain
14309 -- visibility of parent, for proper subsequent cleanup, and
14310 -- reinstall private declarations as well.
14312 Set_In_Private_Part (P);
14313 Install_Private_Declarations (P);
14314 end if;
14316 -- If the ultimate parent is a top-level unit recorded in
14317 -- Instance_Parent_Unit, then reset its visibility to what it was
14318 -- before instantiation. (It's not clear what the purpose is of
14319 -- testing whether Scope (P) is In_Open_Scopes, but that test was
14320 -- present before the ultimate parent test was added.???)
14322 elsif not In_Open_Scopes (Scope (P))
14323 or else (P = Instance_Parent_Unit
14324 and then not Parent_Unit_Visible)
14325 then
14326 Set_Is_Immediately_Visible (P, False);
14328 -- If the current scope is itself an instantiation of a generic
14329 -- nested within P, and we are in the private part of body of this
14330 -- instantiation, restore the full views of P, that were removed
14331 -- in End_Package_Scope above. This obscure case can occur when a
14332 -- subunit of a generic contains an instance of a child unit of
14333 -- its generic parent unit.
14335 elsif S = Current_Scope and then Is_Generic_Instance (S) then
14336 declare
14337 Par : constant Entity_Id :=
14338 Generic_Parent (Package_Specification (S));
14339 begin
14340 if Present (Par)
14341 and then P = Scope (Par)
14342 and then (In_Package_Body (S) or else In_Private_Part (S))
14343 then
14344 Set_In_Private_Part (P);
14345 Install_Private_Declarations (P);
14346 end if;
14347 end;
14348 end if;
14349 end loop;
14351 -- Reset visibility of entities in the enclosing scope
14353 Set_Is_Hidden_Open_Scope (Current_Scope, False);
14355 Hidden := First_Elmt (Hidden_Entities);
14356 while Present (Hidden) loop
14357 Set_Is_Immediately_Visible (Node (Hidden), True);
14358 Next_Elmt (Hidden);
14359 end loop;
14361 else
14362 -- Each body is analyzed separately, and there is no context that
14363 -- needs preserving from one body instance to the next, so remove all
14364 -- parent scopes that have been installed.
14366 while Present (S) loop
14367 End_Package_Scope (S);
14368 Set_Is_Immediately_Visible (S, False);
14369 S := Current_Scope;
14370 exit when S = Standard_Standard;
14371 end loop;
14372 end if;
14373 end Remove_Parent;
14375 -----------------
14376 -- Restore_Env --
14377 -----------------
14379 procedure Restore_Env is
14380 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
14382 begin
14383 if No (Current_Instantiated_Parent.Act_Id) then
14384 -- Restore environment after subprogram inlining
14386 Restore_Private_Views (Empty);
14387 end if;
14389 Current_Instantiated_Parent := Saved.Instantiated_Parent;
14390 Exchanged_Views := Saved.Exchanged_Views;
14391 Hidden_Entities := Saved.Hidden_Entities;
14392 Current_Sem_Unit := Saved.Current_Sem_Unit;
14393 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
14394 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
14396 Restore_Opt_Config_Switches (Saved.Switches);
14398 Instance_Envs.Decrement_Last;
14399 end Restore_Env;
14401 ---------------------------
14402 -- Restore_Private_Views --
14403 ---------------------------
14405 procedure Restore_Private_Views
14406 (Pack_Id : Entity_Id;
14407 Is_Package : Boolean := True)
14409 M : Elmt_Id;
14410 E : Entity_Id;
14411 Typ : Entity_Id;
14412 Dep_Elmt : Elmt_Id;
14413 Dep_Typ : Node_Id;
14415 procedure Restore_Nested_Formal (Formal : Entity_Id);
14416 -- Hide the generic formals of formal packages declared with box which
14417 -- were reachable in the current instantiation.
14419 ---------------------------
14420 -- Restore_Nested_Formal --
14421 ---------------------------
14423 procedure Restore_Nested_Formal (Formal : Entity_Id) is
14424 Ent : Entity_Id;
14426 begin
14427 if Present (Renamed_Object (Formal))
14428 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
14429 then
14430 return;
14432 elsif Present (Associated_Formal_Package (Formal)) then
14433 Ent := First_Entity (Formal);
14434 while Present (Ent) loop
14435 exit when Ekind (Ent) = E_Package
14436 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
14438 Set_Is_Hidden (Ent);
14439 Set_Is_Potentially_Use_Visible (Ent, False);
14441 -- If package, then recurse
14443 if Ekind (Ent) = E_Package then
14444 Restore_Nested_Formal (Ent);
14445 end if;
14447 Next_Entity (Ent);
14448 end loop;
14449 end if;
14450 end Restore_Nested_Formal;
14452 -- Start of processing for Restore_Private_Views
14454 begin
14455 M := First_Elmt (Exchanged_Views);
14456 while Present (M) loop
14457 Typ := Node (M);
14459 -- Subtypes of types whose views have been exchanged, and that are
14460 -- defined within the instance, were not on the Private_Dependents
14461 -- list on entry to the instance, so they have to be exchanged
14462 -- explicitly now, in order to remain consistent with the view of the
14463 -- parent type.
14465 if Ekind_In (Typ, E_Private_Type,
14466 E_Limited_Private_Type,
14467 E_Record_Type_With_Private)
14468 then
14469 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
14470 while Present (Dep_Elmt) loop
14471 Dep_Typ := Node (Dep_Elmt);
14473 if Scope (Dep_Typ) = Pack_Id
14474 and then Present (Full_View (Dep_Typ))
14475 then
14476 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
14477 Exchange_Declarations (Dep_Typ);
14478 end if;
14480 Next_Elmt (Dep_Elmt);
14481 end loop;
14482 end if;
14484 Exchange_Declarations (Node (M));
14485 Next_Elmt (M);
14486 end loop;
14488 if No (Pack_Id) then
14489 return;
14490 end if;
14492 -- Make the generic formal parameters private, and make the formal types
14493 -- into subtypes of the actuals again.
14495 E := First_Entity (Pack_Id);
14496 while Present (E) loop
14497 Set_Is_Hidden (E, True);
14499 if Is_Type (E)
14500 and then Nkind (Parent (E)) = N_Subtype_Declaration
14501 then
14502 -- If the actual for E is itself a generic actual type from
14503 -- an enclosing instance, E is still a generic actual type
14504 -- outside of the current instance. This matter when resolving
14505 -- an overloaded call that may be ambiguous in the enclosing
14506 -- instance, when two of its actuals coincide.
14508 if Is_Entity_Name (Subtype_Indication (Parent (E)))
14509 and then Is_Generic_Actual_Type
14510 (Entity (Subtype_Indication (Parent (E))))
14511 then
14512 null;
14513 else
14514 Set_Is_Generic_Actual_Type (E, False);
14515 end if;
14517 -- An unusual case of aliasing: the actual may also be directly
14518 -- visible in the generic, and be private there, while it is fully
14519 -- visible in the context of the instance. The internal subtype
14520 -- is private in the instance but has full visibility like its
14521 -- parent in the enclosing scope. This enforces the invariant that
14522 -- the privacy status of all private dependents of a type coincide
14523 -- with that of the parent type. This can only happen when a
14524 -- generic child unit is instantiated within a sibling.
14526 if Is_Private_Type (E)
14527 and then not Is_Private_Type (Etype (E))
14528 then
14529 Exchange_Declarations (E);
14530 end if;
14532 elsif Ekind (E) = E_Package then
14534 -- The end of the renaming list is the renaming of the generic
14535 -- package itself. If the instance is a subprogram, all entities
14536 -- in the corresponding package are renamings. If this entity is
14537 -- a formal package, make its own formals private as well. The
14538 -- actual in this case is itself the renaming of an instantiation.
14539 -- If the entity is not a package renaming, it is the entity
14540 -- created to validate formal package actuals: ignore it.
14542 -- If the actual is itself a formal package for the enclosing
14543 -- generic, or the actual for such a formal package, it remains
14544 -- visible on exit from the instance, and therefore nothing needs
14545 -- to be done either, except to keep it accessible.
14547 if Is_Package and then Renamed_Object (E) = Pack_Id then
14548 exit;
14550 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
14551 null;
14553 elsif
14554 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
14555 then
14556 Set_Is_Hidden (E, False);
14558 else
14559 declare
14560 Act_P : constant Entity_Id := Renamed_Object (E);
14561 Id : Entity_Id;
14563 begin
14564 Id := First_Entity (Act_P);
14565 while Present (Id)
14566 and then Id /= First_Private_Entity (Act_P)
14567 loop
14568 exit when Ekind (Id) = E_Package
14569 and then Renamed_Object (Id) = Act_P;
14571 Set_Is_Hidden (Id, True);
14572 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
14574 if Ekind (Id) = E_Package then
14575 Restore_Nested_Formal (Id);
14576 end if;
14578 Next_Entity (Id);
14579 end loop;
14580 end;
14581 end if;
14582 end if;
14584 Next_Entity (E);
14585 end loop;
14586 end Restore_Private_Views;
14588 --------------
14589 -- Save_Env --
14590 --------------
14592 procedure Save_Env
14593 (Gen_Unit : Entity_Id;
14594 Act_Unit : Entity_Id)
14596 begin
14597 Init_Env;
14598 Set_Instance_Env (Gen_Unit, Act_Unit);
14599 end Save_Env;
14601 ----------------------------
14602 -- Save_Global_References --
14603 ----------------------------
14605 procedure Save_Global_References (Templ : Node_Id) is
14607 -- ??? it is horrible to use global variables in highly recursive code
14609 E : Entity_Id;
14610 -- The entity of the current associated node
14612 Gen_Scope : Entity_Id;
14613 -- The scope of the generic for which references are being saved
14615 N2 : Node_Id;
14616 -- The current associated node
14618 function Is_Global (E : Entity_Id) return Boolean;
14619 -- Check whether entity is defined outside of generic unit. Examine the
14620 -- scope of an entity, and the scope of the scope, etc, until we find
14621 -- either Standard, in which case the entity is global, or the generic
14622 -- unit itself, which indicates that the entity is local. If the entity
14623 -- is the generic unit itself, as in the case of a recursive call, or
14624 -- the enclosing generic unit, if different from the current scope, then
14625 -- it is local as well, because it will be replaced at the point of
14626 -- instantiation. On the other hand, if it is a reference to a child
14627 -- unit of a common ancestor, which appears in an instantiation, it is
14628 -- global because it is used to denote a specific compilation unit at
14629 -- the time the instantiations will be analyzed.
14631 procedure Qualify_Universal_Operands
14632 (Op : Node_Id;
14633 Func_Call : Node_Id);
14634 -- Op denotes a binary or unary operator in generic template Templ. Node
14635 -- Func_Call is the function call alternative of the operator within the
14636 -- the analyzed copy of the template. Change each operand which yields a
14637 -- universal type by wrapping it into a qualified expression
14639 -- Actual_Typ'(Operand)
14641 -- where Actual_Typ is the type of corresponding actual parameter of
14642 -- Operand in Func_Call.
14644 procedure Reset_Entity (N : Node_Id);
14645 -- Save semantic information on global entity so that it is not resolved
14646 -- again at instantiation time.
14648 procedure Save_Entity_Descendants (N : Node_Id);
14649 -- Apply Save_Global_References to the two syntactic descendants of
14650 -- non-terminal nodes that carry an Associated_Node and are processed
14651 -- through Reset_Entity. Once the global entity (if any) has been
14652 -- captured together with its type, only two syntactic descendants need
14653 -- to be traversed to complete the processing of the tree rooted at N.
14654 -- This applies to Selected_Components, Expanded_Names, and to Operator
14655 -- nodes. N can also be a character literal, identifier, or operator
14656 -- symbol node, but the call has no effect in these cases.
14658 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
14659 -- Default actuals in nested instances must be handled specially
14660 -- because there is no link to them from the original tree. When an
14661 -- actual subprogram is given by a default, we add an explicit generic
14662 -- association for it in the instantiation node. When we save the
14663 -- global references on the name of the instance, we recover the list
14664 -- of generic associations, and add an explicit one to the original
14665 -- generic tree, through which a global actual can be preserved.
14666 -- Similarly, if a child unit is instantiated within a sibling, in the
14667 -- context of the parent, we must preserve the identifier of the parent
14668 -- so that it can be properly resolved in a subsequent instantiation.
14670 procedure Save_Global_Descendant (D : Union_Id);
14671 -- Apply Save_References recursively to the descendants of node D
14673 procedure Save_References (N : Node_Id);
14674 -- This is the recursive procedure that does the work, once the
14675 -- enclosing generic scope has been established.
14677 ---------------
14678 -- Is_Global --
14679 ---------------
14681 function Is_Global (E : Entity_Id) return Boolean is
14682 Se : Entity_Id;
14684 function Is_Instance_Node (Decl : Node_Id) return Boolean;
14685 -- Determine whether the parent node of a reference to a child unit
14686 -- denotes an instantiation or a formal package, in which case the
14687 -- reference to the child unit is global, even if it appears within
14688 -- the current scope (e.g. when the instance appears within the body
14689 -- of an ancestor).
14691 ----------------------
14692 -- Is_Instance_Node --
14693 ----------------------
14695 function Is_Instance_Node (Decl : Node_Id) return Boolean is
14696 begin
14697 return Nkind (Decl) in N_Generic_Instantiation
14698 or else
14699 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
14700 end Is_Instance_Node;
14702 -- Start of processing for Is_Global
14704 begin
14705 if E = Gen_Scope then
14706 return False;
14708 elsif E = Standard_Standard then
14709 return True;
14711 elsif Is_Child_Unit (E)
14712 and then (Is_Instance_Node (Parent (N2))
14713 or else (Nkind (Parent (N2)) = N_Expanded_Name
14714 and then N2 = Selector_Name (Parent (N2))
14715 and then
14716 Is_Instance_Node (Parent (Parent (N2)))))
14717 then
14718 return True;
14720 else
14721 Se := Scope (E);
14722 while Se /= Gen_Scope loop
14723 if Se = Standard_Standard then
14724 return True;
14725 else
14726 Se := Scope (Se);
14727 end if;
14728 end loop;
14730 return False;
14731 end if;
14732 end Is_Global;
14734 --------------------------------
14735 -- Qualify_Universal_Operands --
14736 --------------------------------
14738 procedure Qualify_Universal_Operands
14739 (Op : Node_Id;
14740 Func_Call : Node_Id)
14742 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
14743 -- Rewrite operand Opnd as a qualified expression of the form
14745 -- Actual_Typ'(Opnd)
14747 -- where Actual is the corresponding actual parameter of Opnd in
14748 -- function call Func_Call.
14750 function Qualify_Type
14751 (Loc : Source_Ptr;
14752 Typ : Entity_Id) return Node_Id;
14753 -- Qualify type Typ by creating a selected component of the form
14755 -- Scope_Of_Typ.Typ
14757 ---------------------
14758 -- Qualify_Operand --
14759 ---------------------
14761 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
14762 Loc : constant Source_Ptr := Sloc (Opnd);
14763 Typ : constant Entity_Id := Etype (Actual);
14764 Mark : Node_Id;
14765 Qual : Node_Id;
14767 begin
14768 -- Qualify the operand when it is of a universal type. Note that
14769 -- the template is unanalyzed and it is not possible to directly
14770 -- query the type. This transformation is not done when the type
14771 -- of the actual is internally generated because the type will be
14772 -- regenerated in the instance.
14774 if Yields_Universal_Type (Opnd)
14775 and then Comes_From_Source (Typ)
14776 and then not Is_Hidden (Typ)
14777 then
14778 -- The type of the actual may be a global reference. Save this
14779 -- information by creating a reference to it.
14781 if Is_Global (Typ) then
14782 Mark := New_Occurrence_Of (Typ, Loc);
14784 -- Otherwise rely on resolution to find the proper type within
14785 -- the instance.
14787 else
14788 Mark := Qualify_Type (Loc, Typ);
14789 end if;
14791 Qual :=
14792 Make_Qualified_Expression (Loc,
14793 Subtype_Mark => Mark,
14794 Expression => Relocate_Node (Opnd));
14796 -- Mark the qualification to distinguish it from other source
14797 -- constructs and signal the instantiation mechanism that this
14798 -- node requires special processing. See Copy_Generic_Node for
14799 -- details.
14801 Set_Is_Qualified_Universal_Literal (Qual);
14803 Rewrite (Opnd, Qual);
14804 end if;
14805 end Qualify_Operand;
14807 ------------------
14808 -- Qualify_Type --
14809 ------------------
14811 function Qualify_Type
14812 (Loc : Source_Ptr;
14813 Typ : Entity_Id) return Node_Id
14815 Scop : constant Entity_Id := Scope (Typ);
14816 Result : Node_Id;
14818 begin
14819 Result := Make_Identifier (Loc, Chars (Typ));
14821 if Present (Scop) and then not Is_Generic_Unit (Scop) then
14822 Result :=
14823 Make_Selected_Component (Loc,
14824 Prefix => Make_Identifier (Loc, Chars (Scop)),
14825 Selector_Name => Result);
14826 end if;
14828 return Result;
14829 end Qualify_Type;
14831 -- Local variables
14833 Actuals : constant List_Id := Parameter_Associations (Func_Call);
14835 -- Start of processing for Qualify_Universal_Operands
14837 begin
14838 if Nkind (Op) in N_Binary_Op then
14839 Qualify_Operand (Left_Opnd (Op), First (Actuals));
14840 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
14842 elsif Nkind (Op) in N_Unary_Op then
14843 Qualify_Operand (Right_Opnd (Op), First (Actuals));
14844 end if;
14845 end Qualify_Universal_Operands;
14847 ------------------
14848 -- Reset_Entity --
14849 ------------------
14851 procedure Reset_Entity (N : Node_Id) is
14852 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
14853 -- If the type of N2 is global to the generic unit, save the type in
14854 -- the generic node. Just as we perform name capture for explicit
14855 -- references within the generic, we must capture the global types
14856 -- of local entities because they may participate in resolution in
14857 -- the instance.
14859 function Top_Ancestor (E : Entity_Id) return Entity_Id;
14860 -- Find the ultimate ancestor of the current unit. If it is not a
14861 -- generic unit, then the name of the current unit in the prefix of
14862 -- an expanded name must be replaced with its generic homonym to
14863 -- ensure that it will be properly resolved in an instance.
14865 ---------------------
14866 -- Set_Global_Type --
14867 ---------------------
14869 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
14870 Typ : constant Entity_Id := Etype (N2);
14872 begin
14873 Set_Etype (N, Typ);
14875 -- If the entity of N is not the associated node, this is a
14876 -- nested generic and it has an associated node as well, whose
14877 -- type is already the full view (see below). Indicate that the
14878 -- original node has a private view.
14880 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
14881 Set_Has_Private_View (N);
14882 end if;
14884 -- If not a private type, nothing else to do
14886 if not Is_Private_Type (Typ) then
14887 if Is_Array_Type (Typ)
14888 and then Is_Private_Type (Component_Type (Typ))
14889 then
14890 Set_Has_Private_View (N);
14891 end if;
14893 -- If it is a derivation of a private type in a context where no
14894 -- full view is needed, nothing to do either.
14896 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
14897 null;
14899 -- Otherwise mark the type for flipping and use the full view when
14900 -- available.
14902 else
14903 Set_Has_Private_View (N);
14905 if Present (Full_View (Typ)) then
14906 Set_Etype (N2, Full_View (Typ));
14907 end if;
14908 end if;
14910 if Is_Floating_Point_Type (Typ)
14911 and then Has_Dimension_System (Typ)
14912 then
14913 Copy_Dimensions (N2, N);
14914 end if;
14915 end Set_Global_Type;
14917 ------------------
14918 -- Top_Ancestor --
14919 ------------------
14921 function Top_Ancestor (E : Entity_Id) return Entity_Id is
14922 Par : Entity_Id;
14924 begin
14925 Par := E;
14926 while Is_Child_Unit (Par) loop
14927 Par := Scope (Par);
14928 end loop;
14930 return Par;
14931 end Top_Ancestor;
14933 -- Start of processing for Reset_Entity
14935 begin
14936 N2 := Get_Associated_Node (N);
14937 E := Entity (N2);
14939 if Present (E) then
14941 -- If the node is an entry call to an entry in an enclosing task,
14942 -- it is rewritten as a selected component. No global entity to
14943 -- preserve in this case, since the expansion will be redone in
14944 -- the instance.
14946 if not Nkind_In (E, N_Defining_Character_Literal,
14947 N_Defining_Identifier,
14948 N_Defining_Operator_Symbol)
14949 then
14950 Set_Associated_Node (N, Empty);
14951 Set_Etype (N, Empty);
14952 return;
14953 end if;
14955 -- If the entity is an itype created as a subtype of an access
14956 -- type with a null exclusion restore source entity for proper
14957 -- visibility. The itype will be created anew in the instance.
14959 if Is_Itype (E)
14960 and then Ekind (E) = E_Access_Subtype
14961 and then Is_Entity_Name (N)
14962 and then Chars (Etype (E)) = Chars (N)
14963 then
14964 E := Etype (E);
14965 Set_Entity (N2, E);
14966 Set_Etype (N2, E);
14967 end if;
14969 if Is_Global (E) then
14971 -- If the entity is a package renaming that is the prefix of
14972 -- an expanded name, it has been rewritten as the renamed
14973 -- package, which is necessary semantically but complicates
14974 -- ASIS tree traversal, so we recover the original entity to
14975 -- expose the renaming. Take into account that the context may
14976 -- be a nested generic, that the original node may itself have
14977 -- an associated node that had better be an entity, and that
14978 -- the current node is still a selected component.
14980 if Ekind (E) = E_Package
14981 and then Nkind (N) = N_Selected_Component
14982 and then Nkind (Parent (N)) = N_Expanded_Name
14983 and then Present (Original_Node (N2))
14984 and then Is_Entity_Name (Original_Node (N2))
14985 and then Present (Entity (Original_Node (N2)))
14986 then
14987 if Is_Global (Entity (Original_Node (N2))) then
14988 N2 := Original_Node (N2);
14989 Set_Associated_Node (N, N2);
14990 Set_Global_Type (N, N2);
14992 -- Renaming is local, and will be resolved in instance
14994 else
14995 Set_Associated_Node (N, Empty);
14996 Set_Etype (N, Empty);
14997 end if;
14999 else
15000 Set_Global_Type (N, N2);
15001 end if;
15003 elsif Nkind (N) = N_Op_Concat
15004 and then Is_Generic_Type (Etype (N2))
15005 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
15006 or else
15007 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
15008 and then Is_Intrinsic_Subprogram (E)
15009 then
15010 null;
15012 -- Entity is local. Mark generic node as unresolved. Note that now
15013 -- it does not have an entity.
15015 else
15016 Set_Associated_Node (N, Empty);
15017 Set_Etype (N, Empty);
15018 end if;
15020 if Nkind (Parent (N)) in N_Generic_Instantiation
15021 and then N = Name (Parent (N))
15022 then
15023 Save_Global_Defaults (Parent (N), Parent (N2));
15024 end if;
15026 elsif Nkind (Parent (N)) = N_Selected_Component
15027 and then Nkind (Parent (N2)) = N_Expanded_Name
15028 then
15029 if Is_Global (Entity (Parent (N2))) then
15030 Change_Selected_Component_To_Expanded_Name (Parent (N));
15031 Set_Associated_Node (Parent (N), Parent (N2));
15032 Set_Global_Type (Parent (N), Parent (N2));
15033 Save_Entity_Descendants (N);
15035 -- If this is a reference to the current generic entity, replace
15036 -- by the name of the generic homonym of the current package. This
15037 -- is because in an instantiation Par.P.Q will not resolve to the
15038 -- name of the instance, whose enclosing scope is not necessarily
15039 -- Par. We use the generic homonym rather that the name of the
15040 -- generic itself because it may be hidden by a local declaration.
15042 elsif In_Open_Scopes (Entity (Parent (N2)))
15043 and then not
15044 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
15045 then
15046 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
15047 Rewrite (Parent (N),
15048 Make_Identifier (Sloc (N),
15049 Chars =>
15050 Chars (Generic_Homonym (Entity (Parent (N2))))));
15051 else
15052 Rewrite (Parent (N),
15053 Make_Identifier (Sloc (N),
15054 Chars => Chars (Selector_Name (Parent (N2)))));
15055 end if;
15056 end if;
15058 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
15059 and then Parent (N) = Name (Parent (Parent (N)))
15060 then
15061 Save_Global_Defaults
15062 (Parent (Parent (N)), Parent (Parent (N2)));
15063 end if;
15065 -- A selected component may denote a static constant that has been
15066 -- folded. If the static constant is global to the generic, capture
15067 -- its value. Otherwise the folding will happen in any instantiation.
15069 elsif Nkind (Parent (N)) = N_Selected_Component
15070 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
15071 then
15072 if Present (Entity (Original_Node (Parent (N2))))
15073 and then Is_Global (Entity (Original_Node (Parent (N2))))
15074 then
15075 Rewrite (Parent (N), New_Copy (Parent (N2)));
15076 Set_Analyzed (Parent (N), False);
15077 end if;
15079 -- A selected component may be transformed into a parameterless
15080 -- function call. If the called entity is global, rewrite the node
15081 -- appropriately, i.e. as an extended name for the global entity.
15083 elsif Nkind (Parent (N)) = N_Selected_Component
15084 and then Nkind (Parent (N2)) = N_Function_Call
15085 and then N = Selector_Name (Parent (N))
15086 then
15087 if No (Parameter_Associations (Parent (N2))) then
15088 if Is_Global (Entity (Name (Parent (N2)))) then
15089 Change_Selected_Component_To_Expanded_Name (Parent (N));
15090 Set_Associated_Node (Parent (N), Name (Parent (N2)));
15091 Set_Global_Type (Parent (N), Name (Parent (N2)));
15092 Save_Entity_Descendants (N);
15094 else
15095 Set_Is_Prefixed_Call (Parent (N));
15096 Set_Associated_Node (N, Empty);
15097 Set_Etype (N, Empty);
15098 end if;
15100 -- In Ada 2005, X.F may be a call to a primitive operation,
15101 -- rewritten as F (X). This rewriting will be done again in an
15102 -- instance, so keep the original node. Global entities will be
15103 -- captured as for other constructs. Indicate that this must
15104 -- resolve as a call, to prevent accidental overloading in the
15105 -- instance, if both a component and a primitive operation appear
15106 -- as candidates.
15108 else
15109 Set_Is_Prefixed_Call (Parent (N));
15110 end if;
15112 -- Entity is local. Reset in generic unit, so that node is resolved
15113 -- anew at the point of instantiation.
15115 else
15116 Set_Associated_Node (N, Empty);
15117 Set_Etype (N, Empty);
15118 end if;
15119 end Reset_Entity;
15121 -----------------------------
15122 -- Save_Entity_Descendants --
15123 -----------------------------
15125 procedure Save_Entity_Descendants (N : Node_Id) is
15126 begin
15127 case Nkind (N) is
15128 when N_Binary_Op =>
15129 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
15130 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
15132 when N_Unary_Op =>
15133 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
15135 when N_Expanded_Name
15136 | N_Selected_Component
15138 Save_Global_Descendant (Union_Id (Prefix (N)));
15139 Save_Global_Descendant (Union_Id (Selector_Name (N)));
15141 when N_Character_Literal
15142 | N_Identifier
15143 | N_Operator_Symbol
15145 null;
15147 when others =>
15148 raise Program_Error;
15149 end case;
15150 end Save_Entity_Descendants;
15152 --------------------------
15153 -- Save_Global_Defaults --
15154 --------------------------
15156 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
15157 Loc : constant Source_Ptr := Sloc (N1);
15158 Assoc2 : constant List_Id := Generic_Associations (N2);
15159 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
15160 Assoc1 : List_Id;
15161 Act1 : Node_Id;
15162 Act2 : Node_Id;
15163 Def : Node_Id;
15164 Ndec : Node_Id;
15165 Subp : Entity_Id;
15166 Actual : Entity_Id;
15168 begin
15169 Assoc1 := Generic_Associations (N1);
15171 if Present (Assoc1) then
15172 Act1 := First (Assoc1);
15173 else
15174 Act1 := Empty;
15175 Set_Generic_Associations (N1, New_List);
15176 Assoc1 := Generic_Associations (N1);
15177 end if;
15179 if Present (Assoc2) then
15180 Act2 := First (Assoc2);
15181 else
15182 return;
15183 end if;
15185 while Present (Act1) and then Present (Act2) loop
15186 Next (Act1);
15187 Next (Act2);
15188 end loop;
15190 -- Find the associations added for default subprograms
15192 if Present (Act2) then
15193 while Nkind (Act2) /= N_Generic_Association
15194 or else No (Entity (Selector_Name (Act2)))
15195 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
15196 loop
15197 Next (Act2);
15198 end loop;
15200 -- Add a similar association if the default is global. The
15201 -- renaming declaration for the actual has been analyzed, and
15202 -- its alias is the program it renames. Link the actual in the
15203 -- original generic tree with the node in the analyzed tree.
15205 while Present (Act2) loop
15206 Subp := Entity (Selector_Name (Act2));
15207 Def := Explicit_Generic_Actual_Parameter (Act2);
15209 -- Following test is defence against rubbish errors
15211 if No (Alias (Subp)) then
15212 return;
15213 end if;
15215 -- Retrieve the resolved actual from the renaming declaration
15216 -- created for the instantiated formal.
15218 Actual := Entity (Name (Parent (Parent (Subp))));
15219 Set_Entity (Def, Actual);
15220 Set_Etype (Def, Etype (Actual));
15222 if Is_Global (Actual) then
15223 Ndec :=
15224 Make_Generic_Association (Loc,
15225 Selector_Name =>
15226 New_Occurrence_Of (Subp, Loc),
15227 Explicit_Generic_Actual_Parameter =>
15228 New_Occurrence_Of (Actual, Loc));
15230 Set_Associated_Node
15231 (Explicit_Generic_Actual_Parameter (Ndec), Def);
15233 Append (Ndec, Assoc1);
15235 -- If there are other defaults, add a dummy association in case
15236 -- there are other defaulted formals with the same name.
15238 elsif Present (Next (Act2)) then
15239 Ndec :=
15240 Make_Generic_Association (Loc,
15241 Selector_Name =>
15242 New_Occurrence_Of (Subp, Loc),
15243 Explicit_Generic_Actual_Parameter => Empty);
15245 Append (Ndec, Assoc1);
15246 end if;
15248 Next (Act2);
15249 end loop;
15250 end if;
15252 if Nkind (Name (N1)) = N_Identifier
15253 and then Is_Child_Unit (Gen_Id)
15254 and then Is_Global (Gen_Id)
15255 and then Is_Generic_Unit (Scope (Gen_Id))
15256 and then In_Open_Scopes (Scope (Gen_Id))
15257 then
15258 -- This is an instantiation of a child unit within a sibling, so
15259 -- that the generic parent is in scope. An eventual instance must
15260 -- occur within the scope of an instance of the parent. Make name
15261 -- in instance into an expanded name, to preserve the identifier
15262 -- of the parent, so it can be resolved subsequently.
15264 Rewrite (Name (N2),
15265 Make_Expanded_Name (Loc,
15266 Chars => Chars (Gen_Id),
15267 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
15268 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
15269 Set_Entity (Name (N2), Gen_Id);
15271 Rewrite (Name (N1),
15272 Make_Expanded_Name (Loc,
15273 Chars => Chars (Gen_Id),
15274 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
15275 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
15277 Set_Associated_Node (Name (N1), Name (N2));
15278 Set_Associated_Node (Prefix (Name (N1)), Empty);
15279 Set_Associated_Node
15280 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
15281 Set_Etype (Name (N1), Etype (Gen_Id));
15282 end if;
15283 end Save_Global_Defaults;
15285 ----------------------------
15286 -- Save_Global_Descendant --
15287 ----------------------------
15289 procedure Save_Global_Descendant (D : Union_Id) is
15290 N1 : Node_Id;
15292 begin
15293 if D in Node_Range then
15294 if D = Union_Id (Empty) then
15295 null;
15297 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
15298 Save_References (Node_Id (D));
15299 end if;
15301 elsif D in List_Range then
15302 pragma Assert (D /= Union_Id (No_List));
15303 -- Because No_List = Empty, which is in Node_Range above
15305 if Is_Empty_List (List_Id (D)) then
15306 null;
15308 else
15309 N1 := First (List_Id (D));
15310 while Present (N1) loop
15311 Save_References (N1);
15312 Next (N1);
15313 end loop;
15314 end if;
15316 -- Element list or other non-node field, nothing to do
15318 else
15319 null;
15320 end if;
15321 end Save_Global_Descendant;
15323 ---------------------
15324 -- Save_References --
15325 ---------------------
15327 -- This is the recursive procedure that does the work once the enclosing
15328 -- generic scope has been established. We have to treat specially a
15329 -- number of node rewritings that are required by semantic processing
15330 -- and which change the kind of nodes in the generic copy: typically
15331 -- constant-folding, replacing an operator node by a string literal, or
15332 -- a selected component by an expanded name. In each of those cases, the
15333 -- transformation is propagated to the generic unit.
15335 procedure Save_References (N : Node_Id) is
15336 Loc : constant Source_Ptr := Sloc (N);
15338 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
15339 -- Determine whether arbitrary node Nod requires delayed capture of
15340 -- global references within its aspect specifications.
15342 procedure Save_References_In_Aggregate (N : Node_Id);
15343 -- Save all global references in [extension] aggregate node N
15345 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
15346 -- Save all global references in a character literal or operator
15347 -- symbol denoted by N.
15349 procedure Save_References_In_Descendants (N : Node_Id);
15350 -- Save all global references in all descendants of node N
15352 procedure Save_References_In_Identifier (N : Node_Id);
15353 -- Save all global references in identifier node N
15355 procedure Save_References_In_Operator (N : Node_Id);
15356 -- Save all global references in operator node N
15358 procedure Save_References_In_Pragma (Prag : Node_Id);
15359 -- Save all global references found within the expression of pragma
15360 -- Prag.
15362 ---------------------------
15363 -- Requires_Delayed_Save --
15364 ---------------------------
15366 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
15367 begin
15368 -- Generic packages and subprograms require delayed capture of
15369 -- global references within their aspects due to the timing of
15370 -- annotation analysis.
15372 if Nkind_In (Nod, N_Generic_Package_Declaration,
15373 N_Generic_Subprogram_Declaration,
15374 N_Package_Body,
15375 N_Package_Body_Stub,
15376 N_Subprogram_Body,
15377 N_Subprogram_Body_Stub)
15378 then
15379 -- Since the capture of global references is done on the
15380 -- unanalyzed generic template, there is no information around
15381 -- to infer the context. Use the Associated_Entity linkages to
15382 -- peek into the analyzed generic copy and determine what the
15383 -- template corresponds to.
15385 if Nod = Templ then
15386 return
15387 Is_Generic_Declaration_Or_Body
15388 (Unit_Declaration_Node
15389 (Associated_Entity (Defining_Entity (Nod))));
15391 -- Otherwise the generic unit being processed is not the top
15392 -- level template. It is safe to capture of global references
15393 -- within the generic unit because at this point the top level
15394 -- copy is fully analyzed.
15396 else
15397 return False;
15398 end if;
15400 -- Otherwise capture the global references without interference
15402 else
15403 return False;
15404 end if;
15405 end Requires_Delayed_Save;
15407 ----------------------------------
15408 -- Save_References_In_Aggregate --
15409 ----------------------------------
15411 procedure Save_References_In_Aggregate (N : Node_Id) is
15412 Nam : Node_Id;
15413 Qual : Node_Id := Empty;
15414 Typ : Entity_Id := Empty;
15416 use Atree.Unchecked_Access;
15417 -- This code section is part of implementing an untyped tree
15418 -- traversal, so it needs direct access to node fields.
15420 begin
15421 N2 := Get_Associated_Node (N);
15423 if Present (N2) then
15424 Typ := Etype (N2);
15426 -- In an instance within a generic, use the name of the actual
15427 -- and not the original generic parameter. If the actual is
15428 -- global in the current generic it must be preserved for its
15429 -- instantiation.
15431 if Nkind (Parent (Typ)) = N_Subtype_Declaration
15432 and then Present (Generic_Parent_Type (Parent (Typ)))
15433 then
15434 Typ := Base_Type (Typ);
15435 Set_Etype (N2, Typ);
15436 end if;
15437 end if;
15439 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
15440 Set_Associated_Node (N, Empty);
15442 -- If the aggregate is an actual in a call, it has been
15443 -- resolved in the current context, to some local type. The
15444 -- enclosing call may have been disambiguated by the aggregate,
15445 -- and this disambiguation might fail at instantiation time
15446 -- because the type to which the aggregate did resolve is not
15447 -- preserved. In order to preserve some of this information,
15448 -- wrap the aggregate in a qualified expression, using the id
15449 -- of its type. For further disambiguation we qualify the type
15450 -- name with its scope (if visible and not hidden by a local
15451 -- homograph) because both id's will have corresponding
15452 -- entities in an instance. This resolves most of the problems
15453 -- with missing type information on aggregates in instances.
15455 if Present (N2)
15456 and then Nkind (N2) = Nkind (N)
15457 and then Nkind (Parent (N2)) in N_Subprogram_Call
15458 and then Present (Typ)
15459 and then Comes_From_Source (Typ)
15460 then
15461 Nam := Make_Identifier (Loc, Chars (Typ));
15463 if Is_Immediately_Visible (Scope (Typ))
15464 and then
15465 (not In_Open_Scopes (Scope (Typ))
15466 or else Current_Entity (Scope (Typ)) = Scope (Typ))
15467 then
15468 Nam :=
15469 Make_Selected_Component (Loc,
15470 Prefix =>
15471 Make_Identifier (Loc, Chars (Scope (Typ))),
15472 Selector_Name => Nam);
15473 end if;
15475 Qual :=
15476 Make_Qualified_Expression (Loc,
15477 Subtype_Mark => Nam,
15478 Expression => Relocate_Node (N));
15479 end if;
15480 end if;
15482 Save_Global_Descendant (Field1 (N));
15483 Save_Global_Descendant (Field2 (N));
15484 Save_Global_Descendant (Field3 (N));
15485 Save_Global_Descendant (Field5 (N));
15487 if Present (Qual) then
15488 Rewrite (N, Qual);
15489 end if;
15490 end Save_References_In_Aggregate;
15492 ----------------------------------------------
15493 -- Save_References_In_Char_Lit_Or_Op_Symbol --
15494 ----------------------------------------------
15496 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
15497 begin
15498 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15499 Reset_Entity (N);
15501 elsif Nkind (N) = N_Operator_Symbol
15502 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
15503 then
15504 Change_Operator_Symbol_To_String_Literal (N);
15505 end if;
15506 end Save_References_In_Char_Lit_Or_Op_Symbol;
15508 ------------------------------------
15509 -- Save_References_In_Descendants --
15510 ------------------------------------
15512 procedure Save_References_In_Descendants (N : Node_Id) is
15513 use Atree.Unchecked_Access;
15514 -- This code section is part of implementing an untyped tree
15515 -- traversal, so it needs direct access to node fields.
15517 begin
15518 Save_Global_Descendant (Field1 (N));
15519 Save_Global_Descendant (Field2 (N));
15520 Save_Global_Descendant (Field3 (N));
15521 Save_Global_Descendant (Field4 (N));
15522 Save_Global_Descendant (Field5 (N));
15523 end Save_References_In_Descendants;
15525 -----------------------------------
15526 -- Save_References_In_Identifier --
15527 -----------------------------------
15529 procedure Save_References_In_Identifier (N : Node_Id) is
15530 begin
15531 -- The node did not undergo a transformation
15533 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15534 declare
15535 Aux_N2 : constant Node_Id := Get_Associated_Node (N);
15536 Orig_N2_Parent : constant Node_Id :=
15537 Original_Node (Parent (Aux_N2));
15538 begin
15539 -- The parent of this identifier is a selected component
15540 -- which denotes a named number that was constant folded.
15541 -- Preserve the original name for ASIS and link the parent
15542 -- with its expanded name. The constant folding will be
15543 -- repeated in the instance.
15545 if Nkind (Parent (N)) = N_Selected_Component
15546 and then Nkind_In (Parent (Aux_N2), N_Integer_Literal,
15547 N_Real_Literal)
15548 and then Is_Entity_Name (Orig_N2_Parent)
15549 and then Ekind (Entity (Orig_N2_Parent)) in Named_Kind
15550 and then Is_Global (Entity (Orig_N2_Parent))
15551 then
15552 N2 := Aux_N2;
15553 Set_Associated_Node
15554 (Parent (N), Original_Node (Parent (N2)));
15556 -- Common case
15558 else
15559 -- If this is a discriminant reference, always save it.
15560 -- It is used in the instance to find the corresponding
15561 -- discriminant positionally rather than by name.
15563 Set_Original_Discriminant
15564 (N, Original_Discriminant (Get_Associated_Node (N)));
15565 end if;
15567 Reset_Entity (N);
15568 end;
15570 -- The analysis of the generic copy transformed the identifier
15571 -- into another construct. Propagate the changes to the template.
15573 else
15574 N2 := Get_Associated_Node (N);
15576 -- The identifier denotes a call to a parameterless function.
15577 -- Mark the node as resolved when the function is external.
15579 if Nkind (N2) = N_Function_Call then
15580 E := Entity (Name (N2));
15582 if Present (E) and then Is_Global (E) then
15583 Set_Etype (N, Etype (N2));
15584 else
15585 Set_Associated_Node (N, Empty);
15586 Set_Etype (N, Empty);
15587 end if;
15589 -- The identifier denotes a named number that was constant
15590 -- folded. Preserve the original name for ASIS and undo the
15591 -- constant folding which will be repeated in the instance.
15593 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
15594 and then Is_Entity_Name (Original_Node (N2))
15595 then
15596 Set_Associated_Node (N, Original_Node (N2));
15597 Reset_Entity (N);
15599 -- The identifier resolved to a string literal. Propagate this
15600 -- information to the generic template.
15602 elsif Nkind (N2) = N_String_Literal then
15603 Rewrite (N, New_Copy (N2));
15605 -- The identifier is rewritten as a dereference if it is the
15606 -- prefix of an implicit dereference. Preserve the original
15607 -- tree as the analysis of the instance will expand the node
15608 -- again, but preserve the resolved entity if it is global.
15610 elsif Nkind (N2) = N_Explicit_Dereference then
15611 if Is_Entity_Name (Prefix (N2))
15612 and then Present (Entity (Prefix (N2)))
15613 and then Is_Global (Entity (Prefix (N2)))
15614 then
15615 Set_Associated_Node (N, Prefix (N2));
15617 elsif Nkind (Prefix (N2)) = N_Function_Call
15618 and then Present (Entity (Name (Prefix (N2))))
15619 and then Is_Global (Entity (Name (Prefix (N2))))
15620 then
15621 Rewrite (N,
15622 Make_Explicit_Dereference (Loc,
15623 Prefix =>
15624 Make_Function_Call (Loc,
15625 Name =>
15626 New_Occurrence_Of
15627 (Entity (Name (Prefix (N2))), Loc))));
15629 else
15630 Set_Associated_Node (N, Empty);
15631 Set_Etype (N, Empty);
15632 end if;
15634 -- The subtype mark of a nominally unconstrained object is
15635 -- rewritten as a subtype indication using the bounds of the
15636 -- expression. Recover the original subtype mark.
15638 elsif Nkind (N2) = N_Subtype_Indication
15639 and then Is_Entity_Name (Original_Node (N2))
15640 then
15641 Set_Associated_Node (N, Original_Node (N2));
15642 Reset_Entity (N);
15643 end if;
15644 end if;
15645 end Save_References_In_Identifier;
15647 ---------------------------------
15648 -- Save_References_In_Operator --
15649 ---------------------------------
15651 procedure Save_References_In_Operator (N : Node_Id) is
15652 begin
15653 -- The node did not undergo a transformation
15655 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15656 if Nkind (N) = N_Op_Concat then
15657 Set_Is_Component_Left_Opnd (N,
15658 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15660 Set_Is_Component_Right_Opnd (N,
15661 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15662 end if;
15664 Reset_Entity (N);
15666 -- The analysis of the generic copy transformed the operator into
15667 -- some other construct. Propagate the changes to the template if
15668 -- applicable.
15670 else
15671 N2 := Get_Associated_Node (N);
15673 -- The operator resoved to a function call
15675 if Nkind (N2) = N_Function_Call then
15677 -- Add explicit qualifications in the generic template for
15678 -- all operands of universal type. This aids resolution by
15679 -- preserving the actual type of a literal or an attribute
15680 -- that yields a universal result.
15682 Qualify_Universal_Operands (N, N2);
15684 E := Entity (Name (N2));
15686 if Present (E) and then Is_Global (E) then
15687 Set_Etype (N, Etype (N2));
15688 else
15689 Set_Associated_Node (N, Empty);
15690 Set_Etype (N, Empty);
15691 end if;
15693 -- The operator was folded into a literal
15695 elsif Nkind_In (N2, N_Integer_Literal,
15696 N_Real_Literal,
15697 N_String_Literal)
15698 then
15699 if Present (Original_Node (N2))
15700 and then Nkind (Original_Node (N2)) = Nkind (N)
15701 then
15702 -- Operation was constant-folded. Whenever possible,
15703 -- recover semantic information from unfolded node,
15704 -- for ASIS use.
15706 Set_Associated_Node (N, Original_Node (N2));
15708 if Nkind (N) = N_Op_Concat then
15709 Set_Is_Component_Left_Opnd (N,
15710 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15711 Set_Is_Component_Right_Opnd (N,
15712 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15713 end if;
15715 Reset_Entity (N);
15717 -- Propagate the constant folding back to the template
15719 else
15720 Rewrite (N, New_Copy (N2));
15721 Set_Analyzed (N, False);
15722 end if;
15724 -- The operator was folded into an enumeration literal. Retain
15725 -- the entity to avoid spurious ambiguities if it is overloaded
15726 -- at the point of instantiation or inlining.
15728 elsif Nkind (N2) = N_Identifier
15729 and then Ekind (Entity (N2)) = E_Enumeration_Literal
15730 then
15731 Rewrite (N, New_Copy (N2));
15732 Set_Analyzed (N, False);
15733 end if;
15734 end if;
15736 -- Complete the operands check if node has not been constant
15737 -- folded.
15739 if Nkind (N) in N_Op then
15740 Save_Entity_Descendants (N);
15741 end if;
15742 end Save_References_In_Operator;
15744 -------------------------------
15745 -- Save_References_In_Pragma --
15746 -------------------------------
15748 procedure Save_References_In_Pragma (Prag : Node_Id) is
15749 Context : Node_Id;
15750 Do_Save : Boolean := True;
15752 use Atree.Unchecked_Access;
15753 -- This code section is part of implementing an untyped tree
15754 -- traversal, so it needs direct access to node fields.
15756 begin
15757 -- Do not save global references in pragmas generated from aspects
15758 -- because the pragmas will be regenerated at instantiation time.
15760 if From_Aspect_Specification (Prag) then
15761 Do_Save := False;
15763 -- The capture of global references within contract-related source
15764 -- pragmas associated with generic packages, subprograms or their
15765 -- respective bodies must be delayed due to timing of annotation
15766 -- analysis. Global references are still captured in routine
15767 -- Save_Global_References_In_Contract.
15769 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
15770 if Is_Package_Contract_Annotation (Prag) then
15771 Context := Find_Related_Package_Or_Body (Prag);
15772 else
15773 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
15774 Context := Find_Related_Declaration_Or_Body (Prag);
15775 end if;
15777 -- The use of Original_Node accounts for the case when the
15778 -- related context is generic template.
15780 if Requires_Delayed_Save (Original_Node (Context)) then
15781 Do_Save := False;
15782 end if;
15783 end if;
15785 -- For all other cases, save all global references within the
15786 -- descendants, but skip the following semantic fields:
15788 -- Field1 - Next_Pragma
15789 -- Field3 - Corresponding_Aspect
15790 -- Field5 - Next_Rep_Item
15792 if Do_Save then
15793 Save_Global_Descendant (Field2 (Prag));
15794 Save_Global_Descendant (Field4 (Prag));
15795 end if;
15796 end Save_References_In_Pragma;
15798 -- Start of processing for Save_References
15800 begin
15801 if N = Empty then
15802 null;
15804 -- Aggregates
15806 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
15807 Save_References_In_Aggregate (N);
15809 -- Character literals, operator symbols
15811 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
15812 Save_References_In_Char_Lit_Or_Op_Symbol (N);
15814 -- Defining identifiers
15816 elsif Nkind (N) in N_Entity then
15817 null;
15819 -- Identifiers
15821 elsif Nkind (N) = N_Identifier then
15822 Save_References_In_Identifier (N);
15824 -- Operators
15826 elsif Nkind (N) in N_Op then
15827 Save_References_In_Operator (N);
15829 -- Pragmas
15831 elsif Nkind (N) = N_Pragma then
15832 Save_References_In_Pragma (N);
15834 else
15835 Save_References_In_Descendants (N);
15836 end if;
15838 -- Save all global references found within the aspect specifications
15839 -- of the related node.
15841 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
15843 -- The capture of global references within aspects associated with
15844 -- generic packages, subprograms or their bodies must be delayed
15845 -- due to timing of annotation analysis. Global references are
15846 -- still captured in routine Save_Global_References_In_Contract.
15848 if Requires_Delayed_Save (N) then
15849 null;
15851 -- Otherwise save all global references within the aspects
15853 else
15854 Save_Global_References_In_Aspects (N);
15855 end if;
15856 end if;
15857 end Save_References;
15859 -- Start of processing for Save_Global_References
15861 begin
15862 Gen_Scope := Current_Scope;
15864 -- If the generic unit is a child unit, references to entities in the
15865 -- parent are treated as local, because they will be resolved anew in
15866 -- the context of the instance of the parent.
15868 while Is_Child_Unit (Gen_Scope)
15869 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
15870 loop
15871 Gen_Scope := Scope (Gen_Scope);
15872 end loop;
15874 Save_References (Templ);
15875 end Save_Global_References;
15877 ---------------------------------------
15878 -- Save_Global_References_In_Aspects --
15879 ---------------------------------------
15881 procedure Save_Global_References_In_Aspects (N : Node_Id) is
15882 Asp : Node_Id;
15883 Expr : Node_Id;
15885 begin
15886 Asp := First (Aspect_Specifications (N));
15887 while Present (Asp) loop
15888 Expr := Expression (Asp);
15890 if Present (Expr) then
15891 Save_Global_References (Expr);
15892 end if;
15894 Next (Asp);
15895 end loop;
15896 end Save_Global_References_In_Aspects;
15898 ------------------------------------------
15899 -- Set_Copied_Sloc_For_Inherited_Pragma --
15900 ------------------------------------------
15902 procedure Set_Copied_Sloc_For_Inherited_Pragma
15903 (N : Node_Id;
15904 E : Entity_Id)
15906 begin
15907 Create_Instantiation_Source (N, E,
15908 Inlined_Body => False,
15909 Inherited_Pragma => True,
15910 Factor => S_Adjustment);
15911 end Set_Copied_Sloc_For_Inherited_Pragma;
15913 --------------------------------------
15914 -- Set_Copied_Sloc_For_Inlined_Body --
15915 --------------------------------------
15917 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
15918 begin
15919 Create_Instantiation_Source (N, E,
15920 Inlined_Body => True,
15921 Inherited_Pragma => False,
15922 Factor => S_Adjustment);
15923 end Set_Copied_Sloc_For_Inlined_Body;
15925 ---------------------
15926 -- Set_Instance_Of --
15927 ---------------------
15929 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
15930 begin
15931 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
15932 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
15933 Generic_Renamings.Increment_Last;
15934 end Set_Instance_Of;
15936 --------------------
15937 -- Set_Next_Assoc --
15938 --------------------
15940 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
15941 begin
15942 Generic_Renamings.Table (E).Next_In_HTable := Next;
15943 end Set_Next_Assoc;
15945 -------------------
15946 -- Start_Generic --
15947 -------------------
15949 procedure Start_Generic is
15950 begin
15951 -- ??? More things could be factored out in this routine.
15952 -- Should probably be done at a later stage.
15954 Generic_Flags.Append (Inside_A_Generic);
15955 Inside_A_Generic := True;
15957 Expander_Mode_Save_And_Set (False);
15958 end Start_Generic;
15960 ----------------------
15961 -- Set_Instance_Env --
15962 ----------------------
15964 -- WARNING: This routine manages SPARK regions
15966 procedure Set_Instance_Env
15967 (Gen_Unit : Entity_Id;
15968 Act_Unit : Entity_Id)
15970 Saved_AE : constant Boolean := Assertions_Enabled;
15971 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
15972 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
15973 -- Save the SPARK mode-related data because utilizing the configuration
15974 -- values of pragmas and switches will eliminate any previously set
15975 -- SPARK_Mode.
15977 begin
15978 -- Regardless of the current mode, predefined units are analyzed in the
15979 -- most current Ada mode, and earlier version Ada checks do not apply
15980 -- to predefined units. Nothing needs to be done for non-internal units.
15981 -- These are always analyzed in the current mode.
15983 if In_Internal_Unit (Gen_Unit) then
15984 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
15986 -- In Ada2012 we may want to enable assertions in an instance of a
15987 -- predefined unit, in which case we need to preserve the current
15988 -- setting for the Assertions_Enabled flag. This will become more
15989 -- critical when pre/postconditions are added to predefined units,
15990 -- as is already the case for some numeric libraries.
15992 if Ada_Version >= Ada_2012 then
15993 Assertions_Enabled := Saved_AE;
15994 end if;
15996 -- Reinstall the SPARK_Mode which was in effect at the point of
15997 -- instantiation.
15999 Install_SPARK_Mode (Saved_SM, Saved_SMP);
16000 end if;
16002 Current_Instantiated_Parent :=
16003 (Gen_Id => Gen_Unit,
16004 Act_Id => Act_Unit,
16005 Next_In_HTable => Assoc_Null);
16006 end Set_Instance_Env;
16008 -----------------
16009 -- Switch_View --
16010 -----------------
16012 procedure Switch_View (T : Entity_Id) is
16013 BT : constant Entity_Id := Base_Type (T);
16014 Priv_Elmt : Elmt_Id := No_Elmt;
16015 Priv_Sub : Entity_Id;
16017 begin
16018 -- T may be private but its base type may have been exchanged through
16019 -- some other occurrence, in which case there is nothing to switch
16020 -- besides T itself. Note that a private dependent subtype of a private
16021 -- type might not have been switched even if the base type has been,
16022 -- because of the last branch of Check_Private_View (see comment there).
16024 if not Is_Private_Type (BT) then
16025 Prepend_Elmt (Full_View (T), Exchanged_Views);
16026 Exchange_Declarations (T);
16027 return;
16028 end if;
16030 Priv_Elmt := First_Elmt (Private_Dependents (BT));
16032 if Present (Full_View (BT)) then
16033 Prepend_Elmt (Full_View (BT), Exchanged_Views);
16034 Exchange_Declarations (BT);
16035 end if;
16037 while Present (Priv_Elmt) loop
16038 Priv_Sub := (Node (Priv_Elmt));
16040 -- We avoid flipping the subtype if the Etype of its full view is
16041 -- private because this would result in a malformed subtype. This
16042 -- occurs when the Etype of the subtype full view is the full view of
16043 -- the base type (and since the base types were just switched, the
16044 -- subtype is pointing to the wrong view). This is currently the case
16045 -- for tagged record types, access types (maybe more?) and needs to
16046 -- be resolved. ???
16048 if Present (Full_View (Priv_Sub))
16049 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
16050 then
16051 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
16052 Exchange_Declarations (Priv_Sub);
16053 end if;
16055 Next_Elmt (Priv_Elmt);
16056 end loop;
16057 end Switch_View;
16059 -----------------
16060 -- True_Parent --
16061 -----------------
16063 function True_Parent (N : Node_Id) return Node_Id is
16064 begin
16065 if Nkind (Parent (N)) = N_Subunit then
16066 return Parent (Corresponding_Stub (Parent (N)));
16067 else
16068 return Parent (N);
16069 end if;
16070 end True_Parent;
16072 -----------------------------
16073 -- Valid_Default_Attribute --
16074 -----------------------------
16076 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
16077 Attr_Id : constant Attribute_Id :=
16078 Get_Attribute_Id (Attribute_Name (Def));
16079 T : constant Entity_Id := Entity (Prefix (Def));
16080 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
16081 F : Entity_Id;
16082 Num_F : Nat;
16083 OK : Boolean;
16085 begin
16086 if No (T) or else T = Any_Id then
16087 return;
16088 end if;
16090 Num_F := 0;
16091 F := First_Formal (Nam);
16092 while Present (F) loop
16093 Num_F := Num_F + 1;
16094 Next_Formal (F);
16095 end loop;
16097 case Attr_Id is
16098 when Attribute_Adjacent
16099 | Attribute_Ceiling
16100 | Attribute_Copy_Sign
16101 | Attribute_Floor
16102 | Attribute_Fraction
16103 | Attribute_Machine
16104 | Attribute_Model
16105 | Attribute_Remainder
16106 | Attribute_Rounding
16107 | Attribute_Unbiased_Rounding
16109 OK := Is_Fun
16110 and then Num_F = 1
16111 and then Is_Floating_Point_Type (T);
16113 when Attribute_Image
16114 | Attribute_Pred
16115 | Attribute_Succ
16116 | Attribute_Value
16117 | Attribute_Wide_Image
16118 | Attribute_Wide_Value
16120 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
16122 when Attribute_Max
16123 | Attribute_Min
16125 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
16127 when Attribute_Input =>
16128 OK := (Is_Fun and then Num_F = 1);
16130 when Attribute_Output
16131 | Attribute_Read
16132 | Attribute_Write
16134 OK := not Is_Fun and then Num_F = 2;
16136 when others =>
16137 OK := False;
16138 end case;
16140 if not OK then
16141 Error_Msg_N
16142 ("attribute reference has wrong profile for subprogram", Def);
16143 end if;
16144 end Valid_Default_Attribute;
16146 end Sem_Ch12;