[multiple changes]
[gcc.git] / gcc / ada / sem_ch12.adb
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-2016, 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 ------------------------------------------------------------------------------
25
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Contracts; use Contracts;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Disp; use Exp_Disp;
34 with Fname; use Fname;
35 with Fname.UF; use Fname.UF;
36 with Freeze; use Freeze;
37 with Ghost; use Ghost;
38 with Itypes; use Itypes;
39 with Lib; use Lib;
40 with Lib.Load; use Lib.Load;
41 with Lib.Xref; use Lib.Xref;
42 with Nlists; use Nlists;
43 with Namet; use Namet;
44 with Nmake; use Nmake;
45 with Opt; use Opt;
46 with Rident; use Rident;
47 with Restrict; use Restrict;
48 with Rtsfind; use Rtsfind;
49 with Sem; use Sem;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Cat; use Sem_Cat;
52 with Sem_Ch3; use Sem_Ch3;
53 with Sem_Ch6; use Sem_Ch6;
54 with Sem_Ch7; use Sem_Ch7;
55 with Sem_Ch8; use Sem_Ch8;
56 with Sem_Ch10; use Sem_Ch10;
57 with Sem_Ch13; use Sem_Ch13;
58 with Sem_Dim; use Sem_Dim;
59 with Sem_Disp; use Sem_Disp;
60 with Sem_Elab; use Sem_Elab;
61 with Sem_Elim; use Sem_Elim;
62 with Sem_Eval; use Sem_Eval;
63 with Sem_Prag; use Sem_Prag;
64 with Sem_Res; use Sem_Res;
65 with Sem_Type; use Sem_Type;
66 with Sem_Util; use Sem_Util;
67 with Sem_Warn; use Sem_Warn;
68 with Stand; use Stand;
69 with Sinfo; use Sinfo;
70 with Sinfo.CN; use Sinfo.CN;
71 with Sinput; use Sinput;
72 with Sinput.L; use Sinput.L;
73 with Snames; use Snames;
74 with Stringt; use Stringt;
75 with Uname; use Uname;
76 with Table;
77 with Tbuild; use Tbuild;
78 with Uintp; use Uintp;
79 with Urealp; use Urealp;
80 with Warnsw; use Warnsw;
81
82 with GNAT.HTable;
83
84 package body Sem_Ch12 is
85
86 ----------------------------------------------------------
87 -- Implementation of Generic Analysis and Instantiation --
88 ----------------------------------------------------------
89
90 -- GNAT implements generics by macro expansion. No attempt is made to share
91 -- generic instantiations (for now). Analysis of a generic definition does
92 -- not perform any expansion action, but the expander must be called on the
93 -- tree for each instantiation, because the expansion may of course depend
94 -- on the generic actuals. All of this is best achieved as follows:
95 --
96 -- a) Semantic analysis of a generic unit is performed on a copy of the
97 -- tree for the generic unit. All tree modifications that follow analysis
98 -- do not affect the original tree. Links are kept between the original
99 -- tree and the copy, in order to recognize non-local references within
100 -- the generic, and propagate them to each instance (recall that name
101 -- resolution is done on the generic declaration: generics are not really
102 -- macros). This is summarized in the following diagram:
103
104 -- .-----------. .----------.
105 -- | semantic |<--------------| generic |
106 -- | copy | | unit |
107 -- | |==============>| |
108 -- |___________| global |__________|
109 -- references | | |
110 -- | | |
111 -- .-----|--|.
112 -- | .-----|---.
113 -- | | .----------.
114 -- | | | generic |
115 -- |__| | |
116 -- |__| instance |
117 -- |__________|
118
119 -- b) Each instantiation copies the original tree, and inserts into it a
120 -- series of declarations that describe the mapping between generic formals
121 -- and actuals. For example, a generic In OUT parameter is an object
122 -- renaming of the corresponding actual, etc. Generic IN parameters are
123 -- constant declarations.
124
125 -- c) In order to give the right visibility for these renamings, we use
126 -- a different scheme for package and subprogram instantiations. For
127 -- packages, the list of renamings is inserted into the package
128 -- specification, before the visible declarations of the package. The
129 -- renamings are analyzed before any of the text of the instance, and are
130 -- thus visible at the right place. Furthermore, outside of the instance,
131 -- the generic parameters are visible and denote their corresponding
132 -- actuals.
133
134 -- For subprograms, we create a container package to hold the renamings
135 -- and the subprogram instance itself. Analysis of the package makes the
136 -- renaming declarations visible to the subprogram. After analyzing the
137 -- package, the defining entity for the subprogram is touched-up so that
138 -- it appears declared in the current scope, and not inside the container
139 -- package.
140
141 -- If the instantiation is a compilation unit, the container package is
142 -- given the same name as the subprogram instance. This ensures that
143 -- the elaboration procedure called by the binder, using the compilation
144 -- unit name, calls in fact the elaboration procedure for the package.
145
146 -- Not surprisingly, private types complicate this approach. By saving in
147 -- the original generic object the non-local references, we guarantee that
148 -- the proper entities are referenced at the point of instantiation.
149 -- However, for private types, this by itself does not insure that the
150 -- proper VIEW of the entity is used (the full type may be visible at the
151 -- point of generic definition, but not at instantiation, or vice-versa).
152 -- In order to reference the proper view, we special-case any reference
153 -- to private types in the generic object, by saving both views, one in
154 -- the generic and one in the semantic copy. At time of instantiation, we
155 -- check whether the two views are consistent, and exchange declarations if
156 -- necessary, in order to restore the correct visibility. Similarly, if
157 -- the instance view is private when the generic view was not, we perform
158 -- the exchange. After completing the instantiation, we restore the
159 -- current visibility. The flag Has_Private_View marks identifiers in the
160 -- the generic unit that require checking.
161
162 -- Visibility within nested generic units requires special handling.
163 -- Consider the following scheme:
164
165 -- type Global is ... -- outside of generic unit.
166 -- generic ...
167 -- package Outer is
168 -- ...
169 -- type Semi_Global is ... -- global to inner.
170
171 -- generic ... -- 1
172 -- procedure inner (X1 : Global; X2 : Semi_Global);
173
174 -- procedure in2 is new inner (...); -- 4
175 -- end Outer;
176
177 -- package New_Outer is new Outer (...); -- 2
178 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
179
180 -- The semantic analysis of Outer captures all occurrences of Global.
181 -- The semantic analysis of Inner (at 1) captures both occurrences of
182 -- Global and Semi_Global.
183
184 -- At point 2 (instantiation of Outer), we also produce a generic copy
185 -- of Inner, even though Inner is, at that point, not being instantiated.
186 -- (This is just part of the semantic analysis of New_Outer).
187
188 -- Critically, references to Global within Inner must be preserved, while
189 -- references to Semi_Global should not preserved, because they must now
190 -- resolve to an entity within New_Outer. To distinguish between these, we
191 -- use a global variable, Current_Instantiated_Parent, which is set when
192 -- performing a generic copy during instantiation (at 2). This variable is
193 -- used when performing a generic copy that is not an instantiation, but
194 -- that is nested within one, as the occurrence of 1 within 2. The analysis
195 -- of a nested generic only preserves references that are global to the
196 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
197 -- determine whether a reference is external to the given parent.
198
199 -- The instantiation at point 3 requires no special treatment. The method
200 -- works as well for further nestings of generic units, but of course the
201 -- variable Current_Instantiated_Parent must be stacked because nested
202 -- instantiations can occur, e.g. the occurrence of 4 within 2.
203
204 -- The instantiation of package and subprogram bodies is handled in a
205 -- similar manner, except that it is delayed until after semantic
206 -- analysis is complete. In this fashion complex cross-dependencies
207 -- between several package declarations and bodies containing generics
208 -- can be compiled which otherwise would diagnose spurious circularities.
209
210 -- For example, it is possible to compile two packages A and B that
211 -- have the following structure:
212
213 -- package A is package B is
214 -- generic ... generic ...
215 -- package G_A is package G_B is
216
217 -- with B; with A;
218 -- package body A is package body B is
219 -- package N_B is new G_B (..) package N_A is new G_A (..)
220
221 -- The table Pending_Instantiations in package Inline is used to keep
222 -- track of body instantiations that are delayed in this manner. Inline
223 -- handles the actual calls to do the body instantiations. This activity
224 -- is part of Inline, since the processing occurs at the same point, and
225 -- for essentially the same reason, as the handling of inlined routines.
226
227 ----------------------------------------------
228 -- Detection of Instantiation Circularities --
229 ----------------------------------------------
230
231 -- If we have a chain of instantiations that is circular, this is static
232 -- error which must be detected at compile time. The detection of these
233 -- circularities is carried out at the point that we insert a generic
234 -- instance spec or body. If there is a circularity, then the analysis of
235 -- the offending spec or body will eventually result in trying to load the
236 -- same unit again, and we detect this problem as we analyze the package
237 -- instantiation for the second time.
238
239 -- At least in some cases after we have detected the circularity, we get
240 -- into trouble if we try to keep going. The following flag is set if a
241 -- circularity is detected, and used to abandon compilation after the
242 -- messages have been posted.
243
244 -----------------------------------------
245 -- Implementation of Generic Contracts --
246 -----------------------------------------
247
248 -- A "contract" is a collection of aspects and pragmas that either verify a
249 -- property of a construct at runtime or classify the data flow to and from
250 -- the construct in some fashion.
251
252 -- Generic packages, subprograms and their respective bodies may be subject
253 -- to the following contract-related aspects or pragmas collectively known
254 -- as annotations:
255
256 -- package subprogram [body]
257 -- Abstract_State Contract_Cases
258 -- Initial_Condition Depends
259 -- Initializes Extensions_Visible
260 -- Global
261 -- package body Post
262 -- Refined_State Post_Class
263 -- Postcondition
264 -- Pre
265 -- Pre_Class
266 -- Precondition
267 -- Refined_Depends
268 -- Refined_Global
269 -- Refined_Post
270 -- Test_Case
271
272 -- Most package contract annotations utilize forward references to classify
273 -- data declared within the package [body]. Subprogram annotations then use
274 -- the classifications to further refine them. These inter dependencies are
275 -- problematic with respect to the implementation of generics because their
276 -- analysis, capture of global references and instantiation does not mesh
277 -- well with the existing mechanism.
278
279 -- 1) Analysis of generic contracts is carried out the same way non-generic
280 -- contracts are analyzed:
281
282 -- 1.1) General rule - a contract is analyzed after all related aspects
283 -- and pragmas are analyzed. This is done by routines
284
285 -- Analyze_Package_Body_Contract
286 -- Analyze_Package_Contract
287 -- Analyze_Subprogram_Body_Contract
288 -- Analyze_Subprogram_Contract
289
290 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
291 -- are processed.
292
293 -- 1.3) Compilation unit body - the contract is analyzed at the end of
294 -- the body declaration list.
295
296 -- 1.4) Package - the contract is analyzed at the end of the private or
297 -- visible declarations, prior to analyzing the contracts of any nested
298 -- packages or subprograms.
299
300 -- 1.5) Package body - the contract is analyzed at the end of the body
301 -- declaration list, prior to analyzing the contracts of any nested
302 -- packages or subprograms.
303
304 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
305 -- package or a subprogram, then its contract is analyzed at the end of
306 -- the enclosing declarations, otherwise the subprogram is a compilation
307 -- unit 1.2).
308
309 -- 1.7) Subprogram body - if the subprogram body is declared inside a
310 -- block, a package body or a subprogram body, then its contract is
311 -- analyzed at the end of the enclosing declarations, otherwise the
312 -- subprogram is a compilation unit 1.3).
313
314 -- 2) Capture of global references within contracts is done after capturing
315 -- global references within the generic template. There are two reasons for
316 -- this delay - pragma annotations are not part of the generic template in
317 -- the case of a generic subprogram declaration, and analysis of contracts
318 -- is delayed.
319
320 -- Contract-related source pragmas within generic templates are prepared
321 -- for delayed capture of global references by routine
322
323 -- Create_Generic_Contract
324
325 -- The routine associates these pragmas with the contract of the template.
326 -- In the case of a generic subprogram declaration, the routine creates
327 -- generic templates for the pragmas declared after the subprogram because
328 -- they are not part of the template.
329
330 -- generic -- template starts
331 -- procedure Gen_Proc (Input : Integer); -- template ends
332 -- pragma Precondition (Input > 0); -- requires own template
333
334 -- 2.1) The capture of global references with aspect specifications and
335 -- source pragmas that apply to a generic unit must be suppressed when
336 -- the generic template is being processed because the contracts have not
337 -- been analyzed yet. Any attempts to capture global references at that
338 -- point will destroy the Associated_Node linkages and leave the template
339 -- undecorated. This delay is controlled by routine
340
341 -- Requires_Delayed_Save
342
343 -- 2.2) The real capture of global references within a contract is done
344 -- after the contract has been analyzed, by routine
345
346 -- Save_Global_References_In_Contract
347
348 -- 3) The instantiation of a generic contract occurs as part of the
349 -- instantiation of the contract owner. Generic subprogram declarations
350 -- require additional processing when the contract is specified by pragmas
351 -- because the pragmas are not part of the generic template. This is done
352 -- by routine
353
354 -- Instantiate_Subprogram_Contract
355
356 Circularity_Detected : Boolean := False;
357 -- This should really be reset on encountering a new main unit, but in
358 -- practice we are not using multiple main units so it is not critical.
359
360 --------------------------------------------------
361 -- Formal packages and partial parameterization --
362 --------------------------------------------------
363
364 -- When compiling a generic, a formal package is a local instantiation. If
365 -- declared with a box, its generic formals are visible in the enclosing
366 -- generic. If declared with a partial list of actuals, those actuals that
367 -- are defaulted (covered by an Others clause, or given an explicit box
368 -- initialization) are also visible in the enclosing generic, while those
369 -- that have a corresponding actual are not.
370
371 -- In our source model of instantiation, the same visibility must be
372 -- present in the spec and body of an instance: the names of the formals
373 -- that are defaulted must be made visible within the instance, and made
374 -- invisible (hidden) after the instantiation is complete, so that they
375 -- are not accessible outside of the instance.
376
377 -- In a generic, a formal package is treated like a special instantiation.
378 -- Our Ada 95 compiler handled formals with and without box in different
379 -- ways. With partial parameterization, we use a single model for both.
380 -- We create a package declaration that consists of the specification of
381 -- the generic package, and a set of declarations that map the actuals
382 -- into local renamings, just as we do for bona fide instantiations. For
383 -- defaulted parameters and formals with a box, we copy directly the
384 -- declarations of the formal into this local package. The result is a
385 -- a package whose visible declarations may include generic formals. This
386 -- package is only used for type checking and visibility analysis, and
387 -- never reaches the back-end, so it can freely violate the placement
388 -- rules for generic formal declarations.
389
390 -- The list of declarations (renamings and copies of formals) is built
391 -- by Analyze_Associations, just as for regular instantiations.
392
393 -- At the point of instantiation, conformance checking must be applied only
394 -- to those parameters that were specified in the formal. We perform this
395 -- checking by creating another internal instantiation, this one including
396 -- only the renamings and the formals (the rest of the package spec is not
397 -- relevant to conformance checking). We can then traverse two lists: the
398 -- list of actuals in the instance that corresponds to the formal package,
399 -- and the list of actuals produced for this bogus instantiation. We apply
400 -- the conformance rules to those actuals that are not defaulted (i.e.
401 -- which still appear as generic formals.
402
403 -- When we compile an instance body we must make the right parameters
404 -- visible again. The predicate Is_Generic_Formal indicates which of the
405 -- formals should have its Is_Hidden flag reset.
406
407 -----------------------
408 -- Local subprograms --
409 -----------------------
410
411 procedure Abandon_Instantiation (N : Node_Id);
412 pragma No_Return (Abandon_Instantiation);
413 -- Posts an error message "instantiation abandoned" at the indicated node
414 -- and then raises the exception Instantiation_Error to do it.
415
416 procedure Analyze_Formal_Array_Type
417 (T : in out Entity_Id;
418 Def : Node_Id);
419 -- A formal array type is treated like an array type declaration, and
420 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
421 -- in-out, because in the case of an anonymous type the entity is
422 -- actually created in the procedure.
423
424 -- The following procedures treat other kinds of formal parameters
425
426 procedure Analyze_Formal_Derived_Interface_Type
427 (N : Node_Id;
428 T : Entity_Id;
429 Def : Node_Id);
430
431 procedure Analyze_Formal_Derived_Type
432 (N : Node_Id;
433 T : Entity_Id;
434 Def : Node_Id);
435
436 procedure Analyze_Formal_Interface_Type
437 (N : Node_Id;
438 T : Entity_Id;
439 Def : Node_Id);
440
441 -- The following subprograms create abbreviated declarations for formal
442 -- scalar types. We introduce an anonymous base of the proper class for
443 -- each of them, and define the formals as constrained first subtypes of
444 -- their bases. The bounds are expressions that are non-static in the
445 -- generic.
446
447 procedure Analyze_Formal_Decimal_Fixed_Point_Type
448 (T : Entity_Id; Def : Node_Id);
449 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
450 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
451 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
452 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
453 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
454 (T : Entity_Id; Def : Node_Id);
455
456 procedure Analyze_Formal_Private_Type
457 (N : Node_Id;
458 T : Entity_Id;
459 Def : Node_Id);
460 -- Creates a new private type, which does not require completion
461
462 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
463 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
464
465 procedure Analyze_Generic_Formal_Part (N : Node_Id);
466 -- Analyze generic formal part
467
468 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
469 -- Create a new access type with the given designated type
470
471 function Analyze_Associations
472 (I_Node : Node_Id;
473 Formals : List_Id;
474 F_Copy : List_Id) return List_Id;
475 -- At instantiation time, build the list of associations between formals
476 -- and actuals. Each association becomes a renaming declaration for the
477 -- formal entity. F_Copy is the analyzed list of formals in the generic
478 -- copy. It is used to apply legality checks to the actuals. I_Node is the
479 -- instantiation node itself.
480
481 procedure Analyze_Subprogram_Instantiation
482 (N : Node_Id;
483 K : Entity_Kind);
484
485 procedure Build_Instance_Compilation_Unit_Nodes
486 (N : Node_Id;
487 Act_Body : Node_Id;
488 Act_Decl : Node_Id);
489 -- This procedure is used in the case where the generic instance of a
490 -- subprogram body or package body is a library unit. In this case, the
491 -- original library unit node for the generic instantiation must be
492 -- replaced by the resulting generic body, and a link made to a new
493 -- compilation unit node for the generic declaration. The argument N is
494 -- the original generic instantiation. Act_Body and Act_Decl are the body
495 -- and declaration of the instance (either package body and declaration
496 -- nodes or subprogram body and declaration nodes depending on the case).
497 -- On return, the node N has been rewritten with the actual body.
498
499 procedure Check_Access_Definition (N : Node_Id);
500 -- Subsidiary routine to null exclusion processing. Perform an assertion
501 -- check on Ada version and the presence of an access definition in N.
502
503 procedure Check_Formal_Packages (P_Id : Entity_Id);
504 -- Apply the following to all formal packages in generic associations
505
506 procedure Check_Formal_Package_Instance
507 (Formal_Pack : Entity_Id;
508 Actual_Pack : Entity_Id);
509 -- Verify that the actuals of the actual instance match the actuals of
510 -- the template for a formal package that is not declared with a box.
511
512 procedure Check_Forward_Instantiation (Decl : Node_Id);
513 -- If the generic is a local entity and the corresponding body has not
514 -- been seen yet, flag enclosing packages to indicate that it will be
515 -- elaborated after the generic body. Subprograms declared in the same
516 -- package cannot be inlined by the front end because front-end inlining
517 -- requires a strict linear order of elaboration.
518
519 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
520 -- Check if some association between formals and actuals requires to make
521 -- visible primitives of a tagged type, and make those primitives visible.
522 -- Return the list of primitives whose visibility is modified (to restore
523 -- their visibility later through Restore_Hidden_Primitives). If no
524 -- candidate is found then return No_Elist.
525
526 procedure Check_Hidden_Child_Unit
527 (N : Node_Id;
528 Gen_Unit : Entity_Id;
529 Act_Decl_Id : Entity_Id);
530 -- If the generic unit is an implicit child instance within a parent
531 -- instance, we need to make an explicit test that it is not hidden by
532 -- a child instance of the same name and parent.
533
534 procedure Check_Generic_Actuals
535 (Instance : Entity_Id;
536 Is_Formal_Box : Boolean);
537 -- Similar to previous one. Check the actuals in the instantiation,
538 -- whose views can change between the point of instantiation and the point
539 -- of instantiation of the body. In addition, mark the generic renamings
540 -- as generic actuals, so that they are not compatible with other actuals.
541 -- Recurse on an actual that is a formal package whose declaration has
542 -- a box.
543
544 function Contains_Instance_Of
545 (Inner : Entity_Id;
546 Outer : Entity_Id;
547 N : Node_Id) return Boolean;
548 -- Inner is instantiated within the generic Outer. Check whether Inner
549 -- directly or indirectly contains an instance of Outer or of one of its
550 -- parents, in the case of a subunit. Each generic unit holds a list of
551 -- the entities instantiated within (at any depth). This procedure
552 -- determines whether the set of such lists contains a cycle, i.e. an
553 -- illegal circular instantiation.
554
555 function Denotes_Formal_Package
556 (Pack : Entity_Id;
557 On_Exit : Boolean := False;
558 Instance : Entity_Id := Empty) return Boolean;
559 -- Returns True if E is a formal package of an enclosing generic, or
560 -- the actual for such a formal in an enclosing instantiation. If such
561 -- a package is used as a formal in an nested generic, or as an actual
562 -- in a nested instantiation, the visibility of ITS formals should not
563 -- be modified. When called from within Restore_Private_Views, the flag
564 -- On_Exit is true, to indicate that the search for a possible enclosing
565 -- instance should ignore the current one. In that case Instance denotes
566 -- the declaration for which this is an actual. This declaration may be
567 -- an instantiation in the source, or the internal instantiation that
568 -- corresponds to the actual for a formal package.
569
570 function Earlier (N1, N2 : Node_Id) return Boolean;
571 -- Yields True if N1 and N2 appear in the same compilation unit,
572 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
573 -- traversal of the tree for the unit. Used to determine the placement
574 -- of freeze nodes for instance bodies that may depend on other instances.
575
576 function Find_Actual_Type
577 (Typ : Entity_Id;
578 Gen_Type : Entity_Id) return Entity_Id;
579 -- When validating the actual types of a child instance, check whether
580 -- the formal is a formal type of the parent unit, and retrieve the current
581 -- actual for it. Typ is the entity in the analyzed formal type declaration
582 -- (component or index type of an array type, or designated type of an
583 -- access formal) and Gen_Type is the enclosing analyzed formal array
584 -- or access type. The desired actual may be a formal of a parent, or may
585 -- be declared in a formal package of a parent. In both cases it is a
586 -- generic actual type because it appears within a visible instance.
587 -- Finally, it may be declared in a parent unit without being a formal
588 -- of that unit, in which case it must be retrieved by visibility.
589 -- Ambiguities may still arise if two homonyms are declared in two formal
590 -- packages, and the prefix of the formal type may be needed to resolve
591 -- the ambiguity in the instance ???
592
593 procedure Freeze_Subprogram_Body
594 (Inst_Node : Node_Id;
595 Gen_Body : Node_Id;
596 Pack_Id : Entity_Id);
597 -- The generic body may appear textually after the instance, including
598 -- in the proper body of a stub, or within a different package instance.
599 -- Given that the instance can only be elaborated after the generic, we
600 -- place freeze_nodes for the instance and/or for packages that may enclose
601 -- the instance and the generic, so that the back-end can establish the
602 -- proper order of elaboration.
603
604 function Get_Associated_Node (N : Node_Id) return Node_Id;
605 -- In order to propagate semantic information back from the analyzed copy
606 -- to the original generic, we maintain links between selected nodes in the
607 -- generic and their corresponding copies. At the end of generic analysis,
608 -- the routine Save_Global_References traverses the generic tree, examines
609 -- the semantic information, and preserves the links to those nodes that
610 -- contain global information. At instantiation, the information from the
611 -- associated node is placed on the new copy, so that name resolution is
612 -- not repeated.
613 --
614 -- Three kinds of source nodes have associated nodes:
615 --
616 -- a) those that can reference (denote) entities, that is identifiers,
617 -- character literals, expanded_names, operator symbols, operators,
618 -- and attribute reference nodes. These nodes have an Entity field
619 -- and are the set of nodes that are in N_Has_Entity.
620 --
621 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
622 --
623 -- c) selected components (N_Selected_Component)
624 --
625 -- For the first class, the associated node preserves the entity if it is
626 -- global. If the generic contains nested instantiations, the associated
627 -- node itself has been recopied, and a chain of them must be followed.
628 --
629 -- For aggregates, the associated node allows retrieval of the type, which
630 -- may otherwise not appear in the generic. The view of this type may be
631 -- different between generic and instantiation, and the full view can be
632 -- installed before the instantiation is analyzed. For aggregates of type
633 -- extensions, the same view exchange may have to be performed for some of
634 -- the ancestor types, if their view is private at the point of
635 -- instantiation.
636 --
637 -- Nodes that are selected components in the parse tree may be rewritten
638 -- as expanded names after resolution, and must be treated as potential
639 -- entity holders, which is why they also have an Associated_Node.
640 --
641 -- Nodes that do not come from source, such as freeze nodes, do not appear
642 -- in the generic tree, and need not have an associated node.
643 --
644 -- The associated node is stored in the Associated_Node field. Note that
645 -- this field overlaps Entity, which is fine, because the whole point is
646 -- that we don't need or want the normal Entity field in this situation.
647
648 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
649 -- Traverse the Exchanged_Views list to see if a type was private
650 -- and has already been flipped during this phase of instantiation.
651
652 procedure Hide_Current_Scope;
653 -- When instantiating a generic child unit, the parent context must be
654 -- present, but the instance and all entities that may be generated
655 -- must be inserted in the current scope. We leave the current scope
656 -- on the stack, but make its entities invisible to avoid visibility
657 -- problems. This is reversed at the end of the instantiation. This is
658 -- not done for the instantiation of the bodies, which only require the
659 -- instances of the generic parents to be in scope.
660
661 function In_Same_Declarative_Part
662 (F_Node : Node_Id;
663 Inst : Node_Id) return Boolean;
664 -- True if the instantiation Inst and the given freeze_node F_Node appear
665 -- within the same declarative part, ignoring subunits, but with no inter-
666 -- vening subprograms or concurrent units. Used to find the proper plave
667 -- for the freeze node of an instance, when the generic is declared in a
668 -- previous instance. If predicate is true, the freeze node of the instance
669 -- can be placed after the freeze node of the previous instance, Otherwise
670 -- it has to be placed at the end of the current declarative part.
671
672 function In_Main_Context (E : Entity_Id) return Boolean;
673 -- Check whether an instantiation is in the context of the main unit.
674 -- Used to determine whether its body should be elaborated to allow
675 -- front-end inlining.
676
677 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
678 -- Add the context clause of the unit containing a generic unit to a
679 -- compilation unit that is, or contains, an instantiation.
680
681 procedure Init_Env;
682 -- Establish environment for subsequent instantiation. Separated from
683 -- Save_Env because data-structures for visibility handling must be
684 -- initialized before call to Check_Generic_Child_Unit.
685
686 procedure Inline_Instance_Body
687 (N : Node_Id;
688 Gen_Unit : Entity_Id;
689 Act_Decl : Node_Id);
690 -- If front-end inlining is requested, instantiate the package body,
691 -- and preserve the visibility of its compilation unit, to insure
692 -- that successive instantiations succeed.
693
694 procedure Insert_Freeze_Node_For_Instance
695 (N : Node_Id;
696 F_Node : Node_Id);
697 -- N denotes a package or a subprogram instantiation and F_Node is the
698 -- associated freeze node. Insert the freeze node before the first source
699 -- body which follows immediately after N. If no such body is found, the
700 -- freeze node is inserted at the end of the declarative region which
701 -- contains N.
702
703 procedure Install_Body
704 (Act_Body : Node_Id;
705 N : Node_Id;
706 Gen_Body : Node_Id;
707 Gen_Decl : Node_Id);
708 -- If the instantiation happens textually before the body of the generic,
709 -- the instantiation of the body must be analyzed after the generic body,
710 -- and not at the point of instantiation. Such early instantiations can
711 -- happen if the generic and the instance appear in a package declaration
712 -- because the generic body can only appear in the corresponding package
713 -- body. Early instantiations can also appear if generic, instance and
714 -- body are all in the declarative part of a subprogram or entry. Entities
715 -- of packages that are early instantiations are delayed, and their freeze
716 -- node appears after the generic body. This rather complex machinery is
717 -- needed when nested instantiations are present, because the source does
718 -- not carry any indication of where the corresponding instance bodies must
719 -- be installed and frozen.
720
721 procedure Install_Formal_Packages (Par : Entity_Id);
722 -- Install the visible part of any formal of the parent that is a formal
723 -- package. Note that for the case of a formal package with a box, this
724 -- includes the formal part of the formal package (12.7(10/2)).
725
726 procedure Install_Hidden_Primitives
727 (Prims_List : in out Elist_Id;
728 Gen_T : Entity_Id;
729 Act_T : Entity_Id);
730 -- Remove suffix 'P' from hidden primitives of Act_T to match the
731 -- visibility of primitives of Gen_T. The list of primitives to which
732 -- the suffix is removed is added to Prims_List to restore them later.
733
734 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
735 -- When compiling an instance of a child unit the parent (which is
736 -- itself an instance) is an enclosing scope that must be made
737 -- immediately visible. This procedure is also used to install the non-
738 -- generic parent of a generic child unit when compiling its body, so
739 -- that full views of types in the parent are made visible.
740
741 -- The functions Instantiate_XXX perform various legality checks and build
742 -- the declarations for instantiated generic parameters. In all of these
743 -- Formal is the entity in the generic unit, Actual is the entity of
744 -- expression in the generic associations, and Analyzed_Formal is the
745 -- formal in the generic copy, which contains the semantic information to
746 -- be used to validate the actual.
747
748 function Instantiate_Object
749 (Formal : Node_Id;
750 Actual : Node_Id;
751 Analyzed_Formal : Node_Id) return List_Id;
752
753 function Instantiate_Type
754 (Formal : Node_Id;
755 Actual : Node_Id;
756 Analyzed_Formal : Node_Id;
757 Actual_Decls : List_Id) return List_Id;
758
759 function Instantiate_Formal_Subprogram
760 (Formal : Node_Id;
761 Actual : Node_Id;
762 Analyzed_Formal : Node_Id) return Node_Id;
763
764 function Instantiate_Formal_Package
765 (Formal : Node_Id;
766 Actual : Node_Id;
767 Analyzed_Formal : Node_Id) return List_Id;
768 -- If the formal package is declared with a box, special visibility rules
769 -- apply to its formals: they are in the visible part of the package. This
770 -- is true in the declarative region of the formal package, that is to say
771 -- in the enclosing generic or instantiation. For an instantiation, the
772 -- parameters of the formal package are made visible in an explicit step.
773 -- Furthermore, if the actual has a visible USE clause, these formals must
774 -- be made potentially use-visible as well. On exit from the enclosing
775 -- instantiation, the reverse must be done.
776
777 -- For a formal package declared without a box, there are conformance rules
778 -- that apply to the actuals in the generic declaration and the actuals of
779 -- the actual package in the enclosing instantiation. The simplest way to
780 -- apply these rules is to repeat the instantiation of the formal package
781 -- in the context of the enclosing instance, and compare the generic
782 -- associations of this instantiation with those of the actual package.
783 -- This internal instantiation only needs to contain the renamings of the
784 -- formals: the visible and private declarations themselves need not be
785 -- created.
786
787 -- In Ada 2005, the formal package may be only partially parameterized.
788 -- In that case the visibility step must make visible those actuals whose
789 -- corresponding formals were given with a box. A final complication
790 -- involves inherited operations from formal derived types, which must
791 -- be visible if the type is.
792
793 function Is_In_Main_Unit (N : Node_Id) return Boolean;
794 -- Test if given node is in the main unit
795
796 procedure Load_Parent_Of_Generic
797 (N : Node_Id;
798 Spec : Node_Id;
799 Body_Optional : Boolean := False);
800 -- If the generic appears in a separate non-generic library unit, load the
801 -- corresponding body to retrieve the body of the generic. N is the node
802 -- for the generic instantiation, Spec is the generic package declaration.
803 --
804 -- Body_Optional is a flag that indicates that the body is being loaded to
805 -- ensure that temporaries are generated consistently when there are other
806 -- instances in the current declarative part that precede the one being
807 -- loaded. In that case a missing body is acceptable.
808
809 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
810 -- Within the generic part, entities in the formal package are
811 -- visible. To validate subsequent type declarations, indicate
812 -- the correspondence between the entities in the analyzed formal,
813 -- and the entities in the actual package. There are three packages
814 -- involved in the instantiation of a formal package: the parent
815 -- generic P1 which appears in the generic declaration, the fake
816 -- instantiation P2 which appears in the analyzed generic, and whose
817 -- visible entities may be used in subsequent formals, and the actual
818 -- P3 in the instance. To validate subsequent formals, me indicate
819 -- that the entities in P2 are mapped into those of P3. The mapping of
820 -- entities has to be done recursively for nested packages.
821
822 procedure Move_Freeze_Nodes
823 (Out_Of : Entity_Id;
824 After : Node_Id;
825 L : List_Id);
826 -- Freeze nodes can be generated in the analysis of a generic unit, but
827 -- will not be seen by the back-end. It is necessary to move those nodes
828 -- to the enclosing scope if they freeze an outer entity. We place them
829 -- at the end of the enclosing generic package, which is semantically
830 -- neutral.
831
832 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty);
833 -- Analyze actuals to perform name resolution. Full resolution is done
834 -- later, when the expected types are known, but names have to be captured
835 -- before installing parents of generics, that are not visible for the
836 -- actuals themselves.
837 --
838 -- If Inst is present, it is the entity of the package instance. This
839 -- entity is marked as having a limited_view actual when some actual is
840 -- a limited view. This is used to place the instance body properly.
841
842 procedure Remove_Parent (In_Body : Boolean := False);
843 -- Reverse effect after instantiation of child is complete
844
845 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
846 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
847 -- set to No_Elist.
848
849 procedure Set_Instance_Env
850 (Gen_Unit : Entity_Id;
851 Act_Unit : Entity_Id);
852 -- Save current instance on saved environment, to be used to determine
853 -- the global status of entities in nested instances. Part of Save_Env.
854 -- called after verifying that the generic unit is legal for the instance,
855 -- The procedure also examines whether the generic unit is a predefined
856 -- unit, in order to set configuration switches accordingly. As a result
857 -- the procedure must be called after analyzing and freezing the actuals.
858
859 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
860 -- Associate analyzed generic parameter with corresponding instance. Used
861 -- for semantic checks at instantiation time.
862
863 function True_Parent (N : Node_Id) return Node_Id;
864 -- For a subunit, return parent of corresponding stub, else return
865 -- parent of node.
866
867 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
868 -- Verify that an attribute that appears as the default for a formal
869 -- subprogram is a function or procedure with the correct profile.
870
871 -------------------------------------------
872 -- Data Structures for Generic Renamings --
873 -------------------------------------------
874
875 -- The map Generic_Renamings associates generic entities with their
876 -- corresponding actuals. Currently used to validate type instances. It
877 -- will eventually be used for all generic parameters to eliminate the
878 -- need for overload resolution in the instance.
879
880 type Assoc_Ptr is new Int;
881
882 Assoc_Null : constant Assoc_Ptr := -1;
883
884 type Assoc is record
885 Gen_Id : Entity_Id;
886 Act_Id : Entity_Id;
887 Next_In_HTable : Assoc_Ptr;
888 end record;
889
890 package Generic_Renamings is new Table.Table
891 (Table_Component_Type => Assoc,
892 Table_Index_Type => Assoc_Ptr,
893 Table_Low_Bound => 0,
894 Table_Initial => 10,
895 Table_Increment => 100,
896 Table_Name => "Generic_Renamings");
897
898 -- Variable to hold enclosing instantiation. When the environment is
899 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
900
901 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
902
903 -- Hash table for associations
904
905 HTable_Size : constant := 37;
906 type HTable_Range is range 0 .. HTable_Size - 1;
907
908 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
909 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
910 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
911 function Hash (F : Entity_Id) return HTable_Range;
912
913 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
914 Header_Num => HTable_Range,
915 Element => Assoc,
916 Elmt_Ptr => Assoc_Ptr,
917 Null_Ptr => Assoc_Null,
918 Set_Next => Set_Next_Assoc,
919 Next => Next_Assoc,
920 Key => Entity_Id,
921 Get_Key => Get_Gen_Id,
922 Hash => Hash,
923 Equal => "=");
924
925 Exchanged_Views : Elist_Id;
926 -- This list holds the private views that have been exchanged during
927 -- instantiation to restore the visibility of the generic declaration.
928 -- (see comments above). After instantiation, the current visibility is
929 -- reestablished by means of a traversal of this list.
930
931 Hidden_Entities : Elist_Id;
932 -- This list holds the entities of the current scope that are removed
933 -- from immediate visibility when instantiating a child unit. Their
934 -- visibility is restored in Remove_Parent.
935
936 -- Because instantiations can be recursive, the following must be saved
937 -- on entry and restored on exit from an instantiation (spec or body).
938 -- This is done by the two procedures Save_Env and Restore_Env. For
939 -- package and subprogram instantiations (but not for the body instances)
940 -- the action of Save_Env is done in two steps: Init_Env is called before
941 -- Check_Generic_Child_Unit, because setting the parent instances requires
942 -- that the visibility data structures be properly initialized. Once the
943 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
944
945 Parent_Unit_Visible : Boolean := False;
946 -- Parent_Unit_Visible is used when the generic is a child unit, and
947 -- indicates whether the ultimate parent of the generic is visible in the
948 -- instantiation environment. It is used to reset the visibility of the
949 -- parent at the end of the instantiation (see Remove_Parent).
950
951 Instance_Parent_Unit : Entity_Id := Empty;
952 -- This records the ultimate parent unit of an instance of a generic
953 -- child unit and is used in conjunction with Parent_Unit_Visible to
954 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
955
956 type Instance_Env is record
957 Instantiated_Parent : Assoc;
958 Exchanged_Views : Elist_Id;
959 Hidden_Entities : Elist_Id;
960 Current_Sem_Unit : Unit_Number_Type;
961 Parent_Unit_Visible : Boolean := False;
962 Instance_Parent_Unit : Entity_Id := Empty;
963 Switches : Config_Switches_Type;
964 end record;
965
966 package Instance_Envs is new Table.Table (
967 Table_Component_Type => Instance_Env,
968 Table_Index_Type => Int,
969 Table_Low_Bound => 0,
970 Table_Initial => 32,
971 Table_Increment => 100,
972 Table_Name => "Instance_Envs");
973
974 procedure Restore_Private_Views
975 (Pack_Id : Entity_Id;
976 Is_Package : Boolean := True);
977 -- Restore the private views of external types, and unmark the generic
978 -- renamings of actuals, so that they become compatible subtypes again.
979 -- For subprograms, Pack_Id is the package constructed to hold the
980 -- renamings.
981
982 procedure Switch_View (T : Entity_Id);
983 -- Switch the partial and full views of a type and its private
984 -- dependents (i.e. its subtypes and derived types).
985
986 ------------------------------------
987 -- Structures for Error Reporting --
988 ------------------------------------
989
990 Instantiation_Node : Node_Id;
991 -- Used by subprograms that validate instantiation of formal parameters
992 -- where there might be no actual on which to place the error message.
993 -- Also used to locate the instantiation node for generic subunits.
994
995 Instantiation_Error : exception;
996 -- When there is a semantic error in the generic parameter matching,
997 -- there is no point in continuing the instantiation, because the
998 -- number of cascaded errors is unpredictable. This exception aborts
999 -- the instantiation process altogether.
1000
1001 S_Adjustment : Sloc_Adjustment;
1002 -- Offset created for each node in an instantiation, in order to keep
1003 -- track of the source position of the instantiation in each of its nodes.
1004 -- A subsequent semantic error or warning on a construct of the instance
1005 -- points to both places: the original generic node, and the point of
1006 -- instantiation. See Sinput and Sinput.L for additional details.
1007
1008 ------------------------------------------------------------
1009 -- Data structure for keeping track when inside a Generic --
1010 ------------------------------------------------------------
1011
1012 -- The following table is used to save values of the Inside_A_Generic
1013 -- flag (see spec of Sem) when they are saved by Start_Generic.
1014
1015 package Generic_Flags is new Table.Table (
1016 Table_Component_Type => Boolean,
1017 Table_Index_Type => Int,
1018 Table_Low_Bound => 0,
1019 Table_Initial => 32,
1020 Table_Increment => 200,
1021 Table_Name => "Generic_Flags");
1022
1023 ---------------------------
1024 -- Abandon_Instantiation --
1025 ---------------------------
1026
1027 procedure Abandon_Instantiation (N : Node_Id) is
1028 begin
1029 Error_Msg_N ("\instantiation abandoned!", N);
1030 raise Instantiation_Error;
1031 end Abandon_Instantiation;
1032
1033 --------------------------------
1034 -- Add_Pending_Instantiation --
1035 --------------------------------
1036
1037 procedure Add_Pending_Instantiation (Inst : Node_Id; Act_Decl : Node_Id) is
1038 begin
1039
1040 -- Add to the instantiation node and the corresponding unit declaration
1041 -- the current values of global flags to be used when analyzing the
1042 -- instance body.
1043
1044 Pending_Instantiations.Append
1045 ((Inst_Node => Inst,
1046 Act_Decl => Act_Decl,
1047 Expander_Status => Expander_Active,
1048 Current_Sem_Unit => Current_Sem_Unit,
1049 Scope_Suppress => Scope_Suppress,
1050 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
1051 Version => Ada_Version,
1052 Version_Pragma => Ada_Version_Pragma,
1053 Warnings => Save_Warnings,
1054 SPARK_Mode => SPARK_Mode,
1055 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
1056 end Add_Pending_Instantiation;
1057
1058 ----------------------------------
1059 -- Adjust_Inherited_Pragma_Sloc --
1060 ----------------------------------
1061
1062 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
1063 begin
1064 Adjust_Instantiation_Sloc (N, S_Adjustment);
1065 end Adjust_Inherited_Pragma_Sloc;
1066
1067 --------------------------
1068 -- Analyze_Associations --
1069 --------------------------
1070
1071 function Analyze_Associations
1072 (I_Node : Node_Id;
1073 Formals : List_Id;
1074 F_Copy : List_Id) return List_Id
1075 is
1076 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1077 Assoc : constant List_Id := New_List;
1078 Default_Actuals : constant List_Id := New_List;
1079 Gen_Unit : constant Entity_Id :=
1080 Defining_Entity (Parent (F_Copy));
1081
1082 Actuals : List_Id;
1083 Actual : Node_Id;
1084 Analyzed_Formal : Node_Id;
1085 First_Named : Node_Id := Empty;
1086 Formal : Node_Id;
1087 Match : Node_Id;
1088 Named : Node_Id;
1089 Saved_Formal : Node_Id;
1090
1091 Default_Formals : constant List_Id := New_List;
1092 -- If an Others_Choice is present, some of the formals may be defaulted.
1093 -- To simplify the treatment of visibility in an instance, we introduce
1094 -- individual defaults for each such formal. These defaults are
1095 -- appended to the list of associations and replace the Others_Choice.
1096
1097 Found_Assoc : Node_Id;
1098 -- Association for the current formal being match. Empty if there are
1099 -- no remaining actuals, or if there is no named association with the
1100 -- name of the formal.
1101
1102 Is_Named_Assoc : Boolean;
1103 Num_Matched : Nat := 0;
1104 Num_Actuals : Nat := 0;
1105
1106 Others_Present : Boolean := False;
1107 Others_Choice : Node_Id := Empty;
1108 -- In Ada 2005, indicates partial parameterization of a formal
1109 -- package. As usual an other association must be last in the list.
1110
1111 procedure Check_Fixed_Point_Actual (Actual : Node_Id);
1112 -- Warn if an actual fixed-point type has user-defined arithmetic
1113 -- operations, but there is no corresponding formal in the generic,
1114 -- in which case the predefined operations will be used. This merits
1115 -- a warning because of the special semantics of fixed point ops.
1116
1117 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1118 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1119 -- cannot have a named association for it. AI05-0025 extends this rule
1120 -- to formals of formal packages by AI05-0025, and it also applies to
1121 -- box-initialized formals.
1122
1123 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1124 -- Determine whether the parameter types and the return type of Subp
1125 -- are fully defined at the point of instantiation.
1126
1127 function Matching_Actual
1128 (F : Entity_Id;
1129 A_F : Entity_Id) return Node_Id;
1130 -- Find actual that corresponds to a given a formal parameter. If the
1131 -- actuals are positional, return the next one, if any. If the actuals
1132 -- are named, scan the parameter associations to find the right one.
1133 -- A_F is the corresponding entity in the analyzed generic, which is
1134 -- placed on the selector name for ASIS use.
1135 --
1136 -- In Ada 2005, a named association may be given with a box, in which
1137 -- case Matching_Actual sets Found_Assoc to the generic association,
1138 -- but return Empty for the actual itself. In this case the code below
1139 -- creates a corresponding declaration for the formal.
1140
1141 function Partial_Parameterization return Boolean;
1142 -- Ada 2005: if no match is found for a given formal, check if the
1143 -- association for it includes a box, or whether the associations
1144 -- include an Others clause.
1145
1146 procedure Process_Default (F : Entity_Id);
1147 -- Add a copy of the declaration of generic formal F to the list of
1148 -- associations, and add an explicit box association for F if there
1149 -- is none yet, and the default comes from an Others_Choice.
1150
1151 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1152 -- Determine whether Subp renames one of the subprograms defined in the
1153 -- generated package Standard.
1154
1155 procedure Set_Analyzed_Formal;
1156 -- Find the node in the generic copy that corresponds to a given formal.
1157 -- The semantic information on this node is used to perform legality
1158 -- checks on the actuals. Because semantic analysis can introduce some
1159 -- anonymous entities or modify the declaration node itself, the
1160 -- correspondence between the two lists is not one-one. In addition to
1161 -- anonymous types, the presence a formal equality will introduce an
1162 -- implicit declaration for the corresponding inequality.
1163
1164 ----------------------------------------
1165 -- Check_Overloaded_Formal_Subprogram --
1166 ----------------------------------------
1167
1168 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1169 Temp_Formal : Entity_Id;
1170
1171 begin
1172 Temp_Formal := First (Formals);
1173 while Present (Temp_Formal) loop
1174 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1175 and then Temp_Formal /= Formal
1176 and then
1177 Chars (Defining_Unit_Name (Specification (Formal))) =
1178 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1179 then
1180 if Present (Found_Assoc) then
1181 Error_Msg_N
1182 ("named association not allowed for overloaded formal",
1183 Found_Assoc);
1184
1185 else
1186 Error_Msg_N
1187 ("named association not allowed for overloaded formal",
1188 Others_Choice);
1189 end if;
1190
1191 Abandon_Instantiation (Instantiation_Node);
1192 end if;
1193
1194 Next (Temp_Formal);
1195 end loop;
1196 end Check_Overloaded_Formal_Subprogram;
1197
1198 -------------------------------
1199 -- Check_Fixed_Point_Actual --
1200 -------------------------------
1201
1202 procedure Check_Fixed_Point_Actual (Actual : Node_Id) is
1203 Typ : constant Entity_Id := Entity (Actual);
1204 Prims : constant Elist_Id := Collect_Primitive_Operations (Typ);
1205 Elem : Elmt_Id;
1206 Formal : Node_Id;
1207
1208 begin
1209 -- Locate primitive operations of the type that are arithmetic
1210 -- operations.
1211
1212 Elem := First_Elmt (Prims);
1213 while Present (Elem) loop
1214 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
1215
1216 -- Check whether the generic unit has a formal subprogram of
1217 -- the same name. This does not check types but is good enough
1218 -- to justify a warning.
1219
1220 Formal := First_Non_Pragma (Formals);
1221 while Present (Formal) loop
1222 if Nkind (Formal) = N_Formal_Concrete_Subprogram_Declaration
1223 and then Chars (Defining_Entity (Formal)) =
1224 Chars (Node (Elem))
1225 then
1226 exit;
1227 end if;
1228
1229 Next (Formal);
1230 end loop;
1231
1232 if No (Formal) then
1233 Error_Msg_Sloc := Sloc (Node (Elem));
1234 Error_Msg_NE
1235 ("?instance does not use primitive operation&#",
1236 Actual, Node (Elem));
1237 end if;
1238 end if;
1239
1240 Next_Elmt (Elem);
1241 end loop;
1242 end Check_Fixed_Point_Actual;
1243
1244 -------------------------------
1245 -- Has_Fully_Defined_Profile --
1246 -------------------------------
1247
1248 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1249 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1250 -- Determine whethet type Typ is fully defined
1251
1252 ---------------------------
1253 -- Is_Fully_Defined_Type --
1254 ---------------------------
1255
1256 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1257 begin
1258 -- A private type without a full view is not fully defined
1259
1260 if Is_Private_Type (Typ)
1261 and then No (Full_View (Typ))
1262 then
1263 return False;
1264
1265 -- An incomplete type is never fully defined
1266
1267 elsif Is_Incomplete_Type (Typ) then
1268 return False;
1269
1270 -- All other types are fully defined
1271
1272 else
1273 return True;
1274 end if;
1275 end Is_Fully_Defined_Type;
1276
1277 -- Local declarations
1278
1279 Param : Entity_Id;
1280
1281 -- Start of processing for Has_Fully_Defined_Profile
1282
1283 begin
1284 -- Check the parameters
1285
1286 Param := First_Formal (Subp);
1287 while Present (Param) loop
1288 if not Is_Fully_Defined_Type (Etype (Param)) then
1289 return False;
1290 end if;
1291
1292 Next_Formal (Param);
1293 end loop;
1294
1295 -- Check the return type
1296
1297 return Is_Fully_Defined_Type (Etype (Subp));
1298 end Has_Fully_Defined_Profile;
1299
1300 ---------------------
1301 -- Matching_Actual --
1302 ---------------------
1303
1304 function Matching_Actual
1305 (F : Entity_Id;
1306 A_F : Entity_Id) return Node_Id
1307 is
1308 Prev : Node_Id;
1309 Act : Node_Id;
1310
1311 begin
1312 Is_Named_Assoc := False;
1313
1314 -- End of list of purely positional parameters
1315
1316 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1317 Found_Assoc := Empty;
1318 Act := Empty;
1319
1320 -- Case of positional parameter corresponding to current formal
1321
1322 elsif No (Selector_Name (Actual)) then
1323 Found_Assoc := Actual;
1324 Act := Explicit_Generic_Actual_Parameter (Actual);
1325 Num_Matched := Num_Matched + 1;
1326 Next (Actual);
1327
1328 -- Otherwise scan list of named actuals to find the one with the
1329 -- desired name. All remaining actuals have explicit names.
1330
1331 else
1332 Is_Named_Assoc := True;
1333 Found_Assoc := Empty;
1334 Act := Empty;
1335 Prev := Empty;
1336
1337 while Present (Actual) loop
1338 if Nkind (Actual) = N_Others_Choice then
1339 Found_Assoc := Empty;
1340 Act := Empty;
1341
1342 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1343 Set_Entity (Selector_Name (Actual), A_F);
1344 Set_Etype (Selector_Name (Actual), Etype (A_F));
1345 Generate_Reference (A_F, Selector_Name (Actual));
1346
1347 Found_Assoc := Actual;
1348 Act := Explicit_Generic_Actual_Parameter (Actual);
1349 Num_Matched := Num_Matched + 1;
1350 exit;
1351 end if;
1352
1353 Prev := Actual;
1354 Next (Actual);
1355 end loop;
1356
1357 -- Reset for subsequent searches. In most cases the named
1358 -- associations are in order. If they are not, we reorder them
1359 -- to avoid scanning twice the same actual. This is not just a
1360 -- question of efficiency: there may be multiple defaults with
1361 -- boxes that have the same name. In a nested instantiation we
1362 -- insert actuals for those defaults, and cannot rely on their
1363 -- names to disambiguate them.
1364
1365 if Actual = First_Named then
1366 Next (First_Named);
1367
1368 elsif Present (Actual) then
1369 Insert_Before (First_Named, Remove_Next (Prev));
1370 end if;
1371
1372 Actual := First_Named;
1373 end if;
1374
1375 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1376 Set_Used_As_Generic_Actual (Entity (Act));
1377 end if;
1378
1379 return Act;
1380 end Matching_Actual;
1381
1382 ------------------------------
1383 -- Partial_Parameterization --
1384 ------------------------------
1385
1386 function Partial_Parameterization return Boolean is
1387 begin
1388 return Others_Present
1389 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1390 end Partial_Parameterization;
1391
1392 ---------------------
1393 -- Process_Default --
1394 ---------------------
1395
1396 procedure Process_Default (F : Entity_Id) is
1397 Loc : constant Source_Ptr := Sloc (I_Node);
1398 F_Id : constant Entity_Id := Defining_Entity (F);
1399 Decl : Node_Id;
1400 Default : Node_Id;
1401 Id : Entity_Id;
1402
1403 begin
1404 -- Append copy of formal declaration to associations, and create new
1405 -- defining identifier for it.
1406
1407 Decl := New_Copy_Tree (F);
1408 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1409
1410 if Nkind (F) in N_Formal_Subprogram_Declaration then
1411 Set_Defining_Unit_Name (Specification (Decl), Id);
1412
1413 else
1414 Set_Defining_Identifier (Decl, Id);
1415 end if;
1416
1417 Append (Decl, Assoc);
1418
1419 if No (Found_Assoc) then
1420 Default :=
1421 Make_Generic_Association (Loc,
1422 Selector_Name =>
1423 New_Occurrence_Of (Id, Loc),
1424 Explicit_Generic_Actual_Parameter => Empty);
1425 Set_Box_Present (Default);
1426 Append (Default, Default_Formals);
1427 end if;
1428 end Process_Default;
1429
1430 ---------------------------------
1431 -- Renames_Standard_Subprogram --
1432 ---------------------------------
1433
1434 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1435 Id : Entity_Id;
1436
1437 begin
1438 Id := Alias (Subp);
1439 while Present (Id) loop
1440 if Scope (Id) = Standard_Standard then
1441 return True;
1442 end if;
1443
1444 Id := Alias (Id);
1445 end loop;
1446
1447 return False;
1448 end Renames_Standard_Subprogram;
1449
1450 -------------------------
1451 -- Set_Analyzed_Formal --
1452 -------------------------
1453
1454 procedure Set_Analyzed_Formal is
1455 Kind : Node_Kind;
1456
1457 begin
1458 while Present (Analyzed_Formal) loop
1459 Kind := Nkind (Analyzed_Formal);
1460
1461 case Nkind (Formal) is
1462 when N_Formal_Subprogram_Declaration =>
1463 exit when Kind in N_Formal_Subprogram_Declaration
1464 and then
1465 Chars
1466 (Defining_Unit_Name (Specification (Formal))) =
1467 Chars
1468 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1469
1470 when N_Formal_Package_Declaration =>
1471 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1472 N_Generic_Package_Declaration,
1473 N_Package_Declaration);
1474
1475 when N_Use_Package_Clause
1476 | N_Use_Type_Clause
1477 =>
1478 exit;
1479
1480 when others =>
1481
1482 -- Skip freeze nodes, and nodes inserted to replace
1483 -- unrecognized pragmas.
1484
1485 exit when
1486 Kind not in N_Formal_Subprogram_Declaration
1487 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1488 N_Freeze_Entity,
1489 N_Null_Statement,
1490 N_Itype_Reference)
1491 and then Chars (Defining_Identifier (Formal)) =
1492 Chars (Defining_Identifier (Analyzed_Formal));
1493 end case;
1494
1495 Next (Analyzed_Formal);
1496 end loop;
1497 end Set_Analyzed_Formal;
1498
1499 -- Start of processing for Analyze_Associations
1500
1501 begin
1502 Actuals := Generic_Associations (I_Node);
1503
1504 if Present (Actuals) then
1505
1506 -- Check for an Others choice, indicating a partial parameterization
1507 -- for a formal package.
1508
1509 Actual := First (Actuals);
1510 while Present (Actual) loop
1511 if Nkind (Actual) = N_Others_Choice then
1512 Others_Present := True;
1513 Others_Choice := Actual;
1514
1515 if Present (Next (Actual)) then
1516 Error_Msg_N ("others must be last association", Actual);
1517 end if;
1518
1519 -- This subprogram is used both for formal packages and for
1520 -- instantiations. For the latter, associations must all be
1521 -- explicit.
1522
1523 if Nkind (I_Node) /= N_Formal_Package_Declaration
1524 and then Comes_From_Source (I_Node)
1525 then
1526 Error_Msg_N
1527 ("others association not allowed in an instance",
1528 Actual);
1529 end if;
1530
1531 -- In any case, nothing to do after the others association
1532
1533 exit;
1534
1535 elsif Box_Present (Actual)
1536 and then Comes_From_Source (I_Node)
1537 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1538 then
1539 Error_Msg_N
1540 ("box association not allowed in an instance", Actual);
1541 end if;
1542
1543 Next (Actual);
1544 end loop;
1545
1546 -- If named associations are present, save first named association
1547 -- (it may of course be Empty) to facilitate subsequent name search.
1548
1549 First_Named := First (Actuals);
1550 while Present (First_Named)
1551 and then Nkind (First_Named) /= N_Others_Choice
1552 and then No (Selector_Name (First_Named))
1553 loop
1554 Num_Actuals := Num_Actuals + 1;
1555 Next (First_Named);
1556 end loop;
1557 end if;
1558
1559 Named := First_Named;
1560 while Present (Named) loop
1561 if Nkind (Named) /= N_Others_Choice
1562 and then No (Selector_Name (Named))
1563 then
1564 Error_Msg_N ("invalid positional actual after named one", Named);
1565 Abandon_Instantiation (Named);
1566 end if;
1567
1568 -- A named association may lack an actual parameter, if it was
1569 -- introduced for a default subprogram that turns out to be local
1570 -- to the outer instantiation. If it has a box association it must
1571 -- correspond to some formal in the generic.
1572
1573 if Nkind (Named) /= N_Others_Choice
1574 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1575 or else Box_Present (Named))
1576 then
1577 Num_Actuals := Num_Actuals + 1;
1578 end if;
1579
1580 Next (Named);
1581 end loop;
1582
1583 if Present (Formals) then
1584 Formal := First_Non_Pragma (Formals);
1585 Analyzed_Formal := First_Non_Pragma (F_Copy);
1586
1587 if Present (Actuals) then
1588 Actual := First (Actuals);
1589
1590 -- All formals should have default values
1591
1592 else
1593 Actual := Empty;
1594 end if;
1595
1596 while Present (Formal) loop
1597 Set_Analyzed_Formal;
1598 Saved_Formal := Next_Non_Pragma (Formal);
1599
1600 case Nkind (Formal) is
1601 when N_Formal_Object_Declaration =>
1602 Match :=
1603 Matching_Actual
1604 (Defining_Identifier (Formal),
1605 Defining_Identifier (Analyzed_Formal));
1606
1607 if No (Match) and then Partial_Parameterization then
1608 Process_Default (Formal);
1609
1610 else
1611 Append_List
1612 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1613 Assoc);
1614
1615 -- For a defaulted in_parameter, create an entry in the
1616 -- the list of defaulted actuals, for GNATProve use. Do
1617 -- not included these defaults for an instance nested
1618 -- within a generic, because the defaults are also used
1619 -- in the analysis of the enclosing generic, and only
1620 -- defaulted subprograms are relevant there.
1621
1622 if No (Match) and then not Inside_A_Generic then
1623 Append_To (Default_Actuals,
1624 Make_Generic_Association (Sloc (I_Node),
1625 Selector_Name =>
1626 New_Occurrence_Of
1627 (Defining_Identifier (Formal), Sloc (I_Node)),
1628 Explicit_Generic_Actual_Parameter =>
1629 New_Copy_Tree (Default_Expression (Formal))));
1630 end if;
1631 end if;
1632
1633 -- If the object is a call to an expression function, this
1634 -- is a freezing point for it.
1635
1636 if Is_Entity_Name (Match)
1637 and then Present (Entity (Match))
1638 and then Nkind
1639 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1640 = N_Expression_Function
1641 then
1642 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1643 end if;
1644
1645 when N_Formal_Type_Declaration =>
1646 Match :=
1647 Matching_Actual
1648 (Defining_Identifier (Formal),
1649 Defining_Identifier (Analyzed_Formal));
1650
1651 if No (Match) then
1652 if Partial_Parameterization then
1653 Process_Default (Formal);
1654
1655 else
1656 Error_Msg_Sloc := Sloc (Gen_Unit);
1657 Error_Msg_NE
1658 ("missing actual&",
1659 Instantiation_Node, Defining_Identifier (Formal));
1660 Error_Msg_NE
1661 ("\in instantiation of & declared#",
1662 Instantiation_Node, Gen_Unit);
1663 Abandon_Instantiation (Instantiation_Node);
1664 end if;
1665
1666 else
1667 Analyze (Match);
1668 Append_List
1669 (Instantiate_Type
1670 (Formal, Match, Analyzed_Formal, Assoc),
1671 Assoc);
1672
1673 if Is_Fixed_Point_Type (Entity (Match)) then
1674 Check_Fixed_Point_Actual (Match);
1675 end if;
1676
1677 -- An instantiation is a freeze point for the actuals,
1678 -- unless this is a rewritten formal package, or the
1679 -- formal is an Ada 2012 formal incomplete type.
1680
1681 if Nkind (I_Node) = N_Formal_Package_Declaration
1682 or else
1683 (Ada_Version >= Ada_2012
1684 and then
1685 Ekind (Defining_Identifier (Analyzed_Formal)) =
1686 E_Incomplete_Type)
1687 then
1688 null;
1689
1690 else
1691 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1692 end if;
1693 end if;
1694
1695 -- A remote access-to-class-wide type is not a legal actual
1696 -- for a generic formal of an access type (E.2.2(17/2)).
1697 -- In GNAT an exception to this rule is introduced when
1698 -- the formal is marked as remote using implementation
1699 -- defined aspect/pragma Remote_Access_Type. In that case
1700 -- the actual must be remote as well.
1701
1702 -- If the current instantiation is the construction of a
1703 -- local copy for a formal package the actuals may be
1704 -- defaulted, and there is no matching actual to check.
1705
1706 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1707 and then
1708 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1709 N_Access_To_Object_Definition
1710 and then Present (Match)
1711 then
1712 declare
1713 Formal_Ent : constant Entity_Id :=
1714 Defining_Identifier (Analyzed_Formal);
1715 begin
1716 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1717 = Is_Remote_Types (Formal_Ent)
1718 then
1719 -- Remoteness of formal and actual match
1720
1721 null;
1722
1723 elsif Is_Remote_Types (Formal_Ent) then
1724
1725 -- Remote formal, non-remote actual
1726
1727 Error_Msg_NE
1728 ("actual for& must be remote", Match, Formal_Ent);
1729
1730 else
1731 -- Non-remote formal, remote actual
1732
1733 Error_Msg_NE
1734 ("actual for& may not be remote",
1735 Match, Formal_Ent);
1736 end if;
1737 end;
1738 end if;
1739
1740 when N_Formal_Subprogram_Declaration =>
1741 Match :=
1742 Matching_Actual
1743 (Defining_Unit_Name (Specification (Formal)),
1744 Defining_Unit_Name (Specification (Analyzed_Formal)));
1745
1746 -- If the formal subprogram has the same name as another
1747 -- formal subprogram of the generic, then a named
1748 -- association is illegal (12.3(9)). Exclude named
1749 -- associations that are generated for a nested instance.
1750
1751 if Present (Match)
1752 and then Is_Named_Assoc
1753 and then Comes_From_Source (Found_Assoc)
1754 then
1755 Check_Overloaded_Formal_Subprogram (Formal);
1756 end if;
1757
1758 -- If there is no corresponding actual, this may be case
1759 -- of partial parameterization, or else the formal has a
1760 -- default or a box.
1761
1762 if No (Match) and then Partial_Parameterization then
1763 Process_Default (Formal);
1764
1765 if Nkind (I_Node) = N_Formal_Package_Declaration then
1766 Check_Overloaded_Formal_Subprogram (Formal);
1767 end if;
1768
1769 else
1770 Append_To (Assoc,
1771 Instantiate_Formal_Subprogram
1772 (Formal, Match, Analyzed_Formal));
1773
1774 -- An instantiation is a freeze point for the actuals,
1775 -- unless this is a rewritten formal package.
1776
1777 if Nkind (I_Node) /= N_Formal_Package_Declaration
1778 and then Nkind (Match) = N_Identifier
1779 and then Is_Subprogram (Entity (Match))
1780
1781 -- The actual subprogram may rename a routine defined
1782 -- in Standard. Avoid freezing such renamings because
1783 -- subprograms coming from Standard cannot be frozen.
1784
1785 and then
1786 not Renames_Standard_Subprogram (Entity (Match))
1787
1788 -- If the actual subprogram comes from a different
1789 -- unit, it is already frozen, either by a body in
1790 -- that unit or by the end of the declarative part
1791 -- of the unit. This check avoids the freezing of
1792 -- subprograms defined in Standard which are used
1793 -- as generic actuals.
1794
1795 and then In_Same_Code_Unit (Entity (Match), I_Node)
1796 and then Has_Fully_Defined_Profile (Entity (Match))
1797 then
1798 -- Mark the subprogram as having a delayed freeze
1799 -- since this may be an out-of-order action.
1800
1801 Set_Has_Delayed_Freeze (Entity (Match));
1802 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1803 end if;
1804 end if;
1805
1806 -- If this is a nested generic, preserve default for later
1807 -- instantiations. We do this as well for GNATProve use,
1808 -- so that the list of generic associations is complete.
1809
1810 if No (Match) and then Box_Present (Formal) then
1811 declare
1812 Subp : constant Entity_Id :=
1813 Defining_Unit_Name (Specification (Last (Assoc)));
1814
1815 begin
1816 Append_To (Default_Actuals,
1817 Make_Generic_Association (Sloc (I_Node),
1818 Selector_Name =>
1819 New_Occurrence_Of (Subp, Sloc (I_Node)),
1820 Explicit_Generic_Actual_Parameter =>
1821 New_Occurrence_Of (Subp, Sloc (I_Node))));
1822 end;
1823 end if;
1824
1825 when N_Formal_Package_Declaration =>
1826 Match :=
1827 Matching_Actual
1828 (Defining_Identifier (Formal),
1829 Defining_Identifier (Original_Node (Analyzed_Formal)));
1830
1831 if No (Match) then
1832 if Partial_Parameterization then
1833 Process_Default (Formal);
1834
1835 else
1836 Error_Msg_Sloc := Sloc (Gen_Unit);
1837 Error_Msg_NE
1838 ("missing actual&",
1839 Instantiation_Node, Defining_Identifier (Formal));
1840 Error_Msg_NE
1841 ("\in instantiation of & declared#",
1842 Instantiation_Node, Gen_Unit);
1843
1844 Abandon_Instantiation (Instantiation_Node);
1845 end if;
1846
1847 else
1848 Analyze (Match);
1849 Append_List
1850 (Instantiate_Formal_Package
1851 (Formal, Match, Analyzed_Formal),
1852 Assoc);
1853 end if;
1854
1855 -- For use type and use package appearing in the generic part,
1856 -- we have already copied them, so we can just move them where
1857 -- they belong (we mustn't recopy them since this would mess up
1858 -- the Sloc values).
1859
1860 when N_Use_Package_Clause
1861 | N_Use_Type_Clause
1862 =>
1863 if Nkind (Original_Node (I_Node)) =
1864 N_Formal_Package_Declaration
1865 then
1866 Append (New_Copy_Tree (Formal), Assoc);
1867 else
1868 Remove (Formal);
1869 Append (Formal, Assoc);
1870 end if;
1871
1872 when others =>
1873 raise Program_Error;
1874 end case;
1875
1876 Formal := Saved_Formal;
1877 Next_Non_Pragma (Analyzed_Formal);
1878 end loop;
1879
1880 if Num_Actuals > Num_Matched then
1881 Error_Msg_Sloc := Sloc (Gen_Unit);
1882
1883 if Present (Selector_Name (Actual)) then
1884 Error_Msg_NE
1885 ("unmatched actual &", Actual, Selector_Name (Actual));
1886 Error_Msg_NE
1887 ("\in instantiation of & declared#", Actual, Gen_Unit);
1888 else
1889 Error_Msg_NE
1890 ("unmatched actual in instantiation of & declared#",
1891 Actual, Gen_Unit);
1892 end if;
1893 end if;
1894
1895 elsif Present (Actuals) then
1896 Error_Msg_N
1897 ("too many actuals in generic instantiation", Instantiation_Node);
1898 end if;
1899
1900 -- An instantiation freezes all generic actuals. The only exceptions
1901 -- to this are incomplete types and subprograms which are not fully
1902 -- defined at the point of instantiation.
1903
1904 declare
1905 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1906 begin
1907 while Present (Elmt) loop
1908 Freeze_Before (I_Node, Node (Elmt));
1909 Next_Elmt (Elmt);
1910 end loop;
1911 end;
1912
1913 -- If there are default subprograms, normalize the tree by adding
1914 -- explicit associations for them. This is required if the instance
1915 -- appears within a generic.
1916
1917 if not Is_Empty_List (Default_Actuals) then
1918 declare
1919 Default : Node_Id;
1920
1921 begin
1922 Default := First (Default_Actuals);
1923 while Present (Default) loop
1924 Mark_Rewrite_Insertion (Default);
1925 Next (Default);
1926 end loop;
1927
1928 if No (Actuals) then
1929 Set_Generic_Associations (I_Node, Default_Actuals);
1930 else
1931 Append_List_To (Actuals, Default_Actuals);
1932 end if;
1933 end;
1934 end if;
1935
1936 -- If this is a formal package, normalize the parameter list by adding
1937 -- explicit box associations for the formals that are covered by an
1938 -- Others_Choice.
1939
1940 if not Is_Empty_List (Default_Formals) then
1941 Append_List (Default_Formals, Formals);
1942 end if;
1943
1944 return Assoc;
1945 end Analyze_Associations;
1946
1947 -------------------------------
1948 -- Analyze_Formal_Array_Type --
1949 -------------------------------
1950
1951 procedure Analyze_Formal_Array_Type
1952 (T : in out Entity_Id;
1953 Def : Node_Id)
1954 is
1955 DSS : Node_Id;
1956
1957 begin
1958 -- Treated like a non-generic array declaration, with additional
1959 -- semantic checks.
1960
1961 Enter_Name (T);
1962
1963 if Nkind (Def) = N_Constrained_Array_Definition then
1964 DSS := First (Discrete_Subtype_Definitions (Def));
1965 while Present (DSS) loop
1966 if Nkind_In (DSS, N_Subtype_Indication,
1967 N_Range,
1968 N_Attribute_Reference)
1969 then
1970 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1971 end if;
1972
1973 Next (DSS);
1974 end loop;
1975 end if;
1976
1977 Array_Type_Declaration (T, Def);
1978 Set_Is_Generic_Type (Base_Type (T));
1979
1980 if Ekind (Component_Type (T)) = E_Incomplete_Type
1981 and then No (Full_View (Component_Type (T)))
1982 then
1983 Error_Msg_N ("premature usage of incomplete type", Def);
1984
1985 -- Check that range constraint is not allowed on the component type
1986 -- of a generic formal array type (AARM 12.5.3(3))
1987
1988 elsif Is_Internal (Component_Type (T))
1989 and then Present (Subtype_Indication (Component_Definition (Def)))
1990 and then Nkind (Original_Node
1991 (Subtype_Indication (Component_Definition (Def)))) =
1992 N_Subtype_Indication
1993 then
1994 Error_Msg_N
1995 ("in a formal, a subtype indication can only be "
1996 & "a subtype mark (RM 12.5.3(3))",
1997 Subtype_Indication (Component_Definition (Def)));
1998 end if;
1999
2000 end Analyze_Formal_Array_Type;
2001
2002 ---------------------------------------------
2003 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2004 ---------------------------------------------
2005
2006 -- As for other generic types, we create a valid type representation with
2007 -- legal but arbitrary attributes, whose values are never considered
2008 -- static. For all scalar types we introduce an anonymous base type, with
2009 -- the same attributes. We choose the corresponding integer type to be
2010 -- Standard_Integer.
2011 -- Here and in other similar routines, the Sloc of the generated internal
2012 -- type must be the same as the sloc of the defining identifier of the
2013 -- formal type declaration, to provide proper source navigation.
2014
2015 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2016 (T : Entity_Id;
2017 Def : Node_Id)
2018 is
2019 Loc : constant Source_Ptr := Sloc (Def);
2020
2021 Base : constant Entity_Id :=
2022 New_Internal_Entity
2023 (E_Decimal_Fixed_Point_Type,
2024 Current_Scope,
2025 Sloc (Defining_Identifier (Parent (Def))), 'G');
2026
2027 Int_Base : constant Entity_Id := Standard_Integer;
2028 Delta_Val : constant Ureal := Ureal_1;
2029 Digs_Val : constant Uint := Uint_6;
2030
2031 function Make_Dummy_Bound return Node_Id;
2032 -- Return a properly typed universal real literal to use as a bound
2033
2034 ----------------------
2035 -- Make_Dummy_Bound --
2036 ----------------------
2037
2038 function Make_Dummy_Bound return Node_Id is
2039 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2040 begin
2041 Set_Etype (Bound, Universal_Real);
2042 return Bound;
2043 end Make_Dummy_Bound;
2044
2045 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2046
2047 begin
2048 Enter_Name (T);
2049
2050 Set_Etype (Base, Base);
2051 Set_Size_Info (Base, Int_Base);
2052 Set_RM_Size (Base, RM_Size (Int_Base));
2053 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2054 Set_Digits_Value (Base, Digs_Val);
2055 Set_Delta_Value (Base, Delta_Val);
2056 Set_Small_Value (Base, Delta_Val);
2057 Set_Scalar_Range (Base,
2058 Make_Range (Loc,
2059 Low_Bound => Make_Dummy_Bound,
2060 High_Bound => Make_Dummy_Bound));
2061
2062 Set_Is_Generic_Type (Base);
2063 Set_Parent (Base, Parent (Def));
2064
2065 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2066 Set_Etype (T, Base);
2067 Set_Size_Info (T, Int_Base);
2068 Set_RM_Size (T, RM_Size (Int_Base));
2069 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2070 Set_Digits_Value (T, Digs_Val);
2071 Set_Delta_Value (T, Delta_Val);
2072 Set_Small_Value (T, Delta_Val);
2073 Set_Scalar_Range (T, Scalar_Range (Base));
2074 Set_Is_Constrained (T);
2075
2076 Check_Restriction (No_Fixed_Point, Def);
2077 end Analyze_Formal_Decimal_Fixed_Point_Type;
2078
2079 -------------------------------------------
2080 -- Analyze_Formal_Derived_Interface_Type --
2081 -------------------------------------------
2082
2083 procedure Analyze_Formal_Derived_Interface_Type
2084 (N : Node_Id;
2085 T : Entity_Id;
2086 Def : Node_Id)
2087 is
2088 Loc : constant Source_Ptr := Sloc (Def);
2089
2090 begin
2091 -- Rewrite as a type declaration of a derived type. This ensures that
2092 -- the interface list and primitive operations are properly captured.
2093
2094 Rewrite (N,
2095 Make_Full_Type_Declaration (Loc,
2096 Defining_Identifier => T,
2097 Type_Definition => Def));
2098 Analyze (N);
2099 Set_Is_Generic_Type (T);
2100 end Analyze_Formal_Derived_Interface_Type;
2101
2102 ---------------------------------
2103 -- Analyze_Formal_Derived_Type --
2104 ---------------------------------
2105
2106 procedure Analyze_Formal_Derived_Type
2107 (N : Node_Id;
2108 T : Entity_Id;
2109 Def : Node_Id)
2110 is
2111 Loc : constant Source_Ptr := Sloc (Def);
2112 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2113 New_N : Node_Id;
2114
2115 begin
2116 Set_Is_Generic_Type (T);
2117
2118 if Private_Present (Def) then
2119 New_N :=
2120 Make_Private_Extension_Declaration (Loc,
2121 Defining_Identifier => T,
2122 Discriminant_Specifications => Discriminant_Specifications (N),
2123 Unknown_Discriminants_Present => Unk_Disc,
2124 Subtype_Indication => Subtype_Mark (Def),
2125 Interface_List => Interface_List (Def));
2126
2127 Set_Abstract_Present (New_N, Abstract_Present (Def));
2128 Set_Limited_Present (New_N, Limited_Present (Def));
2129 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2130
2131 else
2132 New_N :=
2133 Make_Full_Type_Declaration (Loc,
2134 Defining_Identifier => T,
2135 Discriminant_Specifications =>
2136 Discriminant_Specifications (Parent (T)),
2137 Type_Definition =>
2138 Make_Derived_Type_Definition (Loc,
2139 Subtype_Indication => Subtype_Mark (Def)));
2140
2141 Set_Abstract_Present
2142 (Type_Definition (New_N), Abstract_Present (Def));
2143 Set_Limited_Present
2144 (Type_Definition (New_N), Limited_Present (Def));
2145 end if;
2146
2147 Rewrite (N, New_N);
2148 Analyze (N);
2149
2150 if Unk_Disc then
2151 if not Is_Composite_Type (T) then
2152 Error_Msg_N
2153 ("unknown discriminants not allowed for elementary types", N);
2154 else
2155 Set_Has_Unknown_Discriminants (T);
2156 Set_Is_Constrained (T, False);
2157 end if;
2158 end if;
2159
2160 -- If the parent type has a known size, so does the formal, which makes
2161 -- legal representation clauses that involve the formal.
2162
2163 Set_Size_Known_At_Compile_Time
2164 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2165 end Analyze_Formal_Derived_Type;
2166
2167 ----------------------------------
2168 -- Analyze_Formal_Discrete_Type --
2169 ----------------------------------
2170
2171 -- The operations defined for a discrete types are those of an enumeration
2172 -- type. The size is set to an arbitrary value, for use in analyzing the
2173 -- generic unit.
2174
2175 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2176 Loc : constant Source_Ptr := Sloc (Def);
2177 Lo : Node_Id;
2178 Hi : Node_Id;
2179
2180 Base : constant Entity_Id :=
2181 New_Internal_Entity
2182 (E_Floating_Point_Type, Current_Scope,
2183 Sloc (Defining_Identifier (Parent (Def))), 'G');
2184
2185 begin
2186 Enter_Name (T);
2187 Set_Ekind (T, E_Enumeration_Subtype);
2188 Set_Etype (T, Base);
2189 Init_Size (T, 8);
2190 Init_Alignment (T);
2191 Set_Is_Generic_Type (T);
2192 Set_Is_Constrained (T);
2193
2194 -- For semantic analysis, the bounds of the type must be set to some
2195 -- non-static value. The simplest is to create attribute nodes for those
2196 -- bounds, that refer to the type itself. These bounds are never
2197 -- analyzed but serve as place-holders.
2198
2199 Lo :=
2200 Make_Attribute_Reference (Loc,
2201 Attribute_Name => Name_First,
2202 Prefix => New_Occurrence_Of (T, Loc));
2203 Set_Etype (Lo, T);
2204
2205 Hi :=
2206 Make_Attribute_Reference (Loc,
2207 Attribute_Name => Name_Last,
2208 Prefix => New_Occurrence_Of (T, Loc));
2209 Set_Etype (Hi, T);
2210
2211 Set_Scalar_Range (T,
2212 Make_Range (Loc,
2213 Low_Bound => Lo,
2214 High_Bound => Hi));
2215
2216 Set_Ekind (Base, E_Enumeration_Type);
2217 Set_Etype (Base, Base);
2218 Init_Size (Base, 8);
2219 Init_Alignment (Base);
2220 Set_Is_Generic_Type (Base);
2221 Set_Scalar_Range (Base, Scalar_Range (T));
2222 Set_Parent (Base, Parent (Def));
2223 end Analyze_Formal_Discrete_Type;
2224
2225 ----------------------------------
2226 -- Analyze_Formal_Floating_Type --
2227 ---------------------------------
2228
2229 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2230 Base : constant Entity_Id :=
2231 New_Internal_Entity
2232 (E_Floating_Point_Type, Current_Scope,
2233 Sloc (Defining_Identifier (Parent (Def))), 'G');
2234
2235 begin
2236 -- The various semantic attributes are taken from the predefined type
2237 -- Float, just so that all of them are initialized. Their values are
2238 -- never used because no constant folding or expansion takes place in
2239 -- the generic itself.
2240
2241 Enter_Name (T);
2242 Set_Ekind (T, E_Floating_Point_Subtype);
2243 Set_Etype (T, Base);
2244 Set_Size_Info (T, (Standard_Float));
2245 Set_RM_Size (T, RM_Size (Standard_Float));
2246 Set_Digits_Value (T, Digits_Value (Standard_Float));
2247 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2248 Set_Is_Constrained (T);
2249
2250 Set_Is_Generic_Type (Base);
2251 Set_Etype (Base, Base);
2252 Set_Size_Info (Base, (Standard_Float));
2253 Set_RM_Size (Base, RM_Size (Standard_Float));
2254 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2255 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2256 Set_Parent (Base, Parent (Def));
2257
2258 Check_Restriction (No_Floating_Point, Def);
2259 end Analyze_Formal_Floating_Type;
2260
2261 -----------------------------------
2262 -- Analyze_Formal_Interface_Type;--
2263 -----------------------------------
2264
2265 procedure Analyze_Formal_Interface_Type
2266 (N : Node_Id;
2267 T : Entity_Id;
2268 Def : Node_Id)
2269 is
2270 Loc : constant Source_Ptr := Sloc (N);
2271 New_N : Node_Id;
2272
2273 begin
2274 New_N :=
2275 Make_Full_Type_Declaration (Loc,
2276 Defining_Identifier => T,
2277 Type_Definition => Def);
2278
2279 Rewrite (N, New_N);
2280 Analyze (N);
2281 Set_Is_Generic_Type (T);
2282 end Analyze_Formal_Interface_Type;
2283
2284 ---------------------------------
2285 -- Analyze_Formal_Modular_Type --
2286 ---------------------------------
2287
2288 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2289 begin
2290 -- Apart from their entity kind, generic modular types are treated like
2291 -- signed integer types, and have the same attributes.
2292
2293 Analyze_Formal_Signed_Integer_Type (T, Def);
2294 Set_Ekind (T, E_Modular_Integer_Subtype);
2295 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2296
2297 end Analyze_Formal_Modular_Type;
2298
2299 ---------------------------------------
2300 -- Analyze_Formal_Object_Declaration --
2301 ---------------------------------------
2302
2303 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2304 E : constant Node_Id := Default_Expression (N);
2305 Id : constant Node_Id := Defining_Identifier (N);
2306 K : Entity_Kind;
2307 T : Node_Id;
2308
2309 begin
2310 Enter_Name (Id);
2311
2312 -- Determine the mode of the formal object
2313
2314 if Out_Present (N) then
2315 K := E_Generic_In_Out_Parameter;
2316
2317 if not In_Present (N) then
2318 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2319 end if;
2320
2321 else
2322 K := E_Generic_In_Parameter;
2323 end if;
2324
2325 if Present (Subtype_Mark (N)) then
2326 Find_Type (Subtype_Mark (N));
2327 T := Entity (Subtype_Mark (N));
2328
2329 -- Verify that there is no redundant null exclusion
2330
2331 if Null_Exclusion_Present (N) then
2332 if not Is_Access_Type (T) then
2333 Error_Msg_N
2334 ("null exclusion can only apply to an access type", N);
2335
2336 elsif Can_Never_Be_Null (T) then
2337 Error_Msg_NE
2338 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2339 end if;
2340 end if;
2341
2342 -- Ada 2005 (AI-423): Formal object with an access definition
2343
2344 else
2345 Check_Access_Definition (N);
2346 T := Access_Definition
2347 (Related_Nod => N,
2348 N => Access_Definition (N));
2349 end if;
2350
2351 if Ekind (T) = E_Incomplete_Type then
2352 declare
2353 Error_Node : Node_Id;
2354
2355 begin
2356 if Present (Subtype_Mark (N)) then
2357 Error_Node := Subtype_Mark (N);
2358 else
2359 Check_Access_Definition (N);
2360 Error_Node := Access_Definition (N);
2361 end if;
2362
2363 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2364 end;
2365 end if;
2366
2367 if K = E_Generic_In_Parameter then
2368
2369 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2370
2371 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2372 Error_Msg_N
2373 ("generic formal of mode IN must not be of limited type", N);
2374 Explain_Limited_Type (T, N);
2375 end if;
2376
2377 if Is_Abstract_Type (T) then
2378 Error_Msg_N
2379 ("generic formal of mode IN must not be of abstract type", N);
2380 end if;
2381
2382 if Present (E) then
2383 Preanalyze_Spec_Expression (E, T);
2384
2385 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2386 Error_Msg_N
2387 ("initialization not allowed for limited types", E);
2388 Explain_Limited_Type (T, E);
2389 end if;
2390 end if;
2391
2392 Set_Ekind (Id, K);
2393 Set_Etype (Id, T);
2394
2395 -- Case of generic IN OUT parameter
2396
2397 else
2398 -- If the formal has an unconstrained type, construct its actual
2399 -- subtype, as is done for subprogram formals. In this fashion, all
2400 -- its uses can refer to specific bounds.
2401
2402 Set_Ekind (Id, K);
2403 Set_Etype (Id, T);
2404
2405 if (Is_Array_Type (T) and then not Is_Constrained (T))
2406 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2407 then
2408 declare
2409 Non_Freezing_Ref : constant Node_Id :=
2410 New_Occurrence_Of (Id, Sloc (Id));
2411 Decl : Node_Id;
2412
2413 begin
2414 -- Make sure the actual subtype doesn't generate bogus freezing
2415
2416 Set_Must_Not_Freeze (Non_Freezing_Ref);
2417 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2418 Insert_Before_And_Analyze (N, Decl);
2419 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2420 end;
2421 else
2422 Set_Actual_Subtype (Id, T);
2423 end if;
2424
2425 if Present (E) then
2426 Error_Msg_N
2427 ("initialization not allowed for `IN OUT` formals", N);
2428 end if;
2429 end if;
2430
2431 if Has_Aspects (N) then
2432 Analyze_Aspect_Specifications (N, Id);
2433 end if;
2434 end Analyze_Formal_Object_Declaration;
2435
2436 ----------------------------------------------
2437 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2438 ----------------------------------------------
2439
2440 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2441 (T : Entity_Id;
2442 Def : Node_Id)
2443 is
2444 Loc : constant Source_Ptr := Sloc (Def);
2445 Base : constant Entity_Id :=
2446 New_Internal_Entity
2447 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2448 Sloc (Defining_Identifier (Parent (Def))), 'G');
2449
2450 begin
2451 -- The semantic attributes are set for completeness only, their values
2452 -- will never be used, since all properties of the type are non-static.
2453
2454 Enter_Name (T);
2455 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2456 Set_Etype (T, Base);
2457 Set_Size_Info (T, Standard_Integer);
2458 Set_RM_Size (T, RM_Size (Standard_Integer));
2459 Set_Small_Value (T, Ureal_1);
2460 Set_Delta_Value (T, Ureal_1);
2461 Set_Scalar_Range (T,
2462 Make_Range (Loc,
2463 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2464 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2465 Set_Is_Constrained (T);
2466
2467 Set_Is_Generic_Type (Base);
2468 Set_Etype (Base, Base);
2469 Set_Size_Info (Base, Standard_Integer);
2470 Set_RM_Size (Base, RM_Size (Standard_Integer));
2471 Set_Small_Value (Base, Ureal_1);
2472 Set_Delta_Value (Base, Ureal_1);
2473 Set_Scalar_Range (Base, Scalar_Range (T));
2474 Set_Parent (Base, Parent (Def));
2475
2476 Check_Restriction (No_Fixed_Point, Def);
2477 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2478
2479 ----------------------------------------
2480 -- Analyze_Formal_Package_Declaration --
2481 ----------------------------------------
2482
2483 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2484 Gen_Id : constant Node_Id := Name (N);
2485 Loc : constant Source_Ptr := Sloc (N);
2486 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2487 Formal : Entity_Id;
2488 Gen_Decl : Node_Id;
2489 Gen_Unit : Entity_Id;
2490 Renaming : Node_Id;
2491
2492 Vis_Prims_List : Elist_Id := No_Elist;
2493 -- List of primitives made temporarily visible in the instantiation
2494 -- to match the visibility of the formal type.
2495
2496 function Build_Local_Package return Node_Id;
2497 -- The formal package is rewritten so that its parameters are replaced
2498 -- with corresponding declarations. For parameters with bona fide
2499 -- associations these declarations are created by Analyze_Associations
2500 -- as for a regular instantiation. For boxed parameters, we preserve
2501 -- the formal declarations and analyze them, in order to introduce
2502 -- entities of the right kind in the environment of the formal.
2503
2504 -------------------------
2505 -- Build_Local_Package --
2506 -------------------------
2507
2508 function Build_Local_Package return Node_Id is
2509 Decls : List_Id;
2510 Pack_Decl : Node_Id;
2511
2512 begin
2513 -- Within the formal, the name of the generic package is a renaming
2514 -- of the formal (as for a regular instantiation).
2515
2516 Pack_Decl :=
2517 Make_Package_Declaration (Loc,
2518 Specification =>
2519 Copy_Generic_Node
2520 (Specification (Original_Node (Gen_Decl)),
2521 Empty, Instantiating => True));
2522
2523 Renaming :=
2524 Make_Package_Renaming_Declaration (Loc,
2525 Defining_Unit_Name =>
2526 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2527 Name => New_Occurrence_Of (Formal, Loc));
2528
2529 if Nkind (Gen_Id) = N_Identifier
2530 and then Chars (Gen_Id) = Chars (Pack_Id)
2531 then
2532 Error_Msg_NE
2533 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2534 end if;
2535
2536 -- If the formal is declared with a box, or with an others choice,
2537 -- create corresponding declarations for all entities in the formal
2538 -- part, so that names with the proper types are available in the
2539 -- specification of the formal package.
2540
2541 -- On the other hand, if there are no associations, then all the
2542 -- formals must have defaults, and this will be checked by the
2543 -- call to Analyze_Associations.
2544
2545 if Box_Present (N)
2546 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2547 then
2548 declare
2549 Formal_Decl : Node_Id;
2550
2551 begin
2552 -- TBA : for a formal package, need to recurse ???
2553
2554 Decls := New_List;
2555 Formal_Decl :=
2556 First
2557 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2558 while Present (Formal_Decl) loop
2559 Append_To
2560 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2561 Next (Formal_Decl);
2562 end loop;
2563 end;
2564
2565 -- If generic associations are present, use Analyze_Associations to
2566 -- create the proper renaming declarations.
2567
2568 else
2569 declare
2570 Act_Tree : constant Node_Id :=
2571 Copy_Generic_Node
2572 (Original_Node (Gen_Decl), Empty,
2573 Instantiating => True);
2574
2575 begin
2576 Generic_Renamings.Set_Last (0);
2577 Generic_Renamings_HTable.Reset;
2578 Instantiation_Node := N;
2579
2580 Decls :=
2581 Analyze_Associations
2582 (I_Node => Original_Node (N),
2583 Formals => Generic_Formal_Declarations (Act_Tree),
2584 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2585
2586 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2587 end;
2588 end if;
2589
2590 Append (Renaming, To => Decls);
2591
2592 -- Add generated declarations ahead of local declarations in
2593 -- the package.
2594
2595 if No (Visible_Declarations (Specification (Pack_Decl))) then
2596 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2597 else
2598 Insert_List_Before
2599 (First (Visible_Declarations (Specification (Pack_Decl))),
2600 Decls);
2601 end if;
2602
2603 return Pack_Decl;
2604 end Build_Local_Package;
2605
2606 -- Local variables
2607
2608 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
2609 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2610
2611 Associations : Boolean := True;
2612 New_N : Node_Id;
2613 Parent_Installed : Boolean := False;
2614 Parent_Instance : Entity_Id;
2615 Renaming_In_Par : Entity_Id;
2616
2617 -- Start of processing for Analyze_Formal_Package_Declaration
2618
2619 begin
2620 Check_Text_IO_Special_Unit (Gen_Id);
2621
2622 Init_Env;
2623 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2624 Gen_Unit := Entity (Gen_Id);
2625
2626 -- Check for a formal package that is a package renaming
2627
2628 if Present (Renamed_Object (Gen_Unit)) then
2629
2630 -- Indicate that unit is used, before replacing it with renamed
2631 -- entity for use below.
2632
2633 if In_Extended_Main_Source_Unit (N) then
2634 Set_Is_Instantiated (Gen_Unit);
2635 Generate_Reference (Gen_Unit, N);
2636 end if;
2637
2638 Gen_Unit := Renamed_Object (Gen_Unit);
2639 end if;
2640
2641 if Ekind (Gen_Unit) /= E_Generic_Package then
2642 Error_Msg_N ("expect generic package name", Gen_Id);
2643 Restore_Env;
2644 goto Leave;
2645
2646 elsif Gen_Unit = Current_Scope then
2647 Error_Msg_N
2648 ("generic package cannot be used as a formal package of itself",
2649 Gen_Id);
2650 Restore_Env;
2651 goto Leave;
2652
2653 elsif In_Open_Scopes (Gen_Unit) then
2654 if Is_Compilation_Unit (Gen_Unit)
2655 and then Is_Child_Unit (Current_Scope)
2656 then
2657 -- Special-case the error when the formal is a parent, and
2658 -- continue analysis to minimize cascaded errors.
2659
2660 Error_Msg_N
2661 ("generic parent cannot be used as formal package of a child "
2662 & "unit", Gen_Id);
2663
2664 else
2665 Error_Msg_N
2666 ("generic package cannot be used as a formal package within "
2667 & "itself", Gen_Id);
2668 Restore_Env;
2669 goto Leave;
2670 end if;
2671 end if;
2672
2673 -- Check that name of formal package does not hide name of generic,
2674 -- or its leading prefix. This check must be done separately because
2675 -- the name of the generic has already been analyzed.
2676
2677 declare
2678 Gen_Name : Entity_Id;
2679
2680 begin
2681 Gen_Name := Gen_Id;
2682 while Nkind (Gen_Name) = N_Expanded_Name loop
2683 Gen_Name := Prefix (Gen_Name);
2684 end loop;
2685
2686 if Chars (Gen_Name) = Chars (Pack_Id) then
2687 Error_Msg_NE
2688 ("& is hidden within declaration of formal package",
2689 Gen_Id, Gen_Name);
2690 end if;
2691 end;
2692
2693 if Box_Present (N)
2694 or else No (Generic_Associations (N))
2695 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2696 then
2697 Associations := False;
2698 end if;
2699
2700 -- If there are no generic associations, the generic parameters appear
2701 -- as local entities and are instantiated like them. We copy the generic
2702 -- package declaration as if it were an instantiation, and analyze it
2703 -- like a regular package, except that we treat the formals as
2704 -- additional visible components.
2705
2706 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2707
2708 if In_Extended_Main_Source_Unit (N) then
2709 Set_Is_Instantiated (Gen_Unit);
2710 Generate_Reference (Gen_Unit, N);
2711 end if;
2712
2713 Formal := New_Copy (Pack_Id);
2714 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
2715
2716 -- Make local generic without formals. The formals will be replaced with
2717 -- internal declarations.
2718
2719 begin
2720 New_N := Build_Local_Package;
2721
2722 -- If there are errors in the parameter list, Analyze_Associations
2723 -- raises Instantiation_Error. Patch the declaration to prevent further
2724 -- exception propagation.
2725
2726 exception
2727 when Instantiation_Error =>
2728 Enter_Name (Formal);
2729 Set_Ekind (Formal, E_Variable);
2730 Set_Etype (Formal, Any_Type);
2731 Restore_Hidden_Primitives (Vis_Prims_List);
2732
2733 if Parent_Installed then
2734 Remove_Parent;
2735 end if;
2736
2737 goto Leave;
2738 end;
2739
2740 Rewrite (N, New_N);
2741 Set_Defining_Unit_Name (Specification (New_N), Formal);
2742 Set_Generic_Parent (Specification (N), Gen_Unit);
2743 Set_Instance_Env (Gen_Unit, Formal);
2744 Set_Is_Generic_Instance (Formal);
2745
2746 Enter_Name (Formal);
2747 Set_Ekind (Formal, E_Package);
2748 Set_Etype (Formal, Standard_Void_Type);
2749 Set_Inner_Instances (Formal, New_Elmt_List);
2750 Push_Scope (Formal);
2751
2752 -- Manually set the SPARK_Mode from the context because the package
2753 -- declaration is never analyzed.
2754
2755 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
2756 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
2757 Set_SPARK_Pragma_Inherited (Formal);
2758 Set_SPARK_Aux_Pragma_Inherited (Formal);
2759
2760 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2761
2762 -- Similarly, we have to make the name of the formal visible in the
2763 -- parent instance, to resolve properly fully qualified names that
2764 -- may appear in the generic unit. The parent instance has been
2765 -- placed on the scope stack ahead of the current scope.
2766
2767 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2768
2769 Renaming_In_Par :=
2770 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2771 Set_Ekind (Renaming_In_Par, E_Package);
2772 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2773 Set_Scope (Renaming_In_Par, Parent_Instance);
2774 Set_Parent (Renaming_In_Par, Parent (Formal));
2775 Set_Renamed_Object (Renaming_In_Par, Formal);
2776 Append_Entity (Renaming_In_Par, Parent_Instance);
2777 end if;
2778
2779 -- A formal package declaration behaves as a package instantiation with
2780 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
2781 -- missing, set the global flag which signals Analyze_Pragma to ingnore
2782 -- all SPARK_Mode pragmas within the generic_package_name.
2783
2784 if SPARK_Mode /= On then
2785 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
2786
2787 -- Mark the formal spec in case the body is instantiated at a later
2788 -- pass. This preserves the original context in effect for the body.
2789
2790 Set_Ignore_SPARK_Mode_Pragmas (Formal);
2791 end if;
2792
2793 Analyze (Specification (N));
2794
2795 -- The formals for which associations are provided are not visible
2796 -- outside of the formal package. The others are still declared by a
2797 -- formal parameter declaration.
2798
2799 -- If there are no associations, the only local entity to hide is the
2800 -- generated package renaming itself.
2801
2802 declare
2803 E : Entity_Id;
2804
2805 begin
2806 E := First_Entity (Formal);
2807 while Present (E) loop
2808 if Associations and then not Is_Generic_Formal (E) then
2809 Set_Is_Hidden (E);
2810 end if;
2811
2812 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
2813 Set_Is_Hidden (E);
2814 exit;
2815 end if;
2816
2817 Next_Entity (E);
2818 end loop;
2819 end;
2820
2821 End_Package_Scope (Formal);
2822 Restore_Hidden_Primitives (Vis_Prims_List);
2823
2824 if Parent_Installed then
2825 Remove_Parent;
2826 end if;
2827
2828 Restore_Env;
2829
2830 -- Inside the generic unit, the formal package is a regular package, but
2831 -- no body is needed for it. Note that after instantiation, the defining
2832 -- unit name we need is in the new tree and not in the original (see
2833 -- Package_Instantiation). A generic formal package is an instance, and
2834 -- can be used as an actual for an inner instance.
2835
2836 Set_Has_Completion (Formal, True);
2837
2838 -- Add semantic information to the original defining identifier for ASIS
2839 -- use.
2840
2841 Set_Ekind (Pack_Id, E_Package);
2842 Set_Etype (Pack_Id, Standard_Void_Type);
2843 Set_Scope (Pack_Id, Scope (Formal));
2844 Set_Has_Completion (Pack_Id, True);
2845
2846 <<Leave>>
2847 if Has_Aspects (N) then
2848 Analyze_Aspect_Specifications (N, Pack_Id);
2849 end if;
2850
2851 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
2852 end Analyze_Formal_Package_Declaration;
2853
2854 ---------------------------------
2855 -- Analyze_Formal_Private_Type --
2856 ---------------------------------
2857
2858 procedure Analyze_Formal_Private_Type
2859 (N : Node_Id;
2860 T : Entity_Id;
2861 Def : Node_Id)
2862 is
2863 begin
2864 New_Private_Type (N, T, Def);
2865
2866 -- Set the size to an arbitrary but legal value
2867
2868 Set_Size_Info (T, Standard_Integer);
2869 Set_RM_Size (T, RM_Size (Standard_Integer));
2870 end Analyze_Formal_Private_Type;
2871
2872 ------------------------------------
2873 -- Analyze_Formal_Incomplete_Type --
2874 ------------------------------------
2875
2876 procedure Analyze_Formal_Incomplete_Type
2877 (T : Entity_Id;
2878 Def : Node_Id)
2879 is
2880 begin
2881 Enter_Name (T);
2882 Set_Ekind (T, E_Incomplete_Type);
2883 Set_Etype (T, T);
2884 Set_Private_Dependents (T, New_Elmt_List);
2885
2886 if Tagged_Present (Def) then
2887 Set_Is_Tagged_Type (T);
2888 Make_Class_Wide_Type (T);
2889 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2890 end if;
2891 end Analyze_Formal_Incomplete_Type;
2892
2893 ----------------------------------------
2894 -- Analyze_Formal_Signed_Integer_Type --
2895 ----------------------------------------
2896
2897 procedure Analyze_Formal_Signed_Integer_Type
2898 (T : Entity_Id;
2899 Def : Node_Id)
2900 is
2901 Base : constant Entity_Id :=
2902 New_Internal_Entity
2903 (E_Signed_Integer_Type,
2904 Current_Scope,
2905 Sloc (Defining_Identifier (Parent (Def))), 'G');
2906
2907 begin
2908 Enter_Name (T);
2909
2910 Set_Ekind (T, E_Signed_Integer_Subtype);
2911 Set_Etype (T, Base);
2912 Set_Size_Info (T, Standard_Integer);
2913 Set_RM_Size (T, RM_Size (Standard_Integer));
2914 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2915 Set_Is_Constrained (T);
2916
2917 Set_Is_Generic_Type (Base);
2918 Set_Size_Info (Base, Standard_Integer);
2919 Set_RM_Size (Base, RM_Size (Standard_Integer));
2920 Set_Etype (Base, Base);
2921 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2922 Set_Parent (Base, Parent (Def));
2923 end Analyze_Formal_Signed_Integer_Type;
2924
2925 -------------------------------------------
2926 -- Analyze_Formal_Subprogram_Declaration --
2927 -------------------------------------------
2928
2929 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2930 Spec : constant Node_Id := Specification (N);
2931 Def : constant Node_Id := Default_Name (N);
2932 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2933 Subp : Entity_Id;
2934
2935 begin
2936 if Nam = Error then
2937 return;
2938 end if;
2939
2940 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2941 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2942 goto Leave;
2943 end if;
2944
2945 Analyze_Subprogram_Declaration (N);
2946 Set_Is_Formal_Subprogram (Nam);
2947 Set_Has_Completion (Nam);
2948
2949 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2950 Set_Is_Abstract_Subprogram (Nam);
2951
2952 Set_Is_Dispatching_Operation (Nam);
2953
2954 -- A formal abstract procedure cannot have a null default
2955 -- (RM 12.6(4.1/2)).
2956
2957 if Nkind (Spec) = N_Procedure_Specification
2958 and then Null_Present (Spec)
2959 then
2960 Error_Msg_N
2961 ("a formal abstract subprogram cannot default to null", Spec);
2962 end if;
2963
2964 declare
2965 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2966 begin
2967 if No (Ctrl_Type) then
2968 Error_Msg_N
2969 ("abstract formal subprogram must have a controlling type",
2970 N);
2971
2972 elsif Ada_Version >= Ada_2012
2973 and then Is_Incomplete_Type (Ctrl_Type)
2974 then
2975 Error_Msg_NE
2976 ("controlling type of abstract formal subprogram cannot "
2977 & "be incomplete type", N, Ctrl_Type);
2978
2979 else
2980 Check_Controlling_Formals (Ctrl_Type, Nam);
2981 end if;
2982 end;
2983 end if;
2984
2985 -- Default name is resolved at the point of instantiation
2986
2987 if Box_Present (N) then
2988 null;
2989
2990 -- Else default is bound at the point of generic declaration
2991
2992 elsif Present (Def) then
2993 if Nkind (Def) = N_Operator_Symbol then
2994 Find_Direct_Name (Def);
2995
2996 elsif Nkind (Def) /= N_Attribute_Reference then
2997 Analyze (Def);
2998
2999 else
3000 -- For an attribute reference, analyze the prefix and verify
3001 -- that it has the proper profile for the subprogram.
3002
3003 Analyze (Prefix (Def));
3004 Valid_Default_Attribute (Nam, Def);
3005 goto Leave;
3006 end if;
3007
3008 -- Default name may be overloaded, in which case the interpretation
3009 -- with the correct profile must be selected, as for a renaming.
3010 -- If the definition is an indexed component, it must denote a
3011 -- member of an entry family. If it is a selected component, it
3012 -- can be a protected operation.
3013
3014 if Etype (Def) = Any_Type then
3015 goto Leave;
3016
3017 elsif Nkind (Def) = N_Selected_Component then
3018 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3019 Error_Msg_N ("expect valid subprogram name as default", Def);
3020 end if;
3021
3022 elsif Nkind (Def) = N_Indexed_Component then
3023 if Is_Entity_Name (Prefix (Def)) then
3024 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3025 Error_Msg_N ("expect valid subprogram name as default", Def);
3026 end if;
3027
3028 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3029 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3030 E_Entry_Family
3031 then
3032 Error_Msg_N ("expect valid subprogram name as default", Def);
3033 end if;
3034
3035 else
3036 Error_Msg_N ("expect valid subprogram name as default", Def);
3037 goto Leave;
3038 end if;
3039
3040 elsif Nkind (Def) = N_Character_Literal then
3041
3042 -- Needs some type checks: subprogram should be parameterless???
3043
3044 Resolve (Def, (Etype (Nam)));
3045
3046 elsif not Is_Entity_Name (Def)
3047 or else not Is_Overloadable (Entity (Def))
3048 then
3049 Error_Msg_N ("expect valid subprogram name as default", Def);
3050 goto Leave;
3051
3052 elsif not Is_Overloaded (Def) then
3053 Subp := Entity (Def);
3054
3055 if Subp = Nam then
3056 Error_Msg_N ("premature usage of formal subprogram", Def);
3057
3058 elsif not Entity_Matches_Spec (Subp, Nam) then
3059 Error_Msg_N ("no visible entity matches specification", Def);
3060 end if;
3061
3062 -- More than one interpretation, so disambiguate as for a renaming
3063
3064 else
3065 declare
3066 I : Interp_Index;
3067 I1 : Interp_Index := 0;
3068 It : Interp;
3069 It1 : Interp;
3070
3071 begin
3072 Subp := Any_Id;
3073 Get_First_Interp (Def, I, It);
3074 while Present (It.Nam) loop
3075 if Entity_Matches_Spec (It.Nam, Nam) then
3076 if Subp /= Any_Id then
3077 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3078
3079 if It1 = No_Interp then
3080 Error_Msg_N ("ambiguous default subprogram", Def);
3081 else
3082 Subp := It1.Nam;
3083 end if;
3084
3085 exit;
3086
3087 else
3088 I1 := I;
3089 Subp := It.Nam;
3090 end if;
3091 end if;
3092
3093 Get_Next_Interp (I, It);
3094 end loop;
3095 end;
3096
3097 if Subp /= Any_Id then
3098
3099 -- Subprogram found, generate reference to it
3100
3101 Set_Entity (Def, Subp);
3102 Generate_Reference (Subp, Def);
3103
3104 if Subp = Nam then
3105 Error_Msg_N ("premature usage of formal subprogram", Def);
3106
3107 elsif Ekind (Subp) /= E_Operator then
3108 Check_Mode_Conformant (Subp, Nam);
3109 end if;
3110
3111 else
3112 Error_Msg_N ("no visible subprogram matches specification", N);
3113 end if;
3114 end if;
3115 end if;
3116
3117 <<Leave>>
3118 if Has_Aspects (N) then
3119 Analyze_Aspect_Specifications (N, Nam);
3120 end if;
3121
3122 end Analyze_Formal_Subprogram_Declaration;
3123
3124 -------------------------------------
3125 -- Analyze_Formal_Type_Declaration --
3126 -------------------------------------
3127
3128 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3129 Def : constant Node_Id := Formal_Type_Definition (N);
3130 T : Entity_Id;
3131
3132 begin
3133 T := Defining_Identifier (N);
3134
3135 if Present (Discriminant_Specifications (N))
3136 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3137 then
3138 Error_Msg_N
3139 ("discriminants not allowed for this formal type", T);
3140 end if;
3141
3142 -- Enter the new name, and branch to specific routine
3143
3144 case Nkind (Def) is
3145 when N_Formal_Private_Type_Definition =>
3146 Analyze_Formal_Private_Type (N, T, Def);
3147
3148 when N_Formal_Derived_Type_Definition =>
3149 Analyze_Formal_Derived_Type (N, T, Def);
3150
3151 when N_Formal_Incomplete_Type_Definition =>
3152 Analyze_Formal_Incomplete_Type (T, Def);
3153
3154 when N_Formal_Discrete_Type_Definition =>
3155 Analyze_Formal_Discrete_Type (T, Def);
3156
3157 when N_Formal_Signed_Integer_Type_Definition =>
3158 Analyze_Formal_Signed_Integer_Type (T, Def);
3159
3160 when N_Formal_Modular_Type_Definition =>
3161 Analyze_Formal_Modular_Type (T, Def);
3162
3163 when N_Formal_Floating_Point_Definition =>
3164 Analyze_Formal_Floating_Type (T, Def);
3165
3166 when N_Formal_Ordinary_Fixed_Point_Definition =>
3167 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3168
3169 when N_Formal_Decimal_Fixed_Point_Definition =>
3170 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3171
3172 when N_Array_Type_Definition =>
3173 Analyze_Formal_Array_Type (T, Def);
3174
3175 when N_Access_Function_Definition
3176 | N_Access_Procedure_Definition
3177 | N_Access_To_Object_Definition
3178 =>
3179 Analyze_Generic_Access_Type (T, Def);
3180
3181 -- Ada 2005: a interface declaration is encoded as an abstract
3182 -- record declaration or a abstract type derivation.
3183
3184 when N_Record_Definition =>
3185 Analyze_Formal_Interface_Type (N, T, Def);
3186
3187 when N_Derived_Type_Definition =>
3188 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3189
3190 when N_Error =>
3191 null;
3192
3193 when others =>
3194 raise Program_Error;
3195 end case;
3196
3197 Set_Is_Generic_Type (T);
3198
3199 if Has_Aspects (N) then
3200 Analyze_Aspect_Specifications (N, T);
3201 end if;
3202 end Analyze_Formal_Type_Declaration;
3203
3204 ------------------------------------
3205 -- Analyze_Function_Instantiation --
3206 ------------------------------------
3207
3208 procedure Analyze_Function_Instantiation (N : Node_Id) is
3209 begin
3210 Analyze_Subprogram_Instantiation (N, E_Function);
3211 end Analyze_Function_Instantiation;
3212
3213 ---------------------------------
3214 -- Analyze_Generic_Access_Type --
3215 ---------------------------------
3216
3217 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3218 begin
3219 Enter_Name (T);
3220
3221 if Nkind (Def) = N_Access_To_Object_Definition then
3222 Access_Type_Declaration (T, Def);
3223
3224 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3225 and then No (Full_View (Designated_Type (T)))
3226 and then not Is_Generic_Type (Designated_Type (T))
3227 then
3228 Error_Msg_N ("premature usage of incomplete type", Def);
3229
3230 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3231 Error_Msg_N
3232 ("only a subtype mark is allowed in a formal", Def);
3233 end if;
3234
3235 else
3236 Access_Subprogram_Declaration (T, Def);
3237 end if;
3238 end Analyze_Generic_Access_Type;
3239
3240 ---------------------------------
3241 -- Analyze_Generic_Formal_Part --
3242 ---------------------------------
3243
3244 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3245 Gen_Parm_Decl : Node_Id;
3246
3247 begin
3248 -- The generic formals are processed in the scope of the generic unit,
3249 -- where they are immediately visible. The scope is installed by the
3250 -- caller.
3251
3252 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3253 while Present (Gen_Parm_Decl) loop
3254 Analyze (Gen_Parm_Decl);
3255 Next (Gen_Parm_Decl);
3256 end loop;
3257
3258 Generate_Reference_To_Generic_Formals (Current_Scope);
3259 end Analyze_Generic_Formal_Part;
3260
3261 ------------------------------------------
3262 -- Analyze_Generic_Package_Declaration --
3263 ------------------------------------------
3264
3265 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3266 Loc : constant Source_Ptr := Sloc (N);
3267 Decls : constant List_Id :=
3268 Visible_Declarations (Specification (N));
3269 Decl : Node_Id;
3270 Id : Entity_Id;
3271 New_N : Node_Id;
3272 Renaming : Node_Id;
3273 Save_Parent : Node_Id;
3274
3275 begin
3276 Check_SPARK_05_Restriction ("generic is not allowed", N);
3277
3278 -- We introduce a renaming of the enclosing package, to have a usable
3279 -- entity as the prefix of an expanded name for a local entity of the
3280 -- form Par.P.Q, where P is the generic package. This is because a local
3281 -- entity named P may hide it, so that the usual visibility rules in
3282 -- the instance will not resolve properly.
3283
3284 Renaming :=
3285 Make_Package_Renaming_Declaration (Loc,
3286 Defining_Unit_Name =>
3287 Make_Defining_Identifier (Loc,
3288 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3289 Name =>
3290 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3291
3292 if Present (Decls) then
3293 Decl := First (Decls);
3294 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
3295 Next (Decl);
3296 end loop;
3297
3298 if Present (Decl) then
3299 Insert_Before (Decl, Renaming);
3300 else
3301 Append (Renaming, Visible_Declarations (Specification (N)));
3302 end if;
3303
3304 else
3305 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3306 end if;
3307
3308 -- Create copy of generic unit, and save for instantiation. If the unit
3309 -- is a child unit, do not copy the specifications for the parent, which
3310 -- are not part of the generic tree.
3311
3312 Save_Parent := Parent_Spec (N);
3313 Set_Parent_Spec (N, Empty);
3314
3315 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3316 Set_Parent_Spec (New_N, Save_Parent);
3317 Rewrite (N, New_N);
3318
3319 -- Once the contents of the generic copy and the template are swapped,
3320 -- do the same for their respective aspect specifications.
3321
3322 Exchange_Aspects (N, New_N);
3323
3324 -- Collect all contract-related source pragmas found within the template
3325 -- and attach them to the contract of the package spec. This contract is
3326 -- used in the capture of global references within annotations.
3327
3328 Create_Generic_Contract (N);
3329
3330 Id := Defining_Entity (N);
3331 Generate_Definition (Id);
3332
3333 -- Expansion is not applied to generic units
3334
3335 Start_Generic;
3336
3337 Enter_Name (Id);
3338 Set_Ekind (Id, E_Generic_Package);
3339 Set_Etype (Id, Standard_Void_Type);
3340
3341 -- Analyze aspects now, so that generated pragmas appear in the
3342 -- declarations before building and analyzing the generic copy.
3343
3344 if Has_Aspects (N) then
3345 Analyze_Aspect_Specifications (N, Id);
3346 end if;
3347
3348 Push_Scope (Id);
3349 Enter_Generic_Scope (Id);
3350 Set_Inner_Instances (Id, New_Elmt_List);
3351
3352 Set_Categorization_From_Pragmas (N);
3353 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3354
3355 -- Link the declaration of the generic homonym in the generic copy to
3356 -- the package it renames, so that it is always resolved properly.
3357
3358 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3359 Set_Entity (Associated_Node (Name (Renaming)), Id);
3360
3361 -- For a library unit, we have reconstructed the entity for the unit,
3362 -- and must reset it in the library tables.
3363
3364 if Nkind (Parent (N)) = N_Compilation_Unit then
3365 Set_Cunit_Entity (Current_Sem_Unit, Id);
3366 end if;
3367
3368 Analyze_Generic_Formal_Part (N);
3369
3370 -- After processing the generic formals, analysis proceeds as for a
3371 -- non-generic package.
3372
3373 Analyze (Specification (N));
3374
3375 Validate_Categorization_Dependency (N, Id);
3376
3377 End_Generic;
3378
3379 End_Package_Scope (Id);
3380 Exit_Generic_Scope (Id);
3381
3382 -- If the generic appears within a package unit, the body of that unit
3383 -- has to be present for instantiation and inlining.
3384
3385 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3386 Set_Body_Needed_For_Inlining
3387 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3388 end if;
3389
3390 if Nkind (Parent (N)) /= N_Compilation_Unit then
3391 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3392 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3393 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3394
3395 else
3396 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3397 Validate_RT_RAT_Component (N);
3398
3399 -- If this is a spec without a body, check that generic parameters
3400 -- are referenced.
3401
3402 if not Body_Required (Parent (N)) then
3403 Check_References (Id);
3404 end if;
3405 end if;
3406
3407 -- If there is a specified storage pool in the context, create an
3408 -- aspect on the package declaration, so that it is used in any
3409 -- instance that does not override it.
3410
3411 if Present (Default_Pool) then
3412 declare
3413 ASN : Node_Id;
3414
3415 begin
3416 ASN :=
3417 Make_Aspect_Specification (Loc,
3418 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3419 Expression => New_Copy (Default_Pool));
3420
3421 if No (Aspect_Specifications (Specification (N))) then
3422 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3423 else
3424 Append (ASN, Aspect_Specifications (Specification (N)));
3425 end if;
3426 end;
3427 end if;
3428 end Analyze_Generic_Package_Declaration;
3429
3430 --------------------------------------------
3431 -- Analyze_Generic_Subprogram_Declaration --
3432 --------------------------------------------
3433
3434 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3435 Formals : List_Id;
3436 Id : Entity_Id;
3437 New_N : Node_Id;
3438 Result_Type : Entity_Id;
3439 Save_Parent : Node_Id;
3440 Spec : Node_Id;
3441 Typ : Entity_Id;
3442
3443 begin
3444 Check_SPARK_05_Restriction ("generic is not allowed", N);
3445
3446 -- Create copy of generic unit, and save for instantiation. If the unit
3447 -- is a child unit, do not copy the specifications for the parent, which
3448 -- are not part of the generic tree.
3449
3450 Save_Parent := Parent_Spec (N);
3451 Set_Parent_Spec (N, Empty);
3452
3453 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3454 Set_Parent_Spec (New_N, Save_Parent);
3455 Rewrite (N, New_N);
3456
3457 -- Once the contents of the generic copy and the template are swapped,
3458 -- do the same for their respective aspect specifications.
3459
3460 Exchange_Aspects (N, New_N);
3461
3462 -- Collect all contract-related source pragmas found within the template
3463 -- and attach them to the contract of the subprogram spec. This contract
3464 -- is used in the capture of global references within annotations.
3465
3466 Create_Generic_Contract (N);
3467
3468 Spec := Specification (N);
3469 Id := Defining_Entity (Spec);
3470 Generate_Definition (Id);
3471
3472 if Nkind (Id) = N_Defining_Operator_Symbol then
3473 Error_Msg_N
3474 ("operator symbol not allowed for generic subprogram", Id);
3475 end if;
3476
3477 Start_Generic;
3478
3479 Enter_Name (Id);
3480 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3481
3482 -- Analyze the aspects of the generic copy to ensure that all generated
3483 -- pragmas (if any) perform their semantic effects.
3484
3485 if Has_Aspects (N) then
3486 Analyze_Aspect_Specifications (N, Id);
3487 end if;
3488
3489 Push_Scope (Id);
3490 Enter_Generic_Scope (Id);
3491 Set_Inner_Instances (Id, New_Elmt_List);
3492 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3493
3494 Analyze_Generic_Formal_Part (N);
3495
3496 Formals := Parameter_Specifications (Spec);
3497
3498 if Nkind (Spec) = N_Function_Specification then
3499 Set_Ekind (Id, E_Generic_Function);
3500 else
3501 Set_Ekind (Id, E_Generic_Procedure);
3502 end if;
3503
3504 if Present (Formals) then
3505 Process_Formals (Formals, Spec);
3506 end if;
3507
3508 if Nkind (Spec) = N_Function_Specification then
3509 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3510 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3511 Set_Etype (Id, Result_Type);
3512
3513 -- Check restriction imposed by AI05-073: a generic function
3514 -- cannot return an abstract type or an access to such.
3515
3516 -- This is a binding interpretation should it apply to earlier
3517 -- versions of Ada as well as Ada 2012???
3518
3519 if Is_Abstract_Type (Designated_Type (Result_Type))
3520 and then Ada_Version >= Ada_2012
3521 then
3522 Error_Msg_N
3523 ("generic function cannot have an access result "
3524 & "that designates an abstract type", Spec);
3525 end if;
3526
3527 else
3528 Find_Type (Result_Definition (Spec));
3529 Typ := Entity (Result_Definition (Spec));
3530
3531 if Is_Abstract_Type (Typ)
3532 and then Ada_Version >= Ada_2012
3533 then
3534 Error_Msg_N
3535 ("generic function cannot have abstract result type", Spec);
3536 end if;
3537
3538 -- If a null exclusion is imposed on the result type, then create
3539 -- a null-excluding itype (an access subtype) and use it as the
3540 -- function's Etype.
3541
3542 if Is_Access_Type (Typ)
3543 and then Null_Exclusion_Present (Spec)
3544 then
3545 Set_Etype (Id,
3546 Create_Null_Excluding_Itype
3547 (T => Typ,
3548 Related_Nod => Spec,
3549 Scope_Id => Defining_Unit_Name (Spec)));
3550 else
3551 Set_Etype (Id, Typ);
3552 end if;
3553 end if;
3554
3555 else
3556 Set_Etype (Id, Standard_Void_Type);
3557 end if;
3558
3559 -- For a library unit, we have reconstructed the entity for the unit,
3560 -- and must reset it in the library tables. We also make sure that
3561 -- Body_Required is set properly in the original compilation unit node.
3562
3563 if Nkind (Parent (N)) = N_Compilation_Unit then
3564 Set_Cunit_Entity (Current_Sem_Unit, Id);
3565 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3566 end if;
3567
3568 -- If the generic appears within a package unit, the body of that unit
3569 -- has to be present for instantiation and inlining.
3570
3571 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
3572 and then Unit_Requires_Body (Id)
3573 then
3574 Set_Body_Needed_For_Inlining
3575 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3576 end if;
3577
3578 Set_Categorization_From_Pragmas (N);
3579 Validate_Categorization_Dependency (N, Id);
3580
3581 -- Capture all global references that occur within the profile of the
3582 -- generic subprogram. Aspects are not part of this processing because
3583 -- they must be delayed. If processed now, Save_Global_References will
3584 -- destroy the Associated_Node links and prevent the capture of global
3585 -- references when the contract of the generic subprogram is analyzed.
3586
3587 Save_Global_References (Original_Node (N));
3588
3589 End_Generic;
3590 End_Scope;
3591 Exit_Generic_Scope (Id);
3592 Generate_Reference_To_Formals (Id);
3593
3594 List_Inherited_Pre_Post_Aspects (Id);
3595 end Analyze_Generic_Subprogram_Declaration;
3596
3597 -----------------------------------
3598 -- Analyze_Package_Instantiation --
3599 -----------------------------------
3600
3601 -- WARNING: This routine manages Ghost regions. Return statements must be
3602 -- replaced by gotos which jump to the end of the routine and restore the
3603 -- Ghost mode.
3604
3605 procedure Analyze_Package_Instantiation (N : Node_Id) is
3606 Loc : constant Source_Ptr := Sloc (N);
3607 Gen_Id : constant Node_Id := Name (N);
3608
3609 Act_Decl : Node_Id;
3610 Act_Decl_Name : Node_Id;
3611 Act_Decl_Id : Entity_Id;
3612 Act_Spec : Node_Id;
3613 Act_Tree : Node_Id;
3614
3615 Gen_Decl : Node_Id;
3616 Gen_Spec : Node_Id;
3617 Gen_Unit : Entity_Id;
3618
3619 Is_Actual_Pack : constant Boolean :=
3620 Is_Internal (Defining_Entity (N));
3621
3622 Env_Installed : Boolean := False;
3623 Parent_Installed : Boolean := False;
3624 Renaming_List : List_Id;
3625 Unit_Renaming : Node_Id;
3626 Needs_Body : Boolean;
3627 Inline_Now : Boolean := False;
3628 Has_Inline_Always : Boolean := False;
3629
3630 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
3631 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
3632
3633 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
3634 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
3635 -- Save the SPARK_Mode-related data for restore on exit
3636
3637 Save_Style_Check : constant Boolean := Style_Check;
3638 -- Save style check mode for restore on exit
3639
3640 procedure Delay_Descriptors (E : Entity_Id);
3641 -- Delay generation of subprogram descriptors for given entity
3642
3643 function Might_Inline_Subp return Boolean;
3644 -- If inlining is active and the generic contains inlined subprograms,
3645 -- we instantiate the body. This may cause superfluous instantiations,
3646 -- but it is simpler than detecting the need for the body at the point
3647 -- of inlining, when the context of the instance is not available.
3648
3649 -----------------------
3650 -- Delay_Descriptors --
3651 -----------------------
3652
3653 procedure Delay_Descriptors (E : Entity_Id) is
3654 begin
3655 if not Delay_Subprogram_Descriptors (E) then
3656 Set_Delay_Subprogram_Descriptors (E);
3657 Pending_Descriptor.Append (E);
3658 end if;
3659 end Delay_Descriptors;
3660
3661 -----------------------
3662 -- Might_Inline_Subp --
3663 -----------------------
3664
3665 function Might_Inline_Subp return Boolean is
3666 E : Entity_Id;
3667
3668 begin
3669 if not Inline_Processing_Required then
3670 return False;
3671
3672 else
3673 E := First_Entity (Gen_Unit);
3674 while Present (E) loop
3675 if Is_Subprogram (E) and then Is_Inlined (E) then
3676 -- Remember if there are any subprograms with Inline_Always
3677
3678 if Has_Pragma_Inline_Always (E) then
3679 Has_Inline_Always := True;
3680 end if;
3681
3682 return True;
3683 end if;
3684
3685 Next_Entity (E);
3686 end loop;
3687 end if;
3688
3689 return False;
3690 end Might_Inline_Subp;
3691
3692 -- Local declarations
3693
3694 Mode : Ghost_Mode_Type;
3695 Mode_Set : Boolean := False;
3696
3697 Vis_Prims_List : Elist_Id := No_Elist;
3698 -- List of primitives made temporarily visible in the instantiation
3699 -- to match the visibility of the formal type
3700
3701 -- Start of processing for Analyze_Package_Instantiation
3702
3703 begin
3704 Check_SPARK_05_Restriction ("generic is not allowed", N);
3705
3706 -- Very first thing: check for Text_IO special unit in case we are
3707 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3708
3709 Check_Text_IO_Special_Unit (Name (N));
3710
3711 -- Make node global for error reporting
3712
3713 Instantiation_Node := N;
3714
3715 -- Case of instantiation of a generic package
3716
3717 if Nkind (N) = N_Package_Instantiation then
3718 Act_Decl_Id := New_Copy (Defining_Entity (N));
3719 Set_Comes_From_Source (Act_Decl_Id, True);
3720
3721 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3722 Act_Decl_Name :=
3723 Make_Defining_Program_Unit_Name (Loc,
3724 Name =>
3725 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3726 Defining_Identifier => Act_Decl_Id);
3727 else
3728 Act_Decl_Name := Act_Decl_Id;
3729 end if;
3730
3731 -- Case of instantiation of a formal package
3732
3733 else
3734 Act_Decl_Id := Defining_Identifier (N);
3735 Act_Decl_Name := Act_Decl_Id;
3736 end if;
3737
3738 Generate_Definition (Act_Decl_Id);
3739 Set_Ekind (Act_Decl_Id, E_Package);
3740
3741 -- Initialize list of incomplete actuals before analysis
3742
3743 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
3744
3745 Preanalyze_Actuals (N, Act_Decl_Id);
3746
3747 -- Turn off style checking in instances. If the check is enabled on the
3748 -- generic unit, a warning in an instance would just be noise. If not
3749 -- enabled on the generic, then a warning in an instance is just wrong.
3750 -- This must be done after analyzing the actuals, which do come from
3751 -- source and are subject to style checking.
3752
3753 Style_Check := False;
3754
3755 Init_Env;
3756 Env_Installed := True;
3757
3758 -- Reset renaming map for formal types. The mapping is established
3759 -- when analyzing the generic associations, but some mappings are
3760 -- inherited from formal packages of parent units, and these are
3761 -- constructed when the parents are installed.
3762
3763 Generic_Renamings.Set_Last (0);
3764 Generic_Renamings_HTable.Reset;
3765
3766 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3767 Gen_Unit := Entity (Gen_Id);
3768
3769 -- A package instantiation is Ghost when it is subject to pragma Ghost
3770 -- or the generic template is Ghost. Set the mode now to ensure that
3771 -- any nodes generated during analysis and expansion are marked as
3772 -- Ghost.
3773
3774 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit, Mode);
3775 Mode_Set := True;
3776
3777 -- Verify that it is the name of a generic package
3778
3779 -- A visibility glitch: if the instance is a child unit and the generic
3780 -- is the generic unit of a parent instance (i.e. both the parent and
3781 -- the child units are instances of the same package) the name now
3782 -- denotes the renaming within the parent, not the intended generic
3783 -- unit. See if there is a homonym that is the desired generic. The
3784 -- renaming declaration must be visible inside the instance of the
3785 -- child, but not when analyzing the name in the instantiation itself.
3786
3787 if Ekind (Gen_Unit) = E_Package
3788 and then Present (Renamed_Entity (Gen_Unit))
3789 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3790 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3791 and then Present (Homonym (Gen_Unit))
3792 then
3793 Gen_Unit := Homonym (Gen_Unit);
3794 end if;
3795
3796 if Etype (Gen_Unit) = Any_Type then
3797 Restore_Env;
3798 goto Leave;
3799
3800 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3801
3802 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3803
3804 if From_Limited_With (Gen_Unit) then
3805 Error_Msg_N
3806 ("cannot instantiate a limited withed package", Gen_Id);
3807 else
3808 Error_Msg_NE
3809 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3810 end if;
3811
3812 Restore_Env;
3813 goto Leave;
3814 end if;
3815
3816 if In_Extended_Main_Source_Unit (N) then
3817 Set_Is_Instantiated (Gen_Unit);
3818 Generate_Reference (Gen_Unit, N);
3819
3820 if Present (Renamed_Object (Gen_Unit)) then
3821 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3822 Generate_Reference (Renamed_Object (Gen_Unit), N);
3823 end if;
3824 end if;
3825
3826 if Nkind (Gen_Id) = N_Identifier
3827 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3828 then
3829 Error_Msg_NE
3830 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3831
3832 elsif Nkind (Gen_Id) = N_Expanded_Name
3833 and then Is_Child_Unit (Gen_Unit)
3834 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3835 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3836 then
3837 Error_Msg_N
3838 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3839 end if;
3840
3841 Set_Entity (Gen_Id, Gen_Unit);
3842
3843 -- If generic is a renaming, get original generic unit
3844
3845 if Present (Renamed_Object (Gen_Unit))
3846 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3847 then
3848 Gen_Unit := Renamed_Object (Gen_Unit);
3849 end if;
3850
3851 -- Verify that there are no circular instantiations
3852
3853 if In_Open_Scopes (Gen_Unit) then
3854 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3855 Restore_Env;
3856 goto Leave;
3857
3858 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3859 Error_Msg_Node_2 := Current_Scope;
3860 Error_Msg_NE
3861 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3862 Circularity_Detected := True;
3863 Restore_Env;
3864 goto Leave;
3865
3866 else
3867 -- If the context of the instance is subject to SPARK_Mode "off" or
3868 -- the annotation is altogether missing, set the global flag which
3869 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
3870 -- the instance.
3871
3872 if SPARK_Mode /= On then
3873 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3874
3875 -- Mark the instance spec in case the body is instantiated at a
3876 -- later pass. This preserves the original context in effect for
3877 -- the body.
3878
3879 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
3880 end if;
3881
3882 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3883 Gen_Spec := Specification (Gen_Decl);
3884
3885 -- Initialize renamings map, for error checking, and the list that
3886 -- holds private entities whose views have changed between generic
3887 -- definition and instantiation. If this is the instance created to
3888 -- validate an actual package, the instantiation environment is that
3889 -- of the enclosing instance.
3890
3891 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3892
3893 -- Copy original generic tree, to produce text for instantiation
3894
3895 Act_Tree :=
3896 Copy_Generic_Node
3897 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3898
3899 Act_Spec := Specification (Act_Tree);
3900
3901 -- If this is the instance created to validate an actual package,
3902 -- only the formals matter, do not examine the package spec itself.
3903
3904 if Is_Actual_Pack then
3905 Set_Visible_Declarations (Act_Spec, New_List);
3906 Set_Private_Declarations (Act_Spec, New_List);
3907 end if;
3908
3909 Renaming_List :=
3910 Analyze_Associations
3911 (I_Node => N,
3912 Formals => Generic_Formal_Declarations (Act_Tree),
3913 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3914
3915 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3916
3917 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3918 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3919 Set_Is_Generic_Instance (Act_Decl_Id);
3920 Set_Generic_Parent (Act_Spec, Gen_Unit);
3921
3922 -- References to the generic in its own declaration or its body are
3923 -- references to the instance. Add a renaming declaration for the
3924 -- generic unit itself. This declaration, as well as the renaming
3925 -- declarations for the generic formals, must remain private to the
3926 -- unit: the formals, because this is the language semantics, and
3927 -- the unit because its use is an artifact of the implementation.
3928
3929 Unit_Renaming :=
3930 Make_Package_Renaming_Declaration (Loc,
3931 Defining_Unit_Name =>
3932 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3933 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3934
3935 Append (Unit_Renaming, Renaming_List);
3936
3937 -- The renaming declarations are the first local declarations of the
3938 -- new unit.
3939
3940 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3941 Insert_List_Before
3942 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3943 else
3944 Set_Visible_Declarations (Act_Spec, Renaming_List);
3945 end if;
3946
3947 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
3948
3949 -- Propagate the aspect specifications from the package declaration
3950 -- template to the instantiated version of the package declaration.
3951
3952 if Has_Aspects (Act_Tree) then
3953 Set_Aspect_Specifications (Act_Decl,
3954 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3955 end if;
3956
3957 -- The generic may have a generated Default_Storage_Pool aspect,
3958 -- set at the point of generic declaration. If the instance has
3959 -- that aspect, it overrides the one inherited from the generic.
3960
3961 if Has_Aspects (Gen_Spec) then
3962 if No (Aspect_Specifications (N)) then
3963 Set_Aspect_Specifications (N,
3964 (New_Copy_List_Tree
3965 (Aspect_Specifications (Gen_Spec))));
3966
3967 else
3968 declare
3969 ASN1, ASN2 : Node_Id;
3970
3971 begin
3972 ASN1 := First (Aspect_Specifications (N));
3973 while Present (ASN1) loop
3974 if Chars (Identifier (ASN1)) = Name_Default_Storage_Pool
3975 then
3976 -- If generic carries a default storage pool, remove
3977 -- it in favor of the instance one.
3978
3979 ASN2 := First (Aspect_Specifications (Gen_Spec));
3980 while Present (ASN2) loop
3981 if Chars (Identifier (ASN2)) =
3982 Name_Default_Storage_Pool
3983 then
3984 Remove (ASN2);
3985 exit;
3986 end if;
3987
3988 Next (ASN2);
3989 end loop;
3990 end if;
3991
3992 Next (ASN1);
3993 end loop;
3994
3995 Prepend_List_To (Aspect_Specifications (N),
3996 (New_Copy_List_Tree
3997 (Aspect_Specifications (Gen_Spec))));
3998 end;
3999 end if;
4000 end if;
4001
4002 -- Save the instantiation node, for subsequent instantiation of the
4003 -- body, if there is one and we are generating code for the current
4004 -- unit. Mark unit as having a body (avoids premature error message).
4005
4006 -- We instantiate the body if we are generating code, if we are
4007 -- generating cross-reference information, or if we are building
4008 -- trees for ASIS use or GNATprove use.
4009
4010 declare
4011 Enclosing_Body_Present : Boolean := False;
4012 -- If the generic unit is not a compilation unit, then a body may
4013 -- be present in its parent even if none is required. We create a
4014 -- tentative pending instantiation for the body, which will be
4015 -- discarded if none is actually present.
4016
4017 Scop : Entity_Id;
4018
4019 begin
4020 if Scope (Gen_Unit) /= Standard_Standard
4021 and then not Is_Child_Unit (Gen_Unit)
4022 then
4023 Scop := Scope (Gen_Unit);
4024 while Present (Scop) and then Scop /= Standard_Standard loop
4025 if Unit_Requires_Body (Scop) then
4026 Enclosing_Body_Present := True;
4027 exit;
4028
4029 elsif In_Open_Scopes (Scop)
4030 and then In_Package_Body (Scop)
4031 then
4032 Enclosing_Body_Present := True;
4033 exit;
4034 end if;
4035
4036 exit when Is_Compilation_Unit (Scop);
4037 Scop := Scope (Scop);
4038 end loop;
4039 end if;
4040
4041 -- If front-end inlining is enabled or there are any subprograms
4042 -- marked with Inline_Always, and this is a unit for which code
4043 -- will be generated, we instantiate the body at once.
4044
4045 -- This is done if the instance is not the main unit, and if the
4046 -- generic is not a child unit of another generic, to avoid scope
4047 -- problems and the reinstallation of parent instances.
4048
4049 if Expander_Active
4050 and then (not Is_Child_Unit (Gen_Unit)
4051 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4052 and then Might_Inline_Subp
4053 and then not Is_Actual_Pack
4054 then
4055 if not Back_End_Inlining
4056 and then (Front_End_Inlining or else Has_Inline_Always)
4057 and then (Is_In_Main_Unit (N)
4058 or else In_Main_Context (Current_Scope))
4059 and then Nkind (Parent (N)) /= N_Compilation_Unit
4060 then
4061 Inline_Now := True;
4062
4063 -- In configurable_run_time mode we force the inlining of
4064 -- predefined subprograms marked Inline_Always, to minimize
4065 -- the use of the run-time library.
4066
4067 elsif Is_Predefined_File_Name
4068 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
4069 and then Configurable_Run_Time_Mode
4070 and then Nkind (Parent (N)) /= N_Compilation_Unit
4071 then
4072 Inline_Now := True;
4073 end if;
4074
4075 -- If the current scope is itself an instance within a child
4076 -- unit, there will be duplications in the scope stack, and the
4077 -- unstacking mechanism in Inline_Instance_Body will fail.
4078 -- This loses some rare cases of optimization, and might be
4079 -- improved some day, if we can find a proper abstraction for
4080 -- "the complete compilation context" that can be saved and
4081 -- restored. ???
4082
4083 if Is_Generic_Instance (Current_Scope) then
4084 declare
4085 Curr_Unit : constant Entity_Id :=
4086 Cunit_Entity (Current_Sem_Unit);
4087 begin
4088 if Curr_Unit /= Current_Scope
4089 and then Is_Child_Unit (Curr_Unit)
4090 then
4091 Inline_Now := False;
4092 end if;
4093 end;
4094 end if;
4095 end if;
4096
4097 Needs_Body :=
4098 (Unit_Requires_Body (Gen_Unit)
4099 or else Enclosing_Body_Present
4100 or else Present (Corresponding_Body (Gen_Decl)))
4101 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
4102 and then not Is_Actual_Pack
4103 and then not Inline_Now
4104 and then (Operating_Mode = Generate_Code
4105
4106 -- Need comment for this check ???
4107
4108 or else (Operating_Mode = Check_Semantics
4109 and then (ASIS_Mode or GNATprove_Mode)));
4110
4111 -- If front-end inlining is enabled or there are any subprograms
4112 -- marked with Inline_Always, do not instantiate body when within
4113 -- a generic context.
4114
4115 if ((Front_End_Inlining or else Has_Inline_Always)
4116 and then not Expander_Active)
4117 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
4118 then
4119 Needs_Body := False;
4120 end if;
4121
4122 -- If the current context is generic, and the package being
4123 -- instantiated is declared within a formal package, there is no
4124 -- body to instantiate until the enclosing generic is instantiated
4125 -- and there is an actual for the formal package. If the formal
4126 -- package has parameters, we build a regular package instance for
4127 -- it, that precedes the original formal package declaration.
4128
4129 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4130 declare
4131 Decl : constant Node_Id :=
4132 Original_Node
4133 (Unit_Declaration_Node (Scope (Gen_Unit)));
4134 begin
4135 if Nkind (Decl) = N_Formal_Package_Declaration
4136 or else (Nkind (Decl) = N_Package_Declaration
4137 and then Is_List_Member (Decl)
4138 and then Present (Next (Decl))
4139 and then
4140 Nkind (Next (Decl)) =
4141 N_Formal_Package_Declaration)
4142 then
4143 Needs_Body := False;
4144 end if;
4145 end;
4146 end if;
4147 end;
4148
4149 -- For RCI unit calling stubs, we omit the instance body if the
4150 -- instance is the RCI library unit itself.
4151
4152 -- However there is a special case for nested instances: in this case
4153 -- we do generate the instance body, as it might be required, e.g.
4154 -- because it provides stream attributes for some type used in the
4155 -- profile of a remote subprogram. This is consistent with 12.3(12),
4156 -- which indicates that the instance body occurs at the place of the
4157 -- instantiation, and thus is part of the RCI declaration, which is
4158 -- present on all client partitions (this is E.2.3(18)).
4159
4160 -- Note that AI12-0002 may make it illegal at some point to have
4161 -- stream attributes defined in an RCI unit, in which case this
4162 -- special case will become unnecessary. In the meantime, there
4163 -- is known application code in production that depends on this
4164 -- being possible, so we definitely cannot eliminate the body in
4165 -- the case of nested instances for the time being.
4166
4167 -- When we generate a nested instance body, calling stubs for any
4168 -- relevant subprogram will be be inserted immediately after the
4169 -- subprogram declarations, and will take precedence over the
4170 -- subsequent (original) body. (The stub and original body will be
4171 -- complete homographs, but this is permitted in an instance).
4172 -- (Could we do better and remove the original body???)
4173
4174 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4175 and then Comes_From_Source (N)
4176 and then Nkind (Parent (N)) = N_Compilation_Unit
4177 then
4178 Needs_Body := False;
4179 end if;
4180
4181 if Needs_Body then
4182
4183 -- Here is a defence against a ludicrous number of instantiations
4184 -- caused by a circular set of instantiation attempts.
4185
4186 if Pending_Instantiations.Last > Maximum_Instantiations then
4187 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4188 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4189 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4190 raise Unrecoverable_Error;
4191 end if;
4192
4193 -- Indicate that the enclosing scopes contain an instantiation,
4194 -- and that cleanup actions should be delayed until after the
4195 -- instance body is expanded.
4196
4197 Check_Forward_Instantiation (Gen_Decl);
4198 if Nkind (N) = N_Package_Instantiation then
4199 declare
4200 Enclosing_Master : Entity_Id;
4201
4202 begin
4203 -- Loop to search enclosing masters
4204
4205 Enclosing_Master := Current_Scope;
4206 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4207 if Ekind (Enclosing_Master) = E_Package then
4208 if Is_Compilation_Unit (Enclosing_Master) then
4209 if In_Package_Body (Enclosing_Master) then
4210 Delay_Descriptors
4211 (Body_Entity (Enclosing_Master));
4212 else
4213 Delay_Descriptors
4214 (Enclosing_Master);
4215 end if;
4216
4217 exit Scope_Loop;
4218
4219 else
4220 Enclosing_Master := Scope (Enclosing_Master);
4221 end if;
4222
4223 elsif Is_Generic_Unit (Enclosing_Master)
4224 or else Ekind (Enclosing_Master) = E_Void
4225 then
4226 -- Cleanup actions will eventually be performed on the
4227 -- enclosing subprogram or package instance, if any.
4228 -- Enclosing scope is void in the formal part of a
4229 -- generic subprogram.
4230
4231 exit Scope_Loop;
4232
4233 else
4234 if Ekind (Enclosing_Master) = E_Entry
4235 and then
4236 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4237 then
4238 if not Expander_Active then
4239 exit Scope_Loop;
4240 else
4241 Enclosing_Master :=
4242 Protected_Body_Subprogram (Enclosing_Master);
4243 end if;
4244 end if;
4245
4246 Set_Delay_Cleanups (Enclosing_Master);
4247
4248 while Ekind (Enclosing_Master) = E_Block loop
4249 Enclosing_Master := Scope (Enclosing_Master);
4250 end loop;
4251
4252 if Is_Subprogram (Enclosing_Master) then
4253 Delay_Descriptors (Enclosing_Master);
4254
4255 elsif Is_Task_Type (Enclosing_Master) then
4256 declare
4257 TBP : constant Node_Id :=
4258 Get_Task_Body_Procedure
4259 (Enclosing_Master);
4260 begin
4261 if Present (TBP) then
4262 Delay_Descriptors (TBP);
4263 Set_Delay_Cleanups (TBP);
4264 end if;
4265 end;
4266 end if;
4267
4268 exit Scope_Loop;
4269 end if;
4270 end loop Scope_Loop;
4271 end;
4272
4273 -- Make entry in table
4274
4275 Add_Pending_Instantiation (N, Act_Decl);
4276 end if;
4277 end if;
4278
4279 Set_Categorization_From_Pragmas (Act_Decl);
4280
4281 if Parent_Installed then
4282 Hide_Current_Scope;
4283 end if;
4284
4285 Set_Instance_Spec (N, Act_Decl);
4286
4287 -- If not a compilation unit, insert the package declaration before
4288 -- the original instantiation node.
4289
4290 if Nkind (Parent (N)) /= N_Compilation_Unit then
4291 Mark_Rewrite_Insertion (Act_Decl);
4292 Insert_Before (N, Act_Decl);
4293
4294 if Has_Aspects (N) then
4295 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4296
4297 -- The pragma created for a Default_Storage_Pool aspect must
4298 -- appear ahead of the declarations in the instance spec.
4299 -- Analysis has placed it after the instance node, so remove
4300 -- it and reinsert it properly now.
4301
4302 declare
4303 ASN : constant Node_Id := First (Aspect_Specifications (N));
4304 A_Name : constant Name_Id := Chars (Identifier (ASN));
4305 Decl : Node_Id;
4306
4307 begin
4308 if A_Name = Name_Default_Storage_Pool then
4309 if No (Visible_Declarations (Act_Spec)) then
4310 Set_Visible_Declarations (Act_Spec, New_List);
4311 end if;
4312
4313 Decl := Next (N);
4314 while Present (Decl) loop
4315 if Nkind (Decl) = N_Pragma then
4316 Remove (Decl);
4317 Prepend (Decl, Visible_Declarations (Act_Spec));
4318 exit;
4319 end if;
4320
4321 Next (Decl);
4322 end loop;
4323 end if;
4324 end;
4325 end if;
4326
4327 Analyze (Act_Decl);
4328
4329 -- For an instantiation that is a compilation unit, place
4330 -- declaration on current node so context is complete for analysis
4331 -- (including nested instantiations). If this is the main unit,
4332 -- the declaration eventually replaces the instantiation node.
4333 -- If the instance body is created later, it replaces the
4334 -- instance node, and the declaration is attached to it
4335 -- (see Build_Instance_Compilation_Unit_Nodes).
4336
4337 else
4338 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4339
4340 -- The entity for the current unit is the newly created one,
4341 -- and all semantic information is attached to it.
4342
4343 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4344
4345 -- If this is the main unit, replace the main entity as well
4346
4347 if Current_Sem_Unit = Main_Unit then
4348 Main_Unit_Entity := Act_Decl_Id;
4349 end if;
4350 end if;
4351
4352 Set_Unit (Parent (N), Act_Decl);
4353 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4354 Set_Package_Instantiation (Act_Decl_Id, N);
4355
4356 -- Process aspect specifications of the instance node, if any, to
4357 -- take into account categorization pragmas before analyzing the
4358 -- instance.
4359
4360 if Has_Aspects (N) then
4361 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4362 end if;
4363
4364 Analyze (Act_Decl);
4365 Set_Unit (Parent (N), N);
4366 Set_Body_Required (Parent (N), False);
4367
4368 -- We never need elaboration checks on instantiations, since by
4369 -- definition, the body instantiation is elaborated at the same
4370 -- time as the spec instantiation.
4371
4372 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4373 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4374 end if;
4375
4376 Check_Elab_Instantiation (N);
4377
4378 if ABE_Is_Certain (N) and then Needs_Body then
4379 Pending_Instantiations.Decrement_Last;
4380 end if;
4381
4382 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4383
4384 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4385 First_Private_Entity (Act_Decl_Id));
4386
4387 -- If the instantiation will receive a body, the unit will be
4388 -- transformed into a package body, and receive its own elaboration
4389 -- entity. Otherwise, the nature of the unit is now a package
4390 -- declaration.
4391
4392 if Nkind (Parent (N)) = N_Compilation_Unit
4393 and then not Needs_Body
4394 then
4395 Rewrite (N, Act_Decl);
4396 end if;
4397
4398 if Present (Corresponding_Body (Gen_Decl))
4399 or else Unit_Requires_Body (Gen_Unit)
4400 then
4401 Set_Has_Completion (Act_Decl_Id);
4402 end if;
4403
4404 Check_Formal_Packages (Act_Decl_Id);
4405
4406 Restore_Hidden_Primitives (Vis_Prims_List);
4407 Restore_Private_Views (Act_Decl_Id);
4408
4409 Inherit_Context (Gen_Decl, N);
4410
4411 if Parent_Installed then
4412 Remove_Parent;
4413 end if;
4414
4415 Restore_Env;
4416 Env_Installed := False;
4417 end if;
4418
4419 Validate_Categorization_Dependency (N, Act_Decl_Id);
4420
4421 -- There used to be a check here to prevent instantiations in local
4422 -- contexts if the No_Local_Allocators restriction was active. This
4423 -- check was removed by a binding interpretation in AI-95-00130/07,
4424 -- but we retain the code for documentation purposes.
4425
4426 -- if Ekind (Act_Decl_Id) /= E_Void
4427 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4428 -- then
4429 -- Check_Restriction (No_Local_Allocators, N);
4430 -- end if;
4431
4432 if Inline_Now then
4433 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4434 end if;
4435
4436 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4437 -- be used as defining identifiers for a formal package and for the
4438 -- corresponding expanded package.
4439
4440 if Nkind (N) = N_Formal_Package_Declaration then
4441 Act_Decl_Id := New_Copy (Defining_Entity (N));
4442 Set_Comes_From_Source (Act_Decl_Id, True);
4443 Set_Is_Generic_Instance (Act_Decl_Id, False);
4444 Set_Defining_Identifier (N, Act_Decl_Id);
4445 end if;
4446
4447 -- Check that if N is an instantiation of System.Dim_Float_IO or
4448 -- System.Dim_Integer_IO, the formal type has a dimension system.
4449
4450 if Nkind (N) = N_Package_Instantiation
4451 and then Is_Dim_IO_Package_Instantiation (N)
4452 then
4453 declare
4454 Assoc : constant Node_Id := First (Generic_Associations (N));
4455 begin
4456 if not Has_Dimension_System
4457 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4458 then
4459 Error_Msg_N ("type with a dimension system expected", Assoc);
4460 end if;
4461 end;
4462 end if;
4463
4464 <<Leave>>
4465 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4466 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4467 end if;
4468
4469 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
4470 SPARK_Mode := Save_SM;
4471 SPARK_Mode_Pragma := Save_SMP;
4472 Style_Check := Save_Style_Check;
4473
4474 if Mode_Set then
4475 Restore_Ghost_Mode (Mode);
4476 end if;
4477
4478 exception
4479 when Instantiation_Error =>
4480 if Parent_Installed then
4481 Remove_Parent;
4482 end if;
4483
4484 if Env_Installed then
4485 Restore_Env;
4486 end if;
4487
4488 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
4489 SPARK_Mode := Save_SM;
4490 SPARK_Mode_Pragma := Save_SMP;
4491 Style_Check := Save_Style_Check;
4492
4493 if Mode_Set then
4494 Restore_Ghost_Mode (Mode);
4495 end if;
4496 end Analyze_Package_Instantiation;
4497
4498 --------------------------
4499 -- Inline_Instance_Body --
4500 --------------------------
4501
4502 procedure Inline_Instance_Body
4503 (N : Node_Id;
4504 Gen_Unit : Entity_Id;
4505 Act_Decl : Node_Id)
4506 is
4507 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4508 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4509 Gen_Comp : constant Entity_Id :=
4510 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4511
4512 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
4513 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
4514 -- Save all SPARK_Mode-related attributes as removing enclosing scopes
4515 -- to provide a clean environment for analysis of the inlined body will
4516 -- eliminate any previously set SPARK_Mode.
4517
4518 Scope_Stack_Depth : constant Pos :=
4519 Scope_Stack.Last - Scope_Stack.First + 1;
4520
4521 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4522 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4523 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4524 Curr_Scope : Entity_Id := Empty;
4525 List : Elist_Id;
4526 Num_Inner : Nat := 0;
4527 Num_Scopes : Nat := 0;
4528 N_Instances : Nat := 0;
4529 Removed : Boolean := False;
4530 S : Entity_Id;
4531 Vis : Boolean;
4532
4533 begin
4534 -- Case of generic unit defined in another unit. We must remove the
4535 -- complete context of the current unit to install that of the generic.
4536
4537 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4538
4539 -- Add some comments for the following two loops ???
4540
4541 S := Current_Scope;
4542 while Present (S) and then S /= Standard_Standard loop
4543 loop
4544 Num_Scopes := Num_Scopes + 1;
4545
4546 Use_Clauses (Num_Scopes) :=
4547 (Scope_Stack.Table
4548 (Scope_Stack.Last - Num_Scopes + 1).
4549 First_Use_Clause);
4550 End_Use_Clauses (Use_Clauses (Num_Scopes));
4551
4552 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4553 or else Scope_Stack.Table
4554 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4555 end loop;
4556
4557 exit when Is_Generic_Instance (S)
4558 and then (In_Package_Body (S)
4559 or else Ekind (S) = E_Procedure
4560 or else Ekind (S) = E_Function);
4561 S := Scope (S);
4562 end loop;
4563
4564 Vis := Is_Immediately_Visible (Gen_Comp);
4565
4566 -- Find and save all enclosing instances
4567
4568 S := Current_Scope;
4569
4570 while Present (S)
4571 and then S /= Standard_Standard
4572 loop
4573 if Is_Generic_Instance (S) then
4574 N_Instances := N_Instances + 1;
4575 Instances (N_Instances) := S;
4576
4577 exit when In_Package_Body (S);
4578 end if;
4579
4580 S := Scope (S);
4581 end loop;
4582
4583 -- Remove context of current compilation unit, unless we are within a
4584 -- nested package instantiation, in which case the context has been
4585 -- removed previously.
4586
4587 -- If current scope is the body of a child unit, remove context of
4588 -- spec as well. If an enclosing scope is an instance body, the
4589 -- context has already been removed, but the entities in the body
4590 -- must be made invisible as well.
4591
4592 S := Current_Scope;
4593 while Present (S) and then S /= Standard_Standard loop
4594 if Is_Generic_Instance (S)
4595 and then (In_Package_Body (S)
4596 or else Ekind_In (S, E_Procedure, E_Function))
4597 then
4598 -- We still have to remove the entities of the enclosing
4599 -- instance from direct visibility.
4600
4601 declare
4602 E : Entity_Id;
4603 begin
4604 E := First_Entity (S);
4605 while Present (E) loop
4606 Set_Is_Immediately_Visible (E, False);
4607 Next_Entity (E);
4608 end loop;
4609 end;
4610
4611 exit;
4612 end if;
4613
4614 if S = Curr_Unit
4615 or else (Ekind (Curr_Unit) = E_Package_Body
4616 and then S = Spec_Entity (Curr_Unit))
4617 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4618 and then S = Corresponding_Spec
4619 (Unit_Declaration_Node (Curr_Unit)))
4620 then
4621 Removed := True;
4622
4623 -- Remove entities in current scopes from visibility, so that
4624 -- instance body is compiled in a clean environment.
4625
4626 List := Save_Scope_Stack (Handle_Use => False);
4627
4628 if Is_Child_Unit (S) then
4629
4630 -- Remove child unit from stack, as well as inner scopes.
4631 -- Removing the context of a child unit removes parent units
4632 -- as well.
4633
4634 while Current_Scope /= S loop
4635 Num_Inner := Num_Inner + 1;
4636 Inner_Scopes (Num_Inner) := Current_Scope;
4637 Pop_Scope;
4638 end loop;
4639
4640 Pop_Scope;
4641 Remove_Context (Curr_Comp);
4642 Curr_Scope := S;
4643
4644 else
4645 Remove_Context (Curr_Comp);
4646 end if;
4647
4648 if Ekind (Curr_Unit) = E_Package_Body then
4649 Remove_Context (Library_Unit (Curr_Comp));
4650 end if;
4651 end if;
4652
4653 S := Scope (S);
4654 end loop;
4655
4656 pragma Assert (Num_Inner < Num_Scopes);
4657
4658 -- The inlined package body must be analyzed with the SPARK_Mode of
4659 -- the enclosing context, otherwise the body may cause bogus errors
4660 -- if a configuration SPARK_Mode pragma in in effect.
4661
4662 Push_Scope (Standard_Standard);
4663 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4664 Instantiate_Package_Body
4665 (Body_Info =>
4666 ((Inst_Node => N,
4667 Act_Decl => Act_Decl,
4668 Expander_Status => Expander_Active,
4669 Current_Sem_Unit => Current_Sem_Unit,
4670 Scope_Suppress => Scope_Suppress,
4671 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4672 Version => Ada_Version,
4673 Version_Pragma => Ada_Version_Pragma,
4674 Warnings => Save_Warnings,
4675 SPARK_Mode => Save_SM,
4676 SPARK_Mode_Pragma => Save_SMP)),
4677 Inlined_Body => True);
4678
4679 Pop_Scope;
4680
4681 -- Restore context
4682
4683 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4684
4685 -- Reset Generic_Instance flag so that use clauses can be installed
4686 -- in the proper order. (See Use_One_Package for effect of enclosing
4687 -- instances on processing of use clauses).
4688
4689 for J in 1 .. N_Instances loop
4690 Set_Is_Generic_Instance (Instances (J), False);
4691 end loop;
4692
4693 if Removed then
4694 Install_Context (Curr_Comp);
4695
4696 if Present (Curr_Scope)
4697 and then Is_Child_Unit (Curr_Scope)
4698 then
4699 Push_Scope (Curr_Scope);
4700 Set_Is_Immediately_Visible (Curr_Scope);
4701
4702 -- Finally, restore inner scopes as well
4703
4704 for J in reverse 1 .. Num_Inner loop
4705 Push_Scope (Inner_Scopes (J));
4706 end loop;
4707 end if;
4708
4709 Restore_Scope_Stack (List, Handle_Use => False);
4710
4711 if Present (Curr_Scope)
4712 and then
4713 (In_Private_Part (Curr_Scope)
4714 or else In_Package_Body (Curr_Scope))
4715 then
4716 -- Install private declaration of ancestor units, which are
4717 -- currently available. Restore_Scope_Stack and Install_Context
4718 -- only install the visible part of parents.
4719
4720 declare
4721 Par : Entity_Id;
4722 begin
4723 Par := Scope (Curr_Scope);
4724 while (Present (Par)) and then Par /= Standard_Standard loop
4725 Install_Private_Declarations (Par);
4726 Par := Scope (Par);
4727 end loop;
4728 end;
4729 end if;
4730 end if;
4731
4732 -- Restore use clauses. For a child unit, use clauses in the parents
4733 -- are restored when installing the context, so only those in inner
4734 -- scopes (and those local to the child unit itself) need to be
4735 -- installed explicitly.
4736
4737 if Is_Child_Unit (Curr_Unit) and then Removed then
4738 for J in reverse 1 .. Num_Inner + 1 loop
4739 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4740 Use_Clauses (J);
4741 Install_Use_Clauses (Use_Clauses (J));
4742 end loop;
4743
4744 else
4745 for J in reverse 1 .. Num_Scopes loop
4746 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4747 Use_Clauses (J);
4748 Install_Use_Clauses (Use_Clauses (J));
4749 end loop;
4750 end if;
4751
4752 -- Restore status of instances. If one of them is a body, make its
4753 -- local entities visible again.
4754
4755 declare
4756 E : Entity_Id;
4757 Inst : Entity_Id;
4758
4759 begin
4760 for J in 1 .. N_Instances loop
4761 Inst := Instances (J);
4762 Set_Is_Generic_Instance (Inst, True);
4763
4764 if In_Package_Body (Inst)
4765 or else Ekind_In (S, E_Procedure, E_Function)
4766 then
4767 E := First_Entity (Instances (J));
4768 while Present (E) loop
4769 Set_Is_Immediately_Visible (E);
4770 Next_Entity (E);
4771 end loop;
4772 end if;
4773 end loop;
4774 end;
4775
4776 -- If generic unit is in current unit, current context is correct. Note
4777 -- that the context is guaranteed to carry the correct SPARK_Mode as no
4778 -- enclosing scopes were removed.
4779
4780 else
4781 Instantiate_Package_Body
4782 (Body_Info =>
4783 ((Inst_Node => N,
4784 Act_Decl => Act_Decl,
4785 Expander_Status => Expander_Active,
4786 Current_Sem_Unit => Current_Sem_Unit,
4787 Scope_Suppress => Scope_Suppress,
4788 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4789 Version => Ada_Version,
4790 Version_Pragma => Ada_Version_Pragma,
4791 Warnings => Save_Warnings,
4792 SPARK_Mode => SPARK_Mode,
4793 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4794 Inlined_Body => True);
4795 end if;
4796 end Inline_Instance_Body;
4797
4798 -------------------------------------
4799 -- Analyze_Procedure_Instantiation --
4800 -------------------------------------
4801
4802 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4803 begin
4804 Analyze_Subprogram_Instantiation (N, E_Procedure);
4805 end Analyze_Procedure_Instantiation;
4806
4807 -----------------------------------
4808 -- Need_Subprogram_Instance_Body --
4809 -----------------------------------
4810
4811 function Need_Subprogram_Instance_Body
4812 (N : Node_Id;
4813 Subp : Entity_Id) return Boolean
4814 is
4815
4816 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
4817 -- Return True if E is an inlined subprogram, an inlined renaming or a
4818 -- subprogram nested in an inlined subprogram. The inlining machinery
4819 -- totally disregards nested subprograms since it considers that they
4820 -- will always be compiled if the parent is (see Inline.Is_Nested).
4821
4822 ------------------------------------
4823 -- Is_Inlined_Or_Child_Of_Inlined --
4824 ------------------------------------
4825
4826 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
4827 Scop : Entity_Id;
4828
4829 begin
4830 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
4831 return True;
4832 end if;
4833
4834 Scop := Scope (E);
4835 while Scop /= Standard_Standard loop
4836 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
4837 return True;
4838 end if;
4839
4840 Scop := Scope (Scop);
4841 end loop;
4842
4843 return False;
4844 end Is_Inlined_Or_Child_Of_Inlined;
4845
4846 begin
4847 -- Must be in the main unit or inlined (or child of inlined)
4848
4849 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
4850
4851 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4852
4853 and then (Operating_Mode = Generate_Code
4854 or else (Operating_Mode = Check_Semantics
4855 and then (ASIS_Mode or GNATprove_Mode)))
4856
4857 -- The body is needed when generating code (full expansion), in ASIS
4858 -- mode for other tools, and in GNATprove mode (special expansion) for
4859 -- formal verification of the body itself.
4860
4861 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4862
4863 -- No point in inlining if ABE is inevitable
4864
4865 and then not ABE_Is_Certain (N)
4866
4867 -- Or if subprogram is eliminated
4868
4869 and then not Is_Eliminated (Subp)
4870 then
4871 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
4872 return True;
4873
4874 -- Here if not inlined, or we ignore the inlining
4875
4876 else
4877 return False;
4878 end if;
4879 end Need_Subprogram_Instance_Body;
4880
4881 --------------------------------------
4882 -- Analyze_Subprogram_Instantiation --
4883 --------------------------------------
4884
4885 -- WARNING: This routine manages Ghost regions. Return statements must be
4886 -- replaced by gotos which jump to the end of the routine and restore the
4887 -- Ghost mode.
4888
4889 procedure Analyze_Subprogram_Instantiation
4890 (N : Node_Id;
4891 K : Entity_Kind)
4892 is
4893 Loc : constant Source_Ptr := Sloc (N);
4894 Gen_Id : constant Node_Id := Name (N);
4895
4896 Anon_Id : constant Entity_Id :=
4897 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4898 Chars => New_External_Name
4899 (Chars (Defining_Entity (N)), 'R'));
4900
4901 Act_Decl_Id : Entity_Id;
4902 Act_Decl : Node_Id;
4903 Act_Spec : Node_Id;
4904 Act_Tree : Node_Id;
4905
4906 Env_Installed : Boolean := False;
4907 Gen_Unit : Entity_Id;
4908 Gen_Decl : Node_Id;
4909 Pack_Id : Entity_Id;
4910 Parent_Installed : Boolean := False;
4911
4912 Renaming_List : List_Id;
4913 -- The list of declarations that link formals and actuals of the
4914 -- instance. These are subtype declarations for formal types, and
4915 -- renaming declarations for other formals. The subprogram declaration
4916 -- for the instance is then appended to the list, and the last item on
4917 -- the list is the renaming declaration for the instance.
4918
4919 procedure Analyze_Instance_And_Renamings;
4920 -- The instance must be analyzed in a context that includes the mappings
4921 -- of generic parameters into actuals. We create a package declaration
4922 -- for this purpose, and a subprogram with an internal name within the
4923 -- package. The subprogram instance is simply an alias for the internal
4924 -- subprogram, declared in the current scope.
4925
4926 procedure Build_Subprogram_Renaming;
4927 -- If the subprogram is recursive, there are occurrences of the name of
4928 -- the generic within the body, which must resolve to the current
4929 -- instance. We add a renaming declaration after the declaration, which
4930 -- is available in the instance body, as well as in the analysis of
4931 -- aspects that appear in the generic. This renaming declaration is
4932 -- inserted after the instance declaration which it renames.
4933
4934 ------------------------------------
4935 -- Analyze_Instance_And_Renamings --
4936 ------------------------------------
4937
4938 procedure Analyze_Instance_And_Renamings is
4939 Def_Ent : constant Entity_Id := Defining_Entity (N);
4940 Pack_Decl : Node_Id;
4941
4942 begin
4943 if Nkind (Parent (N)) = N_Compilation_Unit then
4944
4945 -- For the case of a compilation unit, the container package has
4946 -- the same name as the instantiation, to insure that the binder
4947 -- calls the elaboration procedure with the right name. Copy the
4948 -- entity of the instance, which may have compilation level flags
4949 -- (e.g. Is_Child_Unit) set.
4950
4951 Pack_Id := New_Copy (Def_Ent);
4952
4953 else
4954 -- Otherwise we use the name of the instantiation concatenated
4955 -- with its source position to ensure uniqueness if there are
4956 -- several instantiations with the same name.
4957
4958 Pack_Id :=
4959 Make_Defining_Identifier (Loc,
4960 Chars => New_External_Name
4961 (Related_Id => Chars (Def_Ent),
4962 Suffix => "GP",
4963 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4964 end if;
4965
4966 Pack_Decl :=
4967 Make_Package_Declaration (Loc,
4968 Specification => Make_Package_Specification (Loc,
4969 Defining_Unit_Name => Pack_Id,
4970 Visible_Declarations => Renaming_List,
4971 End_Label => Empty));
4972
4973 Set_Instance_Spec (N, Pack_Decl);
4974 Set_Is_Generic_Instance (Pack_Id);
4975 Set_Debug_Info_Needed (Pack_Id);
4976
4977 -- Case of not a compilation unit
4978
4979 if Nkind (Parent (N)) /= N_Compilation_Unit then
4980 Mark_Rewrite_Insertion (Pack_Decl);
4981 Insert_Before (N, Pack_Decl);
4982 Set_Has_Completion (Pack_Id);
4983
4984 -- Case of an instantiation that is a compilation unit
4985
4986 -- Place declaration on current node so context is complete for
4987 -- analysis (including nested instantiations), and for use in a
4988 -- context_clause (see Analyze_With_Clause).
4989
4990 else
4991 Set_Unit (Parent (N), Pack_Decl);
4992 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4993 end if;
4994
4995 Analyze (Pack_Decl);
4996 Check_Formal_Packages (Pack_Id);
4997 Set_Is_Generic_Instance (Pack_Id, False);
4998
4999 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
5000 -- above???
5001
5002 -- Body of the enclosing package is supplied when instantiating the
5003 -- subprogram body, after semantic analysis is completed.
5004
5005 if Nkind (Parent (N)) = N_Compilation_Unit then
5006
5007 -- Remove package itself from visibility, so it does not
5008 -- conflict with subprogram.
5009
5010 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5011
5012 -- Set name and scope of internal subprogram so that the proper
5013 -- external name will be generated. The proper scope is the scope
5014 -- of the wrapper package. We need to generate debugging info for
5015 -- the internal subprogram, so set flag accordingly.
5016
5017 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5018 Set_Scope (Anon_Id, Scope (Pack_Id));
5019
5020 -- Mark wrapper package as referenced, to avoid spurious warnings
5021 -- if the instantiation appears in various with_ clauses of
5022 -- subunits of the main unit.
5023
5024 Set_Referenced (Pack_Id);
5025 end if;
5026
5027 Set_Is_Generic_Instance (Anon_Id);
5028 Set_Debug_Info_Needed (Anon_Id);
5029 Act_Decl_Id := New_Copy (Anon_Id);
5030
5031 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5032 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5033 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5034
5035 -- Subprogram instance comes from source only if generic does
5036
5037 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
5038
5039 -- If the instance is a child unit, mark the Id accordingly. Mark
5040 -- the anonymous entity as well, which is the real subprogram and
5041 -- which is used when the instance appears in a context clause.
5042 -- Similarly, propagate the Is_Eliminated flag to handle properly
5043 -- nested eliminated subprograms.
5044
5045 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5046 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5047 New_Overloaded_Entity (Act_Decl_Id);
5048 Check_Eliminated (Act_Decl_Id);
5049 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5050
5051 -- In compilation unit case, kill elaboration checks on the
5052 -- instantiation, since they are never needed -- the body is
5053 -- instantiated at the same point as the spec.
5054
5055 if Nkind (Parent (N)) = N_Compilation_Unit then
5056 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5057 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5058 Set_Is_Compilation_Unit (Anon_Id);
5059
5060 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5061 end if;
5062
5063 -- The instance is not a freezing point for the new subprogram.
5064 -- The anonymous subprogram may have a freeze node, created for
5065 -- some delayed aspects. This freeze node must not be inherited
5066 -- by the visible subprogram entity.
5067
5068 Set_Is_Frozen (Act_Decl_Id, False);
5069 Set_Freeze_Node (Act_Decl_Id, Empty);
5070
5071 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5072 Valid_Operator_Definition (Act_Decl_Id);
5073 end if;
5074
5075 Set_Alias (Act_Decl_Id, Anon_Id);
5076 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5077 Set_Has_Completion (Act_Decl_Id);
5078 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5079
5080 if Nkind (Parent (N)) = N_Compilation_Unit then
5081 Set_Body_Required (Parent (N), False);
5082 end if;
5083 end Analyze_Instance_And_Renamings;
5084
5085 -------------------------------
5086 -- Build_Subprogram_Renaming --
5087 -------------------------------
5088
5089 procedure Build_Subprogram_Renaming is
5090 Renaming_Decl : Node_Id;
5091 Unit_Renaming : Node_Id;
5092
5093 begin
5094 Unit_Renaming :=
5095 Make_Subprogram_Renaming_Declaration (Loc,
5096 Specification =>
5097 Copy_Generic_Node
5098 (Specification (Original_Node (Gen_Decl)),
5099 Empty,
5100 Instantiating => True),
5101 Name => New_Occurrence_Of (Anon_Id, Loc));
5102
5103 -- The generic may be a a child unit. The renaming needs an
5104 -- identifier with the proper name.
5105
5106 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5107 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5108
5109 -- If there is a formal subprogram with the same name as the unit
5110 -- itself, do not add this renaming declaration, to prevent
5111 -- ambiguities when there is a call with that name in the body.
5112 -- This is a partial and ugly fix for one ACATS test. ???
5113
5114 Renaming_Decl := First (Renaming_List);
5115 while Present (Renaming_Decl) loop
5116 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5117 and then
5118 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5119 then
5120 exit;
5121 end if;
5122
5123 Next (Renaming_Decl);
5124 end loop;
5125
5126 if No (Renaming_Decl) then
5127 Append (Unit_Renaming, Renaming_List);
5128 end if;
5129 end Build_Subprogram_Renaming;
5130
5131 -- Local variables
5132
5133 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
5134 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
5135
5136 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
5137 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
5138 -- Save the SPARK_Mode-related data for restore on exit
5139
5140 Mode : Ghost_Mode_Type;
5141 Mode_Set : Boolean := False;
5142
5143 Vis_Prims_List : Elist_Id := No_Elist;
5144 -- List of primitives made temporarily visible in the instantiation
5145 -- to match the visibility of the formal type
5146
5147 -- Start of processing for Analyze_Subprogram_Instantiation
5148
5149 begin
5150 Check_SPARK_05_Restriction ("generic is not allowed", N);
5151
5152 -- Very first thing: check for special Text_IO unit in case we are
5153 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5154 -- such an instantiation is bogus (these are packages, not subprograms),
5155 -- but we get a better error message if we do this.
5156
5157 Check_Text_IO_Special_Unit (Gen_Id);
5158
5159 -- Make node global for error reporting
5160
5161 Instantiation_Node := N;
5162
5163 -- For package instantiations we turn off style checks, because they
5164 -- will have been emitted in the generic. For subprogram instantiations
5165 -- we want to apply at least the check on overriding indicators so we
5166 -- do not modify the style check status.
5167
5168 -- The renaming declarations for the actuals do not come from source and
5169 -- will not generate spurious warnings.
5170
5171 Preanalyze_Actuals (N);
5172
5173 Init_Env;
5174 Env_Installed := True;
5175 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5176 Gen_Unit := Entity (Gen_Id);
5177
5178 -- A subprogram instantiation is Ghost when it is subject to pragma
5179 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5180 -- that any nodes generated during analysis and expansion are marked as
5181 -- Ghost.
5182
5183 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit, Mode);
5184 Mode_Set := True;
5185
5186 Generate_Reference (Gen_Unit, Gen_Id);
5187
5188 if Nkind (Gen_Id) = N_Identifier
5189 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5190 then
5191 Error_Msg_NE
5192 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5193 end if;
5194
5195 if Etype (Gen_Unit) = Any_Type then
5196 Restore_Env;
5197 goto Leave;
5198 end if;
5199
5200 -- Verify that it is a generic subprogram of the right kind, and that
5201 -- it does not lead to a circular instantiation.
5202
5203 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5204 Error_Msg_NE
5205 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5206
5207 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5208 Error_Msg_NE
5209 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5210
5211 elsif In_Open_Scopes (Gen_Unit) then
5212 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5213
5214 else
5215 Set_Entity (Gen_Id, Gen_Unit);
5216 Set_Is_Instantiated (Gen_Unit);
5217
5218 if In_Extended_Main_Source_Unit (N) then
5219 Generate_Reference (Gen_Unit, N);
5220 end if;
5221
5222 -- If renaming, get original unit
5223
5224 if Present (Renamed_Object (Gen_Unit))
5225 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5226 E_Generic_Function)
5227 then
5228 Gen_Unit := Renamed_Object (Gen_Unit);
5229 Set_Is_Instantiated (Gen_Unit);
5230 Generate_Reference (Gen_Unit, N);
5231 end if;
5232
5233 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5234 Error_Msg_Node_2 := Current_Scope;
5235 Error_Msg_NE
5236 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5237 Circularity_Detected := True;
5238 Restore_Hidden_Primitives (Vis_Prims_List);
5239 goto Leave;
5240 end if;
5241
5242 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5243
5244 -- Initialize renamings map, for error checking
5245
5246 Generic_Renamings.Set_Last (0);
5247 Generic_Renamings_HTable.Reset;
5248
5249 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5250
5251 -- Copy original generic tree, to produce text for instantiation
5252
5253 Act_Tree :=
5254 Copy_Generic_Node
5255 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5256
5257 -- Inherit overriding indicator from instance node
5258
5259 Act_Spec := Specification (Act_Tree);
5260 Set_Must_Override (Act_Spec, Must_Override (N));
5261 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5262
5263 Renaming_List :=
5264 Analyze_Associations
5265 (I_Node => N,
5266 Formals => Generic_Formal_Declarations (Act_Tree),
5267 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5268
5269 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5270
5271 -- The subprogram itself cannot contain a nested instance, so the
5272 -- current parent is left empty.
5273
5274 Set_Instance_Env (Gen_Unit, Empty);
5275
5276 -- Build the subprogram declaration, which does not appear in the
5277 -- generic template, and give it a sloc consistent with that of the
5278 -- template.
5279
5280 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5281 Set_Generic_Parent (Act_Spec, Gen_Unit);
5282 Act_Decl :=
5283 Make_Subprogram_Declaration (Sloc (Act_Spec),
5284 Specification => Act_Spec);
5285
5286 -- The aspects have been copied previously, but they have to be
5287 -- linked explicitly to the new subprogram declaration. Explicit
5288 -- pre/postconditions on the instance are analyzed below, in a
5289 -- separate step.
5290
5291 Move_Aspects (Act_Tree, To => Act_Decl);
5292 Set_Categorization_From_Pragmas (Act_Decl);
5293
5294 if Parent_Installed then
5295 Hide_Current_Scope;
5296 end if;
5297
5298 Append (Act_Decl, Renaming_List);
5299
5300 -- Contract-related source pragmas that follow a generic subprogram
5301 -- must be instantiated explicitly because they are not part of the
5302 -- subprogram template.
5303
5304 Instantiate_Subprogram_Contract
5305 (Original_Node (Gen_Decl), Renaming_List);
5306
5307 Build_Subprogram_Renaming;
5308 Analyze_Instance_And_Renamings;
5309
5310 -- If the generic is marked Import (Intrinsic), then so is the
5311 -- instance. This indicates that there is no body to instantiate. If
5312 -- generic is marked inline, so it the instance, and the anonymous
5313 -- subprogram it renames. If inlined, or else if inlining is enabled
5314 -- for the compilation, we generate the instance body even if it is
5315 -- not within the main unit.
5316
5317 if Is_Intrinsic_Subprogram (Gen_Unit) then
5318 Set_Is_Intrinsic_Subprogram (Anon_Id);
5319 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5320
5321 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5322 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5323 end if;
5324 end if;
5325
5326 -- Inherit convention from generic unit. Intrinsic convention, as for
5327 -- an instance of unchecked conversion, is not inherited because an
5328 -- explicit Ada instance has been created.
5329
5330 if Has_Convention_Pragma (Gen_Unit)
5331 and then Convention (Gen_Unit) /= Convention_Intrinsic
5332 then
5333 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5334 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5335 end if;
5336
5337 Generate_Definition (Act_Decl_Id);
5338
5339 -- Inherit all inlining-related flags which apply to the generic in
5340 -- the subprogram and its declaration.
5341
5342 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5343 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5344
5345 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5346 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5347
5348 Set_Has_Pragma_Inline_Always
5349 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5350 Set_Has_Pragma_Inline_Always
5351 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5352
5353 -- If the context of the instance is subject to SPARK_Mode "off" or
5354 -- the annotation is altogether missing, set the global flag which
5355 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5356 -- the instance.
5357
5358 if SPARK_Mode /= On then
5359 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5360
5361 -- Mark both the instance spec and the anonymous package in case
5362 -- the body is instantiated at a later pass. This preserves the
5363 -- original context in effect for the body.
5364
5365 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
5366 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
5367 end if;
5368
5369 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5370 Check_Elab_Instantiation (N);
5371 end if;
5372
5373 if Is_Dispatching_Operation (Act_Decl_Id)
5374 and then Ada_Version >= Ada_2005
5375 then
5376 declare
5377 Formal : Entity_Id;
5378
5379 begin
5380 Formal := First_Formal (Act_Decl_Id);
5381 while Present (Formal) loop
5382 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5383 and then Is_Controlling_Formal (Formal)
5384 and then not Can_Never_Be_Null (Formal)
5385 then
5386 Error_Msg_NE
5387 ("access parameter& is controlling,", N, Formal);
5388 Error_Msg_NE
5389 ("\corresponding parameter of & must be explicitly "
5390 & "null-excluding", N, Gen_Id);
5391 end if;
5392
5393 Next_Formal (Formal);
5394 end loop;
5395 end;
5396 end if;
5397
5398 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5399
5400 Validate_Categorization_Dependency (N, Act_Decl_Id);
5401
5402 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5403 Inherit_Context (Gen_Decl, N);
5404
5405 Restore_Private_Views (Pack_Id, False);
5406
5407 -- If the context requires a full instantiation, mark node for
5408 -- subsequent construction of the body.
5409
5410 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5411 Check_Forward_Instantiation (Gen_Decl);
5412
5413 -- The wrapper package is always delayed, because it does not
5414 -- constitute a freeze point, but to insure that the freeze node
5415 -- is placed properly, it is created directly when instantiating
5416 -- the body (otherwise the freeze node might appear to early for
5417 -- nested instantiations). For ASIS purposes, indicate that the
5418 -- wrapper package has replaced the instantiation node.
5419
5420 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5421 Rewrite (N, Unit (Parent (N)));
5422 Set_Unit (Parent (N), N);
5423 end if;
5424
5425 -- Replace instance node for library-level instantiations of
5426 -- intrinsic subprograms, for ASIS use.
5427
5428 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5429 Rewrite (N, Unit (Parent (N)));
5430 Set_Unit (Parent (N), N);
5431 end if;
5432
5433 if Parent_Installed then
5434 Remove_Parent;
5435 end if;
5436
5437 Restore_Hidden_Primitives (Vis_Prims_List);
5438 Restore_Env;
5439 Env_Installed := False;
5440 Generic_Renamings.Set_Last (0);
5441 Generic_Renamings_HTable.Reset;
5442 end if;
5443
5444 <<Leave>>
5445 if Has_Aspects (N) then
5446 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5447 end if;
5448
5449 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
5450 SPARK_Mode := Save_SM;
5451 SPARK_Mode_Pragma := Save_SMP;
5452
5453 if Mode_Set then
5454 Restore_Ghost_Mode (Mode);
5455 end if;
5456
5457 exception
5458 when Instantiation_Error =>
5459 if Parent_Installed then
5460 Remove_Parent;
5461 end if;
5462
5463 if Env_Installed then
5464 Restore_Env;
5465 end if;
5466
5467 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
5468 SPARK_Mode := Save_SM;
5469 SPARK_Mode_Pragma := Save_SMP;
5470
5471 if Mode_Set then
5472 Restore_Ghost_Mode (Mode);
5473 end if;
5474 end Analyze_Subprogram_Instantiation;
5475
5476 -------------------------
5477 -- Get_Associated_Node --
5478 -------------------------
5479
5480 function Get_Associated_Node (N : Node_Id) return Node_Id is
5481 Assoc : Node_Id;
5482
5483 begin
5484 Assoc := Associated_Node (N);
5485
5486 if Nkind (Assoc) /= Nkind (N) then
5487 return Assoc;
5488
5489 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5490 return Assoc;
5491
5492 else
5493 -- If the node is part of an inner generic, it may itself have been
5494 -- remapped into a further generic copy. Associated_Node is otherwise
5495 -- used for the entity of the node, and will be of a different node
5496 -- kind, or else N has been rewritten as a literal or function call.
5497
5498 while Present (Associated_Node (Assoc))
5499 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5500 loop
5501 Assoc := Associated_Node (Assoc);
5502 end loop;
5503
5504 -- Follow and additional link in case the final node was rewritten.
5505 -- This can only happen with nested generic units.
5506
5507 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5508 and then Present (Associated_Node (Assoc))
5509 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5510 N_Explicit_Dereference,
5511 N_Integer_Literal,
5512 N_Real_Literal,
5513 N_String_Literal))
5514 then
5515 Assoc := Associated_Node (Assoc);
5516 end if;
5517
5518 -- An additional special case: an unconstrained type in an object
5519 -- declaration may have been rewritten as a local subtype constrained
5520 -- by the expression in the declaration. We need to recover the
5521 -- original entity which may be global.
5522
5523 if Present (Original_Node (Assoc))
5524 and then Nkind (Parent (N)) = N_Object_Declaration
5525 then
5526 Assoc := Original_Node (Assoc);
5527 end if;
5528
5529 return Assoc;
5530 end if;
5531 end Get_Associated_Node;
5532
5533 ----------------------------
5534 -- Build_Function_Wrapper --
5535 ----------------------------
5536
5537 function Build_Function_Wrapper
5538 (Formal_Subp : Entity_Id;
5539 Actual_Subp : Entity_Id) return Node_Id
5540 is
5541 Loc : constant Source_Ptr := Sloc (Current_Scope);
5542 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5543 Actuals : List_Id;
5544 Decl : Node_Id;
5545 Func_Name : Node_Id;
5546 Func : Entity_Id;
5547 Parm_Type : Node_Id;
5548 Profile : List_Id := New_List;
5549 Spec : Node_Id;
5550 Act_F : Entity_Id;
5551 Form_F : Entity_Id;
5552 New_F : Entity_Id;
5553
5554 begin
5555 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5556
5557 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5558 Set_Ekind (Func, E_Function);
5559 Set_Is_Generic_Actual_Subprogram (Func);
5560
5561 Actuals := New_List;
5562 Profile := New_List;
5563
5564 Act_F := First_Formal (Actual_Subp);
5565 Form_F := First_Formal (Formal_Subp);
5566 while Present (Form_F) loop
5567
5568 -- Create new formal for profile of wrapper, and add a reference
5569 -- to it in the list of actuals for the enclosing call. The name
5570 -- must be that of the formal in the formal subprogram, because
5571 -- calls to it in the generic body may use named associations.
5572
5573 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5574
5575 Parm_Type :=
5576 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5577
5578 Append_To (Profile,
5579 Make_Parameter_Specification (Loc,
5580 Defining_Identifier => New_F,
5581 Parameter_Type => Parm_Type));
5582
5583 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5584 Next_Formal (Form_F);
5585
5586 if Present (Act_F) then
5587 Next_Formal (Act_F);
5588 end if;
5589 end loop;
5590
5591 Spec :=
5592 Make_Function_Specification (Loc,
5593 Defining_Unit_Name => Func,
5594 Parameter_Specifications => Profile,
5595 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5596
5597 Decl :=
5598 Make_Expression_Function (Loc,
5599 Specification => Spec,
5600 Expression =>
5601 Make_Function_Call (Loc,
5602 Name => Func_Name,
5603 Parameter_Associations => Actuals));
5604
5605 return Decl;
5606 end Build_Function_Wrapper;
5607
5608 ----------------------------
5609 -- Build_Operator_Wrapper --
5610 ----------------------------
5611
5612 function Build_Operator_Wrapper
5613 (Formal_Subp : Entity_Id;
5614 Actual_Subp : Entity_Id) return Node_Id
5615 is
5616 Loc : constant Source_Ptr := Sloc (Current_Scope);
5617 Ret_Type : constant Entity_Id :=
5618 Get_Instance_Of (Etype (Formal_Subp));
5619 Op_Type : constant Entity_Id :=
5620 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
5621 Is_Binary : constant Boolean :=
5622 Present (Next_Formal (First_Formal (Formal_Subp)));
5623
5624 Decl : Node_Id;
5625 Expr : Node_Id;
5626 F1, F2 : Entity_Id;
5627 Func : Entity_Id;
5628 Op_Name : Name_Id;
5629 Spec : Node_Id;
5630 L, R : Node_Id;
5631
5632 begin
5633 Op_Name := Chars (Actual_Subp);
5634
5635 -- Create entities for wrapper function and its formals
5636
5637 F1 := Make_Temporary (Loc, 'A');
5638 F2 := Make_Temporary (Loc, 'B');
5639 L := New_Occurrence_Of (F1, Loc);
5640 R := New_Occurrence_Of (F2, Loc);
5641
5642 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5643 Set_Ekind (Func, E_Function);
5644 Set_Is_Generic_Actual_Subprogram (Func);
5645
5646 Spec :=
5647 Make_Function_Specification (Loc,
5648 Defining_Unit_Name => Func,
5649 Parameter_Specifications => New_List (
5650 Make_Parameter_Specification (Loc,
5651 Defining_Identifier => F1,
5652 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
5653 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5654
5655 if Is_Binary then
5656 Append_To (Parameter_Specifications (Spec),
5657 Make_Parameter_Specification (Loc,
5658 Defining_Identifier => F2,
5659 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
5660 end if;
5661
5662 -- Build expression as a function call, or as an operator node
5663 -- that corresponds to the name of the actual, starting with
5664 -- binary operators.
5665
5666 if Op_Name not in Any_Operator_Name then
5667 Expr :=
5668 Make_Function_Call (Loc,
5669 Name =>
5670 New_Occurrence_Of (Actual_Subp, Loc),
5671 Parameter_Associations => New_List (L));
5672
5673 if Is_Binary then
5674 Append_To (Parameter_Associations (Expr), R);
5675 end if;
5676
5677 -- Binary operators
5678
5679 elsif Is_Binary then
5680 if Op_Name = Name_Op_And then
5681 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
5682 elsif Op_Name = Name_Op_Or then
5683 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
5684 elsif Op_Name = Name_Op_Xor then
5685 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
5686 elsif Op_Name = Name_Op_Eq then
5687 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
5688 elsif Op_Name = Name_Op_Ne then
5689 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
5690 elsif Op_Name = Name_Op_Le then
5691 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
5692 elsif Op_Name = Name_Op_Gt then
5693 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
5694 elsif Op_Name = Name_Op_Ge then
5695 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
5696 elsif Op_Name = Name_Op_Lt then
5697 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
5698 elsif Op_Name = Name_Op_Add then
5699 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
5700 elsif Op_Name = Name_Op_Subtract then
5701 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
5702 elsif Op_Name = Name_Op_Concat then
5703 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
5704 elsif Op_Name = Name_Op_Multiply then
5705 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
5706 elsif Op_Name = Name_Op_Divide then
5707 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
5708 elsif Op_Name = Name_Op_Mod then
5709 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
5710 elsif Op_Name = Name_Op_Rem then
5711 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
5712 elsif Op_Name = Name_Op_Expon then
5713 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
5714 end if;
5715
5716 -- Unary operators
5717
5718 else
5719 if Op_Name = Name_Op_Add then
5720 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
5721 elsif Op_Name = Name_Op_Subtract then
5722 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
5723 elsif Op_Name = Name_Op_Abs then
5724 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
5725 elsif Op_Name = Name_Op_Not then
5726 Expr := Make_Op_Not (Loc, Right_Opnd => L);
5727 end if;
5728 end if;
5729
5730 Decl :=
5731 Make_Expression_Function (Loc,
5732 Specification => Spec,
5733 Expression => Expr);
5734
5735 return Decl;
5736 end Build_Operator_Wrapper;
5737
5738 -------------------------------------------
5739 -- Build_Instance_Compilation_Unit_Nodes --
5740 -------------------------------------------
5741
5742 procedure Build_Instance_Compilation_Unit_Nodes
5743 (N : Node_Id;
5744 Act_Body : Node_Id;
5745 Act_Decl : Node_Id)
5746 is
5747 Decl_Cunit : Node_Id;
5748 Body_Cunit : Node_Id;
5749 Citem : Node_Id;
5750 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5751 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5752
5753 begin
5754 -- A new compilation unit node is built for the instance declaration
5755
5756 Decl_Cunit :=
5757 Make_Compilation_Unit (Sloc (N),
5758 Context_Items => Empty_List,
5759 Unit => Act_Decl,
5760 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5761
5762 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5763
5764 -- The new compilation unit is linked to its body, but both share the
5765 -- same file, so we do not set Body_Required on the new unit so as not
5766 -- to create a spurious dependency on a non-existent body in the ali.
5767 -- This simplifies CodePeer unit traversal.
5768
5769 -- We use the original instantiation compilation unit as the resulting
5770 -- compilation unit of the instance, since this is the main unit.
5771
5772 Rewrite (N, Act_Body);
5773
5774 -- Propagate the aspect specifications from the package body template to
5775 -- the instantiated version of the package body.
5776
5777 if Has_Aspects (Act_Body) then
5778 Set_Aspect_Specifications
5779 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5780 end if;
5781
5782 Body_Cunit := Parent (N);
5783
5784 -- The two compilation unit nodes are linked by the Library_Unit field
5785
5786 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5787 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5788
5789 -- Preserve the private nature of the package if needed
5790
5791 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5792
5793 -- If the instance is not the main unit, its context, categorization
5794 -- and elaboration entity are not relevant to the compilation.
5795
5796 if Body_Cunit /= Cunit (Main_Unit) then
5797 Make_Instance_Unit (Body_Cunit, In_Main => False);
5798 return;
5799 end if;
5800
5801 -- The context clause items on the instantiation, which are now attached
5802 -- to the body compilation unit (since the body overwrote the original
5803 -- instantiation node), semantically belong on the spec, so copy them
5804 -- there. It's harmless to leave them on the body as well. In fact one
5805 -- could argue that they belong in both places.
5806
5807 Citem := First (Context_Items (Body_Cunit));
5808 while Present (Citem) loop
5809 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5810 Next (Citem);
5811 end loop;
5812
5813 -- Propagate categorization flags on packages, so that they appear in
5814 -- the ali file for the spec of the unit.
5815
5816 if Ekind (New_Main) = E_Package then
5817 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5818 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5819 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5820 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5821 Set_Is_Remote_Call_Interface
5822 (Old_Main, Is_Remote_Call_Interface (New_Main));
5823 end if;
5824
5825 -- Make entry in Units table, so that binder can generate call to
5826 -- elaboration procedure for body, if any.
5827
5828 Make_Instance_Unit (Body_Cunit, In_Main => True);
5829 Main_Unit_Entity := New_Main;
5830 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5831
5832 -- Build elaboration entity, since the instance may certainly generate
5833 -- elaboration code requiring a flag for protection.
5834
5835 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5836 end Build_Instance_Compilation_Unit_Nodes;
5837
5838 -----------------------------
5839 -- Check_Access_Definition --
5840 -----------------------------
5841
5842 procedure Check_Access_Definition (N : Node_Id) is
5843 begin
5844 pragma Assert
5845 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5846 null;
5847 end Check_Access_Definition;
5848
5849 -----------------------------------
5850 -- Check_Formal_Package_Instance --
5851 -----------------------------------
5852
5853 -- If the formal has specific parameters, they must match those of the
5854 -- actual. Both of them are instances, and the renaming declarations for
5855 -- their formal parameters appear in the same order in both. The analyzed
5856 -- formal has been analyzed in the context of the current instance.
5857
5858 procedure Check_Formal_Package_Instance
5859 (Formal_Pack : Entity_Id;
5860 Actual_Pack : Entity_Id)
5861 is
5862 E1 : Entity_Id := First_Entity (Actual_Pack);
5863 E2 : Entity_Id := First_Entity (Formal_Pack);
5864 Prev_E1 : Entity_Id;
5865
5866 Expr1 : Node_Id;
5867 Expr2 : Node_Id;
5868
5869 procedure Check_Mismatch (B : Boolean);
5870 -- Common error routine for mismatch between the parameters of the
5871 -- actual instance and those of the formal package.
5872
5873 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5874 -- The formal may come from a nested formal package, and the actual may
5875 -- have been constant-folded. To determine whether the two denote the
5876 -- same entity we may have to traverse several definitions to recover
5877 -- the ultimate entity that they refer to.
5878
5879 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
5880 -- The formal and the actual must be identical, but if both are
5881 -- given by attributes they end up renaming different generated bodies,
5882 -- and we must verify that the attributes themselves match.
5883
5884 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5885 -- Similarly, if the formal comes from a nested formal package, the
5886 -- actual may designate the formal through multiple renamings, which
5887 -- have to be followed to determine the original variable in question.
5888
5889 --------------------
5890 -- Check_Mismatch --
5891 --------------------
5892
5893 procedure Check_Mismatch (B : Boolean) is
5894 -- A Formal_Type_Declaration for a derived private type is rewritten
5895 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
5896 -- which is why we examine the original node.
5897
5898 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
5899
5900 begin
5901 if Kind = N_Formal_Type_Declaration then
5902 return;
5903
5904 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5905 N_Formal_Package_Declaration)
5906 or else Kind in N_Formal_Subprogram_Declaration
5907 then
5908 null;
5909
5910 -- Ada 2012: If both formal and actual are incomplete types they
5911 -- are conformant.
5912
5913 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
5914 null;
5915
5916 elsif B then
5917 Error_Msg_NE
5918 ("actual for & in actual instance does not match formal",
5919 Parent (Actual_Pack), E1);
5920 end if;
5921 end Check_Mismatch;
5922
5923 --------------------------------
5924 -- Same_Instantiated_Constant --
5925 --------------------------------
5926
5927 function Same_Instantiated_Constant
5928 (E1, E2 : Entity_Id) return Boolean
5929 is
5930 Ent : Entity_Id;
5931
5932 begin
5933 Ent := E2;
5934 while Present (Ent) loop
5935 if E1 = Ent then
5936 return True;
5937
5938 elsif Ekind (Ent) /= E_Constant then
5939 return False;
5940
5941 elsif Is_Entity_Name (Constant_Value (Ent)) then
5942 if Entity (Constant_Value (Ent)) = E1 then
5943 return True;
5944 else
5945 Ent := Entity (Constant_Value (Ent));
5946 end if;
5947
5948 -- The actual may be a constant that has been folded. Recover
5949 -- original name.
5950
5951 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5952 Ent := Entity (Original_Node (Constant_Value (Ent)));
5953
5954 else
5955 return False;
5956 end if;
5957 end loop;
5958
5959 return False;
5960 end Same_Instantiated_Constant;
5961
5962 --------------------------------
5963 -- Same_Instantiated_Function --
5964 --------------------------------
5965
5966 function Same_Instantiated_Function
5967 (E1, E2 : Entity_Id) return Boolean
5968 is
5969 U1, U2 : Node_Id;
5970 begin
5971 if Alias (E1) = Alias (E2) then
5972 return True;
5973
5974 elsif Present (Alias (E2)) then
5975 U1 := Original_Node (Unit_Declaration_Node (E1));
5976 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
5977
5978 return Nkind (U1) = N_Subprogram_Renaming_Declaration
5979 and then Nkind (Name (U1)) = N_Attribute_Reference
5980
5981 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
5982 and then Nkind (Name (U2)) = N_Attribute_Reference
5983
5984 and then
5985 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
5986 else
5987 return False;
5988 end if;
5989 end Same_Instantiated_Function;
5990
5991 --------------------------------
5992 -- Same_Instantiated_Variable --
5993 --------------------------------
5994
5995 function Same_Instantiated_Variable
5996 (E1, E2 : Entity_Id) return Boolean
5997 is
5998 function Original_Entity (E : Entity_Id) return Entity_Id;
5999 -- Follow chain of renamings to the ultimate ancestor
6000
6001 ---------------------
6002 -- Original_Entity --
6003 ---------------------
6004
6005 function Original_Entity (E : Entity_Id) return Entity_Id is
6006 Orig : Entity_Id;
6007
6008 begin
6009 Orig := E;
6010 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6011 and then Present (Renamed_Object (Orig))
6012 and then Is_Entity_Name (Renamed_Object (Orig))
6013 loop
6014 Orig := Entity (Renamed_Object (Orig));
6015 end loop;
6016
6017 return Orig;
6018 end Original_Entity;
6019
6020 -- Start of processing for Same_Instantiated_Variable
6021
6022 begin
6023 return Ekind (E1) = Ekind (E2)
6024 and then Original_Entity (E1) = Original_Entity (E2);
6025 end Same_Instantiated_Variable;
6026
6027 -- Start of processing for Check_Formal_Package_Instance
6028
6029 begin
6030 Prev_E1 := E1;
6031 while Present (E1) and then Present (E2) loop
6032 exit when Ekind (E1) = E_Package
6033 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6034
6035 -- If the formal is the renaming of the formal package, this
6036 -- is the end of its formal part, which may occur before the
6037 -- end of the formal part in the actual in the presence of
6038 -- defaulted parameters in the formal package.
6039
6040 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6041 and then Renamed_Entity (E2) = Scope (E2);
6042
6043 -- The analysis of the actual may generate additional internal
6044 -- entities. If the formal is defaulted, there is no corresponding
6045 -- analysis and the internal entities must be skipped, until we
6046 -- find corresponding entities again.
6047
6048 if Comes_From_Source (E2)
6049 and then not Comes_From_Source (E1)
6050 and then Chars (E1) /= Chars (E2)
6051 then
6052 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6053 Next_Entity (E1);
6054 end loop;
6055 end if;
6056
6057 if No (E1) then
6058 return;
6059
6060 -- Entities may be declared without full declaration, such as
6061 -- itypes and predefined operators (concatenation for arrays, eg).
6062 -- Skip it and keep the formal entity to find a later match for it.
6063
6064 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6065 E1 := Prev_E1;
6066 goto Next_E;
6067
6068 -- If the formal entity comes from a formal declaration, it was
6069 -- defaulted in the formal package, and no check is needed on it.
6070
6071 elsif Nkind_In (Original_Node (Parent (E2)),
6072 N_Formal_Object_Declaration,
6073 N_Formal_Type_Declaration)
6074 then
6075 -- If the formal is a tagged type the corresponding class-wide
6076 -- type has been generated as well, and it must be skipped.
6077
6078 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6079 Next_Entity (E2);
6080 end if;
6081
6082 goto Next_E;
6083
6084 -- Ditto for defaulted formal subprograms.
6085
6086 elsif Is_Overloadable (E1)
6087 and then Nkind (Unit_Declaration_Node (E2)) in
6088 N_Formal_Subprogram_Declaration
6089 then
6090 goto Next_E;
6091
6092 elsif Is_Type (E1) then
6093
6094 -- Subtypes must statically match. E1, E2 are the local entities
6095 -- that are subtypes of the actuals. Itypes generated for other
6096 -- parameters need not be checked, the check will be performed
6097 -- on the parameters themselves.
6098
6099 -- If E2 is a formal type declaration, it is a defaulted parameter
6100 -- and needs no checking.
6101
6102 if not Is_Itype (E1) and then not Is_Itype (E2) then
6103 Check_Mismatch
6104 (not Is_Type (E2)
6105 or else Etype (E1) /= Etype (E2)
6106 or else not Subtypes_Statically_Match (E1, E2));
6107 end if;
6108
6109 elsif Ekind (E1) = E_Constant then
6110
6111 -- IN parameters must denote the same static value, or the same
6112 -- constant, or the literal null.
6113
6114 Expr1 := Expression (Parent (E1));
6115
6116 if Ekind (E2) /= E_Constant then
6117 Check_Mismatch (True);
6118 goto Next_E;
6119 else
6120 Expr2 := Expression (Parent (E2));
6121 end if;
6122
6123 if Is_OK_Static_Expression (Expr1) then
6124 if not Is_OK_Static_Expression (Expr2) then
6125 Check_Mismatch (True);
6126
6127 elsif Is_Discrete_Type (Etype (E1)) then
6128 declare
6129 V1 : constant Uint := Expr_Value (Expr1);
6130 V2 : constant Uint := Expr_Value (Expr2);
6131 begin
6132 Check_Mismatch (V1 /= V2);
6133 end;
6134
6135 elsif Is_Real_Type (Etype (E1)) then
6136 declare
6137 V1 : constant Ureal := Expr_Value_R (Expr1);
6138 V2 : constant Ureal := Expr_Value_R (Expr2);
6139 begin
6140 Check_Mismatch (V1 /= V2);
6141 end;
6142
6143 elsif Is_String_Type (Etype (E1))
6144 and then Nkind (Expr1) = N_String_Literal
6145 then
6146 if Nkind (Expr2) /= N_String_Literal then
6147 Check_Mismatch (True);
6148 else
6149 Check_Mismatch
6150 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6151 end if;
6152 end if;
6153
6154 elsif Is_Entity_Name (Expr1) then
6155 if Is_Entity_Name (Expr2) then
6156 if Entity (Expr1) = Entity (Expr2) then
6157 null;
6158 else
6159 Check_Mismatch
6160 (not Same_Instantiated_Constant
6161 (Entity (Expr1), Entity (Expr2)));
6162 end if;
6163
6164 else
6165 Check_Mismatch (True);
6166 end if;
6167
6168 elsif Is_Entity_Name (Original_Node (Expr1))
6169 and then Is_Entity_Name (Expr2)
6170 and then Same_Instantiated_Constant
6171 (Entity (Original_Node (Expr1)), Entity (Expr2))
6172 then
6173 null;
6174
6175 elsif Nkind (Expr1) = N_Null then
6176 Check_Mismatch (Nkind (Expr1) /= N_Null);
6177
6178 else
6179 Check_Mismatch (True);
6180 end if;
6181
6182 elsif Ekind (E1) = E_Variable then
6183 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6184
6185 elsif Ekind (E1) = E_Package then
6186 Check_Mismatch
6187 (Ekind (E1) /= Ekind (E2)
6188 or else Renamed_Object (E1) /= Renamed_Object (E2));
6189
6190 elsif Is_Overloadable (E1) then
6191
6192 -- Verify that the actual subprograms match. Note that actuals
6193 -- that are attributes are rewritten as subprograms. If the
6194 -- subprogram in the formal package is defaulted, no check is
6195 -- needed. Note that this can only happen in Ada 2005 when the
6196 -- formal package can be partially parameterized.
6197
6198 if Nkind (Unit_Declaration_Node (E1)) =
6199 N_Subprogram_Renaming_Declaration
6200 and then From_Default (Unit_Declaration_Node (E1))
6201 then
6202 null;
6203
6204 -- If the formal package has an "others" box association that
6205 -- covers this formal, there is no need for a check either.
6206
6207 elsif Nkind (Unit_Declaration_Node (E2)) in
6208 N_Formal_Subprogram_Declaration
6209 and then Box_Present (Unit_Declaration_Node (E2))
6210 then
6211 null;
6212
6213 -- No check needed if subprogram is a defaulted null procedure
6214
6215 elsif No (Alias (E2))
6216 and then Ekind (E2) = E_Procedure
6217 and then
6218 Null_Present (Specification (Unit_Declaration_Node (E2)))
6219 then
6220 null;
6221
6222 -- Otherwise the actual in the formal and the actual in the
6223 -- instantiation of the formal must match, up to renamings.
6224
6225 else
6226 Check_Mismatch
6227 (Ekind (E2) /= Ekind (E1)
6228 or else not Same_Instantiated_Function (E1, E2));
6229 end if;
6230
6231 else
6232 raise Program_Error;
6233 end if;
6234
6235 <<Next_E>>
6236 Prev_E1 := E1;
6237 Next_Entity (E1);
6238 Next_Entity (E2);
6239 end loop;
6240 end Check_Formal_Package_Instance;
6241
6242 ---------------------------
6243 -- Check_Formal_Packages --
6244 ---------------------------
6245
6246 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6247 E : Entity_Id;
6248 Formal_P : Entity_Id;
6249 Formal_Decl : Node_Id;
6250
6251 begin
6252 -- Iterate through the declarations in the instance, looking for package
6253 -- renaming declarations that denote instances of formal packages. Stop
6254 -- when we find the renaming of the current package itself. The
6255 -- declaration for a formal package without a box is followed by an
6256 -- internal entity that repeats the instantiation.
6257
6258 E := First_Entity (P_Id);
6259 while Present (E) loop
6260 if Ekind (E) = E_Package then
6261 if Renamed_Object (E) = P_Id then
6262 exit;
6263
6264 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6265 null;
6266
6267 else
6268 Formal_Decl := Parent (Associated_Formal_Package (E));
6269
6270 -- Nothing to check if the formal has a box or an others_clause
6271 -- (necessarily with a box).
6272
6273 if Box_Present (Formal_Decl) then
6274 null;
6275
6276 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6277 N_Others_Choice
6278 then
6279 -- The internal validating package was generated but formal
6280 -- and instance are known to be compatible.
6281
6282 Formal_P := Next_Entity (E);
6283 Remove (Unit_Declaration_Node (Formal_P));
6284
6285 else
6286 Formal_P := Next_Entity (E);
6287 Check_Formal_Package_Instance (Formal_P, E);
6288
6289 -- After checking, remove the internal validating package.
6290 -- It is only needed for semantic checks, and as it may
6291 -- contain generic formal declarations it should not reach
6292 -- gigi.
6293
6294 Remove (Unit_Declaration_Node (Formal_P));
6295 end if;
6296 end if;
6297 end if;
6298
6299 Next_Entity (E);
6300 end loop;
6301 end Check_Formal_Packages;
6302
6303 ---------------------------------
6304 -- Check_Forward_Instantiation --
6305 ---------------------------------
6306
6307 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6308 S : Entity_Id;
6309 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6310
6311 begin
6312 -- The instantiation appears before the generic body if we are in the
6313 -- scope of the unit containing the generic, either in its spec or in
6314 -- the package body, and before the generic body.
6315
6316 if Ekind (Gen_Comp) = E_Package_Body then
6317 Gen_Comp := Spec_Entity (Gen_Comp);
6318 end if;
6319
6320 if In_Open_Scopes (Gen_Comp)
6321 and then No (Corresponding_Body (Decl))
6322 then
6323 S := Current_Scope;
6324
6325 while Present (S)
6326 and then not Is_Compilation_Unit (S)
6327 and then not Is_Child_Unit (S)
6328 loop
6329 if Ekind (S) = E_Package then
6330 Set_Has_Forward_Instantiation (S);
6331 end if;
6332
6333 S := Scope (S);
6334 end loop;
6335 end if;
6336 end Check_Forward_Instantiation;
6337
6338 ---------------------------
6339 -- Check_Generic_Actuals --
6340 ---------------------------
6341
6342 -- The visibility of the actuals may be different between the point of
6343 -- generic instantiation and the instantiation of the body.
6344
6345 procedure Check_Generic_Actuals
6346 (Instance : Entity_Id;
6347 Is_Formal_Box : Boolean)
6348 is
6349 E : Entity_Id;
6350 Astype : Entity_Id;
6351
6352 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
6353 -- For a formal that is an array type, the component type is often a
6354 -- previous formal in the same unit. The privacy status of the component
6355 -- type will have been examined earlier in the traversal of the
6356 -- corresponding actuals, and this status should not be modified for
6357 -- the array (sub)type itself. However, if the base type of the array
6358 -- (sub)type is private, its full view must be restored in the body to
6359 -- be consistent with subsequent index subtypes, etc.
6360 --
6361 -- To detect this case we have to rescan the list of formals, which is
6362 -- usually short enough to ignore the resulting inefficiency.
6363
6364 -----------------------------
6365 -- Denotes_Previous_Actual --
6366 -----------------------------
6367
6368 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
6369 Prev : Entity_Id;
6370
6371 begin
6372 Prev := First_Entity (Instance);
6373 while Present (Prev) loop
6374 if Is_Type (Prev)
6375 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
6376 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
6377 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
6378 then
6379 return True;
6380
6381 elsif Prev = E then
6382 return False;
6383
6384 else
6385 Next_Entity (Prev);
6386 end if;
6387 end loop;
6388
6389 return False;
6390 end Denotes_Previous_Actual;
6391
6392 -- Start of processing for Check_Generic_Actuals
6393
6394 begin
6395 E := First_Entity (Instance);
6396 while Present (E) loop
6397 if Is_Type (E)
6398 and then Nkind (Parent (E)) = N_Subtype_Declaration
6399 and then Scope (Etype (E)) /= Instance
6400 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6401 then
6402 if Is_Array_Type (E)
6403 and then not Is_Private_Type (Etype (E))
6404 and then Denotes_Previous_Actual (Component_Type (E))
6405 then
6406 null;
6407 else
6408 Check_Private_View (Subtype_Indication (Parent (E)));
6409 end if;
6410
6411 Set_Is_Generic_Actual_Type (E, True);
6412 Set_Is_Hidden (E, False);
6413 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
6414
6415 -- We constructed the generic actual type as a subtype of the
6416 -- supplied type. This means that it normally would not inherit
6417 -- subtype specific attributes of the actual, which is wrong for
6418 -- the generic case.
6419
6420 Astype := Ancestor_Subtype (E);
6421
6422 if No (Astype) then
6423
6424 -- This can happen when E is an itype that is the full view of
6425 -- a private type completed, e.g. with a constrained array. In
6426 -- that case, use the first subtype, which will carry size
6427 -- information. The base type itself is unconstrained and will
6428 -- not carry it.
6429
6430 Astype := First_Subtype (E);
6431 end if;
6432
6433 Set_Size_Info (E, (Astype));
6434 Set_RM_Size (E, RM_Size (Astype));
6435 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6436
6437 if Is_Discrete_Or_Fixed_Point_Type (E) then
6438 Set_RM_Size (E, RM_Size (Astype));
6439
6440 -- In nested instances, the base type of an access actual may
6441 -- itself be private, and need to be exchanged.
6442
6443 elsif Is_Access_Type (E)
6444 and then Is_Private_Type (Etype (E))
6445 then
6446 Check_Private_View
6447 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
6448 end if;
6449
6450 elsif Ekind (E) = E_Package then
6451
6452 -- If this is the renaming for the current instance, we're done.
6453 -- Otherwise it is a formal package. If the corresponding formal
6454 -- was declared with a box, the (instantiations of the) generic
6455 -- formal part are also visible. Otherwise, ignore the entity
6456 -- created to validate the actuals.
6457
6458 if Renamed_Object (E) = Instance then
6459 exit;
6460
6461 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6462 null;
6463
6464 -- The visibility of a formal of an enclosing generic is already
6465 -- correct.
6466
6467 elsif Denotes_Formal_Package (E) then
6468 null;
6469
6470 elsif Present (Associated_Formal_Package (E))
6471 and then not Is_Generic_Formal (E)
6472 then
6473 if Box_Present (Parent (Associated_Formal_Package (E))) then
6474 Check_Generic_Actuals (Renamed_Object (E), True);
6475
6476 else
6477 Check_Generic_Actuals (Renamed_Object (E), False);
6478 end if;
6479
6480 Set_Is_Hidden (E, False);
6481 end if;
6482
6483 -- If this is a subprogram instance (in a wrapper package) the
6484 -- actual is fully visible.
6485
6486 elsif Is_Wrapper_Package (Instance) then
6487 Set_Is_Hidden (E, False);
6488
6489 -- If the formal package is declared with a box, or if the formal
6490 -- parameter is defaulted, it is visible in the body.
6491
6492 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6493 Set_Is_Hidden (E, False);
6494 end if;
6495
6496 if Ekind (E) = E_Constant then
6497
6498 -- If the type of the actual is a private type declared in the
6499 -- enclosing scope of the generic unit, the body of the generic
6500 -- sees the full view of the type (because it has to appear in
6501 -- the corresponding package body). If the type is private now,
6502 -- exchange views to restore the proper visiblity in the instance.
6503
6504 declare
6505 Typ : constant Entity_Id := Base_Type (Etype (E));
6506 -- The type of the actual
6507
6508 Gen_Id : Entity_Id;
6509 -- The generic unit
6510
6511 Parent_Scope : Entity_Id;
6512 -- The enclosing scope of the generic unit
6513
6514 begin
6515 if Is_Wrapper_Package (Instance) then
6516 Gen_Id :=
6517 Generic_Parent
6518 (Specification
6519 (Unit_Declaration_Node
6520 (Related_Instance (Instance))));
6521 else
6522 Gen_Id :=
6523 Generic_Parent (Package_Specification (Instance));
6524 end if;
6525
6526 Parent_Scope := Scope (Gen_Id);
6527
6528 -- The exchange is only needed if the generic is defined
6529 -- within a package which is not a common ancestor of the
6530 -- scope of the instance, and is not already in scope.
6531
6532 if Is_Private_Type (Typ)
6533 and then Scope (Typ) = Parent_Scope
6534 and then Scope (Instance) /= Parent_Scope
6535 and then Ekind (Parent_Scope) = E_Package
6536 and then not Is_Child_Unit (Gen_Id)
6537 then
6538 Switch_View (Typ);
6539
6540 -- If the type of the entity is a subtype, it may also have
6541 -- to be made visible, together with the base type of its
6542 -- full view, after exchange.
6543
6544 if Is_Private_Type (Etype (E)) then
6545 Switch_View (Etype (E));
6546 Switch_View (Base_Type (Etype (E)));
6547 end if;
6548 end if;
6549 end;
6550 end if;
6551
6552 Next_Entity (E);
6553 end loop;
6554 end Check_Generic_Actuals;
6555
6556 ------------------------------
6557 -- Check_Generic_Child_Unit --
6558 ------------------------------
6559
6560 procedure Check_Generic_Child_Unit
6561 (Gen_Id : Node_Id;
6562 Parent_Installed : in out Boolean)
6563 is
6564 Loc : constant Source_Ptr := Sloc (Gen_Id);
6565 Gen_Par : Entity_Id := Empty;
6566 E : Entity_Id;
6567 Inst_Par : Entity_Id;
6568 S : Node_Id;
6569
6570 function Find_Generic_Child
6571 (Scop : Entity_Id;
6572 Id : Node_Id) return Entity_Id;
6573 -- Search generic parent for possible child unit with the given name
6574
6575 function In_Enclosing_Instance return Boolean;
6576 -- Within an instance of the parent, the child unit may be denoted by
6577 -- a simple name, or an abbreviated expanded name. Examine enclosing
6578 -- scopes to locate a possible parent instantiation.
6579
6580 ------------------------
6581 -- Find_Generic_Child --
6582 ------------------------
6583
6584 function Find_Generic_Child
6585 (Scop : Entity_Id;
6586 Id : Node_Id) return Entity_Id
6587 is
6588 E : Entity_Id;
6589
6590 begin
6591 -- If entity of name is already set, instance has already been
6592 -- resolved, e.g. in an enclosing instantiation.
6593
6594 if Present (Entity (Id)) then
6595 if Scope (Entity (Id)) = Scop then
6596 return Entity (Id);
6597 else
6598 return Empty;
6599 end if;
6600
6601 else
6602 E := First_Entity (Scop);
6603 while Present (E) loop
6604 if Chars (E) = Chars (Id)
6605 and then Is_Child_Unit (E)
6606 then
6607 if Is_Child_Unit (E)
6608 and then not Is_Visible_Lib_Unit (E)
6609 then
6610 Error_Msg_NE
6611 ("generic child unit& is not visible", Gen_Id, E);
6612 end if;
6613
6614 Set_Entity (Id, E);
6615 return E;
6616 end if;
6617
6618 Next_Entity (E);
6619 end loop;
6620
6621 return Empty;
6622 end if;
6623 end Find_Generic_Child;
6624
6625 ---------------------------
6626 -- In_Enclosing_Instance --
6627 ---------------------------
6628
6629 function In_Enclosing_Instance return Boolean is
6630 Enclosing_Instance : Node_Id;
6631 Instance_Decl : Node_Id;
6632
6633 begin
6634 -- We do not inline any call that contains instantiations, except
6635 -- for instantiations of Unchecked_Conversion, so if we are within
6636 -- an inlined body the current instance does not require parents.
6637
6638 if In_Inlined_Body then
6639 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6640 return False;
6641 end if;
6642
6643 -- Loop to check enclosing scopes
6644
6645 Enclosing_Instance := Current_Scope;
6646 while Present (Enclosing_Instance) loop
6647 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6648
6649 if Ekind (Enclosing_Instance) = E_Package
6650 and then Is_Generic_Instance (Enclosing_Instance)
6651 and then Present
6652 (Generic_Parent (Specification (Instance_Decl)))
6653 then
6654 -- Check whether the generic we are looking for is a child of
6655 -- this instance.
6656
6657 E := Find_Generic_Child
6658 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6659 exit when Present (E);
6660
6661 else
6662 E := Empty;
6663 end if;
6664
6665 Enclosing_Instance := Scope (Enclosing_Instance);
6666 end loop;
6667
6668 if No (E) then
6669
6670 -- Not a child unit
6671
6672 Analyze (Gen_Id);
6673 return False;
6674
6675 else
6676 Rewrite (Gen_Id,
6677 Make_Expanded_Name (Loc,
6678 Chars => Chars (E),
6679 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6680 Selector_Name => New_Occurrence_Of (E, Loc)));
6681
6682 Set_Entity (Gen_Id, E);
6683 Set_Etype (Gen_Id, Etype (E));
6684 Parent_Installed := False; -- Already in scope.
6685 return True;
6686 end if;
6687 end In_Enclosing_Instance;
6688
6689 -- Start of processing for Check_Generic_Child_Unit
6690
6691 begin
6692 -- If the name of the generic is given by a selected component, it may
6693 -- be the name of a generic child unit, and the prefix is the name of an
6694 -- instance of the parent, in which case the child unit must be visible.
6695 -- If this instance is not in scope, it must be placed there and removed
6696 -- after instantiation, because what is being instantiated is not the
6697 -- original child, but the corresponding child present in the instance
6698 -- of the parent.
6699
6700 -- If the child is instantiated within the parent, it can be given by
6701 -- a simple name. In this case the instance is already in scope, but
6702 -- the child generic must be recovered from the generic parent as well.
6703
6704 if Nkind (Gen_Id) = N_Selected_Component then
6705 S := Selector_Name (Gen_Id);
6706 Analyze (Prefix (Gen_Id));
6707 Inst_Par := Entity (Prefix (Gen_Id));
6708
6709 if Ekind (Inst_Par) = E_Package
6710 and then Present (Renamed_Object (Inst_Par))
6711 then
6712 Inst_Par := Renamed_Object (Inst_Par);
6713 end if;
6714
6715 if Ekind (Inst_Par) = E_Package then
6716 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6717 Gen_Par := Generic_Parent (Parent (Inst_Par));
6718
6719 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6720 and then
6721 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6722 then
6723 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6724 end if;
6725
6726 elsif Ekind (Inst_Par) = E_Generic_Package
6727 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6728 then
6729 -- A formal package may be a real child package, and not the
6730 -- implicit instance within a parent. In this case the child is
6731 -- not visible and has to be retrieved explicitly as well.
6732
6733 Gen_Par := Inst_Par;
6734 end if;
6735
6736 if Present (Gen_Par) then
6737
6738 -- The prefix denotes an instantiation. The entity itself may be a
6739 -- nested generic, or a child unit.
6740
6741 E := Find_Generic_Child (Gen_Par, S);
6742
6743 if Present (E) then
6744 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6745 Set_Entity (Gen_Id, E);
6746 Set_Etype (Gen_Id, Etype (E));
6747 Set_Entity (S, E);
6748 Set_Etype (S, Etype (E));
6749
6750 -- Indicate that this is a reference to the parent
6751
6752 if In_Extended_Main_Source_Unit (Gen_Id) then
6753 Set_Is_Instantiated (Inst_Par);
6754 end if;
6755
6756 -- A common mistake is to replicate the naming scheme of a
6757 -- hierarchy by instantiating a generic child directly, rather
6758 -- than the implicit child in a parent instance:
6759
6760 -- generic .. package Gpar is ..
6761 -- generic .. package Gpar.Child is ..
6762 -- package Par is new Gpar ();
6763
6764 -- with Gpar.Child;
6765 -- package Par.Child is new Gpar.Child ();
6766 -- rather than Par.Child
6767
6768 -- In this case the instantiation is within Par, which is an
6769 -- instance, but Gpar does not denote Par because we are not IN
6770 -- the instance of Gpar, so this is illegal. The test below
6771 -- recognizes this particular case.
6772
6773 if Is_Child_Unit (E)
6774 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6775 and then (not In_Instance
6776 or else Nkind (Parent (Parent (Gen_Id))) =
6777 N_Compilation_Unit)
6778 then
6779 Error_Msg_N
6780 ("prefix of generic child unit must be instance of parent",
6781 Gen_Id);
6782 end if;
6783
6784 if not In_Open_Scopes (Inst_Par)
6785 and then Nkind (Parent (Gen_Id)) not in
6786 N_Generic_Renaming_Declaration
6787 then
6788 Install_Parent (Inst_Par);
6789 Parent_Installed := True;
6790
6791 elsif In_Open_Scopes (Inst_Par) then
6792
6793 -- If the parent is already installed, install the actuals
6794 -- for its formal packages. This is necessary when the child
6795 -- instance is a child of the parent instance: in this case,
6796 -- the parent is placed on the scope stack but the formal
6797 -- packages are not made visible.
6798
6799 Install_Formal_Packages (Inst_Par);
6800 end if;
6801
6802 else
6803 -- If the generic parent does not contain an entity that
6804 -- corresponds to the selector, the instance doesn't either.
6805 -- Analyzing the node will yield the appropriate error message.
6806 -- If the entity is not a child unit, then it is an inner
6807 -- generic in the parent.
6808
6809 Analyze (Gen_Id);
6810 end if;
6811
6812 else
6813 Analyze (Gen_Id);
6814
6815 if Is_Child_Unit (Entity (Gen_Id))
6816 and then
6817 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6818 and then not In_Open_Scopes (Inst_Par)
6819 then
6820 Install_Parent (Inst_Par);
6821 Parent_Installed := True;
6822
6823 -- The generic unit may be the renaming of the implicit child
6824 -- present in an instance. In that case the parent instance is
6825 -- obtained from the name of the renamed entity.
6826
6827 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6828 and then Present (Renamed_Entity (Entity (Gen_Id)))
6829 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6830 then
6831 declare
6832 Renamed_Package : constant Node_Id :=
6833 Name (Parent (Entity (Gen_Id)));
6834 begin
6835 if Nkind (Renamed_Package) = N_Expanded_Name then
6836 Inst_Par := Entity (Prefix (Renamed_Package));
6837 Install_Parent (Inst_Par);
6838 Parent_Installed := True;
6839 end if;
6840 end;
6841 end if;
6842 end if;
6843
6844 elsif Nkind (Gen_Id) = N_Expanded_Name then
6845
6846 -- Entity already present, analyze prefix, whose meaning may be an
6847 -- instance in the current context. If it is an instance of a
6848 -- relative within another, the proper parent may still have to be
6849 -- installed, if they are not of the same generation.
6850
6851 Analyze (Prefix (Gen_Id));
6852
6853 -- Prevent cascaded errors
6854
6855 if Etype (Prefix (Gen_Id)) = Any_Type then
6856 return;
6857 end if;
6858
6859 -- In the unlikely case that a local declaration hides the name of
6860 -- the parent package, locate it on the homonym chain. If the context
6861 -- is an instance of the parent, the renaming entity is flagged as
6862 -- such.
6863
6864 Inst_Par := Entity (Prefix (Gen_Id));
6865 while Present (Inst_Par)
6866 and then not Is_Package_Or_Generic_Package (Inst_Par)
6867 loop
6868 Inst_Par := Homonym (Inst_Par);
6869 end loop;
6870
6871 pragma Assert (Present (Inst_Par));
6872 Set_Entity (Prefix (Gen_Id), Inst_Par);
6873
6874 if In_Enclosing_Instance then
6875 null;
6876
6877 elsif Present (Entity (Gen_Id))
6878 and then Is_Child_Unit (Entity (Gen_Id))
6879 and then not In_Open_Scopes (Inst_Par)
6880 then
6881 Install_Parent (Inst_Par);
6882 Parent_Installed := True;
6883 end if;
6884
6885 elsif In_Enclosing_Instance then
6886
6887 -- The child unit is found in some enclosing scope
6888
6889 null;
6890
6891 else
6892 Analyze (Gen_Id);
6893
6894 -- If this is the renaming of the implicit child in a parent
6895 -- instance, recover the parent name and install it.
6896
6897 if Is_Entity_Name (Gen_Id) then
6898 E := Entity (Gen_Id);
6899
6900 if Is_Generic_Unit (E)
6901 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6902 and then Is_Child_Unit (Renamed_Object (E))
6903 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6904 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6905 then
6906 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
6907 Inst_Par := Entity (Prefix (Gen_Id));
6908
6909 if not In_Open_Scopes (Inst_Par) then
6910 Install_Parent (Inst_Par);
6911 Parent_Installed := True;
6912 end if;
6913
6914 -- If it is a child unit of a non-generic parent, it may be
6915 -- use-visible and given by a direct name. Install parent as
6916 -- for other cases.
6917
6918 elsif Is_Generic_Unit (E)
6919 and then Is_Child_Unit (E)
6920 and then
6921 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6922 and then not Is_Generic_Unit (Scope (E))
6923 then
6924 if not In_Open_Scopes (Scope (E)) then
6925 Install_Parent (Scope (E));
6926 Parent_Installed := True;
6927 end if;
6928 end if;
6929 end if;
6930 end if;
6931 end Check_Generic_Child_Unit;
6932
6933 -----------------------------
6934 -- Check_Hidden_Child_Unit --
6935 -----------------------------
6936
6937 procedure Check_Hidden_Child_Unit
6938 (N : Node_Id;
6939 Gen_Unit : Entity_Id;
6940 Act_Decl_Id : Entity_Id)
6941 is
6942 Gen_Id : constant Node_Id := Name (N);
6943
6944 begin
6945 if Is_Child_Unit (Gen_Unit)
6946 and then Is_Child_Unit (Act_Decl_Id)
6947 and then Nkind (Gen_Id) = N_Expanded_Name
6948 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6949 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6950 then
6951 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6952 Error_Msg_NE
6953 ("generic unit & is implicitly declared in &",
6954 Defining_Unit_Name (N), Gen_Unit);
6955 Error_Msg_N ("\instance must have different name",
6956 Defining_Unit_Name (N));
6957 end if;
6958 end Check_Hidden_Child_Unit;
6959
6960 ------------------------
6961 -- Check_Private_View --
6962 ------------------------
6963
6964 procedure Check_Private_View (N : Node_Id) is
6965 T : constant Entity_Id := Etype (N);
6966 BT : Entity_Id;
6967
6968 begin
6969 -- Exchange views if the type was not private in the generic but is
6970 -- private at the point of instantiation. Do not exchange views if
6971 -- the scope of the type is in scope. This can happen if both generic
6972 -- and instance are sibling units, or if type is defined in a parent.
6973 -- In this case the visibility of the type will be correct for all
6974 -- semantic checks.
6975
6976 if Present (T) then
6977 BT := Base_Type (T);
6978
6979 if Is_Private_Type (T)
6980 and then not Has_Private_View (N)
6981 and then Present (Full_View (T))
6982 and then not In_Open_Scopes (Scope (T))
6983 then
6984 -- In the generic, the full type was visible. Save the private
6985 -- entity, for subsequent exchange.
6986
6987 Switch_View (T);
6988
6989 elsif Has_Private_View (N)
6990 and then not Is_Private_Type (T)
6991 and then not Has_Been_Exchanged (T)
6992 and then Etype (Get_Associated_Node (N)) /= T
6993 then
6994 -- Only the private declaration was visible in the generic. If
6995 -- the type appears in a subtype declaration, the subtype in the
6996 -- instance must have a view compatible with that of its parent,
6997 -- which must be exchanged (see corresponding code in Restore_
6998 -- Private_Views). Otherwise, if the type is defined in a parent
6999 -- unit, leave full visibility within instance, which is safe.
7000
7001 if In_Open_Scopes (Scope (Base_Type (T)))
7002 and then not Is_Private_Type (Base_Type (T))
7003 and then Comes_From_Source (Base_Type (T))
7004 then
7005 null;
7006
7007 elsif Nkind (Parent (N)) = N_Subtype_Declaration
7008 or else not In_Private_Part (Scope (Base_Type (T)))
7009 then
7010 Prepend_Elmt (T, Exchanged_Views);
7011 Exchange_Declarations (Etype (Get_Associated_Node (N)));
7012 end if;
7013
7014 -- For composite types with inconsistent representation exchange
7015 -- component types accordingly.
7016
7017 elsif Is_Access_Type (T)
7018 and then Is_Private_Type (Designated_Type (T))
7019 and then not Has_Private_View (N)
7020 and then Present (Full_View (Designated_Type (T)))
7021 then
7022 Switch_View (Designated_Type (T));
7023
7024 elsif Is_Array_Type (T) then
7025 if Is_Private_Type (Component_Type (T))
7026 and then not Has_Private_View (N)
7027 and then Present (Full_View (Component_Type (T)))
7028 then
7029 Switch_View (Component_Type (T));
7030 end if;
7031
7032 -- The normal exchange mechanism relies on the setting of a
7033 -- flag on the reference in the generic. However, an additional
7034 -- mechanism is needed for types that are not explicitly
7035 -- mentioned in the generic, but may be needed in expanded code
7036 -- in the instance. This includes component types of arrays and
7037 -- designated types of access types. This processing must also
7038 -- include the index types of arrays which we take care of here.
7039
7040 declare
7041 Indx : Node_Id;
7042 Typ : Entity_Id;
7043
7044 begin
7045 Indx := First_Index (T);
7046 while Present (Indx) loop
7047 Typ := Base_Type (Etype (Indx));
7048
7049 if Is_Private_Type (Typ)
7050 and then Present (Full_View (Typ))
7051 then
7052 Switch_View (Typ);
7053 end if;
7054
7055 Next_Index (Indx);
7056 end loop;
7057 end;
7058
7059 elsif Is_Private_Type (T)
7060 and then Present (Full_View (T))
7061 and then Is_Array_Type (Full_View (T))
7062 and then Is_Private_Type (Component_Type (Full_View (T)))
7063 then
7064 Switch_View (T);
7065
7066 -- Finally, a non-private subtype may have a private base type, which
7067 -- must be exchanged for consistency. This can happen when a package
7068 -- body is instantiated, when the scope stack is empty but in fact
7069 -- the subtype and the base type are declared in an enclosing scope.
7070
7071 -- Note that in this case we introduce an inconsistency in the view
7072 -- set, because we switch the base type BT, but there could be some
7073 -- private dependent subtypes of BT which remain unswitched. Such
7074 -- subtypes might need to be switched at a later point (see specific
7075 -- provision for that case in Switch_View).
7076
7077 elsif not Is_Private_Type (T)
7078 and then not Has_Private_View (N)
7079 and then Is_Private_Type (BT)
7080 and then Present (Full_View (BT))
7081 and then not Is_Generic_Type (BT)
7082 and then not In_Open_Scopes (BT)
7083 then
7084 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7085 Exchange_Declarations (BT);
7086 end if;
7087 end if;
7088 end Check_Private_View;
7089
7090 -----------------------------
7091 -- Check_Hidden_Primitives --
7092 -----------------------------
7093
7094 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7095 Actual : Node_Id;
7096 Gen_T : Entity_Id;
7097 Result : Elist_Id := No_Elist;
7098
7099 begin
7100 if No (Assoc_List) then
7101 return No_Elist;
7102 end if;
7103
7104 -- Traverse the list of associations between formals and actuals
7105 -- searching for renamings of tagged types
7106
7107 Actual := First (Assoc_List);
7108 while Present (Actual) loop
7109 if Nkind (Actual) = N_Subtype_Declaration then
7110 Gen_T := Generic_Parent_Type (Actual);
7111
7112 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7113
7114 -- Traverse the list of primitives of the actual types
7115 -- searching for hidden primitives that are visible in the
7116 -- corresponding generic formal; leave them visible and
7117 -- append them to Result to restore their decoration later.
7118
7119 Install_Hidden_Primitives
7120 (Prims_List => Result,
7121 Gen_T => Gen_T,
7122 Act_T => Entity (Subtype_Indication (Actual)));
7123 end if;
7124 end if;
7125
7126 Next (Actual);
7127 end loop;
7128
7129 return Result;
7130 end Check_Hidden_Primitives;
7131
7132 --------------------------
7133 -- Contains_Instance_Of --
7134 --------------------------
7135
7136 function Contains_Instance_Of
7137 (Inner : Entity_Id;
7138 Outer : Entity_Id;
7139 N : Node_Id) return Boolean
7140 is
7141 Elmt : Elmt_Id;
7142 Scop : Entity_Id;
7143
7144 begin
7145 Scop := Outer;
7146
7147 -- Verify that there are no circular instantiations. We check whether
7148 -- the unit contains an instance of the current scope or some enclosing
7149 -- scope (in case one of the instances appears in a subunit). Longer
7150 -- circularities involving subunits might seem too pathological to
7151 -- consider, but they were not too pathological for the authors of
7152 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7153 -- enclosing generic scopes as containing an instance.
7154
7155 loop
7156 -- Within a generic subprogram body, the scope is not generic, to
7157 -- allow for recursive subprograms. Use the declaration to determine
7158 -- whether this is a generic unit.
7159
7160 if Ekind (Scop) = E_Generic_Package
7161 or else (Is_Subprogram (Scop)
7162 and then Nkind (Unit_Declaration_Node (Scop)) =
7163 N_Generic_Subprogram_Declaration)
7164 then
7165 Elmt := First_Elmt (Inner_Instances (Inner));
7166
7167 while Present (Elmt) loop
7168 if Node (Elmt) = Scop then
7169 Error_Msg_Node_2 := Inner;
7170 Error_Msg_NE
7171 ("circular Instantiation: & instantiated within &!",
7172 N, Scop);
7173 return True;
7174
7175 elsif Node (Elmt) = Inner then
7176 return True;
7177
7178 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7179 Error_Msg_Node_2 := Inner;
7180 Error_Msg_NE
7181 ("circular Instantiation: & instantiated within &!",
7182 N, Node (Elmt));
7183 return True;
7184 end if;
7185
7186 Next_Elmt (Elmt);
7187 end loop;
7188
7189 -- Indicate that Inner is being instantiated within Scop
7190
7191 Append_Elmt (Inner, Inner_Instances (Scop));
7192 end if;
7193
7194 if Scop = Standard_Standard then
7195 exit;
7196 else
7197 Scop := Scope (Scop);
7198 end if;
7199 end loop;
7200
7201 return False;
7202 end Contains_Instance_Of;
7203
7204 -----------------------
7205 -- Copy_Generic_Node --
7206 -----------------------
7207
7208 function Copy_Generic_Node
7209 (N : Node_Id;
7210 Parent_Id : Node_Id;
7211 Instantiating : Boolean) return Node_Id
7212 is
7213 Ent : Entity_Id;
7214 New_N : Node_Id;
7215
7216 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7217 -- Check the given value of one of the Fields referenced by the current
7218 -- node to determine whether to copy it recursively. The field may hold
7219 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7220 -- Char) in which case it need not be copied.
7221
7222 procedure Copy_Descendants;
7223 -- Common utility for various nodes
7224
7225 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7226 -- Make copy of element list
7227
7228 function Copy_Generic_List
7229 (L : List_Id;
7230 Parent_Id : Node_Id) return List_Id;
7231 -- Apply Copy_Node recursively to the members of a node list
7232
7233 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7234 -- True if an identifier is part of the defining program unit name of
7235 -- a child unit. The entity of such an identifier must be kept (for
7236 -- ASIS use) even though as the name of an enclosing generic it would
7237 -- otherwise not be preserved in the generic tree.
7238
7239 ----------------------
7240 -- Copy_Descendants --
7241 ----------------------
7242
7243 procedure Copy_Descendants is
7244 use Atree.Unchecked_Access;
7245 -- This code section is part of the implementation of an untyped
7246 -- tree traversal, so it needs direct access to node fields.
7247
7248 begin
7249 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7250 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7251 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7252 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7253 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7254 end Copy_Descendants;
7255
7256 -----------------------------
7257 -- Copy_Generic_Descendant --
7258 -----------------------------
7259
7260 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7261 begin
7262 if D = Union_Id (Empty) then
7263 return D;
7264
7265 elsif D in Node_Range then
7266 return Union_Id
7267 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7268
7269 elsif D in List_Range then
7270 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7271
7272 elsif D in Elist_Range then
7273 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7274
7275 -- Nothing else is copyable (e.g. Uint values), return as is
7276
7277 else
7278 return D;
7279 end if;
7280 end Copy_Generic_Descendant;
7281
7282 ------------------------
7283 -- Copy_Generic_Elist --
7284 ------------------------
7285
7286 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7287 M : Elmt_Id;
7288 L : Elist_Id;
7289
7290 begin
7291 if Present (E) then
7292 L := New_Elmt_List;
7293 M := First_Elmt (E);
7294 while Present (M) loop
7295 Append_Elmt
7296 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7297 Next_Elmt (M);
7298 end loop;
7299
7300 return L;
7301
7302 else
7303 return No_Elist;
7304 end if;
7305 end Copy_Generic_Elist;
7306
7307 -----------------------
7308 -- Copy_Generic_List --
7309 -----------------------
7310
7311 function Copy_Generic_List
7312 (L : List_Id;
7313 Parent_Id : Node_Id) return List_Id
7314 is
7315 N : Node_Id;
7316 New_L : List_Id;
7317
7318 begin
7319 if Present (L) then
7320 New_L := New_List;
7321 Set_Parent (New_L, Parent_Id);
7322
7323 N := First (L);
7324 while Present (N) loop
7325 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7326 Next (N);
7327 end loop;
7328
7329 return New_L;
7330
7331 else
7332 return No_List;
7333 end if;
7334 end Copy_Generic_List;
7335
7336 ---------------------------
7337 -- In_Defining_Unit_Name --
7338 ---------------------------
7339
7340 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7341 begin
7342 return
7343 Present (Parent (Nam))
7344 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7345 or else
7346 (Nkind (Parent (Nam)) = N_Expanded_Name
7347 and then In_Defining_Unit_Name (Parent (Nam))));
7348 end In_Defining_Unit_Name;
7349
7350 -- Start of processing for Copy_Generic_Node
7351
7352 begin
7353 if N = Empty then
7354 return N;
7355 end if;
7356
7357 New_N := New_Copy (N);
7358
7359 -- Copy aspects if present
7360
7361 if Has_Aspects (N) then
7362 Set_Has_Aspects (New_N, False);
7363 Set_Aspect_Specifications
7364 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7365 end if;
7366
7367 if Instantiating then
7368 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7369 end if;
7370
7371 if not Is_List_Member (N) then
7372 Set_Parent (New_N, Parent_Id);
7373 end if;
7374
7375 -- Special casing for identifiers and other entity names and operators
7376
7377 if Nkind_In (New_N, N_Character_Literal,
7378 N_Expanded_Name,
7379 N_Identifier,
7380 N_Operator_Symbol)
7381 or else Nkind (New_N) in N_Op
7382 then
7383 if not Instantiating then
7384
7385 -- Link both nodes in order to assign subsequently the entity of
7386 -- the copy to the original node, in case this is a global
7387 -- reference.
7388
7389 Set_Associated_Node (N, New_N);
7390
7391 -- If we are within an instantiation, this is a nested generic
7392 -- that has already been analyzed at the point of definition.
7393 -- We must preserve references that were global to the enclosing
7394 -- parent at that point. Other occurrences, whether global or
7395 -- local to the current generic, must be resolved anew, so we
7396 -- reset the entity in the generic copy. A global reference has a
7397 -- smaller depth than the parent, or else the same depth in case
7398 -- both are distinct compilation units.
7399
7400 -- A child unit is implicitly declared within the enclosing parent
7401 -- but is in fact global to it, and must be preserved.
7402
7403 -- It is also possible for Current_Instantiated_Parent to be
7404 -- defined, and for this not to be a nested generic, namely if
7405 -- the unit is loaded through Rtsfind. In that case, the entity of
7406 -- New_N is only a link to the associated node, and not a defining
7407 -- occurrence.
7408
7409 -- The entities for parent units in the defining_program_unit of a
7410 -- generic child unit are established when the context of the unit
7411 -- is first analyzed, before the generic copy is made. They are
7412 -- preserved in the copy for use in ASIS queries.
7413
7414 Ent := Entity (New_N);
7415
7416 if No (Current_Instantiated_Parent.Gen_Id) then
7417 if No (Ent)
7418 or else Nkind (Ent) /= N_Defining_Identifier
7419 or else not In_Defining_Unit_Name (N)
7420 then
7421 Set_Associated_Node (New_N, Empty);
7422 end if;
7423
7424 elsif No (Ent)
7425 or else
7426 not Nkind_In (Ent, N_Defining_Identifier,
7427 N_Defining_Character_Literal,
7428 N_Defining_Operator_Symbol)
7429 or else No (Scope (Ent))
7430 or else
7431 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7432 and then not Is_Child_Unit (Ent))
7433 or else
7434 (Scope_Depth (Scope (Ent)) >
7435 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7436 and then
7437 Get_Source_Unit (Ent) =
7438 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7439 then
7440 Set_Associated_Node (New_N, Empty);
7441 end if;
7442
7443 -- Case of instantiating identifier or some other name or operator
7444
7445 else
7446 -- If the associated node is still defined, the entity in it
7447 -- is global, and must be copied to the instance. If this copy
7448 -- is being made for a body to inline, it is applied to an
7449 -- instantiated tree, and the entity is already present and
7450 -- must be also preserved.
7451
7452 declare
7453 Assoc : constant Node_Id := Get_Associated_Node (N);
7454
7455 begin
7456 if Present (Assoc) then
7457 if Nkind (Assoc) = Nkind (N) then
7458 Set_Entity (New_N, Entity (Assoc));
7459 Check_Private_View (N);
7460
7461 -- The node is a reference to a global type and acts as the
7462 -- subtype mark of a qualified expression created in order
7463 -- to aid resolution of accidental overloading in instances.
7464 -- Since N is a reference to a type, the Associated_Node of
7465 -- N denotes an entity rather than another identifier. See
7466 -- Qualify_Universal_Operands for details.
7467
7468 elsif Nkind (N) = N_Identifier
7469 and then Nkind (Parent (N)) = N_Qualified_Expression
7470 and then Subtype_Mark (Parent (N)) = N
7471 and then Is_Qualified_Universal_Literal (Parent (N))
7472 then
7473 Set_Entity (New_N, Assoc);
7474
7475 -- The name in the call may be a selected component if the
7476 -- call has not been analyzed yet, as may be the case for
7477 -- pre/post conditions in a generic unit.
7478
7479 elsif Nkind (Assoc) = N_Function_Call
7480 and then Is_Entity_Name (Name (Assoc))
7481 then
7482 Set_Entity (New_N, Entity (Name (Assoc)));
7483
7484 elsif Nkind_In (Assoc, N_Defining_Identifier,
7485 N_Defining_Character_Literal,
7486 N_Defining_Operator_Symbol)
7487 and then Expander_Active
7488 then
7489 -- Inlining case: we are copying a tree that contains
7490 -- global entities, which are preserved in the copy to be
7491 -- used for subsequent inlining.
7492
7493 null;
7494
7495 else
7496 Set_Entity (New_N, Empty);
7497 end if;
7498 end if;
7499 end;
7500 end if;
7501
7502 -- For expanded name, we must copy the Prefix and Selector_Name
7503
7504 if Nkind (N) = N_Expanded_Name then
7505 Set_Prefix
7506 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7507
7508 Set_Selector_Name (New_N,
7509 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7510
7511 -- For operators, we must copy the right operand
7512
7513 elsif Nkind (N) in N_Op then
7514 Set_Right_Opnd (New_N,
7515 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7516
7517 -- And for binary operators, the left operand as well
7518
7519 if Nkind (N) in N_Binary_Op then
7520 Set_Left_Opnd (New_N,
7521 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7522 end if;
7523 end if;
7524
7525 -- Establish a link between an entity from the generic template and the
7526 -- corresponding entity in the generic copy to be analyzed.
7527
7528 elsif Nkind (N) in N_Entity then
7529 if not Instantiating then
7530 Set_Associated_Entity (N, New_N);
7531 end if;
7532
7533 -- Clear any existing link the copy may inherit from the replicated
7534 -- generic template entity.
7535
7536 Set_Associated_Entity (New_N, Empty);
7537
7538 -- Special casing for stubs
7539
7540 elsif Nkind (N) in N_Body_Stub then
7541
7542 -- In any case, we must copy the specification or defining
7543 -- identifier as appropriate.
7544
7545 if Nkind (N) = N_Subprogram_Body_Stub then
7546 Set_Specification (New_N,
7547 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7548
7549 else
7550 Set_Defining_Identifier (New_N,
7551 Copy_Generic_Node
7552 (Defining_Identifier (N), New_N, Instantiating));
7553 end if;
7554
7555 -- If we are not instantiating, then this is where we load and
7556 -- analyze subunits, i.e. at the point where the stub occurs. A
7557 -- more permissive system might defer this analysis to the point
7558 -- of instantiation, but this seems too complicated for now.
7559
7560 if not Instantiating then
7561 declare
7562 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7563 Subunit : Node_Id;
7564 Unum : Unit_Number_Type;
7565 New_Body : Node_Id;
7566
7567 begin
7568 -- Make sure that, if it is a subunit of the main unit that is
7569 -- preprocessed and if -gnateG is specified, the preprocessed
7570 -- file will be written.
7571
7572 Lib.Analysing_Subunit_Of_Main :=
7573 Lib.In_Extended_Main_Source_Unit (N);
7574 Unum :=
7575 Load_Unit
7576 (Load_Name => Subunit_Name,
7577 Required => False,
7578 Subunit => True,
7579 Error_Node => N);
7580 Lib.Analysing_Subunit_Of_Main := False;
7581
7582 -- If the proper body is not found, a warning message will be
7583 -- emitted when analyzing the stub, or later at the point of
7584 -- instantiation. Here we just leave the stub as is.
7585
7586 if Unum = No_Unit then
7587 Subunits_Missing := True;
7588 goto Subunit_Not_Found;
7589 end if;
7590
7591 Subunit := Cunit (Unum);
7592
7593 if Nkind (Unit (Subunit)) /= N_Subunit then
7594 Error_Msg_N
7595 ("found child unit instead of expected SEPARATE subunit",
7596 Subunit);
7597 Error_Msg_Sloc := Sloc (N);
7598 Error_Msg_N ("\to complete stub #", Subunit);
7599 goto Subunit_Not_Found;
7600 end if;
7601
7602 -- We must create a generic copy of the subunit, in order to
7603 -- perform semantic analysis on it, and we must replace the
7604 -- stub in the original generic unit with the subunit, in order
7605 -- to preserve non-local references within.
7606
7607 -- Only the proper body needs to be copied. Library_Unit and
7608 -- context clause are simply inherited by the generic copy.
7609 -- Note that the copy (which may be recursive if there are
7610 -- nested subunits) must be done first, before attaching it to
7611 -- the enclosing generic.
7612
7613 New_Body :=
7614 Copy_Generic_Node
7615 (Proper_Body (Unit (Subunit)),
7616 Empty, Instantiating => False);
7617
7618 -- Now place the original proper body in the original generic
7619 -- unit. This is a body, not a compilation unit.
7620
7621 Rewrite (N, Proper_Body (Unit (Subunit)));
7622 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7623 Set_Was_Originally_Stub (N);
7624
7625 -- Finally replace the body of the subunit with its copy, and
7626 -- make this new subunit into the library unit of the generic
7627 -- copy, which does not have stubs any longer.
7628
7629 Set_Proper_Body (Unit (Subunit), New_Body);
7630 Set_Library_Unit (New_N, Subunit);
7631 Inherit_Context (Unit (Subunit), N);
7632 end;
7633
7634 -- If we are instantiating, this must be an error case, since
7635 -- otherwise we would have replaced the stub node by the proper body
7636 -- that corresponds. So just ignore it in the copy (i.e. we have
7637 -- copied it, and that is good enough).
7638
7639 else
7640 null;
7641 end if;
7642
7643 <<Subunit_Not_Found>> null;
7644
7645 -- If the node is a compilation unit, it is the subunit of a stub, which
7646 -- has been loaded already (see code below). In this case, the library
7647 -- unit field of N points to the parent unit (which is a compilation
7648 -- unit) and need not (and cannot) be copied.
7649
7650 -- When the proper body of the stub is analyzed, the library_unit link
7651 -- is used to establish the proper context (see sem_ch10).
7652
7653 -- The other fields of a compilation unit are copied as usual
7654
7655 elsif Nkind (N) = N_Compilation_Unit then
7656
7657 -- This code can only be executed when not instantiating, because in
7658 -- the copy made for an instantiation, the compilation unit node has
7659 -- disappeared at the point that a stub is replaced by its proper
7660 -- body.
7661
7662 pragma Assert (not Instantiating);
7663
7664 Set_Context_Items (New_N,
7665 Copy_Generic_List (Context_Items (N), New_N));
7666
7667 Set_Unit (New_N,
7668 Copy_Generic_Node (Unit (N), New_N, False));
7669
7670 Set_First_Inlined_Subprogram (New_N,
7671 Copy_Generic_Node
7672 (First_Inlined_Subprogram (N), New_N, False));
7673
7674 Set_Aux_Decls_Node (New_N,
7675 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7676
7677 -- For an assignment node, the assignment is known to be semantically
7678 -- legal if we are instantiating the template. This avoids incorrect
7679 -- diagnostics in generated code.
7680
7681 elsif Nkind (N) = N_Assignment_Statement then
7682
7683 -- Copy name and expression fields in usual manner
7684
7685 Set_Name (New_N,
7686 Copy_Generic_Node (Name (N), New_N, Instantiating));
7687
7688 Set_Expression (New_N,
7689 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7690
7691 if Instantiating then
7692 Set_Assignment_OK (Name (New_N), True);
7693 end if;
7694
7695 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7696 if not Instantiating then
7697 Set_Associated_Node (N, New_N);
7698
7699 else
7700 if Present (Get_Associated_Node (N))
7701 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7702 then
7703 -- In the generic the aggregate has some composite type. If at
7704 -- the point of instantiation the type has a private view,
7705 -- install the full view (and that of its ancestors, if any).
7706
7707 declare
7708 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7709 Rt : Entity_Id;
7710
7711 begin
7712 if Present (T) and then Is_Private_Type (T) then
7713 Switch_View (T);
7714 end if;
7715
7716 if Present (T)
7717 and then Is_Tagged_Type (T)
7718 and then Is_Derived_Type (T)
7719 then
7720 Rt := Root_Type (T);
7721
7722 loop
7723 T := Etype (T);
7724
7725 if Is_Private_Type (T) then
7726 Switch_View (T);
7727 end if;
7728
7729 exit when T = Rt;
7730 end loop;
7731 end if;
7732 end;
7733 end if;
7734 end if;
7735
7736 -- Do not copy the associated node, which points to the generic copy
7737 -- of the aggregate.
7738
7739 declare
7740 use Atree.Unchecked_Access;
7741 -- This code section is part of the implementation of an untyped
7742 -- tree traversal, so it needs direct access to node fields.
7743
7744 begin
7745 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7746 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7747 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7748 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7749 end;
7750
7751 -- Allocators do not have an identifier denoting the access type, so we
7752 -- must locate it through the expression to check whether the views are
7753 -- consistent.
7754
7755 elsif Nkind (N) = N_Allocator
7756 and then Nkind (Expression (N)) = N_Qualified_Expression
7757 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7758 and then Instantiating
7759 then
7760 declare
7761 T : constant Node_Id :=
7762 Get_Associated_Node (Subtype_Mark (Expression (N)));
7763 Acc_T : Entity_Id;
7764
7765 begin
7766 if Present (T) then
7767
7768 -- Retrieve the allocator node in the generic copy
7769
7770 Acc_T := Etype (Parent (Parent (T)));
7771
7772 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
7773 Switch_View (Acc_T);
7774 end if;
7775 end if;
7776
7777 Copy_Descendants;
7778 end;
7779
7780 -- For a proper body, we must catch the case of a proper body that
7781 -- replaces a stub. This represents the point at which a separate
7782 -- compilation unit, and hence template file, may be referenced, so we
7783 -- must make a new source instantiation entry for the template of the
7784 -- subunit, and ensure that all nodes in the subunit are adjusted using
7785 -- this new source instantiation entry.
7786
7787 elsif Nkind (N) in N_Proper_Body then
7788 declare
7789 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7790
7791 begin
7792 if Instantiating and then Was_Originally_Stub (N) then
7793 Create_Instantiation_Source
7794 (Instantiation_Node,
7795 Defining_Entity (N),
7796 S_Adjustment);
7797 end if;
7798
7799 -- Now copy the fields of the proper body, using the new
7800 -- adjustment factor if one was needed as per test above.
7801
7802 Copy_Descendants;
7803
7804 -- Restore the original adjustment factor in case changed
7805
7806 S_Adjustment := Save_Adjustment;
7807 end;
7808
7809 elsif Nkind (N) = N_Pragma and then Instantiating then
7810
7811 -- Do not copy Comment or Ident pragmas their content is relevant to
7812 -- the generic unit, not to the instantiating unit.
7813
7814 if Nam_In (Pragma_Name_Unmapped (N), Name_Comment, Name_Ident) then
7815 New_N := Make_Null_Statement (Sloc (N));
7816
7817 -- Do not copy pragmas generated from aspects because the pragmas do
7818 -- not carry any semantic information, plus they will be regenerated
7819 -- in the instance.
7820
7821 -- However, generating C we need to copy them since postconditions
7822 -- are inlined by the front end, and the front-end inlining machinery
7823 -- relies on this routine to perform inlining.
7824
7825 elsif From_Aspect_Specification (N)
7826 and then not Modify_Tree_For_C
7827 then
7828 New_N := Make_Null_Statement (Sloc (N));
7829
7830 else
7831 Copy_Descendants;
7832 end if;
7833
7834 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7835
7836 -- No descendant fields need traversing
7837
7838 null;
7839
7840 elsif Nkind (N) = N_String_Literal
7841 and then Present (Etype (N))
7842 and then Instantiating
7843 then
7844 -- If the string is declared in an outer scope, the string_literal
7845 -- subtype created for it may have the wrong scope. Force reanalysis
7846 -- of the constant to generate a new itype in the proper context.
7847
7848 Set_Etype (New_N, Empty);
7849 Set_Analyzed (New_N, False);
7850
7851 -- For the remaining nodes, copy their descendants recursively
7852
7853 else
7854 Copy_Descendants;
7855
7856 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7857 Set_Generic_Parent (Specification (New_N), N);
7858
7859 -- Should preserve Corresponding_Spec??? (12.3(14))
7860 end if;
7861 end if;
7862
7863 -- Propagate dimensions if present, so that they are reflected in the
7864 -- instance.
7865
7866 if Nkind (N) in N_Has_Etype
7867 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
7868 and then Present (Etype (N))
7869 and then Is_Floating_Point_Type (Etype (N))
7870 and then Has_Dimension_System (Etype (N))
7871 then
7872 Copy_Dimensions (N, New_N);
7873 end if;
7874
7875 return New_N;
7876 end Copy_Generic_Node;
7877
7878 ----------------------------
7879 -- Denotes_Formal_Package --
7880 ----------------------------
7881
7882 function Denotes_Formal_Package
7883 (Pack : Entity_Id;
7884 On_Exit : Boolean := False;
7885 Instance : Entity_Id := Empty) return Boolean
7886 is
7887 Par : Entity_Id;
7888 Scop : constant Entity_Id := Scope (Pack);
7889 E : Entity_Id;
7890
7891 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7892 -- The package in question may be an actual for a previous formal
7893 -- package P of the current instance, so examine its actuals as well.
7894 -- This must be recursive over other formal packages.
7895
7896 ----------------------------------
7897 -- Is_Actual_Of_Previous_Formal --
7898 ----------------------------------
7899
7900 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7901 E1 : Entity_Id;
7902
7903 begin
7904 E1 := First_Entity (P);
7905 while Present (E1) and then E1 /= Instance loop
7906 if Ekind (E1) = E_Package
7907 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7908 then
7909 if Renamed_Object (E1) = Pack then
7910 return True;
7911
7912 elsif E1 = P or else Renamed_Object (E1) = P then
7913 return False;
7914
7915 elsif Is_Actual_Of_Previous_Formal (E1) then
7916 return True;
7917 end if;
7918 end if;
7919
7920 Next_Entity (E1);
7921 end loop;
7922
7923 return False;
7924 end Is_Actual_Of_Previous_Formal;
7925
7926 -- Start of processing for Denotes_Formal_Package
7927
7928 begin
7929 if On_Exit then
7930 Par :=
7931 Instance_Envs.Table
7932 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7933 else
7934 Par := Current_Instantiated_Parent.Act_Id;
7935 end if;
7936
7937 if Ekind (Scop) = E_Generic_Package
7938 or else Nkind (Unit_Declaration_Node (Scop)) =
7939 N_Generic_Subprogram_Declaration
7940 then
7941 return True;
7942
7943 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7944 N_Formal_Package_Declaration
7945 then
7946 return True;
7947
7948 elsif No (Par) then
7949 return False;
7950
7951 else
7952 -- Check whether this package is associated with a formal package of
7953 -- the enclosing instantiation. Iterate over the list of renamings.
7954
7955 E := First_Entity (Par);
7956 while Present (E) loop
7957 if Ekind (E) /= E_Package
7958 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7959 then
7960 null;
7961
7962 elsif Renamed_Object (E) = Par then
7963 return False;
7964
7965 elsif Renamed_Object (E) = Pack then
7966 return True;
7967
7968 elsif Is_Actual_Of_Previous_Formal (E) then
7969 return True;
7970
7971 end if;
7972
7973 Next_Entity (E);
7974 end loop;
7975
7976 return False;
7977 end if;
7978 end Denotes_Formal_Package;
7979
7980 -----------------
7981 -- End_Generic --
7982 -----------------
7983
7984 procedure End_Generic is
7985 begin
7986 -- ??? More things could be factored out in this routine. Should
7987 -- probably be done at a later stage.
7988
7989 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7990 Generic_Flags.Decrement_Last;
7991
7992 Expander_Mode_Restore;
7993 end End_Generic;
7994
7995 -------------
7996 -- Earlier --
7997 -------------
7998
7999 function Earlier (N1, N2 : Node_Id) return Boolean is
8000 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8001 -- Find distance from given node to enclosing compilation unit
8002
8003 ----------------
8004 -- Find_Depth --
8005 ----------------
8006
8007 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8008 begin
8009 while Present (P)
8010 and then Nkind (P) /= N_Compilation_Unit
8011 loop
8012 P := True_Parent (P);
8013 D := D + 1;
8014 end loop;
8015 end Find_Depth;
8016
8017 -- Local declarations
8018
8019 D1 : Integer := 0;
8020 D2 : Integer := 0;
8021 P1 : Node_Id := N1;
8022 P2 : Node_Id := N2;
8023 T1 : Source_Ptr;
8024 T2 : Source_Ptr;
8025
8026 -- Start of processing for Earlier
8027
8028 begin
8029 Find_Depth (P1, D1);
8030 Find_Depth (P2, D2);
8031
8032 if P1 /= P2 then
8033 return False;
8034 else
8035 P1 := N1;
8036 P2 := N2;
8037 end if;
8038
8039 while D1 > D2 loop
8040 P1 := True_Parent (P1);
8041 D1 := D1 - 1;
8042 end loop;
8043
8044 while D2 > D1 loop
8045 P2 := True_Parent (P2);
8046 D2 := D2 - 1;
8047 end loop;
8048
8049 -- At this point P1 and P2 are at the same distance from the root.
8050 -- We examine their parents until we find a common declarative list.
8051 -- If we reach the root, N1 and N2 do not descend from the same
8052 -- declarative list (e.g. one is nested in the declarative part and
8053 -- the other is in a block in the statement part) and the earlier
8054 -- one is already frozen.
8055
8056 while not Is_List_Member (P1)
8057 or else not Is_List_Member (P2)
8058 or else List_Containing (P1) /= List_Containing (P2)
8059 loop
8060 P1 := True_Parent (P1);
8061 P2 := True_Parent (P2);
8062
8063 if Nkind (Parent (P1)) = N_Subunit then
8064 P1 := Corresponding_Stub (Parent (P1));
8065 end if;
8066
8067 if Nkind (Parent (P2)) = N_Subunit then
8068 P2 := Corresponding_Stub (Parent (P2));
8069 end if;
8070
8071 if P1 = P2 then
8072 return False;
8073 end if;
8074 end loop;
8075
8076 -- Expanded code usually shares the source location of the original
8077 -- construct it was generated for. This however may not necessarily
8078 -- reflect the true location of the code within the tree.
8079
8080 -- Before comparing the slocs of the two nodes, make sure that we are
8081 -- working with correct source locations. Assume that P1 is to the left
8082 -- of P2. If either one does not come from source, traverse the common
8083 -- list heading towards the other node and locate the first source
8084 -- statement.
8085
8086 -- P1 P2
8087 -- ----+===+===+--------------+===+===+----
8088 -- expanded code expanded code
8089
8090 if not Comes_From_Source (P1) then
8091 while Present (P1) loop
8092
8093 -- Neither P2 nor a source statement were located during the
8094 -- search. If we reach the end of the list, then P1 does not
8095 -- occur earlier than P2.
8096
8097 -- ---->
8098 -- start --- P2 ----- P1 --- end
8099
8100 if No (Next (P1)) then
8101 return False;
8102
8103 -- We encounter P2 while going to the right of the list. This
8104 -- means that P1 does indeed appear earlier.
8105
8106 -- ---->
8107 -- start --- P1 ===== P2 --- end
8108 -- expanded code in between
8109
8110 elsif P1 = P2 then
8111 return True;
8112
8113 -- No need to look any further since we have located a source
8114 -- statement.
8115
8116 elsif Comes_From_Source (P1) then
8117 exit;
8118 end if;
8119
8120 -- Keep going right
8121
8122 Next (P1);
8123 end loop;
8124 end if;
8125
8126 if not Comes_From_Source (P2) then
8127 while Present (P2) loop
8128
8129 -- Neither P1 nor a source statement were located during the
8130 -- search. If we reach the start of the list, then P1 does not
8131 -- occur earlier than P2.
8132
8133 -- <----
8134 -- start --- P2 --- P1 --- end
8135
8136 if No (Prev (P2)) then
8137 return False;
8138
8139 -- We encounter P1 while going to the left of the list. This
8140 -- means that P1 does indeed appear earlier.
8141
8142 -- <----
8143 -- start --- P1 ===== P2 --- end
8144 -- expanded code in between
8145
8146 elsif P2 = P1 then
8147 return True;
8148
8149 -- No need to look any further since we have located a source
8150 -- statement.
8151
8152 elsif Comes_From_Source (P2) then
8153 exit;
8154 end if;
8155
8156 -- Keep going left
8157
8158 Prev (P2);
8159 end loop;
8160 end if;
8161
8162 -- At this point either both nodes came from source or we approximated
8163 -- their source locations through neighboring source statements.
8164
8165 T1 := Top_Level_Location (Sloc (P1));
8166 T2 := Top_Level_Location (Sloc (P2));
8167
8168 -- When two nodes come from the same instance, they have identical top
8169 -- level locations. To determine proper relation within the tree, check
8170 -- their locations within the template.
8171
8172 if T1 = T2 then
8173 return Sloc (P1) < Sloc (P2);
8174
8175 -- The two nodes either come from unrelated instances or do not come
8176 -- from instantiated code at all.
8177
8178 else
8179 return T1 < T2;
8180 end if;
8181 end Earlier;
8182
8183 ----------------------
8184 -- Find_Actual_Type --
8185 ----------------------
8186
8187 function Find_Actual_Type
8188 (Typ : Entity_Id;
8189 Gen_Type : Entity_Id) return Entity_Id
8190 is
8191 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8192 T : Entity_Id;
8193
8194 begin
8195 -- Special processing only applies to child units
8196
8197 if not Is_Child_Unit (Gen_Scope) then
8198 return Get_Instance_Of (Typ);
8199
8200 -- If designated or component type is itself a formal of the child unit,
8201 -- its instance is available.
8202
8203 elsif Scope (Typ) = Gen_Scope then
8204 return Get_Instance_Of (Typ);
8205
8206 -- If the array or access type is not declared in the parent unit,
8207 -- no special processing needed.
8208
8209 elsif not Is_Generic_Type (Typ)
8210 and then Scope (Gen_Scope) /= Scope (Typ)
8211 then
8212 return Get_Instance_Of (Typ);
8213
8214 -- Otherwise, retrieve designated or component type by visibility
8215
8216 else
8217 T := Current_Entity (Typ);
8218 while Present (T) loop
8219 if In_Open_Scopes (Scope (T)) then
8220 return T;
8221 elsif Is_Generic_Actual_Type (T) then
8222 return T;
8223 end if;
8224
8225 T := Homonym (T);
8226 end loop;
8227
8228 return Typ;
8229 end if;
8230 end Find_Actual_Type;
8231
8232 ----------------------------
8233 -- Freeze_Subprogram_Body --
8234 ----------------------------
8235
8236 procedure Freeze_Subprogram_Body
8237 (Inst_Node : Node_Id;
8238 Gen_Body : Node_Id;
8239 Pack_Id : Entity_Id)
8240 is
8241 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8242 Par : constant Entity_Id := Scope (Gen_Unit);
8243 E_G_Id : Entity_Id;
8244 Enc_G : Entity_Id;
8245 Enc_I : Node_Id;
8246 F_Node : Node_Id;
8247
8248 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8249 -- Find innermost package body that encloses the given node, and which
8250 -- is not a compilation unit. Freeze nodes for the instance, or for its
8251 -- enclosing body, may be inserted after the enclosing_body of the
8252 -- generic unit. Used to determine proper placement of freeze node for
8253 -- both package and subprogram instances.
8254
8255 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8256 -- Find entity for given package body, and locate or create a freeze
8257 -- node for it.
8258
8259 ----------------------------
8260 -- Enclosing_Package_Body --
8261 ----------------------------
8262
8263 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8264 P : Node_Id;
8265
8266 begin
8267 P := Parent (N);
8268 while Present (P)
8269 and then Nkind (Parent (P)) /= N_Compilation_Unit
8270 loop
8271 if Nkind (P) = N_Package_Body then
8272 if Nkind (Parent (P)) = N_Subunit then
8273 return Corresponding_Stub (Parent (P));
8274 else
8275 return P;
8276 end if;
8277 end if;
8278
8279 P := True_Parent (P);
8280 end loop;
8281
8282 return Empty;
8283 end Enclosing_Package_Body;
8284
8285 -------------------------
8286 -- Package_Freeze_Node --
8287 -------------------------
8288
8289 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8290 Id : Entity_Id;
8291
8292 begin
8293 if Nkind (B) = N_Package_Body then
8294 Id := Corresponding_Spec (B);
8295 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8296 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8297 end if;
8298
8299 Ensure_Freeze_Node (Id);
8300 return Freeze_Node (Id);
8301 end Package_Freeze_Node;
8302
8303 -- Start of processing for Freeze_Subprogram_Body
8304
8305 begin
8306 -- If the instance and the generic body appear within the same unit, and
8307 -- the instance precedes the generic, the freeze node for the instance
8308 -- must appear after that of the generic. If the generic is nested
8309 -- within another instance I2, then current instance must be frozen
8310 -- after I2. In both cases, the freeze nodes are those of enclosing
8311 -- packages. Otherwise, the freeze node is placed at the end of the
8312 -- current declarative part.
8313
8314 Enc_G := Enclosing_Package_Body (Gen_Body);
8315 Enc_I := Enclosing_Package_Body (Inst_Node);
8316 Ensure_Freeze_Node (Pack_Id);
8317 F_Node := Freeze_Node (Pack_Id);
8318
8319 if Is_Generic_Instance (Par)
8320 and then Present (Freeze_Node (Par))
8321 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
8322 then
8323 -- The parent was a premature instantiation. Insert freeze node at
8324 -- the end the current declarative part.
8325
8326 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
8327 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8328
8329 -- Handle the following case:
8330 --
8331 -- package Parent_Inst is new ...
8332 -- Parent_Inst []
8333 --
8334 -- procedure P ... -- this body freezes Parent_Inst
8335 --
8336 -- package Inst is new ...
8337 --
8338 -- In this particular scenario, the freeze node for Inst must be
8339 -- inserted in the same manner as that of Parent_Inst - before the
8340 -- next source body or at the end of the declarative list (body not
8341 -- available). If body P did not exist and Parent_Inst was frozen
8342 -- after Inst, either by a body following Inst or at the end of the
8343 -- declarative region, the freeze node for Inst must be inserted
8344 -- after that of Parent_Inst. This relation is established by
8345 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8346
8347 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
8348 List_Containing (Inst_Node)
8349 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8350 then
8351 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8352
8353 else
8354 Insert_After (Freeze_Node (Par), F_Node);
8355 end if;
8356
8357 -- The body enclosing the instance should be frozen after the body that
8358 -- includes the generic, because the body of the instance may make
8359 -- references to entities therein. If the two are not in the same
8360 -- declarative part, or if the one enclosing the instance is frozen
8361 -- already, freeze the instance at the end of the current declarative
8362 -- part.
8363
8364 elsif Is_Generic_Instance (Par)
8365 and then Present (Freeze_Node (Par))
8366 and then Present (Enc_I)
8367 then
8368 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
8369 or else
8370 (Nkind (Enc_I) = N_Package_Body
8371 and then
8372 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
8373 then
8374 -- The enclosing package may contain several instances. Rather
8375 -- than computing the earliest point at which to insert its freeze
8376 -- node, we place it at the end of the declarative part of the
8377 -- parent of the generic.
8378
8379 Insert_Freeze_Node_For_Instance
8380 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8381 end if;
8382
8383 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8384
8385 elsif Present (Enc_G)
8386 and then Present (Enc_I)
8387 and then Enc_G /= Enc_I
8388 and then Earlier (Inst_Node, Gen_Body)
8389 then
8390 if Nkind (Enc_G) = N_Package_Body then
8391 E_G_Id :=
8392 Corresponding_Spec (Enc_G);
8393 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8394 E_G_Id :=
8395 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8396 end if;
8397
8398 -- Freeze package that encloses instance, and place node after the
8399 -- package that encloses generic. If enclosing package is already
8400 -- frozen we have to assume it is at the proper place. This may be a
8401 -- potential ABE that requires dynamic checking. Do not add a freeze
8402 -- node if the package that encloses the generic is inside the body
8403 -- that encloses the instance, because the freeze node would be in
8404 -- the wrong scope. Additional contortions needed if the bodies are
8405 -- within a subunit.
8406
8407 declare
8408 Enclosing_Body : Node_Id;
8409
8410 begin
8411 if Nkind (Enc_I) = N_Package_Body_Stub then
8412 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8413 else
8414 Enclosing_Body := Enc_I;
8415 end if;
8416
8417 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8418 Insert_Freeze_Node_For_Instance
8419 (Enc_G, Package_Freeze_Node (Enc_I));
8420 end if;
8421 end;
8422
8423 -- Freeze enclosing subunit before instance
8424
8425 Ensure_Freeze_Node (E_G_Id);
8426
8427 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8428 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8429 end if;
8430
8431 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8432
8433 else
8434 -- If none of the above, insert freeze node at the end of the current
8435 -- declarative part.
8436
8437 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8438 end if;
8439 end Freeze_Subprogram_Body;
8440
8441 ----------------
8442 -- Get_Gen_Id --
8443 ----------------
8444
8445 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8446 begin
8447 return Generic_Renamings.Table (E).Gen_Id;
8448 end Get_Gen_Id;
8449
8450 ---------------------
8451 -- Get_Instance_Of --
8452 ---------------------
8453
8454 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8455 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8456
8457 begin
8458 if Res /= Assoc_Null then
8459 return Generic_Renamings.Table (Res).Act_Id;
8460
8461 else
8462 -- On exit, entity is not instantiated: not a generic parameter, or
8463 -- else parameter of an inner generic unit.
8464
8465 return A;
8466 end if;
8467 end Get_Instance_Of;
8468
8469 ------------------------------------
8470 -- Get_Package_Instantiation_Node --
8471 ------------------------------------
8472
8473 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
8474 Decl : Node_Id := Unit_Declaration_Node (A);
8475 Inst : Node_Id;
8476
8477 begin
8478 -- If the Package_Instantiation attribute has been set on the package
8479 -- entity, then use it directly when it (or its Original_Node) refers
8480 -- to an N_Package_Instantiation node. In principle it should be
8481 -- possible to have this field set in all cases, which should be
8482 -- investigated, and would allow this function to be significantly
8483 -- simplified. ???
8484
8485 Inst := Package_Instantiation (A);
8486
8487 if Present (Inst) then
8488 if Nkind (Inst) = N_Package_Instantiation then
8489 return Inst;
8490
8491 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8492 return Original_Node (Inst);
8493 end if;
8494 end if;
8495
8496 -- If the instantiation is a compilation unit that does not need body
8497 -- then the instantiation node has been rewritten as a package
8498 -- declaration for the instance, and we return the original node.
8499
8500 -- If it is a compilation unit and the instance node has not been
8501 -- rewritten, then it is still the unit of the compilation. Finally, if
8502 -- a body is present, this is a parent of the main unit whose body has
8503 -- been compiled for inlining purposes, and the instantiation node has
8504 -- been rewritten with the instance body.
8505
8506 -- Otherwise the instantiation node appears after the declaration. If
8507 -- the entity is a formal package, the declaration may have been
8508 -- rewritten as a generic declaration (in the case of a formal with box)
8509 -- or left as a formal package declaration if it has actuals, and is
8510 -- found with a forward search.
8511
8512 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8513 if Nkind (Decl) = N_Package_Declaration
8514 and then Present (Corresponding_Body (Decl))
8515 then
8516 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8517 end if;
8518
8519 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
8520 return Original_Node (Decl);
8521 else
8522 return Unit (Parent (Decl));
8523 end if;
8524
8525 elsif Nkind (Decl) = N_Package_Declaration
8526 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8527 then
8528 return Original_Node (Decl);
8529
8530 else
8531 Inst := Next (Decl);
8532 while not Nkind_In (Inst, N_Package_Instantiation,
8533 N_Formal_Package_Declaration)
8534 loop
8535 Next (Inst);
8536 end loop;
8537
8538 return Inst;
8539 end if;
8540 end Get_Package_Instantiation_Node;
8541
8542 ------------------------
8543 -- Has_Been_Exchanged --
8544 ------------------------
8545
8546 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8547 Next : Elmt_Id;
8548
8549 begin
8550 Next := First_Elmt (Exchanged_Views);
8551 while Present (Next) loop
8552 if Full_View (Node (Next)) = E then
8553 return True;
8554 end if;
8555
8556 Next_Elmt (Next);
8557 end loop;
8558
8559 return False;
8560 end Has_Been_Exchanged;
8561
8562 ----------
8563 -- Hash --
8564 ----------
8565
8566 function Hash (F : Entity_Id) return HTable_Range is
8567 begin
8568 return HTable_Range (F mod HTable_Size);
8569 end Hash;
8570
8571 ------------------------
8572 -- Hide_Current_Scope --
8573 ------------------------
8574
8575 procedure Hide_Current_Scope is
8576 C : constant Entity_Id := Current_Scope;
8577 E : Entity_Id;
8578
8579 begin
8580 Set_Is_Hidden_Open_Scope (C);
8581
8582 E := First_Entity (C);
8583 while Present (E) loop
8584 if Is_Immediately_Visible (E) then
8585 Set_Is_Immediately_Visible (E, False);
8586 Append_Elmt (E, Hidden_Entities);
8587 end if;
8588
8589 Next_Entity (E);
8590 end loop;
8591
8592 -- Make the scope name invisible as well. This is necessary, but might
8593 -- conflict with calls to Rtsfind later on, in case the scope is a
8594 -- predefined one. There is no clean solution to this problem, so for
8595 -- now we depend on the user not redefining Standard itself in one of
8596 -- the parent units.
8597
8598 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8599 Set_Is_Immediately_Visible (C, False);
8600 Append_Elmt (C, Hidden_Entities);
8601 end if;
8602
8603 end Hide_Current_Scope;
8604
8605 --------------
8606 -- Init_Env --
8607 --------------
8608
8609 procedure Init_Env is
8610 Saved : Instance_Env;
8611
8612 begin
8613 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8614 Saved.Exchanged_Views := Exchanged_Views;
8615 Saved.Hidden_Entities := Hidden_Entities;
8616 Saved.Current_Sem_Unit := Current_Sem_Unit;
8617 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8618 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8619
8620 -- Save configuration switches. These may be reset if the unit is a
8621 -- predefined unit, and the current mode is not Ada 2005.
8622
8623 Save_Opt_Config_Switches (Saved.Switches);
8624
8625 Instance_Envs.Append (Saved);
8626
8627 Exchanged_Views := New_Elmt_List;
8628 Hidden_Entities := New_Elmt_List;
8629
8630 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8631 -- this is set properly in Set_Instance_Env.
8632
8633 Current_Instantiated_Parent :=
8634 (Current_Scope, Current_Scope, Assoc_Null);
8635 end Init_Env;
8636
8637 ------------------------------
8638 -- In_Same_Declarative_Part --
8639 ------------------------------
8640
8641 function In_Same_Declarative_Part
8642 (F_Node : Node_Id;
8643 Inst : Node_Id) return Boolean
8644 is
8645 Decls : constant Node_Id := Parent (F_Node);
8646 Nod : Node_Id;
8647
8648 begin
8649 Nod := Parent (Inst);
8650 while Present (Nod) loop
8651 if Nod = Decls then
8652 return True;
8653
8654 elsif Nkind_In (Nod, N_Subprogram_Body,
8655 N_Package_Body,
8656 N_Package_Declaration,
8657 N_Task_Body,
8658 N_Protected_Body,
8659 N_Block_Statement)
8660 then
8661 return False;
8662
8663 elsif Nkind (Nod) = N_Subunit then
8664 Nod := Corresponding_Stub (Nod);
8665
8666 elsif Nkind (Nod) = N_Compilation_Unit then
8667 return False;
8668
8669 else
8670 Nod := Parent (Nod);
8671 end if;
8672 end loop;
8673
8674 return False;
8675 end In_Same_Declarative_Part;
8676
8677 ---------------------
8678 -- In_Main_Context --
8679 ---------------------
8680
8681 function In_Main_Context (E : Entity_Id) return Boolean is
8682 Context : List_Id;
8683 Clause : Node_Id;
8684 Nam : Node_Id;
8685
8686 begin
8687 if not Is_Compilation_Unit (E)
8688 or else Ekind (E) /= E_Package
8689 or else In_Private_Part (E)
8690 then
8691 return False;
8692 end if;
8693
8694 Context := Context_Items (Cunit (Main_Unit));
8695
8696 Clause := First (Context);
8697 while Present (Clause) loop
8698 if Nkind (Clause) = N_With_Clause then
8699 Nam := Name (Clause);
8700
8701 -- If the current scope is part of the context of the main unit,
8702 -- analysis of the corresponding with_clause is not complete, and
8703 -- the entity is not set. We use the Chars field directly, which
8704 -- might produce false positives in rare cases, but guarantees
8705 -- that we produce all the instance bodies we will need.
8706
8707 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8708 or else (Nkind (Nam) = N_Selected_Component
8709 and then Chars (Selector_Name (Nam)) = Chars (E))
8710 then
8711 return True;
8712 end if;
8713 end if;
8714
8715 Next (Clause);
8716 end loop;
8717
8718 return False;
8719 end In_Main_Context;
8720
8721 ---------------------
8722 -- Inherit_Context --
8723 ---------------------
8724
8725 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8726 Current_Context : List_Id;
8727 Current_Unit : Node_Id;
8728 Item : Node_Id;
8729 New_I : Node_Id;
8730
8731 Clause : Node_Id;
8732 OK : Boolean;
8733 Lib_Unit : Node_Id;
8734
8735 begin
8736 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8737
8738 -- The inherited context is attached to the enclosing compilation
8739 -- unit. This is either the main unit, or the declaration for the
8740 -- main unit (in case the instantiation appears within the package
8741 -- declaration and the main unit is its body).
8742
8743 Current_Unit := Parent (Inst);
8744 while Present (Current_Unit)
8745 and then Nkind (Current_Unit) /= N_Compilation_Unit
8746 loop
8747 Current_Unit := Parent (Current_Unit);
8748 end loop;
8749
8750 Current_Context := Context_Items (Current_Unit);
8751
8752 Item := First (Context_Items (Parent (Gen_Decl)));
8753 while Present (Item) loop
8754 if Nkind (Item) = N_With_Clause then
8755 Lib_Unit := Library_Unit (Item);
8756
8757 -- Take care to prevent direct cyclic with's
8758
8759 if Lib_Unit /= Current_Unit then
8760
8761 -- Do not add a unit if it is already in the context
8762
8763 Clause := First (Current_Context);
8764 OK := True;
8765 while Present (Clause) loop
8766 if Nkind (Clause) = N_With_Clause and then
8767 Library_Unit (Clause) = Lib_Unit
8768 then
8769 OK := False;
8770 exit;
8771 end if;
8772
8773 Next (Clause);
8774 end loop;
8775
8776 if OK then
8777 New_I := New_Copy (Item);
8778 Set_Implicit_With (New_I, True);
8779 Set_Implicit_With_From_Instantiation (New_I, True);
8780 Append (New_I, Current_Context);
8781 end if;
8782 end if;
8783 end if;
8784
8785 Next (Item);
8786 end loop;
8787 end if;
8788 end Inherit_Context;
8789
8790 ----------------
8791 -- Initialize --
8792 ----------------
8793
8794 procedure Initialize is
8795 begin
8796 Generic_Renamings.Init;
8797 Instance_Envs.Init;
8798 Generic_Flags.Init;
8799 Generic_Renamings_HTable.Reset;
8800 Circularity_Detected := False;
8801 Exchanged_Views := No_Elist;
8802 Hidden_Entities := No_Elist;
8803 end Initialize;
8804
8805 -------------------------------------
8806 -- Insert_Freeze_Node_For_Instance --
8807 -------------------------------------
8808
8809 procedure Insert_Freeze_Node_For_Instance
8810 (N : Node_Id;
8811 F_Node : Node_Id)
8812 is
8813 Decl : Node_Id;
8814 Decls : List_Id;
8815 Inst : Entity_Id;
8816 Par_N : Node_Id;
8817
8818 function Enclosing_Body (N : Node_Id) return Node_Id;
8819 -- Find enclosing package or subprogram body, if any. Freeze node may
8820 -- be placed at end of current declarative list if previous instance
8821 -- and current one have different enclosing bodies.
8822
8823 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8824 -- Find the local instance, if any, that declares the generic that is
8825 -- being instantiated. If present, the freeze node for this instance
8826 -- must follow the freeze node for the previous instance.
8827
8828 --------------------
8829 -- Enclosing_Body --
8830 --------------------
8831
8832 function Enclosing_Body (N : Node_Id) return Node_Id is
8833 P : Node_Id;
8834
8835 begin
8836 P := Parent (N);
8837 while Present (P)
8838 and then Nkind (Parent (P)) /= N_Compilation_Unit
8839 loop
8840 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8841 if Nkind (Parent (P)) = N_Subunit then
8842 return Corresponding_Stub (Parent (P));
8843 else
8844 return P;
8845 end if;
8846 end if;
8847
8848 P := True_Parent (P);
8849 end loop;
8850
8851 return Empty;
8852 end Enclosing_Body;
8853
8854 -----------------------
8855 -- Previous_Instance --
8856 -----------------------
8857
8858 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8859 S : Entity_Id;
8860
8861 begin
8862 S := Scope (Gen);
8863 while Present (S) and then S /= Standard_Standard loop
8864 if Is_Generic_Instance (S)
8865 and then In_Same_Source_Unit (S, N)
8866 then
8867 return S;
8868 end if;
8869
8870 S := Scope (S);
8871 end loop;
8872
8873 return Empty;
8874 end Previous_Instance;
8875
8876 -- Start of processing for Insert_Freeze_Node_For_Instance
8877
8878 begin
8879 if not Is_List_Member (F_Node) then
8880 Decl := N;
8881 Decls := List_Containing (N);
8882 Inst := Entity (F_Node);
8883 Par_N := Parent (Decls);
8884
8885 -- When processing a subprogram instantiation, utilize the actual
8886 -- subprogram instantiation rather than its package wrapper as it
8887 -- carries all the context information.
8888
8889 if Is_Wrapper_Package (Inst) then
8890 Inst := Related_Instance (Inst);
8891 end if;
8892
8893 -- If this is a package instance, check whether the generic is
8894 -- declared in a previous instance and the current instance is
8895 -- not within the previous one.
8896
8897 if Present (Generic_Parent (Parent (Inst)))
8898 and then Is_In_Main_Unit (N)
8899 then
8900 declare
8901 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8902 Par_I : constant Entity_Id :=
8903 Previous_Instance
8904 (Generic_Parent (Parent (Inst)));
8905 Scop : Entity_Id;
8906
8907 begin
8908 if Present (Par_I)
8909 and then Earlier (N, Freeze_Node (Par_I))
8910 then
8911 Scop := Scope (Inst);
8912
8913 -- If the current instance is within the one that contains
8914 -- the generic, the freeze node for the current one must
8915 -- appear in the current declarative part. Ditto, if the
8916 -- current instance is within another package instance or
8917 -- within a body that does not enclose the current instance.
8918 -- In these three cases the freeze node of the previous
8919 -- instance is not relevant.
8920
8921 while Present (Scop) and then Scop /= Standard_Standard loop
8922 exit when Scop = Par_I
8923 or else
8924 (Is_Generic_Instance (Scop)
8925 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8926 Scop := Scope (Scop);
8927 end loop;
8928
8929 -- Previous instance encloses current instance
8930
8931 if Scop = Par_I then
8932 null;
8933
8934 -- If the next node is a source body we must freeze in
8935 -- the current scope as well.
8936
8937 elsif Present (Next (N))
8938 and then Nkind_In (Next (N), N_Subprogram_Body,
8939 N_Package_Body)
8940 and then Comes_From_Source (Next (N))
8941 then
8942 null;
8943
8944 -- Current instance is within an unrelated instance
8945
8946 elsif Is_Generic_Instance (Scop) then
8947 null;
8948
8949 -- Current instance is within an unrelated body
8950
8951 elsif Present (Enclosing_N)
8952 and then Enclosing_N /= Enclosing_Body (Par_I)
8953 then
8954 null;
8955
8956 else
8957 Insert_After (Freeze_Node (Par_I), F_Node);
8958 return;
8959 end if;
8960 end if;
8961 end;
8962 end if;
8963
8964 -- When the instantiation occurs in a package declaration, append the
8965 -- freeze node to the private declarations (if any).
8966
8967 if Nkind (Par_N) = N_Package_Specification
8968 and then Decls = Visible_Declarations (Par_N)
8969 and then Present (Private_Declarations (Par_N))
8970 and then not Is_Empty_List (Private_Declarations (Par_N))
8971 then
8972 Decls := Private_Declarations (Par_N);
8973 Decl := First (Decls);
8974 end if;
8975
8976 -- Determine the proper freeze point of a package instantiation. We
8977 -- adhere to the general rule of a package or subprogram body causing
8978 -- freezing of anything before it in the same declarative region. In
8979 -- this case, the proper freeze point of a package instantiation is
8980 -- before the first source body which follows, or before a stub. This
8981 -- ensures that entities coming from the instance are already frozen
8982 -- and usable in source bodies.
8983
8984 if Nkind (Par_N) /= N_Package_Declaration
8985 and then Ekind (Inst) = E_Package
8986 and then Is_Generic_Instance (Inst)
8987 and then
8988 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8989 then
8990 while Present (Decl) loop
8991 if (Nkind (Decl) in N_Unit_Body
8992 or else
8993 Nkind (Decl) in N_Body_Stub)
8994 and then Comes_From_Source (Decl)
8995 then
8996 Insert_Before (Decl, F_Node);
8997 return;
8998 end if;
8999
9000 Next (Decl);
9001 end loop;
9002 end if;
9003
9004 -- In a package declaration, or if no previous body, insert at end
9005 -- of list.
9006
9007 Set_Sloc (F_Node, Sloc (Last (Decls)));
9008 Insert_After (Last (Decls), F_Node);
9009 end if;
9010 end Insert_Freeze_Node_For_Instance;
9011
9012 ------------------
9013 -- Install_Body --
9014 ------------------
9015
9016 procedure Install_Body
9017 (Act_Body : Node_Id;
9018 N : Node_Id;
9019 Gen_Body : Node_Id;
9020 Gen_Decl : Node_Id)
9021 is
9022 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9023 -- Check if the generic definition and the instantiation come from
9024 -- a common scope, in which case the instance must be frozen after
9025 -- the generic body.
9026
9027 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9028 -- If the instance is nested inside a generic unit, the Sloc of the
9029 -- instance indicates the place of the original definition, not the
9030 -- point of the current enclosing instance. Pending a better usage of
9031 -- Slocs to indicate instantiation places, we determine the place of
9032 -- origin of a node by finding the maximum sloc of any ancestor node.
9033 -- Why is this not equivalent to Top_Level_Location ???
9034
9035 -------------------
9036 -- In_Same_Scope --
9037 -------------------
9038
9039 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9040 Act_Scop : Entity_Id := Scope (Act_Id);
9041 Gen_Scop : Entity_Id := Scope (Gen_Id);
9042
9043 begin
9044 while Act_Scop /= Standard_Standard
9045 and then Gen_Scop /= Standard_Standard
9046 loop
9047 if Act_Scop = Gen_Scop then
9048 return True;
9049 end if;
9050
9051 Act_Scop := Scope (Act_Scop);
9052 Gen_Scop := Scope (Gen_Scop);
9053 end loop;
9054
9055 return False;
9056 end In_Same_Scope;
9057
9058 ---------------
9059 -- True_Sloc --
9060 ---------------
9061
9062 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9063 N1 : Node_Id;
9064 Res : Source_Ptr;
9065
9066 begin
9067 Res := Sloc (N);
9068 N1 := N;
9069 while Present (N1) and then N1 /= Act_Unit loop
9070 if Sloc (N1) > Res then
9071 Res := Sloc (N1);
9072 end if;
9073
9074 N1 := Parent (N1);
9075 end loop;
9076
9077 return Res;
9078 end True_Sloc;
9079
9080 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
9081 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9082 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
9083 Par : constant Entity_Id := Scope (Gen_Id);
9084 Gen_Unit : constant Node_Id :=
9085 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9086
9087 Body_Unit : Node_Id;
9088 F_Node : Node_Id;
9089 Must_Delay : Boolean;
9090 Orig_Body : Node_Id := Gen_Body;
9091
9092 -- Start of processing for Install_Body
9093
9094 begin
9095 -- Handle first the case of an instance with incomplete actual types.
9096 -- The instance body cannot be placed after the declaration because
9097 -- full views have not been seen yet. Any use of the non-limited views
9098 -- in the instance body requires the presence of a regular with_clause
9099 -- in the enclosing unit, and will fail if this with_clause is missing.
9100 -- We place the instance body at the beginning of the enclosing body,
9101 -- which is the unit being compiled. The freeze node for the instance
9102 -- is then placed after the instance body.
9103
9104 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
9105 and then Expander_Active
9106 and then Ekind (Scope (Act_Id)) = E_Package
9107 then
9108 declare
9109 Scop : constant Entity_Id := Scope (Act_Id);
9110 Body_Id : constant Node_Id :=
9111 Corresponding_Body (Unit_Declaration_Node (Scop));
9112
9113 begin
9114 Ensure_Freeze_Node (Act_Id);
9115 F_Node := Freeze_Node (Act_Id);
9116 if Present (Body_Id) then
9117 Set_Is_Frozen (Act_Id, False);
9118 Prepend (Act_Body, Declarations (Parent (Body_Id)));
9119 if Is_List_Member (F_Node) then
9120 Remove (F_Node);
9121 end if;
9122
9123 Insert_After (Act_Body, F_Node);
9124 end if;
9125 end;
9126 return;
9127 end if;
9128
9129 -- If the body is a subunit, the freeze point is the corresponding stub
9130 -- in the current compilation, not the subunit itself.
9131
9132 if Nkind (Parent (Gen_Body)) = N_Subunit then
9133 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9134 else
9135 Orig_Body := Gen_Body;
9136 end if;
9137
9138 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9139
9140 -- If the instantiation and the generic definition appear in the same
9141 -- package declaration, this is an early instantiation. If they appear
9142 -- in the same declarative part, it is an early instantiation only if
9143 -- the generic body appears textually later, and the generic body is
9144 -- also in the main unit.
9145
9146 -- If instance is nested within a subprogram, and the generic body
9147 -- is not, the instance is delayed because the enclosing body is. If
9148 -- instance and body are within the same scope, or the same subprogram
9149 -- body, indicate explicitly that the instance is delayed.
9150
9151 Must_Delay :=
9152 (Gen_Unit = Act_Unit
9153 and then (Nkind_In (Gen_Unit, N_Generic_Package_Declaration,
9154 N_Package_Declaration)
9155 or else (Gen_Unit = Body_Unit
9156 and then True_Sloc (N, Act_Unit)
9157 < Sloc (Orig_Body)))
9158 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9159 and then In_Same_Scope (Gen_Id, Act_Id));
9160
9161 -- If this is an early instantiation, the freeze node is placed after
9162 -- the generic body. Otherwise, if the generic appears in an instance,
9163 -- we cannot freeze the current instance until the outer one is frozen.
9164 -- This is only relevant if the current instance is nested within some
9165 -- inner scope not itself within the outer instance. If this scope is
9166 -- a package body in the same declarative part as the outer instance,
9167 -- then that body needs to be frozen after the outer instance. Finally,
9168 -- if no delay is needed, we place the freeze node at the end of the
9169 -- current declarative part.
9170
9171 if Expander_Active then
9172 Ensure_Freeze_Node (Act_Id);
9173 F_Node := Freeze_Node (Act_Id);
9174
9175 if Must_Delay then
9176 Insert_After (Orig_Body, F_Node);
9177
9178 elsif Is_Generic_Instance (Par)
9179 and then Present (Freeze_Node (Par))
9180 and then Scope (Act_Id) /= Par
9181 then
9182 -- Freeze instance of inner generic after instance of enclosing
9183 -- generic.
9184
9185 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
9186
9187 -- Handle the following case:
9188
9189 -- package Parent_Inst is new ...
9190 -- Parent_Inst []
9191
9192 -- procedure P ... -- this body freezes Parent_Inst
9193
9194 -- package Inst is new ...
9195
9196 -- In this particular scenario, the freeze node for Inst must
9197 -- be inserted in the same manner as that of Parent_Inst,
9198 -- before the next source body or at the end of the declarative
9199 -- list (body not available). If body P did not exist and
9200 -- Parent_Inst was frozen after Inst, either by a body
9201 -- following Inst or at the end of the declarative region,
9202 -- the freeze node for Inst must be inserted after that of
9203 -- Parent_Inst. This relation is established by comparing
9204 -- the Slocs of Parent_Inst freeze node and Inst.
9205
9206 if List_Containing (Get_Package_Instantiation_Node (Par)) =
9207 List_Containing (N)
9208 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9209 then
9210 Insert_Freeze_Node_For_Instance (N, F_Node);
9211 else
9212 Insert_After (Freeze_Node (Par), F_Node);
9213 end if;
9214
9215 -- Freeze package enclosing instance of inner generic after
9216 -- instance of enclosing generic.
9217
9218 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
9219 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
9220 then
9221 declare
9222 Enclosing : Entity_Id;
9223
9224 begin
9225 Enclosing := Corresponding_Spec (Parent (N));
9226
9227 if No (Enclosing) then
9228 Enclosing := Defining_Entity (Parent (N));
9229 end if;
9230
9231 Insert_Freeze_Node_For_Instance (N, F_Node);
9232 Ensure_Freeze_Node (Enclosing);
9233
9234 if not Is_List_Member (Freeze_Node (Enclosing)) then
9235
9236 -- The enclosing context is a subunit, insert the freeze
9237 -- node after the stub.
9238
9239 if Nkind (Parent (Parent (N))) = N_Subunit then
9240 Insert_Freeze_Node_For_Instance
9241 (Corresponding_Stub (Parent (Parent (N))),
9242 Freeze_Node (Enclosing));
9243
9244 -- The enclosing context is a package with a stub body
9245 -- which has already been replaced by the real body.
9246 -- Insert the freeze node after the actual body.
9247
9248 elsif Ekind (Enclosing) = E_Package
9249 and then Present (Body_Entity (Enclosing))
9250 and then Was_Originally_Stub
9251 (Parent (Body_Entity (Enclosing)))
9252 then
9253 Insert_Freeze_Node_For_Instance
9254 (Parent (Body_Entity (Enclosing)),
9255 Freeze_Node (Enclosing));
9256
9257 -- The parent instance has been frozen before the body of
9258 -- the enclosing package, insert the freeze node after
9259 -- the body.
9260
9261 elsif List_Containing (Freeze_Node (Par)) =
9262 List_Containing (Parent (N))
9263 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9264 then
9265 Insert_Freeze_Node_For_Instance
9266 (Parent (N), Freeze_Node (Enclosing));
9267
9268 else
9269 Insert_After
9270 (Freeze_Node (Par), Freeze_Node (Enclosing));
9271 end if;
9272 end if;
9273 end;
9274
9275 else
9276 Insert_Freeze_Node_For_Instance (N, F_Node);
9277 end if;
9278
9279 else
9280 Insert_Freeze_Node_For_Instance (N, F_Node);
9281 end if;
9282 end if;
9283
9284 Set_Is_Frozen (Act_Id);
9285 Insert_Before (N, Act_Body);
9286 Mark_Rewrite_Insertion (Act_Body);
9287 end Install_Body;
9288
9289 -----------------------------
9290 -- Install_Formal_Packages --
9291 -----------------------------
9292
9293 procedure Install_Formal_Packages (Par : Entity_Id) is
9294 E : Entity_Id;
9295 Gen : Entity_Id;
9296 Gen_E : Entity_Id := Empty;
9297
9298 begin
9299 E := First_Entity (Par);
9300
9301 -- If we are installing an instance parent, locate the formal packages
9302 -- of its generic parent.
9303
9304 if Is_Generic_Instance (Par) then
9305 Gen := Generic_Parent (Package_Specification (Par));
9306 Gen_E := First_Entity (Gen);
9307 end if;
9308
9309 while Present (E) loop
9310 if Ekind (E) = E_Package
9311 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9312 then
9313 -- If this is the renaming for the parent instance, done
9314
9315 if Renamed_Object (E) = Par then
9316 exit;
9317
9318 -- The visibility of a formal of an enclosing generic is already
9319 -- correct.
9320
9321 elsif Denotes_Formal_Package (E) then
9322 null;
9323
9324 elsif Present (Associated_Formal_Package (E)) then
9325 Check_Generic_Actuals (Renamed_Object (E), True);
9326 Set_Is_Hidden (E, False);
9327
9328 -- Find formal package in generic unit that corresponds to
9329 -- (instance of) formal package in instance.
9330
9331 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9332 Next_Entity (Gen_E);
9333 end loop;
9334
9335 if Present (Gen_E) then
9336 Map_Formal_Package_Entities (Gen_E, E);
9337 end if;
9338 end if;
9339 end if;
9340
9341 Next_Entity (E);
9342
9343 if Present (Gen_E) then
9344 Next_Entity (Gen_E);
9345 end if;
9346 end loop;
9347 end Install_Formal_Packages;
9348
9349 --------------------
9350 -- Install_Parent --
9351 --------------------
9352
9353 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9354 Ancestors : constant Elist_Id := New_Elmt_List;
9355 S : constant Entity_Id := Current_Scope;
9356 Inst_Par : Entity_Id;
9357 First_Par : Entity_Id;
9358 Inst_Node : Node_Id;
9359 Gen_Par : Entity_Id;
9360 First_Gen : Entity_Id;
9361 Elmt : Elmt_Id;
9362
9363 procedure Install_Noninstance_Specs (Par : Entity_Id);
9364 -- Install the scopes of noninstance parent units ending with Par
9365
9366 procedure Install_Spec (Par : Entity_Id);
9367 -- The child unit is within the declarative part of the parent, so the
9368 -- declarations within the parent are immediately visible.
9369
9370 -------------------------------
9371 -- Install_Noninstance_Specs --
9372 -------------------------------
9373
9374 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9375 begin
9376 if Present (Par)
9377 and then Par /= Standard_Standard
9378 and then not In_Open_Scopes (Par)
9379 then
9380 Install_Noninstance_Specs (Scope (Par));
9381 Install_Spec (Par);
9382 end if;
9383 end Install_Noninstance_Specs;
9384
9385 ------------------
9386 -- Install_Spec --
9387 ------------------
9388
9389 procedure Install_Spec (Par : Entity_Id) is
9390 Spec : constant Node_Id := Package_Specification (Par);
9391
9392 begin
9393 -- If this parent of the child instance is a top-level unit,
9394 -- then record the unit and its visibility for later resetting in
9395 -- Remove_Parent. We exclude units that are generic instances, as we
9396 -- only want to record this information for the ultimate top-level
9397 -- noninstance parent (is that always correct???).
9398
9399 if Scope (Par) = Standard_Standard
9400 and then not Is_Generic_Instance (Par)
9401 then
9402 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9403 Instance_Parent_Unit := Par;
9404 end if;
9405
9406 -- Open the parent scope and make it and its declarations visible.
9407 -- If this point is not within a body, then only the visible
9408 -- declarations should be made visible, and installation of the
9409 -- private declarations is deferred until the appropriate point
9410 -- within analysis of the spec being instantiated (see the handling
9411 -- of parent visibility in Analyze_Package_Specification). This is
9412 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9413 -- private view problems that occur when compiling instantiations of
9414 -- a generic child of that package (Generic_Dispatching_Constructor).
9415 -- If the instance freezes a tagged type, inlinings of operations
9416 -- from Ada.Tags may need the full view of type Tag. If inlining took
9417 -- proper account of establishing visibility of inlined subprograms'
9418 -- parents then it should be possible to remove this
9419 -- special check. ???
9420
9421 Push_Scope (Par);
9422 Set_Is_Immediately_Visible (Par);
9423 Install_Visible_Declarations (Par);
9424 Set_Use (Visible_Declarations (Spec));
9425
9426 if In_Body or else Is_RTU (Par, Ada_Tags) then
9427 Install_Private_Declarations (Par);
9428 Set_Use (Private_Declarations (Spec));
9429 end if;
9430 end Install_Spec;
9431
9432 -- Start of processing for Install_Parent
9433
9434 begin
9435 -- We need to install the parent instance to compile the instantiation
9436 -- of the child, but the child instance must appear in the current
9437 -- scope. Given that we cannot place the parent above the current scope
9438 -- in the scope stack, we duplicate the current scope and unstack both
9439 -- after the instantiation is complete.
9440
9441 -- If the parent is itself the instantiation of a child unit, we must
9442 -- also stack the instantiation of its parent, and so on. Each such
9443 -- ancestor is the prefix of the name in a prior instantiation.
9444
9445 -- If this is a nested instance, the parent unit itself resolves to
9446 -- a renaming of the parent instance, whose declaration we need.
9447
9448 -- Finally, the parent may be a generic (not an instance) when the
9449 -- child unit appears as a formal package.
9450
9451 Inst_Par := P;
9452
9453 if Present (Renamed_Entity (Inst_Par)) then
9454 Inst_Par := Renamed_Entity (Inst_Par);
9455 end if;
9456
9457 First_Par := Inst_Par;
9458
9459 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9460
9461 First_Gen := Gen_Par;
9462
9463 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9464
9465 -- Load grandparent instance as well
9466
9467 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
9468
9469 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9470 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9471
9472 if Present (Renamed_Entity (Inst_Par)) then
9473 Inst_Par := Renamed_Entity (Inst_Par);
9474 end if;
9475
9476 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9477
9478 if Present (Gen_Par) then
9479 Prepend_Elmt (Inst_Par, Ancestors);
9480
9481 else
9482 -- Parent is not the name of an instantiation
9483
9484 Install_Noninstance_Specs (Inst_Par);
9485 exit;
9486 end if;
9487
9488 else
9489 -- Previous error
9490
9491 exit;
9492 end if;
9493 end loop;
9494
9495 if Present (First_Gen) then
9496 Append_Elmt (First_Par, Ancestors);
9497 else
9498 Install_Noninstance_Specs (First_Par);
9499 end if;
9500
9501 if not Is_Empty_Elmt_List (Ancestors) then
9502 Elmt := First_Elmt (Ancestors);
9503 while Present (Elmt) loop
9504 Install_Spec (Node (Elmt));
9505 Install_Formal_Packages (Node (Elmt));
9506 Next_Elmt (Elmt);
9507 end loop;
9508 end if;
9509
9510 if not In_Body then
9511 Push_Scope (S);
9512 end if;
9513 end Install_Parent;
9514
9515 -------------------------------
9516 -- Install_Hidden_Primitives --
9517 -------------------------------
9518
9519 procedure Install_Hidden_Primitives
9520 (Prims_List : in out Elist_Id;
9521 Gen_T : Entity_Id;
9522 Act_T : Entity_Id)
9523 is
9524 Elmt : Elmt_Id;
9525 List : Elist_Id := No_Elist;
9526 Prim_G_Elmt : Elmt_Id;
9527 Prim_A_Elmt : Elmt_Id;
9528 Prim_G : Node_Id;
9529 Prim_A : Node_Id;
9530
9531 begin
9532 -- No action needed in case of serious errors because we cannot trust
9533 -- in the order of primitives
9534
9535 if Serious_Errors_Detected > 0 then
9536 return;
9537
9538 -- No action possible if we don't have available the list of primitive
9539 -- operations
9540
9541 elsif No (Gen_T)
9542 or else not Is_Record_Type (Gen_T)
9543 or else not Is_Tagged_Type (Gen_T)
9544 or else not Is_Record_Type (Act_T)
9545 or else not Is_Tagged_Type (Act_T)
9546 then
9547 return;
9548
9549 -- There is no need to handle interface types since their primitives
9550 -- cannot be hidden
9551
9552 elsif Is_Interface (Gen_T) then
9553 return;
9554 end if;
9555
9556 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9557
9558 if not Is_Class_Wide_Type (Act_T) then
9559 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9560 else
9561 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9562 end if;
9563
9564 loop
9565 -- Skip predefined primitives in the generic formal
9566
9567 while Present (Prim_G_Elmt)
9568 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9569 loop
9570 Next_Elmt (Prim_G_Elmt);
9571 end loop;
9572
9573 -- Skip predefined primitives in the generic actual
9574
9575 while Present (Prim_A_Elmt)
9576 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9577 loop
9578 Next_Elmt (Prim_A_Elmt);
9579 end loop;
9580
9581 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9582
9583 Prim_G := Node (Prim_G_Elmt);
9584 Prim_A := Node (Prim_A_Elmt);
9585
9586 -- There is no need to handle interface primitives because their
9587 -- primitives are not hidden
9588
9589 exit when Present (Interface_Alias (Prim_G));
9590
9591 -- Here we install one hidden primitive
9592
9593 if Chars (Prim_G) /= Chars (Prim_A)
9594 and then Has_Suffix (Prim_A, 'P')
9595 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9596 then
9597 Set_Chars (Prim_A, Chars (Prim_G));
9598 Append_New_Elmt (Prim_A, To => List);
9599 end if;
9600
9601 Next_Elmt (Prim_A_Elmt);
9602 Next_Elmt (Prim_G_Elmt);
9603 end loop;
9604
9605 -- Append the elements to the list of temporarily visible primitives
9606 -- avoiding duplicates.
9607
9608 if Present (List) then
9609 if No (Prims_List) then
9610 Prims_List := New_Elmt_List;
9611 end if;
9612
9613 Elmt := First_Elmt (List);
9614 while Present (Elmt) loop
9615 Append_Unique_Elmt (Node (Elmt), Prims_List);
9616 Next_Elmt (Elmt);
9617 end loop;
9618 end if;
9619 end Install_Hidden_Primitives;
9620
9621 -------------------------------
9622 -- Restore_Hidden_Primitives --
9623 -------------------------------
9624
9625 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9626 Prim_Elmt : Elmt_Id;
9627 Prim : Node_Id;
9628
9629 begin
9630 if Prims_List /= No_Elist then
9631 Prim_Elmt := First_Elmt (Prims_List);
9632 while Present (Prim_Elmt) loop
9633 Prim := Node (Prim_Elmt);
9634 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9635 Next_Elmt (Prim_Elmt);
9636 end loop;
9637
9638 Prims_List := No_Elist;
9639 end if;
9640 end Restore_Hidden_Primitives;
9641
9642 --------------------------------
9643 -- Instantiate_Formal_Package --
9644 --------------------------------
9645
9646 function Instantiate_Formal_Package
9647 (Formal : Node_Id;
9648 Actual : Node_Id;
9649 Analyzed_Formal : Node_Id) return List_Id
9650 is
9651 Loc : constant Source_Ptr := Sloc (Actual);
9652 Actual_Pack : Entity_Id;
9653 Formal_Pack : Entity_Id;
9654 Gen_Parent : Entity_Id;
9655 Decls : List_Id;
9656 Nod : Node_Id;
9657 Parent_Spec : Node_Id;
9658
9659 procedure Find_Matching_Actual
9660 (F : Node_Id;
9661 Act : in out Entity_Id);
9662 -- We need to associate each formal entity in the formal package with
9663 -- the corresponding entity in the actual package. The actual package
9664 -- has been analyzed and possibly expanded, and as a result there is
9665 -- no one-to-one correspondence between the two lists (for example,
9666 -- the actual may include subtypes, itypes, and inherited primitive
9667 -- operations, interspersed among the renaming declarations for the
9668 -- actuals). We retrieve the corresponding actual by name because each
9669 -- actual has the same name as the formal, and they do appear in the
9670 -- same order.
9671
9672 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9673 -- Retrieve entity of defining entity of generic formal parameter.
9674 -- Only the declarations of formals need to be considered when
9675 -- linking them to actuals, but the declarative list may include
9676 -- internal entities generated during analysis, and those are ignored.
9677
9678 procedure Match_Formal_Entity
9679 (Formal_Node : Node_Id;
9680 Formal_Ent : Entity_Id;
9681 Actual_Ent : Entity_Id);
9682 -- Associates the formal entity with the actual. In the case where
9683 -- Formal_Ent is a formal package, this procedure iterates through all
9684 -- of its formals and enters associations between the actuals occurring
9685 -- in the formal package's corresponding actual package (given by
9686 -- Actual_Ent) and the formal package's formal parameters. This
9687 -- procedure recurses if any of the parameters is itself a package.
9688
9689 function Is_Instance_Of
9690 (Act_Spec : Entity_Id;
9691 Gen_Anc : Entity_Id) return Boolean;
9692 -- The actual can be an instantiation of a generic within another
9693 -- instance, in which case there is no direct link from it to the
9694 -- original generic ancestor. In that case, we recognize that the
9695 -- ultimate ancestor is the same by examining names and scopes.
9696
9697 procedure Process_Nested_Formal (Formal : Entity_Id);
9698 -- If the current formal is declared with a box, its own formals are
9699 -- visible in the instance, as they were in the generic, and their
9700 -- Hidden flag must be reset. If some of these formals are themselves
9701 -- packages declared with a box, the processing must be recursive.
9702
9703 --------------------------
9704 -- Find_Matching_Actual --
9705 --------------------------
9706
9707 procedure Find_Matching_Actual
9708 (F : Node_Id;
9709 Act : in out Entity_Id)
9710 is
9711 Formal_Ent : Entity_Id;
9712
9713 begin
9714 case Nkind (Original_Node (F)) is
9715 when N_Formal_Object_Declaration
9716 | N_Formal_Type_Declaration
9717 =>
9718 Formal_Ent := Defining_Identifier (F);
9719
9720 while Chars (Act) /= Chars (Formal_Ent) loop
9721 Next_Entity (Act);
9722 end loop;
9723
9724 when N_Formal_Package_Declaration
9725 | N_Formal_Subprogram_Declaration
9726 | N_Generic_Package_Declaration
9727 | N_Package_Declaration
9728 =>
9729 Formal_Ent := Defining_Entity (F);
9730
9731 while Chars (Act) /= Chars (Formal_Ent) loop
9732 Next_Entity (Act);
9733 end loop;
9734
9735 when others =>
9736 raise Program_Error;
9737 end case;
9738 end Find_Matching_Actual;
9739
9740 -------------------------
9741 -- Match_Formal_Entity --
9742 -------------------------
9743
9744 procedure Match_Formal_Entity
9745 (Formal_Node : Node_Id;
9746 Formal_Ent : Entity_Id;
9747 Actual_Ent : Entity_Id)
9748 is
9749 Act_Pkg : Entity_Id;
9750
9751 begin
9752 Set_Instance_Of (Formal_Ent, Actual_Ent);
9753
9754 if Ekind (Actual_Ent) = E_Package then
9755
9756 -- Record associations for each parameter
9757
9758 Act_Pkg := Actual_Ent;
9759
9760 declare
9761 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9762 F_Ent : Entity_Id;
9763 F_Node : Node_Id;
9764
9765 Gen_Decl : Node_Id;
9766 Formals : List_Id;
9767 Actual : Entity_Id;
9768
9769 begin
9770 -- Retrieve the actual given in the formal package declaration
9771
9772 Actual := Entity (Name (Original_Node (Formal_Node)));
9773
9774 -- The actual in the formal package declaration may be a
9775 -- renamed generic package, in which case we want to retrieve
9776 -- the original generic in order to traverse its formal part.
9777
9778 if Present (Renamed_Entity (Actual)) then
9779 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9780 else
9781 Gen_Decl := Unit_Declaration_Node (Actual);
9782 end if;
9783
9784 Formals := Generic_Formal_Declarations (Gen_Decl);
9785
9786 if Present (Formals) then
9787 F_Node := First_Non_Pragma (Formals);
9788 else
9789 F_Node := Empty;
9790 end if;
9791
9792 while Present (A_Ent)
9793 and then Present (F_Node)
9794 and then A_Ent /= First_Private_Entity (Act_Pkg)
9795 loop
9796 F_Ent := Get_Formal_Entity (F_Node);
9797
9798 if Present (F_Ent) then
9799
9800 -- This is a formal of the original package. Record
9801 -- association and recurse.
9802
9803 Find_Matching_Actual (F_Node, A_Ent);
9804 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9805 Next_Entity (A_Ent);
9806 end if;
9807
9808 Next_Non_Pragma (F_Node);
9809 end loop;
9810 end;
9811 end if;
9812 end Match_Formal_Entity;
9813
9814 -----------------------
9815 -- Get_Formal_Entity --
9816 -----------------------
9817
9818 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9819 Kind : constant Node_Kind := Nkind (Original_Node (N));
9820 begin
9821 case Kind is
9822 when N_Formal_Object_Declaration =>
9823 return Defining_Identifier (N);
9824
9825 when N_Formal_Type_Declaration =>
9826 return Defining_Identifier (N);
9827
9828 when N_Formal_Subprogram_Declaration =>
9829 return Defining_Unit_Name (Specification (N));
9830
9831 when N_Formal_Package_Declaration =>
9832 return Defining_Identifier (Original_Node (N));
9833
9834 when N_Generic_Package_Declaration =>
9835 return Defining_Identifier (Original_Node (N));
9836
9837 -- All other declarations are introduced by semantic analysis and
9838 -- have no match in the actual.
9839
9840 when others =>
9841 return Empty;
9842 end case;
9843 end Get_Formal_Entity;
9844
9845 --------------------
9846 -- Is_Instance_Of --
9847 --------------------
9848
9849 function Is_Instance_Of
9850 (Act_Spec : Entity_Id;
9851 Gen_Anc : Entity_Id) return Boolean
9852 is
9853 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9854
9855 begin
9856 if No (Gen_Par) then
9857 return False;
9858
9859 -- Simplest case: the generic parent of the actual is the formal
9860
9861 elsif Gen_Par = Gen_Anc then
9862 return True;
9863
9864 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9865 return False;
9866
9867 -- The actual may be obtained through several instantiations. Its
9868 -- scope must itself be an instance of a generic declared in the
9869 -- same scope as the formal. Any other case is detected above.
9870
9871 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9872 return False;
9873
9874 else
9875 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9876 end if;
9877 end Is_Instance_Of;
9878
9879 ---------------------------
9880 -- Process_Nested_Formal --
9881 ---------------------------
9882
9883 procedure Process_Nested_Formal (Formal : Entity_Id) is
9884 Ent : Entity_Id;
9885
9886 begin
9887 if Present (Associated_Formal_Package (Formal))
9888 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9889 then
9890 Ent := First_Entity (Formal);
9891 while Present (Ent) loop
9892 Set_Is_Hidden (Ent, False);
9893 Set_Is_Visible_Formal (Ent);
9894 Set_Is_Potentially_Use_Visible
9895 (Ent, Is_Potentially_Use_Visible (Formal));
9896
9897 if Ekind (Ent) = E_Package then
9898 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9899 Process_Nested_Formal (Ent);
9900 end if;
9901
9902 Next_Entity (Ent);
9903 end loop;
9904 end if;
9905 end Process_Nested_Formal;
9906
9907 -- Start of processing for Instantiate_Formal_Package
9908
9909 begin
9910 Analyze (Actual);
9911
9912 if not Is_Entity_Name (Actual)
9913 or else Ekind (Entity (Actual)) /= E_Package
9914 then
9915 Error_Msg_N
9916 ("expect package instance to instantiate formal", Actual);
9917 Abandon_Instantiation (Actual);
9918 raise Program_Error;
9919
9920 else
9921 Actual_Pack := Entity (Actual);
9922 Set_Is_Instantiated (Actual_Pack);
9923
9924 -- The actual may be a renamed package, or an outer generic formal
9925 -- package whose instantiation is converted into a renaming.
9926
9927 if Present (Renamed_Object (Actual_Pack)) then
9928 Actual_Pack := Renamed_Object (Actual_Pack);
9929 end if;
9930
9931 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9932 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9933 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9934 else
9935 Gen_Parent :=
9936 Generic_Parent (Specification (Analyzed_Formal));
9937 Formal_Pack :=
9938 Defining_Unit_Name (Specification (Analyzed_Formal));
9939 end if;
9940
9941 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9942 Parent_Spec := Package_Specification (Actual_Pack);
9943 else
9944 Parent_Spec := Parent (Actual_Pack);
9945 end if;
9946
9947 if Gen_Parent = Any_Id then
9948 Error_Msg_N
9949 ("previous error in declaration of formal package", Actual);
9950 Abandon_Instantiation (Actual);
9951
9952 elsif
9953 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9954 then
9955 null;
9956
9957 else
9958 Error_Msg_NE
9959 ("actual parameter must be instance of&", Actual, Gen_Parent);
9960 Abandon_Instantiation (Actual);
9961 end if;
9962
9963 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9964 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9965
9966 Nod :=
9967 Make_Package_Renaming_Declaration (Loc,
9968 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9969 Name => New_Occurrence_Of (Actual_Pack, Loc));
9970
9971 Set_Associated_Formal_Package
9972 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
9973 Decls := New_List (Nod);
9974
9975 -- If the formal F has a box, then the generic declarations are
9976 -- visible in the generic G. In an instance of G, the corresponding
9977 -- entities in the actual for F (which are the actuals for the
9978 -- instantiation of the generic that F denotes) must also be made
9979 -- visible for analysis of the current instance. On exit from the
9980 -- current instance, those entities are made private again. If the
9981 -- actual is currently in use, these entities are also use-visible.
9982
9983 -- The loop through the actual entities also steps through the formal
9984 -- entities and enters associations from formals to actuals into the
9985 -- renaming map. This is necessary to properly handle checking of
9986 -- actual parameter associations for later formals that depend on
9987 -- actuals declared in the formal package.
9988
9989 -- In Ada 2005, partial parameterization requires that we make
9990 -- visible the actuals corresponding to formals that were defaulted
9991 -- in the formal package. There formals are identified because they
9992 -- remain formal generics within the formal package, rather than
9993 -- being renamings of the actuals supplied.
9994
9995 declare
9996 Gen_Decl : constant Node_Id :=
9997 Unit_Declaration_Node (Gen_Parent);
9998 Formals : constant List_Id :=
9999 Generic_Formal_Declarations (Gen_Decl);
10000
10001 Actual_Ent : Entity_Id;
10002 Actual_Of_Formal : Node_Id;
10003 Formal_Node : Node_Id;
10004 Formal_Ent : Entity_Id;
10005
10006 begin
10007 if Present (Formals) then
10008 Formal_Node := First_Non_Pragma (Formals);
10009 else
10010 Formal_Node := Empty;
10011 end if;
10012
10013 Actual_Ent := First_Entity (Actual_Pack);
10014 Actual_Of_Formal :=
10015 First (Visible_Declarations (Specification (Analyzed_Formal)));
10016 while Present (Actual_Ent)
10017 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10018 loop
10019 if Present (Formal_Node) then
10020 Formal_Ent := Get_Formal_Entity (Formal_Node);
10021
10022 if Present (Formal_Ent) then
10023 Find_Matching_Actual (Formal_Node, Actual_Ent);
10024 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10025
10026 -- We iterate at the same time over the actuals of the
10027 -- local package created for the formal, to determine
10028 -- which one of the formals of the original generic were
10029 -- defaulted in the formal. The corresponding actual
10030 -- entities are visible in the enclosing instance.
10031
10032 if Box_Present (Formal)
10033 or else
10034 (Present (Actual_Of_Formal)
10035 and then
10036 Is_Generic_Formal
10037 (Get_Formal_Entity (Actual_Of_Formal)))
10038 then
10039 Set_Is_Hidden (Actual_Ent, False);
10040 Set_Is_Visible_Formal (Actual_Ent);
10041 Set_Is_Potentially_Use_Visible
10042 (Actual_Ent, In_Use (Actual_Pack));
10043
10044 if Ekind (Actual_Ent) = E_Package then
10045 Process_Nested_Formal (Actual_Ent);
10046 end if;
10047
10048 else
10049 Set_Is_Hidden (Actual_Ent);
10050 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10051 end if;
10052 end if;
10053
10054 Next_Non_Pragma (Formal_Node);
10055 Next (Actual_Of_Formal);
10056
10057 else
10058 -- No further formals to match, but the generic part may
10059 -- contain inherited operation that are not hidden in the
10060 -- enclosing instance.
10061
10062 Next_Entity (Actual_Ent);
10063 end if;
10064 end loop;
10065
10066 -- Inherited subprograms generated by formal derived types are
10067 -- also visible if the types are.
10068
10069 Actual_Ent := First_Entity (Actual_Pack);
10070 while Present (Actual_Ent)
10071 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10072 loop
10073 if Is_Overloadable (Actual_Ent)
10074 and then
10075 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10076 and then
10077 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10078 then
10079 Set_Is_Hidden (Actual_Ent, False);
10080 Set_Is_Potentially_Use_Visible
10081 (Actual_Ent, In_Use (Actual_Pack));
10082 end if;
10083
10084 Next_Entity (Actual_Ent);
10085 end loop;
10086 end;
10087
10088 -- If the formal is not declared with a box, reanalyze it as an
10089 -- abbreviated instantiation, to verify the matching rules of 12.7.
10090 -- The actual checks are performed after the generic associations
10091 -- have been analyzed, to guarantee the same visibility for this
10092 -- instantiation and for the actuals.
10093
10094 -- In Ada 2005, the generic associations for the formal can include
10095 -- defaulted parameters. These are ignored during check. This
10096 -- internal instantiation is removed from the tree after conformance
10097 -- checking, because it contains formal declarations for those
10098 -- defaulted parameters, and those should not reach the back-end.
10099
10100 if not Box_Present (Formal) then
10101 declare
10102 I_Pack : constant Entity_Id :=
10103 Make_Temporary (Sloc (Actual), 'P');
10104
10105 begin
10106 Set_Is_Internal (I_Pack);
10107
10108 Append_To (Decls,
10109 Make_Package_Instantiation (Sloc (Actual),
10110 Defining_Unit_Name => I_Pack,
10111 Name =>
10112 New_Occurrence_Of
10113 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10114 Generic_Associations => Generic_Associations (Formal)));
10115 end;
10116 end if;
10117
10118 return Decls;
10119 end if;
10120 end Instantiate_Formal_Package;
10121
10122 -----------------------------------
10123 -- Instantiate_Formal_Subprogram --
10124 -----------------------------------
10125
10126 function Instantiate_Formal_Subprogram
10127 (Formal : Node_Id;
10128 Actual : Node_Id;
10129 Analyzed_Formal : Node_Id) return Node_Id
10130 is
10131 Analyzed_S : constant Entity_Id :=
10132 Defining_Unit_Name (Specification (Analyzed_Formal));
10133 Formal_Sub : constant Entity_Id :=
10134 Defining_Unit_Name (Specification (Formal));
10135
10136 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10137 -- If the generic is a child unit, the parent has been installed on the
10138 -- scope stack, but a default subprogram cannot resolve to something
10139 -- on the parent because that parent is not really part of the visible
10140 -- context (it is there to resolve explicit local entities). If the
10141 -- default has resolved in this way, we remove the entity from immediate
10142 -- visibility and analyze the node again to emit an error message or
10143 -- find another visible candidate.
10144
10145 procedure Valid_Actual_Subprogram (Act : Node_Id);
10146 -- Perform legality check and raise exception on failure
10147
10148 -----------------------
10149 -- From_Parent_Scope --
10150 -----------------------
10151
10152 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10153 Gen_Scope : Node_Id;
10154
10155 begin
10156 Gen_Scope := Scope (Analyzed_S);
10157 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10158 if Scope (Subp) = Scope (Gen_Scope) then
10159 return True;
10160 end if;
10161
10162 Gen_Scope := Scope (Gen_Scope);
10163 end loop;
10164
10165 return False;
10166 end From_Parent_Scope;
10167
10168 -----------------------------
10169 -- Valid_Actual_Subprogram --
10170 -----------------------------
10171
10172 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10173 Act_E : Entity_Id;
10174
10175 begin
10176 if Is_Entity_Name (Act) then
10177 Act_E := Entity (Act);
10178
10179 elsif Nkind (Act) = N_Selected_Component
10180 and then Is_Entity_Name (Selector_Name (Act))
10181 then
10182 Act_E := Entity (Selector_Name (Act));
10183
10184 else
10185 Act_E := Empty;
10186 end if;
10187
10188 if (Present (Act_E) and then Is_Overloadable (Act_E))
10189 or else Nkind_In (Act, N_Attribute_Reference,
10190 N_Indexed_Component,
10191 N_Character_Literal,
10192 N_Explicit_Dereference)
10193 then
10194 return;
10195 end if;
10196
10197 Error_Msg_NE
10198 ("expect subprogram or entry name in instantiation of &",
10199 Instantiation_Node, Formal_Sub);
10200 Abandon_Instantiation (Instantiation_Node);
10201 end Valid_Actual_Subprogram;
10202
10203 -- Local variables
10204
10205 Decl_Node : Node_Id;
10206 Loc : Source_Ptr;
10207 Nam : Node_Id;
10208 New_Spec : Node_Id;
10209 New_Subp : Entity_Id;
10210
10211 -- Start of processing for Instantiate_Formal_Subprogram
10212
10213 begin
10214 New_Spec := New_Copy_Tree (Specification (Formal));
10215
10216 -- The tree copy has created the proper instantiation sloc for the
10217 -- new specification. Use this location for all other constructed
10218 -- declarations.
10219
10220 Loc := Sloc (Defining_Unit_Name (New_Spec));
10221
10222 -- Create new entity for the actual (New_Copy_Tree does not), and
10223 -- indicate that it is an actual.
10224
10225 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10226 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10227 Set_Is_Generic_Actual_Subprogram (New_Subp);
10228 Set_Defining_Unit_Name (New_Spec, New_Subp);
10229
10230 -- Create new entities for the each of the formals in the specification
10231 -- of the renaming declaration built for the actual.
10232
10233 if Present (Parameter_Specifications (New_Spec)) then
10234 declare
10235 F : Node_Id;
10236 F_Id : Entity_Id;
10237
10238 begin
10239 F := First (Parameter_Specifications (New_Spec));
10240 while Present (F) loop
10241 F_Id := Defining_Identifier (F);
10242
10243 Set_Defining_Identifier (F,
10244 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10245 Next (F);
10246 end loop;
10247 end;
10248 end if;
10249
10250 -- Find entity of actual. If the actual is an attribute reference, it
10251 -- cannot be resolved here (its formal is missing) but is handled
10252 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10253 -- fully resolved subsequently, when the renaming declaration for the
10254 -- formal is analyzed. If it is an explicit dereference, resolve the
10255 -- prefix but not the actual itself, to prevent interpretation as call.
10256
10257 if Present (Actual) then
10258 Loc := Sloc (Actual);
10259 Set_Sloc (New_Spec, Loc);
10260
10261 if Nkind (Actual) = N_Operator_Symbol then
10262 Find_Direct_Name (Actual);
10263
10264 elsif Nkind (Actual) = N_Explicit_Dereference then
10265 Analyze (Prefix (Actual));
10266
10267 elsif Nkind (Actual) /= N_Attribute_Reference then
10268 Analyze (Actual);
10269 end if;
10270
10271 Valid_Actual_Subprogram (Actual);
10272 Nam := Actual;
10273
10274 elsif Present (Default_Name (Formal)) then
10275 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
10276 N_Selected_Component,
10277 N_Indexed_Component,
10278 N_Character_Literal)
10279 and then Present (Entity (Default_Name (Formal)))
10280 then
10281 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10282 else
10283 Nam := New_Copy (Default_Name (Formal));
10284 Set_Sloc (Nam, Loc);
10285 end if;
10286
10287 elsif Box_Present (Formal) then
10288
10289 -- Actual is resolved at the point of instantiation. Create an
10290 -- identifier or operator with the same name as the formal.
10291
10292 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10293 Nam :=
10294 Make_Operator_Symbol (Loc,
10295 Chars => Chars (Formal_Sub),
10296 Strval => No_String);
10297 else
10298 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10299 end if;
10300
10301 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10302 and then Null_Present (Specification (Formal))
10303 then
10304 -- Generate null body for procedure, for use in the instance
10305
10306 Decl_Node :=
10307 Make_Subprogram_Body (Loc,
10308 Specification => New_Spec,
10309 Declarations => New_List,
10310 Handled_Statement_Sequence =>
10311 Make_Handled_Sequence_Of_Statements (Loc,
10312 Statements => New_List (Make_Null_Statement (Loc))));
10313
10314 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10315 return Decl_Node;
10316
10317 else
10318 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10319 Error_Msg_NE
10320 ("missing actual&", Instantiation_Node, Formal_Sub);
10321 Error_Msg_NE
10322 ("\in instantiation of & declared#",
10323 Instantiation_Node, Scope (Analyzed_S));
10324 Abandon_Instantiation (Instantiation_Node);
10325 end if;
10326
10327 Decl_Node :=
10328 Make_Subprogram_Renaming_Declaration (Loc,
10329 Specification => New_Spec,
10330 Name => Nam);
10331
10332 -- If we do not have an actual and the formal specified <> then set to
10333 -- get proper default.
10334
10335 if No (Actual) and then Box_Present (Formal) then
10336 Set_From_Default (Decl_Node);
10337 end if;
10338
10339 -- Gather possible interpretations for the actual before analyzing the
10340 -- instance. If overloaded, it will be resolved when analyzing the
10341 -- renaming declaration.
10342
10343 if Box_Present (Formal) and then No (Actual) then
10344 Analyze (Nam);
10345
10346 if Is_Child_Unit (Scope (Analyzed_S))
10347 and then Present (Entity (Nam))
10348 then
10349 if not Is_Overloaded (Nam) then
10350 if From_Parent_Scope (Entity (Nam)) then
10351 Set_Is_Immediately_Visible (Entity (Nam), False);
10352 Set_Entity (Nam, Empty);
10353 Set_Etype (Nam, Empty);
10354
10355 Analyze (Nam);
10356 Set_Is_Immediately_Visible (Entity (Nam));
10357 end if;
10358
10359 else
10360 declare
10361 I : Interp_Index;
10362 It : Interp;
10363
10364 begin
10365 Get_First_Interp (Nam, I, It);
10366 while Present (It.Nam) loop
10367 if From_Parent_Scope (It.Nam) then
10368 Remove_Interp (I);
10369 end if;
10370
10371 Get_Next_Interp (I, It);
10372 end loop;
10373 end;
10374 end if;
10375 end if;
10376 end if;
10377
10378 -- The generic instantiation freezes the actual. This can only be done
10379 -- once the actual is resolved, in the analysis of the renaming
10380 -- declaration. To make the formal subprogram entity available, we set
10381 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10382 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10383 -- of formal abstract subprograms.
10384
10385 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10386
10387 -- We cannot analyze the renaming declaration, and thus find the actual,
10388 -- until all the actuals are assembled in the instance. For subsequent
10389 -- checks of other actuals, indicate the node that will hold the
10390 -- instance of this formal.
10391
10392 Set_Instance_Of (Analyzed_S, Nam);
10393
10394 if Nkind (Actual) = N_Selected_Component
10395 and then Is_Task_Type (Etype (Prefix (Actual)))
10396 and then not Is_Frozen (Etype (Prefix (Actual)))
10397 then
10398 -- The renaming declaration will create a body, which must appear
10399 -- outside of the instantiation, We move the renaming declaration
10400 -- out of the instance, and create an additional renaming inside,
10401 -- to prevent freezing anomalies.
10402
10403 declare
10404 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10405
10406 begin
10407 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10408 Insert_Before (Instantiation_Node, Decl_Node);
10409 Analyze (Decl_Node);
10410
10411 -- Now create renaming within the instance
10412
10413 Decl_Node :=
10414 Make_Subprogram_Renaming_Declaration (Loc,
10415 Specification => New_Copy_Tree (New_Spec),
10416 Name => New_Occurrence_Of (Anon_Id, Loc));
10417
10418 Set_Defining_Unit_Name (Specification (Decl_Node),
10419 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10420 end;
10421 end if;
10422
10423 return Decl_Node;
10424 end Instantiate_Formal_Subprogram;
10425
10426 ------------------------
10427 -- Instantiate_Object --
10428 ------------------------
10429
10430 function Instantiate_Object
10431 (Formal : Node_Id;
10432 Actual : Node_Id;
10433 Analyzed_Formal : Node_Id) return List_Id
10434 is
10435 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10436 A_Gen_Obj : constant Entity_Id :=
10437 Defining_Identifier (Analyzed_Formal);
10438 Acc_Def : Node_Id := Empty;
10439 Act_Assoc : constant Node_Id := Parent (Actual);
10440 Actual_Decl : Node_Id := Empty;
10441 Decl_Node : Node_Id;
10442 Def : Node_Id;
10443 Ftyp : Entity_Id;
10444 List : constant List_Id := New_List;
10445 Loc : constant Source_Ptr := Sloc (Actual);
10446 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10447 Subt_Decl : Node_Id := Empty;
10448 Subt_Mark : Node_Id := Empty;
10449
10450 function Copy_Access_Def return Node_Id;
10451 -- If formal is an anonymous access, copy access definition of formal
10452 -- for generated object declaration.
10453
10454 ---------------------
10455 -- Copy_Access_Def --
10456 ---------------------
10457
10458 function Copy_Access_Def return Node_Id is
10459 begin
10460 Def := New_Copy_Tree (Acc_Def);
10461
10462 -- In addition, if formal is an access to subprogram we need to
10463 -- generate new formals for the signature of the default, so that
10464 -- the tree is properly formatted for ASIS use.
10465
10466 if Present (Access_To_Subprogram_Definition (Acc_Def)) then
10467 declare
10468 Par_Spec : Node_Id;
10469 begin
10470 Par_Spec :=
10471 First (Parameter_Specifications
10472 (Access_To_Subprogram_Definition (Def)));
10473 while Present (Par_Spec) loop
10474 Set_Defining_Identifier (Par_Spec,
10475 Make_Defining_Identifier (Sloc (Acc_Def),
10476 Chars => Chars (Defining_Identifier (Par_Spec))));
10477 Next (Par_Spec);
10478 end loop;
10479 end;
10480 end if;
10481
10482 return Def;
10483 end Copy_Access_Def;
10484
10485 -- Start of processing for Instantiate_Object
10486
10487 begin
10488 -- Formal may be an anonymous access
10489
10490 if Present (Subtype_Mark (Formal)) then
10491 Subt_Mark := Subtype_Mark (Formal);
10492 else
10493 Check_Access_Definition (Formal);
10494 Acc_Def := Access_Definition (Formal);
10495 end if;
10496
10497 -- Sloc for error message on missing actual
10498
10499 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10500
10501 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10502 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10503 end if;
10504
10505 Set_Parent (List, Parent (Actual));
10506
10507 -- OUT present
10508
10509 if Out_Present (Formal) then
10510
10511 -- An IN OUT generic actual must be a name. The instantiation is a
10512 -- renaming declaration. The actual is the name being renamed. We
10513 -- use the actual directly, rather than a copy, because it is not
10514 -- used further in the list of actuals, and because a copy or a use
10515 -- of relocate_node is incorrect if the instance is nested within a
10516 -- generic. In order to simplify ASIS searches, the Generic_Parent
10517 -- field links the declaration to the generic association.
10518
10519 if No (Actual) then
10520 Error_Msg_NE
10521 ("missing actual &",
10522 Instantiation_Node, Gen_Obj);
10523 Error_Msg_NE
10524 ("\in instantiation of & declared#",
10525 Instantiation_Node, Scope (A_Gen_Obj));
10526 Abandon_Instantiation (Instantiation_Node);
10527 end if;
10528
10529 if Present (Subt_Mark) then
10530 Decl_Node :=
10531 Make_Object_Renaming_Declaration (Loc,
10532 Defining_Identifier => New_Copy (Gen_Obj),
10533 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10534 Name => Actual);
10535
10536 else pragma Assert (Present (Acc_Def));
10537 Decl_Node :=
10538 Make_Object_Renaming_Declaration (Loc,
10539 Defining_Identifier => New_Copy (Gen_Obj),
10540 Access_Definition => New_Copy_Tree (Acc_Def),
10541 Name => Actual);
10542 end if;
10543
10544 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10545
10546 -- The analysis of the actual may produce Insert_Action nodes, so
10547 -- the declaration must have a context in which to attach them.
10548
10549 Append (Decl_Node, List);
10550 Analyze (Actual);
10551
10552 -- Return if the analysis of the actual reported some error
10553
10554 if Etype (Actual) = Any_Type then
10555 return List;
10556 end if;
10557
10558 -- This check is performed here because Analyze_Object_Renaming will
10559 -- not check it when Comes_From_Source is False. Note though that the
10560 -- check for the actual being the name of an object will be performed
10561 -- in Analyze_Object_Renaming.
10562
10563 if Is_Object_Reference (Actual)
10564 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10565 then
10566 Error_Msg_N
10567 ("illegal discriminant-dependent component for in out parameter",
10568 Actual);
10569 end if;
10570
10571 -- The actual has to be resolved in order to check that it is a
10572 -- variable (due to cases such as F (1), where F returns access to
10573 -- an array, and for overloaded prefixes).
10574
10575 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10576
10577 -- If the type of the formal is not itself a formal, and the current
10578 -- unit is a child unit, the formal type must be declared in a
10579 -- parent, and must be retrieved by visibility.
10580
10581 if Ftyp = Orig_Ftyp
10582 and then Is_Generic_Unit (Scope (Ftyp))
10583 and then Is_Child_Unit (Scope (A_Gen_Obj))
10584 then
10585 declare
10586 Temp : constant Node_Id :=
10587 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10588 begin
10589 Set_Entity (Temp, Empty);
10590 Find_Type (Temp);
10591 Ftyp := Entity (Temp);
10592 end;
10593 end if;
10594
10595 if Is_Private_Type (Ftyp)
10596 and then not Is_Private_Type (Etype (Actual))
10597 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10598 or else Base_Type (Etype (Actual)) = Ftyp)
10599 then
10600 -- If the actual has the type of the full view of the formal, or
10601 -- else a non-private subtype of the formal, then the visibility
10602 -- of the formal type has changed. Add to the actuals a subtype
10603 -- declaration that will force the exchange of views in the body
10604 -- of the instance as well.
10605
10606 Subt_Decl :=
10607 Make_Subtype_Declaration (Loc,
10608 Defining_Identifier => Make_Temporary (Loc, 'P'),
10609 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10610
10611 Prepend (Subt_Decl, List);
10612
10613 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10614 Exchange_Declarations (Ftyp);
10615 end if;
10616
10617 Resolve (Actual, Ftyp);
10618
10619 if not Denotes_Variable (Actual) then
10620 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
10621
10622 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10623
10624 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10625 -- the type of the actual shall resolve to a specific anonymous
10626 -- access type.
10627
10628 if Ada_Version < Ada_2005
10629 or else Ekind (Base_Type (Ftyp)) /=
10630 E_Anonymous_Access_Type
10631 or else Ekind (Base_Type (Etype (Actual))) /=
10632 E_Anonymous_Access_Type
10633 then
10634 Error_Msg_NE
10635 ("type of actual does not match type of&", Actual, Gen_Obj);
10636 end if;
10637 end if;
10638
10639 Note_Possible_Modification (Actual, Sure => True);
10640
10641 -- Check for instantiation of atomic/volatile actual for
10642 -- non-atomic/volatile formal (RM C.6 (12)).
10643
10644 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
10645 Error_Msg_N
10646 ("cannot instantiate non-atomic formal object "
10647 & "with atomic actual", Actual);
10648
10649 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
10650 then
10651 Error_Msg_N
10652 ("cannot instantiate non-volatile formal object "
10653 & "with volatile actual", Actual);
10654 end if;
10655
10656 -- Formal in-parameter
10657
10658 else
10659 -- The instantiation of a generic formal in-parameter is constant
10660 -- declaration. The actual is the expression for that declaration.
10661 -- Its type is a full copy of the type of the formal. This may be
10662 -- an access to subprogram, for which we need to generate entities
10663 -- for the formals in the new signature.
10664
10665 if Present (Actual) then
10666 if Present (Subt_Mark) then
10667 Def := New_Copy_Tree (Subt_Mark);
10668 else pragma Assert (Present (Acc_Def));
10669 Def := Copy_Access_Def;
10670 end if;
10671
10672 Decl_Node :=
10673 Make_Object_Declaration (Loc,
10674 Defining_Identifier => New_Copy (Gen_Obj),
10675 Constant_Present => True,
10676 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10677 Object_Definition => Def,
10678 Expression => Actual);
10679
10680 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10681
10682 -- A generic formal object of a tagged type is defined to be
10683 -- aliased so the new constant must also be treated as aliased.
10684
10685 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
10686 Set_Aliased_Present (Decl_Node);
10687 end if;
10688
10689 Append (Decl_Node, List);
10690
10691 -- No need to repeat (pre-)analysis of some expression nodes
10692 -- already handled in Preanalyze_Actuals.
10693
10694 if Nkind (Actual) /= N_Allocator then
10695 Analyze (Actual);
10696
10697 -- Return if the analysis of the actual reported some error
10698
10699 if Etype (Actual) = Any_Type then
10700 return List;
10701 end if;
10702 end if;
10703
10704 declare
10705 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
10706 Typ : Entity_Id;
10707
10708 begin
10709 Typ := Get_Instance_Of (Formal_Type);
10710
10711 -- If the actual appears in the current or an enclosing scope,
10712 -- use its type directly. This is relevant if it has an actual
10713 -- subtype that is distinct from its nominal one. This cannot
10714 -- be done in general because the type of the actual may
10715 -- depend on other actuals, and only be fully determined when
10716 -- the enclosing instance is analyzed.
10717
10718 if Present (Etype (Actual))
10719 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
10720 then
10721 Freeze_Before (Instantiation_Node, Etype (Actual));
10722 else
10723 Freeze_Before (Instantiation_Node, Typ);
10724 end if;
10725
10726 -- If the actual is an aggregate, perform name resolution on
10727 -- its components (the analysis of an aggregate does not do it)
10728 -- to capture local names that may be hidden if the generic is
10729 -- a child unit.
10730
10731 if Nkind (Actual) = N_Aggregate then
10732 Preanalyze_And_Resolve (Actual, Typ);
10733 end if;
10734
10735 if Is_Limited_Type (Typ)
10736 and then not OK_For_Limited_Init (Typ, Actual)
10737 then
10738 Error_Msg_N
10739 ("initialization not allowed for limited types", Actual);
10740 Explain_Limited_Type (Typ, Actual);
10741 end if;
10742 end;
10743
10744 elsif Present (Default_Expression (Formal)) then
10745
10746 -- Use default to construct declaration
10747
10748 if Present (Subt_Mark) then
10749 Def := New_Copy (Subt_Mark);
10750 else pragma Assert (Present (Acc_Def));
10751 Def := Copy_Access_Def;
10752 end if;
10753
10754 Decl_Node :=
10755 Make_Object_Declaration (Sloc (Formal),
10756 Defining_Identifier => New_Copy (Gen_Obj),
10757 Constant_Present => True,
10758 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10759 Object_Definition => Def,
10760 Expression => New_Copy_Tree
10761 (Default_Expression (Formal)));
10762
10763 Append (Decl_Node, List);
10764 Set_Analyzed (Expression (Decl_Node), False);
10765
10766 else
10767 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
10768 Error_Msg_NE ("\in instantiation of & declared#",
10769 Instantiation_Node, Scope (A_Gen_Obj));
10770
10771 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10772
10773 -- Create dummy constant declaration so that instance can be
10774 -- analyzed, to minimize cascaded visibility errors.
10775
10776 if Present (Subt_Mark) then
10777 Def := Subt_Mark;
10778 else pragma Assert (Present (Acc_Def));
10779 Def := Acc_Def;
10780 end if;
10781
10782 Decl_Node :=
10783 Make_Object_Declaration (Loc,
10784 Defining_Identifier => New_Copy (Gen_Obj),
10785 Constant_Present => True,
10786 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10787 Object_Definition => New_Copy (Def),
10788 Expression =>
10789 Make_Attribute_Reference (Sloc (Gen_Obj),
10790 Attribute_Name => Name_First,
10791 Prefix => New_Copy (Def)));
10792
10793 Append (Decl_Node, List);
10794
10795 else
10796 Abandon_Instantiation (Instantiation_Node);
10797 end if;
10798 end if;
10799 end if;
10800
10801 if Nkind (Actual) in N_Has_Entity then
10802 Actual_Decl := Parent (Entity (Actual));
10803 end if;
10804
10805 -- Ada 2005 (AI-423): For a formal object declaration with a null
10806 -- exclusion or an access definition that has a null exclusion: If the
10807 -- actual matching the formal object declaration denotes a generic
10808 -- formal object of another generic unit G, and the instantiation
10809 -- containing the actual occurs within the body of G or within the body
10810 -- of a generic unit declared within the declarative region of G, then
10811 -- the declaration of the formal object of G must have a null exclusion.
10812 -- Otherwise, the subtype of the actual matching the formal object
10813 -- declaration shall exclude null.
10814
10815 if Ada_Version >= Ada_2005
10816 and then Present (Actual_Decl)
10817 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10818 N_Object_Declaration)
10819 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10820 and then not Has_Null_Exclusion (Actual_Decl)
10821 and then Has_Null_Exclusion (Analyzed_Formal)
10822 then
10823 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10824 Error_Msg_N
10825 ("actual must exclude null to match generic formal#", Actual);
10826 end if;
10827
10828 -- An effectively volatile object cannot be used as an actual in a
10829 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
10830 -- relevant only when SPARK_Mode is on as it is not a standard Ada
10831 -- legality rule, and also verifies that the actual is an object.
10832
10833 if SPARK_Mode = On
10834 and then Present (Actual)
10835 and then Is_Object_Reference (Actual)
10836 and then Is_Effectively_Volatile_Object (Actual)
10837 then
10838 Error_Msg_N
10839 ("volatile object cannot act as actual in generic instantiation",
10840 Actual);
10841 end if;
10842
10843 return List;
10844 end Instantiate_Object;
10845
10846 ------------------------------
10847 -- Instantiate_Package_Body --
10848 ------------------------------
10849
10850 -- WARNING: This routine manages Ghost regions. Return statements must be
10851 -- replaced by gotos which jump to the end of the routine and restore the
10852 -- Ghost mode.
10853
10854 procedure Instantiate_Package_Body
10855 (Body_Info : Pending_Body_Info;
10856 Inlined_Body : Boolean := False;
10857 Body_Optional : Boolean := False)
10858 is
10859 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10860 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
10861 Act_Spec : constant Node_Id := Specification (Act_Decl);
10862 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10863 Gen_Id : constant Node_Id := Name (Inst_Node);
10864 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10865 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10866 Loc : constant Source_Ptr := Sloc (Inst_Node);
10867
10868 Save_ISMP : constant Boolean :=
10869 Ignore_SPARK_Mode_Pragmas_In_Instance;
10870 Save_Style_Check : constant Boolean := Style_Check;
10871
10872 procedure Check_Initialized_Types;
10873 -- In a generic package body, an entity of a generic private type may
10874 -- appear uninitialized. This is suspicious, unless the actual is a
10875 -- fully initialized type.
10876
10877 -----------------------------
10878 -- Check_Initialized_Types --
10879 -----------------------------
10880
10881 procedure Check_Initialized_Types is
10882 Decl : Node_Id;
10883 Formal : Entity_Id;
10884 Actual : Entity_Id;
10885 Uninit_Var : Entity_Id;
10886
10887 begin
10888 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10889 while Present (Decl) loop
10890 Uninit_Var := Empty;
10891
10892 if Nkind (Decl) = N_Private_Extension_Declaration then
10893 Uninit_Var := Uninitialized_Variable (Decl);
10894
10895 elsif Nkind (Decl) = N_Formal_Type_Declaration
10896 and then Nkind (Formal_Type_Definition (Decl)) =
10897 N_Formal_Private_Type_Definition
10898 then
10899 Uninit_Var :=
10900 Uninitialized_Variable (Formal_Type_Definition (Decl));
10901 end if;
10902
10903 if Present (Uninit_Var) then
10904 Formal := Defining_Identifier (Decl);
10905 Actual := First_Entity (Act_Decl_Id);
10906
10907 -- For each formal there is a subtype declaration that renames
10908 -- the actual and has the same name as the formal. Locate the
10909 -- formal for warning message about uninitialized variables
10910 -- in the generic, for which the actual type should be a fully
10911 -- initialized type.
10912
10913 while Present (Actual) loop
10914 exit when Ekind (Actual) = E_Package
10915 and then Present (Renamed_Object (Actual));
10916
10917 if Chars (Actual) = Chars (Formal)
10918 and then not Is_Scalar_Type (Actual)
10919 and then not Is_Fully_Initialized_Type (Actual)
10920 and then Warn_On_No_Value_Assigned
10921 then
10922 Error_Msg_Node_2 := Formal;
10923 Error_Msg_NE
10924 ("generic unit has uninitialized variable& of "
10925 & "formal private type &?v?", Actual, Uninit_Var);
10926 Error_Msg_NE
10927 ("actual type for& should be fully initialized type?v?",
10928 Actual, Formal);
10929 exit;
10930 end if;
10931
10932 Next_Entity (Actual);
10933 end loop;
10934 end if;
10935
10936 Next (Decl);
10937 end loop;
10938 end Check_Initialized_Types;
10939
10940 -- Local variables
10941
10942 Act_Body : Node_Id;
10943 Act_Body_Id : Entity_Id;
10944 Act_Body_Name : Node_Id;
10945 Gen_Body : Node_Id;
10946 Gen_Body_Id : Node_Id;
10947 Mode : Ghost_Mode_Type;
10948 Par_Ent : Entity_Id := Empty;
10949 Par_Vis : Boolean := False;
10950
10951 Parent_Installed : Boolean := False;
10952
10953 Vis_Prims_List : Elist_Id := No_Elist;
10954 -- List of primitives made temporarily visible in the instantiation
10955 -- to match the visibility of the formal type.
10956
10957 -- Start of processing for Instantiate_Package_Body
10958
10959 begin
10960 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10961
10962 -- The instance body may already have been processed, as the parent of
10963 -- another instance that is inlined (Load_Parent_Of_Generic).
10964
10965 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10966 return;
10967 end if;
10968
10969 -- The package being instantiated may be subject to pragma Ghost. Set
10970 -- the mode now to ensure that any nodes generated during instantiation
10971 -- are properly marked as Ghost.
10972
10973 Set_Ghost_Mode (Act_Decl_Id, Mode);
10974
10975 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10976
10977 -- Re-establish the state of information on which checks are suppressed.
10978 -- This information was set in Body_Info at the point of instantiation,
10979 -- and now we restore it so that the instance is compiled using the
10980 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
10981
10982 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10983 Scope_Suppress := Body_Info.Scope_Suppress;
10984 Opt.Ada_Version := Body_Info.Version;
10985 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10986 Restore_Warnings (Body_Info.Warnings);
10987 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10988 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10989
10990 if No (Gen_Body_Id) then
10991
10992 -- Do not look for parent of generic body if none is required.
10993 -- This may happen when the routine is called as part of the
10994 -- Pending_Instantiations processing, when nested instances
10995 -- may precede the one generated from the main unit.
10996
10997 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10998 and then Body_Optional
10999 then
11000 goto Leave;
11001 else
11002 Load_Parent_Of_Generic
11003 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11004 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11005 end if;
11006 end if;
11007
11008 -- Establish global variable for sloc adjustment and for error recovery
11009 -- In the case of an instance body for an instantiation with actuals
11010 -- from a limited view, the instance body is placed at the beginning
11011 -- of the enclosing package body: use the body entity as the source
11012 -- location for nodes of the instance body.
11013
11014 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
11015 declare
11016 Scop : constant Entity_Id := Scope (Act_Decl_Id);
11017 Body_Id : constant Node_Id :=
11018 Corresponding_Body (Unit_Declaration_Node (Scop));
11019
11020 begin
11021 Instantiation_Node := Body_Id;
11022 end;
11023 else
11024 Instantiation_Node := Inst_Node;
11025 end if;
11026
11027 if Present (Gen_Body_Id) then
11028 Save_Env (Gen_Unit, Act_Decl_Id);
11029 Style_Check := False;
11030
11031 -- If the context of the instance is subject to SPARK_Mode "off", the
11032 -- annotation is missing, or the body is instantiated at a later pass
11033 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11034 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11035 -- instance.
11036
11037 if SPARK_Mode /= On
11038 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11039 then
11040 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11041 end if;
11042
11043 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11044 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11045
11046 Create_Instantiation_Source
11047 (Inst_Node, Gen_Body_Id, S_Adjustment);
11048
11049 Act_Body :=
11050 Copy_Generic_Node
11051 (Original_Node (Gen_Body), Empty, Instantiating => True);
11052
11053 -- Create proper (possibly qualified) defining name for the body, to
11054 -- correspond to the one in the spec.
11055
11056 Act_Body_Id :=
11057 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11058 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11059
11060 -- Some attributes of spec entity are not inherited by body entity
11061
11062 Set_Handler_Records (Act_Body_Id, No_List);
11063
11064 if Nkind (Defining_Unit_Name (Act_Spec)) =
11065 N_Defining_Program_Unit_Name
11066 then
11067 Act_Body_Name :=
11068 Make_Defining_Program_Unit_Name (Loc,
11069 Name =>
11070 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
11071 Defining_Identifier => Act_Body_Id);
11072 else
11073 Act_Body_Name := Act_Body_Id;
11074 end if;
11075
11076 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
11077
11078 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11079 Check_Generic_Actuals (Act_Decl_Id, False);
11080 Check_Initialized_Types;
11081
11082 -- Install primitives hidden at the point of the instantiation but
11083 -- visible when processing the generic formals
11084
11085 declare
11086 E : Entity_Id;
11087
11088 begin
11089 E := First_Entity (Act_Decl_Id);
11090 while Present (E) loop
11091 if Is_Type (E)
11092 and then not Is_Itype (E)
11093 and then Is_Generic_Actual_Type (E)
11094 and then Is_Tagged_Type (E)
11095 then
11096 Install_Hidden_Primitives
11097 (Prims_List => Vis_Prims_List,
11098 Gen_T => Generic_Parent_Type (Parent (E)),
11099 Act_T => E);
11100 end if;
11101
11102 Next_Entity (E);
11103 end loop;
11104 end;
11105
11106 -- If it is a child unit, make the parent instance (which is an
11107 -- instance of the parent of the generic) visible. The parent
11108 -- instance is the prefix of the name of the generic unit.
11109
11110 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11111 and then Nkind (Gen_Id) = N_Expanded_Name
11112 then
11113 Par_Ent := Entity (Prefix (Gen_Id));
11114 Par_Vis := Is_Immediately_Visible (Par_Ent);
11115 Install_Parent (Par_Ent, In_Body => True);
11116 Parent_Installed := True;
11117
11118 elsif Is_Child_Unit (Gen_Unit) then
11119 Par_Ent := Scope (Gen_Unit);
11120 Par_Vis := Is_Immediately_Visible (Par_Ent);
11121 Install_Parent (Par_Ent, In_Body => True);
11122 Parent_Installed := True;
11123 end if;
11124
11125 -- If the instantiation is a library unit, and this is the main unit,
11126 -- then build the resulting compilation unit nodes for the instance.
11127 -- If this is a compilation unit but it is not the main unit, then it
11128 -- is the body of a unit in the context, that is being compiled
11129 -- because it is encloses some inlined unit or another generic unit
11130 -- being instantiated. In that case, this body is not part of the
11131 -- current compilation, and is not attached to the tree, but its
11132 -- parent must be set for analysis.
11133
11134 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11135
11136 -- Replace instance node with body of instance, and create new
11137 -- node for corresponding instance declaration.
11138
11139 Build_Instance_Compilation_Unit_Nodes
11140 (Inst_Node, Act_Body, Act_Decl);
11141 Analyze (Inst_Node);
11142
11143 if Parent (Inst_Node) = Cunit (Main_Unit) then
11144
11145 -- If the instance is a child unit itself, then set the scope
11146 -- of the expanded body to be the parent of the instantiation
11147 -- (ensuring that the fully qualified name will be generated
11148 -- for the elaboration subprogram).
11149
11150 if Nkind (Defining_Unit_Name (Act_Spec)) =
11151 N_Defining_Program_Unit_Name
11152 then
11153 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
11154 end if;
11155 end if;
11156
11157 -- Case where instantiation is not a library unit
11158
11159 else
11160 -- If this is an early instantiation, i.e. appears textually
11161 -- before the corresponding body and must be elaborated first,
11162 -- indicate that the body instance is to be delayed.
11163
11164 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
11165
11166 -- Now analyze the body. We turn off all checks if this is an
11167 -- internal unit, since there is no reason to have checks on for
11168 -- any predefined run-time library code. All such code is designed
11169 -- to be compiled with checks off.
11170
11171 -- Note that we do NOT apply this criterion to children of GNAT
11172 -- The latter units must suppress checks explicitly if needed.
11173
11174 -- We also do not suppress checks in CodePeer mode where we are
11175 -- interested in finding possible runtime errors.
11176
11177 if not CodePeer_Mode
11178 and then Is_Predefined_File_Name
11179 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
11180 then
11181 Analyze (Act_Body, Suppress => All_Checks);
11182 else
11183 Analyze (Act_Body);
11184 end if;
11185 end if;
11186
11187 Inherit_Context (Gen_Body, Inst_Node);
11188
11189 -- Remove the parent instances if they have been placed on the scope
11190 -- stack to compile the body.
11191
11192 if Parent_Installed then
11193 Remove_Parent (In_Body => True);
11194
11195 -- Restore the previous visibility of the parent
11196
11197 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11198 end if;
11199
11200 Restore_Hidden_Primitives (Vis_Prims_List);
11201 Restore_Private_Views (Act_Decl_Id);
11202
11203 -- Remove the current unit from visibility if this is an instance
11204 -- that is not elaborated on the fly for inlining purposes.
11205
11206 if not Inlined_Body then
11207 Set_Is_Immediately_Visible (Act_Decl_Id, False);
11208 end if;
11209
11210 Restore_Env;
11211
11212 -- If we have no body, and the unit requires a body, then complain. This
11213 -- complaint is suppressed if we have detected other errors (since a
11214 -- common reason for missing the body is that it had errors).
11215 -- In CodePeer mode, a warning has been emitted already, no need for
11216 -- further messages.
11217
11218 elsif Unit_Requires_Body (Gen_Unit)
11219 and then not Body_Optional
11220 then
11221 if CodePeer_Mode then
11222 null;
11223
11224 elsif Serious_Errors_Detected = 0 then
11225 Error_Msg_NE
11226 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
11227
11228 -- Don't attempt to perform any cleanup actions if some other error
11229 -- was already detected, since this can cause blowups.
11230
11231 else
11232 goto Leave;
11233 end if;
11234
11235 -- Case of package that does not need a body
11236
11237 else
11238 -- If the instantiation of the declaration is a library unit, rewrite
11239 -- the original package instantiation as a package declaration in the
11240 -- compilation unit node.
11241
11242 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11243 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
11244 Rewrite (Inst_Node, Act_Decl);
11245
11246 -- Generate elaboration entity, in case spec has elaboration code.
11247 -- This cannot be done when the instance is analyzed, because it
11248 -- is not known yet whether the body exists.
11249
11250 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
11251 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
11252
11253 -- If the instantiation is not a library unit, then append the
11254 -- declaration to the list of implicitly generated entities, unless
11255 -- it is already a list member which means that it was already
11256 -- processed
11257
11258 elsif not Is_List_Member (Act_Decl) then
11259 Mark_Rewrite_Insertion (Act_Decl);
11260 Insert_Before (Inst_Node, Act_Decl);
11261 end if;
11262 end if;
11263
11264 Expander_Mode_Restore;
11265
11266 <<Leave>>
11267 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
11268 Style_Check := Save_Style_Check;
11269
11270 Restore_Ghost_Mode (Mode);
11271 end Instantiate_Package_Body;
11272
11273 ---------------------------------
11274 -- Instantiate_Subprogram_Body --
11275 ---------------------------------
11276
11277 -- WARNING: This routine manages Ghost regions. Return statements must be
11278 -- replaced by gotos which jump to the end of the routine and restore the
11279 -- Ghost mode.
11280
11281 procedure Instantiate_Subprogram_Body
11282 (Body_Info : Pending_Body_Info;
11283 Body_Optional : Boolean := False)
11284 is
11285 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11286 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11287 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11288 Gen_Id : constant Node_Id := Name (Inst_Node);
11289 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11290 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11291 Loc : constant Source_Ptr := Sloc (Inst_Node);
11292 Pack_Id : constant Entity_Id :=
11293 Defining_Unit_Name (Parent (Act_Decl));
11294
11295 Saved_ISMP : constant Boolean :=
11296 Ignore_SPARK_Mode_Pragmas_In_Instance;
11297 Saved_Style_Check : constant Boolean := Style_Check;
11298 Saved_Warnings : constant Warning_Record := Save_Warnings;
11299
11300 Act_Body : Node_Id;
11301 Act_Body_Id : Entity_Id;
11302 Gen_Body : Node_Id;
11303 Gen_Body_Id : Node_Id;
11304 Mode : Ghost_Mode_Type;
11305 Pack_Body : Node_Id;
11306 Par_Ent : Entity_Id := Empty;
11307 Par_Vis : Boolean := False;
11308 Ret_Expr : Node_Id;
11309
11310 Parent_Installed : Boolean := False;
11311
11312 begin
11313 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11314
11315 -- Subprogram body may have been created already because of an inline
11316 -- pragma, or because of multiple elaborations of the enclosing package
11317 -- when several instances of the subprogram appear in the main unit.
11318
11319 if Present (Corresponding_Body (Act_Decl)) then
11320 return;
11321 end if;
11322
11323 -- The subprogram being instantiated may be subject to pragma Ghost. Set
11324 -- the mode now to ensure that any nodes generated during instantiation
11325 -- are properly marked as Ghost.
11326
11327 Set_Ghost_Mode (Act_Decl_Id, Mode);
11328
11329 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11330
11331 -- Re-establish the state of information on which checks are suppressed.
11332 -- This information was set in Body_Info at the point of instantiation,
11333 -- and now we restore it so that the instance is compiled using the
11334 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11335
11336 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11337 Scope_Suppress := Body_Info.Scope_Suppress;
11338 Opt.Ada_Version := Body_Info.Version;
11339 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11340 Restore_Warnings (Body_Info.Warnings);
11341 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
11342 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
11343
11344 if No (Gen_Body_Id) then
11345
11346 -- For imported generic subprogram, no body to compile, complete
11347 -- the spec entity appropriately.
11348
11349 if Is_Imported (Gen_Unit) then
11350 Set_Is_Imported (Act_Decl_Id);
11351 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
11352 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
11353 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
11354 Set_Has_Completion (Act_Decl_Id);
11355 goto Leave;
11356
11357 -- For other cases, compile the body
11358
11359 else
11360 Load_Parent_Of_Generic
11361 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11362 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11363 end if;
11364 end if;
11365
11366 Instantiation_Node := Inst_Node;
11367
11368 if Present (Gen_Body_Id) then
11369 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11370
11371 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11372
11373 -- Either body is not present, or context is non-expanding, as
11374 -- when compiling a subunit. Mark the instance as completed, and
11375 -- diagnose a missing body when needed.
11376
11377 if Expander_Active
11378 and then Operating_Mode = Generate_Code
11379 then
11380 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
11381 end if;
11382
11383 Set_Has_Completion (Act_Decl_Id);
11384 goto Leave;
11385 end if;
11386
11387 Save_Env (Gen_Unit, Act_Decl_Id);
11388 Style_Check := False;
11389
11390 -- If the context of the instance is subject to SPARK_Mode "off", the
11391 -- annotation is missing, or the body is instantiated at a later pass
11392 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11393 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11394 -- instance.
11395
11396 if SPARK_Mode /= On
11397 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11398 then
11399 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11400 end if;
11401
11402 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11403 Create_Instantiation_Source
11404 (Inst_Node,
11405 Gen_Body_Id,
11406 S_Adjustment);
11407
11408 Act_Body :=
11409 Copy_Generic_Node
11410 (Original_Node (Gen_Body), Empty, Instantiating => True);
11411
11412 -- Create proper defining name for the body, to correspond to the one
11413 -- in the spec.
11414
11415 Act_Body_Id :=
11416 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11417
11418 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11419 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
11420
11421 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11422 Set_Has_Completion (Act_Decl_Id);
11423 Check_Generic_Actuals (Pack_Id, False);
11424
11425 -- Generate a reference to link the visible subprogram instance to
11426 -- the generic body, which for navigation purposes is the only
11427 -- available source for the instance.
11428
11429 Generate_Reference
11430 (Related_Instance (Pack_Id),
11431 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11432
11433 -- If it is a child unit, make the parent instance (which is an
11434 -- instance of the parent of the generic) visible. The parent
11435 -- instance is the prefix of the name of the generic unit.
11436
11437 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11438 and then Nkind (Gen_Id) = N_Expanded_Name
11439 then
11440 Par_Ent := Entity (Prefix (Gen_Id));
11441 Par_Vis := Is_Immediately_Visible (Par_Ent);
11442 Install_Parent (Par_Ent, In_Body => True);
11443 Parent_Installed := True;
11444
11445 elsif Is_Child_Unit (Gen_Unit) then
11446 Par_Ent := Scope (Gen_Unit);
11447 Par_Vis := Is_Immediately_Visible (Par_Ent);
11448 Install_Parent (Par_Ent, In_Body => True);
11449 Parent_Installed := True;
11450 end if;
11451
11452 -- Subprogram body is placed in the body of wrapper package,
11453 -- whose spec contains the subprogram declaration as well as
11454 -- the renaming declarations for the generic parameters.
11455
11456 Pack_Body :=
11457 Make_Package_Body (Loc,
11458 Defining_Unit_Name => New_Copy (Pack_Id),
11459 Declarations => New_List (Act_Body));
11460
11461 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11462
11463 -- If the instantiation is a library unit, then build resulting
11464 -- compilation unit nodes for the instance. The declaration of
11465 -- the enclosing package is the grandparent of the subprogram
11466 -- declaration. First replace the instantiation node as the unit
11467 -- of the corresponding compilation.
11468
11469 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11470 if Parent (Inst_Node) = Cunit (Main_Unit) then
11471 Set_Unit (Parent (Inst_Node), Inst_Node);
11472 Build_Instance_Compilation_Unit_Nodes
11473 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11474 Analyze (Inst_Node);
11475 else
11476 Set_Parent (Pack_Body, Parent (Inst_Node));
11477 Analyze (Pack_Body);
11478 end if;
11479
11480 else
11481 Insert_Before (Inst_Node, Pack_Body);
11482 Mark_Rewrite_Insertion (Pack_Body);
11483 Analyze (Pack_Body);
11484
11485 if Expander_Active then
11486 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11487 end if;
11488 end if;
11489
11490 Inherit_Context (Gen_Body, Inst_Node);
11491
11492 Restore_Private_Views (Pack_Id, False);
11493
11494 if Parent_Installed then
11495 Remove_Parent (In_Body => True);
11496
11497 -- Restore the previous visibility of the parent
11498
11499 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11500 end if;
11501
11502 Restore_Env;
11503 Restore_Warnings (Saved_Warnings);
11504
11505 -- Body not found. Error was emitted already. If there were no previous
11506 -- errors, this may be an instance whose scope is a premature instance.
11507 -- In that case we must insure that the (legal) program does raise
11508 -- program error if executed. We generate a subprogram body for this
11509 -- purpose. See DEC ac30vso.
11510
11511 -- Should not reference proprietary DEC tests in comments ???
11512
11513 elsif Serious_Errors_Detected = 0
11514 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
11515 then
11516 if Body_Optional then
11517 goto Leave;
11518
11519 elsif Ekind (Act_Decl_Id) = E_Procedure then
11520 Act_Body :=
11521 Make_Subprogram_Body (Loc,
11522 Specification =>
11523 Make_Procedure_Specification (Loc,
11524 Defining_Unit_Name =>
11525 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11526 Parameter_Specifications =>
11527 New_Copy_List
11528 (Parameter_Specifications (Parent (Act_Decl_Id)))),
11529
11530 Declarations => Empty_List,
11531 Handled_Statement_Sequence =>
11532 Make_Handled_Sequence_Of_Statements (Loc,
11533 Statements => New_List (
11534 Make_Raise_Program_Error (Loc,
11535 Reason => PE_Access_Before_Elaboration))));
11536
11537 else
11538 Ret_Expr :=
11539 Make_Raise_Program_Error (Loc,
11540 Reason => PE_Access_Before_Elaboration);
11541
11542 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
11543 Set_Analyzed (Ret_Expr);
11544
11545 Act_Body :=
11546 Make_Subprogram_Body (Loc,
11547 Specification =>
11548 Make_Function_Specification (Loc,
11549 Defining_Unit_Name =>
11550 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11551 Parameter_Specifications =>
11552 New_Copy_List
11553 (Parameter_Specifications (Parent (Act_Decl_Id))),
11554 Result_Definition =>
11555 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
11556
11557 Declarations => Empty_List,
11558 Handled_Statement_Sequence =>
11559 Make_Handled_Sequence_Of_Statements (Loc,
11560 Statements => New_List (
11561 Make_Simple_Return_Statement (Loc, Ret_Expr))));
11562 end if;
11563
11564 Pack_Body :=
11565 Make_Package_Body (Loc,
11566 Defining_Unit_Name => New_Copy (Pack_Id),
11567 Declarations => New_List (Act_Body));
11568
11569 Insert_After (Inst_Node, Pack_Body);
11570 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11571 Analyze (Pack_Body);
11572 end if;
11573
11574 Expander_Mode_Restore;
11575
11576 <<Leave>>
11577 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
11578 Style_Check := Saved_Style_Check;
11579
11580 Restore_Ghost_Mode (Mode);
11581 end Instantiate_Subprogram_Body;
11582
11583 ----------------------
11584 -- Instantiate_Type --
11585 ----------------------
11586
11587 function Instantiate_Type
11588 (Formal : Node_Id;
11589 Actual : Node_Id;
11590 Analyzed_Formal : Node_Id;
11591 Actual_Decls : List_Id) return List_Id
11592 is
11593 A_Gen_T : constant Entity_Id :=
11594 Defining_Identifier (Analyzed_Formal);
11595 Def : constant Node_Id := Formal_Type_Definition (Formal);
11596 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
11597 Act_T : Entity_Id;
11598 Ancestor : Entity_Id := Empty;
11599 Decl_Node : Node_Id;
11600 Decl_Nodes : List_Id;
11601 Loc : Source_Ptr;
11602 Subt : Entity_Id;
11603
11604 procedure Diagnose_Predicated_Actual;
11605 -- There are a number of constructs in which a discrete type with
11606 -- predicates is illegal, e.g. as an index in an array type declaration.
11607 -- If a generic type is used is such a construct in a generic package
11608 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11609 -- of the generic contract that the actual cannot have predicates.
11610
11611 procedure Validate_Array_Type_Instance;
11612 procedure Validate_Access_Subprogram_Instance;
11613 procedure Validate_Access_Type_Instance;
11614 procedure Validate_Derived_Type_Instance;
11615 procedure Validate_Derived_Interface_Type_Instance;
11616 procedure Validate_Discriminated_Formal_Type;
11617 procedure Validate_Interface_Type_Instance;
11618 procedure Validate_Private_Type_Instance;
11619 procedure Validate_Incomplete_Type_Instance;
11620 -- These procedures perform validation tests for the named case.
11621 -- Validate_Discriminated_Formal_Type is shared by formal private
11622 -- types and Ada 2012 formal incomplete types.
11623
11624 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
11625 -- Check that base types are the same and that the subtypes match
11626 -- statically. Used in several of the above.
11627
11628 ---------------------------------
11629 -- Diagnose_Predicated_Actual --
11630 ---------------------------------
11631
11632 procedure Diagnose_Predicated_Actual is
11633 begin
11634 if No_Predicate_On_Actual (A_Gen_T)
11635 and then Has_Predicates (Act_T)
11636 then
11637 Error_Msg_NE
11638 ("actual for& cannot be a type with predicate",
11639 Instantiation_Node, A_Gen_T);
11640
11641 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
11642 and then Has_Predicates (Act_T)
11643 and then not Has_Static_Predicate_Aspect (Act_T)
11644 then
11645 Error_Msg_NE
11646 ("actual for& cannot be a type with a dynamic predicate",
11647 Instantiation_Node, A_Gen_T);
11648 end if;
11649 end Diagnose_Predicated_Actual;
11650
11651 --------------------
11652 -- Subtypes_Match --
11653 --------------------
11654
11655 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
11656 T : constant Entity_Id := Get_Instance_Of (Gen_T);
11657
11658 begin
11659 -- Some detailed comments would be useful here ???
11660
11661 return ((Base_Type (T) = Act_T
11662 or else Base_Type (T) = Base_Type (Act_T))
11663 and then Subtypes_Statically_Match (T, Act_T))
11664
11665 or else (Is_Class_Wide_Type (Gen_T)
11666 and then Is_Class_Wide_Type (Act_T)
11667 and then Subtypes_Match
11668 (Get_Instance_Of (Root_Type (Gen_T)),
11669 Root_Type (Act_T)))
11670
11671 or else
11672 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
11673 E_Anonymous_Access_Type)
11674 and then Ekind (Act_T) = Ekind (Gen_T)
11675 and then Subtypes_Statically_Match
11676 (Designated_Type (Gen_T), Designated_Type (Act_T)));
11677 end Subtypes_Match;
11678
11679 -----------------------------------------
11680 -- Validate_Access_Subprogram_Instance --
11681 -----------------------------------------
11682
11683 procedure Validate_Access_Subprogram_Instance is
11684 begin
11685 if not Is_Access_Type (Act_T)
11686 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
11687 then
11688 Error_Msg_NE
11689 ("expect access type in instantiation of &", Actual, Gen_T);
11690 Abandon_Instantiation (Actual);
11691 end if;
11692
11693 -- According to AI05-288, actuals for access_to_subprograms must be
11694 -- subtype conformant with the generic formal. Previous to AI05-288
11695 -- only mode conformance was required.
11696
11697 -- This is a binding interpretation that applies to previous versions
11698 -- of the language, no need to maintain previous weaker checks.
11699
11700 Check_Subtype_Conformant
11701 (Designated_Type (Act_T),
11702 Designated_Type (A_Gen_T),
11703 Actual,
11704 Get_Inst => True);
11705
11706 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
11707 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
11708 Error_Msg_NE
11709 ("protected access type not allowed for formal &",
11710 Actual, Gen_T);
11711 end if;
11712
11713 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
11714 Error_Msg_NE
11715 ("expect protected access type for formal &",
11716 Actual, Gen_T);
11717 end if;
11718
11719 -- If the formal has a specified convention (which in most cases
11720 -- will be StdCall) verify that the actual has the same convention.
11721
11722 if Has_Convention_Pragma (A_Gen_T)
11723 and then Convention (A_Gen_T) /= Convention (Act_T)
11724 then
11725 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
11726 Error_Msg_NE
11727 ("actual for formal & must have convention %", Actual, Gen_T);
11728 end if;
11729 end Validate_Access_Subprogram_Instance;
11730
11731 -----------------------------------
11732 -- Validate_Access_Type_Instance --
11733 -----------------------------------
11734
11735 procedure Validate_Access_Type_Instance is
11736 Desig_Type : constant Entity_Id :=
11737 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
11738 Desig_Act : Entity_Id;
11739
11740 begin
11741 if not Is_Access_Type (Act_T) then
11742 Error_Msg_NE
11743 ("expect access type in instantiation of &", Actual, Gen_T);
11744 Abandon_Instantiation (Actual);
11745 end if;
11746
11747 if Is_Access_Constant (A_Gen_T) then
11748 if not Is_Access_Constant (Act_T) then
11749 Error_Msg_N
11750 ("actual type must be access-to-constant type", Actual);
11751 Abandon_Instantiation (Actual);
11752 end if;
11753 else
11754 if Is_Access_Constant (Act_T) then
11755 Error_Msg_N
11756 ("actual type must be access-to-variable type", Actual);
11757 Abandon_Instantiation (Actual);
11758
11759 elsif Ekind (A_Gen_T) = E_General_Access_Type
11760 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
11761 then
11762 Error_Msg_N -- CODEFIX
11763 ("actual must be general access type!", Actual);
11764 Error_Msg_NE -- CODEFIX
11765 ("add ALL to }!", Actual, Act_T);
11766 Abandon_Instantiation (Actual);
11767 end if;
11768 end if;
11769
11770 -- The designated subtypes, that is to say the subtypes introduced
11771 -- by an access type declaration (and not by a subtype declaration)
11772 -- must match.
11773
11774 Desig_Act := Designated_Type (Base_Type (Act_T));
11775
11776 -- The designated type may have been introduced through a limited_
11777 -- with clause, in which case retrieve the non-limited view. This
11778 -- applies to incomplete types as well as to class-wide types.
11779
11780 if From_Limited_With (Desig_Act) then
11781 Desig_Act := Available_View (Desig_Act);
11782 end if;
11783
11784 if not Subtypes_Match (Desig_Type, Desig_Act) then
11785 Error_Msg_NE
11786 ("designated type of actual does not match that of formal &",
11787 Actual, Gen_T);
11788
11789 if not Predicates_Match (Desig_Type, Desig_Act) then
11790 Error_Msg_N ("\predicates do not match", Actual);
11791 end if;
11792
11793 Abandon_Instantiation (Actual);
11794
11795 elsif Is_Access_Type (Designated_Type (Act_T))
11796 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11797 /=
11798 Is_Constrained (Designated_Type (Desig_Type))
11799 then
11800 Error_Msg_NE
11801 ("designated type of actual does not match that of formal &",
11802 Actual, Gen_T);
11803
11804 if not Predicates_Match (Desig_Type, Desig_Act) then
11805 Error_Msg_N ("\predicates do not match", Actual);
11806 end if;
11807
11808 Abandon_Instantiation (Actual);
11809 end if;
11810
11811 -- Ada 2005: null-exclusion indicators of the two types must agree
11812
11813 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11814 Error_Msg_NE
11815 ("non null exclusion of actual and formal & do not match",
11816 Actual, Gen_T);
11817 end if;
11818 end Validate_Access_Type_Instance;
11819
11820 ----------------------------------
11821 -- Validate_Array_Type_Instance --
11822 ----------------------------------
11823
11824 procedure Validate_Array_Type_Instance is
11825 I1 : Node_Id;
11826 I2 : Node_Id;
11827 T2 : Entity_Id;
11828
11829 function Formal_Dimensions return Nat;
11830 -- Count number of dimensions in array type formal
11831
11832 -----------------------
11833 -- Formal_Dimensions --
11834 -----------------------
11835
11836 function Formal_Dimensions return Nat is
11837 Num : Nat := 0;
11838 Index : Node_Id;
11839
11840 begin
11841 if Nkind (Def) = N_Constrained_Array_Definition then
11842 Index := First (Discrete_Subtype_Definitions (Def));
11843 else
11844 Index := First (Subtype_Marks (Def));
11845 end if;
11846
11847 while Present (Index) loop
11848 Num := Num + 1;
11849 Next_Index (Index);
11850 end loop;
11851
11852 return Num;
11853 end Formal_Dimensions;
11854
11855 -- Start of processing for Validate_Array_Type_Instance
11856
11857 begin
11858 if not Is_Array_Type (Act_T) then
11859 Error_Msg_NE
11860 ("expect array type in instantiation of &", Actual, Gen_T);
11861 Abandon_Instantiation (Actual);
11862
11863 elsif Nkind (Def) = N_Constrained_Array_Definition then
11864 if not (Is_Constrained (Act_T)) then
11865 Error_Msg_NE
11866 ("expect constrained array in instantiation of &",
11867 Actual, Gen_T);
11868 Abandon_Instantiation (Actual);
11869 end if;
11870
11871 else
11872 if Is_Constrained (Act_T) then
11873 Error_Msg_NE
11874 ("expect unconstrained array in instantiation of &",
11875 Actual, Gen_T);
11876 Abandon_Instantiation (Actual);
11877 end if;
11878 end if;
11879
11880 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11881 Error_Msg_NE
11882 ("dimensions of actual do not match formal &", Actual, Gen_T);
11883 Abandon_Instantiation (Actual);
11884 end if;
11885
11886 I1 := First_Index (A_Gen_T);
11887 I2 := First_Index (Act_T);
11888 for J in 1 .. Formal_Dimensions loop
11889
11890 -- If the indexes of the actual were given by a subtype_mark,
11891 -- the index was transformed into a range attribute. Retrieve
11892 -- the original type mark for checking.
11893
11894 if Is_Entity_Name (Original_Node (I2)) then
11895 T2 := Entity (Original_Node (I2));
11896 else
11897 T2 := Etype (I2);
11898 end if;
11899
11900 if not Subtypes_Match
11901 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11902 then
11903 Error_Msg_NE
11904 ("index types of actual do not match those of formal &",
11905 Actual, Gen_T);
11906 Abandon_Instantiation (Actual);
11907 end if;
11908
11909 Next_Index (I1);
11910 Next_Index (I2);
11911 end loop;
11912
11913 -- Check matching subtypes. Note that there are complex visibility
11914 -- issues when the generic is a child unit and some aspect of the
11915 -- generic type is declared in a parent unit of the generic. We do
11916 -- the test to handle this special case only after a direct check
11917 -- for static matching has failed. The case where both the component
11918 -- type and the array type are separate formals, and the component
11919 -- type is a private view may also require special checking in
11920 -- Subtypes_Match.
11921
11922 if Subtypes_Match
11923 (Component_Type (A_Gen_T), Component_Type (Act_T))
11924 or else
11925 Subtypes_Match
11926 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11927 Component_Type (Act_T))
11928 then
11929 null;
11930 else
11931 Error_Msg_NE
11932 ("component subtype of actual does not match that of formal &",
11933 Actual, Gen_T);
11934 Abandon_Instantiation (Actual);
11935 end if;
11936
11937 if Has_Aliased_Components (A_Gen_T)
11938 and then not Has_Aliased_Components (Act_T)
11939 then
11940 Error_Msg_NE
11941 ("actual must have aliased components to match formal type &",
11942 Actual, Gen_T);
11943 end if;
11944 end Validate_Array_Type_Instance;
11945
11946 -----------------------------------------------
11947 -- Validate_Derived_Interface_Type_Instance --
11948 -----------------------------------------------
11949
11950 procedure Validate_Derived_Interface_Type_Instance is
11951 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11952 Elmt : Elmt_Id;
11953
11954 begin
11955 -- First apply interface instance checks
11956
11957 Validate_Interface_Type_Instance;
11958
11959 -- Verify that immediate parent interface is an ancestor of
11960 -- the actual.
11961
11962 if Present (Par)
11963 and then not Interface_Present_In_Ancestor (Act_T, Par)
11964 then
11965 Error_Msg_NE
11966 ("interface actual must include progenitor&", Actual, Par);
11967 end if;
11968
11969 -- Now verify that the actual includes all other ancestors of
11970 -- the formal.
11971
11972 Elmt := First_Elmt (Interfaces (A_Gen_T));
11973 while Present (Elmt) loop
11974 if not Interface_Present_In_Ancestor
11975 (Act_T, Get_Instance_Of (Node (Elmt)))
11976 then
11977 Error_Msg_NE
11978 ("interface actual must include progenitor&",
11979 Actual, Node (Elmt));
11980 end if;
11981
11982 Next_Elmt (Elmt);
11983 end loop;
11984 end Validate_Derived_Interface_Type_Instance;
11985
11986 ------------------------------------
11987 -- Validate_Derived_Type_Instance --
11988 ------------------------------------
11989
11990 procedure Validate_Derived_Type_Instance is
11991 Actual_Discr : Entity_Id;
11992 Ancestor_Discr : Entity_Id;
11993
11994 begin
11995 -- If the parent type in the generic declaration is itself a previous
11996 -- formal type, then it is local to the generic and absent from the
11997 -- analyzed generic definition. In that case the ancestor is the
11998 -- instance of the formal (which must have been instantiated
11999 -- previously), unless the ancestor is itself a formal derived type.
12000 -- In this latter case (which is the subject of Corrigendum 8652/0038
12001 -- (AI-202) the ancestor of the formals is the ancestor of its
12002 -- parent. Otherwise, the analyzed generic carries the parent type.
12003 -- If the parent type is defined in a previous formal package, then
12004 -- the scope of that formal package is that of the generic type
12005 -- itself, and it has already been mapped into the corresponding type
12006 -- in the actual package.
12007
12008 -- Common case: parent type defined outside of the generic
12009
12010 if Is_Entity_Name (Subtype_Mark (Def))
12011 and then Present (Entity (Subtype_Mark (Def)))
12012 then
12013 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
12014
12015 -- Check whether parent is defined in a previous formal package
12016
12017 elsif
12018 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
12019 then
12020 Ancestor :=
12021 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
12022
12023 -- The type may be a local derivation, or a type extension of a
12024 -- previous formal, or of a formal of a parent package.
12025
12026 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
12027 or else
12028 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
12029 then
12030 -- Check whether the parent is another derived formal type in the
12031 -- same generic unit.
12032
12033 if Etype (A_Gen_T) /= A_Gen_T
12034 and then Is_Generic_Type (Etype (A_Gen_T))
12035 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
12036 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
12037 then
12038 -- Locate ancestor of parent from the subtype declaration
12039 -- created for the actual.
12040
12041 declare
12042 Decl : Node_Id;
12043
12044 begin
12045 Decl := First (Actual_Decls);
12046 while Present (Decl) loop
12047 if Nkind (Decl) = N_Subtype_Declaration
12048 and then Chars (Defining_Identifier (Decl)) =
12049 Chars (Etype (A_Gen_T))
12050 then
12051 Ancestor := Generic_Parent_Type (Decl);
12052 exit;
12053 else
12054 Next (Decl);
12055 end if;
12056 end loop;
12057 end;
12058
12059 pragma Assert (Present (Ancestor));
12060
12061 -- The ancestor itself may be a previous formal that has been
12062 -- instantiated.
12063
12064 Ancestor := Get_Instance_Of (Ancestor);
12065
12066 else
12067 Ancestor :=
12068 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
12069 end if;
12070
12071 -- Check whether parent is a previous formal of the current generic
12072
12073 elsif Is_Derived_Type (A_Gen_T)
12074 and then Is_Generic_Type (Etype (A_Gen_T))
12075 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
12076 then
12077 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
12078
12079 -- An unusual case: the actual is a type declared in a parent unit,
12080 -- but is not a formal type so there is no instance_of for it.
12081 -- Retrieve it by analyzing the record extension.
12082
12083 elsif Is_Child_Unit (Scope (A_Gen_T))
12084 and then In_Open_Scopes (Scope (Act_T))
12085 and then Is_Generic_Instance (Scope (Act_T))
12086 then
12087 Analyze (Subtype_Mark (Def));
12088 Ancestor := Entity (Subtype_Mark (Def));
12089
12090 else
12091 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
12092 end if;
12093
12094 -- If the formal derived type has pragma Preelaborable_Initialization
12095 -- then the actual type must have preelaborable initialization.
12096
12097 if Known_To_Have_Preelab_Init (A_Gen_T)
12098 and then not Has_Preelaborable_Initialization (Act_T)
12099 then
12100 Error_Msg_NE
12101 ("actual for & must have preelaborable initialization",
12102 Actual, Gen_T);
12103 end if;
12104
12105 -- Ada 2005 (AI-251)
12106
12107 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
12108 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
12109 Error_Msg_NE
12110 ("(Ada 2005) expected type implementing & in instantiation",
12111 Actual, Ancestor);
12112 end if;
12113
12114 -- Finally verify that the (instance of) the ancestor is an ancestor
12115 -- of the actual.
12116
12117 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
12118 Error_Msg_NE
12119 ("expect type derived from & in instantiation",
12120 Actual, First_Subtype (Ancestor));
12121 Abandon_Instantiation (Actual);
12122 end if;
12123
12124 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
12125 -- that the formal type declaration has been rewritten as a private
12126 -- extension.
12127
12128 if Ada_Version >= Ada_2005
12129 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
12130 and then Synchronized_Present (Parent (A_Gen_T))
12131 then
12132 -- The actual must be a synchronized tagged type
12133
12134 if not Is_Tagged_Type (Act_T) then
12135 Error_Msg_N
12136 ("actual of synchronized type must be tagged", Actual);
12137 Abandon_Instantiation (Actual);
12138
12139 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
12140 and then Nkind (Type_Definition (Parent (Act_T))) =
12141 N_Derived_Type_Definition
12142 and then not Synchronized_Present
12143 (Type_Definition (Parent (Act_T)))
12144 then
12145 Error_Msg_N
12146 ("actual of synchronized type must be synchronized", Actual);
12147 Abandon_Instantiation (Actual);
12148 end if;
12149 end if;
12150
12151 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
12152 -- removes the second instance of the phrase "or allow pass by copy".
12153
12154 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
12155 Error_Msg_N
12156 ("cannot have atomic actual type for non-atomic formal type",
12157 Actual);
12158
12159 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
12160 Error_Msg_N
12161 ("cannot have volatile actual type for non-volatile formal type",
12162 Actual);
12163 end if;
12164
12165 -- It should not be necessary to check for unknown discriminants on
12166 -- Formal, but for some reason Has_Unknown_Discriminants is false for
12167 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
12168 -- needs fixing. ???
12169
12170 if Is_Definite_Subtype (A_Gen_T)
12171 and then not Unknown_Discriminants_Present (Formal)
12172 and then not Is_Definite_Subtype (Act_T)
12173 then
12174 Error_Msg_N ("actual subtype must be constrained", Actual);
12175 Abandon_Instantiation (Actual);
12176 end if;
12177
12178 if not Unknown_Discriminants_Present (Formal) then
12179 if Is_Constrained (Ancestor) then
12180 if not Is_Constrained (Act_T) then
12181 Error_Msg_N ("actual subtype must be constrained", Actual);
12182 Abandon_Instantiation (Actual);
12183 end if;
12184
12185 -- Ancestor is unconstrained, Check if generic formal and actual
12186 -- agree on constrainedness. The check only applies to array types
12187 -- and discriminated types.
12188
12189 elsif Is_Constrained (Act_T) then
12190 if Ekind (Ancestor) = E_Access_Type
12191 or else (not Is_Constrained (A_Gen_T)
12192 and then Is_Composite_Type (A_Gen_T))
12193 then
12194 Error_Msg_N ("actual subtype must be unconstrained", Actual);
12195 Abandon_Instantiation (Actual);
12196 end if;
12197
12198 -- A class-wide type is only allowed if the formal has unknown
12199 -- discriminants.
12200
12201 elsif Is_Class_Wide_Type (Act_T)
12202 and then not Has_Unknown_Discriminants (Ancestor)
12203 then
12204 Error_Msg_NE
12205 ("actual for & cannot be a class-wide type", Actual, Gen_T);
12206 Abandon_Instantiation (Actual);
12207
12208 -- Otherwise, the formal and actual must have the same number
12209 -- of discriminants and each discriminant of the actual must
12210 -- correspond to a discriminant of the formal.
12211
12212 elsif Has_Discriminants (Act_T)
12213 and then not Has_Unknown_Discriminants (Act_T)
12214 and then Has_Discriminants (Ancestor)
12215 then
12216 Actual_Discr := First_Discriminant (Act_T);
12217 Ancestor_Discr := First_Discriminant (Ancestor);
12218 while Present (Actual_Discr)
12219 and then Present (Ancestor_Discr)
12220 loop
12221 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
12222 No (Corresponding_Discriminant (Actual_Discr))
12223 then
12224 Error_Msg_NE
12225 ("discriminant & does not correspond "
12226 & "to ancestor discriminant", Actual, Actual_Discr);
12227 Abandon_Instantiation (Actual);
12228 end if;
12229
12230 Next_Discriminant (Actual_Discr);
12231 Next_Discriminant (Ancestor_Discr);
12232 end loop;
12233
12234 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
12235 Error_Msg_NE
12236 ("actual for & must have same number of discriminants",
12237 Actual, Gen_T);
12238 Abandon_Instantiation (Actual);
12239 end if;
12240
12241 -- This case should be caught by the earlier check for
12242 -- constrainedness, but the check here is added for completeness.
12243
12244 elsif Has_Discriminants (Act_T)
12245 and then not Has_Unknown_Discriminants (Act_T)
12246 then
12247 Error_Msg_NE
12248 ("actual for & must not have discriminants", Actual, Gen_T);
12249 Abandon_Instantiation (Actual);
12250
12251 elsif Has_Discriminants (Ancestor) then
12252 Error_Msg_NE
12253 ("actual for & must have known discriminants", Actual, Gen_T);
12254 Abandon_Instantiation (Actual);
12255 end if;
12256
12257 if not Subtypes_Statically_Compatible
12258 (Act_T, Ancestor, Formal_Derived_Matching => True)
12259 then
12260 Error_Msg_N
12261 ("constraint on actual is incompatible with formal", Actual);
12262 Abandon_Instantiation (Actual);
12263 end if;
12264 end if;
12265
12266 -- If the formal and actual types are abstract, check that there
12267 -- are no abstract primitives of the actual type that correspond to
12268 -- nonabstract primitives of the formal type (second sentence of
12269 -- RM95 3.9.3(9)).
12270
12271 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
12272 Check_Abstract_Primitives : declare
12273 Gen_Prims : constant Elist_Id :=
12274 Primitive_Operations (A_Gen_T);
12275 Gen_Elmt : Elmt_Id;
12276 Gen_Subp : Entity_Id;
12277 Anc_Subp : Entity_Id;
12278 Anc_Formal : Entity_Id;
12279 Anc_F_Type : Entity_Id;
12280
12281 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
12282 Act_Elmt : Elmt_Id;
12283 Act_Subp : Entity_Id;
12284 Act_Formal : Entity_Id;
12285 Act_F_Type : Entity_Id;
12286
12287 Subprograms_Correspond : Boolean;
12288
12289 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
12290 -- Returns true if T2 is derived directly or indirectly from
12291 -- T1, including derivations from interfaces. T1 and T2 are
12292 -- required to be specific tagged base types.
12293
12294 ------------------------
12295 -- Is_Tagged_Ancestor --
12296 ------------------------
12297
12298 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
12299 is
12300 Intfc_Elmt : Elmt_Id;
12301
12302 begin
12303 -- The predicate is satisfied if the types are the same
12304
12305 if T1 = T2 then
12306 return True;
12307
12308 -- If we've reached the top of the derivation chain then
12309 -- we know that T1 is not an ancestor of T2.
12310
12311 elsif Etype (T2) = T2 then
12312 return False;
12313
12314 -- Proceed to check T2's immediate parent
12315
12316 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
12317 return True;
12318
12319 -- Finally, check to see if T1 is an ancestor of any of T2's
12320 -- progenitors.
12321
12322 else
12323 Intfc_Elmt := First_Elmt (Interfaces (T2));
12324 while Present (Intfc_Elmt) loop
12325 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
12326 return True;
12327 end if;
12328
12329 Next_Elmt (Intfc_Elmt);
12330 end loop;
12331 end if;
12332
12333 return False;
12334 end Is_Tagged_Ancestor;
12335
12336 -- Start of processing for Check_Abstract_Primitives
12337
12338 begin
12339 -- Loop over all of the formal derived type's primitives
12340
12341 Gen_Elmt := First_Elmt (Gen_Prims);
12342 while Present (Gen_Elmt) loop
12343 Gen_Subp := Node (Gen_Elmt);
12344
12345 -- If the primitive of the formal is not abstract, then
12346 -- determine whether there is a corresponding primitive of
12347 -- the actual type that's abstract.
12348
12349 if not Is_Abstract_Subprogram (Gen_Subp) then
12350 Act_Elmt := First_Elmt (Act_Prims);
12351 while Present (Act_Elmt) loop
12352 Act_Subp := Node (Act_Elmt);
12353
12354 -- If we find an abstract primitive of the actual,
12355 -- then we need to test whether it corresponds to the
12356 -- subprogram from which the generic formal primitive
12357 -- is inherited.
12358
12359 if Is_Abstract_Subprogram (Act_Subp) then
12360 Anc_Subp := Alias (Gen_Subp);
12361
12362 -- Test whether we have a corresponding primitive
12363 -- by comparing names, kinds, formal types, and
12364 -- result types.
12365
12366 if Chars (Anc_Subp) = Chars (Act_Subp)
12367 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
12368 then
12369 Anc_Formal := First_Formal (Anc_Subp);
12370 Act_Formal := First_Formal (Act_Subp);
12371 while Present (Anc_Formal)
12372 and then Present (Act_Formal)
12373 loop
12374 Anc_F_Type := Etype (Anc_Formal);
12375 Act_F_Type := Etype (Act_Formal);
12376
12377 if Ekind (Anc_F_Type) =
12378 E_Anonymous_Access_Type
12379 then
12380 Anc_F_Type := Designated_Type (Anc_F_Type);
12381
12382 if Ekind (Act_F_Type) =
12383 E_Anonymous_Access_Type
12384 then
12385 Act_F_Type :=
12386 Designated_Type (Act_F_Type);
12387 else
12388 exit;
12389 end if;
12390
12391 elsif
12392 Ekind (Act_F_Type) = E_Anonymous_Access_Type
12393 then
12394 exit;
12395 end if;
12396
12397 Anc_F_Type := Base_Type (Anc_F_Type);
12398 Act_F_Type := Base_Type (Act_F_Type);
12399
12400 -- If the formal is controlling, then the
12401 -- the type of the actual primitive's formal
12402 -- must be derived directly or indirectly
12403 -- from the type of the ancestor primitive's
12404 -- formal.
12405
12406 if Is_Controlling_Formal (Anc_Formal) then
12407 if not Is_Tagged_Ancestor
12408 (Anc_F_Type, Act_F_Type)
12409 then
12410 exit;
12411 end if;
12412
12413 -- Otherwise the types of the formals must
12414 -- be the same.
12415
12416 elsif Anc_F_Type /= Act_F_Type then
12417 exit;
12418 end if;
12419
12420 Next_Entity (Anc_Formal);
12421 Next_Entity (Act_Formal);
12422 end loop;
12423
12424 -- If we traversed through all of the formals
12425 -- then so far the subprograms correspond, so
12426 -- now check that any result types correspond.
12427
12428 if No (Anc_Formal) and then No (Act_Formal) then
12429 Subprograms_Correspond := True;
12430
12431 if Ekind (Act_Subp) = E_Function then
12432 Anc_F_Type := Etype (Anc_Subp);
12433 Act_F_Type := Etype (Act_Subp);
12434
12435 if Ekind (Anc_F_Type) =
12436 E_Anonymous_Access_Type
12437 then
12438 Anc_F_Type :=
12439 Designated_Type (Anc_F_Type);
12440
12441 if Ekind (Act_F_Type) =
12442 E_Anonymous_Access_Type
12443 then
12444 Act_F_Type :=
12445 Designated_Type (Act_F_Type);
12446 else
12447 Subprograms_Correspond := False;
12448 end if;
12449
12450 elsif
12451 Ekind (Act_F_Type)
12452 = E_Anonymous_Access_Type
12453 then
12454 Subprograms_Correspond := False;
12455 end if;
12456
12457 Anc_F_Type := Base_Type (Anc_F_Type);
12458 Act_F_Type := Base_Type (Act_F_Type);
12459
12460 -- Now either the result types must be
12461 -- the same or, if the result type is
12462 -- controlling, the result type of the
12463 -- actual primitive must descend from the
12464 -- result type of the ancestor primitive.
12465
12466 if Subprograms_Correspond
12467 and then Anc_F_Type /= Act_F_Type
12468 and then
12469 Has_Controlling_Result (Anc_Subp)
12470 and then not Is_Tagged_Ancestor
12471 (Anc_F_Type, Act_F_Type)
12472 then
12473 Subprograms_Correspond := False;
12474 end if;
12475 end if;
12476
12477 -- Found a matching subprogram belonging to
12478 -- formal ancestor type, so actual subprogram
12479 -- corresponds and this violates 3.9.3(9).
12480
12481 if Subprograms_Correspond then
12482 Error_Msg_NE
12483 ("abstract subprogram & overrides "
12484 & "nonabstract subprogram of ancestor",
12485 Actual, Act_Subp);
12486 end if;
12487 end if;
12488 end if;
12489 end if;
12490
12491 Next_Elmt (Act_Elmt);
12492 end loop;
12493 end if;
12494
12495 Next_Elmt (Gen_Elmt);
12496 end loop;
12497 end Check_Abstract_Primitives;
12498 end if;
12499
12500 -- Verify that limitedness matches. If parent is a limited
12501 -- interface then the generic formal is not unless declared
12502 -- explicitly so. If not declared limited, the actual cannot be
12503 -- limited (see AI05-0087).
12504
12505 -- Even though this AI is a binding interpretation, we enable the
12506 -- check only in Ada 2012 mode, because this improper construct
12507 -- shows up in user code and in existing B-tests.
12508
12509 if Is_Limited_Type (Act_T)
12510 and then not Is_Limited_Type (A_Gen_T)
12511 and then Ada_Version >= Ada_2012
12512 then
12513 if In_Instance then
12514 null;
12515 else
12516 Error_Msg_NE
12517 ("actual for non-limited & cannot be a limited type",
12518 Actual, Gen_T);
12519 Explain_Limited_Type (Act_T, Actual);
12520 Abandon_Instantiation (Actual);
12521 end if;
12522 end if;
12523 end Validate_Derived_Type_Instance;
12524
12525 ----------------------------------------
12526 -- Validate_Discriminated_Formal_Type --
12527 ----------------------------------------
12528
12529 procedure Validate_Discriminated_Formal_Type is
12530 Formal_Discr : Entity_Id;
12531 Actual_Discr : Entity_Id;
12532 Formal_Subt : Entity_Id;
12533
12534 begin
12535 if Has_Discriminants (A_Gen_T) then
12536 if not Has_Discriminants (Act_T) then
12537 Error_Msg_NE
12538 ("actual for & must have discriminants", Actual, Gen_T);
12539 Abandon_Instantiation (Actual);
12540
12541 elsif Is_Constrained (Act_T) then
12542 Error_Msg_NE
12543 ("actual for & must be unconstrained", Actual, Gen_T);
12544 Abandon_Instantiation (Actual);
12545
12546 else
12547 Formal_Discr := First_Discriminant (A_Gen_T);
12548 Actual_Discr := First_Discriminant (Act_T);
12549 while Formal_Discr /= Empty loop
12550 if Actual_Discr = Empty then
12551 Error_Msg_NE
12552 ("discriminants on actual do not match formal",
12553 Actual, Gen_T);
12554 Abandon_Instantiation (Actual);
12555 end if;
12556
12557 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
12558
12559 -- Access discriminants match if designated types do
12560
12561 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
12562 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
12563 E_Anonymous_Access_Type
12564 and then
12565 Get_Instance_Of
12566 (Designated_Type (Base_Type (Formal_Subt))) =
12567 Designated_Type (Base_Type (Etype (Actual_Discr)))
12568 then
12569 null;
12570
12571 elsif Base_Type (Formal_Subt) /=
12572 Base_Type (Etype (Actual_Discr))
12573 then
12574 Error_Msg_NE
12575 ("types of actual discriminants must match formal",
12576 Actual, Gen_T);
12577 Abandon_Instantiation (Actual);
12578
12579 elsif not Subtypes_Statically_Match
12580 (Formal_Subt, Etype (Actual_Discr))
12581 and then Ada_Version >= Ada_95
12582 then
12583 Error_Msg_NE
12584 ("subtypes of actual discriminants must match formal",
12585 Actual, Gen_T);
12586 Abandon_Instantiation (Actual);
12587 end if;
12588
12589 Next_Discriminant (Formal_Discr);
12590 Next_Discriminant (Actual_Discr);
12591 end loop;
12592
12593 if Actual_Discr /= Empty then
12594 Error_Msg_NE
12595 ("discriminants on actual do not match formal",
12596 Actual, Gen_T);
12597 Abandon_Instantiation (Actual);
12598 end if;
12599 end if;
12600 end if;
12601 end Validate_Discriminated_Formal_Type;
12602
12603 ---------------------------------------
12604 -- Validate_Incomplete_Type_Instance --
12605 ---------------------------------------
12606
12607 procedure Validate_Incomplete_Type_Instance is
12608 begin
12609 if not Is_Tagged_Type (Act_T)
12610 and then Is_Tagged_Type (A_Gen_T)
12611 then
12612 Error_Msg_NE
12613 ("actual for & must be a tagged type", Actual, Gen_T);
12614 end if;
12615
12616 Validate_Discriminated_Formal_Type;
12617 end Validate_Incomplete_Type_Instance;
12618
12619 --------------------------------------
12620 -- Validate_Interface_Type_Instance --
12621 --------------------------------------
12622
12623 procedure Validate_Interface_Type_Instance is
12624 begin
12625 if not Is_Interface (Act_T) then
12626 Error_Msg_NE
12627 ("actual for formal interface type must be an interface",
12628 Actual, Gen_T);
12629
12630 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
12631 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
12632 or else Is_Protected_Interface (A_Gen_T) /=
12633 Is_Protected_Interface (Act_T)
12634 or else Is_Synchronized_Interface (A_Gen_T) /=
12635 Is_Synchronized_Interface (Act_T)
12636 then
12637 Error_Msg_NE
12638 ("actual for interface& does not match (RM 12.5.5(4))",
12639 Actual, Gen_T);
12640 end if;
12641 end Validate_Interface_Type_Instance;
12642
12643 ------------------------------------
12644 -- Validate_Private_Type_Instance --
12645 ------------------------------------
12646
12647 procedure Validate_Private_Type_Instance is
12648 begin
12649 if Is_Limited_Type (Act_T)
12650 and then not Is_Limited_Type (A_Gen_T)
12651 then
12652 if In_Instance then
12653 null;
12654 else
12655 Error_Msg_NE
12656 ("actual for non-limited & cannot be a limited type", Actual,
12657 Gen_T);
12658 Explain_Limited_Type (Act_T, Actual);
12659 Abandon_Instantiation (Actual);
12660 end if;
12661
12662 elsif Known_To_Have_Preelab_Init (A_Gen_T)
12663 and then not Has_Preelaborable_Initialization (Act_T)
12664 then
12665 Error_Msg_NE
12666 ("actual for & must have preelaborable initialization", Actual,
12667 Gen_T);
12668
12669 elsif not Is_Definite_Subtype (Act_T)
12670 and then Is_Definite_Subtype (A_Gen_T)
12671 and then Ada_Version >= Ada_95
12672 then
12673 Error_Msg_NE
12674 ("actual for & must be a definite subtype", Actual, Gen_T);
12675
12676 elsif not Is_Tagged_Type (Act_T)
12677 and then Is_Tagged_Type (A_Gen_T)
12678 then
12679 Error_Msg_NE
12680 ("actual for & must be a tagged type", Actual, Gen_T);
12681 end if;
12682
12683 Validate_Discriminated_Formal_Type;
12684 Ancestor := Gen_T;
12685 end Validate_Private_Type_Instance;
12686
12687 -- Start of processing for Instantiate_Type
12688
12689 begin
12690 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
12691 Error_Msg_N ("duplicate instantiation of generic type", Actual);
12692 return New_List (Error);
12693
12694 elsif not Is_Entity_Name (Actual)
12695 or else not Is_Type (Entity (Actual))
12696 then
12697 Error_Msg_NE
12698 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
12699 Abandon_Instantiation (Actual);
12700
12701 else
12702 Act_T := Entity (Actual);
12703
12704 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
12705 -- as a generic actual parameter if the corresponding formal type
12706 -- does not have a known_discriminant_part, or is a formal derived
12707 -- type that is an Unchecked_Union type.
12708
12709 if Is_Unchecked_Union (Base_Type (Act_T)) then
12710 if not Has_Discriminants (A_Gen_T)
12711 or else (Is_Derived_Type (A_Gen_T)
12712 and then Is_Unchecked_Union (A_Gen_T))
12713 then
12714 null;
12715 else
12716 Error_Msg_N ("unchecked union cannot be the actual for a "
12717 & "discriminated formal type", Act_T);
12718
12719 end if;
12720 end if;
12721
12722 -- Deal with fixed/floating restrictions
12723
12724 if Is_Floating_Point_Type (Act_T) then
12725 Check_Restriction (No_Floating_Point, Actual);
12726 elsif Is_Fixed_Point_Type (Act_T) then
12727 Check_Restriction (No_Fixed_Point, Actual);
12728 end if;
12729
12730 -- Deal with error of using incomplete type as generic actual.
12731 -- This includes limited views of a type, even if the non-limited
12732 -- view may be available.
12733
12734 if Ekind (Act_T) = E_Incomplete_Type
12735 or else (Is_Class_Wide_Type (Act_T)
12736 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
12737 then
12738 -- If the formal is an incomplete type, the actual can be
12739 -- incomplete as well.
12740
12741 if Ekind (A_Gen_T) = E_Incomplete_Type then
12742 null;
12743
12744 elsif Is_Class_Wide_Type (Act_T)
12745 or else No (Full_View (Act_T))
12746 then
12747 Error_Msg_N ("premature use of incomplete type", Actual);
12748 Abandon_Instantiation (Actual);
12749 else
12750 Act_T := Full_View (Act_T);
12751 Set_Entity (Actual, Act_T);
12752
12753 if Has_Private_Component (Act_T) then
12754 Error_Msg_N
12755 ("premature use of type with private component", Actual);
12756 end if;
12757 end if;
12758
12759 -- Deal with error of premature use of private type as generic actual
12760
12761 elsif Is_Private_Type (Act_T)
12762 and then Is_Private_Type (Base_Type (Act_T))
12763 and then not Is_Generic_Type (Act_T)
12764 and then not Is_Derived_Type (Act_T)
12765 and then No (Full_View (Root_Type (Act_T)))
12766 then
12767 -- If the formal is an incomplete type, the actual can be
12768 -- private or incomplete as well.
12769
12770 if Ekind (A_Gen_T) = E_Incomplete_Type then
12771 null;
12772 else
12773 Error_Msg_N ("premature use of private type", Actual);
12774 end if;
12775
12776 elsif Has_Private_Component (Act_T) then
12777 Error_Msg_N
12778 ("premature use of type with private component", Actual);
12779 end if;
12780
12781 Set_Instance_Of (A_Gen_T, Act_T);
12782
12783 -- If the type is generic, the class-wide type may also be used
12784
12785 if Is_Tagged_Type (A_Gen_T)
12786 and then Is_Tagged_Type (Act_T)
12787 and then not Is_Class_Wide_Type (A_Gen_T)
12788 then
12789 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
12790 Class_Wide_Type (Act_T));
12791 end if;
12792
12793 if not Is_Abstract_Type (A_Gen_T)
12794 and then Is_Abstract_Type (Act_T)
12795 then
12796 Error_Msg_N
12797 ("actual of non-abstract formal cannot be abstract", Actual);
12798 end if;
12799
12800 -- A generic scalar type is a first subtype for which we generate
12801 -- an anonymous base type. Indicate that the instance of this base
12802 -- is the base type of the actual.
12803
12804 if Is_Scalar_Type (A_Gen_T) then
12805 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12806 end if;
12807 end if;
12808
12809 if Error_Posted (Act_T) then
12810 null;
12811 else
12812 case Nkind (Def) is
12813 when N_Formal_Private_Type_Definition =>
12814 Validate_Private_Type_Instance;
12815
12816 when N_Formal_Incomplete_Type_Definition =>
12817 Validate_Incomplete_Type_Instance;
12818
12819 when N_Formal_Derived_Type_Definition =>
12820 Validate_Derived_Type_Instance;
12821
12822 when N_Formal_Discrete_Type_Definition =>
12823 if not Is_Discrete_Type (Act_T) then
12824 Error_Msg_NE
12825 ("expect discrete type in instantiation of&",
12826 Actual, Gen_T);
12827 Abandon_Instantiation (Actual);
12828 end if;
12829
12830 Diagnose_Predicated_Actual;
12831
12832 when N_Formal_Signed_Integer_Type_Definition =>
12833 if not Is_Signed_Integer_Type (Act_T) then
12834 Error_Msg_NE
12835 ("expect signed integer type in instantiation of&",
12836 Actual, Gen_T);
12837 Abandon_Instantiation (Actual);
12838 end if;
12839
12840 Diagnose_Predicated_Actual;
12841
12842 when N_Formal_Modular_Type_Definition =>
12843 if not Is_Modular_Integer_Type (Act_T) then
12844 Error_Msg_NE
12845 ("expect modular type in instantiation of &",
12846 Actual, Gen_T);
12847 Abandon_Instantiation (Actual);
12848 end if;
12849
12850 Diagnose_Predicated_Actual;
12851
12852 when N_Formal_Floating_Point_Definition =>
12853 if not Is_Floating_Point_Type (Act_T) then
12854 Error_Msg_NE
12855 ("expect float type in instantiation of &", Actual, Gen_T);
12856 Abandon_Instantiation (Actual);
12857 end if;
12858
12859 when N_Formal_Ordinary_Fixed_Point_Definition =>
12860 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12861 Error_Msg_NE
12862 ("expect ordinary fixed point type in instantiation of &",
12863 Actual, Gen_T);
12864 Abandon_Instantiation (Actual);
12865 end if;
12866
12867 when N_Formal_Decimal_Fixed_Point_Definition =>
12868 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12869 Error_Msg_NE
12870 ("expect decimal type in instantiation of &",
12871 Actual, Gen_T);
12872 Abandon_Instantiation (Actual);
12873 end if;
12874
12875 when N_Array_Type_Definition =>
12876 Validate_Array_Type_Instance;
12877
12878 when N_Access_To_Object_Definition =>
12879 Validate_Access_Type_Instance;
12880
12881 when N_Access_Function_Definition
12882 | N_Access_Procedure_Definition
12883 =>
12884 Validate_Access_Subprogram_Instance;
12885
12886 when N_Record_Definition =>
12887 Validate_Interface_Type_Instance;
12888
12889 when N_Derived_Type_Definition =>
12890 Validate_Derived_Interface_Type_Instance;
12891
12892 when others =>
12893 raise Program_Error;
12894 end case;
12895 end if;
12896
12897 Subt := New_Copy (Gen_T);
12898
12899 -- Use adjusted sloc of subtype name as the location for other nodes in
12900 -- the subtype declaration.
12901
12902 Loc := Sloc (Subt);
12903
12904 Decl_Node :=
12905 Make_Subtype_Declaration (Loc,
12906 Defining_Identifier => Subt,
12907 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12908
12909 if Is_Private_Type (Act_T) then
12910 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12911
12912 elsif Is_Access_Type (Act_T)
12913 and then Is_Private_Type (Designated_Type (Act_T))
12914 then
12915 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12916 end if;
12917
12918 -- In Ada 2012 the actual may be a limited view. Indicate that
12919 -- the local subtype must be treated as such.
12920
12921 if From_Limited_With (Act_T) then
12922 Set_Ekind (Subt, E_Incomplete_Subtype);
12923 Set_From_Limited_With (Subt);
12924 end if;
12925
12926 Decl_Nodes := New_List (Decl_Node);
12927
12928 -- Flag actual derived types so their elaboration produces the
12929 -- appropriate renamings for the primitive operations of the ancestor.
12930 -- Flag actual for formal private types as well, to determine whether
12931 -- operations in the private part may override inherited operations.
12932 -- If the formal has an interface list, the ancestor is not the
12933 -- parent, but the analyzed formal that includes the interface
12934 -- operations of all its progenitors.
12935
12936 -- Same treatment for formal private types, so we can check whether the
12937 -- type is tagged limited when validating derivations in the private
12938 -- part. (See AI05-096).
12939
12940 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12941 if Present (Interface_List (Def)) then
12942 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12943 else
12944 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12945 end if;
12946
12947 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
12948 N_Formal_Incomplete_Type_Definition)
12949 then
12950 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12951 end if;
12952
12953 -- If the actual is a synchronized type that implements an interface,
12954 -- the primitive operations are attached to the corresponding record,
12955 -- and we have to treat it as an additional generic actual, so that its
12956 -- primitive operations become visible in the instance. The task or
12957 -- protected type itself does not carry primitive operations.
12958
12959 if Is_Concurrent_Type (Act_T)
12960 and then Is_Tagged_Type (Act_T)
12961 and then Present (Corresponding_Record_Type (Act_T))
12962 and then Present (Ancestor)
12963 and then Is_Interface (Ancestor)
12964 then
12965 declare
12966 Corr_Rec : constant Entity_Id :=
12967 Corresponding_Record_Type (Act_T);
12968 New_Corr : Entity_Id;
12969 Corr_Decl : Node_Id;
12970
12971 begin
12972 New_Corr := Make_Temporary (Loc, 'S');
12973 Corr_Decl :=
12974 Make_Subtype_Declaration (Loc,
12975 Defining_Identifier => New_Corr,
12976 Subtype_Indication =>
12977 New_Occurrence_Of (Corr_Rec, Loc));
12978 Append_To (Decl_Nodes, Corr_Decl);
12979
12980 if Ekind (Act_T) = E_Task_Type then
12981 Set_Ekind (Subt, E_Task_Subtype);
12982 else
12983 Set_Ekind (Subt, E_Protected_Subtype);
12984 end if;
12985
12986 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12987 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12988 Set_Generic_Parent_Type (Decl_Node, Empty);
12989 end;
12990 end if;
12991
12992 -- For a floating-point type, capture dimension info if any, because
12993 -- the generated subtype declaration does not come from source and
12994 -- will not process dimensions.
12995
12996 if Is_Floating_Point_Type (Act_T) then
12997 Copy_Dimensions (Act_T, Subt);
12998 end if;
12999
13000 return Decl_Nodes;
13001 end Instantiate_Type;
13002
13003 ---------------------
13004 -- Is_In_Main_Unit --
13005 ---------------------
13006
13007 function Is_In_Main_Unit (N : Node_Id) return Boolean is
13008 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
13009 Current_Unit : Node_Id;
13010
13011 begin
13012 if Unum = Main_Unit then
13013 return True;
13014
13015 -- If the current unit is a subunit then it is either the main unit or
13016 -- is being compiled as part of the main unit.
13017
13018 elsif Nkind (N) = N_Compilation_Unit then
13019 return Nkind (Unit (N)) = N_Subunit;
13020 end if;
13021
13022 Current_Unit := Parent (N);
13023 while Present (Current_Unit)
13024 and then Nkind (Current_Unit) /= N_Compilation_Unit
13025 loop
13026 Current_Unit := Parent (Current_Unit);
13027 end loop;
13028
13029 -- The instantiation node is in the main unit, or else the current node
13030 -- (perhaps as the result of nested instantiations) is in the main unit,
13031 -- or in the declaration of the main unit, which in this last case must
13032 -- be a body.
13033
13034 return
13035 Current_Unit = Cunit (Main_Unit)
13036 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
13037 or else (Present (Current_Unit)
13038 and then Present (Library_Unit (Current_Unit))
13039 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
13040 end Is_In_Main_Unit;
13041
13042 ----------------------------
13043 -- Load_Parent_Of_Generic --
13044 ----------------------------
13045
13046 procedure Load_Parent_Of_Generic
13047 (N : Node_Id;
13048 Spec : Node_Id;
13049 Body_Optional : Boolean := False)
13050 is
13051 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
13052 Saved_Style_Check : constant Boolean := Style_Check;
13053 Saved_Warnings : constant Warning_Record := Save_Warnings;
13054 True_Parent : Node_Id;
13055 Inst_Node : Node_Id;
13056 OK : Boolean;
13057 Previous_Instances : constant Elist_Id := New_Elmt_List;
13058
13059 procedure Collect_Previous_Instances (Decls : List_Id);
13060 -- Collect all instantiations in the given list of declarations, that
13061 -- precede the generic that we need to load. If the bodies of these
13062 -- instantiations are available, we must analyze them, to ensure that
13063 -- the public symbols generated are the same when the unit is compiled
13064 -- to generate code, and when it is compiled in the context of a unit
13065 -- that needs a particular nested instance. This process is applied to
13066 -- both package and subprogram instances.
13067
13068 --------------------------------
13069 -- Collect_Previous_Instances --
13070 --------------------------------
13071
13072 procedure Collect_Previous_Instances (Decls : List_Id) is
13073 Decl : Node_Id;
13074
13075 begin
13076 Decl := First (Decls);
13077 while Present (Decl) loop
13078 if Sloc (Decl) >= Sloc (Inst_Node) then
13079 return;
13080
13081 -- If Decl is an instantiation, then record it as requiring
13082 -- instantiation of the corresponding body, except if it is an
13083 -- abbreviated instantiation generated internally for conformance
13084 -- checking purposes only for the case of a formal package
13085 -- declared without a box (see Instantiate_Formal_Package). Such
13086 -- an instantiation does not generate any code (the actual code
13087 -- comes from actual) and thus does not need to be analyzed here.
13088 -- If the instantiation appears with a generic package body it is
13089 -- not analyzed here either.
13090
13091 elsif Nkind (Decl) = N_Package_Instantiation
13092 and then not Is_Internal (Defining_Entity (Decl))
13093 then
13094 Append_Elmt (Decl, Previous_Instances);
13095
13096 -- For a subprogram instantiation, omit instantiations intrinsic
13097 -- operations (Unchecked_Conversions, etc.) that have no bodies.
13098
13099 elsif Nkind_In (Decl, N_Function_Instantiation,
13100 N_Procedure_Instantiation)
13101 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
13102 then
13103 Append_Elmt (Decl, Previous_Instances);
13104
13105 elsif Nkind (Decl) = N_Package_Declaration then
13106 Collect_Previous_Instances
13107 (Visible_Declarations (Specification (Decl)));
13108 Collect_Previous_Instances
13109 (Private_Declarations (Specification (Decl)));
13110
13111 -- Previous non-generic bodies may contain instances as well
13112
13113 elsif Nkind (Decl) = N_Package_Body
13114 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
13115 then
13116 Collect_Previous_Instances (Declarations (Decl));
13117
13118 elsif Nkind (Decl) = N_Subprogram_Body
13119 and then not Acts_As_Spec (Decl)
13120 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
13121 then
13122 Collect_Previous_Instances (Declarations (Decl));
13123 end if;
13124
13125 Next (Decl);
13126 end loop;
13127 end Collect_Previous_Instances;
13128
13129 -- Start of processing for Load_Parent_Of_Generic
13130
13131 begin
13132 if not In_Same_Source_Unit (N, Spec)
13133 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
13134 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
13135 and then not Is_In_Main_Unit (Spec))
13136 then
13137 -- Find body of parent of spec, and analyze it. A special case arises
13138 -- when the parent is an instantiation, that is to say when we are
13139 -- currently instantiating a nested generic. In that case, there is
13140 -- no separate file for the body of the enclosing instance. Instead,
13141 -- the enclosing body must be instantiated as if it were a pending
13142 -- instantiation, in order to produce the body for the nested generic
13143 -- we require now. Note that in that case the generic may be defined
13144 -- in a package body, the instance defined in the same package body,
13145 -- and the original enclosing body may not be in the main unit.
13146
13147 Inst_Node := Empty;
13148
13149 True_Parent := Parent (Spec);
13150 while Present (True_Parent)
13151 and then Nkind (True_Parent) /= N_Compilation_Unit
13152 loop
13153 if Nkind (True_Parent) = N_Package_Declaration
13154 and then
13155 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
13156 then
13157 -- Parent is a compilation unit that is an instantiation.
13158 -- Instantiation node has been replaced with package decl.
13159
13160 Inst_Node := Original_Node (True_Parent);
13161 exit;
13162
13163 elsif Nkind (True_Parent) = N_Package_Declaration
13164 and then Present (Generic_Parent (Specification (True_Parent)))
13165 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13166 then
13167 -- Parent is an instantiation within another specification.
13168 -- Declaration for instance has been inserted before original
13169 -- instantiation node. A direct link would be preferable?
13170
13171 Inst_Node := Next (True_Parent);
13172 while Present (Inst_Node)
13173 and then Nkind (Inst_Node) /= N_Package_Instantiation
13174 loop
13175 Next (Inst_Node);
13176 end loop;
13177
13178 -- If the instance appears within a generic, and the generic
13179 -- unit is defined within a formal package of the enclosing
13180 -- generic, there is no generic body available, and none
13181 -- needed. A more precise test should be used ???
13182
13183 if No (Inst_Node) then
13184 return;
13185 end if;
13186
13187 exit;
13188
13189 else
13190 True_Parent := Parent (True_Parent);
13191 end if;
13192 end loop;
13193
13194 -- Case where we are currently instantiating a nested generic
13195
13196 if Present (Inst_Node) then
13197 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
13198
13199 -- Instantiation node and declaration of instantiated package
13200 -- were exchanged when only the declaration was needed.
13201 -- Restore instantiation node before proceeding with body.
13202
13203 Set_Unit (Parent (True_Parent), Inst_Node);
13204 end if;
13205
13206 -- Now complete instantiation of enclosing body, if it appears in
13207 -- some other unit. If it appears in the current unit, the body
13208 -- will have been instantiated already.
13209
13210 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
13211
13212 -- We need to determine the expander mode to instantiate the
13213 -- enclosing body. Because the generic body we need may use
13214 -- global entities declared in the enclosing package (including
13215 -- aggregates) it is in general necessary to compile this body
13216 -- with expansion enabled, except if we are within a generic
13217 -- package, in which case the usual generic rule applies.
13218
13219 declare
13220 Exp_Status : Boolean := True;
13221 Scop : Entity_Id;
13222
13223 begin
13224 -- Loop through scopes looking for generic package
13225
13226 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
13227 while Present (Scop)
13228 and then Scop /= Standard_Standard
13229 loop
13230 if Ekind (Scop) = E_Generic_Package then
13231 Exp_Status := False;
13232 exit;
13233 end if;
13234
13235 Scop := Scope (Scop);
13236 end loop;
13237
13238 -- Collect previous instantiations in the unit that contains
13239 -- the desired generic.
13240
13241 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13242 and then not Body_Optional
13243 then
13244 declare
13245 Decl : Elmt_Id;
13246 Info : Pending_Body_Info;
13247 Par : Node_Id;
13248
13249 begin
13250 Par := Parent (Inst_Node);
13251 while Present (Par) loop
13252 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
13253 Par := Parent (Par);
13254 end loop;
13255
13256 pragma Assert (Present (Par));
13257
13258 if Nkind (Par) = N_Package_Body then
13259 Collect_Previous_Instances (Declarations (Par));
13260
13261 elsif Nkind (Par) = N_Package_Declaration then
13262 Collect_Previous_Instances
13263 (Visible_Declarations (Specification (Par)));
13264 Collect_Previous_Instances
13265 (Private_Declarations (Specification (Par)));
13266
13267 else
13268 -- Enclosing unit is a subprogram body. In this
13269 -- case all instance bodies are processed in order
13270 -- and there is no need to collect them separately.
13271
13272 null;
13273 end if;
13274
13275 Decl := First_Elmt (Previous_Instances);
13276 while Present (Decl) loop
13277 Info :=
13278 (Inst_Node => Node (Decl),
13279 Act_Decl =>
13280 Instance_Spec (Node (Decl)),
13281 Expander_Status => Exp_Status,
13282 Current_Sem_Unit =>
13283 Get_Code_Unit (Sloc (Node (Decl))),
13284 Scope_Suppress => Scope_Suppress,
13285 Local_Suppress_Stack_Top =>
13286 Local_Suppress_Stack_Top,
13287 Version => Ada_Version,
13288 Version_Pragma => Ada_Version_Pragma,
13289 Warnings => Save_Warnings,
13290 SPARK_Mode => SPARK_Mode,
13291 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
13292
13293 -- Package instance
13294
13295 if
13296 Nkind (Node (Decl)) = N_Package_Instantiation
13297 then
13298 Instantiate_Package_Body
13299 (Info, Body_Optional => True);
13300
13301 -- Subprogram instance
13302
13303 else
13304 -- The instance_spec is in the wrapper package,
13305 -- usually followed by its local renaming
13306 -- declaration. See Build_Subprogram_Renaming
13307 -- for details. If the instance carries aspects,
13308 -- these result in the corresponding pragmas,
13309 -- inserted after the subprogram declaration.
13310 -- They must be skipped as well when retrieving
13311 -- the desired spec. A direct link would be
13312 -- more robust ???
13313
13314 declare
13315 Decl : Node_Id :=
13316 (Last (Visible_Declarations
13317 (Specification (Info.Act_Decl))));
13318 begin
13319 while Nkind_In (Decl,
13320 N_Subprogram_Renaming_Declaration, N_Pragma)
13321 loop
13322 Decl := Prev (Decl);
13323 end loop;
13324
13325 Info.Act_Decl := Decl;
13326 end;
13327
13328 Instantiate_Subprogram_Body
13329 (Info, Body_Optional => True);
13330 end if;
13331
13332 Next_Elmt (Decl);
13333 end loop;
13334 end;
13335 end if;
13336
13337 Instantiate_Package_Body
13338 (Body_Info =>
13339 ((Inst_Node => Inst_Node,
13340 Act_Decl => True_Parent,
13341 Expander_Status => Exp_Status,
13342 Current_Sem_Unit => Get_Code_Unit
13343 (Sloc (Inst_Node)),
13344 Scope_Suppress => Scope_Suppress,
13345 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
13346 Version => Ada_Version,
13347 Version_Pragma => Ada_Version_Pragma,
13348 Warnings => Save_Warnings,
13349 SPARK_Mode => SPARK_Mode,
13350 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
13351 Body_Optional => Body_Optional);
13352 end;
13353 end if;
13354
13355 -- Case where we are not instantiating a nested generic
13356
13357 else
13358 Opt.Style_Check := False;
13359 Expander_Mode_Save_And_Set (True);
13360 Load_Needed_Body (Comp_Unit, OK);
13361 Opt.Style_Check := Saved_Style_Check;
13362 Restore_Warnings (Saved_Warnings);
13363 Expander_Mode_Restore;
13364
13365 if not OK
13366 and then Unit_Requires_Body (Defining_Entity (Spec))
13367 and then not Body_Optional
13368 then
13369 declare
13370 Bname : constant Unit_Name_Type :=
13371 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
13372
13373 begin
13374 -- In CodePeer mode, the missing body may make the analysis
13375 -- incomplete, but we do not treat it as fatal.
13376
13377 if CodePeer_Mode then
13378 return;
13379
13380 else
13381 Error_Msg_Unit_1 := Bname;
13382 Error_Msg_N ("this instantiation requires$!", N);
13383 Error_Msg_File_1 :=
13384 Get_File_Name (Bname, Subunit => False);
13385 Error_Msg_N ("\but file{ was not found!", N);
13386 raise Unrecoverable_Error;
13387 end if;
13388 end;
13389 end if;
13390 end if;
13391 end if;
13392
13393 -- If loading parent of the generic caused an instantiation circularity,
13394 -- we abandon compilation at this point, because otherwise in some cases
13395 -- we get into trouble with infinite recursions after this point.
13396
13397 if Circularity_Detected then
13398 raise Unrecoverable_Error;
13399 end if;
13400 end Load_Parent_Of_Generic;
13401
13402 ---------------------------------
13403 -- Map_Formal_Package_Entities --
13404 ---------------------------------
13405
13406 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
13407 E1 : Entity_Id;
13408 E2 : Entity_Id;
13409
13410 begin
13411 Set_Instance_Of (Form, Act);
13412
13413 -- Traverse formal and actual package to map the corresponding entities.
13414 -- We skip over internal entities that may be generated during semantic
13415 -- analysis, and find the matching entities by name, given that they
13416 -- must appear in the same order.
13417
13418 E1 := First_Entity (Form);
13419 E2 := First_Entity (Act);
13420 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
13421 -- Could this test be a single condition??? Seems like it could, and
13422 -- isn't FPE (Form) a constant anyway???
13423
13424 if not Is_Internal (E1)
13425 and then Present (Parent (E1))
13426 and then not Is_Class_Wide_Type (E1)
13427 and then not Is_Internal_Name (Chars (E1))
13428 then
13429 while Present (E2) and then Chars (E2) /= Chars (E1) loop
13430 Next_Entity (E2);
13431 end loop;
13432
13433 if No (E2) then
13434 exit;
13435 else
13436 Set_Instance_Of (E1, E2);
13437
13438 if Is_Type (E1) and then Is_Tagged_Type (E2) then
13439 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
13440 end if;
13441
13442 if Is_Constrained (E1) then
13443 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
13444 end if;
13445
13446 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
13447 Map_Formal_Package_Entities (E1, E2);
13448 end if;
13449 end if;
13450 end if;
13451
13452 Next_Entity (E1);
13453 end loop;
13454 end Map_Formal_Package_Entities;
13455
13456 -----------------------
13457 -- Move_Freeze_Nodes --
13458 -----------------------
13459
13460 procedure Move_Freeze_Nodes
13461 (Out_Of : Entity_Id;
13462 After : Node_Id;
13463 L : List_Id)
13464 is
13465 Decl : Node_Id;
13466 Next_Decl : Node_Id;
13467 Next_Node : Node_Id := After;
13468 Spec : Node_Id;
13469
13470 function Is_Outer_Type (T : Entity_Id) return Boolean;
13471 -- Check whether entity is declared in a scope external to that of the
13472 -- generic unit.
13473
13474 -------------------
13475 -- Is_Outer_Type --
13476 -------------------
13477
13478 function Is_Outer_Type (T : Entity_Id) return Boolean is
13479 Scop : Entity_Id := Scope (T);
13480
13481 begin
13482 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
13483 return True;
13484
13485 else
13486 while Scop /= Standard_Standard loop
13487 if Scop = Out_Of then
13488 return False;
13489 else
13490 Scop := Scope (Scop);
13491 end if;
13492 end loop;
13493
13494 return True;
13495 end if;
13496 end Is_Outer_Type;
13497
13498 -- Start of processing for Move_Freeze_Nodes
13499
13500 begin
13501 if No (L) then
13502 return;
13503 end if;
13504
13505 -- First remove the freeze nodes that may appear before all other
13506 -- declarations.
13507
13508 Decl := First (L);
13509 while Present (Decl)
13510 and then Nkind (Decl) = N_Freeze_Entity
13511 and then Is_Outer_Type (Entity (Decl))
13512 loop
13513 Decl := Remove_Head (L);
13514 Insert_After (Next_Node, Decl);
13515 Set_Analyzed (Decl, False);
13516 Next_Node := Decl;
13517 Decl := First (L);
13518 end loop;
13519
13520 -- Next scan the list of declarations and remove each freeze node that
13521 -- appears ahead of the current node.
13522
13523 while Present (Decl) loop
13524 while Present (Next (Decl))
13525 and then Nkind (Next (Decl)) = N_Freeze_Entity
13526 and then Is_Outer_Type (Entity (Next (Decl)))
13527 loop
13528 Next_Decl := Remove_Next (Decl);
13529 Insert_After (Next_Node, Next_Decl);
13530 Set_Analyzed (Next_Decl, False);
13531 Next_Node := Next_Decl;
13532 end loop;
13533
13534 -- If the declaration is a nested package or concurrent type, then
13535 -- recurse. Nested generic packages will have been processed from the
13536 -- inside out.
13537
13538 case Nkind (Decl) is
13539 when N_Package_Declaration =>
13540 Spec := Specification (Decl);
13541
13542 when N_Task_Type_Declaration =>
13543 Spec := Task_Definition (Decl);
13544
13545 when N_Protected_Type_Declaration =>
13546 Spec := Protected_Definition (Decl);
13547
13548 when others =>
13549 Spec := Empty;
13550 end case;
13551
13552 if Present (Spec) then
13553 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
13554 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
13555 end if;
13556
13557 Next (Decl);
13558 end loop;
13559 end Move_Freeze_Nodes;
13560
13561 ----------------
13562 -- Next_Assoc --
13563 ----------------
13564
13565 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
13566 begin
13567 return Generic_Renamings.Table (E).Next_In_HTable;
13568 end Next_Assoc;
13569
13570 ------------------------
13571 -- Preanalyze_Actuals --
13572 ------------------------
13573
13574 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
13575 Assoc : Node_Id;
13576 Act : Node_Id;
13577 Errs : constant Nat := Serious_Errors_Detected;
13578
13579 Cur : Entity_Id := Empty;
13580 -- Current homograph of the instance name
13581
13582 Vis : Boolean;
13583 -- Saved visibility status of the current homograph
13584
13585 begin
13586 Assoc := First (Generic_Associations (N));
13587
13588 -- If the instance is a child unit, its name may hide an outer homonym,
13589 -- so make it invisible to perform name resolution on the actuals.
13590
13591 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
13592 and then Present
13593 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
13594 then
13595 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
13596
13597 if Is_Compilation_Unit (Cur) then
13598 Vis := Is_Immediately_Visible (Cur);
13599 Set_Is_Immediately_Visible (Cur, False);
13600 else
13601 Cur := Empty;
13602 end if;
13603 end if;
13604
13605 while Present (Assoc) loop
13606 if Nkind (Assoc) /= N_Others_Choice then
13607 Act := Explicit_Generic_Actual_Parameter (Assoc);
13608
13609 -- Within a nested instantiation, a defaulted actual is an empty
13610 -- association, so nothing to analyze. If the subprogram actual
13611 -- is an attribute, analyze prefix only, because actual is not a
13612 -- complete attribute reference.
13613
13614 -- If actual is an allocator, analyze expression only. The full
13615 -- analysis can generate code, and if instance is a compilation
13616 -- unit we have to wait until the package instance is installed
13617 -- to have a proper place to insert this code.
13618
13619 -- String literals may be operators, but at this point we do not
13620 -- know whether the actual is a formal subprogram or a string.
13621
13622 if No (Act) then
13623 null;
13624
13625 elsif Nkind (Act) = N_Attribute_Reference then
13626 Analyze (Prefix (Act));
13627
13628 elsif Nkind (Act) = N_Explicit_Dereference then
13629 Analyze (Prefix (Act));
13630
13631 elsif Nkind (Act) = N_Allocator then
13632 declare
13633 Expr : constant Node_Id := Expression (Act);
13634
13635 begin
13636 if Nkind (Expr) = N_Subtype_Indication then
13637 Analyze (Subtype_Mark (Expr));
13638
13639 -- Analyze separately each discriminant constraint, when
13640 -- given with a named association.
13641
13642 declare
13643 Constr : Node_Id;
13644
13645 begin
13646 Constr := First (Constraints (Constraint (Expr)));
13647 while Present (Constr) loop
13648 if Nkind (Constr) = N_Discriminant_Association then
13649 Analyze (Expression (Constr));
13650 else
13651 Analyze (Constr);
13652 end if;
13653
13654 Next (Constr);
13655 end loop;
13656 end;
13657
13658 else
13659 Analyze (Expr);
13660 end if;
13661 end;
13662
13663 elsif Nkind (Act) /= N_Operator_Symbol then
13664 Analyze (Act);
13665
13666 -- Within a package instance, mark actuals that are limited
13667 -- views, so their use can be moved to the body of the
13668 -- enclosing unit.
13669
13670 if Is_Entity_Name (Act)
13671 and then Is_Type (Entity (Act))
13672 and then From_Limited_With (Entity (Act))
13673 and then Present (Inst)
13674 then
13675 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
13676 end if;
13677 end if;
13678
13679 if Errs /= Serious_Errors_Detected then
13680
13681 -- Do a minimal analysis of the generic, to prevent spurious
13682 -- warnings complaining about the generic being unreferenced,
13683 -- before abandoning the instantiation.
13684
13685 Analyze (Name (N));
13686
13687 if Is_Entity_Name (Name (N))
13688 and then Etype (Name (N)) /= Any_Type
13689 then
13690 Generate_Reference (Entity (Name (N)), Name (N));
13691 Set_Is_Instantiated (Entity (Name (N)));
13692 end if;
13693
13694 if Present (Cur) then
13695
13696 -- For the case of a child instance hiding an outer homonym,
13697 -- provide additional warning which might explain the error.
13698
13699 Set_Is_Immediately_Visible (Cur, Vis);
13700 Error_Msg_NE
13701 ("& hides outer unit with the same name??",
13702 N, Defining_Unit_Name (N));
13703 end if;
13704
13705 Abandon_Instantiation (Act);
13706 end if;
13707 end if;
13708
13709 Next (Assoc);
13710 end loop;
13711
13712 if Present (Cur) then
13713 Set_Is_Immediately_Visible (Cur, Vis);
13714 end if;
13715 end Preanalyze_Actuals;
13716
13717 -------------------
13718 -- Remove_Parent --
13719 -------------------
13720
13721 procedure Remove_Parent (In_Body : Boolean := False) is
13722 S : Entity_Id := Current_Scope;
13723 -- S is the scope containing the instantiation just completed. The scope
13724 -- stack contains the parent instances of the instantiation, followed by
13725 -- the original S.
13726
13727 Cur_P : Entity_Id;
13728 E : Entity_Id;
13729 P : Entity_Id;
13730 Hidden : Elmt_Id;
13731
13732 begin
13733 -- After child instantiation is complete, remove from scope stack the
13734 -- extra copy of the current scope, and then remove parent instances.
13735
13736 if not In_Body then
13737 Pop_Scope;
13738
13739 while Current_Scope /= S loop
13740 P := Current_Scope;
13741 End_Package_Scope (Current_Scope);
13742
13743 if In_Open_Scopes (P) then
13744 E := First_Entity (P);
13745 while Present (E) loop
13746 Set_Is_Immediately_Visible (E, True);
13747 Next_Entity (E);
13748 end loop;
13749
13750 -- If instantiation is declared in a block, it is the enclosing
13751 -- scope that might be a parent instance. Note that only one
13752 -- block can be involved, because the parent instances have
13753 -- been installed within it.
13754
13755 if Ekind (P) = E_Block then
13756 Cur_P := Scope (P);
13757 else
13758 Cur_P := P;
13759 end if;
13760
13761 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
13762 -- We are within an instance of some sibling. Retain
13763 -- visibility of parent, for proper subsequent cleanup, and
13764 -- reinstall private declarations as well.
13765
13766 Set_In_Private_Part (P);
13767 Install_Private_Declarations (P);
13768 end if;
13769
13770 -- If the ultimate parent is a top-level unit recorded in
13771 -- Instance_Parent_Unit, then reset its visibility to what it was
13772 -- before instantiation. (It's not clear what the purpose is of
13773 -- testing whether Scope (P) is In_Open_Scopes, but that test was
13774 -- present before the ultimate parent test was added.???)
13775
13776 elsif not In_Open_Scopes (Scope (P))
13777 or else (P = Instance_Parent_Unit
13778 and then not Parent_Unit_Visible)
13779 then
13780 Set_Is_Immediately_Visible (P, False);
13781
13782 -- If the current scope is itself an instantiation of a generic
13783 -- nested within P, and we are in the private part of body of this
13784 -- instantiation, restore the full views of P, that were removed
13785 -- in End_Package_Scope above. This obscure case can occur when a
13786 -- subunit of a generic contains an instance of a child unit of
13787 -- its generic parent unit.
13788
13789 elsif S = Current_Scope and then Is_Generic_Instance (S) then
13790 declare
13791 Par : constant Entity_Id :=
13792 Generic_Parent (Package_Specification (S));
13793 begin
13794 if Present (Par)
13795 and then P = Scope (Par)
13796 and then (In_Package_Body (S) or else In_Private_Part (S))
13797 then
13798 Set_In_Private_Part (P);
13799 Install_Private_Declarations (P);
13800 end if;
13801 end;
13802 end if;
13803 end loop;
13804
13805 -- Reset visibility of entities in the enclosing scope
13806
13807 Set_Is_Hidden_Open_Scope (Current_Scope, False);
13808
13809 Hidden := First_Elmt (Hidden_Entities);
13810 while Present (Hidden) loop
13811 Set_Is_Immediately_Visible (Node (Hidden), True);
13812 Next_Elmt (Hidden);
13813 end loop;
13814
13815 else
13816 -- Each body is analyzed separately, and there is no context that
13817 -- needs preserving from one body instance to the next, so remove all
13818 -- parent scopes that have been installed.
13819
13820 while Present (S) loop
13821 End_Package_Scope (S);
13822 Set_Is_Immediately_Visible (S, False);
13823 S := Current_Scope;
13824 exit when S = Standard_Standard;
13825 end loop;
13826 end if;
13827 end Remove_Parent;
13828
13829 -----------------
13830 -- Restore_Env --
13831 -----------------
13832
13833 procedure Restore_Env is
13834 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13835
13836 begin
13837 if No (Current_Instantiated_Parent.Act_Id) then
13838 -- Restore environment after subprogram inlining
13839
13840 Restore_Private_Views (Empty);
13841 end if;
13842
13843 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13844 Exchanged_Views := Saved.Exchanged_Views;
13845 Hidden_Entities := Saved.Hidden_Entities;
13846 Current_Sem_Unit := Saved.Current_Sem_Unit;
13847 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13848 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13849
13850 Restore_Opt_Config_Switches (Saved.Switches);
13851
13852 Instance_Envs.Decrement_Last;
13853 end Restore_Env;
13854
13855 ---------------------------
13856 -- Restore_Private_Views --
13857 ---------------------------
13858
13859 procedure Restore_Private_Views
13860 (Pack_Id : Entity_Id;
13861 Is_Package : Boolean := True)
13862 is
13863 M : Elmt_Id;
13864 E : Entity_Id;
13865 Typ : Entity_Id;
13866 Dep_Elmt : Elmt_Id;
13867 Dep_Typ : Node_Id;
13868
13869 procedure Restore_Nested_Formal (Formal : Entity_Id);
13870 -- Hide the generic formals of formal packages declared with box which
13871 -- were reachable in the current instantiation.
13872
13873 ---------------------------
13874 -- Restore_Nested_Formal --
13875 ---------------------------
13876
13877 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13878 Ent : Entity_Id;
13879
13880 begin
13881 if Present (Renamed_Object (Formal))
13882 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13883 then
13884 return;
13885
13886 elsif Present (Associated_Formal_Package (Formal)) then
13887 Ent := First_Entity (Formal);
13888 while Present (Ent) loop
13889 exit when Ekind (Ent) = E_Package
13890 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13891
13892 Set_Is_Hidden (Ent);
13893 Set_Is_Potentially_Use_Visible (Ent, False);
13894
13895 -- If package, then recurse
13896
13897 if Ekind (Ent) = E_Package then
13898 Restore_Nested_Formal (Ent);
13899 end if;
13900
13901 Next_Entity (Ent);
13902 end loop;
13903 end if;
13904 end Restore_Nested_Formal;
13905
13906 -- Start of processing for Restore_Private_Views
13907
13908 begin
13909 M := First_Elmt (Exchanged_Views);
13910 while Present (M) loop
13911 Typ := Node (M);
13912
13913 -- Subtypes of types whose views have been exchanged, and that are
13914 -- defined within the instance, were not on the Private_Dependents
13915 -- list on entry to the instance, so they have to be exchanged
13916 -- explicitly now, in order to remain consistent with the view of the
13917 -- parent type.
13918
13919 if Ekind_In (Typ, E_Private_Type,
13920 E_Limited_Private_Type,
13921 E_Record_Type_With_Private)
13922 then
13923 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13924 while Present (Dep_Elmt) loop
13925 Dep_Typ := Node (Dep_Elmt);
13926
13927 if Scope (Dep_Typ) = Pack_Id
13928 and then Present (Full_View (Dep_Typ))
13929 then
13930 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13931 Exchange_Declarations (Dep_Typ);
13932 end if;
13933
13934 Next_Elmt (Dep_Elmt);
13935 end loop;
13936 end if;
13937
13938 Exchange_Declarations (Node (M));
13939 Next_Elmt (M);
13940 end loop;
13941
13942 if No (Pack_Id) then
13943 return;
13944 end if;
13945
13946 -- Make the generic formal parameters private, and make the formal types
13947 -- into subtypes of the actuals again.
13948
13949 E := First_Entity (Pack_Id);
13950 while Present (E) loop
13951 Set_Is_Hidden (E, True);
13952
13953 if Is_Type (E)
13954 and then Nkind (Parent (E)) = N_Subtype_Declaration
13955 then
13956 -- If the actual for E is itself a generic actual type from
13957 -- an enclosing instance, E is still a generic actual type
13958 -- outside of the current instance. This matter when resolving
13959 -- an overloaded call that may be ambiguous in the enclosing
13960 -- instance, when two of its actuals coincide.
13961
13962 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13963 and then Is_Generic_Actual_Type
13964 (Entity (Subtype_Indication (Parent (E))))
13965 then
13966 null;
13967 else
13968 Set_Is_Generic_Actual_Type (E, False);
13969 end if;
13970
13971 -- An unusual case of aliasing: the actual may also be directly
13972 -- visible in the generic, and be private there, while it is fully
13973 -- visible in the context of the instance. The internal subtype
13974 -- is private in the instance but has full visibility like its
13975 -- parent in the enclosing scope. This enforces the invariant that
13976 -- the privacy status of all private dependents of a type coincide
13977 -- with that of the parent type. This can only happen when a
13978 -- generic child unit is instantiated within a sibling.
13979
13980 if Is_Private_Type (E)
13981 and then not Is_Private_Type (Etype (E))
13982 then
13983 Exchange_Declarations (E);
13984 end if;
13985
13986 elsif Ekind (E) = E_Package then
13987
13988 -- The end of the renaming list is the renaming of the generic
13989 -- package itself. If the instance is a subprogram, all entities
13990 -- in the corresponding package are renamings. If this entity is
13991 -- a formal package, make its own formals private as well. The
13992 -- actual in this case is itself the renaming of an instantiation.
13993 -- If the entity is not a package renaming, it is the entity
13994 -- created to validate formal package actuals: ignore it.
13995
13996 -- If the actual is itself a formal package for the enclosing
13997 -- generic, or the actual for such a formal package, it remains
13998 -- visible on exit from the instance, and therefore nothing needs
13999 -- to be done either, except to keep it accessible.
14000
14001 if Is_Package and then Renamed_Object (E) = Pack_Id then
14002 exit;
14003
14004 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
14005 null;
14006
14007 elsif
14008 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
14009 then
14010 Set_Is_Hidden (E, False);
14011
14012 else
14013 declare
14014 Act_P : constant Entity_Id := Renamed_Object (E);
14015 Id : Entity_Id;
14016
14017 begin
14018 Id := First_Entity (Act_P);
14019 while Present (Id)
14020 and then Id /= First_Private_Entity (Act_P)
14021 loop
14022 exit when Ekind (Id) = E_Package
14023 and then Renamed_Object (Id) = Act_P;
14024
14025 Set_Is_Hidden (Id, True);
14026 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
14027
14028 if Ekind (Id) = E_Package then
14029 Restore_Nested_Formal (Id);
14030 end if;
14031
14032 Next_Entity (Id);
14033 end loop;
14034 end;
14035 end if;
14036 end if;
14037
14038 Next_Entity (E);
14039 end loop;
14040 end Restore_Private_Views;
14041
14042 --------------
14043 -- Save_Env --
14044 --------------
14045
14046 procedure Save_Env
14047 (Gen_Unit : Entity_Id;
14048 Act_Unit : Entity_Id)
14049 is
14050 begin
14051 Init_Env;
14052 Set_Instance_Env (Gen_Unit, Act_Unit);
14053 end Save_Env;
14054
14055 ----------------------------
14056 -- Save_Global_References --
14057 ----------------------------
14058
14059 procedure Save_Global_References (Templ : Node_Id) is
14060
14061 -- ??? it is horrible to use global variables in highly recursive code
14062
14063 E : Entity_Id;
14064 -- The entity of the current associated node
14065
14066 Gen_Scope : Entity_Id;
14067 -- The scope of the generic for which references are being saved
14068
14069 N2 : Node_Id;
14070 -- The current associated node
14071
14072 function Is_Global (E : Entity_Id) return Boolean;
14073 -- Check whether entity is defined outside of generic unit. Examine the
14074 -- scope of an entity, and the scope of the scope, etc, until we find
14075 -- either Standard, in which case the entity is global, or the generic
14076 -- unit itself, which indicates that the entity is local. If the entity
14077 -- is the generic unit itself, as in the case of a recursive call, or
14078 -- the enclosing generic unit, if different from the current scope, then
14079 -- it is local as well, because it will be replaced at the point of
14080 -- instantiation. On the other hand, if it is a reference to a child
14081 -- unit of a common ancestor, which appears in an instantiation, it is
14082 -- global because it is used to denote a specific compilation unit at
14083 -- the time the instantiations will be analyzed.
14084
14085 procedure Qualify_Universal_Operands
14086 (Op : Node_Id;
14087 Func_Call : Node_Id);
14088 -- Op denotes a binary or unary operator in generic template Templ. Node
14089 -- Func_Call is the function call alternative of the operator within the
14090 -- the analyzed copy of the template. Change each operand which yields a
14091 -- universal type by wrapping it into a qualified expression
14092 --
14093 -- Actual_Typ'(Operand)
14094 --
14095 -- where Actual_Typ is the type of corresponding actual parameter of
14096 -- Operand in Func_Call.
14097
14098 procedure Reset_Entity (N : Node_Id);
14099 -- Save semantic information on global entity so that it is not resolved
14100 -- again at instantiation time.
14101
14102 procedure Save_Entity_Descendants (N : Node_Id);
14103 -- Apply Save_Global_References to the two syntactic descendants of
14104 -- non-terminal nodes that carry an Associated_Node and are processed
14105 -- through Reset_Entity. Once the global entity (if any) has been
14106 -- captured together with its type, only two syntactic descendants need
14107 -- to be traversed to complete the processing of the tree rooted at N.
14108 -- This applies to Selected_Components, Expanded_Names, and to Operator
14109 -- nodes. N can also be a character literal, identifier, or operator
14110 -- symbol node, but the call has no effect in these cases.
14111
14112 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
14113 -- Default actuals in nested instances must be handled specially
14114 -- because there is no link to them from the original tree. When an
14115 -- actual subprogram is given by a default, we add an explicit generic
14116 -- association for it in the instantiation node. When we save the
14117 -- global references on the name of the instance, we recover the list
14118 -- of generic associations, and add an explicit one to the original
14119 -- generic tree, through which a global actual can be preserved.
14120 -- Similarly, if a child unit is instantiated within a sibling, in the
14121 -- context of the parent, we must preserve the identifier of the parent
14122 -- so that it can be properly resolved in a subsequent instantiation.
14123
14124 procedure Save_Global_Descendant (D : Union_Id);
14125 -- Apply Save_References recursively to the descendants of node D
14126
14127 procedure Save_References (N : Node_Id);
14128 -- This is the recursive procedure that does the work, once the
14129 -- enclosing generic scope has been established.
14130
14131 ---------------
14132 -- Is_Global --
14133 ---------------
14134
14135 function Is_Global (E : Entity_Id) return Boolean is
14136 Se : Entity_Id;
14137
14138 function Is_Instance_Node (Decl : Node_Id) return Boolean;
14139 -- Determine whether the parent node of a reference to a child unit
14140 -- denotes an instantiation or a formal package, in which case the
14141 -- reference to the child unit is global, even if it appears within
14142 -- the current scope (e.g. when the instance appears within the body
14143 -- of an ancestor).
14144
14145 ----------------------
14146 -- Is_Instance_Node --
14147 ----------------------
14148
14149 function Is_Instance_Node (Decl : Node_Id) return Boolean is
14150 begin
14151 return Nkind (Decl) in N_Generic_Instantiation
14152 or else
14153 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
14154 end Is_Instance_Node;
14155
14156 -- Start of processing for Is_Global
14157
14158 begin
14159 if E = Gen_Scope then
14160 return False;
14161
14162 elsif E = Standard_Standard then
14163 return True;
14164
14165 elsif Is_Child_Unit (E)
14166 and then (Is_Instance_Node (Parent (N2))
14167 or else (Nkind (Parent (N2)) = N_Expanded_Name
14168 and then N2 = Selector_Name (Parent (N2))
14169 and then
14170 Is_Instance_Node (Parent (Parent (N2)))))
14171 then
14172 return True;
14173
14174 else
14175 Se := Scope (E);
14176 while Se /= Gen_Scope loop
14177 if Se = Standard_Standard then
14178 return True;
14179 else
14180 Se := Scope (Se);
14181 end if;
14182 end loop;
14183
14184 return False;
14185 end if;
14186 end Is_Global;
14187
14188 --------------------------------
14189 -- Qualify_Universal_Operands --
14190 --------------------------------
14191
14192 procedure Qualify_Universal_Operands
14193 (Op : Node_Id;
14194 Func_Call : Node_Id)
14195 is
14196 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
14197 -- Rewrite operand Opnd as a qualified expression of the form
14198 --
14199 -- Actual_Typ'(Opnd)
14200 --
14201 -- where Actual is the corresponding actual parameter of Opnd in
14202 -- function call Func_Call.
14203
14204 function Qualify_Type
14205 (Loc : Source_Ptr;
14206 Typ : Entity_Id) return Node_Id;
14207 -- Qualify type Typ by creating a selected component of the form
14208 --
14209 -- Scope_Of_Typ.Typ
14210
14211 ---------------------
14212 -- Qualify_Operand --
14213 ---------------------
14214
14215 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
14216 Loc : constant Source_Ptr := Sloc (Opnd);
14217 Typ : constant Entity_Id := Etype (Actual);
14218 Mark : Node_Id;
14219 Qual : Node_Id;
14220
14221 begin
14222 -- Qualify the operand when it is of a universal type. Note that
14223 -- the template is unanalyzed and it is not possible to directly
14224 -- query the type. This transformation is not done when the type
14225 -- of the actual is internally generated because the type will be
14226 -- regenerated in the instance.
14227
14228 if Yields_Universal_Type (Opnd)
14229 and then Comes_From_Source (Typ)
14230 and then not Is_Hidden (Typ)
14231 then
14232 -- The type of the actual may be a global reference. Save this
14233 -- information by creating a reference to it.
14234
14235 if Is_Global (Typ) then
14236 Mark := New_Occurrence_Of (Typ, Loc);
14237
14238 -- Otherwise rely on resolution to find the proper type within
14239 -- the instance.
14240
14241 else
14242 Mark := Qualify_Type (Loc, Typ);
14243 end if;
14244
14245 Qual :=
14246 Make_Qualified_Expression (Loc,
14247 Subtype_Mark => Mark,
14248 Expression => Relocate_Node (Opnd));
14249
14250 -- Mark the qualification to distinguish it from other source
14251 -- constructs and signal the instantiation mechanism that this
14252 -- node requires special processing. See Copy_Generic_Node for
14253 -- details.
14254
14255 Set_Is_Qualified_Universal_Literal (Qual);
14256
14257 Rewrite (Opnd, Qual);
14258 end if;
14259 end Qualify_Operand;
14260
14261 ------------------
14262 -- Qualify_Type --
14263 ------------------
14264
14265 function Qualify_Type
14266 (Loc : Source_Ptr;
14267 Typ : Entity_Id) return Node_Id
14268 is
14269 Scop : constant Entity_Id := Scope (Typ);
14270 Result : Node_Id;
14271
14272 begin
14273 Result := Make_Identifier (Loc, Chars (Typ));
14274
14275 if Present (Scop) and then not Is_Generic_Unit (Scop) then
14276 Result :=
14277 Make_Selected_Component (Loc,
14278 Prefix => Make_Identifier (Loc, Chars (Scop)),
14279 Selector_Name => Result);
14280 end if;
14281
14282 return Result;
14283 end Qualify_Type;
14284
14285 -- Local variables
14286
14287 Actuals : constant List_Id := Parameter_Associations (Func_Call);
14288
14289 -- Start of processing for Qualify_Universal_Operands
14290
14291 begin
14292 if Nkind (Op) in N_Binary_Op then
14293 Qualify_Operand (Left_Opnd (Op), First (Actuals));
14294 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
14295
14296 elsif Nkind (Op) in N_Unary_Op then
14297 Qualify_Operand (Right_Opnd (Op), First (Actuals));
14298 end if;
14299 end Qualify_Universal_Operands;
14300
14301 ------------------
14302 -- Reset_Entity --
14303 ------------------
14304
14305 procedure Reset_Entity (N : Node_Id) is
14306 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
14307 -- If the type of N2 is global to the generic unit, save the type in
14308 -- the generic node. Just as we perform name capture for explicit
14309 -- references within the generic, we must capture the global types
14310 -- of local entities because they may participate in resolution in
14311 -- the instance.
14312
14313 function Top_Ancestor (E : Entity_Id) return Entity_Id;
14314 -- Find the ultimate ancestor of the current unit. If it is not a
14315 -- generic unit, then the name of the current unit in the prefix of
14316 -- an expanded name must be replaced with its generic homonym to
14317 -- ensure that it will be properly resolved in an instance.
14318
14319 ---------------------
14320 -- Set_Global_Type --
14321 ---------------------
14322
14323 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
14324 Typ : constant Entity_Id := Etype (N2);
14325
14326 begin
14327 Set_Etype (N, Typ);
14328
14329 -- If the entity of N is not the associated node, this is a
14330 -- nested generic and it has an associated node as well, whose
14331 -- type is already the full view (see below). Indicate that the
14332 -- original node has a private view.
14333
14334 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
14335 Set_Has_Private_View (N);
14336 end if;
14337
14338 -- If not a private type, nothing else to do
14339
14340 if not Is_Private_Type (Typ) then
14341 if Is_Array_Type (Typ)
14342 and then Is_Private_Type (Component_Type (Typ))
14343 then
14344 Set_Has_Private_View (N);
14345 end if;
14346
14347 -- If it is a derivation of a private type in a context where no
14348 -- full view is needed, nothing to do either.
14349
14350 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
14351 null;
14352
14353 -- Otherwise mark the type for flipping and use the full view when
14354 -- available.
14355
14356 else
14357 Set_Has_Private_View (N);
14358
14359 if Present (Full_View (Typ)) then
14360 Set_Etype (N2, Full_View (Typ));
14361 end if;
14362 end if;
14363
14364 if Is_Floating_Point_Type (Typ)
14365 and then Has_Dimension_System (Typ)
14366 then
14367 Copy_Dimensions (N2, N);
14368 end if;
14369 end Set_Global_Type;
14370
14371 ------------------
14372 -- Top_Ancestor --
14373 ------------------
14374
14375 function Top_Ancestor (E : Entity_Id) return Entity_Id is
14376 Par : Entity_Id;
14377
14378 begin
14379 Par := E;
14380 while Is_Child_Unit (Par) loop
14381 Par := Scope (Par);
14382 end loop;
14383
14384 return Par;
14385 end Top_Ancestor;
14386
14387 -- Start of processing for Reset_Entity
14388
14389 begin
14390 N2 := Get_Associated_Node (N);
14391 E := Entity (N2);
14392
14393 if Present (E) then
14394
14395 -- If the node is an entry call to an entry in an enclosing task,
14396 -- it is rewritten as a selected component. No global entity to
14397 -- preserve in this case, since the expansion will be redone in
14398 -- the instance.
14399
14400 if not Nkind_In (E, N_Defining_Character_Literal,
14401 N_Defining_Identifier,
14402 N_Defining_Operator_Symbol)
14403 then
14404 Set_Associated_Node (N, Empty);
14405 Set_Etype (N, Empty);
14406 return;
14407 end if;
14408
14409 -- If the entity is an itype created as a subtype of an access
14410 -- type with a null exclusion restore source entity for proper
14411 -- visibility. The itype will be created anew in the instance.
14412
14413 if Is_Itype (E)
14414 and then Ekind (E) = E_Access_Subtype
14415 and then Is_Entity_Name (N)
14416 and then Chars (Etype (E)) = Chars (N)
14417 then
14418 E := Etype (E);
14419 Set_Entity (N2, E);
14420 Set_Etype (N2, E);
14421 end if;
14422
14423 if Is_Global (E) then
14424
14425 -- If the entity is a package renaming that is the prefix of
14426 -- an expanded name, it has been rewritten as the renamed
14427 -- package, which is necessary semantically but complicates
14428 -- ASIS tree traversal, so we recover the original entity to
14429 -- expose the renaming. Take into account that the context may
14430 -- be a nested generic, that the original node may itself have
14431 -- an associated node that had better be an entity, and that
14432 -- the current node is still a selected component.
14433
14434 if Ekind (E) = E_Package
14435 and then Nkind (N) = N_Selected_Component
14436 and then Nkind (Parent (N)) = N_Expanded_Name
14437 and then Present (Original_Node (N2))
14438 and then Is_Entity_Name (Original_Node (N2))
14439 and then Present (Entity (Original_Node (N2)))
14440 then
14441 if Is_Global (Entity (Original_Node (N2))) then
14442 N2 := Original_Node (N2);
14443 Set_Associated_Node (N, N2);
14444 Set_Global_Type (N, N2);
14445
14446 -- Renaming is local, and will be resolved in instance
14447
14448 else
14449 Set_Associated_Node (N, Empty);
14450 Set_Etype (N, Empty);
14451 end if;
14452
14453 else
14454 Set_Global_Type (N, N2);
14455 end if;
14456
14457 elsif Nkind (N) = N_Op_Concat
14458 and then Is_Generic_Type (Etype (N2))
14459 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
14460 or else
14461 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
14462 and then Is_Intrinsic_Subprogram (E)
14463 then
14464 null;
14465
14466 -- Entity is local. Mark generic node as unresolved. Note that now
14467 -- it does not have an entity.
14468
14469 else
14470 Set_Associated_Node (N, Empty);
14471 Set_Etype (N, Empty);
14472 end if;
14473
14474 if Nkind (Parent (N)) in N_Generic_Instantiation
14475 and then N = Name (Parent (N))
14476 then
14477 Save_Global_Defaults (Parent (N), Parent (N2));
14478 end if;
14479
14480 elsif Nkind (Parent (N)) = N_Selected_Component
14481 and then Nkind (Parent (N2)) = N_Expanded_Name
14482 then
14483 if Is_Global (Entity (Parent (N2))) then
14484 Change_Selected_Component_To_Expanded_Name (Parent (N));
14485 Set_Associated_Node (Parent (N), Parent (N2));
14486 Set_Global_Type (Parent (N), Parent (N2));
14487 Save_Entity_Descendants (N);
14488
14489 -- If this is a reference to the current generic entity, replace
14490 -- by the name of the generic homonym of the current package. This
14491 -- is because in an instantiation Par.P.Q will not resolve to the
14492 -- name of the instance, whose enclosing scope is not necessarily
14493 -- Par. We use the generic homonym rather that the name of the
14494 -- generic itself because it may be hidden by a local declaration.
14495
14496 elsif In_Open_Scopes (Entity (Parent (N2)))
14497 and then not
14498 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
14499 then
14500 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
14501 Rewrite (Parent (N),
14502 Make_Identifier (Sloc (N),
14503 Chars =>
14504 Chars (Generic_Homonym (Entity (Parent (N2))))));
14505 else
14506 Rewrite (Parent (N),
14507 Make_Identifier (Sloc (N),
14508 Chars => Chars (Selector_Name (Parent (N2)))));
14509 end if;
14510 end if;
14511
14512 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
14513 and then Parent (N) = Name (Parent (Parent (N)))
14514 then
14515 Save_Global_Defaults
14516 (Parent (Parent (N)), Parent (Parent (N2)));
14517 end if;
14518
14519 -- A selected component may denote a static constant that has been
14520 -- folded. If the static constant is global to the generic, capture
14521 -- its value. Otherwise the folding will happen in any instantiation.
14522
14523 elsif Nkind (Parent (N)) = N_Selected_Component
14524 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
14525 then
14526 if Present (Entity (Original_Node (Parent (N2))))
14527 and then Is_Global (Entity (Original_Node (Parent (N2))))
14528 then
14529 Rewrite (Parent (N), New_Copy (Parent (N2)));
14530 Set_Analyzed (Parent (N), False);
14531 end if;
14532
14533 -- A selected component may be transformed into a parameterless
14534 -- function call. If the called entity is global, rewrite the node
14535 -- appropriately, i.e. as an extended name for the global entity.
14536
14537 elsif Nkind (Parent (N)) = N_Selected_Component
14538 and then Nkind (Parent (N2)) = N_Function_Call
14539 and then N = Selector_Name (Parent (N))
14540 then
14541 if No (Parameter_Associations (Parent (N2))) then
14542 if Is_Global (Entity (Name (Parent (N2)))) then
14543 Change_Selected_Component_To_Expanded_Name (Parent (N));
14544 Set_Associated_Node (Parent (N), Name (Parent (N2)));
14545 Set_Global_Type (Parent (N), Name (Parent (N2)));
14546 Save_Entity_Descendants (N);
14547
14548 else
14549 Set_Is_Prefixed_Call (Parent (N));
14550 Set_Associated_Node (N, Empty);
14551 Set_Etype (N, Empty);
14552 end if;
14553
14554 -- In Ada 2005, X.F may be a call to a primitive operation,
14555 -- rewritten as F (X). This rewriting will be done again in an
14556 -- instance, so keep the original node. Global entities will be
14557 -- captured as for other constructs. Indicate that this must
14558 -- resolve as a call, to prevent accidental overloading in the
14559 -- instance, if both a component and a primitive operation appear
14560 -- as candidates.
14561
14562 else
14563 Set_Is_Prefixed_Call (Parent (N));
14564 end if;
14565
14566 -- Entity is local. Reset in generic unit, so that node is resolved
14567 -- anew at the point of instantiation.
14568
14569 else
14570 Set_Associated_Node (N, Empty);
14571 Set_Etype (N, Empty);
14572 end if;
14573 end Reset_Entity;
14574
14575 -----------------------------
14576 -- Save_Entity_Descendants --
14577 -----------------------------
14578
14579 procedure Save_Entity_Descendants (N : Node_Id) is
14580 begin
14581 case Nkind (N) is
14582 when N_Binary_Op =>
14583 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
14584 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14585
14586 when N_Unary_Op =>
14587 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14588
14589 when N_Expanded_Name
14590 | N_Selected_Component
14591 =>
14592 Save_Global_Descendant (Union_Id (Prefix (N)));
14593 Save_Global_Descendant (Union_Id (Selector_Name (N)));
14594
14595 when N_Character_Literal
14596 | N_Identifier
14597 | N_Operator_Symbol
14598 =>
14599 null;
14600
14601 when others =>
14602 raise Program_Error;
14603 end case;
14604 end Save_Entity_Descendants;
14605
14606 --------------------------
14607 -- Save_Global_Defaults --
14608 --------------------------
14609
14610 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
14611 Loc : constant Source_Ptr := Sloc (N1);
14612 Assoc2 : constant List_Id := Generic_Associations (N2);
14613 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
14614 Assoc1 : List_Id;
14615 Act1 : Node_Id;
14616 Act2 : Node_Id;
14617 Def : Node_Id;
14618 Ndec : Node_Id;
14619 Subp : Entity_Id;
14620 Actual : Entity_Id;
14621
14622 begin
14623 Assoc1 := Generic_Associations (N1);
14624
14625 if Present (Assoc1) then
14626 Act1 := First (Assoc1);
14627 else
14628 Act1 := Empty;
14629 Set_Generic_Associations (N1, New_List);
14630 Assoc1 := Generic_Associations (N1);
14631 end if;
14632
14633 if Present (Assoc2) then
14634 Act2 := First (Assoc2);
14635 else
14636 return;
14637 end if;
14638
14639 while Present (Act1) and then Present (Act2) loop
14640 Next (Act1);
14641 Next (Act2);
14642 end loop;
14643
14644 -- Find the associations added for default subprograms
14645
14646 if Present (Act2) then
14647 while Nkind (Act2) /= N_Generic_Association
14648 or else No (Entity (Selector_Name (Act2)))
14649 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
14650 loop
14651 Next (Act2);
14652 end loop;
14653
14654 -- Add a similar association if the default is global. The
14655 -- renaming declaration for the actual has been analyzed, and
14656 -- its alias is the program it renames. Link the actual in the
14657 -- original generic tree with the node in the analyzed tree.
14658
14659 while Present (Act2) loop
14660 Subp := Entity (Selector_Name (Act2));
14661 Def := Explicit_Generic_Actual_Parameter (Act2);
14662
14663 -- Following test is defence against rubbish errors
14664
14665 if No (Alias (Subp)) then
14666 return;
14667 end if;
14668
14669 -- Retrieve the resolved actual from the renaming declaration
14670 -- created for the instantiated formal.
14671
14672 Actual := Entity (Name (Parent (Parent (Subp))));
14673 Set_Entity (Def, Actual);
14674 Set_Etype (Def, Etype (Actual));
14675
14676 if Is_Global (Actual) then
14677 Ndec :=
14678 Make_Generic_Association (Loc,
14679 Selector_Name =>
14680 New_Occurrence_Of (Subp, Loc),
14681 Explicit_Generic_Actual_Parameter =>
14682 New_Occurrence_Of (Actual, Loc));
14683
14684 Set_Associated_Node
14685 (Explicit_Generic_Actual_Parameter (Ndec), Def);
14686
14687 Append (Ndec, Assoc1);
14688
14689 -- If there are other defaults, add a dummy association in case
14690 -- there are other defaulted formals with the same name.
14691
14692 elsif Present (Next (Act2)) then
14693 Ndec :=
14694 Make_Generic_Association (Loc,
14695 Selector_Name =>
14696 New_Occurrence_Of (Subp, Loc),
14697 Explicit_Generic_Actual_Parameter => Empty);
14698
14699 Append (Ndec, Assoc1);
14700 end if;
14701
14702 Next (Act2);
14703 end loop;
14704 end if;
14705
14706 if Nkind (Name (N1)) = N_Identifier
14707 and then Is_Child_Unit (Gen_Id)
14708 and then Is_Global (Gen_Id)
14709 and then Is_Generic_Unit (Scope (Gen_Id))
14710 and then In_Open_Scopes (Scope (Gen_Id))
14711 then
14712 -- This is an instantiation of a child unit within a sibling, so
14713 -- that the generic parent is in scope. An eventual instance must
14714 -- occur within the scope of an instance of the parent. Make name
14715 -- in instance into an expanded name, to preserve the identifier
14716 -- of the parent, so it can be resolved subsequently.
14717
14718 Rewrite (Name (N2),
14719 Make_Expanded_Name (Loc,
14720 Chars => Chars (Gen_Id),
14721 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14722 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14723 Set_Entity (Name (N2), Gen_Id);
14724
14725 Rewrite (Name (N1),
14726 Make_Expanded_Name (Loc,
14727 Chars => Chars (Gen_Id),
14728 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14729 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14730
14731 Set_Associated_Node (Name (N1), Name (N2));
14732 Set_Associated_Node (Prefix (Name (N1)), Empty);
14733 Set_Associated_Node
14734 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
14735 Set_Etype (Name (N1), Etype (Gen_Id));
14736 end if;
14737 end Save_Global_Defaults;
14738
14739 ----------------------------
14740 -- Save_Global_Descendant --
14741 ----------------------------
14742
14743 procedure Save_Global_Descendant (D : Union_Id) is
14744 N1 : Node_Id;
14745
14746 begin
14747 if D in Node_Range then
14748 if D = Union_Id (Empty) then
14749 null;
14750
14751 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
14752 Save_References (Node_Id (D));
14753 end if;
14754
14755 elsif D in List_Range then
14756 pragma Assert (D /= Union_Id (No_List));
14757 -- Because No_List = Empty, which is in Node_Range above
14758
14759 if Is_Empty_List (List_Id (D)) then
14760 null;
14761
14762 else
14763 N1 := First (List_Id (D));
14764 while Present (N1) loop
14765 Save_References (N1);
14766 Next (N1);
14767 end loop;
14768 end if;
14769
14770 -- Element list or other non-node field, nothing to do
14771
14772 else
14773 null;
14774 end if;
14775 end Save_Global_Descendant;
14776
14777 ---------------------
14778 -- Save_References --
14779 ---------------------
14780
14781 -- This is the recursive procedure that does the work once the enclosing
14782 -- generic scope has been established. We have to treat specially a
14783 -- number of node rewritings that are required by semantic processing
14784 -- and which change the kind of nodes in the generic copy: typically
14785 -- constant-folding, replacing an operator node by a string literal, or
14786 -- a selected component by an expanded name. In each of those cases, the
14787 -- transformation is propagated to the generic unit.
14788
14789 procedure Save_References (N : Node_Id) is
14790 Loc : constant Source_Ptr := Sloc (N);
14791
14792 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
14793 -- Determine whether arbitrary node Nod requires delayed capture of
14794 -- global references within its aspect specifications.
14795
14796 procedure Save_References_In_Aggregate (N : Node_Id);
14797 -- Save all global references in [extension] aggregate node N
14798
14799 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
14800 -- Save all global references in a character literal or operator
14801 -- symbol denoted by N.
14802
14803 procedure Save_References_In_Descendants (N : Node_Id);
14804 -- Save all global references in all descendants of node N
14805
14806 procedure Save_References_In_Identifier (N : Node_Id);
14807 -- Save all global references in identifier node N
14808
14809 procedure Save_References_In_Operator (N : Node_Id);
14810 -- Save all global references in operator node N
14811
14812 procedure Save_References_In_Pragma (Prag : Node_Id);
14813 -- Save all global references found within the expression of pragma
14814 -- Prag.
14815
14816 ---------------------------
14817 -- Requires_Delayed_Save --
14818 ---------------------------
14819
14820 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
14821 begin
14822 -- Generic packages and subprograms require delayed capture of
14823 -- global references within their aspects due to the timing of
14824 -- annotation analysis.
14825
14826 if Nkind_In (Nod, N_Generic_Package_Declaration,
14827 N_Generic_Subprogram_Declaration,
14828 N_Package_Body,
14829 N_Package_Body_Stub,
14830 N_Subprogram_Body,
14831 N_Subprogram_Body_Stub)
14832 then
14833 -- Since the capture of global references is done on the
14834 -- unanalyzed generic template, there is no information around
14835 -- to infer the context. Use the Associated_Entity linkages to
14836 -- peek into the analyzed generic copy and determine what the
14837 -- template corresponds to.
14838
14839 if Nod = Templ then
14840 return
14841 Is_Generic_Declaration_Or_Body
14842 (Unit_Declaration_Node
14843 (Associated_Entity (Defining_Entity (Nod))));
14844
14845 -- Otherwise the generic unit being processed is not the top
14846 -- level template. It is safe to capture of global references
14847 -- within the generic unit because at this point the top level
14848 -- copy is fully analyzed.
14849
14850 else
14851 return False;
14852 end if;
14853
14854 -- Otherwise capture the global references without interference
14855
14856 else
14857 return False;
14858 end if;
14859 end Requires_Delayed_Save;
14860
14861 ----------------------------------
14862 -- Save_References_In_Aggregate --
14863 ----------------------------------
14864
14865 procedure Save_References_In_Aggregate (N : Node_Id) is
14866 Nam : Node_Id;
14867 Qual : Node_Id := Empty;
14868 Typ : Entity_Id := Empty;
14869
14870 use Atree.Unchecked_Access;
14871 -- This code section is part of implementing an untyped tree
14872 -- traversal, so it needs direct access to node fields.
14873
14874 begin
14875 N2 := Get_Associated_Node (N);
14876
14877 if Present (N2) then
14878 Typ := Etype (N2);
14879
14880 -- In an instance within a generic, use the name of the actual
14881 -- and not the original generic parameter. If the actual is
14882 -- global in the current generic it must be preserved for its
14883 -- instantiation.
14884
14885 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14886 and then Present (Generic_Parent_Type (Parent (Typ)))
14887 then
14888 Typ := Base_Type (Typ);
14889 Set_Etype (N2, Typ);
14890 end if;
14891 end if;
14892
14893 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
14894 Set_Associated_Node (N, Empty);
14895
14896 -- If the aggregate is an actual in a call, it has been
14897 -- resolved in the current context, to some local type. The
14898 -- enclosing call may have been disambiguated by the aggregate,
14899 -- and this disambiguation might fail at instantiation time
14900 -- because the type to which the aggregate did resolve is not
14901 -- preserved. In order to preserve some of this information,
14902 -- wrap the aggregate in a qualified expression, using the id
14903 -- of its type. For further disambiguation we qualify the type
14904 -- name with its scope (if visible) because both id's will have
14905 -- corresponding entities in an instance. This resolves most of
14906 -- the problems with missing type information on aggregates in
14907 -- instances.
14908
14909 if Present (N2)
14910 and then Nkind (N2) = Nkind (N)
14911 and then Nkind (Parent (N2)) in N_Subprogram_Call
14912 and then Present (Typ)
14913 and then Comes_From_Source (Typ)
14914 then
14915 Nam := Make_Identifier (Loc, Chars (Typ));
14916
14917 if Is_Immediately_Visible (Scope (Typ)) then
14918 Nam :=
14919 Make_Selected_Component (Loc,
14920 Prefix =>
14921 Make_Identifier (Loc, Chars (Scope (Typ))),
14922 Selector_Name => Nam);
14923 end if;
14924
14925 Qual :=
14926 Make_Qualified_Expression (Loc,
14927 Subtype_Mark => Nam,
14928 Expression => Relocate_Node (N));
14929 end if;
14930 end if;
14931
14932 Save_Global_Descendant (Field1 (N));
14933 Save_Global_Descendant (Field2 (N));
14934 Save_Global_Descendant (Field3 (N));
14935 Save_Global_Descendant (Field5 (N));
14936
14937 if Present (Qual) then
14938 Rewrite (N, Qual);
14939 end if;
14940 end Save_References_In_Aggregate;
14941
14942 ----------------------------------------------
14943 -- Save_References_In_Char_Lit_Or_Op_Symbol --
14944 ----------------------------------------------
14945
14946 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
14947 begin
14948 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14949 Reset_Entity (N);
14950
14951 elsif Nkind (N) = N_Operator_Symbol
14952 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
14953 then
14954 Change_Operator_Symbol_To_String_Literal (N);
14955 end if;
14956 end Save_References_In_Char_Lit_Or_Op_Symbol;
14957
14958 ------------------------------------
14959 -- Save_References_In_Descendants --
14960 ------------------------------------
14961
14962 procedure Save_References_In_Descendants (N : Node_Id) is
14963 use Atree.Unchecked_Access;
14964 -- This code section is part of implementing an untyped tree
14965 -- traversal, so it needs direct access to node fields.
14966
14967 begin
14968 Save_Global_Descendant (Field1 (N));
14969 Save_Global_Descendant (Field2 (N));
14970 Save_Global_Descendant (Field3 (N));
14971 Save_Global_Descendant (Field4 (N));
14972 Save_Global_Descendant (Field5 (N));
14973 end Save_References_In_Descendants;
14974
14975 -----------------------------------
14976 -- Save_References_In_Identifier --
14977 -----------------------------------
14978
14979 procedure Save_References_In_Identifier (N : Node_Id) is
14980 begin
14981 -- The node did not undergo a transformation
14982
14983 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14984 declare
14985 Aux_N2 : constant Node_Id := Get_Associated_Node (N);
14986 Orig_N2_Parent : constant Node_Id :=
14987 Original_Node (Parent (Aux_N2));
14988 begin
14989 -- The parent of this identifier is a selected component
14990 -- which denotes a named number that was constant folded.
14991 -- Preserve the original name for ASIS and link the parent
14992 -- with its expanded name. The constant folding will be
14993 -- repeated in the instance.
14994
14995 if Nkind (Parent (N)) = N_Selected_Component
14996 and then Nkind_In (Parent (Aux_N2), N_Integer_Literal,
14997 N_Real_Literal)
14998 and then Is_Entity_Name (Orig_N2_Parent)
14999 and then Ekind (Entity (Orig_N2_Parent)) in Named_Kind
15000 and then Is_Global (Entity (Orig_N2_Parent))
15001 then
15002 N2 := Aux_N2;
15003 Set_Associated_Node
15004 (Parent (N), Original_Node (Parent (N2)));
15005
15006 -- Common case
15007
15008 else
15009 -- If this is a discriminant reference, always save it.
15010 -- It is used in the instance to find the corresponding
15011 -- discriminant positionally rather than by name.
15012
15013 Set_Original_Discriminant
15014 (N, Original_Discriminant (Get_Associated_Node (N)));
15015 end if;
15016
15017 Reset_Entity (N);
15018 end;
15019
15020 -- The analysis of the generic copy transformed the identifier
15021 -- into another construct. Propagate the changes to the template.
15022
15023 else
15024 N2 := Get_Associated_Node (N);
15025
15026 -- The identifier denotes a call to a parameterless function.
15027 -- Mark the node as resolved when the function is external.
15028
15029 if Nkind (N2) = N_Function_Call then
15030 E := Entity (Name (N2));
15031
15032 if Present (E) and then Is_Global (E) then
15033 Set_Etype (N, Etype (N2));
15034 else
15035 Set_Associated_Node (N, Empty);
15036 Set_Etype (N, Empty);
15037 end if;
15038
15039 -- The identifier denotes a named number that was constant
15040 -- folded. Preserve the original name for ASIS and undo the
15041 -- constant folding which will be repeated in the instance.
15042
15043 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
15044 and then Is_Entity_Name (Original_Node (N2))
15045 then
15046 Set_Associated_Node (N, Original_Node (N2));
15047 Reset_Entity (N);
15048
15049 -- The identifier resolved to a string literal. Propagate this
15050 -- information to the generic template.
15051
15052 elsif Nkind (N2) = N_String_Literal then
15053 Rewrite (N, New_Copy (N2));
15054
15055 -- The identifier is rewritten as a dereference if it is the
15056 -- prefix of an implicit dereference. Preserve the original
15057 -- tree as the analysis of the instance will expand the node
15058 -- again, but preserve the resolved entity if it is global.
15059
15060 elsif Nkind (N2) = N_Explicit_Dereference then
15061 if Is_Entity_Name (Prefix (N2))
15062 and then Present (Entity (Prefix (N2)))
15063 and then Is_Global (Entity (Prefix (N2)))
15064 then
15065 Set_Associated_Node (N, Prefix (N2));
15066
15067 elsif Nkind (Prefix (N2)) = N_Function_Call
15068 and then Present (Entity (Name (Prefix (N2))))
15069 and then Is_Global (Entity (Name (Prefix (N2))))
15070 then
15071 Rewrite (N,
15072 Make_Explicit_Dereference (Loc,
15073 Prefix =>
15074 Make_Function_Call (Loc,
15075 Name =>
15076 New_Occurrence_Of
15077 (Entity (Name (Prefix (N2))), Loc))));
15078
15079 else
15080 Set_Associated_Node (N, Empty);
15081 Set_Etype (N, Empty);
15082 end if;
15083
15084 -- The subtype mark of a nominally unconstrained object is
15085 -- rewritten as a subtype indication using the bounds of the
15086 -- expression. Recover the original subtype mark.
15087
15088 elsif Nkind (N2) = N_Subtype_Indication
15089 and then Is_Entity_Name (Original_Node (N2))
15090 then
15091 Set_Associated_Node (N, Original_Node (N2));
15092 Reset_Entity (N);
15093 end if;
15094 end if;
15095 end Save_References_In_Identifier;
15096
15097 ---------------------------------
15098 -- Save_References_In_Operator --
15099 ---------------------------------
15100
15101 procedure Save_References_In_Operator (N : Node_Id) is
15102 begin
15103 -- The node did not undergo a transformation
15104
15105 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15106 if Nkind (N) = N_Op_Concat then
15107 Set_Is_Component_Left_Opnd (N,
15108 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15109
15110 Set_Is_Component_Right_Opnd (N,
15111 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15112 end if;
15113
15114 Reset_Entity (N);
15115
15116 -- The analysis of the generic copy transformed the operator into
15117 -- some other construct. Propagate the changes to the template if
15118 -- applicable.
15119
15120 else
15121 N2 := Get_Associated_Node (N);
15122
15123 -- The operator resoved to a function call
15124
15125 if Nkind (N2) = N_Function_Call then
15126
15127 -- Add explicit qualifications in the generic template for
15128 -- all operands of universal type. This aids resolution by
15129 -- preserving the actual type of a literal or an attribute
15130 -- that yields a universal result.
15131
15132 Qualify_Universal_Operands (N, N2);
15133
15134 E := Entity (Name (N2));
15135
15136 if Present (E) and then Is_Global (E) then
15137 Set_Etype (N, Etype (N2));
15138 else
15139 Set_Associated_Node (N, Empty);
15140 Set_Etype (N, Empty);
15141 end if;
15142
15143 -- The operator was folded into a literal
15144
15145 elsif Nkind_In (N2, N_Integer_Literal,
15146 N_Real_Literal,
15147 N_String_Literal)
15148 then
15149 if Present (Original_Node (N2))
15150 and then Nkind (Original_Node (N2)) = Nkind (N)
15151 then
15152 -- Operation was constant-folded. Whenever possible,
15153 -- recover semantic information from unfolded node,
15154 -- for ASIS use.
15155
15156 Set_Associated_Node (N, Original_Node (N2));
15157
15158 if Nkind (N) = N_Op_Concat then
15159 Set_Is_Component_Left_Opnd (N,
15160 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15161 Set_Is_Component_Right_Opnd (N,
15162 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15163 end if;
15164
15165 Reset_Entity (N);
15166
15167 -- Propagate the constant folding back to the template
15168
15169 else
15170 Rewrite (N, New_Copy (N2));
15171 Set_Analyzed (N, False);
15172 end if;
15173
15174 -- The operator was folded into an enumeration literal. Retain
15175 -- the entity to avoid spurious ambiguities if it is overloaded
15176 -- at the point of instantiation or inlining.
15177
15178 elsif Nkind (N2) = N_Identifier
15179 and then Ekind (Entity (N2)) = E_Enumeration_Literal
15180 then
15181 Rewrite (N, New_Copy (N2));
15182 Set_Analyzed (N, False);
15183 end if;
15184 end if;
15185
15186 -- Complete the operands check if node has not been constant
15187 -- folded.
15188
15189 if Nkind (N) in N_Op then
15190 Save_Entity_Descendants (N);
15191 end if;
15192 end Save_References_In_Operator;
15193
15194 -------------------------------
15195 -- Save_References_In_Pragma --
15196 -------------------------------
15197
15198 procedure Save_References_In_Pragma (Prag : Node_Id) is
15199 Context : Node_Id;
15200 Do_Save : Boolean := True;
15201
15202 use Atree.Unchecked_Access;
15203 -- This code section is part of implementing an untyped tree
15204 -- traversal, so it needs direct access to node fields.
15205
15206 begin
15207 -- Do not save global references in pragmas generated from aspects
15208 -- because the pragmas will be regenerated at instantiation time.
15209
15210 if From_Aspect_Specification (Prag) then
15211 Do_Save := False;
15212
15213 -- The capture of global references within contract-related source
15214 -- pragmas associated with generic packages, subprograms or their
15215 -- respective bodies must be delayed due to timing of annotation
15216 -- analysis. Global references are still captured in routine
15217 -- Save_Global_References_In_Contract.
15218
15219 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
15220 if Is_Package_Contract_Annotation (Prag) then
15221 Context := Find_Related_Package_Or_Body (Prag);
15222 else
15223 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
15224 Context := Find_Related_Declaration_Or_Body (Prag);
15225 end if;
15226
15227 -- The use of Original_Node accounts for the case when the
15228 -- related context is generic template.
15229
15230 if Requires_Delayed_Save (Original_Node (Context)) then
15231 Do_Save := False;
15232 end if;
15233 end if;
15234
15235 -- For all other cases, save all global references within the
15236 -- descendants, but skip the following semantic fields:
15237
15238 -- Field1 - Next_Pragma
15239 -- Field3 - Corresponding_Aspect
15240 -- Field5 - Next_Rep_Item
15241
15242 if Do_Save then
15243 Save_Global_Descendant (Field2 (Prag));
15244 Save_Global_Descendant (Field4 (Prag));
15245 end if;
15246 end Save_References_In_Pragma;
15247
15248 -- Start of processing for Save_References
15249
15250 begin
15251 if N = Empty then
15252 null;
15253
15254 -- Aggregates
15255
15256 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
15257 Save_References_In_Aggregate (N);
15258
15259 -- Character literals, operator symbols
15260
15261 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
15262 Save_References_In_Char_Lit_Or_Op_Symbol (N);
15263
15264 -- Defining identifiers
15265
15266 elsif Nkind (N) in N_Entity then
15267 null;
15268
15269 -- Identifiers
15270
15271 elsif Nkind (N) = N_Identifier then
15272 Save_References_In_Identifier (N);
15273
15274 -- Operators
15275
15276 elsif Nkind (N) in N_Op then
15277 Save_References_In_Operator (N);
15278
15279 -- Pragmas
15280
15281 elsif Nkind (N) = N_Pragma then
15282 Save_References_In_Pragma (N);
15283
15284 else
15285 Save_References_In_Descendants (N);
15286 end if;
15287
15288 -- Save all global references found within the aspect specifications
15289 -- of the related node.
15290
15291 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
15292
15293 -- The capture of global references within aspects associated with
15294 -- generic packages, subprograms or their bodies must be delayed
15295 -- due to timing of annotation analysis. Global references are
15296 -- still captured in routine Save_Global_References_In_Contract.
15297
15298 if Requires_Delayed_Save (N) then
15299 null;
15300
15301 -- Otherwise save all global references within the aspects
15302
15303 else
15304 Save_Global_References_In_Aspects (N);
15305 end if;
15306 end if;
15307 end Save_References;
15308
15309 -- Start of processing for Save_Global_References
15310
15311 begin
15312 Gen_Scope := Current_Scope;
15313
15314 -- If the generic unit is a child unit, references to entities in the
15315 -- parent are treated as local, because they will be resolved anew in
15316 -- the context of the instance of the parent.
15317
15318 while Is_Child_Unit (Gen_Scope)
15319 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
15320 loop
15321 Gen_Scope := Scope (Gen_Scope);
15322 end loop;
15323
15324 Save_References (Templ);
15325 end Save_Global_References;
15326
15327 ---------------------------------------
15328 -- Save_Global_References_In_Aspects --
15329 ---------------------------------------
15330
15331 procedure Save_Global_References_In_Aspects (N : Node_Id) is
15332 Asp : Node_Id;
15333 Expr : Node_Id;
15334
15335 begin
15336 Asp := First (Aspect_Specifications (N));
15337 while Present (Asp) loop
15338 Expr := Expression (Asp);
15339
15340 if Present (Expr) then
15341 Save_Global_References (Expr);
15342 end if;
15343
15344 Next (Asp);
15345 end loop;
15346 end Save_Global_References_In_Aspects;
15347
15348 ------------------------------------------
15349 -- Set_Copied_Sloc_For_Inherited_Pragma --
15350 ------------------------------------------
15351
15352 procedure Set_Copied_Sloc_For_Inherited_Pragma
15353 (N : Node_Id;
15354 E : Entity_Id)
15355 is
15356 begin
15357 Create_Instantiation_Source (N, E,
15358 Inlined_Body => False,
15359 Inherited_Pragma => True,
15360 Factor => S_Adjustment);
15361 end Set_Copied_Sloc_For_Inherited_Pragma;
15362
15363 --------------------------------------
15364 -- Set_Copied_Sloc_For_Inlined_Body --
15365 --------------------------------------
15366
15367 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
15368 begin
15369 Create_Instantiation_Source (N, E,
15370 Inlined_Body => True,
15371 Inherited_Pragma => False,
15372 Factor => S_Adjustment);
15373 end Set_Copied_Sloc_For_Inlined_Body;
15374
15375 ---------------------
15376 -- Set_Instance_Of --
15377 ---------------------
15378
15379 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
15380 begin
15381 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
15382 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
15383 Generic_Renamings.Increment_Last;
15384 end Set_Instance_Of;
15385
15386 --------------------
15387 -- Set_Next_Assoc --
15388 --------------------
15389
15390 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
15391 begin
15392 Generic_Renamings.Table (E).Next_In_HTable := Next;
15393 end Set_Next_Assoc;
15394
15395 -------------------
15396 -- Start_Generic --
15397 -------------------
15398
15399 procedure Start_Generic is
15400 begin
15401 -- ??? More things could be factored out in this routine.
15402 -- Should probably be done at a later stage.
15403
15404 Generic_Flags.Append (Inside_A_Generic);
15405 Inside_A_Generic := True;
15406
15407 Expander_Mode_Save_And_Set (False);
15408 end Start_Generic;
15409
15410 ----------------------
15411 -- Set_Instance_Env --
15412 ----------------------
15413
15414 procedure Set_Instance_Env
15415 (Gen_Unit : Entity_Id;
15416 Act_Unit : Entity_Id)
15417 is
15418 Assertion_Status : constant Boolean := Assertions_Enabled;
15419 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
15420 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
15421
15422 begin
15423 -- Regardless of the current mode, predefined units are analyzed in the
15424 -- most current Ada mode, and earlier version Ada checks do not apply
15425 -- to predefined units. Nothing needs to be done for non-internal units.
15426 -- These are always analyzed in the current mode.
15427
15428 if Is_Internal_File_Name
15429 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
15430 Renamings_Included => True)
15431 then
15432 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
15433
15434 -- In Ada2012 we may want to enable assertions in an instance of a
15435 -- predefined unit, in which case we need to preserve the current
15436 -- setting for the Assertions_Enabled flag. This will become more
15437 -- critical when pre/postconditions are added to predefined units,
15438 -- as is already the case for some numeric libraries.
15439
15440 if Ada_Version >= Ada_2012 then
15441 Assertions_Enabled := Assertion_Status;
15442 end if;
15443
15444 -- SPARK_Mode for an instance is the one applicable at the point of
15445 -- instantiation.
15446
15447 SPARK_Mode := Save_SPARK_Mode;
15448 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
15449 end if;
15450
15451 Current_Instantiated_Parent :=
15452 (Gen_Id => Gen_Unit,
15453 Act_Id => Act_Unit,
15454 Next_In_HTable => Assoc_Null);
15455 end Set_Instance_Env;
15456
15457 -----------------
15458 -- Switch_View --
15459 -----------------
15460
15461 procedure Switch_View (T : Entity_Id) is
15462 BT : constant Entity_Id := Base_Type (T);
15463 Priv_Elmt : Elmt_Id := No_Elmt;
15464 Priv_Sub : Entity_Id;
15465
15466 begin
15467 -- T may be private but its base type may have been exchanged through
15468 -- some other occurrence, in which case there is nothing to switch
15469 -- besides T itself. Note that a private dependent subtype of a private
15470 -- type might not have been switched even if the base type has been,
15471 -- because of the last branch of Check_Private_View (see comment there).
15472
15473 if not Is_Private_Type (BT) then
15474 Prepend_Elmt (Full_View (T), Exchanged_Views);
15475 Exchange_Declarations (T);
15476 return;
15477 end if;
15478
15479 Priv_Elmt := First_Elmt (Private_Dependents (BT));
15480
15481 if Present (Full_View (BT)) then
15482 Prepend_Elmt (Full_View (BT), Exchanged_Views);
15483 Exchange_Declarations (BT);
15484 end if;
15485
15486 while Present (Priv_Elmt) loop
15487 Priv_Sub := (Node (Priv_Elmt));
15488
15489 -- We avoid flipping the subtype if the Etype of its full view is
15490 -- private because this would result in a malformed subtype. This
15491 -- occurs when the Etype of the subtype full view is the full view of
15492 -- the base type (and since the base types were just switched, the
15493 -- subtype is pointing to the wrong view). This is currently the case
15494 -- for tagged record types, access types (maybe more?) and needs to
15495 -- be resolved. ???
15496
15497 if Present (Full_View (Priv_Sub))
15498 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
15499 then
15500 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
15501 Exchange_Declarations (Priv_Sub);
15502 end if;
15503
15504 Next_Elmt (Priv_Elmt);
15505 end loop;
15506 end Switch_View;
15507
15508 -----------------
15509 -- True_Parent --
15510 -----------------
15511
15512 function True_Parent (N : Node_Id) return Node_Id is
15513 begin
15514 if Nkind (Parent (N)) = N_Subunit then
15515 return Parent (Corresponding_Stub (Parent (N)));
15516 else
15517 return Parent (N);
15518 end if;
15519 end True_Parent;
15520
15521 -----------------------------
15522 -- Valid_Default_Attribute --
15523 -----------------------------
15524
15525 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
15526 Attr_Id : constant Attribute_Id :=
15527 Get_Attribute_Id (Attribute_Name (Def));
15528 T : constant Entity_Id := Entity (Prefix (Def));
15529 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
15530 F : Entity_Id;
15531 Num_F : Nat;
15532 OK : Boolean;
15533
15534 begin
15535 if No (T) or else T = Any_Id then
15536 return;
15537 end if;
15538
15539 Num_F := 0;
15540 F := First_Formal (Nam);
15541 while Present (F) loop
15542 Num_F := Num_F + 1;
15543 Next_Formal (F);
15544 end loop;
15545
15546 case Attr_Id is
15547 when Attribute_Adjacent
15548 | Attribute_Ceiling
15549 | Attribute_Copy_Sign
15550 | Attribute_Floor
15551 | Attribute_Fraction
15552 | Attribute_Machine
15553 | Attribute_Model
15554 | Attribute_Remainder
15555 | Attribute_Rounding
15556 | Attribute_Unbiased_Rounding
15557 =>
15558 OK := Is_Fun
15559 and then Num_F = 1
15560 and then Is_Floating_Point_Type (T);
15561
15562 when Attribute_Image
15563 | Attribute_Pred
15564 | Attribute_Succ
15565 | Attribute_Value
15566 | Attribute_Wide_Image
15567 | Attribute_Wide_Value
15568 =>
15569 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
15570
15571 when Attribute_Max
15572 | Attribute_Min
15573 =>
15574 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
15575
15576 when Attribute_Input =>
15577 OK := (Is_Fun and then Num_F = 1);
15578
15579 when Attribute_Output
15580 | Attribute_Read
15581 | Attribute_Write
15582 =>
15583 OK := not Is_Fun and then Num_F = 2;
15584
15585 when others =>
15586 OK := False;
15587 end case;
15588
15589 if not OK then
15590 Error_Msg_N
15591 ("attribute reference has wrong profile for subprogram", Def);
15592 end if;
15593 end Valid_Default_Attribute;
15594
15595 end Sem_Ch12;