[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-2015, 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 Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Expander; use Expander;
32 with Exp_Disp; use Exp_Disp;
33 with Fname; use Fname;
34 with Fname.UF; use Fname.UF;
35 with Freeze; use Freeze;
36 with Ghost; use Ghost;
37 with Itypes; use Itypes;
38 with Lib; use Lib;
39 with Lib.Load; use Lib.Load;
40 with Lib.Xref; use Lib.Xref;
41 with Nlists; use Nlists;
42 with Namet; use Namet;
43 with Nmake; use Nmake;
44 with Opt; use Opt;
45 with Rident; use Rident;
46 with Restrict; use Restrict;
47 with Rtsfind; use Rtsfind;
48 with Sem; use Sem;
49 with Sem_Aux; use Sem_Aux;
50 with Sem_Cat; use Sem_Cat;
51 with Sem_Ch3; use Sem_Ch3;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch7; use Sem_Ch7;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Ch10; use Sem_Ch10;
56 with Sem_Ch13; use Sem_Ch13;
57 with Sem_Dim; use Sem_Dim;
58 with Sem_Disp; use Sem_Disp;
59 with Sem_Elab; use Sem_Elab;
60 with Sem_Elim; use Sem_Elim;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Prag; use Sem_Prag;
63 with Sem_Res; use Sem_Res;
64 with Sem_Type; use Sem_Type;
65 with Sem_Util; use Sem_Util;
66 with Sem_Warn; use Sem_Warn;
67 with Stand; use Stand;
68 with Sinfo; use Sinfo;
69 with Sinfo.CN; use Sinfo.CN;
70 with Sinput; use Sinput;
71 with Sinput.L; use Sinput.L;
72 with Snames; use Snames;
73 with Stringt; use Stringt;
74 with Uname; use Uname;
75 with Table;
76 with Tbuild; use Tbuild;
77 with Uintp; use Uintp;
78 with Urealp; use Urealp;
79 with Warnsw; use Warnsw;
80
81 with GNAT.HTable;
82
83 package body Sem_Ch12 is
84
85 ----------------------------------------------------------
86 -- Implementation of Generic Analysis and Instantiation --
87 ----------------------------------------------------------
88
89 -- GNAT implements generics by macro expansion. No attempt is made to share
90 -- generic instantiations (for now). Analysis of a generic definition does
91 -- not perform any expansion action, but the expander must be called on the
92 -- tree for each instantiation, because the expansion may of course depend
93 -- on the generic actuals. All of this is best achieved as follows:
94 --
95 -- a) Semantic analysis of a generic unit is performed on a copy of the
96 -- tree for the generic unit. All tree modifications that follow analysis
97 -- do not affect the original tree. Links are kept between the original
98 -- tree and the copy, in order to recognize non-local references within
99 -- the generic, and propagate them to each instance (recall that name
100 -- resolution is done on the generic declaration: generics are not really
101 -- macros). This is summarized in the following diagram:
102
103 -- .-----------. .----------.
104 -- | semantic |<--------------| generic |
105 -- | copy | | unit |
106 -- | |==============>| |
107 -- |___________| global |__________|
108 -- references | | |
109 -- | | |
110 -- .-----|--|.
111 -- | .-----|---.
112 -- | | .----------.
113 -- | | | generic |
114 -- |__| | |
115 -- |__| instance |
116 -- |__________|
117
118 -- b) Each instantiation copies the original tree, and inserts into it a
119 -- series of declarations that describe the mapping between generic formals
120 -- and actuals. For example, a generic In OUT parameter is an object
121 -- renaming of the corresponding actual, etc. Generic IN parameters are
122 -- constant declarations.
123
124 -- c) In order to give the right visibility for these renamings, we use
125 -- a different scheme for package and subprogram instantiations. For
126 -- packages, the list of renamings is inserted into the package
127 -- specification, before the visible declarations of the package. The
128 -- renamings are analyzed before any of the text of the instance, and are
129 -- thus visible at the right place. Furthermore, outside of the instance,
130 -- the generic parameters are visible and denote their corresponding
131 -- actuals.
132
133 -- For subprograms, we create a container package to hold the renamings
134 -- and the subprogram instance itself. Analysis of the package makes the
135 -- renaming declarations visible to the subprogram. After analyzing the
136 -- package, the defining entity for the subprogram is touched-up so that
137 -- it appears declared in the current scope, and not inside the container
138 -- package.
139
140 -- If the instantiation is a compilation unit, the container package is
141 -- given the same name as the subprogram instance. This ensures that
142 -- the elaboration procedure called by the binder, using the compilation
143 -- unit name, calls in fact the elaboration procedure for the package.
144
145 -- Not surprisingly, private types complicate this approach. By saving in
146 -- the original generic object the non-local references, we guarantee that
147 -- the proper entities are referenced at the point of instantiation.
148 -- However, for private types, this by itself does not insure that the
149 -- proper VIEW of the entity is used (the full type may be visible at the
150 -- point of generic definition, but not at instantiation, or vice-versa).
151 -- In order to reference the proper view, we special-case any reference
152 -- to private types in the generic object, by saving both views, one in
153 -- the generic and one in the semantic copy. At time of instantiation, we
154 -- check whether the two views are consistent, and exchange declarations if
155 -- necessary, in order to restore the correct visibility. Similarly, if
156 -- the instance view is private when the generic view was not, we perform
157 -- the exchange. After completing the instantiation, we restore the
158 -- current visibility. The flag Has_Private_View marks identifiers in the
159 -- the generic unit that require checking.
160
161 -- Visibility within nested generic units requires special handling.
162 -- Consider the following scheme:
163
164 -- type Global is ... -- outside of generic unit.
165 -- generic ...
166 -- package Outer is
167 -- ...
168 -- type Semi_Global is ... -- global to inner.
169
170 -- generic ... -- 1
171 -- procedure inner (X1 : Global; X2 : Semi_Global);
172
173 -- procedure in2 is new inner (...); -- 4
174 -- end Outer;
175
176 -- package New_Outer is new Outer (...); -- 2
177 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
178
179 -- The semantic analysis of Outer captures all occurrences of Global.
180 -- The semantic analysis of Inner (at 1) captures both occurrences of
181 -- Global and Semi_Global.
182
183 -- At point 2 (instantiation of Outer), we also produce a generic copy
184 -- of Inner, even though Inner is, at that point, not being instantiated.
185 -- (This is just part of the semantic analysis of New_Outer).
186
187 -- Critically, references to Global within Inner must be preserved, while
188 -- references to Semi_Global should not preserved, because they must now
189 -- resolve to an entity within New_Outer. To distinguish between these, we
190 -- use a global variable, Current_Instantiated_Parent, which is set when
191 -- performing a generic copy during instantiation (at 2). This variable is
192 -- used when performing a generic copy that is not an instantiation, but
193 -- that is nested within one, as the occurrence of 1 within 2. The analysis
194 -- of a nested generic only preserves references that are global to the
195 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
196 -- determine whether a reference is external to the given parent.
197
198 -- The instantiation at point 3 requires no special treatment. The method
199 -- works as well for further nestings of generic units, but of course the
200 -- variable Current_Instantiated_Parent must be stacked because nested
201 -- instantiations can occur, e.g. the occurrence of 4 within 2.
202
203 -- The instantiation of package and subprogram bodies is handled in a
204 -- similar manner, except that it is delayed until after semantic
205 -- analysis is complete. In this fashion complex cross-dependencies
206 -- between several package declarations and bodies containing generics
207 -- can be compiled which otherwise would diagnose spurious circularities.
208
209 -- For example, it is possible to compile two packages A and B that
210 -- have the following structure:
211
212 -- package A is package B is
213 -- generic ... generic ...
214 -- package G_A is package G_B is
215
216 -- with B; with A;
217 -- package body A is package body B is
218 -- package N_B is new G_B (..) package N_A is new G_A (..)
219
220 -- The table Pending_Instantiations in package Inline is used to keep
221 -- track of body instantiations that are delayed in this manner. Inline
222 -- handles the actual calls to do the body instantiations. This activity
223 -- is part of Inline, since the processing occurs at the same point, and
224 -- for essentially the same reason, as the handling of inlined routines.
225
226 ----------------------------------------------
227 -- Detection of Instantiation Circularities --
228 ----------------------------------------------
229
230 -- If we have a chain of instantiations that is circular, this is static
231 -- error which must be detected at compile time. The detection of these
232 -- circularities is carried out at the point that we insert a generic
233 -- instance spec or body. If there is a circularity, then the analysis of
234 -- the offending spec or body will eventually result in trying to load the
235 -- same unit again, and we detect this problem as we analyze the package
236 -- instantiation for the second time.
237
238 -- At least in some cases after we have detected the circularity, we get
239 -- into trouble if we try to keep going. The following flag is set if a
240 -- circularity is detected, and used to abandon compilation after the
241 -- messages have been posted.
242
243 -----------------------------------------
244 -- Implementation of Generic Contracts --
245 -----------------------------------------
246
247 -- A "contract" is a collection of aspects and pragmas that either verify a
248 -- property of a construct at runtime or classify the data flow to and from
249 -- the construct in some fashion.
250
251 -- Generic packages, subprograms and their respective bodies may be subject
252 -- to the following contract-related aspects or pragmas collectively known
253 -- as annotations:
254
255 -- package subprogram [body]
256 -- Abstract_State Contract_Cases
257 -- Initial_Condition Depends
258 -- Initializes Extensions_Visible
259 -- Global
260 -- package body Post
261 -- Refined_State Post_Class
262 -- Postcondition
263 -- Pre
264 -- Pre_Class
265 -- Precondition
266 -- Refined_Depends
267 -- Refined_Global
268 -- Refined_Post
269 -- Test_Case
270
271 -- Most package contract annotations utilize forward references to classify
272 -- data declared within the package [body]. Subprogram annotations then use
273 -- the classifications to further refine them. These inter dependencies are
274 -- problematic with respect to the implementation of generics because their
275 -- analysis, capture of global references and instantiation does not mesh
276 -- well with the existing mechanism.
277
278 -- 1) Analysis of generic contracts is carried out the same way non-generic
279 -- contracts are analyzed:
280
281 -- 1.1) General rule - a contract is analyzed after all related aspects
282 -- and pragmas are analyzed. This is done by routines
283
284 -- Analyze_Package_Body_Contract
285 -- Analyze_Package_Contract
286 -- Analyze_Subprogram_Body_Contract
287 -- Analyze_Subprogram_Contract
288
289 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
290 -- are processed.
291
292 -- 1.3) Compilation unit body - the contract is analyzed at the end of
293 -- the body declaration list.
294
295 -- 1.4) Package - the contract is analyzed at the end of the private or
296 -- visible declarations, prior to analyzing the contracts of any nested
297 -- packages or subprograms.
298
299 -- 1.5) Package body - the contract is analyzed at the end of the body
300 -- declaration list, prior to analyzing the contracts of any nested
301 -- packages or subprograms.
302
303 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
304 -- package or a subprogram, then its contract is analyzed at the end of
305 -- the enclosing declarations, otherwise the subprogram is a compilation
306 -- unit 1.2).
307
308 -- 1.7) Subprogram body - if the subprogram body is declared inside a
309 -- block, a package body or a subprogram body, then its contract is
310 -- analyzed at the end of the enclosing declarations, otherwise the
311 -- subprogram is a compilation unit 1.3).
312
313 -- 2) Capture of global references within contracts is done after capturing
314 -- global references within the generic template. There are two reasons for
315 -- this delay - pragma annotations are not part of the generic template in
316 -- the case of a generic subprogram declaration, and analysis of contracts
317 -- is delayed.
318
319 -- Contract-related source pragmas within generic templates are prepared
320 -- for delayed capture of global references by routine
321
322 -- Create_Generic_Contract
323
324 -- The routine associates these pragmas with the contract of the template.
325 -- In the case of a generic subprogram declaration, the routine creates
326 -- generic templates for the pragmas declared after the subprogram because
327 -- they are not part of the template.
328
329 -- generic -- template starts
330 -- procedure Gen_Proc (Input : Integer); -- template ends
331 -- pragma Precondition (Input > 0); -- requires own template
332
333 -- 2.1) The capture of global references with aspect specifications and
334 -- source pragmas that apply to a generic unit must be suppressed when
335 -- the generic template is being processed because the contracts have not
336 -- been analyzed yet. Any attempts to capture global references at that
337 -- point will destroy the Associated_Node linkages and leave the template
338 -- undecorated. This delay is controlled by routine
339
340 -- Requires_Delayed_Save
341
342 -- 2.2) The real capture of global references within a contract is done
343 -- after the contract has been analyzed, by routine
344
345 -- Save_Global_References_In_Contract
346
347 -- 3) The instantiation of a generic contract occurs as part of the
348 -- instantiation of the contract owner. Generic subprogram declarations
349 -- require additional processing when the contract is specified by pragmas
350 -- because the pragmas are not part of the generic template. This is done
351 -- by routine
352
353 -- Instantiate_Subprogram_Contract
354
355 Circularity_Detected : Boolean := False;
356 -- This should really be reset on encountering a new main unit, but in
357 -- practice we are not using multiple main units so it is not critical.
358
359 --------------------------------------------------
360 -- Formal packages and partial parameterization --
361 --------------------------------------------------
362
363 -- When compiling a generic, a formal package is a local instantiation. If
364 -- declared with a box, its generic formals are visible in the enclosing
365 -- generic. If declared with a partial list of actuals, those actuals that
366 -- are defaulted (covered by an Others clause, or given an explicit box
367 -- initialization) are also visible in the enclosing generic, while those
368 -- that have a corresponding actual are not.
369
370 -- In our source model of instantiation, the same visibility must be
371 -- present in the spec and body of an instance: the names of the formals
372 -- that are defaulted must be made visible within the instance, and made
373 -- invisible (hidden) after the instantiation is complete, so that they
374 -- are not accessible outside of the instance.
375
376 -- In a generic, a formal package is treated like a special instantiation.
377 -- Our Ada 95 compiler handled formals with and without box in different
378 -- ways. With partial parameterization, we use a single model for both.
379 -- We create a package declaration that consists of the specification of
380 -- the generic package, and a set of declarations that map the actuals
381 -- into local renamings, just as we do for bona fide instantiations. For
382 -- defaulted parameters and formals with a box, we copy directly the
383 -- declarations of the formal into this local package. The result is a
384 -- a package whose visible declarations may include generic formals. This
385 -- package is only used for type checking and visibility analysis, and
386 -- never reaches the back-end, so it can freely violate the placement
387 -- rules for generic formal declarations.
388
389 -- The list of declarations (renamings and copies of formals) is built
390 -- by Analyze_Associations, just as for regular instantiations.
391
392 -- At the point of instantiation, conformance checking must be applied only
393 -- to those parameters that were specified in the formal. We perform this
394 -- checking by creating another internal instantiation, this one including
395 -- only the renamings and the formals (the rest of the package spec is not
396 -- relevant to conformance checking). We can then traverse two lists: the
397 -- list of actuals in the instance that corresponds to the formal package,
398 -- and the list of actuals produced for this bogus instantiation. We apply
399 -- the conformance rules to those actuals that are not defaulted (i.e.
400 -- which still appear as generic formals.
401
402 -- When we compile an instance body we must make the right parameters
403 -- visible again. The predicate Is_Generic_Formal indicates which of the
404 -- formals should have its Is_Hidden flag reset.
405
406 -----------------------
407 -- Local subprograms --
408 -----------------------
409
410 procedure Abandon_Instantiation (N : Node_Id);
411 pragma No_Return (Abandon_Instantiation);
412 -- Posts an error message "instantiation abandoned" at the indicated node
413 -- and then raises the exception Instantiation_Error to do it.
414
415 procedure Analyze_Formal_Array_Type
416 (T : in out Entity_Id;
417 Def : Node_Id);
418 -- A formal array type is treated like an array type declaration, and
419 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
420 -- in-out, because in the case of an anonymous type the entity is
421 -- actually created in the procedure.
422
423 -- The following procedures treat other kinds of formal parameters
424
425 procedure Analyze_Formal_Derived_Interface_Type
426 (N : Node_Id;
427 T : Entity_Id;
428 Def : Node_Id);
429
430 procedure Analyze_Formal_Derived_Type
431 (N : Node_Id;
432 T : Entity_Id;
433 Def : Node_Id);
434
435 procedure Analyze_Formal_Interface_Type
436 (N : Node_Id;
437 T : Entity_Id;
438 Def : Node_Id);
439
440 -- The following subprograms create abbreviated declarations for formal
441 -- scalar types. We introduce an anonymous base of the proper class for
442 -- each of them, and define the formals as constrained first subtypes of
443 -- their bases. The bounds are expressions that are non-static in the
444 -- generic.
445
446 procedure Analyze_Formal_Decimal_Fixed_Point_Type
447 (T : Entity_Id; Def : Node_Id);
448 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
449 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
450 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
451 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
452 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
453 (T : Entity_Id; Def : Node_Id);
454
455 procedure Analyze_Formal_Private_Type
456 (N : Node_Id;
457 T : Entity_Id;
458 Def : Node_Id);
459 -- Creates a new private type, which does not require completion
460
461 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
462 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
463
464 procedure Analyze_Generic_Formal_Part (N : Node_Id);
465 -- Analyze generic formal part
466
467 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
468 -- Create a new access type with the given designated type
469
470 function Analyze_Associations
471 (I_Node : Node_Id;
472 Formals : List_Id;
473 F_Copy : List_Id) return List_Id;
474 -- At instantiation time, build the list of associations between formals
475 -- and actuals. Each association becomes a renaming declaration for the
476 -- formal entity. F_Copy is the analyzed list of formals in the generic
477 -- copy. It is used to apply legality checks to the actuals. I_Node is the
478 -- instantiation node itself.
479
480 procedure Analyze_Subprogram_Instantiation
481 (N : Node_Id;
482 K : Entity_Kind);
483
484 procedure Build_Instance_Compilation_Unit_Nodes
485 (N : Node_Id;
486 Act_Body : Node_Id;
487 Act_Decl : Node_Id);
488 -- This procedure is used in the case where the generic instance of a
489 -- subprogram body or package body is a library unit. In this case, the
490 -- original library unit node for the generic instantiation must be
491 -- replaced by the resulting generic body, and a link made to a new
492 -- compilation unit node for the generic declaration. The argument N is
493 -- the original generic instantiation. Act_Body and Act_Decl are the body
494 -- and declaration of the instance (either package body and declaration
495 -- nodes or subprogram body and declaration nodes depending on the case).
496 -- On return, the node N has been rewritten with the actual body.
497
498 procedure Check_Access_Definition (N : Node_Id);
499 -- Subsidiary routine to null exclusion processing. Perform an assertion
500 -- check on Ada version and the presence of an access definition in N.
501
502 procedure Check_Formal_Packages (P_Id : Entity_Id);
503 -- Apply the following to all formal packages in generic associations
504
505 procedure Check_Formal_Package_Instance
506 (Formal_Pack : Entity_Id;
507 Actual_Pack : Entity_Id);
508 -- Verify that the actuals of the actual instance match the actuals of
509 -- the template for a formal package that is not declared with a box.
510
511 procedure Check_Forward_Instantiation (Decl : Node_Id);
512 -- If the generic is a local entity and the corresponding body has not
513 -- been seen yet, flag enclosing packages to indicate that it will be
514 -- elaborated after the generic body. Subprograms declared in the same
515 -- package cannot be inlined by the front-end because front-end inlining
516 -- requires a strict linear order of elaboration.
517
518 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
519 -- Check if some association between formals and actuals requires to make
520 -- visible primitives of a tagged type, and make those primitives visible.
521 -- Return the list of primitives whose visibility is modified (to restore
522 -- their visibility later through Restore_Hidden_Primitives). If no
523 -- candidate is found then return No_Elist.
524
525 procedure Check_Hidden_Child_Unit
526 (N : Node_Id;
527 Gen_Unit : Entity_Id;
528 Act_Decl_Id : Entity_Id);
529 -- If the generic unit is an implicit child instance within a parent
530 -- instance, we need to make an explicit test that it is not hidden by
531 -- a child instance of the same name and parent.
532
533 procedure Check_Generic_Actuals
534 (Instance : Entity_Id;
535 Is_Formal_Box : Boolean);
536 -- Similar to previous one. Check the actuals in the instantiation,
537 -- whose views can change between the point of instantiation and the point
538 -- of instantiation of the body. In addition, mark the generic renamings
539 -- as generic actuals, so that they are not compatible with other actuals.
540 -- Recurse on an actual that is a formal package whose declaration has
541 -- a box.
542
543 function Contains_Instance_Of
544 (Inner : Entity_Id;
545 Outer : Entity_Id;
546 N : Node_Id) return Boolean;
547 -- Inner is instantiated within the generic Outer. Check whether Inner
548 -- directly or indirectly contains an instance of Outer or of one of its
549 -- parents, in the case of a subunit. Each generic unit holds a list of
550 -- the entities instantiated within (at any depth). This procedure
551 -- determines whether the set of such lists contains a cycle, i.e. an
552 -- illegal circular instantiation.
553
554 function Denotes_Formal_Package
555 (Pack : Entity_Id;
556 On_Exit : Boolean := False;
557 Instance : Entity_Id := Empty) return Boolean;
558 -- Returns True if E is a formal package of an enclosing generic, or
559 -- the actual for such a formal in an enclosing instantiation. If such
560 -- a package is used as a formal in an nested generic, or as an actual
561 -- in a nested instantiation, the visibility of ITS formals should not
562 -- be modified. When called from within Restore_Private_Views, the flag
563 -- On_Exit is true, to indicate that the search for a possible enclosing
564 -- instance should ignore the current one. In that case Instance denotes
565 -- the declaration for which this is an actual. This declaration may be
566 -- an instantiation in the source, or the internal instantiation that
567 -- corresponds to the actual for a formal package.
568
569 function Earlier (N1, N2 : Node_Id) return Boolean;
570 -- Yields True if N1 and N2 appear in the same compilation unit,
571 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
572 -- traversal of the tree for the unit. Used to determine the placement
573 -- of freeze nodes for instance bodies that may depend on other instances.
574
575 function Find_Actual_Type
576 (Typ : Entity_Id;
577 Gen_Type : Entity_Id) return Entity_Id;
578 -- When validating the actual types of a child instance, check whether
579 -- the formal is a formal type of the parent unit, and retrieve the current
580 -- actual for it. Typ is the entity in the analyzed formal type declaration
581 -- (component or index type of an array type, or designated type of an
582 -- access formal) and Gen_Type is the enclosing analyzed formal array
583 -- or access type. The desired actual may be a formal of a parent, or may
584 -- be declared in a formal package of a parent. In both cases it is a
585 -- generic actual type because it appears within a visible instance.
586 -- Finally, it may be declared in a parent unit without being a formal
587 -- of that unit, in which case it must be retrieved by visibility.
588 -- Ambiguities may still arise if two homonyms are declared in two formal
589 -- packages, and the prefix of the formal type may be needed to resolve
590 -- the ambiguity in the instance ???
591
592 procedure Freeze_Subprogram_Body
593 (Inst_Node : Node_Id;
594 Gen_Body : Node_Id;
595 Pack_Id : Entity_Id);
596 -- The generic body may appear textually after the instance, including
597 -- in the proper body of a stub, or within a different package instance.
598 -- Given that the instance can only be elaborated after the generic, we
599 -- place freeze_nodes for the instance and/or for packages that may enclose
600 -- the instance and the generic, so that the back-end can establish the
601 -- proper order of elaboration.
602
603 function Get_Associated_Node (N : Node_Id) return Node_Id;
604 -- In order to propagate semantic information back from the analyzed copy
605 -- to the original generic, we maintain links between selected nodes in the
606 -- generic and their corresponding copies. At the end of generic analysis,
607 -- the routine Save_Global_References traverses the generic tree, examines
608 -- the semantic information, and preserves the links to those nodes that
609 -- contain global information. At instantiation, the information from the
610 -- associated node is placed on the new copy, so that name resolution is
611 -- not repeated.
612 --
613 -- Three kinds of source nodes have associated nodes:
614 --
615 -- a) those that can reference (denote) entities, that is identifiers,
616 -- character literals, expanded_names, operator symbols, operators,
617 -- and attribute reference nodes. These nodes have an Entity field
618 -- and are the set of nodes that are in N_Has_Entity.
619 --
620 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
621 --
622 -- c) selected components (N_Selected_Component)
623 --
624 -- For the first class, the associated node preserves the entity if it is
625 -- global. If the generic contains nested instantiations, the associated
626 -- node itself has been recopied, and a chain of them must be followed.
627 --
628 -- For aggregates, the associated node allows retrieval of the type, which
629 -- may otherwise not appear in the generic. The view of this type may be
630 -- different between generic and instantiation, and the full view can be
631 -- installed before the instantiation is analyzed. For aggregates of type
632 -- extensions, the same view exchange may have to be performed for some of
633 -- the ancestor types, if their view is private at the point of
634 -- instantiation.
635 --
636 -- Nodes that are selected components in the parse tree may be rewritten
637 -- as expanded names after resolution, and must be treated as potential
638 -- entity holders, which is why they also have an Associated_Node.
639 --
640 -- Nodes that do not come from source, such as freeze nodes, do not appear
641 -- in the generic tree, and need not have an associated node.
642 --
643 -- The associated node is stored in the Associated_Node field. Note that
644 -- this field overlaps Entity, which is fine, because the whole point is
645 -- that we don't need or want the normal Entity field in this situation.
646
647 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
648 -- Traverse the Exchanged_Views list to see if a type was private
649 -- and has already been flipped during this phase of instantiation.
650
651 procedure Hide_Current_Scope;
652 -- When instantiating a generic child unit, the parent context must be
653 -- present, but the instance and all entities that may be generated
654 -- must be inserted in the current scope. We leave the current scope
655 -- on the stack, but make its entities invisible to avoid visibility
656 -- problems. This is reversed at the end of the instantiation. This is
657 -- not done for the instantiation of the bodies, which only require the
658 -- instances of the generic parents to be in scope.
659
660 function In_Same_Declarative_Part
661 (F_Node : Node_Id;
662 Inst : Node_Id) return Boolean;
663 -- True if the instantiation Inst and the given freeze_node F_Node appear
664 -- within the same declarative part, ignoring subunits, but with no inter-
665 -- vening subprograms or concurrent units. Used to find the proper plave
666 -- for the freeze node of an instance, when the generic is declared in a
667 -- previous instance. If predicate is true, the freeze node of the instance
668 -- can be placed after the freeze node of the previous instance, Otherwise
669 -- it has to be placed at the end of the current declarative part.
670
671 function In_Main_Context (E : Entity_Id) return Boolean;
672 -- Check whether an instantiation is in the context of the main unit.
673 -- Used to determine whether its body should be elaborated to allow
674 -- front-end inlining.
675
676 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
677 -- Add the context clause of the unit containing a generic unit to a
678 -- compilation unit that is, or contains, an instantiation.
679
680 procedure Init_Env;
681 -- Establish environment for subsequent instantiation. Separated from
682 -- Save_Env because data-structures for visibility handling must be
683 -- initialized before call to Check_Generic_Child_Unit.
684
685 procedure Inline_Instance_Body
686 (N : Node_Id;
687 Gen_Unit : Entity_Id;
688 Act_Decl : Node_Id);
689 -- If front-end inlining is requested, instantiate the package body,
690 -- and preserve the visibility of its compilation unit, to insure
691 -- that successive instantiations succeed.
692
693 procedure Insert_Freeze_Node_For_Instance
694 (N : Node_Id;
695 F_Node : Node_Id);
696 -- N denotes a package or a subprogram instantiation and F_Node is the
697 -- associated freeze node. Insert the freeze node before the first source
698 -- body which follows immediately after N. If no such body is found, the
699 -- freeze node is inserted at the end of the declarative region which
700 -- contains N.
701
702 procedure Install_Body
703 (Act_Body : Node_Id;
704 N : Node_Id;
705 Gen_Body : Node_Id;
706 Gen_Decl : Node_Id);
707 -- If the instantiation happens textually before the body of the generic,
708 -- the instantiation of the body must be analyzed after the generic body,
709 -- and not at the point of instantiation. Such early instantiations can
710 -- happen if the generic and the instance appear in a package declaration
711 -- because the generic body can only appear in the corresponding package
712 -- body. Early instantiations can also appear if generic, instance and
713 -- body are all in the declarative part of a subprogram or entry. Entities
714 -- of packages that are early instantiations are delayed, and their freeze
715 -- node appears after the generic body.
716
717 procedure Install_Formal_Packages (Par : Entity_Id);
718 -- Install the visible part of any formal of the parent that is a formal
719 -- package. Note that for the case of a formal package with a box, this
720 -- includes the formal part of the formal package (12.7(10/2)).
721
722 procedure Install_Hidden_Primitives
723 (Prims_List : in out Elist_Id;
724 Gen_T : Entity_Id;
725 Act_T : Entity_Id);
726 -- Remove suffix 'P' from hidden primitives of Act_T to match the
727 -- visibility of primitives of Gen_T. The list of primitives to which
728 -- the suffix is removed is added to Prims_List to restore them later.
729
730 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
731 -- When compiling an instance of a child unit the parent (which is
732 -- itself an instance) is an enclosing scope that must be made
733 -- immediately visible. This procedure is also used to install the non-
734 -- generic parent of a generic child unit when compiling its body, so
735 -- that full views of types in the parent are made visible.
736
737 -- The functions Instantiate_XXX perform various legality checks and build
738 -- the declarations for instantiated generic parameters. In all of these
739 -- Formal is the entity in the generic unit, Actual is the entity of
740 -- expression in the generic associations, and Analyzed_Formal is the
741 -- formal in the generic copy, which contains the semantic information to
742 -- be used to validate the actual.
743
744 function Instantiate_Object
745 (Formal : Node_Id;
746 Actual : Node_Id;
747 Analyzed_Formal : Node_Id) return List_Id;
748
749 function Instantiate_Type
750 (Formal : Node_Id;
751 Actual : Node_Id;
752 Analyzed_Formal : Node_Id;
753 Actual_Decls : List_Id) return List_Id;
754
755 function Instantiate_Formal_Subprogram
756 (Formal : Node_Id;
757 Actual : Node_Id;
758 Analyzed_Formal : Node_Id) return Node_Id;
759
760 function Instantiate_Formal_Package
761 (Formal : Node_Id;
762 Actual : Node_Id;
763 Analyzed_Formal : Node_Id) return List_Id;
764 -- If the formal package is declared with a box, special visibility rules
765 -- apply to its formals: they are in the visible part of the package. This
766 -- is true in the declarative region of the formal package, that is to say
767 -- in the enclosing generic or instantiation. For an instantiation, the
768 -- parameters of the formal package are made visible in an explicit step.
769 -- Furthermore, if the actual has a visible USE clause, these formals must
770 -- be made potentially use-visible as well. On exit from the enclosing
771 -- instantiation, the reverse must be done.
772
773 -- For a formal package declared without a box, there are conformance rules
774 -- that apply to the actuals in the generic declaration and the actuals of
775 -- the actual package in the enclosing instantiation. The simplest way to
776 -- apply these rules is to repeat the instantiation of the formal package
777 -- in the context of the enclosing instance, and compare the generic
778 -- associations of this instantiation with those of the actual package.
779 -- This internal instantiation only needs to contain the renamings of the
780 -- formals: the visible and private declarations themselves need not be
781 -- created.
782
783 -- In Ada 2005, the formal package may be only partially parameterized.
784 -- In that case the visibility step must make visible those actuals whose
785 -- corresponding formals were given with a box. A final complication
786 -- involves inherited operations from formal derived types, which must
787 -- be visible if the type is.
788
789 function Is_In_Main_Unit (N : Node_Id) return Boolean;
790 -- Test if given node is in the main unit
791
792 procedure Load_Parent_Of_Generic
793 (N : Node_Id;
794 Spec : Node_Id;
795 Body_Optional : Boolean := False);
796 -- If the generic appears in a separate non-generic library unit, load the
797 -- corresponding body to retrieve the body of the generic. N is the node
798 -- for the generic instantiation, Spec is the generic package declaration.
799 --
800 -- Body_Optional is a flag that indicates that the body is being loaded to
801 -- ensure that temporaries are generated consistently when there are other
802 -- instances in the current declarative part that precede the one being
803 -- loaded. In that case a missing body is acceptable.
804
805 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
806 -- Within the generic part, entities in the formal package are
807 -- visible. To validate subsequent type declarations, indicate
808 -- the correspondence between the entities in the analyzed formal,
809 -- and the entities in the actual package. There are three packages
810 -- involved in the instantiation of a formal package: the parent
811 -- generic P1 which appears in the generic declaration, the fake
812 -- instantiation P2 which appears in the analyzed generic, and whose
813 -- visible entities may be used in subsequent formals, and the actual
814 -- P3 in the instance. To validate subsequent formals, me indicate
815 -- that the entities in P2 are mapped into those of P3. The mapping of
816 -- entities has to be done recursively for nested packages.
817
818 procedure Move_Freeze_Nodes
819 (Out_Of : Entity_Id;
820 After : Node_Id;
821 L : List_Id);
822 -- Freeze nodes can be generated in the analysis of a generic unit, but
823 -- will not be seen by the back-end. It is necessary to move those nodes
824 -- to the enclosing scope if they freeze an outer entity. We place them
825 -- at the end of the enclosing generic package, which is semantically
826 -- neutral.
827
828 procedure Preanalyze_Actuals (N : Node_Id);
829 -- Analyze actuals to perform name resolution. Full resolution is done
830 -- later, when the expected types are known, but names have to be captured
831 -- before installing parents of generics, that are not visible for the
832 -- actuals themselves.
833
834 procedure Remove_Parent (In_Body : Boolean := False);
835 -- Reverse effect after instantiation of child is complete
836
837 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
838 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
839 -- set to No_Elist.
840
841 procedure Save_Global_References_In_Aspects (N : Node_Id);
842 -- Save all global references found within the expressions of all aspects
843 -- that appear on node N.
844
845 procedure Set_Instance_Env
846 (Gen_Unit : Entity_Id;
847 Act_Unit : Entity_Id);
848 -- Save current instance on saved environment, to be used to determine
849 -- the global status of entities in nested instances. Part of Save_Env.
850 -- called after verifying that the generic unit is legal for the instance,
851 -- The procedure also examines whether the generic unit is a predefined
852 -- unit, in order to set configuration switches accordingly. As a result
853 -- the procedure must be called after analyzing and freezing the actuals.
854
855 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
856 -- Associate analyzed generic parameter with corresponding instance. Used
857 -- for semantic checks at instantiation time.
858
859 function True_Parent (N : Node_Id) return Node_Id;
860 -- For a subunit, return parent of corresponding stub, else return
861 -- parent of node.
862
863 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
864 -- Verify that an attribute that appears as the default for a formal
865 -- subprogram is a function or procedure with the correct profile.
866
867 -------------------------------------------
868 -- Data Structures for Generic Renamings --
869 -------------------------------------------
870
871 -- The map Generic_Renamings associates generic entities with their
872 -- corresponding actuals. Currently used to validate type instances. It
873 -- will eventually be used for all generic parameters to eliminate the
874 -- need for overload resolution in the instance.
875
876 type Assoc_Ptr is new Int;
877
878 Assoc_Null : constant Assoc_Ptr := -1;
879
880 type Assoc is record
881 Gen_Id : Entity_Id;
882 Act_Id : Entity_Id;
883 Next_In_HTable : Assoc_Ptr;
884 end record;
885
886 package Generic_Renamings is new Table.Table
887 (Table_Component_Type => Assoc,
888 Table_Index_Type => Assoc_Ptr,
889 Table_Low_Bound => 0,
890 Table_Initial => 10,
891 Table_Increment => 100,
892 Table_Name => "Generic_Renamings");
893
894 -- Variable to hold enclosing instantiation. When the environment is
895 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
896
897 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
898
899 -- Hash table for associations
900
901 HTable_Size : constant := 37;
902 type HTable_Range is range 0 .. HTable_Size - 1;
903
904 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
905 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
906 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
907 function Hash (F : Entity_Id) return HTable_Range;
908
909 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
910 Header_Num => HTable_Range,
911 Element => Assoc,
912 Elmt_Ptr => Assoc_Ptr,
913 Null_Ptr => Assoc_Null,
914 Set_Next => Set_Next_Assoc,
915 Next => Next_Assoc,
916 Key => Entity_Id,
917 Get_Key => Get_Gen_Id,
918 Hash => Hash,
919 Equal => "=");
920
921 Exchanged_Views : Elist_Id;
922 -- This list holds the private views that have been exchanged during
923 -- instantiation to restore the visibility of the generic declaration.
924 -- (see comments above). After instantiation, the current visibility is
925 -- reestablished by means of a traversal of this list.
926
927 Hidden_Entities : Elist_Id;
928 -- This list holds the entities of the current scope that are removed
929 -- from immediate visibility when instantiating a child unit. Their
930 -- visibility is restored in Remove_Parent.
931
932 -- Because instantiations can be recursive, the following must be saved
933 -- on entry and restored on exit from an instantiation (spec or body).
934 -- This is done by the two procedures Save_Env and Restore_Env. For
935 -- package and subprogram instantiations (but not for the body instances)
936 -- the action of Save_Env is done in two steps: Init_Env is called before
937 -- Check_Generic_Child_Unit, because setting the parent instances requires
938 -- that the visibility data structures be properly initialized. Once the
939 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
940
941 Parent_Unit_Visible : Boolean := False;
942 -- Parent_Unit_Visible is used when the generic is a child unit, and
943 -- indicates whether the ultimate parent of the generic is visible in the
944 -- instantiation environment. It is used to reset the visibility of the
945 -- parent at the end of the instantiation (see Remove_Parent).
946
947 Instance_Parent_Unit : Entity_Id := Empty;
948 -- This records the ultimate parent unit of an instance of a generic
949 -- child unit and is used in conjunction with Parent_Unit_Visible to
950 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
951
952 type Instance_Env is record
953 Instantiated_Parent : Assoc;
954 Exchanged_Views : Elist_Id;
955 Hidden_Entities : Elist_Id;
956 Current_Sem_Unit : Unit_Number_Type;
957 Parent_Unit_Visible : Boolean := False;
958 Instance_Parent_Unit : Entity_Id := Empty;
959 Switches : Config_Switches_Type;
960 end record;
961
962 package Instance_Envs is new Table.Table (
963 Table_Component_Type => Instance_Env,
964 Table_Index_Type => Int,
965 Table_Low_Bound => 0,
966 Table_Initial => 32,
967 Table_Increment => 100,
968 Table_Name => "Instance_Envs");
969
970 procedure Restore_Private_Views
971 (Pack_Id : Entity_Id;
972 Is_Package : Boolean := True);
973 -- Restore the private views of external types, and unmark the generic
974 -- renamings of actuals, so that they become compatible subtypes again.
975 -- For subprograms, Pack_Id is the package constructed to hold the
976 -- renamings.
977
978 procedure Switch_View (T : Entity_Id);
979 -- Switch the partial and full views of a type and its private
980 -- dependents (i.e. its subtypes and derived types).
981
982 ------------------------------------
983 -- Structures for Error Reporting --
984 ------------------------------------
985
986 Instantiation_Node : Node_Id;
987 -- Used by subprograms that validate instantiation of formal parameters
988 -- where there might be no actual on which to place the error message.
989 -- Also used to locate the instantiation node for generic subunits.
990
991 Instantiation_Error : exception;
992 -- When there is a semantic error in the generic parameter matching,
993 -- there is no point in continuing the instantiation, because the
994 -- number of cascaded errors is unpredictable. This exception aborts
995 -- the instantiation process altogether.
996
997 S_Adjustment : Sloc_Adjustment;
998 -- Offset created for each node in an instantiation, in order to keep
999 -- track of the source position of the instantiation in each of its nodes.
1000 -- A subsequent semantic error or warning on a construct of the instance
1001 -- points to both places: the original generic node, and the point of
1002 -- instantiation. See Sinput and Sinput.L for additional details.
1003
1004 ------------------------------------------------------------
1005 -- Data structure for keeping track when inside a Generic --
1006 ------------------------------------------------------------
1007
1008 -- The following table is used to save values of the Inside_A_Generic
1009 -- flag (see spec of Sem) when they are saved by Start_Generic.
1010
1011 package Generic_Flags is new Table.Table (
1012 Table_Component_Type => Boolean,
1013 Table_Index_Type => Int,
1014 Table_Low_Bound => 0,
1015 Table_Initial => 32,
1016 Table_Increment => 200,
1017 Table_Name => "Generic_Flags");
1018
1019 ---------------------------
1020 -- Abandon_Instantiation --
1021 ---------------------------
1022
1023 procedure Abandon_Instantiation (N : Node_Id) is
1024 begin
1025 Error_Msg_N ("\instantiation abandoned!", N);
1026 raise Instantiation_Error;
1027 end Abandon_Instantiation;
1028
1029 --------------------------
1030 -- Analyze_Associations --
1031 --------------------------
1032
1033 function Analyze_Associations
1034 (I_Node : Node_Id;
1035 Formals : List_Id;
1036 F_Copy : List_Id) return List_Id
1037 is
1038 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1039 Assoc : constant List_Id := New_List;
1040 Default_Actuals : constant List_Id := New_List;
1041 Gen_Unit : constant Entity_Id :=
1042 Defining_Entity (Parent (F_Copy));
1043
1044 Actuals : List_Id;
1045 Actual : Node_Id;
1046 Analyzed_Formal : Node_Id;
1047 First_Named : Node_Id := Empty;
1048 Formal : Node_Id;
1049 Match : Node_Id;
1050 Named : Node_Id;
1051 Saved_Formal : Node_Id;
1052
1053 Default_Formals : constant List_Id := New_List;
1054 -- If an Others_Choice is present, some of the formals may be defaulted.
1055 -- To simplify the treatment of visibility in an instance, we introduce
1056 -- individual defaults for each such formal. These defaults are
1057 -- appended to the list of associations and replace the Others_Choice.
1058
1059 Found_Assoc : Node_Id;
1060 -- Association for the current formal being match. Empty if there are
1061 -- no remaining actuals, or if there is no named association with the
1062 -- name of the formal.
1063
1064 Is_Named_Assoc : Boolean;
1065 Num_Matched : Int := 0;
1066 Num_Actuals : Int := 0;
1067
1068 Others_Present : Boolean := False;
1069 Others_Choice : Node_Id := Empty;
1070 -- In Ada 2005, indicates partial parameterization of a formal
1071 -- package. As usual an other association must be last in the list.
1072
1073 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1074 -- Apply RM 12.3 (9): if a formal subprogram is overloaded, the instance
1075 -- cannot have a named association for it. AI05-0025 extends this rule
1076 -- to formals of formal packages by AI05-0025, and it also applies to
1077 -- box-initialized formals.
1078
1079 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1080 -- Determine whether the parameter types and the return type of Subp
1081 -- are fully defined at the point of instantiation.
1082
1083 function Matching_Actual
1084 (F : Entity_Id;
1085 A_F : Entity_Id) return Node_Id;
1086 -- Find actual that corresponds to a given a formal parameter. If the
1087 -- actuals are positional, return the next one, if any. If the actuals
1088 -- are named, scan the parameter associations to find the right one.
1089 -- A_F is the corresponding entity in the analyzed generic,which is
1090 -- placed on the selector name for ASIS use.
1091 --
1092 -- In Ada 2005, a named association may be given with a box, in which
1093 -- case Matching_Actual sets Found_Assoc to the generic association,
1094 -- but return Empty for the actual itself. In this case the code below
1095 -- creates a corresponding declaration for the formal.
1096
1097 function Partial_Parameterization return Boolean;
1098 -- Ada 2005: if no match is found for a given formal, check if the
1099 -- association for it includes a box, or whether the associations
1100 -- include an Others clause.
1101
1102 procedure Process_Default (F : Entity_Id);
1103 -- Add a copy of the declaration of generic formal F to the list of
1104 -- associations, and add an explicit box association for F if there
1105 -- is none yet, and the default comes from an Others_Choice.
1106
1107 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1108 -- Determine whether Subp renames one of the subprograms defined in the
1109 -- generated package Standard.
1110
1111 procedure Set_Analyzed_Formal;
1112 -- Find the node in the generic copy that corresponds to a given formal.
1113 -- The semantic information on this node is used to perform legality
1114 -- checks on the actuals. Because semantic analysis can introduce some
1115 -- anonymous entities or modify the declaration node itself, the
1116 -- correspondence between the two lists is not one-one. In addition to
1117 -- anonymous types, the presence a formal equality will introduce an
1118 -- implicit declaration for the corresponding inequality.
1119
1120 ----------------------------------------
1121 -- Check_Overloaded_Formal_Subprogram --
1122 ----------------------------------------
1123
1124 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1125 Temp_Formal : Entity_Id;
1126
1127 begin
1128 Temp_Formal := First (Formals);
1129 while Present (Temp_Formal) loop
1130 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1131 and then Temp_Formal /= Formal
1132 and then
1133 Chars (Defining_Unit_Name (Specification (Formal))) =
1134 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1135 then
1136 if Present (Found_Assoc) then
1137 Error_Msg_N
1138 ("named association not allowed for overloaded formal",
1139 Found_Assoc);
1140
1141 else
1142 Error_Msg_N
1143 ("named association not allowed for overloaded formal",
1144 Others_Choice);
1145 end if;
1146
1147 Abandon_Instantiation (Instantiation_Node);
1148 end if;
1149
1150 Next (Temp_Formal);
1151 end loop;
1152 end Check_Overloaded_Formal_Subprogram;
1153
1154 -------------------------------
1155 -- Has_Fully_Defined_Profile --
1156 -------------------------------
1157
1158 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1159 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1160 -- Determine whethet type Typ is fully defined
1161
1162 ---------------------------
1163 -- Is_Fully_Defined_Type --
1164 ---------------------------
1165
1166 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1167 begin
1168 -- A private type without a full view is not fully defined
1169
1170 if Is_Private_Type (Typ)
1171 and then No (Full_View (Typ))
1172 then
1173 return False;
1174
1175 -- An incomplete type is never fully defined
1176
1177 elsif Is_Incomplete_Type (Typ) then
1178 return False;
1179
1180 -- All other types are fully defined
1181
1182 else
1183 return True;
1184 end if;
1185 end Is_Fully_Defined_Type;
1186
1187 -- Local declarations
1188
1189 Param : Entity_Id;
1190
1191 -- Start of processing for Has_Fully_Defined_Profile
1192
1193 begin
1194 -- Check the parameters
1195
1196 Param := First_Formal (Subp);
1197 while Present (Param) loop
1198 if not Is_Fully_Defined_Type (Etype (Param)) then
1199 return False;
1200 end if;
1201
1202 Next_Formal (Param);
1203 end loop;
1204
1205 -- Check the return type
1206
1207 return Is_Fully_Defined_Type (Etype (Subp));
1208 end Has_Fully_Defined_Profile;
1209
1210 ---------------------
1211 -- Matching_Actual --
1212 ---------------------
1213
1214 function Matching_Actual
1215 (F : Entity_Id;
1216 A_F : Entity_Id) return Node_Id
1217 is
1218 Prev : Node_Id;
1219 Act : Node_Id;
1220
1221 begin
1222 Is_Named_Assoc := False;
1223
1224 -- End of list of purely positional parameters
1225
1226 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1227 Found_Assoc := Empty;
1228 Act := Empty;
1229
1230 -- Case of positional parameter corresponding to current formal
1231
1232 elsif No (Selector_Name (Actual)) then
1233 Found_Assoc := Actual;
1234 Act := Explicit_Generic_Actual_Parameter (Actual);
1235 Num_Matched := Num_Matched + 1;
1236 Next (Actual);
1237
1238 -- Otherwise scan list of named actuals to find the one with the
1239 -- desired name. All remaining actuals have explicit names.
1240
1241 else
1242 Is_Named_Assoc := True;
1243 Found_Assoc := Empty;
1244 Act := Empty;
1245 Prev := Empty;
1246
1247 while Present (Actual) loop
1248 if Chars (Selector_Name (Actual)) = Chars (F) then
1249 Set_Entity (Selector_Name (Actual), A_F);
1250 Set_Etype (Selector_Name (Actual), Etype (A_F));
1251 Generate_Reference (A_F, Selector_Name (Actual));
1252 Found_Assoc := Actual;
1253 Act := Explicit_Generic_Actual_Parameter (Actual);
1254 Num_Matched := Num_Matched + 1;
1255 exit;
1256 end if;
1257
1258 Prev := Actual;
1259 Next (Actual);
1260 end loop;
1261
1262 -- Reset for subsequent searches. In most cases the named
1263 -- associations are in order. If they are not, we reorder them
1264 -- to avoid scanning twice the same actual. This is not just a
1265 -- question of efficiency: there may be multiple defaults with
1266 -- boxes that have the same name. In a nested instantiation we
1267 -- insert actuals for those defaults, and cannot rely on their
1268 -- names to disambiguate them.
1269
1270 if Actual = First_Named then
1271 Next (First_Named);
1272
1273 elsif Present (Actual) then
1274 Insert_Before (First_Named, Remove_Next (Prev));
1275 end if;
1276
1277 Actual := First_Named;
1278 end if;
1279
1280 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1281 Set_Used_As_Generic_Actual (Entity (Act));
1282 end if;
1283
1284 return Act;
1285 end Matching_Actual;
1286
1287 ------------------------------
1288 -- Partial_Parameterization --
1289 ------------------------------
1290
1291 function Partial_Parameterization return Boolean is
1292 begin
1293 return Others_Present
1294 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1295 end Partial_Parameterization;
1296
1297 ---------------------
1298 -- Process_Default --
1299 ---------------------
1300
1301 procedure Process_Default (F : Entity_Id) is
1302 Loc : constant Source_Ptr := Sloc (I_Node);
1303 F_Id : constant Entity_Id := Defining_Entity (F);
1304 Decl : Node_Id;
1305 Default : Node_Id;
1306 Id : Entity_Id;
1307
1308 begin
1309 -- Append copy of formal declaration to associations, and create new
1310 -- defining identifier for it.
1311
1312 Decl := New_Copy_Tree (F);
1313 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1314
1315 if Nkind (F) in N_Formal_Subprogram_Declaration then
1316 Set_Defining_Unit_Name (Specification (Decl), Id);
1317
1318 else
1319 Set_Defining_Identifier (Decl, Id);
1320 end if;
1321
1322 Append (Decl, Assoc);
1323
1324 if No (Found_Assoc) then
1325 Default :=
1326 Make_Generic_Association (Loc,
1327 Selector_Name =>
1328 New_Occurrence_Of (Id, Loc),
1329 Explicit_Generic_Actual_Parameter => Empty);
1330 Set_Box_Present (Default);
1331 Append (Default, Default_Formals);
1332 end if;
1333 end Process_Default;
1334
1335 ---------------------------------
1336 -- Renames_Standard_Subprogram --
1337 ---------------------------------
1338
1339 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1340 Id : Entity_Id;
1341
1342 begin
1343 Id := Alias (Subp);
1344 while Present (Id) loop
1345 if Scope (Id) = Standard_Standard then
1346 return True;
1347 end if;
1348
1349 Id := Alias (Id);
1350 end loop;
1351
1352 return False;
1353 end Renames_Standard_Subprogram;
1354
1355 -------------------------
1356 -- Set_Analyzed_Formal --
1357 -------------------------
1358
1359 procedure Set_Analyzed_Formal is
1360 Kind : Node_Kind;
1361
1362 begin
1363 while Present (Analyzed_Formal) loop
1364 Kind := Nkind (Analyzed_Formal);
1365
1366 case Nkind (Formal) is
1367
1368 when N_Formal_Subprogram_Declaration =>
1369 exit when Kind in N_Formal_Subprogram_Declaration
1370 and then
1371 Chars
1372 (Defining_Unit_Name (Specification (Formal))) =
1373 Chars
1374 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1375
1376 when N_Formal_Package_Declaration =>
1377 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1378 N_Generic_Package_Declaration,
1379 N_Package_Declaration);
1380
1381 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1382
1383 when others =>
1384
1385 -- Skip freeze nodes, and nodes inserted to replace
1386 -- unrecognized pragmas.
1387
1388 exit when
1389 Kind not in N_Formal_Subprogram_Declaration
1390 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1391 N_Freeze_Entity,
1392 N_Null_Statement,
1393 N_Itype_Reference)
1394 and then Chars (Defining_Identifier (Formal)) =
1395 Chars (Defining_Identifier (Analyzed_Formal));
1396 end case;
1397
1398 Next (Analyzed_Formal);
1399 end loop;
1400 end Set_Analyzed_Formal;
1401
1402 -- Start of processing for Analyze_Associations
1403
1404 begin
1405 Actuals := Generic_Associations (I_Node);
1406
1407 if Present (Actuals) then
1408
1409 -- Check for an Others choice, indicating a partial parameterization
1410 -- for a formal package.
1411
1412 Actual := First (Actuals);
1413 while Present (Actual) loop
1414 if Nkind (Actual) = N_Others_Choice then
1415 Others_Present := True;
1416 Others_Choice := Actual;
1417
1418 if Present (Next (Actual)) then
1419 Error_Msg_N ("others must be last association", Actual);
1420 end if;
1421
1422 -- This subprogram is used both for formal packages and for
1423 -- instantiations. For the latter, associations must all be
1424 -- explicit.
1425
1426 if Nkind (I_Node) /= N_Formal_Package_Declaration
1427 and then Comes_From_Source (I_Node)
1428 then
1429 Error_Msg_N
1430 ("others association not allowed in an instance",
1431 Actual);
1432 end if;
1433
1434 -- In any case, nothing to do after the others association
1435
1436 exit;
1437
1438 elsif Box_Present (Actual)
1439 and then Comes_From_Source (I_Node)
1440 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1441 then
1442 Error_Msg_N
1443 ("box association not allowed in an instance", Actual);
1444 end if;
1445
1446 Next (Actual);
1447 end loop;
1448
1449 -- If named associations are present, save first named association
1450 -- (it may of course be Empty) to facilitate subsequent name search.
1451
1452 First_Named := First (Actuals);
1453 while Present (First_Named)
1454 and then Nkind (First_Named) /= N_Others_Choice
1455 and then No (Selector_Name (First_Named))
1456 loop
1457 Num_Actuals := Num_Actuals + 1;
1458 Next (First_Named);
1459 end loop;
1460 end if;
1461
1462 Named := First_Named;
1463 while Present (Named) loop
1464 if Nkind (Named) /= N_Others_Choice
1465 and then No (Selector_Name (Named))
1466 then
1467 Error_Msg_N ("invalid positional actual after named one", Named);
1468 Abandon_Instantiation (Named);
1469 end if;
1470
1471 -- A named association may lack an actual parameter, if it was
1472 -- introduced for a default subprogram that turns out to be local
1473 -- to the outer instantiation.
1474
1475 if Nkind (Named) /= N_Others_Choice
1476 and then Present (Explicit_Generic_Actual_Parameter (Named))
1477 then
1478 Num_Actuals := Num_Actuals + 1;
1479 end if;
1480
1481 Next (Named);
1482 end loop;
1483
1484 if Present (Formals) then
1485 Formal := First_Non_Pragma (Formals);
1486 Analyzed_Formal := First_Non_Pragma (F_Copy);
1487
1488 if Present (Actuals) then
1489 Actual := First (Actuals);
1490
1491 -- All formals should have default values
1492
1493 else
1494 Actual := Empty;
1495 end if;
1496
1497 while Present (Formal) loop
1498 Set_Analyzed_Formal;
1499 Saved_Formal := Next_Non_Pragma (Formal);
1500
1501 case Nkind (Formal) is
1502 when N_Formal_Object_Declaration =>
1503 Match :=
1504 Matching_Actual
1505 (Defining_Identifier (Formal),
1506 Defining_Identifier (Analyzed_Formal));
1507
1508 if No (Match) and then Partial_Parameterization then
1509 Process_Default (Formal);
1510
1511 else
1512 Append_List
1513 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1514 Assoc);
1515
1516 -- For a defaulted in_parameter, create an entry in the
1517 -- the list of defaulted actuals, for GNATProve use. Do
1518 -- not included these defaults for an instance nested
1519 -- within a generic, because the defaults are also used
1520 -- in the analysis of the enclosing generic, and only
1521 -- defaulted subprograms are relevant there.
1522
1523 if No (Match) and then not Inside_A_Generic then
1524 Append_To (Default_Actuals,
1525 Make_Generic_Association (Sloc (I_Node),
1526 Selector_Name =>
1527 New_Occurrence_Of
1528 (Defining_Identifier (Formal), Sloc (I_Node)),
1529 Explicit_Generic_Actual_Parameter =>
1530 New_Copy_Tree (Default_Expression (Formal))));
1531 end if;
1532 end if;
1533
1534 -- If the object is a call to an expression function, this
1535 -- is a freezing point for it.
1536
1537 if Is_Entity_Name (Match)
1538 and then Present (Entity (Match))
1539 and then Nkind
1540 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1541 = N_Expression_Function
1542 then
1543 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1544 end if;
1545
1546 when N_Formal_Type_Declaration =>
1547 Match :=
1548 Matching_Actual
1549 (Defining_Identifier (Formal),
1550 Defining_Identifier (Analyzed_Formal));
1551
1552 if No (Match) then
1553 if Partial_Parameterization then
1554 Process_Default (Formal);
1555
1556 else
1557 Error_Msg_Sloc := Sloc (Gen_Unit);
1558 Error_Msg_NE
1559 ("missing actual&",
1560 Instantiation_Node, Defining_Identifier (Formal));
1561 Error_Msg_NE
1562 ("\in instantiation of & declared#",
1563 Instantiation_Node, Gen_Unit);
1564 Abandon_Instantiation (Instantiation_Node);
1565 end if;
1566
1567 else
1568 Analyze (Match);
1569 Append_List
1570 (Instantiate_Type
1571 (Formal, Match, Analyzed_Formal, Assoc),
1572 Assoc);
1573
1574 -- An instantiation is a freeze point for the actuals,
1575 -- unless this is a rewritten formal package, or the
1576 -- formal is an Ada 2012 formal incomplete type.
1577
1578 if Nkind (I_Node) = N_Formal_Package_Declaration
1579 or else
1580 (Ada_Version >= Ada_2012
1581 and then
1582 Ekind (Defining_Identifier (Analyzed_Formal)) =
1583 E_Incomplete_Type)
1584 then
1585 null;
1586
1587 else
1588 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1589 end if;
1590 end if;
1591
1592 -- A remote access-to-class-wide type is not a legal actual
1593 -- for a generic formal of an access type (E.2.2(17/2)).
1594 -- In GNAT an exception to this rule is introduced when
1595 -- the formal is marked as remote using implementation
1596 -- defined aspect/pragma Remote_Access_Type. In that case
1597 -- the actual must be remote as well.
1598
1599 -- If the current instantiation is the construction of a
1600 -- local copy for a formal package the actuals may be
1601 -- defaulted, and there is no matching actual to check.
1602
1603 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1604 and then
1605 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1606 N_Access_To_Object_Definition
1607 and then Present (Match)
1608 then
1609 declare
1610 Formal_Ent : constant Entity_Id :=
1611 Defining_Identifier (Analyzed_Formal);
1612 begin
1613 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1614 = Is_Remote_Types (Formal_Ent)
1615 then
1616 -- Remoteness of formal and actual match
1617
1618 null;
1619
1620 elsif Is_Remote_Types (Formal_Ent) then
1621
1622 -- Remote formal, non-remote actual
1623
1624 Error_Msg_NE
1625 ("actual for& must be remote", Match, Formal_Ent);
1626
1627 else
1628 -- Non-remote formal, remote actual
1629
1630 Error_Msg_NE
1631 ("actual for& may not be remote",
1632 Match, Formal_Ent);
1633 end if;
1634 end;
1635 end if;
1636
1637 when N_Formal_Subprogram_Declaration =>
1638 Match :=
1639 Matching_Actual
1640 (Defining_Unit_Name (Specification (Formal)),
1641 Defining_Unit_Name (Specification (Analyzed_Formal)));
1642
1643 -- If the formal subprogram has the same name as another
1644 -- formal subprogram of the generic, then a named
1645 -- association is illegal (12.3(9)). Exclude named
1646 -- associations that are generated for a nested instance.
1647
1648 if Present (Match)
1649 and then Is_Named_Assoc
1650 and then Comes_From_Source (Found_Assoc)
1651 then
1652 Check_Overloaded_Formal_Subprogram (Formal);
1653 end if;
1654
1655 -- If there is no corresponding actual, this may be case
1656 -- of partial parameterization, or else the formal has a
1657 -- default or a box.
1658
1659 if No (Match) and then Partial_Parameterization then
1660 Process_Default (Formal);
1661
1662 if Nkind (I_Node) = N_Formal_Package_Declaration then
1663 Check_Overloaded_Formal_Subprogram (Formal);
1664 end if;
1665
1666 else
1667 Append_To (Assoc,
1668 Instantiate_Formal_Subprogram
1669 (Formal, Match, Analyzed_Formal));
1670
1671 -- An instantiation is a freeze point for the actuals,
1672 -- unless this is a rewritten formal package.
1673
1674 if Nkind (I_Node) /= N_Formal_Package_Declaration
1675 and then Nkind (Match) = N_Identifier
1676 and then Is_Subprogram (Entity (Match))
1677
1678 -- The actual subprogram may rename a routine defined
1679 -- in Standard. Avoid freezing such renamings because
1680 -- subprograms coming from Standard cannot be frozen.
1681
1682 and then
1683 not Renames_Standard_Subprogram (Entity (Match))
1684
1685 -- If the actual subprogram comes from a different
1686 -- unit, it is already frozen, either by a body in
1687 -- that unit or by the end of the declarative part
1688 -- of the unit. This check avoids the freezing of
1689 -- subprograms defined in Standard which are used
1690 -- as generic actuals.
1691
1692 and then In_Same_Code_Unit (Entity (Match), I_Node)
1693 and then Has_Fully_Defined_Profile (Entity (Match))
1694 then
1695 -- Mark the subprogram as having a delayed freeze
1696 -- since this may be an out-of-order action.
1697
1698 Set_Has_Delayed_Freeze (Entity (Match));
1699 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1700 end if;
1701 end if;
1702
1703 -- If this is a nested generic, preserve default for later
1704 -- instantiations. We do this as well for GNATProve use,
1705 -- so that the list of generic associations is complete.
1706
1707 if No (Match) and then Box_Present (Formal) then
1708 declare
1709 Subp : constant Entity_Id :=
1710 Defining_Unit_Name (Specification (Last (Assoc)));
1711
1712 begin
1713 Append_To (Default_Actuals,
1714 Make_Generic_Association (Sloc (I_Node),
1715 Selector_Name =>
1716 New_Occurrence_Of (Subp, Sloc (I_Node)),
1717 Explicit_Generic_Actual_Parameter =>
1718 New_Occurrence_Of (Subp, Sloc (I_Node))));
1719 end;
1720 end if;
1721
1722 when N_Formal_Package_Declaration =>
1723 Match :=
1724 Matching_Actual
1725 (Defining_Identifier (Formal),
1726 Defining_Identifier (Original_Node (Analyzed_Formal)));
1727
1728 if No (Match) then
1729 if Partial_Parameterization then
1730 Process_Default (Formal);
1731
1732 else
1733 Error_Msg_Sloc := Sloc (Gen_Unit);
1734 Error_Msg_NE
1735 ("missing actual&",
1736 Instantiation_Node, Defining_Identifier (Formal));
1737 Error_Msg_NE
1738 ("\in instantiation of & declared#",
1739 Instantiation_Node, Gen_Unit);
1740
1741 Abandon_Instantiation (Instantiation_Node);
1742 end if;
1743
1744 else
1745 Analyze (Match);
1746 Append_List
1747 (Instantiate_Formal_Package
1748 (Formal, Match, Analyzed_Formal),
1749 Assoc);
1750 end if;
1751
1752 -- For use type and use package appearing in the generic part,
1753 -- we have already copied them, so we can just move them where
1754 -- they belong (we mustn't recopy them since this would mess up
1755 -- the Sloc values).
1756
1757 when N_Use_Package_Clause |
1758 N_Use_Type_Clause =>
1759 if Nkind (Original_Node (I_Node)) =
1760 N_Formal_Package_Declaration
1761 then
1762 Append (New_Copy_Tree (Formal), Assoc);
1763 else
1764 Remove (Formal);
1765 Append (Formal, Assoc);
1766 end if;
1767
1768 when others =>
1769 raise Program_Error;
1770
1771 end case;
1772
1773 Formal := Saved_Formal;
1774 Next_Non_Pragma (Analyzed_Formal);
1775 end loop;
1776
1777 if Num_Actuals > Num_Matched then
1778 Error_Msg_Sloc := Sloc (Gen_Unit);
1779
1780 if Present (Selector_Name (Actual)) then
1781 Error_Msg_NE
1782 ("unmatched actual &", Actual, Selector_Name (Actual));
1783 Error_Msg_NE
1784 ("\in instantiation of & declared#", Actual, Gen_Unit);
1785 else
1786 Error_Msg_NE
1787 ("unmatched actual in instantiation of & declared#",
1788 Actual, Gen_Unit);
1789 end if;
1790 end if;
1791
1792 elsif Present (Actuals) then
1793 Error_Msg_N
1794 ("too many actuals in generic instantiation", Instantiation_Node);
1795 end if;
1796
1797 -- An instantiation freezes all generic actuals. The only exceptions
1798 -- to this are incomplete types and subprograms which are not fully
1799 -- defined at the point of instantiation.
1800
1801 declare
1802 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1803 begin
1804 while Present (Elmt) loop
1805 Freeze_Before (I_Node, Node (Elmt));
1806 Next_Elmt (Elmt);
1807 end loop;
1808 end;
1809
1810 -- If there are default subprograms, normalize the tree by adding
1811 -- explicit associations for them. This is required if the instance
1812 -- appears within a generic.
1813
1814 if not Is_Empty_List (Default_Actuals) then
1815 declare
1816 Default : Node_Id;
1817
1818 begin
1819 Default := First (Default_Actuals);
1820 while Present (Default) loop
1821 Mark_Rewrite_Insertion (Default);
1822 Next (Default);
1823 end loop;
1824
1825 if No (Actuals) then
1826 Set_Generic_Associations (I_Node, Default_Actuals);
1827 else
1828 Append_List_To (Actuals, Default_Actuals);
1829 end if;
1830 end;
1831 end if;
1832
1833 -- If this is a formal package, normalize the parameter list by adding
1834 -- explicit box associations for the formals that are covered by an
1835 -- Others_Choice.
1836
1837 if not Is_Empty_List (Default_Formals) then
1838 Append_List (Default_Formals, Formals);
1839 end if;
1840
1841 return Assoc;
1842 end Analyze_Associations;
1843
1844 -------------------------------
1845 -- Analyze_Formal_Array_Type --
1846 -------------------------------
1847
1848 procedure Analyze_Formal_Array_Type
1849 (T : in out Entity_Id;
1850 Def : Node_Id)
1851 is
1852 DSS : Node_Id;
1853
1854 begin
1855 -- Treated like a non-generic array declaration, with additional
1856 -- semantic checks.
1857
1858 Enter_Name (T);
1859
1860 if Nkind (Def) = N_Constrained_Array_Definition then
1861 DSS := First (Discrete_Subtype_Definitions (Def));
1862 while Present (DSS) loop
1863 if Nkind_In (DSS, N_Subtype_Indication,
1864 N_Range,
1865 N_Attribute_Reference)
1866 then
1867 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1868 end if;
1869
1870 Next (DSS);
1871 end loop;
1872 end if;
1873
1874 Array_Type_Declaration (T, Def);
1875 Set_Is_Generic_Type (Base_Type (T));
1876
1877 if Ekind (Component_Type (T)) = E_Incomplete_Type
1878 and then No (Full_View (Component_Type (T)))
1879 then
1880 Error_Msg_N ("premature usage of incomplete type", Def);
1881
1882 -- Check that range constraint is not allowed on the component type
1883 -- of a generic formal array type (AARM 12.5.3(3))
1884
1885 elsif Is_Internal (Component_Type (T))
1886 and then Present (Subtype_Indication (Component_Definition (Def)))
1887 and then Nkind (Original_Node
1888 (Subtype_Indication (Component_Definition (Def)))) =
1889 N_Subtype_Indication
1890 then
1891 Error_Msg_N
1892 ("in a formal, a subtype indication can only be "
1893 & "a subtype mark (RM 12.5.3(3))",
1894 Subtype_Indication (Component_Definition (Def)));
1895 end if;
1896
1897 end Analyze_Formal_Array_Type;
1898
1899 ---------------------------------------------
1900 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1901 ---------------------------------------------
1902
1903 -- As for other generic types, we create a valid type representation with
1904 -- legal but arbitrary attributes, whose values are never considered
1905 -- static. For all scalar types we introduce an anonymous base type, with
1906 -- the same attributes. We choose the corresponding integer type to be
1907 -- Standard_Integer.
1908 -- Here and in other similar routines, the Sloc of the generated internal
1909 -- type must be the same as the sloc of the defining identifier of the
1910 -- formal type declaration, to provide proper source navigation.
1911
1912 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1913 (T : Entity_Id;
1914 Def : Node_Id)
1915 is
1916 Loc : constant Source_Ptr := Sloc (Def);
1917
1918 Base : constant Entity_Id :=
1919 New_Internal_Entity
1920 (E_Decimal_Fixed_Point_Type,
1921 Current_Scope,
1922 Sloc (Defining_Identifier (Parent (Def))), 'G');
1923
1924 Int_Base : constant Entity_Id := Standard_Integer;
1925 Delta_Val : constant Ureal := Ureal_1;
1926 Digs_Val : constant Uint := Uint_6;
1927
1928 function Make_Dummy_Bound return Node_Id;
1929 -- Return a properly typed universal real literal to use as a bound
1930
1931 ----------------------
1932 -- Make_Dummy_Bound --
1933 ----------------------
1934
1935 function Make_Dummy_Bound return Node_Id is
1936 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
1937 begin
1938 Set_Etype (Bound, Universal_Real);
1939 return Bound;
1940 end Make_Dummy_Bound;
1941
1942 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
1943
1944 begin
1945 Enter_Name (T);
1946
1947 Set_Etype (Base, Base);
1948 Set_Size_Info (Base, Int_Base);
1949 Set_RM_Size (Base, RM_Size (Int_Base));
1950 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1951 Set_Digits_Value (Base, Digs_Val);
1952 Set_Delta_Value (Base, Delta_Val);
1953 Set_Small_Value (Base, Delta_Val);
1954 Set_Scalar_Range (Base,
1955 Make_Range (Loc,
1956 Low_Bound => Make_Dummy_Bound,
1957 High_Bound => Make_Dummy_Bound));
1958
1959 Set_Is_Generic_Type (Base);
1960 Set_Parent (Base, Parent (Def));
1961
1962 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1963 Set_Etype (T, Base);
1964 Set_Size_Info (T, Int_Base);
1965 Set_RM_Size (T, RM_Size (Int_Base));
1966 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1967 Set_Digits_Value (T, Digs_Val);
1968 Set_Delta_Value (T, Delta_Val);
1969 Set_Small_Value (T, Delta_Val);
1970 Set_Scalar_Range (T, Scalar_Range (Base));
1971 Set_Is_Constrained (T);
1972
1973 Check_Restriction (No_Fixed_Point, Def);
1974 end Analyze_Formal_Decimal_Fixed_Point_Type;
1975
1976 -------------------------------------------
1977 -- Analyze_Formal_Derived_Interface_Type --
1978 -------------------------------------------
1979
1980 procedure Analyze_Formal_Derived_Interface_Type
1981 (N : Node_Id;
1982 T : Entity_Id;
1983 Def : Node_Id)
1984 is
1985 Loc : constant Source_Ptr := Sloc (Def);
1986
1987 begin
1988 -- Rewrite as a type declaration of a derived type. This ensures that
1989 -- the interface list and primitive operations are properly captured.
1990
1991 Rewrite (N,
1992 Make_Full_Type_Declaration (Loc,
1993 Defining_Identifier => T,
1994 Type_Definition => Def));
1995 Analyze (N);
1996 Set_Is_Generic_Type (T);
1997 end Analyze_Formal_Derived_Interface_Type;
1998
1999 ---------------------------------
2000 -- Analyze_Formal_Derived_Type --
2001 ---------------------------------
2002
2003 procedure Analyze_Formal_Derived_Type
2004 (N : Node_Id;
2005 T : Entity_Id;
2006 Def : Node_Id)
2007 is
2008 Loc : constant Source_Ptr := Sloc (Def);
2009 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2010 New_N : Node_Id;
2011
2012 begin
2013 Set_Is_Generic_Type (T);
2014
2015 if Private_Present (Def) then
2016 New_N :=
2017 Make_Private_Extension_Declaration (Loc,
2018 Defining_Identifier => T,
2019 Discriminant_Specifications => Discriminant_Specifications (N),
2020 Unknown_Discriminants_Present => Unk_Disc,
2021 Subtype_Indication => Subtype_Mark (Def),
2022 Interface_List => Interface_List (Def));
2023
2024 Set_Abstract_Present (New_N, Abstract_Present (Def));
2025 Set_Limited_Present (New_N, Limited_Present (Def));
2026 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2027
2028 else
2029 New_N :=
2030 Make_Full_Type_Declaration (Loc,
2031 Defining_Identifier => T,
2032 Discriminant_Specifications =>
2033 Discriminant_Specifications (Parent (T)),
2034 Type_Definition =>
2035 Make_Derived_Type_Definition (Loc,
2036 Subtype_Indication => Subtype_Mark (Def)));
2037
2038 Set_Abstract_Present
2039 (Type_Definition (New_N), Abstract_Present (Def));
2040 Set_Limited_Present
2041 (Type_Definition (New_N), Limited_Present (Def));
2042 end if;
2043
2044 Rewrite (N, New_N);
2045 Analyze (N);
2046
2047 if Unk_Disc then
2048 if not Is_Composite_Type (T) then
2049 Error_Msg_N
2050 ("unknown discriminants not allowed for elementary types", N);
2051 else
2052 Set_Has_Unknown_Discriminants (T);
2053 Set_Is_Constrained (T, False);
2054 end if;
2055 end if;
2056
2057 -- If the parent type has a known size, so does the formal, which makes
2058 -- legal representation clauses that involve the formal.
2059
2060 Set_Size_Known_At_Compile_Time
2061 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2062 end Analyze_Formal_Derived_Type;
2063
2064 ----------------------------------
2065 -- Analyze_Formal_Discrete_Type --
2066 ----------------------------------
2067
2068 -- The operations defined for a discrete types are those of an enumeration
2069 -- type. The size is set to an arbitrary value, for use in analyzing the
2070 -- generic unit.
2071
2072 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2073 Loc : constant Source_Ptr := Sloc (Def);
2074 Lo : Node_Id;
2075 Hi : Node_Id;
2076
2077 Base : constant Entity_Id :=
2078 New_Internal_Entity
2079 (E_Floating_Point_Type, Current_Scope,
2080 Sloc (Defining_Identifier (Parent (Def))), 'G');
2081
2082 begin
2083 Enter_Name (T);
2084 Set_Ekind (T, E_Enumeration_Subtype);
2085 Set_Etype (T, Base);
2086 Init_Size (T, 8);
2087 Init_Alignment (T);
2088 Set_Is_Generic_Type (T);
2089 Set_Is_Constrained (T);
2090
2091 -- For semantic analysis, the bounds of the type must be set to some
2092 -- non-static value. The simplest is to create attribute nodes for those
2093 -- bounds, that refer to the type itself. These bounds are never
2094 -- analyzed but serve as place-holders.
2095
2096 Lo :=
2097 Make_Attribute_Reference (Loc,
2098 Attribute_Name => Name_First,
2099 Prefix => New_Occurrence_Of (T, Loc));
2100 Set_Etype (Lo, T);
2101
2102 Hi :=
2103 Make_Attribute_Reference (Loc,
2104 Attribute_Name => Name_Last,
2105 Prefix => New_Occurrence_Of (T, Loc));
2106 Set_Etype (Hi, T);
2107
2108 Set_Scalar_Range (T,
2109 Make_Range (Loc,
2110 Low_Bound => Lo,
2111 High_Bound => Hi));
2112
2113 Set_Ekind (Base, E_Enumeration_Type);
2114 Set_Etype (Base, Base);
2115 Init_Size (Base, 8);
2116 Init_Alignment (Base);
2117 Set_Is_Generic_Type (Base);
2118 Set_Scalar_Range (Base, Scalar_Range (T));
2119 Set_Parent (Base, Parent (Def));
2120 end Analyze_Formal_Discrete_Type;
2121
2122 ----------------------------------
2123 -- Analyze_Formal_Floating_Type --
2124 ---------------------------------
2125
2126 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2127 Base : constant Entity_Id :=
2128 New_Internal_Entity
2129 (E_Floating_Point_Type, Current_Scope,
2130 Sloc (Defining_Identifier (Parent (Def))), 'G');
2131
2132 begin
2133 -- The various semantic attributes are taken from the predefined type
2134 -- Float, just so that all of them are initialized. Their values are
2135 -- never used because no constant folding or expansion takes place in
2136 -- the generic itself.
2137
2138 Enter_Name (T);
2139 Set_Ekind (T, E_Floating_Point_Subtype);
2140 Set_Etype (T, Base);
2141 Set_Size_Info (T, (Standard_Float));
2142 Set_RM_Size (T, RM_Size (Standard_Float));
2143 Set_Digits_Value (T, Digits_Value (Standard_Float));
2144 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2145 Set_Is_Constrained (T);
2146
2147 Set_Is_Generic_Type (Base);
2148 Set_Etype (Base, Base);
2149 Set_Size_Info (Base, (Standard_Float));
2150 Set_RM_Size (Base, RM_Size (Standard_Float));
2151 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2152 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2153 Set_Parent (Base, Parent (Def));
2154
2155 Check_Restriction (No_Floating_Point, Def);
2156 end Analyze_Formal_Floating_Type;
2157
2158 -----------------------------------
2159 -- Analyze_Formal_Interface_Type;--
2160 -----------------------------------
2161
2162 procedure Analyze_Formal_Interface_Type
2163 (N : Node_Id;
2164 T : Entity_Id;
2165 Def : Node_Id)
2166 is
2167 Loc : constant Source_Ptr := Sloc (N);
2168 New_N : Node_Id;
2169
2170 begin
2171 New_N :=
2172 Make_Full_Type_Declaration (Loc,
2173 Defining_Identifier => T,
2174 Type_Definition => Def);
2175
2176 Rewrite (N, New_N);
2177 Analyze (N);
2178 Set_Is_Generic_Type (T);
2179 end Analyze_Formal_Interface_Type;
2180
2181 ---------------------------------
2182 -- Analyze_Formal_Modular_Type --
2183 ---------------------------------
2184
2185 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2186 begin
2187 -- Apart from their entity kind, generic modular types are treated like
2188 -- signed integer types, and have the same attributes.
2189
2190 Analyze_Formal_Signed_Integer_Type (T, Def);
2191 Set_Ekind (T, E_Modular_Integer_Subtype);
2192 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2193
2194 end Analyze_Formal_Modular_Type;
2195
2196 ---------------------------------------
2197 -- Analyze_Formal_Object_Declaration --
2198 ---------------------------------------
2199
2200 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2201 E : constant Node_Id := Default_Expression (N);
2202 Id : constant Node_Id := Defining_Identifier (N);
2203 K : Entity_Kind;
2204 T : Node_Id;
2205
2206 begin
2207 Enter_Name (Id);
2208
2209 -- Determine the mode of the formal object
2210
2211 if Out_Present (N) then
2212 K := E_Generic_In_Out_Parameter;
2213
2214 if not In_Present (N) then
2215 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2216 end if;
2217
2218 else
2219 K := E_Generic_In_Parameter;
2220 end if;
2221
2222 if Present (Subtype_Mark (N)) then
2223 Find_Type (Subtype_Mark (N));
2224 T := Entity (Subtype_Mark (N));
2225
2226 -- Verify that there is no redundant null exclusion
2227
2228 if Null_Exclusion_Present (N) then
2229 if not Is_Access_Type (T) then
2230 Error_Msg_N
2231 ("null exclusion can only apply to an access type", N);
2232
2233 elsif Can_Never_Be_Null (T) then
2234 Error_Msg_NE
2235 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2236 end if;
2237 end if;
2238
2239 -- Ada 2005 (AI-423): Formal object with an access definition
2240
2241 else
2242 Check_Access_Definition (N);
2243 T := Access_Definition
2244 (Related_Nod => N,
2245 N => Access_Definition (N));
2246 end if;
2247
2248 if Ekind (T) = E_Incomplete_Type then
2249 declare
2250 Error_Node : Node_Id;
2251
2252 begin
2253 if Present (Subtype_Mark (N)) then
2254 Error_Node := Subtype_Mark (N);
2255 else
2256 Check_Access_Definition (N);
2257 Error_Node := Access_Definition (N);
2258 end if;
2259
2260 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2261 end;
2262 end if;
2263
2264 if K = E_Generic_In_Parameter then
2265
2266 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2267
2268 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2269 Error_Msg_N
2270 ("generic formal of mode IN must not be of limited type", N);
2271 Explain_Limited_Type (T, N);
2272 end if;
2273
2274 if Is_Abstract_Type (T) then
2275 Error_Msg_N
2276 ("generic formal of mode IN must not be of abstract type", N);
2277 end if;
2278
2279 if Present (E) then
2280 Preanalyze_Spec_Expression (E, T);
2281
2282 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2283 Error_Msg_N
2284 ("initialization not allowed for limited types", E);
2285 Explain_Limited_Type (T, E);
2286 end if;
2287 end if;
2288
2289 Set_Ekind (Id, K);
2290 Set_Etype (Id, T);
2291
2292 -- Case of generic IN OUT parameter
2293
2294 else
2295 -- If the formal has an unconstrained type, construct its actual
2296 -- subtype, as is done for subprogram formals. In this fashion, all
2297 -- its uses can refer to specific bounds.
2298
2299 Set_Ekind (Id, K);
2300 Set_Etype (Id, T);
2301
2302 if (Is_Array_Type (T) and then not Is_Constrained (T))
2303 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2304 then
2305 declare
2306 Non_Freezing_Ref : constant Node_Id :=
2307 New_Occurrence_Of (Id, Sloc (Id));
2308 Decl : Node_Id;
2309
2310 begin
2311 -- Make sure the actual subtype doesn't generate bogus freezing
2312
2313 Set_Must_Not_Freeze (Non_Freezing_Ref);
2314 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2315 Insert_Before_And_Analyze (N, Decl);
2316 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2317 end;
2318 else
2319 Set_Actual_Subtype (Id, T);
2320 end if;
2321
2322 if Present (E) then
2323 Error_Msg_N
2324 ("initialization not allowed for `IN OUT` formals", N);
2325 end if;
2326 end if;
2327
2328 if Has_Aspects (N) then
2329 Analyze_Aspect_Specifications (N, Id);
2330 end if;
2331 end Analyze_Formal_Object_Declaration;
2332
2333 ----------------------------------------------
2334 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2335 ----------------------------------------------
2336
2337 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2338 (T : Entity_Id;
2339 Def : Node_Id)
2340 is
2341 Loc : constant Source_Ptr := Sloc (Def);
2342 Base : constant Entity_Id :=
2343 New_Internal_Entity
2344 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2345 Sloc (Defining_Identifier (Parent (Def))), 'G');
2346
2347 begin
2348 -- The semantic attributes are set for completeness only, their values
2349 -- will never be used, since all properties of the type are non-static.
2350
2351 Enter_Name (T);
2352 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2353 Set_Etype (T, Base);
2354 Set_Size_Info (T, Standard_Integer);
2355 Set_RM_Size (T, RM_Size (Standard_Integer));
2356 Set_Small_Value (T, Ureal_1);
2357 Set_Delta_Value (T, Ureal_1);
2358 Set_Scalar_Range (T,
2359 Make_Range (Loc,
2360 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2361 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2362 Set_Is_Constrained (T);
2363
2364 Set_Is_Generic_Type (Base);
2365 Set_Etype (Base, Base);
2366 Set_Size_Info (Base, Standard_Integer);
2367 Set_RM_Size (Base, RM_Size (Standard_Integer));
2368 Set_Small_Value (Base, Ureal_1);
2369 Set_Delta_Value (Base, Ureal_1);
2370 Set_Scalar_Range (Base, Scalar_Range (T));
2371 Set_Parent (Base, Parent (Def));
2372
2373 Check_Restriction (No_Fixed_Point, Def);
2374 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2375
2376 ----------------------------------------
2377 -- Analyze_Formal_Package_Declaration --
2378 ----------------------------------------
2379
2380 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2381 Loc : constant Source_Ptr := Sloc (N);
2382 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2383 Formal : Entity_Id;
2384 Gen_Id : constant Node_Id := Name (N);
2385 Gen_Decl : Node_Id;
2386 Gen_Unit : Entity_Id;
2387 New_N : Node_Id;
2388 Parent_Installed : Boolean := False;
2389 Renaming : Node_Id;
2390 Parent_Instance : Entity_Id;
2391 Renaming_In_Par : Entity_Id;
2392 Associations : Boolean := True;
2393
2394 Vis_Prims_List : Elist_Id := No_Elist;
2395 -- List of primitives made temporarily visible in the instantiation
2396 -- to match the visibility of the formal type
2397
2398 function Build_Local_Package return Node_Id;
2399 -- The formal package is rewritten so that its parameters are replaced
2400 -- with corresponding declarations. For parameters with bona fide
2401 -- associations these declarations are created by Analyze_Associations
2402 -- as for a regular instantiation. For boxed parameters, we preserve
2403 -- the formal declarations and analyze them, in order to introduce
2404 -- entities of the right kind in the environment of the formal.
2405
2406 -------------------------
2407 -- Build_Local_Package --
2408 -------------------------
2409
2410 function Build_Local_Package return Node_Id is
2411 Decls : List_Id;
2412 Pack_Decl : Node_Id;
2413
2414 begin
2415 -- Within the formal, the name of the generic package is a renaming
2416 -- of the formal (as for a regular instantiation).
2417
2418 Pack_Decl :=
2419 Make_Package_Declaration (Loc,
2420 Specification =>
2421 Copy_Generic_Node
2422 (Specification (Original_Node (Gen_Decl)),
2423 Empty, Instantiating => True));
2424
2425 Renaming :=
2426 Make_Package_Renaming_Declaration (Loc,
2427 Defining_Unit_Name =>
2428 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2429 Name => New_Occurrence_Of (Formal, Loc));
2430
2431 if Nkind (Gen_Id) = N_Identifier
2432 and then Chars (Gen_Id) = Chars (Pack_Id)
2433 then
2434 Error_Msg_NE
2435 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2436 end if;
2437
2438 -- If the formal is declared with a box, or with an others choice,
2439 -- create corresponding declarations for all entities in the formal
2440 -- part, so that names with the proper types are available in the
2441 -- specification of the formal package.
2442
2443 -- On the other hand, if there are no associations, then all the
2444 -- formals must have defaults, and this will be checked by the
2445 -- call to Analyze_Associations.
2446
2447 if Box_Present (N)
2448 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2449 then
2450 declare
2451 Formal_Decl : Node_Id;
2452
2453 begin
2454 -- TBA : for a formal package, need to recurse ???
2455
2456 Decls := New_List;
2457 Formal_Decl :=
2458 First
2459 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2460 while Present (Formal_Decl) loop
2461 Append_To
2462 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2463 Next (Formal_Decl);
2464 end loop;
2465 end;
2466
2467 -- If generic associations are present, use Analyze_Associations to
2468 -- create the proper renaming declarations.
2469
2470 else
2471 declare
2472 Act_Tree : constant Node_Id :=
2473 Copy_Generic_Node
2474 (Original_Node (Gen_Decl), Empty,
2475 Instantiating => True);
2476
2477 begin
2478 Generic_Renamings.Set_Last (0);
2479 Generic_Renamings_HTable.Reset;
2480 Instantiation_Node := N;
2481
2482 Decls :=
2483 Analyze_Associations
2484 (I_Node => Original_Node (N),
2485 Formals => Generic_Formal_Declarations (Act_Tree),
2486 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2487
2488 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2489 end;
2490 end if;
2491
2492 Append (Renaming, To => Decls);
2493
2494 -- Add generated declarations ahead of local declarations in
2495 -- the package.
2496
2497 if No (Visible_Declarations (Specification (Pack_Decl))) then
2498 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2499 else
2500 Insert_List_Before
2501 (First (Visible_Declarations (Specification (Pack_Decl))),
2502 Decls);
2503 end if;
2504
2505 return Pack_Decl;
2506 end Build_Local_Package;
2507
2508 -- Start of processing for Analyze_Formal_Package_Declaration
2509
2510 begin
2511 Check_Text_IO_Special_Unit (Gen_Id);
2512
2513 Init_Env;
2514 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2515 Gen_Unit := Entity (Gen_Id);
2516
2517 -- Check for a formal package that is a package renaming
2518
2519 if Present (Renamed_Object (Gen_Unit)) then
2520
2521 -- Indicate that unit is used, before replacing it with renamed
2522 -- entity for use below.
2523
2524 if In_Extended_Main_Source_Unit (N) then
2525 Set_Is_Instantiated (Gen_Unit);
2526 Generate_Reference (Gen_Unit, N);
2527 end if;
2528
2529 Gen_Unit := Renamed_Object (Gen_Unit);
2530 end if;
2531
2532 if Ekind (Gen_Unit) /= E_Generic_Package then
2533 Error_Msg_N ("expect generic package name", Gen_Id);
2534 Restore_Env;
2535 goto Leave;
2536
2537 elsif Gen_Unit = Current_Scope then
2538 Error_Msg_N
2539 ("generic package cannot be used as a formal package of itself",
2540 Gen_Id);
2541 Restore_Env;
2542 goto Leave;
2543
2544 elsif In_Open_Scopes (Gen_Unit) then
2545 if Is_Compilation_Unit (Gen_Unit)
2546 and then Is_Child_Unit (Current_Scope)
2547 then
2548 -- Special-case the error when the formal is a parent, and
2549 -- continue analysis to minimize cascaded errors.
2550
2551 Error_Msg_N
2552 ("generic parent cannot be used as formal package "
2553 & "of a child unit", Gen_Id);
2554
2555 else
2556 Error_Msg_N
2557 ("generic package cannot be used as a formal package "
2558 & "within itself", Gen_Id);
2559 Restore_Env;
2560 goto Leave;
2561 end if;
2562 end if;
2563
2564 -- Check that name of formal package does not hide name of generic,
2565 -- or its leading prefix. This check must be done separately because
2566 -- the name of the generic has already been analyzed.
2567
2568 declare
2569 Gen_Name : Entity_Id;
2570
2571 begin
2572 Gen_Name := Gen_Id;
2573 while Nkind (Gen_Name) = N_Expanded_Name loop
2574 Gen_Name := Prefix (Gen_Name);
2575 end loop;
2576
2577 if Chars (Gen_Name) = Chars (Pack_Id) then
2578 Error_Msg_NE
2579 ("& is hidden within declaration of formal package",
2580 Gen_Id, Gen_Name);
2581 end if;
2582 end;
2583
2584 if Box_Present (N)
2585 or else No (Generic_Associations (N))
2586 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2587 then
2588 Associations := False;
2589 end if;
2590
2591 -- If there are no generic associations, the generic parameters appear
2592 -- as local entities and are instantiated like them. We copy the generic
2593 -- package declaration as if it were an instantiation, and analyze it
2594 -- like a regular package, except that we treat the formals as
2595 -- additional visible components.
2596
2597 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2598
2599 if In_Extended_Main_Source_Unit (N) then
2600 Set_Is_Instantiated (Gen_Unit);
2601 Generate_Reference (Gen_Unit, N);
2602 end if;
2603
2604 Formal := New_Copy (Pack_Id);
2605 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2606
2607 begin
2608 -- Make local generic without formals. The formals will be replaced
2609 -- with internal declarations.
2610
2611 New_N := Build_Local_Package;
2612
2613 -- If there are errors in the parameter list, Analyze_Associations
2614 -- raises Instantiation_Error. Patch the declaration to prevent
2615 -- further exception propagation.
2616
2617 exception
2618 when Instantiation_Error =>
2619
2620 Enter_Name (Formal);
2621 Set_Ekind (Formal, E_Variable);
2622 Set_Etype (Formal, Any_Type);
2623 Restore_Hidden_Primitives (Vis_Prims_List);
2624
2625 if Parent_Installed then
2626 Remove_Parent;
2627 end if;
2628
2629 goto Leave;
2630 end;
2631
2632 Rewrite (N, New_N);
2633 Set_Defining_Unit_Name (Specification (New_N), Formal);
2634 Set_Generic_Parent (Specification (N), Gen_Unit);
2635 Set_Instance_Env (Gen_Unit, Formal);
2636 Set_Is_Generic_Instance (Formal);
2637
2638 Enter_Name (Formal);
2639 Set_Ekind (Formal, E_Package);
2640 Set_Etype (Formal, Standard_Void_Type);
2641 Set_Inner_Instances (Formal, New_Elmt_List);
2642 Push_Scope (Formal);
2643
2644 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2645
2646 -- Similarly, we have to make the name of the formal visible in the
2647 -- parent instance, to resolve properly fully qualified names that
2648 -- may appear in the generic unit. The parent instance has been
2649 -- placed on the scope stack ahead of the current scope.
2650
2651 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2652
2653 Renaming_In_Par :=
2654 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2655 Set_Ekind (Renaming_In_Par, E_Package);
2656 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2657 Set_Scope (Renaming_In_Par, Parent_Instance);
2658 Set_Parent (Renaming_In_Par, Parent (Formal));
2659 Set_Renamed_Object (Renaming_In_Par, Formal);
2660 Append_Entity (Renaming_In_Par, Parent_Instance);
2661 end if;
2662
2663 Analyze (Specification (N));
2664
2665 -- The formals for which associations are provided are not visible
2666 -- outside of the formal package. The others are still declared by a
2667 -- formal parameter declaration.
2668
2669 -- If there are no associations, the only local entity to hide is the
2670 -- generated package renaming itself.
2671
2672 declare
2673 E : Entity_Id;
2674
2675 begin
2676 E := First_Entity (Formal);
2677 while Present (E) loop
2678 if Associations and then not Is_Generic_Formal (E) then
2679 Set_Is_Hidden (E);
2680 end if;
2681
2682 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
2683 Set_Is_Hidden (E);
2684 exit;
2685 end if;
2686
2687 Next_Entity (E);
2688 end loop;
2689 end;
2690
2691 End_Package_Scope (Formal);
2692 Restore_Hidden_Primitives (Vis_Prims_List);
2693
2694 if Parent_Installed then
2695 Remove_Parent;
2696 end if;
2697
2698 Restore_Env;
2699
2700 -- Inside the generic unit, the formal package is a regular package, but
2701 -- no body is needed for it. Note that after instantiation, the defining
2702 -- unit name we need is in the new tree and not in the original (see
2703 -- Package_Instantiation). A generic formal package is an instance, and
2704 -- can be used as an actual for an inner instance.
2705
2706 Set_Has_Completion (Formal, True);
2707
2708 -- Add semantic information to the original defining identifier.
2709 -- for ASIS use.
2710
2711 Set_Ekind (Pack_Id, E_Package);
2712 Set_Etype (Pack_Id, Standard_Void_Type);
2713 Set_Scope (Pack_Id, Scope (Formal));
2714 Set_Has_Completion (Pack_Id, True);
2715
2716 <<Leave>>
2717 if Has_Aspects (N) then
2718 Analyze_Aspect_Specifications (N, Pack_Id);
2719 end if;
2720 end Analyze_Formal_Package_Declaration;
2721
2722 ---------------------------------
2723 -- Analyze_Formal_Private_Type --
2724 ---------------------------------
2725
2726 procedure Analyze_Formal_Private_Type
2727 (N : Node_Id;
2728 T : Entity_Id;
2729 Def : Node_Id)
2730 is
2731 begin
2732 New_Private_Type (N, T, Def);
2733
2734 -- Set the size to an arbitrary but legal value
2735
2736 Set_Size_Info (T, Standard_Integer);
2737 Set_RM_Size (T, RM_Size (Standard_Integer));
2738 end Analyze_Formal_Private_Type;
2739
2740 ------------------------------------
2741 -- Analyze_Formal_Incomplete_Type --
2742 ------------------------------------
2743
2744 procedure Analyze_Formal_Incomplete_Type
2745 (T : Entity_Id;
2746 Def : Node_Id)
2747 is
2748 begin
2749 Enter_Name (T);
2750 Set_Ekind (T, E_Incomplete_Type);
2751 Set_Etype (T, T);
2752 Set_Private_Dependents (T, New_Elmt_List);
2753
2754 if Tagged_Present (Def) then
2755 Set_Is_Tagged_Type (T);
2756 Make_Class_Wide_Type (T);
2757 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2758 end if;
2759 end Analyze_Formal_Incomplete_Type;
2760
2761 ----------------------------------------
2762 -- Analyze_Formal_Signed_Integer_Type --
2763 ----------------------------------------
2764
2765 procedure Analyze_Formal_Signed_Integer_Type
2766 (T : Entity_Id;
2767 Def : Node_Id)
2768 is
2769 Base : constant Entity_Id :=
2770 New_Internal_Entity
2771 (E_Signed_Integer_Type,
2772 Current_Scope,
2773 Sloc (Defining_Identifier (Parent (Def))), 'G');
2774
2775 begin
2776 Enter_Name (T);
2777
2778 Set_Ekind (T, E_Signed_Integer_Subtype);
2779 Set_Etype (T, Base);
2780 Set_Size_Info (T, Standard_Integer);
2781 Set_RM_Size (T, RM_Size (Standard_Integer));
2782 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2783 Set_Is_Constrained (T);
2784
2785 Set_Is_Generic_Type (Base);
2786 Set_Size_Info (Base, Standard_Integer);
2787 Set_RM_Size (Base, RM_Size (Standard_Integer));
2788 Set_Etype (Base, Base);
2789 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2790 Set_Parent (Base, Parent (Def));
2791 end Analyze_Formal_Signed_Integer_Type;
2792
2793 -------------------------------------------
2794 -- Analyze_Formal_Subprogram_Declaration --
2795 -------------------------------------------
2796
2797 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2798 Spec : constant Node_Id := Specification (N);
2799 Def : constant Node_Id := Default_Name (N);
2800 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2801 Subp : Entity_Id;
2802
2803 begin
2804 if Nam = Error then
2805 return;
2806 end if;
2807
2808 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2809 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2810 goto Leave;
2811 end if;
2812
2813 Analyze_Subprogram_Declaration (N);
2814 Set_Is_Formal_Subprogram (Nam);
2815 Set_Has_Completion (Nam);
2816
2817 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2818 Set_Is_Abstract_Subprogram (Nam);
2819 Set_Is_Dispatching_Operation (Nam);
2820
2821 declare
2822 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2823 begin
2824 if No (Ctrl_Type) then
2825 Error_Msg_N
2826 ("abstract formal subprogram must have a controlling type",
2827 N);
2828
2829 elsif Ada_Version >= Ada_2012
2830 and then Is_Incomplete_Type (Ctrl_Type)
2831 then
2832 Error_Msg_NE
2833 ("controlling type of abstract formal subprogram cannot "
2834 & "be incomplete type", N, Ctrl_Type);
2835
2836 else
2837 Check_Controlling_Formals (Ctrl_Type, Nam);
2838 end if;
2839 end;
2840 end if;
2841
2842 -- Default name is resolved at the point of instantiation
2843
2844 if Box_Present (N) then
2845 null;
2846
2847 -- Else default is bound at the point of generic declaration
2848
2849 elsif Present (Def) then
2850 if Nkind (Def) = N_Operator_Symbol then
2851 Find_Direct_Name (Def);
2852
2853 elsif Nkind (Def) /= N_Attribute_Reference then
2854 Analyze (Def);
2855
2856 else
2857 -- For an attribute reference, analyze the prefix and verify
2858 -- that it has the proper profile for the subprogram.
2859
2860 Analyze (Prefix (Def));
2861 Valid_Default_Attribute (Nam, Def);
2862 goto Leave;
2863 end if;
2864
2865 -- Default name may be overloaded, in which case the interpretation
2866 -- with the correct profile must be selected, as for a renaming.
2867 -- If the definition is an indexed component, it must denote a
2868 -- member of an entry family. If it is a selected component, it
2869 -- can be a protected operation.
2870
2871 if Etype (Def) = Any_Type then
2872 goto Leave;
2873
2874 elsif Nkind (Def) = N_Selected_Component then
2875 if not Is_Overloadable (Entity (Selector_Name (Def))) then
2876 Error_Msg_N ("expect valid subprogram name as default", Def);
2877 end if;
2878
2879 elsif Nkind (Def) = N_Indexed_Component then
2880 if Is_Entity_Name (Prefix (Def)) then
2881 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
2882 Error_Msg_N ("expect valid subprogram name as default", Def);
2883 end if;
2884
2885 elsif Nkind (Prefix (Def)) = N_Selected_Component then
2886 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
2887 E_Entry_Family
2888 then
2889 Error_Msg_N ("expect valid subprogram name as default", Def);
2890 end if;
2891
2892 else
2893 Error_Msg_N ("expect valid subprogram name as default", Def);
2894 goto Leave;
2895 end if;
2896
2897 elsif Nkind (Def) = N_Character_Literal then
2898
2899 -- Needs some type checks: subprogram should be parameterless???
2900
2901 Resolve (Def, (Etype (Nam)));
2902
2903 elsif not Is_Entity_Name (Def)
2904 or else not Is_Overloadable (Entity (Def))
2905 then
2906 Error_Msg_N ("expect valid subprogram name as default", Def);
2907 goto Leave;
2908
2909 elsif not Is_Overloaded (Def) then
2910 Subp := Entity (Def);
2911
2912 if Subp = Nam then
2913 Error_Msg_N ("premature usage of formal subprogram", Def);
2914
2915 elsif not Entity_Matches_Spec (Subp, Nam) then
2916 Error_Msg_N ("no visible entity matches specification", Def);
2917 end if;
2918
2919 -- More than one interpretation, so disambiguate as for a renaming
2920
2921 else
2922 declare
2923 I : Interp_Index;
2924 I1 : Interp_Index := 0;
2925 It : Interp;
2926 It1 : Interp;
2927
2928 begin
2929 Subp := Any_Id;
2930 Get_First_Interp (Def, I, It);
2931 while Present (It.Nam) loop
2932 if Entity_Matches_Spec (It.Nam, Nam) then
2933 if Subp /= Any_Id then
2934 It1 := Disambiguate (Def, I1, I, Etype (Subp));
2935
2936 if It1 = No_Interp then
2937 Error_Msg_N ("ambiguous default subprogram", Def);
2938 else
2939 Subp := It1.Nam;
2940 end if;
2941
2942 exit;
2943
2944 else
2945 I1 := I;
2946 Subp := It.Nam;
2947 end if;
2948 end if;
2949
2950 Get_Next_Interp (I, It);
2951 end loop;
2952 end;
2953
2954 if Subp /= Any_Id then
2955
2956 -- Subprogram found, generate reference to it
2957
2958 Set_Entity (Def, Subp);
2959 Generate_Reference (Subp, Def);
2960
2961 if Subp = Nam then
2962 Error_Msg_N ("premature usage of formal subprogram", Def);
2963
2964 elsif Ekind (Subp) /= E_Operator then
2965 Check_Mode_Conformant (Subp, Nam);
2966 end if;
2967
2968 else
2969 Error_Msg_N ("no visible subprogram matches specification", N);
2970 end if;
2971 end if;
2972 end if;
2973
2974 <<Leave>>
2975 if Has_Aspects (N) then
2976 Analyze_Aspect_Specifications (N, Nam);
2977 end if;
2978
2979 end Analyze_Formal_Subprogram_Declaration;
2980
2981 -------------------------------------
2982 -- Analyze_Formal_Type_Declaration --
2983 -------------------------------------
2984
2985 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
2986 Def : constant Node_Id := Formal_Type_Definition (N);
2987 T : Entity_Id;
2988
2989 begin
2990 T := Defining_Identifier (N);
2991
2992 if Present (Discriminant_Specifications (N))
2993 and then Nkind (Def) /= N_Formal_Private_Type_Definition
2994 then
2995 Error_Msg_N
2996 ("discriminants not allowed for this formal type", T);
2997 end if;
2998
2999 -- Enter the new name, and branch to specific routine
3000
3001 case Nkind (Def) is
3002 when N_Formal_Private_Type_Definition =>
3003 Analyze_Formal_Private_Type (N, T, Def);
3004
3005 when N_Formal_Derived_Type_Definition =>
3006 Analyze_Formal_Derived_Type (N, T, Def);
3007
3008 when N_Formal_Incomplete_Type_Definition =>
3009 Analyze_Formal_Incomplete_Type (T, Def);
3010
3011 when N_Formal_Discrete_Type_Definition =>
3012 Analyze_Formal_Discrete_Type (T, Def);
3013
3014 when N_Formal_Signed_Integer_Type_Definition =>
3015 Analyze_Formal_Signed_Integer_Type (T, Def);
3016
3017 when N_Formal_Modular_Type_Definition =>
3018 Analyze_Formal_Modular_Type (T, Def);
3019
3020 when N_Formal_Floating_Point_Definition =>
3021 Analyze_Formal_Floating_Type (T, Def);
3022
3023 when N_Formal_Ordinary_Fixed_Point_Definition =>
3024 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3025
3026 when N_Formal_Decimal_Fixed_Point_Definition =>
3027 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3028
3029 when N_Array_Type_Definition =>
3030 Analyze_Formal_Array_Type (T, Def);
3031
3032 when N_Access_To_Object_Definition |
3033 N_Access_Function_Definition |
3034 N_Access_Procedure_Definition =>
3035 Analyze_Generic_Access_Type (T, Def);
3036
3037 -- Ada 2005: a interface declaration is encoded as an abstract
3038 -- record declaration or a abstract type derivation.
3039
3040 when N_Record_Definition =>
3041 Analyze_Formal_Interface_Type (N, T, Def);
3042
3043 when N_Derived_Type_Definition =>
3044 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3045
3046 when N_Error =>
3047 null;
3048
3049 when others =>
3050 raise Program_Error;
3051
3052 end case;
3053
3054 Set_Is_Generic_Type (T);
3055
3056 if Has_Aspects (N) then
3057 Analyze_Aspect_Specifications (N, T);
3058 end if;
3059 end Analyze_Formal_Type_Declaration;
3060
3061 ------------------------------------
3062 -- Analyze_Function_Instantiation --
3063 ------------------------------------
3064
3065 procedure Analyze_Function_Instantiation (N : Node_Id) is
3066 begin
3067 Analyze_Subprogram_Instantiation (N, E_Function);
3068 end Analyze_Function_Instantiation;
3069
3070 ---------------------------------
3071 -- Analyze_Generic_Access_Type --
3072 ---------------------------------
3073
3074 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3075 begin
3076 Enter_Name (T);
3077
3078 if Nkind (Def) = N_Access_To_Object_Definition then
3079 Access_Type_Declaration (T, Def);
3080
3081 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3082 and then No (Full_View (Designated_Type (T)))
3083 and then not Is_Generic_Type (Designated_Type (T))
3084 then
3085 Error_Msg_N ("premature usage of incomplete type", Def);
3086
3087 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3088 Error_Msg_N
3089 ("only a subtype mark is allowed in a formal", Def);
3090 end if;
3091
3092 else
3093 Access_Subprogram_Declaration (T, Def);
3094 end if;
3095 end Analyze_Generic_Access_Type;
3096
3097 ---------------------------------
3098 -- Analyze_Generic_Formal_Part --
3099 ---------------------------------
3100
3101 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3102 Gen_Parm_Decl : Node_Id;
3103
3104 begin
3105 -- The generic formals are processed in the scope of the generic unit,
3106 -- where they are immediately visible. The scope is installed by the
3107 -- caller.
3108
3109 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3110 while Present (Gen_Parm_Decl) loop
3111 Analyze (Gen_Parm_Decl);
3112 Next (Gen_Parm_Decl);
3113 end loop;
3114
3115 Generate_Reference_To_Generic_Formals (Current_Scope);
3116 end Analyze_Generic_Formal_Part;
3117
3118 ------------------------------------------
3119 -- Analyze_Generic_Package_Declaration --
3120 ------------------------------------------
3121
3122 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3123 Loc : constant Source_Ptr := Sloc (N);
3124 Id : Entity_Id;
3125 New_N : Node_Id;
3126 Save_Parent : Node_Id;
3127 Renaming : Node_Id;
3128 Decls : constant List_Id :=
3129 Visible_Declarations (Specification (N));
3130 Decl : Node_Id;
3131
3132 begin
3133 -- The generic package declaration may be subject to pragma Ghost with
3134 -- policy Ignore. Set the mode now to ensure that any nodes generated
3135 -- during analysis and expansion are properly flagged as ignored Ghost.
3136
3137 Set_Ghost_Mode (N);
3138 Check_SPARK_05_Restriction ("generic is not allowed", N);
3139
3140 -- We introduce a renaming of the enclosing package, to have a usable
3141 -- entity as the prefix of an expanded name for a local entity of the
3142 -- form Par.P.Q, where P is the generic package. This is because a local
3143 -- entity named P may hide it, so that the usual visibility rules in
3144 -- the instance will not resolve properly.
3145
3146 Renaming :=
3147 Make_Package_Renaming_Declaration (Loc,
3148 Defining_Unit_Name =>
3149 Make_Defining_Identifier (Loc,
3150 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3151 Name =>
3152 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3153
3154 if Present (Decls) then
3155 Decl := First (Decls);
3156 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
3157 Next (Decl);
3158 end loop;
3159
3160 if Present (Decl) then
3161 Insert_Before (Decl, Renaming);
3162 else
3163 Append (Renaming, Visible_Declarations (Specification (N)));
3164 end if;
3165
3166 else
3167 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3168 end if;
3169
3170 -- Create copy of generic unit, and save for instantiation. If the unit
3171 -- is a child unit, do not copy the specifications for the parent, which
3172 -- are not part of the generic tree.
3173
3174 Save_Parent := Parent_Spec (N);
3175 Set_Parent_Spec (N, Empty);
3176
3177 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3178 Set_Parent_Spec (New_N, Save_Parent);
3179 Rewrite (N, New_N);
3180
3181 -- Once the contents of the generic copy and the template are swapped,
3182 -- do the same for their respective aspect specifications.
3183
3184 Exchange_Aspects (N, New_N);
3185
3186 -- Collect all contract-related source pragmas found within the template
3187 -- and attach them to the contract of the package spec. This contract is
3188 -- used in the capture of global references within annotations.
3189
3190 Create_Generic_Contract (N);
3191
3192 Id := Defining_Entity (N);
3193 Generate_Definition (Id);
3194
3195 -- Expansion is not applied to generic units
3196
3197 Start_Generic;
3198
3199 Enter_Name (Id);
3200 Set_Ekind (Id, E_Generic_Package);
3201 Set_Etype (Id, Standard_Void_Type);
3202
3203 -- A generic package declared within a Ghost region is rendered Ghost
3204 -- (SPARK RM 6.9(2)).
3205
3206 if Ghost_Mode > None then
3207 Set_Is_Ghost_Entity (Id);
3208 end if;
3209
3210 -- Analyze aspects now, so that generated pragmas appear in the
3211 -- declarations before building and analyzing the generic copy.
3212
3213 if Has_Aspects (N) then
3214 Analyze_Aspect_Specifications (N, Id);
3215 end if;
3216
3217 Push_Scope (Id);
3218 Enter_Generic_Scope (Id);
3219 Set_Inner_Instances (Id, New_Elmt_List);
3220
3221 Set_Categorization_From_Pragmas (N);
3222 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3223
3224 -- Link the declaration of the generic homonym in the generic copy to
3225 -- the package it renames, so that it is always resolved properly.
3226
3227 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3228 Set_Entity (Associated_Node (Name (Renaming)), Id);
3229
3230 -- For a library unit, we have reconstructed the entity for the unit,
3231 -- and must reset it in the library tables.
3232
3233 if Nkind (Parent (N)) = N_Compilation_Unit then
3234 Set_Cunit_Entity (Current_Sem_Unit, Id);
3235 end if;
3236
3237 Analyze_Generic_Formal_Part (N);
3238
3239 -- After processing the generic formals, analysis proceeds as for a
3240 -- non-generic package.
3241
3242 Analyze (Specification (N));
3243
3244 Validate_Categorization_Dependency (N, Id);
3245
3246 End_Generic;
3247
3248 End_Package_Scope (Id);
3249 Exit_Generic_Scope (Id);
3250
3251 if Nkind (Parent (N)) /= N_Compilation_Unit then
3252 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3253 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3254 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3255
3256 else
3257 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3258 Validate_RT_RAT_Component (N);
3259
3260 -- If this is a spec without a body, check that generic parameters
3261 -- are referenced.
3262
3263 if not Body_Required (Parent (N)) then
3264 Check_References (Id);
3265 end if;
3266 end if;
3267
3268 -- If there is a specified storage pool in the context, create an
3269 -- aspect on the package declaration, so that it is used in any
3270 -- instance that does not override it.
3271
3272 if Present (Default_Pool) then
3273 declare
3274 ASN : Node_Id;
3275
3276 begin
3277 ASN :=
3278 Make_Aspect_Specification (Loc,
3279 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3280 Expression => New_Copy (Default_Pool));
3281
3282 if No (Aspect_Specifications (Specification (N))) then
3283 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3284 else
3285 Append (ASN, Aspect_Specifications (Specification (N)));
3286 end if;
3287 end;
3288 end if;
3289 end Analyze_Generic_Package_Declaration;
3290
3291 --------------------------------------------
3292 -- Analyze_Generic_Subprogram_Declaration --
3293 --------------------------------------------
3294
3295 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3296 Formals : List_Id;
3297 Id : Entity_Id;
3298 New_N : Node_Id;
3299 Result_Type : Entity_Id;
3300 Save_Parent : Node_Id;
3301 Spec : Node_Id;
3302 Typ : Entity_Id;
3303
3304 begin
3305 -- The generic subprogram declaration may be subject to pragma Ghost
3306 -- with policy Ignore. Set the mode now to ensure that any nodes
3307 -- generated during analysis and expansion are properly flagged as
3308 -- ignored Ghost.
3309
3310 Set_Ghost_Mode (N);
3311 Check_SPARK_05_Restriction ("generic is not allowed", N);
3312
3313 -- Create copy of generic unit, and save for instantiation. If the unit
3314 -- is a child unit, do not copy the specifications for the parent, which
3315 -- are not part of the generic tree.
3316
3317 Save_Parent := Parent_Spec (N);
3318 Set_Parent_Spec (N, Empty);
3319
3320 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3321 Set_Parent_Spec (New_N, Save_Parent);
3322 Rewrite (N, New_N);
3323
3324 -- Once the contents of the generic copy and the template are swapped,
3325 -- do the same for their respective aspect specifications.
3326
3327 Exchange_Aspects (N, New_N);
3328
3329 -- Collect all contract-related source pragmas found within the template
3330 -- and attach them to the contract of the subprogram spec. This contract
3331 -- is used in the capture of global references within annotations.
3332
3333 Create_Generic_Contract (N);
3334
3335 Spec := Specification (N);
3336 Id := Defining_Entity (Spec);
3337 Generate_Definition (Id);
3338
3339 if Nkind (Id) = N_Defining_Operator_Symbol then
3340 Error_Msg_N
3341 ("operator symbol not allowed for generic subprogram", Id);
3342 end if;
3343
3344 Start_Generic;
3345
3346 Enter_Name (Id);
3347 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3348
3349 -- Analyze the aspects of the generic copy to ensure that all generated
3350 -- pragmas (if any) perform their semantic effects.
3351
3352 if Has_Aspects (N) then
3353 Analyze_Aspect_Specifications (N, Id);
3354 end if;
3355
3356 Push_Scope (Id);
3357 Enter_Generic_Scope (Id);
3358 Set_Inner_Instances (Id, New_Elmt_List);
3359 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3360
3361 Analyze_Generic_Formal_Part (N);
3362
3363 Formals := Parameter_Specifications (Spec);
3364
3365 if Present (Formals) then
3366 Process_Formals (Formals, Spec);
3367 end if;
3368
3369 if Nkind (Spec) = N_Function_Specification then
3370 Set_Ekind (Id, E_Generic_Function);
3371
3372 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3373 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3374 Set_Etype (Id, Result_Type);
3375
3376 -- Check restriction imposed by AI05-073: a generic function
3377 -- cannot return an abstract type or an access to such.
3378
3379 -- This is a binding interpretation should it apply to earlier
3380 -- versions of Ada as well as Ada 2012???
3381
3382 if Is_Abstract_Type (Designated_Type (Result_Type))
3383 and then Ada_Version >= Ada_2012
3384 then
3385 Error_Msg_N
3386 ("generic function cannot have an access result "
3387 & "that designates an abstract type", Spec);
3388 end if;
3389
3390 else
3391 Find_Type (Result_Definition (Spec));
3392 Typ := Entity (Result_Definition (Spec));
3393
3394 if Is_Abstract_Type (Typ)
3395 and then Ada_Version >= Ada_2012
3396 then
3397 Error_Msg_N
3398 ("generic function cannot have abstract result type", Spec);
3399 end if;
3400
3401 -- If a null exclusion is imposed on the result type, then create
3402 -- a null-excluding itype (an access subtype) and use it as the
3403 -- function's Etype.
3404
3405 if Is_Access_Type (Typ)
3406 and then Null_Exclusion_Present (Spec)
3407 then
3408 Set_Etype (Id,
3409 Create_Null_Excluding_Itype
3410 (T => Typ,
3411 Related_Nod => Spec,
3412 Scope_Id => Defining_Unit_Name (Spec)));
3413 else
3414 Set_Etype (Id, Typ);
3415 end if;
3416 end if;
3417
3418 else
3419 Set_Ekind (Id, E_Generic_Procedure);
3420 Set_Etype (Id, Standard_Void_Type);
3421 end if;
3422
3423 -- A generic subprogram declared within a Ghost region is rendered Ghost
3424 -- (SPARK RM 6.9(2)).
3425
3426 if Ghost_Mode > None then
3427 Set_Is_Ghost_Entity (Id);
3428 end if;
3429
3430 -- For a library unit, we have reconstructed the entity for the unit,
3431 -- and must reset it in the library tables. We also make sure that
3432 -- Body_Required is set properly in the original compilation unit node.
3433
3434 if Nkind (Parent (N)) = N_Compilation_Unit then
3435 Set_Cunit_Entity (Current_Sem_Unit, Id);
3436 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3437 end if;
3438
3439 Set_Categorization_From_Pragmas (N);
3440 Validate_Categorization_Dependency (N, Id);
3441
3442 -- Capture all global references that occur within the profile of the
3443 -- generic subprogram. Aspects are not part of this processing because
3444 -- they must be delayed. If processed now, Save_Global_References will
3445 -- destroy the Associated_Node links and prevent the capture of global
3446 -- references when the contract of the generic subprogram is analyzed.
3447
3448 Save_Global_References (Original_Node (N));
3449
3450 End_Generic;
3451 End_Scope;
3452 Exit_Generic_Scope (Id);
3453 Generate_Reference_To_Formals (Id);
3454
3455 List_Inherited_Pre_Post_Aspects (Id);
3456 end Analyze_Generic_Subprogram_Declaration;
3457
3458 -----------------------------------
3459 -- Analyze_Package_Instantiation --
3460 -----------------------------------
3461
3462 procedure Analyze_Package_Instantiation (N : Node_Id) is
3463 Loc : constant Source_Ptr := Sloc (N);
3464 Gen_Id : constant Node_Id := Name (N);
3465
3466 Act_Decl : Node_Id;
3467 Act_Decl_Name : Node_Id;
3468 Act_Decl_Id : Entity_Id;
3469 Act_Spec : Node_Id;
3470 Act_Tree : Node_Id;
3471
3472 Gen_Decl : Node_Id;
3473 Gen_Spec : Node_Id;
3474 Gen_Unit : Entity_Id;
3475
3476 Is_Actual_Pack : constant Boolean :=
3477 Is_Internal (Defining_Entity (N));
3478
3479 Env_Installed : Boolean := False;
3480 Parent_Installed : Boolean := False;
3481 Renaming_List : List_Id;
3482 Unit_Renaming : Node_Id;
3483 Needs_Body : Boolean;
3484 Inline_Now : Boolean := False;
3485 Has_Inline_Always : Boolean := False;
3486
3487 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
3488 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
3489
3490 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
3491 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
3492 -- Save the SPARK_Mode-related data for restore on exit
3493
3494 Save_Style_Check : constant Boolean := Style_Check;
3495 -- Save style check mode for restore on exit
3496
3497 procedure Delay_Descriptors (E : Entity_Id);
3498 -- Delay generation of subprogram descriptors for given entity
3499
3500 function Might_Inline_Subp return Boolean;
3501 -- If inlining is active and the generic contains inlined subprograms,
3502 -- we instantiate the body. This may cause superfluous instantiations,
3503 -- but it is simpler than detecting the need for the body at the point
3504 -- of inlining, when the context of the instance is not available.
3505
3506 -----------------------
3507 -- Delay_Descriptors --
3508 -----------------------
3509
3510 procedure Delay_Descriptors (E : Entity_Id) is
3511 begin
3512 if not Delay_Subprogram_Descriptors (E) then
3513 Set_Delay_Subprogram_Descriptors (E);
3514 Pending_Descriptor.Append (E);
3515 end if;
3516 end Delay_Descriptors;
3517
3518 -----------------------
3519 -- Might_Inline_Subp --
3520 -----------------------
3521
3522 function Might_Inline_Subp return Boolean is
3523 E : Entity_Id;
3524
3525 begin
3526 if not Inline_Processing_Required then
3527 return False;
3528
3529 else
3530 E := First_Entity (Gen_Unit);
3531 while Present (E) loop
3532 if Is_Subprogram (E) and then Is_Inlined (E) then
3533 -- Remember if there are any subprograms with Inline_Always
3534
3535 if Has_Pragma_Inline_Always (E) then
3536 Has_Inline_Always := True;
3537 end if;
3538
3539 return True;
3540 end if;
3541
3542 Next_Entity (E);
3543 end loop;
3544 end if;
3545
3546 return False;
3547 end Might_Inline_Subp;
3548
3549 -- Local declarations
3550
3551 Vis_Prims_List : Elist_Id := No_Elist;
3552 -- List of primitives made temporarily visible in the instantiation
3553 -- to match the visibility of the formal type
3554
3555 -- Start of processing for Analyze_Package_Instantiation
3556
3557 begin
3558 Check_SPARK_05_Restriction ("generic is not allowed", N);
3559
3560 -- Very first thing: check for Text_IO sp[ecial unit in case we are
3561 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3562
3563 Check_Text_IO_Special_Unit (Name (N));
3564
3565 -- Make node global for error reporting
3566
3567 Instantiation_Node := N;
3568
3569 -- Turn off style checking in instances. If the check is enabled on the
3570 -- generic unit, a warning in an instance would just be noise. If not
3571 -- enabled on the generic, then a warning in an instance is just wrong.
3572
3573 Style_Check := False;
3574
3575 -- Case of instantiation of a generic package
3576
3577 if Nkind (N) = N_Package_Instantiation then
3578 Act_Decl_Id := New_Copy (Defining_Entity (N));
3579 Set_Comes_From_Source (Act_Decl_Id, True);
3580
3581 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3582 Act_Decl_Name :=
3583 Make_Defining_Program_Unit_Name (Loc,
3584 Name =>
3585 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3586 Defining_Identifier => Act_Decl_Id);
3587 else
3588 Act_Decl_Name := Act_Decl_Id;
3589 end if;
3590
3591 -- Case of instantiation of a formal package
3592
3593 else
3594 Act_Decl_Id := Defining_Identifier (N);
3595 Act_Decl_Name := Act_Decl_Id;
3596 end if;
3597
3598 Generate_Definition (Act_Decl_Id);
3599 Preanalyze_Actuals (N);
3600
3601 Init_Env;
3602 Env_Installed := True;
3603
3604 -- Reset renaming map for formal types. The mapping is established
3605 -- when analyzing the generic associations, but some mappings are
3606 -- inherited from formal packages of parent units, and these are
3607 -- constructed when the parents are installed.
3608
3609 Generic_Renamings.Set_Last (0);
3610 Generic_Renamings_HTable.Reset;
3611
3612 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3613 Gen_Unit := Entity (Gen_Id);
3614
3615 -- Verify that it is the name of a generic package
3616
3617 -- A visibility glitch: if the instance is a child unit and the generic
3618 -- is the generic unit of a parent instance (i.e. both the parent and
3619 -- the child units are instances of the same package) the name now
3620 -- denotes the renaming within the parent, not the intended generic
3621 -- unit. See if there is a homonym that is the desired generic. The
3622 -- renaming declaration must be visible inside the instance of the
3623 -- child, but not when analyzing the name in the instantiation itself.
3624
3625 if Ekind (Gen_Unit) = E_Package
3626 and then Present (Renamed_Entity (Gen_Unit))
3627 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3628 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3629 and then Present (Homonym (Gen_Unit))
3630 then
3631 Gen_Unit := Homonym (Gen_Unit);
3632 end if;
3633
3634 if Etype (Gen_Unit) = Any_Type then
3635 Restore_Env;
3636 goto Leave;
3637
3638 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3639
3640 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3641
3642 if From_Limited_With (Gen_Unit) then
3643 Error_Msg_N
3644 ("cannot instantiate a limited withed package", Gen_Id);
3645 else
3646 Error_Msg_NE
3647 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3648 end if;
3649
3650 Restore_Env;
3651 goto Leave;
3652 end if;
3653
3654 if In_Extended_Main_Source_Unit (N) then
3655 Set_Is_Instantiated (Gen_Unit);
3656 Generate_Reference (Gen_Unit, N);
3657
3658 if Present (Renamed_Object (Gen_Unit)) then
3659 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3660 Generate_Reference (Renamed_Object (Gen_Unit), N);
3661 end if;
3662 end if;
3663
3664 if Nkind (Gen_Id) = N_Identifier
3665 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3666 then
3667 Error_Msg_NE
3668 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3669
3670 elsif Nkind (Gen_Id) = N_Expanded_Name
3671 and then Is_Child_Unit (Gen_Unit)
3672 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3673 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3674 then
3675 Error_Msg_N
3676 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3677 end if;
3678
3679 Set_Entity (Gen_Id, Gen_Unit);
3680
3681 -- If generic is a renaming, get original generic unit
3682
3683 if Present (Renamed_Object (Gen_Unit))
3684 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3685 then
3686 Gen_Unit := Renamed_Object (Gen_Unit);
3687 end if;
3688
3689 -- Verify that there are no circular instantiations
3690
3691 if In_Open_Scopes (Gen_Unit) then
3692 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3693 Restore_Env;
3694 goto Leave;
3695
3696 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3697 Error_Msg_Node_2 := Current_Scope;
3698 Error_Msg_NE
3699 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3700 Circularity_Detected := True;
3701 Restore_Env;
3702 goto Leave;
3703
3704 else
3705 -- If the context of the instance is subject to SPARK_Mode "off",
3706 -- set the global flag which signals Analyze_Pragma to ignore all
3707 -- SPARK_Mode pragmas within the instance.
3708
3709 if SPARK_Mode = Off then
3710 Ignore_Pragma_SPARK_Mode := True;
3711 end if;
3712
3713 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3714 Gen_Spec := Specification (Gen_Decl);
3715
3716 -- Initialize renamings map, for error checking, and the list that
3717 -- holds private entities whose views have changed between generic
3718 -- definition and instantiation. If this is the instance created to
3719 -- validate an actual package, the instantiation environment is that
3720 -- of the enclosing instance.
3721
3722 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3723
3724 -- Copy original generic tree, to produce text for instantiation
3725
3726 Act_Tree :=
3727 Copy_Generic_Node
3728 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3729
3730 Act_Spec := Specification (Act_Tree);
3731
3732 -- If this is the instance created to validate an actual package,
3733 -- only the formals matter, do not examine the package spec itself.
3734
3735 if Is_Actual_Pack then
3736 Set_Visible_Declarations (Act_Spec, New_List);
3737 Set_Private_Declarations (Act_Spec, New_List);
3738 end if;
3739
3740 Renaming_List :=
3741 Analyze_Associations
3742 (I_Node => N,
3743 Formals => Generic_Formal_Declarations (Act_Tree),
3744 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3745
3746 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3747
3748 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3749 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3750 Set_Is_Generic_Instance (Act_Decl_Id);
3751 Set_Generic_Parent (Act_Spec, Gen_Unit);
3752
3753 -- References to the generic in its own declaration or its body are
3754 -- references to the instance. Add a renaming declaration for the
3755 -- generic unit itself. This declaration, as well as the renaming
3756 -- declarations for the generic formals, must remain private to the
3757 -- unit: the formals, because this is the language semantics, and
3758 -- the unit because its use is an artifact of the implementation.
3759
3760 Unit_Renaming :=
3761 Make_Package_Renaming_Declaration (Loc,
3762 Defining_Unit_Name =>
3763 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3764 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3765
3766 Append (Unit_Renaming, Renaming_List);
3767
3768 -- The renaming declarations are the first local declarations of the
3769 -- new unit.
3770
3771 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3772 Insert_List_Before
3773 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3774 else
3775 Set_Visible_Declarations (Act_Spec, Renaming_List);
3776 end if;
3777
3778 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
3779
3780 -- Propagate the aspect specifications from the package declaration
3781 -- template to the instantiated version of the package declaration.
3782
3783 if Has_Aspects (Act_Tree) then
3784 Set_Aspect_Specifications (Act_Decl,
3785 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3786 end if;
3787
3788 -- The generic may have a generated Default_Storage_Pool aspect,
3789 -- set at the point of generic declaration. If the instance has
3790 -- that aspect, it overrides the one inherited from the generic.
3791
3792 if Has_Aspects (Gen_Spec) then
3793 if No (Aspect_Specifications (N)) then
3794 Set_Aspect_Specifications (N,
3795 (New_Copy_List_Tree
3796 (Aspect_Specifications (Gen_Spec))));
3797
3798 else
3799 declare
3800 ASN1, ASN2 : Node_Id;
3801
3802 begin
3803 ASN1 := First (Aspect_Specifications (N));
3804 while Present (ASN1) loop
3805 if Chars (Identifier (ASN1)) = Name_Default_Storage_Pool
3806 then
3807 -- If generic carries a default storage pool, remove
3808 -- it in favor of the instance one.
3809
3810 ASN2 := First (Aspect_Specifications (Gen_Spec));
3811 while Present (ASN2) loop
3812 if Chars (Identifier (ASN2)) =
3813 Name_Default_Storage_Pool
3814 then
3815 Remove (ASN2);
3816 exit;
3817 end if;
3818
3819 Next (ASN2);
3820 end loop;
3821 end if;
3822
3823 Next (ASN1);
3824 end loop;
3825
3826 Prepend_List_To (Aspect_Specifications (N),
3827 (New_Copy_List_Tree
3828 (Aspect_Specifications (Gen_Spec))));
3829 end;
3830 end if;
3831 end if;
3832
3833 -- Save the instantiation node, for subsequent instantiation of the
3834 -- body, if there is one and we are generating code for the current
3835 -- unit. Mark unit as having a body (avoids premature error message).
3836
3837 -- We instantiate the body if we are generating code, if we are
3838 -- generating cross-reference information, or if we are building
3839 -- trees for ASIS use or GNATprove use.
3840
3841 declare
3842 Enclosing_Body_Present : Boolean := False;
3843 -- If the generic unit is not a compilation unit, then a body may
3844 -- be present in its parent even if none is required. We create a
3845 -- tentative pending instantiation for the body, which will be
3846 -- discarded if none is actually present.
3847
3848 Scop : Entity_Id;
3849
3850 begin
3851 if Scope (Gen_Unit) /= Standard_Standard
3852 and then not Is_Child_Unit (Gen_Unit)
3853 then
3854 Scop := Scope (Gen_Unit);
3855 while Present (Scop) and then Scop /= Standard_Standard loop
3856 if Unit_Requires_Body (Scop) then
3857 Enclosing_Body_Present := True;
3858 exit;
3859
3860 elsif In_Open_Scopes (Scop)
3861 and then In_Package_Body (Scop)
3862 then
3863 Enclosing_Body_Present := True;
3864 exit;
3865 end if;
3866
3867 exit when Is_Compilation_Unit (Scop);
3868 Scop := Scope (Scop);
3869 end loop;
3870 end if;
3871
3872 -- If front-end inlining is enabled or there are any subprograms
3873 -- marked with Inline_Always, and this is a unit for which code
3874 -- will be generated, we instantiate the body at once.
3875
3876 -- This is done if the instance is not the main unit, and if the
3877 -- generic is not a child unit of another generic, to avoid scope
3878 -- problems and the reinstallation of parent instances.
3879
3880 if Expander_Active
3881 and then (not Is_Child_Unit (Gen_Unit)
3882 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3883 and then Might_Inline_Subp
3884 and then not Is_Actual_Pack
3885 then
3886 if not Back_End_Inlining
3887 and then (Front_End_Inlining or else Has_Inline_Always)
3888 and then (Is_In_Main_Unit (N)
3889 or else In_Main_Context (Current_Scope))
3890 and then Nkind (Parent (N)) /= N_Compilation_Unit
3891 then
3892 Inline_Now := True;
3893
3894 -- In configurable_run_time mode we force the inlining of
3895 -- predefined subprograms marked Inline_Always, to minimize
3896 -- the use of the run-time library.
3897
3898 elsif Is_Predefined_File_Name
3899 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3900 and then Configurable_Run_Time_Mode
3901 and then Nkind (Parent (N)) /= N_Compilation_Unit
3902 then
3903 Inline_Now := True;
3904 end if;
3905
3906 -- If the current scope is itself an instance within a child
3907 -- unit, there will be duplications in the scope stack, and the
3908 -- unstacking mechanism in Inline_Instance_Body will fail.
3909 -- This loses some rare cases of optimization, and might be
3910 -- improved some day, if we can find a proper abstraction for
3911 -- "the complete compilation context" that can be saved and
3912 -- restored. ???
3913
3914 if Is_Generic_Instance (Current_Scope) then
3915 declare
3916 Curr_Unit : constant Entity_Id :=
3917 Cunit_Entity (Current_Sem_Unit);
3918 begin
3919 if Curr_Unit /= Current_Scope
3920 and then Is_Child_Unit (Curr_Unit)
3921 then
3922 Inline_Now := False;
3923 end if;
3924 end;
3925 end if;
3926 end if;
3927
3928 Needs_Body :=
3929 (Unit_Requires_Body (Gen_Unit)
3930 or else Enclosing_Body_Present
3931 or else Present (Corresponding_Body (Gen_Decl)))
3932 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
3933 and then not Is_Actual_Pack
3934 and then not Inline_Now
3935 and then (Operating_Mode = Generate_Code
3936
3937 -- Need comment for this check ???
3938
3939 or else (Operating_Mode = Check_Semantics
3940 and then (ASIS_Mode or GNATprove_Mode)));
3941
3942 -- If front-end inlining is enabled or there are any subprograms
3943 -- marked with Inline_Always, do not instantiate body when within
3944 -- a generic context.
3945
3946 if ((Front_End_Inlining or else Has_Inline_Always)
3947 and then not Expander_Active)
3948 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
3949 then
3950 Needs_Body := False;
3951 end if;
3952
3953 -- If the current context is generic, and the package being
3954 -- instantiated is declared within a formal package, there is no
3955 -- body to instantiate until the enclosing generic is instantiated
3956 -- and there is an actual for the formal package. If the formal
3957 -- package has parameters, we build a regular package instance for
3958 -- it, that precedes the original formal package declaration.
3959
3960 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
3961 declare
3962 Decl : constant Node_Id :=
3963 Original_Node
3964 (Unit_Declaration_Node (Scope (Gen_Unit)));
3965 begin
3966 if Nkind (Decl) = N_Formal_Package_Declaration
3967 or else (Nkind (Decl) = N_Package_Declaration
3968 and then Is_List_Member (Decl)
3969 and then Present (Next (Decl))
3970 and then
3971 Nkind (Next (Decl)) =
3972 N_Formal_Package_Declaration)
3973 then
3974 Needs_Body := False;
3975 end if;
3976 end;
3977 end if;
3978 end;
3979
3980 -- For RCI unit calling stubs, we omit the instance body if the
3981 -- instance is the RCI library unit itself.
3982
3983 -- However there is a special case for nested instances: in this case
3984 -- we do generate the instance body, as it might be required, e.g.
3985 -- because it provides stream attributes for some type used in the
3986 -- profile of a remote subprogram. This is consistent with 12.3(12),
3987 -- which indicates that the instance body occurs at the place of the
3988 -- instantiation, and thus is part of the RCI declaration, which is
3989 -- present on all client partitions (this is E.2.3(18)).
3990
3991 -- Note that AI12-0002 may make it illegal at some point to have
3992 -- stream attributes defined in an RCI unit, in which case this
3993 -- special case will become unnecessary. In the meantime, there
3994 -- is known application code in production that depends on this
3995 -- being possible, so we definitely cannot eliminate the body in
3996 -- the case of nested instances for the time being.
3997
3998 -- When we generate a nested instance body, calling stubs for any
3999 -- relevant subprogram will be be inserted immediately after the
4000 -- subprogram declarations, and will take precedence over the
4001 -- subsequent (original) body. (The stub and original body will be
4002 -- complete homographs, but this is permitted in an instance).
4003 -- (Could we do better and remove the original body???)
4004
4005 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4006 and then Comes_From_Source (N)
4007 and then Nkind (Parent (N)) = N_Compilation_Unit
4008 then
4009 Needs_Body := False;
4010 end if;
4011
4012 if Needs_Body then
4013
4014 -- Here is a defence against a ludicrous number of instantiations
4015 -- caused by a circular set of instantiation attempts.
4016
4017 if Pending_Instantiations.Last > Maximum_Instantiations then
4018 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4019 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4020 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4021 raise Unrecoverable_Error;
4022 end if;
4023
4024 -- Indicate that the enclosing scopes contain an instantiation,
4025 -- and that cleanup actions should be delayed until after the
4026 -- instance body is expanded.
4027
4028 Check_Forward_Instantiation (Gen_Decl);
4029 if Nkind (N) = N_Package_Instantiation then
4030 declare
4031 Enclosing_Master : Entity_Id;
4032
4033 begin
4034 -- Loop to search enclosing masters
4035
4036 Enclosing_Master := Current_Scope;
4037 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4038 if Ekind (Enclosing_Master) = E_Package then
4039 if Is_Compilation_Unit (Enclosing_Master) then
4040 if In_Package_Body (Enclosing_Master) then
4041 Delay_Descriptors
4042 (Body_Entity (Enclosing_Master));
4043 else
4044 Delay_Descriptors
4045 (Enclosing_Master);
4046 end if;
4047
4048 exit Scope_Loop;
4049
4050 else
4051 Enclosing_Master := Scope (Enclosing_Master);
4052 end if;
4053
4054 elsif Is_Generic_Unit (Enclosing_Master)
4055 or else Ekind (Enclosing_Master) = E_Void
4056 then
4057 -- Cleanup actions will eventually be performed on the
4058 -- enclosing subprogram or package instance, if any.
4059 -- Enclosing scope is void in the formal part of a
4060 -- generic subprogram.
4061
4062 exit Scope_Loop;
4063
4064 else
4065 if Ekind (Enclosing_Master) = E_Entry
4066 and then
4067 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4068 then
4069 if not Expander_Active then
4070 exit Scope_Loop;
4071 else
4072 Enclosing_Master :=
4073 Protected_Body_Subprogram (Enclosing_Master);
4074 end if;
4075 end if;
4076
4077 Set_Delay_Cleanups (Enclosing_Master);
4078
4079 while Ekind (Enclosing_Master) = E_Block loop
4080 Enclosing_Master := Scope (Enclosing_Master);
4081 end loop;
4082
4083 if Is_Subprogram (Enclosing_Master) then
4084 Delay_Descriptors (Enclosing_Master);
4085
4086 elsif Is_Task_Type (Enclosing_Master) then
4087 declare
4088 TBP : constant Node_Id :=
4089 Get_Task_Body_Procedure
4090 (Enclosing_Master);
4091 begin
4092 if Present (TBP) then
4093 Delay_Descriptors (TBP);
4094 Set_Delay_Cleanups (TBP);
4095 end if;
4096 end;
4097 end if;
4098
4099 exit Scope_Loop;
4100 end if;
4101 end loop Scope_Loop;
4102 end;
4103
4104 -- Make entry in table
4105
4106 Pending_Instantiations.Append
4107 ((Inst_Node => N,
4108 Act_Decl => Act_Decl,
4109 Expander_Status => Expander_Active,
4110 Current_Sem_Unit => Current_Sem_Unit,
4111 Scope_Suppress => Scope_Suppress,
4112 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4113 Version => Ada_Version,
4114 Version_Pragma => Ada_Version_Pragma,
4115 Warnings => Save_Warnings,
4116 SPARK_Mode => SPARK_Mode,
4117 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4118 end if;
4119 end if;
4120
4121 Set_Categorization_From_Pragmas (Act_Decl);
4122
4123 if Parent_Installed then
4124 Hide_Current_Scope;
4125 end if;
4126
4127 Set_Instance_Spec (N, Act_Decl);
4128
4129 -- If not a compilation unit, insert the package declaration before
4130 -- the original instantiation node.
4131
4132 if Nkind (Parent (N)) /= N_Compilation_Unit then
4133 Mark_Rewrite_Insertion (Act_Decl);
4134 Insert_Before (N, Act_Decl);
4135
4136 if Has_Aspects (N) then
4137 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4138
4139 -- The pragma created for a Default_Storage_Pool aspect must
4140 -- appear ahead of the declarations in the instance spec.
4141 -- Analysis has placed it after the instance node, so remove
4142 -- it and reinsert it properly now.
4143
4144 declare
4145 ASN : constant Node_Id := First (Aspect_Specifications (N));
4146 A_Name : constant Name_Id := Chars (Identifier (ASN));
4147 Decl : Node_Id;
4148
4149 begin
4150 if A_Name = Name_Default_Storage_Pool then
4151 if No (Visible_Declarations (Act_Spec)) then
4152 Set_Visible_Declarations (Act_Spec, New_List);
4153 end if;
4154
4155 Decl := Next (N);
4156 while Present (Decl) loop
4157 if Nkind (Decl) = N_Pragma then
4158 Remove (Decl);
4159 Prepend (Decl, Visible_Declarations (Act_Spec));
4160 exit;
4161 end if;
4162
4163 Next (Decl);
4164 end loop;
4165 end if;
4166 end;
4167 end if;
4168
4169 Analyze (Act_Decl);
4170
4171 -- For an instantiation that is a compilation unit, place
4172 -- declaration on current node so context is complete for analysis
4173 -- (including nested instantiations). If this is the main unit,
4174 -- the declaration eventually replaces the instantiation node.
4175 -- If the instance body is created later, it replaces the
4176 -- instance node, and the declaration is attached to it
4177 -- (see Build_Instance_Compilation_Unit_Nodes).
4178
4179 else
4180 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4181
4182 -- The entity for the current unit is the newly created one,
4183 -- and all semantic information is attached to it.
4184
4185 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4186
4187 -- If this is the main unit, replace the main entity as well
4188
4189 if Current_Sem_Unit = Main_Unit then
4190 Main_Unit_Entity := Act_Decl_Id;
4191 end if;
4192 end if;
4193
4194 Set_Unit (Parent (N), Act_Decl);
4195 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4196 Set_Package_Instantiation (Act_Decl_Id, N);
4197
4198 -- Process aspect specifications of the instance node, if any, to
4199 -- take into account categorization pragmas before analyzing the
4200 -- instance.
4201
4202 if Has_Aspects (N) then
4203 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4204 end if;
4205
4206 Analyze (Act_Decl);
4207 Set_Unit (Parent (N), N);
4208 Set_Body_Required (Parent (N), False);
4209
4210 -- We never need elaboration checks on instantiations, since by
4211 -- definition, the body instantiation is elaborated at the same
4212 -- time as the spec instantiation.
4213
4214 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4215 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4216 end if;
4217
4218 Check_Elab_Instantiation (N);
4219
4220 if ABE_Is_Certain (N) and then Needs_Body then
4221 Pending_Instantiations.Decrement_Last;
4222 end if;
4223
4224 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4225
4226 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4227 First_Private_Entity (Act_Decl_Id));
4228
4229 -- If the instantiation will receive a body, the unit will be
4230 -- transformed into a package body, and receive its own elaboration
4231 -- entity. Otherwise, the nature of the unit is now a package
4232 -- declaration.
4233
4234 if Nkind (Parent (N)) = N_Compilation_Unit
4235 and then not Needs_Body
4236 then
4237 Rewrite (N, Act_Decl);
4238 end if;
4239
4240 if Present (Corresponding_Body (Gen_Decl))
4241 or else Unit_Requires_Body (Gen_Unit)
4242 then
4243 Set_Has_Completion (Act_Decl_Id);
4244 end if;
4245
4246 Check_Formal_Packages (Act_Decl_Id);
4247
4248 Restore_Hidden_Primitives (Vis_Prims_List);
4249 Restore_Private_Views (Act_Decl_Id);
4250
4251 Inherit_Context (Gen_Decl, N);
4252
4253 if Parent_Installed then
4254 Remove_Parent;
4255 end if;
4256
4257 Restore_Env;
4258 Env_Installed := False;
4259 end if;
4260
4261 Validate_Categorization_Dependency (N, Act_Decl_Id);
4262
4263 -- There used to be a check here to prevent instantiations in local
4264 -- contexts if the No_Local_Allocators restriction was active. This
4265 -- check was removed by a binding interpretation in AI-95-00130/07,
4266 -- but we retain the code for documentation purposes.
4267
4268 -- if Ekind (Act_Decl_Id) /= E_Void
4269 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4270 -- then
4271 -- Check_Restriction (No_Local_Allocators, N);
4272 -- end if;
4273
4274 if Inline_Now then
4275 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4276 end if;
4277
4278 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4279 -- be used as defining identifiers for a formal package and for the
4280 -- corresponding expanded package.
4281
4282 if Nkind (N) = N_Formal_Package_Declaration then
4283 Act_Decl_Id := New_Copy (Defining_Entity (N));
4284 Set_Comes_From_Source (Act_Decl_Id, True);
4285 Set_Is_Generic_Instance (Act_Decl_Id, False);
4286 Set_Defining_Identifier (N, Act_Decl_Id);
4287 end if;
4288
4289 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4290 SPARK_Mode := Save_SM;
4291 SPARK_Mode_Pragma := Save_SMP;
4292 Style_Check := Save_Style_Check;
4293
4294 if SPARK_Mode = On then
4295 Dynamic_Elaboration_Checks := False;
4296 end if;
4297
4298 -- Check that if N is an instantiation of System.Dim_Float_IO or
4299 -- System.Dim_Integer_IO, the formal type has a dimension system.
4300
4301 if Nkind (N) = N_Package_Instantiation
4302 and then Is_Dim_IO_Package_Instantiation (N)
4303 then
4304 declare
4305 Assoc : constant Node_Id := First (Generic_Associations (N));
4306 begin
4307 if not Has_Dimension_System
4308 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4309 then
4310 Error_Msg_N ("type with a dimension system expected", Assoc);
4311 end if;
4312 end;
4313 end if;
4314
4315 <<Leave>>
4316 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4317 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4318 end if;
4319
4320 exception
4321 when Instantiation_Error =>
4322 if Parent_Installed then
4323 Remove_Parent;
4324 end if;
4325
4326 if Env_Installed then
4327 Restore_Env;
4328 end if;
4329
4330 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4331 SPARK_Mode := Save_SM;
4332 SPARK_Mode_Pragma := Save_SMP;
4333 Style_Check := Save_Style_Check;
4334
4335 if SPARK_Mode = On then
4336 Dynamic_Elaboration_Checks := False;
4337 end if;
4338 end Analyze_Package_Instantiation;
4339
4340 --------------------------
4341 -- Inline_Instance_Body --
4342 --------------------------
4343
4344 procedure Inline_Instance_Body
4345 (N : Node_Id;
4346 Gen_Unit : Entity_Id;
4347 Act_Decl : Node_Id)
4348 is
4349 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4350 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4351 Gen_Comp : constant Entity_Id :=
4352 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4353
4354 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
4355 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
4356 -- Save all SPARK_Mode-related attributes as removing enclosing scopes
4357 -- to provide a clean environment for analysis of the inlined body will
4358 -- eliminate any previously set SPARK_Mode.
4359
4360 Scope_Stack_Depth : constant Int :=
4361 Scope_Stack.Last - Scope_Stack.First + 1;
4362
4363 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4364 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4365 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4366 Curr_Scope : Entity_Id := Empty;
4367 List : Elist_Id;
4368 Num_Inner : Int := 0;
4369 Num_Scopes : Int := 0;
4370 N_Instances : Int := 0;
4371 Removed : Boolean := False;
4372 S : Entity_Id;
4373 Vis : Boolean;
4374
4375 begin
4376 -- Case of generic unit defined in another unit. We must remove the
4377 -- complete context of the current unit to install that of the generic.
4378
4379 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4380
4381 -- Add some comments for the following two loops ???
4382
4383 S := Current_Scope;
4384 while Present (S) and then S /= Standard_Standard loop
4385 loop
4386 Num_Scopes := Num_Scopes + 1;
4387
4388 Use_Clauses (Num_Scopes) :=
4389 (Scope_Stack.Table
4390 (Scope_Stack.Last - Num_Scopes + 1).
4391 First_Use_Clause);
4392 End_Use_Clauses (Use_Clauses (Num_Scopes));
4393
4394 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4395 or else Scope_Stack.Table
4396 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4397 end loop;
4398
4399 exit when Is_Generic_Instance (S)
4400 and then (In_Package_Body (S)
4401 or else Ekind (S) = E_Procedure
4402 or else Ekind (S) = E_Function);
4403 S := Scope (S);
4404 end loop;
4405
4406 Vis := Is_Immediately_Visible (Gen_Comp);
4407
4408 -- Find and save all enclosing instances
4409
4410 S := Current_Scope;
4411
4412 while Present (S)
4413 and then S /= Standard_Standard
4414 loop
4415 if Is_Generic_Instance (S) then
4416 N_Instances := N_Instances + 1;
4417 Instances (N_Instances) := S;
4418
4419 exit when In_Package_Body (S);
4420 end if;
4421
4422 S := Scope (S);
4423 end loop;
4424
4425 -- Remove context of current compilation unit, unless we are within a
4426 -- nested package instantiation, in which case the context has been
4427 -- removed previously.
4428
4429 -- If current scope is the body of a child unit, remove context of
4430 -- spec as well. If an enclosing scope is an instance body, the
4431 -- context has already been removed, but the entities in the body
4432 -- must be made invisible as well.
4433
4434 S := Current_Scope;
4435 while Present (S) and then S /= Standard_Standard loop
4436 if Is_Generic_Instance (S)
4437 and then (In_Package_Body (S)
4438 or else Ekind_In (S, E_Procedure, E_Function))
4439 then
4440 -- We still have to remove the entities of the enclosing
4441 -- instance from direct visibility.
4442
4443 declare
4444 E : Entity_Id;
4445 begin
4446 E := First_Entity (S);
4447 while Present (E) loop
4448 Set_Is_Immediately_Visible (E, False);
4449 Next_Entity (E);
4450 end loop;
4451 end;
4452
4453 exit;
4454 end if;
4455
4456 if S = Curr_Unit
4457 or else (Ekind (Curr_Unit) = E_Package_Body
4458 and then S = Spec_Entity (Curr_Unit))
4459 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4460 and then S = Corresponding_Spec
4461 (Unit_Declaration_Node (Curr_Unit)))
4462 then
4463 Removed := True;
4464
4465 -- Remove entities in current scopes from visibility, so that
4466 -- instance body is compiled in a clean environment.
4467
4468 List := Save_Scope_Stack (Handle_Use => False);
4469
4470 if Is_Child_Unit (S) then
4471
4472 -- Remove child unit from stack, as well as inner scopes.
4473 -- Removing the context of a child unit removes parent units
4474 -- as well.
4475
4476 while Current_Scope /= S loop
4477 Num_Inner := Num_Inner + 1;
4478 Inner_Scopes (Num_Inner) := Current_Scope;
4479 Pop_Scope;
4480 end loop;
4481
4482 Pop_Scope;
4483 Remove_Context (Curr_Comp);
4484 Curr_Scope := S;
4485
4486 else
4487 Remove_Context (Curr_Comp);
4488 end if;
4489
4490 if Ekind (Curr_Unit) = E_Package_Body then
4491 Remove_Context (Library_Unit (Curr_Comp));
4492 end if;
4493 end if;
4494
4495 S := Scope (S);
4496 end loop;
4497
4498 pragma Assert (Num_Inner < Num_Scopes);
4499
4500 -- The inlined package body must be analyzed with the SPARK_Mode of
4501 -- the enclosing context, otherwise the body may cause bogus errors
4502 -- if a configuration SPARK_Mode pragma in in effect.
4503
4504 Push_Scope (Standard_Standard);
4505 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4506 Instantiate_Package_Body
4507 (Body_Info =>
4508 ((Inst_Node => N,
4509 Act_Decl => Act_Decl,
4510 Expander_Status => Expander_Active,
4511 Current_Sem_Unit => Current_Sem_Unit,
4512 Scope_Suppress => Scope_Suppress,
4513 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4514 Version => Ada_Version,
4515 Version_Pragma => Ada_Version_Pragma,
4516 Warnings => Save_Warnings,
4517 SPARK_Mode => Save_SM,
4518 SPARK_Mode_Pragma => Save_SMP)),
4519 Inlined_Body => True);
4520
4521 Pop_Scope;
4522
4523 -- Restore context
4524
4525 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4526
4527 -- Reset Generic_Instance flag so that use clauses can be installed
4528 -- in the proper order. (See Use_One_Package for effect of enclosing
4529 -- instances on processing of use clauses).
4530
4531 for J in 1 .. N_Instances loop
4532 Set_Is_Generic_Instance (Instances (J), False);
4533 end loop;
4534
4535 if Removed then
4536 Install_Context (Curr_Comp);
4537
4538 if Present (Curr_Scope)
4539 and then Is_Child_Unit (Curr_Scope)
4540 then
4541 Push_Scope (Curr_Scope);
4542 Set_Is_Immediately_Visible (Curr_Scope);
4543
4544 -- Finally, restore inner scopes as well
4545
4546 for J in reverse 1 .. Num_Inner loop
4547 Push_Scope (Inner_Scopes (J));
4548 end loop;
4549 end if;
4550
4551 Restore_Scope_Stack (List, Handle_Use => False);
4552
4553 if Present (Curr_Scope)
4554 and then
4555 (In_Private_Part (Curr_Scope)
4556 or else In_Package_Body (Curr_Scope))
4557 then
4558 -- Install private declaration of ancestor units, which are
4559 -- currently available. Restore_Scope_Stack and Install_Context
4560 -- only install the visible part of parents.
4561
4562 declare
4563 Par : Entity_Id;
4564 begin
4565 Par := Scope (Curr_Scope);
4566 while (Present (Par)) and then Par /= Standard_Standard loop
4567 Install_Private_Declarations (Par);
4568 Par := Scope (Par);
4569 end loop;
4570 end;
4571 end if;
4572 end if;
4573
4574 -- Restore use clauses. For a child unit, use clauses in the parents
4575 -- are restored when installing the context, so only those in inner
4576 -- scopes (and those local to the child unit itself) need to be
4577 -- installed explicitly.
4578
4579 if Is_Child_Unit (Curr_Unit) and then Removed then
4580 for J in reverse 1 .. Num_Inner + 1 loop
4581 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4582 Use_Clauses (J);
4583 Install_Use_Clauses (Use_Clauses (J));
4584 end loop;
4585
4586 else
4587 for J in reverse 1 .. Num_Scopes loop
4588 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4589 Use_Clauses (J);
4590 Install_Use_Clauses (Use_Clauses (J));
4591 end loop;
4592 end if;
4593
4594 -- Restore status of instances. If one of them is a body, make its
4595 -- local entities visible again.
4596
4597 declare
4598 E : Entity_Id;
4599 Inst : Entity_Id;
4600
4601 begin
4602 for J in 1 .. N_Instances loop
4603 Inst := Instances (J);
4604 Set_Is_Generic_Instance (Inst, True);
4605
4606 if In_Package_Body (Inst)
4607 or else Ekind_In (S, E_Procedure, E_Function)
4608 then
4609 E := First_Entity (Instances (J));
4610 while Present (E) loop
4611 Set_Is_Immediately_Visible (E);
4612 Next_Entity (E);
4613 end loop;
4614 end if;
4615 end loop;
4616 end;
4617
4618 -- If generic unit is in current unit, current context is correct. Note
4619 -- that the context is guaranteed to carry the correct SPARK_Mode as no
4620 -- enclosing scopes were removed.
4621
4622 else
4623 Instantiate_Package_Body
4624 (Body_Info =>
4625 ((Inst_Node => N,
4626 Act_Decl => Act_Decl,
4627 Expander_Status => Expander_Active,
4628 Current_Sem_Unit => Current_Sem_Unit,
4629 Scope_Suppress => Scope_Suppress,
4630 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4631 Version => Ada_Version,
4632 Version_Pragma => Ada_Version_Pragma,
4633 Warnings => Save_Warnings,
4634 SPARK_Mode => SPARK_Mode,
4635 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4636 Inlined_Body => True);
4637 end if;
4638 end Inline_Instance_Body;
4639
4640 -------------------------------------
4641 -- Analyze_Procedure_Instantiation --
4642 -------------------------------------
4643
4644 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4645 begin
4646 Analyze_Subprogram_Instantiation (N, E_Procedure);
4647 end Analyze_Procedure_Instantiation;
4648
4649 -----------------------------------
4650 -- Need_Subprogram_Instance_Body --
4651 -----------------------------------
4652
4653 function Need_Subprogram_Instance_Body
4654 (N : Node_Id;
4655 Subp : Entity_Id) return Boolean
4656 is
4657 begin
4658 -- Must be inlined (or inlined renaming)
4659
4660 if (Is_In_Main_Unit (N)
4661 or else Is_Inlined (Subp)
4662 or else Is_Inlined (Alias (Subp)))
4663
4664 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4665
4666 and then (Operating_Mode = Generate_Code
4667 or else (Operating_Mode = Check_Semantics
4668 and then (ASIS_Mode or GNATprove_Mode)))
4669
4670 -- The body is needed when generating code (full expansion), in ASIS
4671 -- mode for other tools, and in GNATprove mode (special expansion) for
4672 -- formal verification of the body itself.
4673
4674 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4675
4676 -- No point in inlining if ABE is inevitable
4677
4678 and then not ABE_Is_Certain (N)
4679
4680 -- Or if subprogram is eliminated
4681
4682 and then not Is_Eliminated (Subp)
4683 then
4684 Pending_Instantiations.Append
4685 ((Inst_Node => N,
4686 Act_Decl => Unit_Declaration_Node (Subp),
4687 Expander_Status => Expander_Active,
4688 Current_Sem_Unit => Current_Sem_Unit,
4689 Scope_Suppress => Scope_Suppress,
4690 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4691 Version => Ada_Version,
4692 Version_Pragma => Ada_Version_Pragma,
4693 Warnings => Save_Warnings,
4694 SPARK_Mode => SPARK_Mode,
4695 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4696 return True;
4697
4698 -- Here if not inlined, or we ignore the inlining
4699
4700 else
4701 return False;
4702 end if;
4703 end Need_Subprogram_Instance_Body;
4704
4705 --------------------------------------
4706 -- Analyze_Subprogram_Instantiation --
4707 --------------------------------------
4708
4709 procedure Analyze_Subprogram_Instantiation
4710 (N : Node_Id;
4711 K : Entity_Kind)
4712 is
4713 Loc : constant Source_Ptr := Sloc (N);
4714 Gen_Id : constant Node_Id := Name (N);
4715
4716 Anon_Id : constant Entity_Id :=
4717 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4718 Chars => New_External_Name
4719 (Chars (Defining_Entity (N)), 'R'));
4720
4721 Act_Decl_Id : Entity_Id;
4722 Act_Decl : Node_Id;
4723 Act_Spec : Node_Id;
4724 Act_Tree : Node_Id;
4725
4726 Env_Installed : Boolean := False;
4727 Gen_Unit : Entity_Id;
4728 Gen_Decl : Node_Id;
4729 Pack_Id : Entity_Id;
4730 Parent_Installed : Boolean := False;
4731
4732 Renaming_List : List_Id;
4733 -- The list of declarations that link formals and actuals of the
4734 -- instance. These are subtype declarations for formal types, and
4735 -- renaming declarations for other formals. The subprogram declaration
4736 -- for the instance is then appended to the list, and the last item on
4737 -- the list is the renaming declaration for the instance.
4738
4739 procedure Analyze_Instance_And_Renamings;
4740 -- The instance must be analyzed in a context that includes the mappings
4741 -- of generic parameters into actuals. We create a package declaration
4742 -- for this purpose, and a subprogram with an internal name within the
4743 -- package. The subprogram instance is simply an alias for the internal
4744 -- subprogram, declared in the current scope.
4745
4746 procedure Build_Subprogram_Renaming;
4747 -- If the subprogram is recursive, there are occurrences of the name of
4748 -- the generic within the body, which must resolve to the current
4749 -- instance. We add a renaming declaration after the declaration, which
4750 -- is available in the instance body, as well as in the analysis of
4751 -- aspects that appear in the generic. This renaming declaration is
4752 -- inserted after the instance declaration which it renames.
4753
4754 procedure Instantiate_Subprogram_Contract (Templ : Node_Id);
4755 -- Instantiate all source pragmas found in the contract of the generic
4756 -- subprogram declaration template denoted by Templ. The instantiated
4757 -- pragmas are added to list Renaming_List.
4758
4759 ------------------------------------
4760 -- Analyze_Instance_And_Renamings --
4761 ------------------------------------
4762
4763 procedure Analyze_Instance_And_Renamings is
4764 Def_Ent : constant Entity_Id := Defining_Entity (N);
4765 Pack_Decl : Node_Id;
4766
4767 begin
4768 if Nkind (Parent (N)) = N_Compilation_Unit then
4769
4770 -- For the case of a compilation unit, the container package has
4771 -- the same name as the instantiation, to insure that the binder
4772 -- calls the elaboration procedure with the right name. Copy the
4773 -- entity of the instance, which may have compilation level flags
4774 -- (e.g. Is_Child_Unit) set.
4775
4776 Pack_Id := New_Copy (Def_Ent);
4777
4778 else
4779 -- Otherwise we use the name of the instantiation concatenated
4780 -- with its source position to ensure uniqueness if there are
4781 -- several instantiations with the same name.
4782
4783 Pack_Id :=
4784 Make_Defining_Identifier (Loc,
4785 Chars => New_External_Name
4786 (Related_Id => Chars (Def_Ent),
4787 Suffix => "GP",
4788 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4789 end if;
4790
4791 Pack_Decl :=
4792 Make_Package_Declaration (Loc,
4793 Specification => Make_Package_Specification (Loc,
4794 Defining_Unit_Name => Pack_Id,
4795 Visible_Declarations => Renaming_List,
4796 End_Label => Empty));
4797
4798 Set_Instance_Spec (N, Pack_Decl);
4799 Set_Is_Generic_Instance (Pack_Id);
4800 Set_Debug_Info_Needed (Pack_Id);
4801
4802 -- Case of not a compilation unit
4803
4804 if Nkind (Parent (N)) /= N_Compilation_Unit then
4805 Mark_Rewrite_Insertion (Pack_Decl);
4806 Insert_Before (N, Pack_Decl);
4807 Set_Has_Completion (Pack_Id);
4808
4809 -- Case of an instantiation that is a compilation unit
4810
4811 -- Place declaration on current node so context is complete for
4812 -- analysis (including nested instantiations), and for use in a
4813 -- context_clause (see Analyze_With_Clause).
4814
4815 else
4816 Set_Unit (Parent (N), Pack_Decl);
4817 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4818 end if;
4819
4820 Analyze (Pack_Decl);
4821 Check_Formal_Packages (Pack_Id);
4822 Set_Is_Generic_Instance (Pack_Id, False);
4823
4824 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4825 -- above???
4826
4827 -- Body of the enclosing package is supplied when instantiating the
4828 -- subprogram body, after semantic analysis is completed.
4829
4830 if Nkind (Parent (N)) = N_Compilation_Unit then
4831
4832 -- Remove package itself from visibility, so it does not
4833 -- conflict with subprogram.
4834
4835 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
4836
4837 -- Set name and scope of internal subprogram so that the proper
4838 -- external name will be generated. The proper scope is the scope
4839 -- of the wrapper package. We need to generate debugging info for
4840 -- the internal subprogram, so set flag accordingly.
4841
4842 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
4843 Set_Scope (Anon_Id, Scope (Pack_Id));
4844
4845 -- Mark wrapper package as referenced, to avoid spurious warnings
4846 -- if the instantiation appears in various with_ clauses of
4847 -- subunits of the main unit.
4848
4849 Set_Referenced (Pack_Id);
4850 end if;
4851
4852 Set_Is_Generic_Instance (Anon_Id);
4853 Set_Debug_Info_Needed (Anon_Id);
4854 Act_Decl_Id := New_Copy (Anon_Id);
4855
4856 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4857 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
4858 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
4859
4860 -- Subprogram instance comes from source only if generic does
4861
4862 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
4863
4864 -- The signature may involve types that are not frozen yet, but the
4865 -- subprogram will be frozen at the point the wrapper package is
4866 -- frozen, so it does not need its own freeze node. In fact, if one
4867 -- is created, it might conflict with the freezing actions from the
4868 -- wrapper package.
4869
4870 Set_Has_Delayed_Freeze (Anon_Id, False);
4871
4872 -- If the instance is a child unit, mark the Id accordingly. Mark
4873 -- the anonymous entity as well, which is the real subprogram and
4874 -- which is used when the instance appears in a context clause.
4875 -- Similarly, propagate the Is_Eliminated flag to handle properly
4876 -- nested eliminated subprograms.
4877
4878 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
4879 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
4880 New_Overloaded_Entity (Act_Decl_Id);
4881 Check_Eliminated (Act_Decl_Id);
4882 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
4883
4884 -- In compilation unit case, kill elaboration checks on the
4885 -- instantiation, since they are never needed -- the body is
4886 -- instantiated at the same point as the spec.
4887
4888 if Nkind (Parent (N)) = N_Compilation_Unit then
4889 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4890 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4891 Set_Is_Compilation_Unit (Anon_Id);
4892
4893 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
4894 end if;
4895
4896 -- The instance is not a freezing point for the new subprogram
4897
4898 Set_Is_Frozen (Act_Decl_Id, False);
4899
4900 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
4901 Valid_Operator_Definition (Act_Decl_Id);
4902 end if;
4903
4904 Set_Alias (Act_Decl_Id, Anon_Id);
4905 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4906 Set_Has_Completion (Act_Decl_Id);
4907 Set_Related_Instance (Pack_Id, Act_Decl_Id);
4908
4909 if Nkind (Parent (N)) = N_Compilation_Unit then
4910 Set_Body_Required (Parent (N), False);
4911 end if;
4912 end Analyze_Instance_And_Renamings;
4913
4914 -------------------------------
4915 -- Build_Subprogram_Renaming --
4916 -------------------------------
4917
4918 procedure Build_Subprogram_Renaming is
4919 Renaming_Decl : Node_Id;
4920 Unit_Renaming : Node_Id;
4921
4922 begin
4923 Unit_Renaming :=
4924 Make_Subprogram_Renaming_Declaration (Loc,
4925 Specification =>
4926 Copy_Generic_Node
4927 (Specification (Original_Node (Gen_Decl)),
4928 Empty,
4929 Instantiating => True),
4930 Name => New_Occurrence_Of (Anon_Id, Loc));
4931
4932 -- The generic may be a a child unit. The renaming needs an
4933 -- identifier with the proper name.
4934
4935 Set_Defining_Unit_Name (Specification (Unit_Renaming),
4936 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
4937
4938 -- If there is a formal subprogram with the same name as the unit
4939 -- itself, do not add this renaming declaration, to prevent
4940 -- ambiguities when there is a call with that name in the body.
4941 -- This is a partial and ugly fix for one ACATS test. ???
4942
4943 Renaming_Decl := First (Renaming_List);
4944 while Present (Renaming_Decl) loop
4945 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
4946 and then
4947 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
4948 then
4949 exit;
4950 end if;
4951
4952 Next (Renaming_Decl);
4953 end loop;
4954
4955 if No (Renaming_Decl) then
4956 Append (Unit_Renaming, Renaming_List);
4957 end if;
4958 end Build_Subprogram_Renaming;
4959
4960 -------------------------------------
4961 -- Instantiate_Subprogram_Contract --
4962 -------------------------------------
4963
4964 procedure Instantiate_Subprogram_Contract (Templ : Node_Id) is
4965 procedure Instantiate_Pragmas (First_Prag : Node_Id);
4966 -- Instantiate all contract-related source pragmas found in the list
4967 -- starting with pragma First_Prag. Each instantiated pragma is added
4968 -- to list Renaming_List.
4969
4970 -------------------------
4971 -- Instantiate_Pragmas --
4972 -------------------------
4973
4974 procedure Instantiate_Pragmas (First_Prag : Node_Id) is
4975 Inst_Prag : Node_Id;
4976 Prag : Node_Id;
4977
4978 begin
4979 Prag := First_Prag;
4980 while Present (Prag) loop
4981 if Is_Generic_Contract_Pragma (Prag) then
4982 Inst_Prag :=
4983 Copy_Generic_Node (Prag, Empty, Instantiating => True);
4984
4985 Set_Analyzed (Inst_Prag, False);
4986 Append_To (Renaming_List, Inst_Prag);
4987 end if;
4988
4989 Prag := Next_Pragma (Prag);
4990 end loop;
4991 end Instantiate_Pragmas;
4992
4993 -- Local variables
4994
4995 Items : constant Node_Id := Contract (Defining_Entity (Templ));
4996
4997 -- Start of processing for Instantiate_Subprogram_Contract
4998
4999 begin
5000 if Present (Items) then
5001 Instantiate_Pragmas (Pre_Post_Conditions (Items));
5002 Instantiate_Pragmas (Contract_Test_Cases (Items));
5003 Instantiate_Pragmas (Classifications (Items));
5004 end if;
5005 end Instantiate_Subprogram_Contract;
5006
5007 -- Local variables
5008
5009 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
5010 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
5011
5012 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
5013 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
5014 -- Save the SPARK_Mode-related data for restore on exit
5015
5016 Vis_Prims_List : Elist_Id := No_Elist;
5017 -- List of primitives made temporarily visible in the instantiation
5018 -- to match the visibility of the formal type
5019
5020 -- Start of processing for Analyze_Subprogram_Instantiation
5021
5022 begin
5023 Check_SPARK_05_Restriction ("generic is not allowed", N);
5024
5025 -- Very first thing: check for special Text_IO unit in case we are
5026 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5027 -- such an instantiation is bogus (these are packages, not subprograms),
5028 -- but we get a better error message if we do this.
5029
5030 Check_Text_IO_Special_Unit (Gen_Id);
5031
5032 -- Make node global for error reporting
5033
5034 Instantiation_Node := N;
5035
5036 -- For package instantiations we turn off style checks, because they
5037 -- will have been emitted in the generic. For subprogram instantiations
5038 -- we want to apply at least the check on overriding indicators so we
5039 -- do not modify the style check status.
5040
5041 -- The renaming declarations for the actuals do not come from source and
5042 -- will not generate spurious warnings.
5043
5044 Preanalyze_Actuals (N);
5045
5046 Init_Env;
5047 Env_Installed := True;
5048 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5049 Gen_Unit := Entity (Gen_Id);
5050
5051 Generate_Reference (Gen_Unit, Gen_Id);
5052
5053 if Nkind (Gen_Id) = N_Identifier
5054 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5055 then
5056 Error_Msg_NE
5057 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5058 end if;
5059
5060 if Etype (Gen_Unit) = Any_Type then
5061 Restore_Env;
5062 return;
5063 end if;
5064
5065 -- Verify that it is a generic subprogram of the right kind, and that
5066 -- it does not lead to a circular instantiation.
5067
5068 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5069 Error_Msg_NE
5070 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5071
5072 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5073 Error_Msg_NE
5074 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5075
5076 elsif In_Open_Scopes (Gen_Unit) then
5077 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5078
5079 else
5080 -- If the context of the instance is subject to SPARK_Mode "off",
5081 -- set the global flag which signals Analyze_Pragma to ignore all
5082 -- SPARK_Mode pragmas within the instance.
5083
5084 if SPARK_Mode = Off then
5085 Ignore_Pragma_SPARK_Mode := True;
5086 end if;
5087
5088 Set_Entity (Gen_Id, Gen_Unit);
5089 Set_Is_Instantiated (Gen_Unit);
5090
5091 if In_Extended_Main_Source_Unit (N) then
5092 Generate_Reference (Gen_Unit, N);
5093 end if;
5094
5095 -- If renaming, get original unit
5096
5097 if Present (Renamed_Object (Gen_Unit))
5098 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5099 E_Generic_Function)
5100 then
5101 Gen_Unit := Renamed_Object (Gen_Unit);
5102 Set_Is_Instantiated (Gen_Unit);
5103 Generate_Reference (Gen_Unit, N);
5104 end if;
5105
5106 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5107 Error_Msg_Node_2 := Current_Scope;
5108 Error_Msg_NE
5109 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5110 Circularity_Detected := True;
5111 Restore_Hidden_Primitives (Vis_Prims_List);
5112 goto Leave;
5113 end if;
5114
5115 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5116
5117 -- Initialize renamings map, for error checking
5118
5119 Generic_Renamings.Set_Last (0);
5120 Generic_Renamings_HTable.Reset;
5121
5122 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
5123
5124 -- Copy original generic tree, to produce text for instantiation
5125
5126 Act_Tree :=
5127 Copy_Generic_Node
5128 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5129
5130 -- Inherit overriding indicator from instance node
5131
5132 Act_Spec := Specification (Act_Tree);
5133 Set_Must_Override (Act_Spec, Must_Override (N));
5134 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5135
5136 Renaming_List :=
5137 Analyze_Associations
5138 (I_Node => N,
5139 Formals => Generic_Formal_Declarations (Act_Tree),
5140 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5141
5142 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5143
5144 -- The subprogram itself cannot contain a nested instance, so the
5145 -- current parent is left empty.
5146
5147 Set_Instance_Env (Gen_Unit, Empty);
5148
5149 -- Build the subprogram declaration, which does not appear in the
5150 -- generic template, and give it a sloc consistent with that of the
5151 -- template.
5152
5153 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5154 Set_Generic_Parent (Act_Spec, Gen_Unit);
5155 Act_Decl :=
5156 Make_Subprogram_Declaration (Sloc (Act_Spec),
5157 Specification => Act_Spec);
5158
5159 -- The aspects have been copied previously, but they have to be
5160 -- linked explicitly to the new subprogram declaration. Explicit
5161 -- pre/postconditions on the instance are analyzed below, in a
5162 -- separate step.
5163
5164 Move_Aspects (Act_Tree, To => Act_Decl);
5165 Set_Categorization_From_Pragmas (Act_Decl);
5166
5167 if Parent_Installed then
5168 Hide_Current_Scope;
5169 end if;
5170
5171 Append (Act_Decl, Renaming_List);
5172
5173 -- Contract-related source pragmas that follow a generic subprogram
5174 -- must be instantiated explicitly because they are not part of the
5175 -- subprogram template.
5176
5177 Instantiate_Subprogram_Contract (Original_Node (Gen_Decl));
5178 Build_Subprogram_Renaming;
5179
5180 Analyze_Instance_And_Renamings;
5181
5182 -- If the generic is marked Import (Intrinsic), then so is the
5183 -- instance. This indicates that there is no body to instantiate. If
5184 -- generic is marked inline, so it the instance, and the anonymous
5185 -- subprogram it renames. If inlined, or else if inlining is enabled
5186 -- for the compilation, we generate the instance body even if it is
5187 -- not within the main unit.
5188
5189 if Is_Intrinsic_Subprogram (Gen_Unit) then
5190 Set_Is_Intrinsic_Subprogram (Anon_Id);
5191 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5192
5193 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5194 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5195 end if;
5196 end if;
5197
5198 -- Inherit convention from generic unit. Intrinsic convention, as for
5199 -- an instance of unchecked conversion, is not inherited because an
5200 -- explicit Ada instance has been created.
5201
5202 if Has_Convention_Pragma (Gen_Unit)
5203 and then Convention (Gen_Unit) /= Convention_Intrinsic
5204 then
5205 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5206 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5207 end if;
5208
5209 Generate_Definition (Act_Decl_Id);
5210
5211 -- Inherit all inlining-related flags which apply to the generic in
5212 -- the subprogram and its declaration.
5213
5214 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5215 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5216
5217 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5218 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5219
5220 Set_Has_Pragma_Inline_Always
5221 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5222 Set_Has_Pragma_Inline_Always
5223 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5224
5225 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5226 Check_Elab_Instantiation (N);
5227 end if;
5228
5229 if Is_Dispatching_Operation (Act_Decl_Id)
5230 and then Ada_Version >= Ada_2005
5231 then
5232 declare
5233 Formal : Entity_Id;
5234
5235 begin
5236 Formal := First_Formal (Act_Decl_Id);
5237 while Present (Formal) loop
5238 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5239 and then Is_Controlling_Formal (Formal)
5240 and then not Can_Never_Be_Null (Formal)
5241 then
5242 Error_Msg_NE
5243 ("access parameter& is controlling,", N, Formal);
5244 Error_Msg_NE
5245 ("\corresponding parameter of & must be "
5246 & "explicitly null-excluding", N, Gen_Id);
5247 end if;
5248
5249 Next_Formal (Formal);
5250 end loop;
5251 end;
5252 end if;
5253
5254 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5255
5256 Validate_Categorization_Dependency (N, Act_Decl_Id);
5257
5258 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5259 Inherit_Context (Gen_Decl, N);
5260
5261 Restore_Private_Views (Pack_Id, False);
5262
5263 -- If the context requires a full instantiation, mark node for
5264 -- subsequent construction of the body.
5265
5266 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5267 Check_Forward_Instantiation (Gen_Decl);
5268
5269 -- The wrapper package is always delayed, because it does not
5270 -- constitute a freeze point, but to insure that the freeze
5271 -- node is placed properly, it is created directly when
5272 -- instantiating the body (otherwise the freeze node might
5273 -- appear to early for nested instantiations).
5274
5275 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5276
5277 -- For ASIS purposes, indicate that the wrapper package has
5278 -- replaced the instantiation node.
5279
5280 Rewrite (N, Unit (Parent (N)));
5281 Set_Unit (Parent (N), N);
5282 end if;
5283
5284 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5285
5286 -- Replace instance node for library-level instantiations of
5287 -- intrinsic subprograms, for ASIS use.
5288
5289 Rewrite (N, Unit (Parent (N)));
5290 Set_Unit (Parent (N), N);
5291 end if;
5292
5293 if Parent_Installed then
5294 Remove_Parent;
5295 end if;
5296
5297 Restore_Hidden_Primitives (Vis_Prims_List);
5298 Restore_Env;
5299 Env_Installed := False;
5300 Generic_Renamings.Set_Last (0);
5301 Generic_Renamings_HTable.Reset;
5302
5303 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5304 SPARK_Mode := Save_SM;
5305 SPARK_Mode_Pragma := Save_SMP;
5306
5307 if SPARK_Mode = On then
5308 Dynamic_Elaboration_Checks := False;
5309 end if;
5310
5311 end if;
5312
5313 <<Leave>>
5314 if Has_Aspects (N) then
5315 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5316 end if;
5317
5318 exception
5319 when Instantiation_Error =>
5320 if Parent_Installed then
5321 Remove_Parent;
5322 end if;
5323
5324 if Env_Installed then
5325 Restore_Env;
5326 end if;
5327
5328 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5329 SPARK_Mode := Save_SM;
5330 SPARK_Mode_Pragma := Save_SMP;
5331
5332 if SPARK_Mode = On then
5333 Dynamic_Elaboration_Checks := False;
5334 end if;
5335 end Analyze_Subprogram_Instantiation;
5336
5337 -------------------------
5338 -- Get_Associated_Node --
5339 -------------------------
5340
5341 function Get_Associated_Node (N : Node_Id) return Node_Id is
5342 Assoc : Node_Id;
5343
5344 begin
5345 Assoc := Associated_Node (N);
5346
5347 if Nkind (Assoc) /= Nkind (N) then
5348 return Assoc;
5349
5350 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5351 return Assoc;
5352
5353 else
5354 -- If the node is part of an inner generic, it may itself have been
5355 -- remapped into a further generic copy. Associated_Node is otherwise
5356 -- used for the entity of the node, and will be of a different node
5357 -- kind, or else N has been rewritten as a literal or function call.
5358
5359 while Present (Associated_Node (Assoc))
5360 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5361 loop
5362 Assoc := Associated_Node (Assoc);
5363 end loop;
5364
5365 -- Follow and additional link in case the final node was rewritten.
5366 -- This can only happen with nested generic units.
5367
5368 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5369 and then Present (Associated_Node (Assoc))
5370 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5371 N_Explicit_Dereference,
5372 N_Integer_Literal,
5373 N_Real_Literal,
5374 N_String_Literal))
5375 then
5376 Assoc := Associated_Node (Assoc);
5377 end if;
5378
5379 -- An additional special case: an unconstrained type in an object
5380 -- declaration may have been rewritten as a local subtype constrained
5381 -- by the expression in the declaration. We need to recover the
5382 -- original entity which may be global.
5383
5384 if Present (Original_Node (Assoc))
5385 and then Nkind (Parent (N)) = N_Object_Declaration
5386 then
5387 Assoc := Original_Node (Assoc);
5388 end if;
5389
5390 return Assoc;
5391 end if;
5392 end Get_Associated_Node;
5393
5394 ----------------------------
5395 -- Build_Function_Wrapper --
5396 ----------------------------
5397
5398 function Build_Function_Wrapper
5399 (Formal_Subp : Entity_Id;
5400 Actual_Subp : Entity_Id) return Node_Id
5401 is
5402 Loc : constant Source_Ptr := Sloc (Current_Scope);
5403 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5404 Actuals : List_Id;
5405 Decl : Node_Id;
5406 Func_Name : Node_Id;
5407 Func : Entity_Id;
5408 Parm_Type : Node_Id;
5409 Profile : List_Id := New_List;
5410 Spec : Node_Id;
5411 Act_F : Entity_Id;
5412 Form_F : Entity_Id;
5413 New_F : Entity_Id;
5414
5415 begin
5416 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5417
5418 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5419 Set_Ekind (Func, E_Function);
5420 Set_Is_Generic_Actual_Subprogram (Func);
5421
5422 Actuals := New_List;
5423 Profile := New_List;
5424
5425 Act_F := First_Formal (Actual_Subp);
5426 Form_F := First_Formal (Formal_Subp);
5427 while Present (Form_F) loop
5428
5429 -- Create new formal for profile of wrapper, and add a reference
5430 -- to it in the list of actuals for the enclosing call. The name
5431 -- must be that of the formal in the formal subprogram, because
5432 -- calls to it in the generic body may use named associations.
5433
5434 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5435
5436 Parm_Type :=
5437 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5438
5439 Append_To (Profile,
5440 Make_Parameter_Specification (Loc,
5441 Defining_Identifier => New_F,
5442 Parameter_Type => Parm_Type));
5443
5444 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5445 Next_Formal (Form_F);
5446
5447 if Present (Act_F) then
5448 Next_Formal (Act_F);
5449 end if;
5450 end loop;
5451
5452 Spec :=
5453 Make_Function_Specification (Loc,
5454 Defining_Unit_Name => Func,
5455 Parameter_Specifications => Profile,
5456 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5457
5458 Decl :=
5459 Make_Expression_Function (Loc,
5460 Specification => Spec,
5461 Expression =>
5462 Make_Function_Call (Loc,
5463 Name => Func_Name,
5464 Parameter_Associations => Actuals));
5465
5466 return Decl;
5467 end Build_Function_Wrapper;
5468
5469 ----------------------------
5470 -- Build_Operator_Wrapper --
5471 ----------------------------
5472
5473 function Build_Operator_Wrapper
5474 (Formal_Subp : Entity_Id;
5475 Actual_Subp : Entity_Id) return Node_Id
5476 is
5477 Loc : constant Source_Ptr := Sloc (Current_Scope);
5478 Ret_Type : constant Entity_Id :=
5479 Get_Instance_Of (Etype (Formal_Subp));
5480 Op_Type : constant Entity_Id :=
5481 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
5482 Is_Binary : constant Boolean :=
5483 Present (Next_Formal (First_Formal (Formal_Subp)));
5484
5485 Decl : Node_Id;
5486 Expr : Node_Id;
5487 F1, F2 : Entity_Id;
5488 Func : Entity_Id;
5489 Op_Name : Name_Id;
5490 Spec : Node_Id;
5491 L, R : Node_Id;
5492
5493 begin
5494 Op_Name := Chars (Actual_Subp);
5495
5496 -- Create entities for wrapper function and its formals
5497
5498 F1 := Make_Temporary (Loc, 'A');
5499 F2 := Make_Temporary (Loc, 'B');
5500 L := New_Occurrence_Of (F1, Loc);
5501 R := New_Occurrence_Of (F2, Loc);
5502
5503 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5504 Set_Ekind (Func, E_Function);
5505 Set_Is_Generic_Actual_Subprogram (Func);
5506
5507 Spec :=
5508 Make_Function_Specification (Loc,
5509 Defining_Unit_Name => Func,
5510 Parameter_Specifications => New_List (
5511 Make_Parameter_Specification (Loc,
5512 Defining_Identifier => F1,
5513 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
5514 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5515
5516 if Is_Binary then
5517 Append_To (Parameter_Specifications (Spec),
5518 Make_Parameter_Specification (Loc,
5519 Defining_Identifier => F2,
5520 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
5521 end if;
5522
5523 -- Build expression as a function call, or as an operator node
5524 -- that corresponds to the name of the actual, starting with
5525 -- binary operators.
5526
5527 if Op_Name not in Any_Operator_Name then
5528 Expr :=
5529 Make_Function_Call (Loc,
5530 Name =>
5531 New_Occurrence_Of (Actual_Subp, Loc),
5532 Parameter_Associations => New_List (L));
5533
5534 if Is_Binary then
5535 Append_To (Parameter_Associations (Expr), R);
5536 end if;
5537
5538 -- Binary operators
5539
5540 elsif Is_Binary then
5541 if Op_Name = Name_Op_And then
5542 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
5543 elsif Op_Name = Name_Op_Or then
5544 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
5545 elsif Op_Name = Name_Op_Xor then
5546 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
5547 elsif Op_Name = Name_Op_Eq then
5548 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
5549 elsif Op_Name = Name_Op_Ne then
5550 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
5551 elsif Op_Name = Name_Op_Le then
5552 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
5553 elsif Op_Name = Name_Op_Gt then
5554 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
5555 elsif Op_Name = Name_Op_Ge then
5556 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
5557 elsif Op_Name = Name_Op_Lt then
5558 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
5559 elsif Op_Name = Name_Op_Add then
5560 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
5561 elsif Op_Name = Name_Op_Subtract then
5562 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
5563 elsif Op_Name = Name_Op_Concat then
5564 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
5565 elsif Op_Name = Name_Op_Multiply then
5566 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
5567 elsif Op_Name = Name_Op_Divide then
5568 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
5569 elsif Op_Name = Name_Op_Mod then
5570 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
5571 elsif Op_Name = Name_Op_Rem then
5572 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
5573 elsif Op_Name = Name_Op_Expon then
5574 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
5575 end if;
5576
5577 -- Unary operators
5578
5579 else
5580 if Op_Name = Name_Op_Add then
5581 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
5582 elsif Op_Name = Name_Op_Subtract then
5583 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
5584 elsif Op_Name = Name_Op_Abs then
5585 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
5586 elsif Op_Name = Name_Op_Not then
5587 Expr := Make_Op_Not (Loc, Right_Opnd => L);
5588 end if;
5589 end if;
5590
5591 Decl :=
5592 Make_Expression_Function (Loc,
5593 Specification => Spec,
5594 Expression => Expr);
5595
5596 return Decl;
5597 end Build_Operator_Wrapper;
5598
5599 -------------------------------------------
5600 -- Build_Instance_Compilation_Unit_Nodes --
5601 -------------------------------------------
5602
5603 procedure Build_Instance_Compilation_Unit_Nodes
5604 (N : Node_Id;
5605 Act_Body : Node_Id;
5606 Act_Decl : Node_Id)
5607 is
5608 Decl_Cunit : Node_Id;
5609 Body_Cunit : Node_Id;
5610 Citem : Node_Id;
5611 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5612 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5613
5614 begin
5615 -- A new compilation unit node is built for the instance declaration
5616
5617 Decl_Cunit :=
5618 Make_Compilation_Unit (Sloc (N),
5619 Context_Items => Empty_List,
5620 Unit => Act_Decl,
5621 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5622
5623 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5624
5625 -- The new compilation unit is linked to its body, but both share the
5626 -- same file, so we do not set Body_Required on the new unit so as not
5627 -- to create a spurious dependency on a non-existent body in the ali.
5628 -- This simplifies CodePeer unit traversal.
5629
5630 -- We use the original instantiation compilation unit as the resulting
5631 -- compilation unit of the instance, since this is the main unit.
5632
5633 Rewrite (N, Act_Body);
5634
5635 -- Propagate the aspect specifications from the package body template to
5636 -- the instantiated version of the package body.
5637
5638 if Has_Aspects (Act_Body) then
5639 Set_Aspect_Specifications
5640 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5641 end if;
5642
5643 Body_Cunit := Parent (N);
5644
5645 -- The two compilation unit nodes are linked by the Library_Unit field
5646
5647 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5648 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5649
5650 -- Preserve the private nature of the package if needed
5651
5652 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5653
5654 -- If the instance is not the main unit, its context, categorization
5655 -- and elaboration entity are not relevant to the compilation.
5656
5657 if Body_Cunit /= Cunit (Main_Unit) then
5658 Make_Instance_Unit (Body_Cunit, In_Main => False);
5659 return;
5660 end if;
5661
5662 -- The context clause items on the instantiation, which are now attached
5663 -- to the body compilation unit (since the body overwrote the original
5664 -- instantiation node), semantically belong on the spec, so copy them
5665 -- there. It's harmless to leave them on the body as well. In fact one
5666 -- could argue that they belong in both places.
5667
5668 Citem := First (Context_Items (Body_Cunit));
5669 while Present (Citem) loop
5670 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5671 Next (Citem);
5672 end loop;
5673
5674 -- Propagate categorization flags on packages, so that they appear in
5675 -- the ali file for the spec of the unit.
5676
5677 if Ekind (New_Main) = E_Package then
5678 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5679 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5680 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5681 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5682 Set_Is_Remote_Call_Interface
5683 (Old_Main, Is_Remote_Call_Interface (New_Main));
5684 end if;
5685
5686 -- Make entry in Units table, so that binder can generate call to
5687 -- elaboration procedure for body, if any.
5688
5689 Make_Instance_Unit (Body_Cunit, In_Main => True);
5690 Main_Unit_Entity := New_Main;
5691 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5692
5693 -- Build elaboration entity, since the instance may certainly generate
5694 -- elaboration code requiring a flag for protection.
5695
5696 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5697 end Build_Instance_Compilation_Unit_Nodes;
5698
5699 -----------------------------
5700 -- Check_Access_Definition --
5701 -----------------------------
5702
5703 procedure Check_Access_Definition (N : Node_Id) is
5704 begin
5705 pragma Assert
5706 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5707 null;
5708 end Check_Access_Definition;
5709
5710 -----------------------------------
5711 -- Check_Formal_Package_Instance --
5712 -----------------------------------
5713
5714 -- If the formal has specific parameters, they must match those of the
5715 -- actual. Both of them are instances, and the renaming declarations for
5716 -- their formal parameters appear in the same order in both. The analyzed
5717 -- formal has been analyzed in the context of the current instance.
5718
5719 procedure Check_Formal_Package_Instance
5720 (Formal_Pack : Entity_Id;
5721 Actual_Pack : Entity_Id)
5722 is
5723 E1 : Entity_Id := First_Entity (Actual_Pack);
5724 E2 : Entity_Id := First_Entity (Formal_Pack);
5725
5726 Expr1 : Node_Id;
5727 Expr2 : Node_Id;
5728
5729 procedure Check_Mismatch (B : Boolean);
5730 -- Common error routine for mismatch between the parameters of the
5731 -- actual instance and those of the formal package.
5732
5733 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5734 -- The formal may come from a nested formal package, and the actual may
5735 -- have been constant-folded. To determine whether the two denote the
5736 -- same entity we may have to traverse several definitions to recover
5737 -- the ultimate entity that they refer to.
5738
5739 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5740 -- Similarly, if the formal comes from a nested formal package, the
5741 -- actual may designate the formal through multiple renamings, which
5742 -- have to be followed to determine the original variable in question.
5743
5744 --------------------
5745 -- Check_Mismatch --
5746 --------------------
5747
5748 procedure Check_Mismatch (B : Boolean) is
5749 Kind : constant Node_Kind := Nkind (Parent (E2));
5750
5751 begin
5752 if Kind = N_Formal_Type_Declaration then
5753 return;
5754
5755 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5756 N_Formal_Package_Declaration)
5757 or else Kind in N_Formal_Subprogram_Declaration
5758 then
5759 null;
5760
5761 -- Ada 2012: If both formal and actual are incomplete types they
5762 -- are conformant.
5763
5764 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
5765 null;
5766
5767 elsif B then
5768 Error_Msg_NE
5769 ("actual for & in actual instance does not match formal",
5770 Parent (Actual_Pack), E1);
5771 end if;
5772 end Check_Mismatch;
5773
5774 --------------------------------
5775 -- Same_Instantiated_Constant --
5776 --------------------------------
5777
5778 function Same_Instantiated_Constant
5779 (E1, E2 : Entity_Id) return Boolean
5780 is
5781 Ent : Entity_Id;
5782
5783 begin
5784 Ent := E2;
5785 while Present (Ent) loop
5786 if E1 = Ent then
5787 return True;
5788
5789 elsif Ekind (Ent) /= E_Constant then
5790 return False;
5791
5792 elsif Is_Entity_Name (Constant_Value (Ent)) then
5793 if Entity (Constant_Value (Ent)) = E1 then
5794 return True;
5795 else
5796 Ent := Entity (Constant_Value (Ent));
5797 end if;
5798
5799 -- The actual may be a constant that has been folded. Recover
5800 -- original name.
5801
5802 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5803 Ent := Entity (Original_Node (Constant_Value (Ent)));
5804
5805 else
5806 return False;
5807 end if;
5808 end loop;
5809
5810 return False;
5811 end Same_Instantiated_Constant;
5812
5813 --------------------------------
5814 -- Same_Instantiated_Variable --
5815 --------------------------------
5816
5817 function Same_Instantiated_Variable
5818 (E1, E2 : Entity_Id) return Boolean
5819 is
5820 function Original_Entity (E : Entity_Id) return Entity_Id;
5821 -- Follow chain of renamings to the ultimate ancestor
5822
5823 ---------------------
5824 -- Original_Entity --
5825 ---------------------
5826
5827 function Original_Entity (E : Entity_Id) return Entity_Id is
5828 Orig : Entity_Id;
5829
5830 begin
5831 Orig := E;
5832 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
5833 and then Present (Renamed_Object (Orig))
5834 and then Is_Entity_Name (Renamed_Object (Orig))
5835 loop
5836 Orig := Entity (Renamed_Object (Orig));
5837 end loop;
5838
5839 return Orig;
5840 end Original_Entity;
5841
5842 -- Start of processing for Same_Instantiated_Variable
5843
5844 begin
5845 return Ekind (E1) = Ekind (E2)
5846 and then Original_Entity (E1) = Original_Entity (E2);
5847 end Same_Instantiated_Variable;
5848
5849 -- Start of processing for Check_Formal_Package_Instance
5850
5851 begin
5852 while Present (E1) and then Present (E2) loop
5853 exit when Ekind (E1) = E_Package
5854 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
5855
5856 -- If the formal is the renaming of the formal package, this
5857 -- is the end of its formal part, which may occur before the
5858 -- end of the formal part in the actual in the presence of
5859 -- defaulted parameters in the formal package.
5860
5861 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
5862 and then Renamed_Entity (E2) = Scope (E2);
5863
5864 -- The analysis of the actual may generate additional internal
5865 -- entities. If the formal is defaulted, there is no corresponding
5866 -- analysis and the internal entities must be skipped, until we
5867 -- find corresponding entities again.
5868
5869 if Comes_From_Source (E2)
5870 and then not Comes_From_Source (E1)
5871 and then Chars (E1) /= Chars (E2)
5872 then
5873 while Present (E1) and then Chars (E1) /= Chars (E2) loop
5874 Next_Entity (E1);
5875 end loop;
5876 end if;
5877
5878 if No (E1) then
5879 return;
5880
5881 -- If the formal entity comes from a formal declaration, it was
5882 -- defaulted in the formal package, and no check is needed on it.
5883
5884 elsif Nkind (Parent (E2)) = N_Formal_Object_Declaration then
5885 goto Next_E;
5886
5887 -- Ditto for defaulted formal subprograms.
5888
5889 elsif Is_Overloadable (E1)
5890 and then Nkind (Unit_Declaration_Node (E2)) in
5891 N_Formal_Subprogram_Declaration
5892 then
5893 goto Next_E;
5894
5895 elsif Is_Type (E1) then
5896
5897 -- Subtypes must statically match. E1, E2 are the local entities
5898 -- that are subtypes of the actuals. Itypes generated for other
5899 -- parameters need not be checked, the check will be performed
5900 -- on the parameters themselves.
5901
5902 -- If E2 is a formal type declaration, it is a defaulted parameter
5903 -- and needs no checking.
5904
5905 if not Is_Itype (E1) and then not Is_Itype (E2) then
5906 Check_Mismatch
5907 (not Is_Type (E2)
5908 or else Etype (E1) /= Etype (E2)
5909 or else not Subtypes_Statically_Match (E1, E2));
5910 end if;
5911
5912 elsif Ekind (E1) = E_Constant then
5913
5914 -- IN parameters must denote the same static value, or the same
5915 -- constant, or the literal null.
5916
5917 Expr1 := Expression (Parent (E1));
5918
5919 if Ekind (E2) /= E_Constant then
5920 Check_Mismatch (True);
5921 goto Next_E;
5922 else
5923 Expr2 := Expression (Parent (E2));
5924 end if;
5925
5926 if Is_OK_Static_Expression (Expr1) then
5927 if not Is_OK_Static_Expression (Expr2) then
5928 Check_Mismatch (True);
5929
5930 elsif Is_Discrete_Type (Etype (E1)) then
5931 declare
5932 V1 : constant Uint := Expr_Value (Expr1);
5933 V2 : constant Uint := Expr_Value (Expr2);
5934 begin
5935 Check_Mismatch (V1 /= V2);
5936 end;
5937
5938 elsif Is_Real_Type (Etype (E1)) then
5939 declare
5940 V1 : constant Ureal := Expr_Value_R (Expr1);
5941 V2 : constant Ureal := Expr_Value_R (Expr2);
5942 begin
5943 Check_Mismatch (V1 /= V2);
5944 end;
5945
5946 elsif Is_String_Type (Etype (E1))
5947 and then Nkind (Expr1) = N_String_Literal
5948 then
5949 if Nkind (Expr2) /= N_String_Literal then
5950 Check_Mismatch (True);
5951 else
5952 Check_Mismatch
5953 (not String_Equal (Strval (Expr1), Strval (Expr2)));
5954 end if;
5955 end if;
5956
5957 elsif Is_Entity_Name (Expr1) then
5958 if Is_Entity_Name (Expr2) then
5959 if Entity (Expr1) = Entity (Expr2) then
5960 null;
5961 else
5962 Check_Mismatch
5963 (not Same_Instantiated_Constant
5964 (Entity (Expr1), Entity (Expr2)));
5965 end if;
5966
5967 else
5968 Check_Mismatch (True);
5969 end if;
5970
5971 elsif Is_Entity_Name (Original_Node (Expr1))
5972 and then Is_Entity_Name (Expr2)
5973 and then Same_Instantiated_Constant
5974 (Entity (Original_Node (Expr1)), Entity (Expr2))
5975 then
5976 null;
5977
5978 elsif Nkind (Expr1) = N_Null then
5979 Check_Mismatch (Nkind (Expr1) /= N_Null);
5980
5981 else
5982 Check_Mismatch (True);
5983 end if;
5984
5985 elsif Ekind (E1) = E_Variable then
5986 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
5987
5988 elsif Ekind (E1) = E_Package then
5989 Check_Mismatch
5990 (Ekind (E1) /= Ekind (E2)
5991 or else Renamed_Object (E1) /= Renamed_Object (E2));
5992
5993 elsif Is_Overloadable (E1) then
5994
5995 -- Verify that the actual subprograms match. Note that actuals
5996 -- that are attributes are rewritten as subprograms. If the
5997 -- subprogram in the formal package is defaulted, no check is
5998 -- needed. Note that this can only happen in Ada 2005 when the
5999 -- formal package can be partially parameterized.
6000
6001 if Nkind (Unit_Declaration_Node (E1)) =
6002 N_Subprogram_Renaming_Declaration
6003 and then From_Default (Unit_Declaration_Node (E1))
6004 then
6005 null;
6006
6007 -- If the formal package has an "others" box association that
6008 -- covers this formal, there is no need for a check either.
6009
6010 elsif Nkind (Unit_Declaration_Node (E2)) in
6011 N_Formal_Subprogram_Declaration
6012 and then Box_Present (Unit_Declaration_Node (E2))
6013 then
6014 null;
6015
6016 -- No check needed if subprogram is a defaulted null procedure
6017
6018 elsif No (Alias (E2))
6019 and then Ekind (E2) = E_Procedure
6020 and then
6021 Null_Present (Specification (Unit_Declaration_Node (E2)))
6022 then
6023 null;
6024
6025 -- Otherwise the actual in the formal and the actual in the
6026 -- instantiation of the formal must match, up to renamings.
6027
6028 else
6029 Check_Mismatch
6030 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
6031 end if;
6032
6033 else
6034 raise Program_Error;
6035 end if;
6036
6037 <<Next_E>>
6038 Next_Entity (E1);
6039 Next_Entity (E2);
6040 end loop;
6041 end Check_Formal_Package_Instance;
6042
6043 ---------------------------
6044 -- Check_Formal_Packages --
6045 ---------------------------
6046
6047 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6048 E : Entity_Id;
6049 Formal_P : Entity_Id;
6050
6051 begin
6052 -- Iterate through the declarations in the instance, looking for package
6053 -- renaming declarations that denote instances of formal packages. Stop
6054 -- when we find the renaming of the current package itself. The
6055 -- declaration for a formal package without a box is followed by an
6056 -- internal entity that repeats the instantiation.
6057
6058 E := First_Entity (P_Id);
6059 while Present (E) loop
6060 if Ekind (E) = E_Package then
6061 if Renamed_Object (E) = P_Id then
6062 exit;
6063
6064 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6065 null;
6066
6067 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
6068 Formal_P := Next_Entity (E);
6069 Check_Formal_Package_Instance (Formal_P, E);
6070
6071 -- After checking, remove the internal validating package. It
6072 -- is only needed for semantic checks, and as it may contain
6073 -- generic formal declarations it should not reach gigi.
6074
6075 Remove (Unit_Declaration_Node (Formal_P));
6076 end if;
6077 end if;
6078
6079 Next_Entity (E);
6080 end loop;
6081 end Check_Formal_Packages;
6082
6083 ---------------------------------
6084 -- Check_Forward_Instantiation --
6085 ---------------------------------
6086
6087 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6088 S : Entity_Id;
6089 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6090
6091 begin
6092 -- The instantiation appears before the generic body if we are in the
6093 -- scope of the unit containing the generic, either in its spec or in
6094 -- the package body, and before the generic body.
6095
6096 if Ekind (Gen_Comp) = E_Package_Body then
6097 Gen_Comp := Spec_Entity (Gen_Comp);
6098 end if;
6099
6100 if In_Open_Scopes (Gen_Comp)
6101 and then No (Corresponding_Body (Decl))
6102 then
6103 S := Current_Scope;
6104
6105 while Present (S)
6106 and then not Is_Compilation_Unit (S)
6107 and then not Is_Child_Unit (S)
6108 loop
6109 if Ekind (S) = E_Package then
6110 Set_Has_Forward_Instantiation (S);
6111 end if;
6112
6113 S := Scope (S);
6114 end loop;
6115 end if;
6116 end Check_Forward_Instantiation;
6117
6118 ---------------------------
6119 -- Check_Generic_Actuals --
6120 ---------------------------
6121
6122 -- The visibility of the actuals may be different between the point of
6123 -- generic instantiation and the instantiation of the body.
6124
6125 procedure Check_Generic_Actuals
6126 (Instance : Entity_Id;
6127 Is_Formal_Box : Boolean)
6128 is
6129 E : Entity_Id;
6130 Astype : Entity_Id;
6131
6132 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
6133 -- For a formal that is an array type, the component type is often a
6134 -- previous formal in the same unit. The privacy status of the component
6135 -- type will have been examined earlier in the traversal of the
6136 -- corresponding actuals, and this status should not be modified for
6137 -- the array (sub)type itself. However, if the base type of the array
6138 -- (sub)type is private, its full view must be restored in the body to
6139 -- be consistent with subsequent index subtypes, etc.
6140 --
6141 -- To detect this case we have to rescan the list of formals, which is
6142 -- usually short enough to ignore the resulting inefficiency.
6143
6144 -----------------------------
6145 -- Denotes_Previous_Actual --
6146 -----------------------------
6147
6148 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
6149 Prev : Entity_Id;
6150
6151 begin
6152 Prev := First_Entity (Instance);
6153 while Present (Prev) loop
6154 if Is_Type (Prev)
6155 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
6156 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
6157 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
6158 then
6159 return True;
6160
6161 elsif Prev = E then
6162 return False;
6163
6164 else
6165 Next_Entity (Prev);
6166 end if;
6167 end loop;
6168
6169 return False;
6170 end Denotes_Previous_Actual;
6171
6172 -- Start of processing for Check_Generic_Actuals
6173
6174 begin
6175 E := First_Entity (Instance);
6176 while Present (E) loop
6177 if Is_Type (E)
6178 and then Nkind (Parent (E)) = N_Subtype_Declaration
6179 and then Scope (Etype (E)) /= Instance
6180 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6181 then
6182 if Is_Array_Type (E)
6183 and then not Is_Private_Type (Etype (E))
6184 and then Denotes_Previous_Actual (Component_Type (E))
6185 then
6186 null;
6187 else
6188 Check_Private_View (Subtype_Indication (Parent (E)));
6189 end if;
6190
6191 Set_Is_Generic_Actual_Type (E, True);
6192 Set_Is_Hidden (E, False);
6193 Set_Is_Potentially_Use_Visible (E,
6194 In_Use (Instance));
6195
6196 -- We constructed the generic actual type as a subtype of the
6197 -- supplied type. This means that it normally would not inherit
6198 -- subtype specific attributes of the actual, which is wrong for
6199 -- the generic case.
6200
6201 Astype := Ancestor_Subtype (E);
6202
6203 if No (Astype) then
6204
6205 -- This can happen when E is an itype that is the full view of
6206 -- a private type completed, e.g. with a constrained array. In
6207 -- that case, use the first subtype, which will carry size
6208 -- information. The base type itself is unconstrained and will
6209 -- not carry it.
6210
6211 Astype := First_Subtype (E);
6212 end if;
6213
6214 Set_Size_Info (E, (Astype));
6215 Set_RM_Size (E, RM_Size (Astype));
6216 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6217
6218 if Is_Discrete_Or_Fixed_Point_Type (E) then
6219 Set_RM_Size (E, RM_Size (Astype));
6220
6221 -- In nested instances, the base type of an access actual may
6222 -- itself be private, and need to be exchanged.
6223
6224 elsif Is_Access_Type (E)
6225 and then Is_Private_Type (Etype (E))
6226 then
6227 Check_Private_View
6228 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
6229 end if;
6230
6231 elsif Ekind (E) = E_Package then
6232
6233 -- If this is the renaming for the current instance, we're done.
6234 -- Otherwise it is a formal package. If the corresponding formal
6235 -- was declared with a box, the (instantiations of the) generic
6236 -- formal part are also visible. Otherwise, ignore the entity
6237 -- created to validate the actuals.
6238
6239 if Renamed_Object (E) = Instance then
6240 exit;
6241
6242 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6243 null;
6244
6245 -- The visibility of a formal of an enclosing generic is already
6246 -- correct.
6247
6248 elsif Denotes_Formal_Package (E) then
6249 null;
6250
6251 elsif Present (Associated_Formal_Package (E))
6252 and then not Is_Generic_Formal (E)
6253 then
6254 if Box_Present (Parent (Associated_Formal_Package (E))) then
6255 Check_Generic_Actuals (Renamed_Object (E), True);
6256
6257 else
6258 Check_Generic_Actuals (Renamed_Object (E), False);
6259 end if;
6260
6261 Set_Is_Hidden (E, False);
6262 end if;
6263
6264 -- If this is a subprogram instance (in a wrapper package) the
6265 -- actual is fully visible.
6266
6267 elsif Is_Wrapper_Package (Instance) then
6268 Set_Is_Hidden (E, False);
6269
6270 -- If the formal package is declared with a box, or if the formal
6271 -- parameter is defaulted, it is visible in the body.
6272
6273 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6274 Set_Is_Hidden (E, False);
6275 end if;
6276
6277 if Ekind (E) = E_Constant then
6278
6279 -- If the type of the actual is a private type declared in the
6280 -- enclosing scope of the generic unit, the body of the generic
6281 -- sees the full view of the type (because it has to appear in
6282 -- the corresponding package body). If the type is private now,
6283 -- exchange views to restore the proper visiblity in the instance.
6284
6285 declare
6286 Typ : constant Entity_Id := Base_Type (Etype (E));
6287 -- The type of the actual
6288
6289 Gen_Id : Entity_Id;
6290 -- The generic unit
6291
6292 Parent_Scope : Entity_Id;
6293 -- The enclosing scope of the generic unit
6294
6295 begin
6296 if Is_Wrapper_Package (Instance) then
6297 Gen_Id :=
6298 Generic_Parent
6299 (Specification
6300 (Unit_Declaration_Node
6301 (Related_Instance (Instance))));
6302 else
6303 Gen_Id :=
6304 Generic_Parent (Package_Specification (Instance));
6305 end if;
6306
6307 Parent_Scope := Scope (Gen_Id);
6308
6309 -- The exchange is only needed if the generic is defined
6310 -- within a package which is not a common ancestor of the
6311 -- scope of the instance, and is not already in scope.
6312
6313 if Is_Private_Type (Typ)
6314 and then Scope (Typ) = Parent_Scope
6315 and then Scope (Instance) /= Parent_Scope
6316 and then Ekind (Parent_Scope) = E_Package
6317 and then not Is_Child_Unit (Gen_Id)
6318 then
6319 Switch_View (Typ);
6320
6321 -- If the type of the entity is a subtype, it may also have
6322 -- to be made visible, together with the base type of its
6323 -- full view, after exchange.
6324
6325 if Is_Private_Type (Etype (E)) then
6326 Switch_View (Etype (E));
6327 Switch_View (Base_Type (Etype (E)));
6328 end if;
6329 end if;
6330 end;
6331 end if;
6332
6333 Next_Entity (E);
6334 end loop;
6335 end Check_Generic_Actuals;
6336
6337 ------------------------------
6338 -- Check_Generic_Child_Unit --
6339 ------------------------------
6340
6341 procedure Check_Generic_Child_Unit
6342 (Gen_Id : Node_Id;
6343 Parent_Installed : in out Boolean)
6344 is
6345 Loc : constant Source_Ptr := Sloc (Gen_Id);
6346 Gen_Par : Entity_Id := Empty;
6347 E : Entity_Id;
6348 Inst_Par : Entity_Id;
6349 S : Node_Id;
6350
6351 function Find_Generic_Child
6352 (Scop : Entity_Id;
6353 Id : Node_Id) return Entity_Id;
6354 -- Search generic parent for possible child unit with the given name
6355
6356 function In_Enclosing_Instance return Boolean;
6357 -- Within an instance of the parent, the child unit may be denoted by
6358 -- a simple name, or an abbreviated expanded name. Examine enclosing
6359 -- scopes to locate a possible parent instantiation.
6360
6361 ------------------------
6362 -- Find_Generic_Child --
6363 ------------------------
6364
6365 function Find_Generic_Child
6366 (Scop : Entity_Id;
6367 Id : Node_Id) return Entity_Id
6368 is
6369 E : Entity_Id;
6370
6371 begin
6372 -- If entity of name is already set, instance has already been
6373 -- resolved, e.g. in an enclosing instantiation.
6374
6375 if Present (Entity (Id)) then
6376 if Scope (Entity (Id)) = Scop then
6377 return Entity (Id);
6378 else
6379 return Empty;
6380 end if;
6381
6382 else
6383 E := First_Entity (Scop);
6384 while Present (E) loop
6385 if Chars (E) = Chars (Id)
6386 and then Is_Child_Unit (E)
6387 then
6388 if Is_Child_Unit (E)
6389 and then not Is_Visible_Lib_Unit (E)
6390 then
6391 Error_Msg_NE
6392 ("generic child unit& is not visible", Gen_Id, E);
6393 end if;
6394
6395 Set_Entity (Id, E);
6396 return E;
6397 end if;
6398
6399 Next_Entity (E);
6400 end loop;
6401
6402 return Empty;
6403 end if;
6404 end Find_Generic_Child;
6405
6406 ---------------------------
6407 -- In_Enclosing_Instance --
6408 ---------------------------
6409
6410 function In_Enclosing_Instance return Boolean is
6411 Enclosing_Instance : Node_Id;
6412 Instance_Decl : Node_Id;
6413
6414 begin
6415 -- We do not inline any call that contains instantiations, except
6416 -- for instantiations of Unchecked_Conversion, so if we are within
6417 -- an inlined body the current instance does not require parents.
6418
6419 if In_Inlined_Body then
6420 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6421 return False;
6422 end if;
6423
6424 -- Loop to check enclosing scopes
6425
6426 Enclosing_Instance := Current_Scope;
6427 while Present (Enclosing_Instance) loop
6428 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6429
6430 if Ekind (Enclosing_Instance) = E_Package
6431 and then Is_Generic_Instance (Enclosing_Instance)
6432 and then Present
6433 (Generic_Parent (Specification (Instance_Decl)))
6434 then
6435 -- Check whether the generic we are looking for is a child of
6436 -- this instance.
6437
6438 E := Find_Generic_Child
6439 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6440 exit when Present (E);
6441
6442 else
6443 E := Empty;
6444 end if;
6445
6446 Enclosing_Instance := Scope (Enclosing_Instance);
6447 end loop;
6448
6449 if No (E) then
6450
6451 -- Not a child unit
6452
6453 Analyze (Gen_Id);
6454 return False;
6455
6456 else
6457 Rewrite (Gen_Id,
6458 Make_Expanded_Name (Loc,
6459 Chars => Chars (E),
6460 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6461 Selector_Name => New_Occurrence_Of (E, Loc)));
6462
6463 Set_Entity (Gen_Id, E);
6464 Set_Etype (Gen_Id, Etype (E));
6465 Parent_Installed := False; -- Already in scope.
6466 return True;
6467 end if;
6468 end In_Enclosing_Instance;
6469
6470 -- Start of processing for Check_Generic_Child_Unit
6471
6472 begin
6473 -- If the name of the generic is given by a selected component, it may
6474 -- be the name of a generic child unit, and the prefix is the name of an
6475 -- instance of the parent, in which case the child unit must be visible.
6476 -- If this instance is not in scope, it must be placed there and removed
6477 -- after instantiation, because what is being instantiated is not the
6478 -- original child, but the corresponding child present in the instance
6479 -- of the parent.
6480
6481 -- If the child is instantiated within the parent, it can be given by
6482 -- a simple name. In this case the instance is already in scope, but
6483 -- the child generic must be recovered from the generic parent as well.
6484
6485 if Nkind (Gen_Id) = N_Selected_Component then
6486 S := Selector_Name (Gen_Id);
6487 Analyze (Prefix (Gen_Id));
6488 Inst_Par := Entity (Prefix (Gen_Id));
6489
6490 if Ekind (Inst_Par) = E_Package
6491 and then Present (Renamed_Object (Inst_Par))
6492 then
6493 Inst_Par := Renamed_Object (Inst_Par);
6494 end if;
6495
6496 if Ekind (Inst_Par) = E_Package then
6497 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6498 Gen_Par := Generic_Parent (Parent (Inst_Par));
6499
6500 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6501 and then
6502 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6503 then
6504 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6505 end if;
6506
6507 elsif Ekind (Inst_Par) = E_Generic_Package
6508 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6509 then
6510 -- A formal package may be a real child package, and not the
6511 -- implicit instance within a parent. In this case the child is
6512 -- not visible and has to be retrieved explicitly as well.
6513
6514 Gen_Par := Inst_Par;
6515 end if;
6516
6517 if Present (Gen_Par) then
6518
6519 -- The prefix denotes an instantiation. The entity itself may be a
6520 -- nested generic, or a child unit.
6521
6522 E := Find_Generic_Child (Gen_Par, S);
6523
6524 if Present (E) then
6525 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6526 Set_Entity (Gen_Id, E);
6527 Set_Etype (Gen_Id, Etype (E));
6528 Set_Entity (S, E);
6529 Set_Etype (S, Etype (E));
6530
6531 -- Indicate that this is a reference to the parent
6532
6533 if In_Extended_Main_Source_Unit (Gen_Id) then
6534 Set_Is_Instantiated (Inst_Par);
6535 end if;
6536
6537 -- A common mistake is to replicate the naming scheme of a
6538 -- hierarchy by instantiating a generic child directly, rather
6539 -- than the implicit child in a parent instance:
6540
6541 -- generic .. package Gpar is ..
6542 -- generic .. package Gpar.Child is ..
6543 -- package Par is new Gpar ();
6544
6545 -- with Gpar.Child;
6546 -- package Par.Child is new Gpar.Child ();
6547 -- rather than Par.Child
6548
6549 -- In this case the instantiation is within Par, which is an
6550 -- instance, but Gpar does not denote Par because we are not IN
6551 -- the instance of Gpar, so this is illegal. The test below
6552 -- recognizes this particular case.
6553
6554 if Is_Child_Unit (E)
6555 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6556 and then (not In_Instance
6557 or else Nkind (Parent (Parent (Gen_Id))) =
6558 N_Compilation_Unit)
6559 then
6560 Error_Msg_N
6561 ("prefix of generic child unit must be instance of parent",
6562 Gen_Id);
6563 end if;
6564
6565 if not In_Open_Scopes (Inst_Par)
6566 and then Nkind (Parent (Gen_Id)) not in
6567 N_Generic_Renaming_Declaration
6568 then
6569 Install_Parent (Inst_Par);
6570 Parent_Installed := True;
6571
6572 elsif In_Open_Scopes (Inst_Par) then
6573
6574 -- If the parent is already installed, install the actuals
6575 -- for its formal packages. This is necessary when the child
6576 -- instance is a child of the parent instance: in this case,
6577 -- the parent is placed on the scope stack but the formal
6578 -- packages are not made visible.
6579
6580 Install_Formal_Packages (Inst_Par);
6581 end if;
6582
6583 else
6584 -- If the generic parent does not contain an entity that
6585 -- corresponds to the selector, the instance doesn't either.
6586 -- Analyzing the node will yield the appropriate error message.
6587 -- If the entity is not a child unit, then it is an inner
6588 -- generic in the parent.
6589
6590 Analyze (Gen_Id);
6591 end if;
6592
6593 else
6594 Analyze (Gen_Id);
6595
6596 if Is_Child_Unit (Entity (Gen_Id))
6597 and then
6598 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6599 and then not In_Open_Scopes (Inst_Par)
6600 then
6601 Install_Parent (Inst_Par);
6602 Parent_Installed := True;
6603
6604 -- The generic unit may be the renaming of the implicit child
6605 -- present in an instance. In that case the parent instance is
6606 -- obtained from the name of the renamed entity.
6607
6608 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6609 and then Present (Renamed_Entity (Entity (Gen_Id)))
6610 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6611 then
6612 declare
6613 Renamed_Package : constant Node_Id :=
6614 Name (Parent (Entity (Gen_Id)));
6615 begin
6616 if Nkind (Renamed_Package) = N_Expanded_Name then
6617 Inst_Par := Entity (Prefix (Renamed_Package));
6618 Install_Parent (Inst_Par);
6619 Parent_Installed := True;
6620 end if;
6621 end;
6622 end if;
6623 end if;
6624
6625 elsif Nkind (Gen_Id) = N_Expanded_Name then
6626
6627 -- Entity already present, analyze prefix, whose meaning may be
6628 -- an instance in the current context. If it is an instance of
6629 -- a relative within another, the proper parent may still have
6630 -- to be installed, if they are not of the same generation.
6631
6632 Analyze (Prefix (Gen_Id));
6633
6634 -- In the unlikely case that a local declaration hides the name
6635 -- of the parent package, locate it on the homonym chain. If the
6636 -- context is an instance of the parent, the renaming entity is
6637 -- flagged as such.
6638
6639 Inst_Par := Entity (Prefix (Gen_Id));
6640 while Present (Inst_Par)
6641 and then not Is_Package_Or_Generic_Package (Inst_Par)
6642 loop
6643 Inst_Par := Homonym (Inst_Par);
6644 end loop;
6645
6646 pragma Assert (Present (Inst_Par));
6647 Set_Entity (Prefix (Gen_Id), Inst_Par);
6648
6649 if In_Enclosing_Instance then
6650 null;
6651
6652 elsif Present (Entity (Gen_Id))
6653 and then Is_Child_Unit (Entity (Gen_Id))
6654 and then not In_Open_Scopes (Inst_Par)
6655 then
6656 Install_Parent (Inst_Par);
6657 Parent_Installed := True;
6658 end if;
6659
6660 elsif In_Enclosing_Instance then
6661
6662 -- The child unit is found in some enclosing scope
6663
6664 null;
6665
6666 else
6667 Analyze (Gen_Id);
6668
6669 -- If this is the renaming of the implicit child in a parent
6670 -- instance, recover the parent name and install it.
6671
6672 if Is_Entity_Name (Gen_Id) then
6673 E := Entity (Gen_Id);
6674
6675 if Is_Generic_Unit (E)
6676 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6677 and then Is_Child_Unit (Renamed_Object (E))
6678 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6679 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6680 then
6681 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
6682 Inst_Par := Entity (Prefix (Gen_Id));
6683
6684 if not In_Open_Scopes (Inst_Par) then
6685 Install_Parent (Inst_Par);
6686 Parent_Installed := True;
6687 end if;
6688
6689 -- If it is a child unit of a non-generic parent, it may be
6690 -- use-visible and given by a direct name. Install parent as
6691 -- for other cases.
6692
6693 elsif Is_Generic_Unit (E)
6694 and then Is_Child_Unit (E)
6695 and then
6696 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6697 and then not Is_Generic_Unit (Scope (E))
6698 then
6699 if not In_Open_Scopes (Scope (E)) then
6700 Install_Parent (Scope (E));
6701 Parent_Installed := True;
6702 end if;
6703 end if;
6704 end if;
6705 end if;
6706 end Check_Generic_Child_Unit;
6707
6708 -----------------------------
6709 -- Check_Hidden_Child_Unit --
6710 -----------------------------
6711
6712 procedure Check_Hidden_Child_Unit
6713 (N : Node_Id;
6714 Gen_Unit : Entity_Id;
6715 Act_Decl_Id : Entity_Id)
6716 is
6717 Gen_Id : constant Node_Id := Name (N);
6718
6719 begin
6720 if Is_Child_Unit (Gen_Unit)
6721 and then Is_Child_Unit (Act_Decl_Id)
6722 and then Nkind (Gen_Id) = N_Expanded_Name
6723 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6724 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6725 then
6726 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6727 Error_Msg_NE
6728 ("generic unit & is implicitly declared in &",
6729 Defining_Unit_Name (N), Gen_Unit);
6730 Error_Msg_N ("\instance must have different name",
6731 Defining_Unit_Name (N));
6732 end if;
6733 end Check_Hidden_Child_Unit;
6734
6735 ------------------------
6736 -- Check_Private_View --
6737 ------------------------
6738
6739 procedure Check_Private_View (N : Node_Id) is
6740 T : constant Entity_Id := Etype (N);
6741 BT : Entity_Id;
6742
6743 begin
6744 -- Exchange views if the type was not private in the generic but is
6745 -- private at the point of instantiation. Do not exchange views if
6746 -- the scope of the type is in scope. This can happen if both generic
6747 -- and instance are sibling units, or if type is defined in a parent.
6748 -- In this case the visibility of the type will be correct for all
6749 -- semantic checks.
6750
6751 if Present (T) then
6752 BT := Base_Type (T);
6753
6754 if Is_Private_Type (T)
6755 and then not Has_Private_View (N)
6756 and then Present (Full_View (T))
6757 and then not In_Open_Scopes (Scope (T))
6758 then
6759 -- In the generic, the full type was visible. Save the private
6760 -- entity, for subsequent exchange.
6761
6762 Switch_View (T);
6763
6764 elsif Has_Private_View (N)
6765 and then not Is_Private_Type (T)
6766 and then not Has_Been_Exchanged (T)
6767 and then Etype (Get_Associated_Node (N)) /= T
6768 then
6769 -- Only the private declaration was visible in the generic. If
6770 -- the type appears in a subtype declaration, the subtype in the
6771 -- instance must have a view compatible with that of its parent,
6772 -- which must be exchanged (see corresponding code in Restore_
6773 -- Private_Views). Otherwise, if the type is defined in a parent
6774 -- unit, leave full visibility within instance, which is safe.
6775
6776 if In_Open_Scopes (Scope (Base_Type (T)))
6777 and then not Is_Private_Type (Base_Type (T))
6778 and then Comes_From_Source (Base_Type (T))
6779 then
6780 null;
6781
6782 elsif Nkind (Parent (N)) = N_Subtype_Declaration
6783 or else not In_Private_Part (Scope (Base_Type (T)))
6784 then
6785 Prepend_Elmt (T, Exchanged_Views);
6786 Exchange_Declarations (Etype (Get_Associated_Node (N)));
6787 end if;
6788
6789 -- For composite types with inconsistent representation exchange
6790 -- component types accordingly.
6791
6792 elsif Is_Access_Type (T)
6793 and then Is_Private_Type (Designated_Type (T))
6794 and then not Has_Private_View (N)
6795 and then Present (Full_View (Designated_Type (T)))
6796 then
6797 Switch_View (Designated_Type (T));
6798
6799 elsif Is_Array_Type (T) then
6800 if Is_Private_Type (Component_Type (T))
6801 and then not Has_Private_View (N)
6802 and then Present (Full_View (Component_Type (T)))
6803 then
6804 Switch_View (Component_Type (T));
6805 end if;
6806
6807 -- The normal exchange mechanism relies on the setting of a
6808 -- flag on the reference in the generic. However, an additional
6809 -- mechanism is needed for types that are not explicitly
6810 -- mentioned in the generic, but may be needed in expanded code
6811 -- in the instance. This includes component types of arrays and
6812 -- designated types of access types. This processing must also
6813 -- include the index types of arrays which we take care of here.
6814
6815 declare
6816 Indx : Node_Id;
6817 Typ : Entity_Id;
6818
6819 begin
6820 Indx := First_Index (T);
6821 while Present (Indx) loop
6822 Typ := Base_Type (Etype (Indx));
6823
6824 if Is_Private_Type (Typ)
6825 and then Present (Full_View (Typ))
6826 then
6827 Switch_View (Typ);
6828 end if;
6829
6830 Next_Index (Indx);
6831 end loop;
6832 end;
6833
6834 elsif Is_Private_Type (T)
6835 and then Present (Full_View (T))
6836 and then Is_Array_Type (Full_View (T))
6837 and then Is_Private_Type (Component_Type (Full_View (T)))
6838 then
6839 Switch_View (T);
6840
6841 -- Finally, a non-private subtype may have a private base type, which
6842 -- must be exchanged for consistency. This can happen when a package
6843 -- body is instantiated, when the scope stack is empty but in fact
6844 -- the subtype and the base type are declared in an enclosing scope.
6845
6846 -- Note that in this case we introduce an inconsistency in the view
6847 -- set, because we switch the base type BT, but there could be some
6848 -- private dependent subtypes of BT which remain unswitched. Such
6849 -- subtypes might need to be switched at a later point (see specific
6850 -- provision for that case in Switch_View).
6851
6852 elsif not Is_Private_Type (T)
6853 and then not Has_Private_View (N)
6854 and then Is_Private_Type (BT)
6855 and then Present (Full_View (BT))
6856 and then not Is_Generic_Type (BT)
6857 and then not In_Open_Scopes (BT)
6858 then
6859 Prepend_Elmt (Full_View (BT), Exchanged_Views);
6860 Exchange_Declarations (BT);
6861 end if;
6862 end if;
6863 end Check_Private_View;
6864
6865 -----------------------------
6866 -- Check_Hidden_Primitives --
6867 -----------------------------
6868
6869 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
6870 Actual : Node_Id;
6871 Gen_T : Entity_Id;
6872 Result : Elist_Id := No_Elist;
6873
6874 begin
6875 if No (Assoc_List) then
6876 return No_Elist;
6877 end if;
6878
6879 -- Traverse the list of associations between formals and actuals
6880 -- searching for renamings of tagged types
6881
6882 Actual := First (Assoc_List);
6883 while Present (Actual) loop
6884 if Nkind (Actual) = N_Subtype_Declaration then
6885 Gen_T := Generic_Parent_Type (Actual);
6886
6887 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
6888
6889 -- Traverse the list of primitives of the actual types
6890 -- searching for hidden primitives that are visible in the
6891 -- corresponding generic formal; leave them visible and
6892 -- append them to Result to restore their decoration later.
6893
6894 Install_Hidden_Primitives
6895 (Prims_List => Result,
6896 Gen_T => Gen_T,
6897 Act_T => Entity (Subtype_Indication (Actual)));
6898 end if;
6899 end if;
6900
6901 Next (Actual);
6902 end loop;
6903
6904 return Result;
6905 end Check_Hidden_Primitives;
6906
6907 --------------------------
6908 -- Contains_Instance_Of --
6909 --------------------------
6910
6911 function Contains_Instance_Of
6912 (Inner : Entity_Id;
6913 Outer : Entity_Id;
6914 N : Node_Id) return Boolean
6915 is
6916 Elmt : Elmt_Id;
6917 Scop : Entity_Id;
6918
6919 begin
6920 Scop := Outer;
6921
6922 -- Verify that there are no circular instantiations. We check whether
6923 -- the unit contains an instance of the current scope or some enclosing
6924 -- scope (in case one of the instances appears in a subunit). Longer
6925 -- circularities involving subunits might seem too pathological to
6926 -- consider, but they were not too pathological for the authors of
6927 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
6928 -- enclosing generic scopes as containing an instance.
6929
6930 loop
6931 -- Within a generic subprogram body, the scope is not generic, to
6932 -- allow for recursive subprograms. Use the declaration to determine
6933 -- whether this is a generic unit.
6934
6935 if Ekind (Scop) = E_Generic_Package
6936 or else (Is_Subprogram (Scop)
6937 and then Nkind (Unit_Declaration_Node (Scop)) =
6938 N_Generic_Subprogram_Declaration)
6939 then
6940 Elmt := First_Elmt (Inner_Instances (Inner));
6941
6942 while Present (Elmt) loop
6943 if Node (Elmt) = Scop then
6944 Error_Msg_Node_2 := Inner;
6945 Error_Msg_NE
6946 ("circular Instantiation: & instantiated within &!",
6947 N, Scop);
6948 return True;
6949
6950 elsif Node (Elmt) = Inner then
6951 return True;
6952
6953 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
6954 Error_Msg_Node_2 := Inner;
6955 Error_Msg_NE
6956 ("circular Instantiation: & instantiated within &!",
6957 N, Node (Elmt));
6958 return True;
6959 end if;
6960
6961 Next_Elmt (Elmt);
6962 end loop;
6963
6964 -- Indicate that Inner is being instantiated within Scop
6965
6966 Append_Elmt (Inner, Inner_Instances (Scop));
6967 end if;
6968
6969 if Scop = Standard_Standard then
6970 exit;
6971 else
6972 Scop := Scope (Scop);
6973 end if;
6974 end loop;
6975
6976 return False;
6977 end Contains_Instance_Of;
6978
6979 -----------------------
6980 -- Copy_Generic_Node --
6981 -----------------------
6982
6983 function Copy_Generic_Node
6984 (N : Node_Id;
6985 Parent_Id : Node_Id;
6986 Instantiating : Boolean) return Node_Id
6987 is
6988 Ent : Entity_Id;
6989 New_N : Node_Id;
6990
6991 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
6992 -- Check the given value of one of the Fields referenced by the current
6993 -- node to determine whether to copy it recursively. The field may hold
6994 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
6995 -- Char) in which case it need not be copied.
6996
6997 procedure Copy_Descendants;
6998 -- Common utility for various nodes
6999
7000 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7001 -- Make copy of element list
7002
7003 function Copy_Generic_List
7004 (L : List_Id;
7005 Parent_Id : Node_Id) return List_Id;
7006 -- Apply Copy_Node recursively to the members of a node list
7007
7008 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7009 -- True if an identifier is part of the defining program unit name of
7010 -- a child unit. The entity of such an identifier must be kept (for
7011 -- ASIS use) even though as the name of an enclosing generic it would
7012 -- otherwise not be preserved in the generic tree.
7013
7014 ----------------------
7015 -- Copy_Descendants --
7016 ----------------------
7017
7018 procedure Copy_Descendants is
7019 use Atree.Unchecked_Access;
7020 -- This code section is part of the implementation of an untyped
7021 -- tree traversal, so it needs direct access to node fields.
7022
7023 begin
7024 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7025 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7026 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7027 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7028 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7029 end Copy_Descendants;
7030
7031 -----------------------------
7032 -- Copy_Generic_Descendant --
7033 -----------------------------
7034
7035 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7036 begin
7037 if D = Union_Id (Empty) then
7038 return D;
7039
7040 elsif D in Node_Range then
7041 return Union_Id
7042 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7043
7044 elsif D in List_Range then
7045 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7046
7047 elsif D in Elist_Range then
7048 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7049
7050 -- Nothing else is copyable (e.g. Uint values), return as is
7051
7052 else
7053 return D;
7054 end if;
7055 end Copy_Generic_Descendant;
7056
7057 ------------------------
7058 -- Copy_Generic_Elist --
7059 ------------------------
7060
7061 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7062 M : Elmt_Id;
7063 L : Elist_Id;
7064
7065 begin
7066 if Present (E) then
7067 L := New_Elmt_List;
7068 M := First_Elmt (E);
7069 while Present (M) loop
7070 Append_Elmt
7071 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7072 Next_Elmt (M);
7073 end loop;
7074
7075 return L;
7076
7077 else
7078 return No_Elist;
7079 end if;
7080 end Copy_Generic_Elist;
7081
7082 -----------------------
7083 -- Copy_Generic_List --
7084 -----------------------
7085
7086 function Copy_Generic_List
7087 (L : List_Id;
7088 Parent_Id : Node_Id) return List_Id
7089 is
7090 N : Node_Id;
7091 New_L : List_Id;
7092
7093 begin
7094 if Present (L) then
7095 New_L := New_List;
7096 Set_Parent (New_L, Parent_Id);
7097
7098 N := First (L);
7099 while Present (N) loop
7100 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7101 Next (N);
7102 end loop;
7103
7104 return New_L;
7105
7106 else
7107 return No_List;
7108 end if;
7109 end Copy_Generic_List;
7110
7111 ---------------------------
7112 -- In_Defining_Unit_Name --
7113 ---------------------------
7114
7115 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7116 begin
7117 return
7118 Present (Parent (Nam))
7119 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7120 or else
7121 (Nkind (Parent (Nam)) = N_Expanded_Name
7122 and then In_Defining_Unit_Name (Parent (Nam))));
7123 end In_Defining_Unit_Name;
7124
7125 -- Start of processing for Copy_Generic_Node
7126
7127 begin
7128 if N = Empty then
7129 return N;
7130 end if;
7131
7132 New_N := New_Copy (N);
7133
7134 -- Copy aspects if present
7135
7136 if Has_Aspects (N) then
7137 Set_Has_Aspects (New_N, False);
7138 Set_Aspect_Specifications
7139 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7140 end if;
7141
7142 if Instantiating then
7143 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7144 end if;
7145
7146 if not Is_List_Member (N) then
7147 Set_Parent (New_N, Parent_Id);
7148 end if;
7149
7150 -- Special casing for identifiers and other entity names and operators
7151
7152 if Nkind_In (New_N, N_Character_Literal,
7153 N_Expanded_Name,
7154 N_Identifier,
7155 N_Operator_Symbol)
7156 or else Nkind (New_N) in N_Op
7157 then
7158 if not Instantiating then
7159
7160 -- Link both nodes in order to assign subsequently the entity of
7161 -- the copy to the original node, in case this is a global
7162 -- reference.
7163
7164 Set_Associated_Node (N, New_N);
7165
7166 -- If we are within an instantiation, this is a nested generic
7167 -- that has already been analyzed at the point of definition.
7168 -- We must preserve references that were global to the enclosing
7169 -- parent at that point. Other occurrences, whether global or
7170 -- local to the current generic, must be resolved anew, so we
7171 -- reset the entity in the generic copy. A global reference has a
7172 -- smaller depth than the parent, or else the same depth in case
7173 -- both are distinct compilation units.
7174
7175 -- A child unit is implicitly declared within the enclosing parent
7176 -- but is in fact global to it, and must be preserved.
7177
7178 -- It is also possible for Current_Instantiated_Parent to be
7179 -- defined, and for this not to be a nested generic, namely if
7180 -- the unit is loaded through Rtsfind. In that case, the entity of
7181 -- New_N is only a link to the associated node, and not a defining
7182 -- occurrence.
7183
7184 -- The entities for parent units in the defining_program_unit of a
7185 -- generic child unit are established when the context of the unit
7186 -- is first analyzed, before the generic copy is made. They are
7187 -- preserved in the copy for use in ASIS queries.
7188
7189 Ent := Entity (New_N);
7190
7191 if No (Current_Instantiated_Parent.Gen_Id) then
7192 if No (Ent)
7193 or else Nkind (Ent) /= N_Defining_Identifier
7194 or else not In_Defining_Unit_Name (N)
7195 then
7196 Set_Associated_Node (New_N, Empty);
7197 end if;
7198
7199 elsif No (Ent)
7200 or else
7201 not Nkind_In (Ent, N_Defining_Identifier,
7202 N_Defining_Character_Literal,
7203 N_Defining_Operator_Symbol)
7204 or else No (Scope (Ent))
7205 or else
7206 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7207 and then not Is_Child_Unit (Ent))
7208 or else
7209 (Scope_Depth (Scope (Ent)) >
7210 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7211 and then
7212 Get_Source_Unit (Ent) =
7213 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7214 then
7215 Set_Associated_Node (New_N, Empty);
7216 end if;
7217
7218 -- Case of instantiating identifier or some other name or operator
7219
7220 else
7221 -- If the associated node is still defined, the entity in it
7222 -- is global, and must be copied to the instance. If this copy
7223 -- is being made for a body to inline, it is applied to an
7224 -- instantiated tree, and the entity is already present and
7225 -- must be also preserved.
7226
7227 declare
7228 Assoc : constant Node_Id := Get_Associated_Node (N);
7229
7230 begin
7231 if Present (Assoc) then
7232 if Nkind (Assoc) = Nkind (N) then
7233 Set_Entity (New_N, Entity (Assoc));
7234 Check_Private_View (N);
7235
7236 -- The name in the call may be a selected component if the
7237 -- call has not been analyzed yet, as may be the case for
7238 -- pre/post conditions in a generic unit.
7239
7240 elsif Nkind (Assoc) = N_Function_Call
7241 and then Is_Entity_Name (Name (Assoc))
7242 then
7243 Set_Entity (New_N, Entity (Name (Assoc)));
7244
7245 elsif Nkind_In (Assoc, N_Defining_Identifier,
7246 N_Defining_Character_Literal,
7247 N_Defining_Operator_Symbol)
7248 and then Expander_Active
7249 then
7250 -- Inlining case: we are copying a tree that contains
7251 -- global entities, which are preserved in the copy to be
7252 -- used for subsequent inlining.
7253
7254 null;
7255
7256 else
7257 Set_Entity (New_N, Empty);
7258 end if;
7259 end if;
7260 end;
7261 end if;
7262
7263 -- For expanded name, we must copy the Prefix and Selector_Name
7264
7265 if Nkind (N) = N_Expanded_Name then
7266 Set_Prefix
7267 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7268
7269 Set_Selector_Name (New_N,
7270 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7271
7272 -- For operators, we must copy the right operand
7273
7274 elsif Nkind (N) in N_Op then
7275 Set_Right_Opnd (New_N,
7276 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7277
7278 -- And for binary operators, the left operand as well
7279
7280 if Nkind (N) in N_Binary_Op then
7281 Set_Left_Opnd (New_N,
7282 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7283 end if;
7284 end if;
7285
7286 -- Establish a link between an entity from the generic template and the
7287 -- corresponding entity in the generic copy to be analyzed.
7288
7289 elsif Nkind (N) in N_Entity then
7290 if not Instantiating then
7291 Set_Associated_Entity (N, New_N);
7292 end if;
7293
7294 -- Clear any existing link the copy may inherit from the replicated
7295 -- generic template entity.
7296
7297 Set_Associated_Entity (New_N, Empty);
7298
7299 -- Special casing for stubs
7300
7301 elsif Nkind (N) in N_Body_Stub then
7302
7303 -- In any case, we must copy the specification or defining
7304 -- identifier as appropriate.
7305
7306 if Nkind (N) = N_Subprogram_Body_Stub then
7307 Set_Specification (New_N,
7308 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7309
7310 else
7311 Set_Defining_Identifier (New_N,
7312 Copy_Generic_Node
7313 (Defining_Identifier (N), New_N, Instantiating));
7314 end if;
7315
7316 -- If we are not instantiating, then this is where we load and
7317 -- analyze subunits, i.e. at the point where the stub occurs. A
7318 -- more permissive system might defer this analysis to the point
7319 -- of instantiation, but this seems too complicated for now.
7320
7321 if not Instantiating then
7322 declare
7323 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7324 Subunit : Node_Id;
7325 Unum : Unit_Number_Type;
7326 New_Body : Node_Id;
7327
7328 begin
7329 -- Make sure that, if it is a subunit of the main unit that is
7330 -- preprocessed and if -gnateG is specified, the preprocessed
7331 -- file will be written.
7332
7333 Lib.Analysing_Subunit_Of_Main :=
7334 Lib.In_Extended_Main_Source_Unit (N);
7335 Unum :=
7336 Load_Unit
7337 (Load_Name => Subunit_Name,
7338 Required => False,
7339 Subunit => True,
7340 Error_Node => N);
7341 Lib.Analysing_Subunit_Of_Main := False;
7342
7343 -- If the proper body is not found, a warning message will be
7344 -- emitted when analyzing the stub, or later at the point of
7345 -- instantiation. Here we just leave the stub as is.
7346
7347 if Unum = No_Unit then
7348 Subunits_Missing := True;
7349 goto Subunit_Not_Found;
7350 end if;
7351
7352 Subunit := Cunit (Unum);
7353
7354 if Nkind (Unit (Subunit)) /= N_Subunit then
7355 Error_Msg_N
7356 ("found child unit instead of expected SEPARATE subunit",
7357 Subunit);
7358 Error_Msg_Sloc := Sloc (N);
7359 Error_Msg_N ("\to complete stub #", Subunit);
7360 goto Subunit_Not_Found;
7361 end if;
7362
7363 -- We must create a generic copy of the subunit, in order to
7364 -- perform semantic analysis on it, and we must replace the
7365 -- stub in the original generic unit with the subunit, in order
7366 -- to preserve non-local references within.
7367
7368 -- Only the proper body needs to be copied. Library_Unit and
7369 -- context clause are simply inherited by the generic copy.
7370 -- Note that the copy (which may be recursive if there are
7371 -- nested subunits) must be done first, before attaching it to
7372 -- the enclosing generic.
7373
7374 New_Body :=
7375 Copy_Generic_Node
7376 (Proper_Body (Unit (Subunit)),
7377 Empty, Instantiating => False);
7378
7379 -- Now place the original proper body in the original generic
7380 -- unit. This is a body, not a compilation unit.
7381
7382 Rewrite (N, Proper_Body (Unit (Subunit)));
7383 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7384 Set_Was_Originally_Stub (N);
7385
7386 -- Finally replace the body of the subunit with its copy, and
7387 -- make this new subunit into the library unit of the generic
7388 -- copy, which does not have stubs any longer.
7389
7390 Set_Proper_Body (Unit (Subunit), New_Body);
7391 Set_Library_Unit (New_N, Subunit);
7392 Inherit_Context (Unit (Subunit), N);
7393 end;
7394
7395 -- If we are instantiating, this must be an error case, since
7396 -- otherwise we would have replaced the stub node by the proper body
7397 -- that corresponds. So just ignore it in the copy (i.e. we have
7398 -- copied it, and that is good enough).
7399
7400 else
7401 null;
7402 end if;
7403
7404 <<Subunit_Not_Found>> null;
7405
7406 -- If the node is a compilation unit, it is the subunit of a stub, which
7407 -- has been loaded already (see code below). In this case, the library
7408 -- unit field of N points to the parent unit (which is a compilation
7409 -- unit) and need not (and cannot) be copied.
7410
7411 -- When the proper body of the stub is analyzed, the library_unit link
7412 -- is used to establish the proper context (see sem_ch10).
7413
7414 -- The other fields of a compilation unit are copied as usual
7415
7416 elsif Nkind (N) = N_Compilation_Unit then
7417
7418 -- This code can only be executed when not instantiating, because in
7419 -- the copy made for an instantiation, the compilation unit node has
7420 -- disappeared at the point that a stub is replaced by its proper
7421 -- body.
7422
7423 pragma Assert (not Instantiating);
7424
7425 Set_Context_Items (New_N,
7426 Copy_Generic_List (Context_Items (N), New_N));
7427
7428 Set_Unit (New_N,
7429 Copy_Generic_Node (Unit (N), New_N, False));
7430
7431 Set_First_Inlined_Subprogram (New_N,
7432 Copy_Generic_Node
7433 (First_Inlined_Subprogram (N), New_N, False));
7434
7435 Set_Aux_Decls_Node (New_N,
7436 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7437
7438 -- For an assignment node, the assignment is known to be semantically
7439 -- legal if we are instantiating the template. This avoids incorrect
7440 -- diagnostics in generated code.
7441
7442 elsif Nkind (N) = N_Assignment_Statement then
7443
7444 -- Copy name and expression fields in usual manner
7445
7446 Set_Name (New_N,
7447 Copy_Generic_Node (Name (N), New_N, Instantiating));
7448
7449 Set_Expression (New_N,
7450 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7451
7452 if Instantiating then
7453 Set_Assignment_OK (Name (New_N), True);
7454 end if;
7455
7456 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7457 if not Instantiating then
7458 Set_Associated_Node (N, New_N);
7459
7460 else
7461 if Present (Get_Associated_Node (N))
7462 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7463 then
7464 -- In the generic the aggregate has some composite type. If at
7465 -- the point of instantiation the type has a private view,
7466 -- install the full view (and that of its ancestors, if any).
7467
7468 declare
7469 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7470 Rt : Entity_Id;
7471
7472 begin
7473 if Present (T) and then Is_Private_Type (T) then
7474 Switch_View (T);
7475 end if;
7476
7477 if Present (T)
7478 and then Is_Tagged_Type (T)
7479 and then Is_Derived_Type (T)
7480 then
7481 Rt := Root_Type (T);
7482
7483 loop
7484 T := Etype (T);
7485
7486 if Is_Private_Type (T) then
7487 Switch_View (T);
7488 end if;
7489
7490 exit when T = Rt;
7491 end loop;
7492 end if;
7493 end;
7494 end if;
7495 end if;
7496
7497 -- Do not copy the associated node, which points to the generic copy
7498 -- of the aggregate.
7499
7500 declare
7501 use Atree.Unchecked_Access;
7502 -- This code section is part of the implementation of an untyped
7503 -- tree traversal, so it needs direct access to node fields.
7504
7505 begin
7506 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7507 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7508 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7509 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7510 end;
7511
7512 -- Allocators do not have an identifier denoting the access type, so we
7513 -- must locate it through the expression to check whether the views are
7514 -- consistent.
7515
7516 elsif Nkind (N) = N_Allocator
7517 and then Nkind (Expression (N)) = N_Qualified_Expression
7518 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7519 and then Instantiating
7520 then
7521 declare
7522 T : constant Node_Id :=
7523 Get_Associated_Node (Subtype_Mark (Expression (N)));
7524 Acc_T : Entity_Id;
7525
7526 begin
7527 if Present (T) then
7528
7529 -- Retrieve the allocator node in the generic copy
7530
7531 Acc_T := Etype (Parent (Parent (T)));
7532
7533 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
7534 Switch_View (Acc_T);
7535 end if;
7536 end if;
7537
7538 Copy_Descendants;
7539 end;
7540
7541 -- For a proper body, we must catch the case of a proper body that
7542 -- replaces a stub. This represents the point at which a separate
7543 -- compilation unit, and hence template file, may be referenced, so we
7544 -- must make a new source instantiation entry for the template of the
7545 -- subunit, and ensure that all nodes in the subunit are adjusted using
7546 -- this new source instantiation entry.
7547
7548 elsif Nkind (N) in N_Proper_Body then
7549 declare
7550 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7551
7552 begin
7553 if Instantiating and then Was_Originally_Stub (N) then
7554 Create_Instantiation_Source
7555 (Instantiation_Node,
7556 Defining_Entity (N),
7557 False,
7558 S_Adjustment);
7559 end if;
7560
7561 -- Now copy the fields of the proper body, using the new
7562 -- adjustment factor if one was needed as per test above.
7563
7564 Copy_Descendants;
7565
7566 -- Restore the original adjustment factor in case changed
7567
7568 S_Adjustment := Save_Adjustment;
7569 end;
7570
7571 elsif Nkind (N) = N_Pragma and then Instantiating then
7572
7573 -- Do not copy Comment or Ident pragmas their content is relevant to
7574 -- the generic unit, not to the instantiating unit.
7575
7576 if Nam_In (Pragma_Name (N), Name_Comment, Name_Ident) then
7577 New_N := Make_Null_Statement (Sloc (N));
7578
7579 -- Do not copy pragmas generated from aspects because the pragmas do
7580 -- not carry any semantic information, plus they will be regenerated
7581 -- in the instance.
7582
7583 elsif From_Aspect_Specification (N) then
7584 New_N := Make_Null_Statement (Sloc (N));
7585
7586 else
7587 Copy_Descendants;
7588 end if;
7589
7590 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7591
7592 -- No descendant fields need traversing
7593
7594 null;
7595
7596 elsif Nkind (N) = N_String_Literal
7597 and then Present (Etype (N))
7598 and then Instantiating
7599 then
7600 -- If the string is declared in an outer scope, the string_literal
7601 -- subtype created for it may have the wrong scope. Force reanalysis
7602 -- of the constant to generate a new itype in the proper context.
7603
7604 Set_Etype (New_N, Empty);
7605 Set_Analyzed (New_N, False);
7606
7607 -- For the remaining nodes, copy their descendants recursively
7608
7609 else
7610 Copy_Descendants;
7611
7612 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7613 Set_Generic_Parent (Specification (New_N), N);
7614
7615 -- Should preserve Corresponding_Spec??? (12.3(14))
7616 end if;
7617 end if;
7618
7619 return New_N;
7620 end Copy_Generic_Node;
7621
7622 ----------------------------
7623 -- Denotes_Formal_Package --
7624 ----------------------------
7625
7626 function Denotes_Formal_Package
7627 (Pack : Entity_Id;
7628 On_Exit : Boolean := False;
7629 Instance : Entity_Id := Empty) return Boolean
7630 is
7631 Par : Entity_Id;
7632 Scop : constant Entity_Id := Scope (Pack);
7633 E : Entity_Id;
7634
7635 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7636 -- The package in question may be an actual for a previous formal
7637 -- package P of the current instance, so examine its actuals as well.
7638 -- This must be recursive over other formal packages.
7639
7640 ----------------------------------
7641 -- Is_Actual_Of_Previous_Formal --
7642 ----------------------------------
7643
7644 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7645 E1 : Entity_Id;
7646
7647 begin
7648 E1 := First_Entity (P);
7649 while Present (E1) and then E1 /= Instance loop
7650 if Ekind (E1) = E_Package
7651 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7652 then
7653 if Renamed_Object (E1) = Pack then
7654 return True;
7655
7656 elsif E1 = P or else Renamed_Object (E1) = P then
7657 return False;
7658
7659 elsif Is_Actual_Of_Previous_Formal (E1) then
7660 return True;
7661 end if;
7662 end if;
7663
7664 Next_Entity (E1);
7665 end loop;
7666
7667 return False;
7668 end Is_Actual_Of_Previous_Formal;
7669
7670 -- Start of processing for Denotes_Formal_Package
7671
7672 begin
7673 if On_Exit then
7674 Par :=
7675 Instance_Envs.Table
7676 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7677 else
7678 Par := Current_Instantiated_Parent.Act_Id;
7679 end if;
7680
7681 if Ekind (Scop) = E_Generic_Package
7682 or else Nkind (Unit_Declaration_Node (Scop)) =
7683 N_Generic_Subprogram_Declaration
7684 then
7685 return True;
7686
7687 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7688 N_Formal_Package_Declaration
7689 then
7690 return True;
7691
7692 elsif No (Par) then
7693 return False;
7694
7695 else
7696 -- Check whether this package is associated with a formal package of
7697 -- the enclosing instantiation. Iterate over the list of renamings.
7698
7699 E := First_Entity (Par);
7700 while Present (E) loop
7701 if Ekind (E) /= E_Package
7702 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7703 then
7704 null;
7705
7706 elsif Renamed_Object (E) = Par then
7707 return False;
7708
7709 elsif Renamed_Object (E) = Pack then
7710 return True;
7711
7712 elsif Is_Actual_Of_Previous_Formal (E) then
7713 return True;
7714
7715 end if;
7716
7717 Next_Entity (E);
7718 end loop;
7719
7720 return False;
7721 end if;
7722 end Denotes_Formal_Package;
7723
7724 -----------------
7725 -- End_Generic --
7726 -----------------
7727
7728 procedure End_Generic is
7729 begin
7730 -- ??? More things could be factored out in this routine. Should
7731 -- probably be done at a later stage.
7732
7733 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7734 Generic_Flags.Decrement_Last;
7735
7736 Expander_Mode_Restore;
7737 end End_Generic;
7738
7739 -------------
7740 -- Earlier --
7741 -------------
7742
7743 function Earlier (N1, N2 : Node_Id) return Boolean is
7744 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
7745 -- Find distance from given node to enclosing compilation unit
7746
7747 ----------------
7748 -- Find_Depth --
7749 ----------------
7750
7751 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
7752 begin
7753 while Present (P)
7754 and then Nkind (P) /= N_Compilation_Unit
7755 loop
7756 P := True_Parent (P);
7757 D := D + 1;
7758 end loop;
7759 end Find_Depth;
7760
7761 -- Local declarations
7762
7763 D1 : Integer := 0;
7764 D2 : Integer := 0;
7765 P1 : Node_Id := N1;
7766 P2 : Node_Id := N2;
7767 T1 : Source_Ptr;
7768 T2 : Source_Ptr;
7769
7770 -- Start of processing for Earlier
7771
7772 begin
7773 Find_Depth (P1, D1);
7774 Find_Depth (P2, D2);
7775
7776 if P1 /= P2 then
7777 return False;
7778 else
7779 P1 := N1;
7780 P2 := N2;
7781 end if;
7782
7783 while D1 > D2 loop
7784 P1 := True_Parent (P1);
7785 D1 := D1 - 1;
7786 end loop;
7787
7788 while D2 > D1 loop
7789 P2 := True_Parent (P2);
7790 D2 := D2 - 1;
7791 end loop;
7792
7793 -- At this point P1 and P2 are at the same distance from the root.
7794 -- We examine their parents until we find a common declarative list.
7795 -- If we reach the root, N1 and N2 do not descend from the same
7796 -- declarative list (e.g. one is nested in the declarative part and
7797 -- the other is in a block in the statement part) and the earlier
7798 -- one is already frozen.
7799
7800 while not Is_List_Member (P1)
7801 or else not Is_List_Member (P2)
7802 or else List_Containing (P1) /= List_Containing (P2)
7803 loop
7804 P1 := True_Parent (P1);
7805 P2 := True_Parent (P2);
7806
7807 if Nkind (Parent (P1)) = N_Subunit then
7808 P1 := Corresponding_Stub (Parent (P1));
7809 end if;
7810
7811 if Nkind (Parent (P2)) = N_Subunit then
7812 P2 := Corresponding_Stub (Parent (P2));
7813 end if;
7814
7815 if P1 = P2 then
7816 return False;
7817 end if;
7818 end loop;
7819
7820 -- Expanded code usually shares the source location of the original
7821 -- construct it was generated for. This however may not necessarely
7822 -- reflect the true location of the code within the tree.
7823
7824 -- Before comparing the slocs of the two nodes, make sure that we are
7825 -- working with correct source locations. Assume that P1 is to the left
7826 -- of P2. If either one does not come from source, traverse the common
7827 -- list heading towards the other node and locate the first source
7828 -- statement.
7829
7830 -- P1 P2
7831 -- ----+===+===+--------------+===+===+----
7832 -- expanded code expanded code
7833
7834 if not Comes_From_Source (P1) then
7835 while Present (P1) loop
7836
7837 -- Neither P2 nor a source statement were located during the
7838 -- search. If we reach the end of the list, then P1 does not
7839 -- occur earlier than P2.
7840
7841 -- ---->
7842 -- start --- P2 ----- P1 --- end
7843
7844 if No (Next (P1)) then
7845 return False;
7846
7847 -- We encounter P2 while going to the right of the list. This
7848 -- means that P1 does indeed appear earlier.
7849
7850 -- ---->
7851 -- start --- P1 ===== P2 --- end
7852 -- expanded code in between
7853
7854 elsif P1 = P2 then
7855 return True;
7856
7857 -- No need to look any further since we have located a source
7858 -- statement.
7859
7860 elsif Comes_From_Source (P1) then
7861 exit;
7862 end if;
7863
7864 -- Keep going right
7865
7866 Next (P1);
7867 end loop;
7868 end if;
7869
7870 if not Comes_From_Source (P2) then
7871 while Present (P2) loop
7872
7873 -- Neither P1 nor a source statement were located during the
7874 -- search. If we reach the start of the list, then P1 does not
7875 -- occur earlier than P2.
7876
7877 -- <----
7878 -- start --- P2 --- P1 --- end
7879
7880 if No (Prev (P2)) then
7881 return False;
7882
7883 -- We encounter P1 while going to the left of the list. This
7884 -- means that P1 does indeed appear earlier.
7885
7886 -- <----
7887 -- start --- P1 ===== P2 --- end
7888 -- expanded code in between
7889
7890 elsif P2 = P1 then
7891 return True;
7892
7893 -- No need to look any further since we have located a source
7894 -- statement.
7895
7896 elsif Comes_From_Source (P2) then
7897 exit;
7898 end if;
7899
7900 -- Keep going left
7901
7902 Prev (P2);
7903 end loop;
7904 end if;
7905
7906 -- At this point either both nodes came from source or we approximated
7907 -- their source locations through neighbouring source statements.
7908
7909 T1 := Top_Level_Location (Sloc (P1));
7910 T2 := Top_Level_Location (Sloc (P2));
7911
7912 -- When two nodes come from the same instance, they have identical top
7913 -- level locations. To determine proper relation within the tree, check
7914 -- their locations within the template.
7915
7916 if T1 = T2 then
7917 return Sloc (P1) < Sloc (P2);
7918
7919 -- The two nodes either come from unrelated instances or do not come
7920 -- from instantiated code at all.
7921
7922 else
7923 return T1 < T2;
7924 end if;
7925 end Earlier;
7926
7927 ----------------------
7928 -- Find_Actual_Type --
7929 ----------------------
7930
7931 function Find_Actual_Type
7932 (Typ : Entity_Id;
7933 Gen_Type : Entity_Id) return Entity_Id
7934 is
7935 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
7936 T : Entity_Id;
7937
7938 begin
7939 -- Special processing only applies to child units
7940
7941 if not Is_Child_Unit (Gen_Scope) then
7942 return Get_Instance_Of (Typ);
7943
7944 -- If designated or component type is itself a formal of the child unit,
7945 -- its instance is available.
7946
7947 elsif Scope (Typ) = Gen_Scope then
7948 return Get_Instance_Of (Typ);
7949
7950 -- If the array or access type is not declared in the parent unit,
7951 -- no special processing needed.
7952
7953 elsif not Is_Generic_Type (Typ)
7954 and then Scope (Gen_Scope) /= Scope (Typ)
7955 then
7956 return Get_Instance_Of (Typ);
7957
7958 -- Otherwise, retrieve designated or component type by visibility
7959
7960 else
7961 T := Current_Entity (Typ);
7962 while Present (T) loop
7963 if In_Open_Scopes (Scope (T)) then
7964 return T;
7965 elsif Is_Generic_Actual_Type (T) then
7966 return T;
7967 end if;
7968
7969 T := Homonym (T);
7970 end loop;
7971
7972 return Typ;
7973 end if;
7974 end Find_Actual_Type;
7975
7976 ----------------------------
7977 -- Freeze_Subprogram_Body --
7978 ----------------------------
7979
7980 procedure Freeze_Subprogram_Body
7981 (Inst_Node : Node_Id;
7982 Gen_Body : Node_Id;
7983 Pack_Id : Entity_Id)
7984 is
7985 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7986 Par : constant Entity_Id := Scope (Gen_Unit);
7987 E_G_Id : Entity_Id;
7988 Enc_G : Entity_Id;
7989 Enc_I : Node_Id;
7990 F_Node : Node_Id;
7991
7992 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
7993 -- Find innermost package body that encloses the given node, and which
7994 -- is not a compilation unit. Freeze nodes for the instance, or for its
7995 -- enclosing body, may be inserted after the enclosing_body of the
7996 -- generic unit. Used to determine proper placement of freeze node for
7997 -- both package and subprogram instances.
7998
7999 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8000 -- Find entity for given package body, and locate or create a freeze
8001 -- node for it.
8002
8003 ----------------------------
8004 -- Enclosing_Package_Body --
8005 ----------------------------
8006
8007 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8008 P : Node_Id;
8009
8010 begin
8011 P := Parent (N);
8012 while Present (P)
8013 and then Nkind (Parent (P)) /= N_Compilation_Unit
8014 loop
8015 if Nkind (P) = N_Package_Body then
8016 if Nkind (Parent (P)) = N_Subunit then
8017 return Corresponding_Stub (Parent (P));
8018 else
8019 return P;
8020 end if;
8021 end if;
8022
8023 P := True_Parent (P);
8024 end loop;
8025
8026 return Empty;
8027 end Enclosing_Package_Body;
8028
8029 -------------------------
8030 -- Package_Freeze_Node --
8031 -------------------------
8032
8033 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8034 Id : Entity_Id;
8035
8036 begin
8037 if Nkind (B) = N_Package_Body then
8038 Id := Corresponding_Spec (B);
8039 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8040 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8041 end if;
8042
8043 Ensure_Freeze_Node (Id);
8044 return Freeze_Node (Id);
8045 end Package_Freeze_Node;
8046
8047 -- Start of processing of Freeze_Subprogram_Body
8048
8049 begin
8050 -- If the instance and the generic body appear within the same unit, and
8051 -- the instance precedes the generic, the freeze node for the instance
8052 -- must appear after that of the generic. If the generic is nested
8053 -- within another instance I2, then current instance must be frozen
8054 -- after I2. In both cases, the freeze nodes are those of enclosing
8055 -- packages. Otherwise, the freeze node is placed at the end of the
8056 -- current declarative part.
8057
8058 Enc_G := Enclosing_Package_Body (Gen_Body);
8059 Enc_I := Enclosing_Package_Body (Inst_Node);
8060 Ensure_Freeze_Node (Pack_Id);
8061 F_Node := Freeze_Node (Pack_Id);
8062
8063 if Is_Generic_Instance (Par)
8064 and then Present (Freeze_Node (Par))
8065 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
8066 then
8067 -- The parent was a premature instantiation. Insert freeze node at
8068 -- the end the current declarative part.
8069
8070 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
8071 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8072
8073 -- Handle the following case:
8074 --
8075 -- package Parent_Inst is new ...
8076 -- Parent_Inst []
8077 --
8078 -- procedure P ... -- this body freezes Parent_Inst
8079 --
8080 -- package Inst is new ...
8081 --
8082 -- In this particular scenario, the freeze node for Inst must be
8083 -- inserted in the same manner as that of Parent_Inst - before the
8084 -- next source body or at the end of the declarative list (body not
8085 -- available). If body P did not exist and Parent_Inst was frozen
8086 -- after Inst, either by a body following Inst or at the end of the
8087 -- declarative region, the freeze node for Inst must be inserted
8088 -- after that of Parent_Inst. This relation is established by
8089 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8090
8091 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
8092 List_Containing (Inst_Node)
8093 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8094 then
8095 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8096
8097 else
8098 Insert_After (Freeze_Node (Par), F_Node);
8099 end if;
8100
8101 -- The body enclosing the instance should be frozen after the body that
8102 -- includes the generic, because the body of the instance may make
8103 -- references to entities therein. If the two are not in the same
8104 -- declarative part, or if the one enclosing the instance is frozen
8105 -- already, freeze the instance at the end of the current declarative
8106 -- part.
8107
8108 elsif Is_Generic_Instance (Par)
8109 and then Present (Freeze_Node (Par))
8110 and then Present (Enc_I)
8111 then
8112 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
8113 or else
8114 (Nkind (Enc_I) = N_Package_Body
8115 and then
8116 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
8117 then
8118 -- The enclosing package may contain several instances. Rather
8119 -- than computing the earliest point at which to insert its freeze
8120 -- node, we place it at the end of the declarative part of the
8121 -- parent of the generic.
8122
8123 Insert_Freeze_Node_For_Instance
8124 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8125 end if;
8126
8127 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8128
8129 elsif Present (Enc_G)
8130 and then Present (Enc_I)
8131 and then Enc_G /= Enc_I
8132 and then Earlier (Inst_Node, Gen_Body)
8133 then
8134 if Nkind (Enc_G) = N_Package_Body then
8135 E_G_Id :=
8136 Corresponding_Spec (Enc_G);
8137 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8138 E_G_Id :=
8139 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8140 end if;
8141
8142 -- Freeze package that encloses instance, and place node after the
8143 -- package that encloses generic. If enclosing package is already
8144 -- frozen we have to assume it is at the proper place. This may be a
8145 -- potential ABE that requires dynamic checking. Do not add a freeze
8146 -- node if the package that encloses the generic is inside the body
8147 -- that encloses the instance, because the freeze node would be in
8148 -- the wrong scope. Additional contortions needed if the bodies are
8149 -- within a subunit.
8150
8151 declare
8152 Enclosing_Body : Node_Id;
8153
8154 begin
8155 if Nkind (Enc_I) = N_Package_Body_Stub then
8156 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8157 else
8158 Enclosing_Body := Enc_I;
8159 end if;
8160
8161 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8162 Insert_Freeze_Node_For_Instance
8163 (Enc_G, Package_Freeze_Node (Enc_I));
8164 end if;
8165 end;
8166
8167 -- Freeze enclosing subunit before instance
8168
8169 Ensure_Freeze_Node (E_G_Id);
8170
8171 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8172 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8173 end if;
8174
8175 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8176
8177 else
8178 -- If none of the above, insert freeze node at the end of the current
8179 -- declarative part.
8180
8181 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8182 end if;
8183 end Freeze_Subprogram_Body;
8184
8185 ----------------
8186 -- Get_Gen_Id --
8187 ----------------
8188
8189 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8190 begin
8191 return Generic_Renamings.Table (E).Gen_Id;
8192 end Get_Gen_Id;
8193
8194 ---------------------
8195 -- Get_Instance_Of --
8196 ---------------------
8197
8198 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8199 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8200
8201 begin
8202 if Res /= Assoc_Null then
8203 return Generic_Renamings.Table (Res).Act_Id;
8204
8205 else
8206 -- On exit, entity is not instantiated: not a generic parameter, or
8207 -- else parameter of an inner generic unit.
8208
8209 return A;
8210 end if;
8211 end Get_Instance_Of;
8212
8213 ------------------------------------
8214 -- Get_Package_Instantiation_Node --
8215 ------------------------------------
8216
8217 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
8218 Decl : Node_Id := Unit_Declaration_Node (A);
8219 Inst : Node_Id;
8220
8221 begin
8222 -- If the Package_Instantiation attribute has been set on the package
8223 -- entity, then use it directly when it (or its Original_Node) refers
8224 -- to an N_Package_Instantiation node. In principle it should be
8225 -- possible to have this field set in all cases, which should be
8226 -- investigated, and would allow this function to be significantly
8227 -- simplified. ???
8228
8229 Inst := Package_Instantiation (A);
8230
8231 if Present (Inst) then
8232 if Nkind (Inst) = N_Package_Instantiation then
8233 return Inst;
8234
8235 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8236 return Original_Node (Inst);
8237 end if;
8238 end if;
8239
8240 -- If the instantiation is a compilation unit that does not need body
8241 -- then the instantiation node has been rewritten as a package
8242 -- declaration for the instance, and we return the original node.
8243
8244 -- If it is a compilation unit and the instance node has not been
8245 -- rewritten, then it is still the unit of the compilation. Finally, if
8246 -- a body is present, this is a parent of the main unit whose body has
8247 -- been compiled for inlining purposes, and the instantiation node has
8248 -- been rewritten with the instance body.
8249
8250 -- Otherwise the instantiation node appears after the declaration. If
8251 -- the entity is a formal package, the declaration may have been
8252 -- rewritten as a generic declaration (in the case of a formal with box)
8253 -- or left as a formal package declaration if it has actuals, and is
8254 -- found with a forward search.
8255
8256 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8257 if Nkind (Decl) = N_Package_Declaration
8258 and then Present (Corresponding_Body (Decl))
8259 then
8260 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8261 end if;
8262
8263 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
8264 return Original_Node (Decl);
8265 else
8266 return Unit (Parent (Decl));
8267 end if;
8268
8269 elsif Nkind (Decl) = N_Package_Declaration
8270 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8271 then
8272 return Original_Node (Decl);
8273
8274 else
8275 Inst := Next (Decl);
8276 while not Nkind_In (Inst, N_Package_Instantiation,
8277 N_Formal_Package_Declaration)
8278 loop
8279 Next (Inst);
8280 end loop;
8281
8282 return Inst;
8283 end if;
8284 end Get_Package_Instantiation_Node;
8285
8286 ------------------------
8287 -- Has_Been_Exchanged --
8288 ------------------------
8289
8290 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8291 Next : Elmt_Id;
8292
8293 begin
8294 Next := First_Elmt (Exchanged_Views);
8295 while Present (Next) loop
8296 if Full_View (Node (Next)) = E then
8297 return True;
8298 end if;
8299
8300 Next_Elmt (Next);
8301 end loop;
8302
8303 return False;
8304 end Has_Been_Exchanged;
8305
8306 ----------
8307 -- Hash --
8308 ----------
8309
8310 function Hash (F : Entity_Id) return HTable_Range is
8311 begin
8312 return HTable_Range (F mod HTable_Size);
8313 end Hash;
8314
8315 ------------------------
8316 -- Hide_Current_Scope --
8317 ------------------------
8318
8319 procedure Hide_Current_Scope is
8320 C : constant Entity_Id := Current_Scope;
8321 E : Entity_Id;
8322
8323 begin
8324 Set_Is_Hidden_Open_Scope (C);
8325
8326 E := First_Entity (C);
8327 while Present (E) loop
8328 if Is_Immediately_Visible (E) then
8329 Set_Is_Immediately_Visible (E, False);
8330 Append_Elmt (E, Hidden_Entities);
8331 end if;
8332
8333 Next_Entity (E);
8334 end loop;
8335
8336 -- Make the scope name invisible as well. This is necessary, but might
8337 -- conflict with calls to Rtsfind later on, in case the scope is a
8338 -- predefined one. There is no clean solution to this problem, so for
8339 -- now we depend on the user not redefining Standard itself in one of
8340 -- the parent units.
8341
8342 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8343 Set_Is_Immediately_Visible (C, False);
8344 Append_Elmt (C, Hidden_Entities);
8345 end if;
8346
8347 end Hide_Current_Scope;
8348
8349 --------------
8350 -- Init_Env --
8351 --------------
8352
8353 procedure Init_Env is
8354 Saved : Instance_Env;
8355
8356 begin
8357 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8358 Saved.Exchanged_Views := Exchanged_Views;
8359 Saved.Hidden_Entities := Hidden_Entities;
8360 Saved.Current_Sem_Unit := Current_Sem_Unit;
8361 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8362 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8363
8364 -- Save configuration switches. These may be reset if the unit is a
8365 -- predefined unit, and the current mode is not Ada 2005.
8366
8367 Save_Opt_Config_Switches (Saved.Switches);
8368
8369 Instance_Envs.Append (Saved);
8370
8371 Exchanged_Views := New_Elmt_List;
8372 Hidden_Entities := New_Elmt_List;
8373
8374 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8375 -- this is set properly in Set_Instance_Env.
8376
8377 Current_Instantiated_Parent :=
8378 (Current_Scope, Current_Scope, Assoc_Null);
8379 end Init_Env;
8380
8381 ------------------------------
8382 -- In_Same_Declarative_Part --
8383 ------------------------------
8384
8385 function In_Same_Declarative_Part
8386 (F_Node : Node_Id;
8387 Inst : Node_Id) return Boolean
8388 is
8389 Decls : constant Node_Id := Parent (F_Node);
8390 Nod : Node_Id;
8391
8392 begin
8393 Nod := Parent (Inst);
8394 while Present (Nod) loop
8395 if Nod = Decls then
8396 return True;
8397
8398 elsif Nkind_In (Nod, N_Subprogram_Body,
8399 N_Package_Body,
8400 N_Package_Declaration,
8401 N_Task_Body,
8402 N_Protected_Body,
8403 N_Block_Statement)
8404 then
8405 return False;
8406
8407 elsif Nkind (Nod) = N_Subunit then
8408 Nod := Corresponding_Stub (Nod);
8409
8410 elsif Nkind (Nod) = N_Compilation_Unit then
8411 return False;
8412
8413 else
8414 Nod := Parent (Nod);
8415 end if;
8416 end loop;
8417
8418 return False;
8419 end In_Same_Declarative_Part;
8420
8421 ---------------------
8422 -- In_Main_Context --
8423 ---------------------
8424
8425 function In_Main_Context (E : Entity_Id) return Boolean is
8426 Context : List_Id;
8427 Clause : Node_Id;
8428 Nam : Node_Id;
8429
8430 begin
8431 if not Is_Compilation_Unit (E)
8432 or else Ekind (E) /= E_Package
8433 or else In_Private_Part (E)
8434 then
8435 return False;
8436 end if;
8437
8438 Context := Context_Items (Cunit (Main_Unit));
8439
8440 Clause := First (Context);
8441 while Present (Clause) loop
8442 if Nkind (Clause) = N_With_Clause then
8443 Nam := Name (Clause);
8444
8445 -- If the current scope is part of the context of the main unit,
8446 -- analysis of the corresponding with_clause is not complete, and
8447 -- the entity is not set. We use the Chars field directly, which
8448 -- might produce false positives in rare cases, but guarantees
8449 -- that we produce all the instance bodies we will need.
8450
8451 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8452 or else (Nkind (Nam) = N_Selected_Component
8453 and then Chars (Selector_Name (Nam)) = Chars (E))
8454 then
8455 return True;
8456 end if;
8457 end if;
8458
8459 Next (Clause);
8460 end loop;
8461
8462 return False;
8463 end In_Main_Context;
8464
8465 ---------------------
8466 -- Inherit_Context --
8467 ---------------------
8468
8469 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8470 Current_Context : List_Id;
8471 Current_Unit : Node_Id;
8472 Item : Node_Id;
8473 New_I : Node_Id;
8474
8475 Clause : Node_Id;
8476 OK : Boolean;
8477 Lib_Unit : Node_Id;
8478
8479 begin
8480 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8481
8482 -- The inherited context is attached to the enclosing compilation
8483 -- unit. This is either the main unit, or the declaration for the
8484 -- main unit (in case the instantiation appears within the package
8485 -- declaration and the main unit is its body).
8486
8487 Current_Unit := Parent (Inst);
8488 while Present (Current_Unit)
8489 and then Nkind (Current_Unit) /= N_Compilation_Unit
8490 loop
8491 Current_Unit := Parent (Current_Unit);
8492 end loop;
8493
8494 Current_Context := Context_Items (Current_Unit);
8495
8496 Item := First (Context_Items (Parent (Gen_Decl)));
8497 while Present (Item) loop
8498 if Nkind (Item) = N_With_Clause then
8499 Lib_Unit := Library_Unit (Item);
8500
8501 -- Take care to prevent direct cyclic with's
8502
8503 if Lib_Unit /= Current_Unit then
8504
8505 -- Do not add a unit if it is already in the context
8506
8507 Clause := First (Current_Context);
8508 OK := True;
8509 while Present (Clause) loop
8510 if Nkind (Clause) = N_With_Clause and then
8511 Library_Unit (Clause) = Lib_Unit
8512 then
8513 OK := False;
8514 exit;
8515 end if;
8516
8517 Next (Clause);
8518 end loop;
8519
8520 if OK then
8521 New_I := New_Copy (Item);
8522 Set_Implicit_With (New_I, True);
8523 Set_Implicit_With_From_Instantiation (New_I, True);
8524 Append (New_I, Current_Context);
8525 end if;
8526 end if;
8527 end if;
8528
8529 Next (Item);
8530 end loop;
8531 end if;
8532 end Inherit_Context;
8533
8534 ----------------
8535 -- Initialize --
8536 ----------------
8537
8538 procedure Initialize is
8539 begin
8540 Generic_Renamings.Init;
8541 Instance_Envs.Init;
8542 Generic_Flags.Init;
8543 Generic_Renamings_HTable.Reset;
8544 Circularity_Detected := False;
8545 Exchanged_Views := No_Elist;
8546 Hidden_Entities := No_Elist;
8547 end Initialize;
8548
8549 -------------------------------------
8550 -- Insert_Freeze_Node_For_Instance --
8551 -------------------------------------
8552
8553 procedure Insert_Freeze_Node_For_Instance
8554 (N : Node_Id;
8555 F_Node : Node_Id)
8556 is
8557 Decl : Node_Id;
8558 Decls : List_Id;
8559 Inst : Entity_Id;
8560 Par_N : Node_Id;
8561
8562 function Enclosing_Body (N : Node_Id) return Node_Id;
8563 -- Find enclosing package or subprogram body, if any. Freeze node may
8564 -- be placed at end of current declarative list if previous instance
8565 -- and current one have different enclosing bodies.
8566
8567 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8568 -- Find the local instance, if any, that declares the generic that is
8569 -- being instantiated. If present, the freeze node for this instance
8570 -- must follow the freeze node for the previous instance.
8571
8572 --------------------
8573 -- Enclosing_Body --
8574 --------------------
8575
8576 function Enclosing_Body (N : Node_Id) return Node_Id is
8577 P : Node_Id;
8578
8579 begin
8580 P := Parent (N);
8581 while Present (P)
8582 and then Nkind (Parent (P)) /= N_Compilation_Unit
8583 loop
8584 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8585 if Nkind (Parent (P)) = N_Subunit then
8586 return Corresponding_Stub (Parent (P));
8587 else
8588 return P;
8589 end if;
8590 end if;
8591
8592 P := True_Parent (P);
8593 end loop;
8594
8595 return Empty;
8596 end Enclosing_Body;
8597
8598 -----------------------
8599 -- Previous_Instance --
8600 -----------------------
8601
8602 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8603 S : Entity_Id;
8604
8605 begin
8606 S := Scope (Gen);
8607 while Present (S) and then S /= Standard_Standard loop
8608 if Is_Generic_Instance (S)
8609 and then In_Same_Source_Unit (S, N)
8610 then
8611 return S;
8612 end if;
8613
8614 S := Scope (S);
8615 end loop;
8616
8617 return Empty;
8618 end Previous_Instance;
8619
8620 -- Start of processing for Insert_Freeze_Node_For_Instance
8621
8622 begin
8623 if not Is_List_Member (F_Node) then
8624 Decl := N;
8625 Decls := List_Containing (N);
8626 Inst := Entity (F_Node);
8627 Par_N := Parent (Decls);
8628
8629 -- When processing a subprogram instantiation, utilize the actual
8630 -- subprogram instantiation rather than its package wrapper as it
8631 -- carries all the context information.
8632
8633 if Is_Wrapper_Package (Inst) then
8634 Inst := Related_Instance (Inst);
8635 end if;
8636
8637 -- If this is a package instance, check whether the generic is
8638 -- declared in a previous instance and the current instance is
8639 -- not within the previous one.
8640
8641 if Present (Generic_Parent (Parent (Inst)))
8642 and then Is_In_Main_Unit (N)
8643 then
8644 declare
8645 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8646 Par_I : constant Entity_Id :=
8647 Previous_Instance
8648 (Generic_Parent (Parent (Inst)));
8649 Scop : Entity_Id;
8650
8651 begin
8652 if Present (Par_I)
8653 and then Earlier (N, Freeze_Node (Par_I))
8654 then
8655 Scop := Scope (Inst);
8656
8657 -- If the current instance is within the one that contains
8658 -- the generic, the freeze node for the current one must
8659 -- appear in the current declarative part. Ditto, if the
8660 -- current instance is within another package instance or
8661 -- within a body that does not enclose the current instance.
8662 -- In these three cases the freeze node of the previous
8663 -- instance is not relevant.
8664
8665 while Present (Scop) and then Scop /= Standard_Standard loop
8666 exit when Scop = Par_I
8667 or else
8668 (Is_Generic_Instance (Scop)
8669 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8670 Scop := Scope (Scop);
8671 end loop;
8672
8673 -- Previous instance encloses current instance
8674
8675 if Scop = Par_I then
8676 null;
8677
8678 -- If the next node is a source body we must freeze in
8679 -- the current scope as well.
8680
8681 elsif Present (Next (N))
8682 and then Nkind_In (Next (N), N_Subprogram_Body,
8683 N_Package_Body)
8684 and then Comes_From_Source (Next (N))
8685 then
8686 null;
8687
8688 -- Current instance is within an unrelated instance
8689
8690 elsif Is_Generic_Instance (Scop) then
8691 null;
8692
8693 -- Current instance is within an unrelated body
8694
8695 elsif Present (Enclosing_N)
8696 and then Enclosing_N /= Enclosing_Body (Par_I)
8697 then
8698 null;
8699
8700 else
8701 Insert_After (Freeze_Node (Par_I), F_Node);
8702 return;
8703 end if;
8704 end if;
8705 end;
8706 end if;
8707
8708 -- When the instantiation occurs in a package declaration, append the
8709 -- freeze node to the private declarations (if any).
8710
8711 if Nkind (Par_N) = N_Package_Specification
8712 and then Decls = Visible_Declarations (Par_N)
8713 and then Present (Private_Declarations (Par_N))
8714 and then not Is_Empty_List (Private_Declarations (Par_N))
8715 then
8716 Decls := Private_Declarations (Par_N);
8717 Decl := First (Decls);
8718 end if;
8719
8720 -- Determine the proper freeze point of a package instantiation. We
8721 -- adhere to the general rule of a package or subprogram body causing
8722 -- freezing of anything before it in the same declarative region. In
8723 -- this case, the proper freeze point of a package instantiation is
8724 -- before the first source body which follows, or before a stub. This
8725 -- ensures that entities coming from the instance are already frozen
8726 -- and usable in source bodies.
8727
8728 if Nkind (Par_N) /= N_Package_Declaration
8729 and then Ekind (Inst) = E_Package
8730 and then Is_Generic_Instance (Inst)
8731 and then
8732 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8733 then
8734 while Present (Decl) loop
8735 if (Nkind (Decl) in N_Unit_Body
8736 or else
8737 Nkind (Decl) in N_Body_Stub)
8738 and then Comes_From_Source (Decl)
8739 then
8740 Insert_Before (Decl, F_Node);
8741 return;
8742 end if;
8743
8744 Next (Decl);
8745 end loop;
8746 end if;
8747
8748 -- In a package declaration, or if no previous body, insert at end
8749 -- of list.
8750
8751 Set_Sloc (F_Node, Sloc (Last (Decls)));
8752 Insert_After (Last (Decls), F_Node);
8753 end if;
8754 end Insert_Freeze_Node_For_Instance;
8755
8756 ------------------
8757 -- Install_Body --
8758 ------------------
8759
8760 procedure Install_Body
8761 (Act_Body : Node_Id;
8762 N : Node_Id;
8763 Gen_Body : Node_Id;
8764 Gen_Decl : Node_Id)
8765 is
8766 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
8767 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
8768 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
8769 Par : constant Entity_Id := Scope (Gen_Id);
8770 Gen_Unit : constant Node_Id :=
8771 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
8772 Orig_Body : Node_Id := Gen_Body;
8773 F_Node : Node_Id;
8774 Body_Unit : Node_Id;
8775
8776 Must_Delay : Boolean;
8777
8778 function In_Same_Enclosing_Subp return Boolean;
8779 -- Check whether instance and generic body are within same subprogram.
8780
8781 function True_Sloc (N : Node_Id) return Source_Ptr;
8782 -- If the instance is nested inside a generic unit, the Sloc of the
8783 -- instance indicates the place of the original definition, not the
8784 -- point of the current enclosing instance. Pending a better usage of
8785 -- Slocs to indicate instantiation places, we determine the place of
8786 -- origin of a node by finding the maximum sloc of any ancestor node.
8787 -- Why is this not equivalent to Top_Level_Location ???
8788
8789 ----------------------------
8790 -- In_Same_Enclosing_Subp --
8791 ----------------------------
8792
8793 function In_Same_Enclosing_Subp return Boolean is
8794 Scop : Entity_Id;
8795 Subp : Entity_Id;
8796
8797 begin
8798 Scop := Scope (Act_Id);
8799 while Scop /= Standard_Standard
8800 and then not Is_Overloadable (Scop)
8801 loop
8802 Scop := Scope (Scop);
8803 end loop;
8804
8805 if Scop = Standard_Standard then
8806 return False;
8807 else
8808 Subp := Scop;
8809 end if;
8810
8811 Scop := Scope (Gen_Id);
8812 while Scop /= Standard_Standard loop
8813 if Scop = Subp then
8814 return True;
8815 else
8816 Scop := Scope (Scop);
8817 end if;
8818 end loop;
8819
8820 return False;
8821 end In_Same_Enclosing_Subp;
8822
8823 ---------------
8824 -- True_Sloc --
8825 ---------------
8826
8827 function True_Sloc (N : Node_Id) return Source_Ptr is
8828 Res : Source_Ptr;
8829 N1 : Node_Id;
8830
8831 begin
8832 Res := Sloc (N);
8833 N1 := N;
8834 while Present (N1) and then N1 /= Act_Unit loop
8835 if Sloc (N1) > Res then
8836 Res := Sloc (N1);
8837 end if;
8838
8839 N1 := Parent (N1);
8840 end loop;
8841
8842 return Res;
8843 end True_Sloc;
8844
8845 -- Start of processing for Install_Body
8846
8847 begin
8848 -- If the body is a subunit, the freeze point is the corresponding stub
8849 -- in the current compilation, not the subunit itself.
8850
8851 if Nkind (Parent (Gen_Body)) = N_Subunit then
8852 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
8853 else
8854 Orig_Body := Gen_Body;
8855 end if;
8856
8857 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
8858
8859 -- If the instantiation and the generic definition appear in the same
8860 -- package declaration, this is an early instantiation. If they appear
8861 -- in the same declarative part, it is an early instantiation only if
8862 -- the generic body appears textually later, and the generic body is
8863 -- also in the main unit.
8864
8865 -- If instance is nested within a subprogram, and the generic body
8866 -- is not, the instance is delayed because the enclosing body is. If
8867 -- instance and body are within the same scope, or the same subprogram
8868 -- body, indicate explicitly that the instance is delayed.
8869
8870 Must_Delay :=
8871 (Gen_Unit = Act_Unit
8872 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
8873 N_Generic_Package_Declaration)
8874 or else (Gen_Unit = Body_Unit
8875 and then True_Sloc (N) < Sloc (Orig_Body)))
8876 and then Is_In_Main_Unit (Gen_Unit)
8877 and then (Scope (Act_Id) = Scope (Gen_Id)
8878 or else In_Same_Enclosing_Subp));
8879
8880 -- If this is an early instantiation, the freeze node is placed after
8881 -- the generic body. Otherwise, if the generic appears in an instance,
8882 -- we cannot freeze the current instance until the outer one is frozen.
8883 -- This is only relevant if the current instance is nested within some
8884 -- inner scope not itself within the outer instance. If this scope is
8885 -- a package body in the same declarative part as the outer instance,
8886 -- then that body needs to be frozen after the outer instance. Finally,
8887 -- if no delay is needed, we place the freeze node at the end of the
8888 -- current declarative part.
8889
8890 if Expander_Active then
8891 Ensure_Freeze_Node (Act_Id);
8892 F_Node := Freeze_Node (Act_Id);
8893
8894 if Must_Delay then
8895 Insert_After (Orig_Body, F_Node);
8896
8897 elsif Is_Generic_Instance (Par)
8898 and then Present (Freeze_Node (Par))
8899 and then Scope (Act_Id) /= Par
8900 then
8901 -- Freeze instance of inner generic after instance of enclosing
8902 -- generic.
8903
8904 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
8905
8906 -- Handle the following case:
8907
8908 -- package Parent_Inst is new ...
8909 -- Parent_Inst []
8910
8911 -- procedure P ... -- this body freezes Parent_Inst
8912
8913 -- package Inst is new ...
8914
8915 -- In this particular scenario, the freeze node for Inst must
8916 -- be inserted in the same manner as that of Parent_Inst,
8917 -- before the next source body or at the end of the declarative
8918 -- list (body not available). If body P did not exist and
8919 -- Parent_Inst was frozen after Inst, either by a body
8920 -- following Inst or at the end of the declarative region,
8921 -- the freeze node for Inst must be inserted after that of
8922 -- Parent_Inst. This relation is established by comparing
8923 -- the Slocs of Parent_Inst freeze node and Inst.
8924
8925 if List_Containing (Get_Package_Instantiation_Node (Par)) =
8926 List_Containing (N)
8927 and then Sloc (Freeze_Node (Par)) < Sloc (N)
8928 then
8929 Insert_Freeze_Node_For_Instance (N, F_Node);
8930 else
8931 Insert_After (Freeze_Node (Par), F_Node);
8932 end if;
8933
8934 -- Freeze package enclosing instance of inner generic after
8935 -- instance of enclosing generic.
8936
8937 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
8938 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
8939 then
8940 declare
8941 Enclosing : Entity_Id;
8942
8943 begin
8944 Enclosing := Corresponding_Spec (Parent (N));
8945
8946 if No (Enclosing) then
8947 Enclosing := Defining_Entity (Parent (N));
8948 end if;
8949
8950 Insert_Freeze_Node_For_Instance (N, F_Node);
8951 Ensure_Freeze_Node (Enclosing);
8952
8953 if not Is_List_Member (Freeze_Node (Enclosing)) then
8954
8955 -- The enclosing context is a subunit, insert the freeze
8956 -- node after the stub.
8957
8958 if Nkind (Parent (Parent (N))) = N_Subunit then
8959 Insert_Freeze_Node_For_Instance
8960 (Corresponding_Stub (Parent (Parent (N))),
8961 Freeze_Node (Enclosing));
8962
8963 -- The enclosing context is a package with a stub body
8964 -- which has already been replaced by the real body.
8965 -- Insert the freeze node after the actual body.
8966
8967 elsif Ekind (Enclosing) = E_Package
8968 and then Present (Body_Entity (Enclosing))
8969 and then Was_Originally_Stub
8970 (Parent (Body_Entity (Enclosing)))
8971 then
8972 Insert_Freeze_Node_For_Instance
8973 (Parent (Body_Entity (Enclosing)),
8974 Freeze_Node (Enclosing));
8975
8976 -- The parent instance has been frozen before the body of
8977 -- the enclosing package, insert the freeze node after
8978 -- the body.
8979
8980 elsif List_Containing (Freeze_Node (Par)) =
8981 List_Containing (Parent (N))
8982 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
8983 then
8984 Insert_Freeze_Node_For_Instance
8985 (Parent (N), Freeze_Node (Enclosing));
8986
8987 else
8988 Insert_After
8989 (Freeze_Node (Par), Freeze_Node (Enclosing));
8990 end if;
8991 end if;
8992 end;
8993
8994 else
8995 Insert_Freeze_Node_For_Instance (N, F_Node);
8996 end if;
8997
8998 else
8999 Insert_Freeze_Node_For_Instance (N, F_Node);
9000 end if;
9001 end if;
9002
9003 Set_Is_Frozen (Act_Id);
9004 Insert_Before (N, Act_Body);
9005 Mark_Rewrite_Insertion (Act_Body);
9006 end Install_Body;
9007
9008 -----------------------------
9009 -- Install_Formal_Packages --
9010 -----------------------------
9011
9012 procedure Install_Formal_Packages (Par : Entity_Id) is
9013 E : Entity_Id;
9014 Gen : Entity_Id;
9015 Gen_E : Entity_Id := Empty;
9016
9017 begin
9018 E := First_Entity (Par);
9019
9020 -- If we are installing an instance parent, locate the formal packages
9021 -- of its generic parent.
9022
9023 if Is_Generic_Instance (Par) then
9024 Gen := Generic_Parent (Package_Specification (Par));
9025 Gen_E := First_Entity (Gen);
9026 end if;
9027
9028 while Present (E) loop
9029 if Ekind (E) = E_Package
9030 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9031 then
9032 -- If this is the renaming for the parent instance, done
9033
9034 if Renamed_Object (E) = Par then
9035 exit;
9036
9037 -- The visibility of a formal of an enclosing generic is already
9038 -- correct.
9039
9040 elsif Denotes_Formal_Package (E) then
9041 null;
9042
9043 elsif Present (Associated_Formal_Package (E)) then
9044 Check_Generic_Actuals (Renamed_Object (E), True);
9045 Set_Is_Hidden (E, False);
9046
9047 -- Find formal package in generic unit that corresponds to
9048 -- (instance of) formal package in instance.
9049
9050 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9051 Next_Entity (Gen_E);
9052 end loop;
9053
9054 if Present (Gen_E) then
9055 Map_Formal_Package_Entities (Gen_E, E);
9056 end if;
9057 end if;
9058 end if;
9059
9060 Next_Entity (E);
9061
9062 if Present (Gen_E) then
9063 Next_Entity (Gen_E);
9064 end if;
9065 end loop;
9066 end Install_Formal_Packages;
9067
9068 --------------------
9069 -- Install_Parent --
9070 --------------------
9071
9072 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9073 Ancestors : constant Elist_Id := New_Elmt_List;
9074 S : constant Entity_Id := Current_Scope;
9075 Inst_Par : Entity_Id;
9076 First_Par : Entity_Id;
9077 Inst_Node : Node_Id;
9078 Gen_Par : Entity_Id;
9079 First_Gen : Entity_Id;
9080 Elmt : Elmt_Id;
9081
9082 procedure Install_Noninstance_Specs (Par : Entity_Id);
9083 -- Install the scopes of noninstance parent units ending with Par
9084
9085 procedure Install_Spec (Par : Entity_Id);
9086 -- The child unit is within the declarative part of the parent, so the
9087 -- declarations within the parent are immediately visible.
9088
9089 -------------------------------
9090 -- Install_Noninstance_Specs --
9091 -------------------------------
9092
9093 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9094 begin
9095 if Present (Par)
9096 and then Par /= Standard_Standard
9097 and then not In_Open_Scopes (Par)
9098 then
9099 Install_Noninstance_Specs (Scope (Par));
9100 Install_Spec (Par);
9101 end if;
9102 end Install_Noninstance_Specs;
9103
9104 ------------------
9105 -- Install_Spec --
9106 ------------------
9107
9108 procedure Install_Spec (Par : Entity_Id) is
9109 Spec : constant Node_Id := Package_Specification (Par);
9110
9111 begin
9112 -- If this parent of the child instance is a top-level unit,
9113 -- then record the unit and its visibility for later resetting in
9114 -- Remove_Parent. We exclude units that are generic instances, as we
9115 -- only want to record this information for the ultimate top-level
9116 -- noninstance parent (is that always correct???).
9117
9118 if Scope (Par) = Standard_Standard
9119 and then not Is_Generic_Instance (Par)
9120 then
9121 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9122 Instance_Parent_Unit := Par;
9123 end if;
9124
9125 -- Open the parent scope and make it and its declarations visible.
9126 -- If this point is not within a body, then only the visible
9127 -- declarations should be made visible, and installation of the
9128 -- private declarations is deferred until the appropriate point
9129 -- within analysis of the spec being instantiated (see the handling
9130 -- of parent visibility in Analyze_Package_Specification). This is
9131 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9132 -- private view problems that occur when compiling instantiations of
9133 -- a generic child of that package (Generic_Dispatching_Constructor).
9134 -- If the instance freezes a tagged type, inlinings of operations
9135 -- from Ada.Tags may need the full view of type Tag. If inlining took
9136 -- proper account of establishing visibility of inlined subprograms'
9137 -- parents then it should be possible to remove this
9138 -- special check. ???
9139
9140 Push_Scope (Par);
9141 Set_Is_Immediately_Visible (Par);
9142 Install_Visible_Declarations (Par);
9143 Set_Use (Visible_Declarations (Spec));
9144
9145 if In_Body or else Is_RTU (Par, Ada_Tags) then
9146 Install_Private_Declarations (Par);
9147 Set_Use (Private_Declarations (Spec));
9148 end if;
9149 end Install_Spec;
9150
9151 -- Start of processing for Install_Parent
9152
9153 begin
9154 -- We need to install the parent instance to compile the instantiation
9155 -- of the child, but the child instance must appear in the current
9156 -- scope. Given that we cannot place the parent above the current scope
9157 -- in the scope stack, we duplicate the current scope and unstack both
9158 -- after the instantiation is complete.
9159
9160 -- If the parent is itself the instantiation of a child unit, we must
9161 -- also stack the instantiation of its parent, and so on. Each such
9162 -- ancestor is the prefix of the name in a prior instantiation.
9163
9164 -- If this is a nested instance, the parent unit itself resolves to
9165 -- a renaming of the parent instance, whose declaration we need.
9166
9167 -- Finally, the parent may be a generic (not an instance) when the
9168 -- child unit appears as a formal package.
9169
9170 Inst_Par := P;
9171
9172 if Present (Renamed_Entity (Inst_Par)) then
9173 Inst_Par := Renamed_Entity (Inst_Par);
9174 end if;
9175
9176 First_Par := Inst_Par;
9177
9178 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9179
9180 First_Gen := Gen_Par;
9181
9182 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9183
9184 -- Load grandparent instance as well
9185
9186 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
9187
9188 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9189 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9190
9191 if Present (Renamed_Entity (Inst_Par)) then
9192 Inst_Par := Renamed_Entity (Inst_Par);
9193 end if;
9194
9195 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9196
9197 if Present (Gen_Par) then
9198 Prepend_Elmt (Inst_Par, Ancestors);
9199
9200 else
9201 -- Parent is not the name of an instantiation
9202
9203 Install_Noninstance_Specs (Inst_Par);
9204 exit;
9205 end if;
9206
9207 else
9208 -- Previous error
9209
9210 exit;
9211 end if;
9212 end loop;
9213
9214 if Present (First_Gen) then
9215 Append_Elmt (First_Par, Ancestors);
9216 else
9217 Install_Noninstance_Specs (First_Par);
9218 end if;
9219
9220 if not Is_Empty_Elmt_List (Ancestors) then
9221 Elmt := First_Elmt (Ancestors);
9222 while Present (Elmt) loop
9223 Install_Spec (Node (Elmt));
9224 Install_Formal_Packages (Node (Elmt));
9225 Next_Elmt (Elmt);
9226 end loop;
9227 end if;
9228
9229 if not In_Body then
9230 Push_Scope (S);
9231 end if;
9232 end Install_Parent;
9233
9234 -------------------------------
9235 -- Install_Hidden_Primitives --
9236 -------------------------------
9237
9238 procedure Install_Hidden_Primitives
9239 (Prims_List : in out Elist_Id;
9240 Gen_T : Entity_Id;
9241 Act_T : Entity_Id)
9242 is
9243 Elmt : Elmt_Id;
9244 List : Elist_Id := No_Elist;
9245 Prim_G_Elmt : Elmt_Id;
9246 Prim_A_Elmt : Elmt_Id;
9247 Prim_G : Node_Id;
9248 Prim_A : Node_Id;
9249
9250 begin
9251 -- No action needed in case of serious errors because we cannot trust
9252 -- in the order of primitives
9253
9254 if Serious_Errors_Detected > 0 then
9255 return;
9256
9257 -- No action possible if we don't have available the list of primitive
9258 -- operations
9259
9260 elsif No (Gen_T)
9261 or else not Is_Record_Type (Gen_T)
9262 or else not Is_Tagged_Type (Gen_T)
9263 or else not Is_Record_Type (Act_T)
9264 or else not Is_Tagged_Type (Act_T)
9265 then
9266 return;
9267
9268 -- There is no need to handle interface types since their primitives
9269 -- cannot be hidden
9270
9271 elsif Is_Interface (Gen_T) then
9272 return;
9273 end if;
9274
9275 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9276
9277 if not Is_Class_Wide_Type (Act_T) then
9278 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9279 else
9280 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9281 end if;
9282
9283 loop
9284 -- Skip predefined primitives in the generic formal
9285
9286 while Present (Prim_G_Elmt)
9287 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9288 loop
9289 Next_Elmt (Prim_G_Elmt);
9290 end loop;
9291
9292 -- Skip predefined primitives in the generic actual
9293
9294 while Present (Prim_A_Elmt)
9295 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9296 loop
9297 Next_Elmt (Prim_A_Elmt);
9298 end loop;
9299
9300 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9301
9302 Prim_G := Node (Prim_G_Elmt);
9303 Prim_A := Node (Prim_A_Elmt);
9304
9305 -- There is no need to handle interface primitives because their
9306 -- primitives are not hidden
9307
9308 exit when Present (Interface_Alias (Prim_G));
9309
9310 -- Here we install one hidden primitive
9311
9312 if Chars (Prim_G) /= Chars (Prim_A)
9313 and then Has_Suffix (Prim_A, 'P')
9314 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9315 then
9316 Set_Chars (Prim_A, Chars (Prim_G));
9317 Append_New_Elmt (Prim_A, To => List);
9318 end if;
9319
9320 Next_Elmt (Prim_A_Elmt);
9321 Next_Elmt (Prim_G_Elmt);
9322 end loop;
9323
9324 -- Append the elements to the list of temporarily visible primitives
9325 -- avoiding duplicates.
9326
9327 if Present (List) then
9328 if No (Prims_List) then
9329 Prims_List := New_Elmt_List;
9330 end if;
9331
9332 Elmt := First_Elmt (List);
9333 while Present (Elmt) loop
9334 Append_Unique_Elmt (Node (Elmt), Prims_List);
9335 Next_Elmt (Elmt);
9336 end loop;
9337 end if;
9338 end Install_Hidden_Primitives;
9339
9340 -------------------------------
9341 -- Restore_Hidden_Primitives --
9342 -------------------------------
9343
9344 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9345 Prim_Elmt : Elmt_Id;
9346 Prim : Node_Id;
9347
9348 begin
9349 if Prims_List /= No_Elist then
9350 Prim_Elmt := First_Elmt (Prims_List);
9351 while Present (Prim_Elmt) loop
9352 Prim := Node (Prim_Elmt);
9353 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9354 Next_Elmt (Prim_Elmt);
9355 end loop;
9356
9357 Prims_List := No_Elist;
9358 end if;
9359 end Restore_Hidden_Primitives;
9360
9361 --------------------------------
9362 -- Instantiate_Formal_Package --
9363 --------------------------------
9364
9365 function Instantiate_Formal_Package
9366 (Formal : Node_Id;
9367 Actual : Node_Id;
9368 Analyzed_Formal : Node_Id) return List_Id
9369 is
9370 Loc : constant Source_Ptr := Sloc (Actual);
9371 Actual_Pack : Entity_Id;
9372 Formal_Pack : Entity_Id;
9373 Gen_Parent : Entity_Id;
9374 Decls : List_Id;
9375 Nod : Node_Id;
9376 Parent_Spec : Node_Id;
9377
9378 procedure Find_Matching_Actual
9379 (F : Node_Id;
9380 Act : in out Entity_Id);
9381 -- We need to associate each formal entity in the formal package with
9382 -- the corresponding entity in the actual package. The actual package
9383 -- has been analyzed and possibly expanded, and as a result there is
9384 -- no one-to-one correspondence between the two lists (for example,
9385 -- the actual may include subtypes, itypes, and inherited primitive
9386 -- operations, interspersed among the renaming declarations for the
9387 -- actuals) . We retrieve the corresponding actual by name because each
9388 -- actual has the same name as the formal, and they do appear in the
9389 -- same order.
9390
9391 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9392 -- Retrieve entity of defining entity of generic formal parameter.
9393 -- Only the declarations of formals need to be considered when
9394 -- linking them to actuals, but the declarative list may include
9395 -- internal entities generated during analysis, and those are ignored.
9396
9397 procedure Match_Formal_Entity
9398 (Formal_Node : Node_Id;
9399 Formal_Ent : Entity_Id;
9400 Actual_Ent : Entity_Id);
9401 -- Associates the formal entity with the actual. In the case where
9402 -- Formal_Ent is a formal package, this procedure iterates through all
9403 -- of its formals and enters associations between the actuals occurring
9404 -- in the formal package's corresponding actual package (given by
9405 -- Actual_Ent) and the formal package's formal parameters. This
9406 -- procedure recurses if any of the parameters is itself a package.
9407
9408 function Is_Instance_Of
9409 (Act_Spec : Entity_Id;
9410 Gen_Anc : Entity_Id) return Boolean;
9411 -- The actual can be an instantiation of a generic within another
9412 -- instance, in which case there is no direct link from it to the
9413 -- original generic ancestor. In that case, we recognize that the
9414 -- ultimate ancestor is the same by examining names and scopes.
9415
9416 procedure Process_Nested_Formal (Formal : Entity_Id);
9417 -- If the current formal is declared with a box, its own formals are
9418 -- visible in the instance, as they were in the generic, and their
9419 -- Hidden flag must be reset. If some of these formals are themselves
9420 -- packages declared with a box, the processing must be recursive.
9421
9422 --------------------------
9423 -- Find_Matching_Actual --
9424 --------------------------
9425
9426 procedure Find_Matching_Actual
9427 (F : Node_Id;
9428 Act : in out Entity_Id)
9429 is
9430 Formal_Ent : Entity_Id;
9431
9432 begin
9433 case Nkind (Original_Node (F)) is
9434 when N_Formal_Object_Declaration |
9435 N_Formal_Type_Declaration =>
9436 Formal_Ent := Defining_Identifier (F);
9437
9438 while Chars (Act) /= Chars (Formal_Ent) loop
9439 Next_Entity (Act);
9440 end loop;
9441
9442 when N_Formal_Subprogram_Declaration |
9443 N_Formal_Package_Declaration |
9444 N_Package_Declaration |
9445 N_Generic_Package_Declaration =>
9446 Formal_Ent := Defining_Entity (F);
9447
9448 while Chars (Act) /= Chars (Formal_Ent) loop
9449 Next_Entity (Act);
9450 end loop;
9451
9452 when others =>
9453 raise Program_Error;
9454 end case;
9455 end Find_Matching_Actual;
9456
9457 -------------------------
9458 -- Match_Formal_Entity --
9459 -------------------------
9460
9461 procedure Match_Formal_Entity
9462 (Formal_Node : Node_Id;
9463 Formal_Ent : Entity_Id;
9464 Actual_Ent : Entity_Id)
9465 is
9466 Act_Pkg : Entity_Id;
9467
9468 begin
9469 Set_Instance_Of (Formal_Ent, Actual_Ent);
9470
9471 if Ekind (Actual_Ent) = E_Package then
9472
9473 -- Record associations for each parameter
9474
9475 Act_Pkg := Actual_Ent;
9476
9477 declare
9478 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9479 F_Ent : Entity_Id;
9480 F_Node : Node_Id;
9481
9482 Gen_Decl : Node_Id;
9483 Formals : List_Id;
9484 Actual : Entity_Id;
9485
9486 begin
9487 -- Retrieve the actual given in the formal package declaration
9488
9489 Actual := Entity (Name (Original_Node (Formal_Node)));
9490
9491 -- The actual in the formal package declaration may be a
9492 -- renamed generic package, in which case we want to retrieve
9493 -- the original generic in order to traverse its formal part.
9494
9495 if Present (Renamed_Entity (Actual)) then
9496 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9497 else
9498 Gen_Decl := Unit_Declaration_Node (Actual);
9499 end if;
9500
9501 Formals := Generic_Formal_Declarations (Gen_Decl);
9502
9503 if Present (Formals) then
9504 F_Node := First_Non_Pragma (Formals);
9505 else
9506 F_Node := Empty;
9507 end if;
9508
9509 while Present (A_Ent)
9510 and then Present (F_Node)
9511 and then A_Ent /= First_Private_Entity (Act_Pkg)
9512 loop
9513 F_Ent := Get_Formal_Entity (F_Node);
9514
9515 if Present (F_Ent) then
9516
9517 -- This is a formal of the original package. Record
9518 -- association and recurse.
9519
9520 Find_Matching_Actual (F_Node, A_Ent);
9521 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9522 Next_Entity (A_Ent);
9523 end if;
9524
9525 Next_Non_Pragma (F_Node);
9526 end loop;
9527 end;
9528 end if;
9529 end Match_Formal_Entity;
9530
9531 -----------------------
9532 -- Get_Formal_Entity --
9533 -----------------------
9534
9535 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9536 Kind : constant Node_Kind := Nkind (Original_Node (N));
9537 begin
9538 case Kind is
9539 when N_Formal_Object_Declaration =>
9540 return Defining_Identifier (N);
9541
9542 when N_Formal_Type_Declaration =>
9543 return Defining_Identifier (N);
9544
9545 when N_Formal_Subprogram_Declaration =>
9546 return Defining_Unit_Name (Specification (N));
9547
9548 when N_Formal_Package_Declaration =>
9549 return Defining_Identifier (Original_Node (N));
9550
9551 when N_Generic_Package_Declaration =>
9552 return Defining_Identifier (Original_Node (N));
9553
9554 -- All other declarations are introduced by semantic analysis and
9555 -- have no match in the actual.
9556
9557 when others =>
9558 return Empty;
9559 end case;
9560 end Get_Formal_Entity;
9561
9562 --------------------
9563 -- Is_Instance_Of --
9564 --------------------
9565
9566 function Is_Instance_Of
9567 (Act_Spec : Entity_Id;
9568 Gen_Anc : Entity_Id) return Boolean
9569 is
9570 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9571
9572 begin
9573 if No (Gen_Par) then
9574 return False;
9575
9576 -- Simplest case: the generic parent of the actual is the formal
9577
9578 elsif Gen_Par = Gen_Anc then
9579 return True;
9580
9581 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9582 return False;
9583
9584 -- The actual may be obtained through several instantiations. Its
9585 -- scope must itself be an instance of a generic declared in the
9586 -- same scope as the formal. Any other case is detected above.
9587
9588 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9589 return False;
9590
9591 else
9592 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9593 end if;
9594 end Is_Instance_Of;
9595
9596 ---------------------------
9597 -- Process_Nested_Formal --
9598 ---------------------------
9599
9600 procedure Process_Nested_Formal (Formal : Entity_Id) is
9601 Ent : Entity_Id;
9602
9603 begin
9604 if Present (Associated_Formal_Package (Formal))
9605 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9606 then
9607 Ent := First_Entity (Formal);
9608 while Present (Ent) loop
9609 Set_Is_Hidden (Ent, False);
9610 Set_Is_Visible_Formal (Ent);
9611 Set_Is_Potentially_Use_Visible
9612 (Ent, Is_Potentially_Use_Visible (Formal));
9613
9614 if Ekind (Ent) = E_Package then
9615 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9616 Process_Nested_Formal (Ent);
9617 end if;
9618
9619 Next_Entity (Ent);
9620 end loop;
9621 end if;
9622 end Process_Nested_Formal;
9623
9624 -- Start of processing for Instantiate_Formal_Package
9625
9626 begin
9627 Analyze (Actual);
9628
9629 if not Is_Entity_Name (Actual)
9630 or else Ekind (Entity (Actual)) /= E_Package
9631 then
9632 Error_Msg_N
9633 ("expect package instance to instantiate formal", Actual);
9634 Abandon_Instantiation (Actual);
9635 raise Program_Error;
9636
9637 else
9638 Actual_Pack := Entity (Actual);
9639 Set_Is_Instantiated (Actual_Pack);
9640
9641 -- The actual may be a renamed package, or an outer generic formal
9642 -- package whose instantiation is converted into a renaming.
9643
9644 if Present (Renamed_Object (Actual_Pack)) then
9645 Actual_Pack := Renamed_Object (Actual_Pack);
9646 end if;
9647
9648 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9649 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9650 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9651 else
9652 Gen_Parent :=
9653 Generic_Parent (Specification (Analyzed_Formal));
9654 Formal_Pack :=
9655 Defining_Unit_Name (Specification (Analyzed_Formal));
9656 end if;
9657
9658 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9659 Parent_Spec := Package_Specification (Actual_Pack);
9660 else
9661 Parent_Spec := Parent (Actual_Pack);
9662 end if;
9663
9664 if Gen_Parent = Any_Id then
9665 Error_Msg_N
9666 ("previous error in declaration of formal package", Actual);
9667 Abandon_Instantiation (Actual);
9668
9669 elsif
9670 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9671 then
9672 null;
9673
9674 else
9675 Error_Msg_NE
9676 ("actual parameter must be instance of&", Actual, Gen_Parent);
9677 Abandon_Instantiation (Actual);
9678 end if;
9679
9680 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9681 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9682
9683 Nod :=
9684 Make_Package_Renaming_Declaration (Loc,
9685 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9686 Name => New_Occurrence_Of (Actual_Pack, Loc));
9687
9688 Set_Associated_Formal_Package
9689 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
9690 Decls := New_List (Nod);
9691
9692 -- If the formal F has a box, then the generic declarations are
9693 -- visible in the generic G. In an instance of G, the corresponding
9694 -- entities in the actual for F (which are the actuals for the
9695 -- instantiation of the generic that F denotes) must also be made
9696 -- visible for analysis of the current instance. On exit from the
9697 -- current instance, those entities are made private again. If the
9698 -- actual is currently in use, these entities are also use-visible.
9699
9700 -- The loop through the actual entities also steps through the formal
9701 -- entities and enters associations from formals to actuals into the
9702 -- renaming map. This is necessary to properly handle checking of
9703 -- actual parameter associations for later formals that depend on
9704 -- actuals declared in the formal package.
9705
9706 -- In Ada 2005, partial parameterization requires that we make
9707 -- visible the actuals corresponding to formals that were defaulted
9708 -- in the formal package. There formals are identified because they
9709 -- remain formal generics within the formal package, rather than
9710 -- being renamings of the actuals supplied.
9711
9712 declare
9713 Gen_Decl : constant Node_Id :=
9714 Unit_Declaration_Node (Gen_Parent);
9715 Formals : constant List_Id :=
9716 Generic_Formal_Declarations (Gen_Decl);
9717
9718 Actual_Ent : Entity_Id;
9719 Actual_Of_Formal : Node_Id;
9720 Formal_Node : Node_Id;
9721 Formal_Ent : Entity_Id;
9722
9723 begin
9724 if Present (Formals) then
9725 Formal_Node := First_Non_Pragma (Formals);
9726 else
9727 Formal_Node := Empty;
9728 end if;
9729
9730 Actual_Ent := First_Entity (Actual_Pack);
9731 Actual_Of_Formal :=
9732 First (Visible_Declarations (Specification (Analyzed_Formal)));
9733 while Present (Actual_Ent)
9734 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9735 loop
9736 if Present (Formal_Node) then
9737 Formal_Ent := Get_Formal_Entity (Formal_Node);
9738
9739 if Present (Formal_Ent) then
9740 Find_Matching_Actual (Formal_Node, Actual_Ent);
9741 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
9742
9743 -- We iterate at the same time over the actuals of the
9744 -- local package created for the formal, to determine
9745 -- which one of the formals of the original generic were
9746 -- defaulted in the formal. The corresponding actual
9747 -- entities are visible in the enclosing instance.
9748
9749 if Box_Present (Formal)
9750 or else
9751 (Present (Actual_Of_Formal)
9752 and then
9753 Is_Generic_Formal
9754 (Get_Formal_Entity (Actual_Of_Formal)))
9755 then
9756 Set_Is_Hidden (Actual_Ent, False);
9757 Set_Is_Visible_Formal (Actual_Ent);
9758 Set_Is_Potentially_Use_Visible
9759 (Actual_Ent, In_Use (Actual_Pack));
9760
9761 if Ekind (Actual_Ent) = E_Package then
9762 Process_Nested_Formal (Actual_Ent);
9763 end if;
9764
9765 else
9766 Set_Is_Hidden (Actual_Ent);
9767 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
9768 end if;
9769 end if;
9770
9771 Next_Non_Pragma (Formal_Node);
9772 Next (Actual_Of_Formal);
9773
9774 else
9775 -- No further formals to match, but the generic part may
9776 -- contain inherited operation that are not hidden in the
9777 -- enclosing instance.
9778
9779 Next_Entity (Actual_Ent);
9780 end if;
9781 end loop;
9782
9783 -- Inherited subprograms generated by formal derived types are
9784 -- also visible if the types are.
9785
9786 Actual_Ent := First_Entity (Actual_Pack);
9787 while Present (Actual_Ent)
9788 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9789 loop
9790 if Is_Overloadable (Actual_Ent)
9791 and then
9792 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
9793 and then
9794 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
9795 then
9796 Set_Is_Hidden (Actual_Ent, False);
9797 Set_Is_Potentially_Use_Visible
9798 (Actual_Ent, In_Use (Actual_Pack));
9799 end if;
9800
9801 Next_Entity (Actual_Ent);
9802 end loop;
9803 end;
9804
9805 -- If the formal is not declared with a box, reanalyze it as an
9806 -- abbreviated instantiation, to verify the matching rules of 12.7.
9807 -- The actual checks are performed after the generic associations
9808 -- have been analyzed, to guarantee the same visibility for this
9809 -- instantiation and for the actuals.
9810
9811 -- In Ada 2005, the generic associations for the formal can include
9812 -- defaulted parameters. These are ignored during check. This
9813 -- internal instantiation is removed from the tree after conformance
9814 -- checking, because it contains formal declarations for those
9815 -- defaulted parameters, and those should not reach the back-end.
9816
9817 if not Box_Present (Formal) then
9818 declare
9819 I_Pack : constant Entity_Id :=
9820 Make_Temporary (Sloc (Actual), 'P');
9821
9822 begin
9823 Set_Is_Internal (I_Pack);
9824
9825 Append_To (Decls,
9826 Make_Package_Instantiation (Sloc (Actual),
9827 Defining_Unit_Name => I_Pack,
9828 Name =>
9829 New_Occurrence_Of
9830 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
9831 Generic_Associations => Generic_Associations (Formal)));
9832 end;
9833 end if;
9834
9835 return Decls;
9836 end if;
9837 end Instantiate_Formal_Package;
9838
9839 -----------------------------------
9840 -- Instantiate_Formal_Subprogram --
9841 -----------------------------------
9842
9843 function Instantiate_Formal_Subprogram
9844 (Formal : Node_Id;
9845 Actual : Node_Id;
9846 Analyzed_Formal : Node_Id) return Node_Id
9847 is
9848 Analyzed_S : constant Entity_Id :=
9849 Defining_Unit_Name (Specification (Analyzed_Formal));
9850 Formal_Sub : constant Entity_Id :=
9851 Defining_Unit_Name (Specification (Formal));
9852
9853 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
9854 -- If the generic is a child unit, the parent has been installed on the
9855 -- scope stack, but a default subprogram cannot resolve to something
9856 -- on the parent because that parent is not really part of the visible
9857 -- context (it is there to resolve explicit local entities). If the
9858 -- default has resolved in this way, we remove the entity from immediate
9859 -- visibility and analyze the node again to emit an error message or
9860 -- find another visible candidate.
9861
9862 procedure Valid_Actual_Subprogram (Act : Node_Id);
9863 -- Perform legality check and raise exception on failure
9864
9865 -----------------------
9866 -- From_Parent_Scope --
9867 -----------------------
9868
9869 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
9870 Gen_Scope : Node_Id;
9871
9872 begin
9873 Gen_Scope := Scope (Analyzed_S);
9874 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
9875 if Scope (Subp) = Scope (Gen_Scope) then
9876 return True;
9877 end if;
9878
9879 Gen_Scope := Scope (Gen_Scope);
9880 end loop;
9881
9882 return False;
9883 end From_Parent_Scope;
9884
9885 -----------------------------
9886 -- Valid_Actual_Subprogram --
9887 -----------------------------
9888
9889 procedure Valid_Actual_Subprogram (Act : Node_Id) is
9890 Act_E : Entity_Id;
9891
9892 begin
9893 if Is_Entity_Name (Act) then
9894 Act_E := Entity (Act);
9895
9896 elsif Nkind (Act) = N_Selected_Component
9897 and then Is_Entity_Name (Selector_Name (Act))
9898 then
9899 Act_E := Entity (Selector_Name (Act));
9900
9901 else
9902 Act_E := Empty;
9903 end if;
9904
9905 if (Present (Act_E) and then Is_Overloadable (Act_E))
9906 or else Nkind_In (Act, N_Attribute_Reference,
9907 N_Indexed_Component,
9908 N_Character_Literal,
9909 N_Explicit_Dereference)
9910 then
9911 return;
9912 end if;
9913
9914 Error_Msg_NE
9915 ("expect subprogram or entry name in instantiation of &",
9916 Instantiation_Node, Formal_Sub);
9917 Abandon_Instantiation (Instantiation_Node);
9918 end Valid_Actual_Subprogram;
9919
9920 -- Local variables
9921
9922 Decl_Node : Node_Id;
9923 Loc : Source_Ptr;
9924 Nam : Node_Id;
9925 New_Spec : Node_Id;
9926 New_Subp : Entity_Id;
9927
9928 -- Start of processing for Instantiate_Formal_Subprogram
9929
9930 begin
9931 New_Spec := New_Copy_Tree (Specification (Formal));
9932
9933 -- The tree copy has created the proper instantiation sloc for the
9934 -- new specification. Use this location for all other constructed
9935 -- declarations.
9936
9937 Loc := Sloc (Defining_Unit_Name (New_Spec));
9938
9939 -- Create new entity for the actual (New_Copy_Tree does not), and
9940 -- indicate that it is an actual.
9941
9942 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
9943 Set_Ekind (New_Subp, Ekind (Analyzed_S));
9944 Set_Is_Generic_Actual_Subprogram (New_Subp);
9945 Set_Defining_Unit_Name (New_Spec, New_Subp);
9946
9947 -- Create new entities for the each of the formals in the specification
9948 -- of the renaming declaration built for the actual.
9949
9950 if Present (Parameter_Specifications (New_Spec)) then
9951 declare
9952 F : Node_Id;
9953 F_Id : Entity_Id;
9954
9955 begin
9956 F := First (Parameter_Specifications (New_Spec));
9957 while Present (F) loop
9958 F_Id := Defining_Identifier (F);
9959
9960 Set_Defining_Identifier (F,
9961 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
9962 Next (F);
9963 end loop;
9964 end;
9965 end if;
9966
9967 -- Find entity of actual. If the actual is an attribute reference, it
9968 -- cannot be resolved here (its formal is missing) but is handled
9969 -- instead in Attribute_Renaming. If the actual is overloaded, it is
9970 -- fully resolved subsequently, when the renaming declaration for the
9971 -- formal is analyzed. If it is an explicit dereference, resolve the
9972 -- prefix but not the actual itself, to prevent interpretation as call.
9973
9974 if Present (Actual) then
9975 Loc := Sloc (Actual);
9976 Set_Sloc (New_Spec, Loc);
9977
9978 if Nkind (Actual) = N_Operator_Symbol then
9979 Find_Direct_Name (Actual);
9980
9981 elsif Nkind (Actual) = N_Explicit_Dereference then
9982 Analyze (Prefix (Actual));
9983
9984 elsif Nkind (Actual) /= N_Attribute_Reference then
9985 Analyze (Actual);
9986 end if;
9987
9988 Valid_Actual_Subprogram (Actual);
9989 Nam := Actual;
9990
9991 elsif Present (Default_Name (Formal)) then
9992 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
9993 N_Selected_Component,
9994 N_Indexed_Component,
9995 N_Character_Literal)
9996 and then Present (Entity (Default_Name (Formal)))
9997 then
9998 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
9999 else
10000 Nam := New_Copy (Default_Name (Formal));
10001 Set_Sloc (Nam, Loc);
10002 end if;
10003
10004 elsif Box_Present (Formal) then
10005
10006 -- Actual is resolved at the point of instantiation. Create an
10007 -- identifier or operator with the same name as the formal.
10008
10009 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10010 Nam :=
10011 Make_Operator_Symbol (Loc,
10012 Chars => Chars (Formal_Sub),
10013 Strval => No_String);
10014 else
10015 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10016 end if;
10017
10018 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10019 and then Null_Present (Specification (Formal))
10020 then
10021 -- Generate null body for procedure, for use in the instance
10022
10023 Decl_Node :=
10024 Make_Subprogram_Body (Loc,
10025 Specification => New_Spec,
10026 Declarations => New_List,
10027 Handled_Statement_Sequence =>
10028 Make_Handled_Sequence_Of_Statements (Loc,
10029 Statements => New_List (Make_Null_Statement (Loc))));
10030
10031 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10032 return Decl_Node;
10033
10034 else
10035 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10036 Error_Msg_NE
10037 ("missing actual&", Instantiation_Node, Formal_Sub);
10038 Error_Msg_NE
10039 ("\in instantiation of & declared#",
10040 Instantiation_Node, Scope (Analyzed_S));
10041 Abandon_Instantiation (Instantiation_Node);
10042 end if;
10043
10044 Decl_Node :=
10045 Make_Subprogram_Renaming_Declaration (Loc,
10046 Specification => New_Spec,
10047 Name => Nam);
10048
10049 -- If we do not have an actual and the formal specified <> then set to
10050 -- get proper default.
10051
10052 if No (Actual) and then Box_Present (Formal) then
10053 Set_From_Default (Decl_Node);
10054 end if;
10055
10056 -- Gather possible interpretations for the actual before analyzing the
10057 -- instance. If overloaded, it will be resolved when analyzing the
10058 -- renaming declaration.
10059
10060 if Box_Present (Formal) and then No (Actual) then
10061 Analyze (Nam);
10062
10063 if Is_Child_Unit (Scope (Analyzed_S))
10064 and then Present (Entity (Nam))
10065 then
10066 if not Is_Overloaded (Nam) then
10067 if From_Parent_Scope (Entity (Nam)) then
10068 Set_Is_Immediately_Visible (Entity (Nam), False);
10069 Set_Entity (Nam, Empty);
10070 Set_Etype (Nam, Empty);
10071
10072 Analyze (Nam);
10073 Set_Is_Immediately_Visible (Entity (Nam));
10074 end if;
10075
10076 else
10077 declare
10078 I : Interp_Index;
10079 It : Interp;
10080
10081 begin
10082 Get_First_Interp (Nam, I, It);
10083 while Present (It.Nam) loop
10084 if From_Parent_Scope (It.Nam) then
10085 Remove_Interp (I);
10086 end if;
10087
10088 Get_Next_Interp (I, It);
10089 end loop;
10090 end;
10091 end if;
10092 end if;
10093 end if;
10094
10095 -- The generic instantiation freezes the actual. This can only be done
10096 -- once the actual is resolved, in the analysis of the renaming
10097 -- declaration. To make the formal subprogram entity available, we set
10098 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10099 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10100 -- of formal abstract subprograms.
10101
10102 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10103
10104 -- We cannot analyze the renaming declaration, and thus find the actual,
10105 -- until all the actuals are assembled in the instance. For subsequent
10106 -- checks of other actuals, indicate the node that will hold the
10107 -- instance of this formal.
10108
10109 Set_Instance_Of (Analyzed_S, Nam);
10110
10111 if Nkind (Actual) = N_Selected_Component
10112 and then Is_Task_Type (Etype (Prefix (Actual)))
10113 and then not Is_Frozen (Etype (Prefix (Actual)))
10114 then
10115 -- The renaming declaration will create a body, which must appear
10116 -- outside of the instantiation, We move the renaming declaration
10117 -- out of the instance, and create an additional renaming inside,
10118 -- to prevent freezing anomalies.
10119
10120 declare
10121 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10122
10123 begin
10124 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10125 Insert_Before (Instantiation_Node, Decl_Node);
10126 Analyze (Decl_Node);
10127
10128 -- Now create renaming within the instance
10129
10130 Decl_Node :=
10131 Make_Subprogram_Renaming_Declaration (Loc,
10132 Specification => New_Copy_Tree (New_Spec),
10133 Name => New_Occurrence_Of (Anon_Id, Loc));
10134
10135 Set_Defining_Unit_Name (Specification (Decl_Node),
10136 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10137 end;
10138 end if;
10139
10140 return Decl_Node;
10141 end Instantiate_Formal_Subprogram;
10142
10143 ------------------------
10144 -- Instantiate_Object --
10145 ------------------------
10146
10147 function Instantiate_Object
10148 (Formal : Node_Id;
10149 Actual : Node_Id;
10150 Analyzed_Formal : Node_Id) return List_Id
10151 is
10152 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10153 A_Gen_Obj : constant Entity_Id :=
10154 Defining_Identifier (Analyzed_Formal);
10155 Acc_Def : Node_Id := Empty;
10156 Act_Assoc : constant Node_Id := Parent (Actual);
10157 Actual_Decl : Node_Id := Empty;
10158 Decl_Node : Node_Id;
10159 Def : Node_Id;
10160 Ftyp : Entity_Id;
10161 List : constant List_Id := New_List;
10162 Loc : constant Source_Ptr := Sloc (Actual);
10163 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10164 Subt_Decl : Node_Id := Empty;
10165 Subt_Mark : Node_Id := Empty;
10166
10167 function Copy_Access_Def return Node_Id;
10168 -- If formal is an anonymous access, copy access definition of formal
10169 -- for generated object declaration.
10170
10171 ---------------------
10172 -- Copy_Access_Def --
10173 ---------------------
10174
10175 function Copy_Access_Def return Node_Id is
10176 begin
10177 Def := New_Copy_Tree (Acc_Def);
10178
10179 -- In addition, if formal is an access to subprogram we need to
10180 -- generate new formals for the signature of the default, so that
10181 -- the tree is properly formatted for ASIS use.
10182
10183 if Present (Access_To_Subprogram_Definition (Acc_Def)) then
10184 declare
10185 Par_Spec : Node_Id;
10186 begin
10187 Par_Spec :=
10188 First (Parameter_Specifications
10189 (Access_To_Subprogram_Definition (Def)));
10190 while Present (Par_Spec) loop
10191 Set_Defining_Identifier (Par_Spec,
10192 Make_Defining_Identifier (Sloc (Acc_Def),
10193 Chars => Chars (Defining_Identifier (Par_Spec))));
10194 Next (Par_Spec);
10195 end loop;
10196 end;
10197 end if;
10198
10199 return Def;
10200 end Copy_Access_Def;
10201
10202 -- Start of processing for Instantiate_Object
10203
10204 begin
10205 -- Formal may be an anonymous access
10206
10207 if Present (Subtype_Mark (Formal)) then
10208 Subt_Mark := Subtype_Mark (Formal);
10209 else
10210 Check_Access_Definition (Formal);
10211 Acc_Def := Access_Definition (Formal);
10212 end if;
10213
10214 -- Sloc for error message on missing actual
10215
10216 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10217
10218 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10219 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10220 end if;
10221
10222 Set_Parent (List, Parent (Actual));
10223
10224 -- OUT present
10225
10226 if Out_Present (Formal) then
10227
10228 -- An IN OUT generic actual must be a name. The instantiation is a
10229 -- renaming declaration. The actual is the name being renamed. We
10230 -- use the actual directly, rather than a copy, because it is not
10231 -- used further in the list of actuals, and because a copy or a use
10232 -- of relocate_node is incorrect if the instance is nested within a
10233 -- generic. In order to simplify ASIS searches, the Generic_Parent
10234 -- field links the declaration to the generic association.
10235
10236 if No (Actual) then
10237 Error_Msg_NE
10238 ("missing actual &",
10239 Instantiation_Node, Gen_Obj);
10240 Error_Msg_NE
10241 ("\in instantiation of & declared#",
10242 Instantiation_Node, Scope (A_Gen_Obj));
10243 Abandon_Instantiation (Instantiation_Node);
10244 end if;
10245
10246 if Present (Subt_Mark) then
10247 Decl_Node :=
10248 Make_Object_Renaming_Declaration (Loc,
10249 Defining_Identifier => New_Copy (Gen_Obj),
10250 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10251 Name => Actual);
10252
10253 else pragma Assert (Present (Acc_Def));
10254 Decl_Node :=
10255 Make_Object_Renaming_Declaration (Loc,
10256 Defining_Identifier => New_Copy (Gen_Obj),
10257 Access_Definition => New_Copy_Tree (Acc_Def),
10258 Name => Actual);
10259 end if;
10260
10261 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10262
10263 -- The analysis of the actual may produce Insert_Action nodes, so
10264 -- the declaration must have a context in which to attach them.
10265
10266 Append (Decl_Node, List);
10267 Analyze (Actual);
10268
10269 -- Return if the analysis of the actual reported some error
10270
10271 if Etype (Actual) = Any_Type then
10272 return List;
10273 end if;
10274
10275 -- This check is performed here because Analyze_Object_Renaming will
10276 -- not check it when Comes_From_Source is False. Note though that the
10277 -- check for the actual being the name of an object will be performed
10278 -- in Analyze_Object_Renaming.
10279
10280 if Is_Object_Reference (Actual)
10281 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10282 then
10283 Error_Msg_N
10284 ("illegal discriminant-dependent component for in out parameter",
10285 Actual);
10286 end if;
10287
10288 -- The actual has to be resolved in order to check that it is a
10289 -- variable (due to cases such as F (1), where F returns access to
10290 -- an array, and for overloaded prefixes).
10291
10292 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10293
10294 -- If the type of the formal is not itself a formal, and the current
10295 -- unit is a child unit, the formal type must be declared in a
10296 -- parent, and must be retrieved by visibility.
10297
10298 if Ftyp = Orig_Ftyp
10299 and then Is_Generic_Unit (Scope (Ftyp))
10300 and then Is_Child_Unit (Scope (A_Gen_Obj))
10301 then
10302 declare
10303 Temp : constant Node_Id :=
10304 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10305 begin
10306 Set_Entity (Temp, Empty);
10307 Find_Type (Temp);
10308 Ftyp := Entity (Temp);
10309 end;
10310 end if;
10311
10312 if Is_Private_Type (Ftyp)
10313 and then not Is_Private_Type (Etype (Actual))
10314 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10315 or else Base_Type (Etype (Actual)) = Ftyp)
10316 then
10317 -- If the actual has the type of the full view of the formal, or
10318 -- else a non-private subtype of the formal, then the visibility
10319 -- of the formal type has changed. Add to the actuals a subtype
10320 -- declaration that will force the exchange of views in the body
10321 -- of the instance as well.
10322
10323 Subt_Decl :=
10324 Make_Subtype_Declaration (Loc,
10325 Defining_Identifier => Make_Temporary (Loc, 'P'),
10326 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10327
10328 Prepend (Subt_Decl, List);
10329
10330 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10331 Exchange_Declarations (Ftyp);
10332 end if;
10333
10334 Resolve (Actual, Ftyp);
10335
10336 if not Denotes_Variable (Actual) then
10337 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
10338
10339 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10340
10341 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10342 -- the type of the actual shall resolve to a specific anonymous
10343 -- access type.
10344
10345 if Ada_Version < Ada_2005
10346 or else Ekind (Base_Type (Ftyp)) /=
10347 E_Anonymous_Access_Type
10348 or else Ekind (Base_Type (Etype (Actual))) /=
10349 E_Anonymous_Access_Type
10350 then
10351 Error_Msg_NE
10352 ("type of actual does not match type of&", Actual, Gen_Obj);
10353 end if;
10354 end if;
10355
10356 Note_Possible_Modification (Actual, Sure => True);
10357
10358 -- Check for instantiation of atomic/volatile actual for
10359 -- non-atomic/volatile formal (RM C.6 (12)).
10360
10361 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
10362 Error_Msg_N
10363 ("cannot instantiate non-atomic formal object "
10364 & "with atomic actual", Actual);
10365
10366 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
10367 then
10368 Error_Msg_N
10369 ("cannot instantiate non-volatile formal object "
10370 & "with volatile actual", Actual);
10371 end if;
10372
10373 -- Formal in-parameter
10374
10375 else
10376 -- The instantiation of a generic formal in-parameter is constant
10377 -- declaration. The actual is the expression for that declaration.
10378 -- Its type is a full copy of the type of the formal. This may be
10379 -- an access to subprogram, for which we need to generate entities
10380 -- for the formals in the new signature.
10381
10382 if Present (Actual) then
10383 if Present (Subt_Mark) then
10384 Def := New_Copy_Tree (Subt_Mark);
10385 else pragma Assert (Present (Acc_Def));
10386 Def := Copy_Access_Def;
10387 end if;
10388
10389 Decl_Node :=
10390 Make_Object_Declaration (Loc,
10391 Defining_Identifier => New_Copy (Gen_Obj),
10392 Constant_Present => True,
10393 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10394 Object_Definition => Def,
10395 Expression => Actual);
10396
10397 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10398
10399 -- A generic formal object of a tagged type is defined to be
10400 -- aliased so the new constant must also be treated as aliased.
10401
10402 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
10403 Set_Aliased_Present (Decl_Node);
10404 end if;
10405
10406 Append (Decl_Node, List);
10407
10408 -- No need to repeat (pre-)analysis of some expression nodes
10409 -- already handled in Preanalyze_Actuals.
10410
10411 if Nkind (Actual) /= N_Allocator then
10412 Analyze (Actual);
10413
10414 -- Return if the analysis of the actual reported some error
10415
10416 if Etype (Actual) = Any_Type then
10417 return List;
10418 end if;
10419 end if;
10420
10421 declare
10422 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
10423 Typ : Entity_Id;
10424
10425 begin
10426 Typ := Get_Instance_Of (Formal_Type);
10427
10428 -- If the actual appears in the current or an enclosing scope,
10429 -- use its type directly. This is relevant if it has an actual
10430 -- subtype that is distinct from its nominal one. This cannot
10431 -- be done in general because the type of the actual may
10432 -- depend on other actuals, and only be fully determined when
10433 -- the enclosing instance is analyzed.
10434
10435 if Present (Etype (Actual))
10436 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
10437 then
10438 Freeze_Before (Instantiation_Node, Etype (Actual));
10439 else
10440 Freeze_Before (Instantiation_Node, Typ);
10441 end if;
10442
10443 -- If the actual is an aggregate, perform name resolution on
10444 -- its components (the analysis of an aggregate does not do it)
10445 -- to capture local names that may be hidden if the generic is
10446 -- a child unit.
10447
10448 if Nkind (Actual) = N_Aggregate then
10449 Preanalyze_And_Resolve (Actual, Typ);
10450 end if;
10451
10452 if Is_Limited_Type (Typ)
10453 and then not OK_For_Limited_Init (Typ, Actual)
10454 then
10455 Error_Msg_N
10456 ("initialization not allowed for limited types", Actual);
10457 Explain_Limited_Type (Typ, Actual);
10458 end if;
10459 end;
10460
10461 elsif Present (Default_Expression (Formal)) then
10462
10463 -- Use default to construct declaration
10464
10465 if Present (Subt_Mark) then
10466 Def := New_Copy (Subt_Mark);
10467 else pragma Assert (Present (Acc_Def));
10468 Def := Copy_Access_Def;
10469 end if;
10470
10471 Decl_Node :=
10472 Make_Object_Declaration (Sloc (Formal),
10473 Defining_Identifier => New_Copy (Gen_Obj),
10474 Constant_Present => True,
10475 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10476 Object_Definition => Def,
10477 Expression => New_Copy_Tree
10478 (Default_Expression (Formal)));
10479
10480 Append (Decl_Node, List);
10481 Set_Analyzed (Expression (Decl_Node), False);
10482
10483 else
10484 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
10485 Error_Msg_NE ("\in instantiation of & declared#",
10486 Instantiation_Node, Scope (A_Gen_Obj));
10487
10488 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10489
10490 -- Create dummy constant declaration so that instance can be
10491 -- analyzed, to minimize cascaded visibility errors.
10492
10493 if Present (Subt_Mark) then
10494 Def := Subt_Mark;
10495 else pragma Assert (Present (Acc_Def));
10496 Def := Acc_Def;
10497 end if;
10498
10499 Decl_Node :=
10500 Make_Object_Declaration (Loc,
10501 Defining_Identifier => New_Copy (Gen_Obj),
10502 Constant_Present => True,
10503 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10504 Object_Definition => New_Copy (Def),
10505 Expression =>
10506 Make_Attribute_Reference (Sloc (Gen_Obj),
10507 Attribute_Name => Name_First,
10508 Prefix => New_Copy (Def)));
10509
10510 Append (Decl_Node, List);
10511
10512 else
10513 Abandon_Instantiation (Instantiation_Node);
10514 end if;
10515 end if;
10516 end if;
10517
10518 if Nkind (Actual) in N_Has_Entity then
10519 Actual_Decl := Parent (Entity (Actual));
10520 end if;
10521
10522 -- Ada 2005 (AI-423): For a formal object declaration with a null
10523 -- exclusion or an access definition that has a null exclusion: If the
10524 -- actual matching the formal object declaration denotes a generic
10525 -- formal object of another generic unit G, and the instantiation
10526 -- containing the actual occurs within the body of G or within the body
10527 -- of a generic unit declared within the declarative region of G, then
10528 -- the declaration of the formal object of G must have a null exclusion.
10529 -- Otherwise, the subtype of the actual matching the formal object
10530 -- declaration shall exclude null.
10531
10532 if Ada_Version >= Ada_2005
10533 and then Present (Actual_Decl)
10534 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10535 N_Object_Declaration)
10536 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10537 and then not Has_Null_Exclusion (Actual_Decl)
10538 and then Has_Null_Exclusion (Analyzed_Formal)
10539 then
10540 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10541 Error_Msg_N
10542 ("actual must exclude null to match generic formal#", Actual);
10543 end if;
10544
10545 -- An effectively volatile object cannot be used as an actual in
10546 -- a generic instance. The following check is only relevant when
10547 -- SPARK_Mode is on as it is not a standard Ada legality rule.
10548
10549 if SPARK_Mode = On
10550 and then Present (Actual)
10551 and then Is_Effectively_Volatile_Object (Actual)
10552 then
10553 Error_Msg_N
10554 ("volatile object cannot act as actual in generic instantiation "
10555 & "(SPARK RM 7.1.3(8))", Actual);
10556 end if;
10557
10558 return List;
10559 end Instantiate_Object;
10560
10561 ------------------------------
10562 -- Instantiate_Package_Body --
10563 ------------------------------
10564
10565 procedure Instantiate_Package_Body
10566 (Body_Info : Pending_Body_Info;
10567 Inlined_Body : Boolean := False;
10568 Body_Optional : Boolean := False)
10569 is
10570 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10571 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10572 Loc : constant Source_Ptr := Sloc (Inst_Node);
10573
10574 Gen_Id : constant Node_Id := Name (Inst_Node);
10575 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10576 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10577 Act_Spec : constant Node_Id := Specification (Act_Decl);
10578 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
10579
10580 Act_Body_Name : Node_Id;
10581 Gen_Body : Node_Id;
10582 Gen_Body_Id : Node_Id;
10583 Act_Body : Node_Id;
10584 Act_Body_Id : Entity_Id;
10585
10586 Parent_Installed : Boolean := False;
10587 Save_Style_Check : constant Boolean := Style_Check;
10588
10589 Par_Ent : Entity_Id := Empty;
10590 Par_Vis : Boolean := False;
10591
10592 Vis_Prims_List : Elist_Id := No_Elist;
10593 -- List of primitives made temporarily visible in the instantiation
10594 -- to match the visibility of the formal type
10595
10596 procedure Check_Initialized_Types;
10597 -- In a generic package body, an entity of a generic private type may
10598 -- appear uninitialized. This is suspicious, unless the actual is a
10599 -- fully initialized type.
10600
10601 -----------------------------
10602 -- Check_Initialized_Types --
10603 -----------------------------
10604
10605 procedure Check_Initialized_Types is
10606 Decl : Node_Id;
10607 Formal : Entity_Id;
10608 Actual : Entity_Id;
10609 Uninit_Var : Entity_Id;
10610
10611 begin
10612 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10613 while Present (Decl) loop
10614 Uninit_Var := Empty;
10615
10616 if Nkind (Decl) = N_Private_Extension_Declaration then
10617 Uninit_Var := Uninitialized_Variable (Decl);
10618
10619 elsif Nkind (Decl) = N_Formal_Type_Declaration
10620 and then Nkind (Formal_Type_Definition (Decl)) =
10621 N_Formal_Private_Type_Definition
10622 then
10623 Uninit_Var :=
10624 Uninitialized_Variable (Formal_Type_Definition (Decl));
10625 end if;
10626
10627 if Present (Uninit_Var) then
10628 Formal := Defining_Identifier (Decl);
10629 Actual := First_Entity (Act_Decl_Id);
10630
10631 -- For each formal there is a subtype declaration that renames
10632 -- the actual and has the same name as the formal. Locate the
10633 -- formal for warning message about uninitialized variables
10634 -- in the generic, for which the actual type should be a fully
10635 -- initialized type.
10636
10637 while Present (Actual) loop
10638 exit when Ekind (Actual) = E_Package
10639 and then Present (Renamed_Object (Actual));
10640
10641 if Chars (Actual) = Chars (Formal)
10642 and then not Is_Scalar_Type (Actual)
10643 and then not Is_Fully_Initialized_Type (Actual)
10644 and then Warn_On_No_Value_Assigned
10645 then
10646 Error_Msg_Node_2 := Formal;
10647 Error_Msg_NE
10648 ("generic unit has uninitialized variable& of "
10649 & "formal private type &?v?", Actual, Uninit_Var);
10650 Error_Msg_NE
10651 ("actual type for& should be fully initialized type?v?",
10652 Actual, Formal);
10653 exit;
10654 end if;
10655
10656 Next_Entity (Actual);
10657 end loop;
10658 end if;
10659
10660 Next (Decl);
10661 end loop;
10662 end Check_Initialized_Types;
10663
10664 -- Start of processing for Instantiate_Package_Body
10665
10666 begin
10667 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10668
10669 -- The instance body may already have been processed, as the parent of
10670 -- another instance that is inlined (Load_Parent_Of_Generic).
10671
10672 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10673 return;
10674 end if;
10675
10676 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10677
10678 -- Re-establish the state of information on which checks are suppressed.
10679 -- This information was set in Body_Info at the point of instantiation,
10680 -- and now we restore it so that the instance is compiled using the
10681 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
10682
10683 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10684 Scope_Suppress := Body_Info.Scope_Suppress;
10685 Opt.Ada_Version := Body_Info.Version;
10686 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10687 Restore_Warnings (Body_Info.Warnings);
10688 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10689 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10690
10691 if No (Gen_Body_Id) then
10692
10693 -- Do not look for parent of generic body if none is required.
10694 -- This may happen when the routine is called as part of the
10695 -- Pending_Instantiations processing, when nested instances
10696 -- may precede the one generated from the main unit.
10697
10698 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10699 and then Body_Optional
10700 then
10701 return;
10702 else
10703 Load_Parent_Of_Generic
10704 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10705 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10706 end if;
10707 end if;
10708
10709 -- Establish global variable for sloc adjustment and for error recovery
10710
10711 Instantiation_Node := Inst_Node;
10712
10713 if Present (Gen_Body_Id) then
10714 Save_Env (Gen_Unit, Act_Decl_Id);
10715 Style_Check := False;
10716 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10717
10718 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10719
10720 Create_Instantiation_Source
10721 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
10722
10723 Act_Body :=
10724 Copy_Generic_Node
10725 (Original_Node (Gen_Body), Empty, Instantiating => True);
10726
10727 -- Build new name (possibly qualified) for body declaration
10728
10729 Act_Body_Id := New_Copy (Act_Decl_Id);
10730
10731 -- Some attributes of spec entity are not inherited by body entity
10732
10733 Set_Handler_Records (Act_Body_Id, No_List);
10734
10735 if Nkind (Defining_Unit_Name (Act_Spec)) =
10736 N_Defining_Program_Unit_Name
10737 then
10738 Act_Body_Name :=
10739 Make_Defining_Program_Unit_Name (Loc,
10740 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
10741 Defining_Identifier => Act_Body_Id);
10742 else
10743 Act_Body_Name := Act_Body_Id;
10744 end if;
10745
10746 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
10747
10748 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
10749 Check_Generic_Actuals (Act_Decl_Id, False);
10750 Check_Initialized_Types;
10751
10752 -- Install primitives hidden at the point of the instantiation but
10753 -- visible when processing the generic formals
10754
10755 declare
10756 E : Entity_Id;
10757
10758 begin
10759 E := First_Entity (Act_Decl_Id);
10760 while Present (E) loop
10761 if Is_Type (E)
10762 and then Is_Generic_Actual_Type (E)
10763 and then Is_Tagged_Type (E)
10764 then
10765 Install_Hidden_Primitives
10766 (Prims_List => Vis_Prims_List,
10767 Gen_T => Generic_Parent_Type (Parent (E)),
10768 Act_T => E);
10769 end if;
10770
10771 Next_Entity (E);
10772 end loop;
10773 end;
10774
10775 -- If it is a child unit, make the parent instance (which is an
10776 -- instance of the parent of the generic) visible. The parent
10777 -- instance is the prefix of the name of the generic unit.
10778
10779 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10780 and then Nkind (Gen_Id) = N_Expanded_Name
10781 then
10782 Par_Ent := Entity (Prefix (Gen_Id));
10783 Par_Vis := Is_Immediately_Visible (Par_Ent);
10784 Install_Parent (Par_Ent, In_Body => True);
10785 Parent_Installed := True;
10786
10787 elsif Is_Child_Unit (Gen_Unit) then
10788 Par_Ent := Scope (Gen_Unit);
10789 Par_Vis := Is_Immediately_Visible (Par_Ent);
10790 Install_Parent (Par_Ent, In_Body => True);
10791 Parent_Installed := True;
10792 end if;
10793
10794 -- If the instantiation is a library unit, and this is the main unit,
10795 -- then build the resulting compilation unit nodes for the instance.
10796 -- If this is a compilation unit but it is not the main unit, then it
10797 -- is the body of a unit in the context, that is being compiled
10798 -- because it is encloses some inlined unit or another generic unit
10799 -- being instantiated. In that case, this body is not part of the
10800 -- current compilation, and is not attached to the tree, but its
10801 -- parent must be set for analysis.
10802
10803 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10804
10805 -- Replace instance node with body of instance, and create new
10806 -- node for corresponding instance declaration.
10807
10808 Build_Instance_Compilation_Unit_Nodes
10809 (Inst_Node, Act_Body, Act_Decl);
10810 Analyze (Inst_Node);
10811
10812 if Parent (Inst_Node) = Cunit (Main_Unit) then
10813
10814 -- If the instance is a child unit itself, then set the scope
10815 -- of the expanded body to be the parent of the instantiation
10816 -- (ensuring that the fully qualified name will be generated
10817 -- for the elaboration subprogram).
10818
10819 if Nkind (Defining_Unit_Name (Act_Spec)) =
10820 N_Defining_Program_Unit_Name
10821 then
10822 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
10823 end if;
10824 end if;
10825
10826 -- Case where instantiation is not a library unit
10827
10828 else
10829 -- If this is an early instantiation, i.e. appears textually
10830 -- before the corresponding body and must be elaborated first,
10831 -- indicate that the body instance is to be delayed.
10832
10833 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
10834
10835 -- Now analyze the body. We turn off all checks if this is an
10836 -- internal unit, since there is no reason to have checks on for
10837 -- any predefined run-time library code. All such code is designed
10838 -- to be compiled with checks off.
10839
10840 -- Note that we do NOT apply this criterion to children of GNAT
10841 -- The latter units must suppress checks explicitly if needed.
10842
10843 if Is_Predefined_File_Name
10844 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
10845 then
10846 Analyze (Act_Body, Suppress => All_Checks);
10847 else
10848 Analyze (Act_Body);
10849 end if;
10850 end if;
10851
10852 Inherit_Context (Gen_Body, Inst_Node);
10853
10854 -- Remove the parent instances if they have been placed on the scope
10855 -- stack to compile the body.
10856
10857 if Parent_Installed then
10858 Remove_Parent (In_Body => True);
10859
10860 -- Restore the previous visibility of the parent
10861
10862 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10863 end if;
10864
10865 Restore_Hidden_Primitives (Vis_Prims_List);
10866 Restore_Private_Views (Act_Decl_Id);
10867
10868 -- Remove the current unit from visibility if this is an instance
10869 -- that is not elaborated on the fly for inlining purposes.
10870
10871 if not Inlined_Body then
10872 Set_Is_Immediately_Visible (Act_Decl_Id, False);
10873 end if;
10874
10875 Restore_Env;
10876 Style_Check := Save_Style_Check;
10877
10878 -- If we have no body, and the unit requires a body, then complain. This
10879 -- complaint is suppressed if we have detected other errors (since a
10880 -- common reason for missing the body is that it had errors).
10881 -- In CodePeer mode, a warning has been emitted already, no need for
10882 -- further messages.
10883
10884 elsif Unit_Requires_Body (Gen_Unit)
10885 and then not Body_Optional
10886 then
10887 if CodePeer_Mode then
10888 null;
10889
10890 elsif Serious_Errors_Detected = 0 then
10891 Error_Msg_NE
10892 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
10893
10894 -- Don't attempt to perform any cleanup actions if some other error
10895 -- was already detected, since this can cause blowups.
10896
10897 else
10898 return;
10899 end if;
10900
10901 -- Case of package that does not need a body
10902
10903 else
10904 -- If the instantiation of the declaration is a library unit, rewrite
10905 -- the original package instantiation as a package declaration in the
10906 -- compilation unit node.
10907
10908 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10909 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
10910 Rewrite (Inst_Node, Act_Decl);
10911
10912 -- Generate elaboration entity, in case spec has elaboration code.
10913 -- This cannot be done when the instance is analyzed, because it
10914 -- is not known yet whether the body exists.
10915
10916 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
10917 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
10918
10919 -- If the instantiation is not a library unit, then append the
10920 -- declaration to the list of implicitly generated entities, unless
10921 -- it is already a list member which means that it was already
10922 -- processed
10923
10924 elsif not Is_List_Member (Act_Decl) then
10925 Mark_Rewrite_Insertion (Act_Decl);
10926 Insert_Before (Inst_Node, Act_Decl);
10927 end if;
10928 end if;
10929
10930 Expander_Mode_Restore;
10931 end Instantiate_Package_Body;
10932
10933 ---------------------------------
10934 -- Instantiate_Subprogram_Body --
10935 ---------------------------------
10936
10937 procedure Instantiate_Subprogram_Body
10938 (Body_Info : Pending_Body_Info;
10939 Body_Optional : Boolean := False)
10940 is
10941 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10942 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10943 Loc : constant Source_Ptr := Sloc (Inst_Node);
10944 Gen_Id : constant Node_Id := Name (Inst_Node);
10945 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10946 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10947 Anon_Id : constant Entity_Id :=
10948 Defining_Unit_Name (Specification (Act_Decl));
10949 Pack_Id : constant Entity_Id :=
10950 Defining_Unit_Name (Parent (Act_Decl));
10951
10952 Saved_Style_Check : constant Boolean := Style_Check;
10953 Saved_Warnings : constant Warning_Record := Save_Warnings;
10954
10955 Act_Body : Node_Id;
10956 Gen_Body : Node_Id;
10957 Gen_Body_Id : Node_Id;
10958 Pack_Body : Node_Id;
10959 Par_Ent : Entity_Id := Empty;
10960 Par_Vis : Boolean := False;
10961 Ret_Expr : Node_Id;
10962
10963 Parent_Installed : Boolean := False;
10964
10965 begin
10966 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10967
10968 -- Subprogram body may have been created already because of an inline
10969 -- pragma, or because of multiple elaborations of the enclosing package
10970 -- when several instances of the subprogram appear in the main unit.
10971
10972 if Present (Corresponding_Body (Act_Decl)) then
10973 return;
10974 end if;
10975
10976 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10977
10978 -- Re-establish the state of information on which checks are suppressed.
10979 -- This information was set in Body_Info at the point of instantiation,
10980 -- and now we restore it so that the instance is compiled using the
10981 -- check status at the instantiation (RM 11.5 (7.2/2), AI95-00224-01).
10982
10983 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10984 Scope_Suppress := Body_Info.Scope_Suppress;
10985 Opt.Ada_Version := Body_Info.Version;
10986 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10987 Restore_Warnings (Body_Info.Warnings);
10988 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10989 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10990
10991 if No (Gen_Body_Id) then
10992
10993 -- For imported generic subprogram, no body to compile, complete
10994 -- the spec entity appropriately.
10995
10996 if Is_Imported (Gen_Unit) then
10997 Set_Is_Imported (Anon_Id);
10998 Set_First_Rep_Item (Anon_Id, First_Rep_Item (Gen_Unit));
10999 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
11000 Set_Convention (Anon_Id, Convention (Gen_Unit));
11001 Set_Has_Completion (Anon_Id);
11002 return;
11003
11004 -- For other cases, compile the body
11005
11006 else
11007 Load_Parent_Of_Generic
11008 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11009 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11010 end if;
11011 end if;
11012
11013 Instantiation_Node := Inst_Node;
11014
11015 if Present (Gen_Body_Id) then
11016 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11017
11018 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11019
11020 -- Either body is not present, or context is non-expanding, as
11021 -- when compiling a subunit. Mark the instance as completed, and
11022 -- diagnose a missing body when needed.
11023
11024 if Expander_Active
11025 and then Operating_Mode = Generate_Code
11026 then
11027 Error_Msg_N
11028 ("missing proper body for instantiation", Gen_Body);
11029 end if;
11030
11031 Set_Has_Completion (Anon_Id);
11032 return;
11033 end if;
11034
11035 Save_Env (Gen_Unit, Anon_Id);
11036 Style_Check := False;
11037 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11038 Create_Instantiation_Source
11039 (Inst_Node,
11040 Gen_Body_Id,
11041 False,
11042 S_Adjustment);
11043
11044 Act_Body :=
11045 Copy_Generic_Node
11046 (Original_Node (Gen_Body), Empty, Instantiating => True);
11047
11048 -- Create proper defining name for the body, to correspond to
11049 -- the one in the spec.
11050
11051 Set_Defining_Unit_Name (Specification (Act_Body),
11052 Make_Defining_Identifier
11053 (Sloc (Defining_Entity (Inst_Node)), Chars (Anon_Id)));
11054 Set_Corresponding_Spec (Act_Body, Anon_Id);
11055 Set_Has_Completion (Anon_Id);
11056 Check_Generic_Actuals (Pack_Id, False);
11057
11058 -- Generate a reference to link the visible subprogram instance to
11059 -- the generic body, which for navigation purposes is the only
11060 -- available source for the instance.
11061
11062 Generate_Reference
11063 (Related_Instance (Pack_Id),
11064 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11065
11066 -- If it is a child unit, make the parent instance (which is an
11067 -- instance of the parent of the generic) visible. The parent
11068 -- instance is the prefix of the name of the generic unit.
11069
11070 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11071 and then Nkind (Gen_Id) = N_Expanded_Name
11072 then
11073 Par_Ent := Entity (Prefix (Gen_Id));
11074 Par_Vis := Is_Immediately_Visible (Par_Ent);
11075 Install_Parent (Par_Ent, In_Body => True);
11076 Parent_Installed := True;
11077
11078 elsif Is_Child_Unit (Gen_Unit) then
11079 Par_Ent := Scope (Gen_Unit);
11080 Par_Vis := Is_Immediately_Visible (Par_Ent);
11081 Install_Parent (Par_Ent, In_Body => True);
11082 Parent_Installed := True;
11083 end if;
11084
11085 -- Subprogram body is placed in the body of wrapper package,
11086 -- whose spec contains the subprogram declaration as well as
11087 -- the renaming declarations for the generic parameters.
11088
11089 Pack_Body :=
11090 Make_Package_Body (Loc,
11091 Defining_Unit_Name => New_Copy (Pack_Id),
11092 Declarations => New_List (Act_Body));
11093
11094 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11095
11096 -- If the instantiation is a library unit, then build resulting
11097 -- compilation unit nodes for the instance. The declaration of
11098 -- the enclosing package is the grandparent of the subprogram
11099 -- declaration. First replace the instantiation node as the unit
11100 -- of the corresponding compilation.
11101
11102 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11103 if Parent (Inst_Node) = Cunit (Main_Unit) then
11104 Set_Unit (Parent (Inst_Node), Inst_Node);
11105 Build_Instance_Compilation_Unit_Nodes
11106 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11107 Analyze (Inst_Node);
11108 else
11109 Set_Parent (Pack_Body, Parent (Inst_Node));
11110 Analyze (Pack_Body);
11111 end if;
11112
11113 else
11114 Insert_Before (Inst_Node, Pack_Body);
11115 Mark_Rewrite_Insertion (Pack_Body);
11116 Analyze (Pack_Body);
11117
11118 if Expander_Active then
11119 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11120 end if;
11121 end if;
11122
11123 Inherit_Context (Gen_Body, Inst_Node);
11124
11125 Restore_Private_Views (Pack_Id, False);
11126
11127 if Parent_Installed then
11128 Remove_Parent (In_Body => True);
11129
11130 -- Restore the previous visibility of the parent
11131
11132 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11133 end if;
11134
11135 Restore_Env;
11136 Style_Check := Saved_Style_Check;
11137 Restore_Warnings (Saved_Warnings);
11138
11139 -- Body not found. Error was emitted already. If there were no previous
11140 -- errors, this may be an instance whose scope is a premature instance.
11141 -- In that case we must insure that the (legal) program does raise
11142 -- program error if executed. We generate a subprogram body for this
11143 -- purpose. See DEC ac30vso.
11144
11145 -- Should not reference proprietary DEC tests in comments ???
11146
11147 elsif Serious_Errors_Detected = 0
11148 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
11149 then
11150 if Body_Optional then
11151 return;
11152
11153 elsif Ekind (Anon_Id) = E_Procedure then
11154 Act_Body :=
11155 Make_Subprogram_Body (Loc,
11156 Specification =>
11157 Make_Procedure_Specification (Loc,
11158 Defining_Unit_Name =>
11159 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
11160 Parameter_Specifications =>
11161 New_Copy_List
11162 (Parameter_Specifications (Parent (Anon_Id)))),
11163
11164 Declarations => Empty_List,
11165 Handled_Statement_Sequence =>
11166 Make_Handled_Sequence_Of_Statements (Loc,
11167 Statements =>
11168 New_List (
11169 Make_Raise_Program_Error (Loc,
11170 Reason =>
11171 PE_Access_Before_Elaboration))));
11172
11173 else
11174 Ret_Expr :=
11175 Make_Raise_Program_Error (Loc,
11176 Reason => PE_Access_Before_Elaboration);
11177
11178 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
11179 Set_Analyzed (Ret_Expr);
11180
11181 Act_Body :=
11182 Make_Subprogram_Body (Loc,
11183 Specification =>
11184 Make_Function_Specification (Loc,
11185 Defining_Unit_Name =>
11186 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
11187 Parameter_Specifications =>
11188 New_Copy_List
11189 (Parameter_Specifications (Parent (Anon_Id))),
11190 Result_Definition =>
11191 New_Occurrence_Of (Etype (Anon_Id), Loc)),
11192
11193 Declarations => Empty_List,
11194 Handled_Statement_Sequence =>
11195 Make_Handled_Sequence_Of_Statements (Loc,
11196 Statements =>
11197 New_List
11198 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
11199 end if;
11200
11201 Pack_Body := Make_Package_Body (Loc,
11202 Defining_Unit_Name => New_Copy (Pack_Id),
11203 Declarations => New_List (Act_Body));
11204
11205 Insert_After (Inst_Node, Pack_Body);
11206 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11207 Analyze (Pack_Body);
11208 end if;
11209
11210 Expander_Mode_Restore;
11211 end Instantiate_Subprogram_Body;
11212
11213 ----------------------
11214 -- Instantiate_Type --
11215 ----------------------
11216
11217 function Instantiate_Type
11218 (Formal : Node_Id;
11219 Actual : Node_Id;
11220 Analyzed_Formal : Node_Id;
11221 Actual_Decls : List_Id) return List_Id
11222 is
11223 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
11224 A_Gen_T : constant Entity_Id :=
11225 Defining_Identifier (Analyzed_Formal);
11226 Ancestor : Entity_Id := Empty;
11227 Def : constant Node_Id := Formal_Type_Definition (Formal);
11228 Act_T : Entity_Id;
11229 Decl_Node : Node_Id;
11230 Decl_Nodes : List_Id;
11231 Loc : Source_Ptr;
11232 Subt : Entity_Id;
11233
11234 procedure Diagnose_Predicated_Actual;
11235 -- There are a number of constructs in which a discrete type with
11236 -- predicates is illegal, e.g. as an index in an array type declaration.
11237 -- If a generic type is used is such a construct in a generic package
11238 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11239 -- of the generic contract that the actual cannot have predicates.
11240
11241 procedure Validate_Array_Type_Instance;
11242 procedure Validate_Access_Subprogram_Instance;
11243 procedure Validate_Access_Type_Instance;
11244 procedure Validate_Derived_Type_Instance;
11245 procedure Validate_Derived_Interface_Type_Instance;
11246 procedure Validate_Discriminated_Formal_Type;
11247 procedure Validate_Interface_Type_Instance;
11248 procedure Validate_Private_Type_Instance;
11249 procedure Validate_Incomplete_Type_Instance;
11250 -- These procedures perform validation tests for the named case.
11251 -- Validate_Discriminated_Formal_Type is shared by formal private
11252 -- types and Ada 2012 formal incomplete types.
11253
11254 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
11255 -- Check that base types are the same and that the subtypes match
11256 -- statically. Used in several of the above.
11257
11258 ---------------------------------
11259 -- Diagnose_Predicated_Actual --
11260 ---------------------------------
11261
11262 procedure Diagnose_Predicated_Actual is
11263 begin
11264 if No_Predicate_On_Actual (A_Gen_T)
11265 and then Has_Predicates (Act_T)
11266 then
11267 Error_Msg_NE
11268 ("actual for& cannot be a type with predicate",
11269 Instantiation_Node, A_Gen_T);
11270
11271 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
11272 and then Has_Predicates (Act_T)
11273 and then not Has_Static_Predicate_Aspect (Act_T)
11274 then
11275 Error_Msg_NE
11276 ("actual for& cannot be a type with a dynamic predicate",
11277 Instantiation_Node, A_Gen_T);
11278 end if;
11279 end Diagnose_Predicated_Actual;
11280
11281 --------------------
11282 -- Subtypes_Match --
11283 --------------------
11284
11285 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
11286 T : constant Entity_Id := Get_Instance_Of (Gen_T);
11287
11288 begin
11289 -- Some detailed comments would be useful here ???
11290
11291 return ((Base_Type (T) = Act_T
11292 or else Base_Type (T) = Base_Type (Act_T))
11293 and then Subtypes_Statically_Match (T, Act_T))
11294
11295 or else (Is_Class_Wide_Type (Gen_T)
11296 and then Is_Class_Wide_Type (Act_T)
11297 and then Subtypes_Match
11298 (Get_Instance_Of (Root_Type (Gen_T)),
11299 Root_Type (Act_T)))
11300
11301 or else
11302 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
11303 E_Anonymous_Access_Type)
11304 and then Ekind (Act_T) = Ekind (Gen_T)
11305 and then Subtypes_Statically_Match
11306 (Designated_Type (Gen_T), Designated_Type (Act_T)));
11307 end Subtypes_Match;
11308
11309 -----------------------------------------
11310 -- Validate_Access_Subprogram_Instance --
11311 -----------------------------------------
11312
11313 procedure Validate_Access_Subprogram_Instance is
11314 begin
11315 if not Is_Access_Type (Act_T)
11316 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
11317 then
11318 Error_Msg_NE
11319 ("expect access type in instantiation of &", Actual, Gen_T);
11320 Abandon_Instantiation (Actual);
11321 end if;
11322
11323 -- According to AI05-288, actuals for access_to_subprograms must be
11324 -- subtype conformant with the generic formal. Previous to AI05-288
11325 -- only mode conformance was required.
11326
11327 -- This is a binding interpretation that applies to previous versions
11328 -- of the language, no need to maintain previous weaker checks.
11329
11330 Check_Subtype_Conformant
11331 (Designated_Type (Act_T),
11332 Designated_Type (A_Gen_T),
11333 Actual,
11334 Get_Inst => True);
11335
11336 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
11337 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
11338 Error_Msg_NE
11339 ("protected access type not allowed for formal &",
11340 Actual, Gen_T);
11341 end if;
11342
11343 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
11344 Error_Msg_NE
11345 ("expect protected access type for formal &",
11346 Actual, Gen_T);
11347 end if;
11348
11349 -- If the formal has a specified convention (which in most cases
11350 -- will be StdCall) verify that the actual has the same convention.
11351
11352 if Has_Convention_Pragma (A_Gen_T)
11353 and then Convention (A_Gen_T) /= Convention (Act_T)
11354 then
11355 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
11356 Error_Msg_NE
11357 ("actual for formal & must have convention %", Actual, Gen_T);
11358 end if;
11359 end Validate_Access_Subprogram_Instance;
11360
11361 -----------------------------------
11362 -- Validate_Access_Type_Instance --
11363 -----------------------------------
11364
11365 procedure Validate_Access_Type_Instance is
11366 Desig_Type : constant Entity_Id :=
11367 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
11368 Desig_Act : Entity_Id;
11369
11370 begin
11371 if not Is_Access_Type (Act_T) then
11372 Error_Msg_NE
11373 ("expect access type in instantiation of &", Actual, Gen_T);
11374 Abandon_Instantiation (Actual);
11375 end if;
11376
11377 if Is_Access_Constant (A_Gen_T) then
11378 if not Is_Access_Constant (Act_T) then
11379 Error_Msg_N
11380 ("actual type must be access-to-constant type", Actual);
11381 Abandon_Instantiation (Actual);
11382 end if;
11383 else
11384 if Is_Access_Constant (Act_T) then
11385 Error_Msg_N
11386 ("actual type must be access-to-variable type", Actual);
11387 Abandon_Instantiation (Actual);
11388
11389 elsif Ekind (A_Gen_T) = E_General_Access_Type
11390 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
11391 then
11392 Error_Msg_N -- CODEFIX
11393 ("actual must be general access type!", Actual);
11394 Error_Msg_NE -- CODEFIX
11395 ("add ALL to }!", Actual, Act_T);
11396 Abandon_Instantiation (Actual);
11397 end if;
11398 end if;
11399
11400 -- The designated subtypes, that is to say the subtypes introduced
11401 -- by an access type declaration (and not by a subtype declaration)
11402 -- must match.
11403
11404 Desig_Act := Designated_Type (Base_Type (Act_T));
11405
11406 -- The designated type may have been introduced through a limited_
11407 -- with clause, in which case retrieve the non-limited view. This
11408 -- applies to incomplete types as well as to class-wide types.
11409
11410 if From_Limited_With (Desig_Act) then
11411 Desig_Act := Available_View (Desig_Act);
11412 end if;
11413
11414 if not Subtypes_Match (Desig_Type, Desig_Act) then
11415 Error_Msg_NE
11416 ("designated type of actual does not match that of formal &",
11417 Actual, Gen_T);
11418
11419 if not Predicates_Match (Desig_Type, Desig_Act) then
11420 Error_Msg_N ("\predicates do not match", Actual);
11421 end if;
11422
11423 Abandon_Instantiation (Actual);
11424
11425 elsif Is_Access_Type (Designated_Type (Act_T))
11426 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11427 /=
11428 Is_Constrained (Designated_Type (Desig_Type))
11429 then
11430 Error_Msg_NE
11431 ("designated type of actual does not match that of formal &",
11432 Actual, Gen_T);
11433
11434 if not Predicates_Match (Desig_Type, Desig_Act) then
11435 Error_Msg_N ("\predicates do not match", Actual);
11436 end if;
11437
11438 Abandon_Instantiation (Actual);
11439 end if;
11440
11441 -- Ada 2005: null-exclusion indicators of the two types must agree
11442
11443 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11444 Error_Msg_NE
11445 ("non null exclusion of actual and formal & do not match",
11446 Actual, Gen_T);
11447 end if;
11448 end Validate_Access_Type_Instance;
11449
11450 ----------------------------------
11451 -- Validate_Array_Type_Instance --
11452 ----------------------------------
11453
11454 procedure Validate_Array_Type_Instance is
11455 I1 : Node_Id;
11456 I2 : Node_Id;
11457 T2 : Entity_Id;
11458
11459 function Formal_Dimensions return Int;
11460 -- Count number of dimensions in array type formal
11461
11462 -----------------------
11463 -- Formal_Dimensions --
11464 -----------------------
11465
11466 function Formal_Dimensions return Int is
11467 Num : Int := 0;
11468 Index : Node_Id;
11469
11470 begin
11471 if Nkind (Def) = N_Constrained_Array_Definition then
11472 Index := First (Discrete_Subtype_Definitions (Def));
11473 else
11474 Index := First (Subtype_Marks (Def));
11475 end if;
11476
11477 while Present (Index) loop
11478 Num := Num + 1;
11479 Next_Index (Index);
11480 end loop;
11481
11482 return Num;
11483 end Formal_Dimensions;
11484
11485 -- Start of processing for Validate_Array_Type_Instance
11486
11487 begin
11488 if not Is_Array_Type (Act_T) then
11489 Error_Msg_NE
11490 ("expect array type in instantiation of &", Actual, Gen_T);
11491 Abandon_Instantiation (Actual);
11492
11493 elsif Nkind (Def) = N_Constrained_Array_Definition then
11494 if not (Is_Constrained (Act_T)) then
11495 Error_Msg_NE
11496 ("expect constrained array in instantiation of &",
11497 Actual, Gen_T);
11498 Abandon_Instantiation (Actual);
11499 end if;
11500
11501 else
11502 if Is_Constrained (Act_T) then
11503 Error_Msg_NE
11504 ("expect unconstrained array in instantiation of &",
11505 Actual, Gen_T);
11506 Abandon_Instantiation (Actual);
11507 end if;
11508 end if;
11509
11510 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11511 Error_Msg_NE
11512 ("dimensions of actual do not match formal &", Actual, Gen_T);
11513 Abandon_Instantiation (Actual);
11514 end if;
11515
11516 I1 := First_Index (A_Gen_T);
11517 I2 := First_Index (Act_T);
11518 for J in 1 .. Formal_Dimensions loop
11519
11520 -- If the indexes of the actual were given by a subtype_mark,
11521 -- the index was transformed into a range attribute. Retrieve
11522 -- the original type mark for checking.
11523
11524 if Is_Entity_Name (Original_Node (I2)) then
11525 T2 := Entity (Original_Node (I2));
11526 else
11527 T2 := Etype (I2);
11528 end if;
11529
11530 if not Subtypes_Match
11531 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11532 then
11533 Error_Msg_NE
11534 ("index types of actual do not match those of formal &",
11535 Actual, Gen_T);
11536 Abandon_Instantiation (Actual);
11537 end if;
11538
11539 Next_Index (I1);
11540 Next_Index (I2);
11541 end loop;
11542
11543 -- Check matching subtypes. Note that there are complex visibility
11544 -- issues when the generic is a child unit and some aspect of the
11545 -- generic type is declared in a parent unit of the generic. We do
11546 -- the test to handle this special case only after a direct check
11547 -- for static matching has failed. The case where both the component
11548 -- type and the array type are separate formals, and the component
11549 -- type is a private view may also require special checking in
11550 -- Subtypes_Match.
11551
11552 if Subtypes_Match
11553 (Component_Type (A_Gen_T), Component_Type (Act_T))
11554 or else
11555 Subtypes_Match
11556 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11557 Component_Type (Act_T))
11558 then
11559 null;
11560 else
11561 Error_Msg_NE
11562 ("component subtype of actual does not match that of formal &",
11563 Actual, Gen_T);
11564 Abandon_Instantiation (Actual);
11565 end if;
11566
11567 if Has_Aliased_Components (A_Gen_T)
11568 and then not Has_Aliased_Components (Act_T)
11569 then
11570 Error_Msg_NE
11571 ("actual must have aliased components to match formal type &",
11572 Actual, Gen_T);
11573 end if;
11574 end Validate_Array_Type_Instance;
11575
11576 -----------------------------------------------
11577 -- Validate_Derived_Interface_Type_Instance --
11578 -----------------------------------------------
11579
11580 procedure Validate_Derived_Interface_Type_Instance is
11581 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11582 Elmt : Elmt_Id;
11583
11584 begin
11585 -- First apply interface instance checks
11586
11587 Validate_Interface_Type_Instance;
11588
11589 -- Verify that immediate parent interface is an ancestor of
11590 -- the actual.
11591
11592 if Present (Par)
11593 and then not Interface_Present_In_Ancestor (Act_T, Par)
11594 then
11595 Error_Msg_NE
11596 ("interface actual must include progenitor&", Actual, Par);
11597 end if;
11598
11599 -- Now verify that the actual includes all other ancestors of
11600 -- the formal.
11601
11602 Elmt := First_Elmt (Interfaces (A_Gen_T));
11603 while Present (Elmt) loop
11604 if not Interface_Present_In_Ancestor
11605 (Act_T, Get_Instance_Of (Node (Elmt)))
11606 then
11607 Error_Msg_NE
11608 ("interface actual must include progenitor&",
11609 Actual, Node (Elmt));
11610 end if;
11611
11612 Next_Elmt (Elmt);
11613 end loop;
11614 end Validate_Derived_Interface_Type_Instance;
11615
11616 ------------------------------------
11617 -- Validate_Derived_Type_Instance --
11618 ------------------------------------
11619
11620 procedure Validate_Derived_Type_Instance is
11621 Actual_Discr : Entity_Id;
11622 Ancestor_Discr : Entity_Id;
11623
11624 begin
11625 -- If the parent type in the generic declaration is itself a previous
11626 -- formal type, then it is local to the generic and absent from the
11627 -- analyzed generic definition. In that case the ancestor is the
11628 -- instance of the formal (which must have been instantiated
11629 -- previously), unless the ancestor is itself a formal derived type.
11630 -- In this latter case (which is the subject of Corrigendum 8652/0038
11631 -- (AI-202) the ancestor of the formals is the ancestor of its
11632 -- parent. Otherwise, the analyzed generic carries the parent type.
11633 -- If the parent type is defined in a previous formal package, then
11634 -- the scope of that formal package is that of the generic type
11635 -- itself, and it has already been mapped into the corresponding type
11636 -- in the actual package.
11637
11638 -- Common case: parent type defined outside of the generic
11639
11640 if Is_Entity_Name (Subtype_Mark (Def))
11641 and then Present (Entity (Subtype_Mark (Def)))
11642 then
11643 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
11644
11645 -- Check whether parent is defined in a previous formal package
11646
11647 elsif
11648 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
11649 then
11650 Ancestor :=
11651 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
11652
11653 -- The type may be a local derivation, or a type extension of a
11654 -- previous formal, or of a formal of a parent package.
11655
11656 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
11657 or else
11658 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
11659 then
11660 -- Check whether the parent is another derived formal type in the
11661 -- same generic unit.
11662
11663 if Etype (A_Gen_T) /= A_Gen_T
11664 and then Is_Generic_Type (Etype (A_Gen_T))
11665 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
11666 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
11667 then
11668 -- Locate ancestor of parent from the subtype declaration
11669 -- created for the actual.
11670
11671 declare
11672 Decl : Node_Id;
11673
11674 begin
11675 Decl := First (Actual_Decls);
11676 while Present (Decl) loop
11677 if Nkind (Decl) = N_Subtype_Declaration
11678 and then Chars (Defining_Identifier (Decl)) =
11679 Chars (Etype (A_Gen_T))
11680 then
11681 Ancestor := Generic_Parent_Type (Decl);
11682 exit;
11683 else
11684 Next (Decl);
11685 end if;
11686 end loop;
11687 end;
11688
11689 pragma Assert (Present (Ancestor));
11690
11691 -- The ancestor itself may be a previous formal that has been
11692 -- instantiated.
11693
11694 Ancestor := Get_Instance_Of (Ancestor);
11695
11696 else
11697 Ancestor :=
11698 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
11699 end if;
11700
11701 -- An unusual case: the actual is a type declared in a parent unit,
11702 -- but is not a formal type so there is no instance_of for it.
11703 -- Retrieve it by analyzing the record extension.
11704
11705 elsif Is_Child_Unit (Scope (A_Gen_T))
11706 and then In_Open_Scopes (Scope (Act_T))
11707 and then Is_Generic_Instance (Scope (Act_T))
11708 then
11709 Analyze (Subtype_Mark (Def));
11710 Ancestor := Entity (Subtype_Mark (Def));
11711
11712 else
11713 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
11714 end if;
11715
11716 -- If the formal derived type has pragma Preelaborable_Initialization
11717 -- then the actual type must have preelaborable initialization.
11718
11719 if Known_To_Have_Preelab_Init (A_Gen_T)
11720 and then not Has_Preelaborable_Initialization (Act_T)
11721 then
11722 Error_Msg_NE
11723 ("actual for & must have preelaborable initialization",
11724 Actual, Gen_T);
11725 end if;
11726
11727 -- Ada 2005 (AI-251)
11728
11729 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
11730 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
11731 Error_Msg_NE
11732 ("(Ada 2005) expected type implementing & in instantiation",
11733 Actual, Ancestor);
11734 end if;
11735
11736 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
11737 Error_Msg_NE
11738 ("expect type derived from & in instantiation",
11739 Actual, First_Subtype (Ancestor));
11740 Abandon_Instantiation (Actual);
11741 end if;
11742
11743 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
11744 -- that the formal type declaration has been rewritten as a private
11745 -- extension.
11746
11747 if Ada_Version >= Ada_2005
11748 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
11749 and then Synchronized_Present (Parent (A_Gen_T))
11750 then
11751 -- The actual must be a synchronized tagged type
11752
11753 if not Is_Tagged_Type (Act_T) then
11754 Error_Msg_N
11755 ("actual of synchronized type must be tagged", Actual);
11756 Abandon_Instantiation (Actual);
11757
11758 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
11759 and then Nkind (Type_Definition (Parent (Act_T))) =
11760 N_Derived_Type_Definition
11761 and then not Synchronized_Present
11762 (Type_Definition (Parent (Act_T)))
11763 then
11764 Error_Msg_N
11765 ("actual of synchronized type must be synchronized", Actual);
11766 Abandon_Instantiation (Actual);
11767 end if;
11768 end if;
11769
11770 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
11771 -- removes the second instance of the phrase "or allow pass by copy".
11772
11773 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
11774 Error_Msg_N
11775 ("cannot have atomic actual type for non-atomic formal type",
11776 Actual);
11777
11778 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
11779 Error_Msg_N
11780 ("cannot have volatile actual type for non-volatile formal type",
11781 Actual);
11782 end if;
11783
11784 -- It should not be necessary to check for unknown discriminants on
11785 -- Formal, but for some reason Has_Unknown_Discriminants is false for
11786 -- A_Gen_T, so Is_Indefinite_Subtype incorrectly returns False. This
11787 -- needs fixing. ???
11788
11789 if not Is_Indefinite_Subtype (A_Gen_T)
11790 and then not Unknown_Discriminants_Present (Formal)
11791 and then Is_Indefinite_Subtype (Act_T)
11792 then
11793 Error_Msg_N ("actual subtype must be constrained", Actual);
11794 Abandon_Instantiation (Actual);
11795 end if;
11796
11797 if not Unknown_Discriminants_Present (Formal) then
11798 if Is_Constrained (Ancestor) then
11799 if not Is_Constrained (Act_T) then
11800 Error_Msg_N ("actual subtype must be constrained", Actual);
11801 Abandon_Instantiation (Actual);
11802 end if;
11803
11804 -- Ancestor is unconstrained, Check if generic formal and actual
11805 -- agree on constrainedness. The check only applies to array types
11806 -- and discriminated types.
11807
11808 elsif Is_Constrained (Act_T) then
11809 if Ekind (Ancestor) = E_Access_Type
11810 or else (not Is_Constrained (A_Gen_T)
11811 and then Is_Composite_Type (A_Gen_T))
11812 then
11813 Error_Msg_N ("actual subtype must be unconstrained", Actual);
11814 Abandon_Instantiation (Actual);
11815 end if;
11816
11817 -- A class-wide type is only allowed if the formal has unknown
11818 -- discriminants.
11819
11820 elsif Is_Class_Wide_Type (Act_T)
11821 and then not Has_Unknown_Discriminants (Ancestor)
11822 then
11823 Error_Msg_NE
11824 ("actual for & cannot be a class-wide type", Actual, Gen_T);
11825 Abandon_Instantiation (Actual);
11826
11827 -- Otherwise, the formal and actual must have the same number
11828 -- of discriminants and each discriminant of the actual must
11829 -- correspond to a discriminant of the formal.
11830
11831 elsif Has_Discriminants (Act_T)
11832 and then not Has_Unknown_Discriminants (Act_T)
11833 and then Has_Discriminants (Ancestor)
11834 then
11835 Actual_Discr := First_Discriminant (Act_T);
11836 Ancestor_Discr := First_Discriminant (Ancestor);
11837 while Present (Actual_Discr)
11838 and then Present (Ancestor_Discr)
11839 loop
11840 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
11841 No (Corresponding_Discriminant (Actual_Discr))
11842 then
11843 Error_Msg_NE
11844 ("discriminant & does not correspond "
11845 & "to ancestor discriminant", Actual, Actual_Discr);
11846 Abandon_Instantiation (Actual);
11847 end if;
11848
11849 Next_Discriminant (Actual_Discr);
11850 Next_Discriminant (Ancestor_Discr);
11851 end loop;
11852
11853 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
11854 Error_Msg_NE
11855 ("actual for & must have same number of discriminants",
11856 Actual, Gen_T);
11857 Abandon_Instantiation (Actual);
11858 end if;
11859
11860 -- This case should be caught by the earlier check for
11861 -- constrainedness, but the check here is added for completeness.
11862
11863 elsif Has_Discriminants (Act_T)
11864 and then not Has_Unknown_Discriminants (Act_T)
11865 then
11866 Error_Msg_NE
11867 ("actual for & must not have discriminants", Actual, Gen_T);
11868 Abandon_Instantiation (Actual);
11869
11870 elsif Has_Discriminants (Ancestor) then
11871 Error_Msg_NE
11872 ("actual for & must have known discriminants", Actual, Gen_T);
11873 Abandon_Instantiation (Actual);
11874 end if;
11875
11876 if not Subtypes_Statically_Compatible
11877 (Act_T, Ancestor, Formal_Derived_Matching => True)
11878 then
11879 Error_Msg_N
11880 ("constraint on actual is incompatible with formal", Actual);
11881 Abandon_Instantiation (Actual);
11882 end if;
11883 end if;
11884
11885 -- If the formal and actual types are abstract, check that there
11886 -- are no abstract primitives of the actual type that correspond to
11887 -- nonabstract primitives of the formal type (second sentence of
11888 -- RM95-3.9.3(9)).
11889
11890 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
11891 Check_Abstract_Primitives : declare
11892 Gen_Prims : constant Elist_Id :=
11893 Primitive_Operations (A_Gen_T);
11894 Gen_Elmt : Elmt_Id;
11895 Gen_Subp : Entity_Id;
11896 Anc_Subp : Entity_Id;
11897 Anc_Formal : Entity_Id;
11898 Anc_F_Type : Entity_Id;
11899
11900 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
11901 Act_Elmt : Elmt_Id;
11902 Act_Subp : Entity_Id;
11903 Act_Formal : Entity_Id;
11904 Act_F_Type : Entity_Id;
11905
11906 Subprograms_Correspond : Boolean;
11907
11908 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
11909 -- Returns true if T2 is derived directly or indirectly from
11910 -- T1, including derivations from interfaces. T1 and T2 are
11911 -- required to be specific tagged base types.
11912
11913 ------------------------
11914 -- Is_Tagged_Ancestor --
11915 ------------------------
11916
11917 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
11918 is
11919 Intfc_Elmt : Elmt_Id;
11920
11921 begin
11922 -- The predicate is satisfied if the types are the same
11923
11924 if T1 = T2 then
11925 return True;
11926
11927 -- If we've reached the top of the derivation chain then
11928 -- we know that T1 is not an ancestor of T2.
11929
11930 elsif Etype (T2) = T2 then
11931 return False;
11932
11933 -- Proceed to check T2's immediate parent
11934
11935 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
11936 return True;
11937
11938 -- Finally, check to see if T1 is an ancestor of any of T2's
11939 -- progenitors.
11940
11941 else
11942 Intfc_Elmt := First_Elmt (Interfaces (T2));
11943 while Present (Intfc_Elmt) loop
11944 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
11945 return True;
11946 end if;
11947
11948 Next_Elmt (Intfc_Elmt);
11949 end loop;
11950 end if;
11951
11952 return False;
11953 end Is_Tagged_Ancestor;
11954
11955 -- Start of processing for Check_Abstract_Primitives
11956
11957 begin
11958 -- Loop over all of the formal derived type's primitives
11959
11960 Gen_Elmt := First_Elmt (Gen_Prims);
11961 while Present (Gen_Elmt) loop
11962 Gen_Subp := Node (Gen_Elmt);
11963
11964 -- If the primitive of the formal is not abstract, then
11965 -- determine whether there is a corresponding primitive of
11966 -- the actual type that's abstract.
11967
11968 if not Is_Abstract_Subprogram (Gen_Subp) then
11969 Act_Elmt := First_Elmt (Act_Prims);
11970 while Present (Act_Elmt) loop
11971 Act_Subp := Node (Act_Elmt);
11972
11973 -- If we find an abstract primitive of the actual,
11974 -- then we need to test whether it corresponds to the
11975 -- subprogram from which the generic formal primitive
11976 -- is inherited.
11977
11978 if Is_Abstract_Subprogram (Act_Subp) then
11979 Anc_Subp := Alias (Gen_Subp);
11980
11981 -- Test whether we have a corresponding primitive
11982 -- by comparing names, kinds, formal types, and
11983 -- result types.
11984
11985 if Chars (Anc_Subp) = Chars (Act_Subp)
11986 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
11987 then
11988 Anc_Formal := First_Formal (Anc_Subp);
11989 Act_Formal := First_Formal (Act_Subp);
11990 while Present (Anc_Formal)
11991 and then Present (Act_Formal)
11992 loop
11993 Anc_F_Type := Etype (Anc_Formal);
11994 Act_F_Type := Etype (Act_Formal);
11995
11996 if Ekind (Anc_F_Type) =
11997 E_Anonymous_Access_Type
11998 then
11999 Anc_F_Type := Designated_Type (Anc_F_Type);
12000
12001 if Ekind (Act_F_Type) =
12002 E_Anonymous_Access_Type
12003 then
12004 Act_F_Type :=
12005 Designated_Type (Act_F_Type);
12006 else
12007 exit;
12008 end if;
12009
12010 elsif
12011 Ekind (Act_F_Type) = E_Anonymous_Access_Type
12012 then
12013 exit;
12014 end if;
12015
12016 Anc_F_Type := Base_Type (Anc_F_Type);
12017 Act_F_Type := Base_Type (Act_F_Type);
12018
12019 -- If the formal is controlling, then the
12020 -- the type of the actual primitive's formal
12021 -- must be derived directly or indirectly
12022 -- from the type of the ancestor primitive's
12023 -- formal.
12024
12025 if Is_Controlling_Formal (Anc_Formal) then
12026 if not Is_Tagged_Ancestor
12027 (Anc_F_Type, Act_F_Type)
12028 then
12029 exit;
12030 end if;
12031
12032 -- Otherwise the types of the formals must
12033 -- be the same.
12034
12035 elsif Anc_F_Type /= Act_F_Type then
12036 exit;
12037 end if;
12038
12039 Next_Entity (Anc_Formal);
12040 Next_Entity (Act_Formal);
12041 end loop;
12042
12043 -- If we traversed through all of the formals
12044 -- then so far the subprograms correspond, so
12045 -- now check that any result types correspond.
12046
12047 if No (Anc_Formal) and then No (Act_Formal) then
12048 Subprograms_Correspond := True;
12049
12050 if Ekind (Act_Subp) = E_Function then
12051 Anc_F_Type := Etype (Anc_Subp);
12052 Act_F_Type := Etype (Act_Subp);
12053
12054 if Ekind (Anc_F_Type) =
12055 E_Anonymous_Access_Type
12056 then
12057 Anc_F_Type :=
12058 Designated_Type (Anc_F_Type);
12059
12060 if Ekind (Act_F_Type) =
12061 E_Anonymous_Access_Type
12062 then
12063 Act_F_Type :=
12064 Designated_Type (Act_F_Type);
12065 else
12066 Subprograms_Correspond := False;
12067 end if;
12068
12069 elsif
12070 Ekind (Act_F_Type)
12071 = E_Anonymous_Access_Type
12072 then
12073 Subprograms_Correspond := False;
12074 end if;
12075
12076 Anc_F_Type := Base_Type (Anc_F_Type);
12077 Act_F_Type := Base_Type (Act_F_Type);
12078
12079 -- Now either the result types must be
12080 -- the same or, if the result type is
12081 -- controlling, the result type of the
12082 -- actual primitive must descend from the
12083 -- result type of the ancestor primitive.
12084
12085 if Subprograms_Correspond
12086 and then Anc_F_Type /= Act_F_Type
12087 and then
12088 Has_Controlling_Result (Anc_Subp)
12089 and then not Is_Tagged_Ancestor
12090 (Anc_F_Type, Act_F_Type)
12091 then
12092 Subprograms_Correspond := False;
12093 end if;
12094 end if;
12095
12096 -- Found a matching subprogram belonging to
12097 -- formal ancestor type, so actual subprogram
12098 -- corresponds and this violates 3.9.3(9).
12099
12100 if Subprograms_Correspond then
12101 Error_Msg_NE
12102 ("abstract subprogram & overrides "
12103 & "nonabstract subprogram of ancestor",
12104 Actual, Act_Subp);
12105 end if;
12106 end if;
12107 end if;
12108 end if;
12109
12110 Next_Elmt (Act_Elmt);
12111 end loop;
12112 end if;
12113
12114 Next_Elmt (Gen_Elmt);
12115 end loop;
12116 end Check_Abstract_Primitives;
12117 end if;
12118
12119 -- Verify that limitedness matches. If parent is a limited
12120 -- interface then the generic formal is not unless declared
12121 -- explicitly so. If not declared limited, the actual cannot be
12122 -- limited (see AI05-0087).
12123
12124 -- Even though this AI is a binding interpretation, we enable the
12125 -- check only in Ada 2012 mode, because this improper construct
12126 -- shows up in user code and in existing B-tests.
12127
12128 if Is_Limited_Type (Act_T)
12129 and then not Is_Limited_Type (A_Gen_T)
12130 and then Ada_Version >= Ada_2012
12131 then
12132 if In_Instance then
12133 null;
12134 else
12135 Error_Msg_NE
12136 ("actual for non-limited & cannot be a limited type",
12137 Actual, Gen_T);
12138 Explain_Limited_Type (Act_T, Actual);
12139 Abandon_Instantiation (Actual);
12140 end if;
12141 end if;
12142 end Validate_Derived_Type_Instance;
12143
12144 ----------------------------------------
12145 -- Validate_Discriminated_Formal_Type --
12146 ----------------------------------------
12147
12148 procedure Validate_Discriminated_Formal_Type is
12149 Formal_Discr : Entity_Id;
12150 Actual_Discr : Entity_Id;
12151 Formal_Subt : Entity_Id;
12152
12153 begin
12154 if Has_Discriminants (A_Gen_T) then
12155 if not Has_Discriminants (Act_T) then
12156 Error_Msg_NE
12157 ("actual for & must have discriminants", Actual, Gen_T);
12158 Abandon_Instantiation (Actual);
12159
12160 elsif Is_Constrained (Act_T) then
12161 Error_Msg_NE
12162 ("actual for & must be unconstrained", Actual, Gen_T);
12163 Abandon_Instantiation (Actual);
12164
12165 else
12166 Formal_Discr := First_Discriminant (A_Gen_T);
12167 Actual_Discr := First_Discriminant (Act_T);
12168 while Formal_Discr /= Empty loop
12169 if Actual_Discr = Empty then
12170 Error_Msg_NE
12171 ("discriminants on actual do not match formal",
12172 Actual, Gen_T);
12173 Abandon_Instantiation (Actual);
12174 end if;
12175
12176 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
12177
12178 -- Access discriminants match if designated types do
12179
12180 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
12181 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
12182 E_Anonymous_Access_Type
12183 and then
12184 Get_Instance_Of
12185 (Designated_Type (Base_Type (Formal_Subt))) =
12186 Designated_Type (Base_Type (Etype (Actual_Discr)))
12187 then
12188 null;
12189
12190 elsif Base_Type (Formal_Subt) /=
12191 Base_Type (Etype (Actual_Discr))
12192 then
12193 Error_Msg_NE
12194 ("types of actual discriminants must match formal",
12195 Actual, Gen_T);
12196 Abandon_Instantiation (Actual);
12197
12198 elsif not Subtypes_Statically_Match
12199 (Formal_Subt, Etype (Actual_Discr))
12200 and then Ada_Version >= Ada_95
12201 then
12202 Error_Msg_NE
12203 ("subtypes of actual discriminants must match formal",
12204 Actual, Gen_T);
12205 Abandon_Instantiation (Actual);
12206 end if;
12207
12208 Next_Discriminant (Formal_Discr);
12209 Next_Discriminant (Actual_Discr);
12210 end loop;
12211
12212 if Actual_Discr /= Empty then
12213 Error_Msg_NE
12214 ("discriminants on actual do not match formal",
12215 Actual, Gen_T);
12216 Abandon_Instantiation (Actual);
12217 end if;
12218 end if;
12219 end if;
12220 end Validate_Discriminated_Formal_Type;
12221
12222 ---------------------------------------
12223 -- Validate_Incomplete_Type_Instance --
12224 ---------------------------------------
12225
12226 procedure Validate_Incomplete_Type_Instance is
12227 begin
12228 if not Is_Tagged_Type (Act_T)
12229 and then Is_Tagged_Type (A_Gen_T)
12230 then
12231 Error_Msg_NE
12232 ("actual for & must be a tagged type", Actual, Gen_T);
12233 end if;
12234
12235 Validate_Discriminated_Formal_Type;
12236 end Validate_Incomplete_Type_Instance;
12237
12238 --------------------------------------
12239 -- Validate_Interface_Type_Instance --
12240 --------------------------------------
12241
12242 procedure Validate_Interface_Type_Instance is
12243 begin
12244 if not Is_Interface (Act_T) then
12245 Error_Msg_NE
12246 ("actual for formal interface type must be an interface",
12247 Actual, Gen_T);
12248
12249 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
12250 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
12251 or else Is_Protected_Interface (A_Gen_T) /=
12252 Is_Protected_Interface (Act_T)
12253 or else Is_Synchronized_Interface (A_Gen_T) /=
12254 Is_Synchronized_Interface (Act_T)
12255 then
12256 Error_Msg_NE
12257 ("actual for interface& does not match (RM 12.5.5(4))",
12258 Actual, Gen_T);
12259 end if;
12260 end Validate_Interface_Type_Instance;
12261
12262 ------------------------------------
12263 -- Validate_Private_Type_Instance --
12264 ------------------------------------
12265
12266 procedure Validate_Private_Type_Instance is
12267 begin
12268 if Is_Limited_Type (Act_T)
12269 and then not Is_Limited_Type (A_Gen_T)
12270 then
12271 if In_Instance then
12272 null;
12273 else
12274 Error_Msg_NE
12275 ("actual for non-limited & cannot be a limited type", Actual,
12276 Gen_T);
12277 Explain_Limited_Type (Act_T, Actual);
12278 Abandon_Instantiation (Actual);
12279 end if;
12280
12281 elsif Known_To_Have_Preelab_Init (A_Gen_T)
12282 and then not Has_Preelaborable_Initialization (Act_T)
12283 then
12284 Error_Msg_NE
12285 ("actual for & must have preelaborable initialization", Actual,
12286 Gen_T);
12287
12288 elsif Is_Indefinite_Subtype (Act_T)
12289 and then not Is_Indefinite_Subtype (A_Gen_T)
12290 and then Ada_Version >= Ada_95
12291 then
12292 Error_Msg_NE
12293 ("actual for & must be a definite subtype", Actual, Gen_T);
12294
12295 elsif not Is_Tagged_Type (Act_T)
12296 and then Is_Tagged_Type (A_Gen_T)
12297 then
12298 Error_Msg_NE
12299 ("actual for & must be a tagged type", Actual, Gen_T);
12300 end if;
12301
12302 Validate_Discriminated_Formal_Type;
12303 Ancestor := Gen_T;
12304 end Validate_Private_Type_Instance;
12305
12306 -- Start of processing for Instantiate_Type
12307
12308 begin
12309 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
12310 Error_Msg_N ("duplicate instantiation of generic type", Actual);
12311 return New_List (Error);
12312
12313 elsif not Is_Entity_Name (Actual)
12314 or else not Is_Type (Entity (Actual))
12315 then
12316 Error_Msg_NE
12317 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
12318 Abandon_Instantiation (Actual);
12319
12320 else
12321 Act_T := Entity (Actual);
12322
12323 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
12324 -- as a generic actual parameter if the corresponding formal type
12325 -- does not have a known_discriminant_part, or is a formal derived
12326 -- type that is an Unchecked_Union type.
12327
12328 if Is_Unchecked_Union (Base_Type (Act_T)) then
12329 if not Has_Discriminants (A_Gen_T)
12330 or else (Is_Derived_Type (A_Gen_T)
12331 and then Is_Unchecked_Union (A_Gen_T))
12332 then
12333 null;
12334 else
12335 Error_Msg_N ("unchecked union cannot be the actual for a "
12336 & "discriminated formal type", Act_T);
12337
12338 end if;
12339 end if;
12340
12341 -- Deal with fixed/floating restrictions
12342
12343 if Is_Floating_Point_Type (Act_T) then
12344 Check_Restriction (No_Floating_Point, Actual);
12345 elsif Is_Fixed_Point_Type (Act_T) then
12346 Check_Restriction (No_Fixed_Point, Actual);
12347 end if;
12348
12349 -- Deal with error of using incomplete type as generic actual.
12350 -- This includes limited views of a type, even if the non-limited
12351 -- view may be available.
12352
12353 if Ekind (Act_T) = E_Incomplete_Type
12354 or else (Is_Class_Wide_Type (Act_T)
12355 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
12356 then
12357 -- If the formal is an incomplete type, the actual can be
12358 -- incomplete as well.
12359
12360 if Ekind (A_Gen_T) = E_Incomplete_Type then
12361 null;
12362
12363 elsif Is_Class_Wide_Type (Act_T)
12364 or else No (Full_View (Act_T))
12365 then
12366 Error_Msg_N ("premature use of incomplete type", Actual);
12367 Abandon_Instantiation (Actual);
12368 else
12369 Act_T := Full_View (Act_T);
12370 Set_Entity (Actual, Act_T);
12371
12372 if Has_Private_Component (Act_T) then
12373 Error_Msg_N
12374 ("premature use of type with private component", Actual);
12375 end if;
12376 end if;
12377
12378 -- Deal with error of premature use of private type as generic actual
12379
12380 elsif Is_Private_Type (Act_T)
12381 and then Is_Private_Type (Base_Type (Act_T))
12382 and then not Is_Generic_Type (Act_T)
12383 and then not Is_Derived_Type (Act_T)
12384 and then No (Full_View (Root_Type (Act_T)))
12385 then
12386 -- If the formal is an incomplete type, the actual can be
12387 -- private or incomplete as well.
12388
12389 if Ekind (A_Gen_T) = E_Incomplete_Type then
12390 null;
12391 else
12392 Error_Msg_N ("premature use of private type", Actual);
12393 end if;
12394
12395 elsif Has_Private_Component (Act_T) then
12396 Error_Msg_N
12397 ("premature use of type with private component", Actual);
12398 end if;
12399
12400 Set_Instance_Of (A_Gen_T, Act_T);
12401
12402 -- If the type is generic, the class-wide type may also be used
12403
12404 if Is_Tagged_Type (A_Gen_T)
12405 and then Is_Tagged_Type (Act_T)
12406 and then not Is_Class_Wide_Type (A_Gen_T)
12407 then
12408 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
12409 Class_Wide_Type (Act_T));
12410 end if;
12411
12412 if not Is_Abstract_Type (A_Gen_T)
12413 and then Is_Abstract_Type (Act_T)
12414 then
12415 Error_Msg_N
12416 ("actual of non-abstract formal cannot be abstract", Actual);
12417 end if;
12418
12419 -- A generic scalar type is a first subtype for which we generate
12420 -- an anonymous base type. Indicate that the instance of this base
12421 -- is the base type of the actual.
12422
12423 if Is_Scalar_Type (A_Gen_T) then
12424 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12425 end if;
12426 end if;
12427
12428 if Error_Posted (Act_T) then
12429 null;
12430 else
12431 case Nkind (Def) is
12432 when N_Formal_Private_Type_Definition =>
12433 Validate_Private_Type_Instance;
12434
12435 when N_Formal_Incomplete_Type_Definition =>
12436 Validate_Incomplete_Type_Instance;
12437
12438 when N_Formal_Derived_Type_Definition =>
12439 Validate_Derived_Type_Instance;
12440
12441 when N_Formal_Discrete_Type_Definition =>
12442 if not Is_Discrete_Type (Act_T) then
12443 Error_Msg_NE
12444 ("expect discrete type in instantiation of&",
12445 Actual, Gen_T);
12446 Abandon_Instantiation (Actual);
12447 end if;
12448
12449 Diagnose_Predicated_Actual;
12450
12451 when N_Formal_Signed_Integer_Type_Definition =>
12452 if not Is_Signed_Integer_Type (Act_T) then
12453 Error_Msg_NE
12454 ("expect signed integer type in instantiation of&",
12455 Actual, Gen_T);
12456 Abandon_Instantiation (Actual);
12457 end if;
12458
12459 Diagnose_Predicated_Actual;
12460
12461 when N_Formal_Modular_Type_Definition =>
12462 if not Is_Modular_Integer_Type (Act_T) then
12463 Error_Msg_NE
12464 ("expect modular type in instantiation of &",
12465 Actual, Gen_T);
12466 Abandon_Instantiation (Actual);
12467 end if;
12468
12469 Diagnose_Predicated_Actual;
12470
12471 when N_Formal_Floating_Point_Definition =>
12472 if not Is_Floating_Point_Type (Act_T) then
12473 Error_Msg_NE
12474 ("expect float type in instantiation of &", Actual, Gen_T);
12475 Abandon_Instantiation (Actual);
12476 end if;
12477
12478 when N_Formal_Ordinary_Fixed_Point_Definition =>
12479 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12480 Error_Msg_NE
12481 ("expect ordinary fixed point type in instantiation of &",
12482 Actual, Gen_T);
12483 Abandon_Instantiation (Actual);
12484 end if;
12485
12486 when N_Formal_Decimal_Fixed_Point_Definition =>
12487 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12488 Error_Msg_NE
12489 ("expect decimal type in instantiation of &",
12490 Actual, Gen_T);
12491 Abandon_Instantiation (Actual);
12492 end if;
12493
12494 when N_Array_Type_Definition =>
12495 Validate_Array_Type_Instance;
12496
12497 when N_Access_To_Object_Definition =>
12498 Validate_Access_Type_Instance;
12499
12500 when N_Access_Function_Definition |
12501 N_Access_Procedure_Definition =>
12502 Validate_Access_Subprogram_Instance;
12503
12504 when N_Record_Definition =>
12505 Validate_Interface_Type_Instance;
12506
12507 when N_Derived_Type_Definition =>
12508 Validate_Derived_Interface_Type_Instance;
12509
12510 when others =>
12511 raise Program_Error;
12512
12513 end case;
12514 end if;
12515
12516 Subt := New_Copy (Gen_T);
12517
12518 -- Use adjusted sloc of subtype name as the location for other nodes in
12519 -- the subtype declaration.
12520
12521 Loc := Sloc (Subt);
12522
12523 Decl_Node :=
12524 Make_Subtype_Declaration (Loc,
12525 Defining_Identifier => Subt,
12526 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12527
12528 if Is_Private_Type (Act_T) then
12529 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12530
12531 elsif Is_Access_Type (Act_T)
12532 and then Is_Private_Type (Designated_Type (Act_T))
12533 then
12534 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12535 end if;
12536
12537 -- In Ada 2012 the actual may be a limited view. Indicate that
12538 -- the local subtype must be treated as such.
12539
12540 if From_Limited_With (Act_T) then
12541 Set_Ekind (Subt, E_Incomplete_Subtype);
12542 Set_From_Limited_With (Subt);
12543 end if;
12544
12545 Decl_Nodes := New_List (Decl_Node);
12546
12547 -- Flag actual derived types so their elaboration produces the
12548 -- appropriate renamings for the primitive operations of the ancestor.
12549 -- Flag actual for formal private types as well, to determine whether
12550 -- operations in the private part may override inherited operations.
12551 -- If the formal has an interface list, the ancestor is not the
12552 -- parent, but the analyzed formal that includes the interface
12553 -- operations of all its progenitors.
12554
12555 -- Same treatment for formal private types, so we can check whether the
12556 -- type is tagged limited when validating derivations in the private
12557 -- part. (See AI05-096).
12558
12559 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12560 if Present (Interface_List (Def)) then
12561 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12562 else
12563 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12564 end if;
12565
12566 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
12567 N_Formal_Incomplete_Type_Definition)
12568 then
12569 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12570 end if;
12571
12572 -- If the actual is a synchronized type that implements an interface,
12573 -- the primitive operations are attached to the corresponding record,
12574 -- and we have to treat it as an additional generic actual, so that its
12575 -- primitive operations become visible in the instance. The task or
12576 -- protected type itself does not carry primitive operations.
12577
12578 if Is_Concurrent_Type (Act_T)
12579 and then Is_Tagged_Type (Act_T)
12580 and then Present (Corresponding_Record_Type (Act_T))
12581 and then Present (Ancestor)
12582 and then Is_Interface (Ancestor)
12583 then
12584 declare
12585 Corr_Rec : constant Entity_Id :=
12586 Corresponding_Record_Type (Act_T);
12587 New_Corr : Entity_Id;
12588 Corr_Decl : Node_Id;
12589
12590 begin
12591 New_Corr := Make_Temporary (Loc, 'S');
12592 Corr_Decl :=
12593 Make_Subtype_Declaration (Loc,
12594 Defining_Identifier => New_Corr,
12595 Subtype_Indication =>
12596 New_Occurrence_Of (Corr_Rec, Loc));
12597 Append_To (Decl_Nodes, Corr_Decl);
12598
12599 if Ekind (Act_T) = E_Task_Type then
12600 Set_Ekind (Subt, E_Task_Subtype);
12601 else
12602 Set_Ekind (Subt, E_Protected_Subtype);
12603 end if;
12604
12605 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12606 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12607 Set_Generic_Parent_Type (Decl_Node, Empty);
12608 end;
12609 end if;
12610
12611 -- For a floating-point type, capture dimension info if any, because
12612 -- the generated subtype declaration does not come from source and
12613 -- will not process dimensions.
12614
12615 if Is_Floating_Point_Type (Act_T) then
12616 Copy_Dimensions (Act_T, Subt);
12617 end if;
12618
12619 return Decl_Nodes;
12620 end Instantiate_Type;
12621
12622 ---------------------
12623 -- Is_In_Main_Unit --
12624 ---------------------
12625
12626 function Is_In_Main_Unit (N : Node_Id) return Boolean is
12627 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
12628 Current_Unit : Node_Id;
12629
12630 begin
12631 if Unum = Main_Unit then
12632 return True;
12633
12634 -- If the current unit is a subunit then it is either the main unit or
12635 -- is being compiled as part of the main unit.
12636
12637 elsif Nkind (N) = N_Compilation_Unit then
12638 return Nkind (Unit (N)) = N_Subunit;
12639 end if;
12640
12641 Current_Unit := Parent (N);
12642 while Present (Current_Unit)
12643 and then Nkind (Current_Unit) /= N_Compilation_Unit
12644 loop
12645 Current_Unit := Parent (Current_Unit);
12646 end loop;
12647
12648 -- The instantiation node is in the main unit, or else the current node
12649 -- (perhaps as the result of nested instantiations) is in the main unit,
12650 -- or in the declaration of the main unit, which in this last case must
12651 -- be a body.
12652
12653 return Unum = Main_Unit
12654 or else Current_Unit = Cunit (Main_Unit)
12655 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
12656 or else (Present (Library_Unit (Current_Unit))
12657 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
12658 end Is_In_Main_Unit;
12659
12660 ----------------------------
12661 -- Load_Parent_Of_Generic --
12662 ----------------------------
12663
12664 procedure Load_Parent_Of_Generic
12665 (N : Node_Id;
12666 Spec : Node_Id;
12667 Body_Optional : Boolean := False)
12668 is
12669 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
12670 Saved_Style_Check : constant Boolean := Style_Check;
12671 Saved_Warnings : constant Warning_Record := Save_Warnings;
12672 True_Parent : Node_Id;
12673 Inst_Node : Node_Id;
12674 OK : Boolean;
12675 Previous_Instances : constant Elist_Id := New_Elmt_List;
12676
12677 procedure Collect_Previous_Instances (Decls : List_Id);
12678 -- Collect all instantiations in the given list of declarations, that
12679 -- precede the generic that we need to load. If the bodies of these
12680 -- instantiations are available, we must analyze them, to ensure that
12681 -- the public symbols generated are the same when the unit is compiled
12682 -- to generate code, and when it is compiled in the context of a unit
12683 -- that needs a particular nested instance. This process is applied to
12684 -- both package and subprogram instances.
12685
12686 --------------------------------
12687 -- Collect_Previous_Instances --
12688 --------------------------------
12689
12690 procedure Collect_Previous_Instances (Decls : List_Id) is
12691 Decl : Node_Id;
12692
12693 begin
12694 Decl := First (Decls);
12695 while Present (Decl) loop
12696 if Sloc (Decl) >= Sloc (Inst_Node) then
12697 return;
12698
12699 -- If Decl is an instantiation, then record it as requiring
12700 -- instantiation of the corresponding body, except if it is an
12701 -- abbreviated instantiation generated internally for conformance
12702 -- checking purposes only for the case of a formal package
12703 -- declared without a box (see Instantiate_Formal_Package). Such
12704 -- an instantiation does not generate any code (the actual code
12705 -- comes from actual) and thus does not need to be analyzed here.
12706 -- If the instantiation appears with a generic package body it is
12707 -- not analyzed here either.
12708
12709 elsif Nkind (Decl) = N_Package_Instantiation
12710 and then not Is_Internal (Defining_Entity (Decl))
12711 then
12712 Append_Elmt (Decl, Previous_Instances);
12713
12714 -- For a subprogram instantiation, omit instantiations intrinsic
12715 -- operations (Unchecked_Conversions, etc.) that have no bodies.
12716
12717 elsif Nkind_In (Decl, N_Function_Instantiation,
12718 N_Procedure_Instantiation)
12719 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
12720 then
12721 Append_Elmt (Decl, Previous_Instances);
12722
12723 elsif Nkind (Decl) = N_Package_Declaration then
12724 Collect_Previous_Instances
12725 (Visible_Declarations (Specification (Decl)));
12726 Collect_Previous_Instances
12727 (Private_Declarations (Specification (Decl)));
12728
12729 -- Previous non-generic bodies may contain instances as well
12730
12731 elsif Nkind (Decl) = N_Package_Body
12732 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12733 then
12734 Collect_Previous_Instances (Declarations (Decl));
12735
12736 elsif Nkind (Decl) = N_Subprogram_Body
12737 and then not Acts_As_Spec (Decl)
12738 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
12739 then
12740 Collect_Previous_Instances (Declarations (Decl));
12741 end if;
12742
12743 Next (Decl);
12744 end loop;
12745 end Collect_Previous_Instances;
12746
12747 -- Start of processing for Load_Parent_Of_Generic
12748
12749 begin
12750 if not In_Same_Source_Unit (N, Spec)
12751 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
12752 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
12753 and then not Is_In_Main_Unit (Spec))
12754 then
12755 -- Find body of parent of spec, and analyze it. A special case arises
12756 -- when the parent is an instantiation, that is to say when we are
12757 -- currently instantiating a nested generic. In that case, there is
12758 -- no separate file for the body of the enclosing instance. Instead,
12759 -- the enclosing body must be instantiated as if it were a pending
12760 -- instantiation, in order to produce the body for the nested generic
12761 -- we require now. Note that in that case the generic may be defined
12762 -- in a package body, the instance defined in the same package body,
12763 -- and the original enclosing body may not be in the main unit.
12764
12765 Inst_Node := Empty;
12766
12767 True_Parent := Parent (Spec);
12768 while Present (True_Parent)
12769 and then Nkind (True_Parent) /= N_Compilation_Unit
12770 loop
12771 if Nkind (True_Parent) = N_Package_Declaration
12772 and then
12773 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
12774 then
12775 -- Parent is a compilation unit that is an instantiation.
12776 -- Instantiation node has been replaced with package decl.
12777
12778 Inst_Node := Original_Node (True_Parent);
12779 exit;
12780
12781 elsif Nkind (True_Parent) = N_Package_Declaration
12782 and then Present (Generic_Parent (Specification (True_Parent)))
12783 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12784 then
12785 -- Parent is an instantiation within another specification.
12786 -- Declaration for instance has been inserted before original
12787 -- instantiation node. A direct link would be preferable?
12788
12789 Inst_Node := Next (True_Parent);
12790 while Present (Inst_Node)
12791 and then Nkind (Inst_Node) /= N_Package_Instantiation
12792 loop
12793 Next (Inst_Node);
12794 end loop;
12795
12796 -- If the instance appears within a generic, and the generic
12797 -- unit is defined within a formal package of the enclosing
12798 -- generic, there is no generic body available, and none
12799 -- needed. A more precise test should be used ???
12800
12801 if No (Inst_Node) then
12802 return;
12803 end if;
12804
12805 exit;
12806
12807 else
12808 True_Parent := Parent (True_Parent);
12809 end if;
12810 end loop;
12811
12812 -- Case where we are currently instantiating a nested generic
12813
12814 if Present (Inst_Node) then
12815 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
12816
12817 -- Instantiation node and declaration of instantiated package
12818 -- were exchanged when only the declaration was needed.
12819 -- Restore instantiation node before proceeding with body.
12820
12821 Set_Unit (Parent (True_Parent), Inst_Node);
12822 end if;
12823
12824 -- Now complete instantiation of enclosing body, if it appears in
12825 -- some other unit. If it appears in the current unit, the body
12826 -- will have been instantiated already.
12827
12828 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
12829
12830 -- We need to determine the expander mode to instantiate the
12831 -- enclosing body. Because the generic body we need may use
12832 -- global entities declared in the enclosing package (including
12833 -- aggregates) it is in general necessary to compile this body
12834 -- with expansion enabled, except if we are within a generic
12835 -- package, in which case the usual generic rule applies.
12836
12837 declare
12838 Exp_Status : Boolean := True;
12839 Scop : Entity_Id;
12840
12841 begin
12842 -- Loop through scopes looking for generic package
12843
12844 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
12845 while Present (Scop)
12846 and then Scop /= Standard_Standard
12847 loop
12848 if Ekind (Scop) = E_Generic_Package then
12849 Exp_Status := False;
12850 exit;
12851 end if;
12852
12853 Scop := Scope (Scop);
12854 end loop;
12855
12856 -- Collect previous instantiations in the unit that contains
12857 -- the desired generic.
12858
12859 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12860 and then not Body_Optional
12861 then
12862 declare
12863 Decl : Elmt_Id;
12864 Info : Pending_Body_Info;
12865 Par : Node_Id;
12866
12867 begin
12868 Par := Parent (Inst_Node);
12869 while Present (Par) loop
12870 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
12871 Par := Parent (Par);
12872 end loop;
12873
12874 pragma Assert (Present (Par));
12875
12876 if Nkind (Par) = N_Package_Body then
12877 Collect_Previous_Instances (Declarations (Par));
12878
12879 elsif Nkind (Par) = N_Package_Declaration then
12880 Collect_Previous_Instances
12881 (Visible_Declarations (Specification (Par)));
12882 Collect_Previous_Instances
12883 (Private_Declarations (Specification (Par)));
12884
12885 else
12886 -- Enclosing unit is a subprogram body. In this
12887 -- case all instance bodies are processed in order
12888 -- and there is no need to collect them separately.
12889
12890 null;
12891 end if;
12892
12893 Decl := First_Elmt (Previous_Instances);
12894 while Present (Decl) loop
12895 Info :=
12896 (Inst_Node => Node (Decl),
12897 Act_Decl =>
12898 Instance_Spec (Node (Decl)),
12899 Expander_Status => Exp_Status,
12900 Current_Sem_Unit =>
12901 Get_Code_Unit (Sloc (Node (Decl))),
12902 Scope_Suppress => Scope_Suppress,
12903 Local_Suppress_Stack_Top =>
12904 Local_Suppress_Stack_Top,
12905 Version => Ada_Version,
12906 Version_Pragma => Ada_Version_Pragma,
12907 Warnings => Save_Warnings,
12908 SPARK_Mode => SPARK_Mode,
12909 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
12910
12911 -- Package instance
12912
12913 if
12914 Nkind (Node (Decl)) = N_Package_Instantiation
12915 then
12916 Instantiate_Package_Body
12917 (Info, Body_Optional => True);
12918
12919 -- Subprogram instance
12920
12921 else
12922 -- The instance_spec is in the wrapper package,
12923 -- usually followed by its local renaming
12924 -- declaration. See Build_Subprogram_Renaming
12925 -- for details.
12926
12927 declare
12928 Decl : Node_Id :=
12929 (Last (Visible_Declarations
12930 (Specification (Info.Act_Decl))));
12931 begin
12932 if Nkind (Decl) =
12933 N_Subprogram_Renaming_Declaration
12934 then
12935 Decl := Prev (Decl);
12936 end if;
12937
12938 Info.Act_Decl := Decl;
12939 end;
12940
12941 Instantiate_Subprogram_Body
12942 (Info, Body_Optional => True);
12943 end if;
12944
12945 Next_Elmt (Decl);
12946 end loop;
12947 end;
12948 end if;
12949
12950 Instantiate_Package_Body
12951 (Body_Info =>
12952 ((Inst_Node => Inst_Node,
12953 Act_Decl => True_Parent,
12954 Expander_Status => Exp_Status,
12955 Current_Sem_Unit => Get_Code_Unit
12956 (Sloc (Inst_Node)),
12957 Scope_Suppress => Scope_Suppress,
12958 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
12959 Version => Ada_Version,
12960 Version_Pragma => Ada_Version_Pragma,
12961 Warnings => Save_Warnings,
12962 SPARK_Mode => SPARK_Mode,
12963 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
12964 Body_Optional => Body_Optional);
12965 end;
12966 end if;
12967
12968 -- Case where we are not instantiating a nested generic
12969
12970 else
12971 Opt.Style_Check := False;
12972 Expander_Mode_Save_And_Set (True);
12973 Load_Needed_Body (Comp_Unit, OK);
12974 Opt.Style_Check := Saved_Style_Check;
12975 Restore_Warnings (Saved_Warnings);
12976 Expander_Mode_Restore;
12977
12978 if not OK
12979 and then Unit_Requires_Body (Defining_Entity (Spec))
12980 and then not Body_Optional
12981 then
12982 declare
12983 Bname : constant Unit_Name_Type :=
12984 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
12985
12986 begin
12987 -- In CodePeer mode, the missing body may make the analysis
12988 -- incomplete, but we do not treat it as fatal.
12989
12990 if CodePeer_Mode then
12991 return;
12992
12993 else
12994 Error_Msg_Unit_1 := Bname;
12995 Error_Msg_N ("this instantiation requires$!", N);
12996 Error_Msg_File_1 :=
12997 Get_File_Name (Bname, Subunit => False);
12998 Error_Msg_N ("\but file{ was not found!", N);
12999 raise Unrecoverable_Error;
13000 end if;
13001 end;
13002 end if;
13003 end if;
13004 end if;
13005
13006 -- If loading parent of the generic caused an instantiation circularity,
13007 -- we abandon compilation at this point, because otherwise in some cases
13008 -- we get into trouble with infinite recursions after this point.
13009
13010 if Circularity_Detected then
13011 raise Unrecoverable_Error;
13012 end if;
13013 end Load_Parent_Of_Generic;
13014
13015 ---------------------------------
13016 -- Map_Formal_Package_Entities --
13017 ---------------------------------
13018
13019 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
13020 E1 : Entity_Id;
13021 E2 : Entity_Id;
13022
13023 begin
13024 Set_Instance_Of (Form, Act);
13025
13026 -- Traverse formal and actual package to map the corresponding entities.
13027 -- We skip over internal entities that may be generated during semantic
13028 -- analysis, and find the matching entities by name, given that they
13029 -- must appear in the same order.
13030
13031 E1 := First_Entity (Form);
13032 E2 := First_Entity (Act);
13033 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
13034 -- Could this test be a single condition??? Seems like it could, and
13035 -- isn't FPE (Form) a constant anyway???
13036
13037 if not Is_Internal (E1)
13038 and then Present (Parent (E1))
13039 and then not Is_Class_Wide_Type (E1)
13040 and then not Is_Internal_Name (Chars (E1))
13041 then
13042 while Present (E2) and then Chars (E2) /= Chars (E1) loop
13043 Next_Entity (E2);
13044 end loop;
13045
13046 if No (E2) then
13047 exit;
13048 else
13049 Set_Instance_Of (E1, E2);
13050
13051 if Is_Type (E1) and then Is_Tagged_Type (E2) then
13052 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
13053 end if;
13054
13055 if Is_Constrained (E1) then
13056 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
13057 end if;
13058
13059 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
13060 Map_Formal_Package_Entities (E1, E2);
13061 end if;
13062 end if;
13063 end if;
13064
13065 Next_Entity (E1);
13066 end loop;
13067 end Map_Formal_Package_Entities;
13068
13069 -----------------------
13070 -- Move_Freeze_Nodes --
13071 -----------------------
13072
13073 procedure Move_Freeze_Nodes
13074 (Out_Of : Entity_Id;
13075 After : Node_Id;
13076 L : List_Id)
13077 is
13078 Decl : Node_Id;
13079 Next_Decl : Node_Id;
13080 Next_Node : Node_Id := After;
13081 Spec : Node_Id;
13082
13083 function Is_Outer_Type (T : Entity_Id) return Boolean;
13084 -- Check whether entity is declared in a scope external to that of the
13085 -- generic unit.
13086
13087 -------------------
13088 -- Is_Outer_Type --
13089 -------------------
13090
13091 function Is_Outer_Type (T : Entity_Id) return Boolean is
13092 Scop : Entity_Id := Scope (T);
13093
13094 begin
13095 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
13096 return True;
13097
13098 else
13099 while Scop /= Standard_Standard loop
13100 if Scop = Out_Of then
13101 return False;
13102 else
13103 Scop := Scope (Scop);
13104 end if;
13105 end loop;
13106
13107 return True;
13108 end if;
13109 end Is_Outer_Type;
13110
13111 -- Start of processing for Move_Freeze_Nodes
13112
13113 begin
13114 if No (L) then
13115 return;
13116 end if;
13117
13118 -- First remove the freeze nodes that may appear before all other
13119 -- declarations.
13120
13121 Decl := First (L);
13122 while Present (Decl)
13123 and then Nkind (Decl) = N_Freeze_Entity
13124 and then Is_Outer_Type (Entity (Decl))
13125 loop
13126 Decl := Remove_Head (L);
13127 Insert_After (Next_Node, Decl);
13128 Set_Analyzed (Decl, False);
13129 Next_Node := Decl;
13130 Decl := First (L);
13131 end loop;
13132
13133 -- Next scan the list of declarations and remove each freeze node that
13134 -- appears ahead of the current node.
13135
13136 while Present (Decl) loop
13137 while Present (Next (Decl))
13138 and then Nkind (Next (Decl)) = N_Freeze_Entity
13139 and then Is_Outer_Type (Entity (Next (Decl)))
13140 loop
13141 Next_Decl := Remove_Next (Decl);
13142 Insert_After (Next_Node, Next_Decl);
13143 Set_Analyzed (Next_Decl, False);
13144 Next_Node := Next_Decl;
13145 end loop;
13146
13147 -- If the declaration is a nested package or concurrent type, then
13148 -- recurse. Nested generic packages will have been processed from the
13149 -- inside out.
13150
13151 case Nkind (Decl) is
13152 when N_Package_Declaration =>
13153 Spec := Specification (Decl);
13154
13155 when N_Task_Type_Declaration =>
13156 Spec := Task_Definition (Decl);
13157
13158 when N_Protected_Type_Declaration =>
13159 Spec := Protected_Definition (Decl);
13160
13161 when others =>
13162 Spec := Empty;
13163 end case;
13164
13165 if Present (Spec) then
13166 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
13167 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
13168 end if;
13169
13170 Next (Decl);
13171 end loop;
13172 end Move_Freeze_Nodes;
13173
13174 ----------------
13175 -- Next_Assoc --
13176 ----------------
13177
13178 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
13179 begin
13180 return Generic_Renamings.Table (E).Next_In_HTable;
13181 end Next_Assoc;
13182
13183 ------------------------
13184 -- Preanalyze_Actuals --
13185 ------------------------
13186
13187 procedure Preanalyze_Actuals (N : Node_Id) is
13188 Assoc : Node_Id;
13189 Act : Node_Id;
13190 Errs : constant Int := Serious_Errors_Detected;
13191
13192 Cur : Entity_Id := Empty;
13193 -- Current homograph of the instance name
13194
13195 Vis : Boolean;
13196 -- Saved visibility status of the current homograph
13197
13198 begin
13199 Assoc := First (Generic_Associations (N));
13200
13201 -- If the instance is a child unit, its name may hide an outer homonym,
13202 -- so make it invisible to perform name resolution on the actuals.
13203
13204 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
13205 and then Present
13206 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
13207 then
13208 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
13209
13210 if Is_Compilation_Unit (Cur) then
13211 Vis := Is_Immediately_Visible (Cur);
13212 Set_Is_Immediately_Visible (Cur, False);
13213 else
13214 Cur := Empty;
13215 end if;
13216 end if;
13217
13218 while Present (Assoc) loop
13219 if Nkind (Assoc) /= N_Others_Choice then
13220 Act := Explicit_Generic_Actual_Parameter (Assoc);
13221
13222 -- Within a nested instantiation, a defaulted actual is an empty
13223 -- association, so nothing to analyze. If the subprogram actual
13224 -- is an attribute, analyze prefix only, because actual is not a
13225 -- complete attribute reference.
13226
13227 -- If actual is an allocator, analyze expression only. The full
13228 -- analysis can generate code, and if instance is a compilation
13229 -- unit we have to wait until the package instance is installed
13230 -- to have a proper place to insert this code.
13231
13232 -- String literals may be operators, but at this point we do not
13233 -- know whether the actual is a formal subprogram or a string.
13234
13235 if No (Act) then
13236 null;
13237
13238 elsif Nkind (Act) = N_Attribute_Reference then
13239 Analyze (Prefix (Act));
13240
13241 elsif Nkind (Act) = N_Explicit_Dereference then
13242 Analyze (Prefix (Act));
13243
13244 elsif Nkind (Act) = N_Allocator then
13245 declare
13246 Expr : constant Node_Id := Expression (Act);
13247
13248 begin
13249 if Nkind (Expr) = N_Subtype_Indication then
13250 Analyze (Subtype_Mark (Expr));
13251
13252 -- Analyze separately each discriminant constraint, when
13253 -- given with a named association.
13254
13255 declare
13256 Constr : Node_Id;
13257
13258 begin
13259 Constr := First (Constraints (Constraint (Expr)));
13260 while Present (Constr) loop
13261 if Nkind (Constr) = N_Discriminant_Association then
13262 Analyze (Expression (Constr));
13263 else
13264 Analyze (Constr);
13265 end if;
13266
13267 Next (Constr);
13268 end loop;
13269 end;
13270
13271 else
13272 Analyze (Expr);
13273 end if;
13274 end;
13275
13276 elsif Nkind (Act) /= N_Operator_Symbol then
13277 Analyze (Act);
13278 end if;
13279
13280 if Errs /= Serious_Errors_Detected then
13281
13282 -- Do a minimal analysis of the generic, to prevent spurious
13283 -- warnings complaining about the generic being unreferenced,
13284 -- before abandoning the instantiation.
13285
13286 Analyze (Name (N));
13287
13288 if Is_Entity_Name (Name (N))
13289 and then Etype (Name (N)) /= Any_Type
13290 then
13291 Generate_Reference (Entity (Name (N)), Name (N));
13292 Set_Is_Instantiated (Entity (Name (N)));
13293 end if;
13294
13295 if Present (Cur) then
13296
13297 -- For the case of a child instance hiding an outer homonym,
13298 -- provide additional warning which might explain the error.
13299
13300 Set_Is_Immediately_Visible (Cur, Vis);
13301 Error_Msg_NE
13302 ("& hides outer unit with the same name??",
13303 N, Defining_Unit_Name (N));
13304 end if;
13305
13306 Abandon_Instantiation (Act);
13307 end if;
13308 end if;
13309
13310 Next (Assoc);
13311 end loop;
13312
13313 if Present (Cur) then
13314 Set_Is_Immediately_Visible (Cur, Vis);
13315 end if;
13316 end Preanalyze_Actuals;
13317
13318 -------------------
13319 -- Remove_Parent --
13320 -------------------
13321
13322 procedure Remove_Parent (In_Body : Boolean := False) is
13323 S : Entity_Id := Current_Scope;
13324 -- S is the scope containing the instantiation just completed. The scope
13325 -- stack contains the parent instances of the instantiation, followed by
13326 -- the original S.
13327
13328 Cur_P : Entity_Id;
13329 E : Entity_Id;
13330 P : Entity_Id;
13331 Hidden : Elmt_Id;
13332
13333 begin
13334 -- After child instantiation is complete, remove from scope stack the
13335 -- extra copy of the current scope, and then remove parent instances.
13336
13337 if not In_Body then
13338 Pop_Scope;
13339
13340 while Current_Scope /= S loop
13341 P := Current_Scope;
13342 End_Package_Scope (Current_Scope);
13343
13344 if In_Open_Scopes (P) then
13345 E := First_Entity (P);
13346 while Present (E) loop
13347 Set_Is_Immediately_Visible (E, True);
13348 Next_Entity (E);
13349 end loop;
13350
13351 -- If instantiation is declared in a block, it is the enclosing
13352 -- scope that might be a parent instance. Note that only one
13353 -- block can be involved, because the parent instances have
13354 -- been installed within it.
13355
13356 if Ekind (P) = E_Block then
13357 Cur_P := Scope (P);
13358 else
13359 Cur_P := P;
13360 end if;
13361
13362 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
13363 -- We are within an instance of some sibling. Retain
13364 -- visibility of parent, for proper subsequent cleanup, and
13365 -- reinstall private declarations as well.
13366
13367 Set_In_Private_Part (P);
13368 Install_Private_Declarations (P);
13369 end if;
13370
13371 -- If the ultimate parent is a top-level unit recorded in
13372 -- Instance_Parent_Unit, then reset its visibility to what it was
13373 -- before instantiation. (It's not clear what the purpose is of
13374 -- testing whether Scope (P) is In_Open_Scopes, but that test was
13375 -- present before the ultimate parent test was added.???)
13376
13377 elsif not In_Open_Scopes (Scope (P))
13378 or else (P = Instance_Parent_Unit
13379 and then not Parent_Unit_Visible)
13380 then
13381 Set_Is_Immediately_Visible (P, False);
13382
13383 -- If the current scope is itself an instantiation of a generic
13384 -- nested within P, and we are in the private part of body of this
13385 -- instantiation, restore the full views of P, that were removed
13386 -- in End_Package_Scope above. This obscure case can occur when a
13387 -- subunit of a generic contains an instance of a child unit of
13388 -- its generic parent unit.
13389
13390 elsif S = Current_Scope and then Is_Generic_Instance (S) then
13391 declare
13392 Par : constant Entity_Id :=
13393 Generic_Parent (Package_Specification (S));
13394 begin
13395 if Present (Par)
13396 and then P = Scope (Par)
13397 and then (In_Package_Body (S) or else In_Private_Part (S))
13398 then
13399 Set_In_Private_Part (P);
13400 Install_Private_Declarations (P);
13401 end if;
13402 end;
13403 end if;
13404 end loop;
13405
13406 -- Reset visibility of entities in the enclosing scope
13407
13408 Set_Is_Hidden_Open_Scope (Current_Scope, False);
13409
13410 Hidden := First_Elmt (Hidden_Entities);
13411 while Present (Hidden) loop
13412 Set_Is_Immediately_Visible (Node (Hidden), True);
13413 Next_Elmt (Hidden);
13414 end loop;
13415
13416 else
13417 -- Each body is analyzed separately, and there is no context that
13418 -- needs preserving from one body instance to the next, so remove all
13419 -- parent scopes that have been installed.
13420
13421 while Present (S) loop
13422 End_Package_Scope (S);
13423 Set_Is_Immediately_Visible (S, False);
13424 S := Current_Scope;
13425 exit when S = Standard_Standard;
13426 end loop;
13427 end if;
13428 end Remove_Parent;
13429
13430 -----------------
13431 -- Restore_Env --
13432 -----------------
13433
13434 procedure Restore_Env is
13435 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13436
13437 begin
13438 if No (Current_Instantiated_Parent.Act_Id) then
13439 -- Restore environment after subprogram inlining
13440
13441 Restore_Private_Views (Empty);
13442 end if;
13443
13444 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13445 Exchanged_Views := Saved.Exchanged_Views;
13446 Hidden_Entities := Saved.Hidden_Entities;
13447 Current_Sem_Unit := Saved.Current_Sem_Unit;
13448 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13449 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13450
13451 Restore_Opt_Config_Switches (Saved.Switches);
13452
13453 Instance_Envs.Decrement_Last;
13454 end Restore_Env;
13455
13456 ---------------------------
13457 -- Restore_Private_Views --
13458 ---------------------------
13459
13460 procedure Restore_Private_Views
13461 (Pack_Id : Entity_Id;
13462 Is_Package : Boolean := True)
13463 is
13464 M : Elmt_Id;
13465 E : Entity_Id;
13466 Typ : Entity_Id;
13467 Dep_Elmt : Elmt_Id;
13468 Dep_Typ : Node_Id;
13469
13470 procedure Restore_Nested_Formal (Formal : Entity_Id);
13471 -- Hide the generic formals of formal packages declared with box which
13472 -- were reachable in the current instantiation.
13473
13474 ---------------------------
13475 -- Restore_Nested_Formal --
13476 ---------------------------
13477
13478 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13479 Ent : Entity_Id;
13480
13481 begin
13482 if Present (Renamed_Object (Formal))
13483 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13484 then
13485 return;
13486
13487 elsif Present (Associated_Formal_Package (Formal)) then
13488 Ent := First_Entity (Formal);
13489 while Present (Ent) loop
13490 exit when Ekind (Ent) = E_Package
13491 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13492
13493 Set_Is_Hidden (Ent);
13494 Set_Is_Potentially_Use_Visible (Ent, False);
13495
13496 -- If package, then recurse
13497
13498 if Ekind (Ent) = E_Package then
13499 Restore_Nested_Formal (Ent);
13500 end if;
13501
13502 Next_Entity (Ent);
13503 end loop;
13504 end if;
13505 end Restore_Nested_Formal;
13506
13507 -- Start of processing for Restore_Private_Views
13508
13509 begin
13510 M := First_Elmt (Exchanged_Views);
13511 while Present (M) loop
13512 Typ := Node (M);
13513
13514 -- Subtypes of types whose views have been exchanged, and that are
13515 -- defined within the instance, were not on the Private_Dependents
13516 -- list on entry to the instance, so they have to be exchanged
13517 -- explicitly now, in order to remain consistent with the view of the
13518 -- parent type.
13519
13520 if Ekind_In (Typ, E_Private_Type,
13521 E_Limited_Private_Type,
13522 E_Record_Type_With_Private)
13523 then
13524 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13525 while Present (Dep_Elmt) loop
13526 Dep_Typ := Node (Dep_Elmt);
13527
13528 if Scope (Dep_Typ) = Pack_Id
13529 and then Present (Full_View (Dep_Typ))
13530 then
13531 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13532 Exchange_Declarations (Dep_Typ);
13533 end if;
13534
13535 Next_Elmt (Dep_Elmt);
13536 end loop;
13537 end if;
13538
13539 Exchange_Declarations (Node (M));
13540 Next_Elmt (M);
13541 end loop;
13542
13543 if No (Pack_Id) then
13544 return;
13545 end if;
13546
13547 -- Make the generic formal parameters private, and make the formal types
13548 -- into subtypes of the actuals again.
13549
13550 E := First_Entity (Pack_Id);
13551 while Present (E) loop
13552 Set_Is_Hidden (E, True);
13553
13554 if Is_Type (E)
13555 and then Nkind (Parent (E)) = N_Subtype_Declaration
13556 then
13557 -- If the actual for E is itself a generic actual type from
13558 -- an enclosing instance, E is still a generic actual type
13559 -- outside of the current instance. This matter when resolving
13560 -- an overloaded call that may be ambiguous in the enclosing
13561 -- instance, when two of its actuals coincide.
13562
13563 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13564 and then Is_Generic_Actual_Type
13565 (Entity (Subtype_Indication (Parent (E))))
13566 then
13567 null;
13568 else
13569 Set_Is_Generic_Actual_Type (E, False);
13570 end if;
13571
13572 -- An unusual case of aliasing: the actual may also be directly
13573 -- visible in the generic, and be private there, while it is fully
13574 -- visible in the context of the instance. The internal subtype
13575 -- is private in the instance but has full visibility like its
13576 -- parent in the enclosing scope. This enforces the invariant that
13577 -- the privacy status of all private dependents of a type coincide
13578 -- with that of the parent type. This can only happen when a
13579 -- generic child unit is instantiated within a sibling.
13580
13581 if Is_Private_Type (E)
13582 and then not Is_Private_Type (Etype (E))
13583 then
13584 Exchange_Declarations (E);
13585 end if;
13586
13587 elsif Ekind (E) = E_Package then
13588
13589 -- The end of the renaming list is the renaming of the generic
13590 -- package itself. If the instance is a subprogram, all entities
13591 -- in the corresponding package are renamings. If this entity is
13592 -- a formal package, make its own formals private as well. The
13593 -- actual in this case is itself the renaming of an instantiation.
13594 -- If the entity is not a package renaming, it is the entity
13595 -- created to validate formal package actuals: ignore it.
13596
13597 -- If the actual is itself a formal package for the enclosing
13598 -- generic, or the actual for such a formal package, it remains
13599 -- visible on exit from the instance, and therefore nothing needs
13600 -- to be done either, except to keep it accessible.
13601
13602 if Is_Package and then Renamed_Object (E) = Pack_Id then
13603 exit;
13604
13605 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
13606 null;
13607
13608 elsif
13609 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
13610 then
13611 Set_Is_Hidden (E, False);
13612
13613 else
13614 declare
13615 Act_P : constant Entity_Id := Renamed_Object (E);
13616 Id : Entity_Id;
13617
13618 begin
13619 Id := First_Entity (Act_P);
13620 while Present (Id)
13621 and then Id /= First_Private_Entity (Act_P)
13622 loop
13623 exit when Ekind (Id) = E_Package
13624 and then Renamed_Object (Id) = Act_P;
13625
13626 Set_Is_Hidden (Id, True);
13627 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
13628
13629 if Ekind (Id) = E_Package then
13630 Restore_Nested_Formal (Id);
13631 end if;
13632
13633 Next_Entity (Id);
13634 end loop;
13635 end;
13636 end if;
13637 end if;
13638
13639 Next_Entity (E);
13640 end loop;
13641 end Restore_Private_Views;
13642
13643 --------------
13644 -- Save_Env --
13645 --------------
13646
13647 procedure Save_Env
13648 (Gen_Unit : Entity_Id;
13649 Act_Unit : Entity_Id)
13650 is
13651 begin
13652 Init_Env;
13653 Set_Instance_Env (Gen_Unit, Act_Unit);
13654 end Save_Env;
13655
13656 ----------------------------
13657 -- Save_Global_References --
13658 ----------------------------
13659
13660 procedure Save_Global_References (Templ : Node_Id) is
13661
13662 -- ??? it is horrible to use global variables in highly recursive code
13663
13664 E : Entity_Id;
13665 -- The entity of the current associated node
13666
13667 Gen_Scope : Entity_Id;
13668 -- The scope of the generic for which references are being saved
13669
13670 N2 : Node_Id;
13671 -- The current associated node
13672
13673 function Is_Global (E : Entity_Id) return Boolean;
13674 -- Check whether entity is defined outside of generic unit. Examine the
13675 -- scope of an entity, and the scope of the scope, etc, until we find
13676 -- either Standard, in which case the entity is global, or the generic
13677 -- unit itself, which indicates that the entity is local. If the entity
13678 -- is the generic unit itself, as in the case of a recursive call, or
13679 -- the enclosing generic unit, if different from the current scope, then
13680 -- it is local as well, because it will be replaced at the point of
13681 -- instantiation. On the other hand, if it is a reference to a child
13682 -- unit of a common ancestor, which appears in an instantiation, it is
13683 -- global because it is used to denote a specific compilation unit at
13684 -- the time the instantiations will be analyzed.
13685
13686 procedure Reset_Entity (N : Node_Id);
13687 -- Save semantic information on global entity so that it is not resolved
13688 -- again at instantiation time.
13689
13690 procedure Save_Entity_Descendants (N : Node_Id);
13691 -- Apply Save_Global_References to the two syntactic descendants of
13692 -- non-terminal nodes that carry an Associated_Node and are processed
13693 -- through Reset_Entity. Once the global entity (if any) has been
13694 -- captured together with its type, only two syntactic descendants need
13695 -- to be traversed to complete the processing of the tree rooted at N.
13696 -- This applies to Selected_Components, Expanded_Names, and to Operator
13697 -- nodes. N can also be a character literal, identifier, or operator
13698 -- symbol node, but the call has no effect in these cases.
13699
13700 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
13701 -- Default actuals in nested instances must be handled specially
13702 -- because there is no link to them from the original tree. When an
13703 -- actual subprogram is given by a default, we add an explicit generic
13704 -- association for it in the instantiation node. When we save the
13705 -- global references on the name of the instance, we recover the list
13706 -- of generic associations, and add an explicit one to the original
13707 -- generic tree, through which a global actual can be preserved.
13708 -- Similarly, if a child unit is instantiated within a sibling, in the
13709 -- context of the parent, we must preserve the identifier of the parent
13710 -- so that it can be properly resolved in a subsequent instantiation.
13711
13712 procedure Save_Global_Descendant (D : Union_Id);
13713 -- Apply Save_References recursively to the descendents of node D
13714
13715 procedure Save_References (N : Node_Id);
13716 -- This is the recursive procedure that does the work, once the
13717 -- enclosing generic scope has been established.
13718
13719 ---------------
13720 -- Is_Global --
13721 ---------------
13722
13723 function Is_Global (E : Entity_Id) return Boolean is
13724 Se : Entity_Id;
13725
13726 function Is_Instance_Node (Decl : Node_Id) return Boolean;
13727 -- Determine whether the parent node of a reference to a child unit
13728 -- denotes an instantiation or a formal package, in which case the
13729 -- reference to the child unit is global, even if it appears within
13730 -- the current scope (e.g. when the instance appears within the body
13731 -- of an ancestor).
13732
13733 ----------------------
13734 -- Is_Instance_Node --
13735 ----------------------
13736
13737 function Is_Instance_Node (Decl : Node_Id) return Boolean is
13738 begin
13739 return Nkind (Decl) in N_Generic_Instantiation
13740 or else
13741 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
13742 end Is_Instance_Node;
13743
13744 -- Start of processing for Is_Global
13745
13746 begin
13747 if E = Gen_Scope then
13748 return False;
13749
13750 elsif E = Standard_Standard then
13751 return True;
13752
13753 elsif Is_Child_Unit (E)
13754 and then (Is_Instance_Node (Parent (N2))
13755 or else (Nkind (Parent (N2)) = N_Expanded_Name
13756 and then N2 = Selector_Name (Parent (N2))
13757 and then
13758 Is_Instance_Node (Parent (Parent (N2)))))
13759 then
13760 return True;
13761
13762 else
13763 Se := Scope (E);
13764 while Se /= Gen_Scope loop
13765 if Se = Standard_Standard then
13766 return True;
13767 else
13768 Se := Scope (Se);
13769 end if;
13770 end loop;
13771
13772 return False;
13773 end if;
13774 end Is_Global;
13775
13776 ------------------
13777 -- Reset_Entity --
13778 ------------------
13779
13780 procedure Reset_Entity (N : Node_Id) is
13781 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
13782 -- If the type of N2 is global to the generic unit, save the type in
13783 -- the generic node. Just as we perform name capture for explicit
13784 -- references within the generic, we must capture the global types
13785 -- of local entities because they may participate in resolution in
13786 -- the instance.
13787
13788 function Top_Ancestor (E : Entity_Id) return Entity_Id;
13789 -- Find the ultimate ancestor of the current unit. If it is not a
13790 -- generic unit, then the name of the current unit in the prefix of
13791 -- an expanded name must be replaced with its generic homonym to
13792 -- ensure that it will be properly resolved in an instance.
13793
13794 ---------------------
13795 -- Set_Global_Type --
13796 ---------------------
13797
13798 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
13799 Typ : constant Entity_Id := Etype (N2);
13800
13801 begin
13802 Set_Etype (N, Typ);
13803
13804 -- If the entity of N is not the associated node, this is a
13805 -- nested generic and it has an associated node as well, whose
13806 -- type is already the full view (see below). Indicate that the
13807 -- original node has a private view.
13808
13809 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
13810 Set_Has_Private_View (N);
13811 end if;
13812
13813 -- If not a private type, nothing else to do
13814
13815 if not Is_Private_Type (Typ) then
13816 if Is_Array_Type (Typ)
13817 and then Is_Private_Type (Component_Type (Typ))
13818 then
13819 Set_Has_Private_View (N);
13820 end if;
13821
13822 -- If it is a derivation of a private type in a context where no
13823 -- full view is needed, nothing to do either.
13824
13825 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
13826 null;
13827
13828 -- Otherwise mark the type for flipping and use the full view when
13829 -- available.
13830
13831 else
13832 Set_Has_Private_View (N);
13833
13834 if Present (Full_View (Typ)) then
13835 Set_Etype (N2, Full_View (Typ));
13836 end if;
13837 end if;
13838 end Set_Global_Type;
13839
13840 ------------------
13841 -- Top_Ancestor --
13842 ------------------
13843
13844 function Top_Ancestor (E : Entity_Id) return Entity_Id is
13845 Par : Entity_Id;
13846
13847 begin
13848 Par := E;
13849 while Is_Child_Unit (Par) loop
13850 Par := Scope (Par);
13851 end loop;
13852
13853 return Par;
13854 end Top_Ancestor;
13855
13856 -- Start of processing for Reset_Entity
13857
13858 begin
13859 N2 := Get_Associated_Node (N);
13860 E := Entity (N2);
13861
13862 if Present (E) then
13863
13864 -- If the node is an entry call to an entry in an enclosing task,
13865 -- it is rewritten as a selected component. No global entity to
13866 -- preserve in this case, since the expansion will be redone in
13867 -- the instance.
13868
13869 if not Nkind_In (E, N_Defining_Character_Literal,
13870 N_Defining_Identifier,
13871 N_Defining_Operator_Symbol)
13872 then
13873 Set_Associated_Node (N, Empty);
13874 Set_Etype (N, Empty);
13875 return;
13876 end if;
13877
13878 -- If the entity is an itype created as a subtype of an access
13879 -- type with a null exclusion restore source entity for proper
13880 -- visibility. The itype will be created anew in the instance.
13881
13882 if Is_Itype (E)
13883 and then Ekind (E) = E_Access_Subtype
13884 and then Is_Entity_Name (N)
13885 and then Chars (Etype (E)) = Chars (N)
13886 then
13887 E := Etype (E);
13888 Set_Entity (N2, E);
13889 Set_Etype (N2, E);
13890 end if;
13891
13892 if Is_Global (E) then
13893
13894 -- If the entity is a package renaming that is the prefix of
13895 -- an expanded name, it has been rewritten as the renamed
13896 -- package, which is necessary semantically but complicates
13897 -- ASIS tree traversal, so we recover the original entity to
13898 -- expose the renaming. Take into account that the context may
13899 -- be a nested generic, that the original node may itself have
13900 -- an associated node that had better be an entity, and that
13901 -- the current node is still a selected component.
13902
13903 if Ekind (E) = E_Package
13904 and then Nkind (N) = N_Selected_Component
13905 and then Nkind (Parent (N)) = N_Expanded_Name
13906 and then Present (Original_Node (N2))
13907 and then Is_Entity_Name (Original_Node (N2))
13908 and then Present (Entity (Original_Node (N2)))
13909 then
13910 if Is_Global (Entity (Original_Node (N2))) then
13911 N2 := Original_Node (N2);
13912 Set_Associated_Node (N, N2);
13913 Set_Global_Type (N, N2);
13914
13915 -- Renaming is local, and will be resolved in instance
13916
13917 else
13918 Set_Associated_Node (N, Empty);
13919 Set_Etype (N, Empty);
13920 end if;
13921
13922 else
13923 Set_Global_Type (N, N2);
13924 end if;
13925
13926 elsif Nkind (N) = N_Op_Concat
13927 and then Is_Generic_Type (Etype (N2))
13928 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
13929 or else
13930 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
13931 and then Is_Intrinsic_Subprogram (E)
13932 then
13933 null;
13934
13935 -- Entity is local. Mark generic node as unresolved. Note that now
13936 -- it does not have an entity.
13937
13938 else
13939 Set_Associated_Node (N, Empty);
13940 Set_Etype (N, Empty);
13941 end if;
13942
13943 if Nkind (Parent (N)) in N_Generic_Instantiation
13944 and then N = Name (Parent (N))
13945 then
13946 Save_Global_Defaults (Parent (N), Parent (N2));
13947 end if;
13948
13949 elsif Nkind (Parent (N)) = N_Selected_Component
13950 and then Nkind (Parent (N2)) = N_Expanded_Name
13951 then
13952 if Is_Global (Entity (Parent (N2))) then
13953 Change_Selected_Component_To_Expanded_Name (Parent (N));
13954 Set_Associated_Node (Parent (N), Parent (N2));
13955 Set_Global_Type (Parent (N), Parent (N2));
13956 Save_Entity_Descendants (N);
13957
13958 -- If this is a reference to the current generic entity, replace
13959 -- by the name of the generic homonym of the current package. This
13960 -- is because in an instantiation Par.P.Q will not resolve to the
13961 -- name of the instance, whose enclosing scope is not necessarily
13962 -- Par. We use the generic homonym rather that the name of the
13963 -- generic itself because it may be hidden by a local declaration.
13964
13965 elsif In_Open_Scopes (Entity (Parent (N2)))
13966 and then not
13967 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
13968 then
13969 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
13970 Rewrite (Parent (N),
13971 Make_Identifier (Sloc (N),
13972 Chars =>
13973 Chars (Generic_Homonym (Entity (Parent (N2))))));
13974 else
13975 Rewrite (Parent (N),
13976 Make_Identifier (Sloc (N),
13977 Chars => Chars (Selector_Name (Parent (N2)))));
13978 end if;
13979 end if;
13980
13981 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
13982 and then Parent (N) = Name (Parent (Parent (N)))
13983 then
13984 Save_Global_Defaults
13985 (Parent (Parent (N)), Parent (Parent (N2)));
13986 end if;
13987
13988 -- A selected component may denote a static constant that has been
13989 -- folded. If the static constant is global to the generic, capture
13990 -- its value. Otherwise the folding will happen in any instantiation.
13991
13992 elsif Nkind (Parent (N)) = N_Selected_Component
13993 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
13994 then
13995 if Present (Entity (Original_Node (Parent (N2))))
13996 and then Is_Global (Entity (Original_Node (Parent (N2))))
13997 then
13998 Rewrite (Parent (N), New_Copy (Parent (N2)));
13999 Set_Analyzed (Parent (N), False);
14000 end if;
14001
14002 -- A selected component may be transformed into a parameterless
14003 -- function call. If the called entity is global, rewrite the node
14004 -- appropriately, i.e. as an extended name for the global entity.
14005
14006 elsif Nkind (Parent (N)) = N_Selected_Component
14007 and then Nkind (Parent (N2)) = N_Function_Call
14008 and then N = Selector_Name (Parent (N))
14009 then
14010 if No (Parameter_Associations (Parent (N2))) then
14011 if Is_Global (Entity (Name (Parent (N2)))) then
14012 Change_Selected_Component_To_Expanded_Name (Parent (N));
14013 Set_Associated_Node (Parent (N), Name (Parent (N2)));
14014 Set_Global_Type (Parent (N), Name (Parent (N2)));
14015 Save_Entity_Descendants (N);
14016
14017 else
14018 Set_Is_Prefixed_Call (Parent (N));
14019 Set_Associated_Node (N, Empty);
14020 Set_Etype (N, Empty);
14021 end if;
14022
14023 -- In Ada 2005, X.F may be a call to a primitive operation,
14024 -- rewritten as F (X). This rewriting will be done again in an
14025 -- instance, so keep the original node. Global entities will be
14026 -- captured as for other constructs. Indicate that this must
14027 -- resolve as a call, to prevent accidental overloading in the
14028 -- instance, if both a component and a primitive operation appear
14029 -- as candidates.
14030
14031 else
14032 Set_Is_Prefixed_Call (Parent (N));
14033 end if;
14034
14035 -- Entity is local. Reset in generic unit, so that node is resolved
14036 -- anew at the point of instantiation.
14037
14038 else
14039 Set_Associated_Node (N, Empty);
14040 Set_Etype (N, Empty);
14041 end if;
14042 end Reset_Entity;
14043
14044 -----------------------------
14045 -- Save_Entity_Descendants --
14046 -----------------------------
14047
14048 procedure Save_Entity_Descendants (N : Node_Id) is
14049 begin
14050 case Nkind (N) is
14051 when N_Binary_Op =>
14052 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
14053 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14054
14055 when N_Unary_Op =>
14056 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14057
14058 when N_Expanded_Name |
14059 N_Selected_Component =>
14060 Save_Global_Descendant (Union_Id (Prefix (N)));
14061 Save_Global_Descendant (Union_Id (Selector_Name (N)));
14062
14063 when N_Identifier |
14064 N_Character_Literal |
14065 N_Operator_Symbol =>
14066 null;
14067
14068 when others =>
14069 raise Program_Error;
14070 end case;
14071 end Save_Entity_Descendants;
14072
14073 --------------------------
14074 -- Save_Global_Defaults --
14075 --------------------------
14076
14077 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
14078 Loc : constant Source_Ptr := Sloc (N1);
14079 Assoc2 : constant List_Id := Generic_Associations (N2);
14080 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
14081 Assoc1 : List_Id;
14082 Act1 : Node_Id;
14083 Act2 : Node_Id;
14084 Def : Node_Id;
14085 Ndec : Node_Id;
14086 Subp : Entity_Id;
14087 Actual : Entity_Id;
14088
14089 begin
14090 Assoc1 := Generic_Associations (N1);
14091
14092 if Present (Assoc1) then
14093 Act1 := First (Assoc1);
14094 else
14095 Act1 := Empty;
14096 Set_Generic_Associations (N1, New_List);
14097 Assoc1 := Generic_Associations (N1);
14098 end if;
14099
14100 if Present (Assoc2) then
14101 Act2 := First (Assoc2);
14102 else
14103 return;
14104 end if;
14105
14106 while Present (Act1) and then Present (Act2) loop
14107 Next (Act1);
14108 Next (Act2);
14109 end loop;
14110
14111 -- Find the associations added for default subprograms
14112
14113 if Present (Act2) then
14114 while Nkind (Act2) /= N_Generic_Association
14115 or else No (Entity (Selector_Name (Act2)))
14116 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
14117 loop
14118 Next (Act2);
14119 end loop;
14120
14121 -- Add a similar association if the default is global. The
14122 -- renaming declaration for the actual has been analyzed, and
14123 -- its alias is the program it renames. Link the actual in the
14124 -- original generic tree with the node in the analyzed tree.
14125
14126 while Present (Act2) loop
14127 Subp := Entity (Selector_Name (Act2));
14128 Def := Explicit_Generic_Actual_Parameter (Act2);
14129
14130 -- Following test is defence against rubbish errors
14131
14132 if No (Alias (Subp)) then
14133 return;
14134 end if;
14135
14136 -- Retrieve the resolved actual from the renaming declaration
14137 -- created for the instantiated formal.
14138
14139 Actual := Entity (Name (Parent (Parent (Subp))));
14140 Set_Entity (Def, Actual);
14141 Set_Etype (Def, Etype (Actual));
14142
14143 if Is_Global (Actual) then
14144 Ndec :=
14145 Make_Generic_Association (Loc,
14146 Selector_Name =>
14147 New_Occurrence_Of (Subp, Loc),
14148 Explicit_Generic_Actual_Parameter =>
14149 New_Occurrence_Of (Actual, Loc));
14150
14151 Set_Associated_Node
14152 (Explicit_Generic_Actual_Parameter (Ndec), Def);
14153
14154 Append (Ndec, Assoc1);
14155
14156 -- If there are other defaults, add a dummy association in case
14157 -- there are other defaulted formals with the same name.
14158
14159 elsif Present (Next (Act2)) then
14160 Ndec :=
14161 Make_Generic_Association (Loc,
14162 Selector_Name =>
14163 New_Occurrence_Of (Subp, Loc),
14164 Explicit_Generic_Actual_Parameter => Empty);
14165
14166 Append (Ndec, Assoc1);
14167 end if;
14168
14169 Next (Act2);
14170 end loop;
14171 end if;
14172
14173 if Nkind (Name (N1)) = N_Identifier
14174 and then Is_Child_Unit (Gen_Id)
14175 and then Is_Global (Gen_Id)
14176 and then Is_Generic_Unit (Scope (Gen_Id))
14177 and then In_Open_Scopes (Scope (Gen_Id))
14178 then
14179 -- This is an instantiation of a child unit within a sibling, so
14180 -- that the generic parent is in scope. An eventual instance must
14181 -- occur within the scope of an instance of the parent. Make name
14182 -- in instance into an expanded name, to preserve the identifier
14183 -- of the parent, so it can be resolved subsequently.
14184
14185 Rewrite (Name (N2),
14186 Make_Expanded_Name (Loc,
14187 Chars => Chars (Gen_Id),
14188 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14189 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14190 Set_Entity (Name (N2), Gen_Id);
14191
14192 Rewrite (Name (N1),
14193 Make_Expanded_Name (Loc,
14194 Chars => Chars (Gen_Id),
14195 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14196 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14197
14198 Set_Associated_Node (Name (N1), Name (N2));
14199 Set_Associated_Node (Prefix (Name (N1)), Empty);
14200 Set_Associated_Node
14201 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
14202 Set_Etype (Name (N1), Etype (Gen_Id));
14203 end if;
14204 end Save_Global_Defaults;
14205
14206 ----------------------------
14207 -- Save_Global_Descendant --
14208 ----------------------------
14209
14210 procedure Save_Global_Descendant (D : Union_Id) is
14211 N1 : Node_Id;
14212
14213 begin
14214 if D in Node_Range then
14215 if D = Union_Id (Empty) then
14216 null;
14217
14218 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
14219 Save_References (Node_Id (D));
14220 end if;
14221
14222 elsif D in List_Range then
14223 if D = Union_Id (No_List) or else Is_Empty_List (List_Id (D)) then
14224 null;
14225
14226 else
14227 N1 := First (List_Id (D));
14228 while Present (N1) loop
14229 Save_References (N1);
14230 Next (N1);
14231 end loop;
14232 end if;
14233
14234 -- Element list or other non-node field, nothing to do
14235
14236 else
14237 null;
14238 end if;
14239 end Save_Global_Descendant;
14240
14241 ---------------------
14242 -- Save_References --
14243 ---------------------
14244
14245 -- This is the recursive procedure that does the work once the enclosing
14246 -- generic scope has been established. We have to treat specially a
14247 -- number of node rewritings that are required by semantic processing
14248 -- and which change the kind of nodes in the generic copy: typically
14249 -- constant-folding, replacing an operator node by a string literal, or
14250 -- a selected component by an expanded name. In each of those cases, the
14251 -- transformation is propagated to the generic unit.
14252
14253 procedure Save_References (N : Node_Id) is
14254 Loc : constant Source_Ptr := Sloc (N);
14255
14256 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
14257 -- Determine whether arbitrary node Nod requires delayed capture of
14258 -- global references within its aspect specifications.
14259
14260 procedure Save_References_In_Aggregate (N : Node_Id);
14261 -- Save all global references in [extension] aggregate node N
14262
14263 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
14264 -- Save all global references in a character literal or operator
14265 -- symbol denoted by N.
14266
14267 procedure Save_References_In_Descendants (N : Node_Id);
14268 -- Save all global references in all descendants of node N
14269
14270 procedure Save_References_In_Identifier (N : Node_Id);
14271 -- Save all global references in identifier node N
14272
14273 procedure Save_References_In_Operator (N : Node_Id);
14274 -- Save all global references in operator node N
14275
14276 procedure Save_References_In_Pragma (Prag : Node_Id);
14277 -- Save all global references found within the expression of pragma
14278 -- Prag.
14279
14280 ---------------------------
14281 -- Requires_Delayed_Save --
14282 ---------------------------
14283
14284 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
14285 begin
14286 -- Generic packages and subprograms require delayed capture of
14287 -- global references within their aspects due to the timing of
14288 -- annotation analysis.
14289
14290 if Nkind_In (Nod, N_Generic_Package_Declaration,
14291 N_Generic_Subprogram_Declaration,
14292 N_Package_Body,
14293 N_Package_Body_Stub,
14294 N_Subprogram_Body,
14295 N_Subprogram_Body_Stub)
14296 then
14297 -- Since the capture of global references is done on the
14298 -- unanalyzed generic template, there is no information around
14299 -- to infer the context. Use the Associated_Entity linkages to
14300 -- peek into the analyzed generic copy and determine what the
14301 -- template corresponds to.
14302
14303 if Nod = Templ then
14304 return
14305 Is_Generic_Declaration_Or_Body
14306 (Unit_Declaration_Node
14307 (Associated_Entity (Defining_Entity (Nod))));
14308
14309 -- Otherwise the generic unit being processed is not the top
14310 -- level template. It is safe to capture of global references
14311 -- within the generic unit because at this point the top level
14312 -- copy is fully analyzed.
14313
14314 else
14315 return False;
14316 end if;
14317
14318 -- Otherwise capture the global references without interference
14319
14320 else
14321 return False;
14322 end if;
14323 end Requires_Delayed_Save;
14324
14325 ----------------------------------
14326 -- Save_References_In_Aggregate --
14327 ----------------------------------
14328
14329 procedure Save_References_In_Aggregate (N : Node_Id) is
14330 Nam : Node_Id;
14331 Qual : Node_Id := Empty;
14332 Typ : Entity_Id := Empty;
14333
14334 use Atree.Unchecked_Access;
14335 -- This code section is part of implementing an untyped tree
14336 -- traversal, so it needs direct access to node fields.
14337
14338 begin
14339 N2 := Get_Associated_Node (N);
14340
14341 if Present (N2) then
14342 Typ := Etype (N2);
14343
14344 -- In an instance within a generic, use the name of the actual
14345 -- and not the original generic parameter. If the actual is
14346 -- global in the current generic it must be preserved for its
14347 -- instantiation.
14348
14349 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14350 and then Present (Generic_Parent_Type (Parent (Typ)))
14351 then
14352 Typ := Base_Type (Typ);
14353 Set_Etype (N2, Typ);
14354 end if;
14355 end if;
14356
14357 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
14358 Set_Associated_Node (N, Empty);
14359
14360 -- If the aggregate is an actual in a call, it has been
14361 -- resolved in the current context, to some local type. The
14362 -- enclosing call may have been disambiguated by the aggregate,
14363 -- and this disambiguation might fail at instantiation time
14364 -- because the type to which the aggregate did resolve is not
14365 -- preserved. In order to preserve some of this information,
14366 -- wrap the aggregate in a qualified expression, using the id
14367 -- of its type. For further disambiguation we qualify the type
14368 -- name with its scope (if visible) because both id's will have
14369 -- corresponding entities in an instance. This resolves most of
14370 -- the problems with missing type information on aggregates in
14371 -- instances.
14372
14373 if Present (N2)
14374 and then Nkind (N2) = Nkind (N)
14375 and then Nkind (Parent (N2)) in N_Subprogram_Call
14376 and then Present (Typ)
14377 and then Comes_From_Source (Typ)
14378 then
14379 Nam := Make_Identifier (Loc, Chars (Typ));
14380
14381 if Is_Immediately_Visible (Scope (Typ)) then
14382 Nam :=
14383 Make_Selected_Component (Loc,
14384 Prefix =>
14385 Make_Identifier (Loc, Chars (Scope (Typ))),
14386 Selector_Name => Nam);
14387 end if;
14388
14389 Qual :=
14390 Make_Qualified_Expression (Loc,
14391 Subtype_Mark => Nam,
14392 Expression => Relocate_Node (N));
14393 end if;
14394 end if;
14395
14396 Save_Global_Descendant (Field1 (N));
14397 Save_Global_Descendant (Field2 (N));
14398 Save_Global_Descendant (Field3 (N));
14399 Save_Global_Descendant (Field5 (N));
14400
14401 if Present (Qual) then
14402 Rewrite (N, Qual);
14403 end if;
14404 end Save_References_In_Aggregate;
14405
14406 ----------------------------------------------
14407 -- Save_References_In_Char_Lit_Or_Op_Symbol --
14408 ----------------------------------------------
14409
14410 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
14411 begin
14412 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14413 Reset_Entity (N);
14414
14415 elsif Nkind (N) = N_Operator_Symbol
14416 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
14417 then
14418 Change_Operator_Symbol_To_String_Literal (N);
14419 end if;
14420 end Save_References_In_Char_Lit_Or_Op_Symbol;
14421
14422 ------------------------------------
14423 -- Save_References_In_Descendants --
14424 ------------------------------------
14425
14426 procedure Save_References_In_Descendants (N : Node_Id) is
14427 use Atree.Unchecked_Access;
14428 -- This code section is part of implementing an untyped tree
14429 -- traversal, so it needs direct access to node fields.
14430
14431 begin
14432 Save_Global_Descendant (Field1 (N));
14433 Save_Global_Descendant (Field2 (N));
14434 Save_Global_Descendant (Field3 (N));
14435 Save_Global_Descendant (Field4 (N));
14436 Save_Global_Descendant (Field5 (N));
14437 end Save_References_In_Descendants;
14438
14439 -----------------------------------
14440 -- Save_References_In_Identifier --
14441 -----------------------------------
14442
14443 procedure Save_References_In_Identifier (N : Node_Id) is
14444 begin
14445 -- The node did not undergo a transformation
14446
14447 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14448
14449 -- If this is a discriminant reference, always save it. It is
14450 -- used in the instance to find the corresponding discriminant
14451 -- positionally rather than by name.
14452
14453 Set_Original_Discriminant
14454 (N, Original_Discriminant (Get_Associated_Node (N)));
14455 Reset_Entity (N);
14456
14457 -- The analysis of the generic copy transformed the identifier
14458 -- into another construct. Propagate the changes to the template.
14459
14460 else
14461 N2 := Get_Associated_Node (N);
14462
14463 -- The identifier denotes a call to a parameterless function.
14464 -- Mark the node as resolved when the function is external.
14465
14466 if Nkind (N2) = N_Function_Call then
14467 E := Entity (Name (N2));
14468
14469 if Present (E) and then Is_Global (E) then
14470 Set_Etype (N, Etype (N2));
14471 else
14472 Set_Associated_Node (N, Empty);
14473 Set_Etype (N, Empty);
14474 end if;
14475
14476 -- The identifier denotes a named number that was constant
14477 -- folded. Preserve the original name for ASIS and undo the
14478 -- constant folding which will be repeated in the instance.
14479
14480 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
14481 and then Is_Entity_Name (Original_Node (N2))
14482 then
14483 Set_Associated_Node (N, Original_Node (N2));
14484 Reset_Entity (N);
14485
14486 -- The identifier resolved to a string literal. Propagate this
14487 -- information to the generic template.
14488
14489 elsif Nkind (N2) = N_String_Literal then
14490 Rewrite (N, New_Copy (N2));
14491
14492 -- The identifier is rewritten as a dereference if it is the
14493 -- prefix of an implicit dereference. Preserve the original
14494 -- tree as the analysis of the instance will expand the node
14495 -- again, but preserve the resolved entity if it is global.
14496
14497 elsif Nkind (N2) = N_Explicit_Dereference then
14498 if Is_Entity_Name (Prefix (N2))
14499 and then Present (Entity (Prefix (N2)))
14500 and then Is_Global (Entity (Prefix (N2)))
14501 then
14502 Set_Associated_Node (N, Prefix (N2));
14503
14504 elsif Nkind (Prefix (N2)) = N_Function_Call
14505 and then Present (Entity (Name (Prefix (N2))))
14506 and then Is_Global (Entity (Name (Prefix (N2))))
14507 then
14508 Rewrite (N,
14509 Make_Explicit_Dereference (Loc,
14510 Prefix =>
14511 Make_Function_Call (Loc,
14512 Name =>
14513 New_Occurrence_Of
14514 (Entity (Name (Prefix (N2))), Loc))));
14515
14516 else
14517 Set_Associated_Node (N, Empty);
14518 Set_Etype (N, Empty);
14519 end if;
14520
14521 -- The subtype mark of a nominally unconstrained object is
14522 -- rewritten as a subtype indication using the bounds of the
14523 -- expression. Recover the original subtype mark.
14524
14525 elsif Nkind (N2) = N_Subtype_Indication
14526 and then Is_Entity_Name (Original_Node (N2))
14527 then
14528 Set_Associated_Node (N, Original_Node (N2));
14529 Reset_Entity (N);
14530 end if;
14531 end if;
14532 end Save_References_In_Identifier;
14533
14534 ---------------------------------
14535 -- Save_References_In_Operator --
14536 ---------------------------------
14537
14538 procedure Save_References_In_Operator (N : Node_Id) is
14539 begin
14540 -- The node did not undergo a transformation
14541
14542 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14543 if Nkind (N) = N_Op_Concat then
14544 Set_Is_Component_Left_Opnd (N,
14545 Is_Component_Left_Opnd (Get_Associated_Node (N)));
14546
14547 Set_Is_Component_Right_Opnd (N,
14548 Is_Component_Right_Opnd (Get_Associated_Node (N)));
14549 end if;
14550
14551 Reset_Entity (N);
14552
14553 -- The analysis of the generic copy transformed the operator into
14554 -- some other construct. Propagate the changes to the template.
14555
14556 else
14557 N2 := Get_Associated_Node (N);
14558
14559 -- The operator resoved to a function call
14560
14561 if Nkind (N2) = N_Function_Call then
14562 E := Entity (Name (N2));
14563
14564 if Present (E) and then Is_Global (E) then
14565 Set_Etype (N, Etype (N2));
14566 else
14567 Set_Associated_Node (N, Empty);
14568 Set_Etype (N, Empty);
14569 end if;
14570
14571 -- The operator was folded into a literal
14572
14573 elsif Nkind_In (N2, N_Integer_Literal,
14574 N_Real_Literal,
14575 N_String_Literal)
14576 then
14577 if Present (Original_Node (N2))
14578 and then Nkind (Original_Node (N2)) = Nkind (N)
14579 then
14580 -- Operation was constant-folded. Whenever possible,
14581 -- recover semantic information from unfolded node,
14582 -- for ASIS use.
14583
14584 Set_Associated_Node (N, Original_Node (N2));
14585
14586 if Nkind (N) = N_Op_Concat then
14587 Set_Is_Component_Left_Opnd (N,
14588 Is_Component_Left_Opnd (Get_Associated_Node (N)));
14589 Set_Is_Component_Right_Opnd (N,
14590 Is_Component_Right_Opnd (Get_Associated_Node (N)));
14591 end if;
14592
14593 Reset_Entity (N);
14594
14595 -- Propagate the constant folding back to the template
14596
14597 else
14598 Rewrite (N, New_Copy (N2));
14599 Set_Analyzed (N, False);
14600 end if;
14601
14602 -- The operator was folded into an enumeration literal. Retain
14603 -- the entity to avoid spurious ambiguities if it is overloaded
14604 -- at the point of instantiation or inlining.
14605
14606 elsif Nkind (N2) = N_Identifier
14607 and then Ekind (Entity (N2)) = E_Enumeration_Literal
14608 then
14609 Rewrite (N, New_Copy (N2));
14610 Set_Analyzed (N, False);
14611 end if;
14612 end if;
14613
14614 -- Complete the operands check if node has not been constant
14615 -- folded.
14616
14617 if Nkind (N) in N_Op then
14618 Save_Entity_Descendants (N);
14619 end if;
14620 end Save_References_In_Operator;
14621
14622 -------------------------------
14623 -- Save_References_In_Pragma --
14624 -------------------------------
14625
14626 procedure Save_References_In_Pragma (Prag : Node_Id) is
14627 Context : Node_Id;
14628 Do_Save : Boolean := True;
14629
14630 use Atree.Unchecked_Access;
14631 -- This code section is part of implementing an untyped tree
14632 -- traversal, so it needs direct access to node fields.
14633
14634 begin
14635 -- Do not save global references in pragmas generated from aspects
14636 -- because the pragmas will be regenerated at instantiation time.
14637
14638 if From_Aspect_Specification (Prag) then
14639 Do_Save := False;
14640
14641 -- The capture of global references within contract-related source
14642 -- pragmas associated with generic packages, subprograms or their
14643 -- respective bodies must be delayed due to timing of annotation
14644 -- analysis. Global references are still captured in routine
14645 -- Save_Global_References_In_Contract.
14646
14647 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
14648 if Is_Package_Contract_Annotation (Prag) then
14649 Context := Find_Related_Package_Or_Body (Prag);
14650
14651 else
14652 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
14653 Context := Find_Related_Subprogram_Or_Body (Prag);
14654 end if;
14655
14656 -- The use of Original_Node accounts for the case when the
14657 -- related context is generic template.
14658
14659 if Requires_Delayed_Save (Original_Node (Context)) then
14660 Do_Save := False;
14661 end if;
14662 end if;
14663
14664 -- For all other cases, save all global references within the
14665 -- descendants, but skip the following semantic fields:
14666
14667 -- Field1 - Next_Pragma
14668 -- Field3 - Corresponding_Aspect
14669 -- Field5 - Next_Rep_Item
14670
14671 if Do_Save then
14672 Save_Global_Descendant (Field2 (Prag));
14673 Save_Global_Descendant (Field4 (Prag));
14674 end if;
14675 end Save_References_In_Pragma;
14676
14677 -- Start of processing for Save_References
14678
14679 begin
14680 if N = Empty then
14681 null;
14682
14683 -- Aggregates
14684
14685 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
14686 Save_References_In_Aggregate (N);
14687
14688 -- Character literals, operator symbols
14689
14690 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
14691 Save_References_In_Char_Lit_Or_Op_Symbol (N);
14692
14693 -- Defining identifiers
14694
14695 elsif Nkind (N) in N_Entity then
14696 null;
14697
14698 -- Identifiers
14699
14700 elsif Nkind (N) = N_Identifier then
14701 Save_References_In_Identifier (N);
14702
14703 -- Operators
14704
14705 elsif Nkind (N) in N_Op then
14706 Save_References_In_Operator (N);
14707
14708 -- Pragmas
14709
14710 elsif Nkind (N) = N_Pragma then
14711 Save_References_In_Pragma (N);
14712
14713 else
14714 Save_References_In_Descendants (N);
14715 end if;
14716
14717 -- Save all global references found within the aspect specifications
14718 -- of the related node.
14719
14720 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
14721
14722 -- The capture of global references within aspects associated with
14723 -- generic packages, subprograms or their bodies must be delayed
14724 -- due to timing of annotation analysis. Global references are
14725 -- still captured in routine Save_Global_References_In_Contract.
14726
14727 if Requires_Delayed_Save (N) then
14728 null;
14729
14730 -- Otherwise save all global references within the aspects
14731
14732 else
14733 Save_Global_References_In_Aspects (N);
14734 end if;
14735 end if;
14736 end Save_References;
14737
14738 -- Start of processing for Save_Global_References
14739
14740 begin
14741 Gen_Scope := Current_Scope;
14742
14743 -- If the generic unit is a child unit, references to entities in the
14744 -- parent are treated as local, because they will be resolved anew in
14745 -- the context of the instance of the parent.
14746
14747 while Is_Child_Unit (Gen_Scope)
14748 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
14749 loop
14750 Gen_Scope := Scope (Gen_Scope);
14751 end loop;
14752
14753 Save_References (Templ);
14754 end Save_Global_References;
14755
14756 ---------------------------------------
14757 -- Save_Global_References_In_Aspects --
14758 ---------------------------------------
14759
14760 procedure Save_Global_References_In_Aspects (N : Node_Id) is
14761 Asp : Node_Id;
14762 Expr : Node_Id;
14763
14764 begin
14765 Asp := First (Aspect_Specifications (N));
14766 while Present (Asp) loop
14767 Expr := Expression (Asp);
14768
14769 if Present (Expr) then
14770 Save_Global_References (Expr);
14771 end if;
14772
14773 Next (Asp);
14774 end loop;
14775 end Save_Global_References_In_Aspects;
14776
14777 ----------------------------------------
14778 -- Save_Global_References_In_Contract --
14779 ----------------------------------------
14780
14781 procedure Save_Global_References_In_Contract
14782 (Templ : Node_Id;
14783 Gen_Id : Entity_Id)
14784 is
14785 procedure Save_Global_References_In_List (First_Prag : Node_Id);
14786 -- Save all global references in contract-related source pragmas found
14787 -- in the list starting with pragma First_Prag.
14788
14789 ------------------------------------
14790 -- Save_Global_References_In_List --
14791 ------------------------------------
14792
14793 procedure Save_Global_References_In_List (First_Prag : Node_Id) is
14794 Prag : Node_Id;
14795
14796 begin
14797 Prag := First_Prag;
14798 while Present (Prag) loop
14799 if Is_Generic_Contract_Pragma (Prag) then
14800 Save_Global_References (Prag);
14801 end if;
14802
14803 Prag := Next_Pragma (Prag);
14804 end loop;
14805 end Save_Global_References_In_List;
14806
14807 -- Local variables
14808
14809 Items : constant Node_Id := Contract (Defining_Entity (Templ));
14810
14811 -- Start of processing for Save_Global_References_In_Contract
14812
14813 begin
14814 -- The entity of the analyzed generic copy must be on the scope stack
14815 -- to ensure proper detection of global references.
14816
14817 Push_Scope (Gen_Id);
14818
14819 if Permits_Aspect_Specifications (Templ)
14820 and then Has_Aspects (Templ)
14821 then
14822 Save_Global_References_In_Aspects (Templ);
14823 end if;
14824
14825 if Present (Items) then
14826 Save_Global_References_In_List (Pre_Post_Conditions (Items));
14827 Save_Global_References_In_List (Contract_Test_Cases (Items));
14828 Save_Global_References_In_List (Classifications (Items));
14829 end if;
14830
14831 Pop_Scope;
14832 end Save_Global_References_In_Contract;
14833
14834 --------------------------------------
14835 -- Set_Copied_Sloc_For_Inlined_Body --
14836 --------------------------------------
14837
14838 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
14839 begin
14840 Create_Instantiation_Source (N, E, True, S_Adjustment);
14841 end Set_Copied_Sloc_For_Inlined_Body;
14842
14843 ---------------------
14844 -- Set_Instance_Of --
14845 ---------------------
14846
14847 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
14848 begin
14849 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
14850 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
14851 Generic_Renamings.Increment_Last;
14852 end Set_Instance_Of;
14853
14854 --------------------
14855 -- Set_Next_Assoc --
14856 --------------------
14857
14858 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
14859 begin
14860 Generic_Renamings.Table (E).Next_In_HTable := Next;
14861 end Set_Next_Assoc;
14862
14863 -------------------
14864 -- Start_Generic --
14865 -------------------
14866
14867 procedure Start_Generic is
14868 begin
14869 -- ??? More things could be factored out in this routine.
14870 -- Should probably be done at a later stage.
14871
14872 Generic_Flags.Append (Inside_A_Generic);
14873 Inside_A_Generic := True;
14874
14875 Expander_Mode_Save_And_Set (False);
14876 end Start_Generic;
14877
14878 ----------------------
14879 -- Set_Instance_Env --
14880 ----------------------
14881
14882 procedure Set_Instance_Env
14883 (Gen_Unit : Entity_Id;
14884 Act_Unit : Entity_Id)
14885 is
14886 Assertion_Status : constant Boolean := Assertions_Enabled;
14887 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
14888 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
14889
14890 begin
14891 -- Regardless of the current mode, predefined units are analyzed in the
14892 -- most current Ada mode, and earlier version Ada checks do not apply
14893 -- to predefined units. Nothing needs to be done for non-internal units.
14894 -- These are always analyzed in the current mode.
14895
14896 if Is_Internal_File_Name
14897 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
14898 Renamings_Included => True)
14899 then
14900 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
14901
14902 -- In Ada2012 we may want to enable assertions in an instance of a
14903 -- predefined unit, in which case we need to preserve the current
14904 -- setting for the Assertions_Enabled flag. This will become more
14905 -- critical when pre/postconditions are added to predefined units,
14906 -- as is already the case for some numeric libraries.
14907
14908 if Ada_Version >= Ada_2012 then
14909 Assertions_Enabled := Assertion_Status;
14910 end if;
14911
14912 -- SPARK_Mode for an instance is the one applicable at the point of
14913 -- instantiation.
14914
14915 SPARK_Mode := Save_SPARK_Mode;
14916 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
14917
14918 -- Make sure dynamic elaboration checks are off in SPARK Mode
14919
14920 if SPARK_Mode = On then
14921 Dynamic_Elaboration_Checks := False;
14922 end if;
14923 end if;
14924
14925 Current_Instantiated_Parent :=
14926 (Gen_Id => Gen_Unit,
14927 Act_Id => Act_Unit,
14928 Next_In_HTable => Assoc_Null);
14929 end Set_Instance_Env;
14930
14931 -----------------
14932 -- Switch_View --
14933 -----------------
14934
14935 procedure Switch_View (T : Entity_Id) is
14936 BT : constant Entity_Id := Base_Type (T);
14937 Priv_Elmt : Elmt_Id := No_Elmt;
14938 Priv_Sub : Entity_Id;
14939
14940 begin
14941 -- T may be private but its base type may have been exchanged through
14942 -- some other occurrence, in which case there is nothing to switch
14943 -- besides T itself. Note that a private dependent subtype of a private
14944 -- type might not have been switched even if the base type has been,
14945 -- because of the last branch of Check_Private_View (see comment there).
14946
14947 if not Is_Private_Type (BT) then
14948 Prepend_Elmt (Full_View (T), Exchanged_Views);
14949 Exchange_Declarations (T);
14950 return;
14951 end if;
14952
14953 Priv_Elmt := First_Elmt (Private_Dependents (BT));
14954
14955 if Present (Full_View (BT)) then
14956 Prepend_Elmt (Full_View (BT), Exchanged_Views);
14957 Exchange_Declarations (BT);
14958 end if;
14959
14960 while Present (Priv_Elmt) loop
14961 Priv_Sub := (Node (Priv_Elmt));
14962
14963 -- We avoid flipping the subtype if the Etype of its full view is
14964 -- private because this would result in a malformed subtype. This
14965 -- occurs when the Etype of the subtype full view is the full view of
14966 -- the base type (and since the base types were just switched, the
14967 -- subtype is pointing to the wrong view). This is currently the case
14968 -- for tagged record types, access types (maybe more?) and needs to
14969 -- be resolved. ???
14970
14971 if Present (Full_View (Priv_Sub))
14972 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
14973 then
14974 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
14975 Exchange_Declarations (Priv_Sub);
14976 end if;
14977
14978 Next_Elmt (Priv_Elmt);
14979 end loop;
14980 end Switch_View;
14981
14982 -----------------
14983 -- True_Parent --
14984 -----------------
14985
14986 function True_Parent (N : Node_Id) return Node_Id is
14987 begin
14988 if Nkind (Parent (N)) = N_Subunit then
14989 return Parent (Corresponding_Stub (Parent (N)));
14990 else
14991 return Parent (N);
14992 end if;
14993 end True_Parent;
14994
14995 -----------------------------
14996 -- Valid_Default_Attribute --
14997 -----------------------------
14998
14999 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
15000 Attr_Id : constant Attribute_Id :=
15001 Get_Attribute_Id (Attribute_Name (Def));
15002 T : constant Entity_Id := Entity (Prefix (Def));
15003 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
15004 F : Entity_Id;
15005 Num_F : Int;
15006 OK : Boolean;
15007
15008 begin
15009 if No (T) or else T = Any_Id then
15010 return;
15011 end if;
15012
15013 Num_F := 0;
15014 F := First_Formal (Nam);
15015 while Present (F) loop
15016 Num_F := Num_F + 1;
15017 Next_Formal (F);
15018 end loop;
15019
15020 case Attr_Id is
15021 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
15022 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
15023 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
15024 Attribute_Unbiased_Rounding =>
15025 OK := Is_Fun
15026 and then Num_F = 1
15027 and then Is_Floating_Point_Type (T);
15028
15029 when Attribute_Image | Attribute_Pred | Attribute_Succ |
15030 Attribute_Value | Attribute_Wide_Image |
15031 Attribute_Wide_Value =>
15032 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
15033
15034 when Attribute_Max | Attribute_Min =>
15035 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
15036
15037 when Attribute_Input =>
15038 OK := (Is_Fun and then Num_F = 1);
15039
15040 when Attribute_Output | Attribute_Read | Attribute_Write =>
15041 OK := (not Is_Fun and then Num_F = 2);
15042
15043 when others =>
15044 OK := False;
15045 end case;
15046
15047 if not OK then
15048 Error_Msg_N
15049 ("attribute reference has wrong profile for subprogram", Def);
15050 end if;
15051 end Valid_Default_Attribute;
15052
15053 end Sem_Ch12;